@helia/utils 1.3.2-a0266903 → 1.4.0-34d3ecd7

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.
package/src/pins.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { Queue } from '@libp2p/utils/queue'
1
+ import { Queue } from '@libp2p/utils'
2
2
  import * as cborg from 'cborg'
3
3
  import { Key } from 'interface-datastore'
4
+ import toBuffer from 'it-to-buffer'
4
5
  import { base36 } from 'multiformats/bases/base36'
5
6
  import { createUnsafe } from 'multiformats/block'
6
7
  import { CID } from 'multiformats/cid'
@@ -124,7 +125,7 @@ export class PinsImpl implements Pins {
124
125
  }
125
126
 
126
127
  const codec = await this.getCodec(cid.code)
127
- const bytes = await this.blockstore.get(cid, options)
128
+ const bytes = await toBuffer(this.blockstore.get(cid, options))
128
129
  const block = createUnsafe({ bytes, cid, codec })
129
130
 
130
131
  yield cid
package/src/routing.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { NoRoutersAvailableError } from '@helia/interface'
2
2
  import { NotFoundError, start, stop } from '@libp2p/interface'
3
- import { PeerQueue } from '@libp2p/utils/peer-queue'
3
+ import { PeerQueue } from '@libp2p/utils'
4
4
  import merge from 'it-merge'
5
5
  import type { Routing as RoutingInterface, Provider, RoutingOptions } from '@helia/interface'
6
6
  import type { AbortOptions, ComponentLogger, Logger, Metrics, PeerId, PeerInfo, Startable } from '@libp2p/interface'
@@ -75,7 +75,6 @@ export class Routing implements RoutingInterface, Startable {
75
75
  const queue = new PeerQueue<Provider | null>({
76
76
  concurrency: this.providerLookupConcurrency
77
77
  })
78
- queue.addEventListener('error', () => {})
79
78
 
80
79
  for await (const peer of merge(
81
80
  queue.toGenerator(),
package/src/storage.ts CHANGED
@@ -3,7 +3,7 @@ import createMortice from 'mortice'
3
3
  import type { Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, SessionBlockstore } from '@helia/interface/blocks'
4
4
  import type { Pins } from '@helia/interface/pins'
5
5
  import type { AbortOptions, Startable } from '@libp2p/interface'
6
- import type { Blockstore } from 'interface-blockstore'
6
+ import type { Blockstore, InputPair } from 'interface-blockstore'
7
7
  import type { AwaitIterable } from 'interface-store'
8
8
  import type { Mortice } from 'mortice'
9
9
  import type { CID } from 'multiformats/cid'
@@ -75,7 +75,7 @@ export class BlockStorage implements Blocks, Startable {
75
75
  /**
76
76
  * Put a multiple blocks to the underlying datastore
77
77
  */
78
- 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> {
79
79
  options?.signal?.throwIfAborted()
80
80
  const releaseLock = await this.lock.readLock()
81
81
 
@@ -89,12 +89,12 @@ export class BlockStorage implements Blocks, Startable {
89
89
  /**
90
90
  * Get a block by cid
91
91
  */
92
- async get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): Promise<Uint8Array> {
92
+ async * get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): AsyncGenerator<Uint8Array> {
93
93
  options?.signal?.throwIfAborted()
94
94
  const releaseLock = await this.lock.readLock()
95
95
 
96
96
  try {
97
- return await this.child.get(cid, options)
97
+ yield * this.child.get(cid, options)
98
98
  } finally {
99
99
  releaseLock()
100
100
  }
@@ -103,7 +103,7 @@ export class BlockStorage implements Blocks, Startable {
103
103
  /**
104
104
  * Get multiple blocks back from an (async) iterable of cids
105
105
  */
106
- async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncIterable<Pair> {
106
+ async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
107
107
  options?.signal?.throwIfAborted()
108
108
  const releaseLock = await this.lock.readLock()
109
109
 
@@ -135,7 +135,7 @@ export class BlockStorage implements Blocks, Startable {
135
135
  /**
136
136
  * Delete multiple blocks from the blockstore
137
137
  */
138
- async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
138
+ async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
139
139
  options?.signal?.throwIfAborted()
140
140
  const releaseLock = await this.lock.writeLock()
141
141
 
@@ -167,7 +167,7 @@ export class BlockStorage implements Blocks, Startable {
167
167
  }
168
168
  }
169
169
 
170
- async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncIterable<Pair> {
170
+ async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
171
171
  options?.signal?.throwIfAborted()
172
172
  const releaseLock = await this.lock.readLock()
173
173
 
@@ -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'
@@ -56,7 +56,7 @@ class Storage implements Blockstore {
56
56
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:put:providers:notify', cid))
57
57
 
58
58
  await Promise.all(
59
- this.components.blockBrokers.map(async broker => broker.announce?.(cid, block, options))
59
+ this.components.blockBrokers.map(async broker => broker.announce?.(cid, options))
60
60
  )
61
61
 
62
62
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:put:blockstore:put', cid))
@@ -67,7 +67,7 @@ class Storage implements Blockstore {
67
67
  /**
68
68
  * Put a multiple blocks to the underlying datastore
69
69
  */
70
- async * putMany (blocks: AwaitIterable<{ cid: CID, block: Uint8Array }>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
70
+ async * putMany (blocks: AwaitIterable<InputPair>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
71
71
  const missingBlocks = filter(blocks, async ({ cid }): Promise<boolean> => {
72
72
  const has = await this.child.has(cid, options)
73
73
 
@@ -78,10 +78,10 @@ class Storage implements Blockstore {
78
78
  return !has
79
79
  })
80
80
 
81
- const notifyEach = forEach(missingBlocks, async ({ cid, block }): Promise<void> => {
81
+ const notifyEach = forEach(missingBlocks, async ({ cid }): Promise<void> => {
82
82
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:put-many:providers:notify', cid))
83
83
  await Promise.all(
84
- this.components.blockBrokers.map(async broker => broker.announce?.(cid, block, options))
84
+ this.components.blockBrokers.map(async broker => broker.announce?.(cid, options))
85
85
  )
86
86
  })
87
87
 
@@ -92,7 +92,7 @@ class Storage implements Blockstore {
92
92
  /**
93
93
  * Get a block by cid
94
94
  */
95
- async get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): Promise<Uint8Array> {
95
+ async * get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): AsyncGenerator<Uint8Array> {
96
96
  if (options.offline !== true && !(await this.child.has(cid, options))) {
97
97
  const hasher = await this.getHasher(cid.multihash.code)
98
98
 
@@ -108,21 +108,22 @@ class Storage implements Blockstore {
108
108
  // notify other block providers of the new block
109
109
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:get:providers:notify', cid))
110
110
  await Promise.all(
111
- this.components.blockBrokers.map(async broker => broker.announce?.(cid, block, options))
111
+ this.components.blockBrokers.map(async broker => broker.announce?.(cid, options))
112
112
  )
113
113
 
114
- return block
114
+ yield block
115
+ return
115
116
  }
116
117
 
117
118
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:get:blockstore:get', cid))
118
119
 
119
- return this.child.get(cid, options)
120
+ yield * this.child.get(cid, options)
120
121
  }
121
122
 
122
123
  /**
123
124
  * Get multiple blocks back from an (async) iterable of cids
124
125
  */
125
- async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncIterable<Pair> {
126
+ async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
126
127
  options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:get-many'))
127
128
 
128
129
  yield * this.child.getMany(forEach(cids, async (cid): Promise<void> => {
@@ -141,7 +142,7 @@ class Storage implements Blockstore {
141
142
  // notify other block providers of the new block
142
143
  options.onProgress?.(new CustomProgressEvent<CID>('blocks:get-many:providers:notify', cid))
143
144
  await Promise.all(
144
- this.components.blockBrokers.map(async broker => broker.announce?.(cid, block, options))
145
+ this.components.blockBrokers.map(async broker => broker.announce?.(cid, options))
145
146
  )
146
147
  }
147
148
  }))
@@ -159,7 +160,7 @@ class Storage implements Blockstore {
159
160
  /**
160
161
  * Delete multiple blocks from the blockstore
161
162
  */
162
- async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
163
+ async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
163
164
  options.onProgress?.(new CustomProgressEvent('blocks:delete-many:blockstore:delete-many'))
164
165
  yield * this.child.deleteMany((async function * (): AsyncGenerator<CID> {
165
166
  for await (const cid of cids) {
@@ -172,7 +173,7 @@ class Storage implements Blockstore {
172
173
  return this.child.has(cid, options)
173
174
  }
174
175
 
175
- async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AwaitIterable<Pair> {
176
+ async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
176
177
  options.onProgress?.(new CustomProgressEvent('blocks:get-all:blockstore:get-many'))
177
178
  yield * this.child.getAll(options)
178
179
  }
@@ -280,7 +281,7 @@ class SessionStorage extends Storage implements SessionBlockstore {
280
281
  /**
281
282
  * Put a multiple blocks to the underlying datastore
282
283
  */
283
- async * putMany (blocks: AwaitIterable<{ cid: CID, block: Uint8Array }>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
284
+ async * putMany (blocks: AwaitIterable<InputPair>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
284
285
  const signal = anySignal([this.closeController.signal, options.signal])
285
286
  setMaxListeners(Infinity, signal)
286
287
 
@@ -297,12 +298,12 @@ class SessionStorage extends Storage implements SessionBlockstore {
297
298
  /**
298
299
  * Get a block by cid
299
300
  */
300
- async get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): Promise<Uint8Array> {
301
+ async * get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): AsyncGenerator<Uint8Array> {
301
302
  const signal = anySignal([this.closeController.signal, options.signal])
302
303
  setMaxListeners(Infinity, signal)
303
304
 
304
305
  try {
305
- return await super.get(cid, {
306
+ yield * super.get(cid, {
306
307
  ...options,
307
308
  signal
308
309
  })
@@ -314,7 +315,7 @@ class SessionStorage extends Storage implements SessionBlockstore {
314
315
  /**
315
316
  * Get multiple blocks back from an (async) iterable of cids
316
317
  */
317
- async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncIterable<Pair> {
318
+ async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
318
319
  const signal = anySignal([this.closeController.signal, options.signal])
319
320
  setMaxListeners(Infinity, signal)
320
321
 
@@ -348,7 +349,7 @@ class SessionStorage extends Storage implements SessionBlockstore {
348
349
  /**
349
350
  * Delete multiple blocks from the blockstore
350
351
  */
351
- async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncIterable<CID> {
352
+ async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
352
353
  const signal = anySignal([this.closeController.signal, options.signal])
353
354
  setMaxListeners(Infinity, signal)
354
355
 
@@ -376,7 +377,7 @@ class SessionStorage extends Storage implements SessionBlockstore {
376
377
  }
377
378
  }
378
379
 
379
- async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AwaitIterable<Pair> {
380
+ async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
380
381
  const signal = anySignal([this.closeController.signal, options.signal])
381
382
  setMaxListeners(Infinity, signal)
382
383