mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-11 06:41:35 +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`
|
- `protocol`
|
||||||
- `handler` - function called when we receive a dial on `protocol. Signature must be `function (conn) {}`
|
- `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)`
|
### `swarm.close(callback)`
|
||||||
|
|
||||||
close all the listeners and muxers.
|
close all the listeners and muxers.
|
||||||
|
19
package.json
19
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-swarm",
|
"name": "libp2p-swarm",
|
||||||
"version": "0.12.5",
|
"version": "0.12.8",
|
||||||
"description": "libp2p swarm implementation in JavaScript",
|
"description": "libp2p swarm implementation in JavaScript",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"jsnext:main": "src/index.js",
|
"jsnext:main": "src/index.js",
|
||||||
@ -45,11 +45,8 @@
|
|||||||
"istanbul": "^0.4.3",
|
"istanbul": "^0.4.3",
|
||||||
"libp2p-multiplex": "^0.2.1",
|
"libp2p-multiplex": "^0.2.1",
|
||||||
"libp2p-spdy": "^0.3.1",
|
"libp2p-spdy": "^0.3.1",
|
||||||
"libp2p-tcp": "^0.5.0",
|
"libp2p-tcp": "^0.5.1",
|
||||||
"libp2p-websockets": "^0.4.1",
|
"libp2p-websockets": "^0.4.3",
|
||||||
"multiaddr": "^1.4.0",
|
|
||||||
"peer-id": "^0.6.6",
|
|
||||||
"peer-info": "^0.6.2",
|
|
||||||
"pre-commit": "^1.1.2",
|
"pre-commit": "^1.1.2",
|
||||||
"stream-pair": "^1.0.3"
|
"stream-pair": "^1.0.3"
|
||||||
},
|
},
|
||||||
@ -58,8 +55,12 @@
|
|||||||
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
|
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
|
||||||
"ip-address": "^5.8.0",
|
"ip-address": "^5.8.0",
|
||||||
"lodash.contains": "^2.4.3",
|
"lodash.contains": "^2.4.3",
|
||||||
|
"multiaddr": "^1.4.0",
|
||||||
"multistream-select": "^0.6.5",
|
"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": {
|
"aegir": {
|
||||||
"webpack": {
|
"webpack": {
|
||||||
@ -74,8 +75,8 @@
|
|||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
"David Dias <mail@daviddias.me>",
|
"David Dias <mail@daviddias.me>",
|
||||||
"Francisco Baio Dias <xicombd@gmail.com>",
|
"Francisco Baio Dias <xicombd@gmail.com>",
|
||||||
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
"Pau Ramon Revilla <masylum@gmail.com>",
|
"Pau Ramon Revilla <masylum@gmail.com>",
|
||||||
"Richard Littauer <richard.littauer@gmail.com>",
|
"Richard Littauer <richard.littauer@gmail.com>"
|
||||||
"dignifiedquire <dignifiedquire@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 contains = require('lodash.contains')
|
||||||
const util = require('util')
|
const util = require('util')
|
||||||
const EE = require('events').EventEmitter
|
const EE = require('events').EventEmitter
|
||||||
|
const parallel = require('run-parallel')
|
||||||
|
|
||||||
exports = module.exports = Swarm
|
exports = module.exports = Swarm
|
||||||
|
|
||||||
@ -117,7 +118,13 @@ function Swarm (peerInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.transport.close = (key, callback) => {
|
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 --
|
// connections --
|
||||||
@ -360,20 +367,20 @@ function Swarm (peerInfo) {
|
|||||||
this.protocols[protocol] = handler
|
this.protocols[protocol] = handler
|
||||||
}
|
}
|
||||||
|
|
||||||
this.close = (callback) => {
|
this.unhandle = (protocol, handler) => {
|
||||||
var count = 0
|
if (this.protocols[protocol]) {
|
||||||
|
delete this.protocols[protocol]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.close = (callback) => {
|
||||||
Object.keys(this.muxedConns).forEach((key) => {
|
Object.keys(this.muxedConns).forEach((key) => {
|
||||||
this.muxedConns[key].muxer.end()
|
this.muxedConns[key].muxer.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
Object.keys(this.transports).forEach((key) => {
|
parallel(Object.keys(this.transports).map((key) => {
|
||||||
this.transports[key].close(() => {
|
return (cb) => this.transports[key].close(cb)
|
||||||
if (++count === Object.keys(this.transports).length) {
|
}), callback)
|
||||||
callback()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
|
|
||||||
|
const parallel = require('run-parallel')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Peer = require('peer-info')
|
const Peer = require('peer-info')
|
||||||
const Swarm = require('../src')
|
const Swarm = require('../src')
|
||||||
@ -92,15 +93,10 @@ describe('transport - tcp', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('close', (done) => {
|
it('close', (done) => {
|
||||||
var count = 0
|
parallel([
|
||||||
swarmA.transport.close('tcp', closed)
|
(cb) => swarmA.transport.close('tcp', cb),
|
||||||
swarmB.transport.close('tcp', closed)
|
(cb) => swarmB.transport.close('tcp', cb)
|
||||||
|
], done)
|
||||||
function closed () {
|
|
||||||
if (++count === 2) {
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('support port 0', (done) => {
|
it('support port 0', (done) => {
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
|
|
||||||
|
const parallel = require('run-parallel')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Peer = require('peer-info')
|
const Peer = require('peer-info')
|
||||||
const Swarm = require('../src')
|
const Swarm = require('../src')
|
||||||
@ -88,14 +89,9 @@ describe('transport - websockets', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('close', (done) => {
|
it('close', (done) => {
|
||||||
var count = 0
|
parallel([
|
||||||
swarmA.transport.close('ws', closed)
|
(cb) => swarmA.transport.close('ws', cb),
|
||||||
swarmB.transport.close('ws', closed)
|
(cb) => swarmB.transport.close('ws', cb)
|
||||||
|
], done)
|
||||||
function closed () {
|
|
||||||
if (++count === 2) {
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
|
|
||||||
|
const parallel = require('run-parallel')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Peer = require('peer-info')
|
const Peer = require('peer-info')
|
||||||
const Swarm = require('../src')
|
const Swarm = require('../src')
|
||||||
@ -10,7 +11,7 @@ const TCP = require('libp2p-tcp')
|
|||||||
const multiplex = require('libp2p-spdy')
|
const multiplex = require('libp2p-spdy')
|
||||||
|
|
||||||
describe('stream muxing with multiplex (on TCP)', function () {
|
describe('stream muxing with multiplex (on TCP)', function () {
|
||||||
this.timeout(20000)
|
this.timeout(60 * 1000)
|
||||||
|
|
||||||
var swarmA
|
var swarmA
|
||||||
var peerA
|
var peerA
|
||||||
@ -37,35 +38,22 @@ describe('stream muxing with multiplex (on TCP)', function () {
|
|||||||
swarmC = new Swarm(peerC)
|
swarmC = new Swarm(peerC)
|
||||||
|
|
||||||
swarmA.transport.add('tcp', new TCP())
|
swarmA.transport.add('tcp', new TCP())
|
||||||
swarmA.transport.listen('tcp', {}, null, ready)
|
|
||||||
|
|
||||||
swarmB.transport.add('tcp', new TCP())
|
swarmB.transport.add('tcp', new TCP())
|
||||||
swarmB.transport.listen('tcp', {}, null, ready)
|
|
||||||
|
|
||||||
swarmC.transport.add('tcp', new TCP())
|
swarmC.transport.add('tcp', new TCP())
|
||||||
swarmC.transport.listen('tcp', {}, null, ready)
|
|
||||||
|
|
||||||
var counter = 0
|
parallel([
|
||||||
|
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
|
||||||
function ready () {
|
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
|
||||||
if (++counter === 3) {
|
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
|
||||||
done()
|
], done)
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
after((done) => {
|
after((done) => {
|
||||||
var counter = 0
|
parallel([
|
||||||
|
(cb) => swarmA.close(cb),
|
||||||
swarmA.close(closed)
|
(cb) => swarmB.close(cb),
|
||||||
swarmB.close(closed)
|
(cb) => swarmC.close(cb)
|
||||||
swarmC.close(closed)
|
], done)
|
||||||
|
|
||||||
function closed () {
|
|
||||||
if (++counter === 3) {
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('add', (done) => {
|
it('add', (done) => {
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
|
|
||||||
|
const parallel = require('run-parallel')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Peer = require('peer-info')
|
const Peer = require('peer-info')
|
||||||
const Swarm = require('../src')
|
const Swarm = require('../src')
|
||||||
@ -10,7 +11,7 @@ const TCP = require('libp2p-tcp')
|
|||||||
const spdy = require('libp2p-spdy')
|
const spdy = require('libp2p-spdy')
|
||||||
|
|
||||||
describe('stream muxing with spdy (on TCP)', function () {
|
describe('stream muxing with spdy (on TCP)', function () {
|
||||||
this.timeout(20000)
|
this.timeout(60 * 1000)
|
||||||
|
|
||||||
var swarmA
|
var swarmA
|
||||||
var peerA
|
var peerA
|
||||||
@ -37,35 +38,22 @@ describe('stream muxing with spdy (on TCP)', function () {
|
|||||||
swarmC = new Swarm(peerC)
|
swarmC = new Swarm(peerC)
|
||||||
|
|
||||||
swarmA.transport.add('tcp', new TCP())
|
swarmA.transport.add('tcp', new TCP())
|
||||||
swarmA.transport.listen('tcp', {}, null, ready)
|
|
||||||
|
|
||||||
swarmB.transport.add('tcp', new TCP())
|
swarmB.transport.add('tcp', new TCP())
|
||||||
swarmB.transport.listen('tcp', {}, null, ready)
|
|
||||||
|
|
||||||
swarmC.transport.add('tcp', new TCP())
|
swarmC.transport.add('tcp', new TCP())
|
||||||
swarmC.transport.listen('tcp', {}, null, ready)
|
|
||||||
|
|
||||||
var counter = 0
|
parallel([
|
||||||
|
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
|
||||||
function ready () {
|
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
|
||||||
if (++counter === 3) {
|
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
|
||||||
done()
|
], done)
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
after((done) => {
|
after((done) => {
|
||||||
var counter = 0
|
parallel([
|
||||||
|
(cb) => swarmA.close(cb),
|
||||||
swarmA.close(closed)
|
(cb) => swarmB.close(cb),
|
||||||
swarmB.close(closed)
|
(cb) => swarmC.close(cb)
|
||||||
swarmC.close(closed)
|
], done)
|
||||||
|
|
||||||
function closed () {
|
|
||||||
if (++counter === 3) {
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('add', (done) => {
|
it('add', (done) => {
|
||||||
@ -128,4 +116,12 @@ describe('stream muxing with spdy (on TCP)', function () {
|
|||||||
}, 500)
|
}, 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 expect = require('chai').expect
|
||||||
|
|
||||||
|
const parallel = require('run-parallel')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Peer = require('peer-info')
|
const Peer = require('peer-info')
|
||||||
const Swarm = require('../src')
|
const Swarm = require('../src')
|
||||||
@ -42,16 +43,10 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function (
|
|||||||
})
|
})
|
||||||
|
|
||||||
after((done) => {
|
after((done) => {
|
||||||
var counter = 0
|
parallel([
|
||||||
|
(cb) => swarmA.close(cb),
|
||||||
swarmA.close(closed)
|
(cb) => swarmB.close(cb)
|
||||||
swarmB.close(closed)
|
], done)
|
||||||
|
|
||||||
function closed () {
|
|
||||||
if (++counter === 2) {
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('handle a protocol', (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)
|
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 expect = require('chai').expect
|
||||||
|
|
||||||
|
const parallel = require('run-parallel')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Peer = require('peer-info')
|
const Peer = require('peer-info')
|
||||||
const Swarm = require('../src')
|
const Swarm = require('../src')
|
||||||
@ -45,19 +46,13 @@ describe('high level API - with everything mixed all together!', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
after((done) => {
|
after((done) => {
|
||||||
var counter = 0
|
parallel([
|
||||||
|
(cb) => swarmA.close(cb),
|
||||||
swarmA.close(closed)
|
(cb) => swarmB.close(cb),
|
||||||
swarmB.close(closed)
|
// (cb) => swarmC.close(cb),
|
||||||
// swarmC.close(closed)
|
(cb) => swarmD.close(cb),
|
||||||
swarmD.close(closed)
|
(cb) => swarmE.close(cb)
|
||||||
swarmE.close(closed)
|
], done)
|
||||||
|
|
||||||
function closed () {
|
|
||||||
if (++counter === 4) {
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('add tcp', (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) => {
|
it('dial from ws to ws', (done) => {
|
||||||
swarmE.handle('/abacaxi/1.0.0', (conn) => {
|
swarmE.handle('/abacaxi/1.0.0', (conn) => {
|
||||||
conn.pipe(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) => {
|
it('close a muxer emits event', (done) => {
|
||||||
swarmC.close(() => {})
|
swarmC.close((err) => {
|
||||||
|
if (err) throw err
|
||||||
|
})
|
||||||
swarmA.once('peer-mux-closed', (peerInfo) => {
|
swarmA.once('peer-mux-closed', (peerInfo) => {
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
@ -73,8 +73,7 @@ describe('high level API - 1st without stream multiplexing (on websockets)', fun
|
|||||||
})
|
})
|
||||||
|
|
||||||
after((done) => {
|
after((done) => {
|
||||||
done()
|
swarm.close(done)
|
||||||
// swarm.close(done)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('add ws', (done) => {
|
it('add ws', (done) => {
|
||||||
|
Reference in New Issue
Block a user