From dd80e2a474a680cdd982215b16c927142f94ffa5 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Sat, 21 Nov 2026 10:44:14 +0100 Subject: [PATCH] fix(ws): drop a frame that is not JSON instead of throwing (0.4.1) A malformed frame threw out of the message listener as an uncaught error (learn-atc GlitchTip #6084). The sendTimeout test's warmup send gets its own timeout, so a slow first connect no longer fails it. Co-Authored-By: Claude Opus 5.5 --- package-lock.json | 4 ++-- package.json | 2 +- src/ws.ts | 22 +++++++++++++++++++--- test/ws.test.mjs | 21 ++++++++++++++++++++- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index b32416a..3124ebb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@blax-software/networking", - "version": "0.4.0", + "version": "0.4.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@blax-software/networking", - "version": "0.4.0", + "version": "0.4.1", "license": "MIT", "devDependencies": { "axios": "^1.7.0", diff --git a/package.json b/package.json index e534141..739de6f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@blax-software/networking", - "version": "0.4.0", + "version": "0.4.1", "description": "Plug-and-play API + WebSocket client. Framework-agnostic core with optional Vue, Nuxt, and React bindings.", "type": "module", "main": "./dist/index.cjs", diff --git a/src/ws.ts b/src/ws.ts index f75058c..af90c3b 100644 --- a/src/ws.ts +++ b/src/ws.ts @@ -92,6 +92,16 @@ export interface WsClient { const _isProtocolEvent = (event: string): boolean => /[.:](?:subscribe|unsubscribe|ping|pong)$/.test(event) +/** JSON.parse, or undefined when the text is not JSON. */ +const _parseJson = (text: unknown): any => { + if (typeof text !== 'string') return undefined + try { + return JSON.parse(text) + } catch { + return undefined + } +} + // --------------------------------------------------------------------------- // SSR stub — safe no-op returned when isServer is true // --------------------------------------------------------------------------- @@ -447,10 +457,16 @@ class WsClientImpl extends EventTarget implements WsClient { }) socket.addEventListener('message', (raw) => { - const msg = JSON.parse(raw.data) + // A frame that is not JSON (cut off on the way, or not from our server) is + // dropped. JSON.parse threw it out of this listener as an uncaught error. + const msg = _parseJson(raw.data) + if (!msg || typeof msg !== 'object') { + console.warn('[blax-networking] Dropped a WebSocket frame that is not JSON') + return + } - if (msg?.event === 'websocket.connection_established') { - const data = JSON.parse(msg.data) + if (msg.event === 'websocket.connection_established') { + const data = typeof msg.data === 'string' ? _parseJson(msg.data) : msg.data if (data?.socket_id && this.socket === socket) { this.socket.socket_id = data.socket_id this.is_connecting_socket.value = false diff --git a/test/ws.test.mjs b/test/ws.test.mjs index 4e51c26..bc462fd 100644 --- a/test/ws.test.mjs +++ b/test/ws.test.mjs @@ -243,7 +243,8 @@ test('sendTimeout: a per-call timeout works without a client default, and timeou test('sendTimeout: a send that timed out before it was written never goes out after the reconnect', async () => { const ws = client({ sendTimeout: 10 }) ws.setAppReady() - await within(ws.send('dash.index')) + // The first connect alone can take over 10 ms on a busy machine. + await within(ws.send('dash.index', {}, null, { timeout: 500 })) FakeWS.sockets[0].drop() const r = await within(ws.send('late.call').then(() => 'RESOLVED', (e) => e)) assert.equal(r?.code, 'timeout') @@ -263,6 +264,24 @@ test('progress: the 4th argument still takes a callback, or { progress }', async assert.deepEqual(seen, [['fn', { step: 1 }], ['opt', { step: 1 }]]) }) +test('a frame that is not JSON is dropped and the connection keeps working', async () => { + const ws = client() + ws.setAppReady() + await within(ws.send('dash.index')) + const warn = console.warn + const warned = [] + console.warn = (...a) => warned.push(a.join(' ')) + try { + // Thrown from the message listener, this used to surface as an uncaught error. + ws.socket.dispatchEvent(new MessageEvent('message', { data: '{"event" "cut off' })) + ws.socket.dispatchEvent(new MessageEvent('message', { data: 'null' })) + } finally { + console.warn = warn + } + assert.equal(warned.length, 2) + assert.deepEqual(await within(ws.send('dash.index')), { ok: true, via: 0 }) +}) + // ── Translations ──────────────────────────────────────────────────────────── const toastsWith = async (translate) => {