libp2p 0.37.3-e6f646e → 0.38.0

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 (119) 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 -0
  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/libp2p.d.ts.map +1 -1
  70. package/dist/src/libp2p.js +3 -2
  71. package/dist/src/libp2p.js.map +1 -1
  72. package/dist/src/metrics/index.d.ts +1 -2
  73. package/dist/src/metrics/index.d.ts.map +1 -1
  74. package/dist/src/metrics/index.js +3 -4
  75. package/dist/src/metrics/index.js.map +1 -1
  76. package/dist/src/ping/index.d.ts +4 -0
  77. package/dist/src/ping/index.d.ts.map +1 -1
  78. package/dist/src/ping/index.js +27 -8
  79. package/dist/src/ping/index.js.map +1 -1
  80. package/dist/src/pnet/index.d.ts.map +1 -1
  81. package/dist/src/pnet/index.js +2 -1
  82. package/dist/src/pnet/index.js.map +1 -1
  83. package/dist/src/registrar.d.ts +2 -2
  84. package/dist/src/registrar.d.ts.map +1 -1
  85. package/dist/src/registrar.js +2 -2
  86. package/dist/src/registrar.js.map +1 -1
  87. package/dist/src/upgrader.d.ts +6 -0
  88. package/dist/src/upgrader.d.ts.map +1 -1
  89. package/dist/src/upgrader.js +121 -75
  90. package/dist/src/upgrader.js.map +1 -1
  91. package/dist/src/version.d.ts.map +1 -1
  92. package/dist/src/version.js.map +1 -1
  93. package/package.json +48 -47
  94. package/src/circuit/README.md +2 -2
  95. package/src/circuit/circuit/hop.ts +18 -9
  96. package/src/circuit/circuit/stop.ts +9 -4
  97. package/src/circuit/circuit/stream-handler.ts +5 -4
  98. package/src/circuit/index.ts +2 -11
  99. package/src/circuit/pb/index.ts +133 -15
  100. package/src/circuit/transport.ts +87 -58
  101. package/src/config.ts +19 -4
  102. package/src/connection-manager/dialer/auto-dialer.ts +6 -0
  103. package/src/connection-manager/dialer/index.ts +10 -10
  104. package/src/connection-manager/index.ts +54 -19
  105. package/src/constants.ts +5 -0
  106. package/src/fetch/index.ts +34 -8
  107. package/src/fetch/pb/proto.ts +109 -11
  108. package/src/identify/index.ts +41 -19
  109. package/src/identify/pb/message.ts +111 -11
  110. package/src/index.ts +1 -0
  111. package/src/insecure/index.ts +8 -3
  112. package/src/insecure/pb/proto.ts +111 -12
  113. package/src/libp2p.ts +3 -2
  114. package/src/metrics/index.ts +4 -7
  115. package/src/ping/index.ts +37 -11
  116. package/src/pnet/index.ts +2 -0
  117. package/src/registrar.ts +2 -2
  118. package/src/upgrader.ts +138 -78
  119. 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, enumeration, bytes } 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 FetchRequest {
@@ -9,17 +10,59 @@ export interface FetchRequest {
9
10
  }
10
11
 
11
12
  export namespace FetchRequest {
13
+ let _codec: Codec<FetchRequest>
14
+
12
15
  export const codec = (): Codec<FetchRequest> => {
13
- return message<FetchRequest>({
14
- 1: { name: 'identifier', codec: string }
15
- })
16
+ if (_codec == null) {
17
+ _codec = message<FetchRequest>((obj, writer, opts = {}) => {
18
+ if (opts.lengthDelimited !== false) {
19
+ writer.fork()
20
+ }
21
+
22
+ if (obj.identifier != null) {
23
+ writer.uint32(10)
24
+ writer.string(obj.identifier)
25
+ } else {
26
+ throw new Error('Protocol error: required field "identifier" was not found in object')
27
+ }
28
+
29
+ if (opts.lengthDelimited !== false) {
30
+ writer.ldelim()
31
+ }
32
+ }, (reader, length) => {
33
+ const obj: any = {}
34
+
35
+ const end = length == null ? reader.len : reader.pos + length
36
+
37
+ while (reader.pos < end) {
38
+ const tag = reader.uint32()
39
+
40
+ switch (tag >>> 3) {
41
+ case 1:
42
+ obj.identifier = reader.string()
43
+ break
44
+ default:
45
+ reader.skipType(tag & 7)
46
+ break
47
+ }
48
+ }
49
+
50
+ if (obj.identifier == null) {
51
+ throw new Error('Protocol error: value for required field "identifier" was not found in protobuf')
52
+ }
53
+
54
+ return obj
55
+ })
56
+ }
57
+
58
+ return _codec
16
59
  }
17
60
 
18
61
  export const encode = (obj: FetchRequest): Uint8Array => {
19
62
  return encodeMessage(obj, FetchRequest.codec())
20
63
  }
21
64
 
22
- export const decode = (buf: Uint8Array): FetchRequest => {
65
+ export const decode = (buf: Uint8Array | Uint8ArrayList): FetchRequest => {
23
66
  return decodeMessage(buf, FetchRequest.codec())
24
67
  }
25
68
  }
@@ -44,22 +87,77 @@ export namespace FetchResponse {
44
87
 
45
88
  export namespace StatusCode {
46
89
  export const codec = () => {
47
- return enumeration<typeof StatusCode>(__StatusCodeValues)
90
+ return enumeration<StatusCode>(__StatusCodeValues)
48
91
  }
49
92
  }
50
93
 
94
+ let _codec: Codec<FetchResponse>
95
+
51
96
  export const codec = (): Codec<FetchResponse> => {
52
- return message<FetchResponse>({
53
- 1: { name: 'status', codec: FetchResponse.StatusCode.codec() },
54
- 2: { name: 'data', codec: bytes }
55
- })
97
+ if (_codec == null) {
98
+ _codec = message<FetchResponse>((obj, writer, opts = {}) => {
99
+ if (opts.lengthDelimited !== false) {
100
+ writer.fork()
101
+ }
102
+
103
+ if (obj.status != null) {
104
+ writer.uint32(8)
105
+ FetchResponse.StatusCode.codec().encode(obj.status, writer)
106
+ } else {
107
+ throw new Error('Protocol error: required field "status" was not found in object')
108
+ }
109
+
110
+ if (obj.data != null) {
111
+ writer.uint32(18)
112
+ writer.bytes(obj.data)
113
+ } else {
114
+ throw new Error('Protocol error: required field "data" was not found in object')
115
+ }
116
+
117
+ if (opts.lengthDelimited !== false) {
118
+ writer.ldelim()
119
+ }
120
+ }, (reader, length) => {
121
+ const obj: any = {}
122
+
123
+ const end = length == null ? reader.len : reader.pos + length
124
+
125
+ while (reader.pos < end) {
126
+ const tag = reader.uint32()
127
+
128
+ switch (tag >>> 3) {
129
+ case 1:
130
+ obj.status = FetchResponse.StatusCode.codec().decode(reader)
131
+ break
132
+ case 2:
133
+ obj.data = reader.bytes()
134
+ break
135
+ default:
136
+ reader.skipType(tag & 7)
137
+ break
138
+ }
139
+ }
140
+
141
+ if (obj.status == null) {
142
+ throw new Error('Protocol error: value for required field "status" was not found in protobuf')
143
+ }
144
+
145
+ if (obj.data == null) {
146
+ throw new Error('Protocol error: value for required field "data" was not found in protobuf')
147
+ }
148
+
149
+ return obj
150
+ })
151
+ }
152
+
153
+ return _codec
56
154
  }
57
155
 
58
156
  export const encode = (obj: FetchResponse): Uint8Array => {
59
157
  return encodeMessage(obj, FetchResponse.codec())
60
158
  }
61
159
 
62
- export const decode = (buf: Uint8Array): FetchResponse => {
160
+ export const decode = (buf: Uint8Array | Uint8ArrayList): FetchResponse => {
63
161
  return decodeMessage(buf, FetchResponse.codec())
64
162
  }
65
163
  }
@@ -26,13 +26,10 @@ import type { Components } from '@libp2p/components'
26
26
  import { TimeoutController } from 'timeout-abort-controller'
27
27
  import type { AbortOptions } from '@libp2p/interfaces'
28
28
  import { abortableDuplex } from 'abortable-iterator'
29
- import type { Duplex } from 'it-stream-types'
29
+ import { setMaxListeners } from 'events'
30
30
 
31
31
  const log = logger('libp2p:identify')
32
32
 
33
- // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L48
34
- const IDENTIFY_TIMEOUT = 60000
35
-
36
33
  // https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L52
37
34
  const MAX_IDENTIFY_MESSAGE_SIZE = 1024 * 8
38
35
 
@@ -54,7 +51,7 @@ export interface IdentifyServiceInit {
54
51
  /**
55
52
  * How long we should wait for a remote peer to send their identify response
56
53
  */
57
- timeout?: number
54
+ timeout: number
58
55
 
59
56
  /**
60
57
  * Identify responses larger than this in bytes will be rejected (default: 8192)
@@ -167,8 +164,13 @@ export class IdentifyService implements Startable {
167
164
  const protocols = await this.components.getPeerStore().protoBook.get(this.components.getPeerId())
168
165
 
169
166
  const pushes = connections.map(async connection => {
170
- const timeoutController = new TimeoutController(this.init.timeout ?? IDENTIFY_TIMEOUT)
171
167
  let stream: Stream | undefined
168
+ const timeoutController = new TimeoutController(this.init.timeout)
169
+
170
+ try {
171
+ // fails on node < 15.4
172
+ setMaxListeners?.(Infinity, timeoutController.signal)
173
+ } catch {}
172
174
 
173
175
  try {
174
176
  stream = await connection.newStream([this.identifyPushProtocolStr], {
@@ -176,7 +178,7 @@ export class IdentifyService implements Startable {
176
178
  })
177
179
 
178
180
  // make stream abortable
179
- const source: Duplex<Uint8Array> = abortableDuplex(stream, timeoutController.signal)
181
+ const source = abortableDuplex(stream, timeoutController.signal)
180
182
 
181
183
  await pipe(
182
184
  [Identify.encode({
@@ -229,21 +231,29 @@ export class IdentifyService implements Startable {
229
231
  }
230
232
 
231
233
  async _identify (connection: Connection, options: AbortOptions = {}): Promise<Identify> {
232
- const stream = await connection.newStream([this.identifyProtocolStr], options)
233
- let source: Duplex<Uint8Array> = stream
234
234
  let timeoutController
235
235
  let signal = options.signal
236
+ let stream: Stream | undefined
236
237
 
237
238
  // create a timeout if no abort signal passed
238
239
  if (signal == null) {
239
- timeoutController = new TimeoutController(this.init.timeout ?? IDENTIFY_TIMEOUT)
240
+ timeoutController = new TimeoutController(this.init.timeout)
240
241
  signal = timeoutController.signal
241
- }
242
242
 
243
- // make stream abortable if AbortSignal passed
244
- source = abortableDuplex(stream, signal)
243
+ try {
244
+ // fails on node < 15.4
245
+ setMaxListeners?.(Infinity, timeoutController.signal)
246
+ } catch {}
247
+ }
245
248
 
246
249
  try {
250
+ stream = await connection.newStream([this.identifyProtocolStr], {
251
+ signal
252
+ })
253
+
254
+ // make stream abortable
255
+ const source = abortableDuplex(stream, signal)
256
+
247
257
  const data = await pipe(
248
258
  [],
249
259
  source,
@@ -267,7 +277,9 @@ export class IdentifyService implements Startable {
267
277
  timeoutController.clear()
268
278
  }
269
279
 
270
- stream.close()
280
+ if (stream != null) {
281
+ stream.close()
282
+ }
271
283
  }
272
284
  }
273
285
 
@@ -370,7 +382,12 @@ export class IdentifyService implements Startable {
370
382
  */
371
383
  async _handleIdentify (data: IncomingStreamData) {
372
384
  const { connection, stream } = data
373
- const timeoutController = new TimeoutController(this.init.timeout ?? IDENTIFY_TIMEOUT)
385
+ const timeoutController = new TimeoutController(this.init.timeout)
386
+
387
+ try {
388
+ // fails on node < 15.4
389
+ setMaxListeners?.(Infinity, timeoutController.signal)
390
+ } catch {}
374
391
 
375
392
  try {
376
393
  const publicKey = this.components.getPeerId().publicKey ?? new Uint8Array(0)
@@ -386,7 +403,7 @@ export class IdentifyService implements Startable {
386
403
 
387
404
  const envelope = await RecordEnvelope.seal(peerRecord, this.components.getPeerId())
388
405
  await this.components.getPeerStore().addressBook.consumePeerRecord(envelope)
389
- signedPeerRecord = envelope.marshal()
406
+ signedPeerRecord = envelope.marshal().subarray()
390
407
  }
391
408
 
392
409
  const message = Identify.encode({
@@ -400,7 +417,7 @@ export class IdentifyService implements Startable {
400
417
  })
401
418
 
402
419
  // make stream abortable
403
- const source: Duplex<Uint8Array> = abortableDuplex(stream, timeoutController.signal)
420
+ const source = abortableDuplex(stream, timeoutController.signal)
404
421
 
405
422
  await pipe(
406
423
  [message],
@@ -421,12 +438,17 @@ export class IdentifyService implements Startable {
421
438
  */
422
439
  async _handlePush (data: IncomingStreamData) {
423
440
  const { connection, stream } = data
424
- const timeoutController = new TimeoutController(this.init.timeout ?? IDENTIFY_TIMEOUT)
441
+ const timeoutController = new TimeoutController(this.init.timeout)
442
+
443
+ try {
444
+ // fails on node < 15.4
445
+ setMaxListeners?.(Infinity, timeoutController.signal)
446
+ } catch {}
425
447
 
426
448
  let message: Identify | undefined
427
449
  try {
428
450
  // make stream abortable
429
- const source: Duplex<Uint8Array> = abortableDuplex(stream, timeoutController.signal)
451
+ const source = abortableDuplex(stream, timeoutController.signal)
430
452
 
431
453
  const data = await pipe(
432
454
  [],
@@ -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 {
@@ -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
  }
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,