bybit-api 4.5.3 → 4.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import EventEmitter from 'events';
2
2
  import WebSocket from 'isomorphic-ws';
3
- import { MessageEventLike, WebsocketClientOptions, WSClientConfigurableOptions, WsMarket } from '../types';
3
+ import { MessageEventLike, WebsocketClientOptions, WSClientConfigurableOptions, WsMarket, WsTopic } from '../types';
4
4
  import { WsOperation } from '../types/websockets/ws-api';
5
5
  import { DefaultLogger } from './logger';
6
6
  import { WSConnectedResult, WsTopicRequest, WsTopicRequestOrStringTopic } from './websockets';
@@ -11,6 +11,8 @@ interface WSClientEventMap<WsKey extends string> {
11
11
  open: (evt: {
12
12
  wsKey: WsKey;
13
13
  event: any;
14
+ wsUrl: string;
15
+ ws: WebSocket;
14
16
  }) => void;
15
17
  /** Reconnecting a dropped connection */
16
18
  reconnect: (evt: {
@@ -21,6 +23,8 @@ interface WSClientEventMap<WsKey extends string> {
21
23
  reconnected: (evt: {
22
24
  wsKey: WsKey;
23
25
  event: any;
26
+ wsUrl: string;
27
+ ws: WebSocket;
24
28
  }) => void;
25
29
  /** Connection closed */
26
30
  close: (evt: {
@@ -55,15 +59,18 @@ interface WSClientEventMap<WsKey extends string> {
55
59
  isWSAPIResponse?: boolean;
56
60
  }) => void;
57
61
  }
62
+ /**
63
+ * Internally normalised structure for incoming WS events sorted into eventType categories
64
+ */
65
+ export interface EmittableEvent<TEventType extends keyof WSClientEventMap<string> = keyof WSClientEventMap<string>> {
66
+ eventType: TEventType | 'pong' | 'connectionReady' | 'connectionReadyForAuth';
67
+ event: Parameters<WSClientEventMap<string>[TEventType]>[0];
68
+ isWSAPIResponse?: boolean;
69
+ }
58
70
  export interface BaseWebsocketClient<TWSKey extends string, TWSRequestEvent extends object> {
59
71
  on<U extends keyof WSClientEventMap<TWSKey>>(event: U, listener: WSClientEventMap<TWSKey>[U]): this;
60
72
  emit<U extends keyof WSClientEventMap<TWSKey>>(event: U, ...args: Parameters<WSClientEventMap<TWSKey>[U]>): boolean;
61
73
  }
62
- export interface EmittableEvent<TEvent = any> {
63
- eventType: 'response' | 'update' | 'exception' | 'authenticated';
64
- event: TEvent;
65
- isWSAPIResponse?: boolean;
66
- }
67
74
  /**
68
75
  * A midflight WS request event (e.g. subscribe to these topics).
69
76
  *
@@ -86,33 +93,39 @@ TWSKey extends string, TWSRequestEvent extends object> extends EventEmitter {
86
93
  * State store to track a list of topics (topic requests) we are expected to be subscribed to if reconnected
87
94
  */
88
95
  private wsStore;
89
- logger: typeof DefaultLogger;
96
+ logger: DefaultLogger;
90
97
  protected options: WebsocketClientOptions;
91
98
  private wsApiRequestId;
92
99
  private timeOffsetMs;
100
+ private WS_LOGGER_CATEGORY;
93
101
  /**
94
102
  * A nested wsKey->request key store.
95
103
  * pendingTopicSubscriptionRequests[wsKey][requestKey] = WsKeyPendingTopicSubscriptions<TWSRequestEvent>
96
104
  */
97
105
  private pendingTopicSubscriptionRequests;
98
- constructor(options?: WSClientConfigurableOptions, logger?: typeof DefaultLogger);
106
+ constructor(options?: WSClientConfigurableOptions & {
107
+ wsLoggerCategory: string;
108
+ }, logger?: DefaultLogger);
99
109
  /**
100
110
  * Return true if this wsKey connection should automatically authenticate immediately after connecting
101
111
  */
102
112
  protected abstract isAuthOnConnectWsKey(wsKey: TWSKey): boolean;
113
+ protected abstract isCustomReconnectionNeeded(wsKey: TWSKey): boolean;
114
+ protected abstract triggerCustomReconnectionWorkflow(wsKey: TWSKey): Promise<void>;
103
115
  protected abstract sendPingEvent(wsKey: TWSKey, ws: WebSocket): void;
104
116
  protected abstract sendPongEvent(wsKey: TWSKey, ws: WebSocket): void;
105
117
  protected abstract isWsPing(data: any): boolean;
106
118
  protected abstract isWsPong(data: any): boolean;
107
- protected abstract getWsAuthRequestEvent(wsKey: TWSKey): Promise<object>;
108
- protected abstract isPrivateTopicRequest(request: WsTopicRequest<string>, wsKey: TWSKey): boolean;
119
+ protected abstract authPrivateConnectionsOnConnect(_wsKey: TWSKey): boolean;
120
+ protected abstract getWsAuthRequestEvent(wsKey: TWSKey, eventToAuth?: object): Promise<object | string | 'waitForEvent' | void>;
121
+ protected abstract isPrivateTopicRequest(request: WsTopicRequest<WsTopic>, wsKey: TWSKey): boolean;
109
122
  protected abstract getPrivateWSKeys(): TWSKey[];
110
123
  protected abstract getWsUrl(wsKey: TWSKey): Promise<string>;
111
124
  protected abstract getMaxTopicsPerSubscribeEvent(wsKey: TWSKey): number | null;
112
125
  /**
113
126
  * @returns one or more correctly structured request events for performing a operations over WS. This can vary per exchange spec.
114
127
  */
115
- protected abstract getWsRequestEvents(market: WsMarket, operation: WsOperation, requests: WsTopicRequest<string>[], wsKey: TWSKey): Promise<MidflightWsRequestEvent<TWSRequestEvent>[]>;
128
+ protected abstract getWsRequestEvents(market: WsMarket, operation: WsOperation, requests: WsTopicRequest<WsTopic>[], wsKey: TWSKey): Promise<MidflightWsRequestEvent<TWSRequestEvent>[]>;
116
129
  /**
117
130
  * Abstraction called to sort ws events into emittable event types (response to a request, data update, etc)
118
131
  */
@@ -158,7 +171,7 @@ TWSKey extends string, TWSRequestEvent extends object> extends EventEmitter {
158
171
  */
159
172
  private sortTopicRequestsIntoPublicPrivate;
160
173
  /** Get the WsStore that tracks websockets & topics */
161
- getWsStore(): WsStore<TWSKey, WsTopicRequest<string>>;
174
+ getWsStore(): WsStore<TWSKey, WsTopicRequest<WsTopic>>;
162
175
  close(wsKey: TWSKey, force?: boolean): void;
163
176
  closeAll(force?: boolean): void;
164
177
  isConnected(wsKey: TWSKey): boolean;
@@ -189,7 +202,7 @@ TWSKey extends string, TWSRequestEvent extends object> extends EventEmitter {
189
202
  *
190
203
  * Events are automatically split into smaller batches, by this method, if needed.
191
204
  */
192
- protected getWsOperationEventsForTopics(topics: WsTopicRequest<string>[], wsKey: TWSKey, operation: WsOperation): Promise<MidflightWsRequestEvent<TWSRequestEvent>[]>;
205
+ protected getWsOperationEventsForTopics(topics: WsTopicRequest<WsTopic>[], wsKey: TWSKey, operation: WsOperation): Promise<MidflightWsRequestEvent<TWSRequestEvent>[]>;
193
206
  /**
194
207
  * Simply builds and sends subscribe events for a list of topics for a ws key
195
208
  *
@@ -207,12 +220,23 @@ TWSKey extends string, TWSRequestEvent extends object> extends EventEmitter {
207
220
  */
208
221
  tryWsSend(wsKey: TWSKey, wsMessage: string, throwExceptions?: boolean): void;
209
222
  private onWsOpen;
223
+ private resolveConnectionInProgressPromise;
224
+ /**
225
+ * Called automatically once a connection is ready.
226
+ * - Some exchanges are ready immediately after the connections open.
227
+ * - Some exchanges send an event to confirm the connection is ready for us.
228
+ *
229
+ * This method is called to act when the connection is ready. Use `requireConnectionReadyConfirmation` to control how this is called.
230
+ */
231
+ private onWsReadyForEvents;
210
232
  /**
211
233
  * Handle subscription to private topics _after_ authentication successfully completes asynchronously.
212
234
  *
213
235
  * Only used for exchanges that require auth before sending private topic subscription requests
214
236
  */
215
237
  private onWsAuthenticated;
238
+ private onWsPing;
239
+ private onWsPong;
216
240
  private onWsMessage;
217
241
  private onWsClose;
218
242
  private getWs;