mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-10 14:21:33 +00:00
Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
20994f5320 | |||
eac00292f2 | |||
bf768d3585 | |||
05f799f983 | |||
a81c328bf7 | |||
a6ba60a5c4 | |||
594b770d8e | |||
dbf0d2c422 | |||
275434f873 | |||
631dad8647 | |||
3eac0e0dd6 | |||
30d4bb641e | |||
b0aeff8f53 | |||
998c71fc84 | |||
b31245adc8 | |||
85a064765a | |||
fb56cc3c30 | |||
03d0c52d4d | |||
0aa7bb72e7 | |||
e9b3d3496f |
@ -109,6 +109,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.
|
||||
|
19
package.json
19
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p-swarm",
|
||||
"version": "0.12.5",
|
||||
"version": "0.12.8",
|
||||
"description": "libp2p swarm implementation in JavaScript",
|
||||
"main": "lib/index.js",
|
||||
"jsnext:main": "src/index.js",
|
||||
@ -45,11 +45,8 @@
|
||||
"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.3",
|
||||
"pre-commit": "^1.1.2",
|
||||
"stream-pair": "^1.0.3"
|
||||
},
|
||||
@ -58,8 +55,12 @@
|
||||
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
|
||||
"ip-address": "^5.8.0",
|
||||
"lodash.contains": "^2.4.3",
|
||||
"multiaddr": "^1.4.0",
|
||||
"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": {
|
||||
@ -74,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>",
|
||||
"dignifiedquire <dignifiedquire@gmail.com>"
|
||||
"Richard Littauer <richard.littauer@gmail.com>"
|
||||
]
|
||||
}
|
27
src/index.js
27
src/index.js
@ -6,6 +6,7 @@ const DuplexPassThrough = require('duplex-passthrough')
|
||||
const contains = require('lodash.contains')
|
||||
const util = require('util')
|
||||
const EE = require('events').EventEmitter
|
||||
const parallel = require('run-parallel')
|
||||
|
||||
exports = module.exports = Swarm
|
||||
|
||||
@ -117,7 +118,13 @@ function Swarm (peerInfo) {
|
||||
}
|
||||
|
||||
this.transport.close = (key, callback) => {
|
||||
this.transports[key].close(callback)
|
||||
const transport = this.transports[key]
|
||||
|
||||
if (!transport) {
|
||||
return callback(new Error(`Trying to close non existing transport: ${key}`))
|
||||
}
|
||||
|
||||
transport.close(callback)
|
||||
}
|
||||
|
||||
// connections --
|
||||
@ -360,20 +367,20 @@ function Swarm (peerInfo) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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')
|
||||
@ -88,14 +89,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,35 +38,22 @@ 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) => {
|
||||
@ -128,4 +116,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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -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')
|
||||
@ -42,16 +43,10 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function (
|
||||
})
|
||||
|
||||
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 +98,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')
|
||||
@ -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) => {
|
||||
@ -153,6 +148,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)
|
||||
@ -206,7 +209,9 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
})
|
||||
|
||||
it('close a muxer emits event', (done) => {
|
||||
swarmC.close(() => {})
|
||||
swarmC.close((err) => {
|
||||
if (err) throw err
|
||||
})
|
||||
swarmA.once('peer-mux-closed', (peerInfo) => {
|
||||
done()
|
||||
})
|
||||
|
@ -73,8 +73,7 @@ describe('high level API - 1st without stream multiplexing (on websockets)', fun
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
done()
|
||||
// swarm.close(done)
|
||||
swarm.close(done)
|
||||
})
|
||||
|
||||
it('add ws', (done) => {
|
||||
|
Reference in New Issue
Block a user