-
Notifications
You must be signed in to change notification settings - Fork 296
refactor: convert bootstrap API to async/await #1154
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} | ||
}) |
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)) | ||
}) |
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 | ||
} | ||
}) |
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)) { | ||
achingbrain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the commit history, what happened with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't be bothered to dig in at the time but thinking about it now it's because browsers are respecting CORS and our default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want me to add DELETE to ctl? ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not suggesting changing this to |
||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}).json() | ||
|
||
send({ | ||
path: 'bootstrap/rm', | ||
args: args, | ||
qs: opts | ||
}, callback) | ||
}) | ||
} | ||
return res | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we trying to support an
options
object as the first argument? If so, is this necessary? The spec saysaddr
is not optional.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's what the current code does. So you can pass
add(null, { default: true })
oradd({ default: true })
to add the default bootstrappers. Same sort of situation forrm
(withall
option).So
addr
is required unless you're passing thedefault
option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ipfs-inactive/interface-js-ipfs-core#557
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should leave it like this in this PR.
But i agree with alex and we should make an issue to improve this in the future cause from https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/BOOTSTRAP.md#ipfsbootstrapaddaddr-options i would understand i could do
add({ default: true })
maybe we can simplify to:if first param is undefined use defaults.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, quite right. Sigh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ me too. I'm just trying to get everything switched to async/await first and then take a second pass at API changes.