Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

refactor: async await roundup #1173

Merged
merged 37 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f281b20
refactor: convert key API to async/await
Nov 19, 2019
acf2233
refactor: convert log API to async/await
Nov 19, 2019
0ad3897
fix: tests
Nov 19, 2019
e0a4ffd
fix: tests
Nov 19, 2019
cdf9390
refactor: convert name API to async/await
Nov 19, 2019
2895384
fix: tests
Nov 19, 2019
96b86f5
refactor: convert misc root level APIs to async/await
Nov 19, 2019
8638f9c
Merge branch 'master' into refactor/key-async-await
Nov 19, 2019
aa33b9a
Merge branch 'master' into refactor/log-async-await
Nov 19, 2019
2c110a5
Merge branch 'master' into refactor/name-async-await
Nov 19, 2019
e673f91
Merge branch 'master' into refactor/key-async-await
Nov 20, 2019
373d1e0
Merge branch 'master' into refactor/log-async-await
Nov 20, 2019
f4476cc
Merge branch 'master' into refactor/name-async-await
Nov 20, 2019
29cce3a
refactor: convert repo API to async await
Nov 20, 2019
a891032
refactor: convert stats API to async/await
Nov 21, 2019
a88767f
refactor: convert swarm API to async/await
Nov 21, 2019
1363199
fix: tests
Nov 21, 2019
c3cd680
refactor: convert mount API to async/await
Nov 21, 2019
e011c8e
fix: load mount
Nov 21, 2019
4186327
fix: post all the things
Nov 21, 2019
2281599
refactor: convert version API to async/await
Nov 21, 2019
d8da835
refactor: convert ping API to async/await
Nov 21, 2019
aa83f91
refactor: convert update API to async/await
Nov 21, 2019
f0d2aca
Merge branch 'master' into refactor/misc-async-await
Nov 21, 2019
09afe67
fix: remove tests for go-ipfs 0.4.4 peer response
Nov 21, 2019
3840f40
fix: ping sub modules test
Nov 21, 2019
1980530
Merge branch 'refactor/log-async-await' into refactor/async-await-rou…
Nov 21, 2019
81e72b3
Merge branch 'refactor/name-async-await' into refactor/async-await-ro…
Nov 21, 2019
12ff0d8
Merge branch 'refactor/misc-async-await' into refactor/async-await-ro…
Nov 21, 2019
6ecf79b
Merge branch 'refactor/repo-async-await' into refactor/async-await-ro…
Nov 21, 2019
d2beb18
Merge branch 'refactor/swarm-async-await' into refactor/async-await-r…
Nov 21, 2019
78b430a
refactor: move loadCommands into index and remove unused code
Nov 21, 2019
d7adafc
fix: browser and Node.js configuration
Nov 21, 2019
ec0e66c
fix: dev dependencies
Nov 21, 2019
b9d6d1b
fix: post all the things so not more breaking changes
Nov 21, 2019
31881e5
fix: inconsistent error message in browsers
Nov 21, 2019
fab4c67
refactor: return generator func not func that returns generator
Nov 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions src/swarm/addrs.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
'use strict'

const promisify = require('promisify-es6')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const configure = require('../lib/configure')

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
send({
path: 'swarm/addrs',
qs: opts
}, (err, result) => {
if (err) {
return callback(err)
}
module.exports = configure(({ ky }) => {
return async options => {
options = options || {}

const peers = Object.keys(result.Addrs).map((id) => {
const peerInfo = new PeerInfo(PeerId.createFromB58String(id))
result.Addrs[id].forEach((addr) => {
peerInfo.multiaddrs.add(multiaddr(addr))
})
return peerInfo
})
const res = await ky.post('swarm/addrs', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
}).json()

callback(null, peers)
return Object.keys(res.Addrs).map(id => {
const peerInfo = new PeerInfo(PeerId.createFromB58String(id))
res.Addrs[id].forEach(addr => peerInfo.multiaddrs.add(multiaddr(addr)))
return peerInfo
})
})
}
}
})
33 changes: 19 additions & 14 deletions src/swarm/connect.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
'use strict'

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

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
send({
path: 'swarm/connect',
args: args,
qs: opts
}, callback)
})
}
module.exports = configure(({ ky }) => {
return async (addrs, options) => {
addrs = Array.isArray(addrs) ? addrs : [addrs]
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
addrs.forEach(addr => searchParams.append('arg', addr))

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

return res.Strings || []
}
})
33 changes: 19 additions & 14 deletions src/swarm/disconnect.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
'use strict'

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

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
send({
path: 'swarm/disconnect',
args: args,
qs: opts
}, callback)
})
}
module.exports = configure(({ ky }) => {
return async (addrs, options) => {
addrs = Array.isArray(addrs) ? addrs : [addrs]
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
addrs.forEach(addr => searchParams.append('arg', addr))

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

return res.Strings || []
}
})
20 changes: 8 additions & 12 deletions src/swarm/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
'use strict'

const moduleConfig = require('../utils/module-config')
const callbackify = require('callbackify')

module.exports = (arg) => {
const send = moduleConfig(arg)

return {
peers: require('./peers')(send),
connect: require('./connect')(send),
disconnect: require('./disconnect')(send),
addrs: require('./addrs')(send),
localAddrs: require('./localAddrs')(send)
}
}
module.exports = config => ({
addrs: callbackify.variadic(require('./addrs')(config)),
connect: callbackify.variadic(require('./connect')(config)),
disconnect: callbackify.variadic(require('./disconnect')(config)),
localAddrs: callbackify.variadic(require('./localAddrs')(config)),
peers: callbackify.variadic(require('./peers')(config))
})
38 changes: 18 additions & 20 deletions src/swarm/localAddrs.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
'use strict'

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

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
send({
path: 'swarm/addrs/local',
qs: opts
}, (err, result) => {
if (err) {
return callback(err)
}
callback(null, result.Strings.map((addr) => {
return multiaddr(addr)
}))
})
})
}
module.exports = configure(({ ky }) => {
return async options => {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
if (options.id != null) searchParams.append('id', options.id)

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

return (res.Strings || []).map(a => multiaddr(a))
}
})
104 changes: 39 additions & 65 deletions src/swarm/peers.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,48 @@
'use strict'

const promisify = require('promisify-es6')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const configure = require('../lib/configure')

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
const verbose = opts.v || opts.verbose
send({
path: 'swarm/peers',
qs: opts
}, (err, response) => {
if (err) {
return callback(err)
}
const peerInfo = parsePeersResponse(verbose, response)
callback(null, peerInfo)
})
})
}
module.exports = configure(({ ky }) => {
return async options => {
options = options || {}

function parsePeersResponse (verbose, response) {
// go-ipfs <= 0.4.4
if (Array.isArray(response.Strings)) {
return response.Strings.map(parseLegacyPeer.bind(null, verbose))
}
// go-ipfs >= 0.4.5
if (Array.isArray(response.Peers)) {
return response.Peers.map(parsePeer.bind(null, verbose))
}
return []
}
const searchParams = new URLSearchParams(options.searchParams)
if (options.direction != null) searchParams.append('direction', options.direction)
if (options.latency != null) searchParams.append('latency', options.latency)
if (options.streams != null) searchParams.append('streams', options.streams)
if (options.verbose != null) searchParams.append('verbose', options.verbose)

function parseLegacyPeer (verbose, peer) {
const res = {}
try {
if (verbose) {
const parts = peer.split(' ')
res.addr = multiaddr(parts[0])
res.latency = parts[1]
} else {
res.addr = multiaddr(peer)
}
res.peer = PeerId.createFromB58String(res.addr.getPeerId())
} catch (error) {
res.error = error
res.rawPeerInfo = peer
}
return res
}
const res = await ky.post('swarm/peers', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

function parsePeer (verbose, peer) {
const res = {}
try {
res.addr = multiaddr(peer.Addr)
res.peer = PeerId.createFromB58String(peer.Peer)
res.muxer = peer.Muxer
} catch (error) {
res.error = error
res.rawPeerInfo = peer
}
if (peer.Latency) {
res.latency = peer.Latency
}
if (peer.Streams) {
res.streams = peer.Streams
return (res.Peers || []).map(peer => {
const info = {}
try {
info.addr = multiaddr(peer.Addr)
info.peer = PeerId.createFromB58String(peer.Peer)
} catch (error) {
info.error = error
info.rawPeerInfo = peer
}
if (peer.Muxer) {
info.muxer = peer.Muxer
}
if (peer.Latency) {
info.latency = peer.Latency
}
if (peer.Streams) {
info.streams = peer.Streams
}
if (peer.Direction != null) {
info.direction = peer.Direction
}
return info
})
}
return res
}
})
3 changes: 1 addition & 2 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function requireCommands (send, config) {
stats: require('../stats')(config),
stop: callbackify.variadic(require('../stop')(config)),
shutdown: callbackify.variadic(require('../stop')(config)),
swarm: require('../swarm')(config),
version: callbackify.variadic(require('../version')(config)),
getEndpointConfig: require('../get-endpoint-config')(config),
bitswap: require('../bitswap')(config),
Expand All @@ -144,8 +145,6 @@ function requireCommands (send, config) {
})

const subCmds = {
// Network
swarm: require('../swarm'),
pubsub: require('../pubsub')
}

Expand Down
18 changes: 0 additions & 18 deletions test/node/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,6 @@ describe('.swarm.peers', function () {
expect(scope.isDone()).to.equal(true)
})

it('handles a go-ipfs <= 0.4.4 peer response', async () => {
const response = { Strings: ['/ip4/73.109.217.59/tcp/49311/ipfs/QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm'] }

const scope = nock(apiUrl)
.post('/api/v0/swarm/peers')
.query(true)
.reply(200, response)

const res = await ipfs.swarm.peers()

expect(res).to.be.a('array')
expect(res.length).to.equal(1)
expect(res[0].error).to.not.exist()
expect(res[0].addr.toString()).to.equal('/ip4/73.109.217.59/tcp/49311/ipfs/QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm')
expect(res[0].peer.toB58String()).to.equal('QmWjxEGC7BthJrCf7QTModrcsRweHbupdPTY4oGMVoDZXm')
expect(scope.isDone()).to.equal(true)
})

it('handles an ip6 quic peer', async () => {
const response = { Peers: [{ Addr: '/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/4001/quic', Peer: 'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC', Latency: '', Muxer: '', Streams: null }] }

Expand Down