diff --git a/package.json b/package.json index 1818427..af69f4c 100644 --- a/package.json +++ b/package.json @@ -20,16 +20,16 @@ "devDependencies": { "aegir": "^19.0.5", "chai": "^4.2.0", - "cids": "~0.5.5", - "go-ipfs-dep": "~0.4.17", - "ipfsd-ctl": "~0.39.2" + "cids": "^0.7.1", + "go-ipfs-dep": "^0.4.21", + "ipfsd-ctl": "^0.43.0" }, "dependencies": { - "async": "^2.6.1", - "ipfs-api": "^24.0.2", - "multiaddr": "^5.0.0", - "peer-id": "~0.11.0", - "peer-info": "~0.14.1" + "async": "^2.6.2", + "ipfs-http-client": "^33.0.2", + "multiaddr": "^6.1.0", + "peer-id": "^0.12.2", + "peer-info": "^0.15.1" }, "contributors": [ "David Dias ", diff --git a/src/index.js b/src/index.js index 7e3fb20..68ad82f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,9 @@ 'use strict' -const PeerInfo = require('peer-info') -const PeerID = require('peer-id') -const dht = require('ipfs-api/src/dht') -const swarm = require('ipfs-api/src/swarm') -const refs = require('ipfs-api/src/refs') -const defaultConfig = require('ipfs-api/src/utils/default-config') +const dht = require('ipfs-http-client/src/dht') +const swarm = require('ipfs-http-client/src/swarm') +const refs = require('ipfs-http-client/src/files-regular/refs') +const defaultConfig = require('ipfs-http-client/src/utils/default-config') const series = require('async/series') const parallel = require('async/parallel') const reflect = require('async/reflect') @@ -79,31 +77,9 @@ class DelegatedContentRouting { options.maxTimeout = options.maxTimeout || DEFAULT_MAX_TIMEOUT - this.dht.findprovs(key.toBaseEncodedString(), { + this.dht.findProvs(key.toString(), { timeout: `${options.maxTimeout}ms` // The api requires specification of the time unit (s/ms) - }, (err, results) => { - if (err) { - return callback(err) - } - - // cleanup result from ipfs-api - const infos = [] - results - .filter((res) => Boolean(res.Responses)) - .forEach((res) => { - res.Responses.forEach((raw) => { - const info = new PeerInfo( - PeerID.createFromB58String(raw.ID) - ) - if (raw.Addrs) { - raw.Addrs.forEach((addr) => info.multiaddrs.add(addr)) - } - infos.push(info) - }) - }) - - callback(null, infos) - }) + }, callback) } /** @@ -138,7 +114,7 @@ class DelegatedContentRouting { cb() }), (cb) => { - this.refs(key.toBaseEncodedString(), { recursive: true }, cb) + this.refs(key.toString(), { recursive: true }, cb) } ], (err) => callback(err)) } diff --git a/test/index.spec.js b/test/index.spec.js index e4a70dd..c05c2d0 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -3,7 +3,8 @@ const expect = require('chai').expect const IPFSFactory = require('ipfsd-ctl') -const async = require('async') +const parallel = require('async/parallel') +const waterfall = require('async/waterfall') const CID = require('cids') const PeerId = require('peer-id') @@ -11,16 +12,16 @@ const factory = IPFSFactory.create({ type: 'go' }) const DelegatedContentRouting = require('../src') -function spawnNode (boostrap, callback) { - if (typeof boostrap === 'function') { - callback = boostrap - boostrap = [] +function spawnNode (bootstrap, callback) { + if (typeof bootstrap === 'function') { + callback = bootstrap + bootstrap = [] } factory.spawn({ // Lock down the nodes so testing can be deterministic config: { - Bootstrap: boostrap, + Bootstrap: bootstrap, Discovery: { MDNS: { Enabled: false @@ -44,12 +45,13 @@ describe('DelegatedContentRouting', function () { let selfNode let selfId let delegatedNode + let delegatedId let bootstrapNode let bootstrapId before((done) => { - async.waterfall([ - // Spawn a "Boostrap" node that doesnt connect to anything + waterfall([ + // Spawn a "bootstrap" node that doesnt connect to anything (cb) => spawnNode(cb), (ipfsd, id, cb) => { bootstrapNode = ipfsd @@ -67,13 +69,14 @@ describe('DelegatedContentRouting', function () { (cb) => spawnNode(bootstrapId.addresses, cb), (ipfsd, id, cb) => { delegatedNode = ipfsd + delegatedId = PeerId.createFromB58String(id.id) cb() } ], done) }) after((done) => { - async.parallel([ + parallel([ (cb) => selfNode.stop(cb), (cb) => delegatedNode.stop(cb), (cb) => bootstrapNode.stop(cb) @@ -123,8 +126,16 @@ describe('DelegatedContentRouting', function () { }) describe('findProviders', () => { + const cid = new CID('QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv') + before('register providers', (done) => { + parallel([ + (cb) => bootstrapNode.api.dht.provide(cid, cb), + (cb) => selfNode.api.dht.provide(cid, cb) + ], done) + }) + it('should be able to find providers through the delegate node', function (done) { - async.waterfall([ + waterfall([ (cb) => { const opts = delegatedNode.apiAddr.toOptions() const routing = new DelegatedContentRouting(selfId, { @@ -132,7 +143,6 @@ describe('DelegatedContentRouting', function () { port: opts.port, host: opts.host }) - const cid = new CID('QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv') routing.findProviders(cid, cb) }, (providers, cb) => { @@ -149,7 +159,7 @@ describe('DelegatedContentRouting', function () { }) it('should be able to specify a maxTimeout', function (done) { - async.waterfall([ + waterfall([ (cb) => { const opts = delegatedNode.apiAddr.toOptions() const routing = new DelegatedContentRouting(selfId, { @@ -179,7 +189,7 @@ describe('DelegatedContentRouting', function () { let contentRouter let cid - async.waterfall([ + waterfall([ (cb) => { const opts = delegatedNode.apiAddr.toOptions() contentRouter = new DelegatedContentRouting(selfId, { @@ -188,25 +198,22 @@ describe('DelegatedContentRouting', function () { host: opts.host }) - selfNode.api.files.add(Buffer.from(`hello-${Math.random()}`), cb) + selfNode.api.add(Buffer.from(`hello-${Math.random()}`), cb) }, (res, cb) => { cid = new CID(res[0].hash) contentRouter.provide(cid, cb) }, (cb) => { - delegatedNode.api.dht.findprovs(cid.toBaseEncodedString(), cb) + delegatedNode.api.dht.findProvs(cid, cb) }, - (provs, cb) => { - let providers = [] - provs.filter((res) => Boolean(res.Responses)).forEach((res) => { - providers = providers.concat(res.Responses) - }) - - // We are hosting the file, validate we're the provider - const res = providers.find((prov) => prov.ID === selfId.toB58String()) - expect(res.ID).to.equal(selfId.toB58String()) - + (providers, cb) => { + const providerIds = providers.map(p => p.id.toB58String()) + // The delegate should be a provider + expect(providerIds).to.have.members([ + selfId.toB58String(), + delegatedId.toB58String() + ]) cb() } ], done)