Skip to content

Commit 6a43657

Browse files
committed
fix(v3.0.5, #137): handling for mixed-auth connections should only auth once private topic is requested
1 parent d057de6 commit 6a43657

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
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": "okx-api",
3-
"version": "3.0.4",
3+
"version": "3.0.5",
44
"description": "Complete Node.js SDK for OKX's REST APIs and WebSockets, with TypeScript & end-to-end tests",
55
"scripts": {
66
"test": "jest",

src/util/BaseWSClient.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { DefaultLogger } from './logger.js';
1616
import { checkWebCryptoAPISupported } from './webCryptoAPI.js';
1717
import {
1818
getNormalisedTopicRequests,
19+
PRIVATE_CHANNELS,
1920
safeTerminateWs,
2021
WS_LOGGER_CATEGORY,
2122
WsTopicRequest,
@@ -330,8 +331,22 @@ export abstract class BaseWebsocketClient<
330331

331332
// We're connected. Check if auth is needed and if already authenticated
332333
const isPrivateConnection = this.isPrivateWsKey(wsKey);
333-
const isAuthenticated = this.wsStore.get(wsKey)?.isAuthenticated;
334-
if (isPrivateConnection && !isAuthenticated) {
334+
const isAuthenticatedBeforeTopicCheck =
335+
this.wsStore.get(wsKey)?.isAuthenticated;
336+
337+
if (!isAuthenticatedBeforeTopicCheck && !isPrivateConnection) {
338+
// Connection hasn't been authenticated yet, do a quick check if any topics we're about to subscribe to require auth
339+
// Queue immediate auth if so
340+
for (const topicRequest of normalisedTopicRequests) {
341+
if (PRIVATE_CHANNELS.includes(topicRequest.topic)) {
342+
await this.assertIsAuthenticated(wsKey);
343+
break;
344+
}
345+
}
346+
}
347+
348+
const isFinallyAuthenticated = this.wsStore.get(wsKey)?.isAuthenticated;
349+
if (isPrivateConnection && !isFinallyAuthenticated) {
335350
/**
336351
* If not authenticated yet and auth is required, don't request topics yet.
337352
*

src/util/websocket-util.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,20 @@ export type WsKey = (typeof WS_KEY_MAP)[keyof typeof WS_KEY_MAP];
148148

149149
export const PRIVATE_WS_KEYS: WsKey[] = [
150150
WS_KEY_MAP.prodPrivate,
151-
WS_KEY_MAP.prodBusiness,
152151
WS_KEY_MAP.prodDemoPrivate,
153-
WS_KEY_MAP.prodDemoBusiness,
154152
WS_KEY_MAP.eeaLivePrivate,
155-
WS_KEY_MAP.eeaLiveBusiness,
156153
WS_KEY_MAP.eeaDemoPrivate,
157-
WS_KEY_MAP.eeaDemoBusiness,
158154
WS_KEY_MAP.usLivePrivate,
159-
WS_KEY_MAP.usLiveBusiness,
160155
WS_KEY_MAP.usDemoPrivate,
156+
];
157+
158+
// These sometimes need auth, depending on the topic
159+
export const MIXED_WS_KEYS: WsKey[] = [
160+
WS_KEY_MAP.prodBusiness,
161+
WS_KEY_MAP.prodDemoBusiness,
162+
WS_KEY_MAP.eeaLiveBusiness,
163+
WS_KEY_MAP.eeaDemoBusiness,
164+
WS_KEY_MAP.usLiveBusiness,
161165
WS_KEY_MAP.usDemoBusiness,
162166
];
163167

@@ -214,7 +218,7 @@ export function getDemoWsKey(wsKey: WsKey): WsKey {
214218
}
215219

216220
/** Used to automatically determine if a sub request should be to the public or private ws (when there's two) */
217-
const PRIVATE_CHANNELS = [
221+
export const PRIVATE_CHANNELS = [
218222
'account',
219223
'positions',
220224
'balance_and_position',

0 commit comments

Comments
 (0)