libp2p 0.37.2 → 0.37.3-0bb1b80

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 (161) hide show
  1. package/README.md +1 -0
  2. package/dist/src/address-manager/index.d.ts +2 -2
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/circuit/auto-relay.d.ts +4 -4
  5. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  6. package/dist/src/circuit/circuit/hop.d.ts +2 -2
  7. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  8. package/dist/src/circuit/circuit/hop.js +2 -2
  9. package/dist/src/circuit/circuit/hop.js.map +1 -1
  10. package/dist/src/circuit/circuit/stop.d.ts +1 -1
  11. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  12. package/dist/src/circuit/circuit/stop.js +1 -1
  13. package/dist/src/circuit/circuit/stop.js.map +1 -1
  14. package/dist/src/circuit/circuit/stream-handler.d.ts +1 -1
  15. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  16. package/dist/src/circuit/index.d.ts +2 -2
  17. package/dist/src/circuit/index.d.ts.map +1 -1
  18. package/dist/src/circuit/listener.d.ts +3 -3
  19. package/dist/src/circuit/listener.d.ts.map +1 -1
  20. package/dist/src/circuit/transport.d.ts +5 -5
  21. package/dist/src/circuit/transport.d.ts.map +1 -1
  22. package/dist/src/circuit/transport.js +2 -2
  23. package/dist/src/circuit/transport.js.map +1 -1
  24. package/dist/src/config.d.ts.map +1 -1
  25. package/dist/src/config.js +25 -3
  26. package/dist/src/config.js.map +1 -1
  27. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  28. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  29. package/dist/src/connection-manager/dialer/auto-dialer.d.ts +2 -2
  30. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -1
  31. package/dist/src/connection-manager/dialer/dial-request.d.ts +1 -1
  32. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  33. package/dist/src/connection-manager/dialer/index.d.ts +9 -9
  34. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  35. package/dist/src/connection-manager/dialer/index.js +11 -11
  36. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  37. package/dist/src/connection-manager/index.d.ts +24 -20
  38. package/dist/src/connection-manager/index.d.ts.map +1 -1
  39. package/dist/src/connection-manager/index.js +199 -82
  40. package/dist/src/connection-manager/index.js.map +1 -1
  41. package/dist/src/constants.d.ts +4 -0
  42. package/dist/src/constants.d.ts.map +1 -1
  43. package/dist/src/constants.js +4 -0
  44. package/dist/src/constants.js.map +1 -1
  45. package/dist/src/content-routing/index.d.ts +4 -4
  46. package/dist/src/content-routing/index.d.ts.map +1 -1
  47. package/dist/src/content-routing/utils.d.ts +2 -2
  48. package/dist/src/content-routing/utils.d.ts.map +1 -1
  49. package/dist/src/dht/dht-content-routing.d.ts +3 -3
  50. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  51. package/dist/src/dht/dht-peer-routing.d.ts +5 -5
  52. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  53. package/dist/src/dht/dummy-dht.d.ts +3 -3
  54. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  55. package/dist/src/dht/dummy-dht.js +1 -1
  56. package/dist/src/dht/dummy-dht.js.map +1 -1
  57. package/dist/src/errors.d.ts +4 -1
  58. package/dist/src/errors.d.ts.map +1 -1
  59. package/dist/src/errors.js +3 -0
  60. package/dist/src/errors.js.map +1 -1
  61. package/dist/src/fetch/index.d.ts +11 -4
  62. package/dist/src/fetch/index.d.ts.map +1 -1
  63. package/dist/src/fetch/index.js +74 -42
  64. package/dist/src/fetch/index.js.map +1 -1
  65. package/dist/src/get-peer.d.ts +2 -2
  66. package/dist/src/get-peer.d.ts.map +1 -1
  67. package/dist/src/get-peer.js +1 -1
  68. package/dist/src/get-peer.js.map +1 -1
  69. package/dist/src/identify/index.d.ts +8 -8
  70. package/dist/src/identify/index.d.ts.map +1 -1
  71. package/dist/src/identify/index.js +23 -32
  72. package/dist/src/identify/index.js.map +1 -1
  73. package/dist/src/index.d.ts +17 -17
  74. package/dist/src/index.d.ts.map +1 -1
  75. package/dist/src/insecure/index.d.ts +2 -2
  76. package/dist/src/insecure/index.d.ts.map +1 -1
  77. package/dist/src/insecure/index.js +1 -1
  78. package/dist/src/insecure/index.js.map +1 -1
  79. package/dist/src/keychain/index.d.ts +2 -2
  80. package/dist/src/keychain/index.d.ts.map +1 -1
  81. package/dist/src/libp2p.d.ts +14 -14
  82. package/dist/src/libp2p.d.ts.map +1 -1
  83. package/dist/src/libp2p.js +16 -16
  84. package/dist/src/libp2p.js.map +1 -1
  85. package/dist/src/metrics/index.d.ts +3 -3
  86. package/dist/src/metrics/index.d.ts.map +1 -1
  87. package/dist/src/metrics/index.js +6 -2
  88. package/dist/src/metrics/index.js.map +1 -1
  89. package/dist/src/metrics/moving-average.d.ts +1 -1
  90. package/dist/src/metrics/moving-average.d.ts.map +1 -1
  91. package/dist/src/metrics/stats.d.ts +1 -1
  92. package/dist/src/metrics/stats.d.ts.map +1 -1
  93. package/dist/src/nat-manager.d.ts +1 -1
  94. package/dist/src/nat-manager.d.ts.map +1 -1
  95. package/dist/src/peer-record-updater.d.ts +1 -1
  96. package/dist/src/peer-record-updater.d.ts.map +1 -1
  97. package/dist/src/peer-routing.d.ts +4 -4
  98. package/dist/src/peer-routing.d.ts.map +1 -1
  99. package/dist/src/ping/index.d.ts +10 -3
  100. package/dist/src/ping/index.d.ts.map +1 -1
  101. package/dist/src/ping/index.js +21 -7
  102. package/dist/src/ping/index.js.map +1 -1
  103. package/dist/src/pnet/index.d.ts +1 -2
  104. package/dist/src/pnet/index.d.ts.map +1 -1
  105. package/dist/src/pnet/index.js.map +1 -1
  106. package/dist/src/pubsub/dummy-pubsub.d.ts +2 -2
  107. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
  108. package/dist/src/registrar.d.ts +10 -8
  109. package/dist/src/registrar.d.ts.map +1 -1
  110. package/dist/src/registrar.js +63 -44
  111. package/dist/src/registrar.js.map +1 -1
  112. package/dist/src/transport-manager.d.ts +3 -3
  113. package/dist/src/transport-manager.d.ts.map +1 -1
  114. package/dist/src/upgrader.d.ts +12 -6
  115. package/dist/src/upgrader.d.ts.map +1 -1
  116. package/dist/src/upgrader.js +151 -66
  117. package/dist/src/upgrader.js.map +1 -1
  118. package/dist/src/version.d.ts.map +1 -1
  119. package/dist/src/version.js.map +1 -1
  120. package/package.json +56 -32
  121. package/src/address-manager/index.ts +2 -2
  122. package/src/circuit/auto-relay.ts +4 -4
  123. package/src/circuit/circuit/hop.ts +4 -4
  124. package/src/circuit/circuit/stop.ts +2 -2
  125. package/src/circuit/circuit/stream-handler.ts +1 -1
  126. package/src/circuit/index.ts +2 -2
  127. package/src/circuit/listener.ts +3 -3
  128. package/src/circuit/transport.ts +5 -5
  129. package/src/config.ts +25 -3
  130. package/src/connection-manager/auto-dialler.ts +1 -1
  131. package/src/connection-manager/dialer/auto-dialer.ts +2 -2
  132. package/src/connection-manager/dialer/dial-request.ts +1 -1
  133. package/src/connection-manager/dialer/index.ts +15 -15
  134. package/src/connection-manager/index.ts +257 -97
  135. package/src/constants.ts +5 -0
  136. package/src/content-routing/index.ts +2 -2
  137. package/src/content-routing/utils.ts +2 -2
  138. package/src/dht/dht-content-routing.ts +2 -2
  139. package/src/dht/dht-peer-routing.ts +3 -3
  140. package/src/dht/dummy-dht.ts +3 -3
  141. package/src/errors.ts +4 -1
  142. package/src/fetch/index.ts +111 -53
  143. package/src/get-peer.ts +3 -3
  144. package/src/identify/index.ts +34 -40
  145. package/src/index.ts +17 -17
  146. package/src/insecure/index.ts +3 -3
  147. package/src/keychain/index.ts +2 -2
  148. package/src/libp2p.ts +33 -28
  149. package/src/metrics/index.ts +9 -5
  150. package/src/metrics/moving-average.ts +1 -1
  151. package/src/metrics/stats.ts +1 -1
  152. package/src/nat-manager.ts +1 -1
  153. package/src/peer-record-updater.ts +1 -1
  154. package/src/peer-routing.ts +4 -4
  155. package/src/ping/index.ts +35 -12
  156. package/src/pnet/index.ts +1 -2
  157. package/src/pubsub/dummy-pubsub.ts +2 -2
  158. package/src/registrar.ts +79 -55
  159. package/src/transport-manager.ts +3 -3
  160. package/src/upgrader.ts +188 -76
  161. package/src/version.ts +1 -2
package/src/upgrader.ts CHANGED
@@ -8,14 +8,18 @@ import { codes } from './errors.js'
8
8
  import { createConnection } from '@libp2p/connection'
9
9
  import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
10
10
  import { peerIdFromString } from '@libp2p/peer-id'
11
- import type { Connection, ProtocolStream, Stream } from '@libp2p/interfaces/connection'
12
- import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interfaces/connection-encrypter'
13
- import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer'
14
- import type { PeerId } from '@libp2p/interfaces/peer-id'
15
- import type { MultiaddrConnection, Upgrader, UpgraderEvents } from '@libp2p/interfaces/transport'
11
+ import type { MultiaddrConnection, Connection, Stream } from '@libp2p/interface-connection'
12
+ import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
13
+ import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
14
+ import type { PeerId } from '@libp2p/interface-peer-id'
15
+ import type { Upgrader, UpgraderEvents } from '@libp2p/interface-transport'
16
16
  import type { Duplex } from 'it-stream-types'
17
- import type { Components } from '@libp2p/interfaces/components'
17
+ import { Components, isInitializable } from '@libp2p/components'
18
18
  import type { AbortOptions } from '@libp2p/interfaces'
19
+ import type { Registrar } from '@libp2p/interface-registrar'
20
+ import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
21
+ import { TimeoutController } from 'timeout-abort-controller'
22
+ import { abortableDuplex } from 'abortable-iterator'
19
23
 
20
24
  const log = logger('libp2p:upgrader')
21
25
 
@@ -41,12 +45,59 @@ export interface CryptoResult extends SecuredConnection {
41
45
  export interface UpgraderInit {
42
46
  connectionEncryption: ConnectionEncrypter[]
43
47
  muxers: StreamMuxerFactory[]
48
+
49
+ /**
50
+ * An amount of ms by which an inbound connection upgrade
51
+ * must complete
52
+ */
53
+ inboundUpgradeTimeout: number
54
+ }
55
+
56
+ function findIncomingStreamLimit (protocol: string, registrar: Registrar) {
57
+ try {
58
+ const { options } = registrar.getHandler(protocol)
59
+
60
+ return options.maxInboundStreams
61
+ } catch (err: any) {
62
+ if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
63
+ throw err
64
+ }
65
+ }
66
+
67
+ return DEFAULT_MAX_INBOUND_STREAMS
68
+ }
69
+
70
+ function findOutgoingStreamLimit (protocol: string, registrar: Registrar) {
71
+ try {
72
+ const { options } = registrar.getHandler(protocol)
73
+
74
+ return options.maxOutboundStreams
75
+ } catch (err: any) {
76
+ if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
77
+ throw err
78
+ }
79
+ }
80
+
81
+ return DEFAULT_MAX_OUTBOUND_STREAMS
82
+ }
83
+
84
+ function countStreams (protocol: string, direction: 'inbound' | 'outbound', connection: Connection) {
85
+ let streamCount = 0
86
+
87
+ connection.streams.forEach(stream => {
88
+ if (stream.stat.direction === direction && stream.stat.protocol === protocol) {
89
+ streamCount++
90
+ }
91
+ })
92
+
93
+ return streamCount
44
94
  }
45
95
 
46
96
  export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upgrader {
47
97
  private readonly components: Components
48
98
  private readonly connectionEncryption: Map<string, ConnectionEncrypter>
49
99
  private readonly muxers: Map<string, StreamMuxerFactory>
100
+ private readonly inboundUpgradeTimeout: number
50
101
 
51
102
  constructor (components: Components, init: UpgraderInit) {
52
103
  super()
@@ -63,6 +114,8 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
63
114
  init.muxers.forEach(muxer => {
64
115
  this.muxers.set(muxer.protocol, muxer)
65
116
  })
117
+
118
+ this.inboundUpgradeTimeout = init.inboundUpgradeTimeout
66
119
  }
67
120
 
68
121
  /**
@@ -78,82 +131,92 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
78
131
  let proxyPeer
79
132
  const metrics = this.components.getMetrics()
80
133
 
81
- if (await this.components.getConnectionGater().denyInboundConnection(maConn)) {
82
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
83
- }
134
+ const timeoutController = new TimeoutController(this.inboundUpgradeTimeout)
84
135
 
85
- if (metrics != null) {
86
- ({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
87
- const idString = `${(Math.random() * 1e9).toString(36)}${Date.now()}`
88
- setPeer({ toString: () => idString })
89
- maConn = metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
90
- }
136
+ try {
137
+ const abortableStream = abortableDuplex(maConn, timeoutController.signal)
138
+ maConn.source = abortableStream.source
139
+ maConn.sink = abortableStream.sink
91
140
 
92
- log('starting the inbound connection upgrade')
141
+ if (await this.components.getConnectionGater().denyInboundConnection(maConn)) {
142
+ throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
143
+ }
93
144
 
94
- // Protect
95
- let protectedConn = maConn
96
- const protector = this.components.getConnectionProtector()
145
+ if (metrics != null) {
146
+ ({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
147
+ const idString = `${(Math.random() * 1e9).toString(36)}${Date.now()}`
148
+ setPeer({ toString: () => idString })
149
+ maConn = metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
150
+ }
97
151
 
98
- if (protector != null) {
99
- log('protecting the inbound connection')
100
- protectedConn = await protector.protect(maConn)
101
- }
152
+ log('starting the inbound connection upgrade')
102
153
 
103
- try {
104
- // Encrypt the connection
105
- ({
106
- conn: encryptedConn,
107
- remotePeer,
108
- protocol: cryptoProtocol
109
- } = await this._encryptInbound(protectedConn))
154
+ // Protect
155
+ let protectedConn = maConn
156
+ const protector = this.components.getConnectionProtector()
157
+
158
+ if (protector != null) {
159
+ log('protecting the inbound connection')
160
+ protectedConn = await protector.protect(maConn)
161
+ }
162
+
163
+ try {
164
+ // Encrypt the connection
165
+ ({
166
+ conn: encryptedConn,
167
+ remotePeer,
168
+ protocol: cryptoProtocol
169
+ } = await this._encryptInbound(protectedConn))
170
+
171
+ if (await this.components.getConnectionGater().denyInboundEncryptedConnection(remotePeer, {
172
+ ...protectedConn,
173
+ ...encryptedConn
174
+ })) {
175
+ throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
176
+ }
177
+
178
+ // Multiplex the connection
179
+ if (this.muxers.size > 0) {
180
+ const multiplexed = await this._multiplexInbound({
181
+ ...protectedConn,
182
+ ...encryptedConn
183
+ }, this.muxers)
184
+ muxerFactory = multiplexed.muxerFactory
185
+ upgradedConn = multiplexed.stream
186
+ } else {
187
+ upgradedConn = encryptedConn
188
+ }
189
+ } catch (err: any) {
190
+ log.error('Failed to upgrade inbound connection', err)
191
+ await maConn.close(err)
192
+ throw err
193
+ }
110
194
 
111
- if (await this.components.getConnectionGater().denyInboundEncryptedConnection(remotePeer, {
195
+ if (await this.components.getConnectionGater().denyInboundUpgradedConnection(remotePeer, {
112
196
  ...protectedConn,
113
197
  ...encryptedConn
114
198
  })) {
115
199
  throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
116
200
  }
117
201
 
118
- // Multiplex the connection
119
- if (this.muxers.size > 0) {
120
- const multiplexed = await this._multiplexInbound({
121
- ...protectedConn,
122
- ...encryptedConn
123
- }, this.muxers)
124
- muxerFactory = multiplexed.muxerFactory
125
- upgradedConn = multiplexed.stream
126
- } else {
127
- upgradedConn = encryptedConn
202
+ if (metrics != null) {
203
+ metrics.updatePlaceholder(proxyPeer, remotePeer)
204
+ setPeer(remotePeer)
128
205
  }
129
- } catch (err: any) {
130
- log.error('Failed to upgrade inbound connection', err)
131
- await maConn.close(err)
132
- throw err
133
- }
134
206
 
135
- if (await this.components.getConnectionGater().denyInboundUpgradedConnection(remotePeer, {
136
- ...protectedConn,
137
- ...encryptedConn
138
- })) {
139
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
140
- }
207
+ log('Successfully upgraded inbound connection')
141
208
 
142
- if (metrics != null) {
143
- metrics.updatePlaceholder(proxyPeer, remotePeer)
144
- setPeer(remotePeer)
209
+ return this._createConnection({
210
+ cryptoProtocol,
211
+ direction: 'inbound',
212
+ maConn,
213
+ upgradedConn,
214
+ muxerFactory,
215
+ remotePeer
216
+ })
217
+ } finally {
218
+ timeoutController.clear()
145
219
  }
146
-
147
- log('Successfully upgraded inbound connection')
148
-
149
- return this._createConnection({
150
- cryptoProtocol,
151
- direction: 'inbound',
152
- maConn,
153
- upgradedConn,
154
- muxerFactory,
155
- remotePeer
156
- })
157
220
  }
158
221
 
159
222
  /**
@@ -267,12 +330,13 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
267
330
  } = opts
268
331
 
269
332
  let muxer: StreamMuxer | undefined
270
- let newStream: ((multicodecs: string[], options?: AbortOptions) => Promise<ProtocolStream>) | undefined
333
+ let newStream: ((multicodecs: string[], options?: AbortOptions) => Promise<Stream>) | undefined
271
334
  let connection: Connection // eslint-disable-line prefer-const
272
335
 
273
336
  if (muxerFactory != null) {
274
337
  // Create the muxer
275
- muxer = muxerFactory.createStreamMuxer(this.components, {
338
+ muxer = muxerFactory.createStreamMuxer({
339
+ direction,
276
340
  // Run anytime a remote stream is created
277
341
  onIncomingStream: muxedStream => {
278
342
  if (connection == null) {
@@ -296,11 +360,24 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
296
360
  return
297
361
  }
298
362
 
299
- connection.addStream(muxedStream, { protocol })
363
+ const incomingLimit = findIncomingStreamLimit(protocol, this.components.getRegistrar())
364
+ const streamCount = countStreams(protocol, 'inbound', connection)
365
+
366
+ if (streamCount === incomingLimit) {
367
+ throw errCode(new Error('Too many incoming protocol streams'), codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS)
368
+ }
369
+
370
+ muxedStream.stat.protocol = protocol
371
+
372
+ connection.addStream(muxedStream)
300
373
  this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol })
301
374
  })
302
375
  .catch(err => {
303
376
  log.error(err)
377
+
378
+ if (muxedStream.stat.timeline.close == null) {
379
+ muxedStream.close()
380
+ }
304
381
  })
305
382
  },
306
383
  // Run anytime a stream closes
@@ -309,7 +386,11 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
309
386
  }
310
387
  })
311
388
 
312
- newStream = async (protocols: string[], options: AbortOptions = {}): Promise<ProtocolStream> => {
389
+ if (isInitializable(muxer)) {
390
+ muxer.init(this.components)
391
+ }
392
+
393
+ newStream = async (protocols: string[], options: AbortOptions = {}): Promise<Stream> => {
313
394
  if (muxer == null) {
314
395
  throw errCode(new Error('Stream is not multiplexed'), codes.ERR_MUXER_UNAVAILABLE)
315
396
  }
@@ -318,23 +399,55 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
318
399
  const muxedStream = muxer.newStream()
319
400
  const mss = new Dialer(muxedStream)
320
401
  const metrics = this.components.getMetrics()
402
+ let controller: TimeoutController | undefined
321
403
 
322
404
  try {
405
+ if (options.signal == null) {
406
+ log('No abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
407
+
408
+ controller = new TimeoutController(30000)
409
+ options.signal = controller.signal
410
+ }
411
+
323
412
  let { stream, protocol } = await mss.select(protocols, options)
324
413
 
325
414
  if (metrics != null) {
326
415
  stream = metrics.trackStream({ stream, remotePeer, protocol })
327
416
  }
328
417
 
329
- return { stream: { ...muxedStream, ...stream }, protocol }
418
+ const outgoingLimit = findOutgoingStreamLimit(protocol, this.components.getRegistrar())
419
+ const streamCount = countStreams(protocol, 'outbound', connection)
420
+
421
+ if (streamCount === outgoingLimit) {
422
+ throw errCode(new Error('Too many outgoing protocol streams'), codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS)
423
+ }
424
+
425
+ muxedStream.stat.protocol = protocol
426
+
427
+ return {
428
+ ...muxedStream,
429
+ ...stream,
430
+ stat: {
431
+ ...muxedStream.stat,
432
+ protocol
433
+ }
434
+ }
330
435
  } catch (err: any) {
331
436
  log.error('could not create new stream', err)
332
437
 
438
+ if (muxedStream.stat.timeline.close == null) {
439
+ muxedStream.close()
440
+ }
441
+
333
442
  if (err.code != null) {
334
443
  throw err
335
444
  }
336
445
 
337
446
  throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
447
+ } finally {
448
+ if (controller != null) {
449
+ controller.clear()
450
+ }
338
451
  }
339
452
  }
340
453
 
@@ -390,9 +503,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
390
503
  await maConn.close()
391
504
  // Ensure remaining streams are closed
392
505
  if (muxer != null) {
393
- await Promise.all(muxer.streams.map(async stream => {
394
- await stream.close()
395
- }))
506
+ muxer.close()
396
507
  }
397
508
  }
398
509
  })
@@ -409,8 +520,9 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
409
520
  */
410
521
  _onStream (opts: OnStreamOptions): void {
411
522
  const { connection, stream, protocol } = opts
412
- const handler = this.components.getRegistrar().getHandler(protocol)
413
- handler({ connection, stream, protocol })
523
+ const { handler } = this.components.getRegistrar().getHandler(protocol)
524
+
525
+ handler({ connection, stream })
414
526
  }
415
527
 
416
528
  /**
package/src/version.ts CHANGED
@@ -1,3 +1,2 @@
1
-
2
- export const version = '0.0.0'
1
+ export const version = '0.37.3-0bb1b80'
3
2
  export const name = 'libp2p'