@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
@@ -0,0 +1,183 @@
1
+ import type { Logger, StreamCloseEvent, StreamMessageEvent, TypedEventTarget, AbortOptions } from './index.js'
2
+ import type { Uint8ArrayList } from 'uint8arraylist'
3
+
4
+ /**
5
+ * The direction of the message stream
6
+ */
7
+ export type MessageStreamDirection = 'inbound' | 'outbound'
8
+
9
+ /**
10
+ * The states a message stream can be in
11
+ */
12
+ export type MessageStreamStatus = 'open' | 'closing' | 'closed' | 'aborted' | 'reset'
13
+
14
+ /**
15
+ * The states the readable end of a message stream can be in
16
+ */
17
+ export type MessageStreamReadStatus = 'readable' | 'paused' | 'closing' | 'closed'
18
+
19
+ /**
20
+ * The states the writable end of a message stream can be in
21
+ */
22
+ export type MessageStreamWriteStatus = 'writable' | 'closing' | 'closed'
23
+
24
+ /**
25
+ * An object that records the times of various events
26
+ */
27
+ export interface MessageStreamTimeline {
28
+ /**
29
+ * A timestamp of when the message stream was opened
30
+ */
31
+ open: number
32
+
33
+ /**
34
+ * A timestamp of when the message stream was closed for both reading and
35
+ * writing by both ends of the stream
36
+ */
37
+ close?: number
38
+ }
39
+
40
+ export interface MessageStreamEvents {
41
+ /**
42
+ * Data was received from the remote end of the message stream
43
+ */
44
+ message: StreamMessageEvent
45
+
46
+ /**
47
+ * The local send buffer has emptied and the stream may be written to once
48
+ * more, unless it is currently closing.
49
+ */
50
+ drain: Event
51
+
52
+ /**
53
+ * The underlying resource is closed - no further events will be emitted and
54
+ * the stream cannot be used to send or receive any more data.
55
+ *
56
+ * When the `.error` field is set, the `local` property of the event will be
57
+ * `true` value if the `.abort` was invoked, otherwise it means a remote error
58
+ * occurred and the peer sent a reset signal.
59
+ */
60
+ close: StreamCloseEvent
61
+
62
+ /**
63
+ * Where the stream implementation supports half-closing, it may emit this
64
+ * event when the remote end of the stream closes it's writable end.
65
+ *
66
+ * After this event is received no further 'message' events will be emitted
67
+ * though the stream can still be written to, if it has not been closed at
68
+ * this end.
69
+ */
70
+ remoteCloseWrite: Event
71
+
72
+ /**
73
+ * The outgoing write queue emptied - there are no more bytes queued for
74
+ * sending to the remote end of the stream.
75
+ */
76
+ idle: Event
77
+ }
78
+
79
+ export interface MessageStream<Timeline extends MessageStreamTimeline = MessageStreamTimeline> extends TypedEventTarget<MessageStreamEvents>, AsyncIterable<Uint8Array | Uint8ArrayList> {
80
+ /**
81
+ * The current status of the message stream
82
+ */
83
+ status: MessageStreamStatus
84
+
85
+ /**
86
+ * Timestamps of when stream events occurred
87
+ */
88
+ timeline: Timeline
89
+
90
+ /**
91
+ * A logging implementation that can be used to log stream-specific messages
92
+ */
93
+ log: Logger
94
+
95
+ /**
96
+ * Whether this stream is inbound or outbound
97
+ */
98
+ direction: MessageStreamDirection
99
+
100
+ /**
101
+ * The maximum number of bytes to store when paused. If receipt of more bytes
102
+ * from the remote end of the stream causes the buffer size to exceed this
103
+ * value the stream will be reset and a 'close' event emitted.
104
+ *
105
+ * This value can be changed at runtime.
106
+ */
107
+ maxReadBufferLength: number
108
+
109
+ /**
110
+ * When the `.send` method returns false it means that the underlying resource
111
+ * has signalled that it's write buffer is full. If the user continues to call
112
+ * `.send`, outgoing bytes are stored in an internal buffer until the
113
+ * underlying resource signals that it can accept more data.
114
+ *
115
+ * If the size of that internal buffer exceed this value the stream will be
116
+ * reset and a 'close' event emitted.
117
+ *
118
+ * This value can be changed at runtime.
119
+ */
120
+ maxWriteBufferLength?: number
121
+
122
+ /**
123
+ * If no data is transmitted over the stream in this many ms, the stream will
124
+ * be aborted with an InactivityTimeoutError
125
+ */
126
+ inactivityTimeout: number
127
+
128
+ /**
129
+ * If this property is `true`, the underlying transport has signalled that its
130
+ * write buffer is full and that `.send` should not be called again.
131
+ *
132
+ * A `drain` event will be emitted after which is its safe to call `.send`
133
+ * again to resume sending.
134
+ */
135
+ writableNeedsDrain: boolean
136
+
137
+ /**
138
+ * Write data to the stream. If the method returns false it means the
139
+ * internal buffer is now full and the caller should wait for the 'drain'
140
+ * event before sending more data.
141
+ *
142
+ * This method may throw if:
143
+ * - The internal send buffer is full
144
+ * - The stream has previously been closed for writing locally or remotely
145
+ */
146
+ send (data: Uint8Array | Uint8ArrayList): boolean
147
+
148
+ /**
149
+ * Stop accepting new data to send and return a promise that resolves when any
150
+ * unsent data has been written into the underlying resource.
151
+ */
152
+ close (options?: AbortOptions): Promise<void>
153
+
154
+ /**
155
+ * Stop accepting new data to send, discard any unsent/unread data, and emit a
156
+ * 'close' event with the 'error' property set to the passed error.
157
+ */
158
+ abort (err: Error): void
159
+
160
+ /**
161
+ * Stop emitting further 'message' events. Any received data will be stored in
162
+ * an internal buffer. If the buffer size reaches `maxReadBufferLength`, the
163
+ * stream will be reset and a StreamAbortEvent emitted.
164
+ *
165
+ * If the underlying resource supports it, the remote peer will be instructed
166
+ * to pause transmission of further data.
167
+ */
168
+ pause (): void
169
+
170
+ /**
171
+ * Resume emitting 'message' events.
172
+ *
173
+ * If the underlying resource supports it, the remote peer will be informed
174
+ * that it is ok to start sending data again.
175
+ */
176
+ resume (): void
177
+
178
+ /**
179
+ * Queue the passed data to be emitted as a 'message' event either during the
180
+ * next tick or sooner if data is received from the underlying resource.
181
+ */
182
+ push (buf: Uint8Array | Uint8ArrayList): void
183
+ }
package/src/metrics.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { MultiaddrConnection, Stream, Connection } from './connection.js'
1
+ import type { MultiaddrConnection, Stream } from './index.js'
2
2
 
3
3
  /**
4
4
  * Create tracked metrics with these options. Loosely based on the
@@ -434,7 +434,7 @@ export interface Metrics {
434
434
  /**
435
435
  * Track a newly opened protocol stream
436
436
  */
437
- trackProtocolStream(stream: Stream, connection: Connection): void
437
+ trackProtocolStream(stream: Stream): void
438
438
 
439
439
  /**
440
440
  * Register an arbitrary metric. Call this to set help/labels for metrics
@@ -0,0 +1,27 @@
1
+ import type { MessageStream, MessageStreamTimeline } from './message-stream.ts'
2
+ import type { Multiaddr } from '@multiformats/multiaddr'
3
+
4
+ export interface MultiaddrConnectionTimeline extends MessageStreamTimeline {
5
+ /**
6
+ * When the MultiaddrConnection was upgraded to a Connection - the type of
7
+ * connection encryption and multiplexing was negotiated.
8
+ */
9
+ upgraded?: number
10
+ }
11
+
12
+ /**
13
+ * A MultiaddrConnection is returned by a transport after dialing a peer. It is
14
+ * a low-level primitive and is the raw connection, typically without encryption
15
+ * or stream multiplexing.
16
+ */
17
+ export interface MultiaddrConnection extends MessageStream<MultiaddrConnectionTimeline> {
18
+ /**
19
+ * The address of the remote end of the connection
20
+ */
21
+ remoteAddr: Multiaddr
22
+
23
+ /**
24
+ * When stream life cycle events occurred
25
+ */
26
+ timeline: MultiaddrConnectionTimeline
27
+ }
package/src/peer-store.ts CHANGED
@@ -314,9 +314,4 @@ export interface PeerStore {
314
314
  * ```
315
315
  */
316
316
  consumePeerRecord(buf: Uint8Array, options?: ConsumePeerRecordOptions): Promise<boolean>
317
-
318
- /**
319
- * @deprecated Pass `expectedPeer` as a property of `options` instead
320
- */
321
- consumePeerRecord(buf: Uint8Array, expectedPeer?: PeerId, options?: AbortOptions): Promise<boolean>
322
317
  }
@@ -1,23 +1,17 @@
1
- import type { Connection, Stream } from './connection.js'
2
- import type { AbortOptions } from './index.ts'
3
-
4
- export interface IncomingStreamData {
5
- /**
6
- * The newly opened stream
7
- */
8
- stream: Stream
9
-
10
- /**
11
- * The connection the stream was opened on
12
- */
13
- connection: Connection
14
- }
1
+ import type { AbortOptions, Connection, Stream } from './index.ts'
15
2
 
16
3
  export interface StreamHandler {
17
4
  /**
18
5
  * A callback function that accepts the incoming stream data
19
6
  */
20
- (data: IncomingStreamData): void | Promise<void>
7
+ (stream: Stream, connection: Connection): void | Promise<void>
8
+ }
9
+
10
+ /**
11
+ * Stream middleware allows accessing stream data outside of the stream handler
12
+ */
13
+ export interface StreamMiddleware {
14
+ (stream: Stream, connection: Connection, next: (stream: Stream, connection: Connection) => void): void | Promise<void>
21
15
  }
22
16
 
23
17
  export interface StreamHandlerOptions extends AbortOptions {
@@ -46,6 +40,11 @@ export interface StreamHandlerOptions extends AbortOptions {
46
40
  * protocol(s), the existing handler will be discarded.
47
41
  */
48
42
  force?: true
43
+
44
+ /**
45
+ * Middleware allows accessing stream data outside of the stream handler
46
+ */
47
+ middleware?: StreamMiddleware[]
49
48
  }
50
49
 
51
50
  export interface StreamHandlerRecord {
@@ -1,68 +1,160 @@
1
- import type { Direction, Stream } from './connection.js'
2
- import type { AbortOptions, Logger } from './index.js'
3
- import type { Duplex } from 'it-stream-types'
4
- import type { Uint8ArrayList } from 'uint8arraylist'
1
+ import type { Stream, TypedEventTarget, MessageStream, AbortOptions } from './index.js'
5
2
 
6
- export interface StreamMuxerFactory {
3
+ /**
4
+ * User-facing message stream muxer options
5
+ */
6
+ export interface StreamMuxerOptions<MuxedStreamOptions extends StreamOptions = StreamOptions> {
7
7
  /**
8
- * The protocol used to select this muxer during connection opening
8
+ * Configuration options for each outgoing/incoming stream
9
9
  */
10
- protocol: string
10
+ streamOptions?: MuxedStreamOptions
11
11
 
12
12
  /**
13
- * Creates a new stream muxer to be used with a new connection
13
+ * libp2p is notified of incoming streams via the muxer's 'stream' event.
14
+ *
15
+ * During connection establishment there may be a small window where a muxer
16
+ * starts to process incoming stream data before a listener has been added for
17
+ * the 'stream' event.
18
+ *
19
+ * If no handler is registered for this event incoming streams can be missed
20
+ * so when this is the case muxers queue streams internally as "early
21
+ * streams", and will defer emitting the 'stream' event until after a listener
22
+ * has been registered.
23
+ *
24
+ * Allowing an unlimited amount of early streams can cause excessive memory
25
+ * consumption so this setting controls how many early streams to store when
26
+ * no 'stream' listener has been registered.
27
+ *
28
+ * If more streams than this are opened before a listener is added the muxed
29
+ * connection will be reset.
30
+ *
31
+ * @default 10
32
+ */
33
+ maxEarlyStreams?: number
34
+
35
+ /**
36
+ * Maximum size of a message in bytes that we'll send on a stream - this
37
+ * ensures that a single stream doesn't hog a connection
38
+ *
39
+ * @default 65_536
40
+ */
41
+ maxMessageSize?: number
42
+
43
+ /**
44
+ * Maximum number of concurrent inbound streams that we accept - if the peer
45
+ * tries to open more streams, those will be reset immediately
46
+ *
47
+ * @default 1_000
14
48
  */
15
- createStreamMuxer(init?: StreamMuxerInit): StreamMuxer
49
+ maxInboundStreams?: number
50
+
51
+ /**
52
+ * Maximum number of concurrent outbound streams that we accept - if the
53
+ * application tries to open more streams, the call to `newStream` will throw
54
+ *
55
+ * @default 1_000
56
+ */
57
+ maxOutboundStreams?: number
16
58
  }
17
59
 
18
60
  /**
19
- * A libp2p stream muxer
61
+ * User-facing message stream options
20
62
  */
21
- export interface StreamMuxer extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> {
63
+ export interface StreamOptions {
64
+ /**
65
+ * If no data is sent or received in this number of ms the stream will be
66
+ * reset and an 'error' event emitted.
67
+ *
68
+ * @default 120_000
69
+ */
70
+ inactivityTimeout?: number
71
+
72
+ /**
73
+ * The maximum number of bytes to store when paused or before a 'message'
74
+ * event handler is added.
75
+ *
76
+ * If the internal buffer overflows this value the stream will be reset.
77
+ *
78
+ * @default 4_194_304
79
+ */
80
+ maxReadBufferLength?: number
81
+
82
+ /**
83
+ * The maximum number of bytes to store when the remote end of the stream is
84
+ * applying backpressure, or when it is slow to accept new bytes.
85
+ *
86
+ * If the internal buffer overflows this value the stream will be reset.
87
+ *
88
+ * @default Infinity
89
+ */
90
+ maxWriteBufferLength?: number
91
+ }
92
+
93
+ export interface StreamMuxerFactory<Muxer extends StreamMuxer = StreamMuxer> {
22
94
  /**
23
95
  * The protocol used to select this muxer during connection opening
24
96
  */
25
97
  protocol: string
26
98
 
27
99
  /**
28
- * A list of streams that are currently open. Closed streams will not be returned.
100
+ * Creates a new stream muxer to be used with a new connection
101
+ */
102
+ createStreamMuxer(maConn: MessageStream): Muxer
103
+ }
104
+
105
+ export interface StreamMuxerEvents<MuxedStream extends Stream = Stream> {
106
+ /**
107
+ * An incoming stream was created
29
108
  */
30
- readonly streams: Stream[]
109
+ stream: CustomEvent<MuxedStream>
110
+ }
111
+
112
+ export interface CreateStreamOptions extends AbortOptions, StreamOptions {
31
113
  /**
32
- * Initiate a new stream with the given name. If no name is
33
- * provided, the id of the stream will be used.
114
+ * If a single protocol was requested and the muxer has support for this,
115
+ * pre-negotiate the protocol using this value, otherwise multistream-select
116
+ * will be run over the stream after opening.
34
117
  */
35
- newStream(name?: string): Stream | Promise<Stream>
118
+ protocol?: string
119
+ }
36
120
 
121
+ export type StreamMuxerStatus = 'open' | 'closing' | 'closed'
122
+
123
+ /**
124
+ * A libp2p stream muxer
125
+ */
126
+ export interface StreamMuxer<MuxedStream extends Stream = Stream> extends TypedEventTarget<StreamMuxerEvents<MuxedStream>> {
37
127
  /**
38
- * Close or abort all tracked streams and stop the muxer
128
+ * The protocol used to select this muxer during connection opening
39
129
  */
40
- close(options?: AbortOptions): Promise<void>
130
+ protocol: string
41
131
 
42
132
  /**
43
- * Close or abort all tracked streams and stop the muxer
133
+ * A list of streams that are currently open
44
134
  */
45
- abort(err: Error): void
46
- }
135
+ streams: MuxedStream[]
47
136
 
48
- export interface StreamMuxerInit {
49
137
  /**
50
- * A callback function invoked every time an incoming stream is opened
138
+ * The status of the muxer
51
139
  */
52
- onIncomingStream?(stream: Stream): void
140
+ status: StreamMuxerStatus
53
141
 
54
142
  /**
55
- * A callback function invoke every time a stream ends
143
+ * Create a new stream
56
144
  */
57
- onStreamEnd?(stream: Stream): void
145
+ createStream(options?: CreateStreamOptions): Promise<MuxedStream>
58
146
 
59
147
  /**
60
- * Outbound stream muxers are opened by the local node, inbound stream muxers are opened by the remote
148
+ * Immediately close the muxer, abort every open stream and discard any
149
+ * unsent/unread data.
61
150
  */
62
- direction?: Direction
151
+ abort (err: Error): void
63
152
 
64
153
  /**
65
- * The logger used by the connection
154
+ * Gracefully close the muxer. All open streams will be gracefully closed, and
155
+ * the returned promise will either resolve when any/all unsent data has been
156
+ * sent, or it will reject if the passed abort signal fires before this
157
+ * happens.
66
158
  */
67
- log?: Logger
159
+ close (options?: AbortOptions): Promise<void>
68
160
  }
package/src/stream.ts ADDED
@@ -0,0 +1,70 @@
1
+ import type { AbortOptions } from './index.ts'
2
+ import type { MessageStream, MessageStreamReadStatus, MessageStreamWriteStatus } from './message-stream.js'
3
+
4
+ /**
5
+ * A Stream is a lightweight data channel between two peers that can be written
6
+ * to and read from at both ends.
7
+ *
8
+ * It is half-closable - that is in order for it to be closed fully and any
9
+ * associated memory reclaimed, both ends must close their writeable end of the
10
+ * stream.
11
+ *
12
+ * It's also possible to close the readable end of the stream, but this depends
13
+ * on the underlying stream muxer supporting this operation which not all do.
14
+ */
15
+ export interface Stream extends MessageStream {
16
+ /**
17
+ * Unique identifier for a stream. Identifiers are not unique across muxers.
18
+ */
19
+ id: string
20
+
21
+ /**
22
+ * The protocol negotiated for this stream
23
+ */
24
+ protocol: string
25
+
26
+ /**
27
+ * The status of the readable end of the stream
28
+ */
29
+ readStatus: MessageStreamReadStatus
30
+
31
+ /**
32
+ * The status of the writable end of the stream
33
+ */
34
+ writeStatus: MessageStreamWriteStatus
35
+
36
+ /**
37
+ * The status of the readable end of the remote end of the stream - n.b. this
38
+ * requires the underlying stream transport to support sending STOP_SENDING
39
+ * messages or similar.
40
+ */
41
+ remoteReadStatus: MessageStreamReadStatus
42
+
43
+ /**
44
+ * The status of the writable end of the remote end of the stream
45
+ */
46
+ remoteWriteStatus: MessageStreamWriteStatus
47
+
48
+ /**
49
+ * Close stream for writing and return a promise that resolves once any
50
+ * pending data has been passed to the underlying transport.
51
+ *
52
+ * Note that the stream itself will remain readable until the remote end also
53
+ * closes it's writable end.
54
+ *
55
+ * To close without waiting for the remote, call `.abort` instead. If you want
56
+ * to wait for data to be sent first, ensure if the `.writableStatus` property
57
+ * is not 'paused', if it is, wait for a `drain` event before aborting.
58
+ */
59
+ close (options?: AbortOptions): Promise<void>
60
+
61
+ /**
62
+ * Send a message to the remote end of the stream informing them that any
63
+ * incoming data will be discarded so they should stop sending.
64
+ *
65
+ * This requires the underlying resource to support this operation - for
66
+ * example the QUIC, WebTransport, WebRTC transports do but anything
67
+ * multiplexed using Yamux or Mplex do not.
68
+ */
69
+ closeRead(options?: AbortOptions): Promise<void>
70
+ }
package/src/topology.ts CHANGED
@@ -5,7 +5,7 @@ import type { PeerId } from './peer-id.js'
5
5
  * A topology filter - this can be used by topologies to ensure they do not
6
6
  * receive duplicate notifications of individual peers
7
7
  *
8
- * @see https://libp2p.github.io/js-libp2p/functions/_libp2p_peer_collections.peerFilter-1.html
8
+ * @see https://libp2p.github.io/js-libp2p/classes/_libp2p_peer-collections.PeerFilter.html
9
9
  */
10
10
  export interface TopologyFilter {
11
11
  has (peerId: PeerId): boolean
@@ -40,11 +40,11 @@ export interface Topology {
40
40
  * Invoked when a new connection is opened to a peer that supports the
41
41
  * registered protocol
42
42
  */
43
- onConnect?(peerId: PeerId, conn: Connection): void
43
+ onConnect?(peerId: PeerId, conn: Connection): void | Promise<void>
44
44
 
45
45
  /**
46
46
  * Invoked when the last connection to a peer that supports the registered
47
47
  * protocol closes
48
48
  */
49
- onDisconnect?(peerId: PeerId): void
49
+ onDisconnect?(peerId: PeerId): void | Promise<void>
50
50
  }
package/src/transport.ts CHANGED
@@ -1,6 +1,4 @@
1
- import type { Connection, ConnectionLimits, MultiaddrConnection } from './connection.js'
2
- import type { AbortOptions, ClearableSignal, ConnectionEncrypter } from './index.js'
3
- import type { StreamMuxerFactory } from './stream-muxer.js'
1
+ import type { AbortOptions, ClearableSignal, ConnectionEncrypter, MultiaddrConnection, Connection, ConnectionLimits, StreamMuxerFactory, PeerId } from './index.js'
4
2
  import type { Multiaddr } from '@multiformats/multiaddr'
5
3
  import type { TypedEventTarget } from 'main-event'
6
4
  import type { ProgressOptions, ProgressEvent } from 'progress-events'
@@ -29,16 +27,19 @@ export interface Listener extends TypedEventTarget<ListenerEvents> {
29
27
  * Start a listener
30
28
  */
31
29
  listen(multiaddr: Multiaddr): Promise<void>
30
+
32
31
  /**
33
32
  * Get listen addresses
34
33
  */
35
34
  getAddrs(): Multiaddr[]
35
+
36
36
  /**
37
37
  * Close listener
38
38
  *
39
39
  * @returns {Promise<void>}
40
40
  */
41
41
  close(): Promise<void>
42
+
42
43
  /**
43
44
  * Allows transports to amend announce addresses - to add certificate hashes
44
45
  * or other metadata that cannot be known before runtime
@@ -134,15 +135,6 @@ export enum FaultTolerance {
134
135
  * Options accepted by the upgrader during connection establishment
135
136
  */
136
137
  export interface UpgraderOptions<ConnectionUpgradeEvents extends ProgressEvent = ProgressEvent> extends ProgressOptions<ConnectionUpgradeEvents>, Required<AbortOptions> {
137
- /**
138
- * If true the invoking transport is expected to implement it's own encryption
139
- * and an encryption protocol will not attempted to be negotiated via
140
- * multi-stream select
141
- *
142
- * @default false
143
- */
144
- skipEncryption?: boolean
145
-
146
138
  /**
147
139
  * If true no connection protection will be performed on the connection.
148
140
  */
@@ -172,6 +164,23 @@ export interface UpgraderOptions<ConnectionUpgradeEvents extends ProgressEvent =
172
164
  initiator?: boolean
173
165
  }
174
166
 
167
+ /**
168
+ * Options accepted by the upgrader during connection establishment
169
+ */
170
+ export interface UpgraderWithoutEncryptionOptions extends UpgraderOptions {
171
+ /**
172
+ * If true the invoking transport is expected to implement it's own encryption
173
+ * and an encryption protocol will not attempted to be negotiated via
174
+ * multi-stream select
175
+ */
176
+ skipEncryption: true
177
+
178
+ /**
179
+ * If `skipEncryption` is true, a remote PeerId must be supplied
180
+ */
181
+ remotePeer: PeerId
182
+ }
183
+
175
184
  export type InboundConnectionUpgradeEvents =
176
185
  ProgressEvent<'upgrader:encrypt-inbound-connection'> |
177
186
  ProgressEvent<'upgrader:multiplex-inbound-connection'>
@@ -184,13 +193,15 @@ export interface Upgrader {
184
193
  /**
185
194
  * Upgrades an outbound connection created by the `dial` method of a transport
186
195
  */
187
- upgradeOutbound(maConn: MultiaddrConnection, opts?: UpgraderOptions<OutboundConnectionUpgradeEvents>): Promise<Connection>
196
+ upgradeOutbound(maConn: MultiaddrConnection, opts: UpgraderOptions<OutboundConnectionUpgradeEvents>): Promise<Connection>
197
+ upgradeOutbound(maConn: MultiaddrConnection, opts: UpgraderWithoutEncryptionOptions): Promise<Connection>
188
198
 
189
199
  /**
190
200
  * Upgrades an inbound connection received by a transport listener and
191
201
  * notifies other libp2p components about the new connection
192
202
  */
193
- upgradeInbound(maConn: MultiaddrConnection, opts?: UpgraderOptions<InboundConnectionUpgradeEvents>): Promise<void>
203
+ upgradeInbound(maConn: MultiaddrConnection, opts: UpgraderOptions<InboundConnectionUpgradeEvents>): Promise<void>
204
+ upgradeInbound(maConn: MultiaddrConnection, opts: UpgraderWithoutEncryptionOptions): Promise<void>
194
205
 
195
206
  /**
196
207
  * Used by transports that perform part of the upgrade process themselves and