diff --git a/.aegir.js b/.aegir.js index fbeffd4..1c2dcb9 100644 --- a/.aegir.js +++ b/.aegir.js @@ -2,9 +2,9 @@ const WebRTCDirect = require('./src') const pipe = require('it-pipe') -const multiaddr = require('multiaddr') +const { Multiaddr } = require('multiaddr') -const ma = multiaddr('/ip4/127.0.0.1/tcp/12345/http/p2p-webrtc-direct') +const ma = new Multiaddr('/ip4/127.0.0.1/tcp/12345/http/p2p-webrtc-direct') let listener const mockUpgrader = { @@ -12,7 +12,7 @@ const mockUpgrader = { upgradeOutbound: maConn => maConn } -function boot () { +function before () { const wd = new WebRTCDirect({ upgrader: mockUpgrader }) listener = wd.createListener((conn) => pipe(conn, conn)) @@ -23,7 +23,7 @@ function boot () { return listener.listen(ma) } -async function shutdown () { +async function after () { await listener.close() // TODO: Temporary fix per wrtc issue // https://github.com/node-webrtc/node-webrtc/issues/636 @@ -32,7 +32,7 @@ async function shutdown () { module.exports = { hooks: { - pre: boot, - post: shutdown + pre: before, + post: after } } diff --git a/package.json b/package.json index 3bfd41f..2293a8a 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,6 @@ "release-major": "aegir release --type major --target node --target browser", "coverage": "nyc --reporter=text --reporter=lcov npm run test:node" }, - "pre-push": [ - "lint" - ], "repository": { "type": "git", "url": "git+https://github.com/libp2p/js-libp2p-webrtc-direct.git" @@ -43,15 +40,14 @@ }, "homepage": "https://github.com/libp2p/js-libp2p-webrtc-direct#readme", "engines": { - "node": ">=12.0.0", - "npm": ">=6.0.0" + "node": ">=14.0.0" }, "devDependencies": { "aegir": "^30.3.0", - "chai": "^4.2.0", + "chai": "^4.3.4", "dirty-chai": "^2.0.1", "it-pipe": "^1.1.0", - "multiaddr": "^8.1.1", + "multiaddr": "^9.0.1", "streaming-iterables": "^5.0.3", "webrtcsupport": "^2.2.0" }, @@ -63,10 +59,10 @@ "detect-node": "^2.0.4", "err-code": "^3.0.0", "libp2p-interfaces": "libp2p/js-interfaces#chore/skip-abort-while-reading-for-webrtc", - "libp2p-utils": "^0.2.3", + "libp2p-utils": "^0.3.0", "libp2p-webrtc-peer": "^10.0.1", - "mafmt": "^8.0.1", - "multibase": "^3.1.0", + "mafmt": "^9.0.0", + "multibase": "^4.0.4", "once": "^1.4.0", "request": "^2.88.0", "stream-to-it": "^0.2.2", diff --git a/src/index.js b/src/index.js index 4c20c27..e0d0924 100644 --- a/src/index.js +++ b/src/index.js @@ -22,6 +22,10 @@ const createListener = require('./listener') function noop () {} +/** + * @typedef {import('multiaddr').Multiaddr} Multiaddr + */ + class WebRTCDirect { /** * @class diff --git a/src/listener.js b/src/listener.js index ee95008..4f42021 100644 --- a/src/listener.js +++ b/src/listener.js @@ -102,13 +102,13 @@ module.exports = ({ handler, upgrader }, options = {}) => { }) } - listener.close = () => { + listener.close = async () => { if (!server.listening) { return } + await Promise.all(listener.__connections.map(c => c.close())) return new Promise((resolve, reject) => { - listener.__connections.forEach(maConn => maConn.close()) server.close((err) => err ? reject(err) : resolve()) }) } diff --git a/test/compliance.js b/test/compliance.js index efbfa89..8212035 100644 --- a/test/compliance.js +++ b/test/compliance.js @@ -2,7 +2,7 @@ 'use strict' const tests = require('libp2p-interfaces/src/transport/tests') -const multiaddr = require('multiaddr') +const { Multiaddr } = require('multiaddr') const WDirect = require('../src') @@ -12,10 +12,10 @@ describe('interface-transport compliance', () => { const ws = new WDirect({ upgrader }) const addrs = [ - multiaddr('/ip4/127.0.0.1/tcp/22222/http/p2p-webrtc-direct'), - multiaddr('/ip4/127.0.0.1/tcp/33333/http/p2p-webrtc-direct'), - multiaddr('/ip4/127.0.0.1/tcp/44444/http/p2p-webrtc-direct'), - multiaddr('/ip4/127.0.0.1/tcp/55555/http/p2p-webrtc-direct') + new Multiaddr('/ip4/127.0.0.1/tcp/22222/http/p2p-webrtc-direct'), + new Multiaddr('/ip4/127.0.0.1/tcp/33333/http/p2p-webrtc-direct'), + new Multiaddr('/ip4/127.0.0.1/tcp/44444/http/p2p-webrtc-direct'), + new Multiaddr('/ip4/127.0.0.1/tcp/55555/http/p2p-webrtc-direct') ] // Used by the dial tests to simulate a delayed connect diff --git a/test/dial.spec.js b/test/dial.spec.js index 70df6d5..b529628 100644 --- a/test/dial.spec.js +++ b/test/dial.spec.js @@ -7,7 +7,7 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const multiaddr = require('multiaddr') +const { Multiaddr } = require('multiaddr') const pipe = require('it-pipe') const { collect } = require('streaming-iterables') @@ -23,7 +23,7 @@ const mockUpgrader = { describe('dial', function () { this.timeout(20 * 1000) - const ma = multiaddr('/ip4/127.0.0.1/tcp/12345/http/p2p-webrtc-direct') + const ma = new Multiaddr('/ip4/127.0.0.1/tcp/12345/http/p2p-webrtc-direct') let wd before(() => { @@ -44,7 +44,7 @@ describe('dial', function () { }) it('dial offline / non-existent node on IPv4, check callback', async () => { - const maOffline = multiaddr('/ip4/127.0.0.1/tcp/55555/http/p2p-webrtc-direct') + const maOffline = new Multiaddr('/ip4/127.0.0.1/tcp/55555/http/p2p-webrtc-direct') try { await wd.dial(maOffline, { config: {} }) diff --git a/test/filter.spec.js b/test/filter.spec.js index 5ed3bc0..9c82985 100644 --- a/test/filter.spec.js +++ b/test/filter.spec.js @@ -5,7 +5,7 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const multiaddr = require('multiaddr') +const { Multiaddr } = require('multiaddr') const WebRTCDirect = require('../src') @@ -18,11 +18,11 @@ describe('filter', () => { it('filters non valid webrtc-direct multiaddrs', () => { const wd = new WebRTCDirect({ upgrader: mockUpgrader }) const maArr = [ - multiaddr('/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct'), - multiaddr('/ip4/127.0.0.1/tcp/9090/ws'), - multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p-webrtc-direct/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo2'), - multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'), - multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4' + + new Multiaddr('/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct'), + new Multiaddr('/ip4/127.0.0.1/tcp/9090/ws'), + new Multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p-webrtc-direct/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo2'), + new Multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'), + new Multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4' + '/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5') ] @@ -32,7 +32,7 @@ describe('filter', () => { it('filter a single addr for this transport', () => { const wd = new WebRTCDirect({ upgrader: mockUpgrader }) - const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct') + const ma = new Multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct') const filtered = wd.filter(ma) expect(filtered.length).to.equal(1) diff --git a/test/listen.js b/test/listen.js index 070ef84..6336cfa 100644 --- a/test/listen.js +++ b/test/listen.js @@ -7,7 +7,7 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const multiaddr = require('multiaddr') +const { Multiaddr } = require('multiaddr') const pipe = require('it-pipe') const WebRTCDirect = require('../src') @@ -20,7 +20,7 @@ const mockUpgrader = { describe('listen', () => { let wd - const ma = multiaddr('/ip4/127.0.0.1/tcp/20123/http/p2p-webrtc-direct') + const ma = new Multiaddr('/ip4/127.0.0.1/tcp/20123/http/p2p-webrtc-direct') before(() => { wd = new WebRTCDirect({ upgrader: mockUpgrader }) @@ -78,7 +78,7 @@ describe('listen', () => { it('should untrack conn after being closed', async function () { this.timeout(20e3) - const ma1 = multiaddr('/ip4/127.0.0.1/tcp/12346/http/p2p-webrtc-direct') + const ma1 = new Multiaddr('/ip4/127.0.0.1/tcp/12346/http/p2p-webrtc-direct') const wd1 = new WebRTCDirect({ upgrader: mockUpgrader }) const listener1 = wd1.createListener((conn) => pipe(conn, conn)) @@ -104,7 +104,7 @@ describe('listen', () => { it('should have remoteAddress in listener connection', async function () { this.timeout(20e3) - const ma1 = multiaddr('/ip4/127.0.0.1/tcp/12346/http/p2p-webrtc-direct') + const ma1 = new Multiaddr('/ip4/127.0.0.1/tcp/12346/http/p2p-webrtc-direct') const wd1 = new WebRTCDirect({ upgrader: mockUpgrader }) const listener1 = wd1.createListener((conn) => {