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 type { KeyValueStorage } from './KeyValueStorage';
import { PassthroughKeyValueStorage } from './PassthroughKeyValueStorage'; import { PassthroughKeyValueStorage } from './PassthroughKeyValueStorage';
/** /**
* A {@link KeyValueStorage} that prepends a relative path to the key. * 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> { export class ContainerPathStorage<T> extends PassthroughKeyValueStorage<T> {
protected readonly basePath: string; protected readonly basePath: string;
public constructor(source: KeyValueStorage<string, T>, relativePath: string) { public constructor(source: KeyValueStorage<string, T>, relativePath: string) {
super(source); super(source);
this.basePath = ensureTrailingSlash(relativePath); this.basePath = trimLeadingSlashes(ensureTrailingSlash(relativePath));
} }
public async* entries(): AsyncIterableIterator<[string, T]> { 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 { protected identifierToKey(identifier: ResourceIdentifier): string {
// Due to the usage of `joinUrl` we don't know for sure if there was a preceding slash, // 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. // so we always remove leading slashes one for consistency.
// In practice this would only be an issue if a class depends // In practice this only has an impact on the `entries` call
// on the `entries` results matching a key that was sent before. // and only if class calling this depends on a leading slash still being there.
return ensureLeadingSlash(identifier.path.slice(this.container.length)); 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'; import type { KeyValueStorage } from '../../../../src/storage/keyvalue/KeyValueStorage';
describe('An ContainerPathStorage', (): void => { describe('An ContainerPathStorage', (): void => {
const relativePath = '/container/'; const relativePath = 'container/';
let map: Map<string, string>; let map: Map<string, string>;
let source: KeyValueStorage<string, unknown>; let source: KeyValueStorage<string, unknown>;
let storage: ContainerPathStorage<unknown>; let storage: ContainerPathStorage<unknown>;

View File

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