libp2p 0.37.2 → 0.37.3-de30c2c
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/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 +2 -2
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +2 -2
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/circuit/stop.d.ts +1 -1
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js +1 -1
- 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 +2 -2
- package/dist/src/circuit/index.d.ts.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 +5 -5
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +2 -2
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +11 -3
- 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/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 +5 -5
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +1 -1
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +9 -9
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +42 -32
- package/dist/src/connection-manager/index.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 +7 -4
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +61 -39
- 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 +7 -7
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +15 -23
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +17 -17
- package/dist/src/index.d.ts.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 +14 -4
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/metrics/index.d.ts +2 -2
- package/dist/src/metrics/index.d.ts.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 +6 -3
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +6 -2
- 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 +6 -6
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +69 -8
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +51 -29
- package/src/address-manager/index.ts +2 -2
- package/src/circuit/auto-relay.ts +4 -4
- package/src/circuit/circuit/hop.ts +4 -4
- package/src/circuit/circuit/stop.ts +2 -2
- package/src/circuit/circuit/stream-handler.ts +1 -1
- package/src/circuit/index.ts +2 -2
- package/src/circuit/listener.ts +3 -3
- package/src/circuit/transport.ts +5 -5
- package/src/config.ts +11 -3
- package/src/connection-manager/auto-dialler.ts +1 -1
- package/src/connection-manager/dialer/auto-dialer.ts +2 -2
- package/src/connection-manager/dialer/dial-request.ts +1 -1
- package/src/connection-manager/dialer/index.ts +5 -5
- package/src/connection-manager/index.ts +55 -39
- 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 +91 -50
- package/src/get-peer.ts +3 -3
- package/src/identify/index.ts +24 -29
- package/src/index.ts +17 -17
- package/src/insecure/index.ts +3 -3
- package/src/keychain/index.ts +2 -2
- package/src/libp2p.ts +31 -15
- package/src/metrics/index.ts +2 -2
- 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 +12 -5
- 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 +94 -16
package/src/registrar.ts
CHANGED
|
@@ -1,30 +1,24 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import errCode from 'err-code'
|
|
3
3
|
import { codes } from './errors.js'
|
|
4
|
-
import { isTopology,
|
|
5
|
-
import
|
|
6
|
-
import type {
|
|
7
|
-
import type {
|
|
8
|
-
import type {
|
|
4
|
+
import { isTopology, StreamHandlerOptions, StreamHandlerRecord } from '@libp2p/interface-registrar'
|
|
5
|
+
import merge from 'merge-options'
|
|
6
|
+
import type { Registrar, StreamHandler, Topology } from '@libp2p/interface-registrar'
|
|
7
|
+
import type { PeerProtocolsChangeData } from '@libp2p/interface-peer-store'
|
|
8
|
+
import type { Connection } from '@libp2p/interface-connection'
|
|
9
|
+
import type { Components } from '@libp2p/components'
|
|
9
10
|
|
|
10
11
|
const log = logger('libp2p:registrar')
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (topologyProtocols.includes(peerProtocol)) {
|
|
15
|
-
return true
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return false
|
|
20
|
-
}
|
|
13
|
+
export const DEFAULT_MAX_INBOUND_STREAMS = 1
|
|
14
|
+
export const DEFAULT_MAX_OUTBOUND_STREAMS = 1
|
|
21
15
|
|
|
22
16
|
/**
|
|
23
17
|
* Responsible for notifying registered protocols of events in the network.
|
|
24
18
|
*/
|
|
25
19
|
export class DefaultRegistrar implements Registrar {
|
|
26
|
-
private readonly topologies: Map<string,
|
|
27
|
-
private readonly handlers: Map<string,
|
|
20
|
+
private readonly topologies: Map<string, Map<string, Topology>>
|
|
21
|
+
private readonly handlers: Map<string, StreamHandlerRecord>
|
|
28
22
|
private readonly components: Components
|
|
29
23
|
|
|
30
24
|
constructor (components: Components) {
|
|
@@ -42,57 +36,54 @@ export class DefaultRegistrar implements Registrar {
|
|
|
42
36
|
}
|
|
43
37
|
|
|
44
38
|
getProtocols () {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
for (const protocol of this.handlers.keys()) {
|
|
52
|
-
protocols.add(protocol)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return Array.from(protocols).sort()
|
|
39
|
+
return Array.from(new Set<string>([
|
|
40
|
+
...this.topologies.keys(),
|
|
41
|
+
...this.handlers.keys()
|
|
42
|
+
])).sort()
|
|
56
43
|
}
|
|
57
44
|
|
|
58
45
|
getHandler (protocol: string) {
|
|
59
46
|
const handler = this.handlers.get(protocol)
|
|
60
47
|
|
|
61
48
|
if (handler == null) {
|
|
62
|
-
throw new Error(`No handler registered for protocol ${protocol}`)
|
|
49
|
+
throw errCode(new Error(`No handler registered for protocol ${protocol}`), codes.ERR_NO_HANDLER_FOR_PROTOCOL)
|
|
63
50
|
}
|
|
64
51
|
|
|
65
52
|
return handler
|
|
66
53
|
}
|
|
67
54
|
|
|
68
55
|
getTopologies (protocol: string) {
|
|
69
|
-
const
|
|
56
|
+
const topologies = this.topologies.get(protocol)
|
|
70
57
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
output.push(topology)
|
|
74
|
-
}
|
|
58
|
+
if (topologies == null) {
|
|
59
|
+
return []
|
|
75
60
|
}
|
|
76
61
|
|
|
77
|
-
return
|
|
62
|
+
return [
|
|
63
|
+
...topologies.values()
|
|
64
|
+
]
|
|
78
65
|
}
|
|
79
66
|
|
|
80
67
|
/**
|
|
81
68
|
* Registers the `handler` for each protocol
|
|
82
69
|
*/
|
|
83
|
-
async handle (
|
|
84
|
-
|
|
70
|
+
async handle (protocol: string, handler: StreamHandler, opts?: StreamHandlerOptions): Promise<void> {
|
|
71
|
+
if (this.handlers.has(protocol)) {
|
|
72
|
+
throw errCode(new Error(`Handler already registered for protocol ${protocol}`), codes.ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED)
|
|
73
|
+
}
|
|
85
74
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
75
|
+
const options = merge.bind({ ignoreUndefined: true })({
|
|
76
|
+
maxInboundStreams: DEFAULT_MAX_INBOUND_STREAMS,
|
|
77
|
+
maxOutboundStreams: DEFAULT_MAX_OUTBOUND_STREAMS
|
|
78
|
+
}, opts)
|
|
90
79
|
|
|
91
|
-
|
|
92
|
-
|
|
80
|
+
this.handlers.set(protocol, {
|
|
81
|
+
handler,
|
|
82
|
+
options
|
|
83
|
+
})
|
|
93
84
|
|
|
94
85
|
// Add new protocols to self protocols in the Protobook
|
|
95
|
-
await this.components.getPeerStore().protoBook.add(this.components.getPeerId(),
|
|
86
|
+
await this.components.getPeerStore().protoBook.add(this.components.getPeerId(), [protocol])
|
|
96
87
|
}
|
|
97
88
|
|
|
98
89
|
/**
|
|
@@ -113,7 +104,7 @@ export class DefaultRegistrar implements Registrar {
|
|
|
113
104
|
/**
|
|
114
105
|
* Register handlers for a set of multicodecs given
|
|
115
106
|
*/
|
|
116
|
-
async register (
|
|
107
|
+
async register (protocol: string, topology: Topology): Promise<string> {
|
|
117
108
|
if (!isTopology(topology)) {
|
|
118
109
|
log.error('topology must be an instance of interfaces/topology')
|
|
119
110
|
throw errCode(new Error('topology must be an instance of interfaces/topology'), codes.ERR_INVALID_PARAMETERS)
|
|
@@ -122,10 +113,14 @@ export class DefaultRegistrar implements Registrar {
|
|
|
122
113
|
// Create topology
|
|
123
114
|
const id = `${(Math.random() * 1e9).toString(36)}${Date.now()}`
|
|
124
115
|
|
|
125
|
-
this.topologies.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
116
|
+
let topologies = this.topologies.get(protocol)
|
|
117
|
+
|
|
118
|
+
if (topologies == null) {
|
|
119
|
+
topologies = new Map<string, Topology>()
|
|
120
|
+
this.topologies.set(protocol, topologies)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
topologies.set(id, topology)
|
|
129
124
|
|
|
130
125
|
// Set registrar
|
|
131
126
|
await topology.setRegistrar(this)
|
|
@@ -137,7 +132,15 @@ export class DefaultRegistrar implements Registrar {
|
|
|
137
132
|
* Unregister topology
|
|
138
133
|
*/
|
|
139
134
|
unregister (id: string) {
|
|
140
|
-
this.topologies.
|
|
135
|
+
for (const [protocol, topologies] of this.topologies.entries()) {
|
|
136
|
+
if (topologies.has(id)) {
|
|
137
|
+
topologies.delete(id)
|
|
138
|
+
|
|
139
|
+
if (topologies.size === 0) {
|
|
140
|
+
this.topologies.delete(protocol)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
141
144
|
}
|
|
142
145
|
|
|
143
146
|
/**
|
|
@@ -148,8 +151,15 @@ export class DefaultRegistrar implements Registrar {
|
|
|
148
151
|
|
|
149
152
|
void this.components.getPeerStore().protoBook.get(connection.remotePeer)
|
|
150
153
|
.then(peerProtocols => {
|
|
151
|
-
for (const
|
|
152
|
-
|
|
154
|
+
for (const protocol of peerProtocols) {
|
|
155
|
+
const topologies = this.topologies.get(protocol)
|
|
156
|
+
|
|
157
|
+
if (topologies == null) {
|
|
158
|
+
// no topologies are interested in this protocol
|
|
159
|
+
continue
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const topology of topologies.values()) {
|
|
153
163
|
topology.onDisconnect(connection.remotePeer)
|
|
154
164
|
}
|
|
155
165
|
}
|
|
@@ -168,14 +178,28 @@ export class DefaultRegistrar implements Registrar {
|
|
|
168
178
|
const removed = oldProtocols.filter(protocol => !protocols.includes(protocol))
|
|
169
179
|
const added = protocols.filter(protocol => !oldProtocols.includes(protocol))
|
|
170
180
|
|
|
171
|
-
for (const
|
|
172
|
-
|
|
181
|
+
for (const protocol of removed) {
|
|
182
|
+
const topologies = this.topologies.get(protocol)
|
|
183
|
+
|
|
184
|
+
if (topologies == null) {
|
|
185
|
+
// no topologies are interested in this protocol
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
for (const topology of topologies.values()) {
|
|
173
190
|
topology.onDisconnect(peerId)
|
|
174
191
|
}
|
|
175
192
|
}
|
|
176
193
|
|
|
177
|
-
for (const
|
|
178
|
-
|
|
194
|
+
for (const protocol of added) {
|
|
195
|
+
const topologies = this.topologies.get(protocol)
|
|
196
|
+
|
|
197
|
+
if (topologies == null) {
|
|
198
|
+
// no topologies are interested in this protocol
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
for (const topology of topologies.values()) {
|
|
179
203
|
const connection = this.components.getConnectionManager().getConnections(peerId)[0]
|
|
180
204
|
|
|
181
205
|
if (connection == null) {
|
package/src/transport-manager.ts
CHANGED
|
@@ -2,13 +2,13 @@ import { logger } from '@libp2p/logger'
|
|
|
2
2
|
import pSettle from 'p-settle'
|
|
3
3
|
import { codes } from './errors.js'
|
|
4
4
|
import errCode from 'err-code'
|
|
5
|
-
import type { Listener, Transport, TransportManager, TransportManagerEvents } from '@libp2p/
|
|
5
|
+
import type { Listener, Transport, TransportManager, TransportManagerEvents } from '@libp2p/interface-transport'
|
|
6
6
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
|
-
import type { Connection } from '@libp2p/
|
|
7
|
+
import type { Connection } from '@libp2p/interface-connection'
|
|
8
8
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
9
9
|
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
10
10
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
11
|
-
import type { Components } from '@libp2p/
|
|
11
|
+
import type { Components } from '@libp2p/components'
|
|
12
12
|
import { trackedMap } from '@libp2p/tracked-map'
|
|
13
13
|
|
|
14
14
|
const log = logger('libp2p:transports')
|
package/src/upgrader.ts
CHANGED
|
@@ -8,14 +8,16 @@ 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'
|
|
19
21
|
|
|
20
22
|
const log = logger('libp2p:upgrader')
|
|
21
23
|
|
|
@@ -43,6 +45,46 @@ export interface UpgraderInit {
|
|
|
43
45
|
muxers: StreamMuxerFactory[]
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
function findIncomingStreamLimit (protocol: string, registrar: Registrar) {
|
|
49
|
+
try {
|
|
50
|
+
const { options } = registrar.getHandler(protocol)
|
|
51
|
+
|
|
52
|
+
return options.maxInboundStreams
|
|
53
|
+
} catch (err: any) {
|
|
54
|
+
if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
|
|
55
|
+
throw err
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return DEFAULT_MAX_INBOUND_STREAMS
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function findOutgoingStreamLimit (protocol: string, registrar: Registrar) {
|
|
63
|
+
try {
|
|
64
|
+
const { options } = registrar.getHandler(protocol)
|
|
65
|
+
|
|
66
|
+
return options.maxOutboundStreams
|
|
67
|
+
} catch (err: any) {
|
|
68
|
+
if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
|
|
69
|
+
throw err
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return DEFAULT_MAX_OUTBOUND_STREAMS
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function countStreams (protocol: string, direction: 'inbound' | 'outbound', connection: Connection) {
|
|
77
|
+
let streamCount = 0
|
|
78
|
+
|
|
79
|
+
connection.streams.forEach(stream => {
|
|
80
|
+
if (stream.stat.direction === direction && stream.stat.protocol === protocol) {
|
|
81
|
+
streamCount++
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
return streamCount
|
|
86
|
+
}
|
|
87
|
+
|
|
46
88
|
export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upgrader {
|
|
47
89
|
private readonly components: Components
|
|
48
90
|
private readonly connectionEncryption: Map<string, ConnectionEncrypter>
|
|
@@ -267,12 +309,12 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
267
309
|
} = opts
|
|
268
310
|
|
|
269
311
|
let muxer: StreamMuxer | undefined
|
|
270
|
-
let newStream: ((multicodecs: string[], options?: AbortOptions) => Promise<
|
|
312
|
+
let newStream: ((multicodecs: string[], options?: AbortOptions) => Promise<Stream>) | undefined
|
|
271
313
|
let connection: Connection // eslint-disable-line prefer-const
|
|
272
314
|
|
|
273
315
|
if (muxerFactory != null) {
|
|
274
316
|
// Create the muxer
|
|
275
|
-
muxer = muxerFactory.createStreamMuxer(
|
|
317
|
+
muxer = muxerFactory.createStreamMuxer({
|
|
276
318
|
// Run anytime a remote stream is created
|
|
277
319
|
onIncomingStream: muxedStream => {
|
|
278
320
|
if (connection == null) {
|
|
@@ -296,11 +338,24 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
296
338
|
return
|
|
297
339
|
}
|
|
298
340
|
|
|
299
|
-
|
|
341
|
+
const incomingLimit = findIncomingStreamLimit(protocol, this.components.getRegistrar())
|
|
342
|
+
const streamCount = countStreams(protocol, 'inbound', connection)
|
|
343
|
+
|
|
344
|
+
if (streamCount === incomingLimit) {
|
|
345
|
+
throw errCode(new Error('Too many incoming protocol streams'), codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
muxedStream.stat.protocol = protocol
|
|
349
|
+
|
|
350
|
+
connection.addStream(muxedStream)
|
|
300
351
|
this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol })
|
|
301
352
|
})
|
|
302
353
|
.catch(err => {
|
|
303
354
|
log.error(err)
|
|
355
|
+
|
|
356
|
+
if (muxedStream.stat.timeline.close == null) {
|
|
357
|
+
muxedStream.close()
|
|
358
|
+
}
|
|
304
359
|
})
|
|
305
360
|
},
|
|
306
361
|
// Run anytime a stream closes
|
|
@@ -309,7 +364,11 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
309
364
|
}
|
|
310
365
|
})
|
|
311
366
|
|
|
312
|
-
|
|
367
|
+
if (isInitializable(muxer)) {
|
|
368
|
+
muxer.init(this.components)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
newStream = async (protocols: string[], options: AbortOptions = {}): Promise<Stream> => {
|
|
313
372
|
if (muxer == null) {
|
|
314
373
|
throw errCode(new Error('Stream is not multiplexed'), codes.ERR_MUXER_UNAVAILABLE)
|
|
315
374
|
}
|
|
@@ -326,10 +385,30 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
326
385
|
stream = metrics.trackStream({ stream, remotePeer, protocol })
|
|
327
386
|
}
|
|
328
387
|
|
|
329
|
-
|
|
388
|
+
const outgoingLimit = findOutgoingStreamLimit(protocol, this.components.getRegistrar())
|
|
389
|
+
const streamCount = countStreams(protocol, 'outbound', connection)
|
|
390
|
+
|
|
391
|
+
if (streamCount === outgoingLimit) {
|
|
392
|
+
throw errCode(new Error('Too many outgoing protocol streams'), codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
muxedStream.stat.protocol = protocol
|
|
396
|
+
|
|
397
|
+
return {
|
|
398
|
+
...muxedStream,
|
|
399
|
+
...stream,
|
|
400
|
+
stat: {
|
|
401
|
+
...muxedStream.stat,
|
|
402
|
+
protocol
|
|
403
|
+
}
|
|
404
|
+
}
|
|
330
405
|
} catch (err: any) {
|
|
331
406
|
log.error('could not create new stream', err)
|
|
332
407
|
|
|
408
|
+
if (muxedStream.stat.timeline.close == null) {
|
|
409
|
+
muxedStream.close()
|
|
410
|
+
}
|
|
411
|
+
|
|
333
412
|
if (err.code != null) {
|
|
334
413
|
throw err
|
|
335
414
|
}
|
|
@@ -390,9 +469,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
390
469
|
await maConn.close()
|
|
391
470
|
// Ensure remaining streams are closed
|
|
392
471
|
if (muxer != null) {
|
|
393
|
-
|
|
394
|
-
await stream.close()
|
|
395
|
-
}))
|
|
472
|
+
muxer.streams.forEach(s => s.close())
|
|
396
473
|
}
|
|
397
474
|
}
|
|
398
475
|
})
|
|
@@ -409,8 +486,9 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
409
486
|
*/
|
|
410
487
|
_onStream (opts: OnStreamOptions): void {
|
|
411
488
|
const { connection, stream, protocol } = opts
|
|
412
|
-
const handler = this.components.getRegistrar().getHandler(protocol)
|
|
413
|
-
|
|
489
|
+
const { handler } = this.components.getRegistrar().getHandler(protocol)
|
|
490
|
+
|
|
491
|
+
handler({ connection, stream })
|
|
414
492
|
}
|
|
415
493
|
|
|
416
494
|
/**
|