mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-24 04:51:56 +00:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
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.
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p-swarm",
|
||||
"version": "0.12.5",
|
||||
"version": "0.12.7",
|
||||
"description": "libp2p swarm implementation in JavaScript",
|
||||
"main": "lib/index.js",
|
||||
"jsnext:main": "src/index.js",
|
||||
@@ -54,6 +54,7 @@
|
||||
"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",
|
||||
@@ -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>"
|
||||
]
|
||||
}
|
24
src/index.js
24
src/index.js
@@ -1,5 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const async = require('async')
|
||||
const multistream = require('multistream-select')
|
||||
const identify = require('./identify')
|
||||
const DuplexPassThrough = require('duplex-passthrough')
|
||||
@@ -360,20 +361,25 @@ 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()
|
||||
}
|
||||
})
|
||||
})
|
||||
async.each(
|
||||
Object.keys(this.transports),
|
||||
(key, cb) => this.transports[key].close(cb),
|
||||
() => {
|
||||
// Ignoring close errors
|
||||
callback()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -59,10 +59,10 @@ describe('stream muxing with spdy (on TCP)', function () {
|
||||
|
||||
swarmA.close(closed)
|
||||
swarmB.close(closed)
|
||||
swarmC.close(closed)
|
||||
// swarmC.close(closed)
|
||||
|
||||
function closed () {
|
||||
if (++counter === 3) {
|
||||
if (++counter === 2) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
@@ -128,4 +128,11 @@ describe('stream muxing with spdy (on TCP)', function () {
|
||||
}, 500)
|
||||
})
|
||||
})
|
||||
|
||||
it('close one end, make sure the other does not blow', (done) => {
|
||||
swarmC.close(() => {
|
||||
// to make sure it has time to propagate
|
||||
setTimeout(done, 1000)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@@ -103,4 +103,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()
|
||||
})
|
||||
})
|
||||
|
@@ -153,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)
|
||||
|
Reference in New Issue
Block a user