From 2245ac516b46e4b6720635a97654e29a6b4b79e6 Mon Sep 17 00:00:00 2001 From: Evan Summers Date: Thu, 1 Aug 2024 12:29:24 +0200 Subject: [PATCH] fix: Add changeUrl method to AutoReconnectingWebsocket --- package-lock.json | 24 +++++++++++++++++++++++- package.json | 3 ++- src/lib/AutoReconnectingWebsocket.ts | 25 +++++++++++++++++-------- test/setup.ts | 2 ++ vitest.config.ts | 6 +++++- 5 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 test/setup.ts diff --git a/package-lock.json b/package-lock.json index 9032a11..bad6f12 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,8 @@ "semantic-release": "^24.0.0", "tsup": "^8.1.0", "typescript": "^5.5.3", - "vitest": "^2.0.2" + "vitest": "^2.0.2", + "ws": "^8.18.0" } }, "node_modules/@ampproject/remapping": { @@ -10080,6 +10081,27 @@ "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "dev": true }, + "node_modules/ws": { + "version": "8.18.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", + "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", + "dev": true, + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/package.json b/package.json index 26bdd8e..b87405d 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "semantic-release": "^24.0.0", "tsup": "^8.1.0", "typescript": "^5.5.3", - "vitest": "^2.0.2" + "vitest": "^2.0.2", + "ws": "^8.18.0" }, "dependencies": { "@wandelbots/wandelbots-api-client": "^0.97.0", diff --git a/src/lib/AutoReconnectingWebsocket.ts b/src/lib/AutoReconnectingWebsocket.ts index e66510a..e262c19 100644 --- a/src/lib/AutoReconnectingWebsocket.ts +++ b/src/lib/AutoReconnectingWebsocket.ts @@ -13,10 +13,6 @@ export class AutoReconnectingWebsocket extends ReconnectingWebSocket { startClosed: true, }) - // reconnecting-websocket doesn't set this properly with startClosed - // so we do it ourselves - Object.defineProperty(this, "url", { value: url }) - this.addEventListener("open", () => { console.log(`Websocket to ${url} opened`) }) @@ -25,11 +21,24 @@ export class AutoReconnectingWebsocket extends ReconnectingWebSocket { console.log(`Websocket to ${url} closed`) }) - if (this.opts.mock) { - this.opts.mock.handleWebsocketConnection(this) - } else { - this.reconnect() + const origReconnect = this.reconnect + this.reconnect = () => { + if (this.opts.mock) { + this.opts.mock.handleWebsocketConnection(this) + } else { + this.reconnect() + } + origReconnect.apply(this) } + + this.changeUrl(url) + } + + changeUrl(url: string) { + // reconnecting-websocket doesn't set this properly with startClosed + // so we do it ourselves + Object.defineProperty(this, "url", { value: url, configurable: true }) + this.reconnect() } sendJson(data: unknown) { diff --git a/test/setup.ts b/test/setup.ts new file mode 100644 index 0000000..dbcfe5f --- /dev/null +++ b/test/setup.ts @@ -0,0 +1,2 @@ +import ws from "ws" +globalThis.WebSocket = ws diff --git a/vitest.config.ts b/vitest.config.ts index 17d3638..67bf718 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,4 +1,8 @@ /// import { defineConfig } from "vite" -export default defineConfig({}) +export default defineConfig({ + test: { + setupFiles: ["./test/setup.ts"], + }, +})