From d2faf0b85e24bcb356ab1e2b20d498d02c41d3ec Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 10:32:17 +0000 Subject: [PATCH 1/2] deps(dev): bump aegir from 37.12.1 to 38.1.7 Bumps [aegir](https://github.com/ipfs/aegir) from 37.12.1 to 38.1.7. - [Release notes](https://github.com/ipfs/aegir/releases) - [Changelog](https://github.com/ipfs/aegir/blob/master/CHANGELOG.md) - [Commits](https://github.com/ipfs/aegir/compare/v37.12.1...v38.1.7) --- updated-dependencies: - dependency-name: aegir dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4895d8c..f03f7ae 100644 --- a/package.json +++ b/package.json @@ -177,7 +177,7 @@ "@libp2p/interface-mocks": "^8.0.2", "@libp2p/interface-transport-compliance-tests": "^3.0.0", "@types/ws": "^8.2.2", - "aegir": "^37.5.3", + "aegir": "^38.1.7", "is-loopback-addr": "^2.0.1", "it-all": "^2.0.0", "it-drain": "^2.0.0", From 701ee0335588c03eef4866c2bacbb6c0014b2ccc Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 9 Mar 2023 16:01:20 +0100 Subject: [PATCH 2/2] chore: fix upgrade --- package.json | 4 ++-- src/filters.ts | 8 +++---- src/index.ts | 12 +++++------ src/listener.browser.ts | 3 ++- src/listener.ts | 10 ++++----- test/compliance.node.ts | 7 +++--- test/node.ts | 48 ++++++++++++++++++++--------------------- 7 files changed, 47 insertions(+), 45 deletions(-) diff --git a/package.json b/package.json index f03f7ae..a3f5316 100644 --- a/package.json +++ b/package.json @@ -171,7 +171,8 @@ "it-ws": "^5.0.6", "p-defer": "^4.0.0", "p-timeout": "^6.0.0", - "wherearewe": "^2.0.1" + "wherearewe": "^2.0.1", + "ws": "^8.12.1" }, "devDependencies": { "@libp2p/interface-mocks": "^8.0.2", @@ -184,7 +185,6 @@ "it-goodbye": "^4.0.1", "it-pipe": "^2.0.3", "it-stream-types": "^1.0.4", - "it-take": "^2.0.0", "p-wait-for": "^5.0.0", "uint8arraylist": "^2.3.2", "uint8arrays": "^4.0.2" diff --git a/src/filters.ts b/src/filters.ts index 8e227fd..e848d92 100644 --- a/src/filters.ts +++ b/src/filters.ts @@ -8,7 +8,7 @@ import { CODE_WSS } from './constants.js' -export function all (multiaddrs: Multiaddr[]) { +export function all (multiaddrs: Multiaddr[]): Multiaddr[] { return multiaddrs.filter((ma) => { if (ma.protoCodes().includes(CODE_CIRCUIT)) { return false @@ -21,7 +21,7 @@ export function all (multiaddrs: Multiaddr[]) { }) } -export function wss (multiaddrs: Multiaddr[]) { +export function wss (multiaddrs: Multiaddr[]): Multiaddr[] { return multiaddrs.filter((ma) => { if (ma.protoCodes().includes(CODE_CIRCUIT)) { return false @@ -33,7 +33,7 @@ export function wss (multiaddrs: Multiaddr[]) { }) } -export function dnsWss (multiaddrs: Multiaddr[]) { +export function dnsWss (multiaddrs: Multiaddr[]): Multiaddr[] { return multiaddrs.filter((ma) => { if (ma.protoCodes().includes(CODE_CIRCUIT)) { return false @@ -46,7 +46,7 @@ export function dnsWss (multiaddrs: Multiaddr[]) { }) } -export function dnsWsOrWss (multiaddrs: Multiaddr[]) { +export function dnsWsOrWss (multiaddrs: Multiaddr[]): Multiaddr[] { return multiaddrs.filter((ma) => { if (ma.protoCodes().includes(CODE_CIRCUIT)) { return false diff --git a/src/index.ts b/src/index.ts index 725cfc6..392f8d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import { isBrowser, isWebWorker } from 'wherearewe' import { createListener } from './listener.js' import { socketToMaConn } from './socket-to-conn.js' import * as filters from './filters.js' -import { Transport, MultiaddrFilter, symbol, CreateListenerOptions, DialOptions } from '@libp2p/interface-transport' +import { Transport, MultiaddrFilter, symbol, CreateListenerOptions, DialOptions, Listener } from '@libp2p/interface-transport' import type { Connection } from '@libp2p/interface-connection' import type { AbortOptions } from '@libp2p/interfaces' import type { Multiaddr } from '@multiformats/multiaddr' @@ -30,7 +30,7 @@ class WebSockets implements Transport { this.init = init } - get [Symbol.toStringTag] () { + get [Symbol.toStringTag] (): string { return '@libp2p/websockets' } @@ -59,7 +59,7 @@ class WebSockets implements Transport { log('dialing %s:%s', cOpts.host, cOpts.port) const errorPromise = pDefer() - const errfn = (err: any) => { + const errfn = (err: any): void => { log.error('connection error:', err) errorPromise.reject(err) @@ -92,7 +92,7 @@ class WebSockets implements Transport { // Already aborted? if (options?.signal?.aborted === true) { - return onAbort() + onAbort(); return } options?.signal?.addEventListener('abort', onAbort) @@ -115,7 +115,7 @@ class WebSockets implements Transport { * anytime a new incoming Connection has been successfully upgraded via * `upgrader.upgradeInbound` */ - createListener (options: CreateListenerOptions) { + createListener (options: CreateListenerOptions): Listener { return createListener({ ...this.init, ...options }) } @@ -124,7 +124,7 @@ class WebSockets implements Transport { * By default, in a browser environment only DNS+WSS multiaddr is accepted, * while in a Node.js environment DNS+{WS, WSS} multiaddrs are accepted. */ - filter (multiaddrs: Multiaddr[]) { + filter (multiaddrs: Multiaddr[]): Multiaddr[] { multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs] if (this.init?.filter != null) { diff --git a/src/listener.browser.ts b/src/listener.browser.ts index 2d2457b..d5568a9 100644 --- a/src/listener.browser.ts +++ b/src/listener.browser.ts @@ -1,4 +1,5 @@ +import type { Listener } from '@libp2p/interface-transport' -export function createListener () { +export function createListener (): Listener { throw new Error('WebSocket Servers can not be created in the browser!') } diff --git a/src/listener.ts b/src/listener.ts index 051ebd2..64d7bac 100644 --- a/src/listener.ts +++ b/src/listener.ts @@ -81,9 +81,9 @@ class WebSocketListener extends EventEmitter implements Listener }) } - async close () { + async close (): Promise { await Promise.all( - Array.from(this.connections).map(async maConn => await maConn.close()) + Array.from(this.connections).map(async maConn => { await maConn.close() }) ) if (this.server.address() == null) { @@ -91,16 +91,16 @@ class WebSocketListener extends EventEmitter implements Listener return } - return await this.server.close() + await this.server.close() } - async listen (ma: Multiaddr) { + async listen (ma: Multiaddr): Promise { this.listeningMultiaddr = ma await this.server.listen(ma.toOptions()) } - getAddrs () { + getAddrs (): Multiaddr[] { const multiaddrs = [] const address = this.server.address() diff --git a/test/compliance.node.ts b/test/compliance.node.ts index 5502f49..f347c99 100644 --- a/test/compliance.node.ts +++ b/test/compliance.node.ts @@ -6,6 +6,7 @@ import http from 'http' import { webSockets } from '../src/index.js' import * as filters from '../src/filters.js' import type { WebSocketListenerInit } from '../src/listener.js' +import type { Listener } from '@libp2p/interface-transport' describe('interface-transport compliance', () => { tests({ @@ -19,7 +20,7 @@ describe('interface-transport compliance', () => { ] let delayMs = 0 - const delayedCreateListener = (options: WebSocketListenerInit) => { + const delayedCreateListener = (options: WebSocketListenerInit): Listener => { // A server that will delay the upgrade event by delayMs options.server = new Proxy(http.createServer(), { get (server, prop) { @@ -27,9 +28,9 @@ describe('interface-transport compliance', () => { return (event: string, handler: (...args: any[]) => void) => { server.on(event, (...args) => { if (event !== 'upgrade' || delayMs === 0) { - return handler(...args) + handler(...args); return } - setTimeout(() => handler(...args), delayMs) + setTimeout(() => { handler(...args) }, delayMs) }) } } diff --git a/test/node.ts b/test/node.ts index b3f2491..5de5846 100644 --- a/test/node.ts +++ b/test/node.ts @@ -22,7 +22,7 @@ import type { Uint8ArrayList } from 'uint8arraylist' import type { Source } from 'it-stream-types' import './compliance.node.js' -async function * toBuffers (source: Source) { +async function * toBuffers (source: Source): AsyncGenerator { for await (const list of source) { yield * list } @@ -57,7 +57,7 @@ describe('listen', () => { const listener = ws.createListener({ handler: (conn) => { void conn.newStream([protocol]).then(async (stream) => { - return await pipe(stream, stream) + await pipe(stream, stream) }) }, upgrader @@ -85,7 +85,7 @@ describe('listen', () => { }) afterEach(async () => { - return await listener.close() + await listener.close() }) it('listen, check for promise', async () => { @@ -108,14 +108,14 @@ describe('listen', () => { const dumbServer = http.createServer() await new Promise(resolve => dumbServer.listen(ma.toOptions().port, resolve)) await expect(listener.listen(ma)).to.eventually.rejectedWith('listen EADDRINUSE') - await new Promise(resolve => dumbServer.close(() => resolve())) + await new Promise(resolve => dumbServer.close(() => { resolve() })) }) it('listen, check for the close event', (done) => { const listener = ws.createListener({ upgrader }) listener.addEventListener('listening', () => { - listener.addEventListener('close', () => done()) + listener.addEventListener('close', () => { done() }) void listener.close() }) @@ -134,7 +134,7 @@ describe('listen', () => { listener = ws.createListener({ upgrader }) await listener.listen(ma) - const addrs = await listener.getAddrs() + const addrs = listener.getAddrs() expect(addrs.map((a) => a.toOptions().port)).to.not.include(0) }) @@ -143,14 +143,14 @@ describe('listen', () => { listener = ws.createListener({ upgrader }) await listener.listen(ma) - const addrs = await listener.getAddrs() + const addrs = listener.getAddrs() expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0') }) it('getAddrs', async () => { listener = ws.createListener({ upgrader }) await listener.listen(ma) - const addrs = await listener.getAddrs() + const addrs = listener.getAddrs() expect(addrs.length).to.equal(1) expect(addrs[0]).to.deep.equal(ma) }) @@ -159,7 +159,7 @@ describe('listen', () => { const addr = multiaddr('/ip4/127.0.0.1/tcp/0/ws') listener = ws.createListener({ upgrader }) await listener.listen(addr) - const addrs = await listener.getAddrs() + const addrs = listener.getAddrs() expect(addrs.length).to.equal(1) expect(addrs.map((a) => a.toOptions().port)).to.not.include('0') }) @@ -168,7 +168,7 @@ describe('listen', () => { const addr = multiaddr('/ip4/0.0.0.0/tcp/47382/ws') listener = ws.createListener({ upgrader }) await listener.listen(addr) - const addrs = await listener.getAddrs() + const addrs = listener.getAddrs() expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0') }) @@ -176,7 +176,7 @@ describe('listen', () => { const addr = multiaddr('/ip4/0.0.0.0/tcp/0/ws') listener = ws.createListener({ upgrader }) await listener.listen(addr) - const addrs = await listener.getAddrs() + const addrs = listener.getAddrs() expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0') expect(addrs.map((a) => a.toOptions().port)).to.not.include('0') }) @@ -186,7 +186,7 @@ describe('listen', () => { listener = ws.createListener({ upgrader }) await listener.listen(ma) - const addrs = await listener.getAddrs() + const addrs = listener.getAddrs() expect(addrs.length).to.equal(1) expect(addrs[0]).to.deep.equal(ma) }) @@ -220,7 +220,7 @@ describe('listen', () => { const listener = ws.createListener({ upgrader }) listener.addEventListener('listening', () => { - listener.addEventListener('close', () => done()) + listener.addEventListener('close', () => { done() }) void listener.close() }) @@ -245,10 +245,10 @@ describe('dial', () => { beforeEach(async () => { ws = webSockets()() listener = ws.createListener({ upgrader }) - return await listener.listen(ma) + await listener.listen(ma) }) - afterEach(async () => await listener.close()) + afterEach(async () => { await listener.close() }) it('dial', async () => { const conn = await ws.dial(ma, { upgrader }) @@ -313,15 +313,15 @@ describe('dial', () => { listener = ws.createListener({ handler: (conn) => { void conn.newStream([protocol]).then(async (stream) => { - return await pipe(stream, stream) + await pipe(stream, stream) }) }, upgrader }) - return await listener.listen(ma) + await listener.listen(ma) }) - afterEach(async () => await listener.close()) + afterEach(async () => { await listener.close() }) it('dial', async () => { const addrs = listener.getAddrs().filter((ma) => { @@ -359,17 +359,17 @@ describe('dial', () => { listener = ws.createListener({ handler: (conn) => { void conn.newStream([protocol]).then(async (stream) => { - return await pipe(stream, stream) + await pipe(stream, stream) }) }, upgrader }) - return await listener.listen(ma) + await listener.listen(ma) }) afterEach(async () => { await listener.close() - await server.close() + server.close() }) it('should listen on wss address', () => { @@ -401,15 +401,15 @@ describe('dial', () => { listener = ws.createListener({ handler: (conn) => { void conn.newStream([protocol]).then(async (stream) => { - return await pipe(stream, stream) + await pipe(stream, stream) }) }, upgrader }) - return await listener.listen(ma) + await listener.listen(ma) }) - afterEach(async () => await listener.close()) + afterEach(async () => { await listener.close() }) it('dial ip6', async () => { const conn = await ws.dial(ma, { upgrader })