@helia/interface 6.2.1 → 7.0.0

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/blocks.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { RoutingFindProvidersProgressEvents } from './routing.ts'
2
- import type { PeerId, AbortOptions } from '@libp2p/interface'
3
2
  import type { Multiaddr } from '@multiformats/multiaddr'
3
+ import type { AbortOptions } from 'abort-error'
4
4
  import type { Blockstore } from 'interface-blockstore'
5
5
  import type { CID } from 'multiformats/cid'
6
6
  import type { ProgressEvent, ProgressOptions } from 'progress-events'
@@ -16,7 +16,7 @@ export interface ProviderOptions {
16
16
  * child blocks, a `findProviders` routing query will be run to find peers
17
17
  * that can supply the blocks.
18
18
  */
19
- providers?: Array<PeerId | Multiaddr | Multiaddr[]>
19
+ providers?: Array<CID | Multiaddr | Multiaddr[]>
20
20
  }
21
21
 
22
22
  /**
@@ -25,7 +25,7 @@ export interface ProviderOptions {
25
25
  export interface BlockBrokerConnectProgressEvent {
26
26
  broker: string
27
27
  type: 'connect'
28
- provider: PeerId
28
+ provider: CID
29
29
  cid: CID
30
30
  }
31
31
 
@@ -35,7 +35,7 @@ export interface BlockBrokerConnectProgressEvent {
35
35
  export interface BlockBrokerConnectedProgressEvent {
36
36
  broker: string
37
37
  type: 'connected'
38
- provider: PeerId
38
+ provider: CID
39
39
  address: Multiaddr
40
40
  cid: CID
41
41
  }
@@ -46,7 +46,7 @@ export interface BlockBrokerConnectedProgressEvent {
46
46
  export interface BlockBrokerRequestBlockProgressEvent {
47
47
  broker: string
48
48
  type: 'request-block'
49
- provider: PeerId
49
+ provider: CID
50
50
  cid: CID
51
51
  }
52
52
 
@@ -56,7 +56,7 @@ export interface BlockBrokerRequestBlockProgressEvent {
56
56
  export interface BlockBrokerReceiveBlockProgressEvent {
57
57
  broker: string
58
58
  type: 'receive-block'
59
- provider: PeerId
59
+ provider: CID
60
60
  cid: CID
61
61
  }
62
62
 
@@ -150,7 +150,7 @@ ProgressOptions<DeleteBlockProgressEvents>, ProgressOptions<DeleteManyBlocksProg
150
150
  * Adds a new peer to the session if they are supported and are either
151
151
  * not already in the session and have not been evicted previously.
152
152
  */
153
- addPeer (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void>
153
+ addPeer (peer: CID | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void>
154
154
  }
155
155
 
156
156
  export interface BlockRetrievalOptions <ProgressEvents extends ProgressEvent<any, any> = ProgressEvent<any, any>> extends AbortOptions, ProgressOptions<ProgressEvents>, ProviderOptions {
@@ -232,7 +232,7 @@ export interface SessionBlockBroker<RetrieveProgressEvents extends ProgressEvent
232
232
  * Adds a new peer to the session if they are supported and are either
233
233
  * not already in the session and have not been evicted previously.
234
234
  */
235
- addPeer (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void>
235
+ addPeer (peer: CID | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void>
236
236
  }
237
237
 
238
238
  export const DEFAULT_SESSION_MIN_PROVIDERS = 1
package/src/errors.ts CHANGED
@@ -42,3 +42,8 @@ export class InvalidCodecError extends Error {
42
42
  this.name = 'InvalidCodecError'
43
43
  }
44
44
  }
45
+
46
+ export class UnknownCryptoError extends Error {
47
+ static name = 'UnknownCryptoError'
48
+ name = 'UnknownCryptoError'
49
+ }
@@ -0,0 +1,38 @@
1
+ import type { CodecLoader } from './index.js'
2
+ import type { AbortOptions } from '@libp2p/interface'
3
+ import type { Blockstore } from 'interface-blockstore'
4
+ import type { BlockView, CID, Version } from 'multiformats'
5
+
6
+ export interface GraphWalkerComponents {
7
+ blockstore: Blockstore
8
+ getCodec: CodecLoader
9
+ }
10
+
11
+ export interface GraphWalkerInit {}
12
+
13
+ export interface GraphNode<
14
+ T = unknown,
15
+ C extends number = number,
16
+ A extends number = number,
17
+ V extends Version = 0 | 1
18
+ > {
19
+ block: BlockView<T, C, A, V>
20
+ depth: number
21
+ path: CID[]
22
+ }
23
+
24
+ export interface WalkOptions<T> extends AbortOptions {
25
+ /**
26
+ * Stop traversal once `node.depth` reaches this value. The root is
27
+ * depth 0. Default: Infinity (walk the entire DAG).
28
+ */
29
+ depth?: number
30
+
31
+ includeChild?(child: CID, parent: BlockView<T, number, number, 0 | 1>): boolean
32
+ }
33
+
34
+ export interface GraphWalker {
35
+ walk<T = any>(cid: CID, options?: WalkOptions<T>): AsyncGenerator<GraphNode<T>>
36
+ }
37
+
38
+ export type GraphWalkerFactory = (components: GraphWalkerComponents) => GraphWalker
package/src/index.ts CHANGED
@@ -14,35 +14,51 @@
14
14
  * ```
15
15
  */
16
16
 
17
- import type { Blocks } from './blocks.ts'
17
+ import type { BlockBroker, Blocks } from './blocks.ts'
18
18
  import type { Pins } from './pins.ts'
19
- import type { Routing } from './routing.ts'
20
- import type { AbortOptions, ComponentLogger, Libp2p, Metrics, TypedEventEmitter } from '@libp2p/interface'
19
+ import type { Router, Routing } from './routing.ts'
20
+ import type { CryptoLoader, Keychain } from '@ipshipyard/keychain'
21
+ import type { Metrics } from '@libp2p/interface'
21
22
  import type { DNS } from '@multiformats/dns'
23
+ import type { AbortOptions } from 'abort-error'
24
+ import type { ComponentLogger } from 'birnam'
22
25
  import type { Datastore } from 'interface-datastore'
23
- import type { Await } from 'interface-store'
26
+ import type { TypedEventEmitter } from 'main-event'
24
27
  import type { BlockCodec, MultihashHasher } from 'multiformats'
25
28
  import type { CID } from 'multiformats/cid'
26
29
  import type { ProgressEvent, ProgressOptions } from 'progress-events'
27
30
 
28
- export type { Await, AwaitIterable } from 'interface-store'
29
-
30
31
  export interface CodecLoader {
31
- <T = any, Code extends number = any>(code: Code): Await<BlockCodec<Code, T>>
32
+ <T = any, Code extends number = any>(code: Code, options?: AbortOptions): BlockCodec<Code, T> | Promise<BlockCodec<Code, T>>
32
33
  }
33
34
 
34
35
  export interface HasherLoader {
35
- (code: number): Await<MultihashHasher>
36
+ (code: number, options?: AbortOptions): MultihashHasher | Promise<MultihashHasher>
37
+ }
38
+
39
+ export type { CryptoLoader, Keychain } from '@ipshipyard/keychain'
40
+ export type { Crypto, PrivateKey, PublicKey } from '@ipshipyard/crypto'
41
+ export { isPrivateKey, isPublicKey } from '@ipshipyard/crypto'
42
+
43
+ export interface NodeInfo {
44
+ name: string
45
+ version: string
46
+ }
47
+
48
+ export interface HeliaMixin<Start extends Helia = Helia, Stop = Start> {
49
+ name: string
50
+ start?(helia: Start): Promise<void> | void
51
+ stop?(helia: Stop): Promise<void> | void
36
52
  }
37
53
 
38
54
  /**
39
55
  * The API presented by a Helia node
40
56
  */
41
- export interface Helia<T extends Libp2p = Libp2p> {
57
+ export interface Helia {
42
58
  /**
43
- * The libp2p instance
59
+ * Runtime information about the node
44
60
  */
45
- libp2p: T
61
+ info: NodeInfo
46
62
 
47
63
  /**
48
64
  * Where the blocks are stored
@@ -57,7 +73,12 @@ export interface Helia<T extends Libp2p = Libp2p> {
57
73
  /**
58
74
  * Event emitter for Helia start and stop events
59
75
  */
60
- events: TypedEventEmitter<HeliaEvents<T>>
76
+ events: TypedEventEmitter<HeliaEvents<this>>
77
+
78
+ /**
79
+ * Secure storage for private keys
80
+ */
81
+ keychain: Keychain
61
82
 
62
83
  /**
63
84
  * Pinning operations for blocks in the blockstore
@@ -87,15 +108,20 @@ export interface Helia<T extends Libp2p = Libp2p> {
87
108
  */
88
109
  metrics?: Metrics
89
110
 
111
+ /**
112
+ * The current status of the Helia node
113
+ */
114
+ status: 'starting' | 'started' | 'stopping' | 'stopped'
115
+
90
116
  /**
91
117
  * Starts the Helia node
92
118
  */
93
- start(): Promise<void>
119
+ start(options?: AbortOptions): Promise<this>
94
120
 
95
121
  /**
96
122
  * Stops the Helia node
97
123
  */
98
- stop(): Promise<void>
124
+ stop(options?: AbortOptions): Promise<this>
99
125
 
100
126
  /**
101
127
  * Remove any unpinned blocks from the blockstore
@@ -114,6 +140,36 @@ export interface Helia<T extends Libp2p = Libp2p> {
114
140
  * the hasher is being fetched from the network.
115
141
  */
116
142
  getHasher: HasherLoader
143
+
144
+ /**
145
+ * Cryptography implementations securely sign and verify data
146
+ */
147
+ getCrypto: CryptoLoader
148
+
149
+ /**
150
+ * Returns `true` if a router with the passed name has been configured
151
+ */
152
+ hasRouter (name: string): boolean
153
+
154
+ /**
155
+ * Add a router
156
+ */
157
+ addRouter(router: Router): void
158
+
159
+ /**
160
+ * Returns `true` if a block broker with the passed name has been configured
161
+ */
162
+ hasBlockBroker (name: string): boolean
163
+
164
+ /**
165
+ * Add a block broker
166
+ */
167
+ addBlockBroker(blockBroker: BlockBroker): void
168
+
169
+ /**
170
+ * Add a mixin to extend runtime functionality
171
+ */
172
+ addMixin<T extends Helia = Helia & Record<string, any>>(mixin: HeliaMixin<T>): void
117
173
  }
118
174
 
119
175
  export type GcEvents =
@@ -124,7 +180,7 @@ export interface GCOptions extends AbortOptions, ProgressOptions<GcEvents> {
124
180
 
125
181
  }
126
182
 
127
- export interface HeliaEvents<T extends Libp2p = Libp2p> {
183
+ export interface HeliaEvents<H extends Helia = Helia> {
128
184
  /**
129
185
  * This event notifies listeners that the node has started
130
186
  *
@@ -134,7 +190,7 @@ export interface HeliaEvents<T extends Libp2p = Libp2p> {
134
190
  * })
135
191
  * ```
136
192
  */
137
- start: CustomEvent<Helia<T>>
193
+ start: CustomEvent<H>
138
194
 
139
195
  /**
140
196
  * This event notifies listeners that the node has stopped
@@ -145,10 +201,11 @@ export interface HeliaEvents<T extends Libp2p = Libp2p> {
145
201
  * })
146
202
  * ```
147
203
  */
148
- stop: CustomEvent<Helia<T>>
204
+ stop: CustomEvent<H>
149
205
  }
150
206
 
151
207
  export * from './blocks.ts'
152
208
  export * from './errors.ts'
209
+ export * from './graph-walker.ts'
153
210
  export * from './pins.ts'
154
211
  export * from './routing.ts'
package/src/pins.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { GetBlockProgressEvents } from './blocks.ts'
2
- import type { AbortOptions } from '@libp2p/interface'
2
+ import type { GraphWalkerFactory } from './graph-walker.ts'
3
+ import type { AbortOptions } from 'abort-error'
3
4
  import type { CID } from 'multiformats/cid'
4
5
  import type { ProgressEvent, ProgressOptions } from 'progress-events'
5
6
 
@@ -20,6 +21,11 @@ export interface AddOptions extends AbortOptions, ProgressOptions<AddPinEvents |
20
21
  */
21
22
  depth?: number
22
23
 
24
+ /**
25
+ * Walker factory used to traverse the DAG. Default: `depthFirstWalker()`.
26
+ */
27
+ walker?: GraphWalkerFactory
28
+
23
29
  /**
24
30
  * Optional user-defined metadata to store with the pin
25
31
  */
@@ -27,7 +33,10 @@ export interface AddOptions extends AbortOptions, ProgressOptions<AddPinEvents |
27
33
  }
28
34
 
29
35
  export interface RmOptions extends AbortOptions {
30
-
36
+ /**
37
+ * Walker factory used to traverse the DAG. Default: `depthFirstWalker()`.
38
+ */
39
+ walker?: GraphWalkerFactory
31
40
  }
32
41
 
33
42
  export interface LsOptions extends AbortOptions {
package/src/routing.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { AbortOptions, PeerId, PeerInfo, TraceOptions } from '@libp2p/interface'
1
+ import type { Multiaddr } from '@multiformats/multiaddr'
2
+ import type { AbortOptions } from 'abort-error'
2
3
  import type { CID } from 'multiformats/cid'
3
4
  import type { ProgressEvent, ProgressOptions } from 'progress-events'
4
5
 
@@ -8,7 +9,7 @@ import type { ProgressEvent, ProgressOptions } from 'progress-events'
8
9
  * local cache that may be used in preference over network calls, for example
9
10
  * when a record has a TTL.
10
11
  */
11
- export interface RoutingOptions<Event extends ProgressEvent = any> extends AbortOptions, ProgressOptions<Event>, TraceOptions {
12
+ export interface RoutingOptions<Event extends ProgressEvent = any> extends AbortOptions, ProgressOptions<Event> {
12
13
  /**
13
14
  * Pass `false` to not use the network
14
15
  *
@@ -29,12 +30,42 @@ export interface RoutingOptions<Event extends ProgressEvent = any> extends Abort
29
30
  * @default true
30
31
  */
31
32
  validate?: boolean
33
+
34
+ /**
35
+ * Where tracing is used, this value carries tracing context
36
+ */
37
+ trace?: any
38
+ }
39
+
40
+ /**
41
+ * A Peer is another node on the network
42
+ */
43
+ export interface Peer {
44
+ /**
45
+ * The identifier of the remote peer
46
+ */
47
+ id: CID
48
+
49
+ /**
50
+ * The multiaddrs a peer is listening on
51
+ */
52
+ multiaddrs: Multiaddr[]
32
53
  }
33
54
 
34
55
  /**
35
56
  * A provider can supply the content for a CID
36
57
  */
37
- export interface Provider extends PeerInfo {
58
+ export interface Provider {
59
+ /**
60
+ * The identifier of the remote peer, a CID
61
+ */
62
+ id: CID
63
+
64
+ /**
65
+ * The multiaddrs a peer is listening on
66
+ */
67
+ multiaddrs: Multiaddr[]
68
+
38
69
  /**
39
70
  * If present these are the methods that the peer can supply the content via.
40
71
  *
@@ -69,7 +100,7 @@ export interface RoutingFindProvidersEndEvent {
69
100
  export interface RoutingFindProvidersHttpGatewayProvider {
70
101
  routing: 'http-gateway-router'
71
102
  cid: CID
72
- provider: PeerInfo & {
103
+ provider: Peer & {
73
104
  protocols: ['transport-ipfs-gateway-http']
74
105
  }
75
106
  }
@@ -77,7 +108,7 @@ export interface RoutingFindProvidersHttpGatewayProvider {
77
108
  export interface RoutingFindProvidersDelegatedHttpRoutingProvider {
78
109
  routing: 'delegated-http-router'
79
110
  cid: CID
80
- provider: PeerInfo & {
111
+ provider: Peer & {
81
112
  routing: 'delegated-http-routing'
82
113
  protocols: string[]
83
114
  }
@@ -86,7 +117,7 @@ export interface RoutingFindProvidersDelegatedHttpRoutingProvider {
86
117
  export interface RoutingFindProvidersLibp2pProvider {
87
118
  routing: 'libp2p-router'
88
119
  cid: CID
89
- provider: PeerInfo
120
+ provider: Peer
90
121
  }
91
122
 
92
123
  export type RoutingFindProvidersProviderEvent = RoutingFindProvidersHttpGatewayProvider | RoutingFindProvidersDelegatedHttpRoutingProvider | RoutingFindProvidersLibp2pProvider
@@ -157,12 +188,12 @@ export type RoutingGetProgressEvents =
157
188
 
158
189
  export interface RoutingFindPeerStartEvent {
159
190
  routing: string
160
- peerId: PeerId
191
+ peerId: CID
161
192
  }
162
193
 
163
194
  export interface RoutingFindPeerEndEvent {
164
195
  routing: string
165
- peerId: PeerId
196
+ peerId: CID
166
197
  }
167
198
 
168
199
  export type RoutingFindPeerProgressEvents =
@@ -183,7 +214,7 @@ export type RoutingGetClosestPeersProgressEvents =
183
214
  ProgressEvent<'helia:routing:get-closest-peers:start', RoutingGetClosestPeersStartEvent> |
184
215
  ProgressEvent<'helia:routing:get-closest-peers:end', RoutingGetClosestPeersEndEvent>
185
216
 
186
- export interface Routing {
217
+ export interface Router {
187
218
  /**
188
219
  * The name of this routing implementation
189
220
  */
@@ -197,10 +228,12 @@ export interface Routing {
197
228
  *
198
229
  * ```js
199
230
  * // ...
200
- * await contentRouting.provide(cid)
231
+ * await contentRouting.provide(cid, {
232
+ * signal: AbortSignal.timeout(5_000)
233
+ * })
201
234
  * ```
202
235
  */
203
- provide(cid: CID, options?: RoutingOptions<RoutingProvideProgressEvents>): Promise<void>
236
+ provide?(cid: CID, options?: RoutingOptions<RoutingProvideProgressEvents>): Promise<void>
204
237
 
205
238
  /**
206
239
  * Helia will periodically re-provide every previously provided CID. Use this
@@ -210,10 +243,12 @@ export interface Routing {
210
243
  *
211
244
  * ```js
212
245
  * // ...
213
- * await contentRouting.cancelReprovide(cid)
246
+ * await contentRouting.cancelReprovide(cid, {
247
+ * signal: AbortSignal.timeout(5_000)
248
+ * })
214
249
  * ```
215
250
  */
216
- cancelReprovide(key: CID, options?: AbortOptions): Promise<void>
251
+ cancelReprovide?(key: CID, options?: AbortOptions): Promise<void>
217
252
 
218
253
  /**
219
254
  * Find the providers of the passed CID.
@@ -222,12 +257,14 @@ export interface Routing {
222
257
  *
223
258
  * ```js
224
259
  * // Iterate over the providers found for the given cid
225
- * for await (const provider of contentRouting.findProviders(cid)) {
260
+ * for await (const provider of contentRouting.findProviders(cid, {
261
+ * signal: AbortSignal.timeout(5_000)
262
+ * })) {
226
263
  * console.log(provider.id, provider.multiaddrs)
227
264
  * }
228
265
  * ```
229
266
  */
230
- findProviders(cid: CID, options?: RoutingOptions<RoutingFindProvidersProgressEvents>): AsyncIterable<Provider>
267
+ findProviders?(cid: CID, options?: RoutingOptions<RoutingFindProvidersProgressEvents>): AsyncIterable<Provider>
231
268
 
232
269
  /**
233
270
  * Puts a value corresponding to the passed key in a way that can later be
@@ -237,13 +274,15 @@ export interface Routing {
237
274
  *
238
275
  * ```js
239
276
  * // ...
240
- * const key = '/key'
277
+ * const key = uint8ArrayFromString('/key')
241
278
  * const value = uint8ArrayFromString('oh hello there')
242
279
  *
243
- * await contentRouting.put(key, value)
280
+ * await contentRouting.put(key, value, {
281
+ * signal: AbortSignal.timeout(5_000)
282
+ * })
244
283
  * ```
245
284
  */
246
- put(key: Uint8Array, value: Uint8Array, options?: RoutingOptions<RoutingPutProgressEvents>): Promise<void>
285
+ put?(key: Uint8Array, value: Uint8Array, options?: RoutingOptions<RoutingPutProgressEvents>): Promise<void>
247
286
 
248
287
  /**
249
288
  * Retrieves a value from the network corresponding to the passed key.
@@ -253,11 +292,13 @@ export interface Routing {
253
292
  * ```js
254
293
  * // ...
255
294
  *
256
- * const key = '/key'
257
- * const value = await contentRouting.get(key)
295
+ * const key = uint8ArrayFromString('/key')
296
+ * const value = await contentRouting.get(key, {
297
+ * signal: AbortSignal.timeout(5_000)
298
+ * })
258
299
  * ```
259
300
  */
260
- get(key: Uint8Array, options?: RoutingOptions<RoutingGetProgressEvents>): Promise<Uint8Array>
301
+ get?(key: Uint8Array, options?: RoutingOptions<RoutingGetProgressEvents>): Promise<Uint8Array>
261
302
 
262
303
  /**
263
304
  * Searches the network for peer info corresponding to the passed peer id.
@@ -266,10 +307,12 @@ export interface Routing {
266
307
  *
267
308
  * ```js
268
309
  * // ...
269
- * const peer = await peerRouting.findPeer(peerId, options)
310
+ * const peer = await peerRouting.findPeer(peerId, {
311
+ * signal: AbortSignal.timeout(5_000)
312
+ * })
270
313
  * ```
271
314
  */
272
- findPeer(peerId: PeerId, options?: RoutingOptions<RoutingFindPeerProgressEvents>): Promise<PeerInfo>
315
+ findPeer?(peer: CID, options?: RoutingOptions<RoutingFindPeerProgressEvents>): Promise<Peer>
273
316
 
274
317
  /**
275
318
  * Search the network for peers that are closer to the passed key. Peer
@@ -279,10 +322,14 @@ export interface Routing {
279
322
  *
280
323
  * ```js
281
324
  * // Iterate over the closest peers found for the given key
282
- * for await (const peer of peerRouting.getClosestPeers(key)) {
325
+ * for await (const peer of peerRouting.getClosestPeers(key, {
326
+ * signal: AbortSignal.timeout(5_000)
327
+ * })) {
283
328
  * console.log(peer.id, peer.multiaddrs)
284
329
  * }
285
330
  * ```
286
331
  */
287
- getClosestPeers(key: Uint8Array, options?: RoutingOptions<RoutingGetClosestPeersProgressEvents>): AsyncIterable<PeerInfo>
332
+ getClosestPeers?(key: Uint8Array, options?: RoutingOptions<RoutingGetClosestPeersProgressEvents>): AsyncIterable<Peer>
288
333
  }
334
+
335
+ export type Routing = Required<Omit<Router, 'name'>>