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

Commit 0e01187

Browse files
author
Alan Shaw
authored
refactor: convert diag API to async/await (#1157)
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 5110423 commit 0e01187

File tree

5 files changed

+47
-51
lines changed

5 files changed

+47
-51
lines changed

src/diag/cmds.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
3+
const configure = require('../lib/configure')
44

5-
module.exports = (send) => {
6-
return promisify((opts, callback) => {
7-
if (typeof (opts) === 'function') {
8-
callback = opts
9-
opts = {}
10-
}
5+
module.exports = configure(({ ky }) => {
6+
return options => {
7+
options = options || {}
118

12-
send({
13-
path: 'diag/cmds',
14-
qs: opts
15-
}, callback)
16-
})
17-
}
9+
const searchParams = new URLSearchParams(options.searchParams)
10+
if (options.verbose != null) searchParams.set('verbose', options.verbose)
11+
12+
return ky.get('diag/cmds', {
13+
timeout: options.timeout,
14+
signal: options.signal,
15+
headers: options.headers,
16+
searchParams
17+
}).json()
18+
}
19+
})

src/diag/index.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
'use strict'
22

3-
const moduleConfig = require('../utils/module-config')
3+
const callbackify = require('callbackify')
44

5-
module.exports = (arg) => {
6-
const send = moduleConfig(arg)
7-
8-
return {
9-
net: require('./net')(send),
10-
sys: require('./sys')(send),
11-
cmds: require('./cmds')(send)
12-
}
13-
}
5+
module.exports = config => ({
6+
net: callbackify.variadic(require('./net')(config)),
7+
sys: callbackify.variadic(require('./sys')(config)),
8+
cmds: callbackify.variadic(require('./cmds')(config))
9+
})

src/diag/net.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
3+
const configure = require('../lib/configure')
44

5-
module.exports = (send) => {
6-
return promisify((opts, callback) => {
7-
if (typeof (opts) === 'function') {
8-
callback = opts
9-
opts = {}
10-
}
5+
module.exports = configure(({ ky }) => {
6+
return options => {
7+
options = options || {}
118

12-
send({
13-
path: 'diag/net',
14-
qs: opts
15-
}, callback)
16-
})
17-
}
9+
return ky.get('diag/net', {
10+
timeout: options.timeout,
11+
signal: options.signal,
12+
headers: options.headers,
13+
searchParams: options.searchParams
14+
}).json()
15+
}
16+
})

src/diag/sys.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
3+
const configure = require('../lib/configure')
44

5-
module.exports = (send) => {
6-
return promisify((opts, callback) => {
7-
if (typeof (opts) === 'function') {
8-
callback = opts
9-
opts = {}
10-
}
5+
module.exports = configure(({ ky }) => {
6+
return options => {
7+
options = options || {}
118

12-
send({
13-
path: 'diag/sys',
14-
qs: opts
15-
}, callback)
16-
})
17-
}
9+
return ky.get('diag/sys', {
10+
timeout: options.timeout,
11+
signal: options.signal,
12+
headers: options.headers,
13+
searchParams: options.searchParams
14+
}).json()
15+
}
16+
})

src/utils/load-commands.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ function requireCommands (send, config) {
9494
bootstrap: require('../bootstrap')(config),
9595
config: require('../config')(config),
9696
dag: require('../dag')(config),
97-
dht: require('../dht')(config)
97+
dht: require('../dht')(config),
98+
diag: require('../diag')(config)
9899
}
99100

100101
Object.assign(cmds.refs, {
@@ -123,7 +124,6 @@ function requireCommands (send, config) {
123124

124125
// Miscellaneous
125126
commands: require('../commands'),
126-
diag: require('../diag'),
127127
id: require('../id'),
128128
key: require('../key'),
129129
log: require('../log'),

0 commit comments

Comments
 (0)