@helia/utils 0.0.0-031519c

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 (44) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +66 -0
  3. package/dist/index.min.js +3 -0
  4. package/dist/src/index.d.ts +100 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +115 -0
  7. package/dist/src/index.js.map +1 -0
  8. package/dist/src/pins.d.ts +17 -0
  9. package/dist/src/pins.d.ts.map +1 -0
  10. package/dist/src/pins.js +155 -0
  11. package/dist/src/pins.js.map +1 -0
  12. package/dist/src/routing.d.ts +43 -0
  13. package/dist/src/routing.d.ts.map +1 -0
  14. package/dist/src/routing.js +122 -0
  15. package/dist/src/routing.js.map +1 -0
  16. package/dist/src/storage.d.ts +63 -0
  17. package/dist/src/storage.d.ts.map +1 -0
  18. package/dist/src/storage.js +140 -0
  19. package/dist/src/storage.js.map +1 -0
  20. package/dist/src/utils/dag-walkers.d.ts +28 -0
  21. package/dist/src/utils/dag-walkers.d.ts.map +1 -0
  22. package/dist/src/utils/dag-walkers.js +171 -0
  23. package/dist/src/utils/dag-walkers.js.map +1 -0
  24. package/dist/src/utils/datastore-version.d.ts +3 -0
  25. package/dist/src/utils/datastore-version.d.ts.map +1 -0
  26. package/dist/src/utils/datastore-version.js +19 -0
  27. package/dist/src/utils/datastore-version.js.map +1 -0
  28. package/dist/src/utils/default-hashers.d.ts +3 -0
  29. package/dist/src/utils/default-hashers.d.ts.map +1 -0
  30. package/dist/src/utils/default-hashers.js +15 -0
  31. package/dist/src/utils/default-hashers.js.map +1 -0
  32. package/dist/src/utils/networked-storage.d.ts +67 -0
  33. package/dist/src/utils/networked-storage.d.ts.map +1 -0
  34. package/dist/src/utils/networked-storage.js +206 -0
  35. package/dist/src/utils/networked-storage.js.map +1 -0
  36. package/package.json +91 -0
  37. package/src/index.ts +225 -0
  38. package/src/pins.ts +227 -0
  39. package/src/routing.ts +169 -0
  40. package/src/storage.ts +172 -0
  41. package/src/utils/dag-walkers.ts +198 -0
  42. package/src/utils/datastore-version.ts +23 -0
  43. package/src/utils/default-hashers.ts +18 -0
  44. package/src/utils/networked-storage.ts +261 -0
@@ -0,0 +1,171 @@
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
@@ -0,0 +1 @@
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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C,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"}
@@ -0,0 +1,3 @@
1
+ import { type Datastore } from 'interface-datastore';
2
+ export declare function assertDatastoreVersionIsCurrent(datastore: Datastore): Promise<void>;
3
+ //# sourceMappingURL=datastore-version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datastore-version.d.ts","sourceRoot":"","sources":["../../../src/utils/datastore-version.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAO,MAAM,qBAAqB,CAAA;AAOzD,wBAAsB,+BAA+B,CAAE,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAe1F"}
@@ -0,0 +1,19 @@
1
+ import { Key } from 'interface-datastore';
2
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
3
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
4
+ const DS_VERSION_KEY = new Key('/version');
5
+ const CURRENT_VERSION = 1;
6
+ export async function assertDatastoreVersionIsCurrent(datastore) {
7
+ if (!(await datastore.has(DS_VERSION_KEY))) {
8
+ await datastore.put(DS_VERSION_KEY, uint8ArrayFromString(`${CURRENT_VERSION}`));
9
+ return;
10
+ }
11
+ const buf = await datastore.get(DS_VERSION_KEY);
12
+ const str = uint8ArrayToString(buf);
13
+ const version = parseInt(str, 10);
14
+ if (version !== CURRENT_VERSION) {
15
+ // TODO: write migrations when we break compatibility - for an example, see https://github.com/ipfs/js-ipfs-repo/tree/master/packages/ipfs-repo-migrations
16
+ throw new Error('Unknown datastore version, a datastore migration may be required');
17
+ }
18
+ }
19
+ //# sourceMappingURL=datastore-version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datastore-version.js","sourceRoot":"","sources":["../../../src/utils/datastore-version.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,GAAG,EAAE,MAAM,qBAAqB,CAAA;AACzD,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAEtE,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,CAAA;AAC1C,MAAM,eAAe,GAAG,CAAC,CAAA;AAEzB,MAAM,CAAC,KAAK,UAAU,+BAA+B,CAAE,SAAoB;IACzE,IAAI,CAAC,CAAC,MAAM,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC;QAC3C,MAAM,SAAS,CAAC,GAAG,CAAC,cAAc,EAAE,oBAAoB,CAAC,GAAG,eAAe,EAAE,CAAC,CAAC,CAAA;QAE/E,OAAM;IACR,CAAC;IAED,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,CAAA;IAC/C,MAAM,GAAG,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAA;IACnC,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;IAEjC,IAAI,OAAO,KAAK,eAAe,EAAE,CAAC;QAChC,0JAA0J;QAC1J,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAA;IACrF,CAAC;AACH,CAAC"}
@@ -0,0 +1,3 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,15 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,67 @@
1
+ import { type ProgressOptions } from 'progress-events';
2
+ import type { BlockBroker, Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, BlockRetrievalOptions } from '@helia/interface/blocks';
3
+ import type { AbortOptions, ComponentLogger, Startable } from '@libp2p/interface';
4
+ import type { Blockstore } from 'interface-blockstore';
5
+ import type { AwaitIterable } from 'interface-store';
6
+ import type { CID } from 'multiformats/cid';
7
+ import type { MultihashHasher } from 'multiformats/hashes/interface';
8
+ export interface GetOptions extends AbortOptions {
9
+ progress?(evt: Event): void;
10
+ }
11
+ export interface NetworkedStorageComponents {
12
+ blockstore: Blockstore;
13
+ logger: ComponentLogger;
14
+ blockBrokers?: BlockBroker[];
15
+ hashers?: Record<number, MultihashHasher>;
16
+ }
17
+ /**
18
+ * Networked storage wraps a regular blockstore - when getting blocks if the
19
+ * blocks are not present Bitswap will be used to fetch them from network peers.
20
+ */
21
+ export declare class NetworkedStorage implements Blocks, Startable {
22
+ private readonly child;
23
+ private readonly blockRetrievers;
24
+ private readonly blockAnnouncers;
25
+ private readonly hashers;
26
+ private started;
27
+ private readonly log;
28
+ /**
29
+ * Create a new BlockStorage
30
+ */
31
+ constructor(components: NetworkedStorageComponents);
32
+ isStarted(): boolean;
33
+ start(): Promise<void>;
34
+ stop(): Promise<void>;
35
+ unwrap(): Blockstore;
36
+ /**
37
+ * Put a block to the underlying datastore
38
+ */
39
+ put(cid: CID, block: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>): Promise<CID>;
40
+ /**
41
+ * Put a multiple blocks to the underlying datastore
42
+ */
43
+ putMany(blocks: AwaitIterable<{
44
+ cid: CID;
45
+ block: Uint8Array;
46
+ }>, options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>): AsyncIterable<CID>;
47
+ /**
48
+ * Get a block by cid
49
+ */
50
+ get(cid: CID, options?: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents>): Promise<Uint8Array>;
51
+ /**
52
+ * Get multiple blocks back from an (async) iterable of cids
53
+ */
54
+ getMany(cids: AwaitIterable<CID>, options?: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents>): AsyncIterable<Pair>;
55
+ /**
56
+ * Delete a block from the blockstore
57
+ */
58
+ delete(cid: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>): Promise<void>;
59
+ /**
60
+ * Delete multiple blocks from the blockstore
61
+ */
62
+ deleteMany(cids: AwaitIterable<CID>, options?: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents>): AsyncIterable<CID>;
63
+ has(cid: CID, options?: AbortOptions): Promise<boolean>;
64
+ getAll(options?: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents>): AwaitIterable<Pair>;
65
+ }
66
+ export declare const getCidBlockVerifierFunction: (cid: CID, hasher: MultihashHasher) => Required<BlockRetrievalOptions>['validateFn'];
67
+ //# sourceMappingURL=networked-storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"networked-storage.d.ts","sourceRoot":"","sources":["../../../src/utils/networked-storage.ts"],"names":[],"mappings":"AAIA,OAAO,EAAuB,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAE3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,8BAA8B,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,2BAA2B,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,iBAAiB,EAAkC,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AACnV,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAyB,SAAS,EAAE,MAAM,mBAAmB,CAAA;AACxG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAEpE,MAAM,WAAW,UAAW,SAAQ,YAAY;IAC9C,QAAQ,CAAC,C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,GAAG,OAAO,CAAC,UAAU,CAAC;IAyBnI;;OAEG;IACK,OAAO,CAAE,IAAI,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,OAAO,GAAE,iBAAiB,GAAG,YAAY,GAAG,eAAe,CAAC,2BAA2B,CAAM,GAAG,aAAa,CAAC,IAAI,CAAC;IAuB9J;;OAEG;IACG,MAAM,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,YAAY,GAAG,eAAe,CAAC,yBAAyB,CAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAM/G;;OAEG;IACK,UAAU,CAAE,IAAI,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,OAAO,GAAE,YAAY,GAAG,eAAe,CAAC,8BAA8B,CAAM,GAAG,aAAa,CAAC,GAAG,CAAC;IASzI,GAAG,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,OAAO,CAAC;IAI1D,MAAM,CAAE,OAAO,GAAE,YAAY,GAAG,eAAe,CAAC,0BAA0B,CAAM,GAAG,aAAa,CAAC,IAAI,CAAC;CAI/G;AAED,eAAO,MAAM,2BAA2B,QAAS,GAAG,UAAU,eAAe,KAAG,SAAS,qBAAqB,CAAC,CAAC,YAAY,CAc3H,CAAA"}
@@ -0,0 +1,206 @@
1
+ import { CodeError, start, stop } from '@libp2p/interface';
2
+ import { anySignal } from 'any-signal';
3
+ import filter from 'it-filter';
4
+ import forEach from 'it-foreach';
5
+ import { CustomProgressEvent } from 'progress-events';
6
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
7
+ function isBlockRetriever(b) {
8
+ return typeof b.retrieve === 'function';
9
+ }
10
+ function isBlockAnnouncer(b) {
11
+ return typeof b.announce === 'function';
12
+ }
13
+ /**
14
+ * Networked storage wraps a regular blockstore - when getting blocks if the
15
+ * blocks are not present Bitswap will be used to fetch them from network peers.
16
+ */
17
+ export class NetworkedStorage {
18
+ child;
19
+ blockRetrievers;
20
+ blockAnnouncers;
21
+ hashers;
22
+ started;
23
+ log;
24
+ /**
25
+ * Create a new BlockStorage
26
+ */
27
+ constructor(components) {
28
+ this.log = components.logger.forComponent('helia:networked-storage');
29
+ this.child = components.blockstore;
30
+ this.blockRetrievers = (components.blockBrokers ?? []).filter(isBlockRetriever);
31
+ this.blockAnnouncers = (components.blockBrokers ?? []).filter(isBlockAnnouncer);
32
+ this.hashers = components.hashers ?? {};
33
+ this.started = false;
34
+ }
35
+ isStarted() {
36
+ return this.started;
37
+ }
38
+ async start() {
39
+ await start(this.child, ...new Set([...this.blockRetrievers, ...this.blockAnnouncers]));
40
+ this.started = true;
41
+ }
42
+ async stop() {
43
+ await stop(this.child, ...new Set([...this.blockRetrievers, ...this.blockAnnouncers]));
44
+ this.started = false;
45
+ }
46
+ unwrap() {
47
+ return this.child;
48
+ }
49
+ /**
50
+ * Put a block to the underlying datastore
51
+ */
52
+ async put(cid, block, options = {}) {
53
+ if (await this.child.has(cid)) {
54
+ options.onProgress?.(new CustomProgressEvent('blocks:put:duplicate', cid));
55
+ return cid;
56
+ }
57
+ options.onProgress?.(new CustomProgressEvent('blocks:put:providers:notify', cid));
58
+ this.blockAnnouncers.forEach(provider => {
59
+ provider.announce(cid, block, options);
60
+ });
61
+ options.onProgress?.(new CustomProgressEvent('blocks:put:blockstore:put', cid));
62
+ return this.child.put(cid, block, options);
63
+ }
64
+ /**
65
+ * Put a multiple blocks to the underlying datastore
66
+ */
67
+ async *putMany(blocks, options = {}) {
68
+ const missingBlocks = filter(blocks, async ({ cid }) => {
69
+ const has = await this.child.has(cid);
70
+ if (has) {
71
+ options.onProgress?.(new CustomProgressEvent('blocks:put-many:duplicate', cid));
72
+ }
73
+ return !has;
74
+ });
75
+ const notifyEach = forEach(missingBlocks, ({ cid, block }) => {
76
+ options.onProgress?.(new CustomProgressEvent('blocks:put-many:providers:notify', cid));
77
+ this.blockAnnouncers.forEach(provider => {
78
+ provider.announce(cid, block, options);
79
+ });
80
+ });
81
+ options.onProgress?.(new CustomProgressEvent('blocks:put-many:blockstore:put-many'));
82
+ yield* this.child.putMany(notifyEach, options);
83
+ }
84
+ /**
85
+ * Get a block by cid
86
+ */
87
+ async get(cid, options = {}) {
88
+ if (options.offline !== true && !(await this.child.has(cid))) {
89
+ // we do not have the block locally, get it from a block provider
90
+ options.onProgress?.(new CustomProgressEvent('blocks:get:providers:get', cid));
91
+ const block = await raceBlockRetrievers(cid, this.blockRetrievers, this.hashers[cid.multihash.code], {
92
+ ...options,
93
+ log: this.log
94
+ });
95
+ options.onProgress?.(new CustomProgressEvent('blocks:get:blockstore:put', cid));
96
+ await this.child.put(cid, block, options);
97
+ // notify other block providers of the new block
98
+ options.onProgress?.(new CustomProgressEvent('blocks:get:providers:notify', cid));
99
+ this.blockAnnouncers.forEach(provider => {
100
+ provider.announce(cid, block, options);
101
+ });
102
+ return block;
103
+ }
104
+ options.onProgress?.(new CustomProgressEvent('blocks:get:blockstore:get', cid));
105
+ return this.child.get(cid, options);
106
+ }
107
+ /**
108
+ * Get multiple blocks back from an (async) iterable of cids
109
+ */
110
+ async *getMany(cids, options = {}) {
111
+ options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:get-many'));
112
+ yield* this.child.getMany(forEach(cids, async (cid) => {
113
+ if (options.offline !== true && !(await this.child.has(cid))) {
114
+ // we do not have the block locally, get it from a block provider
115
+ options.onProgress?.(new CustomProgressEvent('blocks:get-many:providers:get', cid));
116
+ const block = await raceBlockRetrievers(cid, this.blockRetrievers, this.hashers[cid.multihash.code], {
117
+ ...options,
118
+ log: this.log
119
+ });
120
+ options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:put', cid));
121
+ await this.child.put(cid, block, options);
122
+ // notify other block providers of the new block
123
+ options.onProgress?.(new CustomProgressEvent('blocks:get-many:providers:notify', cid));
124
+ this.blockAnnouncers.forEach(provider => {
125
+ provider.announce(cid, block, options);
126
+ });
127
+ }
128
+ }));
129
+ }
130
+ /**
131
+ * Delete a block from the blockstore
132
+ */
133
+ async delete(cid, options = {}) {
134
+ options.onProgress?.(new CustomProgressEvent('blocks:delete:blockstore:delete', cid));
135
+ await this.child.delete(cid, options);
136
+ }
137
+ /**
138
+ * Delete multiple blocks from the blockstore
139
+ */
140
+ async *deleteMany(cids, options = {}) {
141
+ options.onProgress?.(new CustomProgressEvent('blocks:delete-many:blockstore:delete-many'));
142
+ yield* this.child.deleteMany((async function* () {
143
+ for await (const cid of cids) {
144
+ yield cid;
145
+ }
146
+ }()), options);
147
+ }
148
+ async has(cid, options = {}) {
149
+ return this.child.has(cid, options);
150
+ }
151
+ async *getAll(options = {}) {
152
+ options.onProgress?.(new CustomProgressEvent('blocks:get-all:blockstore:get-many'));
153
+ yield* this.child.getAll(options);
154
+ }
155
+ }
156
+ export const getCidBlockVerifierFunction = (cid, hasher) => {
157
+ if (hasher == null) {
158
+ 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');
159
+ }
160
+ return async (block) => {
161
+ // verify block
162
+ const hash = await hasher.digest(block);
163
+ if (!uint8ArrayEquals(hash.digest, cid.multihash.digest)) {
164
+ // if a hash mismatch occurs for a TrustlessGatewayBlockBroker, we should try another gateway
165
+ throw new CodeError('Hash of downloaded block did not match multihash from passed CID', 'ERR_HASH_MISMATCH');
166
+ }
167
+ };
168
+ };
169
+ /**
170
+ * Race block providers cancelling any pending requests once the block has been
171
+ * found.
172
+ */
173
+ async function raceBlockRetrievers(cid, providers, hasher, options) {
174
+ const validateFn = getCidBlockVerifierFunction(cid, hasher);
175
+ const controller = new AbortController();
176
+ const signal = anySignal([controller.signal, options.signal]);
177
+ try {
178
+ return await Promise.any(providers.map(async (provider) => {
179
+ try {
180
+ let blocksWereValidated = false;
181
+ const block = await provider.retrieve(cid, {
182
+ ...options,
183
+ signal,
184
+ validateFn: async (block) => {
185
+ await validateFn(block);
186
+ blocksWereValidated = true;
187
+ }
188
+ });
189
+ if (!blocksWereValidated) {
190
+ // the blockBroker either did not throw an error when attempting to validate the block
191
+ // or did not call the validateFn at all. We should validate the block ourselves
192
+ await validateFn(block);
193
+ }
194
+ return block;
195
+ }
196
+ catch (err) {
197
+ options.log.error('could not retrieve verified block for %c', cid, err);
198
+ throw err;
199
+ }
200
+ }));
201
+ }
202
+ finally {
203
+ signal.clear();
204
+ }
205
+ }
206
+ //# sourceMappingURL=networked-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"networked-storage.js","sourceRoot":"","sources":["../../../src/utils/networked-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,MAAM,MAAM,WAAW,CAAA;AAC9B,OAAO,OAAO,MAAM,YAAY,CAAA;AAChC,OAAO,EAAE,mBAAmB,EAAwB,MAAM,iBAAiB,CAAA;AAC3E,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAY/D,SAAS,gBAAgB,CAAE,CAAM;IAC/B,OAAO,OAAO,CAAC,CAAC,QAAQ,KAAK,UAAU,CAAA;AACzC,CAAC;AAED,SAAS,gBAAgB,CAAE,CAAM;IAC/B,OAAO,OAAO,CAAC,CAAC,QAAQ,KAAK,UAAU,CAAA;AACzC,CAAC;AASD;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IACV,KAAK,CAAY;IACjB,eAAe,CAAkB;IACjC,eAAe,CAAkB;IACjC,OAAO,CAAiC;IACjD,OAAO,CAAS;IACP,GAAG,CAAQ;IAE5B;;OAEG;IACH,YAAa,UAAsC;QACjD,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,yBAAyB,CAAC,CAAA;QACpE,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC,UAAU,CAAA;Q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,EAAE,KAAK,EAAE,KAAiB,EAAiB,EAAE;wBACrD,MAAM,UAAU,CAAC,KAAK,CAAC,CAAA;wBACvB,mBAAmB,GAAG,IAAI,CAAA;oBAC5B,CAAC;iBACF,CAAC,CAAA;gBAEF,IAAI,CAAC,mBAAmB,EAAE,CAAC;oBACzB,sFAAsF;oBACtF,gFAAgF;oBAChF,MAAM,UAAU,CAAC,KAAK,CAAC,CAAA;gBACzB,CAAC;gBAED,OAAO,KAAK,CAAA;YACd,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,0CAA0C,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;gBACvE,MAAM,GAAG,CAAA;YACX,CAAC;QACH,CAAC,CAAC,CACH,CAAA;IACH,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,KAAK,EAAE,CAAA;IAChB,CAAC;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "@helia/utils",
3
+ "version": "0.0.0-031519c",
4
+ "description": "Shared code that implements the Helia API",
5
+ "license": "Apache-2.0 OR MIT",
6
+ "homepage": "https://github.com/ipfs/helia/tree/main/packages/utils#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ipfs/helia.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/ipfs/helia/issues"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "provenance": true
17
+ },
18
+ "keywords": [
19
+ "IPFS"
20
+ ],
21
+ "type": "module",
22
+ "types": "./dist/src/index.d.ts",
23
+ "files": [
24
+ "src",
25
+ "dist",
26
+ "!dist/test",
27
+ "!**/*.tsbuildinfo"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/src/index.d.ts",
32
+ "import": "./dist/src/index.js"
33
+ }
34
+ },
35
+ "eslintConfig": {
36
+ "extends": "ipfs",
37
+ "parserOptions": {
38
+ "project": true,
39
+ "sourceType": "module"
40
+ }
41
+ },
42
+ "scripts": {
43
+ "clean": "aegir clean",
44
+ "lint": "aegir lint",
45
+ "dep-check": "aegir dep-check",
46
+ "build": "aegir build",
47
+ "test": "aegir test",
48
+ "test:chrome": "aegir test -t browser --cov",
49
+ "test:chrome-webworker": "aegir test -t webworker",
50
+ "test:firefox": "aegir test -t browser -- --browser firefox",
51
+ "test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
52
+ "test:node": "aegir test -t node --cov",
53
+ "test:electron-main": "aegir test -t electron-main"
54
+ },
55
+ "dependencies": {
56
+ "@helia/interface": "3.0.1-031519c",
57
+ "@ipld/dag-cbor": "^9.0.7",
58
+ "@ipld/dag-json": "^10.1.5",
59
+ "@ipld/dag-pb": "^4.0.6",
60
+ "@libp2p/interface": "^1.1.1",
61
+ "@libp2p/logger": "^4.0.4",
62
+ "@libp2p/peer-collections": "^5.1.4",
63
+ "@libp2p/utils": "^5.2.0",
64
+ "any-signal": "^4.1.1",
65
+ "cborg": "^4.0.3",
66
+ "interface-blockstore": "^5.2.7",
67
+ "interface-datastore": "^8.2.9",
68
+ "interface-store": "^5.1.5",
69
+ "it-drain": "^3.0.5",
70
+ "it-filter": "^3.0.4",
71
+ "it-foreach": "^2.0.6",
72
+ "it-merge": "^3.0.3",
73
+ "mortice": "^3.0.1",
74
+ "multiformats": "^13.0.0",
75
+ "progress-events": "^1.0.0",
76
+ "uint8arrays": "^5.0.1"
77
+ },
78
+ "devDependencies": {
79
+ "@types/sinon": "^17.0.2",
80
+ "aegir": "^42.1.0",
81
+ "blockstore-core": "^4.3.10",
82
+ "datastore-core": "^9.2.7",
83
+ "delay": "^6.0.0",
84
+ "it-all": "^3.0.4",
85
+ "sinon": "^17.0.1",
86
+ "sinon-ts": "^2.0.0"
87
+ },
88
+ "browser": {
89
+ "./dist/src/utils/libp2p-defaults.js": "./dist/src/utils/libp2p-defaults.browser.js"
90
+ }
91
+ }