From 0e011873747f9d8754d26ca21792df865fda8b1e Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 14 Nov 2019 20:41:21 +0000 Subject: [PATCH] refactor: convert diag API to async/await (#1157) License: MIT Signed-off-by: Alan Shaw --- src/diag/cmds.js | 28 +++++++++++++++------------- src/diag/index.js | 16 ++++++---------- src/diag/net.js | 25 ++++++++++++------------- src/diag/sys.js | 25 ++++++++++++------------- src/utils/load-commands.js | 4 ++-- 5 files changed, 47 insertions(+), 51 deletions(-) diff --git a/src/diag/cmds.js b/src/diag/cmds.js index e717604fa..68f20ca04 100644 --- a/src/diag/cmds.js +++ b/src/diag/cmds.js @@ -1,17 +1,19 @@ '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 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) + + return ky.get('diag/cmds', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + } +}) 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..a7404b802 100644 --- a/src/diag/net.js +++ b/src/diag/net.js @@ -1,17 +1,16 @@ '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 options => { + options = options || {} - send({ - path: 'diag/net', - qs: opts - }, callback) - }) -} + return ky.get('diag/net', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() + } +}) diff --git a/src/diag/sys.js b/src/diag/sys.js index c06968942..a8cd91b6f 100644 --- a/src/diag/sys.js +++ b/src/diag/sys.js @@ -1,17 +1,16 @@ '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 options => { + options = options || {} - send({ - path: 'diag/sys', - qs: opts - }, callback) - }) -} + return ky.get('diag/sys', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() + } +}) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index fae44d26c..14545b564 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -94,7 +94,8 @@ function requireCommands (send, config) { bootstrap: require('../bootstrap')(config), config: require('../config')(config), dag: require('../dag')(config), - dht: require('../dht')(config) + dht: require('../dht')(config), + diag: require('../diag')(config) } Object.assign(cmds.refs, { @@ -123,7 +124,6 @@ function requireCommands (send, config) { // Miscellaneous commands: require('../commands'), - diag: require('../diag'), id: require('../id'), key: require('../key'), log: require('../log'),