@libp2p/interface 2.11.0 → 3.0.0-425a42cdd

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 (73) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/connection-encrypter.d.ts +17 -15
  4. package/dist/src/connection-encrypter.d.ts.map +1 -1
  5. package/dist/src/connection-gater.d.ts +1 -2
  6. package/dist/src/connection-gater.d.ts.map +1 -1
  7. package/dist/src/connection-protector.d.ts +9 -0
  8. package/dist/src/connection-protector.d.ts.map +1 -0
  9. package/dist/src/connection-protector.js +2 -0
  10. package/dist/src/connection-protector.js.map +1 -0
  11. package/dist/src/connection.d.ts +36 -233
  12. package/dist/src/connection.d.ts.map +1 -1
  13. package/dist/src/connection.js.map +1 -1
  14. package/dist/src/errors.d.ts +14 -0
  15. package/dist/src/errors.d.ts.map +1 -1
  16. package/dist/src/errors.js +20 -0
  17. package/dist/src/errors.js.map +1 -1
  18. package/dist/src/events.d.ts +26 -0
  19. package/dist/src/events.d.ts.map +1 -0
  20. package/dist/src/events.js +36 -0
  21. package/dist/src/events.js.map +1 -0
  22. package/dist/src/index.d.ts +35 -5
  23. package/dist/src/index.d.ts.map +1 -1
  24. package/dist/src/index.js +7 -3
  25. package/dist/src/index.js.map +1 -1
  26. package/dist/src/message-stream.d.ts +159 -0
  27. package/dist/src/message-stream.d.ts.map +1 -0
  28. package/dist/src/message-stream.js +2 -0
  29. package/dist/src/message-stream.js.map +1 -0
  30. package/dist/src/metrics.d.ts +2 -2
  31. package/dist/src/metrics.d.ts.map +1 -1
  32. package/dist/src/multiaddr-connection.d.ts +25 -0
  33. package/dist/src/multiaddr-connection.d.ts.map +1 -0
  34. package/dist/src/multiaddr-connection.js +2 -0
  35. package/dist/src/multiaddr-connection.js.map +1 -0
  36. package/dist/src/peer-store.d.ts +0 -4
  37. package/dist/src/peer-store.d.ts.map +1 -1
  38. package/dist/src/stream-handler.d.ts +12 -13
  39. package/dist/src/stream-handler.d.ts.map +1 -1
  40. package/dist/src/stream-muxer.d.ts +113 -30
  41. package/dist/src/stream-muxer.d.ts.map +1 -1
  42. package/dist/src/stream.d.ts +63 -0
  43. package/dist/src/stream.d.ts.map +1 -0
  44. package/dist/src/stream.js +2 -0
  45. package/dist/src/stream.js.map +1 -0
  46. package/dist/src/topology.d.ts +3 -3
  47. package/dist/src/topology.d.ts.map +1 -1
  48. package/dist/src/transport.d.ts +20 -13
  49. package/dist/src/transport.d.ts.map +1 -1
  50. package/dist/src/transport.js.map +1 -1
  51. package/package.json +4 -6
  52. package/src/connection-encrypter.ts +19 -16
  53. package/src/connection-gater.ts +1 -2
  54. package/src/connection-protector.ts +9 -0
  55. package/src/connection.ts +38 -270
  56. package/src/errors.ts +24 -0
  57. package/src/events.ts +44 -0
  58. package/src/index.ts +38 -5
  59. package/src/message-stream.ts +183 -0
  60. package/src/metrics.ts +2 -2
  61. package/src/multiaddr-connection.ts +27 -0
  62. package/src/peer-store.ts +0 -5
  63. package/src/stream-handler.ts +14 -15
  64. package/src/stream-muxer.ts +122 -30
  65. package/src/stream.ts +70 -0
  66. package/src/topology.ts +3 -3
  67. package/src/transport.ts +25 -14
  68. package/dist/src/pubsub.d.ts +0 -248
  69. package/dist/src/pubsub.d.ts.map +0 -1
  70. package/dist/src/pubsub.js +0 -47
  71. package/dist/src/pubsub.js.map +0 -1
  72. package/dist/typedoc-urls.json +0 -223
  73. package/src/pubsub.ts +0 -286
package/src/connection.ts CHANGED
@@ -1,179 +1,29 @@
1
- import type { AbortOptions, Logger } from './index.js'
2
- import type { PeerId } from './peer-id.js'
1
+ import type { AbortOptions, Logger, TypedEventTarget, Stream, MessageStreamEvents, PeerId, MultiaddrConnectionTimeline, MessageStreamStatus, MessageStreamDirection } from './index.js'
3
2
  import type { Multiaddr } from '@multiformats/multiaddr'
4
- import type { Duplex, Source } from 'it-stream-types'
5
- import type { Uint8ArrayList } from 'uint8arraylist'
6
3
 
7
- export interface ConnectionTimeline {
8
- /**
9
- * When the connection was opened
10
- */
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
- */
17
- upgraded?: number
18
-
19
- /**
20
- * When the connection was closed.
21
- */
22
- close?: number
23
- }
4
+ export type ConnectionStatus = MessageStreamStatus
24
5
 
25
6
  /**
26
- * Outbound connections are opened by the local node, inbound streams are opened by the remote
27
- */
28
- export type Direction = 'inbound' | 'outbound'
29
-
30
- export interface StreamTimeline {
31
- /**
32
- * A timestamp of when the stream was opened
33
- */
34
- open: number
35
-
36
- /**
37
- * A timestamp of when the stream was closed for both reading and writing
38
- */
39
- close?: number
40
-
41
- /**
42
- * A timestamp of when the stream was closed for reading
43
- */
44
- closeRead?: number
45
-
46
- /**
47
- * A timestamp of when the stream was closed for writing
48
- */
49
- closeWrite?: number
50
-
51
- /**
52
- * A timestamp of when the stream was reset
53
- */
54
- reset?: number
55
-
56
- /**
57
- * A timestamp of when the stream was aborted
58
- */
59
- abort?: number
60
- }
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
-
87
- /**
88
- * A Stream is a data channel between two peers that
89
- * can be written to and read from at both ends.
7
+ * Connection limits are present on connections that are only allowed to
8
+ * transfer a certain amount of bytes or be open for a certain number
9
+ * of seconds.
90
10
  *
91
- * It may be encrypted and multiplexed depending on the
92
- * configuration of the nodes.
11
+ * These limits are applied by Circuit Relay v2 servers, for example and
12
+ * the connection will normally be closed abruptly if the limits are
13
+ * exceeded.
93
14
  */
94
- export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>> {
95
- /**
96
- * Closes the stream for **reading** *and* **writing**.
97
- *
98
- * Any buffered data in the source can still be consumed and the stream will end normally.
99
- *
100
- * This will cause a `CLOSE` message to be sent to the remote, *unless* the sink has already ended.
101
- *
102
- * The sink and the source will return normally.
103
- */
104
- close(options?: AbortOptions): Promise<void>
105
-
106
- /**
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.
108
- *
109
- * This function is called automatically by the muxer when it receives a `CLOSE` message from the remote.
110
- *
111
- * The source will return normally, the sink will continue to consume.
112
- */
113
- closeRead(options?: AbortOptions): Promise<void>
114
-
115
- /**
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.
117
- *
118
- * The source will return normally, the sink will continue to consume.
119
- */
120
- closeWrite(options?: AbortOptions): Promise<void>
121
-
122
- /**
123
- * Closes the stream for **reading** *and* **writing**. This should be called when a *local error* has occurred.
124
- *
125
- * Note, if called without an error any buffered data in the source can still be consumed and the stream will end normally.
126
- *
127
- * This will cause a `RESET` message to be sent to the remote, *unless* the sink has already ended.
128
- *
129
- * The sink will return and the source will throw.
130
- */
131
- abort(err: Error): void
132
-
133
- /**
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
140
- */
141
- direction: Direction
142
-
143
- /**
144
- * Lifecycle times for the stream
145
- */
146
- timeline: StreamTimeline
147
-
148
- /**
149
- * The protocol negotiated for this stream
150
- */
151
- protocol?: string
152
-
153
- /**
154
- * User defined stream metadata
155
- */
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
-
15
+ export interface ConnectionLimits {
168
16
  /**
169
- * The current status of the writable end of the stream
17
+ * If present this is the number of bytes remaining that may be
18
+ * transferred over this connection
170
19
  */
171
- writeStatus: WriteStatus
20
+ bytes?: bigint
172
21
 
173
22
  /**
174
- * The stream logger
23
+ * If present this is the number of seconds that this connection will
24
+ * remain open for
175
25
  */
176
- log: Logger
26
+ seconds?: number
177
27
  }
178
28
 
179
29
  export interface NewStreamOptions extends AbortOptions {
@@ -186,10 +36,11 @@ export interface NewStreamOptions extends AbortOptions {
186
36
 
187
37
  /**
188
38
  * Opt-in to running over a limited connection - one that has restrictions
189
- * on the amount of data that may be transferred or how long it may be open for.
39
+ * on the amount of data that may be transferred or how long it may be open
40
+ * for.
190
41
  *
191
- * These limits are typically enforced by a relay server, if the protocol
192
- * will be transferring a lot of data or the stream will be open for a long time
42
+ * These limits are typically enforced by a relay server, if the protocol will
43
+ * be transferring a lot of data or the stream will be open for a long time
193
44
  * consider upgrading to a direct connection before opening the stream.
194
45
  *
195
46
  * @default false
@@ -224,38 +75,13 @@ export interface NewStreamOptions extends AbortOptions {
224
75
  negotiateFully?: boolean
225
76
  }
226
77
 
227
- export type ConnectionStatus = 'open' | 'closing' | 'closed'
228
-
229
- /**
230
- * Connection limits are present on connections that are only allowed to
231
- * transfer a certain amount of bytes or be open for a certain number
232
- * of seconds.
233
- *
234
- * These limits are applied by Circuit Relay v2 servers, for example and
235
- * the connection will normally be closed abruptly if the limits are
236
- * exceeded.
237
- */
238
- export interface ConnectionLimits {
239
- /**
240
- * If present this is the number of bytes remaining that may be
241
- * transferred over this connection
242
- */
243
- bytes?: bigint
244
-
245
- /**
246
- * If present this is the number of seconds that this connection will
247
- * remain open for
248
- */
249
- seconds?: number
250
- }
251
-
252
78
  /**
253
79
  * A Connection is a high-level representation of a connection
254
80
  * to a remote peer that may have been secured by encryption and
255
81
  * multiplexed, depending on the configuration of the nodes
256
82
  * between which the connection is made.
257
83
  */
258
- export interface Connection {
84
+ export interface Connection extends TypedEventTarget<Omit<MessageStreamEvents, 'drain' | 'message'>> {
259
85
  /**
260
86
  * The unique identifier for this connection
261
87
  */
@@ -271,11 +97,6 @@ export interface Connection {
271
97
  */
272
98
  remotePeer: PeerId
273
99
 
274
- /**
275
- * A list of tags applied to this connection
276
- */
277
- tags: string[]
278
-
279
100
  /**
280
101
  * A list of open streams on this connection
281
102
  */
@@ -284,12 +105,12 @@ export interface Connection {
284
105
  /**
285
106
  * Outbound connections are opened by the local node, inbound streams are opened by the remote
286
107
  */
287
- direction: Direction
108
+ direction: MessageStreamDirection
288
109
 
289
110
  /**
290
- * Lifecycle times for the connection
111
+ * When stream life cycle events occurred
291
112
  */
292
- timeline: ConnectionTimeline
113
+ timeline: MultiaddrConnectionTimeline
293
114
 
294
115
  /**
295
116
  * The multiplexer negotiated for this connection
@@ -306,6 +127,11 @@ export interface Connection {
306
127
  */
307
128
  status: ConnectionStatus
308
129
 
130
+ /**
131
+ * Whether this connection is direct or, for example, is via a relay
132
+ */
133
+ direct: boolean
134
+
309
135
  /**
310
136
  * If present, this connection has limits applied to it, perhaps by an
311
137
  * intermediate relay. Once the limits have been reached the connection will
@@ -320,26 +146,28 @@ export interface Connection {
320
146
  */
321
147
  rtt?: number
322
148
 
149
+ /**
150
+ * The connection logger, used to log connection-specific information
151
+ */
152
+ log: Logger
153
+
323
154
  /**
324
155
  * Create a new stream on this connection and negotiate one of the passed protocols
325
156
  */
326
157
  newStream(protocols: string | string[], options?: NewStreamOptions): Promise<Stream>
327
158
 
328
159
  /**
329
- * Gracefully close the connection. All queued data will be written to the
330
- * underlying transport.
160
+ * Gracefully close the connection. The returned promise will resolve when all
161
+ * queued data has been written to the underlying transport. Any unread data
162
+ * will still be emitted as a 'message' event.
331
163
  */
332
164
  close(options?: AbortOptions): Promise<void>
333
165
 
334
166
  /**
335
- * Immediately close the connection, any queued data will be discarded
167
+ * Immediately close the connection. Any data queued to be sent or read will
168
+ * be discarded.
336
169
  */
337
170
  abort(err: Error): void
338
-
339
- /**
340
- * The connection logger
341
- */
342
- log: Logger
343
171
  }
344
172
 
345
173
  export const connectionSymbol = Symbol.for('@libp2p/connection')
@@ -347,63 +175,3 @@ export const connectionSymbol = Symbol.for('@libp2p/connection')
347
175
  export function isConnection (other: any): other is Connection {
348
176
  return other != null && Boolean(other[connectionSymbol])
349
177
  }
350
-
351
- export interface ConnectionProtector {
352
- /**
353
- * Takes a given Connection and creates a private encryption stream
354
- * between its two peers from the PSK the Protector instance was
355
- * created with.
356
- */
357
- protect(connection: MultiaddrConnection, options?: AbortOptions): Promise<MultiaddrConnection>
358
- }
359
-
360
- export interface MultiaddrConnectionTimeline {
361
- /**
362
- * When the connection was opened
363
- */
364
- open: number
365
-
366
- /**
367
- * When the MultiaddrConnection was upgraded to a Connection - the type of
368
- * connection encryption and multiplexing was negotiated.
369
- */
370
- upgraded?: number
371
-
372
- /**
373
- * When the connection was closed.
374
- */
375
- close?: number
376
- }
377
-
378
- /**
379
- * A MultiaddrConnection is returned by transports after dialing
380
- * a peer. It is a low-level primitive and is the raw connection
381
- * without encryption or stream multiplexing.
382
- */
383
- export interface MultiaddrConnection extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> {
384
- /**
385
- * Gracefully close the connection. All queued data will be written to the
386
- * underlying transport.
387
- */
388
- close(options?: AbortOptions): Promise<void>
389
-
390
- /**
391
- * Immediately close the connection, any queued data will be discarded
392
- */
393
- abort(err: Error): void
394
-
395
- /**
396
- * The address of the remote end of the connection
397
- */
398
- remoteAddr: Multiaddr
399
-
400
- /**
401
- * When connection life cycle events occurred
402
- */
403
- timeline: MultiaddrConnectionTimeline
404
-
405
- /**
406
- * The multiaddr connection logger
407
- */
408
- log: Logger
409
- }
package/src/errors.ts CHANGED
@@ -144,6 +144,18 @@ export class StreamResetError extends Error {
144
144
  }
145
145
  }
146
146
 
147
+ /**
148
+ * Thrown when a protocol stream is aborted locally
149
+ */
150
+ export class StreamAbortedError extends Error {
151
+ static name = 'StreamAbortedError'
152
+
153
+ constructor (message = 'The stream has been aborted') {
154
+ super(message)
155
+ this.name = 'StreamAbortedError'
156
+ }
157
+ }
158
+
147
159
  /**
148
160
  * Thrown when a stream is in an invalid state
149
161
  */
@@ -156,6 +168,18 @@ export class StreamStateError extends Error {
156
168
  }
157
169
  }
158
170
 
171
+ /**
172
+ * Thrown when a stream buffer is full
173
+ */
174
+ export class StreamBufferError extends Error {
175
+ static name = 'StreamBufferError'
176
+
177
+ constructor (message = 'The stream buffer was full') {
178
+ super(message)
179
+ this.name = 'StreamBufferError'
180
+ }
181
+ }
182
+
159
183
  /**
160
184
  * Thrown when a value could not be found
161
185
  */
package/src/events.ts ADDED
@@ -0,0 +1,44 @@
1
+ import type { Uint8ArrayList } from 'uint8arraylist'
2
+
3
+ /**
4
+ * A custom implementation of MessageEvent as the Undici version does too much
5
+ * validation in it's constructor so is very slow.
6
+ */
7
+ export class StreamMessageEvent extends Event {
8
+ public data: Uint8Array | Uint8ArrayList
9
+
10
+ constructor (data: Uint8Array | Uint8ArrayList, eventInitDict?: EventInit) {
11
+ super('message', eventInitDict)
12
+
13
+ this.data = data
14
+ }
15
+ }
16
+
17
+ /**
18
+ * An event dispatched when the stream is closed. The `error` property can be
19
+ * inspected to discover if the closing was graceful or not, and the `remote`
20
+ * property shows which end of the stream initiated the closure
21
+ */
22
+ export class StreamCloseEvent extends Event {
23
+ public error?: Error
24
+ public local?: boolean
25
+
26
+ constructor (local?: boolean, error?: Error, eventInitDict?: EventInit) {
27
+ super('close', eventInitDict)
28
+
29
+ this.error = error
30
+ this.local = local
31
+ }
32
+ }
33
+
34
+ export class StreamAbortEvent extends StreamCloseEvent {
35
+ constructor (error: Error, eventInitDict?: EventInit) {
36
+ super(true, error, eventInitDict)
37
+ }
38
+ }
39
+
40
+ export class StreamResetEvent extends StreamCloseEvent {
41
+ constructor (error: Error, eventInitDict?: EventInit) {
42
+ super(false, error, eventInitDict)
43
+ }
44
+ }
package/src/index.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  * ```
15
15
  */
16
16
 
17
- import type { Connection, NewStreamOptions, Stream } from './connection.js'
17
+ import type { Connection, NewStreamOptions } from './connection.js'
18
18
  import type { ContentRouting } from './content-routing.js'
19
19
  import type { Ed25519PublicKey, PublicKey, RSAPublicKey, Secp256k1PublicKey } from './keys.js'
20
20
  import type { Metrics } from './metrics.js'
@@ -23,7 +23,8 @@ import type { PeerInfo } from './peer-info.js'
23
23
  import type { PeerRouting } from './peer-routing.js'
24
24
  import type { Address, Peer, PeerStore } from './peer-store.js'
25
25
  import type { Startable } from './startable.js'
26
- import type { StreamHandler, StreamHandlerOptions } from './stream-handler.js'
26
+ import type { StreamHandler, StreamHandlerOptions, StreamMiddleware } from './stream-handler.js'
27
+ import type { Stream } from './stream.js'
27
28
  import type { Topology } from './topology.js'
28
29
  import type { Listener, OutboundConnectionUpgradeEvents } from './transport.js'
29
30
  import type { DNS } from '@multiformats/dns'
@@ -780,6 +781,33 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
780
781
  */
781
782
  unregister(id: string): void
782
783
 
784
+ /**
785
+ * Registers one or more middleware implementations that will be invoked for
786
+ * incoming and outgoing protocol streams that match the passed protocol.
787
+ *
788
+ * @example
789
+ *
790
+ * ```TypeScript
791
+ * libp2p.use('/my/protocol/1.0.0', (stream, connection, next) => {
792
+ * // do something with stream and/or connection
793
+ * next(stream, connection)
794
+ * })
795
+ * ```
796
+ */
797
+ use (protocol: string, middleware: StreamMiddleware | StreamMiddleware[]): void
798
+
799
+ /**
800
+ * Deregisters all middleware for the passed protocol.
801
+ *
802
+ * @example
803
+ *
804
+ * ```TypeScript
805
+ * libp2p.unuse('/my/protocol/1.0.0')
806
+ * // any previously registered middleware will no longer be invoked
807
+ * ```
808
+ */
809
+ unuse (protocol: string): void
810
+
783
811
  /**
784
812
  * Returns the public key for the passed PeerId. If the PeerId is of the 'RSA'
785
813
  * type this may mean searching the routing if the peer's key is not present
@@ -915,20 +943,25 @@ export const serviceDependencies = Symbol.for('@libp2p/service-dependencies')
915
943
  export * from './connection.js'
916
944
  export * from './connection-encrypter.js'
917
945
  export * from './connection-gater.js'
946
+ export * from './connection-protector.js'
918
947
  export * from './content-routing.js'
948
+ export * from './errors.js'
949
+ export * from './events.js'
919
950
  export * from './keys.js'
951
+ export * from './message-stream.js'
920
952
  export * from './metrics.js'
953
+ export * from './multiaddr-connection.js'
921
954
  export * from './peer-discovery.js'
922
955
  export * from './peer-id.js'
923
956
  export * from './peer-info.js'
924
957
  export * from './peer-routing.js'
925
958
  export * from './peer-store.js'
926
- export * from './pubsub.js'
927
959
  export * from './record.js'
960
+ export * from './startable.js'
928
961
  export * from './stream-handler.js'
929
962
  export * from './stream-muxer.js'
963
+ export * from './stream.js'
930
964
  export * from './topology.js'
931
965
  export * from './transport.js'
932
- export * from './errors.js'
966
+
933
967
  export * from 'main-event'
934
- export * from './startable.js'