From deb6b64db44e70d7ad06370501eda2da06f7bba8 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Thu, 17 Aug 2017 12:02:03 -0600 Subject: [PATCH 01/20] feat: adding circuit dialing --- src/connection.js | 14 ++++++++++++++ src/dial.js | 27 ++++++++++++++++++++++++--- src/limit-dialer/index.js | 2 ++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/connection.js b/src/connection.js index b813bd2..29cbbae 100644 --- a/src/connection.js +++ b/src/connection.js @@ -7,6 +7,8 @@ const debug = require('debug') const log = debug('libp2p:swarm:connection') const setImmediate = require('async/setImmediate') +const Circuit = require('libp2p-circuit') + const protocolMuxer = require('./protocol-muxer') const plaintext = require('./plaintext') @@ -92,6 +94,18 @@ module.exports = function connection (swarm) { }) }, + enableCircuitRelay (config) { + config = config || + { + circuit: { + enabled: false + } + } + + swarm.relay = true // flag to signal if relay has been enaled for dialing + swarm.transport.add(Circuit.tag, new Circuit(swarm, config)) + }, + crypto (tag, encrypt) { if (!tag && !encrypt) { tag = plaintext.tag diff --git a/src/dial.js b/src/dial.js index aeed091..2d65752 100644 --- a/src/dial.js +++ b/src/dial.js @@ -4,6 +4,8 @@ const multistream = require('multistream-select') const Connection = require('interface-connection').Connection const setImmediate = require('async/setImmediate') const getPeerInfo = require('./get-peer-info') +const Circuit = require('libp2p-circuit') + const debug = require('debug') const log = debug('libp2p:swarm:dial') @@ -88,11 +90,19 @@ function dial (swarm) { nextTransport(tKeys.shift()) function nextTransport (key) { + if (!key) { + return dialCircuit((err, circuit) => { + if (err) { + return cb(new Error('Could not dial in any of the transports or relays')) + } + + cb(null, circuit) + }) + } + + log(`dialing transport ${key}`) swarm.transport.dial(key, pi, (err, conn) => { if (err) { - if (tKeys.length === 0) { - return cb(new Error('Could not dial in any of the transports')) - } return nextTransport(tKeys.shift()) } @@ -121,6 +131,17 @@ function dial (swarm) { } } + function dialCircuit (cb) { + swarm.transport.dial(Circuit.tag, pi, (err, conn) => { + if (err) { + log(err) + return cb(err) + } + + cb(null, conn) + }) + } + function attemptMuxerUpgrade (conn, cb) { const muxers = Object.keys(swarm.muxers) if (muxers.length === 0) { diff --git a/src/limit-dialer/index.js b/src/limit-dialer/index.js index 9b0fb31..33f423f 100644 --- a/src/limit-dialer/index.js +++ b/src/limit-dialer/index.js @@ -2,6 +2,7 @@ const map = require('async/map') const debug = require('debug') +const once = require('once') const log = debug('libp2p:swarm:dialer') @@ -37,6 +38,7 @@ class LimitDialer { log('dialMany:start') // we use a token to track if we want to cancel following dials const token = { cancel: false } + callback = once(callback) // only call callback once map(addrs, (m, cb) => { this.dialSingle(peer, transport, m, token, cb) From c6089d21caf517091a37d4c5ddae441038632d24 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 28 Aug 2017 14:12:16 -0600 Subject: [PATCH 02/20] fix: revert circuit back to be part of the transports array --- package.json | 3 ++- src/dial.js | 27 +++------------------------ src/index.js | 4 ++++ 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 284e051..4fb7d63 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "debug": "^3.0.1", "interface-connection": "~0.3.2", "ip-address": "^5.8.8", + "libp2p-circuit": "0.0.3", "libp2p-identify": "~0.6.1", "lodash.includes": "^4.3.0", "multiaddr": "^3.0.1", @@ -88,4 +89,4 @@ "greenkeeper[bot] ", "ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ " ] -} \ No newline at end of file +} diff --git a/src/dial.js b/src/dial.js index 2d65752..aeed091 100644 --- a/src/dial.js +++ b/src/dial.js @@ -4,8 +4,6 @@ const multistream = require('multistream-select') const Connection = require('interface-connection').Connection const setImmediate = require('async/setImmediate') const getPeerInfo = require('./get-peer-info') -const Circuit = require('libp2p-circuit') - const debug = require('debug') const log = debug('libp2p:swarm:dial') @@ -90,19 +88,11 @@ function dial (swarm) { nextTransport(tKeys.shift()) function nextTransport (key) { - if (!key) { - return dialCircuit((err, circuit) => { - if (err) { - return cb(new Error('Could not dial in any of the transports or relays')) - } - - cb(null, circuit) - }) - } - - log(`dialing transport ${key}`) swarm.transport.dial(key, pi, (err, conn) => { if (err) { + if (tKeys.length === 0) { + return cb(new Error('Could not dial in any of the transports')) + } return nextTransport(tKeys.shift()) } @@ -131,17 +121,6 @@ function dial (swarm) { } } - function dialCircuit (cb) { - swarm.transport.dial(Circuit.tag, pi, (err, conn) => { - if (err) { - log(err) - return cb(err) - } - - cb(null, conn) - }) - } - function attemptMuxerUpgrade (conn, cb) { const muxers = Object.keys(swarm.muxers) if (muxers.length === 0) { diff --git a/src/index.js b/src/index.js index a6d5ea3..79cc1c1 100644 --- a/src/index.js +++ b/src/index.js @@ -65,6 +65,10 @@ function Swarm (peerInfo, peerBook) { // Only listen on transports we actually have addresses for return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0) + // push Circuit to be the last proto to be dialed + .sort((a) => { + return a.tag && a.tag === 'Circuit' ? -1 : 1 + }) } // higher level (public) API From d34a8aa7eb50fc9bbc26ada412e1b1206f5e1a62 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 29 Aug 2017 11:23:07 -0600 Subject: [PATCH 03/20] fix: correct sorting to push `Circuit` last --- src/index.js | 2 +- test/circuit.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/circuit.js diff --git a/src/index.js b/src/index.js index 79cc1c1..0d17a8e 100644 --- a/src/index.js +++ b/src/index.js @@ -67,7 +67,7 @@ function Swarm (peerInfo, peerBook) { return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0) // push Circuit to be the last proto to be dialed .sort((a) => { - return a.tag && a.tag === 'Circuit' ? -1 : 1 + return a === 'Circuit' ? -1 : 0 }) } diff --git a/test/circuit.js b/test/circuit.js new file mode 100644 index 0000000..8f1c8d5 --- /dev/null +++ b/test/circuit.js @@ -0,0 +1,21 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) + +describe(`circuit`, function () { + it(`should enable circuit`, function () { + + }) + + it(`should dial circuit last`, function () { + + }) + + it(`should not dial over circuit`, function () { + + }) +}) From 44c2656051c48389ed285895dddfb59280a6cadd Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 29 Aug 2017 11:58:35 -0600 Subject: [PATCH 04/20] test: adding circuit tests --- test/circuit.js | 91 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 3 deletions(-) diff --git a/test/circuit.js b/test/circuit.js index 8f1c8d5..8e653b6 100644 --- a/test/circuit.js +++ b/test/circuit.js @@ -6,16 +6,101 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) +const sinon = require('sinon') + +const parallel = require('async/parallel') +const TCP = require('libp2p-tcp') +const WS = require('libp2p-websockets') +const PeerBook = require('peer-book') + +const utils = require('./utils') +const Swarm = require('../src') + describe(`circuit`, function () { - it(`should enable circuit`, function () { + let swarmA // TCP + let peerA + let swarmB // WS + let peerB + let peerC // just a peer + let dialSpyA + + before((done) => { + utils.createInfos(3, (err, infos) => { + if (err) { + return done(err) + } + + peerA = infos[0] + peerB = infos[1] + peerC = infos[2] + + peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/9001') + peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9002/ws') + + swarmA = new Swarm(peerA, new PeerBook()) + swarmB = new Swarm(peerB, new PeerBook()) + + swarmA.transport.add('tcp', new TCP()) + swarmA.transport.add('WebSockets', new WS()) + + swarmB.transport.add('WebSockets', new WS()) + dialSpyA = sinon.spy(swarmA.transport, 'dial') + + done() + }) + }) + + after((done) => { + parallel([ + (cb) => swarmA.close(cb), + (cb) => swarmB.close(cb) + ], done) + }) + + it(`.enableCircuitRelay - should enable circuit transport`, function () { + swarmA.connection.enableCircuitRelay() + expect(Object.keys(swarmA.transports).length).to.equal(3) + + swarmB.connection.enableCircuitRelay() + expect(Object.keys(swarmB.transports).length).to.equal(2) }) - it(`should dial circuit last`, function () { + it(`should add to transport array`, function () { + expect(swarmA.transports['Circuit']).to.exist() + expect(swarmB.transports['Circuit']).to.exist() + }) + it(`should add /p2p-curcuit addrs on listen`, function (done) { + parallel([ + (cb) => swarmA.listen(cb), + (cb) => swarmB.listen(cb) + ], (err) => { + expect(err).to.not.exist() + expect(peerA.multiaddrs.toArray().filter((a) => a.toString().includes(`/p2p-circuit`)).length).to.be.eql(2) + expect(peerB.multiaddrs.toArray().filter((a) => a.toString().includes(`/p2p-circuit`)).length).to.be.eql(2) + done() + }) }) - it(`should not dial over circuit`, function () { + it(`should dial circuit last`, function (done) { + peerC.multiaddrs.clear() + peerC.multiaddrs.add(`/p2p-circuit/ipfs/ABCD`) + peerC.multiaddrs.add(`/ip4/127.0.0.1/tcp/9998/ipfs/ABCD`) + peerC.multiaddrs.add(`/ip4/127.0.0.1/tcp/9999/ws/ipfs/ABCD`) + swarmA.dial(peerC, (err, conn) => { + expect(err).to.exist() + expect(conn).to.not.exist() + expect(dialSpyA.lastCall.args[0]).to.be.eql('Circuit') + done() + }) + }) + it(`should not dial circuit if other transport succeed`, function (done) { + swarmA.dial(peerB, (err) => { + expect(err).not.to.exist() + expect(dialSpyA.lastCall.args[0]).to.not.be.eql('Circuit') + done() + }) }) }) From 04b8c52235e9f350fc9574a1298bf8c5d02bc417 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 29 Aug 2017 12:02:19 -0600 Subject: [PATCH 05/20] fix: remove needless flag --- src/connection.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/connection.js b/src/connection.js index 29cbbae..37c9e41 100644 --- a/src/connection.js +++ b/src/connection.js @@ -102,7 +102,6 @@ module.exports = function connection (swarm) { } } - swarm.relay = true // flag to signal if relay has been enaled for dialing swarm.transport.add(Circuit.tag, new Circuit(swarm, config)) }, From 563cbe376ce8417dfed4c9c854974bd51e8c7233 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 29 Aug 2017 12:14:55 -0600 Subject: [PATCH 06/20] test: adding sinon dev dependency --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 4fb7d63..ff60354 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "libp2p-websockets": "~0.10.1", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", + "sinon": "^3.2.1", "peer-book": "~0.5.1", "webrtcsupport": "^2.2.0" }, From d60c5bf938bbd6b9be6091adec0da2cb4c6f27dc Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 29 Aug 2017 17:17:51 -0600 Subject: [PATCH 07/20] feat: use object.assign for default conf vals --- src/connection.js | 12 +++++------- test/node.js | 1 + 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/connection.js b/src/connection.js index 37c9e41..b76c093 100644 --- a/src/connection.js +++ b/src/connection.js @@ -95,12 +95,10 @@ module.exports = function connection (swarm) { }, enableCircuitRelay (config) { - config = config || - { - circuit: { - enabled: false - } - } + config = config || {} + if (!config.circuit) { + Object.assign(config, { circuit: { enabled: false, active: false } }) + } swarm.transport.add(Circuit.tag, new Circuit(swarm, config)) }, @@ -119,7 +117,7 @@ module.exports = function connection (swarm) { protocolMuxer(swarm.protocols, secure) }) - swarm.crypto = {tag, encrypt} + swarm.crypto = { tag, encrypt } } } } diff --git a/test/node.js b/test/node.js index fd5a6c2..791eb08 100644 --- a/test/node.js +++ b/test/node.js @@ -10,3 +10,4 @@ require('./conn-upgrade-secio') require('./conn-upgrade-tls') require('./swarm-without-muxing') require('./swarm-with-muxing') +require('./circuit') From ff5683a261416a71b89d2b620d469a5018cca120 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 1 Sep 2017 16:42:42 -0600 Subject: [PATCH 08/20] feat: disable circuit by default --- src/connection.js | 14 +++++++++----- test/circuit.js | 8 ++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/connection.js b/src/connection.js index b76c093..10fe856 100644 --- a/src/connection.js +++ b/src/connection.js @@ -96,11 +96,15 @@ module.exports = function connection (swarm) { enableCircuitRelay (config) { config = config || {} - if (!config.circuit) { - Object.assign(config, { circuit: { enabled: false, active: false } }) - } - swarm.transport.add(Circuit.tag, new Circuit(swarm, config)) + if (config.enabled) { + if (!config.hop) { + Object.assign(config, { hop: { enabled: false, active: false } }) + } + + // TODO: should we enable circuit listener and dialer by default? + swarm.transport.add(Circuit.tag, new Circuit(swarm, config)) + } }, crypto (tag, encrypt) { @@ -117,7 +121,7 @@ module.exports = function connection (swarm) { protocolMuxer(swarm.protocols, secure) }) - swarm.crypto = { tag, encrypt } + swarm.crypto = {tag, encrypt} } } } diff --git a/test/circuit.js b/test/circuit.js index 8e653b6..e92d37d 100644 --- a/test/circuit.js +++ b/test/circuit.js @@ -59,10 +59,14 @@ describe(`circuit`, function () { }) it(`.enableCircuitRelay - should enable circuit transport`, function () { - swarmA.connection.enableCircuitRelay() + swarmA.connection.enableCircuitRelay({ + enabled: true + }) expect(Object.keys(swarmA.transports).length).to.equal(3) - swarmB.connection.enableCircuitRelay() + swarmB.connection.enableCircuitRelay({ + enabled: true + }) expect(Object.keys(swarmB.transports).length).to.equal(2) }) From 33a0426a56797fa4e1f573009707124f3d19a05f Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 8 Sep 2017 21:10:42 -0600 Subject: [PATCH 09/20] fix: dial on circuit last but fallback to circuit if no explicit circuit addr is provided --- src/dial.js | 33 ++++++++++++++++++++++++++------- src/transport.js | 2 +- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/dial.js b/src/dial.js index aeed091..d15468b 100644 --- a/src/dial.js +++ b/src/dial.js @@ -4,6 +4,8 @@ const multistream = require('multistream-select') const Connection = require('interface-connection').Connection const setImmediate = require('async/setImmediate') const getPeerInfo = require('./get-peer-info') +const Circuit = require('libp2p-circuit') + const debug = require('debug') const log = debug('libp2p:swarm:dial') @@ -81,18 +83,22 @@ function dial (swarm) { function attemptDial (pi, cb) { const tKeys = swarm.availableTransports(pi) - if (tKeys.length === 0) { - return cb(new Error('No available transport to dial to')) - } - nextTransport(tKeys.shift()) function nextTransport (key) { + if (!key) { + return dialCircuit((err, circuit) => { + if (err) { + return cb(new Error('Could not dial in any of the transports or relays')) + } + + cb(null, circuit) + }) + } + + log(`dialing transport ${key}`) swarm.transport.dial(key, pi, (err, conn) => { if (err) { - if (tKeys.length === 0) { - return cb(new Error('Could not dial in any of the transports')) - } return nextTransport(tKeys.shift()) } @@ -121,6 +127,19 @@ function dial (swarm) { } } + function dialCircuit (cb) { + log(`Falling back to dialing over circuit`) + pi.multiaddrs.add(`/p2p-circuit/ipfs/${pi.id.toB58String()}`) + swarm.transport.dial(Circuit.tag, pi, (err, conn) => { + if (err) { + log(err) + return cb(err) + } + + cb(null, conn) + }) + } + function attemptMuxerUpgrade (conn, cb) { const muxers = Object.keys(swarm.muxers) if (muxers.length === 0) { diff --git a/src/transport.js b/src/transport.js index 973a5b5..a4dc980 100644 --- a/src/transport.js +++ b/src/transport.js @@ -46,9 +46,9 @@ module.exports = function (swarm) { if (!Array.isArray(multiaddrs)) { multiaddrs = [multiaddrs] } - log('dialing %s', key, multiaddrs.map((m) => m.toString())) // filter the multiaddrs that are actually valid for this transport (use a func from the transport itself) (maybe even make the transport do that) multiaddrs = dialables(t, multiaddrs) + log('dialing %s', key, multiaddrs.map((m) => m.toString())) dialer.dialMany(pi.id, t, multiaddrs, (err, success) => { if (err) { From f0e7326d304e45c8c11a72c647ce7ce2eb765bf0 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 18 Sep 2017 11:54:11 -0600 Subject: [PATCH 10/20] chore: make deps more deterministic --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ff60354..65af764 100644 --- a/package.json +++ b/package.json @@ -69,8 +69,8 @@ "multiaddr": "^3.0.1", "multistream-select": "~0.13.5", "once": "^1.4.0", - "peer-id": "~0.10.1", - "peer-info": "~0.11.0", + "peer-id": "^0.10.1", + "peer-info": "^0.11.0", "pull-stream": "^3.6.1" }, "contributors": [ From 38b28219afc5439ae2ff92a4a2b6fb227d89d6a3 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sun, 10 Sep 2017 05:01:22 +0100 Subject: [PATCH 11/20] chore: update deps --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 65af764..05ae15a 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "libp2p-websockets": "~0.10.1", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", + "peer-book": "~0.5.1", "sinon": "^3.2.1", "peer-book": "~0.5.1", "webrtcsupport": "^2.2.0" From 062c054d6586dc4daa2223f628ffecc07d3677cb Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 17 Oct 2017 13:42:02 -0700 Subject: [PATCH 12/20] test: upgrading to new aegir --- .aegir.js | 99 +++++++++++++++++++ package.json | 30 +++--- ...owser-swarm-with-muxing-plus-websockets.js | 2 +- test/browser-transport-websockets.js | 2 +- test/browser.js | 2 +- test/instance.js | 2 +- test/swarm-with-muxing.js | 2 +- test/transport-tcp.js | 2 +- test/transport-websockets.js | 8 +- 9 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 .aegir.js diff --git a/.aegir.js b/.aegir.js new file mode 100644 index 0000000..1e8bcfe --- /dev/null +++ b/.aegir.js @@ -0,0 +1,99 @@ +'use strict' + +const PeerInfo = require('peer-info') +const PeerId = require('peer-id') +const WebSockets = require('libp2p-websockets') +const pull = require('pull-stream') +const PeerBook = require('peer-book') + +const Swarm = require('./src') +const spdy = require('libp2p-spdy') +const fs = require('fs') +const path = require('path') + +const sigServer = require('libp2p-webrtc-star/src/sig-server') + +let swarmA +let swarmB +let sigS + +const options = { + port: 15555, + host: '127.0.0.1' +} + +function before (done) { + function createListenerA (cb) { + PeerId.createFromJSON( + JSON.parse( + fs.readFileSync(path.join(__dirname, './test/test-data/id-1.json')) + ), + (err, id) => { + if (err) { + return cb(err) + } + + const peerA = new PeerInfo(id) + const maA = '/ip4/127.0.0.1/tcp/9100/ws' + + peerA.multiaddrs.add(maA) + swarmA = new Swarm(peerA, new PeerBook()) + + swarmA.transport.add('ws', new WebSockets()) + swarmA.transport.listen('ws', {}, echo, cb) + }) + } + + function createListenerB (cb) { + PeerId.createFromJSON( + JSON.parse( + fs.readFileSync(path.join(__dirname, './test/test-data/id-2.json')) + ), + (err, id) => { + if (err) { + return cb(err) + } + + const peerB = new PeerInfo(id) + const maB = '/ip4/127.0.0.1/tcp/9200/ws' + + peerB.multiaddrs.add(maB) + swarmB = new Swarm(peerB, new PeerBook()) + + swarmB.transport.add('ws', new WebSockets()) + swarmB.connection.addStreamMuxer(spdy) + swarmB.connection.reuse() + swarmB.listen(cb) + swarmB.handle('/echo/1.0.0', echo) + }) + } + + let count = 0 + const ready = () => ++count === 3 ? done() : null + + createListenerA(ready) + createListenerB(ready) + sigS = sigServer.start(options, ready) + + function echo (protocol, conn) { + pull(conn, conn) + } +} + +function after (done) { + let count = 0 + const ready = () => ++count === 3 ? done() : null + + swarmA.transport.close('ws', ready) + swarmB.close(ready) + sigS.stop(ready) +} + +module.exports = { + hooks: { + browser: { + pre: before, + post: after + } + } +} diff --git a/package.json b/package.json index 05ae15a..932ca8c 100644 --- a/package.json +++ b/package.json @@ -4,19 +4,19 @@ "description": "libp2p swarm implementation in JavaScript", "main": "src/index.js", "scripts": { - "lint": "gulp lint", - "build": "gulp build", - "test": "gulp test --dom", - "test:node": "gulp test:node", - "test:browser": "gulp test:browser --dom", - "release": "gulp release --dom", - "release-minor": "gulp release --type minor --dom", - "release-major": "gulp release --type major --dom", - "coverage": "gulp coverage", - "coverage-publish": "aegir-coverage publish" + "lint": "aegir lint", + "build": "aegir build", + "test": "aegir test --target node --target browser --no-parallel", + "test:node": "aegir test --target node --no-parallel", + "test:browser": "aegir test --target browser --no-parallel", + "release": "aegir test release --target node --target browser --no-parallel", + "release-minor": "aegir release --type minor --target node --target browser --no-parallel", + "release-major": "aegir release --type major --target node --target browser --no-parallel", + "coverage": "aegir coverage", + "coverage-publish": "aegir coverage --provider coveralls" }, "browser": { - "zlib": "browserify-zlib-next" + "zlib": "browserify-zlib" }, "repository": { "type": "git", @@ -40,7 +40,7 @@ "npm": ">=3.0.0" }, "devDependencies": { - "aegir": "^11.0.2", + "aegir": "^12.0.8", "buffer-loader": "0.0.1", "chai": "^4.1.2", "dirty-chai": "^2.0.1", @@ -51,20 +51,20 @@ "libp2p-tcp": "~0.11.0", "libp2p-webrtc-star": "~0.13.2", "libp2p-websockets": "~0.10.1", + "peer-book": "~0.5.1", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", - "peer-book": "~0.5.1", "sinon": "^3.2.1", "peer-book": "~0.5.1", "webrtcsupport": "^2.2.0" }, "dependencies": { "async": "^2.5.0", - "browserify-zlib-next": "^1.0.1", + "browserify-zlib": "^0.2.0", "debug": "^3.0.1", "interface-connection": "~0.3.2", "ip-address": "^5.8.8", - "libp2p-circuit": "0.0.3", + "libp2p-circuit": "1.0.0", "libp2p-identify": "~0.6.1", "lodash.includes": "^4.3.0", "multiaddr": "^3.0.1", diff --git a/test/browser-swarm-with-muxing-plus-websockets.js b/test/browser-swarm-with-muxing-plus-websockets.js index eb89248..6c254f3 100644 --- a/test/browser-swarm-with-muxing-plus-websockets.js +++ b/test/browser-swarm-with-muxing-plus-websockets.js @@ -58,7 +58,7 @@ describe('high level API (swarm with spdy + websockets)', () => { swarm.dial(peerDst, '/echo/1.0.0', (err, conn) => { expect(err).to.not.exist() pull( - pull.values([Buffer('hello')]), + pull.values([Buffer.from('hello')]), conn, pull.onEnd(done) ) diff --git a/test/browser-transport-websockets.js b/test/browser-transport-websockets.js index c7cbf13..a6c998f 100644 --- a/test/browser-transport-websockets.js +++ b/test/browser-transport-websockets.js @@ -50,7 +50,7 @@ describe('transport - websockets', () => { }) pull( - pull.values([Buffer('hey')]), + pull.values([Buffer.from('hey')]), conn, pull.collect((err, data) => { expect(err).to.not.exist() diff --git a/test/browser.js b/test/browser.js index 852cde7..ceb5c13 100644 --- a/test/browser.js +++ b/test/browser.js @@ -11,7 +11,7 @@ const Swarm = require('../src') describe('basics', () => { it('throws on missing peerInfo', (done) => { - expect(Swarm).to.throw(Error) + expect(Swarm).to.throw() done() }) }) diff --git a/test/instance.js b/test/instance.js index f6f7158..cab416a 100644 --- a/test/instance.js +++ b/test/instance.js @@ -10,6 +10,6 @@ const Swarm = require('../src') describe('create Swarm instance', () => { it('throws on missing peerInfo', () => { - expect(() => Swarm()).to.throw(Error) + expect(() => Swarm()).to.throw(/You must provide a `peerInfo`/) }) }) diff --git a/test/swarm-with-muxing.js b/test/swarm-with-muxing.js index 0d2447f..f00ad72 100644 --- a/test/swarm-with-muxing.js +++ b/test/swarm-with-muxing.js @@ -268,5 +268,5 @@ describe('high level API - with everything mixed all together!', () => { (cb) => swarmC.close(cb), (cb) => swarmA.once('peer-mux-closed', (peerInfo) => cb()) ], done) - }).timeout(2500) + }) }) diff --git a/test/transport-tcp.js b/test/transport-tcp.js index 339e8f0..2e89cfb 100644 --- a/test/transport-tcp.js +++ b/test/transport-tcp.js @@ -127,7 +127,7 @@ describe('transport - tcp', () => { (cb) => swarmA.transport.close('tcp', cb), (cb) => swarmB.transport.close('tcp', cb) ], done) - }).timeout(2500) + }) it('support port 0', (done) => { const ma = '/ip4/127.0.0.1/tcp/0' diff --git a/test/transport-websockets.js b/test/transport-websockets.js index 30ef42d..9570ff5 100644 --- a/test/transport-websockets.js +++ b/test/transport-websockets.js @@ -70,10 +70,10 @@ describe('transport - websockets', function () { }) const s = goodbye({ - source: pull.values([Buffer('hey')]), + source: pull.values([Buffer.from('hey')]), sink: pull.collect((err, data) => { expect(err).to.not.exist() - expect(data).to.be.eql([Buffer('hey')]) + expect(data).to.be.eql([Buffer.from('hey')]) done() }) }) @@ -87,10 +87,10 @@ describe('transport - websockets', function () { expect(err).to.not.exist() const s = goodbye({ - source: pull.values([Buffer('hey')]), + source: pull.values([Buffer.from('hey')]), sink: pull.collect((err, data) => { expect(err).to.not.exist() - expect(data).to.be.eql([Buffer('hey')]) + expect(data).to.be.eql([Buffer.from('hey')]) done() }) }) From c96198d36966d00da73884faf03abf13b634b9b5 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 17 Oct 2017 13:49:26 -0700 Subject: [PATCH 13/20] chore: updating ci files --- .travis.yml | 2 +- circle.yml | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 584f308..341df5c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,4 +29,4 @@ addons: sources: - ubuntu-toolchain-r-test packages: - - g++-4.8 + - g++-4.8 \ No newline at end of file diff --git a/circle.yml b/circle.yml index 56f7efb..4e1698a 100644 --- a/circle.yml +++ b/circle.yml @@ -6,9 +6,13 @@ dependencies: pre: - google-chrome --version - curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb + - for v in $(curl http://archive.ubuntu.com/ubuntu/pool/main/n/nss/ | grep "href=" | grep "libnss3.*deb\"" -o | grep -o "libnss3.*deb" | grep "3.28" | grep "14.04"); do curl -L -o $v http://archive.ubuntu.com/ubuntu/pool/main/n/nss/$v; done && rm libnss3-tools*_i386.deb libnss3-dev*_i386.deb - sudo dpkg -i google-chrome.deb || true + - sudo dpkg -i libnss3*.deb || true - sudo apt-get update + - sudo apt-get install -f || true + - sudo dpkg -i libnss3*.deb - sudo apt-get install -f - sudo apt-get install --only-upgrade lsb-base - sudo dpkg -i google-chrome.deb - - google-chrome --version + - google-chrome --version \ No newline at end of file From a480f37c190891434bf5be9da86a5cb4debc111c Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 17 Oct 2017 14:02:24 -0700 Subject: [PATCH 14/20] test: fixing incorrect timeout function invocation --- test/browser.js | 2 +- test/swarm-with-muxing.js | 4 +++- test/transport-tcp.js | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/test/browser.js b/test/browser.js index ceb5c13..f8735d5 100644 --- a/test/browser.js +++ b/test/browser.js @@ -11,7 +11,7 @@ const Swarm = require('../src') describe('basics', () => { it('throws on missing peerInfo', (done) => { - expect(Swarm).to.throw() + expect(Swarm).to.throw(/You must provide a `peerInfo`/) done() }) }) diff --git a/test/swarm-with-muxing.js b/test/swarm-with-muxing.js index f00ad72..ab9528f 100644 --- a/test/swarm-with-muxing.js +++ b/test/swarm-with-muxing.js @@ -210,6 +210,7 @@ describe('high level API - with everything mixed all together!', () => { done() } } + swarmC.handle('/mamao/1.0.0', (protocol, conn) => { conn.getPeerInfo((err, peerInfo) => { expect(err).to.not.exist() @@ -263,7 +264,8 @@ describe('high level API - with everything mixed all together!', () => { }) }) - it('close a muxer emits event', (done) => { + it('close a muxer emits event', function (done) { + this.timeout(2500) parallel([ (cb) => swarmC.close(cb), (cb) => swarmA.once('peer-mux-closed', (peerInfo) => cb()) diff --git a/test/transport-tcp.js b/test/transport-tcp.js index 2e89cfb..593974b 100644 --- a/test/transport-tcp.js +++ b/test/transport-tcp.js @@ -122,7 +122,8 @@ describe('transport - tcp', () => { }) }) - it('.close', (done) => { + it('.close', function (done) { + this.timeout(2500) parallel([ (cb) => swarmA.transport.close('tcp', cb), (cb) => swarmB.transport.close('tcp', cb) From 5b3274bdf077f3802cc10cf345e3b7ba64707ff5 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 17 Oct 2017 14:10:01 -0700 Subject: [PATCH 15/20] chore: fixing package.json --- gulpfile.js | 93 ---------------------------------------------------- package.json | 1 - 2 files changed, 94 deletions(-) delete mode 100644 gulpfile.js diff --git a/gulpfile.js b/gulpfile.js deleted file mode 100644 index 3baabce..0000000 --- a/gulpfile.js +++ /dev/null @@ -1,93 +0,0 @@ -'use strict' - -const gulp = require('gulp') -const PeerInfo = require('peer-info') -const PeerId = require('peer-id') -const WebSockets = require('libp2p-websockets') -const pull = require('pull-stream') -const PeerBook = require('peer-book') - -const Swarm = require('./src') -const spdy = require('libp2p-spdy') -const fs = require('fs') -const path = require('path') - -const sigServer = require('libp2p-webrtc-star/src/sig-server') - -let swarmA -let swarmB -let sigS - -const options = { - port: 15555, - host: '127.0.0.1' -} - -gulp.task('test:browser:before', (done) => { - function createListenerA (cb) { - PeerId.createFromJSON( - JSON.parse( - fs.readFileSync(path.join(__dirname, './test/test-data/id-1.json')) - ), - (err, id) => { - if (err) { - return cb(err) - } - - const peerA = new PeerInfo(id) - const maA = '/ip4/127.0.0.1/tcp/9100/ws' - - peerA.multiaddrs.add(maA) - swarmA = new Swarm(peerA, new PeerBook()) - - swarmA.transport.add('ws', new WebSockets()) - swarmA.transport.listen('ws', {}, echo, cb) - }) - } - - function createListenerB (cb) { - PeerId.createFromJSON( - JSON.parse( - fs.readFileSync(path.join(__dirname, './test/test-data/id-2.json')) - ), - (err, id) => { - if (err) { - return cb(err) - } - - const peerB = new PeerInfo(id) - const maB = '/ip4/127.0.0.1/tcp/9200/ws' - - peerB.multiaddrs.add(maB) - swarmB = new Swarm(peerB, new PeerBook()) - - swarmB.transport.add('ws', new WebSockets()) - swarmB.connection.addStreamMuxer(spdy) - swarmB.connection.reuse() - swarmB.listen(cb) - swarmB.handle('/echo/1.0.0', echo) - }) - } - - let count = 0 - const ready = () => ++count === 3 ? done() : null - - createListenerA(ready) - createListenerB(ready) - sigS = sigServer.start(options, ready) - - function echo (protocol, conn) { - pull(conn, conn) - } -}) - -gulp.task('test:browser:after', (done) => { - let count = 0 - const ready = () => ++count === 3 ? done() : null - - swarmA.transport.close('ws', ready) - swarmB.close(ready) - sigS.stop(ready) -}) - -require('aegir/gulp')(gulp) diff --git a/package.json b/package.json index 932ca8c..4cca7ee 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", "sinon": "^3.2.1", - "peer-book": "~0.5.1", "webrtcsupport": "^2.2.0" }, "dependencies": { From 6dcfff9c06ab008dadf894b4d4430e832f9275c4 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 21 Oct 2017 08:45:05 +0100 Subject: [PATCH 16/20] chore --- .gitignore | 3 +++ package.json | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index e4c5b0e..fca3404 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ + **/node_modules/ **/*.log test/repo-tests* @@ -38,5 +39,7 @@ test/test-data/go-ipfs-repo/LOCK test/test-data/go-ipfs-repo/LOG test/test-data/go-ipfs-repo/LOG.old +docs # while testing npm5 package-lock.json +yarn.lock diff --git a/package.json b/package.json index 4cca7ee..d1c958d 100644 --- a/package.json +++ b/package.json @@ -48,19 +48,19 @@ "libp2p-multiplex": "~0.5.0", "libp2p-secio": "~0.8.1", "libp2p-spdy": "~0.11.0", - "libp2p-tcp": "~0.11.0", + "libp2p-tcp": "~0.11.1", "libp2p-webrtc-star": "~0.13.2", - "libp2p-websockets": "~0.10.1", + "libp2p-websockets": "~0.10.2", "peer-book": "~0.5.1", "pre-commit": "^1.2.2", "pull-goodbye": "0.0.2", - "sinon": "^3.2.1", + "sinon": "^4.0.1", "webrtcsupport": "^2.2.0" }, "dependencies": { "async": "^2.5.0", "browserify-zlib": "^0.2.0", - "debug": "^3.0.1", + "debug": "^3.1.0", "interface-connection": "~0.3.2", "ip-address": "^5.8.8", "libp2p-circuit": "1.0.0", @@ -69,7 +69,7 @@ "multiaddr": "^3.0.1", "multistream-select": "~0.13.5", "once": "^1.4.0", - "peer-id": "^0.10.1", + "peer-id": "^0.10.2", "peer-info": "^0.11.0", "pull-stream": "^3.6.1" }, From fc4af6fed954c12aa2e98c8de490f939b918b813 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 21 Oct 2017 08:52:08 +0100 Subject: [PATCH 17/20] chore --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d1c958d..ce7b788 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "debug": "^3.1.0", "interface-connection": "~0.3.2", "ip-address": "^5.8.8", - "libp2p-circuit": "1.0.0", + "libp2p-circuit": "~0.1.0", "libp2p-identify": "~0.6.1", "lodash.includes": "^4.3.0", "multiaddr": "^3.0.1", From ed97a84cfb73d8151ace56b7bc495b726ef19810 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 22 Oct 2017 17:18:07 -0700 Subject: [PATCH 18/20] test: skip spdy tests and adjust timeouts --- package.json | 2 +- test/browser-swarm-with-muxing-plus-websockets.js | 2 +- test/conn-upgrade-secio.js | 3 ++- test/muxing-spdy.js | 4 +++- test/swarm-with-muxing.js | 3 ++- test/transport-tcp.js | 3 ++- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index ce7b788..a751fea 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "npm": ">=3.0.0" }, "devDependencies": { - "aegir": "^12.0.8", + "aegir": "^12.1.0", "buffer-loader": "0.0.1", "chai": "^4.1.2", "dirty-chai": "^2.0.1", diff --git a/test/browser-swarm-with-muxing-plus-websockets.js b/test/browser-swarm-with-muxing-plus-websockets.js index 6c254f3..3600041 100644 --- a/test/browser-swarm-with-muxing-plus-websockets.js +++ b/test/browser-swarm-with-muxing-plus-websockets.js @@ -15,7 +15,7 @@ const PeerBook = require('peer-book') const Swarm = require('../src') -describe('high level API (swarm with spdy + websockets)', () => { +describe.skip('high level API (swarm with spdy + websockets)', () => { let swarm let peerDst diff --git a/test/conn-upgrade-secio.js b/test/conn-upgrade-secio.js index e8bf59f..e2ce798 100644 --- a/test/conn-upgrade-secio.js +++ b/test/conn-upgrade-secio.js @@ -58,7 +58,8 @@ describe('secio conn upgrade (on TCP)', () => { }) }) - after((done) => { + after(function (done) { + this.timeout(3000) parallel([ (cb) => swarmA.close(cb), (cb) => swarmB.close(cb), diff --git a/test/muxing-spdy.js b/test/muxing-spdy.js index 0e412fc..ec3cd86 100644 --- a/test/muxing-spdy.js +++ b/test/muxing-spdy.js @@ -17,7 +17,9 @@ const PeerBook = require('peer-book') const utils = require('./utils') const Swarm = require('../src') -describe('stream muxing with spdy (on TCP)', () => { +describe('stream muxing with spdy (on TCP)', function () { + this.timeout(5000) + let swarmA let peerA let swarmB diff --git a/test/swarm-with-muxing.js b/test/swarm-with-muxing.js index ab9528f..56122dd 100644 --- a/test/swarm-with-muxing.js +++ b/test/swarm-with-muxing.js @@ -50,7 +50,8 @@ describe('high level API - with everything mixed all together!', () => { }) }) - after((done) => { + after(function (done) { + this.timeout(3000) parallel([ (cb) => swarmA.close(cb), (cb) => swarmB.close(cb), diff --git a/test/transport-tcp.js b/test/transport-tcp.js index 593974b..fd48685 100644 --- a/test/transport-tcp.js +++ b/test/transport-tcp.js @@ -39,7 +39,8 @@ describe('transport - tcp', () => { }) let peer - beforeEach((done) => { + beforeEach(function (done) { + this.timeout(10000) // hook fails with timeout for a number of tests Peer.create((err, info) => { if (err) { return done(err) From c29c11f9d34e6949e189d8333eca549651d5c1ba Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 22 Oct 2017 18:25:28 -0700 Subject: [PATCH 19/20] docs: adding circuit relay docs --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index ed058a5..b81d4ce 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,16 @@ const secio = require('libp2p-secio') swarm.connection.crypto(secio.tag, secio.encrypt) ``` +##### `swarm.connection.enableCircuitRelay(options)` + +Enable circuit relaying. + +- `options` + - enabled - activates relay dialing and listening functionality + - hop - an object with two properties + - enabled - enables circuit relaying + - active - is it an active or passive relay (default false) + ### `swarm.dial(peer, protocol, callback)` dial uses the best transport (whatever works first, in the future we can have some criteria), and jump starts the connection until the point where we have to negotiate the protocol. If a muxer is available, then drop the muxer onto that connection. Good to warm up connections or to check for connectivity. If we have already a muxer for that peerInfo, then do nothing. From 1654f85bcf59a5ae75d5e142d5ab873df75b3250 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 22 Oct 2017 20:05:14 -0700 Subject: [PATCH 20/20] test: adjusting timeouts --- test/transport-tcp.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/transport-tcp.js b/test/transport-tcp.js index fd48685..d0ae515 100644 --- a/test/transport-tcp.js +++ b/test/transport-tcp.js @@ -40,7 +40,7 @@ describe('transport - tcp', () => { let peer beforeEach(function (done) { - this.timeout(10000) // hook fails with timeout for a number of tests + this.timeout(20000) // hook fails with timeout for a number of tests Peer.create((err, info) => { if (err) { return done(err) @@ -180,7 +180,9 @@ describe('transport - tcp', () => { } }) - it('listen in several addrs', (done) => { + it('listen in several addrs', function (done) { + this.timeout(12000) + let swarm peer.multiaddrs.add('/ip4/127.0.0.1/tcp/9001') peer.multiaddrs.add('/ip4/127.0.0.1/tcp/9002')