From acb7e1f834895e96806b01fb65efd32d6e4bb743 Mon Sep 17 00:00:00 2001 From: Mark Henderson Date: Tue, 27 Jul 2021 20:10:45 -0400 Subject: [PATCH] refactor: remove unused swarm functionality --- index.js | 1 - src/swarm.js | 43 ------------------------------------------- test/swarm.spec.js | 46 ---------------------------------------------- 3 files changed, 90 deletions(-) delete mode 100644 src/swarm.js delete mode 100644 test/swarm.spec.js diff --git a/index.js b/index.js index 85a38f0..076e84b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ exports.MemStore = require('./src/mem-store') -exports.swarm = require('./src/swarm') exports.config = require('./src/config') exports.connectPeers = require('./src/connect-peers') exports.getIpfsPeerId = require('./src/get-ipfs-peer-id') diff --git a/src/swarm.js b/src/swarm.js deleted file mode 100644 index 5453f84..0000000 --- a/src/swarm.js +++ /dev/null @@ -1,43 +0,0 @@ -const Combinatorics = require('js-combinatorics') -const Ctl = require('ipfsd-ctl') -const factoryCfg = require('./config/factory') - -const connectPeers = require('./connect-peers') - -const factory = Ctl.createFactory(factoryCfg.defaults, factoryCfg.overrides) - -// Which IPFS implementations do we support? -const allowedTypes = ['proc', 'js', 'go'] - -// Creates local, connected clusters of IPFS implementations -const swarm = async function (ipfsTypes) { - if (!Array.isArray(ipfsTypes)) { - return Promise.reject(new Error('localSwarm: First argument should be an array')) - } - - ipfsTypes.forEach((t) => { - if (allowedTypes.indexOf(t) === -1) { - throw new Error(`${t} is not a valid ipfs type. Supported types are: ${allowedTypes.join(', ')}`) - } - }) - - const nodes = await Promise.all(ipfsTypes.map(async (type) => { - return factory.spawn({ type }) - })) - if (nodes.length === 1) return factory.controllers.map((ipfsd) => ipfsd.api) - - // Connect all the nodes and wait for connecting - const permutations = Combinatorics.combination(nodes, 2).toArray() - - await Promise.all(permutations.map(async (nodePair) => { - if (nodePair.length === 1) return - await connectPeers(nodePair[0].api, nodePair[1].api) - })) - - // Return the apis - return factory.controllers.map((ipfsd) => ipfsd.api) -} - -swarm.factory = factory - -module.exports = swarm diff --git a/test/swarm.spec.js b/test/swarm.spec.js deleted file mode 100644 index b7c79ee..0000000 --- a/test/swarm.spec.js +++ /dev/null @@ -1,46 +0,0 @@ -const { swarm } = require('../') - -const assert = require('assert') - -describe('swarm workflow', function () { - this.timeout(10000) - - it('throws an error if swarm is called without an array', (done) => { - swarm('foo').catch((e) => { - assert.strictEqual(e.message, 'localSwarm: First argument should be an array') - done() - }) - }) - - it('throws an error if localSwarm is called with an invalid IPFS type', (done) => { - swarm(['French']).catch((e) => { - assert.strictEqual(e.message, 'French is not a valid ipfs type. Supported types are: proc, js, go') - done() - }) - }) - - // Test a random selection of 2 through 10 nodes - // FIXME: Sometimes there are intermittent MAC address and/or dial failures - const availableTypes = ['proc', 'go'] - ;[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach((length) => { - const randomType = () => availableTypes[(Math.floor(Math.random() * 2))] - const nodeTypes = Array.from({ length }, () => randomType()) - - it(`localSwarm (${length}): ${nodeTypes.join(', ')}`, async () => { - const nodes = await swarm(nodeTypes) - assert.strictEqual(nodes.length, length) - - for (const node of nodes) { - const uniqueIds = new Set((await node.swarm.peers()).map(peer => { - return peer.peer.id ? peer.peer._idB58String : peer.peer - })) - assert(uniqueIds.size >= (length - 1)) - } - }) - }) - - afterEach(async () => { - await swarm.factory.clean() - assert.strictEqual(swarm.factory.controllers.length, 0) - }) -})