From e37e0dda9aed7d29511cc92f684b5540ebf96f9b Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 14 Nov 2019 16:28:18 +0000 Subject: [PATCH 1/2] refactor: convert diag API to async/await License: MIT Signed-off-by: Alan Shaw --- src/diag/cmds.js | 30 +++++++++++++++++------------- src/diag/index.js | 16 ++++++---------- src/diag/net.js | 27 ++++++++++++++------------- src/diag/sys.js | 27 ++++++++++++++------------- src/utils/load-commands.js | 4 ++-- 5 files changed, 53 insertions(+), 51 deletions(-) diff --git a/src/diag/cmds.js b/src/diag/cmds.js index e717604fa..d34e1ce98 100644 --- a/src/diag/cmds.js +++ b/src/diag/cmds.js @@ -1,17 +1,21 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } +module.exports = configure(({ ky }) => { + return async (options) => { + options = options || {} - send({ - path: 'diag/cmds', - qs: opts - }, callback) - }) -} + const searchParams = new URLSearchParams(options.searchParams) + if (options.verbose != null) searchParams.set('verbose', options.verbose) + + const data = await ky.get('diag/cmds', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return data + } +}) diff --git a/src/diag/index.js b/src/diag/index.js index b24f7af53..7e48d576d 100644 --- a/src/diag/index.js +++ b/src/diag/index.js @@ -1,13 +1,9 @@ 'use strict' -const moduleConfig = require('../utils/module-config') +const callbackify = require('callbackify') -module.exports = (arg) => { - const send = moduleConfig(arg) - - return { - net: require('./net')(send), - sys: require('./sys')(send), - cmds: require('./cmds')(send) - } -} +module.exports = config => ({ + net: callbackify.variadic(require('./net')(config)), + sys: callbackify.variadic(require('./sys')(config)), + cmds: callbackify.variadic(require('./cmds')(config)) +}) diff --git a/src/diag/net.js b/src/diag/net.js index 9c84988d1..815a4c2ba 100644 --- a/src/diag/net.js +++ b/src/diag/net.js @@ -1,17 +1,18 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } +module.exports = configure(({ ky }) => { + return async (options) => { + options = options || {} - send({ - path: 'diag/net', - qs: opts - }, callback) - }) -} + const data = await ky.get('diag/net', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() + + return data + } +}) diff --git a/src/diag/sys.js b/src/diag/sys.js index c06968942..fe539a6ad 100644 --- a/src/diag/sys.js +++ b/src/diag/sys.js @@ -1,17 +1,18 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } +module.exports = configure(({ ky }) => { + return async (options) => { + options = options || {} - send({ - path: 'diag/sys', - qs: opts - }, callback) - }) -} + const data = await ky.get('diag/sys', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() + + return data + } +}) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 242b52463..dbd76367a 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -91,7 +91,8 @@ function requireCommands (send, config) { getEndpointConfig: require('../get-endpoint-config')(config), bitswap: require('../bitswap')(config), block: require('../block')(config), - dag: require('../dag')(config) + dag: require('../dag')(config), + diag: require('../diag')(config) } Object.assign(cmds.refs, { @@ -123,7 +124,6 @@ function requireCommands (send, config) { // Miscellaneous commands: require('../commands'), config: require('../config'), - diag: require('../diag'), id: require('../id'), key: require('../key'), log: require('../log'), From 6fee19fb83ded46c6d2c9019f2c4638f9b852b0c Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 14 Nov 2019 17:19:15 +0000 Subject: [PATCH 2/2] refactor: simplify --- src/diag/cmds.js | 6 ++---- src/diag/net.js | 6 ++---- src/diag/sys.js | 6 ++---- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/diag/cmds.js b/src/diag/cmds.js index d34e1ce98..68f20ca04 100644 --- a/src/diag/cmds.js +++ b/src/diag/cmds.js @@ -3,19 +3,17 @@ const configure = require('../lib/configure') module.exports = configure(({ ky }) => { - return async (options) => { + return options => { options = options || {} const searchParams = new URLSearchParams(options.searchParams) if (options.verbose != null) searchParams.set('verbose', options.verbose) - const data = await ky.get('diag/cmds', { + return ky.get('diag/cmds', { timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams }).json() - - return data } }) diff --git a/src/diag/net.js b/src/diag/net.js index 815a4c2ba..a7404b802 100644 --- a/src/diag/net.js +++ b/src/diag/net.js @@ -3,16 +3,14 @@ const configure = require('../lib/configure') module.exports = configure(({ ky }) => { - return async (options) => { + return options => { options = options || {} - const data = await ky.get('diag/net', { + return ky.get('diag/net', { timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams: options.searchParams }).json() - - return data } }) diff --git a/src/diag/sys.js b/src/diag/sys.js index fe539a6ad..a8cd91b6f 100644 --- a/src/diag/sys.js +++ b/src/diag/sys.js @@ -3,16 +3,14 @@ const configure = require('../lib/configure') module.exports = configure(({ ky }) => { - return async (options) => { + return options => { options = options || {} - const data = await ky.get('diag/sys', { + return ky.get('diag/sys', { timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams: options.searchParams }).json() - - return data } })