From 03de3c49ef989e2936578bfbeab0f351fb041e79 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 14 Mar 2016 20:25:00 +0000 Subject: [PATCH] dial and listen done --- README.md | 8 +-- package.json | 21 ++++++-- src/index.js | 88 ++++++++++++++++++++++++++++----- tests/libp2p-websockets-test.js | 55 +++++++++++++++++++++ 4 files changed, 152 insertions(+), 20 deletions(-) create mode 100644 tests/libp2p-websockets-test.js diff --git a/README.md b/README.md index c2ac2b6..90efb87 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,14 @@ js-libp2p-websockets ==================== [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) -[[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) +[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) ![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) [![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-websockets.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p-websockets) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) -![](https://raw.githubusercontent.com/diasdavid/abstract-connection/master/img/badge.png) -![](https://raw.githubusercontent.com/diasdavid/abstract-transport/master/img/badge.png) +![](https://raw.githubusercontent.com/diasdavid/interace-connection/master/img/badge.png) +![](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png) -> JavaScript implementation of the WebSockets module that libp2p uses and that implements the abstract-transport interface +> JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport interface diff --git a/package.json b/package.json index 10d580c..12613b4 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,20 @@ { "name": "libp2p-websockets", "version": "0.1.0", - "description": "JavaScript implementation of the WebSockets module that libp2p uses and that implements the abstract-transport interface ", + "description": "JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport spec", "main": "src/index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test:compliance:connection": "node tests/connection.js", + "test:compliance:transport": "node tests/transport.js", + "test:specific": "mocha tests/*-test.js", + "test": "npm run test:specific", + "test-2": "npm run test:specific && npm run test:compliance:transport && npm run test:compliance:connection", + "lint": "standard" }, - "pre-commit": [], + "pre-commit": [ + "lint", + "test" + ], "repository": { "type": "git", "url": "git+https://github.com/diasdavid/js-libp2p-websockets.git" @@ -25,10 +33,13 @@ "simple-websocket": "github:diasdavid/simple-websocket#ec31437" }, "devDependencies": { + "chai": "^3.5.0", "interface-connection": "0.0.3", "interface-transport": "^0.1.1", - "pre-commit": "^1.1.1", - "standard": "^5.2.2", + "istanbul": "^0.4.2", + "mocha": "^2.4.5", + "pre-commit": "^1.1.2", + "standard": "^6.0.7", "tape": "^4.2.0" } } diff --git a/src/index.js b/src/index.js index 5cda738..d1f13a2 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,80 @@ -var SWS = require('simple-websocket') +// const debug = require('debug') +// const log = debug('libp2p:tcp') +// const multiaddr = require('multiaddr') +const SWS = require('simple-websocket') -exports = module.exports +exports = module.exports = WebSockets -exports.dial = function (multiaddr, options) { - options.ready = options.ready || function noop () {} - var opts = multiaddr.toOptions() - var url = 'ws://' + opts.host + ':' + opts.port - var socket = new SWS(url) - socket.on('connect', options.ready) - return socket +function WebSockets () { + if (!(this instanceof WebSockets)) { + return new WebSockets() + } + + const listeners = [] + + this.dial = function (multiaddr, options) { + if (!options) { + options = {} + } + + options.ready = options.ready || function noop () {} + const maOpts = multiaddr.toOptions() + const conn = new SWS('ws://' + maOpts.host + ':' + maOpts.port) + conn.on('ready', options.ready) + conn.getObservedAddrs = () => { + return [multiaddr] + } + return conn + } + + this.createListener = (multiaddrs, options, handler, callback) => { + if (typeof options === 'function') { + callback = handler + handler = options + options = {} + } + + if (!Array.isArray(multiaddrs)) { + multiaddrs = [multiaddrs] + } + + var count = 0 + + multiaddrs.forEach((m) => { + const listener = SWS.createServer((conn) => { + conn.getObservedAddrs = () => { + return [] // TODO think if it makes sense for WebSockets + } + handler(conn) + }) + + listener.listen(m.toOptions().port, () => { + if (++count === multiaddrs.length) { + callback() + } + }) + listeners.push(listener) + }) + } + + this.close = (callback) => { + if (listeners.length === 0) { + throw new Error('there are no listeners') + } + var count = 0 + listeners.forEach((listener) => { + listener.close(() => { + if (++count === listeners.length) { + callback() + } + }) + }) + } + + this.filter = (multiaddrs) => { + return multiaddrs.filter((ma) => { + // TODO + // https://github.com/whyrusleeping/js-mafmt/pull/2 + }) + } } - -exports.createListener = SWS.createServer diff --git a/tests/libp2p-websockets-test.js b/tests/libp2p-websockets-test.js new file mode 100644 index 0000000..4c34321 --- /dev/null +++ b/tests/libp2p-websockets-test.js @@ -0,0 +1,55 @@ +/* eslint-env mocha */ + +const expect = require('chai').expect +const WSlibp2p = require('../src') +const multiaddr = require('multiaddr') + +describe('libp2p-websockets', function () { + this.timeout(10000) + var ws + + it('create', (done) => { + ws = new WSlibp2p() + expect(ws).to.exist + done() + }) + + it('listen and dial', (done) => { + const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets') + ws.createListener(mh, (socket) => { + expect(socket).to.exist + socket.end() + ws.close(() => { + done() + }) + }, () => { + const conn = ws.dial(mh) + conn.end() + }) + }) + + it('listen on several', (done) => { + const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets') + const mh2 = multiaddr('/ip4/127.0.0.1/tcp/9091/websockets') + const ws = new WSlibp2p() + + ws.createListener([mh1, mh2], (socket) => {}, () => { + ws.close(done) + }) + }) + + it('get observed addrs', (done) => { + const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets') + ws.createListener(mh, (socket) => { + expect(socket).to.exist + socket.end() + expect(socket.getObservedAddrs()).to.deep.equal([]) + ws.close(() => { + done() + }) + }, () => { + const conn = ws.dial(mh) + conn.end() + }) + }) +})