From 2188efedccb1e8740c84b507d02a2421d5908937 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 12 Oct 2021 06:49:27 +0100 Subject: [PATCH 01/22] feat: update dht Changes dht creation to use factory function. --- src/index.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 3bd1fbfe77..a7a86bbe21 100644 --- a/src/index.js +++ b/src/index.js @@ -302,13 +302,8 @@ class Libp2p extends EventEmitter { if (this._modules.dht) { const DHT = this._modules.dht // @ts-ignore Object is not constructable - this._dht = new DHT({ + this._dht = DHT.create({ libp2p: this, - dialer: this.dialer, - peerId: this.peerId, - peerStore: this.peerStore, - registrar: this.registrar, - datastore: this.datastore, ...this._config.dht }) } From 911772fe02887ed8eaac4aa5a17e792887c5f6b8 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 8 Nov 2021 13:24:37 +0000 Subject: [PATCH 02/22] chore: use dht branch --- package.json | 2 +- src/nat-manager.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1c653f2996..dd4bfac804 100644 --- a/package.json +++ b/package.json @@ -154,7 +154,7 @@ "libp2p-floodsub": "^0.27.0", "libp2p-gossipsub": "^0.11.0", "libp2p-interfaces-compliance-tests": "^1.0.0", - "libp2p-kad-dht": "^0.24.2", + "libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#fix/refactor-query-logic", "libp2p-mdns": "^0.17.0", "libp2p-mplex": "^0.10.1", "libp2p-tcp": "^0.17.0", diff --git a/src/nat-manager.js b/src/nat-manager.js index a53e152d15..0315788468 100644 --- a/src/nat-manager.js +++ b/src/nat-manager.js @@ -10,7 +10,6 @@ const log = Object.assign(debug('libp2p:nat'), { }) const { isBrowser } = require('wherearewe') const retry = require('p-retry') -// @ts-ignore private-api does not export types const isPrivateIp = require('private-ip') const pkg = require('../package.json') const errcode = require('err-code') @@ -115,6 +114,7 @@ class NatManager { const client = this._getClient() const publicIp = this._externalIp || await client.externalIp() + // @ts-expect-error types are wrong if (isPrivateIp(publicIp)) { throw new Error(`${publicIp} is private - please set config.nat.externalIp to an externally routable IP or ensure you are not behind a double NAT`) } From f62745f0c19707d4b46fe48b68b4a08b26818273 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 8 Nov 2021 15:53:54 +0000 Subject: [PATCH 03/22] chore: fix up content routing --- src/content-routing/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/content-routing/index.js b/src/content-routing/index.js index 7fc4b4fb5c..e0e9508c17 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -8,7 +8,7 @@ const { requirePeers, maybeLimitSource } = require('./utils') - +const drain = require('it-drain') const merge = require('it-merge') const { pipe } = require('it-pipe') @@ -38,7 +38,18 @@ class ContentRouting { // If we have the dht, add it to the available content routers if (this.dht && libp2p._config.dht.enabled) { - this.routers.push(this.dht) + this.routers.push({ + provide: async (cid, options) => { + await drain(this.dht.provide(cid, options)) + }, + findProviders: async function * (cid, options) { + for await (const event of this.dht.findProviders(cid, options)) { + if (event.name === 'provider') { + yield * event.providers + } + } + } + }) } } From 020c73e06e153abac2a97a1d621e357639845490 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 8 Nov 2021 15:58:22 +0000 Subject: [PATCH 04/22] chore: fix types and references --- src/content-routing/index.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/content-routing/index.js b/src/content-routing/index.js index e0e9508c17..ca65457642 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -38,12 +38,22 @@ class ContentRouting { // If we have the dht, add it to the available content routers if (this.dht && libp2p._config.dht.enabled) { + const dht = this.dht + this.routers.push({ + /** + * @param {CID} cid + * @param {*} options + */ provide: async (cid, options) => { - await drain(this.dht.provide(cid, options)) + await drain(dht.provide(cid, options)) }, + /** + * @param {CID} cid + * @param {*} options + */ findProviders: async function * (cid, options) { - for await (const event of this.dht.findProviders(cid, options)) { + for await (const event of dht.findProviders(cid, options)) { if (event.name === 'provider') { yield * event.providers } From 42360a6589510b3bc47a5a8c39b77eb373711815 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 8 Nov 2021 16:01:26 +0000 Subject: [PATCH 05/22] chore: fix types and references --- src/content-routing/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content-routing/index.js b/src/content-routing/index.js index ca65457642..3cbf500561 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -45,8 +45,8 @@ class ContentRouting { * @param {CID} cid * @param {*} options */ - provide: async (cid, options) => { - await drain(dht.provide(cid, options)) + provide: async (cid) => { + await drain(dht.provide(cid)) }, /** * @param {CID} cid From 2aae8fb379dd1291901a4f023457f5b51769b5e0 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 8 Nov 2021 16:01:34 +0000 Subject: [PATCH 06/22] chore: fix types and references --- src/content-routing/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/content-routing/index.js b/src/content-routing/index.js index 3cbf500561..b2edff4b74 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -43,7 +43,6 @@ class ContentRouting { this.routers.push({ /** * @param {CID} cid - * @param {*} options */ provide: async (cid) => { await drain(dht.provide(cid)) From 00cef1fe0c5352fb3c6a567455745b373568c5cf Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 8 Nov 2021 18:27:42 +0000 Subject: [PATCH 07/22] chore: update to new dht api --- src/content-routing/index.js | 39 +++++++++++++++++++++++++++++------- src/errors.js | 4 +++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/content-routing/index.js b/src/content-routing/index.js index b2edff4b74..d0781469fb 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -111,12 +111,12 @@ class ContentRouting { * @param {number} [options.minPeers] - minimum number of peers required to successfully put * @returns {Promise} */ - put (key, value, options) { + async put (key, value, options) { if (!this.libp2p.isStarted() || !this.dht.isStarted) { throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) } - return this.dht.put(key, value, options) + await drain(this.dht.put(key, value, options)) } /** @@ -128,12 +128,18 @@ class ContentRouting { * @param {number} [options.timeout] - optional timeout (default: 60000) * @returns {Promise} */ - get (key, options) { + async get (key, options) { if (!this.libp2p.isStarted() || !this.dht.isStarted) { throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) } - return this.dht.get(key, options) + for await (const event of this.dht.get(key, options)) { + if (event.name === 'value') { + return { from: event.peerId, val: event.value } + } + } + + throw errCode(new Error(messages.NOT_FOUND), codes.NOT_FOUND) } /** @@ -143,14 +149,33 @@ class ContentRouting { * @param {number} nVals * @param {Object} [options] - get options * @param {number} [options.timeout] - optional timeout (default: 60000) - * @returns {Promise} */ - async getMany (key, nVals, options) { // eslint-disable-line require-await + async * getMany (key, nVals, options) { // eslint-disable-line require-await if (!this.libp2p.isStarted() || !this.dht.isStarted) { throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) } - return this.dht.getMany(key, nVals, options) + if (!nVals) { + return + } + + let gotValues = 0 + + for await (const event of this.dht.get(key, options)) { + if (event.name === 'value') { + yield { from: event.peerId, val: event.value } + + gotValues++ + + if (gotValues === nVals) { + break + } + } + } + + if (gotValues === 0) { + throw errCode(new Error(messages.NOT_FOUND), codes.NOT_FOUND) + } } } diff --git a/src/errors.js b/src/errors.js index 5b4d070fb2..efaed29c06 100644 --- a/src/errors.js +++ b/src/errors.js @@ -3,7 +3,8 @@ exports.messages = { NOT_STARTED_YET: 'The libp2p node is not started yet', DHT_DISABLED: 'DHT is not available', - CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required' + CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required', + NOT_FOUND: 'Not found' } exports.codes = { @@ -29,6 +30,7 @@ exports.codes = { ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS', ERR_INVALID_PEER: 'ERR_INVALID_PEER', ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE', + ERR_NOT_FOUND: 'ERR_NOT_FOUND', ERR_TIMEOUT: 'ERR_TIMEOUT', ERR_TRANSPORT_UNAVAILABLE: 'ERR_TRANSPORT_UNAVAILABLE', ERR_TRANSPORT_DIAL_FAILED: 'ERR_TRANSPORT_DIAL_FAILED', From 70925c24fe2e78672ee3bcdbf80c5358343db9ad Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 8 Nov 2021 18:32:24 +0000 Subject: [PATCH 08/22] chore: fix build --- src/content-routing/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content-routing/index.js b/src/content-routing/index.js index d0781469fb..38889371ba 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -139,7 +139,7 @@ class ContentRouting { } } - throw errCode(new Error(messages.NOT_FOUND), codes.NOT_FOUND) + throw errCode(new Error(messages.NOT_FOUND), codes.ERR_NOT_FOUND) } /** @@ -174,7 +174,7 @@ class ContentRouting { } if (gotValues === 0) { - throw errCode(new Error(messages.NOT_FOUND), codes.NOT_FOUND) + throw errCode(new Error(messages.NOT_FOUND), codes.ERR_NOT_FOUND) } } } From a61220402a5b02d7fdab5f02c6e6430fc057af55 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 17 Nov 2021 17:04:25 +0000 Subject: [PATCH 09/22] chore: fix interop tests --- .github/workflows/main.yml | 2 +- examples/delegated-routing/package.json | 2 +- examples/libp2p-in-the-browser/test.js | 2 +- examples/transports/3.js | 2 +- examples/transports/README.md | 4 +- examples/utils.js | 4 +- examples/webrtc-direct/test.js | 2 +- package.json | 8 ++-- src/circuit/auto-relay.js | 8 ++-- src/circuit/circuit/hop.js | 4 +- src/circuit/circuit/stop.js | 2 +- src/circuit/circuit/utils.js | 4 +- src/circuit/index.js | 2 +- src/circuit/transport.js | 2 +- src/connection-manager/index.js | 2 +- src/content-routing/index.js | 27 ++---------- src/dht/dht-content-routing.js | 43 ++++++++++++++++++ src/dht/dht-peer-routing.js | 46 ++++++++++++++++++++ src/dialer/index.js | 4 +- src/get-peer.js | 2 +- src/identify/index.js | 16 +++---- src/index.js | 10 ++--- src/insecure/plaintext.js | 2 +- src/keychain/cms.js | 4 +- src/keychain/index.js | 18 ++++---- src/nat-manager.js | 2 +- src/peer-routing.js | 5 ++- src/peer-store/address-book.js | 4 +- src/peer-store/metadata-book.js | 3 +- src/peer-store/persistent/index.js | 10 ++--- src/pnet/crypto.js | 2 +- src/pnet/key-generator.js | 2 +- src/transport-manager.js | 2 +- src/upgrader.js | 18 ++++---- test/content-routing/content-routing.node.js | 4 +- test/dialing/dial-request.spec.js | 6 +-- test/dialing/direct.node.js | 2 +- test/dialing/direct.spec.js | 2 +- test/keychain/keychain.spec.js | 8 ++-- test/peer-routing/peer-routing.node.js | 4 +- test/peer-store/address-book.spec.js | 18 ++++---- test/peer-store/key-book.spec.js | 4 +- test/peer-store/metadata-book.spec.js | 16 +++---- test/transports/transport-manager.spec.js | 2 +- test/ts-use/package.json | 2 +- test/utils/mockConnection.js | 4 +- 46 files changed, 208 insertions(+), 134 deletions(-) create mode 100644 src/dht/dht-content-routing.js create mode 100644 src/dht/dht-peer-routing.js diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 69cd85eb3d..518b12d3aa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -67,4 +67,4 @@ jobs: steps: - uses: actions/checkout@v2 - run: npm install - - run: cd node_modules/interop-libp2p && yarn && LIBP2P_JS=${GITHUB_WORKSPACE}/src/index.js npx aegir test -t node --bail -- --exit + - run: npm run test:interop -- --bail -- --exit diff --git a/examples/delegated-routing/package.json b/examples/delegated-routing/package.json index 164e0d3c54..6598bfd364 100644 --- a/examples/delegated-routing/package.json +++ b/examples/delegated-routing/package.json @@ -7,7 +7,7 @@ "libp2p": "github:libp2p/js-libp2p#master", "libp2p-delegated-content-routing": "~0.2.2", "libp2p-delegated-peer-routing": "~0.2.2", - "libp2p-kad-dht": "~0.14.12", + "libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#fix/refactor-query-logic", "libp2p-mplex": "~0.8.5", "libp2p-secio": "~0.11.1", "libp2p-webrtc-star": "~0.15.8", diff --git a/examples/libp2p-in-the-browser/test.js b/examples/libp2p-in-the-browser/test.js index 574e5739b2..97e65224bd 100644 --- a/examples/libp2p-in-the-browser/test.js +++ b/examples/libp2p-in-the-browser/test.js @@ -38,7 +38,7 @@ async function run() { ) await browser.close(); - } catch (err) { + } catch (/** @type {any} */ err) { console.error(err) process.exit(1) } finally { diff --git a/examples/transports/3.js b/examples/transports/3.js index 90fc1b1768..b3843b15af 100644 --- a/examples/transports/3.js +++ b/examples/transports/3.js @@ -81,7 +81,7 @@ function print ({ stream }) { // node 3 (listening WebSockets) can dial node 1 (TCP) try { await node3.dialProtocol(node1.peerId, '/print') - } catch (err) { + } catch (/** @type {any} */ err) { console.log('node 3 failed to dial to node 1 with:', err.message) } })(); diff --git a/examples/transports/README.md b/examples/transports/README.md index 8c9d23b908..bf2fcf5ce6 100644 --- a/examples/transports/README.md +++ b/examples/transports/README.md @@ -91,7 +91,7 @@ const concat = require('it-concat') const MPLEX = require('libp2p-mplex') ``` -We are going to reuse the `createNode` function from step 1, but this time add a stream multiplexer from `libp2p-mplex`. +We are going to reuse the `createNode` function from step 1, but this time add a stream multiplexer from `libp2p-mplex`. ```js const createNode = async () => { const node = await Libp2p.create({ @@ -245,7 +245,7 @@ await pipe( // node 3 (WebSockets) attempts to dial to node 1 (TCP) try { await node3.dialProtocol(node1.peerId, '/print') -} catch (err) { +} catch (/** @type {any} */ err) { console.log('node 3 failed to dial to node 1 with:', err.message) } ``` diff --git a/examples/utils.js b/examples/utils.js index aec6df5418..95b509ceb4 100644 --- a/examples/utils.js +++ b/examples/utils.js @@ -9,7 +9,7 @@ async function isExecutable (command) { await fs.access(command, fs.constants.X_OK) return true - } catch (err) { + } catch (/** @type {any} */ err) { if (err.code === 'ENOENT') { return isExecutable(await which(command)) } @@ -49,7 +49,7 @@ async function waitForOutput (expectedOutput, command, args = [], opts = {}) { try { await proc - } catch (err) { + } catch (/** @type {any} */ err) { if (!err.killed) { throw err } diff --git a/examples/webrtc-direct/test.js b/examples/webrtc-direct/test.js index 6f990073aa..d6603f36dd 100644 --- a/examples/webrtc-direct/test.js +++ b/examples/webrtc-direct/test.js @@ -72,7 +72,7 @@ async function test () { { timeout: 10000 } ) await browser.close(); - } catch (err) { + } catch (/** @type {any} */ err) { console.error(err) process.exit(1) } finally { diff --git a/package.json b/package.json index dd4bfac804..414c0dff74 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"", "test:browser": "aegir test -t browser", "test:examples": "cd examples && npm run test:all", + "test:interop": "LIBP2P_JS=$PWD npx aegir test -t node -f ./node_modules/interop-libp2p/test/*", "prepare": "aegir build --no-bundle", "release": "aegir release -t node -t browser", "release-minor": "aegir release --type minor -t node -t browser", @@ -74,7 +75,8 @@ "extends": "ipfs", "ignorePatterns": [ "!.aegir.js", - "test/ts-use" + "test/ts-use", + "*.d.ts" ] }, "dependencies": { @@ -137,11 +139,11 @@ "@types/node": "^16.0.1", "@types/node-forge": "^0.10.1", "@types/varint": "^6.0.0", - "aegir": "^33.1.1", + "aegir": "^36.0.0", "buffer": "^6.0.3", "datastore-core": "^6.0.7", "delay": "^5.0.0", - "interop-libp2p": "^0.4.0", + "interop-libp2p": "libp2p/interop#chore/update-dht", "into-stream": "^7.0.0", "ipfs-http-client": "^52.0.2", "it-concat": "^2.0.0", diff --git a/src/circuit/auto-relay.js b/src/circuit/auto-relay.js index c27bfc5631..1c238f54a2 100644 --- a/src/circuit/auto-relay.js +++ b/src/circuit/auto-relay.js @@ -116,7 +116,7 @@ class AutoRelay { this._peerStore.metadataBook.set(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE)) await this._addListenRelay(connection, id) } - } catch (err) { + } catch (/** @type {any} */ err) { this._onError(err) } } @@ -169,7 +169,7 @@ class AutoRelay { try { await this._transportManager.listen([new Multiaddr(listenAddr)]) // Announce multiaddrs will update on listen success by TransportManager event being triggered - } catch (err) { + } catch (/** @type {any} */ err) { this._onError(err) this._listenRelays.delete(id) } @@ -267,7 +267,7 @@ class AutoRelay { return } } - } catch (err) { + } catch (/** @type {any} */ err) { this._onError(err) } } @@ -279,7 +279,7 @@ class AutoRelay { try { const connection = await this._libp2p.dial(peerId) await this._addListenRelay(connection, peerId.toB58String()) - } catch (err) { + } catch (/** @type {any} */ err) { this._onError(err, `could not connect and listen on known hop relay ${peerId.toB58String()}`) } } diff --git a/src/circuit/circuit/hop.js b/src/circuit/circuit/hop.js index b2ce72de0a..73d9b1f855 100644 --- a/src/circuit/circuit/hop.js +++ b/src/circuit/circuit/hop.js @@ -54,7 +54,7 @@ async function handleHop ({ // Validate the HOP request has the required input try { validateAddrs(request, streamHandler) - } catch (err) { + } catch (/** @type {any} */ err) { return log.error('invalid hop request via peer %s', connection.remotePeer.toB58String(), err) } @@ -93,7 +93,7 @@ async function handleHop ({ connection: destinationConnection, request: stopRequest }) - } catch (err) { + } catch (/** @type {any} */ err) { return log.error(err) } diff --git a/src/circuit/circuit/stop.js b/src/circuit/circuit/stop.js index 6fa92a07c0..8efd00f892 100644 --- a/src/circuit/circuit/stop.js +++ b/src/circuit/circuit/stop.js @@ -34,7 +34,7 @@ module.exports.handleStop = function handleStop ({ // Validate the STOP request has the required input try { validateAddrs(request, streamHandler) - } catch (err) { + } catch (/** @type {any} */ err) { return log.error('invalid stop request via peer %s', connection.remotePeer.toB58String(), err) } diff --git a/src/circuit/circuit/utils.js b/src/circuit/circuit/utils.js index a69dcb50ba..624d0ba490 100644 --- a/src/circuit/circuit/utils.js +++ b/src/circuit/circuit/utils.js @@ -34,7 +34,7 @@ function validateAddrs (msg, streamHandler) { return new Multiaddr(addr) }) } - } catch (err) { + } catch (/** @type {any} */ err) { writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP ? CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID : CircuitRelay.Status.STOP_DST_MULTIADDR_INVALID) @@ -47,7 +47,7 @@ function validateAddrs (msg, streamHandler) { return new Multiaddr(addr) }) } - } catch (err) { + } catch (/** @type {any} */ err) { writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP ? CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID : CircuitRelay.Status.STOP_SRC_MULTIADDR_INVALID) diff --git a/src/circuit/index.js b/src/circuit/index.js index da8e879e52..4d180b6edd 100644 --- a/src/circuit/index.js +++ b/src/circuit/index.js @@ -87,7 +87,7 @@ class Relay { try { const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) await this._libp2p.contentRouting.provide(cid) - } catch (err) { + } catch (/** @type {any} */ err) { if (err.code === 'NO_ROUTERS_AVAILABLE') { log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err) // Stop the advertise diff --git a/src/circuit/transport.js b/src/circuit/transport.js index 1dc284cab4..5d0ad50d63 100644 --- a/src/circuit/transport.js +++ b/src/circuit/transport.js @@ -171,7 +171,7 @@ class Circuit { log('new outbound connection %s', maConn.remoteAddr) return this._upgrader.upgradeOutbound(maConn) - } catch (err) { + } catch (/** @type {any} */ err) { log.error('Circuit relay dial failed', err) disconnectOnFailure && await relayConnection.close() throw err diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index d63ebf7581..802c9fe65d 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -350,7 +350,7 @@ class ConnectionManager extends EventEmitter { if (!this._started) { return } - } catch (err) { + } catch (/** @type {any} */ err) { log.error('could not connect to peerStore stored peer', err) } } diff --git a/src/content-routing/index.js b/src/content-routing/index.js index 38889371ba..924b987344 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -11,6 +11,7 @@ const { const drain = require('it-drain') const merge = require('it-merge') const { pipe } = require('it-pipe') +const { DHTContentRouting } = require('../dht/dht-content-routing') /** * @typedef {import('peer-id')} PeerId @@ -38,27 +39,7 @@ class ContentRouting { // If we have the dht, add it to the available content routers if (this.dht && libp2p._config.dht.enabled) { - const dht = this.dht - - this.routers.push({ - /** - * @param {CID} cid - */ - provide: async (cid) => { - await drain(dht.provide(cid)) - }, - /** - * @param {CID} cid - * @param {*} options - */ - findProviders: async function * (cid, options) { - for await (const event of dht.findProviders(cid, options)) { - if (event.name === 'provider') { - yield * event.providers - } - } - } - }) + this.routers.push(new DHTContentRouting(this.dht)) } } @@ -134,7 +115,7 @@ class ContentRouting { } for await (const event of this.dht.get(key, options)) { - if (event.name === 'value') { + if (event.name === 'VALUE') { return { from: event.peerId, val: event.value } } } @@ -162,7 +143,7 @@ class ContentRouting { let gotValues = 0 for await (const event of this.dht.get(key, options)) { - if (event.name === 'value') { + if (event.name === 'VALUE') { yield { from: event.peerId, val: event.value } gotValues++ diff --git a/src/dht/dht-content-routing.js b/src/dht/dht-content-routing.js new file mode 100644 index 0000000000..e60a9615c7 --- /dev/null +++ b/src/dht/dht-content-routing.js @@ -0,0 +1,43 @@ +'use strict' + +const drain = require('it-drain') + +/** + * @typedef {import('peer-id')} PeerId + * @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule + */ + +/** + * Wrapper class to convert events into returned values + * + * @implements {ContentRoutingModule} + */ +class DHTContentRouting { + /** + * @param {import('libp2p-kad-dht').DHT} dht + */ + constructor (dht) { + this._dht = dht + } + + /** + * @param {CID} cid + */ + async provide (cid) { + await drain(this._dht.provide(cid)) + } + + /** + * @param {CID} cid + * @param {*} options + */ + async * findProviders (cid, options) { + for await (const event of this._dht.findProviders(cid, options)) { + if (event.name === 'PROVIDER') { + yield * event.providers + } + } + } +} + +module.exports = { DHTContentRouting } diff --git a/src/dht/dht-peer-routing.js b/src/dht/dht-peer-routing.js new file mode 100644 index 0000000000..bb05003e3b --- /dev/null +++ b/src/dht/dht-peer-routing.js @@ -0,0 +1,46 @@ +'use strict' + +/** + * @typedef {import('peer-id')} PeerId + * @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingModule + */ + +/** + * Wrapper class to convert events into returned values + * + * @implements {PeerRoutingModule} + */ +class DHTPeerRouting { + /** + * @param {import('libp2p-kad-dht').DHT} dht + */ + constructor (dht) { + this._dht = dht + } + + /** + * @param {PeerId} peerId + * @param {any} options + */ + async findPeer (peerId, options = {}) { + for await (const event of this._dht.findPeer(peerId, options)) { + if (event.name === 'FINAL_PEER') { + return event.peer + } + } + } + + /** + * @param {Uint8Array} key + * @param {any} options + */ + async * getClosestPeers (key, options = {}) { + for await (const event of this._dht.getClosestPeers(key, options)) { + if (event.name === 'PEER_RESPONSE') { + yield * event.closer + } + } + } +} + +module.exports = { DHTPeerRouting } diff --git a/src/dialer/index.js b/src/dialer/index.js index 65afe266e7..630e9c50b2 100644 --- a/src/dialer/index.js +++ b/src/dialer/index.js @@ -95,7 +95,7 @@ class Dialer { for (const dial of this._pendingDials.values()) { try { dial.controller.abort() - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } } @@ -129,7 +129,7 @@ class Dialer { const connection = await pendingDial.promise log('dial succeeded to %s', dialTarget.id) return connection - } catch (err) { + } catch (/** @type {any} */ err) { // Error is a timeout if (pendingDial.controller.signal.aborted) { err.code = codes.ERR_TIMEOUT diff --git a/src/get-peer.js b/src/get-peer.js index a0de9ef8b6..afad64df06 100644 --- a/src/get-peer.js +++ b/src/get-peer.js @@ -32,7 +32,7 @@ function getPeer (peer) { try { peer = PeerId.createFromB58String(idStr) - } catch (err) { + } catch (/** @type {any} */ err) { throw errCode( new Error(`${peer} is not a valid peer type`), codes.ERR_INVALID_MULTIADDR diff --git a/src/identify/index.js b/src/identify/index.js index b198b45041..d08d11963b 100644 --- a/src/identify/index.js +++ b/src/identify/index.js @@ -124,7 +124,7 @@ class IdentifyService { stream, consume ) - } catch (err) { + } catch (/** @type {any} */ err) { // Just log errors log.error('could not push identify update to peer', err) } @@ -182,7 +182,7 @@ class IdentifyService { let message try { message = Message.Identify.decode(data) - } catch (err) { + } catch (/** @type {any} */ err) { throw errCode(err, codes.ERR_INVALID_MESSAGE) } @@ -211,14 +211,14 @@ class IdentifyService { this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion)) return } - } catch (err) { + } catch (/** @type {any} */ err) { log('received invalid envelope, discard it and fallback to listenAddrs is available', err) } // LEGACY: Update peers data in PeerStore try { this.peerStore.addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr))) - } catch (err) { + } catch (/** @type {any} */ err) { log.error('received invalid addrs', err) } @@ -287,7 +287,7 @@ class IdentifyService { stream, consume ) - } catch (err) { + } catch (/** @type {any} */ err) { log.error('could not respond to identify request', err) } } @@ -313,7 +313,7 @@ class IdentifyService { collect ) message = Message.Identify.decode(data) - } catch (err) { + } catch (/** @type {any} */ err) { return log.error('received invalid message', err) } @@ -325,7 +325,7 @@ class IdentifyService { this.peerStore.protoBook.set(id, message.protocols) return } - } catch (err) { + } catch (/** @type {any} */ err) { log('received invalid envelope, discard it and fallback to listenAddrs is available', err) } @@ -333,7 +333,7 @@ class IdentifyService { try { this.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => new Multiaddr(addr))) - } catch (err) { + } catch (/** @type {any} */ err) { log.error('received invalid addrs', err) } diff --git a/src/index.js b/src/index.js index a7a86bbe21..22b9f64274 100644 --- a/src/index.js +++ b/src/index.js @@ -301,7 +301,7 @@ class Libp2p extends EventEmitter { // dht provided components (peerRouting, contentRouting, dht) if (this._modules.dht) { const DHT = this._modules.dht - // @ts-ignore Object is not constructable + // @ts-ignore TODO: types need fixing - DHT is an `object` which has no `create` method this._dht = DHT.create({ libp2p: this, ...this._config.dht @@ -358,7 +358,7 @@ class Libp2p extends EventEmitter { await this._onStarting() await this._onDidStart() log('libp2p has started') - } catch (err) { + } catch (/** @type {any} */ err) { this.emit('error', err) log.error('An error occurred starting libp2p', err) await this.stop() @@ -403,7 +403,7 @@ class Libp2p extends EventEmitter { ping.unmount(this) this.dialer.destroy() - } catch (err) { + } catch (/** @type {any} */ err) { if (err) { log.error(err) this.emit('error', err) @@ -426,7 +426,7 @@ class Libp2p extends EventEmitter { try { await this.keychain.findKeyByName('self') - } catch (err) { + } catch (/** @type {any} */ err) { await this.keychain.importPeer('self', this.peerId) } } @@ -693,7 +693,7 @@ class Libp2p extends EventEmitter { log('connecting to discovered peer %s', peerId.toB58String()) try { await this.dialer.connectToPeer(peerId) - } catch (err) { + } catch (/** @type {any} */ err) { log.error(`could not connect to discovered peer ${peerId.toB58String()} with ${err}`) } } diff --git a/src/insecure/plaintext.js b/src/insecure/plaintext.js index 2ea0458315..99921e5376 100644 --- a/src/insecure/plaintext.js +++ b/src/insecure/plaintext.js @@ -55,7 +55,7 @@ async function encrypt (localId, conn, remoteId) { let peerId try { peerId = await PeerId.createFromPubKey(id.pubkey.Data) - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) throw new InvalidCryptoExchangeError('Remote did not provide its public key') } diff --git a/src/keychain/cms.js b/src/keychain/cms.js index 286e75a4ae..e9361882df 100644 --- a/src/keychain/cms.js +++ b/src/keychain/cms.js @@ -90,7 +90,7 @@ class CMS { const obj = forge.asn1.fromDer(buf) // @ts-ignore not defined cms = forge.pkcs7.messageFromAsn1(obj) - } catch (err) { + } catch (/** @type {any} */ err) { throw errcode(new Error('Invalid CMS: ' + err.message), 'ERR_INVALID_CMS') } @@ -114,7 +114,7 @@ class CMS { try { const key = await this.keychain.findKeyById(recipient.keyId) if (key) return true - } catch (err) { + } catch (/** @type {any} */ err) { return false } return false diff --git a/src/keychain/index.js b/src/keychain/index.js index 20d974de36..b25be5a854 100644 --- a/src/keychain/index.js +++ b/src/keychain/index.js @@ -248,7 +248,7 @@ class Keychain { batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo))) await batch.commit() - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(err) } @@ -284,7 +284,7 @@ class Keychain { try { const keys = await this.listKeys() return keys.find((k) => k.id === id) - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(err) } } @@ -304,7 +304,7 @@ class Keychain { try { const res = await this.store.get(dsname) return JSON.parse(uint8ArrayToString(res)) - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(errcode(new Error(`Key '${name}' does not exist. ${err.message}`), 'ERR_KEY_NOT_FOUND')) } } @@ -365,7 +365,7 @@ class Keychain { batch.delete(oldInfoName) await batch.commit() return keyInfo - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(err) } } @@ -393,7 +393,7 @@ class Keychain { const dek = privates.get(this).dek const privateKey = await crypto.keys.import(pem, dek) return privateKey.export(password) - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(err) } } @@ -421,7 +421,7 @@ class Keychain { let privateKey try { privateKey = await crypto.keys.import(pem, password) - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(errcode(new Error('Cannot read the key, most likely the password is wrong'), 'ERR_CANNOT_READ_KEY')) } @@ -431,7 +431,7 @@ class Keychain { /** @type {string} */ const dek = privates.get(this).dek pem = await privateKey.export(dek) - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(err) } @@ -482,7 +482,7 @@ class Keychain { batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo))) await batch.commit() return keyInfo - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(err) } } @@ -502,7 +502,7 @@ class Keychain { const dsname = DsName(name) const res = await this.store.get(dsname) return uint8ArrayToString(res) - } catch (err) { + } catch (/** @type {any} */ err) { return throwDelayed(errcode(new Error(`Key '${name}' does not exist. ${err.message}`), 'ERR_KEY_NOT_FOUND')) } } diff --git a/src/nat-manager.js b/src/nat-manager.js index 0315788468..4b0b60dd24 100644 --- a/src/nat-manager.js +++ b/src/nat-manager.js @@ -188,7 +188,7 @@ class NatManager { try { await this._client.destroy() this._client = null - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } } diff --git a/src/peer-routing.js b/src/peer-routing.js index aaf0674530..f37ce6638f 100644 --- a/src/peer-routing.js +++ b/src/peer-routing.js @@ -21,6 +21,7 @@ const { clearDelayedInterval // @ts-ignore module with no types } = require('set-delayed-interval') +const { DHTPeerRouting } = require('./dht/dht-peer-routing') /** * @typedef {import('peer-id')} PeerId @@ -51,7 +52,7 @@ class PeerRouting { // If we have the dht, add it to the available peer routers if (libp2p._dht && libp2p._config.dht.enabled) { - this._routers.push(libp2p._dht) + this._routers.push(new DHTPeerRouting(libp2p._dht)) } this._refreshManagerOptions = libp2p._options.peerRouting.refreshManager @@ -79,7 +80,7 @@ class PeerRouting { try { // nb getClosestPeers adds the addresses to the address book await drain(this.getClosestPeers(this._peerId.id)) - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } } diff --git a/src/peer-store/address-book.js b/src/peer-store/address-book.js index eda90bdcc3..f28d5c2081 100644 --- a/src/peer-store/address-book.js +++ b/src/peer-store/address-book.js @@ -83,7 +83,7 @@ class AddressBook extends Book { let peerRecord try { peerRecord = PeerRecord.createFromProtobuf(envelope.payload) - } catch (err) { + } catch (/** @type {any} */ err) { log.error('invalid peer record received') return false } @@ -223,7 +223,7 @@ class AddressBook extends Book { add (peerId, multiaddrs) { if (!PeerId.isPeerId(peerId)) { log.error('peerId must be an instance of peer-id to store data') - throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + throw errcode(new Error('peerId must be an instance of peer-id ' + peerId), ERR_INVALID_PARAMETERS) } const addresses = this._toAddresses(multiaddrs) diff --git a/src/peer-store/metadata-book.js b/src/peer-store/metadata-book.js index c2769a0aec..c9f5aedeb1 100644 --- a/src/peer-store/metadata-book.js +++ b/src/peer-store/metadata-book.js @@ -80,10 +80,11 @@ class MetadataBook extends Book { /** * Set data into the datastructure * - * @override * @param {PeerId} peerId * @param {string} key * @param {Uint8Array} value + * @param {object} root0 + * @param {boolean} root0.emit */ _setValue (peerId, key, value, { emit = true } = {}) { const id = peerId.toB58String() diff --git a/src/peer-store/persistent/index.js b/src/peer-store/persistent/index.js index 40c81f6104..5655d20743 100644 --- a/src/peer-store/persistent/index.js +++ b/src/peer-store/persistent/index.js @@ -249,7 +249,7 @@ class PersistentPeerStore extends PeerStore { }).finish() batch.put(key, encodedData) - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } } @@ -275,7 +275,7 @@ class PersistentPeerStore extends PeerStore { const encodedData = peerId.marshalPubKey() batch.put(key, encodedData) - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } } @@ -302,7 +302,7 @@ class PersistentPeerStore extends PeerStore { batch.delete(key) } }) - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } } @@ -330,7 +330,7 @@ class PersistentPeerStore extends PeerStore { const encodedData = Protocols.encode({ protocols }).finish() batch.put(key, encodedData) - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } } @@ -399,7 +399,7 @@ class PersistentPeerStore extends PeerStore { default: log('invalid data persisted for: ', key.toString()) } - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } } diff --git a/src/pnet/crypto.js b/src/pnet/crypto.js index 6ef946b6b3..eb9cb22316 100644 --- a/src/pnet/crypto.js +++ b/src/pnet/crypto.js @@ -77,7 +77,7 @@ module.exports.decodeV1PSK = (pskBuffer) => { codecName: codec, psk: psk } - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) throw new Error(Errors.INVALID_PSK) } diff --git a/src/pnet/key-generator.js b/src/pnet/key-generator.js index 4bd3bab902..ad94b14083 100644 --- a/src/pnet/key-generator.js +++ b/src/pnet/key-generator.js @@ -28,6 +28,6 @@ try { // @ts-ignore generate(process.stdout) } -} catch (error) { +} catch (/** @type {any} */ error) { } diff --git a/src/transport-manager.js b/src/transport-manager.js index 1993edf80c..8a7303ef3d 100644 --- a/src/transport-manager.js +++ b/src/transport-manager.js @@ -109,7 +109,7 @@ class TransportManager { try { return await transport.dial(ma, options) - } catch (err) { + } catch (/** @type {any} */ err) { if (!err.code) err.code = codes.ERR_TRANSPORT_DIAL_FAILED throw err } diff --git a/src/upgrader.js b/src/upgrader.js index 8ee6c65412..8b07be9731 100644 --- a/src/upgrader.js +++ b/src/upgrader.js @@ -106,7 +106,7 @@ class Upgrader { } else { upgradedConn = encryptedConn } - } catch (err) { + } catch (/** @type {any} */ err) { log.error('Failed to upgrade inbound connection', err) await maConn.close(err) throw err @@ -181,7 +181,7 @@ class Upgrader { } else { upgradedConn = encryptedConn } - } catch (err) { + } catch (/** @type {any} */ err) { log.error('Failed to upgrade outbound connection', err) await maConn.close(err) throw err @@ -245,7 +245,7 @@ class Upgrader { if (this.metrics) this.metrics.trackStream({ stream, remotePeer, protocol }) connection.addStream(muxedStream, { protocol }) this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol }) - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } }, @@ -263,7 +263,7 @@ class Upgrader { const { stream, protocol } = await mss.select(protocols) if (this.metrics) this.metrics.trackStream({ stream, remotePeer, protocol }) return { stream: { ...muxedStream, ...stream }, protocol } - } catch (err) { + } catch (/** @type {any} */ err) { log.error('could not create new stream', err) throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL) } @@ -283,7 +283,7 @@ class Upgrader { if (connection.stat.status === 'open') { await connection.close() } - } catch (err) { + } catch (/** @type {any} */ err) { log.error(err) } finally { this.onConnectionEnd(connection) @@ -371,7 +371,7 @@ class Upgrader { ...await crypto.secureInbound(localPeer, stream), protocol } - } catch (err) { + } catch (/** @type {any} */ err) { throw errCode(err, codes.ERR_ENCRYPTION_FAILED) } } @@ -406,7 +406,7 @@ class Upgrader { ...await crypto.secureOutbound(localPeer, stream, remotePeerId), protocol } - } catch (err) { + } catch (/** @type {any} */ err) { throw errCode(err, codes.ERR_ENCRYPTION_FAILED) } } @@ -430,7 +430,7 @@ class Upgrader { log('%s selected as muxer protocol', protocol) const Muxer = muxers.get(protocol) return { stream, Muxer } - } catch (err) { + } catch (/** @type {any} */ err) { throw errCode(err, codes.ERR_MUXER_UNAVAILABLE) } } @@ -453,7 +453,7 @@ class Upgrader { const { stream, protocol } = await listener.handle(protocols) const Muxer = muxers.get(protocol) return { stream, Muxer } - } catch (err) { + } catch (/** @type {any} */ err) { throw errCode(err, codes.ERR_MUXER_UNAVAILABLE) } } diff --git a/test/content-routing/content-routing.node.js b/test/content-routing/content-routing.node.js index 0a8db15b64..7206b19906 100644 --- a/test/content-routing/content-routing.node.js +++ b/test/content-routing/content-routing.node.js @@ -34,7 +34,7 @@ describe('content-routing', () => { try { for await (const _ of node.contentRouting.findProviders('a cid')) {} // eslint-disable-line throw new Error('.findProviders should return an error') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() expect(err.code).to.equal('NO_ROUTERS_AVAILABLE') } @@ -238,7 +238,7 @@ describe('content-routing', () => { try { for await (const _ of node.contentRouting.findProviders(cid)) { } // eslint-disable-line throw new Error('should handle errors when finding providers') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() } diff --git a/test/dialing/dial-request.spec.js b/test/dialing/dial-request.spec.js index fd56620a00..2c72fb7b87 100644 --- a/test/dialing/dial-request.spec.js +++ b/test/dialing/dial-request.spec.js @@ -125,7 +125,7 @@ describe('Dial Request', () => { try { await dialRequest.run({ signal: controller.signal }) expect.fail('Should have thrown') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.be.an.instanceof(AggregateError) } @@ -162,7 +162,7 @@ describe('Dial Request', () => { try { await dialRequest.run({ signal: controller.signal }) expect.fail('Should have thrown') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.be.an.instanceof(AggregateError) } @@ -212,7 +212,7 @@ describe('Dial Request', () => { setTimeout(() => controller.abort(), 100) await dialRequest.run({ signal: controller.signal }) expect.fail('dial should have failed') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.be.an.instanceof(AggregateError) } diff --git a/test/dialing/direct.node.js b/test/dialing/direct.node.js index 57a8008492..6db0cb6b90 100644 --- a/test/dialing/direct.node.js +++ b/test/dialing/direct.node.js @@ -277,7 +277,7 @@ describe('Dialing (direct, TCP)', () => { try { await libp2p.dial(remoteLibp2p.transportManager.getAddrs()[0]) - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.have.property('code', ErrorCodes.ERR_INVALID_MULTIADDR) return } diff --git a/test/dialing/direct.spec.js b/test/dialing/direct.spec.js index 7f56b91dbb..1617322b8a 100644 --- a/test/dialing/direct.spec.js +++ b/test/dialing/direct.spec.js @@ -304,7 +304,7 @@ describe('Dialing (direct, WebSockets)', () => { dialer.destroy() await dialPromise expect.fail('should have failed') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.be.an.instanceof(AggregateError) expect(dialer._pendingDials.size).to.equal(0) // 1 dial request } diff --git a/test/keychain/keychain.spec.js b/test/keychain/keychain.spec.js index 032a1b11b7..0404ebf03e 100644 --- a/test/keychain/keychain.spec.js +++ b/test/keychain/keychain.spec.js @@ -519,7 +519,7 @@ describe('keychain', () => { it('should validate newPass is a string', async () => { try { await kc.rotateKeychainPass(oldPass, 1234567890) - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() } }) @@ -527,7 +527,7 @@ describe('keychain', () => { it('should validate oldPass is a string', async () => { try { await kc.rotateKeychainPass(1234, 'newInsecurePassword1') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() } }) @@ -535,7 +535,7 @@ describe('keychain', () => { it('should validate newPass is at least 20 characters', async () => { try { await kc.rotateKeychainPass(oldPass, 'not20Chars') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() } }) @@ -586,7 +586,7 @@ describe('libp2p.keychain', () => { try { await libp2p.keychain.createKey('keyName', 'rsa', 2048) - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() return } diff --git a/test/peer-routing/peer-routing.node.js b/test/peer-routing/peer-routing.node.js index 9cb9ca0fb1..fd76ee792d 100644 --- a/test/peer-routing/peer-routing.node.js +++ b/test/peer-routing/peer-routing.node.js @@ -43,7 +43,7 @@ describe('peer-routing', () => { try { for await (const _ of node.peerRouting.getClosestPeers('a cid')) { } // eslint-disable-line throw new Error('.getClosestPeers should return an error') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() expect(err.code).to.equal('NO_ROUTERS_AVAILABLE') } @@ -275,7 +275,7 @@ describe('peer-routing', () => { try { for await (const _ of node.peerRouting.getClosestPeers(peerId.id)) { } // eslint-disable-line throw new Error('should handle errors when getting the closest peers') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() } diff --git a/test/peer-store/address-book.spec.js b/test/peer-store/address-book.spec.js index ea71ed0b32..929a45fbff 100644 --- a/test/peer-store/address-book.spec.js +++ b/test/peer-store/address-book.spec.js @@ -45,7 +45,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if invalid PeerId is provided', () => { try { ab.set('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -55,7 +55,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if no addresses provided', () => { try { ab.set(peerId) - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -65,7 +65,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if invalid multiaddrs are provided', () => { try { ab.set(peerId, ['invalid multiaddr']) - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -159,7 +159,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if invalid PeerId is provided', () => { try { ab.add('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -169,7 +169,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if no addresses provided', () => { try { ab.add(peerId) - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -179,7 +179,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if invalid multiaddrs are provided', () => { try { ab.add(peerId, ['invalid multiaddr']) - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -308,7 +308,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if invalid PeerId is provided', () => { try { ab.get('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -343,7 +343,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if invalid PeerId is provided', () => { try { ab.getMultiaddrsForPeer('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -391,7 +391,7 @@ describe('addressBook', () => { it('throwns invalid parameters error if invalid PeerId is provided', () => { try { ab.delete('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } diff --git a/test/peer-store/key-book.spec.js b/test/peer-store/key-book.spec.js index 4e06ea2f4d..8c439a9051 100644 --- a/test/peer-store/key-book.spec.js +++ b/test/peer-store/key-book.spec.js @@ -23,7 +23,7 @@ describe('keyBook', () => { it('throws invalid parameters error if invalid PeerId is provided in set', () => { try { kb.set('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -33,7 +33,7 @@ describe('keyBook', () => { it('throws invalid parameters error if invalid PeerId is provided in get', () => { try { kb.get('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } diff --git a/test/peer-store/metadata-book.spec.js b/test/peer-store/metadata-book.spec.js index a1b0d2a105..478dfff5dd 100644 --- a/test/peer-store/metadata-book.spec.js +++ b/test/peer-store/metadata-book.spec.js @@ -34,7 +34,7 @@ describe('metadataBook', () => { it('throws invalid parameters error if invalid PeerId is provided', () => { try { mb.set('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -44,7 +44,7 @@ describe('metadataBook', () => { it('throws invalid parameters error if no key provided', () => { try { mb.set(peerId) - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -54,7 +54,7 @@ describe('metadataBook', () => { it('throws invalid parameters error if no value provided', () => { try { mb.set(peerId, 'location') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -64,7 +64,7 @@ describe('metadataBook', () => { it('throws invalid parameters error if value is not a buffer', () => { try { mb.set(peerId, 'location', 'mars') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -163,7 +163,7 @@ describe('metadataBook', () => { it('throws invalid parameters error if invalid PeerId is provided', () => { try { mb.get('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -199,7 +199,7 @@ describe('metadataBook', () => { it('throws invalid parameters error if invalid PeerId is provided', () => { try { mb.getValue('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -248,7 +248,7 @@ describe('metadataBook', () => { it('throwns invalid parameters error if invalid PeerId is provided', () => { try { mb.delete('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } @@ -305,7 +305,7 @@ describe('metadataBook', () => { it('throws invalid parameters error if invalid PeerId is provided', () => { try { mb.deleteValue('invalid peerId') - } catch (err) { + } catch (/** @type {any} */ err) { expect(err.code).to.equal(ERR_INVALID_PARAMETERS) return } diff --git a/test/transports/transport-manager.spec.js b/test/transports/transport-manager.spec.js index 1fb5be1ed6..23006cdd6b 100644 --- a/test/transports/transport-manager.spec.js +++ b/test/transports/transport-manager.spec.js @@ -201,7 +201,7 @@ describe('libp2p.transportManager (dial only)', () => { try { await libp2p.start() - } catch (err) { + } catch (/** @type {any} */ err) { expect(err).to.exist() expect(err.code).to.equal(ErrorCodes.ERR_NO_VALID_ADDRESSES) return diff --git a/test/ts-use/package.json b/test/ts-use/package.json index 6445e42fb1..2c71831982 100644 --- a/test/ts-use/package.json +++ b/test/ts-use/package.json @@ -10,7 +10,7 @@ "libp2p-delegated-peer-routing": "^0.10.0", "libp2p-gossipsub": "^0.9.0", "libp2p-interfaces": "^1.0.1", - "libp2p-kad-dht": "^0.23.1", + "libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#fix/refactor-query-logic", "libp2p-mplex": "^0.10.4", "@chainsafe/libp2p-noise": "^4.1.0", "libp2p-record": "^0.10.4", diff --git a/test/utils/mockConnection.js b/test/utils/mockConnection.js index 66c659fb16..6c1439aad8 100644 --- a/test/utils/mockConnection.js +++ b/test/utils/mockConnection.js @@ -108,7 +108,7 @@ function createConnection ({ // Need to be able to notify a peer of this this._onStream({ connection, stream, protocol }) const handler = protocols.get(protocol) handler({ connection, stream, protocol }) - } catch (err) { + } catch (/** @type {any} */ err) { // Do nothing } }, @@ -124,7 +124,7 @@ function createConnection ({ try { const { stream, protocol } = await mss.select(protocols) return { stream: { ...muxedStream, ...stream }, protocol } - } catch (err) { + } catch (/** @type {any} */ err) { throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL) } } From db065d07f98cecb5bdb614ec0fce8eaa574d740a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 17 Nov 2021 17:12:17 +0000 Subject: [PATCH 10/22] chore: linting --- src/dht/dht-content-routing.js | 1 + src/dht/dht-peer-routing.js | 5 +++++ src/peer-store/metadata-book.js | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/dht/dht-content-routing.js b/src/dht/dht-content-routing.js index e60a9615c7..ead668f3d4 100644 --- a/src/dht/dht-content-routing.js +++ b/src/dht/dht-content-routing.js @@ -5,6 +5,7 @@ const drain = require('it-drain') /** * @typedef {import('peer-id')} PeerId * @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule + * @typedef {import('multiformats/cid').CID} CID */ /** diff --git a/src/dht/dht-peer-routing.js b/src/dht/dht-peer-routing.js index bb05003e3b..762abc80fa 100644 --- a/src/dht/dht-peer-routing.js +++ b/src/dht/dht-peer-routing.js @@ -1,5 +1,8 @@ 'use strict' +const errCode = require('err-code') +const { messages, codes } = require('../errors') + /** * @typedef {import('peer-id')} PeerId * @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingModule @@ -28,6 +31,8 @@ class DHTPeerRouting { return event.peer } } + + throw errCode(new Error(messages.NOT_FOUND), codes.ERR_NOT_FOUND) } /** diff --git a/src/peer-store/metadata-book.js b/src/peer-store/metadata-book.js index c9f5aedeb1..19394bdbc3 100644 --- a/src/peer-store/metadata-book.js +++ b/src/peer-store/metadata-book.js @@ -84,7 +84,7 @@ class MetadataBook extends Book { * @param {string} key * @param {Uint8Array} value * @param {object} root0 - * @param {boolean} root0.emit + * @param {boolean} [root0.emit] */ _setValue (peerId, key, value, { emit = true } = {}) { const id = peerId.toB58String() From b771719ea53feb488c4ea79000521ed0be0b13c1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 17 Nov 2021 17:32:39 +0000 Subject: [PATCH 11/22] chore: update config docs --- doc/CONFIGURATION.md | 76 +++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index b9b24fd04d..7d4523f76f 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -1,37 +1,37 @@ -# - -- [Configuration](#configuration) - - [Overview](#overview) - - [Modules](#modules) - - [Transport](#transport) - - [Stream Multiplexing](#stream-multiplexing) - - [Connection Encryption](#connection-encryption) - - [Peer Discovery](#peer-discovery) - - [Content Routing](#content-routing) - - [Peer Routing](#peer-routing) - - [DHT](#dht) - - [Pubsub](#pubsub) - - [Customizing libp2p](#customizing-libp2p) - - [Examples](#examples) - - [Basic setup](#basic-setup) - - [Customizing Peer Discovery](#customizing-peer-discovery) - - [Setup webrtc transport and discovery](#setup-webrtc-transport-and-discovery) - - [Customizing Pubsub](#customizing-pubsub) - - [Customizing DHT](#customizing-dht) - - [Setup with Content and Peer Routing](#setup-with-content-and-peer-routing) - - [Setup with Relay](#setup-with-relay) - - [Setup with Auto Relay](#setup-with-auto-relay) - - [Setup with Keychain](#setup-with-keychain) - - [Configuring Dialing](#configuring-dialing) - - [Configuring Connection Manager](#configuring-connection-manager) - - [Configuring Transport Manager](#configuring-transport-manager) - - [Configuring Metrics](#configuring-metrics) - - [Configuring PeerStore](#configuring-peerstore) - - [Customizing Transports](#customizing-transports) - - [Configuring the NAT Manager](#configuring-the-nat-manager) - - [Browser support](#browser-support) - - [UPnP and NAT-PMP](#upnp-and-nat-pmp) - - [Configuration examples](#configuration-examples) +# + +- [Overview](#overview) +- [Modules](#modules) + - [Transport](#transport) + - [Stream Multiplexing](#stream-multiplexing) + - [Connection Encryption](#connection-encryption) + - [Peer Discovery](#peer-discovery) + - [Content Routing](#content-routing) + - [Peer Routing](#peer-routing) + - [DHT](#dht) + - [Pubsub](#pubsub) +- [Customizing libp2p](#customizing-libp2p) + - [Examples](#examples) + - [Basic setup](#basic-setup) + - [Customizing Peer Discovery](#customizing-peer-discovery) + - [Setup webrtc transport and discovery](#setup-webrtc-transport-and-discovery) + - [Customizing Pubsub](#customizing-pubsub) + - [Customizing DHT](#customizing-dht) + - [Setup with Content and Peer Routing](#setup-with-content-and-peer-routing) + - [Setup with Relay](#setup-with-relay) + - [Setup with Auto Relay](#setup-with-auto-relay) + - [Setup with Keychain](#setup-with-keychain) + - [Configuring Dialing](#configuring-dialing) + - [Configuring Connection Manager](#configuring-connection-manager) + - [Configuring Transport Manager](#configuring-transport-manager) + - [Configuring Metrics](#configuring-metrics) + - [Configuring PeerStore](#configuring-peerstore) + - [Customizing Transports](#customizing-transports) + - [Configuring the NAT Manager](#configuring-the-nat-manager) + - [Browser support](#browser-support) + - [UPnP and NAT-PMP](#upnp-and-nat-pmp) + - [Configuring protocol name](#configuring-protocol-name) +- [Configuration examples](#configuration-examples) ## Overview @@ -374,11 +374,7 @@ const node = await Libp2p.create({ dht: { // The DHT options (and defaults) can be found in its documentation kBucketSize: 20, enabled: true, // This flag is required for DHT to run (disabled by default) - randomWalk: { - enabled: true, // Allows to disable discovery (enabled by default) - interval: 300e3, - timeout: 10e3 - } + clientMode: false // Whether to run the WAN DHT in client or server mode (default: client mode) } } }) @@ -788,7 +784,7 @@ By default under nodejs libp2p will attempt to use [UPnP](https://en.wikipedia.o #### Configuring protocol name -Changing the protocol name prefix can isolate default public network (IPFS) for custom purposes. +Changing the protocol name prefix can isolate default public network (IPFS) for custom purposes. ```js const node = await Libp2p.create({ From 661b26e249c572363b74f2e737736abc4fc9f5aa Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 17 Nov 2021 17:45:46 +0000 Subject: [PATCH 12/22] chore: update build --- .github/workflows/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 518b12d3aa..8c401645d6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,6 @@ jobs: node-version: 14 - run: npm install - run: npx aegir lint - - uses: gozala/typescript-error-reporter-action@v1.0.8 - run: npx aegir build - run: npx aegir dep-check - uses: ipfs/aegir/actions/bundle-size@v32.1.0 From c4f3e3e8bde1a5290781a9a7723f253752192790 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 18 Nov 2021 16:44:56 +0000 Subject: [PATCH 13/22] chore: remove gh urls --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 414c0dff74..382e687952 100644 --- a/package.json +++ b/package.json @@ -156,7 +156,7 @@ "libp2p-floodsub": "^0.27.0", "libp2p-gossipsub": "^0.11.0", "libp2p-interfaces-compliance-tests": "^1.0.0", - "libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#fix/refactor-query-logic", + "libp2p-kad-dht": "^0.26.0", "libp2p-mdns": "^0.17.0", "libp2p-mplex": "^0.10.1", "libp2p-tcp": "^0.17.0", From e17a62cd497071a1111657d6d4bb19771571bb04 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 18 Nov 2021 17:44:35 +0000 Subject: [PATCH 14/22] chore: remove gh versions --- examples/delegated-routing/package.json | 2 +- test/ts-use/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/delegated-routing/package.json b/examples/delegated-routing/package.json index 6598bfd364..1fbf49941a 100644 --- a/examples/delegated-routing/package.json +++ b/examples/delegated-routing/package.json @@ -7,7 +7,7 @@ "libp2p": "github:libp2p/js-libp2p#master", "libp2p-delegated-content-routing": "~0.2.2", "libp2p-delegated-peer-routing": "~0.2.2", - "libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#fix/refactor-query-logic", + "libp2p-kad-dht": "^0.26.0", "libp2p-mplex": "~0.8.5", "libp2p-secio": "~0.11.1", "libp2p-webrtc-star": "~0.15.8", diff --git a/test/ts-use/package.json b/test/ts-use/package.json index 2c71831982..6336d46032 100644 --- a/test/ts-use/package.json +++ b/test/ts-use/package.json @@ -10,7 +10,7 @@ "libp2p-delegated-peer-routing": "^0.10.0", "libp2p-gossipsub": "^0.9.0", "libp2p-interfaces": "^1.0.1", - "libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#fix/refactor-query-logic", + "libp2p-kad-dht": "^0.26.0", "libp2p-mplex": "^0.10.4", "@chainsafe/libp2p-noise": "^4.1.0", "libp2p-record": "^0.10.4", From 40fab21d6dbfdb4194bed52b1f0bdaf138f83490 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 18 Nov 2021 18:13:21 +0000 Subject: [PATCH 15/22] chore: fix examples --- .github/workflows/examples.yml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index b2b0aa37f6..1d58f5d8f7 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -22,81 +22,81 @@ jobs: steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- auto-relay + - run: cd examples && npm i && npm run test -- auto-relay test-chat-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- chat + - run: cd examples && npm i && npm run test -- chat test-connection-encryption-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- connection-encryption + - run: cd examples && npm i && npm run test -- connection-encryption test-discovery-mechanisms-example: needs: check runs-on: macos-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- discovery-mechanisms + - run: cd examples && npm i && npm run test -- discovery-mechanisms test-echo-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- echo + - run: cd examples && npm i && npm run test -- echo test-libp2p-in-the-browser-example: needs: check runs-on: macos-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- libp2p-in-the-browser + - run: cd examples && npm i && npm run test -- libp2p-in-the-browser test-peer-and-content-routing-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- peer-and-content-routing + - run: cd examples && npm i && npm run test -- peer-and-content-routing test-pnet-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- pnet + - run: cd examples && npm i && npm run test -- pnet test-protocol-and-stream-muxing-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- protocol-and-stream-muxing + - run: cd examples && npm i && npm run test -- protocol-and-stream-muxing test-pubsub-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- pubsub + - run: cd examples && npm i && npm run test -- pubsub test-transports-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- transports + - run: cd examples && npm i && npm run test -- transports test-webrtc-direct-example: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- webrtc-direct \ No newline at end of file + - run: cd examples && npm i && npm run test -- webrtc-direct \ No newline at end of file From 47629c39f58b5a87ed4b3729bd3dbe5160597d99 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 18 Nov 2021 18:18:08 +0000 Subject: [PATCH 16/22] chore: remove gh versions --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 382e687952..e9bcf79dd3 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"", "test:browser": "aegir test -t browser", "test:examples": "cd examples && npm run test:all", - "test:interop": "LIBP2P_JS=$PWD npx aegir test -t node -f ./node_modules/interop-libp2p/test/*", + "test:interop": "LIBP2P_JS=$PWD npx aegir test -t node -f ./node_modules/libp2p-interop/test/*", "prepare": "aegir build --no-bundle", "release": "aegir release -t node -t browser", "release-minor": "aegir release --type minor -t node -t browser", @@ -143,7 +143,6 @@ "buffer": "^6.0.3", "datastore-core": "^6.0.7", "delay": "^5.0.0", - "interop-libp2p": "libp2p/interop#chore/update-dht", "into-stream": "^7.0.0", "ipfs-http-client": "^52.0.2", "it-concat": "^2.0.0", @@ -156,6 +155,7 @@ "libp2p-floodsub": "^0.27.0", "libp2p-gossipsub": "^0.11.0", "libp2p-interfaces-compliance-tests": "^1.0.0", + "libp2p-interop": "^0.5.0", "libp2p-kad-dht": "^0.26.0", "libp2p-mdns": "^0.17.0", "libp2p-mplex": "^0.10.1", From 142a724b89e5f4d9f3cec3c84997b871faee8a2a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 18 Nov 2021 18:29:07 +0000 Subject: [PATCH 17/22] chore: set up node --- .github/workflows/examples.yml | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index 1d58f5d8f7..b50cd7b8a3 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -12,6 +12,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: npx aegir lint - run: npx aegir ts -p check @@ -21,6 +24,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- auto-relay test-chat-example: @@ -28,6 +34,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- chat test-connection-encryption-example: @@ -35,6 +44,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- connection-encryption test-discovery-mechanisms-example: @@ -42,6 +54,9 @@ jobs: runs-on: macos-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- discovery-mechanisms test-echo-example: @@ -49,6 +64,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- echo test-libp2p-in-the-browser-example: @@ -56,6 +74,9 @@ jobs: runs-on: macos-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- libp2p-in-the-browser test-peer-and-content-routing-example: @@ -63,6 +84,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- peer-and-content-routing test-pnet-example: @@ -70,6 +94,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- pnet test-protocol-and-stream-muxing-example: @@ -77,6 +104,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- protocol-and-stream-muxing test-pubsub-example: @@ -84,6 +114,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- pubsub test-transports-example: @@ -91,6 +124,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- transports test-webrtc-direct-example: @@ -98,5 +134,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 - run: npm install - run: cd examples && npm i && npm run test -- webrtc-direct \ No newline at end of file From 29970cf195730569b832e9f449134a82fe7c0669 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 18 Nov 2021 18:37:59 +0000 Subject: [PATCH 18/22] chore: add pre-gyp --- .github/workflows/examples.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index b50cd7b8a3..36173f4f8a 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -137,5 +137,5 @@ jobs: - uses: actions/setup-node@v2 with: node-version: 16 - - run: npm install + - run: npm install -g @mapbox/node-pre-gyp && npm install - run: cd examples && npm i && npm run test -- webrtc-direct \ No newline at end of file From f1a539dad9e8794b6ddcc9f2b505c3e2039932e0 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 19 Nov 2021 08:03:27 +0000 Subject: [PATCH 19/22] chore: start is async --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 22b9f64274..63edbf6924 100644 --- a/src/index.js +++ b/src/index.js @@ -619,7 +619,7 @@ class Libp2p extends EventEmitter { // DHT subsystem if (this._config.dht.enabled) { - this._dht && this._dht.start() + this._dht && await this._dht.start() // TODO: this should be modified once random-walk is used as // the other discovery modules From 725c54393ecb154c9a1f864a069e7cf66d02945e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 19 Nov 2021 08:04:24 +0000 Subject: [PATCH 20/22] chore: revert change --- src/peer-store/address-book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peer-store/address-book.js b/src/peer-store/address-book.js index f28d5c2081..4e28eca923 100644 --- a/src/peer-store/address-book.js +++ b/src/peer-store/address-book.js @@ -223,7 +223,7 @@ class AddressBook extends Book { add (peerId, multiaddrs) { if (!PeerId.isPeerId(peerId)) { log.error('peerId must be an instance of peer-id to store data') - throw errcode(new Error('peerId must be an instance of peer-id ' + peerId), ERR_INVALID_PARAMETERS) + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) } const addresses = this._toAddresses(multiaddrs) From ae943578ad7cbde97a95a39c0f5ca3cc10256d2c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 19 Nov 2021 08:06:25 +0000 Subject: [PATCH 21/22] chore: revert change --- examples/transports/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/transports/README.md b/examples/transports/README.md index bf2fcf5ce6..18f5975bc7 100644 --- a/examples/transports/README.md +++ b/examples/transports/README.md @@ -245,7 +245,7 @@ await pipe( // node 3 (WebSockets) attempts to dial to node 1 (TCP) try { await node3.dialProtocol(node1.peerId, '/print') -} catch (/** @type {any} */ err) { +} catch (err) { console.log('node 3 failed to dial to node 1 with:', err.message) } ``` From 28fad6b6f728ef50f72c5b84ae20d6ba763667e4 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 25 Nov 2021 15:42:46 +0000 Subject: [PATCH 22/22] chore: update dht --- examples/delegated-routing/package.json | 2 +- package.json | 2 +- test/ts-use/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/delegated-routing/package.json b/examples/delegated-routing/package.json index 1fbf49941a..76a06b46ed 100644 --- a/examples/delegated-routing/package.json +++ b/examples/delegated-routing/package.json @@ -7,7 +7,7 @@ "libp2p": "github:libp2p/js-libp2p#master", "libp2p-delegated-content-routing": "~0.2.2", "libp2p-delegated-peer-routing": "~0.2.2", - "libp2p-kad-dht": "^0.26.0", + "libp2p-kad-dht": "^0.26.5", "libp2p-mplex": "~0.8.5", "libp2p-secio": "~0.11.1", "libp2p-webrtc-star": "~0.15.8", diff --git a/package.json b/package.json index e9bcf79dd3..7febe18590 100644 --- a/package.json +++ b/package.json @@ -156,7 +156,7 @@ "libp2p-gossipsub": "^0.11.0", "libp2p-interfaces-compliance-tests": "^1.0.0", "libp2p-interop": "^0.5.0", - "libp2p-kad-dht": "^0.26.0", + "libp2p-kad-dht": "^0.26.5", "libp2p-mdns": "^0.17.0", "libp2p-mplex": "^0.10.1", "libp2p-tcp": "^0.17.0", diff --git a/test/ts-use/package.json b/test/ts-use/package.json index 6336d46032..c056526a85 100644 --- a/test/ts-use/package.json +++ b/test/ts-use/package.json @@ -10,7 +10,7 @@ "libp2p-delegated-peer-routing": "^0.10.0", "libp2p-gossipsub": "^0.9.0", "libp2p-interfaces": "^1.0.1", - "libp2p-kad-dht": "^0.26.0", + "libp2p-kad-dht": "^0.26.5", "libp2p-mplex": "^0.10.4", "@chainsafe/libp2p-noise": "^4.1.0", "libp2p-record": "^0.10.4",