libp2p 0.45.9-5315f7bc → 0.45.9-57c32721
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 +24 -24
- package/dist/src/address-manager/utils.d.ts.map +1 -1
- package/dist/src/address-manager/utils.js.map +1 -1
- package/dist/src/autonat/index.d.ts +20 -0
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +20 -0
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/index.d.ts +35 -0
- package/dist/src/circuit-relay/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/index.js +35 -0
- package/dist/src/circuit-relay/index.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +21 -19
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts +11 -0
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +47 -20
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +1 -0
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +6 -0
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +22 -8
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/connection/index.d.ts +19 -10
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +52 -28
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +3 -1
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +2 -2
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/constants.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +5 -10
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +1 -1
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +11 -5
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/fetch/constants.d.ts.map +1 -1
- package/dist/src/fetch/constants.js.map +1 -1
- package/dist/src/fetch/index.js +4 -4
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/consts.d.ts.map +1 -1
- package/dist/src/identify/consts.js.map +1 -1
- package/dist/src/identify/identify.d.ts +5 -4
- package/dist/src/identify/identify.d.ts.map +1 -1
- package/dist/src/identify/identify.js +63 -69
- package/dist/src/identify/identify.js.map +1 -1
- package/dist/src/identify/index.d.ts +22 -3
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +3 -3
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +2 -2
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js.map +1 -1
- package/dist/src/ping/index.d.ts +1 -0
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +31 -20
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +3 -0
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +2 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +36 -25
- 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 +28 -31
- package/src/address-manager/utils.ts +0 -1
- package/src/autonat/index.ts +21 -0
- package/src/circuit-relay/index.ts +36 -0
- package/src/circuit-relay/server/index.ts +24 -21
- package/src/circuit-relay/transport/index.ts +61 -22
- package/src/circuit-relay/transport/listener.ts +2 -0
- package/src/circuit-relay/transport/reservation-store.ts +33 -10
- package/src/connection/index.ts +78 -36
- package/src/connection-manager/auto-dial.ts +3 -1
- package/src/connection-manager/connection-pruner.ts +2 -2
- package/src/connection-manager/constants.ts +0 -1
- package/src/connection-manager/dial-queue.ts +8 -14
- package/src/connection-manager/index.ts +11 -5
- package/src/fetch/constants.ts +0 -1
- package/src/fetch/index.ts +4 -4
- package/src/identify/consts.ts +0 -1
- package/src/identify/identify.ts +73 -79
- package/src/identify/index.ts +26 -2
- package/src/libp2p.ts +4 -4
- package/src/ping/constants.ts +0 -1
- package/src/ping/index.ts +38 -20
- package/src/transport-manager.ts +4 -0
- package/src/upgrader.ts +40 -26
- package/src/version.ts +1 -1
package/src/fetch/index.ts
CHANGED
|
@@ -109,12 +109,12 @@ class DefaultFetchService implements Startable, FetchService {
|
|
|
109
109
|
async start (): Promise<void> {
|
|
110
110
|
await this.components.registrar.handle(this.protocol, (data) => {
|
|
111
111
|
void this.handleMessage(data)
|
|
112
|
+
.then(async () => {
|
|
113
|
+
await data.stream.close()
|
|
114
|
+
})
|
|
112
115
|
.catch(err => {
|
|
113
116
|
log.error(err)
|
|
114
117
|
})
|
|
115
|
-
.finally(() => {
|
|
116
|
-
data.stream.close()
|
|
117
|
-
})
|
|
118
118
|
}, {
|
|
119
119
|
maxInboundStreams: this.init.maxInboundStreams,
|
|
120
120
|
maxOutboundStreams: this.init.maxOutboundStreams
|
|
@@ -201,7 +201,7 @@ class DefaultFetchService implements Startable, FetchService {
|
|
|
201
201
|
return result ?? null
|
|
202
202
|
} finally {
|
|
203
203
|
if (stream != null) {
|
|
204
|
-
stream.close()
|
|
204
|
+
await stream.close()
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
207
|
}
|
package/src/identify/consts.ts
CHANGED
package/src/identify/identify.ts
CHANGED
|
@@ -4,12 +4,7 @@ import { logger } from '@libp2p/logger'
|
|
|
4
4
|
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
5
5
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
6
6
|
import { type Multiaddr, multiaddr, protocols } from '@multiformats/multiaddr'
|
|
7
|
-
import {
|
|
8
|
-
import { anySignal } from 'any-signal'
|
|
9
|
-
import first from 'it-first'
|
|
10
|
-
import * as lp from 'it-length-prefixed'
|
|
11
|
-
import { pbStream } from 'it-pb-stream'
|
|
12
|
-
import { pipe } from 'it-pipe'
|
|
7
|
+
import { pbStream } from 'it-protobuf-stream'
|
|
13
8
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
14
9
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
15
10
|
import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe'
|
|
@@ -23,7 +18,7 @@ import {
|
|
|
23
18
|
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
|
|
24
19
|
} from './consts.js'
|
|
25
20
|
import { Identify } from './pb/message.js'
|
|
26
|
-
import type { IdentifyServiceComponents, IdentifyServiceInit } from './index.js'
|
|
21
|
+
import type { IdentifyService, IdentifyServiceComponents, IdentifyServiceInit } from './index.js'
|
|
27
22
|
import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions } from '@libp2p/interface'
|
|
28
23
|
import type { Connection, Stream } from '@libp2p/interface/connection'
|
|
29
24
|
import type { EventEmitter } from '@libp2p/interface/events'
|
|
@@ -49,10 +44,12 @@ const defaultValues = {
|
|
|
49
44
|
maxPushIncomingStreams: 1,
|
|
50
45
|
maxPushOutgoingStreams: 1,
|
|
51
46
|
maxObservedAddresses: 10,
|
|
52
|
-
maxIdentifyMessageSize: 8192
|
|
47
|
+
maxIdentifyMessageSize: 8192,
|
|
48
|
+
runOnConnectionOpen: true,
|
|
49
|
+
runOnTransientConnection: true
|
|
53
50
|
}
|
|
54
51
|
|
|
55
|
-
export class DefaultIdentifyService implements Startable {
|
|
52
|
+
export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
56
53
|
private readonly identifyProtocolStr: string
|
|
57
54
|
private readonly identifyPushProtocolStr: string
|
|
58
55
|
public readonly host: {
|
|
@@ -74,6 +71,7 @@ export class DefaultIdentifyService implements Startable {
|
|
|
74
71
|
private readonly maxIdentifyMessageSize: number
|
|
75
72
|
private readonly maxObservedAddresses: number
|
|
76
73
|
private readonly events: EventEmitter<Libp2pEvents>
|
|
74
|
+
private readonly runOnTransientConnection: boolean
|
|
77
75
|
|
|
78
76
|
constructor (components: IdentifyServiceComponents, init: IdentifyServiceInit) {
|
|
79
77
|
this.started = false
|
|
@@ -93,6 +91,7 @@ export class DefaultIdentifyService implements Startable {
|
|
|
93
91
|
this.maxPushOutgoingStreams = init.maxPushOutgoingStreams ?? defaultValues.maxPushOutgoingStreams
|
|
94
92
|
this.maxIdentifyMessageSize = init.maxIdentifyMessageSize ?? defaultValues.maxIdentifyMessageSize
|
|
95
93
|
this.maxObservedAddresses = init.maxObservedAddresses ?? defaultValues.maxObservedAddresses
|
|
94
|
+
this.runOnTransientConnection = init.runOnTransientConnection ?? defaultValues.runOnTransientConnection
|
|
96
95
|
|
|
97
96
|
// Store self host metadata
|
|
98
97
|
this.host = {
|
|
@@ -100,11 +99,13 @@ export class DefaultIdentifyService implements Startable {
|
|
|
100
99
|
agentVersion: init.agentVersion ?? defaultValues.agentVersion
|
|
101
100
|
}
|
|
102
101
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
102
|
+
if (init.runOnConnectionOpen ?? defaultValues.runOnConnectionOpen) {
|
|
103
|
+
// When a new connection happens, trigger identify
|
|
104
|
+
components.events.addEventListener('connection:open', (evt) => {
|
|
105
|
+
const connection = evt.detail
|
|
106
|
+
this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
|
|
107
|
+
})
|
|
108
|
+
}
|
|
108
109
|
|
|
109
110
|
// When self peer record changes, trigger identify-push
|
|
110
111
|
components.events.addEventListener('self:peer:update', (evt) => {
|
|
@@ -143,7 +144,8 @@ export class DefaultIdentifyService implements Startable {
|
|
|
143
144
|
})
|
|
144
145
|
}, {
|
|
145
146
|
maxInboundStreams: this.maxInboundStreams,
|
|
146
|
-
maxOutboundStreams: this.maxOutboundStreams
|
|
147
|
+
maxOutboundStreams: this.maxOutboundStreams,
|
|
148
|
+
runOnTransientConnection: this.runOnTransientConnection
|
|
147
149
|
})
|
|
148
150
|
await this.registrar.handle(this.identifyPushProtocolStr, (data) => {
|
|
149
151
|
void this._handlePush(data).catch(err => {
|
|
@@ -151,7 +153,8 @@ export class DefaultIdentifyService implements Startable {
|
|
|
151
153
|
})
|
|
152
154
|
}, {
|
|
153
155
|
maxInboundStreams: this.maxPushIncomingStreams,
|
|
154
|
-
maxOutboundStreams: this.maxPushOutgoingStreams
|
|
156
|
+
maxOutboundStreams: this.maxPushOutgoingStreams,
|
|
157
|
+
runOnTransientConnection: this.runOnTransientConnection
|
|
155
158
|
})
|
|
156
159
|
|
|
157
160
|
this.started = true
|
|
@@ -191,29 +194,31 @@ export class DefaultIdentifyService implements Startable {
|
|
|
191
194
|
|
|
192
195
|
try {
|
|
193
196
|
stream = await connection.newStream([this.identifyPushProtocolStr], {
|
|
197
|
+
signal,
|
|
198
|
+
runOnTransientConnection: this.runOnTransientConnection
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
const pb = pbStream(stream, {
|
|
202
|
+
maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
|
|
203
|
+
}).pb(Identify)
|
|
204
|
+
|
|
205
|
+
await pb.write({
|
|
206
|
+
listenAddrs: listenAddresses.map(ma => ma.bytes),
|
|
207
|
+
signedPeerRecord: signedPeerRecord.marshal(),
|
|
208
|
+
protocols: supportedProtocols,
|
|
209
|
+
agentVersion,
|
|
210
|
+
protocolVersion
|
|
211
|
+
}, {
|
|
194
212
|
signal
|
|
195
213
|
})
|
|
196
214
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
await source.sink(pipe(
|
|
201
|
-
[Identify.encode({
|
|
202
|
-
listenAddrs: listenAddresses.map(ma => ma.bytes),
|
|
203
|
-
signedPeerRecord: signedPeerRecord.marshal(),
|
|
204
|
-
protocols: supportedProtocols,
|
|
205
|
-
agentVersion,
|
|
206
|
-
protocolVersion
|
|
207
|
-
})],
|
|
208
|
-
(source) => lp.encode(source)
|
|
209
|
-
))
|
|
215
|
+
await stream.close({
|
|
216
|
+
signal
|
|
217
|
+
})
|
|
210
218
|
} catch (err: any) {
|
|
211
219
|
// Just log errors
|
|
212
220
|
log.error('could not push identify update to peer', err)
|
|
213
|
-
|
|
214
|
-
if (stream != null) {
|
|
215
|
-
stream.close()
|
|
216
|
-
}
|
|
221
|
+
stream?.abort(err)
|
|
217
222
|
}
|
|
218
223
|
})
|
|
219
224
|
|
|
@@ -255,48 +260,31 @@ export class DefaultIdentifyService implements Startable {
|
|
|
255
260
|
async _identify (connection: Connection, options: AbortOptions = {}): Promise<Identify> {
|
|
256
261
|
let stream: Stream | undefined
|
|
257
262
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
try {
|
|
261
|
-
// fails on node < 15.4
|
|
262
|
-
setMaxListeners?.(Infinity, signal)
|
|
263
|
-
} catch {}
|
|
263
|
+
options.signal = options.signal ?? AbortSignal.timeout(this.timeout)
|
|
264
264
|
|
|
265
265
|
try {
|
|
266
266
|
stream = await connection.newStream([this.identifyProtocolStr], {
|
|
267
|
-
|
|
267
|
+
...options,
|
|
268
|
+
runOnTransientConnection: this.runOnTransientConnection
|
|
268
269
|
})
|
|
269
270
|
|
|
270
|
-
|
|
271
|
-
|
|
271
|
+
const pb = pbStream(stream, {
|
|
272
|
+
maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
|
|
273
|
+
}).pb(Identify)
|
|
272
274
|
|
|
273
|
-
const
|
|
274
|
-
[],
|
|
275
|
-
source,
|
|
276
|
-
(source) => lp.decode(source, {
|
|
277
|
-
maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
|
|
278
|
-
}),
|
|
279
|
-
async (source) => first(source)
|
|
280
|
-
)
|
|
275
|
+
const message = await pb.read(options)
|
|
281
276
|
|
|
282
|
-
|
|
283
|
-
throw new CodeError('No data could be retrieved', codes.ERR_CONNECTION_ENDED)
|
|
284
|
-
}
|
|
277
|
+
await stream.close(options)
|
|
285
278
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
} finally {
|
|
292
|
-
if (stream != null) {
|
|
293
|
-
stream.close()
|
|
294
|
-
}
|
|
295
|
-
signal.clear()
|
|
279
|
+
return message
|
|
280
|
+
} catch (err: any) {
|
|
281
|
+
log.error('error while reading identify message', err)
|
|
282
|
+
stream?.abort(err)
|
|
283
|
+
throw err
|
|
296
284
|
}
|
|
297
285
|
}
|
|
298
286
|
|
|
299
|
-
async identify (connection: Connection, options: AbortOptions = {}): Promise<
|
|
287
|
+
async identify (connection: Connection, options: AbortOptions = {}): Promise<IdentifyResult> {
|
|
300
288
|
const message = await this._identify(connection, options)
|
|
301
289
|
const {
|
|
302
290
|
publicKey,
|
|
@@ -344,6 +332,8 @@ export class DefaultIdentifyService implements Startable {
|
|
|
344
332
|
}
|
|
345
333
|
|
|
346
334
|
this.events.safeDispatchEvent('peer:identify', { detail: result })
|
|
335
|
+
|
|
336
|
+
return result
|
|
347
337
|
}
|
|
348
338
|
|
|
349
339
|
/**
|
|
@@ -376,7 +366,9 @@ export class DefaultIdentifyService implements Startable {
|
|
|
376
366
|
signedPeerRecord = envelope.marshal().subarray()
|
|
377
367
|
}
|
|
378
368
|
|
|
379
|
-
const
|
|
369
|
+
const pb = pbStream(stream).pb(Identify)
|
|
370
|
+
|
|
371
|
+
await pb.write({
|
|
380
372
|
protocolVersion: this.host.protocolVersion,
|
|
381
373
|
agentVersion: this.host.agentVersion,
|
|
382
374
|
publicKey,
|
|
@@ -384,17 +376,16 @@ export class DefaultIdentifyService implements Startable {
|
|
|
384
376
|
signedPeerRecord,
|
|
385
377
|
observedAddr: connection.remoteAddr.bytes,
|
|
386
378
|
protocols: peerData.protocols
|
|
379
|
+
}, {
|
|
380
|
+
signal
|
|
387
381
|
})
|
|
388
382
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const msgWithLenPrefix = pipe([message], (source) => lp.encode(source))
|
|
393
|
-
await source.sink(msgWithLenPrefix)
|
|
383
|
+
await stream.close({
|
|
384
|
+
signal
|
|
385
|
+
})
|
|
394
386
|
} catch (err: any) {
|
|
395
387
|
log.error('could not respond to identify request', err)
|
|
396
|
-
|
|
397
|
-
stream.close()
|
|
388
|
+
stream.abort(err)
|
|
398
389
|
}
|
|
399
390
|
}
|
|
400
391
|
|
|
@@ -409,19 +400,22 @@ export class DefaultIdentifyService implements Startable {
|
|
|
409
400
|
throw new Error('received push from ourselves?')
|
|
410
401
|
}
|
|
411
402
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
403
|
+
const options = {
|
|
404
|
+
signal: AbortSignal.timeout(this.timeout)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const pb = pbStream(stream, {
|
|
415
408
|
maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
|
|
416
|
-
})
|
|
417
|
-
|
|
409
|
+
}).pb(Identify)
|
|
410
|
+
|
|
411
|
+
const message = await pb.read(options)
|
|
412
|
+
await stream.close(options)
|
|
418
413
|
|
|
419
414
|
await this.#consumeIdentifyMessage(connection.remotePeer, message)
|
|
420
415
|
} catch (err: any) {
|
|
421
416
|
log.error('received invalid message', err)
|
|
417
|
+
stream.abort(err)
|
|
422
418
|
return
|
|
423
|
-
} finally {
|
|
424
|
-
stream.close()
|
|
425
419
|
}
|
|
426
420
|
|
|
427
421
|
log('handled push from %p', connection.remotePeer)
|
package/src/identify/index.ts
CHANGED
|
@@ -4,10 +4,11 @@ import {
|
|
|
4
4
|
} from './consts.js'
|
|
5
5
|
import { DefaultIdentifyService } from './identify.js'
|
|
6
6
|
import { Identify } from './pb/message.js'
|
|
7
|
-
import type { Libp2pEvents } from '@libp2p/interface'
|
|
7
|
+
import type { AbortOptions, IdentifyResult, Libp2pEvents } from '@libp2p/interface'
|
|
8
8
|
import type { EventEmitter } from '@libp2p/interface/events'
|
|
9
9
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
10
10
|
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
11
|
+
import type { Connection } from '@libp2p/interface/src/connection/index.js'
|
|
11
12
|
import type { AddressManager } from '@libp2p/interface-internal/address-manager'
|
|
12
13
|
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
13
14
|
import type { Registrar } from '@libp2p/interface-internal/registrar'
|
|
@@ -39,6 +40,16 @@ export interface IdentifyServiceInit {
|
|
|
39
40
|
maxPushIncomingStreams?: number
|
|
40
41
|
maxPushOutgoingStreams?: number
|
|
41
42
|
maxObservedAddresses?: number
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Whether to automatically dial identify on newly opened connections (default: true)
|
|
46
|
+
*/
|
|
47
|
+
runOnConnectionOpen?: boolean
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Whether to run on connections with data or duration limits (default: true)
|
|
51
|
+
*/
|
|
52
|
+
runOnTransientConnection?: boolean
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
export interface IdentifyServiceComponents {
|
|
@@ -60,6 +71,19 @@ export const multicodecs = {
|
|
|
60
71
|
|
|
61
72
|
export const Message = { Identify }
|
|
62
73
|
|
|
63
|
-
export
|
|
74
|
+
export interface IdentifyService {
|
|
75
|
+
/**
|
|
76
|
+
* due to the default limits on inbound/outbound streams for this protocol,
|
|
77
|
+
* invoking this method when runOnConnectionOpen is true can lead to unpredictable results
|
|
78
|
+
* as streams may be closed by the local or the remote node.
|
|
79
|
+
* Please use with caution. If you find yourself needing to call this method to discover other peers that support your protocol,
|
|
80
|
+
* you may be better off configuring a topology to be notified instead.
|
|
81
|
+
*/
|
|
82
|
+
identify: (connection: Connection, options?: AbortOptions) => Promise<IdentifyResult>
|
|
83
|
+
|
|
84
|
+
push: () => Promise<void>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => IdentifyService {
|
|
64
88
|
return (components) => new DefaultIdentifyService(components, init)
|
|
65
89
|
}
|
package/src/libp2p.ts
CHANGED
|
@@ -30,7 +30,7 @@ import { DefaultUpgrader } from './upgrader.js'
|
|
|
30
30
|
import type { Components } from './components.js'
|
|
31
31
|
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
|
|
32
32
|
import type { Libp2pEvents, PendingDial, ServiceMap, AbortOptions } from '@libp2p/interface'
|
|
33
|
-
import type { Connection, Stream } from '@libp2p/interface/connection'
|
|
33
|
+
import type { Connection, NewStreamOptions, Stream } from '@libp2p/interface/connection'
|
|
34
34
|
import type { KeyChain } from '@libp2p/interface/keychain'
|
|
35
35
|
import type { Metrics } from '@libp2p/interface/metrics'
|
|
36
36
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
@@ -283,7 +283,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
283
283
|
return this.components.connectionManager.openConnection(peer, options)
|
|
284
284
|
}
|
|
285
285
|
|
|
286
|
-
async dialProtocol (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options:
|
|
286
|
+
async dialProtocol (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options: NewStreamOptions = {}): Promise<Stream> {
|
|
287
287
|
if (protocols == null) {
|
|
288
288
|
throw new CodeError('no protocols were provided to open a stream', codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
289
289
|
}
|
|
@@ -307,12 +307,12 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
307
307
|
return this.components.registrar.getProtocols()
|
|
308
308
|
}
|
|
309
309
|
|
|
310
|
-
async hangUp (peer: PeerId | Multiaddr): Promise<void> {
|
|
310
|
+
async hangUp (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<void> {
|
|
311
311
|
if (isMultiaddr(peer)) {
|
|
312
312
|
peer = peerIdFromString(peer.getPeerId() ?? '')
|
|
313
313
|
}
|
|
314
314
|
|
|
315
|
-
await this.components.connectionManager.closeConnections(peer)
|
|
315
|
+
await this.components.connectionManager.closeConnections(peer, options)
|
|
316
316
|
}
|
|
317
317
|
|
|
318
318
|
/**
|
package/src/ping/constants.ts
CHANGED
package/src/ping/index.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { setMaxListeners } from 'events'
|
|
2
1
|
import { randomBytes } from '@libp2p/crypto'
|
|
3
2
|
import { CodeError } from '@libp2p/interface/errors'
|
|
4
3
|
import { logger } from '@libp2p/logger'
|
|
5
4
|
import { abortableDuplex } from 'abortable-iterator'
|
|
6
|
-
import { anySignal } from 'any-signal'
|
|
7
5
|
import first from 'it-first'
|
|
8
6
|
import { pipe } from 'it-pipe'
|
|
9
7
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
@@ -27,6 +25,7 @@ export interface PingServiceInit {
|
|
|
27
25
|
protocolPrefix?: string
|
|
28
26
|
maxInboundStreams?: number
|
|
29
27
|
maxOutboundStreams?: number
|
|
28
|
+
runOnTransientConnection?: boolean
|
|
30
29
|
|
|
31
30
|
/**
|
|
32
31
|
* How long we should wait for a ping response
|
|
@@ -46,6 +45,7 @@ class DefaultPingService implements Startable, PingService {
|
|
|
46
45
|
private readonly timeout: number
|
|
47
46
|
private readonly maxInboundStreams: number
|
|
48
47
|
private readonly maxOutboundStreams: number
|
|
48
|
+
private readonly runOnTransientConnection: boolean
|
|
49
49
|
|
|
50
50
|
constructor (components: PingServiceComponents, init: PingServiceInit) {
|
|
51
51
|
this.components = components
|
|
@@ -54,12 +54,14 @@ class DefaultPingService implements Startable, PingService {
|
|
|
54
54
|
this.timeout = init.timeout ?? TIMEOUT
|
|
55
55
|
this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS
|
|
56
56
|
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
|
|
57
|
+
this.runOnTransientConnection = init.runOnTransientConnection ?? true
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
async start (): Promise<void> {
|
|
60
61
|
await this.components.registrar.handle(this.protocol, this.handleMessage, {
|
|
61
62
|
maxInboundStreams: this.maxInboundStreams,
|
|
62
|
-
maxOutboundStreams: this.maxOutboundStreams
|
|
63
|
+
maxOutboundStreams: this.maxOutboundStreams,
|
|
64
|
+
runOnTransientConnection: this.runOnTransientConnection
|
|
63
65
|
})
|
|
64
66
|
this.started = true
|
|
65
67
|
}
|
|
@@ -77,11 +79,19 @@ class DefaultPingService implements Startable, PingService {
|
|
|
77
79
|
* A handler to register with Libp2p to process ping messages
|
|
78
80
|
*/
|
|
79
81
|
handleMessage (data: IncomingStreamData): void {
|
|
82
|
+
log('incoming ping from %p', data.connection.remotePeer)
|
|
83
|
+
|
|
80
84
|
const { stream } = data
|
|
85
|
+
const start = Date.now()
|
|
81
86
|
|
|
82
87
|
void pipe(stream, stream)
|
|
83
88
|
.catch(err => {
|
|
84
|
-
log.error(err)
|
|
89
|
+
log.error('incoming ping from %p failed with error', data.connection.remotePeer, err)
|
|
90
|
+
})
|
|
91
|
+
.finally(() => {
|
|
92
|
+
const ms = Date.now() - start
|
|
93
|
+
|
|
94
|
+
log('incoming ping from %p complete in %dms', data.connection.remotePeer, ms)
|
|
85
95
|
})
|
|
86
96
|
}
|
|
87
97
|
|
|
@@ -92,45 +102,53 @@ class DefaultPingService implements Startable, PingService {
|
|
|
92
102
|
* @returns {Promise<number>}
|
|
93
103
|
*/
|
|
94
104
|
async ping (peer: PeerId | Multiaddr | Multiaddr[], options: AbortOptions = {}): Promise<number> {
|
|
95
|
-
log('
|
|
105
|
+
log('pinging %p', peer)
|
|
96
106
|
|
|
97
107
|
const start = Date.now()
|
|
98
108
|
const data = randomBytes(PING_LENGTH)
|
|
99
109
|
const connection = await this.components.connectionManager.openConnection(peer, options)
|
|
100
110
|
let stream: Stream | undefined
|
|
101
111
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
try {
|
|
105
|
-
// fails on node < 15.4
|
|
106
|
-
setMaxListeners?.(Infinity, signal)
|
|
107
|
-
} catch {}
|
|
112
|
+
options.signal = options.signal ?? AbortSignal.timeout(this.timeout)
|
|
108
113
|
|
|
109
114
|
try {
|
|
110
|
-
stream = await connection.newStream(
|
|
111
|
-
|
|
115
|
+
stream = await connection.newStream(this.protocol, {
|
|
116
|
+
...options,
|
|
117
|
+
runOnTransientConnection: this.runOnTransientConnection
|
|
112
118
|
})
|
|
113
119
|
|
|
114
120
|
// make stream abortable
|
|
115
|
-
const source = abortableDuplex(stream, signal)
|
|
121
|
+
const source = abortableDuplex(stream, options.signal)
|
|
116
122
|
|
|
117
123
|
const result = await pipe(
|
|
118
124
|
[data],
|
|
119
125
|
source,
|
|
120
126
|
async (source) => first(source)
|
|
121
127
|
)
|
|
122
|
-
const end = Date.now()
|
|
123
128
|
|
|
124
|
-
|
|
125
|
-
|
|
129
|
+
const ms = Date.now() - start
|
|
130
|
+
|
|
131
|
+
if (result == null) {
|
|
132
|
+
throw new CodeError(`Did not receive a ping ack after ${ms}ms`, codes.ERR_WRONG_PING_ACK)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!uint8ArrayEquals(data, result.subarray())) {
|
|
136
|
+
throw new CodeError(`Received wrong ping ack after ${ms}ms`, codes.ERR_WRONG_PING_ACK)
|
|
126
137
|
}
|
|
127
138
|
|
|
128
|
-
|
|
139
|
+
log('ping %p complete in %dms', connection.remotePeer, ms)
|
|
140
|
+
|
|
141
|
+
return ms
|
|
142
|
+
} catch (err: any) {
|
|
143
|
+
log.error('error while pinging %p', connection.remotePeer, err)
|
|
144
|
+
|
|
145
|
+
stream?.abort(err)
|
|
146
|
+
|
|
147
|
+
throw err
|
|
129
148
|
} finally {
|
|
130
149
|
if (stream != null) {
|
|
131
|
-
stream.close()
|
|
150
|
+
await stream.close()
|
|
132
151
|
}
|
|
133
|
-
signal.clear()
|
|
134
152
|
}
|
|
135
153
|
}
|
|
136
154
|
}
|
package/src/transport-manager.ts
CHANGED
|
@@ -177,6 +177,10 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
177
177
|
* Starts listeners for each listen Multiaddr
|
|
178
178
|
*/
|
|
179
179
|
async listen (addrs: Multiaddr[]): Promise<void> {
|
|
180
|
+
if (!this.isStarted()) {
|
|
181
|
+
throw new CodeError('Not started', codes.ERR_NODE_NOT_STARTED)
|
|
182
|
+
}
|
|
183
|
+
|
|
180
184
|
if (addrs == null || addrs.length === 0) {
|
|
181
185
|
log('no addresses were provided for listening, this node is dial only')
|
|
182
186
|
return
|