diff --git a/.travis.yml b/.travis.yml index 37005a25df..32d81f9f49 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,19 +24,5 @@ jobs: - npx aegir dep-check -- -i wrtc -i electron-webrtc - npm run lint - - stage: test - name: chrome - addons: - chrome: stable - script: - - npx aegir test -t browser - - - stage: test - name: firefox - addons: - firefox: latest - script: - - npx aegir test -t browser -- --browsers FirefoxHeadless - notifications: email: false diff --git a/package.json b/package.json index e62dab18b4..9ca825c1c4 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "debug": "^4.1.1", "length-prefixed-stream": "^2.0.0", "libp2p-crypto": "~0.16.1", - "libp2p-pubsub": "~0.0.2", + "libp2p-pubsub": "~0.0.4", "protons": "^1.0.1", "pull-length-prefixed": "^1.3.2", "pull-pushable": "^2.2.0", diff --git a/src/index.js b/src/index.js index 666de13275..71469dbb00 100644 --- a/src/index.js +++ b/src/index.js @@ -5,8 +5,7 @@ const lp = require('pull-length-prefixed') const assert = require('assert') const BaseProtocol = require('libp2p-pubsub') -const { message } = require('libp2p-pubsub') -const utils = require('./utils') +const { message, utils } = require('libp2p-pubsub') const config = require('./config') const multicodec = config.multicodec diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index 53c515216e..0000000000 --- a/src/utils.js +++ /dev/null @@ -1,95 +0,0 @@ -'use strict' - -const crypto = require('libp2p-crypto') -const bs58 = require('bs58') - -exports = module.exports - -/** - * Generatea random sequence number. - * - * @returns {Buffer} - * @private - */ -exports.randomSeqno = () => { - return crypto.randomBytes(20) -} - -/** - * Generate a message id, based on the `from` and `seqno`. - * - * @param {string} from - * @param {Buffer} seqno - * @returns {string} - * @private - */ -exports.msgId = (from, seqno) => { - return from + seqno.toString('hex') -} - -/** - * Check if any member of the first set is also a member - * of the second set. - * - * @param {Set|Array} a - * @param {Set|Array} b - * @returns {boolean} - * @private - */ -exports.anyMatch = (a, b) => { - let bHas - if (Array.isArray(b)) { - bHas = (val) => b.indexOf(val) > -1 - } else { - bHas = (val) => b.has(val) - } - - for (let val of a) { - if (bHas(val)) { - return true - } - } - - return false -} - -/** - * Make everything an array. - * - * @param {any} maybeArray - * @returns {Array} - * @private - */ -exports.ensureArray = (maybeArray) => { - if (!Array.isArray(maybeArray)) { - return [maybeArray] - } - - return maybeArray -} - -exports.normalizeInRpcMessages = (messages) => { - if (!messages) { - return messages - } - return messages.map((msg) => { - const m = Object.assign({}, msg) - if (Buffer.isBuffer(msg.from)) { - m.from = bs58.encode(msg.from) - } - return m - }) -} - -exports.normalizeOutRpcMessages = (messages) => { - if (!messages) { - return messages - } - return messages.map((msg) => { - const m = Object.assign({}, msg) - if (typeof msg.from === 'string' || msg.from instanceof String) { - m.from = bs58.decode(msg.from) - } - return m - }) -} diff --git a/test/2-nodes.js b/test/2-nodes.js index 58fb7359cb..eb5caf4b43 100644 --- a/test/2-nodes.js +++ b/test/2-nodes.js @@ -83,7 +83,7 @@ describe('basics between 2 nodes', () => { expectSet(first(fsB.peers).topics, ['Z']) expect(changedPeerInfo.id.toB58String()).to.equal(first(fsB.peers).info.id.toB58String()) expectSet(changedTopics, ['Z']) - expect(changedSubs).to.be.eql([{ topicCID: 'Z', subscribe: true }]) + expect(changedSubs).to.be.eql([{ topicID: 'Z', subscribe: true }]) done() }) }) @@ -174,7 +174,7 @@ describe('basics between 2 nodes', () => { expectSet(first(fsB.peers).topics, []) expect(changedPeerInfo.id.toB58String()).to.equal(first(fsB.peers).info.id.toB58String()) expectSet(changedTopics, []) - expect(changedSubs).to.be.eql([{ topicCID: 'Z', subscribe: false }]) + expect(changedSubs).to.be.eql([{ topicID: 'Z', subscribe: false }]) done() }) }) diff --git a/test/utils.spec.js b/test/utils.spec.js deleted file mode 100644 index d573f06f53..0000000000 --- a/test/utils.spec.js +++ /dev/null @@ -1,78 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const expect = require('chai').expect - -const utils = require('../src/utils') - -describe('utils', () => { - it('randomSeqno', () => { - const first = utils.randomSeqno() - const second = utils.randomSeqno() - - expect(first).to.have.length(20) - expect(second).to.have.length(20) - expect(first).to.not.eql(second) - }) - - it('msgId', () => { - expect(utils.msgId('hello', Buffer.from('world'))).to.be.eql('hello776f726c64') - }) - - it('msgId should not generate same ID for two different buffers', () => { - const peerId = 'QmPNdSYk5Rfpo5euNqwtyizzmKXMNHdXeLjTQhcN4yfX22' - const msgId0 = utils.msgId(peerId, Buffer.from('15603533e990dfde', 'hex')) - const msgId1 = utils.msgId(peerId, Buffer.from('15603533e990dfe0', 'hex')) - expect(msgId0).to.not.eql(msgId1) - }) - - it('anyMatch', () => { - [ - [[1, 2, 3], [4, 5, 6], false], - [[1, 2], [1, 2], true], - [[1, 2, 3], [4, 5, 1], true], - [[5, 6, 1], [1, 2, 3], true], - [[], [], false], - [[1], [2], false] - ].forEach((test) => { - expect(utils.anyMatch(new Set(test[0]), new Set(test[1]))) - .to.eql(test[2]) - - expect(utils.anyMatch(new Set(test[0]), test[1])) - .to.eql(test[2]) - }) - }) - - it('ensureArray', () => { - expect(utils.ensureArray('hello')).to.be.eql(['hello']) - expect(utils.ensureArray([1, 2])).to.be.eql([1, 2]) - }) - - it('converts an IN msg.from to b58', () => { - let binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex') - let stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM' - const m = [ - { from: binaryId }, - { from: stringId } - ] - const expected = [ - { from: stringId }, - { from: stringId } - ] - expect(utils.normalizeInRpcMessages(m)).to.deep.eql(expected) - }) - - it('converts an OUT msg.from to binary', () => { - let binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex') - let stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM' - const m = [ - { from: binaryId }, - { from: stringId } - ] - const expected = [ - { from: binaryId }, - { from: binaryId } - ] - expect(utils.normalizeOutRpcMessages(m)).to.deep.eql(expected) - }) -})