mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-08 13:21:34 +00:00
Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
ae371085c1 | |||
770bee3c66 | |||
6943e3e90b | |||
a008ebd5b9 | |||
20108d2de8 | |||
15fcfb737c | |||
0fa14c9608 | |||
ac7c8a150e | |||
851c8ee2a3 | |||
7a3f9d08d5 | |||
52d60a7391 | |||
165068d05c | |||
9baae15dcf | |||
b87524f36a |
@ -11,6 +11,7 @@ before_install:
|
|||||||
script:
|
script:
|
||||||
- npm run lint
|
- npm run lint
|
||||||
- npm test
|
- npm test
|
||||||
|
- npm run coverage
|
||||||
|
|
||||||
addons:
|
addons:
|
||||||
firefox: 'latest'
|
firefox: 'latest'
|
||||||
@ -18,3 +19,6 @@ addons:
|
|||||||
before_script:
|
before_script:
|
||||||
- export DISPLAY=:99.0
|
- export DISPLAY=:99.0
|
||||||
- sh -e /etc/init.d/xvfb start
|
- sh -e /etc/init.d/xvfb start
|
||||||
|
|
||||||
|
after_success:
|
||||||
|
- npm run coverage-publish
|
||||||
|
10
README.md
10
README.md
@ -1,11 +1,11 @@
|
|||||||
libp2p-swarm JavaScript implementation
|
libp2p-swarm JavaScript implementation
|
||||||
======================================
|
======================================
|
||||||
|
|
||||||
[](http://ipn.io)
|
[](http://ipn.io)
|
||||||
[](http://ipfs.io/)
|
[](http://ipfs.io/)
|
||||||
[](http://webchat.freenode.net/?channels=%23ipfs)
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||
[](https://travis-ci.org/diasdavid/js-libp2p-swarm)
|
[](https://travis-ci.org/diasdavid/js-libp2p-swarm)
|
||||||

|
[](https://coveralls.io/github/diasdavid/js-libp2p-swarm?branch=master)
|
||||||
[](https://david-dm.org/ipfs/js-libp2p-swarm)
|
[](https://david-dm.org/ipfs/js-libp2p-swarm)
|
||||||
[](https://github.com/feross/standard)
|
[](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
|
# Usage
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
libp2p-swarm is available on npm and so, like any other npm module, just:
|
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).
|
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
|
- `key` - the transport identifier
|
||||||
- `transport` -
|
- `transport` -
|
||||||
- `options`
|
- `options`
|
||||||
- `callback`
|
- `callback`
|
||||||
|
|
||||||
|
12
circle.yml
Normal file
12
circle.yml
Normal 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
|
38
package.json
38
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-swarm",
|
"name": "libp2p-swarm",
|
||||||
"version": "0.10.4",
|
"version": "0.10.7",
|
||||||
"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",
|
||||||
@ -13,7 +13,8 @@
|
|||||||
"release": "gulp release",
|
"release": "gulp release",
|
||||||
"release-minor": "gulp release --minor",
|
"release-minor": "gulp release --minor",
|
||||||
"release-major": "gulp release --major",
|
"release-major": "gulp release --major",
|
||||||
"coverage": "gulp coverage"
|
"coverage": "gulp coverage",
|
||||||
|
"coverage-publish": "aegir-coverage publish"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -39,25 +40,26 @@
|
|||||||
"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.0.3",
|
"aegir": "^2.1.1",
|
||||||
"gulp": "^3.9.1",
|
"gulp": "^3.9.1",
|
||||||
"istanbul": "^0.4.2",
|
"istanbul": "^0.4.3",
|
||||||
"libp2p-multiplex": "^0.2.1",
|
"libp2p-multiplex": "^0.2.1",
|
||||||
"libp2p-spdy": "^0.2.3",
|
"libp2p-spdy": "^0.3.1",
|
||||||
"libp2p-tcp": "^0.4.0",
|
"libp2p-tcp": "^0.4.0",
|
||||||
"libp2p-websockets": "^0.3.1",
|
"libp2p-websockets": "^0.3.2",
|
||||||
"multiaddr": "^1.3.0",
|
"multiaddr": "^1.4.0",
|
||||||
"peer-id": "^0.6.0",
|
"peer-id": "^0.6.6",
|
||||||
"peer-info": "^0.6.0",
|
"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"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babel-runtime": "^6.6.1",
|
"babel-runtime": "^6.6.1",
|
||||||
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
|
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
|
||||||
"ip-address": "^5.0.2",
|
"ip-address": "^5.8.0",
|
||||||
"multistream-select": "^0.6.1",
|
"lodash.contains": "^2.4.3",
|
||||||
"protocol-buffers-stream": "^1.2.0"
|
"multistream-select": "^0.6.5",
|
||||||
|
"protocol-buffers-stream": "^1.3.1"
|
||||||
},
|
},
|
||||||
"aegir": {
|
"aegir": {
|
||||||
"webpack": {
|
"webpack": {
|
||||||
@ -67,5 +69,13 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
"contributors": [
|
||||||
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
"David Dias <mail@daviddias.me>",
|
||||||
|
"Francisco Baio Dias <xicombd@gmail.com>",
|
||||||
|
"Pau Ramon Revilla <masylum@gmail.com>",
|
||||||
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
|
"dignifiedquire <dignifiedquire@gmail.com>"
|
||||||
|
]
|
||||||
|
}
|
@ -14,11 +14,7 @@ const Info = require('peer-info')
|
|||||||
const Id = require('peer-id')
|
const Id = require('peer-id')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
const isNode = !global.window
|
const identity = fs.readFileSync(path.join(__dirname, 'identify.proto'))
|
||||||
|
|
||||||
const identity = isNode
|
|
||||||
? fs.readFileSync(path.join(__dirname, 'identify.proto'))
|
|
||||||
: require('buffer!./identify.proto')
|
|
||||||
|
|
||||||
const pbStream = require('protocol-buffers-stream')(identity)
|
const pbStream = require('protocol-buffers-stream')(identity)
|
||||||
|
|
||||||
@ -45,7 +41,7 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
|
|||||||
|
|
||||||
pbs.on('identify', (msg) => {
|
pbs.on('identify', (msg) => {
|
||||||
if (msg.observedAddr.length > 0) {
|
if (msg.observedAddr.length > 0) {
|
||||||
peerInfo.multiaddr.addSafe(msg.observedAddr)
|
peerInfo.multiaddr.addSafe(multiaddr(msg.observedAddr))
|
||||||
}
|
}
|
||||||
|
|
||||||
const peerId = Id.createFromPubKey(msg.publicKey)
|
const peerId = Id.createFromPubKey(msg.publicKey)
|
||||||
@ -63,7 +59,7 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
|
|||||||
protocolVersion: 'na',
|
protocolVersion: 'na',
|
||||||
agentVersion: 'na',
|
agentVersion: 'na',
|
||||||
publicKey: peerInfo.id.pubKey,
|
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('')
|
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -74,15 +70,15 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.handler = (peerInfo, swarm) => {
|
exports.handler = (peerInfo, swarm) => {
|
||||||
return function (conn) {
|
return (conn) => {
|
||||||
// 1. receive incoming observed info about me
|
// 1. receive incoming observed info about me
|
||||||
// 2. update my own information (on peerInfo)
|
// 2. update my own information (on peerInfo)
|
||||||
// 3. send back what I see from the other (get from swarm.muxedConns[incPeerID].conn.getObservedAddrs()
|
// 3. send back what I see from the other (get from swarm.muxedConns[incPeerID].conn.getObservedAddrs()
|
||||||
var pbs = pbStream()
|
var pbs = pbStream()
|
||||||
|
|
||||||
pbs.on('identify', function (msg) {
|
pbs.on('identify', (msg) => {
|
||||||
if (msg.observedAddr.length > 0) {
|
if (msg.observedAddr.length > 0) {
|
||||||
peerInfo.multiaddr.addSafe(msg.observedAddr)
|
peerInfo.multiaddr.addSafe(multiaddr(msg.observedAddr))
|
||||||
}
|
}
|
||||||
|
|
||||||
const peerId = Id.createFromPubKey(msg.publicKey)
|
const peerId = Id.createFromPubKey(msg.publicKey)
|
||||||
@ -93,9 +89,7 @@ exports.handler = (peerInfo, swarm) => {
|
|||||||
protocolVersion: 'na',
|
protocolVersion: 'na',
|
||||||
agentVersion: 'na',
|
agentVersion: 'na',
|
||||||
publicKey: peerInfo.id.pubKey,
|
publicKey: peerInfo.id.pubKey,
|
||||||
listenAddrs: peerInfo.multiaddrs.map(function (ma) {
|
listenAddrs: peerInfo.multiaddrs.map((ma) => ma.buffer),
|
||||||
return ma.buffer
|
|
||||||
}),
|
|
||||||
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
|
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
|
||||||
})
|
})
|
||||||
pbs.finalize()
|
pbs.finalize()
|
||||||
|
12
src/index.js
12
src/index.js
@ -3,6 +3,7 @@
|
|||||||
const multistream = require('multistream-select')
|
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')
|
||||||
|
|
||||||
exports = module.exports = Swarm
|
exports = module.exports = Swarm
|
||||||
|
|
||||||
@ -87,7 +88,16 @@ function Swarm (peerInfo) {
|
|||||||
// if no callback is passed, we pass conns to connHandler
|
// if no callback is passed, we pass conns to connHandler
|
||||||
if (!handler) { handler = 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) => {
|
this.transports[key].createListener(multiaddrs, handler, (err, maUpdate) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -46,9 +46,17 @@ describe('transport - tcp', function () {
|
|||||||
function ready () {
|
function ready () {
|
||||||
if (++count === 2) {
|
if (++count === 2) {
|
||||||
expect(peerA.multiaddrs.length).to.equal(1)
|
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.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()
|
done()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,7 +132,11 @@ describe('transport - tcp', function () {
|
|||||||
|
|
||||||
function ready () {
|
function ready () {
|
||||||
expect(peer.multiaddrs.length).to.equal(1)
|
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)
|
swarm.close(done)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -46,9 +46,17 @@ describe('transport - websockets', function () {
|
|||||||
function ready () {
|
function ready () {
|
||||||
if (++count === 2) {
|
if (++count === 2) {
|
||||||
expect(peerA.multiaddrs.length).to.equal(1)
|
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.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()
|
done()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function (
|
|||||||
peerB = new Peer()
|
peerB = new Peer()
|
||||||
|
|
||||||
peerA.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9001'))
|
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)
|
swarmA = new Swarm(peerA)
|
||||||
swarmB = new Swarm(peerB)
|
swarmB = new Swarm(peerB)
|
||||||
|
@ -11,7 +11,7 @@ const WebSockets = require('libp2p-websockets')
|
|||||||
const spdy = require('libp2p-spdy')
|
const spdy = require('libp2p-spdy')
|
||||||
|
|
||||||
describe('high level API - with everything mixed all together!', function () {
|
describe('high level API - with everything mixed all together!', function () {
|
||||||
this.timeout(20000)
|
this.timeout(100000)
|
||||||
|
|
||||||
var swarmA // tcp
|
var swarmA // tcp
|
||||||
var peerA
|
var peerA
|
||||||
|
Reference in New Issue
Block a user