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

refactor: convert utils to use async/await #390

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions js/src/utils/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const chaiAsPromised = require('chai-as-promised')

chai.use(dirtyChai)
chai.use(chaiAsPromised)

module.exports.expect = chai.expect

Expand Down
31 changes: 12 additions & 19 deletions js/src/utils/spawn.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
'use strict'

const waterfall = require('async/waterfall')
const timesSeries = require('async/timesSeries')
const map = require('async/map')

function identify (node, cb) {
node.id((err, id) => {
if (err) return cb(err)
node.peerId = id
cb(null, node)
})
async function identify (node) {
node.peerId = await node.id()
return node
}

// Spawn a node, get it's id and set it as `peerId` on the node
function spawnNodeWithId (factory, callback) {
waterfall([(cb) => factory.spawnNode(cb), identify], callback)
async function spawnNodeWithId (factory) {
return identify(await factory.spawnNode())
}

exports.spawnNodeWithId = spawnNodeWithId

// Spawn n nodes
function spawnNodes (n, factory, callback) {
timesSeries(n, (_, cb) => factory.spawnNode(cb), callback)
async function spawnNodes (n, factory) {
let nodes = []
for (let i = 0; i < n; i++) nodes.push(await factory.spawnNode())
return nodes
}

exports.spawnNodes = spawnNodes

// Spawn n nodes, getting their id's and setting them as `peerId` on the nodes
function spawnNodesWithId (n, factory, callback) {
spawnNodes(n, factory, (err, nodes) => {
if (err) return callback(err)
map(nodes, identify, callback)
})
async function spawnNodesWithId (n, factory) {
const nodes = await spawnNodes(n, factory)
return Promise.all(nodes.map(node => identify(node)))
}

exports.spawnNodesWithId = spawnNodesWithId
14 changes: 6 additions & 8 deletions js/src/utils/swarm.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
'use strict'

const eachSeries = require('async/eachSeries')
const pause = ms => new Promise((resolve, reject) => setTimeout(resolve, ms))

function connect (fromNode, toAddrs, cb) {
async function connect (fromNode, toAddrs, cb) {
if (!Array.isArray(toAddrs)) {
toAddrs = [toAddrs]
}

// FIXME ??? quick connections to different nodes sometimes cause no
// connection and no error, hence serialize connections and pause between
eachSeries(toAddrs, (toAddr, cb) => {
fromNode.swarm.connect(toAddr, (err) => {
if (err) return cb(err)
setTimeout(cb, 300)
})
}, cb)
for (let i = 0; i < toAddrs.length; i++) {
await fromNode.swarm.connect(toAddrs[i])
await pause(300)
}
}

module.exports.connect = connect
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"homepage": "https://github.com/ipfs/interface-ipfs-core#readme",
"dependencies": {
"aegir": "^17.0.1",
"async": "^2.6.1",
"big.js": "^5.2.2",
"bl": "^2.1.2",
"bs58": "^4.0.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"cids": "~0.5.5",
"concat-stream": "^1.6.2",
"crypto": "^1.0.1",
Expand All @@ -46,6 +46,7 @@
"ipld-dag-cbor": "~0.13.0",
"ipld-dag-pb": "~0.14.11",
"is-ipfs": "~0.4.7",
"is-plain-object": "^2.0.4",
"libp2p-crypto": "~0.14.0",
"multiaddr": "^5.0.0",
"multibase": "~0.5.0",
Expand All @@ -54,8 +55,7 @@
"peer-id": "~0.12.0",
"peer-info": "~0.14.1",
"pull-stream": "^3.6.9",
"pump": "^3.0.0",
"is-plain-object": "^2.0.4"
"pump": "^3.0.0"
},
"contributors": [
"Alan Shaw <alan.shaw@protocol.ai>",
Expand Down