libp2p 0.46.19 → 0.46.20
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 +22 -22
- package/dist/src/components.d.ts +3 -1
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +4 -0
- package/dist/src/components.js.map +1 -1
- package/dist/src/connection/index.d.ts +3 -1
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +17 -11
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +3 -1
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +20 -20
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts +3 -1
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +8 -8
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +3 -1
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +26 -23
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +3 -1
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +21 -18
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +2 -1
- package/dist/src/connection-manager/utils.d.ts.map +1 -1
- package/dist/src/connection-manager/utils.js +2 -4
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/identify/consts.d.ts +1 -1
- package/dist/src/identify/identify.d.ts.map +1 -1
- package/dist/src/identify/identify.js +28 -21
- package/dist/src/identify/identify.js.map +1 -1
- package/dist/src/identify/index.d.ts +2 -1
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +21 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +2 -1
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +20 -16
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +9 -3
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/upgrader.d.ts +3 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +37 -31
- 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 +21 -21
- package/src/components.ts +8 -1
- package/src/connection/index.ts +20 -13
- package/src/connection-manager/auto-dial.ts +22 -22
- package/src/connection-manager/connection-pruner.ts +10 -10
- package/src/connection-manager/dial-queue.ts +28 -25
- package/src/connection-manager/index.ts +23 -20
- package/src/connection-manager/utils.ts +5 -7
- package/src/identify/identify.ts +30 -23
- package/src/identify/index.ts +2 -1
- package/src/index.ts +22 -1
- package/src/libp2p.ts +21 -18
- package/src/ping/index.ts +10 -3
- package/src/upgrader.ts +39 -32
- package/src/version.ts +1 -1
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { setMaxListeners } from '@libp2p/interface/events'
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
3
2
|
import { type AbortOptions, multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
|
4
3
|
import { type ClearableSignal, anySignal } from 'any-signal'
|
|
5
|
-
|
|
6
|
-
const log = logger('libp2p:connection-manager:utils')
|
|
4
|
+
import type { LoggerOptions } from '@libp2p/interface'
|
|
7
5
|
|
|
8
6
|
/**
|
|
9
7
|
* Resolve multiaddr recursively
|
|
10
8
|
*/
|
|
11
|
-
export async function resolveMultiaddrs (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
|
|
9
|
+
export async function resolveMultiaddrs (ma: Multiaddr, options: AbortOptions & LoggerOptions): Promise<Multiaddr[]> {
|
|
12
10
|
// TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place
|
|
13
11
|
// Now only supporting resolve for dnsaddr
|
|
14
12
|
const resolvableProto = ma.protoNames().includes('dnsaddr')
|
|
@@ -31,7 +29,7 @@ export async function resolveMultiaddrs (ma: Multiaddr, options: AbortOptions):
|
|
|
31
29
|
return array
|
|
32
30
|
}, ([]))
|
|
33
31
|
|
|
34
|
-
log('resolved %s to', ma, output.map(ma => ma.toString()))
|
|
32
|
+
options.log('resolved %s to', ma, output.map(ma => ma.toString()))
|
|
35
33
|
|
|
36
34
|
return output
|
|
37
35
|
}
|
|
@@ -39,13 +37,13 @@ export async function resolveMultiaddrs (ma: Multiaddr, options: AbortOptions):
|
|
|
39
37
|
/**
|
|
40
38
|
* Resolve a given multiaddr. If this fails, an empty array will be returned
|
|
41
39
|
*/
|
|
42
|
-
async function resolveRecord (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
|
|
40
|
+
async function resolveRecord (ma: Multiaddr, options: AbortOptions & LoggerOptions): Promise<Multiaddr[]> {
|
|
43
41
|
try {
|
|
44
42
|
ma = multiaddr(ma.toString()) // Use current multiaddr module
|
|
45
43
|
const multiaddrs = await ma.resolve(options)
|
|
46
44
|
return multiaddrs
|
|
47
45
|
} catch (err) {
|
|
48
|
-
log.error(`multiaddr ${ma.toString()} could not be resolved`, err)
|
|
46
|
+
options.log.error(`multiaddr ${ma.toString()} could not be resolved`, err)
|
|
49
47
|
return []
|
|
50
48
|
}
|
|
51
49
|
}
|
package/src/identify/identify.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { setMaxListeners } from '@libp2p/interface/events'
|
|
3
|
-
import { logger } from '@libp2p/logger'
|
|
4
3
|
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
5
4
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
6
5
|
import { type Multiaddr, multiaddr, protocols } from '@multiformats/multiaddr'
|
|
@@ -20,7 +19,7 @@ import {
|
|
|
20
19
|
} from './consts.js'
|
|
21
20
|
import { Identify } from './pb/message.js'
|
|
22
21
|
import type { IdentifyService, IdentifyServiceComponents, IdentifyServiceInit } from './index.js'
|
|
23
|
-
import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions } from '@libp2p/interface'
|
|
22
|
+
import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions, Logger } from '@libp2p/interface'
|
|
24
23
|
import type { Connection, Stream } from '@libp2p/interface/connection'
|
|
25
24
|
import type { TypedEventTarget } from '@libp2p/interface/events'
|
|
26
25
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
@@ -30,8 +29,6 @@ import type { AddressManager } from '@libp2p/interface-internal/address-manager'
|
|
|
30
29
|
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
31
30
|
import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/registrar'
|
|
32
31
|
|
|
33
|
-
const log = logger('libp2p:identify')
|
|
34
|
-
|
|
35
32
|
// https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L52
|
|
36
33
|
const MAX_IDENTIFY_MESSAGE_SIZE = 1024 * 8
|
|
37
34
|
|
|
@@ -73,6 +70,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
73
70
|
private readonly maxObservedAddresses: number
|
|
74
71
|
private readonly events: TypedEventTarget<Libp2pEvents>
|
|
75
72
|
private readonly runOnTransientConnection: boolean
|
|
73
|
+
readonly #log: Logger
|
|
76
74
|
|
|
77
75
|
constructor (components: IdentifyServiceComponents, init: IdentifyServiceInit) {
|
|
78
76
|
this.started = false
|
|
@@ -82,6 +80,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
82
80
|
this.addressManager = components.addressManager
|
|
83
81
|
this.connectionManager = components.connectionManager
|
|
84
82
|
this.events = components.events
|
|
83
|
+
this.#log = components.logger.forComponent('libp2p:identify')
|
|
85
84
|
|
|
86
85
|
this.identifyProtocolStr = `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`
|
|
87
86
|
this.identifyPushProtocolStr = `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
|
|
@@ -104,13 +103,13 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
104
103
|
// When a new connection happens, trigger identify
|
|
105
104
|
components.events.addEventListener('connection:open', (evt) => {
|
|
106
105
|
const connection = evt.detail
|
|
107
|
-
this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
|
|
106
|
+
this.identify(connection).catch(err => { this.#log.error('error during identify trigged by connection:open', err) })
|
|
108
107
|
})
|
|
109
108
|
}
|
|
110
109
|
|
|
111
110
|
// When self peer record changes, trigger identify-push
|
|
112
111
|
components.events.addEventListener('self:peer:update', (evt) => {
|
|
113
|
-
void this.push().catch(err => { log.error(err) })
|
|
112
|
+
void this.push().catch(err => { this.#log.error(err) })
|
|
114
113
|
})
|
|
115
114
|
|
|
116
115
|
// Append user agent version to default AGENT_VERSION depending on the environment
|
|
@@ -141,7 +140,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
141
140
|
|
|
142
141
|
await this.registrar.handle(this.identifyProtocolStr, (data) => {
|
|
143
142
|
void this._handleIdentify(data).catch(err => {
|
|
144
|
-
log.error(err)
|
|
143
|
+
this.#log.error(err)
|
|
145
144
|
})
|
|
146
145
|
}, {
|
|
147
146
|
maxInboundStreams: this.maxInboundStreams,
|
|
@@ -150,7 +149,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
150
149
|
})
|
|
151
150
|
await this.registrar.handle(this.identifyPushProtocolStr, (data) => {
|
|
152
151
|
void this._handlePush(data).catch(err => {
|
|
153
|
-
log.error(err)
|
|
152
|
+
this.#log.error(err)
|
|
154
153
|
})
|
|
155
154
|
}, {
|
|
156
155
|
maxInboundStreams: this.maxPushIncomingStreams,
|
|
@@ -215,7 +214,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
215
214
|
})
|
|
216
215
|
} catch (err: any) {
|
|
217
216
|
// Just log errors
|
|
218
|
-
log.error('could not push identify update to peer', err)
|
|
217
|
+
this.#log.error('could not push identify update to peer', err)
|
|
219
218
|
stream?.abort(err)
|
|
220
219
|
}
|
|
221
220
|
})
|
|
@@ -258,7 +257,15 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
258
257
|
async _identify (connection: Connection, options: AbortOptions = {}): Promise<Identify> {
|
|
259
258
|
let stream: Stream | undefined
|
|
260
259
|
|
|
261
|
-
|
|
260
|
+
if (options.signal == null) {
|
|
261
|
+
const signal = AbortSignal.timeout(this.timeout)
|
|
262
|
+
setMaxListeners(Infinity, signal)
|
|
263
|
+
|
|
264
|
+
options = {
|
|
265
|
+
...options,
|
|
266
|
+
signal
|
|
267
|
+
}
|
|
268
|
+
}
|
|
262
269
|
|
|
263
270
|
try {
|
|
264
271
|
stream = await connection.newStream([this.identifyProtocolStr], {
|
|
@@ -276,7 +283,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
276
283
|
|
|
277
284
|
return message
|
|
278
285
|
} catch (err: any) {
|
|
279
|
-
log.error('error while reading identify message', err)
|
|
286
|
+
this.#log.error('error while reading identify message', err)
|
|
280
287
|
stream?.abort(err)
|
|
281
288
|
throw err
|
|
282
289
|
}
|
|
@@ -307,12 +314,12 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
307
314
|
// Get the observedAddr if there is one
|
|
308
315
|
const cleanObservedAddr = getCleanMultiaddr(observedAddr)
|
|
309
316
|
|
|
310
|
-
log('identify completed for peer %p and protocols %o', id, protocols)
|
|
311
|
-
log('our observed address is %a', cleanObservedAddr)
|
|
317
|
+
this.#log('identify completed for peer %p and protocols %o', id, protocols)
|
|
318
|
+
this.#log('our observed address is %a', cleanObservedAddr)
|
|
312
319
|
|
|
313
320
|
if (cleanObservedAddr != null &&
|
|
314
321
|
this.addressManager.getObservedAddrs().length < (this.maxObservedAddresses ?? Infinity)) {
|
|
315
|
-
log('storing our observed address %a', cleanObservedAddr)
|
|
322
|
+
this.#log('storing our observed address %a', cleanObservedAddr)
|
|
316
323
|
this.addressManager.addObservedAddr(cleanObservedAddr)
|
|
317
324
|
}
|
|
318
325
|
|
|
@@ -370,7 +377,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
370
377
|
signal
|
|
371
378
|
})
|
|
372
379
|
} catch (err: any) {
|
|
373
|
-
log.error('could not respond to identify request', err)
|
|
380
|
+
this.#log.error('could not respond to identify request', err)
|
|
374
381
|
stream.abort(err)
|
|
375
382
|
}
|
|
376
383
|
}
|
|
@@ -399,16 +406,16 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
399
406
|
|
|
400
407
|
await this.#consumeIdentifyMessage(connection, message)
|
|
401
408
|
} catch (err: any) {
|
|
402
|
-
log.error('received invalid message', err)
|
|
409
|
+
this.#log.error('received invalid message', err)
|
|
403
410
|
stream.abort(err)
|
|
404
411
|
return
|
|
405
412
|
}
|
|
406
413
|
|
|
407
|
-
log('handled push from %p', connection.remotePeer)
|
|
414
|
+
this.#log('handled push from %p', connection.remotePeer)
|
|
408
415
|
}
|
|
409
416
|
|
|
410
417
|
async #consumeIdentifyMessage (connection: Connection, message: Identify): Promise<IdentifyResult> {
|
|
411
|
-
log('received identify from %p', connection.remotePeer)
|
|
418
|
+
this.#log('received identify from %p', connection.remotePeer)
|
|
412
419
|
|
|
413
420
|
if (message == null) {
|
|
414
421
|
throw new CodeError('message was null or undefined', 'ERR_INVALID_MESSAGE')
|
|
@@ -441,7 +448,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
441
448
|
|
|
442
449
|
// if the peer record has been sent, prefer the addresses in the record as they are signed by the remote peer
|
|
443
450
|
if (message.signedPeerRecord != null) {
|
|
444
|
-
log('received signedPeerRecord in push from %p', connection.remotePeer)
|
|
451
|
+
this.#log('received signedPeerRecord in push from %p', connection.remotePeer)
|
|
445
452
|
|
|
446
453
|
let peerRecordEnvelope = message.signedPeerRecord
|
|
447
454
|
const envelope = await RecordEnvelope.openAndCertify(peerRecordEnvelope, PeerRecord.DOMAIN)
|
|
@@ -478,7 +485,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
478
485
|
|
|
479
486
|
// ensure seq is greater than, or equal to, the last received
|
|
480
487
|
if (storedRecord.seqNumber >= peerRecord.seqNumber) {
|
|
481
|
-
log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber)
|
|
488
|
+
this.#log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber)
|
|
482
489
|
peerRecord = storedRecord
|
|
483
490
|
peerRecordEnvelope = existingPeer.peerRecordEnvelope
|
|
484
491
|
}
|
|
@@ -499,10 +506,10 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
499
506
|
addresses: peerRecord.multiaddrs
|
|
500
507
|
}
|
|
501
508
|
} else {
|
|
502
|
-
log('%p did not send a signed peer record', connection.remotePeer)
|
|
509
|
+
this.#log('%p did not send a signed peer record', connection.remotePeer)
|
|
503
510
|
}
|
|
504
511
|
|
|
505
|
-
log('patching %p with', connection.remotePeer, peer)
|
|
512
|
+
this.#log('patching %p with', connection.remotePeer, peer)
|
|
506
513
|
await this.peerStore.patch(connection.remotePeer, peer)
|
|
507
514
|
|
|
508
515
|
if (message.agentVersion != null || message.protocolVersion != null) {
|
|
@@ -516,7 +523,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
516
523
|
metadata.ProtocolVersion = uint8ArrayFromString(message.protocolVersion)
|
|
517
524
|
}
|
|
518
525
|
|
|
519
|
-
log('merging %p metadata', connection.remotePeer, metadata)
|
|
526
|
+
this.#log('merging %p metadata', connection.remotePeer, metadata)
|
|
520
527
|
await this.peerStore.merge(connection.remotePeer, {
|
|
521
528
|
metadata
|
|
522
529
|
})
|
package/src/identify/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
} from './consts.js'
|
|
5
5
|
import { DefaultIdentifyService } from './identify.js'
|
|
6
6
|
import { Identify } from './pb/message.js'
|
|
7
|
-
import type { AbortOptions, IdentifyResult, Libp2pEvents } from '@libp2p/interface'
|
|
7
|
+
import type { AbortOptions, IdentifyResult, Libp2pEvents, ComponentLogger } from '@libp2p/interface'
|
|
8
8
|
import type { TypedEventTarget } from '@libp2p/interface/events'
|
|
9
9
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
10
10
|
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
@@ -59,6 +59,7 @@ export interface IdentifyServiceComponents {
|
|
|
59
59
|
registrar: Registrar
|
|
60
60
|
addressManager: AddressManager
|
|
61
61
|
events: TypedEventTarget<Libp2pEvents>
|
|
62
|
+
logger: ComponentLogger
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
/**
|
package/src/index.ts
CHANGED
|
@@ -19,7 +19,7 @@ import type { AddressManagerInit } from './address-manager/index.js'
|
|
|
19
19
|
import type { Components } from './components.js'
|
|
20
20
|
import type { ConnectionManagerInit } from './connection-manager/index.js'
|
|
21
21
|
import type { TransportManagerInit } from './transport-manager.js'
|
|
22
|
-
import type { Libp2p, ServiceMap, RecursivePartial } from '@libp2p/interface'
|
|
22
|
+
import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger } from '@libp2p/interface'
|
|
23
23
|
import type { ConnectionProtector } from '@libp2p/interface/connection'
|
|
24
24
|
import type { ConnectionEncrypter } from '@libp2p/interface/connection-encrypter'
|
|
25
25
|
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
|
|
@@ -108,6 +108,27 @@ export interface Libp2pInit<T extends ServiceMap = { x: Record<string, unknown>
|
|
|
108
108
|
* Arbitrary libp2p modules
|
|
109
109
|
*/
|
|
110
110
|
services: ServiceFactoryMap<T>
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* An optional logging implementation that can be used to write runtime logs.
|
|
114
|
+
*
|
|
115
|
+
* Set the `DEBUG` env var or the `debug` key on LocalStorage to see logs.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
*
|
|
119
|
+
* Node.js:
|
|
120
|
+
*
|
|
121
|
+
* ```console
|
|
122
|
+
* $ DEBUG="*libp2p:*" node myscript.js
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* Browsers:
|
|
126
|
+
*
|
|
127
|
+
* ```javascript
|
|
128
|
+
* localStorage.setItem('debug', '*libp2p:*')
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
logger?: ComponentLogger
|
|
111
132
|
}
|
|
112
133
|
|
|
113
134
|
export type { Libp2p }
|
package/src/libp2p.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { TypedEventEmitter, CustomEvent, setMaxListeners } from '@libp2p/interfa
|
|
|
5
5
|
import { peerDiscovery } from '@libp2p/interface/peer-discovery'
|
|
6
6
|
import { type PeerRouting, peerRouting } from '@libp2p/interface/peer-routing'
|
|
7
7
|
import { DefaultKeyChain } from '@libp2p/keychain'
|
|
8
|
-
import {
|
|
8
|
+
import { defaultLogger } from '@libp2p/logger'
|
|
9
9
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
10
10
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
11
11
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
@@ -28,7 +28,7 @@ import { DefaultTransportManager } from './transport-manager.js'
|
|
|
28
28
|
import { DefaultUpgrader } from './upgrader.js'
|
|
29
29
|
import type { Components } from './components.js'
|
|
30
30
|
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
|
|
31
|
-
import type { Libp2pEvents, PendingDial, ServiceMap, AbortOptions } from '@libp2p/interface'
|
|
31
|
+
import type { Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
|
|
32
32
|
import type { Connection, NewStreamOptions, Stream } from '@libp2p/interface/connection'
|
|
33
33
|
import type { KeyChain } from '@libp2p/interface/keychain'
|
|
34
34
|
import type { Metrics } from '@libp2p/interface/metrics'
|
|
@@ -39,8 +39,6 @@ import type { Topology } from '@libp2p/interface/topology'
|
|
|
39
39
|
import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal/registrar'
|
|
40
40
|
import type { Datastore } from 'interface-datastore'
|
|
41
41
|
|
|
42
|
-
const log = logger('libp2p')
|
|
43
|
-
|
|
44
42
|
export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends TypedEventEmitter<Libp2pEvents> implements Libp2p<T> {
|
|
45
43
|
public peerId: PeerId
|
|
46
44
|
public peerStore: PeerStore
|
|
@@ -49,9 +47,11 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
49
47
|
public keychain: KeyChain
|
|
50
48
|
public metrics?: Metrics
|
|
51
49
|
public services: T
|
|
50
|
+
public logger: ComponentLogger
|
|
52
51
|
|
|
53
52
|
public components: Components
|
|
54
53
|
#started: boolean
|
|
54
|
+
readonly #log: Logger
|
|
55
55
|
|
|
56
56
|
constructor (init: Libp2pInit<T>) {
|
|
57
57
|
super()
|
|
@@ -74,10 +74,13 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
74
74
|
|
|
75
75
|
this.#started = false
|
|
76
76
|
this.peerId = init.peerId
|
|
77
|
+
this.logger = init.logger ?? defaultLogger()
|
|
78
|
+
this.#log = this.logger.forComponent('libp2p')
|
|
77
79
|
// @ts-expect-error {} may not be of type T
|
|
78
80
|
this.services = {}
|
|
79
81
|
const components = this.components = defaultComponents({
|
|
80
82
|
peerId: init.peerId,
|
|
83
|
+
logger: this.logger,
|
|
81
84
|
events,
|
|
82
85
|
datastore: init.datastore ?? new MemoryDatastore(),
|
|
83
86
|
connectionGater: connectionGater(init.connectionGater)
|
|
@@ -170,7 +173,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
170
173
|
const service: any = createService(this.components)
|
|
171
174
|
|
|
172
175
|
if (service == null) {
|
|
173
|
-
log.error('service factory %s returned null or undefined instance', name)
|
|
176
|
+
this.#log.error('service factory %s returned null or undefined instance', name)
|
|
174
177
|
continue
|
|
175
178
|
}
|
|
176
179
|
|
|
@@ -178,17 +181,17 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
178
181
|
this.configureComponent(name, service)
|
|
179
182
|
|
|
180
183
|
if (service[contentRouting] != null) {
|
|
181
|
-
log('registering service %s for content routing', name)
|
|
184
|
+
this.#log('registering service %s for content routing', name)
|
|
182
185
|
contentRouters.push(service[contentRouting])
|
|
183
186
|
}
|
|
184
187
|
|
|
185
188
|
if (service[peerRouting] != null) {
|
|
186
|
-
log('registering service %s for peer routing', name)
|
|
189
|
+
this.#log('registering service %s for peer routing', name)
|
|
187
190
|
peerRouters.push(service[peerRouting])
|
|
188
191
|
}
|
|
189
192
|
|
|
190
193
|
if (service[peerDiscovery] != null) {
|
|
191
|
-
log('registering service %s for peer discovery', name)
|
|
194
|
+
this.#log('registering service %s for peer discovery', name)
|
|
192
195
|
service[peerDiscovery].addEventListener('peer', (evt: CustomEvent<PeerInfo>) => {
|
|
193
196
|
this.#onDiscoveryPeer(evt)
|
|
194
197
|
})
|
|
@@ -199,7 +202,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
199
202
|
|
|
200
203
|
private configureComponent <T> (name: string, component: T): T {
|
|
201
204
|
if (component == null) {
|
|
202
|
-
log.error('component %s was null or undefined', name)
|
|
205
|
+
this.#log.error('component %s was null or undefined', name)
|
|
203
206
|
}
|
|
204
207
|
|
|
205
208
|
this.components[name] = component
|
|
@@ -217,12 +220,12 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
217
220
|
|
|
218
221
|
this.#started = true
|
|
219
222
|
|
|
220
|
-
log('libp2p is starting')
|
|
223
|
+
this.#log('libp2p is starting')
|
|
221
224
|
|
|
222
225
|
const keys = await this.keychain.listKeys()
|
|
223
226
|
|
|
224
227
|
if (keys.find(key => key.name === 'self') == null) {
|
|
225
|
-
log('importing self key into keychain')
|
|
228
|
+
this.#log('importing self key into keychain')
|
|
226
229
|
await this.keychain.importPeer('self', this.components.peerId)
|
|
227
230
|
}
|
|
228
231
|
|
|
@@ -232,9 +235,9 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
232
235
|
await this.components.afterStart?.()
|
|
233
236
|
|
|
234
237
|
this.safeDispatchEvent('start', { detail: this })
|
|
235
|
-
log('libp2p has started')
|
|
238
|
+
this.#log('libp2p has started')
|
|
236
239
|
} catch (err: any) {
|
|
237
|
-
log.error('An error occurred starting libp2p', err)
|
|
240
|
+
this.#log.error('An error occurred starting libp2p', err)
|
|
238
241
|
await this.stop()
|
|
239
242
|
throw err
|
|
240
243
|
}
|
|
@@ -248,7 +251,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
248
251
|
return
|
|
249
252
|
}
|
|
250
253
|
|
|
251
|
-
log('libp2p is stopping')
|
|
254
|
+
this.#log('libp2p is stopping')
|
|
252
255
|
|
|
253
256
|
this.#started = false
|
|
254
257
|
|
|
@@ -257,7 +260,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
257
260
|
await this.components.afterStop?.()
|
|
258
261
|
|
|
259
262
|
this.safeDispatchEvent('stop', { detail: this })
|
|
260
|
-
log('libp2p has stopped')
|
|
263
|
+
this.#log('libp2p has stopped')
|
|
261
264
|
}
|
|
262
265
|
|
|
263
266
|
isStarted (): boolean {
|
|
@@ -322,7 +325,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
322
325
|
* Get the public key for the given peer id
|
|
323
326
|
*/
|
|
324
327
|
async getPublicKey (peer: PeerId, options: AbortOptions = {}): Promise<Uint8Array> {
|
|
325
|
-
log('getPublicKey %p', peer)
|
|
328
|
+
this.#log('getPublicKey %p', peer)
|
|
326
329
|
|
|
327
330
|
if (peer.publicKey != null) {
|
|
328
331
|
return peer.publicKey
|
|
@@ -391,7 +394,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
391
394
|
const { detail: peer } = evt
|
|
392
395
|
|
|
393
396
|
if (peer.id.toString() === this.peerId.toString()) {
|
|
394
|
-
log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
|
397
|
+
this.#log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
|
395
398
|
return
|
|
396
399
|
}
|
|
397
400
|
|
|
@@ -399,7 +402,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
399
402
|
multiaddrs: peer.multiaddrs,
|
|
400
403
|
protocols: peer.protocols
|
|
401
404
|
})
|
|
402
|
-
.catch(err => { log.error(err) })
|
|
405
|
+
.catch(err => { this.#log.error(err) })
|
|
403
406
|
}
|
|
404
407
|
}
|
|
405
408
|
|
package/src/ping/index.ts
CHANGED
|
@@ -109,7 +109,14 @@ class DefaultPingService implements Startable, PingService {
|
|
|
109
109
|
let stream: Stream | undefined
|
|
110
110
|
let onAbort = (): void => {}
|
|
111
111
|
|
|
112
|
-
|
|
112
|
+
if (options.signal == null) {
|
|
113
|
+
const signal = AbortSignal.timeout(this.timeout)
|
|
114
|
+
|
|
115
|
+
options = {
|
|
116
|
+
...options,
|
|
117
|
+
signal
|
|
118
|
+
}
|
|
119
|
+
}
|
|
113
120
|
|
|
114
121
|
try {
|
|
115
122
|
stream = await connection.newStream(this.protocol, {
|
|
@@ -122,7 +129,7 @@ class DefaultPingService implements Startable, PingService {
|
|
|
122
129
|
}
|
|
123
130
|
|
|
124
131
|
// make stream abortable
|
|
125
|
-
options.signal
|
|
132
|
+
options.signal?.addEventListener('abort', onAbort, { once: true })
|
|
126
133
|
|
|
127
134
|
const result = await pipe(
|
|
128
135
|
[data],
|
|
@@ -150,7 +157,7 @@ class DefaultPingService implements Startable, PingService {
|
|
|
150
157
|
|
|
151
158
|
throw err
|
|
152
159
|
} finally {
|
|
153
|
-
options.signal
|
|
160
|
+
options.signal?.removeEventListener('abort', onAbort)
|
|
154
161
|
if (stream != null) {
|
|
155
162
|
await stream.close()
|
|
156
163
|
}
|