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 <noreply@anthropic.com>
This commit is contained in:
parent
9ceb774ad6
commit
dd80e2a474
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@blax-software/networking",
|
"name": "@blax-software/networking",
|
||||||
"version": "0.4.0",
|
"version": "0.4.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@blax-software/networking",
|
"name": "@blax-software/networking",
|
||||||
"version": "0.4.0",
|
"version": "0.4.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"axios": "^1.7.0",
|
"axios": "^1.7.0",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@blax-software/networking",
|
"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.",
|
"description": "Plug-and-play API + WebSocket client. Framework-agnostic core with optional Vue, Nuxt, and React bindings.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/index.cjs",
|
"main": "./dist/index.cjs",
|
||||||
|
|
|
||||||
22
src/ws.ts
22
src/ws.ts
|
|
@ -92,6 +92,16 @@ export interface WsClient {
|
||||||
const _isProtocolEvent = (event: string): boolean =>
|
const _isProtocolEvent = (event: string): boolean =>
|
||||||
/[.:](?:subscribe|unsubscribe|ping|pong)$/.test(event)
|
/[.:](?: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
|
// SSR stub — safe no-op returned when isServer is true
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
@ -447,10 +457,16 @@ class WsClientImpl extends EventTarget implements WsClient {
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.addEventListener('message', (raw) => {
|
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') {
|
if (msg.event === 'websocket.connection_established') {
|
||||||
const data = JSON.parse(msg.data)
|
const data = typeof msg.data === 'string' ? _parseJson(msg.data) : msg.data
|
||||||
if (data?.socket_id && this.socket === socket) {
|
if (data?.socket_id && this.socket === socket) {
|
||||||
this.socket.socket_id = data.socket_id
|
this.socket.socket_id = data.socket_id
|
||||||
this.is_connecting_socket.value = false
|
this.is_connecting_socket.value = false
|
||||||
|
|
|
||||||
|
|
@ -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 () => {
|
test('sendTimeout: a send that timed out before it was written never goes out after the reconnect', async () => {
|
||||||
const ws = client({ sendTimeout: 10 })
|
const ws = client({ sendTimeout: 10 })
|
||||||
ws.setAppReady()
|
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()
|
FakeWS.sockets[0].drop()
|
||||||
const r = await within(ws.send('late.call').then(() => 'RESOLVED', (e) => e))
|
const r = await within(ws.send('late.call').then(() => 'RESOLVED', (e) => e))
|
||||||
assert.equal(r?.code, 'timeout')
|
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 }]])
|
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 ────────────────────────────────────────────────────────────
|
// ── Translations ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
const toastsWith = async (translate) => {
|
const toastsWith = async (translate) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue