Skip to content

Commit

Permalink
refactor: convert bootstrap API to async/await (ipfs#1154)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored Nov 14, 2019
1 parent 72fdc8c commit 14c5b9e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 74 deletions.
46 changes: 21 additions & 25 deletions src/bootstrap/add.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
'use strict'

const promisify = require('promisify-es6')
const Multiaddr = require('multiaddr')
const configure = require('../lib/configure')

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof opts === 'function' &&
!callback) {
callback = opts
opts = {}
module.exports = configure(({ ky }) => {
return async (addr, options) => {
if (addr && typeof addr === 'object' && !Multiaddr.isMultiaddr(addr)) {
options = addr
addr = null
}

// opts is the real callback --
// 'callback' is being injected by promisify
if (typeof opts === 'function' &&
typeof callback === 'function') {
callback = opts
opts = {}
}
options = options || {}

if (args && typeof args === 'object') {
opts = args
args = undefined
}
const searchParams = new URLSearchParams(options.searchParams)
if (addr) searchParams.set('arg', `${addr}`)
if (options.default != null) searchParams.set('default', options.default)

const res = await ky.post('bootstrap/add', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

send({
path: 'bootstrap/add',
args: args,
qs: opts
}, callback)
})
}
return res
}
})
16 changes: 6 additions & 10 deletions src/bootstrap/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 {
add: require('./add')(send),
rm: require('./rm')(send),
list: require('./list')(send)
}
}
module.exports = config => ({
add: callbackify.variadic(require('./add')(config)),
rm: callbackify.variadic(require('./rm')(config)),
list: callbackify.variadic(require('./list')(config))
})
28 changes: 15 additions & 13 deletions src/bootstrap/list.js
Original file line number Diff line number Diff line change
@@ -1,16 +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 = {}
}
send({
path: 'bootstrap/list',
qs: opts
}, callback)
})
}
module.exports = configure(({ ky }) => {
return async (options) => {
options = options || {}

const res = await ky.get('bootstrap/list', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
}).json()

return res
}
})
46 changes: 21 additions & 25 deletions src/bootstrap/rm.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
'use strict'

const promisify = require('promisify-es6')
const Multiaddr = require('multiaddr')
const configure = require('../lib/configure')

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof opts === 'function' &&
!callback) {
callback = opts
opts = {}
module.exports = configure(({ ky }) => {
return async (addr, options) => {
if (addr && typeof addr === 'object' && !Multiaddr.isMultiaddr(addr)) {
options = addr
addr = null
}

// opts is the real callback --
// 'callback' is being injected by promisify
if (typeof opts === 'function' &&
typeof callback === 'function') {
callback = opts
opts = {}
}
options = options || {}

if (args && typeof args === 'object') {
opts = args
args = undefined
}
const searchParams = new URLSearchParams(options.searchParams)
if (addr) searchParams.set('arg', `${addr}`)
if (options.all != null) searchParams.set('all', options.all)

const res = await ky.post('bootstrap/rm', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

send({
path: 'bootstrap/rm',
args: args,
qs: opts
}, callback)
})
}
return res
}
})
2 changes: 1 addition & 1 deletion src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function requireCommands (send, config) {
getEndpointConfig: require('../get-endpoint-config')(config),
bitswap: require('../bitswap')(config),
block: require('../block')(config),
bootstrap: require('../bootstrap')(config),
dag: require('../dag')(config)
}

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

// Network
bootstrap: require('../bootstrap'),
dht: require('../dht'),
name: require('../name'),
ping: require('../ping'),
Expand Down

0 comments on commit 14c5b9e

Please sign in to comment.