diff --git a/.aegir.js b/.aegir.js index f679dcc..53d3b66 100644 --- a/.aegir.js +++ b/.aegir.js @@ -5,6 +5,7 @@ * set up a libp2p instance for browser nodes to relay through * before tests start */ +const path = require('path') const Libp2p = require('libp2p') const PeerId = require('peer-id') @@ -52,15 +53,20 @@ const after = async () => { await libp2p.stop() } +/** @type {import('aegir').Options["build"]["config"]} */ +const esbuild = { + inject: [path.join(__dirname, './scripts/node-globals.js')] +} + +/** @type {import('aegir').PartialOptions} */ module.exports = { - hooks: { - pre: before, - post: after - }, - webpack: { - node: { - // this is needed until bcrypto stops using node buffers in browser code - Buffer: true + test: { + before, + after, + browser: { + config: { + buildConfig: esbuild + } } } } diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..98aa69b --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,70 @@ +name: ci +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx aegir lint + - run: npx aegir dep-check + - run: npx aegir build + test-node: + needs: check + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + node: [14, 15] + fail-fast: true + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node }} + - run: npm install + - run: npx aegir test -t node --cov --bail + - uses: codecov/codecov-action@v1 + test-chrome: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx aegir test -t browser -t webworker --bail + test-firefox: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx aegir test -t browser -t webworker --bail -- --browser firefox + test-webkit: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: microsoft/playwright-github-action@v1 + - run: npm install + - run: npx aegir test -t browser -t webworker --bail -- --browser webkit + test-electron-main: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx xvfb-maybe aegir test -t electron-main --bail + test-electron-renderer: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx xvfb-maybe aegir test -t electron-renderer --bail \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e0d74ff..0000000 --- a/.travis.yml +++ /dev/null @@ -1,44 +0,0 @@ -language: node_js -cache: npm -stages: - - check - - test - - cov - -node_js: - - '10' - -os: - - linux - - osx - - windows - -script: npx nyc -s npm run test:node -- --bail -after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov - -jobs: - include: - - stage: check - script: - - npx aegir commitlint --travis - - 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 - - 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/benchmarks/utils.js b/benchmarks/utils.js index 0f9471f..0462918 100644 --- a/benchmarks/utils.js +++ b/benchmarks/utils.js @@ -7,7 +7,7 @@ if (typeof self !== 'undefined') { self.Benchmark = Benchmark } -const utils = require('../src/utils') +const { utils } = require('libp2p-interfaces/src/pubsub') const suite = new Benchmark.Suite('utils') diff --git a/package.json b/package.json index 95d6d5d..1ef7c04 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).", "leadMaintainer": "Vasco Santos ", "main": "src/index.js", + "types": "dist/src/index.d.ts", "scripts": { "lint": "aegir lint", "test": "aegir test", @@ -42,24 +43,28 @@ }, "homepage": "https://github.com/libp2p/js-libp2p-floodsub#readme", "devDependencies": { - "aegir": "^28.1.0", + "@types/debug": "^4.1.5", + "aegir": "^33.0.0", "benchmark": "^2.1.4", - "chai": "^4.2.0", - "ipfs-utils": "^4.0.1", + "buffer": "^6.0.3", + "chai": "^4.3.4", + "ipfs-utils": "^6.0.6", + "it-pair": "^1.0.0", "libp2p": "^0.30.0", "libp2p-mplex": "^0.10.1", "libp2p-noise": "^2.0.1", - "libp2p-websockets": "^0.14.0", - "multiaddr": "^8.1.1", + "libp2p-websockets": "^0.15.5", + "multiaddr": "^9.0.1", + "os": "^0.1.1", "p-wait-for": "^3.1.0", "peer-id": "^0.14.2", - "sinon": "^9.2.1" + "sinon": "^10.0.0" }, "dependencies": { "debug": "^4.2.0", - "libp2p-interfaces": "^0.8.1", + "libp2p-interfaces": "^0.10.0", "time-cache": "^0.3.0", - "uint8arrays": "^1.1.0" + "uint8arrays": "^2.1.4" }, "peerDependencies": { "libp2p": "^0.30.0" diff --git a/scripts/node-globals.js b/scripts/node-globals.js new file mode 100644 index 0000000..511837b --- /dev/null +++ b/scripts/node-globals.js @@ -0,0 +1 @@ +export const { Buffer } = require('buffer') diff --git a/src/config.js b/src/config.js index 0891e2e..c997a65 100644 --- a/src/config.js +++ b/src/config.js @@ -1,9 +1,9 @@ 'use strict' const debug = require('debug') - -const log = debug('libp2p:floodsub') -log.err = debug('libp2p:floodsub:error') +const log = Object.assign(debug('libp2p:floodsub'), { + error: debug('libp2p:floodsub:err') +}) module.exports = { log: log, diff --git a/src/index.js b/src/index.js index 7018e49..7e007be 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ const debugName = 'libp2p:floodsub' +// @ts-ignore time-cache does not export types const TimeCache = require('time-cache') const toString = require('uint8arrays/to-string') const BaseProtocol = require('libp2p-interfaces/src/pubsub') @@ -9,6 +10,10 @@ const { utils } = require('libp2p-interfaces/src/pubsub') const { multicodec } = require('./config') +/** + * @typedef {import('libp2p-interfaces/src/pubsub').InMessage} InMessage + */ + /** * FloodSub (aka dumbsub is an implementation of pubsub focused on * delivering an API for Publish/Subscribe, but with no CastTree Forming @@ -16,9 +21,9 @@ const { multicodec } = require('./config') */ class FloodSub extends BaseProtocol { /** - * @param {Libp2p} libp2p - instance of libp2p + * @param {import('libp2p')} libp2p - instance of libp2p * @param {Object} [options] - * @param {boolean} options.emitSelf - if publish should emit to self, if subscribed, defaults to false + * @param {boolean} [options.emitSelf] - if publish should emit to self, if subscribed, defaults to false * @class */ constructor (libp2p, options = {}) { @@ -64,10 +69,11 @@ class FloodSub extends BaseProtocol { * * @override * @param {InMessage} message - * @returns {void} + * @returns {Promise} */ _publish (message) { this._forwardMessage(message) + return Promise.resolve() } /** diff --git a/test/utils/create-peer.js b/test/utils/create-peer.js index a844213..b8383f7 100644 --- a/test/utils/create-peer.js +++ b/test/utils/create-peer.js @@ -9,10 +9,11 @@ */ const Libp2p = require('libp2p') -const multiaddr = require('multiaddr') +const { Multiaddr } = require('multiaddr') const PeerId = require('peer-id') const WS = require('libp2p-websockets') +const filters = require('libp2p-websockets/src/filters') const MPLEX = require('libp2p-mplex') const { NOISE } = require('libp2p-noise') @@ -21,6 +22,8 @@ const { isNode } = require('ipfs-utils/src/env') const Peers = require('../fixtures/peers') const RelayPeer = require('../fixtures/relay') +const transportKey = WS.prototype[Symbol.toStringTag] + const defaultConfig = { modules: { transport: [WS], @@ -33,6 +36,11 @@ const defaultConfig = { }, peerDiscovery: { autoDial: false + }, + transport: { + [transportKey]: { + filter: filters.all + } } } } @@ -109,10 +117,10 @@ async function createPeers ({ number = 1, started = true, seedAddressBook = true function getListenAddress (peerId) { if (isNode) { // node - return multiaddr('/ip4/127.0.0.1/tcp/0/ws') + return new Multiaddr('/ip4/127.0.0.1/tcp/0/ws') } else { // browser - return multiaddr(`${RelayPeer.multiaddr}/p2p-circuit/p2p/${peerId.toB58String()}`) + return new Multiaddr(`${RelayPeer.multiaddr}/p2p-circuit/p2p/${peerId.toB58String()}`) } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..ec163b1 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src", + "test" + ] +} \ No newline at end of file