libp2p 2.10.0-8484de8a2 → 2.10.0-87bc8d4fb

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 (111) hide show
  1. package/dist/index.min.js +17 -13
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/address-manager/dns-mappings.d.ts +2 -1
  4. package/dist/src/address-manager/dns-mappings.d.ts.map +1 -1
  5. package/dist/src/address-manager/dns-mappings.js +43 -39
  6. package/dist/src/address-manager/dns-mappings.js.map +1 -1
  7. package/dist/src/address-manager/index.d.ts.map +1 -1
  8. package/dist/src/address-manager/index.js +14 -31
  9. package/dist/src/address-manager/index.js.map +1 -1
  10. package/dist/src/address-manager/ip-mappings.d.ts +0 -1
  11. package/dist/src/address-manager/ip-mappings.d.ts.map +1 -1
  12. package/dist/src/address-manager/ip-mappings.js +40 -51
  13. package/dist/src/address-manager/ip-mappings.js.map +1 -1
  14. package/dist/src/address-manager/observed-addresses.d.ts.map +1 -1
  15. package/dist/src/address-manager/observed-addresses.js +3 -1
  16. package/dist/src/address-manager/observed-addresses.js.map +1 -1
  17. package/dist/src/address-manager/transport-addresses.d.ts.map +1 -1
  18. package/dist/src/address-manager/transport-addresses.js +8 -6
  19. package/dist/src/address-manager/transport-addresses.js.map +1 -1
  20. package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
  21. package/dist/src/config/connection-gater.browser.js +9 -3
  22. package/dist/src/config/connection-gater.browser.js.map +1 -1
  23. package/dist/src/config.js +1 -1
  24. package/dist/src/config.js.map +1 -1
  25. package/dist/src/connection-manager/address-sorter.d.ts.map +1 -1
  26. package/dist/src/connection-manager/address-sorter.js +2 -1
  27. package/dist/src/connection-manager/address-sorter.js.map +1 -1
  28. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  29. package/dist/src/connection-manager/connection-pruner.js +3 -7
  30. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  31. package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
  32. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  33. package/dist/src/connection-manager/constants.defaults.js +0 -4
  34. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  35. package/dist/src/connection-manager/dial-queue.d.ts +2 -2
  36. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  37. package/dist/src/connection-manager/dial-queue.js +23 -11
  38. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  39. package/dist/src/connection-manager/index.d.ts +2 -10
  40. package/dist/src/connection-manager/index.d.ts.map +1 -1
  41. package/dist/src/connection-manager/index.js +26 -44
  42. package/dist/src/connection-manager/index.js.map +1 -1
  43. package/dist/src/connection-manager/reconnect-queue.js +1 -1
  44. package/dist/src/connection-manager/reconnect-queue.js.map +1 -1
  45. package/dist/src/connection-manager/utils.d.ts +3 -31
  46. package/dist/src/connection-manager/utils.d.ts.map +1 -1
  47. package/dist/src/connection-manager/utils.js +18 -99
  48. package/dist/src/connection-manager/utils.js.map +1 -1
  49. package/dist/src/connection-monitor.d.ts +1 -1
  50. package/dist/src/connection-monitor.d.ts.map +1 -1
  51. package/dist/src/connection-monitor.js +3 -2
  52. package/dist/src/connection-monitor.js.map +1 -1
  53. package/dist/src/connection.d.ts +9 -13
  54. package/dist/src/connection.d.ts.map +1 -1
  55. package/dist/src/connection.js +138 -87
  56. package/dist/src/connection.js.map +1 -1
  57. package/dist/src/get-peer.js +3 -3
  58. package/dist/src/get-peer.js.map +1 -1
  59. package/dist/src/index.d.ts +2 -2
  60. package/dist/src/index.js +2 -2
  61. package/dist/src/libp2p.d.ts.map +1 -1
  62. package/dist/src/libp2p.js +5 -5
  63. package/dist/src/libp2p.js.map +1 -1
  64. package/dist/src/peer-routing.js +1 -1
  65. package/dist/src/peer-routing.js.map +1 -1
  66. package/dist/src/random-walk.d.ts.map +1 -1
  67. package/dist/src/random-walk.js +3 -13
  68. package/dist/src/random-walk.js.map +1 -1
  69. package/dist/src/registrar.d.ts +3 -3
  70. package/dist/src/registrar.d.ts.map +1 -1
  71. package/dist/src/registrar.js +41 -50
  72. package/dist/src/registrar.js.map +1 -1
  73. package/dist/src/transport-manager.js +2 -15
  74. package/dist/src/transport-manager.js.map +1 -1
  75. package/dist/src/upgrader.d.ts +16 -26
  76. package/dist/src/upgrader.d.ts.map +1 -1
  77. package/dist/src/upgrader.js +122 -88
  78. package/dist/src/upgrader.js.map +1 -1
  79. package/dist/src/version.d.ts +1 -1
  80. package/dist/src/version.js +1 -1
  81. package/package.json +30 -27
  82. package/src/address-manager/dns-mappings.ts +50 -50
  83. package/src/address-manager/index.ts +17 -37
  84. package/src/address-manager/ip-mappings.ts +44 -64
  85. package/src/address-manager/observed-addresses.ts +3 -1
  86. package/src/address-manager/transport-addresses.ts +9 -7
  87. package/src/config/connection-gater.browser.ts +12 -3
  88. package/src/config.ts +1 -1
  89. package/src/connection-manager/address-sorter.ts +2 -1
  90. package/src/connection-manager/connection-pruner.ts +3 -8
  91. package/src/connection-manager/constants.defaults.ts +0 -5
  92. package/src/connection-manager/dial-queue.ts +27 -12
  93. package/src/connection-manager/index.ts +28 -61
  94. package/src/connection-manager/reconnect-queue.ts +1 -1
  95. package/src/connection-manager/utils.ts +21 -129
  96. package/src/connection-monitor.ts +4 -3
  97. package/src/connection.ts +185 -111
  98. package/src/get-peer.ts +3 -3
  99. package/src/index.ts +2 -2
  100. package/src/libp2p.ts +6 -5
  101. package/src/peer-routing.ts +1 -1
  102. package/src/random-walk.ts +3 -13
  103. package/src/registrar.ts +54 -67
  104. package/src/transport-manager.ts +2 -18
  105. package/src/upgrader.ts +149 -132
  106. package/src/version.ts +1 -1
  107. package/dist/src/utils.d.ts +0 -3
  108. package/dist/src/utils.d.ts.map +0 -1
  109. package/dist/src/utils.js +0 -25
  110. package/dist/src/utils.js.map +0 -1
  111. package/src/utils.ts +0 -31
package/src/upgrader.ts CHANGED
@@ -1,16 +1,15 @@
1
1
  import { InvalidMultiaddrError, InvalidPeerIdError } from '@libp2p/interface'
2
2
  import * as mss from '@libp2p/multistream-select'
3
3
  import { peerIdFromString } from '@libp2p/peer-id'
4
- import { trackedMap } from '@libp2p/utils'
5
- import { CODE_P2P } from '@multiformats/multiaddr'
4
+ import { trackedMap } from '@libp2p/utils/tracked-map'
6
5
  import { anySignal } from 'any-signal'
7
6
  import { setMaxListeners } from 'main-event'
8
7
  import { CustomProgressEvent } from 'progress-events'
9
8
  import { raceSignal } from 'race-signal'
10
- import { PROTOCOL_NEGOTIATION_TIMEOUT, INBOUND_UPGRADE_TIMEOUT, CONNECTION_CLOSE_TIMEOUT } from './connection-manager/constants.js'
9
+ import { PROTOCOL_NEGOTIATION_TIMEOUT, INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
11
10
  import { createConnection } from './connection.js'
12
11
  import { ConnectionDeniedError, ConnectionInterceptedError, EncryptionFailedError, MuxerUnavailableError } from './errors.js'
13
- import type { Libp2pEvents, AbortOptions, ComponentLogger, MultiaddrConnection, Connection, ConnectionProtector, ConnectionEncrypter, ConnectionGater, Metrics, PeerId, PeerStore, StreamMuxerFactory, Upgrader as UpgraderInterface, UpgraderOptions, ConnectionLimits, CounterGroup, ClearableSignal, MessageStream, SecuredConnection, StreamMuxer, UpgraderWithoutEncryptionOptions, SecureConnectionOptions } from '@libp2p/interface'
12
+ import type { Libp2pEvents, AbortOptions, ComponentLogger, MultiaddrConnection, Connection, ConnectionProtector, ConnectionEncrypter, SecuredConnection, ConnectionGater, Metrics, PeerId, PeerStore, StreamMuxerFactory, Upgrader as UpgraderInterface, UpgraderOptions, ConnectionLimits, SecureConnectionOptions, CounterGroup, ClearableSignal } from '@libp2p/interface'
14
13
  import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
15
14
  import type { TypedEventTarget } from 'main-event'
16
15
 
@@ -18,21 +17,15 @@ interface CreateConnectionOptions {
18
17
  id: string
19
18
  cryptoProtocol: string
20
19
  direction: 'inbound' | 'outbound'
21
-
22
- /**
23
- * The raw underlying connection
24
- */
25
20
  maConn: MultiaddrConnection
26
-
27
- /**
28
- * The encrypted, multiplexed connection
29
- */
30
- stream: MessageStream
31
-
21
+ upgradedConn: MultiaddrConnection
32
22
  remotePeer: PeerId
33
- muxer?: StreamMuxer
23
+ muxerFactory?: StreamMuxerFactory
34
24
  limits?: ConnectionLimits
35
- closeTimeout?: number
25
+ }
26
+
27
+ export interface CryptoResult extends SecuredConnection<MultiaddrConnection> {
28
+ protocol: string
36
29
  }
37
30
 
38
31
  export interface UpgraderInit {
@@ -61,13 +54,6 @@ export interface UpgraderInit {
61
54
  * @default 2000
62
55
  */
63
56
  outboundStreamProtocolNegotiationTimeout?: number
64
-
65
- /**
66
- * How long to wait before closing a connection
67
- *
68
- * @default 1_000
69
- */
70
- connectionCloseTimeout?: number
71
57
  }
72
58
 
73
59
  export interface UpgraderComponents {
@@ -82,10 +68,6 @@ export interface UpgraderComponents {
82
68
  logger: ComponentLogger
83
69
  }
84
70
 
85
- interface EncryptedConnection extends SecuredConnection {
86
- protocol: string
87
- }
88
-
89
71
  type ConnectionDeniedType = keyof Pick<ConnectionGater, 'denyOutboundConnection' | 'denyInboundEncryptedConnection' | 'denyOutboundEncryptedConnection' | 'denyInboundUpgradedConnection' | 'denyOutboundUpgradedConnection'>
90
72
 
91
73
  export class Upgrader implements UpgraderInterface {
@@ -103,8 +85,6 @@ export class Upgrader implements UpgraderInterface {
103
85
  outboundErrors?: CounterGroup
104
86
  }
105
87
 
106
- private readonly connectionCloseTimeout?: number
107
-
108
88
  constructor (components: UpgraderComponents, init: UpgraderInit) {
109
89
  this.components = components
110
90
  this.connectionEncrypters = trackedMap({
@@ -128,7 +108,6 @@ export class Upgrader implements UpgraderInterface {
128
108
  this.inboundUpgradeTimeout = init.inboundUpgradeTimeout ?? INBOUND_UPGRADE_TIMEOUT
129
109
  this.inboundStreamProtocolNegotiationTimeout = init.inboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
130
110
  this.outboundStreamProtocolNegotiationTimeout = init.outboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
131
- this.connectionCloseTimeout = init.connectionCloseTimeout ?? CONNECTION_CLOSE_TIMEOUT
132
111
  this.events = components.events
133
112
  this.metrics = {
134
113
  dials: components.metrics?.registerCounterGroup('libp2p_connection_manager_dials_total'),
@@ -169,9 +148,7 @@ export class Upgrader implements UpgraderInterface {
169
148
  /**
170
149
  * Upgrades an inbound connection
171
150
  */
172
- async upgradeInbound (maConn: MultiaddrConnection, opts: UpgraderOptions): Promise<void>
173
- async upgradeInbound (maConn: MultiaddrConnection, opts: UpgraderWithoutEncryptionOptions): Promise<void>
174
- async upgradeInbound (maConn: MultiaddrConnection, opts: UpgraderOptions | UpgraderWithoutEncryptionOptions): Promise<void> {
151
+ async upgradeInbound (maConn: MultiaddrConnection, opts: UpgraderOptions): Promise<void> {
175
152
  let accepted = false
176
153
 
177
154
  // always apply upgrade timeout for incoming upgrades
@@ -182,7 +159,7 @@ export class Upgrader implements UpgraderInterface {
182
159
  inbound: true
183
160
  })
184
161
 
185
- accepted = this.components.connectionManager.acceptIncomingConnection(maConn)
162
+ accepted = await raceSignal(this.components.connectionManager.acceptIncomingConnection(maConn), signal)
186
163
 
187
164
  if (!accepted) {
188
165
  throw new ConnectionDeniedError('Connection denied')
@@ -215,15 +192,13 @@ export class Upgrader implements UpgraderInterface {
215
192
  /**
216
193
  * Upgrades an outbound connection
217
194
  */
218
- async upgradeOutbound (maConn: MultiaddrConnection, opts: UpgraderOptions): Promise<Connection>
219
- async upgradeOutbound (maConn: MultiaddrConnection, opts: UpgraderWithoutEncryptionOptions): Promise<Connection>
220
- async upgradeOutbound (maConn: MultiaddrConnection, opts: UpgraderOptions | UpgraderWithoutEncryptionOptions): Promise<Connection> {
195
+ async upgradeOutbound (maConn: MultiaddrConnection, opts: UpgraderOptions): Promise<Connection> {
221
196
  try {
222
197
  this.metrics.dials?.increment({
223
198
  outbound: true
224
199
  })
225
200
 
226
- const idStr = maConn.remoteAddr.getComponents().findLast(c => c.code === CODE_P2P)?.value
201
+ const idStr = maConn.remoteAddr.getPeerId()
227
202
  let remotePeerId: PeerId | undefined
228
203
 
229
204
  if (idStr != null) {
@@ -251,11 +226,11 @@ export class Upgrader implements UpgraderInterface {
251
226
  }
252
227
  }
253
228
 
254
- private async _performUpgrade (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound', opts: UpgraderOptions | UpgraderWithoutEncryptionOptions): Promise<Connection> {
255
- let stream: MessageStream = maConn
229
+ private async _performUpgrade (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound', opts: UpgraderOptions): Promise<Connection> {
230
+ let encryptedConn: MultiaddrConnection
256
231
  let remotePeer: PeerId
232
+ let upgradedConn: MultiaddrConnection
257
233
  let muxerFactory: StreamMuxerFactory | undefined
258
- let muxer: StreamMuxer | undefined
259
234
  let cryptoProtocol
260
235
 
261
236
  const id = `${(parseInt(String(Math.random() * 1e9))).toString(36)}${Date.now()}`
@@ -266,49 +241,50 @@ export class Upgrader implements UpgraderInterface {
266
241
  maConn.log.trace('starting the %s connection upgrade', direction)
267
242
 
268
243
  // Protect
244
+ let protectedConn = maConn
245
+
269
246
  if (opts?.skipProtection !== true) {
270
247
  const protector = this.components.connectionProtector
271
248
 
272
249
  if (protector != null) {
273
250
  maConn.log('protecting the %s connection', direction)
274
- stream = await protector.protect(stream, opts)
251
+ protectedConn = await protector.protect(maConn, opts)
275
252
  }
276
253
  }
277
254
 
278
255
  try {
279
256
  // Encrypt the connection
280
- if (isEncryptionSkipped(opts)) {
281
- if (opts.remotePeer == null) {
282
- throw new InvalidMultiaddrError(`${direction} connection that skipped encryption must have a peer id`)
283
- }
284
-
285
- cryptoProtocol = 'native'
286
- remotePeer = opts.remotePeer
287
- } else {
288
- const peerIdString = maConn.remoteAddr.getComponents().findLast(c => c.code === CODE_P2P)?.value
289
- let remotePeerFromMultiaddr: PeerId | undefined
290
-
291
- if (peerIdString != null) {
292
- remotePeerFromMultiaddr = peerIdFromString(peerIdString)
293
- }
294
-
257
+ encryptedConn = protectedConn
258
+ if (opts?.skipEncryption !== true) {
295
259
  opts?.onProgress?.(new CustomProgressEvent(`upgrader:encrypt-${direction}-connection`));
296
260
 
297
261
  ({
298
- connection: stream,
262
+ conn: encryptedConn,
299
263
  remotePeer,
300
264
  protocol: cryptoProtocol,
301
265
  streamMuxer: muxerFactory
302
266
  } = await (direction === 'inbound'
303
- ? this._encryptInbound(stream, {
304
- ...opts,
305
- remotePeer: remotePeerFromMultiaddr
306
- })
307
- : this._encryptOutbound(stream, {
308
- ...opts,
309
- remotePeer: remotePeerFromMultiaddr
310
- })
267
+ ? this._encryptInbound(protectedConn, opts)
268
+ : this._encryptOutbound(protectedConn, opts)
311
269
  ))
270
+
271
+ const maConn: MultiaddrConnection = {
272
+ ...protectedConn,
273
+ ...encryptedConn
274
+ }
275
+
276
+ await this.shouldBlockConnection(direction === 'inbound' ? 'denyInboundEncryptedConnection' : 'denyOutboundEncryptedConnection', remotePeer, maConn)
277
+ } else {
278
+ const idStr = maConn.remoteAddr.getPeerId()
279
+
280
+ if (idStr == null) {
281
+ throw new InvalidMultiaddrError(`${direction} connection that skipped encryption must have a peer id`)
282
+ }
283
+
284
+ const remotePeerId = peerIdFromString(idStr)
285
+
286
+ cryptoProtocol = 'native'
287
+ remotePeer = remotePeerId
312
288
  }
313
289
 
314
290
  // this can happen if we dial a multiaddr without a peer id, we only find
@@ -319,32 +295,30 @@ export class Upgrader implements UpgraderInterface {
319
295
  throw err
320
296
  }
321
297
 
322
- // stream.pause()
323
- await this.shouldBlockConnection(direction === 'inbound' ? 'denyInboundEncryptedConnection' : 'denyOutboundEncryptedConnection', remotePeer, maConn)
324
- // stream.resume()
325
-
298
+ upgradedConn = encryptedConn
326
299
  if (opts?.muxerFactory != null) {
327
300
  muxerFactory = opts.muxerFactory
328
301
  } else if (muxerFactory == null && this.streamMuxers.size > 0) {
329
302
  opts?.onProgress?.(new CustomProgressEvent(`upgrader:multiplex-${direction}-connection`))
330
303
 
331
304
  // Multiplex the connection
332
- muxerFactory = await (direction === 'inbound'
333
- ? this._multiplexInbound(stream, this.streamMuxers, opts)
334
- : this._multiplexOutbound(stream, this.streamMuxers, opts))
305
+ const multiplexed = await (direction === 'inbound'
306
+ ? this._multiplexInbound({
307
+ ...protectedConn,
308
+ ...encryptedConn
309
+ }, this.streamMuxers, opts)
310
+ : this._multiplexOutbound({
311
+ ...protectedConn,
312
+ ...encryptedConn
313
+ }, this.streamMuxers, opts))
314
+ muxerFactory = multiplexed.muxerFactory
315
+ upgradedConn = multiplexed.stream
335
316
  }
336
317
  } catch (err: any) {
337
- maConn.log.error('failed to upgrade %s connection %s %a - %e', direction, direction === 'inbound' ? 'from' : 'to', maConn.remoteAddr, err)
318
+ maConn.log.error('failed to upgrade inbound connection %s %a - %e', direction === 'inbound' ? 'from' : 'to', maConn.remoteAddr, err)
338
319
  throw err
339
320
  }
340
321
 
341
- // create the connection muxer if one is configured
342
- if (muxerFactory != null) {
343
- maConn.log('create muxer %s', muxerFactory.protocol)
344
- muxer = muxerFactory.createStreamMuxer(stream)
345
- }
346
-
347
- // stream.pause()
348
322
  await this.shouldBlockConnection(direction === 'inbound' ? 'denyInboundUpgradedConnection' : 'denyOutboundUpgradedConnection', remotePeer, maConn)
349
323
 
350
324
  const conn = this._createConnection({
@@ -352,16 +326,13 @@ export class Upgrader implements UpgraderInterface {
352
326
  cryptoProtocol,
353
327
  direction,
354
328
  maConn,
355
- stream,
356
- muxer,
329
+ upgradedConn,
330
+ muxerFactory,
357
331
  remotePeer,
358
- limits: opts?.limits,
359
- closeTimeout: this.connectionCloseTimeout
332
+ limits: opts?.limits
360
333
  })
361
334
 
362
- conn.log('successfully upgraded connection')
363
-
364
- // stream.resume()
335
+ conn.log('successfully upgraded %s connection', direction)
365
336
 
366
337
  return conn
367
338
  }
@@ -370,19 +341,59 @@ export class Upgrader implements UpgraderInterface {
370
341
  * A convenience method for generating a new `Connection`
371
342
  */
372
343
  _createConnection (opts: CreateConnectionOptions): Connection {
344
+ const {
345
+ id,
346
+ cryptoProtocol,
347
+ direction,
348
+ maConn,
349
+ upgradedConn,
350
+ remotePeer,
351
+ muxerFactory,
352
+ limits
353
+ } = opts
354
+
355
+ let connection: Connection // eslint-disable-line prefer-const
356
+
357
+ const _timeline = maConn.timeline
358
+ maConn.timeline = new Proxy(_timeline, {
359
+ set: (...args) => {
360
+ if (args[1] === 'close' && args[2] != null && _timeline.close == null) {
361
+ // Wait for close to finish before notifying of the closure
362
+ (async () => {
363
+ try {
364
+ if (connection.status === 'open') {
365
+ await connection.close()
366
+ }
367
+ } catch (err: any) {
368
+ connection.log.error('error closing connection after timeline close %e', err)
369
+ } finally {
370
+ this.events.safeDispatchEvent('connection:close', {
371
+ detail: connection
372
+ })
373
+ }
374
+ })().catch(err => {
375
+ connection.log.error('error thrown while dispatching connection:close event %e', err)
376
+ })
377
+ }
378
+
379
+ return Reflect.set(...args)
380
+ }
381
+ })
382
+ maConn.timeline.upgraded = Date.now()
383
+
373
384
  // Create the connection
374
- const connection = createConnection(this.components, {
375
- ...opts,
385
+ connection = createConnection(this.components, {
386
+ id,
387
+ maConn: upgradedConn,
388
+ remotePeer,
389
+ direction,
390
+ muxerFactory,
391
+ encryption: cryptoProtocol,
392
+ limits,
376
393
  outboundStreamProtocolNegotiationTimeout: this.outboundStreamProtocolNegotiationTimeout,
377
394
  inboundStreamProtocolNegotiationTimeout: this.inboundStreamProtocolNegotiationTimeout
378
395
  })
379
396
 
380
- connection.addEventListener('close', () => {
381
- this.events.safeDispatchEvent('connection:close', {
382
- detail: connection
383
- })
384
- })
385
-
386
397
  this.events.safeDispatchEvent('connection:open', {
387
398
  detail: connection
388
399
  })
@@ -393,24 +404,28 @@ export class Upgrader implements UpgraderInterface {
393
404
  /**
394
405
  * Attempts to encrypt the incoming `connection` with the provided `cryptos`
395
406
  */
396
- async _encryptInbound (connection: MessageStream, options?: SecureConnectionOptions): Promise<EncryptedConnection> {
407
+ async _encryptInbound (connection: MultiaddrConnection, options?: AbortOptions): Promise<CryptoResult> {
397
408
  const protocols = Array.from(this.connectionEncrypters.keys())
398
409
 
399
410
  try {
400
- const protocol = await mss.handle(connection, protocols, options)
411
+ const { stream, protocol } = await mss.handle(connection, protocols, {
412
+ ...options,
413
+ log: connection.log
414
+ })
401
415
  const encrypter = this.connectionEncrypters.get(protocol)
402
416
 
403
417
  if (encrypter == null) {
404
418
  throw new EncryptionFailedError(`no crypto module found for ${protocol}`)
405
419
  }
406
420
 
407
- connection.log('encrypting inbound connection using %s', protocol)
421
+ connection.log('encrypting inbound connection to %a using %s', connection.remoteAddr, protocol)
408
422
 
409
423
  return {
410
- ...await encrypter.secureInbound(connection, options),
424
+ ...await encrypter.secureInbound(stream, options),
411
425
  protocol
412
426
  }
413
427
  } catch (err: any) {
428
+ connection.log.error('encrypting inbound connection from %a failed', connection.remoteAddr, err)
414
429
  throw new EncryptionFailedError(err.message)
415
430
  }
416
431
  }
@@ -419,26 +434,31 @@ export class Upgrader implements UpgraderInterface {
419
434
  * Attempts to encrypt the given `connection` with the provided connection encrypters.
420
435
  * The first `ConnectionEncrypter` module to succeed will be used
421
436
  */
422
- async _encryptOutbound (connection: MessageStream, options?: SecureConnectionOptions): Promise<EncryptedConnection> {
437
+ async _encryptOutbound (connection: MultiaddrConnection, options: SecureConnectionOptions): Promise<CryptoResult> {
423
438
  const protocols = Array.from(this.connectionEncrypters.keys())
424
439
 
425
440
  try {
426
441
  connection.log.trace('selecting encrypter from %s', protocols)
427
442
 
428
- const protocol = await mss.select(connection, protocols, options)
443
+ const { stream, protocol } = await mss.select(connection, protocols, {
444
+ ...options,
445
+ log: connection.log,
446
+ yieldBytes: true
447
+ })
429
448
  const encrypter = this.connectionEncrypters.get(protocol)
430
449
 
431
450
  if (encrypter == null) {
432
451
  throw new EncryptionFailedError(`no crypto module found for ${protocol}`)
433
452
  }
434
453
 
435
- connection.log('encrypting outbound connection using %s', protocol)
454
+ connection.log('encrypting outbound connection to %a using %s', connection.remoteAddr, protocol)
436
455
 
437
456
  return {
438
- ...await encrypter.secureOutbound(connection, options),
457
+ ...await encrypter.secureOutbound(stream, options),
439
458
  protocol
440
459
  }
441
460
  } catch (err: any) {
461
+ connection.log.error('encrypting outbound connection to %a failed', connection.remoteAddr, err)
442
462
  throw new EncryptionFailedError(err.message)
443
463
  }
444
464
  }
@@ -447,23 +467,27 @@ export class Upgrader implements UpgraderInterface {
447
467
  * Selects one of the given muxers via multistream-select. That
448
468
  * muxer will be used for all future streams on the connection.
449
469
  */
450
- async _multiplexOutbound (maConn: MessageStream, muxers: Map<string, StreamMuxerFactory>, options: AbortOptions): Promise<StreamMuxerFactory> {
470
+ async _multiplexOutbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>, options: AbortOptions): Promise<{ stream: MultiaddrConnection, muxerFactory?: StreamMuxerFactory }> {
451
471
  const protocols = Array.from(muxers.keys())
452
- maConn.log('outbound selecting muxer %s', protocols)
453
-
472
+ connection.log('outbound selecting muxer %s', protocols)
454
473
  try {
455
- maConn.log.trace('selecting stream muxer from %s', protocols)
456
- const protocol = await mss.select(maConn, protocols, options)
457
- const muxerFactory = muxers.get(protocol)
474
+ connection.log.trace('selecting stream muxer from %s', protocols)
458
475
 
459
- if (muxerFactory == null) {
460
- throw new MuxerUnavailableError(`No muxer configured for protocol "${protocol}"`)
461
- }
476
+ const {
477
+ stream,
478
+ protocol
479
+ } = await mss.select(connection, protocols, {
480
+ ...options,
481
+ log: connection.log,
482
+ yieldBytes: true
483
+ })
462
484
 
463
- maConn.log('selected %s as muxer protocol', protocol)
464
- return muxerFactory
485
+ connection.log('selected %s as muxer protocol', protocol)
486
+ const muxerFactory = muxers.get(protocol)
487
+
488
+ return { stream, muxerFactory }
465
489
  } catch (err: any) {
466
- maConn.log.error('error multiplexing outbound connection', err)
490
+ connection.log.error('error multiplexing outbound connection', err)
467
491
  throw new MuxerUnavailableError(String(err))
468
492
  }
469
493
  }
@@ -472,23 +496,20 @@ export class Upgrader implements UpgraderInterface {
472
496
  * Registers support for one of the given muxers via multistream-select. The
473
497
  * selected muxer will be used for all future streams on the connection.
474
498
  */
475
- async _multiplexInbound (maConn: MessageStream, muxers: Map<string, StreamMuxerFactory>, options: AbortOptions): Promise<StreamMuxerFactory> {
499
+ async _multiplexInbound (connection: MultiaddrConnection, muxers: Map<string, StreamMuxerFactory>, options: AbortOptions): Promise<{ stream: MultiaddrConnection, muxerFactory?: StreamMuxerFactory }> {
476
500
  const protocols = Array.from(muxers.keys())
477
- maConn.log('inbound handling muxers %s', protocols)
501
+ connection.log('inbound handling muxers %s', protocols)
478
502
  try {
479
- maConn.log.trace('selecting stream muxer from %s', protocols)
480
- const protocol = await mss.handle(maConn, protocols, options)
503
+ const { stream, protocol } = await mss.handle(connection, protocols, {
504
+ ...options,
505
+ log: connection.log
506
+ })
481
507
  const muxerFactory = muxers.get(protocol)
482
508
 
483
- if (muxerFactory == null) {
484
- throw new MuxerUnavailableError(`No muxer configured for protocol "${protocol}"`)
485
- }
486
-
487
- maConn.log('selected %s as muxer protocol', protocol)
488
- return muxerFactory
509
+ return { stream, muxerFactory }
489
510
  } catch (err: any) {
490
- maConn.log.error('error multiplexing inbound connection', err)
491
- throw err
511
+ connection.log.error('error multiplexing inbound connection', err)
512
+ throw new MuxerUnavailableError(String(err))
492
513
  }
493
514
  }
494
515
 
@@ -500,7 +521,3 @@ export class Upgrader implements UpgraderInterface {
500
521
  return this.streamMuxers
501
522
  }
502
523
  }
503
-
504
- function isEncryptionSkipped (opts?: any): opts is UpgraderWithoutEncryptionOptions {
505
- return opts.skipEncryption === true
506
- }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '2.10.0-8484de8a2'
1
+ export const version = '2.10.0-87bc8d4fb'
2
2
  export const name = 'js-libp2p'
@@ -1,3 +0,0 @@
1
- import type { MessageStream } from '@libp2p/interface';
2
- export declare function redirect(channelA: MessageStream, channelB: MessageStream): void;
3
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAEtD,wBAAgB,QAAQ,CAAE,QAAQ,EAAE,aAAa,EAAE,QAAQ,EAAE,aAAa,GAAG,IAAI,CA4BhF"}
package/dist/src/utils.js DELETED
@@ -1,25 +0,0 @@
1
- export function redirect(channelA, channelB) {
2
- channelA.addEventListener('message', (evt) => {
3
- const sendMore = channelB.send(evt.data);
4
- if (sendMore === false) {
5
- channelA.pause();
6
- channelA.addEventListener('drain', () => {
7
- channelA.resume();
8
- }, {
9
- once: true
10
- });
11
- }
12
- });
13
- channelB.addEventListener('message', (evt) => {
14
- const sendMore = channelA.send(evt.data);
15
- if (sendMore === false) {
16
- channelB.pause();
17
- channelB.addEventListener('drain', () => {
18
- channelB.resume();
19
- }, {
20
- once: true
21
- });
22
- }
23
- });
24
- }
25
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,QAAQ,CAAE,QAAuB,EAAE,QAAuB;IACxE,QAAQ,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,EAAE;QAC3C,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QAExC,IAAI,QAAQ,KAAK,KAAK,EAAE,CAAC;YACvB,QAAQ,CAAC,KAAK,EAAE,CAAA;YAEhB,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE;gBACtC,QAAQ,CAAC,MAAM,EAAE,CAAA;YACnB,CAAC,EAAE;gBACD,IAAI,EAAE,IAAI;aACX,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAC,CAAA;IAEF,QAAQ,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,EAAE;QAC3C,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QAExC,IAAI,QAAQ,KAAK,KAAK,EAAE,CAAC;YACvB,QAAQ,CAAC,KAAK,EAAE,CAAA;YAEhB,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE;gBACtC,QAAQ,CAAC,MAAM,EAAE,CAAA;YACnB,CAAC,EAAE;gBACD,IAAI,EAAE,IAAI;aACX,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAC,CAAA;AACJ,CAAC"}
package/src/utils.ts DELETED
@@ -1,31 +0,0 @@
1
- import type { MessageStream } from '@libp2p/interface'
2
-
3
- export function redirect (channelA: MessageStream, channelB: MessageStream): void {
4
- channelA.addEventListener('message', (evt) => {
5
- const sendMore = channelB.send(evt.data)
6
-
7
- if (sendMore === false) {
8
- channelA.pause()
9
-
10
- channelA.addEventListener('drain', () => {
11
- channelA.resume()
12
- }, {
13
- once: true
14
- })
15
- }
16
- })
17
-
18
- channelB.addEventListener('message', (evt) => {
19
- const sendMore = channelA.send(evt.data)
20
-
21
- if (sendMore === false) {
22
- channelB.pause()
23
-
24
- channelB.addEventListener('drain', () => {
25
- channelB.resume()
26
- }, {
27
- once: true
28
- })
29
- }
30
- })
31
- }