libp2p 0.36.2-64bfcee → 0.36.2-a15254f

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 (148) hide show
  1. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  2. package/dist/src/circuit/auto-relay.js +7 -6
  3. package/dist/src/circuit/auto-relay.js.map +1 -1
  4. package/dist/src/circuit/circuit/hop.d.ts +4 -4
  5. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  6. package/dist/src/circuit/circuit/hop.js +5 -5
  7. package/dist/src/circuit/circuit/hop.js.map +1 -1
  8. package/dist/src/circuit/circuit/stop.d.ts +3 -3
  9. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  10. package/dist/src/circuit/circuit/stop.js.map +1 -1
  11. package/dist/src/circuit/circuit/stream-handler.d.ts +4 -4
  12. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  13. package/dist/src/circuit/circuit/stream-handler.js +2 -3
  14. package/dist/src/circuit/circuit/stream-handler.js.map +1 -1
  15. package/dist/src/circuit/circuit/utils.d.ts +2 -2
  16. package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
  17. package/dist/src/circuit/circuit/utils.js.map +1 -1
  18. package/dist/src/circuit/listener.d.ts +3 -3
  19. package/dist/src/circuit/listener.d.ts.map +1 -1
  20. package/dist/src/circuit/listener.js +9 -1
  21. package/dist/src/circuit/listener.js.map +1 -1
  22. package/dist/src/circuit/pb/index.d.ts +42 -164
  23. package/dist/src/circuit/pb/index.d.ts.map +1 -0
  24. package/dist/src/circuit/pb/index.js +67 -523
  25. package/dist/src/circuit/pb/index.js.map +1 -0
  26. package/dist/src/circuit/transport.d.ts.map +1 -1
  27. package/dist/src/circuit/transport.js +6 -4
  28. package/dist/src/circuit/transport.js.map +1 -1
  29. package/dist/src/config.d.ts.map +1 -1
  30. package/dist/src/config.js +5 -7
  31. package/dist/src/config.js.map +1 -1
  32. package/dist/src/connection-manager/auto-dialler.js +4 -4
  33. package/dist/src/connection-manager/auto-dialler.js.map +1 -1
  34. package/dist/src/{dialer → connection-manager/dialer}/auto-dialer.d.ts +2 -0
  35. package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
  36. package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
  37. package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
  38. package/dist/src/{dialer → connection-manager/dialer}/dial-request.d.ts +2 -2
  39. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
  40. package/dist/src/{dialer → connection-manager/dialer}/dial-request.js +1 -1
  41. package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
  42. package/dist/src/{dialer → connection-manager/dialer}/index.d.ts +35 -7
  43. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
  44. package/dist/src/{dialer → connection-manager/dialer}/index.js +10 -24
  45. package/dist/src/connection-manager/dialer/index.js.map +1 -0
  46. package/dist/src/connection-manager/index.d.ts +55 -26
  47. package/dist/src/connection-manager/index.d.ts.map +1 -1
  48. package/dist/src/connection-manager/index.js +78 -40
  49. package/dist/src/connection-manager/index.js.map +1 -1
  50. package/dist/src/dht/dummy-dht.d.ts +17 -0
  51. package/dist/src/dht/dummy-dht.d.ts.map +1 -0
  52. package/dist/src/dht/dummy-dht.js +39 -0
  53. package/dist/src/dht/dummy-dht.js.map +1 -0
  54. package/dist/src/errors.d.ts +2 -0
  55. package/dist/src/errors.d.ts.map +1 -1
  56. package/dist/src/errors.js +2 -0
  57. package/dist/src/errors.js.map +1 -1
  58. package/dist/src/fetch/index.d.ts.map +1 -1
  59. package/dist/src/fetch/index.js +6 -7
  60. package/dist/src/fetch/index.js.map +1 -1
  61. package/dist/src/fetch/pb/proto.d.ts +20 -128
  62. package/dist/src/fetch/pb/proto.d.ts.map +1 -0
  63. package/dist/src/fetch/pb/proto.js +44 -331
  64. package/dist/src/fetch/pb/proto.js.map +1 -0
  65. package/dist/src/identify/index.d.ts +4 -2
  66. package/dist/src/identify/index.d.ts.map +1 -1
  67. package/dist/src/identify/index.js +12 -12
  68. package/dist/src/identify/index.js.map +1 -1
  69. package/dist/src/identify/pb/message.d.ts +13 -108
  70. package/dist/src/identify/pb/message.d.ts.map +1 -0
  71. package/dist/src/identify/pb/message.js +21 -366
  72. package/dist/src/identify/pb/message.js.map +1 -0
  73. package/dist/src/index.d.ts +11 -25
  74. package/dist/src/index.d.ts.map +1 -1
  75. package/dist/src/index.js.map +1 -1
  76. package/dist/src/insecure/index.d.ts.map +1 -1
  77. package/dist/src/insecure/index.js +5 -2
  78. package/dist/src/insecure/index.js.map +1 -1
  79. package/dist/src/insecure/pb/proto.d.ts +24 -130
  80. package/dist/src/insecure/pb/proto.d.ts.map +1 -0
  81. package/dist/src/insecure/pb/proto.js +46 -388
  82. package/dist/src/insecure/pb/proto.js.map +1 -0
  83. package/dist/src/keychain/index.d.ts +3 -3
  84. package/dist/src/keychain/index.js +2 -2
  85. package/dist/src/keychain/util.d.ts +1 -1
  86. package/dist/src/keychain/util.d.ts.map +1 -1
  87. package/dist/src/libp2p.d.ts +5 -4
  88. package/dist/src/libp2p.d.ts.map +1 -1
  89. package/dist/src/libp2p.js +69 -45
  90. package/dist/src/libp2p.js.map +1 -1
  91. package/dist/src/metrics/stats.d.ts +1 -5
  92. package/dist/src/metrics/stats.d.ts.map +1 -1
  93. package/dist/src/metrics/stats.js.map +1 -1
  94. package/dist/src/ping/index.d.ts.map +1 -1
  95. package/dist/src/ping/index.js +2 -1
  96. package/dist/src/ping/index.js.map +1 -1
  97. package/dist/src/pubsub/dummy-pubsub.d.ts +17 -0
  98. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
  99. package/dist/src/pubsub/dummy-pubsub.js +37 -0
  100. package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
  101. package/dist/src/registrar.js +1 -1
  102. package/dist/src/registrar.js.map +1 -1
  103. package/package.json +46 -44
  104. package/src/circuit/auto-relay.ts +8 -6
  105. package/src/circuit/circuit/hop.ts +9 -9
  106. package/src/circuit/circuit/stop.ts +3 -3
  107. package/src/circuit/circuit/stream-handler.ts +5 -6
  108. package/src/circuit/circuit/utils.ts +2 -2
  109. package/src/circuit/listener.ts +17 -4
  110. package/src/circuit/pb/index.proto +1 -1
  111. package/src/circuit/pb/index.ts +88 -0
  112. package/src/circuit/transport.ts +7 -4
  113. package/src/config.ts +5 -7
  114. package/src/connection-manager/auto-dialler.ts +4 -4
  115. package/src/{dialer → connection-manager/dialer}/auto-dialer.ts +23 -4
  116. package/src/{dialer → connection-manager/dialer}/dial-request.ts +4 -4
  117. package/src/{dialer → connection-manager/dialer}/index.ts +46 -39
  118. package/src/connection-manager/index.ts +143 -59
  119. package/src/dht/dummy-dht.ts +51 -0
  120. package/src/errors.ts +2 -0
  121. package/src/fetch/index.ts +7 -8
  122. package/src/fetch/pb/proto.ts +58 -0
  123. package/src/identify/index.ts +14 -14
  124. package/src/identify/pb/message.ts +36 -0
  125. package/src/index.ts +12 -30
  126. package/src/insecure/index.ts +8 -4
  127. package/src/insecure/pb/proto.ts +61 -0
  128. package/src/keychain/index.ts +2 -2
  129. package/src/libp2p.ts +81 -54
  130. package/src/metrics/stats.ts +1 -6
  131. package/src/ping/index.ts +2 -1
  132. package/src/pubsub/dummy-pubsub.ts +51 -0
  133. package/src/registrar.ts +1 -1
  134. package/dist/src/dialer/auto-dialer.d.ts.map +0 -1
  135. package/dist/src/dialer/auto-dialer.js +0 -24
  136. package/dist/src/dialer/auto-dialer.js.map +0 -1
  137. package/dist/src/dialer/dial-request.d.ts.map +0 -1
  138. package/dist/src/dialer/dial-request.js.map +0 -1
  139. package/dist/src/dialer/index.d.ts.map +0 -1
  140. package/dist/src/dialer/index.js.map +0 -1
  141. package/src/circuit/pb/index.d.ts +0 -173
  142. package/src/circuit/pb/index.js +0 -528
  143. package/src/fetch/pb/proto.d.ts +0 -134
  144. package/src/fetch/pb/proto.js +0 -331
  145. package/src/identify/pb/message.d.ts +0 -110
  146. package/src/identify/pb/message.js +0 -369
  147. package/src/insecure/pb/proto.d.ts +0 -134
  148. package/src/insecure/pb/proto.js +0 -388
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2p",
3
- "version": "0.36.2-64bfcee",
3
+ "version": "0.36.2-a15254f",
4
4
  "description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p#readme",
@@ -76,38 +76,38 @@
76
76
  ]
77
77
  },
78
78
  "scripts": {
79
+ "clean": "aegir clean",
79
80
  "lint": "aegir lint",
80
- "build": "tsc",
81
- "postbuild": "mkdirp dist/src/circuit/pb dist/src/fetch/pb dist/src/identify/pb dist/src/insecure/pb && cp src/circuit/pb/*.js src/circuit/pb/*.d.ts dist/src/circuit/pb && cp src/fetch/pb/*.js src/fetch/pb/*.d.ts dist/src/fetch/pb && cp src/identify/pb/*.js src/identify/pb/*.d.ts dist/src/identify/pb && cp src/insecure/pb/*.js src/insecure/pb/*.d.ts dist/src/insecure/pb",
82
- "generate": "run-s generate:proto:* generate:proto-types:*",
83
- "generate:proto:circuit": "pbjs -t static-module -w es6 -r libp2p-circuit --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/circuit/protocol/index.js ./src/circuit/protocol/index.proto",
84
- "generate:proto:fetch": "pbjs -t static-module -w es6 -r libp2p-fetch --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/fetch/proto.js ./src/fetch/proto.proto",
85
- "generate:proto:identify": "pbjs -t static-module -w es6 -r libp2p-identify --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/identify/message.js ./src/identify/message.proto",
86
- "generate:proto:plaintext": "pbjs -t static-module -w es6 -r libp2p-plaintext --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/insecure/proto.js ./src/insecure/proto.proto",
87
- "generate:proto-types:circuit": "pbts -o src/circuit/protocol/index.d.ts src/circuit/protocol/index.js",
88
- "generate:proto-types:fetch": "pbts -o src/fetch/proto.d.ts src/fetch/proto.js",
89
- "generate:proto-types:identify": "pbts -o src/identify/message.d.ts src/identify/message.js",
90
- "generate:proto-types:plaintext": "pbts -o src/insecure/proto.d.ts src/insecure/proto.js",
91
- "pretest": "npm run build",
81
+ "dep-check": "aegir dep-check",
82
+ "build": "aegir build",
83
+ "generate": "run-s generate:proto:*",
84
+ "generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
85
+ "generate:proto:fetch": "protons ./src/fetch/pb/proto.proto",
86
+ "generate:proto:identify": "protons ./src/identify/pb/message.proto",
87
+ "generate:proto:plaintext": "protons ./src/insecure/pb/proto.proto",
92
88
  "test": "aegir test",
93
- "test:node": "npm run test -- -t node -f \"./dist/test/**/*.{node,spec}.js\" --cov",
94
- "test:chrome": "npm run test -- -t browser -f \"./dist/test/**/*.spec.js\" --cov",
95
- "test:chrome-webworker": "npm run test -- -t webworker -f \"./dist/test/**/*.spec.js\"",
96
- "test:firefox": "npm run test -- -t browser -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
97
- "test:firefox-webworker": "npm run test -- -t webworker -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
89
+ "test:node": "aegir test -t node -f \"./dist/test/**/*.{node,spec}.js\" --cov",
90
+ "test:chrome": "aegir test -t browser -f \"./dist/test/**/*.spec.js\" --cov",
91
+ "test:chrome-webworker": "aegir test -t webworker -f \"./dist/test/**/*.spec.js\"",
92
+ "test:firefox": "aegir test -t browser -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
93
+ "test:firefox-webworker": "aegir test -t webworker -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
98
94
  "test:examples": "cd examples && npm run test:all",
99
- "test:interop": "npm run test -- -t node -f dist/test/interop.js"
95
+ "test:interop": "aegir test -t node -f dist/test/interop.js"
100
96
  },
101
97
  "dependencies": {
102
98
  "@achingbrain/nat-port-mapper": "^1.0.0",
103
- "@libp2p/connection": "^1.1.4",
104
- "@libp2p/crypto": "^0.22.9",
105
- "@libp2p/interfaces": "^1.3.17",
106
- "@libp2p/multistream-select": "^1.0.3",
107
- "@libp2p/peer-id": "^1.1.8",
108
- "@libp2p/peer-id-factory": "^1.0.8",
109
- "@libp2p/peer-store": "^1.0.6",
110
- "@libp2p/utils": "^1.0.9",
99
+ "@libp2p/connection": "^1.1.5",
100
+ "@libp2p/crypto": "^0.22.11",
101
+ "@libp2p/interfaces": "^1.3.30",
102
+ "@libp2p/logger": "^1.1.4",
103
+ "@libp2p/multistream-select": "^1.0.4",
104
+ "@libp2p/peer-collections": "^1.0.2",
105
+ "@libp2p/peer-id": "^1.1.10",
106
+ "@libp2p/peer-id-factory": "^1.0.9",
107
+ "@libp2p/peer-record": "^1.0.8",
108
+ "@libp2p/peer-store": "^1.0.10",
109
+ "@libp2p/tracked-map": "^1.0.5",
110
+ "@libp2p/utils": "^1.0.10",
111
111
  "@multiformats/mafmt": "^11.0.2",
112
112
  "@multiformats/multiaddr": "^10.1.8",
113
113
  "abortable-iterator": "^4.0.2",
@@ -130,6 +130,7 @@
130
130
  "it-length-prefixed": "^7.0.1",
131
131
  "it-map": "^1.0.6",
132
132
  "it-merge": "^1.0.3",
133
+ "it-pair": "^2.0.2",
133
134
  "it-pipe": "^2.0.3",
134
135
  "it-sort": "^1.0.1",
135
136
  "it-stream-types": "^1.0.4",
@@ -144,7 +145,7 @@
144
145
  "p-retry": "^5.0.0",
145
146
  "p-settle": "^5.0.0",
146
147
  "private-ip": "^2.3.3",
147
- "protobufjs": "^6.11.2",
148
+ "protons-runtime": "^1.0.2",
148
149
  "retimer": "^3.0.0",
149
150
  "sanitize-filename": "^1.6.3",
150
151
  "set-delayed-interval": "^1.0.0",
@@ -156,37 +157,37 @@
156
157
  "xsalsa20": "^1.1.0"
157
158
  },
158
159
  "devDependencies": {
159
- "@achingbrain/libp2p-gossipsub": "^0.13.5",
160
160
  "@chainsafe/libp2p-noise": "^6.0.1",
161
- "@libp2p/bootstrap": "^1.0.2",
162
- "@libp2p/daemon-client": "^1.0.0",
163
- "@libp2p/daemon-server": "^1.0.0",
161
+ "@libp2p/bootstrap": "^1.0.3",
162
+ "@libp2p/daemon-client": "^1.0.2",
163
+ "@libp2p/daemon-server": "^1.0.2",
164
164
  "@libp2p/delegated-content-routing": "^1.0.2",
165
165
  "@libp2p/delegated-peer-routing": "^1.0.2",
166
- "@libp2p/floodsub": "^1.0.2",
167
- "@libp2p/interface-compliance-tests": "^1.1.20",
168
- "@libp2p/interop": "^1.0.0",
169
- "@libp2p/kad-dht": "^1.0.3",
170
- "@libp2p/mdns": "^1.0.3",
171
- "@libp2p/mplex": "^1.0.1",
172
- "@libp2p/tcp": "^1.0.6",
173
- "@libp2p/tracked-map": "^1.0.4",
174
- "@libp2p/webrtc-star": "^1.0.3",
175
- "@libp2p/websockets": "^1.0.3",
166
+ "@libp2p/floodsub": "^1.0.6",
167
+ "@libp2p/interface-compliance-tests": "^1.1.31",
168
+ "@libp2p/interop": "^1.0.3",
169
+ "@libp2p/kad-dht": "^1.0.8",
170
+ "@libp2p/mdns": "^1.0.4",
171
+ "@libp2p/mplex": "^1.0.3",
172
+ "@libp2p/pubsub": "^1.2.18",
173
+ "@libp2p/tcp": "^1.0.8",
174
+ "@libp2p/topology": "^1.1.7",
175
+ "@libp2p/webrtc-star": "^1.0.7",
176
+ "@libp2p/websockets": "^1.0.6",
176
177
  "@nodeutils/defaults-deep": "^1.1.0",
177
178
  "@types/node": "^16.11.26",
178
179
  "@types/node-forge": "^1.0.0",
179
180
  "@types/p-fifo": "^1.0.0",
180
181
  "@types/varint": "^6.0.0",
181
182
  "@types/xsalsa20": "^1.1.0",
182
- "aegir": "^36.1.3",
183
+ "aegir": "^37.0.9",
183
184
  "buffer": "^6.0.3",
184
185
  "cborg": "^1.8.1",
185
186
  "delay": "^5.0.0",
187
+ "execa": "^6.1.0",
186
188
  "go-libp2p": "^0.0.6",
187
189
  "into-stream": "^7.0.0",
188
190
  "ipfs-http-client": "^56.0.1",
189
- "it-pair": "^2.0.2",
190
191
  "it-pushable": "^2.0.1",
191
192
  "nock": "^13.0.3",
192
193
  "npm-run-all": "^4.1.5",
@@ -194,6 +195,7 @@
194
195
  "p-event": "^5.0.1",
195
196
  "p-times": "^4.0.0",
196
197
  "p-wait-for": "^4.1.0",
198
+ "protons": "^3.0.2",
197
199
  "rimraf": "^3.0.2",
198
200
  "sinon": "^13.0.1",
199
201
  "ts-sinon": "^2.0.2"
@@ -85,12 +85,14 @@ export class AutoRelay {
85
85
 
86
86
  // If protocol, check if can hop, store info in the metadataBook and listen on it
87
87
  try {
88
- const connection = this.components.getConnectionManager().getConnection(peerId)
88
+ const connections = this.components.getConnectionManager().getConnections(peerId)
89
89
 
90
- if (connection == null) {
90
+ if (connections.length === 0) {
91
91
  return
92
92
  }
93
93
 
94
+ const connection = connections[0]
95
+
94
96
  // Do not hop on a relayed connection
95
97
  if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
96
98
  log(`relayed connection to ${id} will not be used to hop on`)
@@ -223,15 +225,15 @@ export class AutoRelay {
223
225
  continue
224
226
  }
225
227
 
226
- const connection = this.components.getConnectionManager().getConnection(id)
228
+ const connections = this.components.getConnectionManager().getConnections(id)
227
229
 
228
230
  // If not connected, store for possible later use.
229
- if (connection == null) {
231
+ if (connections.length === 0) {
230
232
  knownHopsToDial.push(id)
231
233
  continue
232
234
  }
233
235
 
234
- await this._addListenRelay(connection, idStr)
236
+ await this._addListenRelay(connections[0], idStr)
235
237
 
236
238
  // Check if already listening on enough relays
237
239
  if (this.listenRelays.size >= this.maxListeners) {
@@ -274,7 +276,7 @@ export class AutoRelay {
274
276
 
275
277
  async _tryToListenOnRelay (peerId: PeerId) {
276
278
  try {
277
- const connection = await this.components.getDialer().dial(peerId)
279
+ const connection = await this.components.getConnectionManager().openConnection(peerId)
278
280
  await this._addListenRelay(connection, peerId.toString())
279
281
  } catch (err: any) {
280
282
  log.error('Could not use %p as relay', peerId, err)
@@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger'
2
2
  import errCode from 'err-code'
3
3
  import { validateAddrs } from './utils.js'
4
4
  import { StreamHandler } from './stream-handler.js'
5
- import { CircuitRelay as CircuitPB, ICircuitRelay } from '../pb/index.js'
5
+ import { CircuitRelay as CircuitPB } from '../pb/index.js'
6
6
  import { pipe } from 'it-pipe'
7
7
  import { codes as Errors } from '../../errors.js'
8
8
  import { stop } from './stop.js'
@@ -11,13 +11,13 @@ import type { Connection } from '@libp2p/interfaces/connection'
11
11
  import { peerIdFromBytes } from '@libp2p/peer-id'
12
12
  import type { Duplex } from 'it-stream-types'
13
13
  import type { Circuit } from '../transport.js'
14
- import type { ConnectionManager } from '@libp2p/interfaces/registrar'
14
+ import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
15
15
 
16
16
  const log = logger('libp2p:circuit:hop')
17
17
 
18
18
  export interface HopRequest {
19
19
  connection: Connection
20
- request: ICircuitRelay
20
+ request: CircuitPB
21
21
  streamHandler: StreamHandler
22
22
  circuit: Circuit
23
23
  connectionManager: ConnectionManager
@@ -58,8 +58,8 @@ export async function handleHop (hopRequest: HopRequest) {
58
58
  // Get the connection to the destination (stop) peer
59
59
  const destinationPeer = peerIdFromBytes(request.dstPeer.id)
60
60
 
61
- const destinationConnection = connectionManager.getConnection(destinationPeer)
62
- if (destinationConnection == null && !circuit.hopActive()) {
61
+ const destinationConnections = connectionManager.getConnections(destinationPeer)
62
+ if (destinationConnections.length === 0 && !circuit.hopActive()) {
63
63
  log('HOP request received but we are not connected to the destination peer')
64
64
  return streamHandler.end({
65
65
  type: CircuitPB.Type.STATUS,
@@ -68,7 +68,7 @@ export async function handleHop (hopRequest: HopRequest) {
68
68
  }
69
69
 
70
70
  // TODO: Handle being an active relay
71
- if (destinationConnection == null) {
71
+ if (destinationConnections.length === 0) {
72
72
  log('did not have connection to remote peer')
73
73
  return streamHandler.end({
74
74
  type: CircuitPB.Type.STATUS,
@@ -87,7 +87,7 @@ export async function handleHop (hopRequest: HopRequest) {
87
87
  try {
88
88
  log('performing STOP request')
89
89
  const result = await stop({
90
- connection: destinationConnection,
90
+ connection: destinationConnections[0],
91
91
  request: stopRequest
92
92
  })
93
93
 
@@ -120,7 +120,7 @@ export async function handleHop (hopRequest: HopRequest) {
120
120
 
121
121
  export interface HopConfig {
122
122
  connection: Connection
123
- request: ICircuitRelay
123
+ request: CircuitPB
124
124
  }
125
125
 
126
126
  /**
@@ -153,7 +153,7 @@ export async function hop (options: HopConfig): Promise<Duplex<Uint8Array>> {
153
153
  log('hop request failed with code %d, closing stream', response.code)
154
154
  streamHandler.close()
155
155
 
156
- throw errCode(new Error(`HOP request failed with code ${response.code}`), Errors.ERR_HOP_REQUEST_FAILED)
156
+ throw errCode(new Error(`HOP request failed with code "${response.code ?? 'unknown'}"`), Errors.ERR_HOP_REQUEST_FAILED)
157
157
  }
158
158
 
159
159
  export interface CanHopOptions {
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import { CircuitRelay as CircuitPB, ICircuitRelay } from '../pb/index.js'
2
+ import { CircuitRelay as CircuitPB } from '../pb/index.js'
3
3
  import { RELAY_CODEC } from '../multicodec.js'
4
4
  import { StreamHandler } from './stream-handler.js'
5
5
  import { validateAddrs } from './utils.js'
@@ -10,7 +10,7 @@ const log = logger('libp2p:circuit:stop')
10
10
 
11
11
  export interface HandleStopOptions {
12
12
  connection: Connection
13
- request: ICircuitRelay
13
+ request: CircuitPB
14
14
  streamHandler: StreamHandler
15
15
  }
16
16
 
@@ -44,7 +44,7 @@ export function handleStop (options: HandleStopOptions): Duplex<Uint8Array> | un
44
44
 
45
45
  export interface StopOptions {
46
46
  connection: Connection
47
- request: ICircuitRelay
47
+ request: CircuitPB
48
48
  }
49
49
 
50
50
  /**
@@ -1,7 +1,7 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import * as lp from 'it-length-prefixed'
3
3
  import { Handshake, handshake } from 'it-handshake'
4
- import { CircuitRelay, ICircuitRelay } from '../pb/index.js'
4
+ import { CircuitRelay } from '../pb/index.js'
5
5
  import type { Stream } from '@libp2p/interfaces/connection'
6
6
  import type { Source } from 'it-stream-types'
7
7
 
@@ -53,10 +53,9 @@ export class StreamHandler {
53
53
  /**
54
54
  * Encode and write array of buffers
55
55
  */
56
- write (msg: ICircuitRelay) {
56
+ write (msg: CircuitRelay) {
57
57
  log('write message type %s', msg.type)
58
- // @ts-expect-error lp.encode expects type type 'Buffer | BufferList', not 'Uint8Array'
59
- this.shake.write(lp.encode.single(CircuitRelay.encode(msg).finish()))
58
+ this.shake.write(lp.encode.single(CircuitRelay.encode(msg)).slice())
60
59
  }
61
60
 
62
61
  /**
@@ -68,9 +67,9 @@ export class StreamHandler {
68
67
  }
69
68
 
70
69
  /**
71
- * @param {ICircuitRelay} msg - An unencoded CircuitRelay protobuf message
70
+ * @param {CircuitRelay} msg - An unencoded CircuitRelay protobuf message
72
71
  */
73
- end (msg: ICircuitRelay) {
72
+ end (msg: CircuitRelay) {
74
73
  this.write(msg)
75
74
  this.close()
76
75
  }
@@ -1,5 +1,5 @@
1
1
  import { Multiaddr } from '@multiformats/multiaddr'
2
- import { CircuitRelay, ICircuitRelay } from '../pb/index.js'
2
+ import { CircuitRelay } from '../pb/index.js'
3
3
  import type { StreamHandler } from './stream-handler.js'
4
4
 
5
5
  /**
@@ -15,7 +15,7 @@ function writeResponse (streamHandler: StreamHandler, status: CircuitRelay.Statu
15
15
  /**
16
16
  * Validate incomming HOP/STOP message
17
17
  */
18
- export function validateAddrs (msg: ICircuitRelay, streamHandler: StreamHandler) {
18
+ export function validateAddrs (msg: CircuitRelay, streamHandler: StreamHandler) {
19
19
  try {
20
20
  if (msg.dstPeer?.addrs != null) {
21
21
  msg.dstPeer.addrs.forEach((addr) => {
@@ -1,11 +1,12 @@
1
1
  import { CustomEvent, EventEmitter } from '@libp2p/interfaces'
2
- import type { ConnectionManager } from '@libp2p/interfaces/registrar'
3
- import type { Dialer } from '@libp2p/interfaces/dialer'
2
+ import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
3
+ import type { PeerStore } from '@libp2p/interfaces/peer-store'
4
4
  import type { Listener } from '@libp2p/interfaces/transport'
5
+ import { peerIdFromString } from '@libp2p/peer-id'
5
6
  import { Multiaddr } from '@multiformats/multiaddr'
6
7
 
7
8
  export interface ListenerOptions {
8
- dialer: Dialer
9
+ peerStore: PeerStore
9
10
  connectionManager: ConnectionManager
10
11
  }
11
12
 
@@ -17,7 +18,19 @@ export function createListener (options: ListenerOptions): Listener {
17
18
  */
18
19
  async function listen (addr: Multiaddr): Promise<void> {
19
20
  const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
20
- const relayConn = await options.dialer.dial(new Multiaddr(addrString))
21
+ const ma = new Multiaddr(addrString)
22
+
23
+ const relayPeerStr = ma.getPeerId()
24
+
25
+ if (relayPeerStr == null) {
26
+ throw new Error('Could not determine relay peer from multiaddr')
27
+ }
28
+
29
+ const relayPeerId = peerIdFromString(relayPeerStr)
30
+
31
+ await options.peerStore.addressBook.add(relayPeerId, [ma])
32
+
33
+ const relayConn = await options.connectionManager.openConnection(relayPeerId)
21
34
  const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
22
35
 
23
36
  listeningAddrs.set(relayConn.remotePeer.toString(), relayedAddr)
@@ -1,4 +1,4 @@
1
- syntax = "proto2";
1
+ syntax = "proto3";
2
2
 
3
3
  message CircuitRelay {
4
4
 
@@ -0,0 +1,88 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+
4
+ import { enumeration, encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
5
+
6
+ export interface CircuitRelay {
7
+ type?: CircuitRelay.Type
8
+ srcPeer?: CircuitRelay.Peer
9
+ dstPeer?: CircuitRelay.Peer
10
+ code?: CircuitRelay.Status
11
+ }
12
+
13
+ export namespace CircuitRelay {
14
+ export enum Status {
15
+ SUCCESS = 'SUCCESS',
16
+ HOP_SRC_ADDR_TOO_LONG = 'HOP_SRC_ADDR_TOO_LONG',
17
+ HOP_DST_ADDR_TOO_LONG = 'HOP_DST_ADDR_TOO_LONG',
18
+ HOP_SRC_MULTIADDR_INVALID = 'HOP_SRC_MULTIADDR_INVALID',
19
+ HOP_DST_MULTIADDR_INVALID = 'HOP_DST_MULTIADDR_INVALID',
20
+ HOP_NO_CONN_TO_DST = 'HOP_NO_CONN_TO_DST',
21
+ HOP_CANT_DIAL_DST = 'HOP_CANT_DIAL_DST',
22
+ HOP_CANT_OPEN_DST_STREAM = 'HOP_CANT_OPEN_DST_STREAM',
23
+ HOP_CANT_SPEAK_RELAY = 'HOP_CANT_SPEAK_RELAY',
24
+ HOP_CANT_RELAY_TO_SELF = 'HOP_CANT_RELAY_TO_SELF',
25
+ STOP_SRC_ADDR_TOO_LONG = 'STOP_SRC_ADDR_TOO_LONG',
26
+ STOP_DST_ADDR_TOO_LONG = 'STOP_DST_ADDR_TOO_LONG',
27
+ STOP_SRC_MULTIADDR_INVALID = 'STOP_SRC_MULTIADDR_INVALID',
28
+ STOP_DST_MULTIADDR_INVALID = 'STOP_DST_MULTIADDR_INVALID',
29
+ STOP_RELAY_REFUSED = 'STOP_RELAY_REFUSED',
30
+ MALFORMED_MESSAGE = 'MALFORMED_MESSAGE'
31
+ }
32
+
33
+ export namespace Status {
34
+ export const codec = () => {
35
+ return enumeration<typeof Status>(Status)
36
+ }
37
+ }
38
+ export enum Type {
39
+ HOP = 'HOP',
40
+ STOP = 'STOP',
41
+ STATUS = 'STATUS',
42
+ CAN_HOP = 'CAN_HOP'
43
+ }
44
+
45
+ export namespace Type {
46
+ export const codec = () => {
47
+ return enumeration<typeof Type>(Type)
48
+ }
49
+ }
50
+ export interface Peer {
51
+ id: Uint8Array
52
+ addrs: Uint8Array[]
53
+ }
54
+
55
+ export namespace Peer {
56
+ export const codec = () => {
57
+ return message<Peer>({
58
+ 1: { name: 'id', codec: bytes },
59
+ 2: { name: 'addrs', codec: bytes, repeats: true }
60
+ })
61
+ }
62
+
63
+ export const encode = (obj: Peer): Uint8Array => {
64
+ return encodeMessage(obj, Peer.codec())
65
+ }
66
+
67
+ export const decode = (buf: Uint8Array): Peer => {
68
+ return decodeMessage(buf, Peer.codec())
69
+ }
70
+ }
71
+
72
+ export const codec = () => {
73
+ return message<CircuitRelay>({
74
+ 1: { name: 'type', codec: CircuitRelay.Type.codec(), optional: true },
75
+ 2: { name: 'srcPeer', codec: CircuitRelay.Peer.codec(), optional: true },
76
+ 3: { name: 'dstPeer', codec: CircuitRelay.Peer.codec(), optional: true },
77
+ 4: { name: 'code', codec: CircuitRelay.Status.codec(), optional: true }
78
+ })
79
+ }
80
+
81
+ export const encode = (obj: CircuitRelay): Uint8Array => {
82
+ return encodeMessage(obj, CircuitRelay.codec())
83
+ }
84
+
85
+ export const decode = (buf: Uint8Array): CircuitRelay => {
86
+ return decodeMessage(buf, CircuitRelay.codec())
87
+ }
88
+ }
@@ -149,9 +149,12 @@ export class Circuit implements Transport, Initializable {
149
149
  const destinationPeer = peerIdFromString(destinationId)
150
150
 
151
151
  let disconnectOnFailure = false
152
- let relayConnection = this.components.getConnectionManager().getConnection(relayPeer)
152
+ const relayConnections = this.components.getConnectionManager().getConnections(relayPeer)
153
+ let relayConnection = relayConnections[0]
154
+
153
155
  if (relayConnection == null) {
154
- relayConnection = await this.components.getDialer().dial(relayAddr, options)
156
+ await this.components.getPeerStore().addressBook.add(relayPeer, [relayAddr])
157
+ relayConnection = await this.components.getConnectionManager().openConnection(relayPeer, options)
155
158
  disconnectOnFailure = true
156
159
  }
157
160
 
@@ -195,8 +198,8 @@ export class Circuit implements Transport, Initializable {
195
198
  this.handler = options.handler
196
199
 
197
200
  return createListener({
198
- dialer: this.components.getDialer(),
199
- connectionManager: this.components.getConnectionManager()
201
+ connectionManager: this.components.getConnectionManager(),
202
+ peerStore: this.components.getPeerStore()
200
203
  })
201
204
  }
202
205
 
package/src/config.ts CHANGED
@@ -21,14 +21,8 @@ const DefaultConfig: Partial<Libp2pInit> = {
21
21
  connectionManager: {
22
22
  maxConnections: 300,
23
23
  minConnections: 50,
24
+ autoDial: true,
24
25
  autoDialInterval: 10000,
25
- autoDial: true
26
- },
27
- connectionGater: {},
28
- transportManager: {
29
- faultTolerance: FaultTolerance.FATAL_ALL
30
- },
31
- dialer: {
32
26
  maxParallelDials: Constants.MAX_PARALLEL_DIALS,
33
27
  maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
34
28
  dialTimeout: Constants.DIAL_TIMEOUT,
@@ -37,6 +31,10 @@ const DefaultConfig: Partial<Libp2pInit> = {
37
31
  },
38
32
  addressSorter: publicAddressesFirst
39
33
  },
34
+ connectionGater: {},
35
+ transportManager: {
36
+ faultTolerance: FaultTolerance.FATAL_ALL
37
+ },
40
38
  host: {
41
39
  agentVersion: AGENT_VERSION
42
40
  },
@@ -102,7 +102,7 @@ export class AutoDialler implements Startable {
102
102
  const minConnections = this.options.minConnections
103
103
 
104
104
  // Already has enough connections
105
- if (this.components.getConnectionManager().getConnectionList().length >= minConnections) {
105
+ if (this.components.getConnectionManager().getConnections().length >= minConnections) {
106
106
  this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
107
107
 
108
108
  return
@@ -126,7 +126,7 @@ export class AutoDialler implements Startable {
126
126
  async (source) => await all(source)
127
127
  )
128
128
 
129
- for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().getConnectionList().length < minConnections; i++) {
129
+ for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().getConnections().length < minConnections; i++) {
130
130
  // Connection Manager was stopped during async dial
131
131
  if (!this.running) {
132
132
  return
@@ -134,10 +134,10 @@ export class AutoDialler implements Startable {
134
134
 
135
135
  const peer = peers[i]
136
136
 
137
- if (this.components.getConnectionManager().getConnection(peer.id) == null) {
137
+ if (this.components.getConnectionManager().getConnections(peer.id).length === 0) {
138
138
  log('connecting to a peerStore stored peer %p', peer.id)
139
139
  try {
140
- await this.components.getDialer().dial(peer.id)
140
+ await this.components.getConnectionManager().openConnection(peer.id)
141
141
  } catch (err: any) {
142
142
  log.error('could not connect to peerStore stored peer', err)
143
143
  }
@@ -1,39 +1,58 @@
1
1
  import type { PeerInfo } from '@libp2p/interfaces/peer-info'
2
2
  import { logger } from '@libp2p/logger'
3
3
  import type { Components } from '@libp2p/interfaces/components'
4
+ import { TimeoutController } from 'timeout-abort-controller'
4
5
 
5
6
  const log = logger('libp2p:dialer:auto-dialer')
6
7
 
7
8
  export interface AutoDialerInit {
8
9
  enabled: boolean
9
10
  minConnections: number
11
+ dialTimeout: number
10
12
  }
11
13
 
12
14
  export class AutoDialer {
13
15
  private readonly components: Components
14
16
  private readonly enabled: boolean
15
17
  private readonly minConnections: number
18
+ private readonly dialTimeout: number
16
19
 
17
20
  constructor (components: Components, init: AutoDialerInit) {
18
21
  this.components = components
19
22
  this.enabled = init.enabled
20
23
  this.minConnections = init.minConnections
24
+ this.dialTimeout = init.dialTimeout
21
25
  }
22
26
 
23
27
  public handle (evt: CustomEvent<PeerInfo>) {
24
28
  const { detail: peer } = evt
25
29
 
30
+ if (!this.enabled) {
31
+ return
32
+ }
33
+
34
+ const connections = this.components.getConnectionManager().getConnections(peer.id)
35
+
26
36
  // If auto dialing is on and we have no connection to the peer, check if we should dial
27
- if (this.enabled && this.components.getConnectionManager().getConnection(peer.id) == null) {
37
+ if (connections.length === 0) {
28
38
  const minConnections = this.minConnections ?? 0
29
39
 
30
- if (minConnections > this.components.getConnectionManager().getConnectionList().length) {
31
- log('auto-dialing discovered peer %p', peer.id)
40
+ const allConnections = this.components.getConnectionManager().getConnections()
32
41
 
33
- void this.components.getDialer().dial(peer.id)
42
+ if (minConnections > allConnections.length) {
43
+ log('auto-dialing discovered peer %p with timeout %d', peer.id, this.dialTimeout)
44
+
45
+ const controller = new TimeoutController(this.dialTimeout)
46
+
47
+ void this.components.getConnectionManager().openConnection(peer.id, {
48
+ signal: controller.signal
49
+ })
34
50
  .catch(err => {
35
51
  log.error('could not connect to discovered peer %p with %o', peer.id, err)
36
52
  })
53
+ .finally(() => {
54
+ controller.clear()
55
+ })
37
56
  }
38
57
  }
39
58
  }
@@ -3,12 +3,12 @@ import { anySignal } from 'any-signal'
3
3
  import FIFO from 'p-fifo'
4
4
  // @ts-expect-error setMaxListeners is missing from the node 16 types
5
5
  import { setMaxListeners } from 'events'
6
- import { codes } from '../errors.js'
6
+ import { codes } from '../../errors.js'
7
7
  import { logger } from '@libp2p/logger'
8
8
  import type { Multiaddr } from '@multiformats/multiaddr'
9
9
  import type { Connection } from '@libp2p/interfaces/connection'
10
10
  import type { AbortOptions } from '@libp2p/interfaces'
11
- import type { DefaultDialer } from './index.js'
11
+ import type { Dialer } from './index.js'
12
12
 
13
13
  const log = logger('libp2p:dialer:dial-request')
14
14
 
@@ -19,12 +19,12 @@ export interface DialAction {
19
19
  export interface DialRequestOptions {
20
20
  addrs: Multiaddr[]
21
21
  dialAction: DialAction
22
- dialer: DefaultDialer
22
+ dialer: Dialer
23
23
  }
24
24
 
25
25
  export class DialRequest {
26
26
  private readonly addrs: Multiaddr[]
27
- private readonly dialer: DefaultDialer
27
+ private readonly dialer: Dialer
28
28
  private readonly dialAction: DialAction
29
29
 
30
30
  /**