@helia/utils 0.3.3-ac4bdb8 → 0.3.3-bc64f47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +3 -2
  2. package/dist/index.min.js +1 -1
  3. package/dist/src/abstract-session.d.ts.map +1 -1
  4. package/dist/src/abstract-session.js +3 -3
  5. package/dist/src/abstract-session.js.map +1 -1
  6. package/dist/src/index.d.ts +23 -7
  7. package/dist/src/index.d.ts.map +1 -1
  8. package/dist/src/index.js +12 -11
  9. package/dist/src/index.js.map +1 -1
  10. package/dist/src/pins.d.ts +6 -3
  11. package/dist/src/pins.d.ts.map +1 -1
  12. package/dist/src/pins.js +21 -10
  13. package/dist/src/pins.js.map +1 -1
  14. package/dist/src/routing.d.ts.map +1 -1
  15. package/dist/src/routing.js +7 -6
  16. package/dist/src/routing.js.map +1 -1
  17. package/dist/src/utils/get-codec.d.ts +4 -0
  18. package/dist/src/utils/get-codec.d.ts.map +1 -0
  19. package/dist/src/utils/get-codec.js +38 -0
  20. package/dist/src/utils/get-codec.js.map +1 -0
  21. package/dist/src/utils/get-hasher.d.ts +4 -0
  22. package/dist/src/utils/get-hasher.d.ts.map +1 -0
  23. package/dist/src/utils/get-hasher.js +32 -0
  24. package/dist/src/utils/get-hasher.js.map +1 -0
  25. package/dist/src/utils/is-promise.d.ts +2 -0
  26. package/dist/src/utils/is-promise.d.ts.map +1 -0
  27. package/dist/src/utils/is-promise.js +4 -0
  28. package/dist/src/utils/is-promise.js.map +1 -0
  29. package/dist/src/utils/networked-storage.d.ts +3 -2
  30. package/dist/src/utils/networked-storage.d.ts.map +1 -1
  31. package/dist/src/utils/networked-storage.js +19 -9
  32. package/dist/src/utils/networked-storage.js.map +1 -1
  33. package/package.json +12 -11
  34. package/src/abstract-session.ts +3 -3
  35. package/src/index.ts +35 -16
  36. package/src/pins.ts +27 -13
  37. package/src/routing.ts +7 -6
  38. package/src/utils/get-codec.ts +47 -0
  39. package/src/utils/get-hasher.ts +40 -0
  40. package/src/utils/is-promise.ts +3 -0
  41. package/src/utils/networked-storage.ts +24 -11
  42. package/dist/src/utils/dag-walkers.d.ts +0 -28
  43. package/dist/src/utils/dag-walkers.d.ts.map +0 -1
  44. package/dist/src/utils/dag-walkers.js +0 -171
  45. package/dist/src/utils/dag-walkers.js.map +0 -1
  46. package/dist/src/utils/default-hashers.d.ts +0 -3
  47. package/dist/src/utils/default-hashers.d.ts.map +0 -1
  48. package/dist/src/utils/default-hashers.js +0 -15
  49. package/dist/src/utils/default-hashers.js.map +0 -1
  50. package/src/utils/dag-walkers.ts +0 -198
  51. package/src/utils/default-hashers.ts +0 -18
@@ -1,16 +1,18 @@
1
- import { CodeError, setMaxListeners, start, stop } from '@libp2p/interface'
1
+ import { InvalidMultihashError, InvalidParametersError, setMaxListeners, start, stop } from '@libp2p/interface'
2
2
  import { anySignal } from 'any-signal'
3
3
  import { IdentityBlockstore } from 'blockstore-core/identity'
4
4
  import filter from 'it-filter'
5
5
  import forEach from 'it-foreach'
6
6
  import { CustomProgressEvent, type ProgressOptions } from 'progress-events'
7
7
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
8
+ import { isPromise } from './is-promise.js'
9
+ import type { HasherLoader } from '@helia/interface'
8
10
  import type { BlockBroker, Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, BlockRetrievalOptions, CreateSessionOptions, SessionBlockstore } from '@helia/interface/blocks'
9
11
  import type { AbortOptions, ComponentLogger, Logger, LoggerOptions, Startable } from '@libp2p/interface'
10
12
  import type { Blockstore } from 'interface-blockstore'
11
13
  import type { AwaitIterable } from 'interface-store'
12
14
  import type { CID } from 'multiformats/cid'
13
- import type { MultihashHasher } from 'multiformats/hashes/interface'
15
+ import type { MultihashDigest, MultihashHasher } from 'multiformats/hashes/interface'
14
16
 
15
17
  export interface GetOptions extends AbortOptions {
16
18
  progress?(evt: Event): void
@@ -20,12 +22,12 @@ export interface StorageComponents {
20
22
  blockstore: Blockstore
21
23
  logger: ComponentLogger
22
24
  blockBrokers: BlockBroker[]
23
- hashers: Record<number, MultihashHasher>
25
+ getHasher: HasherLoader
24
26
  }
25
27
 
26
28
  class Storage implements Blockstore {
27
29
  protected readonly child: Blockstore
28
- protected readonly hashers: Record<number, MultihashHasher>
30
+ protected readonly getHasher: HasherLoader
29
31
  protected log: Logger
30
32
  protected readonly logger: ComponentLogger
31
33
  protected readonly components: StorageComponents
@@ -38,7 +40,7 @@ class Storage implements Blockstore {
38
40
  this.logger = components.logger
39
41
  this.components = components
40
42
  this.child = new IdentityBlockstore(components.blockstore)
41
- this.hashers = components.hashers ?? {}
43
+ this.getHasher = components.getHasher
42
44
  }
43
45
 
44
46
  /**
@@ -91,9 +93,11 @@ class Storage implements Blockstore {
91
93
  */
92
94
  async get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): Promise<Uint8Array> {
93
95
  if (options.offline !== true && !(await this.child.has(cid, options))) {
96
+ const hasher = await this.getHasher(cid.multihash.code)
97
+
94
98
  // we do not have the block locally, get it from a block provider
95
99
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:get:providers:get', cid))
96
- const block = await raceBlockRetrievers(cid, this.components.blockBrokers, this.hashers[cid.multihash.code], {
100
+ const block = await raceBlockRetrievers(cid, this.components.blockBrokers, hasher, {
97
101
  ...options,
98
102
  log: this.log
99
103
  })
@@ -122,9 +126,11 @@ class Storage implements Blockstore {
122
126
 
123
127
  yield * this.child.getMany(forEach(cids, async (cid): Promise<void> => {
124
128
  if (options.offline !== true && !(await this.child.has(cid, options))) {
129
+ const hasher = await this.getHasher(cid.multihash.code)
130
+
125
131
  // we do not have the block locally, get it from a block provider
126
132
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:get-many:providers:get', cid))
127
- const block = await raceBlockRetrievers(cid, this.components.blockBrokers, this.hashers[cid.multihash.code], {
133
+ const block = await raceBlockRetrievers(cid, this.components.blockBrokers, hasher, {
128
134
  ...options,
129
135
  log: this.log
130
136
  })
@@ -219,7 +225,7 @@ export class NetworkedStorage extends Storage implements Blocks, Startable {
219
225
  return new SessionStorage({
220
226
  blockstore: this.child,
221
227
  blockBrokers,
222
- hashers: this.hashers,
228
+ getHasher: this.getHasher,
223
229
  logger: this.logger
224
230
  }, {
225
231
  root
@@ -390,16 +396,23 @@ function isRetrievingBlockBroker (broker: BlockBroker): broker is Required<Pick<
390
396
 
391
397
  export const getCidBlockVerifierFunction = (cid: CID, hasher: MultihashHasher): Required<BlockRetrievalOptions>['validateFn'] => {
392
398
  if (hasher == null) {
393
- throw new CodeError(`No hasher configured for multihash code 0x${cid.multihash.code.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`, 'ERR_UNKNOWN_HASH_ALG')
399
+ throw new InvalidParametersError(`No hasher configured for multihash code 0x${cid.multihash.code.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`)
394
400
  }
395
401
 
396
402
  return async (block: Uint8Array): Promise<void> => {
397
403
  // verify block
398
- const hash = await hasher.digest(block)
404
+ let hash: MultihashDigest<number>
405
+ const res = hasher.digest(block)
406
+
407
+ if (isPromise(res)) {
408
+ hash = await res
409
+ } else {
410
+ hash = res
411
+ }
399
412
 
400
413
  if (!uint8ArrayEquals(hash.digest, cid.multihash.digest)) {
401
414
  // if a hash mismatch occurs for a TrustlessGatewayBlockBroker, we should try another gateway
402
- throw new CodeError('Hash of downloaded block did not match multihash from passed CID', 'ERR_HASH_MISMATCH')
415
+ throw new InvalidMultihashError('Hash of downloaded block did not match multihash from passed CID')
403
416
  }
404
417
  }
405
418
  }
@@ -1,28 +0,0 @@
1
- import type { DAGWalker } from '@helia/interface';
2
- /**
3
- * Dag walker for dag-pb CIDs
4
- */
5
- export declare const dagPbWalker: DAGWalker;
6
- /**
7
- * Dag walker for raw CIDs
8
- */
9
- export declare const rawWalker: DAGWalker;
10
- /**
11
- * Dag walker for dag-cbor CIDs. Does not actually use dag-cbor since
12
- * all we are interested in is extracting the the CIDs from the block
13
- * so we can just use cborg for that.
14
- */
15
- export declare const dagCborWalker: DAGWalker;
16
- /**
17
- * Dag walker for dag-json CIDs. Does not actually use dag-json since
18
- * all we are interested in is extracting the the CIDs from the block
19
- * so we can just use cborg/json for that.
20
- */
21
- export declare const dagJsonWalker: DAGWalker;
22
- /**
23
- * Dag walker for json CIDs. JSON has no facility for linking to
24
- * external blocks so the walker is a no-op.
25
- */
26
- export declare const jsonWalker: DAGWalker;
27
- export declare function defaultDagWalkers(walkers?: DAGWalker[]): Record<number, DAGWalker>;
28
- //# sourceMappingURL=dag-walkers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dag-walkers.d.ts","sourceRoot":"","sources":["../../../src/utils/dag-walkers.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAEjD;;GAEG;AACH,eAAO,MAAM,WAAW,EAAE,SAOzB,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,SAKvB,CAAA;AAKD;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,SAuB3B,CAAA;AAsED;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,SA6B3B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,UAAU,EAAE,SAGxB,CAAA;AAED,wBAAgB,iBAAiB,CAAE,OAAO,GAAE,SAAS,EAAO,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAevF"}
@@ -1,171 +0,0 @@
1
- /* eslint max-depth: ["error", 7] */
2
- import * as dagCbor from '@ipld/dag-cbor';
3
- import * as dagJson from '@ipld/dag-json';
4
- import * as dagPb from '@ipld/dag-pb';
5
- import * as cborg from 'cborg';
6
- import { Type, Token } from 'cborg';
7
- import * as cborgJson from 'cborg/json';
8
- import { CID } from 'multiformats';
9
- import { base64 } from 'multiformats/bases/base64';
10
- import * as json from 'multiformats/codecs/json';
11
- import * as raw from 'multiformats/codecs/raw';
12
- /**
13
- * Dag walker for dag-pb CIDs
14
- */
15
- export const dagPbWalker = {
16
- codec: dagPb.code,
17
- *walk(block) {
18
- const node = dagPb.decode(block);
19
- yield* node.Links.map(l => l.Hash);
20
- }
21
- };
22
- /**
23
- * Dag walker for raw CIDs
24
- */
25
- export const rawWalker = {
26
- codec: raw.code,
27
- *walk() {
28
- // no embedded CIDs in a raw block
29
- }
30
- };
31
- // https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
32
- const CID_TAG = 42;
33
- /**
34
- * Dag walker for dag-cbor CIDs. Does not actually use dag-cbor since
35
- * all we are interested in is extracting the the CIDs from the block
36
- * so we can just use cborg for that.
37
- */
38
- export const dagCborWalker = {
39
- codec: dagCbor.code,
40
- *walk(block) {
41
- const cids = [];
42
- const tags = [];
43
- tags[CID_TAG] = (bytes) => {
44
- if (bytes[0] !== 0) {
45
- throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00');
46
- }
47
- const cid = CID.decode(bytes.subarray(1)); // ignore leading 0x00
48
- cids.push(cid);
49
- return cid;
50
- };
51
- cborg.decode(block, {
52
- tags
53
- });
54
- yield* cids;
55
- }
56
- };
57
- /**
58
- * Borrowed from @ipld/dag-json
59
- */
60
- class DagJsonTokenizer extends cborgJson.Tokenizer {
61
- tokenBuffer;
62
- constructor(data, options) {
63
- super(data, options);
64
- this.tokenBuffer = [];
65
- }
66
- done() {
67
- return this.tokenBuffer.length === 0 && super.done();
68
- }
69
- _next() {
70
- if (this.tokenBuffer.length > 0) {
71
- // @ts-expect-error https://github.com/Microsoft/TypeScript/issues/30406
72
- return this.tokenBuffer.pop();
73
- }
74
- return super.next();
75
- }
76
- /**
77
- * Implements rules outlined in https://github.com/ipld/specs/pull/356
78
- */
79
- next() {
80
- const token = this._next();
81
- if (token.type === Type.map) {
82
- const keyToken = this._next();
83
- if (keyToken.type === Type.string && keyToken.value === '/') {
84
- const valueToken = this._next();
85
- if (valueToken.type === Type.string) { // *must* be a CID
86
- const breakToken = this._next(); // swallow the end-of-map token
87
- if (breakToken.type !== Type.break) {
88
- throw new Error('Invalid encoded CID form');
89
- }
90
- this.tokenBuffer.push(valueToken); // CID.parse will pick this up after our tag token
91
- return new Token(Type.tag, 42, 0);
92
- }
93
- if (valueToken.type === Type.map) {
94
- const innerKeyToken = this._next();
95
- if (innerKeyToken.type === Type.string && innerKeyToken.value === 'bytes') {
96
- const innerValueToken = this._next();
97
- if (innerValueToken.type === Type.string) { // *must* be Bytes
98
- for (let i = 0; i < 2; i++) {
99
- const breakToken = this._next(); // swallow two end-of-map tokens
100
- if (breakToken.type !== Type.break) {
101
- throw new Error('Invalid encoded Bytes form');
102
- }
103
- }
104
- const bytes = base64.decode(`m${innerValueToken.value}`);
105
- return new Token(Type.bytes, bytes, innerValueToken.value.length);
106
- }
107
- this.tokenBuffer.push(innerValueToken); // bail
108
- }
109
- this.tokenBuffer.push(innerKeyToken); // bail
110
- }
111
- this.tokenBuffer.push(valueToken); // bail
112
- }
113
- this.tokenBuffer.push(keyToken); // bail
114
- }
115
- return token;
116
- }
117
- }
118
- /**
119
- * Dag walker for dag-json CIDs. Does not actually use dag-json since
120
- * all we are interested in is extracting the the CIDs from the block
121
- * so we can just use cborg/json for that.
122
- */
123
- export const dagJsonWalker = {
124
- codec: dagJson.code,
125
- *walk(block) {
126
- const cids = [];
127
- const tags = [];
128
- tags[CID_TAG] = (string) => {
129
- const cid = CID.parse(string);
130
- cids.push(cid);
131
- return cid;
132
- };
133
- cborgJson.decode(block, {
134
- tags,
135
- tokenizer: new DagJsonTokenizer(block, {
136
- tags,
137
- allowIndefinite: true,
138
- allowUndefined: true,
139
- allowNaN: true,
140
- allowInfinity: true,
141
- allowBigInt: true,
142
- strict: false,
143
- rejectDuplicateMapKeys: false
144
- })
145
- });
146
- yield* cids;
147
- }
148
- };
149
- /**
150
- * Dag walker for json CIDs. JSON has no facility for linking to
151
- * external blocks so the walker is a no-op.
152
- */
153
- export const jsonWalker = {
154
- codec: json.code,
155
- *walk() { }
156
- };
157
- export function defaultDagWalkers(walkers = []) {
158
- const output = {};
159
- [
160
- dagPbWalker,
161
- rawWalker,
162
- dagCborWalker,
163
- dagJsonWalker,
164
- jsonWalker,
165
- ...walkers
166
- ].forEach(dagWalker => {
167
- output[dagWalker.codec] = dagWalker;
168
- });
169
- return output;
170
- }
171
- //# sourceMappingURL=dag-walkers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dag-walkers.js","sourceRoot":"","sources":["../../../src/utils/dag-walkers.ts"],"names":[],"mappings":"AAAA,oCAAoC;AAEpC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;AACnC,OAAO,KAAK,SAAS,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAClD,OAAO,KAAK,IAAI,MAAM,0BAA0B,CAAA;AAChD,OAAO,KAAK,GAAG,MAAM,yBAAyB,CAAA;AAG9C;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAc;IACpC,KAAK,EAAE,KAAK,CAAC,IAAI;IACjB,CAAE,IAAI,CAAE,KAAK;QACX,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAEhC,KAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,C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B,MAAM,IAAI,GAAuB,EAAE,CAAA;QACnC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE;YACzB,MAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;YAE7B,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;YAEd,OAAO,GAAG,CAAA;QACZ,CAAC,CAAA;QAED,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE;YACtB,IAAI;YACJ,SAAS,EAAE,IAAI,gBAAgB,CAAC,KAAK,EAAE;gBACrC,IAAI;gBACJ,eAAe,EAAE,IAAI;gBACrB,cAAc,EAAE,IAAI;gBACpB,QAAQ,EAAE,IAAI;gBACd,aAAa,EAAE,IAAI;gBACnB,WAAW,EAAE,IAAI;gBACjB,MAAM,EAAE,KAAK;gBACb,sBAAsB,EAAE,KAAK;aAC9B,CAAC;SACH,CAAC,CAAA;QAEF,KAAM,CAAC,CAAC,IAAI,CAAA;IACd,CAAC;CACF,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAc;IACnC,KAAK,EAAE,IAAI,CAAC,IAAI;IAChB,CAAE,IAAI,KAAK,CAAC;CACb,CAAA;AAED,MAAM,UAAU,iBAAiB,CAAE,UAAuB,EAAE;IAC1D,MAAM,MAAM,GAA8B,EAAE,CAE3C;IAAA;QACC,WAAW;QACX,SAAS;QACT,aAAa;QACb,aAAa;QACb,UAAU;QACV,GAAG,OAAO;KACX,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE;QACpB,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,SAAS,CAAA;IACrC,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC"}
@@ -1,3 +0,0 @@
1
- import type { MultihashHasher } from 'multiformats/hashes/interface';
2
- export declare function defaultHashers(hashers?: MultihashHasher[]): Record<number, MultihashHasher>;
3
- //# sourceMappingURL=default-hashers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"default-hashers.d.ts","sourceRoot":"","sources":["../../../src/utils/default-hashers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAEpE,wBAAgB,cAAc,CAAE,OAAO,GAAE,eAAe,EAAO,GAAG,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAahG"}
@@ -1,15 +0,0 @@
1
- import { identity } from 'multiformats/hashes/identity';
2
- import { sha256, sha512 } from 'multiformats/hashes/sha2';
3
- export function defaultHashers(hashers = []) {
4
- const output = {};
5
- [
6
- sha256,
7
- sha512,
8
- identity,
9
- ...hashers
10
- ].forEach(hasher => {
11
- output[hasher.code] = hasher;
12
- });
13
- return output;
14
- }
15
- //# sourceMappingURL=default-hashers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"default-hashers.js","sourceRoot":"","sources":["../../../src/utils/default-hashers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAGzD,MAAM,UAAU,cAAc,CAAE,UAA6B,EAAE;IAC7D,MAAM,MAAM,GAAoC,EAAE,CAEjD;IAAA;QACC,MAAM;QACN,MAAM;QACN,QAAQ;QACR,GAAG,OAAO;KACX,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACjB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAA;IAC9B,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC"}
@@ -1,198 +0,0 @@
1
- /* eslint max-depth: ["error", 7] */
2
-
3
- import * as dagCbor from '@ipld/dag-cbor'
4
- import * as dagJson from '@ipld/dag-json'
5
- import * as dagPb from '@ipld/dag-pb'
6
- import * as cborg from 'cborg'
7
- import { Type, Token } from 'cborg'
8
- import * as cborgJson from 'cborg/json'
9
- import { CID } from 'multiformats'
10
- import { base64 } from 'multiformats/bases/base64'
11
- import * as json from 'multiformats/codecs/json'
12
- import * as raw from 'multiformats/codecs/raw'
13
- import type { DAGWalker } from '@helia/interface'
14
-
15
- /**
16
- * Dag walker for dag-pb CIDs
17
- */
18
- export const dagPbWalker: DAGWalker = {
19
- codec: dagPb.code,
20
- * walk (block) {
21
- const node = dagPb.decode(block)
22
-
23
- yield * node.Links.map(l => l.Hash)
24
- }
25
- }
26
-
27
- /**
28
- * Dag walker for raw CIDs
29
- */
30
- export const rawWalker: DAGWalker = {
31
- codec: raw.code,
32
- * walk () {
33
- // no embedded CIDs in a raw block
34
- }
35
- }
36
-
37
- // https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
38
- const CID_TAG = 42
39
-
40
- /**
41
- * Dag walker for dag-cbor CIDs. Does not actually use dag-cbor since
42
- * all we are interested in is extracting the the CIDs from the block
43
- * so we can just use cborg for that.
44
- */
45
- export const dagCborWalker: DAGWalker = {
46
- codec: dagCbor.code,
47
- * walk (block) {
48
- const cids: CID[] = []
49
- const tags: cborg.TagDecoder[] = []
50
- tags[CID_TAG] = (bytes) => {
51
- if (bytes[0] !== 0) {
52
- throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00')
53
- }
54
-
55
- const cid = CID.decode(bytes.subarray(1)) // ignore leading 0x00
56
-
57
- cids.push(cid)
58
-
59
- return cid
60
- }
61
-
62
- cborg.decode(block, {
63
- tags
64
- })
65
-
66
- yield * cids
67
- }
68
- }
69
-
70
- /**
71
- * Borrowed from @ipld/dag-json
72
- */
73
- class DagJsonTokenizer extends cborgJson.Tokenizer {
74
- private readonly tokenBuffer: cborg.Token[]
75
-
76
- constructor (data: Uint8Array, options?: cborg.DecodeOptions) {
77
- super(data, options)
78
-
79
- this.tokenBuffer = []
80
- }
81
-
82
- done (): boolean {
83
- return this.tokenBuffer.length === 0 && super.done()
84
- }
85
-
86
- _next (): cborg.Token {
87
- if (this.tokenBuffer.length > 0) {
88
- // @ts-expect-error https://github.com/Microsoft/TypeScript/issues/30406
89
- return this.tokenBuffer.pop()
90
- }
91
- return super.next()
92
- }
93
-
94
- /**
95
- * Implements rules outlined in https://github.com/ipld/specs/pull/356
96
- */
97
- next (): cborg.Token {
98
- const token = this._next()
99
-
100
- if (token.type === Type.map) {
101
- const keyToken = this._next()
102
- if (keyToken.type === Type.string && keyToken.value === '/') {
103
- const valueToken = this._next()
104
- if (valueToken.type === Type.string) { // *must* be a CID
105
- const breakToken = this._next() // swallow the end-of-map token
106
- if (breakToken.type !== Type.break) {
107
- throw new Error('Invalid encoded CID form')
108
- }
109
- this.tokenBuffer.push(valueToken) // CID.parse will pick this up after our tag token
110
- return new Token(Type.tag, 42, 0)
111
- }
112
- if (valueToken.type === Type.map) {
113
- const innerKeyToken = this._next()
114
- if (innerKeyToken.type === Type.string && innerKeyToken.value === 'bytes') {
115
- const innerValueToken = this._next()
116
- if (innerValueToken.type === Type.string) { // *must* be Bytes
117
- for (let i = 0; i < 2; i++) {
118
- const breakToken = this._next() // swallow two end-of-map tokens
119
- if (breakToken.type !== Type.break) {
120
- throw new Error('Invalid encoded Bytes form')
121
- }
122
- }
123
- const bytes = base64.decode(`m${innerValueToken.value}`)
124
- return new Token(Type.bytes, bytes, innerValueToken.value.length)
125
- }
126
- this.tokenBuffer.push(innerValueToken) // bail
127
- }
128
- this.tokenBuffer.push(innerKeyToken) // bail
129
- }
130
- this.tokenBuffer.push(valueToken) // bail
131
- }
132
- this.tokenBuffer.push(keyToken) // bail
133
- }
134
- return token
135
- }
136
- }
137
-
138
- /**
139
- * Dag walker for dag-json CIDs. Does not actually use dag-json since
140
- * all we are interested in is extracting the the CIDs from the block
141
- * so we can just use cborg/json for that.
142
- */
143
- export const dagJsonWalker: DAGWalker = {
144
- codec: dagJson.code,
145
- * walk (block) {
146
- const cids: CID[] = []
147
- const tags: cborg.TagDecoder[] = []
148
- tags[CID_TAG] = (string) => {
149
- const cid = CID.parse(string)
150
-
151
- cids.push(cid)
152
-
153
- return cid
154
- }
155
-
156
- cborgJson.decode(block, {
157
- tags,
158
- tokenizer: new DagJsonTokenizer(block, {
159
- tags,
160
- allowIndefinite: true,
161
- allowUndefined: true,
162
- allowNaN: true,
163
- allowInfinity: true,
164
- allowBigInt: true,
165
- strict: false,
166
- rejectDuplicateMapKeys: false
167
- })
168
- })
169
-
170
- yield * cids
171
- }
172
- }
173
-
174
- /**
175
- * Dag walker for json CIDs. JSON has no facility for linking to
176
- * external blocks so the walker is a no-op.
177
- */
178
- export const jsonWalker: DAGWalker = {
179
- codec: json.code,
180
- * walk () {}
181
- }
182
-
183
- export function defaultDagWalkers (walkers: DAGWalker[] = []): Record<number, DAGWalker> {
184
- const output: Record<number, DAGWalker> = {}
185
-
186
- ;[
187
- dagPbWalker,
188
- rawWalker,
189
- dagCborWalker,
190
- dagJsonWalker,
191
- jsonWalker,
192
- ...walkers
193
- ].forEach(dagWalker => {
194
- output[dagWalker.codec] = dagWalker
195
- })
196
-
197
- return output
198
- }
@@ -1,18 +0,0 @@
1
- import { identity } from 'multiformats/hashes/identity'
2
- import { sha256, sha512 } from 'multiformats/hashes/sha2'
3
- import type { MultihashHasher } from 'multiformats/hashes/interface'
4
-
5
- export function defaultHashers (hashers: MultihashHasher[] = []): Record<number, MultihashHasher> {
6
- const output: Record<number, MultihashHasher> = {}
7
-
8
- ;[
9
- sha256,
10
- sha512,
11
- identity,
12
- ...hashers
13
- ].forEach(hasher => {
14
- output[hasher.code] = hasher
15
- })
16
-
17
- return output
18
- }