@helia/utils 1.4.0 → 2.0.0-d9051cdc

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 (40) hide show
  1. package/dist/index.min.js +2 -1
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/abstract-session.d.ts.map +1 -1
  4. package/dist/src/abstract-session.js +6 -4
  5. package/dist/src/abstract-session.js.map +1 -1
  6. package/dist/src/errors.d.ts +13 -0
  7. package/dist/src/errors.d.ts.map +1 -0
  8. package/dist/src/errors.js +13 -0
  9. package/dist/src/errors.js.map +1 -0
  10. package/dist/src/index.d.ts +9 -1
  11. package/dist/src/index.d.ts.map +1 -1
  12. package/dist/src/index.js +6 -2
  13. package/dist/src/index.js.map +1 -1
  14. package/dist/src/pins.d.ts.map +1 -1
  15. package/dist/src/pins.js +7 -4
  16. package/dist/src/pins.js.map +1 -1
  17. package/dist/src/routing.d.ts.map +1 -1
  18. package/dist/src/routing.js +1 -2
  19. package/dist/src/routing.js.map +1 -1
  20. package/dist/src/storage.d.ts +6 -9
  21. package/dist/src/storage.d.ts.map +1 -1
  22. package/dist/src/storage.js +5 -4
  23. package/dist/src/storage.js.map +1 -1
  24. package/dist/src/utils/datastore-version.d.ts.map +1 -1
  25. package/dist/src/utils/datastore-version.js +2 -1
  26. package/dist/src/utils/datastore-version.js.map +1 -1
  27. package/dist/src/utils/networked-storage.d.ts +11 -11
  28. package/dist/src/utils/networked-storage.d.ts.map +1 -1
  29. package/dist/src/utils/networked-storage.js +18 -14
  30. package/dist/src/utils/networked-storage.js.map +1 -1
  31. package/package.json +33 -38
  32. package/src/abstract-session.ts +8 -5
  33. package/src/errors.ts +14 -0
  34. package/src/index.ts +14 -3
  35. package/src/pins.ts +7 -4
  36. package/src/routing.ts +1 -2
  37. package/src/storage.ts +10 -9
  38. package/src/utils/datastore-version.ts +2 -1
  39. package/src/utils/networked-storage.ts +32 -23
  40. package/dist/typedoc-urls.json +0 -12
@@ -9,7 +9,7 @@ import { isPromise } from './is-promise.js'
9
9
  import type { HasherLoader } from '@helia/interface'
10
10
  import type { BlockBroker, Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, BlockRetrievalOptions, CreateSessionOptions, SessionBlockstore } from '@helia/interface/blocks'
11
11
  import type { AbortOptions, ComponentLogger, Logger, LoggerOptions, Startable } from '@libp2p/interface'
12
- import type { Blockstore } from 'interface-blockstore'
12
+ import type { Blockstore, InputPair } from 'interface-blockstore'
13
13
  import type { AwaitIterable } from 'interface-store'
14
14
  import type { CID } from 'multiformats/cid'
15
15
  import type { MultihashDigest, MultihashHasher } from 'multiformats/hashes/interface'
@@ -26,6 +26,12 @@ export interface StorageComponents {
26
26
  getHasher: HasherLoader
27
27
  }
28
28
 
29
+ export interface StorageInit {
30
+ maxIdentityHashDigestLength?: number
31
+ }
32
+
33
+ const DEFAULT_MAX_IDENTITY_HASH_DIGEST_LENGTH = 128
34
+
29
35
  class Storage implements Blockstore {
30
36
  protected readonly child: Blockstore
31
37
  protected readonly getHasher: HasherLoader
@@ -36,11 +42,13 @@ class Storage implements Blockstore {
36
42
  /**
37
43
  * Create a new BlockStorage
38
44
  */
39
- constructor (components: StorageComponents) {
45
+ constructor (components: StorageComponents, init: StorageInit = {}) {
40
46
  this.log = components.logger.forComponent('helia:networked-storage')
41
47
  this.logger = components.logger
42
48
  this.components = components
43
- this.child = new IdentityBlockstore(components.blockstore)
49
+ this.child = new IdentityBlockstore(components.blockstore, {
50
+ maxDigestLength: init.maxIdentityHashDigestLength ?? DEFAULT_MAX_IDENTITY_HASH_DIGEST_LENGTH
51
+ })
44
52
  this.getHasher = components.getHasher
45
53
  }
46
54
 
@@ -56,7 +64,7 @@ class Storage implements Blockstore {
56
64
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:put:providers:notify', cid))
57
65
 
58
66
  await Promise.all(
59
- this.components.blockBrokers.map(async broker => broker.announce?.(cid, block, options))
67
+ this.components.blockBrokers.map(async broker => broker.announce?.(cid, options))
60
68
  )
61
69
 
62
70
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:put:blockstore:put', cid))
@@ -67,7 +75,7 @@ class Storage implements Blockstore {
67
75
  /**
68
76
  * Put a multiple blocks to the underlying datastore
69
77
  */
70
- async * putMany (blocks: AwaitIterable<{ cid: CID, block: Uint8Array }>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
78
+ async * putMany (blocks: AwaitIterable<InputPair>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
71
79
  const missingBlocks = filter(blocks, async ({ cid }): Promise<boolean> => {
72
80
  const has = await this.child.has(cid, options)
73
81
 
@@ -78,10 +86,10 @@ class Storage implements Blockstore {
78
86
  return !has
79
87
  })
80
88
 
81
- const notifyEach = forEach(missingBlocks, async ({ cid, block }): Promise<void> => {
89
+ const notifyEach = forEach(missingBlocks, async ({ cid }): Promise<void> => {
82
90
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:put-many:providers:notify', cid))
83
91
  await Promise.all(
84
- this.components.blockBrokers.map(async broker => broker.announce?.(cid, block, options))
92
+ this.components.blockBrokers.map(async broker => broker.announce?.(cid, options))
85
93
  )
86
94
  })
87
95
 
@@ -92,7 +100,7 @@ class Storage implements Blockstore {
92
100
  /**
93
101
  * Get a block by cid
94
102
  */
95
- async get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): Promise<Uint8Array> {
103
+ async * get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): AsyncGenerator<Uint8Array> {
96
104
  if (options.offline !== true && !(await this.child.has(cid, options))) {
97
105
  const hasher = await this.getHasher(cid.multihash.code)
98
106
 
@@ -108,21 +116,22 @@ class Storage implements Blockstore {
108
116
  // notify other block providers of the new block
109
117
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:get:providers:notify', cid))
110
118
  await Promise.all(
111
- this.components.blockBrokers.map(async broker => broker.announce?.(cid, block, options))
119
+ this.components.blockBrokers.map(async broker => broker.announce?.(cid, options))
112
120
  )
113
121
 
114
- return block
122
+ yield block
123
+ return
115
124
  }
116
125
 
117
126
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:get:blockstore:get', cid))
118
127
 
119
- return this.child.get(cid, options)
128
+ yield * this.child.get(cid, options)
120
129
  }
121
130
 
122
131
  /**
123
132
  * Get multiple blocks back from an (async) iterable of cids
124
133
  */
125
- async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncIterable<Pair> {
134
+ async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
126
135
  options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:get-many'))
127
136
 
128
137
  yield * this.child.getMany(forEach(cids, async (cid): Promise<void> => {
@@ -141,7 +150,7 @@ class Storage implements Blockstore {
141
150
  // notify other block providers of the new block
142
151
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:get-many:providers:notify', cid))
143
152
  await Promise.all(
144
- this.components.blockBrokers.map(async broker => broker.announce?.(cid, block, options))
153
+ this.components.blockBrokers.map(async broker => broker.announce?.(cid, options))
145
154
  )
146
155
  }
147
156
  }))
@@ -159,7 +168,7 @@ class Storage implements Blockstore {
159
168
  /**
160
169
  * Delete multiple blocks from the blockstore
161
170
  */
162
- async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
171
+ async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
163
172
  options.onProgress?.(new CustomProgressEvent('blocks:delete-many:blockstore:delete-many'))
164
173
  yield * this.child.deleteMany((async function * (): AsyncGenerator<CID> {
165
174
  for await (const cid of cids) {
@@ -172,7 +181,7 @@ class Storage implements Blockstore {
172
181
  return this.child.has(cid, options)
173
182
  }
174
183
 
175
- async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AwaitIterable<Pair> {
184
+ async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
176
185
  options.onProgress?.(new CustomProgressEvent('blocks:get-all:blockstore:get-many'))
177
186
  yield * this.child.getAll(options)
178
187
  }
@@ -190,8 +199,8 @@ export class NetworkedStorage extends Storage implements Blocks, Startable {
190
199
  /**
191
200
  * Create a new BlockStorage
192
201
  */
193
- constructor (components: NetworkedStorageComponents) {
194
- super(components)
202
+ constructor (components: NetworkedStorageComponents, init: StorageInit = {}) {
203
+ super(components, init)
195
204
 
196
205
  this.started = false
197
206
  }
@@ -280,7 +289,7 @@ class SessionStorage extends Storage implements SessionBlockstore {
280
289
  /**
281
290
  * Put a multiple blocks to the underlying datastore
282
291
  */
283
- async * putMany (blocks: AwaitIterable<{ cid: CID, block: Uint8Array }>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
292
+ async * putMany (blocks: AwaitIterable<InputPair>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
284
293
  const signal = anySignal([this.closeController.signal, options.signal])
285
294
  setMaxListeners(Infinity, signal)
286
295
 
@@ -297,12 +306,12 @@ class SessionStorage extends Storage implements SessionBlockstore {
297
306
  /**
298
307
  * Get a block by cid
299
308
  */
300
- async get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): Promise<Uint8Array> {
309
+ async * get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): AsyncGenerator<Uint8Array> {
301
310
  const signal = anySignal([this.closeController.signal, options.signal])
302
311
  setMaxListeners(Infinity, signal)
303
312
 
304
313
  try {
305
- return await super.get(cid, {
314
+ yield * super.get(cid, {
306
315
  ...options,
307
316
  signal
308
317
  })
@@ -314,7 +323,7 @@ class SessionStorage extends Storage implements SessionBlockstore {
314
323
  /**
315
324
  * Get multiple blocks back from an (async) iterable of cids
316
325
  */
317
- async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncIterable<Pair> {
326
+ async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
318
327
  const signal = anySignal([this.closeController.signal, options.signal])
319
328
  setMaxListeners(Infinity, signal)
320
329
 
@@ -348,7 +357,7 @@ class SessionStorage extends Storage implements SessionBlockstore {
348
357
  /**
349
358
  * Delete multiple blocks from the blockstore
350
359
  */
351
- async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
360
+ async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
352
361
  const signal = anySignal([this.closeController.signal, options.signal])
353
362
  setMaxListeners(Infinity, signal)
354
363
 
@@ -376,7 +385,7 @@ class SessionStorage extends Storage implements SessionBlockstore {
376
385
  }
377
386
  }
378
387
 
379
- async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AwaitIterable<Pair> {
388
+ async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
380
389
  const signal = anySignal([this.closeController.signal, options.signal])
381
390
  setMaxListeners(Infinity, signal)
382
391
 
@@ -1,12 +0,0 @@
1
- {
2
- "AbstractSession": "https://ipfs.github.io/helia/classes/_helia_utils.AbstractSession.html",
3
- "Helia": "https://ipfs.github.io/helia/classes/_helia_utils.Helia.html",
4
- ".:Helia": "https://ipfs.github.io/helia/classes/_helia_utils.Helia.html",
5
- "AbstractCreateSessionOptions": "https://ipfs.github.io/helia/interfaces/_helia_utils.AbstractCreateSessionOptions.html",
6
- "AbstractSessionComponents": "https://ipfs.github.io/helia/interfaces/_helia_utils.AbstractSessionComponents.html",
7
- "BlockStorage": "https://ipfs.github.io/helia/interfaces/_helia_utils.BlockStorage.html",
8
- "BlockStorageInit": "https://ipfs.github.io/helia/interfaces/_helia_utils.BlockStorageInit.html",
9
- "BlockstoreSessionEvents": "https://ipfs.github.io/helia/interfaces/_helia_utils.BlockstoreSessionEvents.html",
10
- "HeliaInit": "https://ipfs.github.io/helia/interfaces/_helia_utils.HeliaInit.html",
11
- ".:HeliaInit": "https://ipfs.github.io/helia/interfaces/helia.HeliaInit.html"
12
- }