chore(): misc cleaning & note around exception emitter, fix(#411): only resolve demo trading url for private topics
This commit is contained in:
@@ -38,8 +38,17 @@ interface WSClientEventMap<WsKey extends string> {
|
|||||||
) => void;
|
) => void;
|
||||||
/** Received data for topic */
|
/** Received data for topic */
|
||||||
update: (response: any & { wsKey: WsKey }) => void;
|
update: (response: any & { wsKey: WsKey }) => void;
|
||||||
/** Exception from ws client OR custom listeners (e.g. if you throw inside your event handler) */
|
/**
|
||||||
|
* Exception from ws client OR custom listeners (e.g. if you throw inside your event handler)
|
||||||
|
* @deprecated Use 'exception' instead. The 'error' event had the unintended consequence of throwing an unhandled promise rejection.
|
||||||
|
*/
|
||||||
error: (response: any & { wsKey: WsKey; isWSAPIResponse?: boolean }) => void;
|
error: (response: any & { wsKey: WsKey; isWSAPIResponse?: boolean }) => void;
|
||||||
|
/**
|
||||||
|
* Exception from ws client OR custom listeners (e.g. if you throw inside your event handler)
|
||||||
|
*/
|
||||||
|
exception: (
|
||||||
|
response: any & { wsKey: WsKey; isWSAPIResponse?: boolean },
|
||||||
|
) => void;
|
||||||
/** Confirmation that a connection successfully authenticated */
|
/** Confirmation that a connection successfully authenticated */
|
||||||
authenticated: (event: {
|
authenticated: (event: {
|
||||||
wsKey: WsKey;
|
wsKey: WsKey;
|
||||||
@@ -71,14 +80,6 @@ export interface BaseWebsocketClient<
|
|||||||
): boolean;
|
): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// interface TopicsPendingSubscriptions {
|
|
||||||
// wsKey: string;
|
|
||||||
// failedTopicsSubscriptions: Set<string>;
|
|
||||||
// pendingTopicsSubscriptions: Set<string>;
|
|
||||||
// resolver: TopicsPendingSubscriptionsResolver;
|
|
||||||
// rejector: TopicsPendingSubscriptionsRejector;
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A midflight WS request event (e.g. subscribe to these topics).
|
* A midflight WS request event (e.g. subscribe to these topics).
|
||||||
*
|
*
|
||||||
@@ -168,8 +169,10 @@ export abstract class BaseWebsocketClient<
|
|||||||
};
|
};
|
||||||
|
|
||||||
// add default error handling so this doesn't crash node (if the user didn't set a handler)
|
// add default error handling so this doesn't crash node (if the user didn't set a handler)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars, no-unused-vars
|
||||||
this.on('error', () => {});
|
this.on('error', (e) => {
|
||||||
|
// console.log('basewserr: ', e);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -339,10 +342,18 @@ export abstract class BaseWebsocketClient<
|
|||||||
pendingSubscriptionRequest.requestData,
|
pendingSubscriptionRequest.requestData,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
this.logger.trace(
|
||||||
|
`updatePendingTopicSubscriptionStatus.reject(${wsKey}, ${requestKey}, ${msg}, ${isTopicSubscriptionSuccessEvent}): `,
|
||||||
|
msg,
|
||||||
|
);
|
||||||
|
try {
|
||||||
pendingSubscriptionRequest.rejector(
|
pendingSubscriptionRequest.rejector(
|
||||||
pendingSubscriptionRequest.requestData,
|
pendingSubscriptionRequest.requestData,
|
||||||
msg,
|
msg,
|
||||||
);
|
);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Exception rejecting promise: ', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.removeTopicPendingSubscription(wsKey, requestKey);
|
this.removeTopicPendingSubscription(wsKey, requestKey);
|
||||||
@@ -614,6 +625,8 @@ export abstract class BaseWebsocketClient<
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.error(`parseWsError(${context}, ${error}, ${wsKey}) `, error);
|
||||||
|
|
||||||
this.emit('response', { ...error, wsKey });
|
this.emit('response', { ...error, wsKey });
|
||||||
this.emit('error', { ...error, wsKey });
|
this.emit('error', { ...error, wsKey });
|
||||||
}
|
}
|
||||||
@@ -1008,12 +1021,28 @@ export abstract class BaseWebsocketClient<
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Request sub to public topics, if any
|
// Request sub to public topics, if any
|
||||||
this.requestSubscribeTopics(wsKey, publicReqs);
|
try {
|
||||||
|
await this.requestSubscribeTopics(wsKey, publicReqs);
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error(
|
||||||
|
`onWsOpen(): exception in public requestSubscribeTopics(${wsKey}): `,
|
||||||
|
publicReqs,
|
||||||
|
e,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Request sub to private topics, if auth on connect isn't needed
|
// Request sub to private topics, if auth on connect isn't needed
|
||||||
// Else, this is automatic after authentication is successfully confirmed
|
// Else, this is automatic after authentication is successfully confirmed
|
||||||
if (!this.options.authPrivateConnectionsOnConnect) {
|
if (!this.options.authPrivateConnectionsOnConnect) {
|
||||||
|
try {
|
||||||
this.requestSubscribeTopics(wsKey, privateReqs);
|
this.requestSubscribeTopics(wsKey, privateReqs);
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error(
|
||||||
|
`onWsOpen(): exception in private requestSubscribeTopics(${wsKey}: `,
|
||||||
|
privateReqs,
|
||||||
|
e,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some websockets require an auth packet to be sent after opening the connection
|
// Some websockets require an auth packet to be sent after opening the connection
|
||||||
@@ -1114,10 +1143,6 @@ export abstract class BaseWebsocketClient<
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const emittable of emittableEvents) {
|
for (const emittable of emittableEvents) {
|
||||||
// if (emittable.event?.op) {
|
|
||||||
// console.log('emittable: ', emittable);
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (this.isWsPong(emittable)) {
|
if (this.isWsPong(emittable)) {
|
||||||
this.logger.trace('Received pong2', {
|
this.logger.trace('Received pong2', {
|
||||||
...WS_LOGGER_CATEGORY,
|
...WS_LOGGER_CATEGORY,
|
||||||
@@ -1143,7 +1168,22 @@ export abstract class BaseWebsocketClient<
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this.logger.trace(
|
||||||
|
// `onWsMessage().emit(${emittable.eventType})`,
|
||||||
|
// emittableFinalEvent,
|
||||||
|
// );
|
||||||
|
try {
|
||||||
this.emit(emittable.eventType, emittableFinalEvent);
|
this.emit(emittable.eventType, emittableFinalEvent);
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error(
|
||||||
|
`Exception in onWsMessage().emit(${emittable.eventType}) handler:`,
|
||||||
|
e,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// this.logger.trace(
|
||||||
|
// `onWsMessage().emit(${emittable.eventType}).done()`,
|
||||||
|
// emittableFinalEvent,
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -241,19 +241,23 @@ export function getWsUrl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://bybit-exchange.github.io/docs/v5/demo
|
// https://bybit-exchange.github.io/docs/v5/demo
|
||||||
const isDemoTrading = wsClientOptions.demoTrading;
|
const demoTradingPrivateEndpoint = 'wss://stream-demo.bybit.com/v5/private';
|
||||||
if (isDemoTrading) {
|
|
||||||
return 'wss://stream-demo.bybit.com/v5/private';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const isDemoTrading = wsClientOptions.demoTrading;
|
||||||
const isTestnet = wsClientOptions.testnet;
|
const isTestnet = wsClientOptions.testnet;
|
||||||
const networkKey = isTestnet ? 'testnet' : 'livenet';
|
const networkKey = isTestnet ? 'testnet' : 'livenet';
|
||||||
|
|
||||||
switch (wsKey) {
|
switch (wsKey) {
|
||||||
case WS_KEY_MAP.v5Private: {
|
case WS_KEY_MAP.v5Private: {
|
||||||
|
if (isDemoTrading) {
|
||||||
|
return demoTradingPrivateEndpoint;
|
||||||
|
}
|
||||||
return WS_BASE_URL_MAP.v5.private[networkKey];
|
return WS_BASE_URL_MAP.v5.private[networkKey];
|
||||||
}
|
}
|
||||||
case WS_KEY_MAP.v5PrivateTrade: {
|
case WS_KEY_MAP.v5PrivateTrade: {
|
||||||
|
if (isDemoTrading) {
|
||||||
|
return demoTradingPrivateEndpoint;
|
||||||
|
}
|
||||||
return WS_BASE_URL_MAP[wsKey].private[networkKey];
|
return WS_BASE_URL_MAP[wsKey].private[networkKey];
|
||||||
}
|
}
|
||||||
case WS_KEY_MAP.v5SpotPublic: {
|
case WS_KEY_MAP.v5SpotPublic: {
|
||||||
|
|||||||
Reference in New Issue
Block a user