Skip to content

Commit

Permalink
feat: 🎸 allow to customize CAS storage hash and location mappin
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 19, 2024
1 parent 44ceed4 commit e32a57d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/cas/types.ts
@@ -1,10 +1,10 @@
import type { CrudResourceInfo } from '../crud/types';

export interface CasApi {
put(blob: Uint8Array): Promise<string>;
get(hash: string, options?: CasGetOptions): Promise<Uint8Array>;
del(hash: string, silent?: boolean): Promise<void>;
info(hash: string): Promise<CrudResourceInfo>;
export interface CasApi<Hash> {
put(blob: Uint8Array): Promise<Hash>;
get(hash: Hash, options?: CasGetOptions): Promise<Uint8Array>;
del(hash: Hash, silent?: boolean): Promise<void>;
info(hash: Hash): Promise<CrudResourceInfo>;
}

export interface CasGetOptions {
Expand Down
35 changes: 19 additions & 16 deletions 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<string>;
export interface CrudCasOptions<Hash = string> {
hash: (blob: Uint8Array) => Promise<Hash>;
hash2Loc?: (hash: Hash) => FsLocation;
}

const normalizeErrors = async <T>(code: () => Promise<T>): Promise<T> => {
Expand All @@ -21,21 +23,22 @@ const normalizeErrors = async <T>(code: () => Promise<T>): Promise<T> => {
}
};

export class CrudCas implements CasApi {
constructor(
protected readonly crud: CrudApi,
protected readonly options: CrudCasOptions,
) {}
export class CrudCas<Hash = string> implements CasApi<Hash> {
protected readonly hash2Loc: (hash: Hash) => FsLocation;

public readonly put = async (blob: Uint8Array): Promise<string> => {
constructor(protected readonly crud: CrudApi, protected readonly options: CrudCasOptions<Hash>) {
this.hash2Loc = options.hash2Loc || <(hash: Hash) => FsLocation>defaultHashToLocation;
}

public readonly put = async (blob: Uint8Array): Promise<Hash> => {
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<Uint8Array> => {
const [collection, resource] = hashToLocation(hash);
public readonly get = async (hash: Hash, options?: CasGetOptions): Promise<Uint8Array> => {
const [collection, resource] = this.hash2Loc(hash);
return await normalizeErrors(async () => {
const blob = await this.crud.get(collection, resource);
if (!options?.skipVerification) {
Expand All @@ -46,15 +49,15 @@ export class CrudCas implements CasApi {
});
};

public readonly del = async (hash: string, silent?: boolean): Promise<void> => {
const [collection, resource] = hashToLocation(hash);
public readonly del = async (hash: Hash, silent?: boolean): Promise<void> => {
const [collection, resource] = this.hash2Loc(hash);
await normalizeErrors(async () => {
return await this.crud.del(collection, resource, silent);
});
};

public readonly info = async (hash: string): Promise<CrudResourceInfo> => {
const [collection, resource] = hashToLocation(hash);
public readonly info = async (hash: Hash): Promise<CrudResourceInfo> => {
const [collection, resource] = this.hash2Loc(hash);
return await normalizeErrors(async () => {
return await this.crud.info(collection, resource);
});
Expand Down
2 changes: 1 addition & 1 deletion src/crud-to-cas/__tests__/testCasfs.ts
Expand Up @@ -16,7 +16,7 @@ const b = (str: string) => {
};

export type Setup = () => {
cas: CasApi;
cas: CasApi<string>;
crud: CrudApi;
snapshot: () => Record<string, string | null>;
};
Expand Down

0 comments on commit e32a57d

Please sign in to comment.