Skip to content

Commit

Permalink
fix(client): Specify and fail on fatal internal WebSocket close codes
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Aug 21, 2021
1 parent d10a75c commit a720125
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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' &&
Expand Down

0 comments on commit a720125

Please sign in to comment.