@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/index.ts CHANGED
@@ -1,368 +1,5 @@
1
- /**
2
- * @packageDocumentation
3
- *
4
- * This module contains utility code that is shared between various Helia
5
- * modules such as `helia`, `@helia/http`, etc.
6
- */
7
-
8
- import { keychain } from '@ipshipyard/keychain'
9
- import { start, stop, TypedEventEmitter } from '@libp2p/interface'
10
- import { dns } from '@multiformats/dns'
11
- import { defaultLogger } from 'birnam'
12
- import { MemoryBlockstore } from 'blockstore-core'
13
- import { MemoryDatastore } from 'datastore-core'
14
- import drain from 'it-drain'
15
- import { CustomProgressEvent } from 'progress-events'
16
- import { PinsImpl } from './pins.ts'
17
- import { Routing as RoutingClass } from './routing.ts'
18
- import { BlockStorage } from './storage.ts'
19
- import { assertDatastoreVersionIsCurrent } from './utils/datastore-version.ts'
20
- import { getCodec } from './utils/get-codec.ts'
21
- import { getCrypto } from './utils/get-crypto.ts'
22
- import { getHasher } from './utils/get-hasher.ts'
23
- import { NetworkedStorage } from './utils/networked-storage.ts'
24
- import type { BlockStorageInit } from './storage.ts'
25
- import type { CodecLoader, GCOptions, HasherLoader, Helia as HeliaInterface, HeliaEvents, Routing, CryptoLoader, Crypto, NodeInfo, Router, HeliaMixin } from '@helia/interface'
26
- import type { BlockBroker } from '@helia/interface'
27
- import type { Pins } from '@helia/interface'
28
- import type { Keychain, KeychainInit } from '@ipshipyard/keychain'
29
- import type { ComponentLogger, Logger, Metrics } from '@libp2p/interface'
30
- import type { DNS } from '@multiformats/dns'
31
- import type { Blockstore } from 'interface-blockstore'
32
- import type { Datastore } from 'interface-datastore'
33
- import type { BlockCodec } from 'multiformats'
34
- import type { CID } from 'multiformats/cid'
35
- import type { MultihashHasher } from 'multiformats/hashes/interface'
36
-
37
- export { AbstractSession } from './abstract-session.ts'
38
- export type { AbstractCreateSessionOptions, BlockstoreSessionEvents, AbstractSessionComponents } from './abstract-session.ts'
39
-
40
- export { isCID } from './utils/is-cid.ts'
41
-
42
- export type { BlockStorage, BlockStorageInit }
43
-
44
- export { breadthFirstWalker, depthFirstWalker, naturalOrderWalker } from './graph-walker.ts'
45
- export type { GraphWalkerComponents, GraphWalkerInit, GraphNode, GraphWalker } from './graph-walker.ts'
46
-
47
- /**
48
- * Options used to create a Helia node.
49
- */
50
- export interface HeliaInit {
51
- /**
52
- * By default Helia stores the node's PeerId in an encrypted form in a
53
- * libp2p keystore. These options control how that keystore is configured.
54
- */
55
- keychain?: KeychainInit
56
-
57
- /**
58
- * The blockstore is where blocks are stored
59
- */
60
- blockstore?: Blockstore
61
-
62
- /**
63
- * The datastore is where data is stored
64
- */
65
- datastore?: Datastore
66
-
67
- /**
68
- * By default sha256, sha512 and identity hashes are supported for
69
- * bitswap operations. To bitswap blocks with CIDs using other hashes
70
- * pass appropriate MultihashHashers here.
71
- */
72
- hashers?: MultihashHasher[]
73
-
74
- /**
75
- * An optional function that can load a MultihashHasher on demand. May return
76
- * a promise.
77
- */
78
- loadHasher?(code: number): MultihashHasher | Promise<MultihashHasher>
79
-
80
- /**
81
- * In order to pin CIDs that correspond to a DAG, it's necessary to know
82
- * how to traverse that DAG. DAGWalkers take a block and yield any CIDs
83
- * encoded within that block.
84
- */
85
- codecs?: Array<BlockCodec<any, any>>
86
-
87
- /**
88
- * An optional function that can load a BlockCodec on demand. May return a
89
- * promise.
90
- */
91
- loadCodec?(code: number): BlockCodec<any, any> | Promise<BlockCodec<any, any>>
92
-
93
- /**
94
- * A list of pre-supported public/private key implementations
95
- */
96
- cryptos?: Array<Crypto>
97
-
98
- /**
99
- * Dynamically load a cryptography implementation
100
- */
101
- loadCrypto?: CryptoLoader
102
-
103
- /**
104
- * Garbage collection requires preventing blockstore writes during searches
105
- * for unpinned blocks as DAGs are typically pinned after they've been
106
- * imported - without locking this could lead to the deletion of blocks while
107
- * they are being added to the blockstore.
108
- *
109
- * By default this lock is held on the current process and other processes
110
- * will contact this process for access.
111
- *
112
- * If Helia is being run in multiple processes, one process must hold the GC
113
- * lock so use this option to control which process that is.
114
- *
115
- * @default true
116
- */
117
- holdGcLock?: boolean
118
-
119
- /**
120
- * An optional logging component to pass to libp2p. If not specified the
121
- * default implementation from libp2p will be used.
122
- */
123
- logger?: ComponentLogger
124
-
125
- /**
126
- * A list of strategies used to fetch blocks when they are not present in
127
- * the local blockstore
128
- */
129
- blockBrokers?: Array<BlockBroker | ((components: any) => BlockBroker)>
130
-
131
- /**
132
- * Routers perform operations such as looking up content providers,
133
- * information about network peers or getting/putting records.
134
- */
135
- routers?: Array<Router | ((components: any) => Router)>
136
-
137
- /**
138
- * During provider lookups, peers can be returned from routing implementations
139
- * with no multiaddrs.
140
- *
141
- * This can happen when they've been retrieved from network peers that only
142
- * store multiaddrs for a limited amount of time.
143
- *
144
- * When this happens the peer's info has to be looked up with a further query.
145
- *
146
- * To not have this query block the yielding of other providers returned with
147
- * multiaddrs, a separate queue is used to perform this lookup.
148
- *
149
- * This config value controls the concurrency of that queue.
150
- *
151
- * @default 5
152
- */
153
- providerLookupConcurrency?: number
154
-
155
- /**
156
- * Components used by subclasses
157
- */
158
- components?: Record<string, any>
159
-
160
- /**
161
- * An optional DNS implementation used to perform queries for DNS records.
162
- */
163
- dns?: DNS
164
-
165
- /**
166
- * A metrics object that can be used to collected arbitrary stats about node
167
- * usage.
168
- */
169
- metrics?: Metrics
170
-
171
- /**
172
- * Limit the maximum supported size of identity hash digests to this value
173
- *
174
- * @default 128
175
- */
176
- maxIdentityHashDigestLength?: number
177
- }
178
-
179
- interface Components {
180
- blockstore: Blockstore
181
- datastore: Datastore
182
- logger: ComponentLogger
183
- blockBrokers: BlockBroker[]
184
- routing: Routing
185
- dns: DNS
186
- keychain: Keychain
187
- metrics?: Metrics
188
- getCodec: CodecLoader
189
- getHasher: HasherLoader
190
- getCrypto: CryptoLoader
191
- }
192
-
193
- export class Helia implements HeliaInterface {
194
- public info: NodeInfo
195
- public blockstore: BlockStorage
196
- public datastore: Datastore
197
- public events: TypedEventEmitter<HeliaEvents<this>>
198
- public pins: Pins
199
- public logger: ComponentLogger
200
- public routing: RoutingClass
201
- public getCodec: CodecLoader
202
- public getHasher: HasherLoader
203
- public getCrypto: CryptoLoader
204
- public dns: DNS
205
- public keychain: Keychain
206
- public metrics?: Metrics
207
- public status: 'stopped' | 'stopping' | 'starting' | 'started'
208
- private readonly log: Logger
209
- private readonly blockBrokers: BlockBroker[]
210
- private readonly mixins: HeliaMixin[]
211
-
212
- constructor (init: HeliaInit & { name: string, version: string }) {
213
- this.info = {
214
- name: init.name,
215
- version: init.version
216
- }
217
- this.logger = init.logger ?? defaultLogger()
218
- this.log = this.logger.forComponent('helia')
219
- this.getHasher = getHasher(init.hashers, init.loadHasher)
220
- this.getCodec = getCodec(init.codecs, init.loadCodec)
221
- this.getCrypto = getCrypto(init.cryptos, init.loadCrypto)
222
- this.dns = init.dns ?? dns()
223
- this.metrics = init.metrics
224
- this.events = new TypedEventEmitter<HeliaEvents<typeof this>>()
225
- this.status = 'stopped'
226
- this.mixins = []
227
-
228
- // @ts-expect-error routing and keychain are not set
229
- const components: Components = {
230
- blockstore: init.blockstore ?? new MemoryBlockstore(),
231
- datastore: init.datastore ?? new MemoryDatastore(),
232
- logger: this.logger,
233
- blockBrokers: [],
234
- getHasher: this.getHasher,
235
- getCodec: this.getCodec,
236
- getCrypto: this.getCrypto,
237
- dns: this.dns,
238
- metrics: this.metrics,
239
- ...(init.components ?? {})
240
- }
241
-
242
- this.keychain = components.keychain = keychain()(components)
243
-
244
- this.routing = components.routing = new RoutingClass(components, {
245
- routers: (init.routers ?? []).flatMap((router: Router | ((components: any) => Router)) => {
246
- if (typeof router === 'function') {
247
- router = router(components)
248
- }
249
-
250
- // if the router itself is a router
251
- const routers = [
252
- router
253
- ]
254
-
255
- return routers
256
- }),
257
- providerLookupConcurrency: init.providerLookupConcurrency
258
- })
259
-
260
- this.blockBrokers = components.blockBrokers = (init.blockBrokers ?? []).map((broker) => {
261
- if (typeof broker === 'function') {
262
- broker = broker(components)
263
- }
264
-
265
- return broker
266
- })
267
-
268
- const networkedStorage = new NetworkedStorage(components, init)
269
- this.pins = new PinsImpl(components.datastore, networkedStorage, this.getCodec)
270
- this.blockstore = new BlockStorage(networkedStorage, this.pins, this.routing, {
271
- holdGcLock: init.holdGcLock ?? true
272
- })
273
- this.datastore = components.datastore
274
- }
275
-
276
- hasRouter (name: string): boolean {
277
- return this.routing.hasRouter(name)
278
- }
279
-
280
- addRouter (router: Router): void {
281
- this.routing.addRouter(router)
282
- }
283
-
284
- hasBlockBroker (name: string): boolean {
285
- return this.blockBrokers.findIndex(b => b.name === name) !== -1
286
- }
287
-
288
- addBlockBroker (blockBroker: BlockBroker): void {
289
- this.blockBrokers.push(blockBroker)
290
- }
291
-
292
- addMixin (mixin: HeliaMixin): void {
293
- this.mixins.push(mixin)
294
- }
295
-
296
- async start (): Promise<this> {
297
- this.status = 'starting'
298
-
299
- await assertDatastoreVersionIsCurrent(this.datastore)
300
- await start(
301
- this.blockstore,
302
- this.datastore,
303
- this.routing,
304
- ...this.blockBrokers
305
- )
306
-
307
- for (const mixin of this.mixins) {
308
- await mixin.start?.(this)
309
- }
310
-
311
- this.status = 'started'
312
- this.events.dispatchEvent(new CustomEvent('start', { detail: this }))
313
-
314
- return this
315
- }
316
-
317
- async stop (): Promise<this> {
318
- this.status = 'stopping'
319
-
320
- for (const mixin of this.mixins) {
321
- await mixin.stop?.(this)
322
- }
323
-
324
- await stop(
325
- this.blockstore,
326
- this.datastore,
327
- this.routing,
328
- ...this.blockBrokers
329
- )
330
-
331
- this.status = 'stopped'
332
- this.events.dispatchEvent(new CustomEvent('stop', { detail: this }))
333
-
334
- return this
335
- }
336
-
337
- async gc (options: GCOptions = {}): Promise<void> {
338
- const releaseLock = await this.blockstore.lock.writeLock()
339
-
340
- try {
341
- const helia = this
342
- const blockstore = this.blockstore.unwrap()
343
-
344
- this.log('gc start')
345
-
346
- await drain(blockstore.deleteMany((async function * (): AsyncGenerator<CID> {
347
- for await (const { cid } of blockstore.getAll()) {
348
- try {
349
- if (await helia.pins.isPinned(cid, options)) {
350
- continue
351
- }
352
-
353
- yield cid
354
-
355
- options.onProgress?.(new CustomProgressEvent<CID>('helia:gc:deleted', cid))
356
- } catch (err: any) {
357
- helia.log.error('error during gc - %e', err)
358
- options.onProgress?.(new CustomProgressEvent<Error>('helia:gc:error', err))
359
- }
360
- }
361
- }())))
362
- } finally {
363
- releaseLock()
364
- }
365
-
366
- this.log('gc finished')
367
- }
368
- }
1
+ export * from './abstract-session.ts'
2
+ export * from './errors.ts'
3
+ export * from './graph-walker.ts'
4
+ export * from './is-cid.ts'
5
+ export * from './is-promise.ts'
@@ -1,21 +0,0 @@
1
- import { CID } from 'multiformats/cid';
2
- import type { CodecLoader } from '@helia/interface';
3
- import type { AddOptions, IsPinnedOptions, LsOptions, Pin, Pins, RmOptions } from '@helia/interface';
4
- import type { AbortOptions } from '@libp2p/interface';
5
- import type { Blockstore } from 'interface-blockstore';
6
- import type { Datastore } from 'interface-datastore';
7
- import type { Version } from 'multiformats/cid';
8
- export declare class PinsImpl implements Pins {
9
- #private;
10
- private readonly datastore;
11
- private readonly blockstore;
12
- private readonly getCodec;
13
- constructor(datastore: Datastore, blockstore: Blockstore, getCodec: CodecLoader);
14
- add(cid: CID<unknown, number, number, Version>, options?: AddOptions): AsyncGenerator<CID, void, undefined>;
15
- rm(cid: CID<unknown, number, number, Version>, options?: RmOptions): AsyncGenerator<CID, void, undefined>;
16
- ls(options?: LsOptions): AsyncGenerator<Pin, void, undefined>;
17
- isPinned(cid: CID, options?: IsPinnedOptions): Promise<boolean>;
18
- get(cid: CID, options?: AbortOptions): Promise<Pin>;
19
- setMetadata(cid: CID, metadata: Record<string, string | number | boolean> | undefined, options?: AbortOptions): Promise<void>;
20
- }
21
- //# sourceMappingURL=pins.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pins.d.ts","sourceRoot":"","sources":["../../src/pins.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAA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AG,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC;IAOpD,WAAW,CAAE,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;CASrI"}
package/dist/src/pins.js DELETED
@@ -1,141 +0,0 @@
1
- import { InvalidParametersError } from '@libp2p/interface';
2
- import * as cborg from 'cborg';
3
- import { Key } from 'interface-datastore';
4
- import { base36 } from 'multiformats/bases/base36';
5
- import { CID } from 'multiformats/cid';
6
- import { CustomProgressEvent } from 'progress-events';
7
- import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
8
- import { AlreadyPinnedError } from "./errors.js";
9
- import { depthFirstWalker } from "./graph-walker.js";
10
- const DATASTORE_PIN_PREFIX = '/pin/';
11
- const DATASTORE_BLOCK_PREFIX = '/pinned-block/';
12
- const DATASTORE_ENCODING = base36;
13
- function toDSKey(cid) {
14
- if (cid.version === 0) {
15
- cid = cid.toV1();
16
- }
17
- return new Key(`${DATASTORE_PIN_PREFIX}${cid.toString(DATASTORE_ENCODING)}`);
18
- }
19
- export class PinsImpl {
20
- datastore;
21
- blockstore;
22
- getCodec;
23
- constructor(datastore, blockstore, getCodec) {
24
- this.datastore = datastore;
25
- this.blockstore = blockstore;
26
- this.getCodec = getCodec;
27
- }
28
- async *add(cid, options = {}) {
29
- const pinKey = toDSKey(cid);
30
- if (await this.datastore.has(pinKey)) {
31
- throw new AlreadyPinnedError('Already pinned');
32
- }
33
- const depth = Math.round(options.depth ?? Infinity);
34
- if (depth < 0) {
35
- throw new InvalidParametersError('Depth must be greater than or equal to 0');
36
- }
37
- const walker = (options.walker ?? depthFirstWalker())({
38
- blockstore: this.blockstore,
39
- getCodec: this.getCodec
40
- });
41
- for await (const node of walker.walk(cid, { ...options, depth })) {
42
- const childCid = node.block.cid;
43
- await this.#updatePinnedBlock(childCid, (pinnedBlock) => {
44
- // do not update pinned block if this block is already pinned by this CID
45
- if (pinnedBlock.pinnedBy.find(c => uint8ArrayEquals(c, cid.bytes)) != null) {
46
- return false;
47
- }
48
- pinnedBlock.pinCount++;
49
- pinnedBlock.pinnedBy.push(cid.bytes);
50
- return true;
51
- }, options);
52
- yield childCid;
53
- }
54
- const pin = {
55
- depth,
56
- metadata: options.metadata ?? {}
57
- };
58
- await this.datastore.put(pinKey, cborg.encode(pin), options);
59
- }
60
- /**
61
- * Update the pin count for the CID
62
- */
63
- async #updatePinnedBlock(cid, withPinnedBlock, options) {
64
- const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`);
65
- let pinnedBlock = {
66
- pinCount: 0,
67
- pinnedBy: []
68
- };
69
- try {
70
- pinnedBlock = cborg.decode(await this.datastore.get(blockKey, options));
71
- }
72
- catch (err) {
73
- if (err.name !== 'NotFoundError') {
74
- throw err;
75
- }
76
- }
77
- const shouldContinue = withPinnedBlock(pinnedBlock);
78
- if (!shouldContinue) {
79
- return;
80
- }
81
- if (pinnedBlock.pinCount === 0) {
82
- if (await this.datastore.has(blockKey)) {
83
- await this.datastore.delete(blockKey);
84
- return;
85
- }
86
- }
87
- await this.datastore.put(blockKey, cborg.encode(pinnedBlock), options);
88
- options.onProgress?.(new CustomProgressEvent('helia:pin:add', cid));
89
- }
90
- async *rm(cid, options = {}) {
91
- const pinKey = toDSKey(cid);
92
- const buf = await this.datastore.get(pinKey, options);
93
- const pin = cborg.decode(buf);
94
- await this.datastore.delete(pinKey, options);
95
- const walker = (options.walker ?? depthFirstWalker())({
96
- blockstore: this.blockstore,
97
- getCodec: this.getCodec
98
- });
99
- for await (const node of walker.walk(cid, { ...options, depth: pin.depth })) {
100
- const childCid = node.block.cid;
101
- await this.#updatePinnedBlock(childCid, (pinnedBlock) => {
102
- pinnedBlock.pinCount--;
103
- pinnedBlock.pinnedBy = pinnedBlock.pinnedBy.filter(c => uint8ArrayEquals(c, cid.bytes));
104
- return true;
105
- }, {
106
- ...options,
107
- depth: pin.depth
108
- });
109
- yield childCid;
110
- }
111
- }
112
- async *ls(options = {}) {
113
- for await (const { key, value } of this.datastore.query({
114
- prefix: DATASTORE_PIN_PREFIX + (options.cid != null ? `${options.cid.toString(base36)}` : '')
115
- }, options)) {
116
- const cid = CID.parse(key.toString().substring(5), base36);
117
- const pin = cborg.decode(value);
118
- yield {
119
- cid,
120
- ...pin
121
- };
122
- }
123
- }
124
- async isPinned(cid, options = {}) {
125
- const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`);
126
- return this.datastore.has(blockKey, options);
127
- }
128
- async get(cid, options) {
129
- const pinKey = toDSKey(cid);
130
- const buf = await this.datastore.get(pinKey, options);
131
- return cborg.decode(buf);
132
- }
133
- async setMetadata(cid, metadata, options) {
134
- const pinKey = toDSKey(cid);
135
- const buf = await this.datastore.get(pinKey, options);
136
- const pin = cborg.decode(buf);
137
- pin.metadata = metadata ?? {};
138
- await this.datastore.put(pinKey, cborg.encode(pin), options);
139
- }
140
- }
141
- //# sourceMappingURL=pins.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pins.js","sourceRoot":"","sources":["../../src/pins.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAClD,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,kBA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,GAAG,QAAQ,IAAI,EAAE,CAAA;QAE7B,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAA;IAC9D,CAAC;CACF"}
@@ -1,51 +0,0 @@
1
- import type { Routing as RoutingInterface, Provider, RoutingOptions, RoutingFindProvidersProgressEvents, RoutingProvideProgressEvents, RoutingPutProgressEvents, RoutingGetProgressEvents, RoutingFindPeerProgressEvents, RoutingGetClosestPeersProgressEvents, RoutingCancelReprovideProgressEvents, Router, Peer } from '@helia/interface';
2
- import type { ComponentLogger, Metrics, Startable } from '@libp2p/interface';
3
- import type { CID } from 'multiformats/cid';
4
- export interface RoutingInit {
5
- routers: Router[];
6
- providerLookupConcurrency?: number;
7
- }
8
- export interface RoutingComponents {
9
- logger: ComponentLogger;
10
- metrics?: Metrics;
11
- }
12
- export declare class Routing implements RoutingInterface, Startable {
13
- name: string;
14
- private readonly log;
15
- private readonly routers;
16
- private readonly providerLookupConcurrency;
17
- constructor(components: RoutingComponents, init: RoutingInit);
18
- start(): Promise<void>;
19
- stop(): Promise<void>;
20
- hasRouter(name: string): boolean;
21
- addRouter(router: Router): void;
22
- /**
23
- * Iterates over all content routers in parallel to find providers of the
24
- * given key
25
- */
26
- findProviders(key: CID, options?: RoutingOptions<RoutingFindProvidersProgressEvents>): AsyncIterable<Provider>;
27
- /**
28
- * Iterates over all content routers in parallel to notify it is
29
- * a provider of the given key
30
- */
31
- provide(key: CID, options?: RoutingOptions<RoutingProvideProgressEvents>): Promise<void>;
32
- cancelReprovide(key: CID, options?: RoutingOptions<RoutingCancelReprovideProgressEvents>): Promise<void>;
33
- /**
34
- * Store the given key/value pair in the available content routings
35
- */
36
- put(key: Uint8Array, value: Uint8Array, options?: RoutingOptions<RoutingPutProgressEvents>): Promise<void>;
37
- /**
38
- * Get the value to the given key. The first value offered by any configured
39
- * router will be returned.
40
- */
41
- get(key: Uint8Array, options?: RoutingOptions<RoutingGetProgressEvents>): Promise<Uint8Array>;
42
- /**
43
- * Iterates over all peer routers in parallel to find the given peer
44
- */
45
- findPeer(id: CID, options?: RoutingOptions<RoutingFindPeerProgressEvents>): Promise<Peer>;
46
- /**
47
- * Attempt to find the closest peers on the network to the given key
48
- */
49
- getClosestPeers(key: Uint8Array, options?: RoutingOptions<RoutingGetClosestPeersProgressEvents>): AsyncIterable<Peer>;
50
- }
51
- //# sourceMappingURL=routing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"routing.d.ts","sourceRoot":"","sources":["../../src/routing.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,OAAO,IAAI,gBAAgB,EAAE,QAAQ,EAAE,cAAc,EAAE,kCAAkC,EAAE,4BAA4B,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,6BAA6B,EAAE,oCAAoC,EAAE,oCAAoC,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAC5U,OAAO,KAAK,EAAE,eAAe,EAAU,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAEpF,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAI3C,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,yBAAyB,CAAC,EAAE,MAAM,CAAA;CACnC;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,eAAe,CAAA;IACvB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAMD,qBAAa,OAAQ,YAAW,gBAAgB,EAAE,SAAS;IAClD,IAAI,EAAE,MAAM,CAAA;IAEnB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAClC,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAQ;gBAErC,UAAU,EAAE,iBAAiB,EAAE,IAAI,EAAE,WAAW;IA6BvD,KAA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