@helia/interface 6.2.1-f6cc7640 → 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/index.ts CHANGED
@@ -14,32 +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'
26
+ import type { TypedEventEmitter } from 'main-event'
23
27
  import type { BlockCodec, MultihashHasher } from 'multiformats'
24
28
  import type { CID } from 'multiformats/cid'
25
29
  import type { ProgressEvent, ProgressOptions } from 'progress-events'
26
30
 
27
31
  export interface CodecLoader {
28
- <T = any, Code extends number = any>(code: Code): BlockCodec<Code, T> | Promise<BlockCodec<Code, T>>
32
+ <T = any, Code extends number = any>(code: Code, options?: AbortOptions): BlockCodec<Code, T> | Promise<BlockCodec<Code, T>>
29
33
  }
30
34
 
31
35
  export interface HasherLoader {
32
- (code: number): MultihashHasher | Promise<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
33
52
  }
34
53
 
35
54
  /**
36
55
  * The API presented by a Helia node
37
56
  */
38
- export interface Helia<T extends Libp2p = Libp2p> {
57
+ export interface Helia {
39
58
  /**
40
- * The libp2p instance
59
+ * Runtime information about the node
41
60
  */
42
- libp2p: T
61
+ info: NodeInfo
43
62
 
44
63
  /**
45
64
  * Where the blocks are stored
@@ -54,7 +73,12 @@ export interface Helia<T extends Libp2p = Libp2p> {
54
73
  /**
55
74
  * Event emitter for Helia start and stop events
56
75
  */
57
- events: TypedEventEmitter<HeliaEvents<T>>
76
+ events: TypedEventEmitter<HeliaEvents<this>>
77
+
78
+ /**
79
+ * Secure storage for private keys
80
+ */
81
+ keychain: Keychain
58
82
 
59
83
  /**
60
84
  * Pinning operations for blocks in the blockstore
@@ -84,15 +108,20 @@ export interface Helia<T extends Libp2p = Libp2p> {
84
108
  */
85
109
  metrics?: Metrics
86
110
 
111
+ /**
112
+ * The current status of the Helia node
113
+ */
114
+ status: 'starting' | 'started' | 'stopping' | 'stopped'
115
+
87
116
  /**
88
117
  * Starts the Helia node
89
118
  */
90
- start(): Promise<void>
119
+ start(options?: AbortOptions): Promise<this>
91
120
 
92
121
  /**
93
122
  * Stops the Helia node
94
123
  */
95
- stop(): Promise<void>
124
+ stop(options?: AbortOptions): Promise<this>
96
125
 
97
126
  /**
98
127
  * Remove any unpinned blocks from the blockstore
@@ -111,6 +140,36 @@ export interface Helia<T extends Libp2p = Libp2p> {
111
140
  * the hasher is being fetched from the network.
112
141
  */
113
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
114
173
  }
115
174
 
116
175
  export type GcEvents =
@@ -121,7 +180,7 @@ export interface GCOptions extends AbortOptions, ProgressOptions<GcEvents> {
121
180
 
122
181
  }
123
182
 
124
- export interface HeliaEvents<T extends Libp2p = Libp2p> {
183
+ export interface HeliaEvents<H extends Helia = Helia> {
125
184
  /**
126
185
  * This event notifies listeners that the node has started
127
186
  *
@@ -131,7 +190,7 @@ export interface HeliaEvents<T extends Libp2p = Libp2p> {
131
190
  * })
132
191
  * ```
133
192
  */
134
- start: CustomEvent<Helia<T>>
193
+ start: CustomEvent<H>
135
194
 
136
195
  /**
137
196
  * This event notifies listeners that the node has stopped
@@ -142,10 +201,11 @@ export interface HeliaEvents<T extends Libp2p = Libp2p> {
142
201
  * })
143
202
  * ```
144
203
  */
145
- stop: CustomEvent<Helia<T>>
204
+ stop: CustomEvent<H>
146
205
  }
147
206
 
148
207
  export * from './blocks.ts'
149
208
  export * from './errors.ts'
209
+ export * from './graph-walker.ts'
150
210
  export * from './pins.ts'
151
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'>>