diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..f66fe91996 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +sudo: false +language: node_js +node_js: + - "iojs" + - "0.12" + - "0.10" + +# Make sure we have new NPM. +before_install: + - npm install -g npm + +script: + - npm run lint + - npm test diff --git a/README.md b/README.md index 2184a79c2f..33ac99672f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ipfs-swarm Node.js implementation ================================= -[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) +[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Build Status](https://img.shields.io/travis/diasdavid/node-ipfs-swarm/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/node-ipfs-swarm) > IPFS swarm implementation in Node.js diff --git a/package.json b/package.json index b255ff5052..112c52427c 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "./node_modules/.bin/lab tests/*-test.js", "coverage": "./node_modules/.bin/lab -t 100 tests/*-test.js", "codestyle": "./node_modules/.bin/standard --format", - "lint": "jshint .", + "lint": "./node_modules/.bin/standard", "validate": "npm ls" }, "repository": { @@ -31,6 +31,7 @@ "code": "^1.4.1", "lab": "^5.13.0", "precommit-hook": "^3.0.0", + "sinon": "^1.15.4", "standard": "^4.5.2", "stream-pair": "^1.0.3" }, diff --git a/src/swarm.js b/src/swarm.js index 3f89f9f0fe..89beaa1dfb 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -20,7 +20,7 @@ function Swarm () { self.port = parseInt(process.env.IPFS_SWARM_PORT, 10) || 4001 self.connections = {} // {peerIdB58: {conn: <>, socket: <>} - self.handles = [] + self.handles = {} // set the listener @@ -140,7 +140,7 @@ function Swarm () { if (self.handles[protocol]) { return handlerFunc(new Error('Handle for protocol already exists', protocol)) } - self.handles.push({ protocol: protocol, func: handlerFunc }) + self.handles[protocol] = handlerFunc log.info('Registered handler for protocol:', protocol) } @@ -150,9 +150,9 @@ function Swarm () { if (number === 0) { cb() } var c = new Counter(number, cb) - keys.map(function (key) { - c.hit() + keys.forEach(function (key) { self.connections[key].conn.end() + c.hit() }) } @@ -165,8 +165,8 @@ function Swarm () { errorUp(self, stream) var msH = new Select() msH.handle(stream) - self.handles.forEach(function (handle) { - msH.addHandler(handle.protocol, handle.func) + Object.keys(self.handles).forEach(function (protocol) { + msH.addHandler(protocol, self.handles[protocol]) }) } diff --git a/tests/swarm-test.js b/tests/swarm-test.js index 646f0545fb..b8a5c9007e 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -1,5 +1,6 @@ var Lab = require('lab') var Code = require('code') +var sinon = require('sinon') var lab = exports.lab = Lab.script() var experiment = lab.experiment @@ -42,6 +43,64 @@ afterEach(function (done) { done() }) +experiment('BASICS', function () { + experiment('Swarm', function () { + test('enforces instantiation with new', function (done) { + expect(function () { + Swarm() + }).to.throw('Swarm must be called with new') + done() + }) + + test('parses $IPFS_SWARM_PORT', function (done) { + process.env.IPFS_SWARM_PORT = 1111 + var swarm = new Swarm() + expect(swarm.port).to.be.equal(1111) + process.env.IPFS_SWARM_PORT = undefined + done() + }) + }) + + experiment('Swarm.listen', function (done) { + test('handles missing port', function (done) { + var swarm = new Swarm() + swarm.listen(done) + }) + + test('handles passed in port', function (done) { + var swarm = new Swarm() + swarm.listen(1234) + expect(swarm.port).to.be.equal(1234) + done() + }) + }) + + experiment('Swarm.registerHandler', function () { + test('throws when registering a protcol handler twice', function (done) { + var swarm = new Swarm() + swarm.registerHandler('/sparkles/1.1.1', function () {}) + swarm.registerHandler('/sparkles/1.1.1', function (err) { + expect(err).to.be.an.instanceOf(Error) + expect(err.message).to.be.equal('Handle for protocol already exists') + done() + }) + }) + }) + + experiment('Swarm.closeConns', function () { + test('calls end on all connections', function (done) { + swarmA.openConnection(peerB, function () { + var key = Object.keys(swarmA.connections)[0] + sinon.spy(swarmA.connections[key].conn, 'end') + swarmA.closeConns(function () { + expect(swarmA.connections[key].conn.end.called).to.be.equal(true) + done() + }) + }) + }) + }) +}) + experiment('BASE', function () { test('Open a stream', function (done) { var protocol = '/sparkles/3.3.3'