mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-04-25 14:12:22 +00:00
dial and listen done
This commit is contained in:
parent
9441b97917
commit
03de3c49ef
@ -2,14 +2,14 @@ js-libp2p-websockets
|
|||||||
====================
|
====================
|
||||||
|
|
||||||
[](http://ipn.io)
|
[](http://ipn.io)
|
||||||
[[](http://webchat.freenode.net/?channels=%23ipfs)
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||

|

|
||||||
[](https://david-dm.org/diasdavid/js-libp2p-websockets)
|
[](https://david-dm.org/diasdavid/js-libp2p-websockets)
|
||||||
[](https://github.com/feross/standard)
|
[](https://github.com/feross/standard)
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
> 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
|
||||||
|
|
||||||
|
|
||||||
|
21
package.json
21
package.json
@ -1,12 +1,20 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-websockets",
|
"name": "libp2p-websockets",
|
||||||
"version": "0.1.0",
|
"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",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"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": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/diasdavid/js-libp2p-websockets.git"
|
"url": "git+https://github.com/diasdavid/js-libp2p-websockets.git"
|
||||||
@ -25,10 +33,13 @@
|
|||||||
"simple-websocket": "github:diasdavid/simple-websocket#ec31437"
|
"simple-websocket": "github:diasdavid/simple-websocket#ec31437"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"chai": "^3.5.0",
|
||||||
"interface-connection": "0.0.3",
|
"interface-connection": "0.0.3",
|
||||||
"interface-transport": "^0.1.1",
|
"interface-transport": "^0.1.1",
|
||||||
"pre-commit": "^1.1.1",
|
"istanbul": "^0.4.2",
|
||||||
"standard": "^5.2.2",
|
"mocha": "^2.4.5",
|
||||||
|
"pre-commit": "^1.1.2",
|
||||||
|
"standard": "^6.0.7",
|
||||||
"tape": "^4.2.0"
|
"tape": "^4.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
88
src/index.js
88
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) {
|
function WebSockets () {
|
||||||
options.ready = options.ready || function noop () {}
|
if (!(this instanceof WebSockets)) {
|
||||||
var opts = multiaddr.toOptions()
|
return new WebSockets()
|
||||||
var url = 'ws://' + opts.host + ':' + opts.port
|
}
|
||||||
var socket = new SWS(url)
|
|
||||||
socket.on('connect', options.ready)
|
const listeners = []
|
||||||
return socket
|
|
||||||
|
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
|
|
||||||
|
55
tests/libp2p-websockets-test.js
Normal file
55
tests/libp2p-websockets-test.js
Normal file
@ -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()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user