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

View File

@@ -309,14 +309,26 @@ export const WS_ERROR_ENUM = {
/** /**
* #305: ws.terminate() is undefined in browsers. * #305: ws.terminate() is undefined in browsers.
* This only works in node.js, not 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) { export function safeTerminateWs(
// #305: ws.terminate() undefined in browsers // eslint-disable-next-line @typescript-eslint/no-explicit-any
if (ws && typeof ws['terminate'] === 'function') { ws?: WebSocket | any,
ws.terminate(); 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 * WS API promises are stored using a primary key. This key is constructed using
* properties found in every request & reply. * properties found in every request & reply.