Skip to content
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
19 changes: 15 additions & 4 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -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()
}
}
Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1",
"it-all": "^1.0.0",
"multiaddr": "^7.2.1",
"p-defer": "^3.0.0",
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<PeerInfo>}
*/
async * findProviders (key, options = {}) {
Expand All @@ -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
Expand Down
46 changes: 26 additions & 20 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@
'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')

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
}
}
}
}
Expand Down Expand Up @@ -59,11 +68,7 @@ describe('DelegatedContentRouting', function () {
})

after(() => {
return Promise.all([
selfNode.stop(),
delegateNode.stop(),
bootstrapNode.stop()
])
return factory.clean()
})

describe('create', () => {
Expand Down Expand Up @@ -109,11 +114,11 @@ 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 () {
const opts = delegateNode.apiAddr.toOptions()
const routing = new DelegatedContentRouting(selfId, {
protocol: 'http',
Expand Down Expand Up @@ -153,13 +158,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')
})
})
})