This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from diasdavid/update/new-libp2p-transport-scheme
Update/new libp2p transport scheme
- Loading branch information
Showing
5 changed files
with
174 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |