diff --git a/src/cas/types.ts b/src/cas/types.ts index 6b267103..9ec8d786 100644 --- a/src/cas/types.ts +++ b/src/cas/types.ts @@ -1,10 +1,10 @@ import type { CrudResourceInfo } from '../crud/types'; -export interface CasApi { - put(blob: Uint8Array): Promise; - get(hash: string, options?: CasGetOptions): Promise; - del(hash: string, silent?: boolean): Promise; - info(hash: string): Promise; +export interface CasApi { + put(blob: Uint8Array): Promise; + get(hash: Hash, options?: CasGetOptions): Promise; + del(hash: Hash, silent?: boolean): Promise; + info(hash: Hash): Promise; } export interface CasGetOptions { diff --git a/src/crud-to-cas/CrudCas.ts b/src/crud-to-cas/CrudCas.ts index a9fc8e01..75ea6526 100644 --- a/src/crud-to-cas/CrudCas.ts +++ b/src/crud-to-cas/CrudCas.ts @@ -1,9 +1,11 @@ -import { hashToLocation } from './util'; +import { hashToLocation as defaultHashToLocation } from './util'; import type { CasApi, CasGetOptions } from '../cas/types'; import type { CrudApi, CrudResourceInfo } from '../crud/types'; +import type { FsLocation } from '../fsa-to-node/types'; -export interface CrudCasOptions { - hash: (blob: Uint8Array) => Promise; +export interface CrudCasOptions { + hash: (blob: Uint8Array) => Promise; + hash2Loc?: (hash: Hash) => FsLocation; } const normalizeErrors = async (code: () => Promise): Promise => { @@ -21,21 +23,22 @@ const normalizeErrors = async (code: () => Promise): Promise => { } }; -export class CrudCas implements CasApi { - constructor( - protected readonly crud: CrudApi, - protected readonly options: CrudCasOptions, - ) {} +export class CrudCas implements CasApi { + protected readonly hash2Loc: (hash: Hash) => FsLocation; - public readonly put = async (blob: Uint8Array): Promise => { + constructor(protected readonly crud: CrudApi, protected readonly options: CrudCasOptions) { + this.hash2Loc = options.hash2Loc || <(hash: Hash) => FsLocation>defaultHashToLocation; + } + + public readonly put = async (blob: Uint8Array): Promise => { const digest = await this.options.hash(blob); - const [collection, resource] = hashToLocation(digest); + const [collection, resource] = this.hash2Loc(digest); await this.crud.put(collection, resource, blob); return digest; }; - public readonly get = async (hash: string, options?: CasGetOptions): Promise => { - const [collection, resource] = hashToLocation(hash); + public readonly get = async (hash: Hash, options?: CasGetOptions): Promise => { + const [collection, resource] = this.hash2Loc(hash); return await normalizeErrors(async () => { const blob = await this.crud.get(collection, resource); if (!options?.skipVerification) { @@ -46,15 +49,15 @@ export class CrudCas implements CasApi { }); }; - public readonly del = async (hash: string, silent?: boolean): Promise => { - const [collection, resource] = hashToLocation(hash); + public readonly del = async (hash: Hash, silent?: boolean): Promise => { + const [collection, resource] = this.hash2Loc(hash); await normalizeErrors(async () => { return await this.crud.del(collection, resource, silent); }); }; - public readonly info = async (hash: string): Promise => { - const [collection, resource] = hashToLocation(hash); + public readonly info = async (hash: Hash): Promise => { + const [collection, resource] = this.hash2Loc(hash); return await normalizeErrors(async () => { return await this.crud.info(collection, resource); }); diff --git a/src/crud-to-cas/__tests__/testCasfs.ts b/src/crud-to-cas/__tests__/testCasfs.ts index 16c7f5cd..e71f4b4f 100644 --- a/src/crud-to-cas/__tests__/testCasfs.ts +++ b/src/crud-to-cas/__tests__/testCasfs.ts @@ -16,7 +16,7 @@ const b = (str: string) => { }; export type Setup = () => { - cas: CasApi; + cas: CasApi; crud: CrudApi; snapshot: () => Record; };