From 0514b0034bbe9472cf7bb06845437899dcdc7c9f Mon Sep 17 00:00:00 2001 From: Pau Ramon Revilla Date: Fri, 25 Sep 2015 09:26:50 +0200 Subject: [PATCH] Do not allow undefined `peerInfo` # What The code assumes that `peerInfo` exists, the API doesn't. # How to test ```js swarm = new Swarm() swarm.addTransport('tcp', tcp, { multiaddr: mh }, {}, {port: 8095}) ``` This shouldn't explode because of `self.peerInfo.multiaddrs`. --- src/index.js | 1 - src/swarm.js | 4 ++++ tests/swarm-test.js | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b825469a..5e3bb399 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ var Swarm = require('./swarm') exports = module.exports = Swarm -exports.singleton = new Swarm() diff --git a/src/swarm.js b/src/swarm.js index 2fa4f5d4..5f56208c 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -11,6 +11,10 @@ function Swarm (peerInfo) { throw new Error('Swarm must be called with new') } + if (!peerInfo) { + throw new Error('You must provide a value for `peerInfo`') + } + self.peerInfo = peerInfo // peerIdB58: { conn: } diff --git a/tests/swarm-test.js b/tests/swarm-test.js index bf5baeaa..e9bd63cf 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -24,6 +24,16 @@ process.on('uncaughtException', function (err) { console.log('Caught exception: ' + err) }) +experiment('Without a peer', function () { + test('it throws an exception', function (done) { + expect(function () { + var sw = new Swarm() + sw.close() + }).to.throw(Error) + done() + }) +}) + experiment('Without a Stream Muxer', function () { experiment('tcp', function () { test('add the transport', function (done) {