mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-09 22:01:34 +00:00
Compare commits
75 Commits
Author | SHA1 | Date | |
---|---|---|---|
8d792fe954 | |||
de506873e7 | |||
61340e3909 | |||
8e1413b984 | |||
163624c218 | |||
6fd8b076e2 | |||
e54ebb65fe | |||
9c8a8bb26b | |||
3f29ff5d33 | |||
a712fd6d22 | |||
7079f10bcc | |||
1210a9f613 | |||
5c76907f3d | |||
074e7e323b | |||
20994f5320 | |||
eac00292f2 | |||
bf768d3585 | |||
05f799f983 | |||
a81c328bf7 | |||
a6ba60a5c4 | |||
594b770d8e | |||
dbf0d2c422 | |||
275434f873 | |||
631dad8647 | |||
3eac0e0dd6 | |||
30d4bb641e | |||
b0aeff8f53 | |||
998c71fc84 | |||
b31245adc8 | |||
85a064765a | |||
fb56cc3c30 | |||
03d0c52d4d | |||
0aa7bb72e7 | |||
e9b3d3496f | |||
58e18dd01b | |||
fb017ebb07 | |||
08c4c169d6 | |||
de927e8052 | |||
df8e61632b | |||
b453bd4f83 | |||
0143ab6449 | |||
02dd32e7df | |||
4fe91796cd | |||
352876cade | |||
41b700f509 | |||
eea7e91b15 | |||
b11a7972f5 | |||
15d5bc53fb | |||
9d911af8e0 | |||
9f1f3c82dc | |||
d6a1f52962 | |||
7b536819b1 | |||
7158aaf702 | |||
bc87fad5f9 | |||
c9418399a7 | |||
2cac123405 | |||
ff47a9c228 | |||
f86a981eb2 | |||
674d68000b | |||
ae371085c1 | |||
770bee3c66 | |||
6943e3e90b | |||
a008ebd5b9 | |||
20108d2de8 | |||
15fcfb737c | |||
0fa14c9608 | |||
ac7c8a150e | |||
851c8ee2a3 | |||
7a3f9d08d5 | |||
52d60a7391 | |||
165068d05c | |||
9baae15dcf | |||
b87524f36a | |||
b0484c678e | |||
b6c498055f |
@ -11,6 +11,7 @@ before_install:
|
||||
script:
|
||||
- npm run lint
|
||||
- npm test
|
||||
- npm run coverage
|
||||
|
||||
addons:
|
||||
firefox: 'latest'
|
||||
@ -18,3 +19,6 @@ addons:
|
||||
before_script:
|
||||
- export DISPLAY=:99.0
|
||||
- sh -e /etc/init.d/xvfb start
|
||||
|
||||
after_success:
|
||||
- npm run coverage-publish
|
||||
|
20
README.md
20
README.md
@ -1,11 +1,11 @@
|
||||
libp2p-swarm JavaScript implementation
|
||||
======================================
|
||||
|
||||
[](http://ipn.io)
|
||||
[](http://ipfs.io/)
|
||||
[](http://ipn.io)
|
||||
[](http://ipfs.io/)
|
||||
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||
[](https://travis-ci.org/diasdavid/js-libp2p-swarm)
|
||||

|
||||
[](https://coveralls.io/github/diasdavid/js-libp2p-swarm?branch=master)
|
||||
[](https://david-dm.org/ipfs/js-libp2p-swarm)
|
||||
[](https://github.com/feross/standard)
|
||||
|
||||
@ -19,7 +19,7 @@ libp2p-swarm is used by libp2p but it can be also used as a standalone module.
|
||||
|
||||
# Usage
|
||||
|
||||
## Install
|
||||
## Install
|
||||
|
||||
libp2p-swarm is available on npm and so, like any other npm module, just:
|
||||
|
||||
@ -48,7 +48,7 @@ peerInfo is a [PeerInfo](https://github.com/diasdavid/js-peer-info) object that
|
||||
libp2p-swarm expects transports that implement [interface-transport](https://github.com/diasdavid/abstract-transport). For example [libp2p-tcp](https://github.com/diasdavid/js-libp2p-tcp).
|
||||
|
||||
- `key` - the transport identifier
|
||||
- `transport` -
|
||||
- `transport` -
|
||||
- `options`
|
||||
- `callback`
|
||||
|
||||
@ -102,6 +102,10 @@ dial uses the best transport (whatever works first, in the future we can have so
|
||||
- `protocol`
|
||||
- `callback`
|
||||
|
||||
### `swarm.listen(callback)`
|
||||
|
||||
Start listening on all added transports that are available on the current `peerInfo`.
|
||||
|
||||
### `swarm.handle(protocol, handler)`
|
||||
|
||||
handle a new protocol.
|
||||
@ -109,6 +113,12 @@ handle a new protocol.
|
||||
- `protocol`
|
||||
- `handler` - function called when we receive a dial on `protocol. Signature must be `function (conn) {}`
|
||||
|
||||
### `swarm.unhandle(protocol)`
|
||||
|
||||
unhandle a protocol.
|
||||
|
||||
- `protocol`
|
||||
|
||||
### `swarm.close(callback)`
|
||||
|
||||
close all the listeners and muxers.
|
||||
|
12
circle.yml
Normal file
12
circle.yml
Normal file
@ -0,0 +1,12 @@
|
||||
machine:
|
||||
node:
|
||||
version: stable
|
||||
|
||||
dependencies:
|
||||
pre:
|
||||
- google-chrome --version
|
||||
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
|
||||
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
|
||||
- sudo apt-get update
|
||||
- sudo apt-get --only-upgrade install google-chrome-stable
|
||||
- google-chrome --version
|
45
package.json
45
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p-swarm",
|
||||
"version": "0.10.3",
|
||||
"version": "0.12.11",
|
||||
"description": "libp2p swarm implementation in JavaScript",
|
||||
"main": "lib/index.js",
|
||||
"jsnext:main": "src/index.js",
|
||||
@ -11,9 +11,10 @@
|
||||
"test:node": "gulp test:node",
|
||||
"test:browser": "gulp test:browser",
|
||||
"release": "gulp release",
|
||||
"release-minor": "gulp release --minor",
|
||||
"release-major": "gulp release --major",
|
||||
"coverage": "gulp coverage"
|
||||
"release-minor": "gulp release --type minor",
|
||||
"release-major": "gulp release --type major",
|
||||
"coverage": "gulp coverage",
|
||||
"coverage-publish": "aegir-coverage publish"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -39,25 +40,27 @@
|
||||
"bl": "^1.1.2",
|
||||
"buffer-loader": "0.0.1",
|
||||
"chai": "^3.5.0",
|
||||
"aegir": "^2.0.0",
|
||||
"aegir": "^3.0.1",
|
||||
"gulp": "^3.9.1",
|
||||
"istanbul": "^0.4.2",
|
||||
"istanbul": "^0.4.3",
|
||||
"libp2p-multiplex": "^0.2.1",
|
||||
"libp2p-spdy": "^0.2.3",
|
||||
"libp2p-tcp": "^0.4.0",
|
||||
"libp2p-websockets": "^0.3.1",
|
||||
"multiaddr": "^1.3.0",
|
||||
"peer-id": "^0.6.0",
|
||||
"peer-info": "^0.6.0",
|
||||
"libp2p-spdy": "^0.3.1",
|
||||
"libp2p-tcp": "^0.5.1",
|
||||
"libp2p-websockets": "^0.4.4",
|
||||
"pre-commit": "^1.1.2",
|
||||
"stream-pair": "^1.0.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel-runtime": "^6.6.1",
|
||||
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
|
||||
"ip-address": "^5.0.2",
|
||||
"multistream-select": "^0.6.1",
|
||||
"protocol-buffers-stream": "^1.2.0"
|
||||
"ip-address": "^5.8.0",
|
||||
"lodash.contains": "^2.4.3",
|
||||
"multiaddr": "^1.4.1",
|
||||
"multistream-select": "^0.6.5",
|
||||
"peer-id": "^0.6.6",
|
||||
"peer-info": "^0.6.2",
|
||||
"protocol-buffers-stream": "^1.3.1",
|
||||
"run-parallel": "^1.1.6"
|
||||
},
|
||||
"aegir": {
|
||||
"webpack": {
|
||||
@ -67,5 +70,13 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"contributors": [
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"David Dias <mail@daviddias.me>",
|
||||
"Francisco Baio Dias <xicombd@gmail.com>",
|
||||
"Pau Ramon Revilla <masylum@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"dignifiedquire <dignifiedquire@gmail.com>"
|
||||
]
|
||||
}
|
65
src/connection.js
Normal file
65
src/connection.js
Normal file
@ -0,0 +1,65 @@
|
||||
'use strict'
|
||||
|
||||
const connHandler = require('./default-handler')
|
||||
const identify = require('./identify')
|
||||
|
||||
module.exports = function connection (swarm) {
|
||||
return {
|
||||
addUpgrade () {},
|
||||
|
||||
addStreamMuxer (muxer) {
|
||||
// for dialing
|
||||
swarm.muxers[muxer.multicodec] = muxer
|
||||
|
||||
// for listening
|
||||
swarm.handle(muxer.multicodec, (conn) => {
|
||||
const muxedConn = muxer(conn, true)
|
||||
|
||||
var peerIdForConn
|
||||
|
||||
muxedConn.on('stream', (conn) => {
|
||||
function gotId () {
|
||||
if (peerIdForConn) {
|
||||
conn.peerId = peerIdForConn
|
||||
connHandler(swarm.protocols, conn)
|
||||
} else {
|
||||
setTimeout(gotId, 100)
|
||||
}
|
||||
}
|
||||
|
||||
if (swarm.identify) {
|
||||
return gotId()
|
||||
}
|
||||
|
||||
connHandler(swarm.protocols, conn)
|
||||
})
|
||||
|
||||
// if identify is enabled, attempt to do it for muxer reuse
|
||||
if (swarm.identify) {
|
||||
identify.exec(conn, muxedConn, swarm._peerInfo, (err, pi) => {
|
||||
if (err) {
|
||||
return console.log('Identify exec failed', err)
|
||||
}
|
||||
|
||||
peerIdForConn = pi.id
|
||||
swarm.muxedConns[pi.id.toB58String()] = {}
|
||||
swarm.muxedConns[pi.id.toB58String()].muxer = muxedConn
|
||||
swarm.muxedConns[pi.id.toB58String()].conn = conn // to be able to extract addrs
|
||||
|
||||
swarm.emit('peer-mux-established', pi)
|
||||
|
||||
muxedConn.on('close', () => {
|
||||
delete swarm.muxedConns[pi.id.toB58String()]
|
||||
swarm.emit('peer-mux-closed', pi)
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
reuse () {
|
||||
swarm.identify = true
|
||||
swarm.handle(identify.multicodec, identify.handler(swarm._peerInfo, swarm))
|
||||
}
|
||||
}
|
||||
}
|
18
src/default-handler.js
Normal file
18
src/default-handler.js
Normal file
@ -0,0 +1,18 @@
|
||||
'use strict'
|
||||
|
||||
const multistream = require('multistream-select')
|
||||
|
||||
// incomming connection handler
|
||||
module.exports = function connHandler (protocols, conn) {
|
||||
var msS = new multistream.Select()
|
||||
|
||||
Object.keys(protocols).forEach((protocol) => {
|
||||
if (!protocol) {
|
||||
return
|
||||
}
|
||||
|
||||
msS.addHandler(protocol, protocols[protocol])
|
||||
})
|
||||
|
||||
msS.handle(conn)
|
||||
}
|
161
src/dial.js
Normal file
161
src/dial.js
Normal file
@ -0,0 +1,161 @@
|
||||
'use strict'
|
||||
|
||||
const multistream = require('multistream-select')
|
||||
const DuplexPassThrough = require('duplex-passthrough')
|
||||
|
||||
const connHandler = require('./default-handler')
|
||||
|
||||
module.exports = function dial (swarm) {
|
||||
return (pi, protocol, callback) => {
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = null
|
||||
}
|
||||
|
||||
if (!callback) {
|
||||
callback = function noop () {}
|
||||
}
|
||||
|
||||
const pt = new DuplexPassThrough()
|
||||
|
||||
const b58Id = pi.id.toB58String()
|
||||
|
||||
if (!swarm.muxedConns[b58Id]) {
|
||||
if (!swarm.conns[b58Id]) {
|
||||
attemptDial(pi, (err, conn) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
gotWarmedUpConn(conn)
|
||||
})
|
||||
} else {
|
||||
const conn = swarm.conns[b58Id]
|
||||
swarm.conns[b58Id] = undefined
|
||||
gotWarmedUpConn(conn)
|
||||
}
|
||||
} else {
|
||||
if (!protocol) {
|
||||
return callback()
|
||||
}
|
||||
gotMuxer(swarm.muxedConns[b58Id].muxer)
|
||||
}
|
||||
|
||||
return pt
|
||||
|
||||
function gotWarmedUpConn (conn) {
|
||||
attemptMuxerUpgrade(conn, (err, muxer) => {
|
||||
if (!protocol) {
|
||||
if (err) {
|
||||
swarm.conns[b58Id] = conn
|
||||
}
|
||||
return callback()
|
||||
}
|
||||
|
||||
if (err) {
|
||||
// couldn't upgrade to Muxer, it is ok
|
||||
protocolHandshake(conn, protocol, callback)
|
||||
} else {
|
||||
gotMuxer(muxer)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function gotMuxer (muxer) {
|
||||
openConnInMuxedConn(muxer, (conn) => {
|
||||
protocolHandshake(conn, protocol, callback)
|
||||
})
|
||||
}
|
||||
|
||||
function attemptDial (pi, cb) {
|
||||
const tKeys = swarm.availableTransports(pi)
|
||||
|
||||
if (tKeys.length === 0) {
|
||||
return cb(new Error('No available tranport to dial to'))
|
||||
}
|
||||
|
||||
nextTransport(tKeys.shift())
|
||||
|
||||
function nextTransport (key) {
|
||||
const multiaddrs = pi.multiaddrs.slice()
|
||||
swarm.transport.dial(key, multiaddrs, (err, conn) => {
|
||||
if (err) {
|
||||
if (tKeys.length === 0) {
|
||||
return cb(new Error('Could not dial in any of the transports'))
|
||||
}
|
||||
return nextTransport(tKeys.shift())
|
||||
}
|
||||
cb(null, conn)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function attemptMuxerUpgrade (conn, cb) {
|
||||
const muxers = Object.keys(swarm.muxers)
|
||||
if (muxers.length === 0) {
|
||||
return cb(new Error('no muxers available'))
|
||||
}
|
||||
|
||||
// 1. try to handshake in one of the muxers available
|
||||
// 2. if succeeds
|
||||
// - add the muxedConn to the list of muxedConns
|
||||
// - add incomming new streams to connHandler
|
||||
|
||||
nextMuxer(muxers.shift())
|
||||
|
||||
function nextMuxer (key) {
|
||||
var msI = new multistream.Interactive()
|
||||
msI.handle(conn, function () {
|
||||
msI.select(key, (err, conn) => {
|
||||
if (err) {
|
||||
if (muxers.length === 0) {
|
||||
cb(new Error('could not upgrade to stream muxing'))
|
||||
} else {
|
||||
nextMuxer(muxers.shift())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const muxedConn = swarm.muxers[key](conn, false)
|
||||
swarm.muxedConns[b58Id] = {}
|
||||
swarm.muxedConns[b58Id].muxer = muxedConn
|
||||
swarm.muxedConns[b58Id].conn = conn
|
||||
|
||||
swarm.emit('peer-mux-established', pi)
|
||||
|
||||
muxedConn.on('close', () => {
|
||||
delete swarm.muxedConns[pi.id.toB58String()]
|
||||
swarm.emit('peer-mux-closed', pi)
|
||||
})
|
||||
|
||||
// in case identify is on
|
||||
muxedConn.on('stream', (conn) => {
|
||||
conn.peerId = pi.id
|
||||
connHandler(swarm.protocols, conn)
|
||||
})
|
||||
|
||||
cb(null, muxedConn)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function openConnInMuxedConn (muxer, cb) {
|
||||
cb(muxer.newStream())
|
||||
}
|
||||
|
||||
function protocolHandshake (conn, protocol, cb) {
|
||||
var msI = new multistream.Interactive()
|
||||
msI.handle(conn, function () {
|
||||
msI.select(protocol, (err, conn) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
pt.wrapStream(conn)
|
||||
pt.peerId = pi.id
|
||||
callback(null, pt)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
@ -14,11 +14,7 @@ const Info = require('peer-info')
|
||||
const Id = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const isNode = !global.window
|
||||
|
||||
const identity = isNode
|
||||
? fs.readFileSync(path.join(__dirname, 'identify.proto'))
|
||||
: require('buffer!./identify.proto')
|
||||
const identity = fs.readFileSync(path.join(__dirname, 'identify.proto'))
|
||||
|
||||
const pbStream = require('protocol-buffers-stream')(identity)
|
||||
|
||||
@ -45,7 +41,7 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
|
||||
|
||||
pbs.on('identify', (msg) => {
|
||||
if (msg.observedAddr.length > 0) {
|
||||
peerInfo.multiaddr.addSafe(msg.observedAddr)
|
||||
peerInfo.multiaddr.addSafe(multiaddr(msg.observedAddr))
|
||||
}
|
||||
|
||||
const peerId = Id.createFromPubKey(msg.publicKey)
|
||||
@ -63,7 +59,7 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
|
||||
protocolVersion: 'na',
|
||||
agentVersion: 'na',
|
||||
publicKey: peerInfo.id.pubKey,
|
||||
listenAddrs: peerInfo.multiaddrs.map((mh) => { return mh.buffer }),
|
||||
listenAddrs: peerInfo.multiaddrs.map((mh) => mh.buffer),
|
||||
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
|
||||
})
|
||||
|
||||
@ -74,15 +70,15 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
|
||||
}
|
||||
|
||||
exports.handler = (peerInfo, swarm) => {
|
||||
return function (conn) {
|
||||
return (conn) => {
|
||||
// 1. receive incoming observed info about me
|
||||
// 2. update my own information (on peerInfo)
|
||||
// 3. send back what I see from the other (get from swarm.muxedConns[incPeerID].conn.getObservedAddrs()
|
||||
var pbs = pbStream()
|
||||
|
||||
pbs.on('identify', function (msg) {
|
||||
pbs.on('identify', (msg) => {
|
||||
if (msg.observedAddr.length > 0) {
|
||||
peerInfo.multiaddr.addSafe(msg.observedAddr)
|
||||
peerInfo.multiaddr.addSafe(multiaddr(msg.observedAddr))
|
||||
}
|
||||
|
||||
const peerId = Id.createFromPubKey(msg.publicKey)
|
||||
@ -93,9 +89,7 @@ exports.handler = (peerInfo, swarm) => {
|
||||
protocolVersion: 'na',
|
||||
agentVersion: 'na',
|
||||
publicKey: peerInfo.id.pubKey,
|
||||
listenAddrs: peerInfo.multiaddrs.map(function (ma) {
|
||||
return ma.buffer
|
||||
}),
|
||||
listenAddrs: peerInfo.multiaddrs.map((ma) => ma.buffer),
|
||||
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
|
||||
})
|
||||
pbs.finalize()
|
||||
|
313
src/index.js
313
src/index.js
@ -1,11 +1,18 @@
|
||||
'use strict'
|
||||
|
||||
const multistream = require('multistream-select')
|
||||
const identify = require('./identify')
|
||||
const DuplexPassThrough = require('duplex-passthrough')
|
||||
const util = require('util')
|
||||
const EE = require('events').EventEmitter
|
||||
const parallel = require('run-parallel')
|
||||
const contains = require('lodash.contains')
|
||||
|
||||
const transport = require('./transport')
|
||||
const connection = require('./connection')
|
||||
const dial = require('./dial')
|
||||
|
||||
exports = module.exports = Swarm
|
||||
|
||||
util.inherits(Swarm, EE)
|
||||
|
||||
function Swarm (peerInfo) {
|
||||
if (!(this instanceof Swarm)) {
|
||||
return new Swarm(peerInfo)
|
||||
@ -15,97 +22,13 @@ function Swarm (peerInfo) {
|
||||
throw new Error('You must provide a value for `peerInfo`')
|
||||
}
|
||||
|
||||
this._peerInfo = peerInfo
|
||||
|
||||
// transports --
|
||||
|
||||
// { key: transport }; e.g { tcp: <tcp> }
|
||||
this.transports = {}
|
||||
|
||||
this.transport = {}
|
||||
|
||||
this.transport.add = (key, transport, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
if (!callback) { callback = noop }
|
||||
|
||||
if (this.transports[key]) {
|
||||
throw new Error('There is already a transport with this key')
|
||||
}
|
||||
this.transports[key] = transport
|
||||
callback()
|
||||
}
|
||||
|
||||
this.transport.dial = (key, multiaddrs, callback) => {
|
||||
const t = this.transports[key]
|
||||
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
// a) filter the multiaddrs that are actually valid for this transport (use a func from the transport itself) (maybe even make the transport do that)
|
||||
multiaddrs = t.filter(multiaddrs)
|
||||
|
||||
// b) if multiaddrs.length = 1, return the conn from the
|
||||
// transport, otherwise, create a passthrough
|
||||
if (multiaddrs.length === 1) {
|
||||
const conn = t.dial(multiaddrs.shift(), {ready: () => {
|
||||
const cb = callback
|
||||
callback = noop // this is done to avoid connection drops as connect errors
|
||||
cb(null, conn)
|
||||
}})
|
||||
conn.once('error', () => {
|
||||
callback(new Error('failed to connect to every multiaddr'))
|
||||
})
|
||||
return conn
|
||||
}
|
||||
|
||||
// c) multiaddrs should already be a filtered list
|
||||
// specific for the transport we are using
|
||||
const pt = new DuplexPassThrough()
|
||||
|
||||
next(multiaddrs.shift())
|
||||
return pt
|
||||
function next (multiaddr) {
|
||||
const conn = t.dial(multiaddr, {ready: () => {
|
||||
pt.wrapStream(conn)
|
||||
const cb = callback
|
||||
callback = noop // this is done to avoid connection drops as connect errors
|
||||
cb(null, pt)
|
||||
}})
|
||||
|
||||
conn.once('error', () => {
|
||||
if (multiaddrs.length === 0) {
|
||||
return callback(new Error('failed to connect to every multiaddr'))
|
||||
}
|
||||
next(multiaddrs.shift())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
this.transport.listen = (key, options, handler, callback) => {
|
||||
// if no callback is passed, we pass conns to connHandler
|
||||
if (!handler) { handler = connHandler }
|
||||
|
||||
const multiaddrs = this.transports[key].filter(peerInfo.multiaddrs)
|
||||
|
||||
this.transports[key].createListener(multiaddrs, handler, (err, maUpdate) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
if (maUpdate) {
|
||||
// because we can listen on port 0...
|
||||
peerInfo.multiaddr.replace(multiaddrs, maUpdate)
|
||||
}
|
||||
|
||||
callback()
|
||||
})
|
||||
}
|
||||
|
||||
this.transport.close = (key, callback) => {
|
||||
this.transports[key].close(callback)
|
||||
}
|
||||
|
||||
// connections --
|
||||
|
||||
// { peerIdB58: { conn: <conn> }}
|
||||
@ -122,204 +45,60 @@ function Swarm (peerInfo) {
|
||||
// { protocol: handler }
|
||||
this.protocols = {}
|
||||
|
||||
this.connection = {}
|
||||
this.connection.addUpgrade = () => {}
|
||||
|
||||
// { muxerCodec: <muxer> } e.g { '/spdy/0.3.1': spdy }
|
||||
this.muxers = {}
|
||||
this.connection.addStreamMuxer = (muxer) => {
|
||||
// for dialing
|
||||
this.muxers[muxer.multicodec] = muxer
|
||||
|
||||
// for listening
|
||||
this.handle(muxer.multicodec, (conn) => {
|
||||
const muxedConn = muxer(conn, true)
|
||||
muxedConn.on('stream', (conn) => {
|
||||
connHandler(conn)
|
||||
// is the Identify protocol enabled?
|
||||
this.identify = false
|
||||
|
||||
this.transport = transport(this)
|
||||
this.connection = connection(this)
|
||||
|
||||
this.availableTransports = (pi) => {
|
||||
const addrs = pi.multiaddrs
|
||||
|
||||
// Only listen on transports we actually have addresses for
|
||||
return Object.keys(this.transports).filter((ts) => {
|
||||
// ipfs multiaddrs are not dialable so we drop them here
|
||||
let dialable = addrs.map((addr) => {
|
||||
if (contains(addr.protoNames(), 'ipfs')) {
|
||||
return addr.decapsulate('ipfs')
|
||||
}
|
||||
return addr
|
||||
})
|
||||
|
||||
// if identify is enabled, attempt to do it for muxer reuse
|
||||
if (this.identify) {
|
||||
identify.exec(conn, muxedConn, peerInfo, (err, pi) => {
|
||||
if (err) {
|
||||
return console.log('Identify exec failed', err)
|
||||
}
|
||||
this.muxedConns[pi.id.toB58String()] = {}
|
||||
this.muxedConns[pi.id.toB58String()].muxer = muxedConn
|
||||
this.muxedConns[pi.id.toB58String()].conn = conn // to be able to extract addrs
|
||||
})
|
||||
}
|
||||
return this.transports[ts].filter(dialable).length > 0
|
||||
})
|
||||
}
|
||||
|
||||
// enable the Identify protocol
|
||||
this.identify = false
|
||||
this.connection.reuse = () => {
|
||||
this.identify = true
|
||||
this.handle(identify.multicodec, identify.handler(peerInfo, this))
|
||||
}
|
||||
|
||||
const self = this // prefered this to bind
|
||||
|
||||
// incomming connection handler
|
||||
function connHandler (conn) {
|
||||
var msS = new multistream.Select()
|
||||
Object.keys(self.protocols).forEach((protocol) => {
|
||||
if (!protocol) { return }
|
||||
msS.addHandler(protocol, self.protocols[protocol])
|
||||
})
|
||||
msS.handle(conn)
|
||||
}
|
||||
|
||||
// higher level (public) API
|
||||
this.dial = (pi, protocol, callback) => {
|
||||
var pt = null
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = null
|
||||
} else {
|
||||
pt = new DuplexPassThrough()
|
||||
}
|
||||
this.dial = dial(this)
|
||||
|
||||
const b58Id = pi.id.toB58String()
|
||||
if (!this.muxedConns[b58Id]) {
|
||||
if (!this.conns[b58Id]) {
|
||||
attemptDial(pi, (err, conn) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
gotWarmedUpConn(conn)
|
||||
})
|
||||
} else {
|
||||
const conn = this.conns[b58Id]
|
||||
this.conns[b58Id] = undefined
|
||||
gotWarmedUpConn(conn)
|
||||
}
|
||||
} else {
|
||||
gotMuxer(this.muxedConns[b58Id].muxer)
|
||||
}
|
||||
|
||||
return pt
|
||||
|
||||
function gotWarmedUpConn (conn) {
|
||||
attemptMuxerUpgrade(conn, (err, muxer) => {
|
||||
if (!protocol) {
|
||||
if (err) {
|
||||
self.conns[b58Id] = conn
|
||||
}
|
||||
return callback()
|
||||
}
|
||||
|
||||
if (err) {
|
||||
// couldn't upgrade to Muxer, it is ok
|
||||
protocolHandshake(conn, protocol, callback)
|
||||
} else {
|
||||
gotMuxer(muxer)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function gotMuxer (muxer) {
|
||||
openConnInMuxedConn(muxer, (conn) => {
|
||||
protocolHandshake(conn, protocol, callback)
|
||||
})
|
||||
}
|
||||
|
||||
function attemptDial (pi, cb) {
|
||||
const tKeys = Object.keys(self.transports)
|
||||
nextTransport(tKeys.shift())
|
||||
|
||||
function nextTransport (key) {
|
||||
const multiaddrs = pi.multiaddrs.slice()
|
||||
self.transport.dial(key, multiaddrs, (err, conn) => {
|
||||
if (err) {
|
||||
if (tKeys.length === 0) {
|
||||
return cb(new Error('Could not dial in any of the transports'))
|
||||
}
|
||||
return nextTransport(tKeys.shift())
|
||||
}
|
||||
cb(null, conn)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function attemptMuxerUpgrade (conn, cb) {
|
||||
const muxers = Object.keys(self.muxers)
|
||||
if (muxers.length === 0) {
|
||||
return cb(new Error('no muxers available'))
|
||||
}
|
||||
|
||||
// 1. try to handshake in one of the muxers available
|
||||
// 2. if succeeds
|
||||
// - add the muxedConn to the list of muxedConns
|
||||
// - add incomming new streams to connHandler
|
||||
|
||||
nextMuxer(muxers.shift())
|
||||
|
||||
function nextMuxer (key) {
|
||||
var msI = new multistream.Interactive()
|
||||
msI.handle(conn, function () {
|
||||
msI.select(key, (err, conn) => {
|
||||
if (err) {
|
||||
if (muxers.length === 0) {
|
||||
cb(new Error('could not upgrade to stream muxing'))
|
||||
} else {
|
||||
nextMuxer(muxers.shift())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const muxedConn = self.muxers[key](conn, false)
|
||||
self.muxedConns[b58Id] = {}
|
||||
self.muxedConns[b58Id].muxer = muxedConn
|
||||
self.muxedConns[b58Id].conn = conn
|
||||
|
||||
// in case identify is on
|
||||
muxedConn.on('stream', connHandler)
|
||||
|
||||
cb(null, muxedConn)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
function openConnInMuxedConn (muxer, cb) {
|
||||
cb(muxer.newStream())
|
||||
}
|
||||
|
||||
function protocolHandshake (conn, protocol, cb) {
|
||||
var msI = new multistream.Interactive()
|
||||
msI.handle(conn, function () {
|
||||
msI.select(protocol, (err, conn) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
pt.wrapStream(conn)
|
||||
callback(null, pt)
|
||||
})
|
||||
})
|
||||
}
|
||||
// Start listening on all available transports
|
||||
this.listen = (callback) => {
|
||||
parallel(this.availableTransports(peerInfo).map((ts) => (cb) => {
|
||||
// Listen on the given transport
|
||||
this.transport.listen(ts, {}, null, cb)
|
||||
}), callback)
|
||||
}
|
||||
|
||||
this.handle = (protocol, handler) => {
|
||||
this.protocols[protocol] = handler
|
||||
}
|
||||
|
||||
this.close = (callback) => {
|
||||
var count = 0
|
||||
this.unhandle = (protocol, handler) => {
|
||||
if (this.protocols[protocol]) {
|
||||
delete this.protocols[protocol]
|
||||
}
|
||||
}
|
||||
|
||||
this.close = (callback) => {
|
||||
Object.keys(this.muxedConns).forEach((key) => {
|
||||
this.muxedConns[key].muxer.end()
|
||||
})
|
||||
|
||||
Object.keys(this.transports).forEach((key) => {
|
||||
this.transports[key].close(() => {
|
||||
if (++count === Object.keys(this.transports).length) {
|
||||
callback()
|
||||
}
|
||||
})
|
||||
})
|
||||
parallel(Object.keys(this.transports).map((key) => {
|
||||
return (cb) => this.transports[key].close(cb)
|
||||
}), callback)
|
||||
}
|
||||
}
|
||||
|
||||
function noop () {}
|
||||
|
117
src/transport.js
Normal file
117
src/transport.js
Normal file
@ -0,0 +1,117 @@
|
||||
'use strict'
|
||||
|
||||
const contains = require('lodash.contains')
|
||||
const DuplexPassThrough = require('duplex-passthrough')
|
||||
|
||||
const connHandler = require('./default-handler')
|
||||
|
||||
module.exports = function (swarm) {
|
||||
return {
|
||||
add (key, transport, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
if (!callback) { callback = noop }
|
||||
|
||||
if (swarm.transports[key]) {
|
||||
throw new Error('There is already a transport with this key')
|
||||
}
|
||||
swarm.transports[key] = transport
|
||||
callback()
|
||||
},
|
||||
|
||||
dial (key, multiaddrs, callback) {
|
||||
const t = swarm.transports[key]
|
||||
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
// a) filter the multiaddrs that are actually valid for this transport (use a func from the transport itself) (maybe even make the transport do that)
|
||||
multiaddrs = dialables(t, multiaddrs)
|
||||
|
||||
// b) if multiaddrs.length = 1, return the conn from the
|
||||
// transport, otherwise, create a passthrough
|
||||
if (multiaddrs.length === 1) {
|
||||
const conn = t.dial(multiaddrs.shift(), {ready: () => {
|
||||
const cb = callback
|
||||
callback = noop // this is done to avoid connection drops as connect errors
|
||||
cb(null, conn)
|
||||
}})
|
||||
conn.once('error', () => {
|
||||
callback(new Error('failed to connect to every multiaddr'))
|
||||
})
|
||||
return conn
|
||||
}
|
||||
|
||||
// c) multiaddrs should already be a filtered list
|
||||
// specific for the transport we are using
|
||||
const pt = new DuplexPassThrough()
|
||||
|
||||
next(multiaddrs.shift())
|
||||
return pt
|
||||
function next (multiaddr) {
|
||||
const conn = t.dial(multiaddr, {ready: () => {
|
||||
pt.wrapStream(conn)
|
||||
const cb = callback
|
||||
callback = noop // this is done to avoid connection drops as connect errors
|
||||
cb(null, pt)
|
||||
}})
|
||||
|
||||
conn.once('error', () => {
|
||||
if (multiaddrs.length === 0) {
|
||||
return callback(new Error('failed to connect to every multiaddr'))
|
||||
}
|
||||
next(multiaddrs.shift())
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
listen (key, options, handler, callback) {
|
||||
// if no callback is passed, we pass conns to connHandler
|
||||
if (!handler) {
|
||||
handler = connHandler.bind(null, swarm.protocols)
|
||||
}
|
||||
|
||||
const multiaddrs = dialables(swarm.transports[key], swarm._peerInfo.multiaddrs)
|
||||
|
||||
swarm.transports[key].createListener(multiaddrs, handler, (err, maUpdate) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
if (maUpdate) {
|
||||
// because we can listen on port 0...
|
||||
swarm._peerInfo.multiaddr.replace(multiaddrs, maUpdate)
|
||||
}
|
||||
|
||||
callback()
|
||||
})
|
||||
},
|
||||
|
||||
close (key, callback) {
|
||||
const transport = swarm.transports[key]
|
||||
|
||||
if (!transport) {
|
||||
return callback(new Error(`Trying to close non existing transport: ${key}`))
|
||||
}
|
||||
|
||||
transport.close(callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// transform given multiaddrs to a list of dialable addresses
|
||||
// for the given transport `tp`.
|
||||
function dialables (tp, multiaddrs) {
|
||||
return tp.filter(multiaddrs.map((addr) => {
|
||||
// ipfs multiaddrs are not dialable so we drop them here
|
||||
if (contains(addr.protoNames(), 'ipfs')) {
|
||||
return addr.decapsulate('ipfs')
|
||||
}
|
||||
|
||||
return addr
|
||||
}))
|
||||
}
|
||||
|
||||
function noop () {}
|
@ -6,8 +6,7 @@ const expect = require('chai').expect
|
||||
const Swarm = require('../src')
|
||||
|
||||
describe('basics', () => {
|
||||
it('throws on missing peerInfo', (done) => {
|
||||
expect(Swarm).to.throw(Error)
|
||||
done()
|
||||
it('throws on missing peerInfo', () => {
|
||||
expect(() => Swarm()).to.throw(Error)
|
||||
})
|
||||
})
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
|
||||
const parallel = require('run-parallel')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Peer = require('peer-info')
|
||||
const Swarm = require('../src')
|
||||
@ -46,9 +47,17 @@ describe('transport - tcp', function () {
|
||||
function ready () {
|
||||
if (++count === 2) {
|
||||
expect(peerA.multiaddrs.length).to.equal(1)
|
||||
expect(peerA.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9888'))
|
||||
expect(
|
||||
peerA.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9888'))
|
||||
).to.be.equal(
|
||||
true
|
||||
)
|
||||
expect(peerB.multiaddrs.length).to.equal(1)
|
||||
expect(peerB.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9999'))
|
||||
expect(
|
||||
peerB.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9999'))
|
||||
).to.be.equal(
|
||||
true
|
||||
)
|
||||
done()
|
||||
}
|
||||
}
|
||||
@ -84,15 +93,10 @@ describe('transport - tcp', function () {
|
||||
})
|
||||
|
||||
it('close', (done) => {
|
||||
var count = 0
|
||||
swarmA.transport.close('tcp', closed)
|
||||
swarmB.transport.close('tcp', closed)
|
||||
|
||||
function closed () {
|
||||
if (++count === 2) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.transport.close('tcp', cb),
|
||||
(cb) => swarmB.transport.close('tcp', cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('support port 0', (done) => {
|
||||
@ -124,7 +128,11 @@ describe('transport - tcp', function () {
|
||||
|
||||
function ready () {
|
||||
expect(peer.multiaddrs.length).to.equal(1)
|
||||
expect(peer.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/0.0.0.0/tcp/9050'))
|
||||
expect(
|
||||
peer.multiaddrs[0].equals(multiaddr('/ip4/0.0.0.0/tcp/9050'))
|
||||
).to.be.equal(
|
||||
true
|
||||
)
|
||||
swarm.close(done)
|
||||
}
|
||||
})
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
|
||||
const parallel = require('run-parallel')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Peer = require('peer-info')
|
||||
const Swarm = require('../src')
|
||||
@ -17,12 +18,11 @@ describe('transport - websockets', function () {
|
||||
var peerA = new Peer()
|
||||
var peerB = new Peer()
|
||||
|
||||
before((done) => {
|
||||
before(() => {
|
||||
peerA.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9888/websockets'))
|
||||
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9999/websockets'))
|
||||
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9999/websockets/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'))
|
||||
swarmA = new Swarm(peerA)
|
||||
swarmB = new Swarm(peerB)
|
||||
done()
|
||||
})
|
||||
|
||||
it('add', (done) => {
|
||||
@ -35,23 +35,28 @@ describe('transport - websockets', function () {
|
||||
})
|
||||
|
||||
it('listen', (done) => {
|
||||
var count = 0
|
||||
swarmA.transport.listen('ws', {}, (conn) => {
|
||||
conn.pipe(conn)
|
||||
}, ready)
|
||||
swarmB.transport.listen('ws', {}, (conn) => {
|
||||
conn.pipe(conn)
|
||||
}, ready)
|
||||
|
||||
function ready () {
|
||||
if (++count === 2) {
|
||||
expect(peerA.multiaddrs.length).to.equal(1)
|
||||
expect(peerA.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9888/websockets'))
|
||||
expect(peerB.multiaddrs.length).to.equal(1)
|
||||
expect(peerB.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9999/websockets'))
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.transport.listen('ws', {}, (conn) => {
|
||||
conn.pipe(conn)
|
||||
}, cb),
|
||||
(cb) => swarmB.transport.listen('ws', {}, (conn) => {
|
||||
conn.pipe(conn)
|
||||
}, cb)
|
||||
], () => {
|
||||
expect(peerA.multiaddrs.length).to.equal(1)
|
||||
expect(
|
||||
peerA.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9888/websockets'))
|
||||
).to.be.equal(
|
||||
true
|
||||
)
|
||||
expect(peerB.multiaddrs.length).to.equal(1)
|
||||
expect(
|
||||
peerB.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9999/websockets/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'))
|
||||
).to.equal(
|
||||
true
|
||||
)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('dial', (done) => {
|
||||
@ -80,14 +85,9 @@ describe('transport - websockets', function () {
|
||||
})
|
||||
|
||||
it('close', (done) => {
|
||||
var count = 0
|
||||
swarmA.transport.close('ws', closed)
|
||||
swarmB.transport.close('ws', closed)
|
||||
|
||||
function closed () {
|
||||
if (++count === 2) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.transport.close('ws', cb),
|
||||
(cb) => swarmB.transport.close('ws', cb)
|
||||
], done)
|
||||
})
|
||||
})
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
|
||||
const parallel = require('run-parallel')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Peer = require('peer-info')
|
||||
const Swarm = require('../src')
|
||||
@ -10,7 +11,7 @@ const TCP = require('libp2p-tcp')
|
||||
const multiplex = require('libp2p-spdy')
|
||||
|
||||
describe('stream muxing with multiplex (on TCP)', function () {
|
||||
this.timeout(20000)
|
||||
this.timeout(60 * 1000)
|
||||
|
||||
var swarmA
|
||||
var peerA
|
||||
@ -37,35 +38,22 @@ describe('stream muxing with multiplex (on TCP)', function () {
|
||||
swarmC = new Swarm(peerC)
|
||||
|
||||
swarmA.transport.add('tcp', new TCP())
|
||||
swarmA.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
swarmB.transport.add('tcp', new TCP())
|
||||
swarmB.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
swarmC.transport.add('tcp', new TCP())
|
||||
swarmC.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
var counter = 0
|
||||
|
||||
function ready () {
|
||||
if (++counter === 3) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
|
||||
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
|
||||
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
var counter = 0
|
||||
|
||||
swarmA.close(closed)
|
||||
swarmB.close(closed)
|
||||
swarmC.close(closed)
|
||||
|
||||
function closed () {
|
||||
if (++counter === 3) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.close(cb),
|
||||
(cb) => swarmB.close(cb),
|
||||
(cb) => swarmC.close(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('add', (done) => {
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
|
||||
const parallel = require('run-parallel')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Peer = require('peer-info')
|
||||
const Swarm = require('../src')
|
||||
@ -10,7 +11,7 @@ const TCP = require('libp2p-tcp')
|
||||
const spdy = require('libp2p-spdy')
|
||||
|
||||
describe('stream muxing with spdy (on TCP)', function () {
|
||||
this.timeout(20000)
|
||||
this.timeout(60 * 1000)
|
||||
|
||||
var swarmA
|
||||
var peerA
|
||||
@ -37,42 +38,28 @@ describe('stream muxing with spdy (on TCP)', function () {
|
||||
swarmC = new Swarm(peerC)
|
||||
|
||||
swarmA.transport.add('tcp', new TCP())
|
||||
swarmA.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
swarmB.transport.add('tcp', new TCP())
|
||||
swarmB.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
swarmC.transport.add('tcp', new TCP())
|
||||
swarmC.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
var counter = 0
|
||||
|
||||
function ready () {
|
||||
if (++counter === 3) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
|
||||
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
|
||||
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
var counter = 0
|
||||
|
||||
swarmA.close(closed)
|
||||
swarmB.close(closed)
|
||||
swarmC.close(closed)
|
||||
|
||||
function closed () {
|
||||
if (++counter === 3) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.close(cb),
|
||||
(cb) => swarmB.close(cb)
|
||||
// (cb) => swarmC.close(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('add', (done) => {
|
||||
it('add', () => {
|
||||
swarmA.connection.addStreamMuxer(spdy)
|
||||
swarmB.connection.addStreamMuxer(spdy)
|
||||
swarmC.connection.addStreamMuxer(spdy)
|
||||
done()
|
||||
})
|
||||
|
||||
it('handle + dial on protocol', (done) => {
|
||||
@ -128,4 +115,12 @@ describe('stream muxing with spdy (on TCP)', function () {
|
||||
}, 500)
|
||||
})
|
||||
})
|
||||
|
||||
it('close one end, make sure the other does not blow', (done) => {
|
||||
swarmC.close((err) => {
|
||||
if (err) throw err
|
||||
// to make sure it has time to propagate
|
||||
setTimeout(done, 1000)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -4,9 +4,6 @@
|
||||
describe('secio conn upgrade (on TCP)', function () {
|
||||
this.timeout(20000)
|
||||
|
||||
before((done) => { done() })
|
||||
after((done) => { done() })
|
||||
|
||||
it.skip('add', (done) => {})
|
||||
it.skip('dial', (done) => {})
|
||||
it.skip('tls on a muxed stream (not the full conn)', (done) => {})
|
||||
|
@ -2,9 +2,6 @@
|
||||
'use strict'
|
||||
|
||||
describe('tls conn upgrade (on TCP)', function () {
|
||||
before((done) => { done() })
|
||||
after((done) => { done() })
|
||||
|
||||
it.skip('add', (done) => {})
|
||||
it.skip('dial', (done) => {})
|
||||
it.skip('tls on a muxed stream (not the full conn)', (done) => {})
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
|
||||
const parallel = require('run-parallel')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Peer = require('peer-info')
|
||||
const Swarm = require('../src')
|
||||
@ -21,37 +22,25 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function (
|
||||
peerB = new Peer()
|
||||
|
||||
peerA.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9001'))
|
||||
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9002'))
|
||||
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9002/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'))
|
||||
|
||||
swarmA = new Swarm(peerA)
|
||||
swarmB = new Swarm(peerB)
|
||||
|
||||
swarmA.transport.add('tcp', new TCP())
|
||||
swarmA.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
swarmB.transport.add('tcp', new TCP())
|
||||
swarmB.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
var counter = 0
|
||||
|
||||
function ready () {
|
||||
if (++counter === 2) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
|
||||
(cb) => swarmB.transport.listen('tcp', {}, null, cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
var counter = 0
|
||||
|
||||
swarmA.close(closed)
|
||||
swarmB.close(closed)
|
||||
|
||||
function closed () {
|
||||
if (++counter === 2) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.close(cb),
|
||||
(cb) => swarmB.close(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('handle a protocol', (done) => {
|
||||
@ -103,4 +92,11 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function (
|
||||
conn.on('end', done)
|
||||
})
|
||||
})
|
||||
|
||||
it('unhandle', (done) => {
|
||||
const proto = '/bananas/1.0.0'
|
||||
swarmA.unhandle(proto)
|
||||
expect(swarmA.protocols[proto]).to.not.exist
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
|
||||
const parallel = require('run-parallel')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Peer = require('peer-info')
|
||||
const Swarm = require('../src')
|
||||
@ -11,7 +12,7 @@ const WebSockets = require('libp2p-websockets')
|
||||
const spdy = require('libp2p-spdy')
|
||||
|
||||
describe('high level API - with everything mixed all together!', function () {
|
||||
this.timeout(20000)
|
||||
this.timeout(100000)
|
||||
|
||||
var swarmA // tcp
|
||||
var peerA
|
||||
@ -45,19 +46,13 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
var counter = 0
|
||||
|
||||
swarmA.close(closed)
|
||||
swarmB.close(closed)
|
||||
swarmC.close(closed)
|
||||
swarmD.close(closed)
|
||||
swarmE.close(closed)
|
||||
|
||||
function closed () {
|
||||
if (++counter === 4) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.close(cb),
|
||||
(cb) => swarmB.close(cb),
|
||||
// (cb) => swarmC.close(cb),
|
||||
(cb) => swarmD.close(cb),
|
||||
(cb) => swarmE.close(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('add tcp', (done) => {
|
||||
@ -66,21 +61,14 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
peerC.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/0'))
|
||||
|
||||
swarmA.transport.add('tcp', new TCP())
|
||||
swarmA.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
swarmB.transport.add('tcp', new TCP())
|
||||
swarmB.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
swarmC.transport.add('tcp', new TCP())
|
||||
swarmC.transport.listen('tcp', {}, null, ready)
|
||||
|
||||
var counter = 0
|
||||
|
||||
function ready () {
|
||||
if (++counter === 3) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
|
||||
(cb) => swarmB.transport.listen('tcp', {}, null, cb)
|
||||
// (cb) => swarmC.transport.listen('tcp', {}, null, cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it.skip('add utp', (done) => {})
|
||||
@ -92,27 +80,23 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
peerE.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9042/websockets'))
|
||||
|
||||
swarmB.transport.add('ws', new WebSockets())
|
||||
swarmB.transport.listen('ws', {}, null, ready)
|
||||
|
||||
swarmC.transport.add('ws', new WebSockets())
|
||||
swarmC.transport.listen('ws', {}, null, ready)
|
||||
|
||||
swarmD.transport.add('ws', new WebSockets())
|
||||
swarmD.transport.listen('ws', {}, null, ready)
|
||||
|
||||
swarmE.transport.add('ws', new WebSockets())
|
||||
swarmE.transport.listen('ws', {}, null, ready)
|
||||
|
||||
var counter = 0
|
||||
|
||||
function ready () {
|
||||
if (++counter === 4) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
parallel([
|
||||
(cb) => swarmB.transport.listen('ws', {}, null, cb),
|
||||
// (cb) => swarmC.transport.listen('ws', {}, null, cb),
|
||||
(cb) => swarmD.transport.listen('ws', {}, null, cb),
|
||||
(cb) => swarmE.transport.listen('ws', {}, null, cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('add spdy', (done) => {
|
||||
it('listen automatically', (done) => {
|
||||
swarmC.listen(done)
|
||||
})
|
||||
|
||||
it('add spdy', () => {
|
||||
swarmA.connection.addStreamMuxer(spdy)
|
||||
swarmB.connection.addStreamMuxer(spdy)
|
||||
swarmC.connection.addStreamMuxer(spdy)
|
||||
@ -124,13 +108,37 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
swarmC.connection.reuse()
|
||||
swarmD.connection.reuse()
|
||||
swarmE.connection.reuse()
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
it.skip('add multiplex', (done) => {})
|
||||
it.skip('add multiplex', () => {})
|
||||
|
||||
it('dial from tcp to tcp+ws', (done) => {
|
||||
it('warm up from A to B on tcp to tcp+ws', (done) => {
|
||||
parallel([
|
||||
(cb) => swarmB.once('peer-mux-established', (peerInfo) => {
|
||||
expect(peerInfo.id.toB58String()).to.equal(peerA.id.toB58String())
|
||||
cb()
|
||||
}),
|
||||
(cb) => swarmA.once('peer-mux-established', (peerInfo) => {
|
||||
expect(peerInfo.id.toB58String()).to.equal(peerB.id.toB58String())
|
||||
cb()
|
||||
}),
|
||||
(cb) => swarmA.dial(peerB, (err) => {
|
||||
expect(err).to.not.exist
|
||||
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
|
||||
cb()
|
||||
})
|
||||
], done)
|
||||
})
|
||||
|
||||
it('warm up a warmed up, from B to A', (done) => {
|
||||
swarmB.dial(peerA, (err) => {
|
||||
expect(err).to.not.exist
|
||||
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('dial from tcp to tcp+ws, on protocol', (done) => {
|
||||
swarmB.handle('/anona/1.0.0', (conn) => {
|
||||
conn.pipe(conn)
|
||||
})
|
||||
@ -145,6 +153,14 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('dial from ws to ws no proto', (done) => {
|
||||
swarmD.dial(peerE, (err) => {
|
||||
expect(err).to.not.exist
|
||||
expect(Object.keys(swarmD.muxedConns).length).to.equal(1)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('dial from ws to ws', (done) => {
|
||||
swarmE.handle('/abacaxi/1.0.0', (conn) => {
|
||||
conn.pipe(conn)
|
||||
@ -182,11 +198,13 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
|
||||
it('dial from tcp+ws to tcp+ws', (done) => {
|
||||
swarmC.handle('/mamao/1.0.0', (conn) => {
|
||||
expect(conn.peerId).to.exist
|
||||
conn.pipe(conn)
|
||||
})
|
||||
|
||||
swarmA.dial(peerC, '/mamao/1.0.0', (err, conn) => {
|
||||
expect(err).to.not.exist
|
||||
expect(conn.peerId).to.exist
|
||||
expect(Object.keys(swarmA.muxedConns).length).to.equal(2)
|
||||
conn.end()
|
||||
|
||||
@ -194,4 +212,11 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
conn.on('end', done)
|
||||
})
|
||||
})
|
||||
|
||||
it('close a muxer emits event', (done) => {
|
||||
parallel([
|
||||
(cb) => swarmC.close(cb),
|
||||
(cb) => swarmA.once('peer-mux-closed', () => cb())
|
||||
], done)
|
||||
})
|
||||
})
|
||||
|
@ -22,14 +22,12 @@ describe('transport - websockets', function () {
|
||||
|
||||
var swarm
|
||||
|
||||
before((done) => {
|
||||
before(() => {
|
||||
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
|
||||
// use a pre generated Id to save time
|
||||
const idSrc = Id.createFromB58String(b58IdSrc)
|
||||
const peerSrc = new Peer(idSrc)
|
||||
swarm = new Swarm(peerSrc)
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
it('add', (done) => {
|
||||
@ -62,28 +60,24 @@ describe('high level API - 1st without stream multiplexing (on websockets)', fun
|
||||
var swarm
|
||||
var peerDst
|
||||
|
||||
before((done) => {
|
||||
before(() => {
|
||||
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
|
||||
// use a pre generated Id to save time
|
||||
const idSrc = Id.createFromB58String(b58IdSrc)
|
||||
const peerSrc = new Peer(idSrc)
|
||||
swarm = new Swarm(peerSrc)
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
done()
|
||||
// swarm.close(done)
|
||||
swarm.close(done)
|
||||
})
|
||||
|
||||
it('add ws', (done) => {
|
||||
it('add ws', () => {
|
||||
swarm.transport.add('ws', new WebSockets())
|
||||
expect(Object.keys(swarm.transports).length).to.equal(1)
|
||||
done()
|
||||
})
|
||||
|
||||
it('create Dst peer info', (done) => {
|
||||
it('create Dst peer info', () => {
|
||||
const b58IdDst = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
|
||||
// use a pre generated Id to save time
|
||||
const idDst = Id.createFromB58String(b58IdDst)
|
||||
@ -91,7 +85,6 @@ describe('high level API - 1st without stream multiplexing (on websockets)', fun
|
||||
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9200/websockets')
|
||||
peerDst.multiaddr.add(ma)
|
||||
done()
|
||||
})
|
||||
|
||||
it('dial on protocol', (done) => {
|
||||
|
Reference in New Issue
Block a user