@libp2p/interface 0.0.1-e9cafd3d → 0.0.1-eabf6f36

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 (39) hide show
  1. package/dist/src/connection/index.d.ts +109 -16
  2. package/dist/src/connection/index.d.ts.map +1 -1
  3. package/dist/src/connection/index.js.map +1 -1
  4. package/dist/src/errors.d.ts.map +1 -1
  5. package/dist/src/errors.js.map +1 -1
  6. package/dist/src/index.d.ts +8 -4
  7. package/dist/src/index.d.ts.map +1 -1
  8. package/dist/src/keys/index.d.ts.map +1 -1
  9. package/dist/src/keys/index.js.map +1 -1
  10. package/dist/src/peer-store/tags.d.ts.map +1 -1
  11. package/dist/src/peer-store/tags.js.map +1 -1
  12. package/dist/src/startable.d.ts.map +1 -1
  13. package/dist/src/startable.js.map +1 -1
  14. package/dist/src/stream-handler/index.d.ts +5 -0
  15. package/dist/src/stream-handler/index.d.ts.map +1 -1
  16. package/dist/src/stream-muxer/index.d.ts +6 -2
  17. package/dist/src/stream-muxer/index.d.ts.map +1 -1
  18. package/dist/src/stream-muxer/stream.d.ts +77 -20
  19. package/dist/src/stream-muxer/stream.d.ts.map +1 -1
  20. package/dist/src/stream-muxer/stream.js +214 -158
  21. package/dist/src/stream-muxer/stream.js.map +1 -1
  22. package/dist/src/transport/index.d.ts +5 -0
  23. package/dist/src/transport/index.d.ts.map +1 -1
  24. package/package.json +4 -11
  25. package/src/connection/index.ts +130 -18
  26. package/src/errors.ts +0 -1
  27. package/src/index.ts +8 -4
  28. package/src/keys/index.ts +0 -1
  29. package/src/peer-store/tags.ts +0 -1
  30. package/src/startable.ts +0 -1
  31. package/src/stream-handler/index.ts +6 -0
  32. package/src/stream-muxer/index.ts +7 -2
  33. package/src/stream-muxer/stream.ts +285 -176
  34. package/src/transport/index.ts +6 -0
  35. package/dist/src/connection/status.d.ts +0 -4
  36. package/dist/src/connection/status.d.ts.map +0 -1
  37. package/dist/src/connection/status.js +0 -4
  38. package/dist/src/connection/status.js.map +0 -1
  39. package/src/connection/status.ts +0 -4
@@ -1,4 +1,3 @@
1
- import type * as Status from './status.js'
2
1
  import type { AbortOptions } from '../index.js'
3
2
  import type { PeerId } from '../peer-id/index.js'
4
3
  import type { Multiaddr } from '@multiformats/multiaddr'
@@ -6,8 +5,20 @@ import type { Duplex, Source } from 'it-stream-types'
6
5
  import type { Uint8ArrayList } from 'uint8arraylist'
7
6
 
8
7
  export interface ConnectionTimeline {
8
+ /**
9
+ * When the connection was opened
10
+ */
9
11
  open: number
12
+
13
+ /**
14
+ * When the MultiaddrConnection was upgraded to a Connection - e.g. the type
15
+ * of connection encryption and multiplexing was negotiated.
16
+ */
10
17
  upgraded?: number
18
+
19
+ /**
20
+ * When the connection was closed.
21
+ */
11
22
  close?: number
12
23
  }
13
24
 
@@ -41,8 +52,38 @@ export interface StreamTimeline {
41
52
  * A timestamp of when the stream was reset
42
53
  */
43
54
  reset?: number
55
+
56
+ /**
57
+ * A timestamp of when the stream was aborted
58
+ */
59
+ abort?: number
44
60
  }
45
61
 
62
+ /**
63
+ * The states a stream can be in
64
+ */
65
+ export type StreamStatus = 'open' | 'closing' | 'closed' | 'aborted' | 'reset'
66
+
67
+ /**
68
+ * The states the readable end of a stream can be in
69
+ *
70
+ * ready - the readable end is ready for reading
71
+ * closing - the readable end is closing
72
+ * closed - the readable end has closed
73
+ */
74
+ export type ReadStatus = 'ready' | 'closing' | 'closed'
75
+
76
+ /**
77
+ * The states the writable end of a stream can be in
78
+ *
79
+ * ready - the writable end is ready for writing
80
+ * writing - the writable end is in the process of being written to
81
+ * done - the source passed to the `.sink` function yielded all values without error
82
+ * closing - the writable end is closing
83
+ * closed - the writable end has closed
84
+ */
85
+ export type WriteStatus = 'ready' | 'writing' | 'done' | 'closing' | 'closed'
86
+
46
87
  /**
47
88
  * A Stream is a data channel between two peers that
48
89
  * can be written to and read from at both ends.
@@ -60,7 +101,7 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
60
101
  *
61
102
  * The sink and the source will return normally.
62
103
  */
63
- close: () => void
104
+ close: (options?: AbortOptions) => Promise<void>
64
105
 
65
106
  /**
66
107
  * Closes the stream for **reading**. If iterating over the source of this stream in a `for await of` loop, it will return (exit the loop) after any buffered data has been consumed.
@@ -69,14 +110,14 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
69
110
  *
70
111
  * The source will return normally, the sink will continue to consume.
71
112
  */
72
- closeRead: () => void
113
+ closeRead: (options?: AbortOptions) => Promise<void>
73
114
 
74
115
  /**
75
116
  * Closes the stream for **writing**. If iterating over the source of this stream in a `for await of` loop, it will return (exit the loop) after any buffered data has been consumed.
76
117
  *
77
118
  * The source will return normally, the sink will continue to consume.
78
119
  */
79
- closeWrite: () => void
120
+ closeWrite: (options?: AbortOptions) => Promise<void>
80
121
 
81
122
  /**
82
123
  * Closes the stream for **reading** *and* **writing**. This should be called when a *local error* has occurred.
@@ -89,15 +130,6 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
89
130
  */
90
131
  abort: (err: Error) => void
91
132
 
92
- /**
93
- * Closes the stream *immediately* for **reading** *and* **writing**. This should be called when a *remote error* has occurred.
94
- *
95
- * This function is called automatically by the muxer when it receives a `RESET` message from the remote.
96
- *
97
- * The sink will return and the source will throw.
98
- */
99
- reset: () => void
100
-
101
133
  /**
102
134
  * Unique identifier for a stream. Identifiers are not unique across muxers.
103
135
  */
@@ -122,6 +154,21 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
122
154
  * User defined stream metadata
123
155
  */
124
156
  metadata: Record<string, any>
157
+
158
+ /**
159
+ * The current status of the stream
160
+ */
161
+ status: StreamStatus
162
+
163
+ /**
164
+ * The current status of the readable end of the stream
165
+ */
166
+ readStatus: ReadStatus
167
+
168
+ /**
169
+ * The current status of the writable end of the stream
170
+ */
171
+ writeStatus: WriteStatus
125
172
  }
126
173
 
127
174
  export interface NewStreamOptions extends AbortOptions {
@@ -131,8 +178,16 @@ export interface NewStreamOptions extends AbortOptions {
131
178
  * for the protocol
132
179
  */
133
180
  maxOutboundStreams?: number
181
+
182
+ /**
183
+ * Opt-in to running over a transient connection - one that has time/data limits
184
+ * placed on it.
185
+ */
186
+ runOnTransientConnection?: boolean
134
187
  }
135
188
 
189
+ export type ConnectionStatus = 'open' | 'closing' | 'closed'
190
+
136
191
  /**
137
192
  * A Connection is a high-level representation of a connection
138
193
  * to a remote peer that may have been secured by encryption and
@@ -188,12 +243,41 @@ export interface Connection {
188
243
  /**
189
244
  * The current status of the connection
190
245
  */
191
- status: keyof typeof Status
246
+ status: ConnectionStatus
192
247
 
193
- newStream: (multicodecs: string | string[], options?: NewStreamOptions) => Promise<Stream>
248
+ /**
249
+ * A transient connection is one that is not expected to be open for very long
250
+ * or one that cannot transfer very much data, such as one being used as a
251
+ * circuit relay connection. Protocols need to explicitly opt-in to being run
252
+ * over transient connections.
253
+ */
254
+ transient: boolean
255
+
256
+ /**
257
+ * Create a new stream on this connection and negotiate one of the passed protocols
258
+ */
259
+ newStream: (protocols: string | string[], options?: NewStreamOptions) => Promise<Stream>
260
+
261
+ /**
262
+ * Add a stream to this connection
263
+ */
194
264
  addStream: (stream: Stream) => void
265
+
266
+ /**
267
+ * Remove a stream from this connection
268
+ */
195
269
  removeStream: (id: string) => void
196
- close: () => Promise<void>
270
+
271
+ /**
272
+ * Gracefully close the connection. All queued data will be written to the
273
+ * underlying transport.
274
+ */
275
+ close: (options?: AbortOptions) => Promise<void>
276
+
277
+ /**
278
+ * Immediately close the connection, any queued data will be discarded
279
+ */
280
+ abort: (err: Error) => void
197
281
  }
198
282
 
199
283
  export const symbol = Symbol.for('@libp2p/connection')
@@ -203,7 +287,6 @@ export function isConnection (other: any): other is Connection {
203
287
  }
204
288
 
205
289
  export interface ConnectionProtector {
206
-
207
290
  /**
208
291
  * Takes a given Connection and creates a private encryption stream
209
292
  * between its two peers from the PSK the Protector instance was
@@ -213,8 +296,20 @@ export interface ConnectionProtector {
213
296
  }
214
297
 
215
298
  export interface MultiaddrConnectionTimeline {
299
+ /**
300
+ * When the connection was opened
301
+ */
216
302
  open: number
303
+
304
+ /**
305
+ * When the MultiaddrConnection was upgraded to a Connection - the type of
306
+ * connection encryption and multiplexing was negotiated.
307
+ */
217
308
  upgraded?: number
309
+
310
+ /**
311
+ * When the connection was closed.
312
+ */
218
313
  close?: number
219
314
  }
220
315
 
@@ -224,7 +319,24 @@ export interface MultiaddrConnectionTimeline {
224
319
  * without encryption or stream multiplexing.
225
320
  */
226
321
  export interface MultiaddrConnection extends Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>> {
227
- close: (err?: Error) => Promise<void>
322
+ /**
323
+ * Gracefully close the connection. All queued data will be written to the
324
+ * underlying transport.
325
+ */
326
+ close: (options?: AbortOptions) => Promise<void>
327
+
328
+ /**
329
+ * Immediately close the connection, any queued data will be discarded
330
+ */
331
+ abort: (err: Error) => void
332
+
333
+ /**
334
+ * The address of the remote end of the connection
335
+ */
228
336
  remoteAddr: Multiaddr
337
+
338
+ /**
339
+ * When connection lifecycle events occurred
340
+ */
229
341
  timeline: MultiaddrConnectionTimeline
230
342
  }
package/src/errors.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * When this error is thrown it means an operation was aborted,
4
3
  * usually in response to the `abort` event being emitted by an
package/src/index.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  * ```
15
15
  */
16
16
 
17
- import type { Connection, Stream } from './connection/index.js'
17
+ import type { Connection, NewStreamOptions, Stream } from './connection/index.js'
18
18
  import type { ContentRouting } from './content-routing/index.js'
19
19
  import type { EventEmitter } from './events.js'
20
20
  import type { KeyChain } from './keychain/index.js'
@@ -503,10 +503,14 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ev
503
503
  * pipe([1, 2, 3], stream, consume)
504
504
  * ```
505
505
  */
506
- dialProtocol: (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: AbortOptions) => Promise<Stream>
506
+ dialProtocol: (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: NewStreamOptions) => Promise<Stream>
507
507
 
508
508
  /**
509
- * Attempts to gracefully close an open connection to the given peer. If the connection is not closed in the grace period, it will be forcefully closed.
509
+ * Attempts to gracefully close an open connection to the given peer. If the
510
+ * connection is not closed in the grace period, it will be forcefully closed.
511
+ *
512
+ * An AbortSignal can optionally be passed to control when the connection is
513
+ * forcefully closed.
510
514
  *
511
515
  * @example
512
516
  *
@@ -514,7 +518,7 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ev
514
518
  * await libp2p.hangUp(remotePeerId)
515
519
  * ```
516
520
  */
517
- hangUp: (peer: PeerId | Multiaddr) => Promise<void>
521
+ hangUp: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<void>
518
522
 
519
523
  /**
520
524
  * Sets up [multistream-select routing](https://github.com/multiformats/multistream-select) of protocols to their application handlers. Whenever a stream is opened on one of the provided protocols, the handler will be called. `handle` must be called in order to register a handler and support for a given protocol. This also informs other peers of the protocols you support.
package/src/keys/index.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  export interface PublicKey {
3
2
  readonly bytes: Uint8Array
4
3
  verify: (data: Uint8Array, sig: Uint8Array) => Promise<boolean>
@@ -1,2 +1 @@
1
-
2
1
  export const KEEP_ALIVE = 'keep-alive'
package/src/startable.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * Implemented by components that have a lifecycle
4
3
  */
@@ -19,6 +19,12 @@ export interface StreamHandlerOptions {
19
19
  * How many outgoing streams can be open for this protocol at the same time on each connection (default: 64)
20
20
  */
21
21
  maxOutboundStreams?: number
22
+
23
+ /**
24
+ * Opt-in to running over a transient connection - one that has time/data limits
25
+ * placed on it.
26
+ */
27
+ runOnTransientConnection?: boolean
22
28
  }
23
29
 
24
30
  export interface StreamHandlerRecord {
@@ -37,10 +37,15 @@ export interface StreamMuxer extends Duplex<AsyncGenerator<Uint8Array>, Source<U
37
37
  /**
38
38
  * Close or abort all tracked streams and stop the muxer
39
39
  */
40
- close: (err?: Error) => void
40
+ close: (options?: AbortOptions) => Promise<void>
41
+
42
+ /**
43
+ * Close or abort all tracked streams and stop the muxer
44
+ */
45
+ abort: (err: Error) => void
41
46
  }
42
47
 
43
- export interface StreamMuxerInit extends AbortOptions {
48
+ export interface StreamMuxerInit {
44
49
  /**
45
50
  * A callback function invoked every time an incoming stream is opened
46
51
  */