libp2p 0.32.3 → 0.34.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 (74) hide show
  1. package/README.md +1 -1
  2. package/dist/src/circuit/index.d.ts +2 -2
  3. package/dist/src/circuit/index.d.ts.map +1 -1
  4. package/dist/src/circuit/transport.d.ts +1 -1
  5. package/dist/src/circuit/transport.d.ts.map +1 -1
  6. package/dist/src/config.d.ts +0 -1
  7. package/dist/src/content-routing/index.d.ts +4 -2
  8. package/dist/src/content-routing/index.d.ts.map +1 -1
  9. package/dist/src/dht/dht-content-routing.d.ts +30 -0
  10. package/dist/src/dht/dht-content-routing.d.ts.map +1 -0
  11. package/dist/src/dht/dht-peer-routing.d.ts +29 -0
  12. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -0
  13. package/dist/src/dialer/dial-request.d.ts +4 -4
  14. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  15. package/dist/src/dialer/index.d.ts +1 -2
  16. package/dist/src/dialer/index.d.ts.map +1 -1
  17. package/dist/src/errors.d.ts +2 -0
  18. package/dist/src/identify/index.d.ts +1 -1
  19. package/dist/src/identify/index.d.ts.map +1 -1
  20. package/dist/src/index.d.ts +4 -5
  21. package/dist/src/index.d.ts.map +1 -1
  22. package/dist/src/keychain/index.d.ts +1 -1
  23. package/dist/src/keychain/index.d.ts.map +1 -1
  24. package/dist/src/metrics/stats.d.ts +0 -1
  25. package/dist/src/metrics/stats.d.ts.map +1 -1
  26. package/dist/src/nat-manager.d.ts.map +1 -1
  27. package/dist/src/peer-routing.d.ts.map +1 -1
  28. package/dist/src/peer-store/address-book.d.ts +1 -1
  29. package/dist/src/peer-store/address-book.d.ts.map +1 -1
  30. package/dist/src/peer-store/index.d.ts +37 -24
  31. package/dist/src/peer-store/index.d.ts.map +1 -1
  32. package/dist/src/peer-store/metadata-book.d.ts +3 -2
  33. package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
  34. package/dist/src/peer-store/persistent/index.d.ts +1 -2
  35. package/dist/src/peer-store/persistent/index.d.ts.map +1 -1
  36. package/dist/src/record/envelope/index.d.ts +23 -0
  37. package/dist/src/record/envelope/index.d.ts.map +1 -1
  38. package/dist/src/record/peer-record/index.d.ts +6 -0
  39. package/dist/src/record/peer-record/index.d.ts.map +1 -1
  40. package/dist/src/registrar.d.ts +1 -1
  41. package/dist/src/registrar.d.ts.map +1 -1
  42. package/dist/src/upgrader.d.ts +1 -1
  43. package/dist/src/upgrader.d.ts.map +1 -1
  44. package/package.json +61 -54
  45. package/src/circuit/auto-relay.js +6 -6
  46. package/src/circuit/circuit/hop.js +2 -2
  47. package/src/circuit/circuit/stop.js +1 -1
  48. package/src/circuit/circuit/utils.js +2 -2
  49. package/src/circuit/index.js +1 -1
  50. package/src/circuit/transport.js +1 -1
  51. package/src/connection-manager/index.js +1 -1
  52. package/src/content-routing/index.js +35 -9
  53. package/src/dht/dht-content-routing.js +44 -0
  54. package/src/dht/dht-peer-routing.js +51 -0
  55. package/src/dialer/index.js +2 -2
  56. package/src/errors.js +3 -1
  57. package/src/get-peer.js +1 -1
  58. package/src/identify/index.js +9 -9
  59. package/src/index.js +7 -12
  60. package/src/insecure/plaintext.js +1 -1
  61. package/src/keychain/cms.js +4 -4
  62. package/src/keychain/index.js +12 -12
  63. package/src/nat-manager.js +2 -2
  64. package/src/peer-routing.js +3 -2
  65. package/src/peer-store/address-book.js +1 -1
  66. package/src/peer-store/metadata-book.js +3 -2
  67. package/src/peer-store/persistent/index.js +6 -6
  68. package/src/ping/index.js +1 -1
  69. package/src/pnet/crypto.js +3 -3
  70. package/src/pnet/key-generator.js +3 -3
  71. package/src/record/README.md +1 -1
  72. package/src/record/envelope/index.js +3 -3
  73. package/src/transport-manager.js +1 -1
  74. package/src/upgrader.js +9 -9
@@ -132,8 +132,8 @@ declare namespace Upgrader {
132
132
  export { MultiaddrConnection, MuxerFactory, Muxer, MuxedStream, Crypto, Connection, Multiaddr, CryptoResult };
133
133
  }
134
134
  import PeerId = require("peer-id");
135
- import { Connection } from "libp2p-interfaces/src/connection";
136
135
  type MultiaddrConnection = import('libp2p-interfaces/src/transport/types').MultiaddrConnection;
136
+ import { Connection } from "libp2p-interfaces/src/connection";
137
137
  type MuxerFactory = import('libp2p-interfaces/src/stream-muxer/types').MuxerFactory;
138
138
  type Muxer = import('libp2p-interfaces/src/stream-muxer/types').Muxer;
139
139
  type MuxedStream = import('libp2p-interfaces/src/stream-muxer/types').MuxedStream;
@@ -1 +1 @@
1
- {"version":3,"file":"upgrader.d.ts","sourceRoot":"","sources":["../../src/upgrader.js"],"names":[],"mappings":";AAiBA;;;;;;;;GAQG;AAEH;;;;;GAKG;AAEH;IACE;;;;;;;;OAQG;IACH;mBAPW,MAAM;;;;mCAIO,UAAU,KAAK,IAAI;sCACnB,UAAU,KAAK,IAAI;OAmB1C;IATC,kBAA0B;IAC1B,yCAAsB;IACtB,0EAAsB;IACtB,qFAAoB;IACpB,sCAAsC;IACtC,WADW,OAAO,QAAQ,CAAC,GAAG,IAAI,CACb;IACrB,yBAA0B;IAC1B,2BAlBsB,UAAU,KAAK,IAAI,CAkBT;IAChC,8BAlBsB,UAAU,KAAK,IAAI,CAkBH;IAGxC;;;;;;OAMG;IACH,uBAHW,mBAAmB,GACjB,QAAQ,UAAU,CAAC,CA6D/B;IAED;;;;;;OAMG;IACH,wBAHW,mBAAmB,GACjB,QAAQ,UAAU,CAAC,CAoE/B;IAED;;;;;;;;;;;;OAYG;IACH,0BA8GC;IAED;;;;;;;;OAQG;IACH,kBAGC;IAED;;;;;;;;;OASG;IACH,wBAqBC;IAED;;;;;;;;;;;OAWG;IACH,yBAqBC;IAED;;;;;;;;;OASG;IACH,2BAYC;IAED;;;;;;;;;OASG;IACH,0BAWC;CACF;;;;;;2BAzbY,OAAO,uCAAuC,EAAE,mBAAmB;oBACnE,OAAO,0CAA0C,EAAE,YAAY;aAC/D,OAAO,0CAA0C,EAAE,KAAK;mBACxD,OAAO,0CAA0C,EAAE,WAAW;cAC9D,OAAO,oCAAoC,EAAE,MAAM;;iBAEnD,OAAO,WAAW,EAAE,SAAS;;;;;UAK5B,mBAAmB;gBACnB,MAAM;cACN,MAAM"}
1
+ {"version":3,"file":"upgrader.d.ts","sourceRoot":"","sources":["../../src/upgrader.js"],"names":[],"mappings":";AAiBA;;;;;;;;GAQG;AAEH;;;;;GAKG;AAEH;IACE;;;;;;;;OAQG;IACH;mBAPW,MAAM;;;;mCAIO,UAAU,KAAK,IAAI;sCACnB,UAAU,KAAK,IAAI;OAmB1C;IATC,kBAA0B;IAC1B,yCAAsB;IACtB,0EAAsB;IACtB,qFAAoB;IACpB,sCAAsC;IACtC,WADW,OAAO,QAAQ,CAAC,GAAG,IAAI,CACb;IACrB,yBAA0B;IAC1B,2BAlBsB,UAAU,KAAK,IAAI,CAkBT;IAChC,8BAlBsB,UAAU,KAAK,IAAI,CAkBH;IAGxC;;;;;;OAMG;IACH,uBAHW,mBAAmB,GACjB,QAAQ,UAAU,CAAC,CA6D/B;IAED;;;;;;OAMG;IACH,wBAHW,mBAAmB,GACjB,QAAQ,UAAU,CAAC,CAoE/B;IAED;;;;;;;;;;;;OAYG;IACH,0BA8GC;IAED;;;;;;;;OAQG;IACH,kBAGC;IAED;;;;;;;;;OASG;IACH,wBAqBC;IAED;;;;;;;;;;;OAWG;IACH,yBAqBC;IAED;;;;;;;;;OASG;IACH,2BAYC;IAED;;;;;;;;;OASG;IACH,0BAWC;CACF;;;;;2BAzbY,OAAO,uCAAuC,EAAE,mBAAmB;;oBACnE,OAAO,0CAA0C,EAAE,YAAY;aAC/D,OAAO,0CAA0C,EAAE,KAAK;mBACxD,OAAO,0CAA0C,EAAE,WAAW;cAC9D,OAAO,oCAAoC,EAAE,MAAM;;iBAEnD,OAAO,WAAW,EAAE,SAAS;;;;;UAK5B,mBAAmB;gBACnB,MAAM;cACN,MAAM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2p",
3
- "version": "0.32.3",
3
+ "version": "0.34.0",
4
4
  "description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
5
5
  "leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
6
6
  "main": "src/index.js",
@@ -41,6 +41,7 @@
41
41
  "test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"",
42
42
  "test:browser": "aegir test -t browser",
43
43
  "test:examples": "cd examples && npm run test:all",
44
+ "test:interop": "LIBP2P_JS=$PWD npx aegir test -t node -f ./node_modules/libp2p-interop/test/*",
44
45
  "prepare": "aegir build --no-bundle",
45
46
  "release": "aegir release -t node -t browser",
46
47
  "release-minor": "aegir release --type minor -t node -t browser",
@@ -74,10 +75,12 @@
74
75
  "extends": "ipfs",
75
76
  "ignorePatterns": [
76
77
  "!.aegir.js",
77
- "test/ts-use"
78
+ "test/ts-use",
79
+ "*.d.ts"
78
80
  ]
79
81
  },
80
82
  "dependencies": {
83
+ "abortable-iterator": "^3.0.0",
81
84
  "@motrix/nat-api": "^0.3.1",
82
85
  "@vascosantos/moving-average": "^1.1.0",
83
86
  "abort-controller": "^3.0.0",
@@ -87,10 +90,10 @@
87
90
  "class-is": "^1.1.0",
88
91
  "debug": "^4.3.1",
89
92
  "err-code": "^3.0.0",
90
- "es6-promisify": "^6.1.1",
93
+ "es6-promisify": "^7.0.0",
91
94
  "events": "^3.3.0",
92
95
  "hashlru": "^2.3.0",
93
- "interface-datastore": "^5.1.1",
96
+ "interface-datastore": "^6.0.2",
94
97
  "it-all": "^1.0.4",
95
98
  "it-buffer": "^0.1.2",
96
99
  "it-drain": "^1.0.3",
@@ -124,24 +127,24 @@
124
127
  "set-delayed-interval": "^1.0.0",
125
128
  "streaming-iterables": "^6.0.0",
126
129
  "timeout-abort-controller": "^1.1.1",
127
- "uint8arrays": "^2.1.3",
130
+ "uint8arrays": "^3.0.0",
128
131
  "varint": "^6.0.0",
129
132
  "wherearewe": "^1.0.0",
130
133
  "xsalsa20": "^1.1.0"
131
134
  },
132
135
  "devDependencies": {
136
+ "@chainsafe/libp2p-noise": "^4.0.0",
133
137
  "@nodeutils/defaults-deep": "^1.1.0",
134
138
  "@types/es6-promisify": "^6.0.0",
135
139
  "@types/node": "^16.0.1",
136
140
  "@types/node-forge": "^0.10.1",
137
141
  "@types/varint": "^6.0.0",
138
- "abortable-iterator": "^3.0.0",
139
- "aegir": "^33.1.1",
142
+ "aegir": "^36.0.0",
140
143
  "buffer": "^6.0.3",
144
+ "datastore-core": "^6.0.7",
141
145
  "delay": "^5.0.0",
142
- "interop-libp2p": "^0.4.0",
143
- "into-stream": "^6.0.0",
144
- "ipfs-http-client": "^50.1.1",
146
+ "into-stream": "^7.0.0",
147
+ "ipfs-http-client": "^52.0.2",
145
148
  "it-concat": "^2.0.0",
146
149
  "it-pair": "^1.0.0",
147
150
  "it-pushable": "^1.4.0",
@@ -150,16 +153,15 @@
150
153
  "libp2p-delegated-content-routing": "^0.11.0",
151
154
  "libp2p-delegated-peer-routing": "^0.10.0",
152
155
  "libp2p-floodsub": "^0.27.0",
153
- "libp2p-gossipsub": "^0.10.0",
156
+ "libp2p-gossipsub": "^0.11.0",
154
157
  "libp2p-interfaces-compliance-tests": "^1.0.0",
155
- "libp2p-kad-dht": "^0.23.0",
158
+ "libp2p-interop": "^0.5.0",
159
+ "libp2p-kad-dht": "^0.26.5",
156
160
  "libp2p-mdns": "^0.17.0",
157
161
  "libp2p-mplex": "^0.10.1",
158
- "libp2p-noise": "^4.0.0",
159
162
  "libp2p-tcp": "^0.17.0",
160
163
  "libp2p-webrtc-star": "^0.23.0",
161
164
  "libp2p-websockets": "^0.16.0",
162
- "multihashes": "^4.0.2",
163
165
  "nock": "^13.0.3",
164
166
  "p-defer": "^3.0.0",
165
167
  "p-times": "^3.0.0",
@@ -179,67 +181,72 @@
179
181
  "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
180
182
  "Maciej Krüger <mkg20001@gmail.com>",
181
183
  "Hugo Dias <mail@hugodias.me>",
182
- "Volker Mische <volker.mische@gmail.com>",
183
184
  "Chris Dostert <chrisdostert@users.noreply.github.com>",
184
185
  "dirkmc <dirkmdev@gmail.com>",
185
- "Richard Littauer <richard.littauer@gmail.com>",
186
+ "Volker Mische <volker.mische@gmail.com>",
186
187
  "zeim839 <50573884+zeim839@users.noreply.github.com>",
187
- "Ryan Bell <ryan@piing.net>",
188
+ "Richard Littauer <richard.littauer@gmail.com>",
188
189
  "a1300 <matthias-knopp@gmx.net>",
190
+ "Ryan Bell <ryan@piing.net>",
191
+ "Elven <mon.samuel@qq.com>",
189
192
  "ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",
193
+ "Thomas Eizinger <thomas@eizinger.io>",
194
+ "Robert Kiel <robert.kiel@hoprnet.org>",
190
195
  "Andrew Nesbitt <andrewnez@gmail.com>",
191
- "Elven <mon.samuel@qq.com>",
192
- "Franck Royer <franck@royer.one>",
193
- "Giovanni T. Parra <fiatjaf@gmail.com>",
194
196
  "Samlior <samlior@foxmail.com>",
195
- "Thomas Eizinger <thomas@eizinger.io>",
196
197
  "acolytec3 <17355484+acolytec3@users.noreply.github.com>",
197
- "Didrik Nordström <didrik@betamos.se>",
198
- "Irakli Gozalishvili <rfobic@gmail.com>",
199
- "Joel Gustafson <joelg@mit.edu>",
200
- "John Rees <johnrees@users.noreply.github.com>",
201
- "João Santos <joaosantos15@users.noreply.github.com>",
202
- "Julien Bouquillon <contact@revolunet.com>",
203
- "Kevin Kwok <antimatter15@gmail.com>",
204
- "Kevin Lacker <lacker@gmail.com>",
205
- "Lars Gierth <lgierth@users.noreply.github.com>",
206
- "Ethan Lam <elmemphis2000@gmail.com>",
207
- "Marcin Tojek <mtojek@users.noreply.github.com>",
208
- "Michael Burns <5170+mburns@users.noreply.github.com>",
209
- "Miguel Mota <miguelmota2@gmail.com>",
210
- "Nuno Nogueira <nunofmn@gmail.com>",
211
- "Dmitriy Ryajov <dryajov@gmail.com>",
212
- "Philipp Muens <raute1337@gmx.de>",
198
+ "Franck Royer <franck@royer.one>",
199
+ "Giovanni T. Parra <fiatjaf@gmail.com>",
200
+ "Didrik Nordström <didrik.nordstrom@gmail.com>",
213
201
  "RasmusErik Voel Jensen <github@solsort.com>",
214
- "Diogo Silva <fsdiogo@gmail.com>",
215
- "Robert Kiel <robert.kiel@hoprnet.org>",
216
- "phillmac <phillmac@users.noreply.github.com>",
217
- "robertkiel <robert.kiel@validitylabs.org>",
218
202
  "Smite Chow <xiaopengyou@live.com>",
219
203
  "Soeren <nikorpoulsen@gmail.com>",
220
204
  "Sönke Hahn <soenkehahn@gmail.com>",
221
205
  "TJKoury <TJKoury@gmail.com>",
222
- "shresthagrawal <34920931+shresthagrawal@users.noreply.github.com>",
206
+ "TheStarBoys <41286328+TheStarBoys@users.noreply.github.com>",
223
207
  "Tiago Alves <alvesjtiago@gmail.com>",
224
- "Daijiro Wachi <daijiro.wachi@gmail.com>",
208
+ "XiaoZhang <zxinmyth@gmail.com>",
225
209
  "Yusef Napora <yusef@napora.org>",
226
210
  "Zane Starr <zcstarr@gmail.com>",
227
- "swedneck <40505480+swedneck@users.noreply.github.com>",
228
- "Aleksei <vozhdb@gmail.com>",
229
- "Cindy Wu <ciindy.wu@gmail.com>",
230
- "Aditya Bose <13054902+adbose@users.noreply.github.com>",
231
- "Chris Bratlien <chrisbratlien@gmail.com>",
232
211
  "ebinks <elizabethjbinks@gmail.com>",
212
+ "Aditya Bose <13054902+adbose@users.noreply.github.com>",
213
+ "isan_rivkin <isanrivkin@gmail.com>",
214
+ "mayerwin <mayerwin@users.noreply.github.com>",
215
+ "mcclure <andi.m.mcclure@gmail.com>",
216
+ "patrickwoodhead <91056047+patrickwoodhead@users.noreply.github.com>",
217
+ "phillmac <phillmac@users.noreply.github.com>",
218
+ "robertkiel <robert.kiel@validitylabs.org>",
219
+ "shresthagrawal <34920931+shresthagrawal@users.noreply.github.com>",
220
+ "swedneck <40505480+swedneck@users.noreply.github.com>",
233
221
  "greenSnot <greenSnot@users.noreply.github.com>",
222
+ "Alan Smithee <ggnore.alan.smithee@gmail.com>",
223
+ "Aleksei <vozhdb@gmail.com>",
234
224
  "Bernd Strehl <bernd.strehl@gmail.com>",
235
- "Francis Gulotta <wizard@roborooter.com>",
236
- "isan_rivkin <isanrivkin@gmail.com>",
225
+ "Chris Bratlien <chrisbratlien@gmail.com>",
226
+ "Cindy Wu <ciindy.wu@gmail.com>",
227
+ "Daijiro Wachi <daijiro.wachi@gmail.com>",
228
+ "Diogo Silva <fsdiogo@gmail.com>",
229
+ "Dmitriy Ryajov <dryajov@gmail.com>",
230
+ "Ethan Lam <elmemphis2000@gmail.com>",
231
+ "Fei Liu <liu.feiwood@gmail.com>",
232
+ "Felipe Martins <felipebrasil93@gmail.com>",
237
233
  "Florian-Merle <florian.david.merle@gmail.com>",
238
- "mayerwin <mayerwin@users.noreply.github.com>",
234
+ "Francis Gulotta <wizard@roborooter.com>",
239
235
  "Guy Sviry <32539816+guysv@users.noreply.github.com>",
240
236
  "Henrique Dias <hacdias@gmail.com>",
241
- "mcclure <andi.m.mcclure@gmail.com>",
242
- "Felipe Martins <felipebrasil93@gmail.com>",
243
- "Fei Liu <liu.feiwood@gmail.com>"
237
+ "Irakli Gozalishvili <rfobic@gmail.com>",
238
+ "Joel Gustafson <joelg@mit.edu>",
239
+ "John Rees <johnrees@users.noreply.github.com>",
240
+ "João Santos <joaosantos15@users.noreply.github.com>",
241
+ "Julien Bouquillon <contact@revolunet.com>",
242
+ "Kevin Kwok <antimatter15@gmail.com>",
243
+ "Kevin Lacker <lacker@gmail.com>",
244
+ "Lars Gierth <lgierth@users.noreply.github.com>",
245
+ "Leask Wong <i@leaskh.com>",
246
+ "Marcin Tojek <mtojek@users.noreply.github.com>",
247
+ "Michael Burns <5170+mburns@users.noreply.github.com>",
248
+ "Miguel Mota <miguelmota2@gmail.com>",
249
+ "Nuno Nogueira <nunofmn@gmail.com>",
250
+ "Philipp Muens <raute1337@gmx.de>"
244
251
  ]
245
252
  }
@@ -5,8 +5,8 @@ const log = Object.assign(debug('libp2p:auto-relay'), {
5
5
  error: debug('libp2p:auto-relay:err')
6
6
  })
7
7
 
8
- const uint8ArrayFromString = require('uint8arrays/from-string')
9
- const uint8ArrayToString = require('uint8arrays/to-string')
8
+ const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
9
+ const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
10
10
  const { Multiaddr } = require('multiaddr')
11
11
  const PeerId = require('peer-id')
12
12
 
@@ -116,7 +116,7 @@ class AutoRelay {
116
116
  this._peerStore.metadataBook.set(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE))
117
117
  await this._addListenRelay(connection, id)
118
118
  }
119
- } catch (err) {
119
+ } catch (/** @type {any} */ err) {
120
120
  this._onError(err)
121
121
  }
122
122
  }
@@ -169,7 +169,7 @@ class AutoRelay {
169
169
  try {
170
170
  await this._transportManager.listen([new Multiaddr(listenAddr)])
171
171
  // Announce multiaddrs will update on listen success by TransportManager event being triggered
172
- } catch (err) {
172
+ } catch (/** @type {any} */ err) {
173
173
  this._onError(err)
174
174
  this._listenRelays.delete(id)
175
175
  }
@@ -267,7 +267,7 @@ class AutoRelay {
267
267
  return
268
268
  }
269
269
  }
270
- } catch (err) {
270
+ } catch (/** @type {any} */ err) {
271
271
  this._onError(err)
272
272
  }
273
273
  }
@@ -279,7 +279,7 @@ class AutoRelay {
279
279
  try {
280
280
  const connection = await this._libp2p.dial(peerId)
281
281
  await this._addListenRelay(connection, peerId.toB58String())
282
- } catch (err) {
282
+ } catch (/** @type {any} */ err) {
283
283
  this._onError(err, `could not connect and listen on known hop relay ${peerId.toB58String()}`)
284
284
  }
285
285
  }
@@ -54,7 +54,7 @@ async function handleHop ({
54
54
  // Validate the HOP request has the required input
55
55
  try {
56
56
  validateAddrs(request, streamHandler)
57
- } catch (err) {
57
+ } catch (/** @type {any} */ err) {
58
58
  return log.error('invalid hop request via peer %s', connection.remotePeer.toB58String(), err)
59
59
  }
60
60
 
@@ -93,7 +93,7 @@ async function handleHop ({
93
93
  connection: destinationConnection,
94
94
  request: stopRequest
95
95
  })
96
- } catch (err) {
96
+ } catch (/** @type {any} */ err) {
97
97
  return log.error(err)
98
98
  }
99
99
 
@@ -34,7 +34,7 @@ module.exports.handleStop = function handleStop ({
34
34
  // Validate the STOP request has the required input
35
35
  try {
36
36
  validateAddrs(request, streamHandler)
37
- } catch (err) {
37
+ } catch (/** @type {any} */ err) {
38
38
  return log.error('invalid stop request via peer %s', connection.remotePeer.toB58String(), err)
39
39
  }
40
40
 
@@ -34,7 +34,7 @@ function validateAddrs (msg, streamHandler) {
34
34
  return new Multiaddr(addr)
35
35
  })
36
36
  }
37
- } catch (err) {
37
+ } catch (/** @type {any} */ err) {
38
38
  writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
39
39
  ? CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
40
40
  : CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID)
@@ -47,7 +47,7 @@ function validateAddrs (msg, streamHandler) {
47
47
  return new Multiaddr(addr)
48
48
  })
49
49
  }
50
- } catch (err) {
50
+ } catch (/** @type {any} */ err) {
51
51
  writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
52
52
  ? CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID
53
53
  : CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID)
@@ -87,7 +87,7 @@ class Relay {
87
87
  try {
88
88
  const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
89
89
  await this._libp2p.contentRouting.provide(cid)
90
- } catch (err) {
90
+ } catch (/** @type {any} */ err) {
91
91
  if (err.code === 'NO_ROUTERS_AVAILABLE') {
92
92
  log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err)
93
93
  // Stop the advertise
@@ -171,7 +171,7 @@ class Circuit {
171
171
  log('new outbound connection %s', maConn.remoteAddr)
172
172
 
173
173
  return this._upgrader.upgradeOutbound(maConn)
174
- } catch (err) {
174
+ } catch (/** @type {any} */ err) {
175
175
  log.error('Circuit relay dial failed', err)
176
176
  disconnectOnFailure && await relayConnection.close()
177
177
  throw err
@@ -350,7 +350,7 @@ class ConnectionManager extends EventEmitter {
350
350
  if (!this._started) {
351
351
  return
352
352
  }
353
- } catch (err) {
353
+ } catch (/** @type {any} */ err) {
354
354
  log.error('could not connect to peerStore stored peer', err)
355
355
  }
356
356
  }
@@ -8,9 +8,10 @@ const {
8
8
  requirePeers,
9
9
  maybeLimitSource
10
10
  } = require('./utils')
11
-
11
+ const drain = require('it-drain')
12
12
  const merge = require('it-merge')
13
13
  const { pipe } = require('it-pipe')
14
+ const { DHTContentRouting } = require('../dht/dht-content-routing')
14
15
 
15
16
  /**
16
17
  * @typedef {import('peer-id')} PeerId
@@ -38,7 +39,7 @@ class ContentRouting {
38
39
 
39
40
  // If we have the dht, add it to the available content routers
40
41
  if (this.dht && libp2p._config.dht.enabled) {
41
- this.routers.push(this.dht)
42
+ this.routers.push(new DHTContentRouting(this.dht))
42
43
  }
43
44
  }
44
45
 
@@ -91,12 +92,12 @@ class ContentRouting {
91
92
  * @param {number} [options.minPeers] - minimum number of peers required to successfully put
92
93
  * @returns {Promise<void>}
93
94
  */
94
- put (key, value, options) {
95
+ async put (key, value, options) {
95
96
  if (!this.libp2p.isStarted() || !this.dht.isStarted) {
96
97
  throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
97
98
  }
98
99
 
99
- return this.dht.put(key, value, options)
100
+ await drain(this.dht.put(key, value, options))
100
101
  }
101
102
 
102
103
  /**
@@ -108,12 +109,18 @@ class ContentRouting {
108
109
  * @param {number} [options.timeout] - optional timeout (default: 60000)
109
110
  * @returns {Promise<GetData>}
110
111
  */
111
- get (key, options) {
112
+ async get (key, options) {
112
113
  if (!this.libp2p.isStarted() || !this.dht.isStarted) {
113
114
  throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
114
115
  }
115
116
 
116
- return this.dht.get(key, options)
117
+ for await (const event of this.dht.get(key, options)) {
118
+ if (event.name === 'VALUE') {
119
+ return { from: event.peerId, val: event.value }
120
+ }
121
+ }
122
+
123
+ throw errCode(new Error(messages.NOT_FOUND), codes.ERR_NOT_FOUND)
117
124
  }
118
125
 
119
126
  /**
@@ -123,14 +130,33 @@ class ContentRouting {
123
130
  * @param {number} nVals
124
131
  * @param {Object} [options] - get options
125
132
  * @param {number} [options.timeout] - optional timeout (default: 60000)
126
- * @returns {Promise<GetData[]>}
127
133
  */
128
- async getMany (key, nVals, options) { // eslint-disable-line require-await
134
+ async * getMany (key, nVals, options) { // eslint-disable-line require-await
129
135
  if (!this.libp2p.isStarted() || !this.dht.isStarted) {
130
136
  throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
131
137
  }
132
138
 
133
- return this.dht.getMany(key, nVals, options)
139
+ if (!nVals) {
140
+ return
141
+ }
142
+
143
+ let gotValues = 0
144
+
145
+ for await (const event of this.dht.get(key, options)) {
146
+ if (event.name === 'VALUE') {
147
+ yield { from: event.peerId, val: event.value }
148
+
149
+ gotValues++
150
+
151
+ if (gotValues === nVals) {
152
+ break
153
+ }
154
+ }
155
+ }
156
+
157
+ if (gotValues === 0) {
158
+ throw errCode(new Error(messages.NOT_FOUND), codes.ERR_NOT_FOUND)
159
+ }
134
160
  }
135
161
  }
136
162
 
@@ -0,0 +1,44 @@
1
+ 'use strict'
2
+
3
+ const drain = require('it-drain')
4
+
5
+ /**
6
+ * @typedef {import('peer-id')} PeerId
7
+ * @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule
8
+ * @typedef {import('multiformats/cid').CID} CID
9
+ */
10
+
11
+ /**
12
+ * Wrapper class to convert events into returned values
13
+ *
14
+ * @implements {ContentRoutingModule}
15
+ */
16
+ class DHTContentRouting {
17
+ /**
18
+ * @param {import('libp2p-kad-dht').DHT} dht
19
+ */
20
+ constructor (dht) {
21
+ this._dht = dht
22
+ }
23
+
24
+ /**
25
+ * @param {CID} cid
26
+ */
27
+ async provide (cid) {
28
+ await drain(this._dht.provide(cid))
29
+ }
30
+
31
+ /**
32
+ * @param {CID} cid
33
+ * @param {*} options
34
+ */
35
+ async * findProviders (cid, options) {
36
+ for await (const event of this._dht.findProviders(cid, options)) {
37
+ if (event.name === 'PROVIDER') {
38
+ yield * event.providers
39
+ }
40
+ }
41
+ }
42
+ }
43
+
44
+ module.exports = { DHTContentRouting }
@@ -0,0 +1,51 @@
1
+ 'use strict'
2
+
3
+ const errCode = require('err-code')
4
+ const { messages, codes } = require('../errors')
5
+
6
+ /**
7
+ * @typedef {import('peer-id')} PeerId
8
+ * @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingModule
9
+ */
10
+
11
+ /**
12
+ * Wrapper class to convert events into returned values
13
+ *
14
+ * @implements {PeerRoutingModule}
15
+ */
16
+ class DHTPeerRouting {
17
+ /**
18
+ * @param {import('libp2p-kad-dht').DHT} dht
19
+ */
20
+ constructor (dht) {
21
+ this._dht = dht
22
+ }
23
+
24
+ /**
25
+ * @param {PeerId} peerId
26
+ * @param {any} options
27
+ */
28
+ async findPeer (peerId, options = {}) {
29
+ for await (const event of this._dht.findPeer(peerId, options)) {
30
+ if (event.name === 'FINAL_PEER') {
31
+ return event.peer
32
+ }
33
+ }
34
+
35
+ throw errCode(new Error(messages.NOT_FOUND), codes.ERR_NOT_FOUND)
36
+ }
37
+
38
+ /**
39
+ * @param {Uint8Array} key
40
+ * @param {any} options
41
+ */
42
+ async * getClosestPeers (key, options = {}) {
43
+ for await (const event of this._dht.getClosestPeers(key, options)) {
44
+ if (event.name === 'PEER_RESPONSE') {
45
+ yield * event.closer
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ module.exports = { DHTPeerRouting }
@@ -95,7 +95,7 @@ class Dialer {
95
95
  for (const dial of this._pendingDials.values()) {
96
96
  try {
97
97
  dial.controller.abort()
98
- } catch (err) {
98
+ } catch (/** @type {any} */ err) {
99
99
  log.error(err)
100
100
  }
101
101
  }
@@ -129,7 +129,7 @@ class Dialer {
129
129
  const connection = await pendingDial.promise
130
130
  log('dial succeeded to %s', dialTarget.id)
131
131
  return connection
132
- } catch (err) {
132
+ } catch (/** @type {any} */ err) {
133
133
  // Error is a timeout
134
134
  if (pendingDial.controller.signal.aborted) {
135
135
  err.code = codes.ERR_TIMEOUT
package/src/errors.js CHANGED
@@ -3,7 +3,8 @@
3
3
  exports.messages = {
4
4
  NOT_STARTED_YET: 'The libp2p node is not started yet',
5
5
  DHT_DISABLED: 'DHT is not available',
6
- CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required'
6
+ CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required',
7
+ NOT_FOUND: 'Not found'
7
8
  }
8
9
 
9
10
  exports.codes = {
@@ -29,6 +30,7 @@ exports.codes = {
29
30
  ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS',
30
31
  ERR_INVALID_PEER: 'ERR_INVALID_PEER',
31
32
  ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE',
33
+ ERR_NOT_FOUND: 'ERR_NOT_FOUND',
32
34
  ERR_TIMEOUT: 'ERR_TIMEOUT',
33
35
  ERR_TRANSPORT_UNAVAILABLE: 'ERR_TRANSPORT_UNAVAILABLE',
34
36
  ERR_TRANSPORT_DIAL_FAILED: 'ERR_TRANSPORT_DIAL_FAILED',
package/src/get-peer.js CHANGED
@@ -32,7 +32,7 @@ function getPeer (peer) {
32
32
 
33
33
  try {
34
34
  peer = PeerId.createFromB58String(idStr)
35
- } catch (err) {
35
+ } catch (/** @type {any} */ err) {
36
36
  throw errCode(
37
37
  new Error(`${peer} is not a valid peer type`),
38
38
  codes.ERR_INVALID_MULTIADDR
@@ -8,7 +8,7 @@ const errCode = require('err-code')
8
8
  const lp = require('it-length-prefixed')
9
9
  const { pipe } = require('it-pipe')
10
10
  const { collect, take, consume } = require('streaming-iterables')
11
- const uint8ArrayFromString = require('uint8arrays/from-string')
11
+ const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
12
12
 
13
13
  const PeerId = require('peer-id')
14
14
  const { Multiaddr } = require('multiaddr')
@@ -124,7 +124,7 @@ class IdentifyService {
124
124
  stream,
125
125
  consume
126
126
  )
127
- } catch (err) {
127
+ } catch (/** @type {any} */ err) {
128
128
  // Just log errors
129
129
  log.error('could not push identify update to peer', err)
130
130
  }
@@ -182,7 +182,7 @@ class IdentifyService {
182
182
  let message
183
183
  try {
184
184
  message = Message.Identify.decode(data)
185
- } catch (err) {
185
+ } catch (/** @type {any} */ err) {
186
186
  throw errCode(err, codes.ERR_INVALID_MESSAGE)
187
187
  }
188
188
 
@@ -211,14 +211,14 @@ class IdentifyService {
211
211
  this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
212
212
  return
213
213
  }
214
- } catch (err) {
214
+ } catch (/** @type {any} */ err) {
215
215
  log('received invalid envelope, discard it and fallback to listenAddrs is available', err)
216
216
  }
217
217
 
218
218
  // LEGACY: Update peers data in PeerStore
219
219
  try {
220
220
  this.peerStore.addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
221
- } catch (err) {
221
+ } catch (/** @type {any} */ err) {
222
222
  log.error('received invalid addrs', err)
223
223
  }
224
224
 
@@ -287,7 +287,7 @@ class IdentifyService {
287
287
  stream,
288
288
  consume
289
289
  )
290
- } catch (err) {
290
+ } catch (/** @type {any} */ err) {
291
291
  log.error('could not respond to identify request', err)
292
292
  }
293
293
  }
@@ -313,7 +313,7 @@ class IdentifyService {
313
313
  collect
314
314
  )
315
315
  message = Message.Identify.decode(data)
316
- } catch (err) {
316
+ } catch (/** @type {any} */ err) {
317
317
  return log.error('received invalid message', err)
318
318
  }
319
319
 
@@ -325,7 +325,7 @@ class IdentifyService {
325
325
  this.peerStore.protoBook.set(id, message.protocols)
326
326
  return
327
327
  }
328
- } catch (err) {
328
+ } catch (/** @type {any} */ err) {
329
329
  log('received invalid envelope, discard it and fallback to listenAddrs is available', err)
330
330
  }
331
331
 
@@ -333,7 +333,7 @@ class IdentifyService {
333
333
  try {
334
334
  this.peerStore.addressBook.set(id,
335
335
  message.listenAddrs.map((addr) => new Multiaddr(addr)))
336
- } catch (err) {
336
+ } catch (/** @type {any} */ err) {
337
337
  log.error('received invalid addrs', err)
338
338
  }
339
339