chore(): update safeTerminateWs

This commit is contained in:
tiagosiebler
2025-03-04 11:22:16 +00:00
parent e65d7f0e07
commit 78e4025fe5
2 changed files with 19 additions and 7 deletions

View File

@@ -509,7 +509,7 @@ export abstract class BaseWebsocketClient<
const ws = this.getWs(wsKey);
ws?.close();
if (force) {
safeTerminateWs(ws);
safeTerminateWs(ws, false);
}
}
@@ -726,7 +726,7 @@ export abstract class BaseWebsocketClient<
if (ws) {
ws.close();
safeTerminateWs(ws);
safeTerminateWs(ws, false);
}
if (!wasOpen) {

View File

@@ -309,14 +309,26 @@ export const WS_ERROR_ENUM = {
/**
* #305: ws.terminate() is undefined in browsers.
* This only works in node.js, not in browsers.
* Does nothing if `ws` is undefined.
* Does nothing if `ws` is undefined. Does nothing in browsers.
*/
export function safeTerminateWs(ws?: WebSocket | unknown) {
// #305: ws.terminate() undefined in browsers
if (ws && typeof ws['terminate'] === 'function') {
ws.terminate();
export function safeTerminateWs(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ws?: WebSocket | any,
fallbackToClose?: boolean,
): boolean {
if (!ws) {
return false;
}
if (typeof ws['terminate'] === 'function') {
ws.terminate();
return true;
} else if (fallbackToClose) {
ws.close();
}
return false;
}
/**
* WS API promises are stored using a primary key. This key is constructed using
* properties found in every request & reply.