forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert bootstrap API to async/await (ipfs#1154)
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
Showing
5 changed files
with
64 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters