This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert bitswap to async/await (#1149)
- Loading branch information
Alan Shaw
authored
Nov 8, 2019
1 parent
2d9afc8
commit c05c5ec
Showing
5 changed files
with
67 additions
and
63 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,13 +1,9 @@ | ||
'use strict' | ||
|
||
const moduleConfig = require('../utils/module-config') | ||
const callbackify = require('callbackify') | ||
|
||
module.exports = (arg) => { | ||
const send = moduleConfig(arg) | ||
|
||
return { | ||
wantlist: require('./wantlist')(send), | ||
stat: require('./stat')(send), | ||
unwant: require('./unwant')(send) | ||
} | ||
} | ||
module.exports = (config) => ({ | ||
wantlist: callbackify.variadic(require('./wantlist')(config)), | ||
stat: callbackify.variadic(require('./stat')(config)), | ||
unwant: callbackify.variadic(require('./unwant')(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
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,25 +1,27 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
const configure = require('../lib/configure') | ||
|
||
module.exports = (send) => { | ||
return promisify((cid, opts, callback) => { | ||
if (typeof (opts) === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
module.exports = configure(({ ky }) => { | ||
return async (cid, options) => { | ||
options = options || {} | ||
|
||
const searchParams = new URLSearchParams(options.searchParams) | ||
|
||
try { | ||
cid = new CID(cid) | ||
} catch (err) { | ||
return callback(err) | ||
if (typeof cid === 'string') { | ||
searchParams.set('arg', cid) | ||
} else { | ||
searchParams.set('arg', new CID(cid).toString()) | ||
} | ||
|
||
send({ | ||
path: 'bitswap/unwant', | ||
args: cid.toBaseEncodedString(), | ||
qs: opts | ||
}, callback) | ||
}) | ||
} | ||
const res = await ky.get('bitswap/unwant', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
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,30 +1,29 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
const configure = require('../lib/configure') | ||
|
||
module.exports = (send) => { | ||
return promisify((peerId, opts, callback) => { | ||
if (typeof (peerId) === 'function') { | ||
callback = peerId | ||
opts = {} | ||
peerId = null | ||
} else if (typeof (opts) === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
module.exports = configure(({ ky }) => { | ||
return async (peerId, options) => { | ||
options = options || {} | ||
|
||
const searchParams = new URLSearchParams(options.searchParams) | ||
|
||
if (peerId) { | ||
try { | ||
opts.peer = new CID(peerId).toBaseEncodedString() | ||
} catch (err) { | ||
return callback(err) | ||
if (typeof peerId === 'string') { | ||
searchParams.set('peer', peerId) | ||
} else { | ||
searchParams.set('peer', new CID(peerId).toString()) | ||
} | ||
} | ||
|
||
send({ | ||
path: 'bitswap/wantlist', | ||
qs: opts | ||
}, callback) | ||
}) | ||
} | ||
const res = await ky.get('bitswap/wantlist', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
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