1
- import { hashToLocation } from './util' ;
1
+ import { hashToLocation as defaultHashToLocation } from './util' ;
2
2
import type { CasApi , CasGetOptions } from '../cas/types' ;
3
3
import type { CrudApi , CrudResourceInfo } from '../crud/types' ;
4
+ import type { FsLocation } from '../fsa-to-node/types' ;
4
5
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 ;
7
9
}
8
10
9
11
const normalizeErrors = async < T > ( code : ( ) => Promise < T > ) : Promise < T > => {
@@ -21,21 +23,22 @@ const normalizeErrors = async <T>(code: () => Promise<T>): Promise<T> => {
21
23
}
22
24
} ;
23
25
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 ;
29
28
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 > => {
31
34
const digest = await this . options . hash ( blob ) ;
32
- const [ collection , resource ] = hashToLocation ( digest ) ;
35
+ const [ collection , resource ] = this . hash2Loc ( digest ) ;
33
36
await this . crud . put ( collection , resource , blob ) ;
34
37
return digest ;
35
38
} ;
36
39
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 ) ;
39
42
return await normalizeErrors ( async ( ) => {
40
43
const blob = await this . crud . get ( collection , resource ) ;
41
44
if ( ! options ?. skipVerification ) {
@@ -46,15 +49,15 @@ export class CrudCas implements CasApi {
46
49
} ) ;
47
50
} ;
48
51
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 ) ;
51
54
await normalizeErrors ( async ( ) => {
52
55
return await this . crud . del ( collection , resource , silent ) ;
53
56
} ) ;
54
57
} ;
55
58
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 ) ;
58
61
return await normalizeErrors ( async ( ) => {
59
62
return await this . crud . info ( collection , resource ) ;
60
63
} ) ;
0 commit comments