mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-30 07:51:56 +00:00
Compare commits
23 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 |
@@ -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.
|
||||
|
22
package.json
22
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p-swarm",
|
||||
"version": "0.12.7",
|
||||
"version": "0.12.11",
|
||||
"description": "libp2p swarm implementation in JavaScript",
|
||||
"main": "lib/index.js",
|
||||
"jsnext:main": "src/index.js",
|
||||
@@ -40,27 +40,27 @@
|
||||
"bl": "^1.1.2",
|
||||
"buffer-loader": "0.0.1",
|
||||
"chai": "^3.5.0",
|
||||
"aegir": "^3.0.0",
|
||||
"aegir": "^3.0.1",
|
||||
"gulp": "^3.9.1",
|
||||
"istanbul": "^0.4.3",
|
||||
"libp2p-multiplex": "^0.2.1",
|
||||
"libp2p-spdy": "^0.3.1",
|
||||
"libp2p-tcp": "^0.5.0",
|
||||
"libp2p-websockets": "^0.4.1",
|
||||
"multiaddr": "^1.4.0",
|
||||
"peer-id": "^0.6.6",
|
||||
"peer-info": "^0.6.2",
|
||||
"libp2p-tcp": "^0.5.1",
|
||||
"libp2p-websockets": "^0.4.4",
|
||||
"pre-commit": "^1.1.2",
|
||||
"stream-pair": "^1.0.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "^2.0.0-rc.4",
|
||||
"babel-runtime": "^6.6.1",
|
||||
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
|
||||
"ip-address": "^5.8.0",
|
||||
"lodash.contains": "^2.4.3",
|
||||
"multiaddr": "^1.4.1",
|
||||
"multistream-select": "^0.6.5",
|
||||
"protocol-buffers-stream": "^1.3.1"
|
||||
"peer-id": "^0.6.6",
|
||||
"peer-info": "^0.6.2",
|
||||
"protocol-buffers-stream": "^1.3.1",
|
||||
"run-parallel": "^1.1.6"
|
||||
},
|
||||
"aegir": {
|
||||
"webpack": {
|
||||
@@ -75,8 +75,8 @@
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"David Dias <mail@daviddias.me>",
|
||||
"Francisco Baio Dias <xicombd@gmail.com>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Pau Ramon Revilla <masylum@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@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)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
348
src/index.js
348
src/index.js
@@ -1,12 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const async = require('async')
|
||||
const multistream = require('multistream-select')
|
||||
const identify = require('./identify')
|
||||
const DuplexPassThrough = require('duplex-passthrough')
|
||||
const contains = require('lodash.contains')
|
||||
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
|
||||
|
||||
@@ -21,106 +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.map((addr) => {
|
||||
// ipfs multiaddrs are not dialable so we drop them here
|
||||
if (contains(addr.protoNames(), 'ipfs')) {
|
||||
return addr.decapsulate('ipfs')
|
||||
}
|
||||
|
||||
return addr
|
||||
})
|
||||
)
|
||||
|
||||
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> }}
|
||||
@@ -137,224 +45,41 @@ 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)
|
||||
// is the Identify protocol enabled?
|
||||
this.identify = false
|
||||
|
||||
var peerIdForConn
|
||||
this.transport = transport(this)
|
||||
this.connection = connection(this)
|
||||
|
||||
muxedConn.on('stream', (conn) => {
|
||||
function gotId () {
|
||||
if (peerIdForConn) {
|
||||
conn.peerId = peerIdForConn
|
||||
connHandler(conn)
|
||||
} else {
|
||||
setTimeout(gotId, 100)
|
||||
}
|
||||
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')
|
||||
}
|
||||
|
||||
if (this.identify) {
|
||||
return gotId()
|
||||
}
|
||||
|
||||
connHandler(conn)
|
||||
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)
|
||||
}
|
||||
|
||||
peerIdForConn = pi.id
|
||||
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
|
||||
|
||||
self.emit('peer-mux-established', pi)
|
||||
|
||||
muxedConn.on('close', () => {
|
||||
delete self.muxedConns[pi.id.toB58String()]
|
||||
self.emit('peer-mux-closed', pi)
|
||||
})
|
||||
})
|
||||
}
|
||||
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) => {
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = null
|
||||
}
|
||||
this.dial = dial(this)
|
||||
|
||||
if (!callback) {
|
||||
callback = function noop () {}
|
||||
}
|
||||
|
||||
const pt = new DuplexPassThrough()
|
||||
|
||||
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
|
||||
|
||||
self.emit('peer-mux-established', pi)
|
||||
|
||||
muxedConn.on('close', () => {
|
||||
delete self.muxedConns[pi.id.toB58String()]
|
||||
self.emit('peer-mux-closed', pi)
|
||||
})
|
||||
|
||||
// in case identify is on
|
||||
muxedConn.on('stream', (conn) => {
|
||||
conn.peerId = pi.id
|
||||
connHandler(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)
|
||||
})
|
||||
})
|
||||
}
|
||||
// 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) => {
|
||||
@@ -372,15 +97,8 @@ function Swarm (peerInfo) {
|
||||
this.muxedConns[key].muxer.end()
|
||||
})
|
||||
|
||||
async.each(
|
||||
Object.keys(this.transports),
|
||||
(key, cb) => this.transports[key].close(cb),
|
||||
() => {
|
||||
// Ignoring close errors
|
||||
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')
|
||||
@@ -92,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) => {
|
||||
|
@@ -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,31 +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].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'))
|
||||
).to.equal(
|
||||
true
|
||||
)
|
||||
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) => {
|
||||
@@ -88,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 === 2) {
|
||||
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) => {
|
||||
@@ -130,7 +117,8 @@ describe('stream muxing with spdy (on TCP)', function () {
|
||||
})
|
||||
|
||||
it('close one end, make sure the other does not blow', (done) => {
|
||||
swarmC.close(() => {
|
||||
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')
|
||||
@@ -27,31 +28,19 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function (
|
||||
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) => {
|
||||
|
@@ -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')
|
||||
@@ -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,25 +108,41 @@ 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)
|
||||
})
|
||||
|
||||
swarmB.once('peer-mux-established', (peerInfo) => {
|
||||
expect(peerInfo.id.toB58String()).to.equal(peerA.id.toB58String())
|
||||
})
|
||||
|
||||
swarmA.once('peer-mux-established', (peerInfo) => {
|
||||
expect(peerInfo.id.toB58String()).to.equal(peerB.id.toB58String())
|
||||
})
|
||||
|
||||
swarmA.dial(peerB, '/anona/1.0.0', (err, conn) => {
|
||||
expect(err).to.not.exist
|
||||
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
|
||||
@@ -214,9 +214,9 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
})
|
||||
|
||||
it('close a muxer emits event', (done) => {
|
||||
swarmC.close(() => {})
|
||||
swarmA.once('peer-mux-closed', (peerInfo) => {
|
||||
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