libp2p 0.37.3-e6f646e → 0.38.0-0e7096d

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.
Files changed (125) hide show
  1. package/README.md +0 -2
  2. package/dist/src/circuit/circuit/hop.d.ts +5 -3
  3. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  4. package/dist/src/circuit/circuit/hop.js +8 -4
  5. package/dist/src/circuit/circuit/hop.js.map +1 -1
  6. package/dist/src/circuit/circuit/stop.d.ts +5 -3
  7. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  8. package/dist/src/circuit/circuit/stop.js +4 -2
  9. package/dist/src/circuit/circuit/stop.js.map +1 -1
  10. package/dist/src/circuit/circuit/stream-handler.d.ts +2 -1
  11. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  12. package/dist/src/circuit/circuit/stream-handler.js +2 -2
  13. package/dist/src/circuit/circuit/stream-handler.js.map +1 -1
  14. package/dist/src/circuit/index.d.ts +2 -10
  15. package/dist/src/circuit/index.d.ts.map +1 -1
  16. package/dist/src/circuit/index.js.map +1 -1
  17. package/dist/src/circuit/pb/index.d.ts +5 -4
  18. package/dist/src/circuit/pb/index.d.ts.map +1 -1
  19. package/dist/src/circuit/pb/index.js +107 -11
  20. package/dist/src/circuit/pb/index.js.map +1 -1
  21. package/dist/src/circuit/transport.d.ts +3 -0
  22. package/dist/src/circuit/transport.d.ts.map +1 -1
  23. package/dist/src/circuit/transport.js +79 -57
  24. package/dist/src/circuit/transport.js.map +1 -1
  25. package/dist/src/config.d.ts.map +1 -1
  26. package/dist/src/config.js +19 -4
  27. package/dist/src/config.js.map +1 -1
  28. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -1
  29. package/dist/src/connection-manager/dialer/auto-dialer.js +6 -0
  30. package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -1
  31. package/dist/src/connection-manager/dialer/index.d.ts +4 -4
  32. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  33. package/dist/src/connection-manager/dialer/index.js +10 -10
  34. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  35. package/dist/src/connection-manager/index.d.ts +8 -1
  36. package/dist/src/connection-manager/index.d.ts.map +1 -1
  37. package/dist/src/connection-manager/index.js +42 -16
  38. package/dist/src/connection-manager/index.js.map +1 -1
  39. package/dist/src/constants.d.ts +4 -0
  40. package/dist/src/constants.d.ts.map +1 -1
  41. package/dist/src/constants.js +4 -0
  42. package/dist/src/constants.js.map +1 -1
  43. package/dist/src/fetch/index.d.ts +4 -0
  44. package/dist/src/fetch/index.d.ts.map +1 -1
  45. package/dist/src/fetch/index.js +25 -6
  46. package/dist/src/fetch/index.js.map +1 -1
  47. package/dist/src/fetch/pb/proto.d.ts +4 -3
  48. package/dist/src/fetch/pb/proto.d.ts.map +1 -1
  49. package/dist/src/fetch/pb/proto.js +88 -8
  50. package/dist/src/fetch/pb/proto.js.map +1 -1
  51. package/dist/src/identify/index.d.ts +1 -1
  52. package/dist/src/identify/index.d.ts.map +1 -1
  53. package/dist/src/identify/index.js +35 -12
  54. package/dist/src/identify/index.js.map +1 -1
  55. package/dist/src/identify/pb/message.d.ts +2 -1
  56. package/dist/src/identify/pb/message.d.ts.map +1 -1
  57. package/dist/src/identify/pb/message.js +94 -10
  58. package/dist/src/identify/pb/message.js.map +1 -1
  59. package/dist/src/index.d.ts +1 -5
  60. package/dist/src/index.d.ts.map +1 -1
  61. package/dist/src/index.js.map +1 -1
  62. package/dist/src/insecure/index.d.ts.map +1 -1
  63. package/dist/src/insecure/index.js +7 -3
  64. package/dist/src/insecure/index.js.map +1 -1
  65. package/dist/src/insecure/pb/proto.d.ts +4 -3
  66. package/dist/src/insecure/pb/proto.d.ts.map +1 -1
  67. package/dist/src/insecure/pb/proto.js +89 -9
  68. package/dist/src/insecure/pb/proto.js.map +1 -1
  69. package/dist/src/keychain/index.d.ts +6 -1
  70. package/dist/src/keychain/index.d.ts.map +1 -1
  71. package/dist/src/keychain/index.js +19 -2
  72. package/dist/src/keychain/index.js.map +1 -1
  73. package/dist/src/libp2p.d.ts +0 -5
  74. package/dist/src/libp2p.d.ts.map +1 -1
  75. package/dist/src/libp2p.js +3 -17
  76. package/dist/src/libp2p.js.map +1 -1
  77. package/dist/src/metrics/index.d.ts +1 -2
  78. package/dist/src/metrics/index.d.ts.map +1 -1
  79. package/dist/src/metrics/index.js +3 -4
  80. package/dist/src/metrics/index.js.map +1 -1
  81. package/dist/src/ping/index.d.ts +4 -0
  82. package/dist/src/ping/index.d.ts.map +1 -1
  83. package/dist/src/ping/index.js +27 -8
  84. package/dist/src/ping/index.js.map +1 -1
  85. package/dist/src/pnet/index.d.ts.map +1 -1
  86. package/dist/src/pnet/index.js +2 -1
  87. package/dist/src/pnet/index.js.map +1 -1
  88. package/dist/src/registrar.d.ts +2 -2
  89. package/dist/src/registrar.d.ts.map +1 -1
  90. package/dist/src/registrar.js +2 -2
  91. package/dist/src/registrar.js.map +1 -1
  92. package/dist/src/upgrader.d.ts +6 -0
  93. package/dist/src/upgrader.d.ts.map +1 -1
  94. package/dist/src/upgrader.js +121 -75
  95. package/dist/src/upgrader.js.map +1 -1
  96. package/dist/src/version.d.ts.map +1 -1
  97. package/dist/src/version.js.map +1 -1
  98. package/package.json +48 -47
  99. package/src/circuit/README.md +2 -2
  100. package/src/circuit/circuit/hop.ts +18 -9
  101. package/src/circuit/circuit/stop.ts +9 -4
  102. package/src/circuit/circuit/stream-handler.ts +5 -4
  103. package/src/circuit/index.ts +2 -11
  104. package/src/circuit/pb/index.ts +133 -15
  105. package/src/circuit/transport.ts +87 -58
  106. package/src/config.ts +19 -4
  107. package/src/connection-manager/dialer/auto-dialer.ts +6 -0
  108. package/src/connection-manager/dialer/index.ts +10 -10
  109. package/src/connection-manager/index.ts +54 -19
  110. package/src/constants.ts +5 -0
  111. package/src/fetch/index.ts +34 -8
  112. package/src/fetch/pb/proto.ts +109 -11
  113. package/src/identify/index.ts +41 -19
  114. package/src/identify/pb/message.ts +111 -11
  115. package/src/index.ts +1 -6
  116. package/src/insecure/index.ts +8 -3
  117. package/src/insecure/pb/proto.ts +111 -12
  118. package/src/keychain/index.ts +27 -3
  119. package/src/libp2p.ts +3 -18
  120. package/src/metrics/index.ts +4 -7
  121. package/src/ping/index.ts +37 -11
  122. package/src/pnet/index.ts +2 -0
  123. package/src/registrar.ts +2 -2
  124. package/src/upgrader.ts +138 -78
  125. package/src/version.ts +1 -2
@@ -1,7 +1,8 @@
1
1
  /* eslint-disable import/export */
2
2
  /* eslint-disable @typescript-eslint/no-namespace */
3
3
 
4
- import { encodeMessage, decodeMessage, message, string, bytes } from 'protons-runtime'
4
+ import { encodeMessage, decodeMessage, message } from 'protons-runtime'
5
+ import type { Uint8ArrayList } from 'uint8arraylist'
5
6
  import type { Codec } from 'protons-runtime'
6
7
 
7
8
  export interface Identify {
@@ -15,23 +16,122 @@ export interface Identify {
15
16
  }
16
17
 
17
18
  export namespace Identify {
19
+ let _codec: Codec<Identify>
20
+
18
21
  export const codec = (): Codec<Identify> => {
19
- return message<Identify>({
20
- 5: { name: 'protocolVersion', codec: string, optional: true },
21
- 6: { name: 'agentVersion', codec: string, optional: true },
22
- 1: { name: 'publicKey', codec: bytes, optional: true },
23
- 2: { name: 'listenAddrs', codec: bytes, repeats: true },
24
- 4: { name: 'observedAddr', codec: bytes, optional: true },
25
- 3: { name: 'protocols', codec: string, repeats: true },
26
- 8: { name: 'signedPeerRecord', codec: bytes, optional: true }
27
- })
22
+ if (_codec == null) {
23
+ _codec = message<Identify>((obj, writer, opts = {}) => {
24
+ if (opts.lengthDelimited !== false) {
25
+ writer.fork()
26
+ }
27
+
28
+ if (obj.protocolVersion != null) {
29
+ writer.uint32(42)
30
+ writer.string(obj.protocolVersion)
31
+ }
32
+
33
+ if (obj.agentVersion != null) {
34
+ writer.uint32(50)
35
+ writer.string(obj.agentVersion)
36
+ }
37
+
38
+ if (obj.publicKey != null) {
39
+ writer.uint32(10)
40
+ writer.bytes(obj.publicKey)
41
+ }
42
+
43
+ if (obj.listenAddrs != null) {
44
+ for (const value of obj.listenAddrs) {
45
+ writer.uint32(18)
46
+ writer.bytes(value)
47
+ }
48
+ } else {
49
+ throw new Error('Protocol error: required field "listenAddrs" was not found in object')
50
+ }
51
+
52
+ if (obj.observedAddr != null) {
53
+ writer.uint32(34)
54
+ writer.bytes(obj.observedAddr)
55
+ }
56
+
57
+ if (obj.protocols != null) {
58
+ for (const value of obj.protocols) {
59
+ writer.uint32(26)
60
+ writer.string(value)
61
+ }
62
+ } else {
63
+ throw new Error('Protocol error: required field "protocols" was not found in object')
64
+ }
65
+
66
+ if (obj.signedPeerRecord != null) {
67
+ writer.uint32(66)
68
+ writer.bytes(obj.signedPeerRecord)
69
+ }
70
+
71
+ if (opts.lengthDelimited !== false) {
72
+ writer.ldelim()
73
+ }
74
+ }, (reader, length) => {
75
+ const obj: any = {}
76
+
77
+ const end = length == null ? reader.len : reader.pos + length
78
+
79
+ while (reader.pos < end) {
80
+ const tag = reader.uint32()
81
+
82
+ switch (tag >>> 3) {
83
+ case 5:
84
+ obj.protocolVersion = reader.string()
85
+ break
86
+ case 6:
87
+ obj.agentVersion = reader.string()
88
+ break
89
+ case 1:
90
+ obj.publicKey = reader.bytes()
91
+ break
92
+ case 2:
93
+ obj.listenAddrs = obj.listenAddrs ?? []
94
+ obj.listenAddrs.push(reader.bytes())
95
+ break
96
+ case 4:
97
+ obj.observedAddr = reader.bytes()
98
+ break
99
+ case 3:
100
+ obj.protocols = obj.protocols ?? []
101
+ obj.protocols.push(reader.string())
102
+ break
103
+ case 8:
104
+ obj.signedPeerRecord = reader.bytes()
105
+ break
106
+ default:
107
+ reader.skipType(tag & 7)
108
+ break
109
+ }
110
+ }
111
+
112
+ obj.listenAddrs = obj.listenAddrs ?? []
113
+ obj.protocols = obj.protocols ?? []
114
+
115
+ if (obj.listenAddrs == null) {
116
+ throw new Error('Protocol error: value for required field "listenAddrs" was not found in protobuf')
117
+ }
118
+
119
+ if (obj.protocols == null) {
120
+ throw new Error('Protocol error: value for required field "protocols" was not found in protobuf')
121
+ }
122
+
123
+ return obj
124
+ })
125
+ }
126
+
127
+ return _codec
28
128
  }
29
129
 
30
130
  export const encode = (obj: Identify): Uint8Array => {
31
131
  return encodeMessage(obj, Identify.codec())
32
132
  }
33
133
 
34
- export const decode = (buf: Uint8Array): Identify => {
134
+ export const decode = (buf: Uint8Array | Uint8ArrayList): Identify => {
35
135
  return decodeMessage(buf, Identify.codec())
36
136
  }
37
137
  }
package/src/index.ts CHANGED
@@ -50,6 +50,7 @@ export interface MetricsConfig {
50
50
  export interface HopConfig {
51
51
  enabled?: boolean
52
52
  active?: boolean
53
+ timeout: number
53
54
  }
54
55
 
55
56
  export interface RelayConfig {
@@ -139,12 +140,6 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
139
140
  pubsub: PubSub
140
141
  dht: DualDHT
141
142
 
142
- /**
143
- * Load keychain keys from the datastore.
144
- * Imports the private key as 'self', if needed.
145
- */
146
- loadKeychain: () => Promise<void>
147
-
148
143
  /**
149
144
  * Get a deduplicated list of peer advertising multiaddrs by concatenating
150
145
  * the listen addresses used by transports with any configured
@@ -7,6 +7,7 @@ import type { PeerId } from '@libp2p/interface-peer-id'
7
7
  import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
8
8
  import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
9
9
  import type { Duplex } from 'it-stream-types'
10
+ import map from 'it-map'
10
11
 
11
12
  const log = logger('libp2p:plaintext')
12
13
  const PROTOCOL = '/plaintext/2.0.0'
@@ -39,7 +40,7 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
39
40
  Type: type,
40
41
  Data: localId.publicKey ?? new Uint8Array(0)
41
42
  }
42
- }).slice()
43
+ }).subarray()
43
44
  )
44
45
 
45
46
  log('write pubkey exchange to peer %p', remoteId)
@@ -47,7 +48,7 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
47
48
  // Get the Exchange message
48
49
  // @ts-expect-error needs to be generator
49
50
  const response = (await lp.decode.fromReader(shake.reader).next()).value
50
- const id = Exchange.decode(response.slice())
51
+ const id = Exchange.decode(response)
51
52
  log('read pubkey exchange from peer %p', remoteId)
52
53
 
53
54
  let peerId
@@ -81,8 +82,12 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
81
82
  log('plaintext key exchange completed successfully with peer %p', peerId)
82
83
 
83
84
  shake.rest()
85
+
84
86
  return {
85
- conn: shake.stream,
87
+ conn: {
88
+ sink: shake.stream.sink,
89
+ source: map(shake.stream.source, (buf) => buf.subarray())
90
+ },
86
91
  remotePeer: peerId,
87
92
  remoteEarlyData: new Uint8Array()
88
93
  }
@@ -1,7 +1,8 @@
1
1
  /* eslint-disable import/export */
2
2
  /* eslint-disable @typescript-eslint/no-namespace */
3
3
 
4
- import { encodeMessage, decodeMessage, message, bytes, enumeration } from 'protons-runtime'
4
+ import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
5
+ import type { Uint8ArrayList } from 'uint8arraylist'
5
6
  import type { Codec } from 'protons-runtime'
6
7
 
7
8
  export interface Exchange {
@@ -10,18 +11,61 @@ export interface Exchange {
10
11
  }
11
12
 
12
13
  export namespace Exchange {
14
+ let _codec: Codec<Exchange>
15
+
13
16
  export const codec = (): Codec<Exchange> => {
14
- return message<Exchange>({
15
- 1: { name: 'id', codec: bytes, optional: true },
16
- 2: { name: 'pubkey', codec: PublicKey.codec(), optional: true }
17
- })
17
+ if (_codec == null) {
18
+ _codec = message<Exchange>((obj, writer, opts = {}) => {
19
+ if (opts.lengthDelimited !== false) {
20
+ writer.fork()
21
+ }
22
+
23
+ if (obj.id != null) {
24
+ writer.uint32(10)
25
+ writer.bytes(obj.id)
26
+ }
27
+
28
+ if (obj.pubkey != null) {
29
+ writer.uint32(18)
30
+ PublicKey.codec().encode(obj.pubkey, writer)
31
+ }
32
+
33
+ if (opts.lengthDelimited !== false) {
34
+ writer.ldelim()
35
+ }
36
+ }, (reader, length) => {
37
+ const obj: any = {}
38
+
39
+ const end = length == null ? reader.len : reader.pos + length
40
+
41
+ while (reader.pos < end) {
42
+ const tag = reader.uint32()
43
+
44
+ switch (tag >>> 3) {
45
+ case 1:
46
+ obj.id = reader.bytes()
47
+ break
48
+ case 2:
49
+ obj.pubkey = PublicKey.codec().decode(reader, reader.uint32())
50
+ break
51
+ default:
52
+ reader.skipType(tag & 7)
53
+ break
54
+ }
55
+ }
56
+
57
+ return obj
58
+ })
59
+ }
60
+
61
+ return _codec
18
62
  }
19
63
 
20
64
  export const encode = (obj: Exchange): Uint8Array => {
21
65
  return encodeMessage(obj, Exchange.codec())
22
66
  }
23
67
 
24
- export const decode = (buf: Uint8Array): Exchange => {
68
+ export const decode = (buf: Uint8Array | Uint8ArrayList): Exchange => {
25
69
  return decodeMessage(buf, Exchange.codec())
26
70
  }
27
71
  }
@@ -42,7 +86,7 @@ enum __KeyTypeValues {
42
86
 
43
87
  export namespace KeyType {
44
88
  export const codec = () => {
45
- return enumeration<typeof KeyType>(__KeyTypeValues)
89
+ return enumeration<KeyType>(__KeyTypeValues)
46
90
  }
47
91
  }
48
92
  export interface PublicKey {
@@ -51,18 +95,73 @@ export interface PublicKey {
51
95
  }
52
96
 
53
97
  export namespace PublicKey {
98
+ let _codec: Codec<PublicKey>
99
+
54
100
  export const codec = (): Codec<PublicKey> => {
55
- return message<PublicKey>({
56
- 1: { name: 'Type', codec: KeyType.codec() },
57
- 2: { name: 'Data', codec: bytes }
58
- })
101
+ if (_codec == null) {
102
+ _codec = message<PublicKey>((obj, writer, opts = {}) => {
103
+ if (opts.lengthDelimited !== false) {
104
+ writer.fork()
105
+ }
106
+
107
+ if (obj.Type != null) {
108
+ writer.uint32(8)
109
+ KeyType.codec().encode(obj.Type, writer)
110
+ } else {
111
+ throw new Error('Protocol error: required field "Type" was not found in object')
112
+ }
113
+
114
+ if (obj.Data != null) {
115
+ writer.uint32(18)
116
+ writer.bytes(obj.Data)
117
+ } else {
118
+ throw new Error('Protocol error: required field "Data" was not found in object')
119
+ }
120
+
121
+ if (opts.lengthDelimited !== false) {
122
+ writer.ldelim()
123
+ }
124
+ }, (reader, length) => {
125
+ const obj: any = {}
126
+
127
+ const end = length == null ? reader.len : reader.pos + length
128
+
129
+ while (reader.pos < end) {
130
+ const tag = reader.uint32()
131
+
132
+ switch (tag >>> 3) {
133
+ case 1:
134
+ obj.Type = KeyType.codec().decode(reader)
135
+ break
136
+ case 2:
137
+ obj.Data = reader.bytes()
138
+ break
139
+ default:
140
+ reader.skipType(tag & 7)
141
+ break
142
+ }
143
+ }
144
+
145
+ if (obj.Type == null) {
146
+ throw new Error('Protocol error: value for required field "Type" was not found in protobuf')
147
+ }
148
+
149
+ if (obj.Data == null) {
150
+ throw new Error('Protocol error: value for required field "Data" was not found in protobuf')
151
+ }
152
+
153
+ return obj
154
+ })
155
+ }
156
+
157
+ return _codec
59
158
  }
60
159
 
61
160
  export const encode = (obj: PublicKey): Uint8Array => {
62
161
  return encodeMessage(obj, PublicKey.codec())
63
162
  }
64
163
 
65
- export const decode = (buf: Uint8Array): PublicKey => {
164
+ export const decode = (buf: Uint8Array | Uint8ArrayList): PublicKey => {
66
165
  return decodeMessage(buf, PublicKey.codec())
67
166
  }
68
167
  }
@@ -13,6 +13,7 @@ import { generateKeyPair, importKey, unmarshalPrivateKey } from '@libp2p/crypto/
13
13
  import type { PeerId } from '@libp2p/interface-peer-id'
14
14
  import type { Components } from '@libp2p/components'
15
15
  import { pbkdf2, randomBytes } from '@libp2p/crypto'
16
+ import type { Startable } from '@libp2p/interfaces/dist/src/startable'
16
17
 
17
18
  const log = logger('libp2p:keychain')
18
19
 
@@ -110,9 +111,10 @@ function DsInfoName (name: string) {
110
111
  * - '/pkcs8/*key-name*', contains the PKCS #8 for the key
111
112
  *
112
113
  */
113
- export class KeyChain {
114
+ export class KeyChain implements Startable {
114
115
  private readonly components: Components
115
116
  private init: KeyChainInit
117
+ private started: boolean
116
118
 
117
119
  /**
118
120
  * Creates a new instance of a key chain
@@ -145,6 +147,25 @@ export class KeyChain {
145
147
  : ''
146
148
 
147
149
  privates.set(this, { dek })
150
+ this.started = false
151
+ }
152
+
153
+ isStarted () {
154
+ return this.started
155
+ }
156
+
157
+ async start () {
158
+ const dsname = DsInfoName('self')
159
+
160
+ if (!(await this.components.getDatastore().has(dsname))) {
161
+ await this.importPeer('self', this.components.getPeerId())
162
+ }
163
+
164
+ this.started = true
165
+ }
166
+
167
+ stop () {
168
+ this.started = false
148
169
  }
149
170
 
150
171
  /**
@@ -474,8 +495,11 @@ export class KeyChain {
474
495
  if (!validateKeyName(name)) {
475
496
  throw errCode(new Error(`Invalid key name '${name}'`), codes.ERR_INVALID_KEY_NAME)
476
497
  }
477
- if (peer == null || peer.privateKey == null) {
478
- throw errCode(new Error('Peer.privKey is required'), codes.ERR_MISSING_PRIVATE_KEY)
498
+ if (peer == null) {
499
+ throw errCode(new Error('PeerId is required'), codes.ERR_MISSING_PRIVATE_KEY)
500
+ }
501
+ if (peer.privateKey == null) {
502
+ throw errCode(new Error('PeerId.privKey is required'), codes.ERR_MISSING_PRIVATE_KEY)
479
503
  }
480
504
 
481
505
  const privateKey = await unmarshalPrivateKey(peer.privateKey)
package/src/libp2p.ts CHANGED
@@ -124,7 +124,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
124
124
  // Set up the Upgrader
125
125
  this.components.setUpgrader(new DefaultUpgrader(this.components, {
126
126
  connectionEncryption: (init.connectionEncryption ?? []).map(component => this.configureComponent(component)),
127
- muxers: (init.streamMuxers ?? []).map(component => this.configureComponent(component))
127
+ muxers: (init.streamMuxers ?? []).map(component => this.configureComponent(component)),
128
+ inboundUpgradeTimeout: init.connectionManager.inboundUpgradeTimeout
128
129
  }))
129
130
 
130
131
  // Create the Connection Manager
@@ -217,7 +218,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
217
218
  })))
218
219
 
219
220
  if (init.relay.enabled) {
220
- this.components.getTransportManager().add(this.configureComponent(new Circuit()))
221
+ this.components.getTransportManager().add(this.configureComponent(new Circuit(init.relay)))
221
222
 
222
223
  this.configureComponent(new Relay(this.components, {
223
224
  addressSorter: init.connectionManager.addressSorter,
@@ -351,22 +352,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
351
352
  log('libp2p has stopped')
352
353
  }
353
354
 
354
- /**
355
- * Load keychain keys from the datastore.
356
- * Imports the private key as 'self', if needed.
357
- */
358
- async loadKeychain () {
359
- if (this.keychain == null) {
360
- return
361
- }
362
-
363
- try {
364
- await this.keychain.findKeyByName('self')
365
- } catch (err: any) {
366
- await this.keychain.importPeer('self', this.peerId)
367
- }
368
- }
369
-
370
355
  isStarted () {
371
356
  return this.started
372
357
  }
@@ -6,7 +6,6 @@ import { DefaultStats, StatsInit } from './stats.js'
6
6
  import type { ComponentMetricsUpdate, Metrics, Stats, TrackedMetric, TrackStreamOptions } from '@libp2p/interface-metrics'
7
7
  import type { PeerId } from '@libp2p/interface-peer-id'
8
8
  import type { Startable } from '@libp2p/interfaces/startable'
9
- import type { Duplex } from 'it-stream-types'
10
9
 
11
10
  const initialCounters: ['dataReceived', 'dataSent'] = [
12
11
  'dataReceived',
@@ -263,11 +262,11 @@ export class DefaultMetrics implements Metrics, Startable {
263
262
  * When the `PeerId` is known, `Metrics.updatePlaceholder` should be called
264
263
  * with the placeholder string returned from here, and the known `PeerId`.
265
264
  */
266
- trackStream <T extends Duplex<Uint8Array>> (opts: TrackStreamOptions<T>): T {
265
+ trackStream (opts: TrackStreamOptions): void {
267
266
  const { stream, remotePeer, protocol } = opts
268
267
 
269
268
  if (!this.running) {
270
- return stream
269
+ return
271
270
  }
272
271
 
273
272
  const source = stream.source
@@ -275,7 +274,7 @@ export class DefaultMetrics implements Metrics, Startable {
275
274
  remotePeer,
276
275
  protocol,
277
276
  direction: 'in',
278
- dataLength: chunk.length
277
+ dataLength: chunk.byteLength
279
278
  }))
280
279
 
281
280
  const sink = stream.sink
@@ -287,14 +286,12 @@ export class DefaultMetrics implements Metrics, Startable {
287
286
  remotePeer,
288
287
  protocol,
289
288
  direction: 'out',
290
- dataLength: chunk.length
289
+ dataLength: chunk.byteLength
291
290
  })
292
291
  }),
293
292
  sink
294
293
  )
295
294
  }
296
-
297
- return stream
298
295
  }
299
296
  }
300
297
 
package/src/ping/index.ts CHANGED
@@ -11,8 +11,10 @@ import type { PeerId } from '@libp2p/interface-peer-id'
11
11
  import type { Startable } from '@libp2p/interfaces/startable'
12
12
  import type { Components } from '@libp2p/components'
13
13
  import type { AbortOptions } from '@libp2p/interfaces'
14
- import type { Duplex } from 'it-stream-types'
15
14
  import { abortableDuplex } from 'abortable-iterator'
15
+ import { TimeoutController } from 'timeout-abort-controller'
16
+ import type { Stream } from '@libp2p/interface-connection'
17
+ import { setMaxListeners } from 'events'
16
18
 
17
19
  const log = logger('libp2p:ping')
18
20
 
@@ -20,6 +22,11 @@ export interface PingServiceInit {
20
22
  protocolPrefix: string
21
23
  maxInboundStreams: number
22
24
  maxOutboundStreams: number
25
+
26
+ /**
27
+ * How long we should wait for a ping response
28
+ */
29
+ timeout: number
23
30
  }
24
31
 
25
32
  export class PingService implements Startable {
@@ -73,19 +80,32 @@ export class PingService implements Startable {
73
80
  async ping (peer: PeerId, options: AbortOptions = {}): Promise<number> {
74
81
  log('dialing %s to %p', this.protocol, peer)
75
82
 
76
- const connection = await this.components.getConnectionManager().openConnection(peer, options)
77
- const stream = await connection.newStream([this.protocol], options)
78
83
  const start = Date.now()
79
84
  const data = randomBytes(PING_LENGTH)
80
-
81
- let source: Duplex<Uint8Array> = stream
82
-
83
- // make stream abortable if AbortSignal passed
84
- if (options.signal != null) {
85
- source = abortableDuplex(stream, options.signal)
85
+ const connection = await this.components.getConnectionManager().openConnection(peer, options)
86
+ let timeoutController
87
+ let signal = options.signal
88
+ let stream: Stream | undefined
89
+
90
+ // create a timeout if no abort signal passed
91
+ if (signal == null) {
92
+ timeoutController = new TimeoutController(this.init.timeout)
93
+ signal = timeoutController.signal
94
+
95
+ try {
96
+ // fails on node < 15.4
97
+ setMaxListeners?.(Infinity, timeoutController.signal)
98
+ } catch {}
86
99
  }
87
100
 
88
101
  try {
102
+ stream = await connection.newStream([this.protocol], {
103
+ signal
104
+ })
105
+
106
+ // make stream abortable
107
+ const source = abortableDuplex(stream, signal)
108
+
89
109
  const result = await pipe(
90
110
  [data],
91
111
  source,
@@ -93,13 +113,19 @@ export class PingService implements Startable {
93
113
  )
94
114
  const end = Date.now()
95
115
 
96
- if (result == null || !uint8ArrayEquals(data, result)) {
116
+ if (result == null || !uint8ArrayEquals(data, result.subarray())) {
97
117
  throw errCode(new Error('Received wrong ping ack'), codes.ERR_WRONG_PING_ACK)
98
118
  }
99
119
 
100
120
  return end - start
101
121
  } finally {
102
- stream.close()
122
+ if (timeoutController != null) {
123
+ timeoutController.clear()
124
+ }
125
+
126
+ if (stream != null) {
127
+ stream.close()
128
+ }
103
129
  }
104
130
  }
105
131
  }
package/src/pnet/index.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  import { handshake } from 'it-handshake'
14
14
  import { NONCE_LENGTH } from './key-generator.js'
15
15
  import type { ConnectionProtector, MultiaddrConnection } from '@libp2p/interface-connection'
16
+ import map from 'it-map'
16
17
 
17
18
  const log = logger('libp2p:pnet')
18
19
 
@@ -83,6 +84,7 @@ export class PreSharedKeyConnectionProtector implements ConnectionProtector {
83
84
  // Encrypt all outbound traffic
84
85
  createBoxStream(localNonce, this.psk),
85
86
  shake.stream,
87
+ (source) => map(source, (buf) => buf.subarray()),
86
88
  // Decrypt all inbound traffic
87
89
  createUnboxStream(remoteNonce, this.psk),
88
90
  external
package/src/registrar.ts CHANGED
@@ -10,8 +10,8 @@ import type { Components } from '@libp2p/components'
10
10
 
11
11
  const log = logger('libp2p:registrar')
12
12
 
13
- export const DEFAULT_MAX_INBOUND_STREAMS = 1
14
- export const DEFAULT_MAX_OUTBOUND_STREAMS = 1
13
+ export const DEFAULT_MAX_INBOUND_STREAMS = 32
14
+ export const DEFAULT_MAX_OUTBOUND_STREAMS = 64
15
15
 
16
16
  /**
17
17
  * Responsible for notifying registered protocols of events in the network.