@helia/utils 2.5.2-070b7528 → 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 (79) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/abstract-session.d.ts +3 -3
  4. package/dist/src/abstract-session.d.ts.map +1 -1
  5. package/dist/src/abstract-session.js.map +1 -1
  6. package/dist/src/errors.d.ts +4 -0
  7. package/dist/src/errors.d.ts.map +1 -1
  8. package/dist/src/errors.js +4 -0
  9. package/dist/src/errors.js.map +1 -1
  10. package/dist/src/graph-walker.d.ts +3 -21
  11. package/dist/src/graph-walker.d.ts.map +1 -1
  12. package/dist/src/graph-walker.js +20 -17
  13. package/dist/src/graph-walker.js.map +1 -1
  14. package/dist/src/index.d.ts +5 -168
  15. package/dist/src/index.d.ts.map +1 -1
  16. package/dist/src/index.js +5 -130
  17. package/dist/src/index.js.map +1 -1
  18. package/dist/src/is-cid.d.ts +3 -0
  19. package/dist/src/is-cid.d.ts.map +1 -0
  20. package/dist/src/is-cid.js +8 -0
  21. package/dist/src/is-cid.js.map +1 -0
  22. package/dist/src/is-promise.d.ts.map +1 -0
  23. package/dist/src/is-promise.js.map +1 -0
  24. package/package.json +18 -37
  25. package/src/abstract-session.ts +4 -4
  26. package/src/errors.ts +5 -0
  27. package/src/graph-walker.ts +30 -43
  28. package/src/index.ts +5 -333
  29. package/src/is-cid.ts +9 -0
  30. package/dist/src/pins.d.ts +0 -21
  31. package/dist/src/pins.d.ts.map +0 -1
  32. package/dist/src/pins.js +0 -169
  33. package/dist/src/pins.js.map +0 -1
  34. package/dist/src/routing.d.ts +0 -49
  35. package/dist/src/routing.d.ts.map +0 -1
  36. package/dist/src/routing.js +0 -305
  37. package/dist/src/routing.js.map +0 -1
  38. package/dist/src/storage.d.ts +0 -63
  39. package/dist/src/storage.d.ts.map +0 -1
  40. package/dist/src/storage.js +0 -159
  41. package/dist/src/storage.js.map +0 -1
  42. package/dist/src/utils/datastore-version.d.ts +0 -3
  43. package/dist/src/utils/datastore-version.d.ts.map +0 -1
  44. package/dist/src/utils/datastore-version.js +0 -20
  45. package/dist/src/utils/datastore-version.js.map +0 -1
  46. package/dist/src/utils/get-codec.d.ts +0 -4
  47. package/dist/src/utils/get-codec.d.ts.map +0 -1
  48. package/dist/src/utils/get-codec.js +0 -38
  49. package/dist/src/utils/get-codec.js.map +0 -1
  50. package/dist/src/utils/get-hasher.d.ts +0 -4
  51. package/dist/src/utils/get-hasher.d.ts.map +0 -1
  52. package/dist/src/utils/get-hasher.js +0 -32
  53. package/dist/src/utils/get-hasher.js.map +0 -1
  54. package/dist/src/utils/is-promise.d.ts.map +0 -1
  55. package/dist/src/utils/is-promise.js.map +0 -1
  56. package/dist/src/utils/networked-storage.d.ts +0 -27
  57. package/dist/src/utils/networked-storage.d.ts.map +0 -1
  58. package/dist/src/utils/networked-storage.js +0 -52
  59. package/dist/src/utils/networked-storage.js.map +0 -1
  60. package/dist/src/utils/session-storage.d.ts +0 -48
  61. package/dist/src/utils/session-storage.d.ts.map +0 -1
  62. package/dist/src/utils/session-storage.js +0 -148
  63. package/dist/src/utils/session-storage.js.map +0 -1
  64. package/dist/src/utils/storage.d.ts +0 -56
  65. package/dist/src/utils/storage.d.ts.map +0 -1
  66. package/dist/src/utils/storage.js +0 -225
  67. package/dist/src/utils/storage.js.map +0 -1
  68. package/src/pins.ts +0 -247
  69. package/src/routing.ts +0 -376
  70. package/src/storage.ts +0 -195
  71. package/src/utils/datastore-version.ts +0 -25
  72. package/src/utils/get-codec.ts +0 -47
  73. package/src/utils/get-hasher.ts +0 -40
  74. package/src/utils/networked-storage.ts +0 -73
  75. package/src/utils/session-storage.ts +0 -175
  76. package/src/utils/storage.ts +0 -295
  77. /package/dist/src/{utils/is-promise.d.ts → is-promise.d.ts} +0 -0
  78. /package/dist/src/{utils/is-promise.js → is-promise.js} +0 -0
  79. /package/src/{utils/is-promise.ts → is-promise.ts} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helia/utils",
3
- "version": "2.5.2-070b7528",
3
+ "version": "2.5.2-16d805e1",
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",
@@ -48,49 +48,30 @@
48
48
  "test:electron-main": "aegir test -t electron-main"
49
49
  },
50
50
  "dependencies": {
51
- "@helia/interface": "6.2.1-070b7528",
52
- "@ipld/dag-cbor": "^9.2.6",
53
- "@ipld/dag-json": "^10.2.7",
54
- "@ipld/dag-pb": "^4.1.5",
55
- "@libp2p/interface": "^3.2.0",
56
- "@libp2p/keychain": "^6.0.12",
57
- "@libp2p/utils": "^7.0.15",
58
- "@multiformats/dns": "^1.0.13",
59
- "@multiformats/multiaddr": "^13.0.1",
60
- "any-signal": "^4.2.0",
61
- "blockstore-core": "^6.1.3",
62
- "cborg": "^5.1.0",
63
- "interface-blockstore": "^6.0.2",
64
- "interface-datastore": "^9.0.3",
65
- "interface-store": "^7.0.2",
66
- "it-drain": "^3.0.12",
67
- "it-filter": "^3.1.5",
68
- "it-foreach": "^2.1.6",
69
- "it-merge": "^3.0.13",
70
- "it-to-buffer": "^4.0.12",
71
- "libp2p": "^3.2.0",
72
- "mortice": "^3.3.1",
73
- "multiformats": "^13.4.2",
51
+ "@helia/interface": "6.2.1-16d805e1",
52
+ "@libp2p/interface": "^3.2.3",
53
+ "@libp2p/utils": "^7.2.2",
54
+ "@multiformats/multiaddr": "^13.0.3",
55
+ "it-filter": "^3.1.6",
56
+ "it-to-buffer": "^5.0.0",
57
+ "multiformats": "^14.0.0",
74
58
  "p-defer": "^4.0.1",
75
59
  "progress-events": "^1.1.0",
76
- "race-signal": "^2.0.0",
77
- "uint8arrays": "^5.1.0"
60
+ "race-signal": "^2.0.0"
78
61
  },
79
62
  "devDependencies": {
80
- "@libp2p/crypto": "^5.1.15",
81
- "@libp2p/logger": "^6.2.4",
82
- "@libp2p/peer-id": "^6.0.6",
63
+ "@ipld/dag-cbor": "^10.0.1",
64
+ "@libp2p/crypto": "^5.1.19",
65
+ "@libp2p/logger": "^6.2.8",
66
+ "@libp2p/peer-id": "^6.0.10",
83
67
  "@types/sinon": "^21.0.1",
84
- "aegir": "^48.0.4",
85
- "datastore-core": "^11.0.3",
68
+ "aegir": "^48.0.11",
69
+ "blockstore-core": "^7.0.1",
86
70
  "delay": "^7.0.0",
71
+ "interface-blockstore": "^7.0.1",
87
72
  "it-all": "^3.0.11",
88
- "it-map": "^3.1.5",
89
- "sinon": "^22.0.0",
90
- "sinon-ts": "^2.0.0"
91
- },
92
- "browser": {
93
- "./dist/src/utils/libp2p-defaults.js": "./dist/src/utils/libp2p-defaults.browser.js"
73
+ "it-map": "^3.1.6",
74
+ "sinon": "^22.0.0"
94
75
  },
95
76
  "sideEffects": false
96
77
  }
@@ -5,7 +5,7 @@ import { base64 } from 'multiformats/bases/base64'
5
5
  import pDefer from 'p-defer'
6
6
  import { raceSignal } from 'race-signal'
7
7
  import type { BlockBroker, BlockRetrievalOptions, CreateSessionOptions } from '@helia/interface'
8
- import type { AbortOptions, ComponentLogger, Logger, PeerId } from '@libp2p/interface'
8
+ import type { AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
9
9
  import type { Filter } from '@libp2p/utils'
10
10
  import type { Multiaddr } from '@multiformats/multiaddr'
11
11
  import type { CID } from 'multiformats/cid'
@@ -41,7 +41,7 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
41
41
  private readonly maxProviders: number
42
42
  public readonly providers: Provider[]
43
43
  private readonly evictionFilter: Filter
44
- private readonly initialProviders: Array<PeerId | Multiaddr | Multiaddr[]>
44
+ private readonly initialProviders: Array<CID | Multiaddr | Multiaddr[]>
45
45
  private readonly cidPeerFilterSize: number
46
46
 
47
47
  constructor (components: AbstractSessionComponents, init: AbstractCreateSessionOptions) {
@@ -277,7 +277,7 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
277
277
  return false
278
278
  }
279
279
 
280
- async addPeer (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void> {
280
+ async addPeer (peer: CID | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void> {
281
281
  const provider = await this.convertToProvider(peer, 'manually-added', options)
282
282
 
283
283
  if (provider == null || this.hasProvider(provider)) {
@@ -379,7 +379,7 @@ export abstract class AbstractSession<Provider, RetrieveBlockProgressEvents exte
379
379
  * into the format required or return `undefined` if the provider is not
380
380
  * compatible with this session implementation
381
381
  */
382
- abstract convertToProvider (provider: PeerId | Multiaddr | Multiaddr[], routing: string, options?: AbortOptions): Promise<Provider | undefined>
382
+ abstract convertToProvider (provider: CID | Multiaddr | Multiaddr[], routing: string, options?: AbortOptions): Promise<Provider | undefined>
383
383
 
384
384
  /**
385
385
  * This method should search for new providers and yield them.
package/src/errors.ts CHANGED
@@ -32,3 +32,8 @@ export class BlockNotFoundWhileOfflineError extends Error {
32
32
  static name = 'BlockNotFoundWhileOfflineError'
33
33
  name = 'BlockNotFoundWhileOfflineError'
34
34
  }
35
+
36
+ export class DecryptionFailedError extends Error {
37
+ static name = 'DecryptionFailedError'
38
+ name = 'DecryptionFailedError'
39
+ }
@@ -2,29 +2,17 @@ import { Queue } from '@libp2p/utils'
2
2
  import filter from 'it-filter'
3
3
  import toBuffer from 'it-to-buffer'
4
4
  import { createUnsafe } from 'multiformats/block'
5
- import type { CodecLoader } from '@helia/interface'
5
+ import type {
6
+ GraphWalker,
7
+ GraphWalkerComponents,
8
+ GraphWalkerInit,
9
+ GraphNode,
10
+ WalkOptions
11
+ } from '@helia/interface'
6
12
  import type { AbortOptions } from '@libp2p/interface'
7
- import type { Blockstore } from 'interface-blockstore'
8
- import type { BlockView, CID, Version } from 'multiformats'
13
+ import type { CID } from 'multiformats'
9
14
 
10
- export interface GraphWalkerComponents {
11
- blockstore: Blockstore
12
- getCodec: CodecLoader
13
- }
14
-
15
- export interface GraphWalkerInit {
16
-
17
- }
18
-
19
- export interface GraphNode <T = unknown, C extends number = number, A extends number = number, V extends Version = 0 | 1> {
20
- block: BlockView<T, C, A, V>
21
- depth: number
22
- path: CID[]
23
- }
24
-
25
- export interface GraphWalker {
26
- walk <T = any> (cid: CID, options?: WalkOptions<T>): AsyncGenerator<GraphNode<T>>
27
- }
15
+ export type { GraphWalker, GraphWalkerComponents, GraphWalkerInit, GraphNode, WalkOptions }
28
16
 
29
17
  /**
30
18
  * A depth-first walker descends into child blocks before processing successor
@@ -55,10 +43,6 @@ interface JobOptions extends AbortOptions {
55
43
  path: CID[]
56
44
  }
57
45
 
58
- export interface WalkOptions<T> extends AbortOptions {
59
- includeChild?(child: CID, parent: BlockView<T, number, number, 0 | 1>): boolean
60
- }
61
-
62
46
  abstract class AbstractGraphWalker {
63
47
  private readonly components: GraphWalkerComponents
64
48
 
@@ -70,6 +54,7 @@ abstract class AbstractGraphWalker {
70
54
  const queue = this.getQueue()
71
55
  const gen = filter(queue.toGenerator(options), (node) => node != null) as AsyncGenerator<GraphNode<T>>
72
56
  let finished = false
57
+ const maxDepth = options?.depth ?? Infinity
73
58
 
74
59
  const job = async (opts: JobOptions): Promise<GraphNode<T> | undefined> => {
75
60
  const cid = opts.cid
@@ -80,25 +65,27 @@ abstract class AbstractGraphWalker {
80
65
  codec: await this.components.getCodec(cid.code)
81
66
  })
82
67
 
83
- for (const [, linkedCid] of block.links()) {
84
- if (options?.includeChild?.(linkedCid, block) === false) {
85
- continue
86
- }
87
-
88
- queue.add(job, {
89
- ...opts,
90
- cid: linkedCid,
91
- depth: opts.depth + 1,
92
- path: [...opts.path, linkedCid]
93
- })
94
- // eslint-disable-next-line no-loop-func
95
- .catch(err => {
96
- // only throw if the generator is still yielding results, otherwise
97
- // it can cause unhandled promise rejections
98
- if (!finished) {
99
- gen.throw(err)
100
- }
68
+ if (opts.depth < maxDepth) {
69
+ for (const [, linkedCid] of block.links()) {
70
+ if (options?.includeChild?.(linkedCid, block) === false) {
71
+ continue
72
+ }
73
+
74
+ queue.add(job, {
75
+ ...opts,
76
+ cid: linkedCid,
77
+ depth: opts.depth + 1,
78
+ path: [...opts.path, linkedCid]
101
79
  })
80
+ // eslint-disable-next-line no-loop-func
81
+ .catch(err => {
82
+ // only throw if the generator is still yielding results, otherwise
83
+ // it can cause unhandled promise rejections
84
+ if (!finished) {
85
+ gen.throw(err)
86
+ }
87
+ })
88
+ }
102
89
  }
103
90
 
104
91
  return {
package/src/index.ts CHANGED
@@ -1,333 +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 { contentRoutingSymbol, peerRoutingSymbol, start, stop, TypedEventEmitter } from '@libp2p/interface'
9
- import { dns } from '@multiformats/dns'
10
- import drain from 'it-drain'
11
- import { CustomProgressEvent } from 'progress-events'
12
- import { PinsImpl } from './pins.ts'
13
- import { Routing as RoutingClass } from './routing.ts'
14
- import { BlockStorage } from './storage.ts'
15
- import { assertDatastoreVersionIsCurrent } from './utils/datastore-version.ts'
16
- import { getCodec } from './utils/get-codec.ts'
17
- import { getHasher } from './utils/get-hasher.ts'
18
- import { NetworkedStorage } from './utils/networked-storage.ts'
19
- import type { BlockStorageInit } from './storage.ts'
20
- import type { Await, CodecLoader, GCOptions, HasherLoader, Helia as HeliaInterface, HeliaEvents, Routing } from '@helia/interface'
21
- import type { BlockBroker } from '@helia/interface/blocks'
22
- import type { Pins } from '@helia/interface/pins'
23
- import type { ComponentLogger, ContentRouting, Libp2p, Logger, Metrics, PeerRouting } from '@libp2p/interface'
24
- import type { KeychainInit } from '@libp2p/keychain'
25
- import type { DNS } from '@multiformats/dns'
26
- import type { Blockstore } from 'interface-blockstore'
27
- import type { Datastore } from 'interface-datastore'
28
- import type { Libp2pOptions } from 'libp2p'
29
- import type { BlockCodec } from 'multiformats'
30
- import type { CID } from 'multiformats/cid'
31
- import type { MultihashHasher } from 'multiformats/hashes/interface'
32
-
33
- export { AbstractSession } from './abstract-session.ts'
34
- export type { AbstractCreateSessionOptions, BlockstoreSessionEvents, AbstractSessionComponents } from './abstract-session.ts'
35
-
36
- export type { BlockStorage, BlockStorageInit }
37
-
38
- export { breadthFirstWalker, depthFirstWalker, naturalOrderWalker } from './graph-walker.ts'
39
- export type { GraphWalkerComponents, GraphWalkerInit, GraphNode, GraphWalker } from './graph-walker.ts'
40
-
41
- /**
42
- * Options used to create a Helia node.
43
- */
44
- export interface HeliaInit<T extends Libp2p = Libp2p> {
45
- /**
46
- * A libp2p node is required to perform network operations. Either a
47
- * pre-configured node or options to configure a node can be passed
48
- * here.
49
- *
50
- * If node options are passed, they will be merged with the default
51
- * config for the current platform. In this case all passed config
52
- * keys will replace those from the default config.
53
- *
54
- * The libp2p `start` option is not supported, instead please pass `start` in
55
- * the root of the HeliaInit object.
56
- */
57
- libp2p: T | Omit<Libp2pOptions<any>, 'start'>
58
-
59
- /**
60
- * Pass `false` to not start the Helia node
61
- */
62
- start?: boolean
63
-
64
- /**
65
- * By default Helia stores the node's PeerId in an encrypted form in a
66
- * libp2p keystore. These options control how that keystore is configured.
67
- */
68
- keychain?: KeychainInit
69
-
70
- /**
71
- * The blockstore is where blocks are stored
72
- */
73
- blockstore: Blockstore
74
-
75
- /**
76
- * The datastore is where data is stored
77
- */
78
- datastore: Datastore
79
-
80
- /**
81
- * By default sha256, sha512 and identity hashes are supported for
82
- * bitswap operations. To bitswap blocks with CIDs using other hashes
83
- * pass appropriate MultihashHashers here.
84
- */
85
- hashers?: MultihashHasher[]
86
-
87
- /**
88
- * An optional function that can load a MultihashHasher on demand. May return
89
- * a promise.
90
- */
91
- loadHasher?(code: number): Await<MultihashHasher>
92
-
93
- /**
94
- * In order to pin CIDs that correspond to a DAG, it's necessary to know
95
- * how to traverse that DAG. DAGWalkers take a block and yield any CIDs
96
- * encoded within that block.
97
- */
98
- codecs?: Array<BlockCodec<any, any>>
99
-
100
- /**
101
- * An optional function that can load a BlockCodec on demand. May return a
102
- * promise.
103
- */
104
- loadCodec?(code: number): Await<BlockCodec<any, any>>
105
-
106
- /**
107
- * A list of strategies used to fetch blocks when they are not present in
108
- * the local blockstore
109
- */
110
- blockBrokers: Array<(components: any) => BlockBroker>
111
-
112
- /**
113
- * Garbage collection requires preventing blockstore writes during searches
114
- * for unpinned blocks as DAGs are typically pinned after they've been
115
- * imported - without locking this could lead to the deletion of blocks while
116
- * they are being added to the blockstore.
117
- *
118
- * By default this lock is held on the current process and other processes
119
- * will contact this process for access.
120
- *
121
- * If Helia is being run in multiple processes, one process must hold the GC
122
- * lock so use this option to control which process that is.
123
- *
124
- * @default true
125
- */
126
- holdGcLock?: boolean
127
-
128
- /**
129
- * An optional logging component to pass to libp2p. If not specified the
130
- * default implementation from libp2p will be used.
131
- */
132
- logger?: ComponentLogger
133
-
134
- /**
135
- * Routers perform operations such as looking up content providers,
136
- * information about network peers or getting/putting records.
137
- */
138
- routers?: Array<Partial<Routing> | ((components: any) => Partial<Routing>)>
139
-
140
- /**
141
- * During provider lookups, peers can be returned from routing implementations
142
- * with no multiaddrs.
143
- *
144
- * This can happen when they've been retrieved from network peers that only
145
- * store multiaddrs for a limited amount of time.
146
- *
147
- * When this happens the peer's info has to be looked up with a further query.
148
- *
149
- * To not have this query block the yielding of other providers returned with
150
- * multiaddrs, a separate queue is used to perform this lookup.
151
- *
152
- * This config value controls the concurrency of that queue.
153
- *
154
- * @default 5
155
- */
156
- providerLookupConcurrency?: number
157
-
158
- /**
159
- * Components used by subclasses
160
- */
161
- components?: Record<string, any>
162
-
163
- /**
164
- * An optional DNS implementation used to perform queries for DNS records.
165
- */
166
- dns?: DNS
167
-
168
- /**
169
- * A metrics object that can be used to collected arbitrary stats about node
170
- * usage.
171
- */
172
- metrics?: Metrics
173
-
174
- /**
175
- * Limit the maximum supported size of identity hash digests to this value
176
- *
177
- * @default 128
178
- */
179
- maxIdentityHashDigestLength?: number
180
- }
181
-
182
- interface Components {
183
- libp2p: Libp2p
184
- blockstore: Blockstore
185
- datastore: Datastore
186
- logger: ComponentLogger
187
- blockBrokers: BlockBroker[]
188
- routing: Routing
189
- dns: DNS
190
- metrics?: Metrics
191
- getCodec: CodecLoader
192
- getHasher: HasherLoader
193
- }
194
-
195
- export class Helia<T extends Libp2p> implements HeliaInterface<T> {
196
- public libp2p: T
197
- public blockstore: BlockStorage
198
- public datastore: Datastore
199
- public events: TypedEventEmitter<HeliaEvents<T>>
200
- public pins: Pins
201
- public logger: ComponentLogger
202
- public routing: Routing
203
- public getCodec: CodecLoader
204
- public getHasher: HasherLoader
205
- public dns: DNS
206
- public metrics?: Metrics
207
- private readonly log: Logger
208
-
209
- constructor (init: Omit<HeliaInit, 'start' | 'libp2p'> & { libp2p: T }) {
210
- this.logger = init.logger ?? init.libp2p.logger
211
- this.log = this.logger.forComponent('helia')
212
- this.getHasher = getHasher(init.hashers, init.loadHasher)
213
- this.getCodec = getCodec(init.codecs, init.loadCodec)
214
- this.dns = init.dns ?? dns()
215
- this.metrics = init.metrics
216
- this.libp2p = init.libp2p
217
- this.events = new TypedEventEmitter<HeliaEvents<T>>()
218
-
219
- // @ts-expect-error routing is not set
220
- const components: Components = {
221
- blockstore: init.blockstore,
222
- datastore: init.datastore,
223
- logger: this.logger,
224
- libp2p: this.libp2p,
225
- blockBrokers: [],
226
- getHasher: this.getHasher,
227
- getCodec: this.getCodec,
228
- dns: this.dns,
229
- metrics: this.metrics,
230
- ...(init.components ?? {})
231
- }
232
-
233
- this.routing = components.routing = new RoutingClass(components, {
234
- routers: (init.routers ?? []).flatMap((router: Partial<Routing> | ((components: any) => Partial<Routing>)) => {
235
- if (typeof router === 'function') {
236
- router = router(components)
237
- }
238
-
239
- // if the router itself is a router
240
- const routers = [
241
- router
242
- ]
243
-
244
- // if the router provides a libp2p-style ContentRouter
245
- const contentRouting = asContentRouting(router)
246
- if (contentRouting != null) {
247
- routers.push(contentRouting)
248
- }
249
-
250
- // if the router provides a libp2p-style PeerRouter
251
- const peerRouting = asPeerRouting(router)
252
- if (peerRouting != null) {
253
- routers.push(peerRouting)
254
- }
255
-
256
- return routers
257
- }),
258
- providerLookupConcurrency: init.providerLookupConcurrency
259
- })
260
-
261
- components.blockBrokers = init.blockBrokers.map((fn) => {
262
- return fn(components)
263
- })
264
-
265
- const networkedStorage = new NetworkedStorage(components, init)
266
- this.pins = new PinsImpl(init.datastore, networkedStorage, this.getCodec)
267
- this.blockstore = new BlockStorage(networkedStorage, this.pins, this.routing, {
268
- holdGcLock: init.holdGcLock ?? true
269
- })
270
- this.datastore = init.datastore
271
- }
272
-
273
- async start (): Promise<void> {
274
- await assertDatastoreVersionIsCurrent(this.datastore)
275
- await start(
276
- this.blockstore,
277
- this.datastore,
278
- this.routing,
279
- this.libp2p
280
- )
281
- this.events.dispatchEvent(new CustomEvent('start', { detail: this }))
282
- }
283
-
284
- async stop (): Promise<void> {
285
- await stop(
286
- this.blockstore,
287
- this.datastore,
288
- this.routing,
289
- this.libp2p
290
- )
291
- this.events.dispatchEvent(new CustomEvent('stop', { detail: this }))
292
- }
293
-
294
- async gc (options: GCOptions = {}): Promise<void> {
295
- const releaseLock = await this.blockstore.lock.writeLock()
296
-
297
- try {
298
- const helia = this
299
- const blockstore = this.blockstore.unwrap()
300
-
301
- this.log('gc start')
302
-
303
- await drain(blockstore.deleteMany((async function * (): AsyncGenerator<CID> {
304
- for await (const { cid } of blockstore.getAll()) {
305
- try {
306
- if (await helia.pins.isPinned(cid, options)) {
307
- continue
308
- }
309
-
310
- yield cid
311
-
312
- options.onProgress?.(new CustomProgressEvent<CID>('helia:gc:deleted', cid))
313
- } catch (err: any) {
314
- helia.log.error('error during gc - %e', err)
315
- options.onProgress?.(new CustomProgressEvent<Error>('helia:gc:error', err))
316
- }
317
- }
318
- }())))
319
- } finally {
320
- releaseLock()
321
- }
322
-
323
- this.log('gc finished')
324
- }
325
- }
326
-
327
- function asContentRouting (obj?: any): ContentRouting | undefined {
328
- return obj?.[contentRoutingSymbol]
329
- }
330
-
331
- function asPeerRouting (obj?: any): PeerRouting | undefined {
332
- return obj?.[peerRoutingSymbol]
333
- }
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'
package/src/is-cid.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { CID } from 'multiformats/cid'
2
+
3
+ export function isCID (obj?: any): obj is CID {
4
+ if (obj == null) {
5
+ return false
6
+ }
7
+
8
+ return CID.asCID(obj) != null
9
+ }
@@ -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/pins';
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":"AAOA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAItC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,OAAO,KAAK,EAAE,UAAU,EAAgB,eAAe,EAAE,SAAS,EAAE,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACvH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAgD/C,qBAAa,QAAS,YAAW,IAAI;;IACnC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IACrC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAa;gBAEzB,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW;IAMxE,GAAG,CAAE,GAAG,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAE,UAAe,GAAG,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC;IAyGhH,EAAE,CAAE,GAAG,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAE,SAAc,GAAG,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC;IA6B9G,EAAE,CAAE,OAAO,GAAE,SAAc,GAAG,cAAc,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC;IAcpE,QAAQ,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAAC,OAAO,CAAC;IAMpE,GAAG,CAAE,GAAG,EAAE,GAAG,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"}