Compare commits

...

49 Commits

Author SHA1 Message Date
b0aeff8f53 chore: release version v0.12.6 2016-05-06 14:29:24 +01:00
998c71fc84 chore: update contributors 2016-05-06 14:29:24 +01:00
b31245adc8 Merge pull request #49 from diasdavid/fix/close-count
fix: call cb in close after all transport are closed
2016-05-06 14:19:13 +01:00
85a064765a fix: call cb in close after all transport are closed 2016-05-06 15:05:34 +02:00
fb56cc3c30 Merge pull request #48 from diasdavid/feat/unhandle
unhandle a protocol
2016-05-06 13:11:58 +01:00
03d0c52d4d unhandle a protocol 2016-05-06 12:49:31 +01:00
0aa7bb72e7 Merge pull request #47 from diasdavid/test/swarm-dial-no-proto
dialing in no proto is fine
2016-05-06 12:43:26 +01:00
e9b3d3496f dialing in no proto is fine 2016-05-06 12:31:23 +01:00
58e18dd01b chore: release version v0.12.5 2016-05-05 00:44:53 +01:00
fb017ebb07 chore: update contributors 2016-05-05 00:44:53 +01:00
08c4c169d6 remove unnecessary console.log 2016-05-05 00:13:48 +01:00
de927e8052 chore: release version v0.12.4 2016-05-04 20:13:00 +01:00
df8e61632b chore: update contributors 2016-05-04 20:13:00 +01:00
b453bd4f83 Merge pull request #46 from diasdavid/feat/improve-identify
Freeze handling conns till identify is finished on the incomming multiplexed streams
2016-05-04 21:11:22 +02:00
0143ab6449 freeze handling conns till identify is finished on the incomming multiplexed streams 2016-05-04 19:42:53 +01:00
02dd32e7df chore: release version v0.12.3 2016-05-04 16:57:00 +01:00
4fe91796cd chore: update contributors 2016-05-04 16:57:00 +01:00
352876cade Merge pull request #45 from diasdavid/feat/id-on-conns
attach peerId to the conn
2016-05-04 17:11:50 +02:00
41b700f509 attach peerId to the conn 2016-05-04 14:55:40 +01:00
eea7e91b15 chore: release version v0.12.2 2016-04-27 10:09:24 +01:00
b11a7972f5 chore: update contributors 2016-04-27 10:09:24 +01:00
15d5bc53fb update deps and npm scripts 2016-04-27 10:08:02 +01:00
9d911af8e0 Merge pull request #44 from diasdavid/feature/events
feature/events
2016-04-27 10:03:29 +01:00
9f1f3c82dc add peer-mux-closed event 2016-04-27 09:44:16 +01:00
d6a1f52962 add peer-mux-established event 2016-04-26 20:47:31 +01:00
7b536819b1 chore: update contributors 2016-04-25 02:29:18 +01:00
7158aaf702 chore: release version v0.12.1 2016-04-25 02:29:18 +01:00
bc87fad5f9 update deps 2016-04-25 02:27:34 +01:00
c9418399a7 bump version manually 2016-04-25 00:20:23 +01:00
2cac123405 chore: update contributors 2016-04-25 00:18:30 +01:00
ff47a9c228 chore: release version v0.11.8 2016-04-25 00:18:30 +01:00
f86a981eb2 update npm scripts 2016-04-25 00:17:07 +01:00
674d68000b chore: update contributors 2016-04-24 23:29:15 +01:00
ae371085c1 chore: release version v0.10.7 2016-04-24 23:29:15 +01:00
770bee3c66 Merge pull request #42 from diasdavid/fix/multiaddr
fix(identify): convert all addresses to multiaddr
2016-04-24 23:19:34 +01:00
6943e3e90b fix(identify): convert all addresses to multiaddr
Fixes #37
2016-04-24 18:39:35 +02:00
a008ebd5b9 chore: update contributors 2016-04-20 13:25:16 +01:00
20108d2de8 chore: release version v0.10.6 2016-04-20 13:25:16 +01:00
15fcfb737c Merge pull request #41 from diasdavid/feat/ipfs-multiaddr
feat: handle ipfs multiaddrs
2016-04-20 13:13:02 +01:00
0fa14c9608 feat: handle ipfs multiaddrs
Closes #38
2016-04-20 13:35:36 +02:00
ac7c8a150e Merge pull request #39 from diasdavid/fix/require
fix: always use fs.readFileSync
2016-04-19 21:43:44 +01:00
851c8ee2a3 fix: always use fs.readFileSync 2016-04-19 13:35:02 +02:00
7a3f9d08d5 chore: update contributors 2016-04-14 21:57:08 +01:00
52d60a7391 chore: release version v0.10.5 2016-04-14 21:57:08 +01:00
165068d05c add circle and bump up timeout, cause travis is slow 2016-04-14 20:52:03 +01:00
9baae15dcf Merge pull request #36 from dignifiedquire/cover
chore: Enable auto coverage reporting
2016-04-14 20:48:12 +01:00
b87524f36a chore: Enable auto coverage reporting 2016-04-14 15:25:30 +02:00
b0484c678e chore: release version v0.10.4 2016-04-14 13:30:49 +01:00
b6c498055f update deps 2016-04-14 12:15:50 +01:00
10 changed files with 197 additions and 57 deletions

View File

@ -11,6 +11,7 @@ before_install:
script:
- npm run lint
- npm test
- npm run coverage
addons:
firefox: 'latest'
@ -18,3 +19,6 @@ addons:
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
after_success:
- npm run coverage-publish

View File

@ -1,11 +1,11 @@
libp2p-swarm JavaScript implementation
======================================
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://img.shields.io/travis/diasdavid/js-libp2p-swarm/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-swarm)
![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square)
[![Coverage Status](https://coveralls.io/repos/github/diasdavid/js-libp2p-swarm/badge.svg?branch=master)](https://coveralls.io/github/diasdavid/js-libp2p-swarm?branch=master)
[![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-swarm.svg?style=flat-square)](https://david-dm.org/ipfs/js-libp2p-swarm)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
@ -19,7 +19,7 @@ libp2p-swarm is used by libp2p but it can be also used as a standalone module.
# Usage
## Install
## Install
libp2p-swarm is available on npm and so, like any other npm module, just:
@ -48,7 +48,7 @@ peerInfo is a [PeerInfo](https://github.com/diasdavid/js-peer-info) object that
libp2p-swarm expects transports that implement [interface-transport](https://github.com/diasdavid/abstract-transport). For example [libp2p-tcp](https://github.com/diasdavid/js-libp2p-tcp).
- `key` - the transport identifier
- `transport` -
- `transport` -
- `options`
- `callback`
@ -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.

12
circle.yml Normal file
View File

@ -0,0 +1,12 @@
machine:
node:
version: stable
dependencies:
pre:
- google-chrome --version
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
- sudo apt-get update
- sudo apt-get --only-upgrade install google-chrome-stable
- google-chrome --version

View File

@ -1,6 +1,6 @@
{
"name": "libp2p-swarm",
"version": "0.10.3",
"version": "0.12.6",
"description": "libp2p swarm implementation in JavaScript",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
@ -11,9 +11,10 @@
"test:node": "gulp test:node",
"test:browser": "gulp test:browser",
"release": "gulp release",
"release-minor": "gulp release --minor",
"release-major": "gulp release --major",
"coverage": "gulp coverage"
"release-minor": "gulp release --type minor",
"release-major": "gulp release --type major",
"coverage": "gulp coverage",
"coverage-publish": "aegir-coverage publish"
},
"repository": {
"type": "git",
@ -39,25 +40,27 @@
"bl": "^1.1.2",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"aegir": "^2.0.0",
"aegir": "^3.0.0",
"gulp": "^3.9.1",
"istanbul": "^0.4.2",
"istanbul": "^0.4.3",
"libp2p-multiplex": "^0.2.1",
"libp2p-spdy": "^0.2.3",
"libp2p-tcp": "^0.4.0",
"libp2p-websockets": "^0.3.1",
"multiaddr": "^1.3.0",
"peer-id": "^0.6.0",
"peer-info": "^0.6.0",
"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",
"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.0.2",
"multistream-select": "^0.6.1",
"protocol-buffers-stream": "^1.2.0"
"ip-address": "^5.8.0",
"lodash.contains": "^2.4.3",
"multistream-select": "^0.6.5",
"protocol-buffers-stream": "^1.3.1"
},
"aegir": {
"webpack": {
@ -67,5 +70,13 @@
}
}
}
}
}
},
"contributors": [
"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>"
]
}

View File

@ -14,11 +14,7 @@ const Info = require('peer-info')
const Id = require('peer-id')
const multiaddr = require('multiaddr')
const isNode = !global.window
const identity = isNode
? fs.readFileSync(path.join(__dirname, 'identify.proto'))
: require('buffer!./identify.proto')
const identity = fs.readFileSync(path.join(__dirname, 'identify.proto'))
const pbStream = require('protocol-buffers-stream')(identity)
@ -45,7 +41,7 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
pbs.on('identify', (msg) => {
if (msg.observedAddr.length > 0) {
peerInfo.multiaddr.addSafe(msg.observedAddr)
peerInfo.multiaddr.addSafe(multiaddr(msg.observedAddr))
}
const peerId = Id.createFromPubKey(msg.publicKey)
@ -63,7 +59,7 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
protocolVersion: 'na',
agentVersion: 'na',
publicKey: peerInfo.id.pubKey,
listenAddrs: peerInfo.multiaddrs.map((mh) => { return mh.buffer }),
listenAddrs: peerInfo.multiaddrs.map((mh) => mh.buffer),
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
})
@ -74,15 +70,15 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
}
exports.handler = (peerInfo, swarm) => {
return function (conn) {
return (conn) => {
// 1. receive incoming observed info about me
// 2. update my own information (on peerInfo)
// 3. send back what I see from the other (get from swarm.muxedConns[incPeerID].conn.getObservedAddrs()
var pbs = pbStream()
pbs.on('identify', function (msg) {
pbs.on('identify', (msg) => {
if (msg.observedAddr.length > 0) {
peerInfo.multiaddr.addSafe(msg.observedAddr)
peerInfo.multiaddr.addSafe(multiaddr(msg.observedAddr))
}
const peerId = Id.createFromPubKey(msg.publicKey)
@ -93,9 +89,7 @@ exports.handler = (peerInfo, swarm) => {
protocolVersion: 'na',
agentVersion: 'na',
publicKey: peerInfo.id.pubKey,
listenAddrs: peerInfo.multiaddrs.map(function (ma) {
return ma.buffer
}),
listenAddrs: peerInfo.multiaddrs.map((ma) => ma.buffer),
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
})
pbs.finalize()

View File

@ -1,11 +1,17 @@
'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
exports = module.exports = Swarm
util.inherits(Swarm, EE)
function Swarm (peerInfo) {
if (!(this instanceof Swarm)) {
return new Swarm(peerInfo)
@ -87,7 +93,16 @@ function Swarm (peerInfo) {
// if no callback is passed, we pass conns to connHandler
if (!handler) { handler = connHandler }
const multiaddrs = this.transports[key].filter(peerInfo.multiaddrs)
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) {
@ -134,7 +149,23 @@ function Swarm (peerInfo) {
// for listening
this.handle(muxer.multicodec, (conn) => {
const muxedConn = muxer(conn, true)
var peerIdForConn
muxedConn.on('stream', (conn) => {
function gotId () {
if (peerIdForConn) {
conn.peerId = peerIdForConn
connHandler(conn)
} else {
setTimeout(gotId, 100)
}
}
if (this.identify) {
return gotId()
}
connHandler(conn)
})
@ -144,9 +175,18 @@ function Swarm (peerInfo) {
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)
})
})
}
})
@ -173,15 +213,19 @@ function Swarm (peerInfo) {
// higher level (public) API
this.dial = (pi, protocol, callback) => {
var pt = null
if (typeof protocol === 'function') {
callback = protocol
protocol = null
} else {
pt = new DuplexPassThrough()
}
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) => {
@ -274,14 +318,25 @@ function Swarm (peerInfo) {
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', connHandler)
muxedConn.on('stream', (conn) => {
conn.peerId = pi.id
connHandler(conn)
})
cb(null, muxedConn)
})
})
}
}
function openConnInMuxedConn (muxer, cb) {
cb(muxer.newStream())
}
@ -295,6 +350,7 @@ function Swarm (peerInfo) {
}
pt.wrapStream(conn)
pt.peerId = pi.id
callback(null, pt)
})
})
@ -305,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()
}
)
}
}

View File

@ -46,9 +46,17 @@ describe('transport - tcp', function () {
function ready () {
if (++count === 2) {
expect(peerA.multiaddrs.length).to.equal(1)
expect(peerA.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9888'))
expect(
peerA.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9888'))
).to.be.equal(
true
)
expect(peerB.multiaddrs.length).to.equal(1)
expect(peerB.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9999'))
expect(
peerB.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9999'))
).to.be.equal(
true
)
done()
}
}
@ -124,7 +132,11 @@ describe('transport - tcp', function () {
function ready () {
expect(peer.multiaddrs.length).to.equal(1)
expect(peer.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/0.0.0.0/tcp/9050'))
expect(
peer.multiaddrs[0].equals(multiaddr('/ip4/0.0.0.0/tcp/9050'))
).to.be.equal(
true
)
swarm.close(done)
}
})

View File

@ -46,9 +46,17 @@ describe('transport - websockets', function () {
function ready () {
if (++count === 2) {
expect(peerA.multiaddrs.length).to.equal(1)
expect(peerA.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9888/websockets'))
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]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9999/websockets'))
expect(
peerB.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9999/websockets'))
).to.equal(
true
)
done()
}
}

View File

@ -21,7 +21,7 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function (
peerB = new Peer()
peerA.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9001'))
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9002'))
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9002/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'))
swarmA = new Swarm(peerA)
swarmB = new Swarm(peerB)
@ -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()
})
})

View File

@ -11,7 +11,7 @@ const WebSockets = require('libp2p-websockets')
const spdy = require('libp2p-spdy')
describe('high level API - with everything mixed all together!', function () {
this.timeout(20000)
this.timeout(100000)
var swarmA // tcp
var peerA
@ -49,7 +49,7 @@ describe('high level API - with everything mixed all together!', function () {
swarmA.close(closed)
swarmB.close(closed)
swarmC.close(closed)
// swarmC.close(closed)
swarmD.close(closed)
swarmE.close(closed)
@ -135,6 +135,14 @@ describe('high level API - with everything mixed all together!', function () {
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)
@ -145,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)
@ -182,11 +198,13 @@ describe('high level API - with everything mixed all together!', function () {
it('dial from tcp+ws to tcp+ws', (done) => {
swarmC.handle('/mamao/1.0.0', (conn) => {
expect(conn.peerId).to.exist
conn.pipe(conn)
})
swarmA.dial(peerC, '/mamao/1.0.0', (err, conn) => {
expect(err).to.not.exist
expect(conn.peerId).to.exist
expect(Object.keys(swarmA.muxedConns).length).to.equal(2)
conn.end()
@ -194,4 +212,11 @@ describe('high level API - with everything mixed all together!', function () {
conn.on('end', done)
})
})
it('close a muxer emits event', (done) => {
swarmC.close(() => {})
swarmA.once('peer-mux-closed', (peerInfo) => {
done()
})
})
})