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 298
refactor: convert dht API to async/await #1156
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
20ea093
refactor: convert dht.findPeer to async/await
4c4d11c
refactor: convert dht.findProvs to async/await
7a020c5
fix: the Addrs prop may be null
c11fd4c
feat: convert dht.get to async/await
5cf569e
refactor: convert dht.provide to async/await
45fc00f
refactor: convert dht.put to async/await
08f196b
refactor: convert dht.query to async/await
36b7a62
Merge branch 'master' into refactor/dht-async-await
a50bdc9
chore: appease linter
43f21e2
refactor: remove logging
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
const multiaddr = require('multiaddr') | ||
const ndjson = require('iterable-ndjson') | ||
const log = require('debug')('ipfs-http-client:dht:find-peer') | ||
const configure = require('../lib/configure') | ||
const toIterable = require('../lib/stream-to-iterable') | ||
|
||
module.exports = configure(({ ky }) => { | ||
return (peerId, options) => (async function * () { | ||
options = options || {} | ||
|
||
const searchParams = new URLSearchParams(options.searchParams) | ||
searchParams.set('arg', `${peerId}`) | ||
if (options.verbose != null) searchParams.set('verbose', options.verbose) | ||
|
||
const res = await ky.get('dht/findpeer', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}) | ||
|
||
for await (const message of ndjson(toIterable(res.body))) { | ||
log(message) | ||
// 2 = FinalPeer | ||
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18 | ||
if (message.Type === 2 && message.Responses) { | ||
for (const { ID, Addrs } of message.Responses) { | ||
const peerInfo = new PeerInfo(PeerId.createFromB58String(ID)) | ||
if (Addrs) Addrs.forEach(a => peerInfo.multiaddrs.add(multiaddr(a))) | ||
yield peerInfo | ||
} | ||
} | ||
} | ||
})() | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
const multiaddr = require('multiaddr') | ||
const ndjson = require('iterable-ndjson') | ||
const log = require('debug')('ipfs-http-client:dht:find-provs') | ||
const configure = require('../lib/configure') | ||
const toIterable = require('../lib/stream-to-iterable') | ||
|
||
module.exports = configure(({ ky }) => { | ||
return (cid, options) => (async function * () { | ||
options = options || {} | ||
|
||
const searchParams = new URLSearchParams(options.searchParams) | ||
searchParams.set('arg', `${cid}`) | ||
if (options.numProviders) searchParams.set('num-providers', options.numProviders) | ||
if (options.verbose != null) searchParams.set('verbose', options.verbose) | ||
|
||
const res = await ky.get('dht/findprovs', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}) | ||
|
||
for await (const message of ndjson(toIterable(res.body))) { | ||
log(message) | ||
// 4 = Provider | ||
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L20 | ||
if (message.Type === 4 && message.Responses) { | ||
for (const { ID, Addrs } of message.Responses) { | ||
const peerInfo = new PeerInfo(PeerId.createFromB58String(ID)) | ||
if (Addrs) Addrs.forEach(a => peerInfo.multiaddrs.add(multiaddr(a))) | ||
yield peerInfo | ||
} | ||
} | ||
} | ||
})() | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,48 +1,32 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const ndjson = require('iterable-ndjson') | ||
const log = require('debug')('ipfs-http-client:dht:get') | ||
const configure = require('../lib/configure') | ||
const toIterable = require('../lib/stream-to-iterable') | ||
|
||
module.exports = (send) => { | ||
return promisify((key, opts, callback) => { | ||
if (typeof opts === 'function' && !callback) { | ||
callback = opts | ||
opts = {} | ||
} | ||
|
||
// opts is the real callback -- | ||
// 'callback' is being injected by promisify | ||
if (typeof opts === 'function' && typeof callback === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
module.exports = configure(({ ky }) => { | ||
return (key, options) => (async function * () { | ||
options = options || {} | ||
|
||
function handleResult (done, err, res) { | ||
if (err) { | ||
return done(err) | ||
} | ||
if (!res) { | ||
return done(new Error('empty response')) | ||
} | ||
if (res.length === 0) { | ||
return done(new Error('no value returned for key')) | ||
} | ||
const searchParams = new URLSearchParams(options.searchParams) | ||
searchParams.set('arg', `${key}`) | ||
if (options.verbose != null) searchParams.set('verbose', options.verbose) | ||
|
||
// Inconsistent return values in the browser vs node | ||
if (Array.isArray(res)) { | ||
res = res[0] | ||
} | ||
const res = await ky.get('dht/get', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}) | ||
|
||
if (res.Type === 5) { | ||
done(null, res.Extra) | ||
} else { | ||
done(new Error('key was not found (type 6)')) | ||
for await (const message of ndjson(toIterable(res.body))) { | ||
log(message) | ||
// 5 = Value | ||
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L21 | ||
if (message.Type === 5) { | ||
yield message.Extra | ||
} | ||
} | ||
|
||
send({ | ||
path: 'dht/get', | ||
args: key, | ||
qs: opts | ||
}, handleResult.bind(null, callback)) | ||
}) | ||
} | ||
})() | ||
}) |
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,17 +1,30 @@ | ||
'use strict' | ||
|
||
const moduleConfig = require('../utils/module-config') | ||
const callbackify = require('callbackify') | ||
const errCode = require('err-code') | ||
const { collectify } = require('../lib/converters') | ||
|
||
module.exports = (arg) => { | ||
const send = moduleConfig(arg) | ||
module.exports = config => { | ||
const get = require('./get')(config) | ||
const findPeer = require('./find-peer')(config) | ||
|
||
return { | ||
get: require('./get')(send), | ||
put: require('./put')(send), | ||
findProvs: require('./findprovs')(send), | ||
findPeer: require('./findpeer')(send), | ||
provide: require('./provide')(send), | ||
get: callbackify.variadic(async (key, options) => { | ||
for await (const value of get(key, options)) { | ||
return value | ||
} | ||
throw errCode(new Error('value not found'), 'ERR_TYPE_5_NOT_FOUND') | ||
}), | ||
put: callbackify.variadic(collectify(require('./put')(config))), | ||
findProvs: callbackify.variadic(collectify(require('./find-provs')(config))), | ||
findPeer: callbackify.variadic(async (peerId, options) => { | ||
for await (const peerInfo of findPeer(peerId, options)) { | ||
return peerInfo | ||
} | ||
throw errCode(new Error('final peer not found'), 'ERR_TYPE_2_NOT_FOUND') | ||
}), | ||
provide: callbackify.variadic(collectify(require('./provide')(config))), | ||
// find closest peerId to given peerId | ||
query: require('./query')(send) | ||
query: callbackify.variadic(collectify(require('./query')(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,37 +1,42 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
const multiaddr = require('multiaddr') | ||
const ndjson = require('iterable-ndjson') | ||
const log = require('debug')('ipfs-http-client:dht:provide') | ||
const configure = require('../lib/configure') | ||
const toIterable = require('../lib/stream-to-iterable') | ||
const toCamel = require('../lib/object-to-camel') | ||
|
||
module.exports = (send) => { | ||
return promisify((cids, opts, callback) => { | ||
if (typeof opts === 'function' && !callback) { | ||
callback = opts | ||
opts = {} | ||
} | ||
module.exports = configure(({ ky }) => { | ||
return (cids, options) => (async function * () { | ||
cids = Array.isArray(cids) ? cids : [cids] | ||
options = options || {} | ||
|
||
// opts is the real callback -- | ||
// 'callback' is being injected by promisify | ||
if (typeof opts === 'function' && typeof callback === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
const searchParams = new URLSearchParams(options.searchParams) | ||
cids.forEach(cid => searchParams.append('arg', `${cid}`)) | ||
if (options.recursive != null) searchParams.set('recursive', options.recursive) | ||
if (options.verbose != null) searchParams.set('verbose', options.verbose) | ||
|
||
if (!Array.isArray(cids)) { | ||
cids = [cids] | ||
} | ||
const res = await ky.get('dht/provide', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams | ||
}) | ||
|
||
// Validate CID(s) and serialize | ||
try { | ||
cids = cids.map(cid => new CID(cid).toBaseEncodedString('base58btc')) | ||
} catch (err) { | ||
return callback(err) | ||
for await (let message of ndjson(toIterable(res.body))) { | ||
log(message) | ||
message = toCamel(message) | ||
if (message.responses) { | ||
message.responses = message.responses.map(({ ID, Addrs }) => { | ||
const peerInfo = new PeerInfo(PeerId.createFromB58String(ID)) | ||
if (Addrs) Addrs.forEach(a => peerInfo.multiaddrs.add(multiaddr(a))) | ||
return peerInfo | ||
}) | ||
} | ||
yield message | ||
} | ||
|
||
send({ | ||
path: 'dht/provide', | ||
args: cids, | ||
qs: opts | ||
}, callback) | ||
}) | ||
} | ||
})() | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to log the message?
Here and in all the other DHT related calls?