Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from diasdavid/update/new-libp2p-transport-scheme
Browse files Browse the repository at this point in the history
Update/new libp2p transport scheme
  • Loading branch information
daviddias committed Mar 14, 2016
2 parents 9441b97 + 7205af0 commit 19bbe47
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 20 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- "4.0"

# Make sure we have new NPM.
before_install:
- npm install -g npm

script:
- npm test
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

22 changes: 17 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -21,14 +29,18 @@
},
"homepage": "https://github.com/diasdavid/js-libp2p-websockets#readme",
"dependencies": {
"mafmt": "^1.0.1",
"multiaddr": "^1.1.1",
"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"
}
}
87 changes: 76 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,79 @@
var SWS = require('simple-websocket')
// const debug = require('debug')
// const log = debug('libp2p:tcp')
const SWS = require('simple-websocket')
const mafmt = require('mafmt')

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]
}

exports.createListener = SWS.createServer
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) => {
return mafmt.WebSockets.matches(ma)
})
}
}
66 changes: 66 additions & 0 deletions tests/libp2p-websockets-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* 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()
})
})

it('filter', (done) => {
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')

const valid = ws.filter([mh1, mh2, mh3])
expect(valid.length).to.equal(1)
expect(valid[0]).to.deep.equal(mh3)
done()
})
})

0 comments on commit 19bbe47

Please sign in to comment.