Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
refactor: convert diag API to async/await (#1157)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw authored Nov 14, 2019
1 parent 5110423 commit 0e01187
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 51 deletions.
28 changes: 15 additions & 13 deletions src/diag/cmds.js
Original file line number Diff line number Diff line change
@@ -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()
}
})
16 changes: 6 additions & 10 deletions src/diag/index.js
Original file line number Diff line number Diff line change
@@ -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))
})
25 changes: 12 additions & 13 deletions src/diag/net.js
Original file line number Diff line number Diff line change
@@ -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()
}
})
25 changes: 12 additions & 13 deletions src/diag/sys.js
Original file line number Diff line number Diff line change
@@ -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()
}
})
4 changes: 2 additions & 2 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -123,7 +124,6 @@ function requireCommands (send, config) {

// Miscellaneous
commands: require('../commands'),
diag: require('../diag'),
id: require('../id'),
key: require('../key'),
log: require('../log'),
Expand Down

0 comments on commit 0e01187

Please sign in to comment.