fix: Be consistent in slash usage in storages

This commit is contained in:
Joachim Van Herwegen 2023-10-10 14:59:02 +02:00
parent b5a61cbb08
commit f954fc9450
4 changed files with 11 additions and 10 deletions

View File

@ -1,16 +1,17 @@
import { ensureTrailingSlash, joinUrl } from '../../util/PathUtil';
import { ensureTrailingSlash, joinUrl, trimLeadingSlashes } from '../../util/PathUtil';
import type { KeyValueStorage } from './KeyValueStorage';
import { PassthroughKeyValueStorage } from './PassthroughKeyValueStorage';
/**
* A {@link KeyValueStorage} that prepends a relative path to the key.
* Leading slashes of the relative path are trimmed, and a trailing slash is added if needed.
*/
export class ContainerPathStorage<T> extends PassthroughKeyValueStorage<T> {
protected readonly basePath: string;
public constructor(source: KeyValueStorage<string, T>, relativePath: string) {
super(source);
this.basePath = ensureTrailingSlash(relativePath);
this.basePath = trimLeadingSlashes(ensureTrailingSlash(relativePath));
}
public async* entries(): AsyncIterableIterator<[string, T]> {

View File

@ -125,9 +125,9 @@ export class JsonResourceStorage<T> implements KeyValueStorage<string, T> {
*/
protected identifierToKey(identifier: ResourceIdentifier): string {
// Due to the usage of `joinUrl` we don't know for sure if there was a preceding slash,
// so we always add one for consistency.
// In practice this would only be an issue if a class depends
// on the `entries` results matching a key that was sent before.
return ensureLeadingSlash(identifier.path.slice(this.container.length));
// so we always remove leading slashes one for consistency.
// In practice this only has an impact on the `entries` call
// and only if class calling this depends on a leading slash still being there.
return trimLeadingSlashes(identifier.path.slice(this.container.length));
}
}

View File

@ -2,7 +2,7 @@ import { ContainerPathStorage } from '../../../../src/storage/keyvalue/Container
import type { KeyValueStorage } from '../../../../src/storage/keyvalue/KeyValueStorage';
describe('An ContainerPathStorage', (): void => {
const relativePath = '/container/';
const relativePath = 'container/';
let map: Map<string, string>;
let source: KeyValueStorage<string, unknown>;
let storage: ContainerPathStorage<unknown>;

View File

@ -12,9 +12,9 @@ import { LDP } from '../../../../src/util/Vocabularies';
describe('A JsonResourceStorage', (): void => {
const baseUrl = 'http://test.com/';
const container = '/data/';
const path1 = '/foo';
const path2 = '/bar';
const subPath = '/container/document';
const path1 = 'foo';
const path2 = 'bar';
const subPath = 'container/document';
const containerIdentifier = 'http://test.com/data/';
const subContainerIdentifier = 'http://test.com/data/container/';
let data: Map<string, string>;