diff --git a/.aegir.js b/.aegir.js new file mode 100644 index 0000000000..3a7b02de97 --- /dev/null +++ b/.aegir.js @@ -0,0 +1,71 @@ +'use strict' + +const pull = require('pull-stream') +const parallel = require('async/parallel') +const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous') + +const Node = require('./test/utils/nodejs-bundle.js') +const { + getPeerRelay, + WS_RENDEZVOUS_MULTIADDR +} = require('./test/utils/constants.js') + +let wsRendezvous +let node + +const before = (done) => { + parallel([ + (cb) => { + WebSocketStarRendezvous.start({ + port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port, + refreshPeerListIntervalMS: 1000, + strictMultiaddr: false, + cryptoChallenge: true + }, (err, _server) => { + if (err) { + return cb(err) + } + wsRendezvous = _server + cb() + }) + }, + (cb) => { + getPeerRelay((err, peerInfo) => { + if (err) { + return done(err) + } + + node = new Node({ + peerInfo, + config: { + relay: { + enabled: true, + hop: { + enabled: true, + active: true + } + } + } + }) + + node.handle('/echo/1.0.0', (_, conn) => pull(conn, conn)) + node.start(cb) + }) + } + ], done) +} + +const after = (done) => { + setTimeout(() => + parallel( + [node, wsRendezvous].map((s) => (cb) => s.stop(cb)), + done), + 2000) +} + +module.exports = { + hooks: { + pre: before, + post: after + } +} diff --git a/.travis.yml b/.travis.yml index 87c129ed56..dbe19d5a13 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ os: - osx - windows -script: npx nyc -s npm run test -- --bail +script: npx nyc -s npm run test:node -- --bail after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov jobs: @@ -24,5 +24,21 @@ jobs: - npx aegir dep-check - npm run lint + - stage: test + name: chrome + addons: + chrome: stable + script: + - npx aegir test -t browser + - npx aegir test -t webworker + + - stage: test + name: firefox + addons: + firefox: latest + script: + - npx aegir test -t browser -- --browsers FirefoxHeadless + - npx aegir test -t webworker -- --browsers FirefoxHeadless + notifications: email: false diff --git a/package.json b/package.json index 41bfcaa556..2a69cba707 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,9 @@ "main": "src/index.js", "scripts": { "lint": "aegir lint", - "test": "aegir test -t node", + "test": "aegir test", "test:node": "aegir test -t node", + "test:browser": "aegir test -t browser", "build": "aegir build", "docs": "aegir-docs", "release": "aegir release --target node --docs", @@ -16,6 +17,9 @@ "coverage": "aegir coverage", "coverage-publish": "aegir coverage --provider coveralls" }, + "browser": { + "test/utils/nodejs-bundle": "./test/utils/browser-bundle.js" + }, "files": [ "src", "dist" @@ -50,7 +54,10 @@ "libp2p-secio": "~0.11.1", "libp2p-spdy": "~0.13.3", "libp2p-tcp": "~0.13.0", + "libp2p-websocket-star": "~0.10.2", + "libp2p-websocket-star-rendezvous": "~0.3.0", "lodash": "^4.17.11", + "multiaddr": "^6.0.6", "peer-id": "~0.12.2", "peer-info": "~0.15.1" }, diff --git a/test/fixtures/test-peer.json b/test/fixtures/test-peer.json new file mode 100644 index 0000000000..105046aa65 --- /dev/null +++ b/test/fixtures/test-peer.json @@ -0,0 +1,5 @@ +{ + "id": "Qmex1SSsueWFsUfjdkugJ5zhcnjddAt8TxcnDLUXKD9Sx7", + "privKey": "CAASqAkwggSkAgEAAoIBAQCXzV127CvVHOGMzvsn/U+/32JM58KA6k0FSCCeNFzNowiDS/vV5eezGN5AFoxsF6icWLoaczz7l9RdVD+I/t6PEt9X7XUdrDCtSS8WmAcCgvZWSSf7yAd3jT4GSZDUIgIEeRZsERDt/yVqTLwsZ1G9dMIeh8sbf2zwjTXZIWaRM6o4lq3DYFfzLvJUXlJodxPogU7l7nLkITPUv+yQAMcVHizbNwJvwiETKYeUj73/m/wEPAlnFESexDstxNiIwE/FH8Ao50QPZRO6E6Jb0hhYSI/4CLRdrzDFm/Vzplei3Wr2DokSROaNyeG37VAueyA+pDqn84um+L9uXLwbv5FbAgMBAAECggEAdBUzV/GaQ0nmoQrWvOnUxmFIho7kCjkh1NwnNVPNc+Msa1r7pcI9wJNPwap8j1w4L/cZuYhOJgcg+o2mWFiuULKZ4F9Ro/M89g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kEuIF2jGkg1+jmNiCl/zNMaBOAIP3QbDPtqOWbYlPd2YIzdj6WQ6R4", + "pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCXzV127CvVHOGMzvsn/U+/32JM58KA6k0FSCCeNFzNowiDS/vV5eezGN5AFoxsF6icWLoaczz7l9RdVD+I/t6PEt9X7XUdrDCtSS8WmAcCgvZWSSf7yAd3jT4GSZDUIgIEeRZsERDt/yVqTLwsZ1G9dMIeh8sbf2zwjTXZIWaRM6o4lq3DYFfzLvJUXlJodxPogU7l7nLkITPUv+yQAMcVHizbNwJvwiETKYeUj73/m/wEPAlnFESexDstxNiIwE/FH8Ao50QPZRO6E6Jb0hhYSI/4CLRdrzDFm/Vzplei3Wr2DokSROaNyeG37VAueyA+pDqn84um+L9uXLwbv5FbAgMBAAE=" +} \ No newline at end of file diff --git a/test/node.js b/test/node.js deleted file mode 100644 index 4d8e1688dc..0000000000 --- a/test/node.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict' - -require('./pubsub.js') diff --git a/test/pubsub.js b/test/pubsub.spec.js similarity index 93% rename from test/pubsub.js rename to test/pubsub.spec.js index 2c824a7611..b78b2bd964 100644 --- a/test/pubsub.js +++ b/test/pubsub.spec.js @@ -46,8 +46,8 @@ describe('pubsub base protocol', () => { before((done) => { series([ - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb), - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb) + (cb) => createNode(cb), + (cb) => createNode(cb) ], (err, nodes) => { if (err) { return done(err) @@ -126,8 +126,8 @@ describe('pubsub base protocol', () => { before((done) => { series([ - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb), - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb) + (cb) => createNode(cb), + (cb) => createNode(cb) ], (cb, nodes) => { nodeA = nodes[0] nodeB = nodes[1] @@ -177,8 +177,8 @@ describe('pubsub base protocol', () => { sandbox = chai.spy.sandbox() series([ - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb), - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb) + (cb) => createNode(cb), + (cb) => createNode(cb) ], (err, nodes) => { if (err) return done(err) @@ -238,8 +238,8 @@ describe('pubsub base protocol', () => { sandbox = chai.spy.sandbox() series([ - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb), - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb) + (cb) => createNode(cb), + (cb) => createNode(cb) ], (err, nodes) => { if (err) return done(err) @@ -310,8 +310,8 @@ describe('pubsub base protocol', () => { sandbox = chai.spy.sandbox() series([ - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb), - (cb) => createNode('/ip4/127.0.0.1/tcp/0', cb) + (cb) => createNode(cb), + (cb) => createNode(cb) ], (err, nodes) => { if (err) return done(err) diff --git a/test/utils/browser-bundle.js b/test/utils/browser-bundle.js new file mode 100644 index 0000000000..117e0a0e19 --- /dev/null +++ b/test/utils/browser-bundle.js @@ -0,0 +1,31 @@ +'use strict' + +const WebSocketStar = require('libp2p-websocket-star') +const spdy = require('libp2p-spdy') +const secio = require('libp2p-secio') +const libp2p = require('libp2p') + +const { WS_STAR_MULTIADDR } = require('./constants') + +class Node extends libp2p { + constructor ({ peerInfo, peerBook }) { + const starOpts = { id: peerInfo.id } + const wsStar = new WebSocketStar(starOpts) + + peerInfo.multiaddrs.add(WS_STAR_MULTIADDR) + + const modules = { + transport: [wsStar], + streamMuxer: [spdy], + connEncryption: [secio] + } + + super({ + modules, + peerInfo, + peerBook + }) + } +} + +module.exports = Node diff --git a/test/utils/constants.js b/test/utils/constants.js new file mode 100644 index 0000000000..a836a03c43 --- /dev/null +++ b/test/utils/constants.js @@ -0,0 +1,40 @@ +'use strict' + +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') +const nextTick = require('async/nextTick') +const peerJSON = require('../fixtures/test-peer') +const multiaddr = require('multiaddr') + +let peerRelay = null + +/** + * Creates a `PeerInfo` that can be used across testing. Once the + * relay `PeerInfo` has been requested, it will be reused for each + * additional request. + * + * This is currently being used to create a relay on test bootstrapping + * so that it can be used by browser nodes during their test suite. This + * is necessary for running a TCP node during browser tests. + * @private + * @param {function(error, PeerInfo)} callback + * @returns {void} + */ +module.exports.getPeerRelay = (callback) => { + if (peerRelay) return nextTick(callback, null, peerRelay) + + PeerId.createFromJSON(peerJSON, (err, peerId) => { + if (err) { + return callback(err) + } + peerRelay = new PeerInfo(peerId) + + peerRelay.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws') + peerRelay.multiaddrs.add('/ip4/127.0.0.1/tcp/9245') + + callback(null, peerRelay) + }) +} + +module.exports.WS_STAR_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/14444/ws/p2p-websocket-star/') +module.exports.WS_RENDEZVOUS_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/14444/wss') diff --git a/test/utils.js b/test/utils/index.js similarity index 83% rename from test/utils.js rename to test/utils/index.js index ee1915014c..e3cc5ef1d5 100644 --- a/test/utils.js +++ b/test/utils/index.js @@ -3,14 +3,14 @@ const PeerId = require('peer-id') const PeerInfo = require('peer-info') const Node = require('./nodejs-bundle') + const waterfall = require('async/waterfall') -exports.createNode = (maddr, callback) => { +exports.createNode = (callback) => { waterfall([ (cb) => PeerId.create({ bits: 1024 }, cb), (id, cb) => PeerInfo.create(id, cb), (peerInfo, cb) => { - peerInfo.multiaddrs.add(maddr) cb(null, new Node({ peerInfo })) }, (node, cb) => node.start((err) => cb(err, node)) diff --git a/test/nodejs-bundle.js b/test/utils/nodejs-bundle.js similarity index 89% rename from test/nodejs-bundle.js rename to test/utils/nodejs-bundle.js index c5e840a84d..ef572d1dc5 100644 --- a/test/nodejs-bundle.js +++ b/test/utils/nodejs-bundle.js @@ -13,6 +13,8 @@ class Node extends libp2p { connEncryption: [secio] } + peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/0') + super({ modules, peerInfo,