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