From ae75570c852fa9bfe00910dca1c219fa585dcb83 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Mon, 6 Jan 2025 16:52:00 +0100 Subject: [PATCH 1/2] fix: return empty address list during listen operation (#2904) If `getAddrs` is called on a WS listener after `.listen` has been called but before any socket(s) have opened, return an empty list instead of throwing. Fixes #2902 --- packages/transport-websockets/src/listener.ts | 10 +++++----- packages/transport-websockets/test/node.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/transport-websockets/src/listener.ts b/packages/transport-websockets/src/listener.ts index 25d18835b3..cbb4913a91 100644 --- a/packages/transport-websockets/src/listener.ts +++ b/packages/transport-websockets/src/listener.ts @@ -341,20 +341,20 @@ export class WebSocketListener extends TypedEventEmitter impleme } getAddrs (): Multiaddr[] { + if (this.listeningMultiaddr == null) { + throw new Error('Listener is not ready yet') + } + const address = this.server.address() if (address == null) { - throw new Error('Listener is not ready yet') + return [] } if (typeof address === 'string') { throw new Error('Wrong address type received - expected AddressInfo, got string - are you trying to listen on a unix socket?') } - if (this.listeningMultiaddr == null) { - throw new Error('Listener is not ready yet') - } - const options = this.listeningMultiaddr.toOptions() const multiaddrs: Multiaddr[] = [] diff --git a/packages/transport-websockets/test/node.ts b/packages/transport-websockets/test/node.ts index 6baae5a67d..82b255e2cc 100644 --- a/packages/transport-websockets/test/node.ts +++ b/packages/transport-websockets/test/node.ts @@ -73,6 +73,22 @@ describe('listen', () => { void listener.listen(ma) }) + it('should return an empty address list when `getAddrs` called before listening has finished', async () => { + listener = ws.createListener({ upgrader }) + + void listener.listen(ma) + + // call getAddrs before sockets have opened + expect(listener.getAddrs()).to.be.empty() + }) + + it('should throw when `.getAddrs` called before `.listen`', async () => { + listener = ws.createListener({ upgrader }) + + // call getAddrs before sockets have opened + expect(() => listener.getAddrs()).to.throw(/not ready/) + }) + it('should error on starting two listeners on same address', async () => { listener = ws.createListener({ upgrader }) const dumbServer = http.createServer() From 770143800a132a1f720ec140ff303b2ef6e29512 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 7 Jan 2025 09:12:45 +0100 Subject: [PATCH 2/2] chore: release main (#2905) --- .release-please-manifest.json | 2 +- packages/transport-websockets/CHANGELOG.md | 7 +++++++ packages/transport-websockets/package.json | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.release-please-manifest.json b/.release-please-manifest.json index fee723a0b4..02b6bdb480 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1 +1 @@ -{"packages/auto-tls":"1.0.2","packages/config":"1.0.0","packages/connection-encrypter-plaintext":"2.0.12","packages/connection-encrypter-tls":"2.0.12","packages/crypto":"5.0.8","packages/interface":"2.3.0","packages/interface-compliance-tests":"6.2.2","packages/interface-internal":"2.2.1","packages/kad-dht":"14.1.6","packages/keychain":"5.0.11","packages/libp2p":"2.4.2","packages/logger":"5.1.5","packages/metrics-devtools":"1.1.12","packages/metrics-prometheus":"4.2.10","packages/metrics-simple":"1.2.8","packages/multistream-select":"6.0.10","packages/peer-collections":"6.0.13","packages/peer-discovery-bootstrap":"11.0.16","packages/peer-discovery-mdns":"11.0.16","packages/peer-id":"5.0.9","packages/peer-record":"8.0.13","packages/peer-store":"11.0.13","packages/pnet":"2.0.16","packages/protocol-autonat":"2.0.15","packages/protocol-dcutr":"2.0.14","packages/protocol-echo":"2.1.5","packages/protocol-fetch":"2.0.14","packages/protocol-identify":"3.0.14","packages/protocol-perf":"4.0.16","packages/protocol-ping":"2.0.14","packages/pubsub":"10.0.14","packages/pubsub-floodsub":"10.1.14","packages/record":"4.0.4","packages/stream-multiplexer-mplex":"11.0.16","packages/transport-circuit-relay-v2":"3.1.6","packages/transport-memory":"1.0.2","packages/transport-tcp":"10.0.14","packages/transport-webrtc":"5.0.22","packages/transport-websockets":"9.1.0","packages/transport-webtransport":"5.0.21","packages/upnp-nat":"3.0.3","packages/utils":"6.3.0"} +{"packages/auto-tls":"1.0.2","packages/config":"1.0.0","packages/connection-encrypter-plaintext":"2.0.12","packages/connection-encrypter-tls":"2.0.12","packages/crypto":"5.0.8","packages/interface":"2.3.0","packages/interface-compliance-tests":"6.2.2","packages/interface-internal":"2.2.1","packages/kad-dht":"14.1.6","packages/keychain":"5.0.11","packages/libp2p":"2.4.2","packages/logger":"5.1.5","packages/metrics-devtools":"1.1.12","packages/metrics-prometheus":"4.2.10","packages/metrics-simple":"1.2.8","packages/multistream-select":"6.0.10","packages/peer-collections":"6.0.13","packages/peer-discovery-bootstrap":"11.0.16","packages/peer-discovery-mdns":"11.0.16","packages/peer-id":"5.0.9","packages/peer-record":"8.0.13","packages/peer-store":"11.0.13","packages/pnet":"2.0.16","packages/protocol-autonat":"2.0.15","packages/protocol-dcutr":"2.0.14","packages/protocol-echo":"2.1.5","packages/protocol-fetch":"2.0.14","packages/protocol-identify":"3.0.14","packages/protocol-perf":"4.0.16","packages/protocol-ping":"2.0.14","packages/pubsub":"10.0.14","packages/pubsub-floodsub":"10.1.14","packages/record":"4.0.4","packages/stream-multiplexer-mplex":"11.0.16","packages/transport-circuit-relay-v2":"3.1.6","packages/transport-memory":"1.0.2","packages/transport-tcp":"10.0.14","packages/transport-webrtc":"5.0.22","packages/transport-websockets":"9.1.1","packages/transport-webtransport":"5.0.21","packages/upnp-nat":"3.0.3","packages/utils":"6.3.0"} diff --git a/packages/transport-websockets/CHANGELOG.md b/packages/transport-websockets/CHANGELOG.md index d43ad42131..9259ef6b6d 100644 --- a/packages/transport-websockets/CHANGELOG.md +++ b/packages/transport-websockets/CHANGELOG.md @@ -70,6 +70,13 @@ * devDependencies * @libp2p/interface-compliance-tests bumped from ^5.3.0 to ^5.3.1 +## [9.1.1](https://github.com/libp2p/js-libp2p/compare/websockets-v9.1.0...websockets-v9.1.1) (2025-01-06) + + +### Bug Fixes + +* return empty address list during listen operation ([#2904](https://github.com/libp2p/js-libp2p/issues/2904)) ([ae75570](https://github.com/libp2p/js-libp2p/commit/ae75570c852fa9bfe00910dca1c219fa585dcb83)), closes [#2902](https://github.com/libp2p/js-libp2p/issues/2902) + ## [9.1.0](https://github.com/libp2p/js-libp2p/compare/websockets-v9.0.13...websockets-v9.1.0) (2024-12-09) diff --git a/packages/transport-websockets/package.json b/packages/transport-websockets/package.json index 9b1a070c17..328128dffc 100644 --- a/packages/transport-websockets/package.json +++ b/packages/transport-websockets/package.json @@ -1,6 +1,6 @@ { "name": "@libp2p/websockets", - "version": "9.1.0", + "version": "9.1.1", "description": "JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport spec", "license": "Apache-2.0 OR MIT", "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/transport-websockets#readme",