@libp2p/interface 0.0.1-5315f7bc → 0.0.1-562f9b08

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.
@@ -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,43 +5,28 @@ import type { Duplex, Source } from 'it-stream-types'
6
5
  import type { Uint8ArrayList } from 'uint8arraylist'
7
6
 
8
7
  export interface ConnectionTimeline {
9
- open: number
10
- upgraded?: number
11
- close?: number
12
- }
13
-
14
- /**
15
- * Outbound conections are opened by the local node, inbound streams are opened by the remote
16
- */
17
- export type Direction = 'inbound' | 'outbound'
18
-
19
- export interface ConnectionStat {
20
- /**
21
- * Outbound conections are opened by the local node, inbound streams are opened by the remote
22
- */
23
- direction: Direction
24
-
25
- /**
26
- * Lifecycle times for the connection
27
- */
28
- timeline: ConnectionTimeline
29
-
30
8
  /**
31
- * Once a multiplexer has been negotiated for this stream, it will be set on the stat object
9
+ * When the connection was opened
32
10
  */
33
- multiplexer?: string
11
+ open: number
34
12
 
35
13
  /**
36
- * Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
14
+ * When the MultiaddrConnection was upgraded to a Connection - e.g. the type
15
+ * of connection encryption and multiplexing was negotiated.
37
16
  */
38
- encryption?: string
17
+ upgraded?: number
39
18
 
40
19
  /**
41
- * The current status of the connection
20
+ * When the connection was closed.
42
21
  */
43
- status: keyof typeof Status
22
+ close?: number
44
23
  }
45
24
 
25
+ /**
26
+ * Outbound connections are opened by the local node, inbound streams are opened by the remote
27
+ */
28
+ export type Direction = 'inbound' | 'outbound'
29
+
46
30
  export interface StreamTimeline {
47
31
  /**
48
32
  * A timestamp of when the stream was opened
@@ -68,24 +52,37 @@ export interface StreamTimeline {
68
52
  * A timestamp of when the stream was reset
69
53
  */
70
54
  reset?: number
71
- }
72
55
 
73
- export interface StreamStat {
74
56
  /**
75
- * Outbound streams are opened by the local node, inbound streams are opened by the remote
57
+ * A timestamp of when the stream was aborted
76
58
  */
77
- direction: Direction
59
+ abort?: number
60
+ }
78
61
 
79
- /**
80
- * Lifecycle times for the stream
81
- */
82
- timeline: StreamTimeline
62
+ /**
63
+ * The states a stream can be in
64
+ */
65
+ export type StreamStatus = 'open' | 'closing' | 'closed' | 'aborted' | 'reset'
83
66
 
84
- /**
85
- * Once a protocol has been negotiated for this stream, it will be set on the stat object
86
- */
87
- protocol?: string
88
- }
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'
89
86
 
90
87
  /**
91
88
  * A Stream is a data channel between two peers that
@@ -104,7 +101,7 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
104
101
  *
105
102
  * The sink and the source will return normally.
106
103
  */
107
- close: () => void
104
+ close: (options?: AbortOptions) => Promise<void>
108
105
 
109
106
  /**
110
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.
@@ -113,14 +110,14 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
113
110
  *
114
111
  * The source will return normally, the sink will continue to consume.
115
112
  */
116
- closeRead: () => void
113
+ closeRead: (options?: AbortOptions) => Promise<void>
117
114
 
118
115
  /**
119
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.
120
117
  *
121
118
  * The source will return normally, the sink will continue to consume.
122
119
  */
123
- closeWrite: () => void
120
+ closeWrite: (options?: AbortOptions) => Promise<void>
124
121
 
125
122
  /**
126
123
  * Closes the stream for **reading** *and* **writing**. This should be called when a *local error* has occurred.
@@ -134,28 +131,44 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
134
131
  abort: (err: Error) => void
135
132
 
136
133
  /**
137
- * Closes the stream *immediately* for **reading** *and* **writing**. This should be called when a *remote error* has occurred.
138
- *
139
- * This function is called automatically by the muxer when it receives a `RESET` message from the remote.
140
- *
141
- * The sink will return and the source will throw.
134
+ * Unique identifier for a stream. Identifiers are not unique across muxers.
135
+ */
136
+ id: string
137
+
138
+ /**
139
+ * Outbound streams are opened by the local node, inbound streams are opened by the remote
142
140
  */
143
- reset: () => void
141
+ direction: Direction
144
142
 
145
143
  /**
146
- * Unique identifier for a stream. Identifiers are not unique across muxers.
144
+ * Lifecycle times for the stream
147
145
  */
148
- id: string
146
+ timeline: StreamTimeline
149
147
 
150
148
  /**
151
- * Stats about this stream
149
+ * Once a protocol has been negotiated for this stream, it will be set on the stat object
152
150
  */
153
- stat: StreamStat
151
+ protocol?: string
154
152
 
155
153
  /**
156
154
  * User defined stream metadata
157
155
  */
158
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
159
172
  }
160
173
 
161
174
  export interface NewStreamOptions extends AbortOptions {
@@ -167,6 +180,8 @@ export interface NewStreamOptions extends AbortOptions {
167
180
  maxOutboundStreams?: number
168
181
  }
169
182
 
183
+ export type ConnectionStatus = 'open' | 'closing' | 'closed'
184
+
170
185
  /**
171
186
  * A Connection is a high-level representation of a connection
172
187
  * to a remote peer that may have been secured by encryption and
@@ -174,17 +189,81 @@ export interface NewStreamOptions extends AbortOptions {
174
189
  * between which the connection is made.
175
190
  */
176
191
  export interface Connection {
192
+ /**
193
+ * The unique identifier for this connection
194
+ */
177
195
  id: string
178
- stat: ConnectionStat
196
+
197
+ /**
198
+ * The address of the remote end of the connection
199
+ */
179
200
  remoteAddr: Multiaddr
201
+
202
+ /**
203
+ * The id of the peer at the remote end of the connection
204
+ */
180
205
  remotePeer: PeerId
206
+
207
+ /**
208
+ * A list of tags applied to this connection
209
+ */
181
210
  tags: string[]
211
+
212
+ /**
213
+ * A list of open streams on this connection
214
+ */
182
215
  streams: Stream[]
183
216
 
184
- newStream: (multicodecs: string | string[], options?: NewStreamOptions) => Promise<Stream>
217
+ /**
218
+ * Outbound conections are opened by the local node, inbound streams are opened by the remote
219
+ */
220
+ direction: Direction
221
+
222
+ /**
223
+ * Lifecycle times for the connection
224
+ */
225
+ timeline: ConnectionTimeline
226
+
227
+ /**
228
+ * Once a multiplexer has been negotiated for this stream, it will be set on the stat object
229
+ */
230
+ multiplexer?: string
231
+
232
+ /**
233
+ * Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
234
+ */
235
+ encryption?: string
236
+
237
+ /**
238
+ * The current status of the connection
239
+ */
240
+ status: ConnectionStatus
241
+
242
+ /**
243
+ * Create a new stream on this connection and negotiate one of the passed protocols
244
+ */
245
+ newStream: (protocols: string | string[], options?: NewStreamOptions) => Promise<Stream>
246
+
247
+ /**
248
+ * Add a stream to this connection
249
+ */
185
250
  addStream: (stream: Stream) => void
251
+
252
+ /**
253
+ * Remove a stream from this connection
254
+ */
186
255
  removeStream: (id: string) => void
187
- close: () => Promise<void>
256
+
257
+ /**
258
+ * Gracefully close the connection. All queued data will be written to the
259
+ * underlying transport.
260
+ */
261
+ close: (options?: AbortOptions) => Promise<void>
262
+
263
+ /**
264
+ * Immediately close the connection, any queued data will be discarded
265
+ */
266
+ abort: (err: Error) => void
188
267
  }
189
268
 
190
269
  export const symbol = Symbol.for('@libp2p/connection')
@@ -194,7 +273,6 @@ export function isConnection (other: any): other is Connection {
194
273
  }
195
274
 
196
275
  export interface ConnectionProtector {
197
-
198
276
  /**
199
277
  * Takes a given Connection and creates a private encryption stream
200
278
  * between its two peers from the PSK the Protector instance was
@@ -204,8 +282,20 @@ export interface ConnectionProtector {
204
282
  }
205
283
 
206
284
  export interface MultiaddrConnectionTimeline {
285
+ /**
286
+ * When the connection was opened
287
+ */
207
288
  open: number
289
+
290
+ /**
291
+ * When the MultiaddrConnection was upgraded to a Connection - the type of
292
+ * connection encryption and multiplexing was negotiated.
293
+ */
208
294
  upgraded?: number
295
+
296
+ /**
297
+ * When the connection was closed.
298
+ */
209
299
  close?: number
210
300
  }
211
301
 
@@ -215,7 +305,24 @@ export interface MultiaddrConnectionTimeline {
215
305
  * without encryption or stream multiplexing.
216
306
  */
217
307
  export interface MultiaddrConnection extends Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>> {
218
- close: (err?: Error) => Promise<void>
308
+ /**
309
+ * Gracefully close the connection. All queued data will be written to the
310
+ * underlying transport.
311
+ */
312
+ close: (options?: AbortOptions) => Promise<void>
313
+
314
+ /**
315
+ * Immediately close the connection, any queued data will be discarded
316
+ */
317
+ abort: (err: Error) => void
318
+
319
+ /**
320
+ * The address of the remote end of the connection
321
+ */
219
322
  remoteAddr: Multiaddr
323
+
324
+ /**
325
+ * When connection lifecycle events occurred
326
+ */
220
327
  timeline: MultiaddrConnectionTimeline
221
328
  }
package/src/index.ts CHANGED
@@ -506,7 +506,11 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ev
506
506
  dialProtocol: (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: AbortOptions) => 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.
@@ -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
  */