fix(v2.3.3): fallback to ws.close in browsers

This commit is contained in:
Tiago Siebler
2025-02-24 11:49:58 +00:00
parent 2faddea7c2
commit 0f8815e426
5 changed files with 31 additions and 8 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "bitget-api", "name": "bitget-api",
"version": "2.3.2", "version": "2.3.3",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "bitget-api", "name": "bitget-api",
"version": "2.3.2", "version": "2.3.3",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^1.6.1", "axios": "^1.6.1",

View File

@@ -1,6 +1,6 @@
{ {
"name": "bitget-api", "name": "bitget-api",
"version": "2.3.2", "version": "2.3.3",
"description": "Node.js & JavaScript SDK for Bitget REST APIs & WebSockets, with TypeScript & end-to-end tests.", "description": "Node.js & JavaScript SDK for Bitget REST APIs & WebSockets, with TypeScript & end-to-end tests.",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",

View File

@@ -8,7 +8,7 @@ import {
} from '../types/index'; } from '../types/index';
import { DefaultLogger } from './logger'; import { DefaultLogger } from './logger';
import { isWsPong } from './requestUtils'; import { isWsPong } from './requestUtils';
import { getWsAuthSignature } from './websocket-util'; import { getWsAuthSignature, safeTerminateWs } from './websocket-util';
import WsStore from './WsStore'; import WsStore from './WsStore';
import { WsConnectionStateEnum } from './WsStore.types'; import { WsConnectionStateEnum } from './WsStore.types';
@@ -191,7 +191,7 @@ export abstract class BaseWebsocketClient<
const ws = this.getWs(wsKey); const ws = this.getWs(wsKey);
ws?.close(); ws?.close();
if (force) { if (force) {
ws?.terminate(); safeTerminateWs(ws);
} }
} }
@@ -340,7 +340,7 @@ export abstract class BaseWebsocketClient<
...LOGGER_CATEGORY, ...LOGGER_CATEGORY,
wsKey, wsKey,
}); });
this.getWs(wsKey)?.terminate(); safeTerminateWs(this.getWs(wsKey), true);
delete this.wsStore.get(wsKey, true).activePongTimer; delete this.wsStore.get(wsKey, true).activePongTimer;
}, this.options.pongTimeout); }, this.options.pongTimeout);
} }

View File

@@ -178,3 +178,25 @@ export async function getWsAuthSignature(
signature, signature,
}; };
} }
/**
* #305: ws.terminate() is undefined in browsers.
* This only works in node.js, not in browsers.
* Does nothing if `ws` is undefined. Does nothing in browsers.
*/
export function safeTerminateWs(
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;
}

View File

@@ -18,6 +18,7 @@ import {
isPrivateChannel, isPrivateChannel,
isWsPong, isWsPong,
neverGuard, neverGuard,
safeTerminateWs,
WS_AUTH_ON_CONNECT_KEYS, WS_AUTH_ON_CONNECT_KEYS,
WS_BASE_URL_MAP, WS_BASE_URL_MAP,
WS_KEY_MAP, WS_KEY_MAP,
@@ -185,7 +186,7 @@ export class WebsocketClient extends EventEmitter {
const ws = this.getWs(wsKey); const ws = this.getWs(wsKey);
ws?.close(); ws?.close();
if (force) { if (force) {
ws?.terminate(); safeTerminateWs(ws);
} }
} }
@@ -341,7 +342,7 @@ export class WebsocketClient extends EventEmitter {
...LOGGER_CATEGORY, ...LOGGER_CATEGORY,
wsKey, wsKey,
}); });
this.getWs(wsKey)?.terminate(); safeTerminateWs(this.getWs(wsKey), true);
delete this.wsStore.get(wsKey, true).activePongTimer; delete this.wsStore.get(wsKey, true).activePongTimer;
}, this.options.pongTimeout); }, this.options.pongTimeout);
} }