@@ -7,17 +7,32 @@ import type { CID } from 'multiformats/cid'
77// https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2
88const IDENTITY_CODEC = 0x00
99
10+ class IdentityDigestTooLongError extends Error {
11+ static name = 'IdentityDigestTooLongError'
12+ name = 'IdentityDigestTooLongError'
13+ }
14+
15+ export interface IdentityBlockstoreInit {
16+ maxDigestLength ?: number
17+ }
18+
1019export class IdentityBlockstore extends BaseBlockstore {
1120 private readonly child ?: Blockstore
21+ private readonly maxDigestLength ?: number
1222
13- constructor ( child ?: Blockstore ) {
23+ constructor ( child ?: Blockstore , init ?: IdentityBlockstoreInit ) {
1424 super ( )
1525
1626 this . child = child
27+ this . maxDigestLength = init ?. maxDigestLength
1728 }
1829
1930 put ( key : CID , block : Uint8Array | AwaitIterable < Uint8Array > , options ?: AbortOptions ) : Await < CID > {
2031 if ( key . multihash . code === IDENTITY_CODEC ) {
32+ if ( this . maxDigestLength != null && key . multihash . digest . byteLength > this . maxDigestLength ) {
33+ throw new IdentityDigestTooLongError ( `Identity digest too long - ${ key . multihash . digest . byteLength } > this.maxDigestLength` )
34+ }
35+
2136 options ?. signal ?. throwIfAborted ( )
2237 return key
2338 }
@@ -32,6 +47,10 @@ export class IdentityBlockstore extends BaseBlockstore {
3247
3348 * get ( key : CID , options ?: AbortOptions ) : AwaitGenerator < Uint8Array > {
3449 if ( key . multihash . code === IDENTITY_CODEC ) {
50+ if ( this . maxDigestLength != null && key . multihash . digest . byteLength > this . maxDigestLength ) {
51+ throw new IdentityDigestTooLongError ( `Identity digest too long - ${ key . multihash . digest . byteLength } > this.maxDigestLength` )
52+ }
53+
3554 options ?. signal ?. throwIfAborted ( )
3655 yield key . multihash . digest
3756 return
@@ -47,6 +66,10 @@ export class IdentityBlockstore extends BaseBlockstore {
4766
4867 has ( key : CID , options ?: AbortOptions ) : Await < boolean > {
4968 if ( key . multihash . code === IDENTITY_CODEC ) {
69+ if ( this . maxDigestLength != null && key . multihash . digest . byteLength > this . maxDigestLength ) {
70+ throw new IdentityDigestTooLongError ( `Identity digest too long - ${ key . multihash . digest . byteLength } > this.maxDigestLength` )
71+ }
72+
5073 options ?. signal ?. throwIfAborted ( )
5174 return true
5275 }
@@ -61,6 +84,10 @@ export class IdentityBlockstore extends BaseBlockstore {
6184
6285 delete ( key : CID , options ?: AbortOptions ) : Await < void > {
6386 if ( key . code === IDENTITY_CODEC ) {
87+ if ( this . maxDigestLength != null && key . multihash . digest . byteLength > this . maxDigestLength ) {
88+ throw new IdentityDigestTooLongError ( `Identity digest too long - ${ key . multihash . digest . byteLength } > this.maxDigestLength` )
89+ }
90+
6491 options ?. signal ?. throwIfAborted ( )
6592 return
6693 }
0 commit comments