From df85281eceab1b6b53747efc83db90602051b8f8 Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Sat, 21 Aug 2021 12:05:35 +0200 Subject: [PATCH] fix: Use `4406` close code for unsupported subprotocol (`1002` is an internal WebSocket close code) --- docs/interfaces/client.clientoptions.md | 2 +- src/__tests__/client.ts | 2 +- src/__tests__/use.ts | 12 ++++++------ src/client.ts | 4 ++-- src/server.ts | 2 +- src/use/fastify-websocket.ts | 2 +- src/use/ws.ts | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/interfaces/client.clientoptions.md b/docs/interfaces/client.clientoptions.md index 1126777f..0f94827e 100644 --- a/docs/interfaces/client.clientoptions.md +++ b/docs/interfaces/client.clientoptions.md @@ -166,10 +166,10 @@ ___ How many times should the client try to reconnect on abnormal socket closure before it errors out? The library classifies the following close events as fatal: -- `1002: Protocol Error` - `1011: Internal Error` - `4400: Bad Request` - `4401: Unauthorized` _tried subscribing before connect ack_ +- `4406: Subprotocol not acceptable` - `4409: Subscriber for already exists` _distinction is very important_ - `4429: Too many initialisation requests` diff --git a/src/__tests__/client.ts b/src/__tests__/client.ts index fd22209b..1c103a2d 100644 --- a/src/__tests__/client.ts +++ b/src/__tests__/client.ts @@ -1209,7 +1209,7 @@ describe('reconnecting', () => { console.warn = () => { /* hide warnings for test */ }; - await testCloseCode(1002); + await testCloseCode(4406); console.warn = warn; await testCloseCode(1011); await testCloseCode(4400); diff --git a/src/__tests__/use.ts b/src/__tests__/use.ts index 9652d0e2..0d9aacb0 100644 --- a/src/__tests__/use.ts +++ b/src/__tests__/use.ts @@ -28,15 +28,15 @@ for (const { tServer, startTServer } of tServers) { let client = await createTClient(url, 'notme'); await client.waitForClose((event) => { - expect(event.code).toBe(1002); - expect(event.reason).toBe('Protocol Error'); + expect(event.code).toBe(4406); + expect(event.reason).toBe('Subprotocol not acceptable'); expect(event.wasClean).toBeTruthy(); }); client = await createTClient(url, ['graphql', 'json']); await client.waitForClose((event) => { - expect(event.code).toBe(1002); - expect(event.reason).toBe('Protocol Error'); + expect(event.code).toBe(4406); + expect(event.reason).toBe('Subprotocol not acceptable'); expect(event.wasClean).toBeTruthy(); }); @@ -45,8 +45,8 @@ for (const { tServer, startTServer } of tServers) { GRAPHQL_TRANSPORT_WS_PROTOCOL + 'gibberish', ); await client.waitForClose((event) => { - expect(event.code).toBe(1002); - expect(event.reason).toBe('Protocol Error'); + expect(event.code).toBe(4406); + expect(event.reason).toBe('Subprotocol not acceptable'); expect(event.wasClean).toBeTruthy(); }); diff --git a/src/client.ts b/src/client.ts index a78977ad..c8df4dd4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -314,10 +314,10 @@ export interface ClientOptions { * How many times should the client try to reconnect on abnormal socket closure before it errors out? * * The library classifies the following close events as fatal: - * - `1002: Protocol Error` * - `1011: Internal Error` * - `4400: Bad Request` * - `4401: Unauthorized` _tried subscribing before connect ack_ + * - `4406: Subprotocol not acceptable` * - `4409: Subscriber for already exists` _distinction is very important_ * - `4429: Too many initialisation requests` * @@ -710,10 +710,10 @@ export function createClient(options: ClientOptions): Client { if ( isLikeCloseEvent(errOrCloseEvent) && [ - 1002, // Protocol Error 1011, // Internal Error 4400, // Bad Request 4401, // Unauthorized (tried subscribing before connect ack) + 4406, // Subprotocol not acceptable 4409, // Subscriber for already exists (distinction is very important) 4429, // Too many initialisation requests ].includes(errOrCloseEvent.code) diff --git a/src/server.ts b/src/server.ts index fbfeac47..6d3c6253 100644 --- a/src/server.ts +++ b/src/server.ts @@ -544,7 +544,7 @@ export function makeServer(options: ServerOptions): Server { }; if (socket.protocol !== GRAPHQL_TRANSPORT_WS_PROTOCOL) { - socket.close(1002, 'Protocol Error'); + socket.close(4406, 'Subprotocol not acceptable'); return async (code, reason) => { /* nothing was set up, just notify the closure */ await onClose?.(ctx, code, reason); diff --git a/src/use/fastify-websocket.ts b/src/use/fastify-websocket.ts index 474194a0..cb1fd18a 100644 --- a/src/use/fastify-websocket.ts +++ b/src/use/fastify-websocket.ts @@ -98,7 +98,7 @@ export function makeHandler< socket.once('close', (code, reason) => { if (pongWait) clearTimeout(pongWait); if (pingInterval) clearInterval(pingInterval); - if (!isProd && code === 1002) + if (!isProd && code === 4406) console.warn( `WebSocket protocol error occured. It was most likely caused due to an ` + `unsupported subprotocol "${socket.protocol}" requested by the client. ` + diff --git a/src/use/ws.ts b/src/use/ws.ts index 31f787f0..d0726bd6 100644 --- a/src/use/ws.ts +++ b/src/use/ws.ts @@ -112,7 +112,7 @@ export function useServer< socket.once('close', (code, reason) => { if (pongWait) clearTimeout(pongWait); if (pingInterval) clearInterval(pingInterval); - if (!isProd && code === 1002) + if (!isProd && code === 4406) console.warn( `WebSocket protocol error occured. It was most likely caused due to an ` + `unsupported subprotocol "${socket.protocol}" requested by the client. ` +