Skip to content

Commit e32a57d

Browse files
committed
feat: 🎸 allow to customize CAS storage hash and location mappin
1 parent 44ceed4 commit e32a57d

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

‎src/cas/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { CrudResourceInfo } from '../crud/types';
22

3-
export interface CasApi {
4-
put(blob: Uint8Array): Promise<string>;
5-
get(hash: string, options?: CasGetOptions): Promise<Uint8Array>;
6-
del(hash: string, silent?: boolean): Promise<void>;
7-
info(hash: string): Promise<CrudResourceInfo>;
3+
export interface CasApi<Hash> {
4+
put(blob: Uint8Array): Promise<Hash>;
5+
get(hash: Hash, options?: CasGetOptions): Promise<Uint8Array>;
6+
del(hash: Hash, silent?: boolean): Promise<void>;
7+
info(hash: Hash): Promise<CrudResourceInfo>;
88
}
99

1010
export interface CasGetOptions {

‎src/crud-to-cas/CrudCas.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { hashToLocation } from './util';
1+
import { hashToLocation as defaultHashToLocation } from './util';
22
import type { CasApi, CasGetOptions } from '../cas/types';
33
import type { CrudApi, CrudResourceInfo } from '../crud/types';
4+
import type { FsLocation } from '../fsa-to-node/types';
45

5-
export interface CrudCasOptions {
6-
hash: (blob: Uint8Array) => Promise<string>;
6+
export interface CrudCasOptions<Hash = string> {
7+
hash: (blob: Uint8Array) => Promise<Hash>;
8+
hash2Loc?: (hash: Hash) => FsLocation;
79
}
810

911
const normalizeErrors = async <T>(code: () => Promise<T>): Promise<T> => {
@@ -21,21 +23,22 @@ const normalizeErrors = async <T>(code: () => Promise<T>): Promise<T> => {
2123
}
2224
};
2325

24-
export class CrudCas implements CasApi {
25-
constructor(
26-
protected readonly crud: CrudApi,
27-
protected readonly options: CrudCasOptions,
28-
) {}
26+
export class CrudCas<Hash = string> implements CasApi<Hash> {
27+
protected readonly hash2Loc: (hash: Hash) => FsLocation;
2928

30-
public readonly put = async (blob: Uint8Array): Promise<string> => {
29+
constructor(protected readonly crud: CrudApi, protected readonly options: CrudCasOptions<Hash>) {
30+
this.hash2Loc = options.hash2Loc || <(hash: Hash) => FsLocation>defaultHashToLocation;
31+
}
32+
33+
public readonly put = async (blob: Uint8Array): Promise<Hash> => {
3134
const digest = await this.options.hash(blob);
32-
const [collection, resource] = hashToLocation(digest);
35+
const [collection, resource] = this.hash2Loc(digest);
3336
await this.crud.put(collection, resource, blob);
3437
return digest;
3538
};
3639

37-
public readonly get = async (hash: string, options?: CasGetOptions): Promise<Uint8Array> => {
38-
const [collection, resource] = hashToLocation(hash);
40+
public readonly get = async (hash: Hash, options?: CasGetOptions): Promise<Uint8Array> => {
41+
const [collection, resource] = this.hash2Loc(hash);
3942
return await normalizeErrors(async () => {
4043
const blob = await this.crud.get(collection, resource);
4144
if (!options?.skipVerification) {
@@ -46,15 +49,15 @@ export class CrudCas implements CasApi {
4649
});
4750
};
4851

49-
public readonly del = async (hash: string, silent?: boolean): Promise<void> => {
50-
const [collection, resource] = hashToLocation(hash);
52+
public readonly del = async (hash: Hash, silent?: boolean): Promise<void> => {
53+
const [collection, resource] = this.hash2Loc(hash);
5154
await normalizeErrors(async () => {
5255
return await this.crud.del(collection, resource, silent);
5356
});
5457
};
5558

56-
public readonly info = async (hash: string): Promise<CrudResourceInfo> => {
57-
const [collection, resource] = hashToLocation(hash);
59+
public readonly info = async (hash: Hash): Promise<CrudResourceInfo> => {
60+
const [collection, resource] = this.hash2Loc(hash);
5861
return await normalizeErrors(async () => {
5962
return await this.crud.info(collection, resource);
6063
});

‎src/crud-to-cas/__tests__/testCasfs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const b = (str: string) => {
1616
};
1717

1818
export type Setup = () => {
19-
cas: CasApi;
19+
cas: CasApi<string>;
2020
crud: CrudApi;
2121
snapshot: () => Record<string, string | null>;
2222
};

0 commit comments

Comments
 (0)