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