From a7201251ee4d090e2bd3e55e8e50dbc1501231a0 Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Sat, 21 Aug 2021 12:46:24 +0200 Subject: [PATCH] fix(client): Specify and fail on fatal internal WebSocket close codes --- src/client.ts | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/client.ts b/src/client.ts index d5743d82..16bddfde 100644 --- a/src/client.ts +++ b/src/client.ts @@ -710,16 +710,17 @@ export function createClient(options: ClientOptions): Client { // some close codes are worth reporting immediately if ( isLikeCloseEvent(errOrCloseEvent) && - [ - CloseCode.InternalServerError, - CloseCode.BadRequest, - CloseCode.Unauthorized, - // CloseCode.Forbidden, might grant access out after retry - CloseCode.SubprotocolNotAcceptable, - // CloseCode.ConnectionInitialisationTimeout, might not time out after retry - CloseCode.SubscriberAlreadyExists, - CloseCode.TooManyInitialisationRequests, - ].includes(errOrCloseEvent.code) + (isFatalInternalCloseCode(errOrCloseEvent.code) || + [ + CloseCode.InternalServerError, + CloseCode.BadRequest, + CloseCode.Unauthorized, + // CloseCode.Forbidden, might grant access out after retry + CloseCode.SubprotocolNotAcceptable, + // CloseCode.ConnectionInitialisationTimeout, might not time out after retry + CloseCode.SubscriberAlreadyExists, + CloseCode.TooManyInitialisationRequests, + ].includes(errOrCloseEvent.code)) ) throw errOrCloseEvent; @@ -878,6 +879,23 @@ function isLikeCloseEvent(val: unknown): val is LikeCloseEvent { return isObject(val) && 'code' in val && 'reason' in val; } +function isFatalInternalCloseCode(code: number): boolean { + if ( + [ + 1000, // Normal Closure is not an erroneous close code + 1001, // Going Away + 1006, // Abnormal Closure + 1005, // No Status Received + 1012, // Service Restart + 1013, // Try Again Later + 1013, // Bad Gateway + ].includes(code) + ) + return false; + // all other internal errors are fatal + return code >= 1000 && code <= 1999; +} + function isWebSocket(val: unknown): val is typeof WebSocket { return ( typeof val === 'function' &&