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

Commit 14c5b9e

Browse files
author
Alan Shaw
authored
refactor: convert bootstrap API to async/await (#1154)
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 72fdc8c commit 14c5b9e

File tree

5 files changed

+64
-74
lines changed

5 files changed

+64
-74
lines changed

src/bootstrap/add.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
3+
const Multiaddr = require('multiaddr')
4+
const configure = require('../lib/configure')
45

5-
module.exports = (send) => {
6-
return promisify((args, opts, callback) => {
7-
if (typeof opts === 'function' &&
8-
!callback) {
9-
callback = opts
10-
opts = {}
6+
module.exports = configure(({ ky }) => {
7+
return async (addr, options) => {
8+
if (addr && typeof addr === 'object' && !Multiaddr.isMultiaddr(addr)) {
9+
options = addr
10+
addr = null
1111
}
1212

13-
// opts is the real callback --
14-
// 'callback' is being injected by promisify
15-
if (typeof opts === 'function' &&
16-
typeof callback === 'function') {
17-
callback = opts
18-
opts = {}
19-
}
13+
options = options || {}
2014

21-
if (args && typeof args === 'object') {
22-
opts = args
23-
args = undefined
24-
}
15+
const searchParams = new URLSearchParams(options.searchParams)
16+
if (addr) searchParams.set('arg', `${addr}`)
17+
if (options.default != null) searchParams.set('default', options.default)
18+
19+
const res = await ky.post('bootstrap/add', {
20+
timeout: options.timeout,
21+
signal: options.signal,
22+
headers: options.headers,
23+
searchParams
24+
}).json()
2525

26-
send({
27-
path: 'bootstrap/add',
28-
args: args,
29-
qs: opts
30-
}, callback)
31-
})
32-
}
26+
return res
27+
}
28+
})

src/bootstrap/index.js

Lines changed: 6 additions & 10 deletions
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-
add: require('./add')(send),
10-
rm: require('./rm')(send),
11-
list: require('./list')(send)
12-
}
13-
}
5+
module.exports = config => ({
6+
add: callbackify.variadic(require('./add')(config)),
7+
rm: callbackify.variadic(require('./rm')(config)),
8+
list: callbackify.variadic(require('./list')(config))
9+
})

src/bootstrap/list.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
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-
}
11-
send({
12-
path: 'bootstrap/list',
13-
qs: opts
14-
}, callback)
15-
})
16-
}
5+
module.exports = configure(({ ky }) => {
6+
return async (options) => {
7+
options = options || {}
8+
9+
const res = await ky.get('bootstrap/list', {
10+
timeout: options.timeout,
11+
signal: options.signal,
12+
headers: options.headers,
13+
searchParams: options.searchParams
14+
}).json()
15+
16+
return res
17+
}
18+
})

src/bootstrap/rm.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
3+
const Multiaddr = require('multiaddr')
4+
const configure = require('../lib/configure')
45

5-
module.exports = (send) => {
6-
return promisify((args, opts, callback) => {
7-
if (typeof opts === 'function' &&
8-
!callback) {
9-
callback = opts
10-
opts = {}
6+
module.exports = configure(({ ky }) => {
7+
return async (addr, options) => {
8+
if (addr && typeof addr === 'object' && !Multiaddr.isMultiaddr(addr)) {
9+
options = addr
10+
addr = null
1111
}
1212

13-
// opts is the real callback --
14-
// 'callback' is being injected by promisify
15-
if (typeof opts === 'function' &&
16-
typeof callback === 'function') {
17-
callback = opts
18-
opts = {}
19-
}
13+
options = options || {}
2014

21-
if (args && typeof args === 'object') {
22-
opts = args
23-
args = undefined
24-
}
15+
const searchParams = new URLSearchParams(options.searchParams)
16+
if (addr) searchParams.set('arg', `${addr}`)
17+
if (options.all != null) searchParams.set('all', options.all)
18+
19+
const res = await ky.post('bootstrap/rm', {
20+
timeout: options.timeout,
21+
signal: options.signal,
22+
headers: options.headers,
23+
searchParams
24+
}).json()
2525

26-
send({
27-
path: 'bootstrap/rm',
28-
args: args,
29-
qs: opts
30-
}, callback)
31-
})
32-
}
26+
return res
27+
}
28+
})

src/utils/load-commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function requireCommands (send, config) {
9191
getEndpointConfig: require('../get-endpoint-config')(config),
9292
bitswap: require('../bitswap')(config),
9393
block: require('../block')(config),
94+
bootstrap: require('../bootstrap')(config),
9495
dag: require('../dag')(config)
9596
}
9697

@@ -110,7 +111,6 @@ function requireCommands (send, config) {
110111
pin: require('../pin'),
111112

112113
// Network
113-
bootstrap: require('../bootstrap'),
114114
dht: require('../dht'),
115115
name: require('../name'),
116116
ping: require('../ping'),

0 commit comments

Comments
 (0)