From 7c123a5a258542f269ab821ffa29bc283f2920b0 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 2 Apr 2020 17:51:34 +0100 Subject: [PATCH 1/3] chore: update ipfs-http-client and other deps Refactors code do work with new ky-less http client --- .aegir.js | 19 ++++++++++++++---- package.json | 17 +++++++++------- src/index.js | 11 ++++++----- test/index.spec.js | 48 +++++++++++++++++++++++++++------------------- 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/.aegir.js b/.aegir.js index c1a72e9..7432195 100644 --- a/.aegir.js +++ b/.aegir.js @@ -1,13 +1,24 @@ 'use strict' -const createServer = require('ipfsd-ctl').createServer - -const server = createServer() +const { createServer } = require('ipfsd-ctl') +let server module.exports = { hooks: { browser: { - pre: () => server.start(), + pre: async () => { + server = createServer({ + host: '127.0.0.1', + port: 57483 + }, { + type: 'go', + ipfsHttpModule: require('ipfs-http-client'), + ipfsBin: require('go-ipfs-dep').path(), + test: true + }) + + await server.start() + }, post: () => server.stop() } } diff --git a/package.json b/package.json index 8e75e12..fd5d492 100644 --- a/package.json +++ b/package.json @@ -10,24 +10,27 @@ "scripts": { "lint": "aegir lint", "build": "aegir build", - "test": "aegir test -f test/**/*.js", - "test:node": "aegir test --target node -f test/**/*.js", + "test": "aegir test", + "test:node": "aegir test --target node", "release": "aegir release", "release-minor": "aegir release --type minor", "release-major": "aegir release --type major", "coverage": "aegir coverage" }, "devDependencies": { - "aegir": "^21.0.2", + "aegir": "^21.4.5", "chai": "^4.2.0", - "cids": "^0.7.1", - "go-ipfs-dep": "^0.4.23", - "ipfsd-ctl": "~0.47.4", + "cids": "^0.8.0", + "go-ipfs-dep": "0.4.23-3", + "ipfs-utils": "^1.2.1", + "ipfsd-ctl": "^3.0.0", + "it-drain": "^1.0.0", + "it-last": "^1.0.1", "peer-id": "^0.13.7" }, "dependencies": { "debug": "^4.1.1", - "ipfs-http-client": "^42.0.0", + "ipfs-http-client": "^43.0.0", "it-all": "^1.0.0", "multiaddr": "^7.2.1", "p-defer": "^3.0.0", diff --git a/src/index.js b/src/index.js index c7e2c97..50b2cae 100644 --- a/src/index.js +++ b/src/index.js @@ -62,6 +62,7 @@ class DelegatedContentRouting { * @param {CID} key * @param {object} options * @param {number} options.timeout How long the query can take. Defaults to 30 seconds + * @param {number} options.numProviders How many providers to find, defaults to 20 * @returns {AsyncIterable} */ async * findProviders (key, options = {}) { @@ -80,12 +81,12 @@ class DelegatedContentRouting { try { await onStart.promise - const providers = this.dht.findProvs(key, { + for await (const { id, addrs } of this.dht.findProvs(key, { numProviders: options.numProviders, - timeout: `${options.timeout}ms` // The api requires specification of the time unit (s/ms) - }) - - for await (const { id, addrs } of providers) { + searchParams: { + timeout: `${options.timeout}ms` // The api requires specification of the time unit (s/ms) + } + })) { const peerInfo = new PeerInfo(PeerId.createFromCID(id)) addrs.forEach(addr => peerInfo.multiaddrs.add(addr)) yield peerInfo diff --git a/test/index.spec.js b/test/index.spec.js index cf2553f..37c31c1 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -2,12 +2,19 @@ 'use strict' const expect = require('chai').expect -const IPFSFactory = require('ipfsd-ctl') +const { createFactory } = require('ipfsd-ctl') const CID = require('cids') const PeerId = require('peer-id') const all = require('it-all') -const factory = IPFSFactory.create({ - type: 'go' +const last = require('it-last') +const drain = require('it-drain') +const { isNode } = require('ipfs-utils/src/env') +const factory = createFactory({ + type: 'go', + ipfsHttpModule: require('ipfs-http-client'), + ipfsBin: isNode ? require('go-ipfs-dep').path() : undefined, + test: true, + endpoint: 'http://localhost:57483' }) const DelegatedContentRouting = require('../src') @@ -15,11 +22,13 @@ const DelegatedContentRouting = require('../src') async function spawnNode (bootstrap = []) { const node = await factory.spawn({ // Lock down the nodes so testing can be deterministic - config: { - Bootstrap: bootstrap, - Discovery: { - MDNS: { - Enabled: false + ipfsOptions: { + config: { + Bootstrap: bootstrap, + Discovery: { + MDNS: { + Enabled: false + } } } } @@ -59,11 +68,7 @@ describe('DelegatedContentRouting', function () { }) after(() => { - return Promise.all([ - selfNode.stop(), - delegateNode.stop(), - bootstrapNode.stop() - ]) + return factory.clean() }) describe('create', () => { @@ -109,11 +114,13 @@ describe('DelegatedContentRouting', function () { const cid = new CID('QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv') before('register providers', async () => { - await bootstrapNode.api.dht.provide(cid) - await selfNode.api.dht.provide(cid) + await drain(bootstrapNode.api.dht.provide(cid)) + await drain(selfNode.api.dht.provide(cid)) }) - it('should be able to find providers through the delegate node', async () => { + it('should be able to find providers through the delegate node', async function () { + this.timeout(60000) + const opts = delegateNode.apiAddr.toOptions() const routing = new DelegatedContentRouting(selfId, { protocol: 'http', @@ -153,13 +160,14 @@ describe('DelegatedContentRouting', function () { host: opts.host }) - const res = await selfNode.api.add(Buffer.from(`hello-${Math.random()}`)) - const cid = new CID(res[0].hash) + const { cid } = await last(selfNode.api.add(Buffer.from(`hello-${Math.random()}`))) + await contentRouter.provide(cid) - const providers = await delegateNode.api.dht.findProvs(cid.toBaseEncodedString()) + + const providers = await all(delegateNode.api.dht.findProvs(cid, { numProviders: 2 })) // We are hosting the file, validate we're the provider - expect(providers.map((p) => p.id.toB58String())).to.include(selfId.toB58String(), 'Did not include self node') + expect(providers.map((p) => p.id)).to.include(selfId.toB58String(), 'Did not include self node') }) }) }) From 84c9e7c96e92909a72d87533d0d4ab9b1c2b6f96 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 3 Apr 2020 11:13:01 +0100 Subject: [PATCH 2/3] chore: remove timeout from tests --- test/index.spec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/index.spec.js b/test/index.spec.js index 37c31c1..301f26d 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -119,8 +119,6 @@ describe('DelegatedContentRouting', function () { }) it('should be able to find providers through the delegate node', async function () { - this.timeout(60000) - const opts = delegateNode.apiAddr.toOptions() const routing = new DelegatedContentRouting(selfId, { protocol: 'http', From f3da87f6ca3d0d367ce79a7193b80b750e0ef31c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 8 Apr 2020 18:22:47 +0100 Subject: [PATCH 3/3] chore: specify minimum http client version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fd5d492..ea7c103 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "dependencies": { "debug": "^4.1.1", - "ipfs-http-client": "^43.0.0", + "ipfs-http-client": "^43.0.1", "it-all": "^1.0.0", "multiaddr": "^7.2.1", "p-defer": "^3.0.0",