From c72a2803611610280a60a6b069f82995e33bc78f Mon Sep 17 00:00:00 2001 From: Vantz Stockwell Date: Thu, 11 Jun 2026 11:11:29 -0400 Subject: [PATCH] =?UTF-8?q?fix(api):=20WS=20gateways=20crashed=20on=20firs?= =?UTF-8?q?t=20forwarded=20event=20=E2=80=94=20WebSocket.OPEN=20undefined?= =?UTF-8?q?=20at=20runtime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lesson 10 in the flesh: the onApplicationBootstrap fix made the NATS-> WS bridge actually deliver events for the first time, which instantly crashed the API. esModuleInterop is off, so 'import WebSocket from ws' compiles to ws_1.default = undefined; WebSocket.OPEN threw 'Cannot read properties of undefined' and killed the process on the first heartbeat forward. All three WS guard sites (nats-bridge x2, console gateway) switched to the import-agnostic instance constant client.OPEN. Latent in every build — never hit because the bridge was dead-on-boot until today. Co-Authored-By: Claude Fable 5 --- backend-nest/src/gateways/nats-bridge.gateway.ts | 5 ++++- backend-nest/src/modules/console/console.gateway.ts | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/backend-nest/src/gateways/nats-bridge.gateway.ts b/backend-nest/src/gateways/nats-bridge.gateway.ts index 045c584..461f992 100644 --- a/backend-nest/src/gateways/nats-bridge.gateway.ts +++ b/backend-nest/src/gateways/nats-bridge.gateway.ts @@ -71,7 +71,10 @@ export class NatsBridgeGateway implements OnGatewayConnection, OnGatewayDisconne // Subscribe to NATS events for this license const listener = (event: string, data: unknown) => { - if (client.readyState === WebSocket.OPEN) { + // client.OPEN (instance constant) — NOT WebSocket.OPEN: with + // esModuleInterop off, the default `ws` import is undefined at + // runtime, so the static crashes. The instance constant is safe. + if (client.readyState === client.OPEN) { client.send(JSON.stringify({ type: 'event', license_id: payload.license_id, diff --git a/backend-nest/src/modules/console/console.gateway.ts b/backend-nest/src/modules/console/console.gateway.ts index 43b2fa7..34e2ed5 100644 --- a/backend-nest/src/modules/console/console.gateway.ts +++ b/backend-nest/src/modules/console/console.gateway.ts @@ -108,7 +108,9 @@ export class ConsoleGateway implements OnGatewayConnection, OnGatewayDisconnect const message = JSON.stringify({ event, data }); for (const client of clients) { - if (client.readyState === WebSocket.OPEN) { + // client.OPEN, not WebSocket.OPEN — esModuleInterop is off so the + // default `ws` import is undefined at runtime (would crash on forward). + if (client.readyState === client.OPEN) { client.send(message); } }