From 96b86f556db8cf0e32da8f547e9c611c5bf351ff Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Nov 2019 22:26:32 +0000 Subject: [PATCH 1/7] refactor: convert misc root level APIs to async/await License: MIT Signed-off-by: Alan Shaw --- src/commands.js | 29 ++++++++------ src/dns.js | 35 ++++++++--------- src/id.js | 41 +++++++------------- src/resolve.js | 75 ++++++++++++------------------------- src/stop.js | 20 ++++++---- src/utils/load-commands.js | 14 +++---- test/custom-headers.spec.js | 21 ++--------- 7 files changed, 92 insertions(+), 143 deletions(-) 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/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/utils/load-commands.js b/src/utils/load-commands.js index 3e51acdb2..a5d64c4cf 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -55,6 +55,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,6 +100,7 @@ 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), @@ -106,6 +109,9 @@ function requireCommands (send, config) { 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)), getEndpointConfig: require('../get-endpoint-config')(config), bitswap: require('../bitswap')(config), block: require('../block')(config), @@ -138,21 +144,15 @@ function requireCommands (send, config) { 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') + version: require('../version') } Object.keys(subCmds).forEach((file) => { 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() - } - }) }) From c3cd6803cc3be0a4eef4e96f52866d1e792f4bde Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 21 Nov 2019 10:57:26 +0000 Subject: [PATCH 2/7] refactor: convert mount API to async/await BREAKING CHANGE: The signature for `ipfs.mount` has changed from `ipfs.mount([ipfsPath], [ipnsPath])` to `ipfs.mount([options])`. Where `options` is an optional object that may contain two boolean properties `ipfsPath` and `ipnsPath`. The response object has also changed to be camel case. See https://docs.ipfs.io/reference/api/http/#api-v0-mount. License: MIT Signed-off-by: Alan Shaw --- src/mount.js | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) 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) + } +}) From e011c8e79815b12204cb7d34fccddc337ab39f29 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 21 Nov 2019 10:58:37 +0000 Subject: [PATCH 3/7] fix: load mount --- src/utils/load-commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index a5d64c4cf..0dfe1cb8e 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -105,6 +105,7 @@ function requireCommands (send, config) { lsReadableStream: streamify.readable(ls), lsPullStream: pullify.source(ls), _lsAsyncIterator: ls, + mount: require('../mount')(config), refs: callbackify.variadic((path, options) => collectify(refs)(path, options)), refsReadableStream: streamify.readable(refs), refsPullStream: pullify.source(refs), @@ -148,7 +149,6 @@ function requireCommands (send, config) { // Miscellaneous key: require('../key'), log: require('../log'), - mount: require('../mount'), repo: require('../repo'), stats: require('../stats'), update: require('../update'), From 2281599b7a1781a850cc8bd5b3c68a5ddfed57e5 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 21 Nov 2019 11:04:55 +0000 Subject: [PATCH 4/7] refactor: convert version API to async/await License: MIT Signed-off-by: Alan Shaw --- src/utils/load-commands.js | 6 +++--- src/version.js | 39 ++++++++++++++------------------------ 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 0dfe1cb8e..1068c9804 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -105,7 +105,7 @@ function requireCommands (send, config) { lsReadableStream: streamify.readable(ls), lsPullStream: pullify.source(ls), _lsAsyncIterator: ls, - mount: require('../mount')(config), + mount: callbackify.variadic(require('../mount')(config)), refs: callbackify.variadic((path, options) => collectify(refs)(path, options)), refsReadableStream: streamify.readable(refs), refsPullStream: pullify.source(refs), @@ -113,6 +113,7 @@ function requireCommands (send, config) { 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), @@ -151,8 +152,7 @@ function requireCommands (send, config) { log: require('../log'), repo: require('../repo'), stats: require('../stats'), - update: require('../update'), - version: require('../version') + 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) + } +}) From d8da835529afe46298185ec2c6ff4074772960d7 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 21 Nov 2019 11:22:38 +0000 Subject: [PATCH 5/7] refactor: convert ping API to async/await BREAKING CHANGE: Default ping `count` of 1 in client has been removed. The default ping count is now whatever the IPFS node defaults it to (currently 10). If you specifically need 1 ping message then please pass `count: 1` in options for `ipfs.ping()`. License: MIT Signed-off-by: Alan Shaw --- src/ping-pull-stream.js | 34 ---------------- src/ping-readable-stream.js | 30 -------------- src/ping.js | 81 +++++++++++-------------------------- src/utils/load-commands.js | 7 ++-- test/ping.spec.js | 54 +++---------------------- 5 files changed, 34 insertions(+), 172 deletions(-) delete mode 100644 src/ping-pull-stream.js delete mode 100644 src/ping-readable-stream.js 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/utils/load-commands.js b/src/utils/load-commands.js index 1068c9804..f26935cfc 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 = { @@ -106,6 +107,9 @@ function requireCommands (send, config) { lsPullStream: pullify.source(ls), _lsAsyncIterator: ls, mount: callbackify.variadic(require('../mount')(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), @@ -141,9 +145,6 @@ function requireCommands (send, config) { // Network name: require('../name'), - ping: require('../ping'), - pingReadableStream: require('../ping-readable-stream'), - pingPullStream: require('../ping-pull-stream'), swarm: require('../swarm'), pubsub: require('../pubsub'), 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') - }) }) From aa83f9112bb5c9ebf7965e9064e88b93a5c8ac36 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 21 Nov 2019 11:28:31 +0000 Subject: [PATCH 6/7] refactor: convert update API to async/await Other endpoints are not supported by go-ipfs or js-ipfs and so have been removed. License: MIT Signed-off-by: Alan Shaw --- src/update.js | 47 +++++++++++------------------------------------ 1 file changed, 11 insertions(+), 36 deletions(-) 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() } -} +}) From 3840f403af6c7974d0698a97e2fa5bc116b809e4 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 21 Nov 2019 11:44:36 +0000 Subject: [PATCH 7/7] fix: ping sub modules test --- test/sub-modules.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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')