From 8fa28e1ea34aea915c9eecc8bdb562fd88d0f034 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Sat, 10 Nov 2018 21:41:45 +0000 Subject: [PATCH] refactor: convert utils to use async/await License: MIT Signed-off-by: Alan Shaw --- js/src/utils/mocha.js | 2 ++ js/src/utils/spawn.js | 31 ++++++++++++------------------- js/src/utils/swarm.js | 14 ++++++-------- package.json | 6 +++--- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/js/src/utils/mocha.js b/js/src/utils/mocha.js index 9ea71a0fa..50b6ca5e8 100644 --- a/js/src/utils/mocha.js +++ b/js/src/utils/mocha.js @@ -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 diff --git a/js/src/utils/spawn.js b/js/src/utils/spawn.js index 792d482bc..ea20207e7 100644 --- a/js/src/utils/spawn.js +++ b/js/src/utils/spawn.js @@ -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 diff --git a/js/src/utils/swarm.js b/js/src/utils/swarm.js index b36dc7df8..18cb9afe5 100644 --- a/js/src/utils/swarm.js +++ b/js/src/utils/swarm.js @@ -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 diff --git a/package.json b/package.json index 9cf06198c..1ecd41cb5 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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 ",