@helia/utils 2.3.5 → 2.4.0

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.
@@ -0,0 +1,56 @@
1
+ import type { HasherLoader } from '@helia/interface';
2
+ import type { BlockBroker, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, BlockRetrievalOptions } from '@helia/interface/blocks';
3
+ import type { AbortOptions, ComponentLogger, Logger } from '@libp2p/interface';
4
+ import type { Blockstore, InputPair } 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
+ import type { ProgressEvent, ProgressOptions } from 'progress-events';
9
+ export interface StorageComponents<Broker extends BlockBroker<ProgressEvent<any, any>, ProgressEvent<any, any>>> {
10
+ blockstore: Blockstore;
11
+ logger: ComponentLogger;
12
+ blockBrokers: Broker[];
13
+ getHasher: HasherLoader;
14
+ }
15
+ export interface StorageInit {
16
+ maxIdentityHashDigestLength?: number;
17
+ }
18
+ export declare class Storage<Broker extends BlockBroker<ProgressEvent<any, any>, ProgressEvent<any, any>>> implements Blockstore {
19
+ protected readonly child: Blockstore;
20
+ protected readonly getHasher: HasherLoader;
21
+ protected log: Logger;
22
+ protected readonly logger: ComponentLogger;
23
+ protected readonly blockBrokers: Broker[];
24
+ /**
25
+ * Create a new BlockStorage
26
+ */
27
+ constructor(components: StorageComponents<Broker>, init?: StorageInit);
28
+ /**
29
+ * Put a block to the underlying datastore
30
+ */
31
+ put(cid: CID, block: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>): Promise<CID>;
32
+ /**
33
+ * Put a multiple blocks to the underlying datastore
34
+ */
35
+ putMany(blocks: AwaitIterable<InputPair>, options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>): AsyncGenerator<CID>;
36
+ /**
37
+ * Get a block by cid
38
+ */
39
+ get(cid: CID, options?: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents>): AsyncGenerator<Uint8Array>;
40
+ /**
41
+ * Get multiple blocks back from an (async) iterable of cids
42
+ */
43
+ getMany(cids: AwaitIterable<CID>, options?: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents>): AsyncGenerator<Pair>;
44
+ /**
45
+ * Delete a block from the blockstore
46
+ */
47
+ delete(cid: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>): Promise<void>;
48
+ /**
49
+ * Delete multiple blocks from the blockstore
50
+ */
51
+ deleteMany(cids: AwaitIterable<CID>, options?: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents>): AsyncGenerator<CID>;
52
+ has(cid: CID, options?: AbortOptions): Promise<boolean>;
53
+ getAll(options?: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents>): AsyncGenerator<Pair>;
54
+ }
55
+ export declare const getCidBlockVerifierFunction: (cid: CID, hasher: MultihashHasher) => Required<BlockRetrievalOptions>["validateFn"];
56
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../src/utils/storage.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,8BAA8B,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,2BAA2B,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC3S,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,EAAiB,MAAM,mBAAmB,CAAA;AAC7F,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,sB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entityBlockstore } from 'blockstore-core/identity';
4
+ import filter from 'it-filter';
5
+ import forEach from 'it-foreach';
6
+ import { CustomProgressEvent } from 'progress-events';
7
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
8
+ import { BlockNotFoundWhileOfflineError, InvalidConfigurationError, LoadBlockFailedError } from "../errors.js";
9
+ import { isPromise } from './is-promise.js';
10
+ const DEFAULT_MAX_IDENTITY_HASH_DIGEST_LENGTH = 128;
11
+ export class Storage {
12
+ child;
13
+ getHasher;
14
+ log;
15
+ logger;
16
+ blockBrokers;
17
+ /**
18
+ * Create a new BlockStorage
19
+ */
20
+ constructor(components, init = {}) {
21
+ this.log = components.logger.forComponent('helia:networked-storage');
22
+ this.logger = components.logger;
23
+ this.blockBrokers = components.blockBrokers;
24
+ this.child = new IdentityBlockstore(components.blockstore, {
25
+ maxDigestLength: init.maxIdentityHashDigestLength ?? DEFAULT_MAX_IDENTITY_HASH_DIGEST_LENGTH
26
+ });
27
+ this.getHasher = components.getHasher;
28
+ }
29
+ /**
30
+ * Put a block to the underlying datastore
31
+ */
32
+ async put(cid, block, options = {}) {
33
+ if (await this.child.has(cid, options)) {
34
+ options.onProgress?.(new CustomProgressEvent('blocks:put:duplicate', cid));
35
+ return cid;
36
+ }
37
+ options.onProgress?.(new CustomProgressEvent('blocks:put:providers:notify', cid));
38
+ await Promise.all(this.blockBrokers.map(async (broker) => broker.announce?.(cid, options)));
39
+ options.onProgress?.(new CustomProgressEvent('blocks:put:blockstore:put', cid));
40
+ return this.child.put(cid, block, options);
41
+ }
42
+ /**
43
+ * Put a multiple blocks to the underlying datastore
44
+ */
45
+ async *putMany(blocks, options = {}) {
46
+ const missingBlocks = filter(blocks, async ({ cid }) => {
47
+ const has = await this.child.has(cid, options);
48
+ if (has) {
49
+ options.onProgress?.(new CustomProgressEvent('blocks:put-many:duplicate', cid));
50
+ }
51
+ return !has;
52
+ });
53
+ const notifyEach = forEach(missingBlocks, async ({ cid }) => {
54
+ options.onProgress?.(new CustomProgressEvent('blocks:put-many:providers:notify', cid));
55
+ await Promise.all(this.blockBrokers.map(async (broker) => broker.announce?.(cid, options)));
56
+ });
57
+ options.onProgress?.(new CustomProgressEvent('blocks:put-many:blockstore:put-many'));
58
+ yield* this.child.putMany(notifyEach, options);
59
+ }
60
+ /**
61
+ * Get a block by cid
62
+ */
63
+ async *get(cid, options = {}) {
64
+ const has = await this.child.has(cid, options);
65
+ const offline = options.offline === true;
66
+ if (!has) {
67
+ if (offline) {
68
+ throw new BlockNotFoundWhileOfflineError('The block was present in the blockstore and the node is running offline so cannot fetch it');
69
+ }
70
+ const hasher = await this.getHasher(cid.multihash.code);
71
+ options?.signal?.throwIfAborted();
72
+ // we do not have the block locally, get it from a block provider
73
+ options.onProgress?.(new CustomProgressEvent('blocks:get:providers:get', cid));
74
+ const block = await raceBlockRetrievers(cid, this.blockBrokers, hasher, {
75
+ ...options,
76
+ log: this.log
77
+ });
78
+ options.onProgress?.(new CustomProgressEvent('blocks:get:blockstore:put', cid));
79
+ await this.child.put(cid, block, options);
80
+ // notify other block providers of the new block
81
+ options.onProgress?.(new CustomProgressEvent('blocks:get:providers:notify', cid));
82
+ await Promise.all(this.blockBrokers.map(async (broker) => broker.announce?.(cid, options)));
83
+ yield block;
84
+ return;
85
+ }
86
+ options.onProgress?.(new CustomProgressEvent('blocks:get:blockstore:get', cid));
87
+ yield* this.child.get(cid, options);
88
+ }
89
+ /**
90
+ * Get multiple blocks back from an (async) iterable of cids
91
+ */
92
+ async *getMany(cids, options = {}) {
93
+ options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:get-many'));
94
+ yield* this.child.getMany(forEach(cids, async (cid) => {
95
+ const has = await this.child.has(cid, options);
96
+ const offline = options.offline === true;
97
+ if (!has) {
98
+ if (offline) {
99
+ throw new BlockNotFoundWhileOfflineError('The block was present in the blockstore and the node is running offline so cannot fetch it');
100
+ }
101
+ const hasher = await this.getHasher(cid.multihash.code);
102
+ options?.signal?.throwIfAborted();
103
+ // we do not have the block locally, get it from a block provider
104
+ options.onProgress?.(new CustomProgressEvent('blocks:get-many:providers:get', cid));
105
+ const block = await raceBlockRetrievers(cid, this.blockBrokers, hasher, {
106
+ ...options,
107
+ log: this.log
108
+ });
109
+ options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:put', cid));
110
+ await this.child.put(cid, block, options);
111
+ // notify other block providers of the new block
112
+ options.onProgress?.(new CustomProgressEvent('blocks:get-many:providers:notify', cid));
113
+ await Promise.all(this.blockBrokers.map(async (broker) => broker.announce?.(cid, options)));
114
+ }
115
+ }));
116
+ }
117
+ /**
118
+ * Delete a block from the blockstore
119
+ */
120
+ async delete(cid, options = {}) {
121
+ options.onProgress?.(new CustomProgressEvent('blocks:delete:blockstore:delete', cid));
122
+ await this.child.delete(cid, options);
123
+ }
124
+ /**
125
+ * Delete multiple blocks from the blockstore
126
+ */
127
+ async *deleteMany(cids, options = {}) {
128
+ options.onProgress?.(new CustomProgressEvent('blocks:delete-many:blockstore:delete-many'));
129
+ yield* this.child.deleteMany((async function* () {
130
+ for await (const cid of cids) {
131
+ yield cid;
132
+ }
133
+ }()), options);
134
+ }
135
+ async has(cid, options = {}) {
136
+ return this.child.has(cid, options);
137
+ }
138
+ async *getAll(options = {}) {
139
+ options.onProgress?.(new CustomProgressEvent('blocks:get-all:blockstore:get-many'));
140
+ yield* this.child.getAll(options);
141
+ }
142
+ }
143
+ /**
144
+ * Race block providers cancelling any pending requests once the block has been
145
+ * found.
146
+ */
147
+ async function raceBlockRetrievers(cid, blockBrokers, hasher, options) {
148
+ const validateFn = getCidBlockVerifierFunction(cid, hasher);
149
+ const controller = new AbortController();
150
+ const signal = anySignal([controller.signal, options.signal]);
151
+ setMaxListeners(Infinity, controller.signal, signal);
152
+ const retrievers = [];
153
+ for (const broker of blockBrokers) {
154
+ if (isRetrievingBlockBroker(broker)) {
155
+ retrievers.push(broker);
156
+ }
157
+ }
158
+ if (retrievers.length === 0) {
159
+ throw new InvalidConfigurationError(`No block brokers capable of retrieving blocks are configured, the CID ${cid} cannot be fetched from the network`);
160
+ }
161
+ try {
162
+ return await Promise.any(retrievers
163
+ .map(async (retriever) => {
164
+ try {
165
+ let blocksWereValidated = false;
166
+ const block = await retriever.retrieve(cid, {
167
+ ...options,
168
+ signal,
169
+ validateFn: async (block) => {
170
+ await validateFn(block);
171
+ options.signal?.throwIfAborted();
172
+ blocksWereValidated = true;
173
+ }
174
+ });
175
+ if (!blocksWereValidated) {
176
+ // the blockBroker either did not throw an error when attempting to validate the block
177
+ // or did not call the validateFn at all. We should validate the block ourselves
178
+ await validateFn(block);
179
+ options.signal?.throwIfAborted();
180
+ }
181
+ return block;
182
+ }
183
+ catch (err) {
184
+ options.log.error('could not retrieve verified block for %c from %s - %e', cid, retriever.name, err);
185
+ throw err;
186
+ }
187
+ }));
188
+ }
189
+ catch (err) {
190
+ throw new LoadBlockFailedError(err.errors, `Failed to load block for ${cid}`);
191
+ }
192
+ finally {
193
+ // we have the block from the fastest block retriever, abort any still
194
+ // in-flight retrieve attempts
195
+ controller.abort();
196
+ signal.clear();
197
+ }
198
+ }
199
+ function isRetrievingBlockBroker(broker) {
200
+ return typeof broker.retrieve === 'function';
201
+ }
202
+ export const getCidBlockVerifierFunction = (cid, hasher) => {
203
+ if (hasher == null) {
204
+ 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`);
205
+ }
206
+ return async (block) => {
207
+ // verify block
208
+ let hash;
209
+ const res = hasher.digest(block, {
210
+ // support truncated hashes where they are truncated
211
+ truncate: cid.multihash.digest.byteLength
212
+ });
213
+ if (isPromise(res)) {
214
+ hash = await res;
215
+ }
216
+ else {
217
+ hash = res;
218
+ }
219
+ if (!uint8ArrayEquals(hash.digest, cid.multihash.digest)) {
220
+ // if a hash mismatch occurs for a TrustlessGatewayBlockBroker, we should try another gateway
221
+ throw new InvalidMultihashError('Hash of downloaded block did not match multihash from passed CID');
222
+ }
223
+ };
224
+ };
225
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../../src/utils/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAClG,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAC7D,OAAO,MAAM,MAAM,WAAW,CAAA;AAC9B,OAAO,OAAO,MAAM,YAAY,CAAA;AAChC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,8BAA8B,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAC9G,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAqB3C,MAAM,uCAAuC,GAAG,GAAG,CAAA;AAEnD,MAAM,OAAO,OAAO;IACC,KAAK,CAAY;IACjB,SAAS,CAAc;IAChC,GAAG,CAAQ;IACF,MAAM,CAAiB;IACvB,YAAY,CAAU;IAEzC;;OAEG;IACH,YAAa,UAAqC,EAAE,OAAoB,EAAE;QACxE,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,yBAAyB,CAAC,CAAA;QACpE,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,MAAM,CAAA;QAC/B,IAAI,CAAC,YAAY,GAAG,UAAU,CAAC,YAAY,CAAA;QAC3C,IAAI,CAAC,KAAK,GAAG,IAAI,kBAAkB,CAAC,UAAU,CAAC,UAAU,EAAE;YACzD,eAAe,EAAE,IAAI,CAAC,2BAA2B,IAAI,uCAAuC;SAC7F,CAAC,CAAA;QACF,IAAI,CAAC,SAAS,GAAG,UAAU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AC,EAAE,CAAC;YACzD,6FAA6F;YAC7F,MAAM,IAAI,qBAAqB,CAAC,kEAAkE,CAAC,CAAA;QACrG,CAAC;IACH,CAAC,CAAA;AACH,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helia/utils",
3
- "version": "2.3.5",
3
+ "version": "2.4.0",
4
4
  "description": "Shared code that implements the Helia API",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/ipfs/helia/tree/main/packages/utils#readme",
@@ -47,13 +47,12 @@
47
47
  "test:electron-main": "aegir test -t electron-main"
48
48
  },
49
49
  "dependencies": {
50
- "@helia/interface": "^6.0.3",
50
+ "@helia/interface": "^6.1.0",
51
51
  "@ipld/dag-cbor": "^9.2.5",
52
52
  "@ipld/dag-json": "^10.2.5",
53
53
  "@ipld/dag-pb": "^4.1.5",
54
54
  "@libp2p/interface": "^3.1.0",
55
55
  "@libp2p/keychain": "^6.0.5",
56
- "@libp2p/logger": "^6.0.5",
57
56
  "@libp2p/utils": "^7.0.5",
58
57
  "@multiformats/dns": "^1.0.9",
59
58
  "@multiformats/multiaddr": "^13.0.1",
@@ -78,6 +77,7 @@
78
77
  },
79
78
  "devDependencies": {
80
79
  "@libp2p/crypto": "^5.1.12",
80
+ "@libp2p/logger": "^6.0.5",
81
81
  "@libp2p/peer-id": "^6.0.3",
82
82
  "@types/sinon": "^21.0.0",
83
83
  "aegir": "^47.0.22",
@@ -30,9 +30,10 @@ interface Request {
30
30
  }
31
31
 
32
32
  export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents extends ProgressEvent> extends TypedEventEmitter<BlockstoreSessionEvents<Provider>> implements BlockBroker<RetrieveBlockProgressEvents> {
33
+ public abstract name: string
33
34
  private initialPeerSearchComplete?: Promise<void>
34
35
  private readonly requests: Map<string, Request>
35
- private readonly name: string
36
+ private readonly logName: string
36
37
  protected log: Logger
37
38
  protected logger: ComponentLogger
38
39
  private readonly minProviders: number
@@ -45,9 +46,9 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
45
46
  super()
46
47
 
47
48
  setMaxListeners(Infinity, this)
48
- this.name = init.name
49
+ this.logName = init.name
49
50
  this.logger = components.logger
50
- this.log = components.logger.forComponent(this.name)
51
+ this.log = components.logger.forComponent(this.logName)
51
52
  this.requests = new Map()
52
53
  this.minProviders = init.minProviders ?? DEFAULT_SESSION_MIN_PROVIDERS
53
54
  this.maxProviders = init.maxProviders ?? DEFAULT_SESSION_MAX_PROVIDERS
@@ -79,7 +80,7 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
79
80
 
80
81
  if (this.initialPeerSearchComplete == null) {
81
82
  first = true
82
- this.log = this.logger.forComponent(`${this.name}:${cid}`)
83
+ this.log = this.logger.forComponent(`${this.logName}:${cid}`)
83
84
  this.initialPeerSearchComplete = this.findProviders(cid, this.minProviders, options)
84
85
  }
85
86
 
@@ -249,6 +250,21 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
249
250
  return false
250
251
  }
251
252
 
253
+ async addPeer (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void> {
254
+ const provider = await this.convertToProvider(peer, options)
255
+
256
+ if (provider == null || this.hasProvider(provider)) {
257
+ return
258
+ }
259
+
260
+ this.providers.push(provider)
261
+
262
+ // let the new peer join current queries
263
+ this.safeDispatchEvent('provider', {
264
+ detail: provider
265
+ })
266
+ }
267
+
252
268
  private async findProviders (cid: CID, count: number, options: AbortOptions): Promise<void> {
253
269
  const deferred: DeferredPromise<void> = pDefer()
254
270
  let found = 0
package/src/index.ts CHANGED
@@ -6,7 +6,6 @@
6
6
  */
7
7
 
8
8
  import { contentRoutingSymbol, peerRoutingSymbol, start, stop, TypedEventEmitter } from '@libp2p/interface'
9
- import { defaultLogger } from '@libp2p/logger'
10
9
  import { dns } from '@multiformats/dns'
11
10
  import drain from 'it-drain'
12
11
  import { CustomProgressEvent } from 'progress-events'
@@ -208,7 +207,7 @@ export class Helia<T extends Libp2p> implements HeliaInterface<T> {
208
207
  private readonly log: Logger
209
208
 
210
209
  constructor (init: Omit<HeliaInit, 'start' | 'libp2p'> & { libp2p: T }) {
211
- this.logger = init.logger ?? defaultLogger()
210
+ this.logger = init.logger ?? init.libp2p.logger
212
211
  this.log = this.logger.forComponent('helia')
213
212
  this.getHasher = getHasher(init.hashers, init.loadHasher)
214
213
  this.getCodec = getCodec(init.codecs, init.loadCodec)
@@ -259,16 +258,16 @@ export class Helia<T extends Libp2p> implements HeliaInterface<T> {
259
258
  providerLookupConcurrency: init.providerLookupConcurrency
260
259
  })
261
260
 
261
+ components.blockBrokers = init.blockBrokers.map((fn) => {
262
+ return fn(components)
263
+ })
264
+
262
265
  const networkedStorage = new NetworkedStorage(components, init)
263
266
  this.pins = new PinsImpl(init.datastore, networkedStorage, this.getCodec)
264
267
  this.blockstore = new BlockStorage(networkedStorage, this.pins, {
265
268
  holdGcLock: init.holdGcLock ?? true
266
269
  })
267
270
  this.datastore = init.datastore
268
-
269
- components.blockBrokers = init.blockBrokers.map((fn) => {
270
- return fn(components)
271
- })
272
271
  }
273
272
 
274
273
  async start (): Promise<void> {