@helia/utils 2.5.2-1361bfa5 → 2.5.2-16d805e1

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 (77) hide show
  1. package/dist/index.min.js +1 -22
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/index.d.ts +5 -171
  4. package/dist/src/index.d.ts.map +1 -1
  5. package/dist/src/index.js +5 -162
  6. package/dist/src/index.js.map +1 -1
  7. package/dist/src/is-cid.d.ts.map +1 -0
  8. package/dist/src/is-cid.js.map +1 -0
  9. package/dist/src/is-promise.d.ts.map +1 -0
  10. package/dist/src/is-promise.js.map +1 -0
  11. package/package.json +7 -24
  12. package/src/index.ts +5 -368
  13. package/dist/src/pins.d.ts +0 -21
  14. package/dist/src/pins.d.ts.map +0 -1
  15. package/dist/src/pins.js +0 -141
  16. package/dist/src/pins.js.map +0 -1
  17. package/dist/src/routing.d.ts +0 -51
  18. package/dist/src/routing.d.ts.map +0 -1
  19. package/dist/src/routing.js +0 -311
  20. package/dist/src/routing.js.map +0 -1
  21. package/dist/src/storage.d.ts +0 -62
  22. package/dist/src/storage.d.ts.map +0 -1
  23. package/dist/src/storage.js +0 -159
  24. package/dist/src/storage.js.map +0 -1
  25. package/dist/src/utils/constants.d.ts +0 -4
  26. package/dist/src/utils/constants.d.ts.map +0 -1
  27. package/dist/src/utils/constants.js +0 -4
  28. package/dist/src/utils/constants.js.map +0 -1
  29. package/dist/src/utils/datastore-version.d.ts +0 -3
  30. package/dist/src/utils/datastore-version.d.ts.map +0 -1
  31. package/dist/src/utils/datastore-version.js +0 -20
  32. package/dist/src/utils/datastore-version.js.map +0 -1
  33. package/dist/src/utils/get-codec.d.ts +0 -4
  34. package/dist/src/utils/get-codec.d.ts.map +0 -1
  35. package/dist/src/utils/get-codec.js +0 -37
  36. package/dist/src/utils/get-codec.js.map +0 -1
  37. package/dist/src/utils/get-crypto.d.ts +0 -4
  38. package/dist/src/utils/get-crypto.d.ts.map +0 -1
  39. package/dist/src/utils/get-crypto.js +0 -35
  40. package/dist/src/utils/get-crypto.js.map +0 -1
  41. package/dist/src/utils/get-hasher.d.ts +0 -4
  42. package/dist/src/utils/get-hasher.d.ts.map +0 -1
  43. package/dist/src/utils/get-hasher.js +0 -32
  44. package/dist/src/utils/get-hasher.js.map +0 -1
  45. package/dist/src/utils/is-cid.d.ts.map +0 -1
  46. package/dist/src/utils/is-cid.js.map +0 -1
  47. package/dist/src/utils/is-promise.d.ts.map +0 -1
  48. package/dist/src/utils/is-promise.js.map +0 -1
  49. package/dist/src/utils/networked-storage.d.ts +0 -28
  50. package/dist/src/utils/networked-storage.d.ts.map +0 -1
  51. package/dist/src/utils/networked-storage.js +0 -52
  52. package/dist/src/utils/networked-storage.js.map +0 -1
  53. package/dist/src/utils/session-storage.d.ts +0 -47
  54. package/dist/src/utils/session-storage.d.ts.map +0 -1
  55. package/dist/src/utils/session-storage.js +0 -148
  56. package/dist/src/utils/session-storage.js.map +0 -1
  57. package/dist/src/utils/storage.d.ts +0 -55
  58. package/dist/src/utils/storage.d.ts.map +0 -1
  59. package/dist/src/utils/storage.js +0 -225
  60. package/dist/src/utils/storage.js.map +0 -1
  61. package/src/pins.ts +0 -209
  62. package/src/routing.ts +0 -389
  63. package/src/storage.ts +0 -194
  64. package/src/utils/constants.ts +0 -3
  65. package/src/utils/datastore-version.ts +0 -25
  66. package/src/utils/get-codec.ts +0 -45
  67. package/src/utils/get-crypto.ts +0 -44
  68. package/src/utils/get-hasher.ts +0 -40
  69. package/src/utils/networked-storage.ts +0 -74
  70. package/src/utils/session-storage.ts +0 -174
  71. package/src/utils/storage.ts +0 -294
  72. /package/dist/src/{utils/is-cid.d.ts → is-cid.d.ts} +0 -0
  73. /package/dist/src/{utils/is-cid.js → is-cid.js} +0 -0
  74. /package/dist/src/{utils/is-promise.d.ts → is-promise.d.ts} +0 -0
  75. /package/dist/src/{utils/is-promise.js → is-promise.js} +0 -0
  76. /package/src/{utils/is-cid.ts → is-cid.ts} +0 -0
  77. /package/src/{utils/is-promise.ts → is-promise.ts} +0 -0
package/src/routing.ts DELETED
@@ -1,389 +0,0 @@
1
- import { NoRoutersAvailableError } from '@helia/interface'
2
- import { NotFoundError, start, stop } from '@libp2p/interface'
3
- import { Queue } from '@libp2p/utils'
4
- import merge from 'it-merge'
5
- import { CustomProgressEvent } from 'progress-events'
6
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7
- import { GetFailedError } from './errors.ts'
8
- import type { Routing as RoutingInterface, Provider, RoutingOptions, RoutingFindProvidersProgressEvents, RoutingProvideProgressEvents, RoutingPutProgressEvents, RoutingGetProgressEvents, RoutingFindPeerProgressEvents, RoutingGetClosestPeersProgressEvents, RoutingCancelReprovideProgressEvents, Router, Peer } from '@helia/interface'
9
- import type { ComponentLogger, Logger, Metrics, Startable } from '@libp2p/interface'
10
- import type { AbortOptions } from 'abort-error'
11
- import type { CID } from 'multiformats/cid'
12
-
13
- const DEFAULT_PROVIDER_LOOKUP_CONCURRENCY = 5
14
-
15
- export interface RoutingInit {
16
- routers: Router[]
17
- providerLookupConcurrency?: number
18
- }
19
-
20
- export interface RoutingComponents {
21
- logger: ComponentLogger
22
- metrics?: Metrics
23
- }
24
-
25
- interface PeerQueueOptions extends AbortOptions {
26
- peer: CID
27
- }
28
-
29
- export class Routing implements RoutingInterface, Startable {
30
- public name: string
31
-
32
- private readonly log: Logger
33
- private readonly routers: Router[]
34
- private readonly providerLookupConcurrency: number
35
-
36
- constructor (components: RoutingComponents, init: RoutingInit) {
37
- this.name = 'helia'
38
- this.log = components.logger.forComponent('helia:routing')
39
- this.routers = init.routers ?? []
40
- this.providerLookupConcurrency = init.providerLookupConcurrency ?? DEFAULT_PROVIDER_LOOKUP_CONCURRENCY
41
-
42
- this.findProviders = components.metrics?.traceFunction('helia.routing.findProviders', this.findProviders.bind(this), {
43
- optionsIndex: 1
44
- }) ?? this.findProviders
45
- this.provide = components.metrics?.traceFunction('helia.routing.provide', this.provide.bind(this), {
46
- optionsIndex: 1
47
- }) ?? this.provide
48
- this.cancelReprovide = components.metrics?.traceFunction('helia.routing.cancelReprovide', this.cancelReprovide.bind(this), {
49
- optionsIndex: 1
50
- }) ?? this.cancelReprovide
51
- this.put = components.metrics?.traceFunction('helia.routing.put', this.put.bind(this), {
52
- optionsIndex: 2
53
- }) ?? this.put
54
- this.get = components.metrics?.traceFunction('helia.routing.get', this.get.bind(this), {
55
- optionsIndex: 1
56
- }) ?? this.get
57
- this.findPeer = components.metrics?.traceFunction('helia.routing.findPeer', this.findPeer.bind(this), {
58
- optionsIndex: 1
59
- }) ?? this.findPeer
60
- this.getClosestPeers = components.metrics?.traceFunction('helia.routing.getClosestPeers', this.getClosestPeers.bind(this), {
61
- optionsIndex: 1
62
- }) ?? this.getClosestPeers
63
- }
64
-
65
- async start (): Promise<void> {
66
- await start(...this.routers)
67
- }
68
-
69
- async stop (): Promise<void> {
70
- await stop(...this.routers)
71
- }
72
-
73
- hasRouter (name: string): boolean {
74
- return this.routers.findIndex(r => r.name === name) !== -1
75
- }
76
-
77
- addRouter (router: Router): void {
78
- this.routers.push(router)
79
- }
80
-
81
- /**
82
- * Iterates over all content routers in parallel to find providers of the
83
- * given key
84
- */
85
- async * findProviders (key: CID, options: RoutingOptions<RoutingFindProvidersProgressEvents> = {}): AsyncIterable<Provider> {
86
- if (this.routers.length === 0) {
87
- throw new NoRoutersAvailableError('No content routers available')
88
- }
89
-
90
- // provider multiaddrs are only cached for a limited time, so they can come
91
- // back as an empty array - when this happens we have to do a FIND_PEER
92
- // query to get updated addresses, but we shouldn't block on this so use a
93
- // separate bounded queue to perform this lookup
94
- const queue = new Queue<Provider | null, PeerQueueOptions>({
95
- concurrency: this.providerLookupConcurrency
96
- })
97
-
98
- let foundProviders = 0
99
- const errors: Error[] = []
100
- const self = this
101
- let routersFinished = 0
102
-
103
- this.log('findProviders for %c start using routers %s', key, this.routers.map(r => r.toString()).join(', '))
104
-
105
- const routers = supports(this.routers, 'findProviders')
106
- .map(async function * (router) {
107
- let foundProviders = 0
108
-
109
- options?.onProgress?.(new CustomProgressEvent('helia:routing:find-providers:start', {
110
- routing: router.name,
111
- cid: key
112
- }))
113
-
114
- try {
115
- for await (const prov of router.findProviders(key, options)) {
116
- foundProviders++
117
-
118
- // @ts-expect-error router.name is a string, needs to be specific
119
- options?.onProgress?.(new CustomProgressEvent('helia:routing:find-providers:provider', {
120
- routing: router.name,
121
- cid: key,
122
- provider: prov
123
- }))
124
-
125
- yield prov
126
- }
127
- } catch (err: any) {
128
- errors.push(err)
129
- } finally {
130
- self.log('router %s found %d providers for %c', router, foundProviders, key)
131
-
132
- options?.onProgress?.(new CustomProgressEvent('helia:routing:find-providers:end', {
133
- routing: router.name,
134
- cid: key,
135
- found: foundProviders
136
- }))
137
-
138
- routersFinished++
139
-
140
- // if all routers have finished and there are no jobs to find updated
141
- // peer multiaddres running or queued, cause the generator to exit
142
- if (routersFinished === routers.length && queue.size === 0) {
143
- queue.emitIdle()
144
- }
145
- }
146
- })
147
-
148
- for await (const peer of merge(
149
- queue.toGenerator(),
150
- ...routers)
151
- ) {
152
- // the peer was yielded by a content router without multiaddrs and we
153
- // failed to load them
154
- if (peer == null) {
155
- continue
156
- }
157
-
158
- // have to refresh peer info for this peer to get updated multiaddrs
159
- if (peer.multiaddrs.length === 0) {
160
- // already looking this peer up
161
- if (queue.queue.find(job => job.options.peer.equals(peer.id)) != null) {
162
- continue
163
- }
164
-
165
- queue.add(async () => {
166
- try {
167
- const provider = await this.findPeer(peer.id, options)
168
-
169
- if (provider.multiaddrs.length === 0) {
170
- return null
171
- }
172
-
173
- return {
174
- ...provider,
175
- protocols: peer.protocols,
176
- routing: peer.routing
177
- }
178
- } catch (err) {
179
- this.log.error('could not load multiaddrs for peer %p - %e', peer.id, err)
180
- return null
181
- }
182
- }, {
183
- peer: peer.id,
184
- signal: options.signal
185
- })
186
- .catch(err => {
187
- this.log.error('could not load multiaddrs for peer %p - %e', peer.id, err)
188
- })
189
-
190
- continue
191
- }
192
-
193
- foundProviders++
194
- yield peer
195
- }
196
-
197
- this.log('findProviders finished, found %d providers for %c', foundProviders, key)
198
- }
199
-
200
- /**
201
- * Iterates over all content routers in parallel to notify it is
202
- * a provider of the given key
203
- */
204
- async provide (key: CID, options: RoutingOptions<RoutingProvideProgressEvents> = {}): Promise<void> {
205
- if (this.routers.length === 0) {
206
- throw new NoRoutersAvailableError('No content routers available')
207
- }
208
-
209
- await Promise.all(
210
- supports(this.routers, 'provide')
211
- .map(async (router) => {
212
- options?.onProgress?.(new CustomProgressEvent('helia:routing:provide:start', {
213
- routing: router.name,
214
- cid: key
215
- }))
216
-
217
- await router.provide(key, options)
218
-
219
- options?.onProgress?.(new CustomProgressEvent('helia:routing:provide:end', {
220
- routing: router.name,
221
- cid: key
222
- }))
223
- })
224
- )
225
- }
226
-
227
- async cancelReprovide (key: CID, options: RoutingOptions<RoutingCancelReprovideProgressEvents> = {}): Promise<void> {
228
- await Promise.all(
229
- supports(this.routers, 'cancelReprovide')
230
- .map(async (router) => {
231
- options?.onProgress?.(new CustomProgressEvent('helia:routing:cancel-reprovide:start', {
232
- routing: router.name,
233
- cid: key
234
- }))
235
-
236
- await router.cancelReprovide(key, options)
237
-
238
- options?.onProgress?.(new CustomProgressEvent('helia:routing:cancel-reprovide:end', {
239
- routing: router.name,
240
- cid: key
241
- }))
242
- })
243
- )
244
- }
245
-
246
- /**
247
- * Store the given key/value pair in the available content routings
248
- */
249
- async put (key: Uint8Array, value: Uint8Array, options?: RoutingOptions<RoutingPutProgressEvents>): Promise<void> {
250
- await Promise.all(
251
- supports(this.routers, 'put')
252
- .map(async (router) => {
253
- options?.onProgress?.(new CustomProgressEvent('helia:routing:put:start', {
254
- routing: router.name,
255
- key,
256
- value
257
- }))
258
-
259
- await router.put(key, value, options)
260
-
261
- options?.onProgress?.(new CustomProgressEvent('helia:routing:put:end', {
262
- routing: router.name,
263
- key,
264
- value
265
- }))
266
- })
267
- )
268
- }
269
-
270
- /**
271
- * Get the value to the given key. The first value offered by any configured
272
- * router will be returned.
273
- */
274
- async get (key: Uint8Array, options?: RoutingOptions<RoutingGetProgressEvents>): Promise<Uint8Array> {
275
- const errors: Error[] = []
276
- let result: Uint8Array | undefined
277
-
278
- try {
279
- result = await Promise.any(
280
- supports(this.routers, 'get')
281
- .map(async (router) => {
282
- options?.onProgress?.(new CustomProgressEvent('helia:routing:get:start', {
283
- routing: router.name,
284
- key
285
- }))
286
-
287
- try {
288
- return await router.get(key, options)
289
- } catch (err: any) {
290
- this.log('router %s failed with %e', router, err)
291
- errors.push(err)
292
- } finally {
293
- options?.onProgress?.(new CustomProgressEvent('helia:routing:get:end', {
294
- routing: router.name,
295
- key
296
- }))
297
- }
298
- })
299
- )
300
- } catch {
301
- // ignore AggregateError as we will throw a better-named one
302
- }
303
-
304
- if (result == null) {
305
- throw new GetFailedError(errors, `Failed to get value key ${uint8ArrayToString(key, 'base58btc')}`)
306
- }
307
-
308
- return result
309
- }
310
-
311
- /**
312
- * Iterates over all peer routers in parallel to find the given peer
313
- */
314
- async findPeer (id: CID, options?: RoutingOptions<RoutingFindPeerProgressEvents>): Promise<Peer> {
315
- if (this.routers.length === 0) {
316
- throw new NoRoutersAvailableError('No peer routers available')
317
- }
318
-
319
- const self = this
320
- const source = merge(
321
- ...supports(this.routers, 'findPeer')
322
- .map(router => (async function * () {
323
- options?.onProgress?.(new CustomProgressEvent('helia:routing:find-peer:start', {
324
- routing: router.name,
325
- peerId: id
326
- }))
327
-
328
- try {
329
- yield await router.findPeer(id, options)
330
- } catch (err) {
331
- self.log.error(err)
332
- } finally {
333
- options?.onProgress?.(new CustomProgressEvent('helia:routing:find-peer:end', {
334
- routing: router.name,
335
- peerId: id
336
- }))
337
- }
338
- })())
339
- )
340
-
341
- for await (const peer of source) {
342
- if (peer == null) {
343
- continue
344
- }
345
-
346
- return peer
347
- }
348
-
349
- throw new NotFoundError('Could not find peer in routing')
350
- }
351
-
352
- /**
353
- * Attempt to find the closest peers on the network to the given key
354
- */
355
- async * getClosestPeers (key: Uint8Array, options: RoutingOptions<RoutingGetClosestPeersProgressEvents> = {}): AsyncIterable<Peer> {
356
- if (this.routers.length === 0) {
357
- throw new NoRoutersAvailableError('No peer routers available')
358
- }
359
-
360
- for await (const peer of merge(
361
- ...supports(this.routers, 'getClosestPeers')
362
- .map(async function * (router) {
363
- options?.onProgress?.(new CustomProgressEvent('helia:routing:get-closest-peers:start', {
364
- routing: router.name,
365
- key
366
- }))
367
-
368
- try {
369
- yield * router.getClosestPeers(key, options)
370
- } finally {
371
- options?.onProgress?.(new CustomProgressEvent('helia:routing:get-closest-peers:end', {
372
- routing: router.name,
373
- key
374
- }))
375
- }
376
- })
377
- )) {
378
- if (peer == null) {
379
- continue
380
- }
381
-
382
- yield peer
383
- }
384
- }
385
- }
386
-
387
- function supports <Operation extends keyof Routing> (routers: any[], key: Operation): Array<Pick<Routing, Operation | 'name'>> {
388
- return routers.filter(router => router[key] != null)
389
- }
package/src/storage.ts DELETED
@@ -1,194 +0,0 @@
1
- import { start, stop } from '@libp2p/interface'
2
- import createMortice from 'mortice'
3
- import { BlockPinnedError } from './errors.ts'
4
- import type { Routing } from '@helia/interface'
5
- import type { Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, SessionBlockstore } from '@helia/interface'
6
- import type { Pins } from '@helia/interface'
7
- import type { AbortOptions, Startable } from '@libp2p/interface'
8
- import type { Blockstore, InputPair } from 'interface-blockstore'
9
- import type { Mortice } from 'mortice'
10
- import type { CID } from 'multiformats/cid'
11
- import type { ProgressOptions } from 'progress-events'
12
-
13
- export interface BlockStorageInit {
14
- holdGcLock?: boolean
15
- }
16
-
17
- export interface GetOptions extends AbortOptions {
18
- progress?(evt: Event): void
19
- }
20
-
21
- /**
22
- * BlockStorage is a hybrid blockstore that puts/gets blocks from a configured
23
- * blockstore (that may be on disk, s3, or something else). If the blocks are
24
- * not present Bitswap will be used to fetch them from network peers.
25
- */
26
- export class BlockStorage implements Blocks, Startable {
27
- public lock: Mortice
28
- private readonly child: Blocks
29
- private readonly pins: Pins
30
- private readonly routing: Routing
31
- private started: boolean
32
-
33
- /**
34
- * Create a new BlockStorage
35
- */
36
- constructor (blockstore: Blocks, pins: Pins, routing: Routing, options: BlockStorageInit = {}) {
37
- this.child = blockstore
38
- this.pins = pins
39
- this.routing = routing
40
- this.lock = createMortice({
41
- singleProcess: options.holdGcLock
42
- })
43
- this.started = false
44
- }
45
-
46
- isStarted (): boolean {
47
- return this.started
48
- }
49
-
50
- async start (): Promise<void> {
51
- await start(this.child)
52
- this.started = true
53
- }
54
-
55
- async stop (): Promise<void> {
56
- await stop(this.child)
57
- this.started = false
58
- }
59
-
60
- unwrap (): Blockstore {
61
- return this.child
62
- }
63
-
64
- /**
65
- * Put a block to the underlying datastore
66
- */
67
- async put (cid: CID, block: Uint8Array, options: AbortOptions & ProgressOptions<PutBlockProgressEvents> = {}): Promise<CID> {
68
- options?.signal?.throwIfAborted()
69
- const releaseLock = await this.lock.readLock()
70
-
71
- try {
72
- return await this.child.put(cid, block, options)
73
- } finally {
74
- releaseLock()
75
- }
76
- }
77
-
78
- /**
79
- * Put a multiple blocks to the underlying datastore
80
- */
81
- async * putMany (blocks: Iterable<InputPair> | AsyncIterable<InputPair>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
82
- options?.signal?.throwIfAborted()
83
- const releaseLock = await this.lock.readLock()
84
-
85
- try {
86
- yield * this.child.putMany(blocks, options)
87
- } finally {
88
- releaseLock()
89
- }
90
- }
91
-
92
- /**
93
- * Get a block by cid
94
- */
95
- async * get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): AsyncGenerator<Uint8Array> {
96
- options?.signal?.throwIfAborted()
97
- const releaseLock = await this.lock.readLock()
98
-
99
- try {
100
- yield * this.child.get(cid, options)
101
- } finally {
102
- releaseLock()
103
- }
104
- }
105
-
106
- /**
107
- * Get multiple blocks back from an (async) iterable of cids
108
- */
109
- async * getMany (cids: Iterable<CID> | AsyncIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
110
- options?.signal?.throwIfAborted()
111
- const releaseLock = await this.lock.readLock()
112
-
113
- try {
114
- yield * this.child.getMany(cids, options)
115
- } finally {
116
- releaseLock()
117
- }
118
- }
119
-
120
- /**
121
- * Delete a block from the blockstore
122
- */
123
- async delete (cid: CID, options: AbortOptions & ProgressOptions<DeleteBlockProgressEvents> = {}): Promise<void> {
124
- options?.signal?.throwIfAborted()
125
- const releaseLock = await this.lock.writeLock()
126
-
127
- try {
128
- if (await this.pins.isPinned(cid)) {
129
- throw new BlockPinnedError('Block was pinned - please unpin and try again')
130
- }
131
-
132
- // stop re-providing this CID if necessary
133
- await this.routing.cancelReprovide(cid, options)
134
-
135
- await this.child.delete(cid, options)
136
- } finally {
137
- releaseLock()
138
- }
139
- }
140
-
141
- /**
142
- * Delete multiple blocks from the blockstore
143
- */
144
- async * deleteMany (cids: Iterable<CID> | AsyncIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
145
- options?.signal?.throwIfAborted()
146
- const releaseLock = await this.lock.writeLock()
147
-
148
- try {
149
- const storage = this
150
-
151
- yield * this.child.deleteMany((async function * (): AsyncGenerator<CID> {
152
- for await (const cid of cids) {
153
- if (await storage.pins.isPinned(cid)) {
154
- throw new BlockPinnedError('Block was pinned - please unpin and try again')
155
- }
156
-
157
- // stop re-providing this CID if necessary
158
- await storage.routing.cancelReprovide(cid, options)
159
-
160
- yield cid
161
- }
162
- }()), options)
163
- } finally {
164
- releaseLock()
165
- }
166
- }
167
-
168
- async has (cid: CID, options: AbortOptions = {}): Promise<boolean> {
169
- options?.signal?.throwIfAborted()
170
- const releaseLock = await this.lock.readLock()
171
-
172
- try {
173
- return await this.child.has(cid, options)
174
- } finally {
175
- releaseLock()
176
- }
177
- }
178
-
179
- async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
180
- options?.signal?.throwIfAborted()
181
- const releaseLock = await this.lock.readLock()
182
-
183
- try {
184
- yield * this.child.getAll(options)
185
- } finally {
186
- releaseLock()
187
- }
188
- }
189
-
190
- createSession (root: CID, options?: AbortOptions): SessionBlockstore {
191
- options?.signal?.throwIfAborted()
192
- return this.child.createSession(root, options)
193
- }
194
- }
@@ -1,3 +0,0 @@
1
- export const SALT_LENGTH = 16
2
- export const KEY_SIZE = 32
3
- export const ITERATIONS = 10000
@@ -1,25 +0,0 @@
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
- import { InvalidDatastoreVersionError } from '../errors.ts'
5
- import type { Datastore } from 'interface-datastore'
6
-
7
- const DS_VERSION_KEY = new Key('/version')
8
- const CURRENT_VERSION = 1
9
-
10
- export async function assertDatastoreVersionIsCurrent (datastore: Datastore): Promise<void> {
11
- if (!(await datastore.has(DS_VERSION_KEY))) {
12
- await datastore.put(DS_VERSION_KEY, uint8ArrayFromString(`${CURRENT_VERSION}`))
13
-
14
- return
15
- }
16
-
17
- const buf = await datastore.get(DS_VERSION_KEY)
18
- const str = uint8ArrayToString(buf)
19
- const version = parseInt(str, 10)
20
-
21
- if (version !== CURRENT_VERSION) {
22
- // TODO: write migrations when we break compatibility - for an example, see https://github.com/ipfs/js-ipfs-repo/tree/master/packages/ipfs-repo-migrations
23
- throw new InvalidDatastoreVersionError('Invalid datastore version, a datastore migration may be required')
24
- }
25
- }
@@ -1,45 +0,0 @@
1
- import { UnknownCodecError } from '@helia/interface'
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 json from 'multiformats/codecs/json'
6
- import * as raw from 'multiformats/codecs/raw'
7
- import { isPromise } from './is-promise.ts'
8
- import type { CodecLoader } from '@helia/interface'
9
- import type { BlockCodec } from 'multiformats/codecs/interface'
10
-
11
- export function getCodec (initialCodecs: Array<BlockCodec<any, any>> = [], loadCodec?: CodecLoader): CodecLoader {
12
- const codecs: Record<number, BlockCodec<any, any>> = {
13
- [dagPb.code]: dagPb,
14
- [raw.code]: raw,
15
- [dagCbor.code]: dagCbor,
16
- [dagJson.code]: dagJson,
17
- [json.code]: json
18
- }
19
-
20
- initialCodecs.forEach(codec => {
21
- codecs[codec.code] = codec
22
- })
23
-
24
- return async (code) => {
25
- let codec = codecs[code]
26
-
27
- if (codec == null && loadCodec != null) {
28
- const res = loadCodec(code)
29
-
30
- if (isPromise(res)) {
31
- codec = await res
32
- } else {
33
- codec = res
34
- }
35
-
36
- codecs[codec.code] = codec
37
- }
38
-
39
- if (codec != null) {
40
- return codec
41
- }
42
-
43
- throw new UnknownCodecError(`Could not load codec for ${code}`)
44
- }
45
- }
@@ -1,44 +0,0 @@
1
- import { UnknownCryptoError } from '@helia/interface'
2
- import { ecdsaCrypto, ed25519Crypto, rsaCrypto } from '@ipshipyard/crypto'
3
- import { isPromise } from './is-promise.ts'
4
- import type { CryptoLoader } from '@helia/interface'
5
- import type { Crypto } from '@ipshipyard/crypto'
6
-
7
- export function getCrypto (initialCryptos: Array<Crypto> = [], loadCrypto?: CryptoLoader): CryptoLoader {
8
- const cryptos: Record<string | number, Crypto> = {}
9
-
10
- initialCryptos = [
11
- ecdsaCrypto(),
12
- ed25519Crypto(),
13
- rsaCrypto(),
14
- ...initialCryptos
15
- ]
16
-
17
- initialCryptos.forEach(crypto => {
18
- cryptos[crypto.type] = crypto
19
- cryptos[crypto.code] = crypto
20
- })
21
-
22
- return async (nameOrCode) => {
23
- let crypto = cryptos[nameOrCode]
24
-
25
- if (crypto == null && loadCrypto != null) {
26
- const res = loadCrypto(nameOrCode)
27
-
28
- if (isPromise(res)) {
29
- crypto = await res
30
- } else {
31
- crypto = res
32
- }
33
-
34
- cryptos[crypto.type] = crypto
35
- cryptos[crypto.code] = crypto
36
- }
37
-
38
- if (crypto != null) {
39
- return crypto
40
- }
41
-
42
- throw new UnknownCryptoError(`Could not load crypto for ${crypto}`)
43
- }
44
- }