From f954fc94509266c7a7e47ba27ee768888bd1034f Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Tue, 10 Oct 2023 14:59:02 +0200 Subject: [PATCH] fix: Be consistent in slash usage in storages --- src/storage/keyvalue/ContainerPathStorage.ts | 5 +++-- src/storage/keyvalue/JsonResourceStorage.ts | 8 ++++---- test/unit/storage/keyvalue/ContainerPathStorage.test.ts | 2 +- test/unit/storage/keyvalue/JsonResourceStorage.test.ts | 6 +++--- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/storage/keyvalue/ContainerPathStorage.ts b/src/storage/keyvalue/ContainerPathStorage.ts index bf0acd01d..4859ca1ec 100644 --- a/src/storage/keyvalue/ContainerPathStorage.ts +++ b/src/storage/keyvalue/ContainerPathStorage.ts @@ -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 extends PassthroughKeyValueStorage { protected readonly basePath: string; public constructor(source: KeyValueStorage, relativePath: string) { super(source); - this.basePath = ensureTrailingSlash(relativePath); + this.basePath = trimLeadingSlashes(ensureTrailingSlash(relativePath)); } public async* entries(): AsyncIterableIterator<[string, T]> { diff --git a/src/storage/keyvalue/JsonResourceStorage.ts b/src/storage/keyvalue/JsonResourceStorage.ts index 8682bfdee..9af1e523f 100644 --- a/src/storage/keyvalue/JsonResourceStorage.ts +++ b/src/storage/keyvalue/JsonResourceStorage.ts @@ -125,9 +125,9 @@ export class JsonResourceStorage implements KeyValueStorage { */ 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)); } } diff --git a/test/unit/storage/keyvalue/ContainerPathStorage.test.ts b/test/unit/storage/keyvalue/ContainerPathStorage.test.ts index 396680bc0..5e745972a 100644 --- a/test/unit/storage/keyvalue/ContainerPathStorage.test.ts +++ b/test/unit/storage/keyvalue/ContainerPathStorage.test.ts @@ -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; let source: KeyValueStorage; let storage: ContainerPathStorage; diff --git a/test/unit/storage/keyvalue/JsonResourceStorage.test.ts b/test/unit/storage/keyvalue/JsonResourceStorage.test.ts index daa051cbb..15ce4e915 100644 --- a/test/unit/storage/keyvalue/JsonResourceStorage.test.ts +++ b/test/unit/storage/keyvalue/JsonResourceStorage.test.ts @@ -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;