Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update deps #9

Merged
merged 8 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <daviddias.p@gmail.com>",
Expand Down
38 changes: 7 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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))
}
Expand Down
57 changes: 32 additions & 25 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@

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')

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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -123,16 +126,23 @@ 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, {
protocol: 'http',
port: opts.port,
host: opts.host
})
const cid = new CID('QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv')
routing.findProviders(cid, cb)
},
(providers, cb) => {
Expand All @@ -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, {
Expand Down Expand Up @@ -179,7 +189,7 @@ describe('DelegatedContentRouting', function () {
let contentRouter
let cid

async.waterfall([
waterfall([
(cb) => {
const opts = delegatedNode.apiAddr.toOptions()
contentRouter = new DelegatedContentRouting(selfId, {
Expand All @@ -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)
Expand Down