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.
- package/dist/index.min.js +17 -13
- package/dist/index.min.js.map +4 -4
- package/dist/src/address-manager/dns-mappings.d.ts +2 -1
- package/dist/src/address-manager/dns-mappings.d.ts.map +1 -1
- package/dist/src/address-manager/dns-mappings.js +43 -39
- package/dist/src/address-manager/dns-mappings.js.map +1 -1
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +14 -31
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/address-manager/ip-mappings.d.ts +0 -1
- package/dist/src/address-manager/ip-mappings.d.ts.map +1 -1
- package/dist/src/address-manager/ip-mappings.js +40 -51
- package/dist/src/address-manager/ip-mappings.js.map +1 -1
- package/dist/src/address-manager/observed-addresses.d.ts.map +1 -1
- package/dist/src/address-manager/observed-addresses.js +3 -1
- package/dist/src/address-manager/observed-addresses.js.map +1 -1
- package/dist/src/address-manager/transport-addresses.d.ts.map +1 -1
- package/dist/src/address-manager/transport-addresses.js +8 -6
- package/dist/src/address-manager/transport-addresses.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
- package/dist/src/config/connection-gater.browser.js +9 -3
- package/dist/src/config/connection-gater.browser.js.map +1 -1
- package/dist/src/config.js +1 -1
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/address-sorter.d.ts.map +1 -1
- package/dist/src/connection-manager/address-sorter.js +2 -1
- package/dist/src/connection-manager/address-sorter.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +3 -7
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
- package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.defaults.js +0 -4
- package/dist/src/connection-manager/constants.defaults.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +2 -2
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +23 -11
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +2 -10
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +26 -44
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/reconnect-queue.js +1 -1
- package/dist/src/connection-manager/reconnect-queue.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +3 -31
- package/dist/src/connection-manager/utils.d.ts.map +1 -1
- package/dist/src/connection-manager/utils.js +18 -99
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/connection-monitor.d.ts +1 -1
- package/dist/src/connection-monitor.d.ts.map +1 -1
- package/dist/src/connection-monitor.js +3 -2
- package/dist/src/connection-monitor.js.map +1 -1
- package/dist/src/connection.d.ts +9 -13
- package/dist/src/connection.d.ts.map +1 -1
- package/dist/src/connection.js +138 -87
- package/dist/src/connection.js.map +1 -1
- package/dist/src/get-peer.js +3 -3
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.js +2 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +5 -5
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.js +1 -1
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/random-walk.d.ts.map +1 -1
- package/dist/src/random-walk.js +3 -13
- package/dist/src/random-walk.js.map +1 -1
- package/dist/src/registrar.d.ts +3 -3
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +41 -50
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.js +2 -15
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +16 -26
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +122 -88
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +30 -27
- package/src/address-manager/dns-mappings.ts +50 -50
- package/src/address-manager/index.ts +17 -37
- package/src/address-manager/ip-mappings.ts +44 -64
- package/src/address-manager/observed-addresses.ts +3 -1
- package/src/address-manager/transport-addresses.ts +9 -7
- package/src/config/connection-gater.browser.ts +12 -3
- package/src/config.ts +1 -1
- package/src/connection-manager/address-sorter.ts +2 -1
- package/src/connection-manager/connection-pruner.ts +3 -8
- package/src/connection-manager/constants.defaults.ts +0 -5
- package/src/connection-manager/dial-queue.ts +27 -12
- package/src/connection-manager/index.ts +28 -61
- package/src/connection-manager/reconnect-queue.ts +1 -1
- package/src/connection-manager/utils.ts +21 -129
- package/src/connection-monitor.ts +4 -3
- package/src/connection.ts +185 -111
- package/src/get-peer.ts +3 -3
- package/src/index.ts +2 -2
- package/src/libp2p.ts +6 -5
- package/src/peer-routing.ts +1 -1
- package/src/random-walk.ts +3 -13
- package/src/registrar.ts +54 -67
- package/src/transport-manager.ts +2 -18
- package/src/upgrader.ts +149 -132
- package/src/version.ts +1 -1
- package/dist/src/utils.d.ts +0 -3
- package/dist/src/utils.d.ts.map +0 -1
- package/dist/src/utils.js +0 -25
- package/dist/src/utils.js.map +0 -1
- package/src/utils.ts +0 -31
package/src/connection.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { connectionSymbol, LimitedConnectionError, ConnectionClosedError, TooManyOutboundProtocolStreamsError, TooManyInboundProtocolStreamsError
|
|
1
|
+
import { connectionSymbol, LimitedConnectionError, ConnectionClosedError, ConnectionClosingError, TooManyOutboundProtocolStreamsError, TooManyInboundProtocolStreamsError } from '@libp2p/interface'
|
|
2
2
|
import * as mss from '@libp2p/multistream-select'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { CONNECTION_CLOSE_TIMEOUT, PROTOCOL_NEGOTIATION_TIMEOUT } from './connection-manager/constants.defaults.ts'
|
|
6
|
-
import { isDirect } from './connection-manager/utils.ts'
|
|
3
|
+
import { setMaxListeners } from 'main-event'
|
|
4
|
+
import { PROTOCOL_NEGOTIATION_TIMEOUT } from './connection-manager/constants.defaults.ts'
|
|
7
5
|
import { MuxerUnavailableError } from './errors.ts'
|
|
8
6
|
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.ts'
|
|
9
|
-
import type { AbortOptions, Logger,
|
|
7
|
+
import type { AbortOptions, Logger, Direction, Connection as ConnectionInterface, Stream, ConnectionTimeline, ConnectionStatus, NewStreamOptions, PeerId, ConnectionLimits, StreamMuxerFactory, StreamMuxer, Metrics, PeerStore, MultiaddrConnection } from '@libp2p/interface'
|
|
10
8
|
import type { Registrar } from '@libp2p/interface-internal'
|
|
11
9
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
12
10
|
|
|
11
|
+
const CLOSE_TIMEOUT = 500
|
|
12
|
+
|
|
13
13
|
export interface ConnectionComponents {
|
|
14
14
|
peerStore: PeerStore
|
|
15
15
|
registrar: Registrar
|
|
@@ -19,74 +19,80 @@ export interface ConnectionComponents {
|
|
|
19
19
|
export interface ConnectionInit {
|
|
20
20
|
id: string
|
|
21
21
|
maConn: MultiaddrConnection
|
|
22
|
-
stream: MessageStream
|
|
23
22
|
remotePeer: PeerId
|
|
24
|
-
direction?:
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
direction?: Direction
|
|
24
|
+
muxerFactory?: StreamMuxerFactory
|
|
25
|
+
encryption?: string
|
|
27
26
|
limits?: ConnectionLimits
|
|
28
27
|
outboundStreamProtocolNegotiationTimeout?: number
|
|
29
28
|
inboundStreamProtocolNegotiationTimeout?: number
|
|
30
|
-
closeTimeout?: number
|
|
31
29
|
}
|
|
32
30
|
|
|
33
31
|
/**
|
|
34
32
|
* An implementation of the js-libp2p connection.
|
|
35
33
|
* Any libp2p transport should use an upgrader to return this connection.
|
|
36
34
|
*/
|
|
37
|
-
export class Connection
|
|
35
|
+
export class Connection implements ConnectionInterface {
|
|
38
36
|
public readonly id: string
|
|
39
37
|
public readonly remoteAddr: Multiaddr
|
|
40
38
|
public readonly remotePeer: PeerId
|
|
41
|
-
public direction:
|
|
42
|
-
public timeline:
|
|
43
|
-
public direct: boolean
|
|
39
|
+
public direction: Direction
|
|
40
|
+
public timeline: ConnectionTimeline
|
|
44
41
|
public multiplexer?: string
|
|
45
42
|
public encryption?: string
|
|
43
|
+
public status: ConnectionStatus
|
|
46
44
|
public limits?: ConnectionLimits
|
|
47
45
|
public readonly log: Logger
|
|
46
|
+
public tags: string[]
|
|
48
47
|
|
|
49
48
|
private readonly maConn: MultiaddrConnection
|
|
50
49
|
private readonly muxer?: StreamMuxer
|
|
51
50
|
private readonly components: ConnectionComponents
|
|
52
51
|
private readonly outboundStreamProtocolNegotiationTimeout: number
|
|
53
52
|
private readonly inboundStreamProtocolNegotiationTimeout: number
|
|
54
|
-
private readonly closeTimeout: number
|
|
55
53
|
|
|
56
54
|
constructor (components: ConnectionComponents, init: ConnectionInit) {
|
|
57
|
-
super()
|
|
58
|
-
|
|
59
55
|
this.components = components
|
|
60
56
|
|
|
61
57
|
this.id = init.id
|
|
62
58
|
this.remoteAddr = init.maConn.remoteAddr
|
|
63
59
|
this.remotePeer = init.remotePeer
|
|
64
60
|
this.direction = init.direction ?? 'outbound'
|
|
61
|
+
this.status = 'open'
|
|
65
62
|
this.timeline = init.maConn.timeline
|
|
66
|
-
this.encryption = init.
|
|
63
|
+
this.encryption = init.encryption
|
|
67
64
|
this.limits = init.limits
|
|
68
65
|
this.maConn = init.maConn
|
|
69
66
|
this.log = init.maConn.log
|
|
70
67
|
this.outboundStreamProtocolNegotiationTimeout = init.outboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
|
|
71
68
|
this.inboundStreamProtocolNegotiationTimeout = init.inboundStreamProtocolNegotiationTimeout ?? PROTOCOL_NEGOTIATION_TIMEOUT
|
|
72
|
-
this.closeTimeout = init.closeTimeout ?? CONNECTION_CLOSE_TIMEOUT
|
|
73
|
-
this.direct = isDirect(init.maConn.remoteAddr)
|
|
74
|
-
|
|
75
|
-
this.onIncomingStream = this.onIncomingStream.bind(this)
|
|
76
69
|
|
|
77
|
-
if (this.remoteAddr.
|
|
70
|
+
if (this.remoteAddr.getPeerId() == null) {
|
|
78
71
|
this.remoteAddr = this.remoteAddr.encapsulate(`/p2p/${this.remotePeer}`)
|
|
79
72
|
}
|
|
80
73
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.
|
|
85
|
-
}
|
|
74
|
+
this.tags = []
|
|
75
|
+
|
|
76
|
+
if (init.muxerFactory != null) {
|
|
77
|
+
this.multiplexer = init.muxerFactory.protocol
|
|
86
78
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
79
|
+
this.muxer = init.muxerFactory.createStreamMuxer({
|
|
80
|
+
direction: this.direction,
|
|
81
|
+
log: this.log,
|
|
82
|
+
// Run anytime a remote stream is created
|
|
83
|
+
onIncomingStream: (stream) => {
|
|
84
|
+
this.onIncomingStream(stream)
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
// Pipe all data through the muxer
|
|
89
|
+
void Promise.all([
|
|
90
|
+
this.muxer.sink(this.maConn.source),
|
|
91
|
+
this.maConn.sink(this.muxer.source)
|
|
92
|
+
]).catch(err => {
|
|
93
|
+
this.log.error('error piping data through muxer - %e', err)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
readonly [Symbol.toStringTag] = 'Connection'
|
|
@@ -97,43 +103,32 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
|
|
|
97
103
|
return this.muxer?.streams ?? []
|
|
98
104
|
}
|
|
99
105
|
|
|
100
|
-
get status (): ConnectionStatus {
|
|
101
|
-
return this.maConn.status
|
|
102
|
-
}
|
|
103
|
-
|
|
104
106
|
/**
|
|
105
107
|
* Create a new stream over this connection
|
|
106
108
|
*/
|
|
107
109
|
newStream = async (protocols: string[], options: NewStreamOptions = {}): Promise<Stream> => {
|
|
108
|
-
if (this.
|
|
109
|
-
throw new
|
|
110
|
+
if (this.status === 'closing') {
|
|
111
|
+
throw new ConnectionClosingError('the connection is being closed')
|
|
110
112
|
}
|
|
111
113
|
|
|
112
|
-
if (this.
|
|
113
|
-
throw new ConnectionClosedError(
|
|
114
|
+
if (this.status === 'closed') {
|
|
115
|
+
throw new ConnectionClosedError('the connection is closed')
|
|
114
116
|
}
|
|
115
117
|
|
|
116
|
-
if (
|
|
117
|
-
|
|
118
|
+
if (!Array.isArray(protocols)) {
|
|
119
|
+
protocols = [protocols]
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
if (this.limits != null && options?.runOnLimitedConnection !== true) {
|
|
121
123
|
throw new LimitedConnectionError('Cannot open protocol stream on limited connection')
|
|
122
124
|
}
|
|
123
125
|
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
+
if (this.muxer == null) {
|
|
127
|
+
throw new MuxerUnavailableError('Connection is not multiplexed')
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
this.log.trace('starting new stream for protocols %s', protocols)
|
|
129
|
-
const muxedStream = await this.muxer.
|
|
130
|
-
...options,
|
|
131
|
-
|
|
132
|
-
// most underlying transports only support negotiating a single protocol
|
|
133
|
-
// so only pass the early protocol if a single protocol has been requested
|
|
134
|
-
// otherwise fall back to mss
|
|
135
|
-
protocol: protocols.length === 1 ? protocols[0] : undefined
|
|
136
|
-
})
|
|
131
|
+
const muxedStream = await this.muxer.newStream()
|
|
137
132
|
this.log.trace('started new stream %s for protocols %s', muxedStream.id, protocols)
|
|
138
133
|
|
|
139
134
|
try {
|
|
@@ -149,21 +144,24 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
|
|
|
149
144
|
}
|
|
150
145
|
}
|
|
151
146
|
|
|
152
|
-
|
|
153
|
-
muxedStream.log.trace('selecting protocol from protocols %s', protocols)
|
|
147
|
+
muxedStream.log.trace('selecting protocol from protocols %s', protocols)
|
|
154
148
|
|
|
155
|
-
|
|
149
|
+
const {
|
|
150
|
+
stream,
|
|
151
|
+
protocol
|
|
152
|
+
} = await mss.select(muxedStream, protocols, {
|
|
153
|
+
...options,
|
|
154
|
+
log: muxedStream.log,
|
|
155
|
+
yieldBytes: true
|
|
156
|
+
})
|
|
156
157
|
|
|
157
|
-
|
|
158
|
-
} else {
|
|
159
|
-
muxedStream.log('pre-negotiated protocol %s', muxedStream.protocol)
|
|
160
|
-
}
|
|
158
|
+
muxedStream.log('selected protocol %s', protocol)
|
|
161
159
|
|
|
162
|
-
const outgoingLimit = findOutgoingStreamLimit(
|
|
163
|
-
const streamCount = countStreams(
|
|
160
|
+
const outgoingLimit = findOutgoingStreamLimit(protocol, this.components.registrar, options)
|
|
161
|
+
const streamCount = countStreams(protocol, 'outbound', this)
|
|
164
162
|
|
|
165
|
-
if (streamCount
|
|
166
|
-
const err = new TooManyOutboundProtocolStreamsError(`Too many outbound protocol streams for protocol "${
|
|
163
|
+
if (streamCount >= outgoingLimit) {
|
|
164
|
+
const err = new TooManyOutboundProtocolStreamsError(`Too many outbound protocol streams for protocol "${protocol}" - ${streamCount}/${outgoingLimit}`)
|
|
167
165
|
muxedStream.abort(err)
|
|
168
166
|
|
|
169
167
|
throw err
|
|
@@ -172,83 +170,132 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
|
|
|
172
170
|
// If a protocol stream has been successfully negotiated and is to be passed to the application,
|
|
173
171
|
// the peer store should ensure that the peer is registered with that protocol
|
|
174
172
|
await this.components.peerStore.merge(this.remotePeer, {
|
|
175
|
-
protocols: [
|
|
173
|
+
protocols: [protocol]
|
|
176
174
|
})
|
|
177
175
|
|
|
178
|
-
|
|
176
|
+
// after the handshake the returned stream can have early data so override
|
|
177
|
+
// the source/sink
|
|
178
|
+
muxedStream.source = stream.source
|
|
179
|
+
muxedStream.sink = stream.sink
|
|
180
|
+
muxedStream.protocol = protocol
|
|
181
|
+
|
|
182
|
+
// allow closing the write end of a not-yet-negotiated stream
|
|
183
|
+
if (stream.closeWrite != null) {
|
|
184
|
+
muxedStream.closeWrite = stream.closeWrite
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// allow closing the read end of a not-yet-negotiated stream
|
|
188
|
+
if (stream.closeRead != null) {
|
|
189
|
+
muxedStream.closeRead = stream.closeRead
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// make sure we don't try to negotiate a stream we are closing
|
|
193
|
+
if (stream.close != null) {
|
|
194
|
+
muxedStream.close = stream.close
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
this.components.metrics?.trackProtocolStream(muxedStream, this)
|
|
198
|
+
|
|
199
|
+
muxedStream.direction = 'outbound'
|
|
179
200
|
|
|
180
201
|
return muxedStream
|
|
181
202
|
} catch (err: any) {
|
|
182
|
-
|
|
203
|
+
this.log.error('could not create new outbound stream on connection %s %a for protocols %s - %e', this.direction === 'inbound' ? 'from' : 'to', this.remoteAddr, protocols, err)
|
|
204
|
+
|
|
205
|
+
if (muxedStream.timeline.close == null) {
|
|
183
206
|
muxedStream.abort(err)
|
|
184
|
-
} else {
|
|
185
|
-
this.log.error('could not create new outbound stream on connection %s %a for protocols %s - %e', this.direction === 'inbound' ? 'from' : 'to', this.remoteAddr, protocols, err)
|
|
186
207
|
}
|
|
187
208
|
|
|
188
209
|
throw err
|
|
189
210
|
}
|
|
190
211
|
}
|
|
191
212
|
|
|
192
|
-
private
|
|
193
|
-
const muxedStream = evt.detail
|
|
194
|
-
|
|
213
|
+
private onIncomingStream (muxedStream: Stream): void {
|
|
195
214
|
const signal = AbortSignal.timeout(this.inboundStreamProtocolNegotiationTimeout)
|
|
196
215
|
setMaxListeners(Infinity, signal)
|
|
197
216
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
try {
|
|
201
|
-
if (muxedStream.protocol === '') {
|
|
217
|
+
void Promise.resolve()
|
|
218
|
+
.then(async () => {
|
|
202
219
|
const protocols = this.components.registrar.getProtocols()
|
|
203
220
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
221
|
+
const { stream, protocol } = await mss.handle(muxedStream, protocols, {
|
|
222
|
+
signal,
|
|
223
|
+
log: muxedStream.log,
|
|
224
|
+
yieldBytes: false
|
|
208
225
|
})
|
|
209
226
|
|
|
210
|
-
|
|
211
|
-
} else {
|
|
212
|
-
muxedStream.log('pre-negotiated protocol %s', muxedStream.protocol)
|
|
213
|
-
}
|
|
227
|
+
this.log('incoming %s stream opened', protocol)
|
|
214
228
|
|
|
215
|
-
|
|
216
|
-
|
|
229
|
+
const incomingLimit = findIncomingStreamLimit(protocol, this.components.registrar)
|
|
230
|
+
const streamCount = countStreams(protocol, 'inbound', this)
|
|
217
231
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
232
|
+
if (streamCount === incomingLimit) {
|
|
233
|
+
const err = new TooManyInboundProtocolStreamsError(`Too many inbound protocol streams for protocol "${protocol}" - limit ${incomingLimit}`)
|
|
234
|
+
muxedStream.abort(err)
|
|
221
235
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
await this.components.peerStore.merge(this.remotePeer, {
|
|
225
|
-
protocols: [muxedStream.protocol]
|
|
226
|
-
}, {
|
|
227
|
-
signal
|
|
228
|
-
})
|
|
236
|
+
throw err
|
|
237
|
+
}
|
|
229
238
|
|
|
230
|
-
|
|
239
|
+
// after the handshake the returned stream can have early data so override
|
|
240
|
+
// the source/sink
|
|
241
|
+
muxedStream.source = stream.source
|
|
242
|
+
muxedStream.sink = stream.sink
|
|
243
|
+
muxedStream.protocol = protocol
|
|
231
244
|
|
|
232
|
-
|
|
245
|
+
// allow closing the write end of a not-yet-negotiated stream
|
|
246
|
+
if (stream.closeWrite != null) {
|
|
247
|
+
muxedStream.closeWrite = stream.closeWrite
|
|
248
|
+
}
|
|
233
249
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
250
|
+
// allow closing the read end of a not-yet-negotiated stream
|
|
251
|
+
if (stream.closeRead != null) {
|
|
252
|
+
muxedStream.closeRead = stream.closeRead
|
|
253
|
+
}
|
|
237
254
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
255
|
+
// make sure we don't try to negotiate a stream we are closing
|
|
256
|
+
if (stream.close != null) {
|
|
257
|
+
muxedStream.close = stream.close
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// If a protocol stream has been successfully negotiated and is to be passed to the application,
|
|
261
|
+
// the peer store should ensure that the peer is registered with that protocol
|
|
262
|
+
await this.components.peerStore.merge(this.remotePeer, {
|
|
263
|
+
protocols: [protocol]
|
|
264
|
+
}, {
|
|
265
|
+
signal
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
this.components.metrics?.trackProtocolStream(muxedStream, this)
|
|
269
|
+
|
|
270
|
+
const { handler, options } = this.components.registrar.getHandler(protocol)
|
|
271
|
+
|
|
272
|
+
if (this.limits != null && options.runOnLimitedConnection !== true) {
|
|
273
|
+
throw new LimitedConnectionError('Cannot open protocol stream on limited connection')
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
await handler({ connection: this, stream: muxedStream })
|
|
277
|
+
})
|
|
278
|
+
.catch(async err => {
|
|
279
|
+
this.log.error('error handling incoming stream id %s - %e', muxedStream.id, err)
|
|
280
|
+
|
|
281
|
+
muxedStream.abort(err)
|
|
282
|
+
})
|
|
242
283
|
}
|
|
243
284
|
|
|
244
285
|
/**
|
|
245
286
|
* Close the connection
|
|
246
287
|
*/
|
|
247
288
|
async close (options: AbortOptions = {}): Promise<void> {
|
|
289
|
+
if (this.status === 'closed' || this.status === 'closing') {
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
|
|
248
293
|
this.log('closing connection to %a', this.remoteAddr)
|
|
249
294
|
|
|
295
|
+
this.status = 'closing'
|
|
296
|
+
|
|
250
297
|
if (options.signal == null) {
|
|
251
|
-
const signal = AbortSignal.timeout(
|
|
298
|
+
const signal = AbortSignal.timeout(CLOSE_TIMEOUT)
|
|
252
299
|
setMaxListeners(Infinity, signal)
|
|
253
300
|
|
|
254
301
|
options = {
|
|
@@ -257,13 +304,42 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
|
|
|
257
304
|
}
|
|
258
305
|
}
|
|
259
306
|
|
|
260
|
-
|
|
261
|
-
|
|
307
|
+
try {
|
|
308
|
+
this.log.trace('closing underlying transport')
|
|
309
|
+
|
|
310
|
+
// ensure remaining streams are closed gracefully
|
|
311
|
+
await this.muxer?.close(options)
|
|
312
|
+
|
|
313
|
+
// close the underlying transport
|
|
314
|
+
await this.maConn.close(options)
|
|
315
|
+
|
|
316
|
+
this.log.trace('updating timeline with close time')
|
|
317
|
+
|
|
318
|
+
this.status = 'closed'
|
|
319
|
+
this.timeline.close = Date.now()
|
|
320
|
+
} catch (err: any) {
|
|
321
|
+
this.log.error('error encountered during graceful close of connection to %a', this.remoteAddr, err)
|
|
322
|
+
this.abort(err)
|
|
323
|
+
}
|
|
262
324
|
}
|
|
263
325
|
|
|
264
326
|
abort (err: Error): void {
|
|
327
|
+
if (this.status === 'closed') {
|
|
328
|
+
return
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
this.log.error('aborting connection to %a due to error', this.remoteAddr, err)
|
|
332
|
+
|
|
333
|
+
this.status = 'closing'
|
|
334
|
+
|
|
335
|
+
// ensure remaining streams are aborted
|
|
265
336
|
this.muxer?.abort(err)
|
|
337
|
+
|
|
338
|
+
// abort the underlying transport
|
|
266
339
|
this.maConn.abort(err)
|
|
340
|
+
|
|
341
|
+
this.status = 'closed'
|
|
342
|
+
this.timeline.close = Date.now()
|
|
267
343
|
}
|
|
268
344
|
}
|
|
269
345
|
|
|
@@ -271,13 +347,11 @@ export function createConnection (components: ConnectionComponents, init: Connec
|
|
|
271
347
|
return new Connection(components, init)
|
|
272
348
|
}
|
|
273
349
|
|
|
274
|
-
function findIncomingStreamLimit (protocol: string, registrar: Registrar): number {
|
|
350
|
+
function findIncomingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
|
|
275
351
|
try {
|
|
276
352
|
const { options } = registrar.getHandler(protocol)
|
|
277
353
|
|
|
278
|
-
|
|
279
|
-
return options.maxInboundStreams
|
|
280
|
-
}
|
|
354
|
+
return options.maxInboundStreams
|
|
281
355
|
} catch (err: any) {
|
|
282
356
|
if (err.name !== 'UnhandledProtocolError') {
|
|
283
357
|
throw err
|
package/src/get-peer.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InvalidMultiaddrError, InvalidParametersError, isPeerId } from '@libp2p/interface'
|
|
2
2
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
3
|
-
import {
|
|
3
|
+
import { isMultiaddr } from '@multiformats/multiaddr'
|
|
4
4
|
import { PEER_ID } from '@multiformats/multiaddr-matcher'
|
|
5
5
|
import type { PeerId } from '@libp2p/interface'
|
|
6
6
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
@@ -24,7 +24,7 @@ export function getPeerAddress (peer: PeerId | Multiaddr | Multiaddr[]): PeerAdd
|
|
|
24
24
|
let peerId: PeerId | undefined
|
|
25
25
|
|
|
26
26
|
if (multiaddrs.length > 0) {
|
|
27
|
-
const peerIdStr = multiaddrs[0].
|
|
27
|
+
const peerIdStr = multiaddrs[0].getPeerId()
|
|
28
28
|
peerId = peerIdStr == null ? undefined : peerIdFromString(peerIdStr)
|
|
29
29
|
|
|
30
30
|
// ensure PeerId is either not set or is consistent
|
|
@@ -33,7 +33,7 @@ export function getPeerAddress (peer: PeerId | Multiaddr | Multiaddr[]): PeerAdd
|
|
|
33
33
|
throw new InvalidMultiaddrError('Invalid multiaddr')
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const maPeerIdStr = ma.
|
|
36
|
+
const maPeerIdStr = ma.getPeerId()
|
|
37
37
|
|
|
38
38
|
if (maPeerIdStr == null) {
|
|
39
39
|
if (peerId != null) {
|
package/src/index.ts
CHANGED
|
@@ -184,8 +184,8 @@ export type Libp2pOptions<T extends ServiceMap = ServiceMap> = Libp2pInit<T> & {
|
|
|
184
184
|
* import { createLibp2p } from 'libp2p'
|
|
185
185
|
* import { tcp } from '@libp2p/tcp'
|
|
186
186
|
* import { mplex } from '@libp2p/mplex'
|
|
187
|
-
* import { noise } from '@libp2p
|
|
188
|
-
* import { yamux } from '@libp2p
|
|
187
|
+
* import { noise } from '@chainsafe/libp2p-noise'
|
|
188
|
+
* import { yamux } from '@chainsafe/libp2p-yamux'
|
|
189
189
|
*
|
|
190
190
|
* // specify options
|
|
191
191
|
* const options = {
|
package/src/libp2p.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { defaultLogger } from '@libp2p/logger'
|
|
|
4
4
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
5
5
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
6
6
|
import { persistentPeerStore } from '@libp2p/peer-store'
|
|
7
|
-
import {
|
|
7
|
+
import { isMultiaddr } from '@multiformats/multiaddr'
|
|
8
8
|
import { MemoryDatastore } from 'datastore-core/memory'
|
|
9
9
|
import { TypedEventEmitter, setMaxListeners } from 'main-event'
|
|
10
10
|
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
@@ -120,8 +120,7 @@ export class Libp2p<T extends ServiceMap = ServiceMap> extends TypedEventEmitter
|
|
|
120
120
|
streamMuxers: (init.streamMuxers ?? []).map((fn, index) => this.configureComponent(`stream-muxers-${index}`, fn(this.components))),
|
|
121
121
|
inboundUpgradeTimeout: init.connectionManager?.inboundUpgradeTimeout,
|
|
122
122
|
inboundStreamProtocolNegotiationTimeout: init.connectionManager?.inboundStreamProtocolNegotiationTimeout ?? init.connectionManager?.protocolNegotiationTimeout,
|
|
123
|
-
outboundStreamProtocolNegotiationTimeout: init.connectionManager?.outboundStreamProtocolNegotiationTimeout ?? init.connectionManager?.protocolNegotiationTimeout
|
|
124
|
-
connectionCloseTimeout: init.connectionManager?.connectionCloseTimeout
|
|
123
|
+
outboundStreamProtocolNegotiationTimeout: init.connectionManager?.outboundStreamProtocolNegotiationTimeout ?? init.connectionManager?.protocolNegotiationTimeout
|
|
125
124
|
})
|
|
126
125
|
|
|
127
126
|
// Setup the transport manager
|
|
@@ -305,7 +304,9 @@ export class Libp2p<T extends ServiceMap = ServiceMap> extends TypedEventEmitter
|
|
|
305
304
|
throw new InvalidParametersError('no protocols were provided to open a stream')
|
|
306
305
|
}
|
|
307
306
|
|
|
308
|
-
|
|
307
|
+
const connection = await this.dial(peer, options)
|
|
308
|
+
|
|
309
|
+
return connection.newStream(protocols, options)
|
|
309
310
|
}
|
|
310
311
|
|
|
311
312
|
getMultiaddrs (): Multiaddr[] {
|
|
@@ -318,7 +319,7 @@ export class Libp2p<T extends ServiceMap = ServiceMap> extends TypedEventEmitter
|
|
|
318
319
|
|
|
319
320
|
async hangUp (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<void> {
|
|
320
321
|
if (isMultiaddr(peer)) {
|
|
321
|
-
peer = peerIdFromString(peer.
|
|
322
|
+
peer = peerIdFromString(peer.getPeerId() ?? '')
|
|
322
323
|
}
|
|
323
324
|
|
|
324
325
|
await this.components.connectionManager.closeConnections(peer, options)
|
package/src/peer-routing.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NotFoundError } from '@libp2p/interface'
|
|
2
|
-
import { createScalableCuckooFilter } from '@libp2p/utils'
|
|
2
|
+
import { createScalableCuckooFilter } from '@libp2p/utils/filters'
|
|
3
3
|
import merge from 'it-merge'
|
|
4
4
|
import parallel from 'it-parallel'
|
|
5
5
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
package/src/random-walk.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { randomBytes } from '@libp2p/crypto'
|
|
|
2
2
|
import { anySignal } from 'any-signal'
|
|
3
3
|
import { TypedEventEmitter, setMaxListeners } from 'main-event'
|
|
4
4
|
import pDefer from 'p-defer'
|
|
5
|
-
import {
|
|
5
|
+
import { raceEvent } from 'race-event'
|
|
6
6
|
import { raceSignal } from 'race-signal'
|
|
7
7
|
import type { AbortOptions, ComponentLogger, Logger, PeerInfo, PeerRouting, Startable } from '@libp2p/interface'
|
|
8
8
|
import type { RandomWalk as RandomWalkInterface } from '@libp2p/interface-internal'
|
|
@@ -68,22 +68,12 @@ export class RandomWalk extends TypedEventEmitter<RandomWalkEvents> implements R
|
|
|
68
68
|
this.needNext = pDefer()
|
|
69
69
|
|
|
70
70
|
// wait for a walk:peer or walk:error event
|
|
71
|
-
const event = await
|
|
72
|
-
|
|
73
|
-
rejectionEvents: [
|
|
74
|
-
'walk:error'
|
|
75
|
-
]
|
|
71
|
+
const event = await raceEvent<CustomEvent<PeerInfo>>(this, 'walk:peer', signal, {
|
|
72
|
+
errorEvent: 'walk:error'
|
|
76
73
|
})
|
|
77
74
|
|
|
78
75
|
yield event.detail
|
|
79
76
|
}
|
|
80
|
-
} catch (err: any) {
|
|
81
|
-
// test for walk:error event
|
|
82
|
-
if (err.detail != null) {
|
|
83
|
-
throw err.detail
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
throw err
|
|
87
77
|
} finally {
|
|
88
78
|
signal.clear()
|
|
89
79
|
this.walkers--
|