mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-19 10:31:58 +00:00
Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
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 |
12
package.json
12
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-swarm",
|
"name": "libp2p-swarm",
|
||||||
"version": "0.10.7",
|
"version": "0.12.5",
|
||||||
"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",
|
||||||
@@ -11,8 +11,8 @@
|
|||||||
"test:node": "gulp test:node",
|
"test:node": "gulp test:node",
|
||||||
"test:browser": "gulp test:browser",
|
"test:browser": "gulp test:browser",
|
||||||
"release": "gulp release",
|
"release": "gulp release",
|
||||||
"release-minor": "gulp release --minor",
|
"release-minor": "gulp release --type minor",
|
||||||
"release-major": "gulp release --major",
|
"release-major": "gulp release --type major",
|
||||||
"coverage": "gulp coverage",
|
"coverage": "gulp coverage",
|
||||||
"coverage-publish": "aegir-coverage publish"
|
"coverage-publish": "aegir-coverage publish"
|
||||||
},
|
},
|
||||||
@@ -40,13 +40,13 @@
|
|||||||
"bl": "^1.1.2",
|
"bl": "^1.1.2",
|
||||||
"buffer-loader": "0.0.1",
|
"buffer-loader": "0.0.1",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"aegir": "^2.1.1",
|
"aegir": "^3.0.0",
|
||||||
"gulp": "^3.9.1",
|
"gulp": "^3.9.1",
|
||||||
"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.4.0",
|
"libp2p-tcp": "^0.5.0",
|
||||||
"libp2p-websockets": "^0.3.2",
|
"libp2p-websockets": "^0.4.1",
|
||||||
"multiaddr": "^1.4.0",
|
"multiaddr": "^1.4.0",
|
||||||
"peer-id": "^0.6.6",
|
"peer-id": "^0.6.6",
|
||||||
"peer-info": "^0.6.2",
|
"peer-info": "^0.6.2",
|
||||||
|
53
src/index.js
53
src/index.js
@@ -4,9 +4,13 @@ const multistream = require('multistream-select')
|
|||||||
const identify = require('./identify')
|
const identify = require('./identify')
|
||||||
const DuplexPassThrough = require('duplex-passthrough')
|
const DuplexPassThrough = require('duplex-passthrough')
|
||||||
const contains = require('lodash.contains')
|
const contains = require('lodash.contains')
|
||||||
|
const util = require('util')
|
||||||
|
const EE = require('events').EventEmitter
|
||||||
|
|
||||||
exports = module.exports = Swarm
|
exports = module.exports = Swarm
|
||||||
|
|
||||||
|
util.inherits(Swarm, EE)
|
||||||
|
|
||||||
function Swarm (peerInfo) {
|
function Swarm (peerInfo) {
|
||||||
if (!(this instanceof Swarm)) {
|
if (!(this instanceof Swarm)) {
|
||||||
return new Swarm(peerInfo)
|
return new Swarm(peerInfo)
|
||||||
@@ -144,7 +148,23 @@ function Swarm (peerInfo) {
|
|||||||
// for listening
|
// for listening
|
||||||
this.handle(muxer.multicodec, (conn) => {
|
this.handle(muxer.multicodec, (conn) => {
|
||||||
const muxedConn = muxer(conn, true)
|
const muxedConn = muxer(conn, true)
|
||||||
|
|
||||||
|
var peerIdForConn
|
||||||
|
|
||||||
muxedConn.on('stream', (conn) => {
|
muxedConn.on('stream', (conn) => {
|
||||||
|
function gotId () {
|
||||||
|
if (peerIdForConn) {
|
||||||
|
conn.peerId = peerIdForConn
|
||||||
|
connHandler(conn)
|
||||||
|
} else {
|
||||||
|
setTimeout(gotId, 100)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.identify) {
|
||||||
|
return gotId()
|
||||||
|
}
|
||||||
|
|
||||||
connHandler(conn)
|
connHandler(conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -154,9 +174,18 @@ function Swarm (peerInfo) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
return console.log('Identify exec failed', err)
|
return console.log('Identify exec failed', err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
peerIdForConn = pi.id
|
||||||
this.muxedConns[pi.id.toB58String()] = {}
|
this.muxedConns[pi.id.toB58String()] = {}
|
||||||
this.muxedConns[pi.id.toB58String()].muxer = muxedConn
|
this.muxedConns[pi.id.toB58String()].muxer = muxedConn
|
||||||
this.muxedConns[pi.id.toB58String()].conn = conn // to be able to extract addrs
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -183,15 +212,19 @@ function Swarm (peerInfo) {
|
|||||||
|
|
||||||
// higher level (public) API
|
// higher level (public) API
|
||||||
this.dial = (pi, protocol, callback) => {
|
this.dial = (pi, protocol, callback) => {
|
||||||
var pt = null
|
|
||||||
if (typeof protocol === 'function') {
|
if (typeof protocol === 'function') {
|
||||||
callback = protocol
|
callback = protocol
|
||||||
protocol = null
|
protocol = null
|
||||||
} else {
|
|
||||||
pt = new DuplexPassThrough()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!callback) {
|
||||||
|
callback = function noop () {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const pt = new DuplexPassThrough()
|
||||||
|
|
||||||
const b58Id = pi.id.toB58String()
|
const b58Id = pi.id.toB58String()
|
||||||
|
|
||||||
if (!this.muxedConns[b58Id]) {
|
if (!this.muxedConns[b58Id]) {
|
||||||
if (!this.conns[b58Id]) {
|
if (!this.conns[b58Id]) {
|
||||||
attemptDial(pi, (err, conn) => {
|
attemptDial(pi, (err, conn) => {
|
||||||
@@ -284,14 +317,25 @@ function Swarm (peerInfo) {
|
|||||||
self.muxedConns[b58Id].muxer = muxedConn
|
self.muxedConns[b58Id].muxer = muxedConn
|
||||||
self.muxedConns[b58Id].conn = conn
|
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
|
// in case identify is on
|
||||||
muxedConn.on('stream', connHandler)
|
muxedConn.on('stream', (conn) => {
|
||||||
|
conn.peerId = pi.id
|
||||||
|
connHandler(conn)
|
||||||
|
})
|
||||||
|
|
||||||
cb(null, muxedConn)
|
cb(null, muxedConn)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function openConnInMuxedConn (muxer, cb) {
|
function openConnInMuxedConn (muxer, cb) {
|
||||||
cb(muxer.newStream())
|
cb(muxer.newStream())
|
||||||
}
|
}
|
||||||
@@ -305,6 +349,7 @@ function Swarm (peerInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pt.wrapStream(conn)
|
pt.wrapStream(conn)
|
||||||
|
pt.peerId = pi.id
|
||||||
callback(null, pt)
|
callback(null, pt)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@@ -49,7 +49,7 @@ describe('high level API - with everything mixed all together!', function () {
|
|||||||
|
|
||||||
swarmA.close(closed)
|
swarmA.close(closed)
|
||||||
swarmB.close(closed)
|
swarmB.close(closed)
|
||||||
swarmC.close(closed)
|
// swarmC.close(closed)
|
||||||
swarmD.close(closed)
|
swarmD.close(closed)
|
||||||
swarmE.close(closed)
|
swarmE.close(closed)
|
||||||
|
|
||||||
@@ -135,6 +135,14 @@ describe('high level API - with everything mixed all together!', function () {
|
|||||||
conn.pipe(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) => {
|
swarmA.dial(peerB, '/anona/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist
|
expect(err).to.not.exist
|
||||||
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
|
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
|
||||||
@@ -182,11 +190,13 @@ describe('high level API - with everything mixed all together!', function () {
|
|||||||
|
|
||||||
it('dial from tcp+ws to tcp+ws', (done) => {
|
it('dial from tcp+ws to tcp+ws', (done) => {
|
||||||
swarmC.handle('/mamao/1.0.0', (conn) => {
|
swarmC.handle('/mamao/1.0.0', (conn) => {
|
||||||
|
expect(conn.peerId).to.exist
|
||||||
conn.pipe(conn)
|
conn.pipe(conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
swarmA.dial(peerC, '/mamao/1.0.0', (err, conn) => {
|
swarmA.dial(peerC, '/mamao/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist
|
expect(err).to.not.exist
|
||||||
|
expect(conn.peerId).to.exist
|
||||||
expect(Object.keys(swarmA.muxedConns).length).to.equal(2)
|
expect(Object.keys(swarmA.muxedConns).length).to.equal(2)
|
||||||
conn.end()
|
conn.end()
|
||||||
|
|
||||||
@@ -194,4 +204,11 @@ describe('high level API - with everything mixed all together!', function () {
|
|||||||
conn.on('end', done)
|
conn.on('end', done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('close a muxer emits event', (done) => {
|
||||||
|
swarmC.close(() => {})
|
||||||
|
swarmA.once('peer-mux-closed', (peerInfo) => {
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user