Skip to content

Commit 0f8815e

Browse files
committed
fix(v2.3.3): fallback to ws.close in browsers
1 parent 2faddea commit 0f8815e

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitget-api",
3-
"version": "2.3.2",
3+
"version": "2.3.3",
44
"description": "Node.js & JavaScript SDK for Bitget REST APIs & WebSockets, with TypeScript & end-to-end tests.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/util/BaseWSClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../types/index';
99
import { DefaultLogger } from './logger';
1010
import { isWsPong } from './requestUtils';
11-
import { getWsAuthSignature } from './websocket-util';
11+
import { getWsAuthSignature, safeTerminateWs } from './websocket-util';
1212
import WsStore from './WsStore';
1313
import { WsConnectionStateEnum } from './WsStore.types';
1414

@@ -191,7 +191,7 @@ export abstract class BaseWebsocketClient<
191191
const ws = this.getWs(wsKey);
192192
ws?.close();
193193
if (force) {
194-
ws?.terminate();
194+
safeTerminateWs(ws);
195195
}
196196
}
197197

@@ -340,7 +340,7 @@ export abstract class BaseWebsocketClient<
340340
...LOGGER_CATEGORY,
341341
wsKey,
342342
});
343-
this.getWs(wsKey)?.terminate();
343+
safeTerminateWs(this.getWs(wsKey), true);
344344
delete this.wsStore.get(wsKey, true).activePongTimer;
345345
}, this.options.pongTimeout);
346346
}

src/util/websocket-util.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,25 @@ export async function getWsAuthSignature(
178178
signature,
179179
};
180180
}
181+
182+
/**
183+
* #305: ws.terminate() is undefined in browsers.
184+
* This only works in node.js, not in browsers.
185+
* Does nothing if `ws` is undefined. Does nothing in browsers.
186+
*/
187+
export function safeTerminateWs(
188+
ws?: WebSocket | any,
189+
fallbackToClose?: boolean,
190+
): boolean {
191+
if (!ws) {
192+
return false;
193+
}
194+
if (typeof ws['terminate'] === 'function') {
195+
ws.terminate();
196+
return true;
197+
} else if (fallbackToClose) {
198+
ws.close();
199+
}
200+
201+
return false;
202+
}

src/websocket-client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
isPrivateChannel,
1919
isWsPong,
2020
neverGuard,
21+
safeTerminateWs,
2122
WS_AUTH_ON_CONNECT_KEYS,
2223
WS_BASE_URL_MAP,
2324
WS_KEY_MAP,
@@ -185,7 +186,7 @@ export class WebsocketClient extends EventEmitter {
185186
const ws = this.getWs(wsKey);
186187
ws?.close();
187188
if (force) {
188-
ws?.terminate();
189+
safeTerminateWs(ws);
189190
}
190191
}
191192

@@ -341,7 +342,7 @@ export class WebsocketClient extends EventEmitter {
341342
...LOGGER_CATEGORY,
342343
wsKey,
343344
});
344-
this.getWs(wsKey)?.terminate();
345+
safeTerminateWs(this.getWs(wsKey), true);
345346
delete this.wsStore.get(wsKey, true).activePongTimer;
346347
}, this.options.pongTimeout);
347348
}

0 commit comments

Comments
 (0)