diff --git a/src/commands.js b/src/commands.js index 8327e103a..059a60836 100644 --- a/src/commands.js +++ b/src/commands.js @@ -1,14 +1,19 @@ 'use strict' -const promisify = require('promisify-es6') -const moduleConfig = require('./utils/module-config') - -module.exports = (arg) => { - const send = moduleConfig(arg) - - return promisify((callback) => { - send({ - path: 'commands' - }, callback) - }) -} +const configure = require('./lib/configure') + +module.exports = configure(({ ky }) => { + return options => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + if (options.flags != null) searchParams.set('flags', options.flags) + + return ky.get('commands', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + } +}) diff --git a/src/dns.js b/src/dns.js index 3f734bfc2..5032e3fb5 100644 --- a/src/dns.js +++ b/src/dns.js @@ -1,25 +1,22 @@ 'use strict' -const promisify = require('promisify-es6') -const moduleConfig = require('./utils/module-config') +const configure = require('./lib/configure') -const transform = function (res, callback) { - callback(null, res.Path) -} +module.exports = configure(({ ky }) => { + return async (domain, options) => { + options = options || {} -module.exports = (arg) => { - const send = moduleConfig(arg) + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', domain) + if (options.recursive != null) searchParams.set('recursive', options.recursive) - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } + const res = await ky.post('dns', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() - send.andTransform({ - path: 'dns', - args: args, - qs: opts - }, transform, callback) - }) -} + return res.Path + } +}) diff --git a/src/id.js b/src/id.js index b98be8443..64fea10a9 100644 --- a/src/id.js +++ b/src/id.js @@ -1,32 +1,19 @@ 'use strict' -const promisify = require('promisify-es6') -const moduleConfig = require('./utils/module-config') +const configure = require('./lib/configure') +const toCamel = require('./lib/object-to-camel') -module.exports = (arg) => { - const send = moduleConfig(arg) +module.exports = configure(({ ky }) => { + return async options => { + options = options || {} - return promisify((opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = undefined - } + const res = await ky.get('id', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() - send({ - path: 'id', - args: opts - }, (err, result) => { - if (err) { - return callback(err) - } - const identity = { - id: result.ID, - publicKey: result.PublicKey, - addresses: result.Addresses, - agentVersion: result.AgentVersion, - protocolVersion: result.ProtocolVersion - } - callback(null, identity) - }) - }) -} + return toCamel(res) + } +}) diff --git a/src/mount.js b/src/mount.js index 7c2e92cab..a5d2faef1 100644 --- a/src/mount.js +++ b/src/mount.js @@ -1,30 +1,23 @@ 'use strict' -const promisify = require('promisify-es6') -const moduleConfig = require('./utils/module-config') +const configure = require('./lib/configure') +const toCamel = require('./lib/object-to-camel') -module.exports = (arg) => { - const send = moduleConfig(arg) +module.exports = configure(({ ky }) => { + return async options => { + options = options || {} - return promisify((ipfs, ipns, callback) => { - if (typeof ipfs === 'function') { - callback = ipfs - ipfs = null - } else if (typeof ipns === 'function') { - callback = ipns - ipns = null - } - const opts = {} - if (ipfs) { - opts.f = ipfs - } - if (ipns) { - opts.n = ipns - } + const searchParams = new URLSearchParams(options.searchParams) + if (options.ipfsPath != null) searchParams.set('ipfs-path', options.ipfsPath) + if (options.ipnsPath != null) searchParams.set('ipns-path', options.ipnsPath) - send({ - path: 'mount', - qs: opts - }, callback) - }) -} + const res = await ky.post('dns', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res) + } +}) diff --git a/src/ping-pull-stream.js b/src/ping-pull-stream.js deleted file mode 100644 index a7871faa3..000000000 --- a/src/ping-pull-stream.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict' - -const toPull = require('stream-to-pull-stream') -const deferred = require('pull-defer') -const pump = require('pump') -const moduleConfig = require('./utils/module-config') -const PingMessageStream = require('./utils/ping-message-stream') - -module.exports = (arg) => { - const send = moduleConfig(arg) - - return (id, opts = {}) => { - // Default number of packtes to 1 - if (!opts.n && !opts.count) { - opts.n = 1 - } - const request = { - path: 'ping', - args: id, - qs: opts - } - const p = deferred.source() - const response = new PingMessageStream() - - send(request, (err, stream) => { - if (err) { return p.abort(err) } - - pump(stream, response) - p.resolve(toPull.source(response)) - }) - - return p - } -} diff --git a/src/ping-readable-stream.js b/src/ping-readable-stream.js deleted file mode 100644 index df4f70401..000000000 --- a/src/ping-readable-stream.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict' - -const pump = require('pump') -const moduleConfig = require('./utils/module-config') -const PingMessageStream = require('./utils/ping-message-stream') - -module.exports = (arg) => { - const send = moduleConfig(arg) - - return (id, opts = {}) => { - // Default number of packtes to 1 - if (!opts.n && !opts.count) { - opts.n = 1 - } - const request = { - path: 'ping', - args: id, - qs: opts - } - - const response = new PingMessageStream() - - send(request, (err, stream) => { - if (err) { return response.emit('error', err) } - pump(stream, response) - }) - - return response - } -} diff --git a/src/ping.js b/src/ping.js index f3ff63ec8..33b275617 100644 --- a/src/ping.js +++ b/src/ping.js @@ -1,60 +1,27 @@ 'use strict' -const promisify = require('promisify-es6') -const pump = require('pump') -const Writable = require('readable-stream').Writable -const moduleConfig = require('./utils/module-config') -const PingMessageStream = require('./utils/ping-message-stream') - -module.exports = (arg) => { - const send = moduleConfig(arg) - - return promisify((id, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - - if (opts.n && opts.count) { - return callback(new Error('Use either n or count, not both')) - } - - // Default number of packtes to 1 - if (!opts.n && !opts.count) { - opts.n = 1 +const ndjson = require('iterable-ndjson') +const configure = require('./lib/configure') +const toIterable = require('./lib/stream-to-iterable') +const toCamel = require('./lib/object-to-camel') + +module.exports = configure(({ ky }) => { + return async function * ping (peerId, options) { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${peerId}`) + if (options.count != null) searchParams.set('count', options.count) + + const res = await ky.post('ping', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }) + + for await (const chunk of ndjson(toIterable(res.body))) { + yield toCamel(chunk) } - - const request = { - path: 'ping', - args: id, - qs: opts - } - - // Transform the response stream to a value: - // [{ success: , time: , text: }] - const transform = (stream, callback) => { - const messageConverter = new PingMessageStream() - const responses = [] - - pump( - stream, - messageConverter, - new Writable({ - objectMode: true, - write (chunk, enc, cb) { - responses.push(chunk) - cb() - } - }), - (err) => { - if (err) { - return callback(err) - } - callback(null, responses) - } - ) - } - - send.andTransform(request, transform, callback) - }) -} + } +}) diff --git a/src/resolve.js b/src/resolve.js index f48a3b02b..33c44b6e6 100644 --- a/src/resolve.js +++ b/src/resolve.js @@ -1,54 +1,25 @@ 'use strict' -const promisify = require('promisify-es6') -const multibase = require('multibase') -const CID = require('cids') - -module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - - opts = opts || {} - - if (opts.cidBase) { - opts['cid-base'] = opts.cidBase - delete opts.cidBase - } - - const transform = (res, callback) => { - if (!opts['cid-base']) { - return callback(null, res.Path) - } - - // FIXME: remove when go-ipfs supports ?cid-base for /api/v0/resolve - // https://github.com/ipfs/go-ipfs/pull/5777#issuecomment-439838555 - const parts = res.Path.split('/') // ['', 'ipfs', 'QmHash', ...] - - if (multibase.isEncoded(parts[2]) !== opts['cid-base']) { - try { - let cid = new CID(parts[2]) - - if (cid.version === 0 && opts['cid-base'] !== 'base58btc') { - cid = cid.toV1() - } - - parts[2] = cid.toBaseEncodedString(opts['cid-base']) - res.Path = parts.join('/') - } catch (err) { - return callback(err) - } - } - - callback(null, res.Path) - } - - send.andTransform({ - path: 'resolve', - args: args, - qs: opts - }, transform, callback) - }) -} +const configure = require('./lib/configure') + +module.exports = configure(({ ky }) => { + return async (path, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${path}`) + if (options.cidBase) searchParams.set('cid-base', options.cidBase) + if (options.dhtRecordCount) searchParams.set('dht-record-count', options.dhtRecordCount) + if (options.dhtTimeout) searchParams.set('dht-timeout', options.dhtTimeout) + if (options.recursive != null) searchParams.set('recursive', options.recursive) + + const res = await ky.post('resolve', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return res.Path + } +}) diff --git a/src/stop.js b/src/stop.js index 0da69eaa1..7cae46880 100644 --- a/src/stop.js +++ b/src/stop.js @@ -1,12 +1,16 @@ 'use strict' -const promisify = require('promisify-es6') -const moduleConfig = require('./utils/module-config') +const configure = require('./lib/configure') -module.exports = (arg) => { - const send = moduleConfig(arg) +module.exports = configure(({ ky }) => { + return options => { + options = options || {} - return promisify((callback) => { - send({ path: 'shutdown' }, callback) - }) -} + return ky.post('shutdown', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).text() + } +}) diff --git a/src/update.js b/src/update.js index aee1a64a6..91bea6f7f 100644 --- a/src/update.js +++ b/src/update.js @@ -1,41 +1,16 @@ 'use strict' -const promisify = require('promisify-es6') -const moduleConfig = require('./utils/module-config') +const configure = require('./lib/configure') -module.exports = (arg) => { - const send = moduleConfig(arg) +module.exports = configure(({ ky }) => { + return options => { + options = options || {} - return { - apply: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'update', - qs: opts - }, callback) - }), - check: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'update/check', - qs: opts - }, callback) - }), - log: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'update/log', - qs: opts - }, callback) - }) + return ky.post('update', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).text() } -} +}) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index a4a6dcd07..387584c13 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -17,6 +17,7 @@ function requireCommands (send, config) { const cat = require('../cat')(config) const get = require('../get')(config) const ls = require('../ls')(config) + const ping = require('../ping')(config) const refs = require('../refs')(config) const cmds = { @@ -55,6 +56,8 @@ function requireCommands (send, config) { catReadableStream: streamify.readable(cat), catPullStream: pullify.source(cat), _catAsyncIterator: cat, + commands: callbackify.variadic(require('../commands')(config)), + dns: callbackify.variadic(require('../dns')(config)), get: callbackify.variadic(async (path, options) => { const output = [] @@ -98,15 +101,24 @@ function requireCommands (send, config) { ) }, _getAsyncIterator: get, + id: callbackify.variadic(require('../id')(config)), ls: callbackify.variadic((path, options) => collectify(ls)(path, options)), lsReadableStream: streamify.readable(ls), lsPullStream: pullify.source(ls), _lsAsyncIterator: ls, + mount: callbackify.variadic(require('../mount')(config)), object: require('../object')(config), + ping: callbackify.variadic(collectify(ping)), + pingReadableStream: streamify.readable(ping), + pingPullStream: pullify.source(ping), refs: callbackify.variadic((path, options) => collectify(refs)(path, options)), refsReadableStream: streamify.readable(refs), refsPullStream: pullify.source(refs), _refsAsyncIterator: refs, + resolve: callbackify.variadic(require('../resolve')(config)), + stop: callbackify.variadic(require('../stop')(config)), + shutdown: callbackify.variadic(require('../stop')(config)), + version: callbackify.variadic(require('../version')(config)), getEndpointConfig: require('../get-endpoint-config')(config), bitswap: require('../bitswap')(config), block: require('../block')(config), @@ -129,26 +141,15 @@ function requireCommands (send, config) { const subCmds = { // Network name: require('../name'), - ping: require('../ping'), - pingReadableStream: require('../ping-readable-stream'), - pingPullStream: require('../ping-pull-stream'), swarm: require('../swarm'), pubsub: require('../pubsub'), - dns: require('../dns'), // Miscellaneous - commands: require('../commands'), - id: require('../id'), key: require('../key'), log: require('../log'), - mount: require('../mount'), repo: require('../repo'), - stop: require('../stop'), - shutdown: require('../stop'), stats: require('../stats'), - update: require('../update'), - version: require('../version'), - resolve: require('../resolve') + update: require('../update') } Object.keys(subCmds).forEach((file) => { diff --git a/src/version.js b/src/version.js index 438cdd000..31b1ed57c 100644 --- a/src/version.js +++ b/src/version.js @@ -1,30 +1,19 @@ 'use strict' -const promisify = require('promisify-es6') -const moduleConfig = require('./utils/module-config') +const configure = require('./lib/configure') +const toCamel = require('./lib/object-to-camel') -module.exports = (arg) => { - const send = moduleConfig(arg) +module.exports = configure(({ ky }) => { + return async options => { + options = options || {} - return promisify((opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } + const res = await ky.get('version', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() - send({ - path: 'version', - qs: opts - }, (err, result) => { - if (err) { - return callback(err) - } - const version = { - version: result.Version, - commit: result.Commit, - repo: result.Repo - } - callback(null, version) - }) - }) -} + return toCamel(res) + } +}) diff --git a/test/custom-headers.spec.js b/test/custom-headers.spec.js index 0780c23b9..ce14a01e5 100644 --- a/test/custom-headers.spec.js +++ b/test/custom-headers.spec.js @@ -4,23 +4,13 @@ const isNode = require('detect-node') const { expect } = require('interface-ipfs-core/src/utils/mocha') const ipfsClient = require('../src') -const f = require('./utils/factory') describe('custom headers', function () { // do not test in browser if (!isNode) { return } - this.timeout(50 * 1000) // slow CI let ipfs - let ipfsd // initialize ipfs with custom headers - before(async () => { - ipfsd = await f.spawn({ - initOptions: { - bits: 1024, - profile: 'test' - } - }) - + before(() => { ipfs = ipfsClient({ host: 'localhost', port: 6001, @@ -37,6 +27,7 @@ describe('custom headers', function () { req.on('data', () => {}) req.on('end', () => { res.writeHead(200) + res.write(JSON.stringify({})) res.end() // ensure custom headers are present expect(req.headers.authorization).to.equal('Bearer ' + 'YOLO') @@ -48,16 +39,10 @@ describe('custom headers', function () { server.listen(6001, () => { ipfs.id((err, res) => { if (err) { - throw new Error('Unexpected error.') + throw err } // this call is used to test that headers are being sent. }) }) }) - - after(async () => { - if (ipfsd) { - await ipfsd.stop() - } - }) }) diff --git a/test/ping.spec.js b/test/ping.spec.js index 46c58c890..10c131b75 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -6,7 +6,6 @@ const pull = require('pull-stream/pull') const collect = require('pull-stream/sinks/collect') const ipfsClient = require('../src') -const PingMessageStream = require('../src/utils/ping-message-stream') const f = require('./utils/factory') // Determine if a ping response object is a pong, or something else, like a status message @@ -58,23 +57,20 @@ describe('.ping', function () { } }) - it('.ping with default n', async () => { + it('.ping with default count', async () => { const res = await ipfs.ping(otherId) - expect(res).to.be.an('array') - expect(res.filter(isPong)).to.have.lengthOf(1) + expect(res.filter(isPong)).to.have.lengthOf(10) res.forEach(packet => { expect(packet).to.have.keys('success', 'time', 'text') expect(packet.time).to.be.a('number') }) - const resultMsg = res.find(packet => packet.text.includes('Average latency')) expect(resultMsg).to.exist() }) it('.ping with count = 2', async () => { const res = await ipfs.ping(otherId, { count: 2 }) - expect(res).to.be.an('array') expect(res.filter(isPong)).to.have.lengthOf(2) res.forEach(packet => { @@ -85,44 +81,13 @@ describe('.ping', function () { expect(resultMsg).to.exist() }) - it('.ping with n = 2', async () => { - const res = await ipfs.ping(otherId, { n: 2 }) - - expect(res).to.be.an('array') - expect(res.filter(isPong)).to.have.lengthOf(2) - res.forEach(packet => { - expect(packet).to.have.keys('success', 'time', 'text') - expect(packet.time).to.be.a('number') - }) - const resultMsg = res.find(packet => packet.text.includes('Average latency')) - expect(resultMsg).to.exist() - }) - - it('.ping fails with count & n', async function () { - this.timeout(20 * 1000) - - await expect(ipfs.ping(otherId, { count: 2, n: 2 })).to.be.rejected() - }) - - it('.ping with Promises', async () => { - const res = await ipfs.ping(otherId) - expect(res).to.be.an('array') - expect(res.filter(isPong)).to.have.lengthOf(1) - res.forEach(packet => { - expect(packet).to.have.keys('success', 'time', 'text') - expect(packet.time).to.be.a('number') - }) - const resultMsg = res.find(packet => packet.text.includes('Average latency')) - expect(resultMsg).to.exist() - }) - it('.pingPullStream', (done) => { pull( - ipfs.pingPullStream(otherId), + ipfs.pingPullStream(otherId, { count: 2 }), collect((err, data) => { expect(err).to.not.exist() expect(data).to.be.an('array') - expect(data.filter(isPong)).to.have.lengthOf(1) + expect(data.filter(isPong)).to.have.lengthOf(2) data.forEach(packet => { expect(packet).to.have.keys('success', 'time', 'text') expect(packet.time).to.be.a('number') @@ -136,7 +101,7 @@ describe('.ping', function () { it('.pingReadableStream', (done) => { let packetNum = 0 - ipfs.pingReadableStream(otherId) + ipfs.pingReadableStream(otherId, { count: 2 }) .on('data', data => { expect(data).to.be.an('object') expect(data).to.have.keys('success', 'time', 'text') @@ -146,15 +111,8 @@ describe('.ping', function () { expect(err).not.to.exist() }) .on('end', () => { - expect(packetNum).to.equal(1) + expect(packetNum).to.equal(2) done() }) }) - - it('message conversion fails if invalid message is received', () => { - const messageConverter = new PingMessageStream() - expect(() => { - messageConverter.write({ some: 'InvalidMessage' }) - }).to.throw('Invalid ping message received') - }) }) diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js index 62d05ce8e..0bb21eb68 100644 --- a/test/sub-modules.spec.js +++ b/test/sub-modules.spec.js @@ -67,9 +67,9 @@ describe('submodules', () => { }) it('ping', () => { - const ping = require('../src/ping')(config) - const pingPullStream = require('../src/ping-pull-stream')(config) - const pingReadableStream = require('../src/ping-readable-stream')(config) + const ping = require('../src')(config).ping + const pingPullStream = require('../src')(config).pingPullStream + const pingReadableStream = require('../src')(config).pingReadableStream expect(ping).to.be.a('function') expect(pingPullStream).to.be.a('function')