Skip to content

Commit

Permalink
fix: Use 4406 close code for unsupported subprotocol (1002 is an …
Browse files Browse the repository at this point in the history
…internal WebSocket close code)
  • Loading branch information
enisdenjo committed Aug 21, 2021
1 parent 9119153 commit df85281
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/interfaces/client.clientoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <id> already exists` _distinction is very important_
- `4429: Too many initialisation requests`

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/__tests__/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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();
});

Expand Down
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <id> already exists` _distinction is very important_
* - `4429: Too many initialisation requests`
*
Expand Down Expand Up @@ -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 <id> already exists (distinction is very important)
4429, // Too many initialisation requests
].includes(errOrCloseEvent.code)
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ export function makeServer<E = unknown>(options: ServerOptions<E>): Server<E> {
};

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);
Expand Down
2 changes: 1 addition & 1 deletion src/use/fastify-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. ` +
Expand Down
2 changes: 1 addition & 1 deletion src/use/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. ` +
Expand Down

0 comments on commit df85281

Please sign in to comment.