diff --git a/package.json b/package.json index efcc3afaa7..53d9c3eb43 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "form-data": "^2.3.3", "hat": "0.0.3", "interface-ipfs-core": "~0.96.0", - "ipfsd-ctl": "~0.40.2", + "ipfsd-ctl": "~0.41.0", "ncp": "^2.0.0", "qs": "^6.5.2", "rimraf": "^2.6.2", @@ -128,7 +128,7 @@ "joi": "^14.3.0", "joi-browser": "^13.4.0", "joi-multiaddr": "^4.0.0", - "libp2p": "~0.24.3", + "libp2p": "libp2p/js-libp2p#master", "libp2p-bootstrap": "~0.9.3", "libp2p-crypto": "~0.16.0", "libp2p-kad-dht": "~0.14.4", diff --git a/src/cli/commands/daemon.js b/src/cli/commands/daemon.js index 8afcdbe3d6..ba8dbd642a 100644 --- a/src/cli/commands/daemon.js +++ b/src/cli/commands/daemon.js @@ -18,10 +18,6 @@ module.exports = { type: 'boolean', default: false }) - .option('enable-dht-experiment', { - type: 'boolean', - default: false - }) .option('offline', { desc: 'Run offline. Do not connect to the rest of the network but provide local API.', default: false diff --git a/src/cli/commands/dht/find-peer.js b/src/cli/commands/dht/find-peer.js index 45e7be5ffb..933919335f 100644 --- a/src/cli/commands/dht/find-peer.js +++ b/src/cli/commands/dht/find-peer.js @@ -9,12 +9,15 @@ module.exports = { builder: {}, - handler ({ ipfs, peerID, resolve }) { + handler ({ getIpfs, peerID, resolve }) { resolve((async () => { + const ipfs = await getIpfs() const peers = await ipfs.dht.findPeer(peerID) const addresses = peers.multiaddrs.toArray().map((ma) => ma.toString()) - print(addresses) + addresses.forEach((addr) => { + print(addr) + }) })()) } } diff --git a/src/cli/commands/dht/find-providers.js b/src/cli/commands/dht/find-providers.js index 1f06d854ec..46ec3bc90d 100644 --- a/src/cli/commands/dht/find-providers.js +++ b/src/cli/commands/dht/find-providers.js @@ -16,12 +16,13 @@ module.exports = { }, handler (argv) { - const { ipfs, key, resolve } = argv + const { getIpfs, key, resolve } = argv const opts = { maxNumProviders: argv['num-providers'] } resolve((async () => { + const ipfs = await getIpfs() const provs = await ipfs.dht.findProvs(key, opts) provs.forEach((element) => { diff --git a/src/cli/commands/dht/get.js b/src/cli/commands/dht/get.js index b12b5796a4..b2ba34b3fe 100644 --- a/src/cli/commands/dht/get.js +++ b/src/cli/commands/dht/get.js @@ -9,8 +9,9 @@ module.exports = { builder: {}, - handler ({ ipfs, key, resolve }) { + handler ({ getIpfs, key, resolve }) { resolve((async () => { + const ipfs = await getIpfs() const value = await ipfs.dht.get(key) print(value) diff --git a/src/cli/commands/dht/provide.js b/src/cli/commands/dht/provide.js index 27ad0e8f4e..a787f5aa95 100644 --- a/src/cli/commands/dht/provide.js +++ b/src/cli/commands/dht/provide.js @@ -13,11 +13,14 @@ module.exports = { } }, - handler ({ ipfs, key, resolve }) { - // TODO add recursive option + handler ({ getIpfs, key, recursive, resolve }) { + const opts = { + recursive + } resolve((async () => { - await ipfs.dht.provide(key) + const ipfs = await getIpfs() + await ipfs.dht.provide(key, opts) })()) } } diff --git a/src/cli/commands/dht/put.js b/src/cli/commands/dht/put.js index 8bf5a348c5..068e077eee 100644 --- a/src/cli/commands/dht/put.js +++ b/src/cli/commands/dht/put.js @@ -7,8 +7,9 @@ module.exports = { builder: {}, - handler ({ ipfs, key, value, resolve }) { + handler ({ getIpfs, key, value, resolve }) { resolve((async () => { + const ipfs = await getIpfs() await ipfs.dht.put(key, value) })()) } diff --git a/src/cli/commands/dht/query.js b/src/cli/commands/dht/query.js index e5249ee9a4..dd5e716257 100644 --- a/src/cli/commands/dht/query.js +++ b/src/cli/commands/dht/query.js @@ -9,8 +9,9 @@ module.exports = { builder: {}, - handler ({ ipfs, peerID, resolve }) { + handler ({ getIpfs, peerID, resolve }) { resolve((async () => { + const ipfs = await getIpfs() const result = await ipfs.dht.query(peerID) result.forEach((peerID) => { diff --git a/src/core/components/dht.js b/src/core/components/dht.js index e969367d54..e5fc62dbec 100644 --- a/src/core/components/dht.js +++ b/src/core/components/dht.js @@ -6,7 +6,8 @@ const PeerId = require('peer-id') const PeerInfo = require('peer-info') const CID = require('cids') const each = require('async/each') -const setImmediate = require('async/setImmediate') +const nextTick = require('async/nextTick') + const errcode = require('err-code') const debug = require('debug') @@ -38,7 +39,7 @@ module.exports = (self) => { } catch (err) { log.error(err) - return setImmediate(() => callback(errcode(err, 'ERR_INVALID_CID'))) + return nextTick(() => callback(errcode(err, 'ERR_INVALID_CID'))) } } @@ -64,7 +65,7 @@ module.exports = (self) => { } catch (err) { log.error(err) - return setImmediate(() => callback(errcode(err, 'ERR_INVALID_CID'))) + return nextTick(() => callback(errcode(err, 'ERR_INVALID_CID'))) } } @@ -95,7 +96,7 @@ module.exports = (self) => { } catch (err) { log.error(err) - return setImmediate(() => callback(errcode(err, 'ERR_INVALID_CID'))) + return nextTick(() => callback(errcode(err, 'ERR_INVALID_CID'))) } } diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 8f73abaf25..c45b65f625 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -77,7 +77,10 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { }, dht: { kBucketSize: get(options, 'dht.kBucketSize', 20), - enabledDiscovery: get(options, 'dht.enabledDiscovery', true), + enabled: get(options, 'dht.enabled', true) && !(get(options, 'offline', false)), + randomWalk: { + enabled: get(options, 'dht.randomWalk.enabled', true) + }, validators: { ipns: ipnsUtils.validator }, @@ -86,7 +89,6 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { } }, EXPERIMENTAL: { - dht: !(get(options, 'local', false)), pubsub: get(options, 'EXPERIMENTAL.pubsub', false) } }, diff --git a/src/core/runtime/libp2p-browser.js b/src/core/runtime/libp2p-browser.js index 19eb7fd848..8fae1ae176 100644 --- a/src/core/runtime/libp2p-browser.js +++ b/src/core/runtime/libp2p-browser.js @@ -54,11 +54,9 @@ class Node extends libp2p { } }, dht: { - kBucketSize: 20, - enabledDiscovery: true + enabled: false }, EXPERIMENTAL: { - dht: true, pubsub: false } } diff --git a/src/core/runtime/libp2p-nodejs.js b/src/core/runtime/libp2p-nodejs.js index acf3f2af8c..01e7046e19 100644 --- a/src/core/runtime/libp2p-nodejs.js +++ b/src/core/runtime/libp2p-nodejs.js @@ -54,10 +54,12 @@ class Node extends libp2p { }, dht: { kBucketSize: 20, - enabledDiscovery: true + enabled: true, + randomWalk: { + enabled: true + } }, EXPERIMENTAL: { - dht: true, pubsub: false } } diff --git a/src/http/api/resources/dht.js b/src/http/api/resources/dht.js index 48755716d1..c88710c303 100644 --- a/src/http/api/resources/dht.js +++ b/src/http/api/resources/dht.js @@ -1,6 +1,7 @@ 'use strict' const Joi = require('joi') +const Boom = require('boom') const CID = require('cids') @@ -16,34 +17,27 @@ exports.findPeer = { arg: Joi.string().required() }).unknown() }, - handler: (request, reply) => { + async handler (request, h) { const ipfs = request.server.app.ipfs const { arg } = request.query + let res - ipfs.dht.findPeer(arg, (err, res) => { - if (err) { - log.error(err) - - if (err.code === 'ERR_LOOKUP_FAILED') { - return reply({ - Message: err.toString(), - Code: 0 - }).code(404) - } - - return reply({ - Message: err.toString(), - Code: 0 - }).code(500) + try { + res = await ipfs.dht.findPeer(arg) + } catch (err) { + if (err.code === 'ERR_LOOKUP_FAILED') { + throw Boom.notFound(err.toString()) + } else { + throw Boom.boomify(err, { message: err.toString() }) } + } - reply({ - Responses: [{ - ID: res.id.toB58String(), - Addrs: res.multiaddrs.toArray().map((a) => a.toString()) - }], - Type: 2 - }) + return h.response({ + Responses: [{ + ID: res.id.toB58String(), + Addrs: res.multiaddrs.toArray().map((a) => a.toString()) + }], + Type: 2 }) } } @@ -56,29 +50,20 @@ exports.findProvs = { timeout: Joi.number() }).unknown() }, - handler: (request, reply) => { + async handler (request, h) { const ipfs = request.server.app.ipfs const { arg } = request.query request.query.maxNumProviders = request.query['num-providers'] - ipfs.dht.findProvs(arg, request.query, (err, res) => { - if (err) { - log.error(err) - - return reply({ - Message: err.toString(), - Code: 0 - }).code(500) - } + const res = await ipfs.dht.findProvs(arg, request.query) - reply({ - Responses: res.map((peerInfo) => ({ - ID: peerInfo.id.toB58String(), - Addrs: peerInfo.multiaddrs.toArray().map((a) => a.toString()) - })), - Type: 4 - }) + return h.response({ + Responses: res.map((peerInfo) => ({ + ID: peerInfo.id.toB58String(), + Addrs: peerInfo.multiaddrs.toArray().map((a) => a.toString()) + })), + Type: 4 }) } } @@ -90,24 +75,15 @@ exports.get = { timeout: Joi.number() }).unknown() }, - handler: (request, reply) => { + async handler (request, h) { const ipfs = request.server.app.ipfs const { arg } = request.query - ipfs.dht.get(Buffer.from(arg), (err, res) => { - if (err) { - log.error(err) + const res = await ipfs.dht.get(Buffer.from(arg)) - return reply({ - Message: err.toString(), - Code: 0 - }).code(500) - } - - reply({ - Extra: res.toString(), - Type: 5 - }) + return h.response({ + Extra: res.toString(), + Type: 5 }) } } @@ -118,7 +94,7 @@ exports.provide = { arg: Joi.string().required() }).unknown() }, - handler: (request, reply) => { + async handler (request, h) { const ipfs = request.server.app.ipfs const { arg } = request.query let cid @@ -127,25 +103,12 @@ exports.provide = { cid = new CID(arg) } catch (err) { log.error(err) - - return reply({ - Message: err.toString(), - Code: 0 - }).code(500) + throw Boom.boomify(err, { message: err.toString() }) } - ipfs.dht.provide(cid, request.query, (err) => { - if (err) { - log.error(err) - - return reply({ - Message: err.toString(), - Code: 0 - }).code(500) - } + await ipfs.dht.provide(cid) - reply({}) - }) + return h.response() } } @@ -155,29 +118,20 @@ exports.put = { arg: Joi.array().items(Joi.string()).length(2).required() }).unknown() }, - parseArgs: (request, reply) => { - return reply({ + parseArgs: (request, h) => { + return { key: request.query.arg[0], value: request.query.arg[1] - }) + } }, - handler: (request, reply) => { + async handler (request, h) { const key = request.pre.args.key const value = request.pre.args.value const ipfs = request.server.app.ipfs - ipfs.dht.put(Buffer.from(key), Buffer.from(value), (err) => { - if (err) { - log.error(err) - - return reply({ - Message: err.toString(), - Code: 0 - }).code(500) - } + await ipfs.dht.put(Buffer.from(key), Buffer.from(value)) - reply({}) - }) + return h.response() } } @@ -187,25 +141,15 @@ exports.query = { arg: Joi.string().required() }).unknown() }, - handler: (request, reply) => { + async handler (request, h) { const ipfs = request.server.app.ipfs const { arg } = request.query - ipfs.dht.query(arg, (err, res) => { - if (err) { - log.error(err) - - return reply({ - Message: err.toString(), - Code: 0 - }).code(500) - } + const res = await ipfs.dht.query(arg) + const response = res.map((peerInfo) => ({ + ID: peerInfo.id.toB58String() + })) - const response = res.map((peerInfo) => ({ - ID: peerInfo.id.toB58String() - })) - - reply(response) - }) + return h.response(response) } } diff --git a/src/http/api/routes/dht.js b/src/http/api/routes/dht.js index e6d760337a..3dde96b9bd 100644 --- a/src/http/api/routes/dht.js +++ b/src/http/api/routes/dht.js @@ -15,9 +15,9 @@ module.exports = [ method: '*', path: '/api/v0/dht/findprovs', options: { - validate: resources.dht.findprovs.validate + validate: resources.dht.findProvs.validate }, - handler: resources.dht.findprovs.handler + handler: resources.dht.findProvs.handler }, { method: '*', @@ -39,6 +39,9 @@ module.exports = [ method: '*', path: '/api/v0/dht/put', options: { + pre: [ + { method: resources.dht.put.parseArgs, assign: 'args' } + ], validate: resources.dht.put.validate }, handler: resources.dht.put.handler diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index 261bbf3f54..b7aaace791 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -102,7 +102,7 @@ describe('interface-ipfs-core tests', function () { tests.name(CommonFactory.create({ spawnOptions: { - args: ['--pass ipfs-is-awesome-software', '--local'], + args: ['--pass ipfs-is-awesome-software', '--offline'], initOptions: { bits: 512 }, config: { Bootstrap: [], diff --git a/test/core/libp2p.spec.js b/test/core/libp2p.spec.js index cd360f057d..c77709c9ee 100644 --- a/test/core/libp2p.spec.js +++ b/test/core/libp2p.spec.js @@ -13,6 +13,7 @@ const PeerBook = require('peer-book') const WebSocketStar = require('libp2p-websocket-star') const Multiplex = require('libp2p-mplex') const SECIO = require('libp2p-secio') +const KadDHT = require('libp2p-kad-dht') const Libp2p = require('libp2p') const libp2pComponent = require('../../src/core/components/libp2p') @@ -45,7 +46,6 @@ describe('libp2p customization', function () { } }, EXPERIMENTAL: { - dht: false, pubsub: false } } @@ -94,7 +94,8 @@ describe('libp2p customization', function () { ], peerDiscovery: [ wsstar.discovery - ] + ], + dht: KadDHT } }) } @@ -144,7 +145,6 @@ describe('libp2p customization', function () { } }, EXPERIMENTAL: { - dht: true, pubsub: false } }) @@ -172,7 +172,6 @@ describe('libp2p customization', function () { } }, EXPERIMENTAL: { - dht: false, pubsub: true }, libp2p: { @@ -209,7 +208,6 @@ describe('libp2p customization', function () { } }, EXPERIMENTAL: { - dht: true, pubsub: true } }) diff --git a/test/core/name.js b/test/core/name.js index 972d5c25ed..ed73592d28 100644 --- a/test/core/name.js +++ b/test/core/name.js @@ -50,7 +50,7 @@ describe('name', function () { this.timeout(50 * 1000) df.spawn({ exec: IPFS, - args: [`--pass ${hat()}`, '--local'], + args: [`--pass ${hat()}`, '--offline'], config: { Bootstrap: [] } }, (err, _ipfsd) => { expect(err).to.not.exist() @@ -152,7 +152,7 @@ describe('name', function () { this.timeout(40 * 1000) df.spawn({ exec: IPFS, - args: [`--pass ${hat()}`, '--local'], + args: [`--pass ${hat()}`, '--offline'], config: { Bootstrap: [] } }, (err, _ipfsd) => { expect(err).to.not.exist() @@ -474,7 +474,7 @@ describe('name', function () { this.timeout(40 * 1000) df.spawn({ exec: IPFS, - args: [`--pass ${hat()}`, '--local'], + args: [`--pass ${hat()}`, '--offline'], config: { Bootstrap: [], Discovery: { diff --git a/test/core/ping.spec.js b/test/core/ping.spec.js index a3f220f2d9..5b784c5428 100644 --- a/test/core/ping.spec.js +++ b/test/core/ping.spec.js @@ -29,7 +29,7 @@ const config = { } function spawnNode ({ dht = false, type = 'js' }, cb) { - const args = dht ? [] : ['--local'] + const args = dht ? [] : ['--offline'] const factory = type === 'js' ? df : dfProc factory.spawn({ args, diff --git a/test/core/preload.spec.js b/test/core/preload.spec.js index 2b27a068aa..c153e95d59 100644 --- a/test/core/preload.spec.js +++ b/test/core/preload.spec.js @@ -44,17 +44,17 @@ describe('preload', () => { }) after(function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.stop(done) }) after(function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) repo.teardown(done) }) it('should preload content added with add', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.add(Buffer.from(hat()), (err, res) => { expect(err).to.not.exist() MockPreloadNode.waitForCids(res[0].hash, done) @@ -62,7 +62,7 @@ describe('preload', () => { }) it('should preload multiple content added with add', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.add([{ content: Buffer.from(hat()) }, { @@ -76,7 +76,7 @@ describe('preload', () => { }) it('should preload multiple content and intermediate dirs added with add', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.add([{ path: 'dir0/dir1/file0', content: Buffer.from(hat()) @@ -97,7 +97,7 @@ describe('preload', () => { }) it('should preload multiple content and wrapping dir for content added with add and wrapWithDirectory option', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.add([{ path: 'dir0/dir1/file0', content: Buffer.from(hat()) @@ -118,7 +118,7 @@ describe('preload', () => { }) it('should preload content retrieved with cat', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.add(Buffer.from(hat()), { preload: false }, (err, res) => { expect(err).to.not.exist() ipfs.cat(res[0].hash, (err) => { @@ -129,7 +129,7 @@ describe('preload', () => { }) it('should preload content retrieved with get', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.add(Buffer.from(hat()), { preload: false }, (err, res) => { expect(err).to.not.exist() ipfs.get(res[0].hash, (err) => { @@ -140,7 +140,7 @@ describe('preload', () => { }) it('should preload content retrieved with ls', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.add([{ path: 'dir0/dir1/file0', content: Buffer.from(hat()) @@ -169,7 +169,7 @@ describe('preload', () => { }) it('should preload content added with object.new', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.object.new((err, cid) => { expect(err).to.not.exist() MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done) @@ -177,7 +177,7 @@ describe('preload', () => { }) it('should preload content added with object.put', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, cid) => { expect(err).to.not.exist() MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done) @@ -185,7 +185,7 @@ describe('preload', () => { }) it('should preload content added with object.patch.addLink', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) parallel({ parent: (cb) => { waterfall([ @@ -214,7 +214,7 @@ describe('preload', () => { }) it('should preload content added with object.patch.rmLink', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) waterfall([ (cb) => ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, cb), (cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid })), @@ -239,7 +239,7 @@ describe('preload', () => { }) it('should preload content added with object.patch.setData', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, cid) => { expect(err).to.not.exist() @@ -251,7 +251,7 @@ describe('preload', () => { }) it('should preload content added with object.patch.appendData', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, cid) => { expect(err).to.not.exist() @@ -263,7 +263,7 @@ describe('preload', () => { }) it('should preload content retrieved with object.get', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.object.new(null, { preload: false }, (err, cid) => { expect(err).to.not.exist() @@ -275,7 +275,7 @@ describe('preload', () => { }) it('should preload content added with block.put', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.block.put(Buffer.from(hat()), (err, block) => { expect(err).to.not.exist() MockPreloadNode.waitForCids(block.cid.toBaseEncodedString(), done) @@ -283,7 +283,7 @@ describe('preload', () => { }) it('should preload content retrieved with block.get', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.block.put(Buffer.from(hat()), { preload: false }, (err, block) => { expect(err).to.not.exist() ipfs.block.get(block.cid, (err) => { @@ -294,7 +294,7 @@ describe('preload', () => { }) it('should preload content retrieved with block.stat', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) ipfs.block.put(Buffer.from(hat()), { preload: false }, (err, block) => { expect(err).to.not.exist() ipfs.block.stat(block.cid, (err) => { @@ -305,7 +305,7 @@ describe('preload', () => { }) it('should preload content added with dag.put', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) const obj = { test: hat() } ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => { expect(err).to.not.exist() @@ -314,7 +314,7 @@ describe('preload', () => { }) it('should preload content retrieved with dag.get', function (done) { - this.timeout(10 * 1000) + this.timeout(20 * 1000) const obj = { test: hat() } const opts = { format: 'dag-cbor', hashAlg: 'sha2-256', preload: false } ipfs.dag.put(obj, opts, (err, cid) => { diff --git a/test/http-api/inject/dht.js b/test/http-api/inject/dht.js index fb60c3758b..f614ee50dc 100644 --- a/test/http-api/inject/dht.js +++ b/test/http-api/inject/dht.js @@ -12,144 +12,132 @@ module.exports = (http) => { let api before(() => { - api = http.api.server.select('API') + api = http.api._apiServer }) describe('/findpeer', () => { - it('returns 400 if no peerId is provided', (done) => { - api.inject({ + it('returns 400 if no peerId is provided', async () => { + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/findpeer` - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result.Code).to.be.eql(1) - done() }) + + expect(res.statusCode).to.equal(400) + expect(res.result.Code).to.be.eql(1) }) - it('returns 404 if peerId is provided as there is no peers in the routing table', (done) => { + it('returns 404 if peerId is provided as there is no peers in the routing table', async () => { const peerId = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A' - - api.inject({ + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/findpeer?arg=${peerId}` - }, (res) => { - expect(res.statusCode).to.equal(404) - done() }) + + expect(res.statusCode).to.equal(404) }) }) describe('/findprovs', () => { - it('returns 400 if no key is provided', (done) => { - api.inject({ + it('returns 400 if no key is provided', async () => { + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/findprovs` - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result.Code).to.be.eql(1) - done() }) + + expect(res.statusCode).to.equal(400) + expect(res.result.Code).to.be.eql(1) }) - it('returns 200 if key is provided', (done) => { + it('returns 200 if key is provided', async () => { const key = 'Qmc77hSNykXJ6Jxp1C6RpD8VENV7RK6JD7eAcWpc7nEZx2' - - api.inject({ + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/findprovs?arg=${key}` - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Type).to.be.eql(4) - done() }) + + expect(res.statusCode).to.equal(200) + expect(res.result.Type).to.be.eql(4) }) }) describe('/get', () => { - it('returns 400 if no key is provided', (done) => { - api.inject({ + it('returns 400 if no key is provided', async () => { + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/get` - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result.Code).to.be.eql(1) - done() }) + + expect(res.statusCode).to.equal(400) + expect(res.result.Code).to.be.eql(1) }) - it('returns 200 if key is provided', (done) => { + it('returns 200 if key is provided', async () => { const key = 'key' const value = 'value' - api.inject({ + let res = await api.inject({ method: 'GET', url: `/api/v0/dht/put?arg=${key}&arg=${value}` - }, (res) => { - expect(res.statusCode).to.equal(200) - - api.inject({ - method: 'GET', - url: `/api/v0/dht/get?arg=${key}` - }, (res) => { - expect(res.statusCode).to.equal(200) - expect(res.result.Type).to.be.eql(5) - expect(res.result.Extra).to.be.eql(value) - done() - }) }) + + expect(res.statusCode).to.equal(200) + + res = await api.inject({ + method: 'GET', + url: `/api/v0/dht/get?arg=${key}` + }) + + expect(res.statusCode).to.equal(200) + expect(res.result.Type).to.be.eql(5) + expect(res.result.Extra).to.be.eql(value) }) }) describe('/provide', () => { - it('returns 400 if no key is provided', (done) => { - api.inject({ + it('returns 400 if no key is provided', async () => { + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/provide` - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result.Code).to.be.eql(1) - done() }) + + expect(res.statusCode).to.equal(400) + expect(res.result.Code).to.be.eql(1) }) - it('returns 500 if key is provided as the file was not added', (done) => { + it('returns 500 if key is provided as the file was not added', async () => { const key = 'Qmc77hSNykXJ6Jxp1C6RpD8VENV7RK6JD7eAcWpc7nEZx2' - - api.inject({ + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/provide?arg=${key}` - }, (res) => { - expect(res.statusCode).to.equal(500) // needs file add - done() }) + + expect(res.statusCode).to.equal(500) // needs file add }) }) describe('/put', () => { - it('returns 400 if no key or value is provided', (done) => { - api.inject({ + it('returns 400 if no key or value is provided', async () => { + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/put` - }, (res) => { - expect(res.statusCode).to.equal(400) - expect(res.result.Code).to.be.eql(1) - done() }) + + expect(res.statusCode).to.equal(400) + expect(res.result.Code).to.be.eql(1) }) - it('returns 200 if key and value is provided', function (done) { + it('returns 200 if key and value is provided', async function () { this.timeout(60 * 1000) + const key = 'key' const value = 'value' - api.inject({ + const res = await api.inject({ method: 'GET', url: `/api/v0/dht/put?arg=${key}&arg=${value}` - }, (res) => { - expect(res.statusCode).to.equal(200) - done() }) + + expect(res.statusCode).to.equal(200) }) }) }) diff --git a/test/http-api/inject/index.js b/test/http-api/inject/index.js deleted file mode 100644 index 002f8dc4e0..0000000000 --- a/test/http-api/inject/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const fs = require('fs') -const hat = require('hat') -const API = require('../../../src/http/index') -const promisify = require('promisify-es6') -const ncp = promisify(require('ncp').ncp) -const path = require('path') -const clean = require('../../utils/clean') - -describe('HTTP API', () => { - const repoExample = path.join(__dirname, '../../fixtures/go-ipfs-repo') - const repoTests = path.join(__dirname, '../../repo-tests-run') - - let http = {} - - const startHttpAPI = async () => { - http.api = new API({ - repo: repoTests, - pass: hat(), - config: { Bootstrap: [] }, - EXPERIMENTAL: { - pubsub: true - } - }) - await ncp(repoExample, repoTests) - await http.api.start() - } - - before(async function () { - this.timeout(60 * 1000) - await startHttpAPI() - }) - - after(async () => { - await http.api.stop() - clean(repoTests) - }) - - describe('## http-api spec tests', () => { - fs.readdirSync(path.join(__dirname)) - .forEach((file) => file !== 'index.js' && require(`./${file}`)(http)) - }) -}) diff --git a/test/http-api/routes.js b/test/http-api/routes.js index be0f0252c5..70459438ca 100644 --- a/test/http-api/routes.js +++ b/test/http-api/routes.js @@ -4,11 +4,11 @@ const fs = require('fs') const chai = require('chai') const dirtyChai = require('dirty-chai') -const expect = chai.expect chai.use(dirtyChai) const hat = require('hat') const API = require('../../src/http/index') -const ncp = require('ncp').ncp +const promisify = require('promisify-es6') +const ncp = promisify(require('ncp').ncp) const path = require('path') const clean = require('../utils/clean')