binance 2.15.14 → 3.0.0-beta.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.
Files changed (100) hide show
  1. package/README.md +5 -2
  2. package/lib/coinm-client.js.map +1 -1
  3. package/lib/index.d.ts +10 -4
  4. package/lib/index.js +10 -4
  5. package/lib/index.js.map +1 -1
  6. package/lib/main-client.d.ts +13 -2
  7. package/lib/main-client.js +15 -1
  8. package/lib/main-client.js.map +1 -1
  9. package/lib/portfolio-client.js.map +1 -1
  10. package/lib/types/futures.d.ts +1 -0
  11. package/lib/types/futures.js +3 -3
  12. package/lib/types/futures.js.map +1 -1
  13. package/lib/types/spot.js +4 -4
  14. package/lib/types/spot.js.map +1 -1
  15. package/lib/types/websockets/ws-api-requests.d.ts +7 -0
  16. package/lib/types/websockets/ws-api-requests.js +3 -0
  17. package/lib/types/websockets/ws-api-requests.js.map +1 -0
  18. package/lib/types/websockets/ws-api-responses.d.ts +8 -0
  19. package/lib/types/websockets/ws-api-responses.js +3 -0
  20. package/lib/types/websockets/ws-api-responses.js.map +1 -0
  21. package/lib/types/websockets/ws-api.d.ts +201 -0
  22. package/lib/types/websockets/ws-api.js +29 -0
  23. package/lib/types/websockets/ws-api.js.map +1 -0
  24. package/lib/types/{websockets.d.ts → websockets/ws-events-formatted.d.ts} +4 -410
  25. package/lib/types/websockets/ws-events-formatted.js +3 -0
  26. package/lib/types/websockets/ws-events-formatted.js.map +1 -0
  27. package/lib/types/websockets/ws-events-raw.d.ts +401 -0
  28. package/lib/types/{websockets.js → websockets/ws-events-raw.js} +1 -1
  29. package/lib/types/websockets/ws-events-raw.js.map +1 -0
  30. package/lib/types/websockets/ws-general.d.ts +98 -0
  31. package/lib/types/websockets/ws-general.js +11 -0
  32. package/lib/types/websockets/ws-general.js.map +1 -0
  33. package/lib/usdm-client.js.map +1 -1
  34. package/lib/util/BaseRestClient.d.ts +1 -1
  35. package/lib/util/BaseRestClient.js +1 -1
  36. package/lib/util/BaseRestClient.js.map +1 -1
  37. package/lib/util/BaseWSClient.d.ts +225 -0
  38. package/lib/util/BaseWSClient.js +729 -0
  39. package/lib/util/BaseWSClient.js.map +1 -0
  40. package/lib/util/beautifier-maps.d.ts +151 -0
  41. package/lib/util/beautifier-maps.js +198 -37
  42. package/lib/util/beautifier-maps.js.map +1 -1
  43. package/lib/util/beautifier.d.ts +7 -3
  44. package/lib/util/beautifier.js +40 -11
  45. package/lib/util/beautifier.js.map +1 -1
  46. package/lib/util/browser-support.d.ts +2 -1
  47. package/lib/util/browser-support.js +46 -28
  48. package/lib/util/browser-support.js.map +1 -1
  49. package/lib/util/logger.d.ts +8 -0
  50. package/lib/util/logger.js +17 -0
  51. package/lib/util/logger.js.map +1 -0
  52. package/lib/util/node-support.d.ts +2 -1
  53. package/lib/util/node-support.js +35 -15
  54. package/lib/util/node-support.js.map +1 -1
  55. package/lib/util/requestUtils.d.ts +19 -19
  56. package/lib/util/requestUtils.js +119 -38
  57. package/lib/util/requestUtils.js.map +1 -1
  58. package/lib/util/typeGuards.d.ts +9 -1
  59. package/lib/util/typeGuards.js +59 -34
  60. package/lib/util/typeGuards.js.map +1 -1
  61. package/lib/util/usdm/exchangeInfo.js +2 -3
  62. package/lib/util/usdm/exchangeInfo.js.map +1 -1
  63. package/lib/util/webCryptoAPI.d.ts +14 -0
  64. package/lib/util/webCryptoAPI.js +120 -0
  65. package/lib/util/webCryptoAPI.js.map +1 -0
  66. package/lib/util/websockets/WsStore.d.ts +74 -0
  67. package/lib/util/websockets/WsStore.js +279 -0
  68. package/lib/util/websockets/WsStore.js.map +1 -0
  69. package/lib/util/websockets/WsStore.types.d.ts +53 -0
  70. package/lib/util/websockets/WsStore.types.js +14 -0
  71. package/lib/util/websockets/WsStore.types.js.map +1 -0
  72. package/lib/util/websockets/listen-key-state-cache.d.ts +21 -0
  73. package/lib/util/websockets/listen-key-state-cache.js +80 -0
  74. package/lib/util/websockets/listen-key-state-cache.js.map +1 -0
  75. package/lib/util/websockets/rest-client-cache.d.ts +13 -0
  76. package/lib/util/websockets/rest-client-cache.js +56 -0
  77. package/lib/util/websockets/rest-client-cache.js.map +1 -0
  78. package/lib/util/websockets/user-data-stream-manager.d.ts +54 -0
  79. package/lib/util/websockets/user-data-stream-manager.js +256 -0
  80. package/lib/util/websockets/user-data-stream-manager.js.map +1 -0
  81. package/lib/util/websockets/websocket-util.d.ts +124 -0
  82. package/lib/util/websockets/websocket-util.js +481 -0
  83. package/lib/util/websockets/websocket-util.js.map +1 -0
  84. package/lib/websocket-client-legacy.d.ts +288 -0
  85. package/lib/websocket-client-legacy.js +1113 -0
  86. package/lib/websocket-client-legacy.js.map +1 -0
  87. package/lib/websocket-client.d.ts +228 -168
  88. package/lib/websocket-client.js +927 -834
  89. package/lib/websocket-client.js.map +1 -1
  90. package/package.json +5 -5
  91. package/lib/logger.d.ts +0 -9
  92. package/lib/logger.js +0 -23
  93. package/lib/logger.js.map +0 -1
  94. package/lib/types/websockets.js.map +0 -1
  95. package/lib/util/WsStore.d.ts +0 -57
  96. package/lib/util/WsStore.js +0 -101
  97. package/lib/util/WsStore.js.map +0 -1
  98. package/lib/util/ws-utils.d.ts +0 -7
  99. package/lib/util/ws-utils.js +0 -16
  100. package/lib/util/ws-utils.js.map +0 -1
@@ -0,0 +1,225 @@
1
+ import EventEmitter from 'events';
2
+ import WebSocket from 'isomorphic-ws';
3
+ import { WsOperation } from '../types/websockets/ws-api';
4
+ import { MessageEventLike, WebsocketClientOptions, WSClientConfigurableOptions } from '../types/websockets/ws-general';
5
+ import { DefaultLogger } from './logger';
6
+ import { WsTopicRequest, WsTopicRequestOrStringTopic } from './websockets/websocket-util';
7
+ import { WsStore } from './websockets/WsStore';
8
+ import { WSConnectedResult } from './websockets/WsStore.types';
9
+ import { WsFormattedMessage, WsUserDataEvents } from '../types/websockets/ws-events-formatted';
10
+ import { WsRawMessage } from '../types/websockets/ws-events-raw';
11
+ type UseTheExceptionEventInstead = never;
12
+ interface WSClientEventMap<WsKey extends string> {
13
+ /** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
14
+ open: (evt: {
15
+ wsKey: WsKey;
16
+ event: any;
17
+ wsUrl: string;
18
+ ws: WebSocket;
19
+ }) => void;
20
+ /** Reconnecting a dropped connection */
21
+ reconnecting: (evt: {
22
+ wsKey: WsKey;
23
+ event: any;
24
+ }) => void;
25
+ /** Successfully reconnected a connection that dropped */
26
+ reconnected: (evt: {
27
+ wsKey: WsKey;
28
+ event: any;
29
+ wsUrl: string;
30
+ ws: WebSocket;
31
+ }) => void;
32
+ /** Connection closed */
33
+ close: (evt: {
34
+ wsKey: WsKey;
35
+ event: any;
36
+ }) => void;
37
+ /** Received reply to websocket command (e.g. after subscribing to topics) */
38
+ response: (response: any & {
39
+ wsKey: WsKey;
40
+ isWSAPIResponse?: boolean;
41
+ }) => void;
42
+ /** Received data for topic */
43
+ message: (response: WsRawMessage) => void;
44
+ formattedMessage: (response: WsFormattedMessage) => void;
45
+ formattedUserDataMessage: (response: WsUserDataEvents) => void;
46
+ /**
47
+ * See for more information: https://github.com/tiagosiebler/bybit-api/issues/413
48
+ * @deprecated Use the 'exception' event instead. The 'error' event had the unintended consequence of throwing an unhandled promise rejection.
49
+ */
50
+ error: UseTheExceptionEventInstead;
51
+ /**
52
+ * Exception from ws client OR custom listeners (e.g. if you throw inside your event handler)
53
+ */
54
+ exception: (response: any & {
55
+ wsKey: WsKey;
56
+ isWSAPIResponse?: boolean;
57
+ }) => void;
58
+ /** Confirmation that a connection successfully authenticated */
59
+ authenticated: (event: {
60
+ wsKey: WsKey;
61
+ event: any;
62
+ isWSAPIResponse?: boolean;
63
+ }) => void;
64
+ }
65
+ export interface EmittableEvent<TEventType extends keyof WSClientEventMap<string> = keyof WSClientEventMap<string>> {
66
+ eventType: TEventType;
67
+ event: Parameters<WSClientEventMap<string>[TEventType]>[0];
68
+ isWSAPIResponse?: boolean;
69
+ }
70
+ export interface BaseWebsocketClient<TWSKey extends string, TWSRequestEvent extends object> {
71
+ on<U extends keyof WSClientEventMap<TWSKey>>(event: U, listener: WSClientEventMap<TWSKey>[U]): this;
72
+ emit<U extends keyof WSClientEventMap<TWSKey>>(event: U, ...args: Parameters<WSClientEventMap<TWSKey>[U]>): boolean;
73
+ }
74
+ /**
75
+ * A midflight WS request event (e.g. subscribe to these topics).
76
+ *
77
+ * - requestKey: unique identifier for this request, if available. Can be anything as a string.
78
+ * - requestEvent: the raw request, as an object, that will be sent on the ws connection. This may contain multiple topics/requests in one object, if the exchange supports it.
79
+ */
80
+ export interface MidflightWsRequestEvent<TEvent = object> {
81
+ requestKey: string | number;
82
+ requestEvent: TEvent;
83
+ }
84
+ /**
85
+ * Base WebSocket abstraction layer. Handles connections, tracking each connection as a unique "WS Key"
86
+ */
87
+ export declare abstract class BaseWebsocketClient<
88
+ /**
89
+ * The WS connections supported by the client, each identified by a unique primary key
90
+ */
91
+ TWSKey extends string, TWSRequestEvent extends object> extends EventEmitter {
92
+ /**
93
+ * State store to track a list of topics (topic requests) we are expected to be subscribed to if reconnected
94
+ */
95
+ private wsStore;
96
+ protected logger: typeof DefaultLogger;
97
+ protected options: WebsocketClientOptions;
98
+ private wsApiRequestId;
99
+ private timeOffsetMs;
100
+ constructor(options?: WSClientConfigurableOptions, logger?: typeof DefaultLogger);
101
+ /**
102
+ * Return true if this wsKey connection should automatically authenticate immediately after connecting
103
+ */
104
+ protected abstract isAuthOnConnectWsKey(wsKey: TWSKey): boolean;
105
+ protected abstract isCustomReconnectionNeeded(wsKey: TWSKey): boolean;
106
+ protected abstract triggerCustomReconnectionWorkflow(wsKey: TWSKey): Promise<void>;
107
+ protected abstract sendPingEvent(wsKey: TWSKey, ws: WebSocket): void;
108
+ protected abstract sendPongEvent(wsKey: TWSKey, ws: WebSocket): void;
109
+ protected abstract isWsPing(data: any): boolean;
110
+ protected abstract isWsPong(data: any): boolean;
111
+ protected abstract getWsAuthRequestEvent(wsKey: TWSKey): Promise<object>;
112
+ protected abstract isPrivateTopicRequest(request: WsTopicRequest<string>, wsKey: TWSKey): boolean;
113
+ protected abstract getPrivateWSKeys(): TWSKey[];
114
+ protected abstract getWsUrl(wsKey: TWSKey): Promise<string>;
115
+ protected abstract getMaxTopicsPerSubscribeEvent(wsKey: TWSKey): number | null;
116
+ /**
117
+ * @returns one or more correctly structured request events for performing a operations over WS. This can vary per exchange spec.
118
+ */
119
+ protected abstract getWsRequestEvents(wsKey: TWSKey, operation: WsOperation, requests: WsTopicRequest<string>[]): Promise<MidflightWsRequestEvent<TWSRequestEvent>[]>;
120
+ /**
121
+ * Abstraction called to sort ws events into emittable event types (response to a request, data update, etc)
122
+ */
123
+ protected abstract resolveEmittableEvents(wsKey: TWSKey, event: MessageEventLike): EmittableEvent[];
124
+ /**
125
+ * Request connection of all dependent (public & private) websockets, instead of waiting for automatic connection by library
126
+ */
127
+ protected abstract connectAll(): Promise<WSConnectedResult | undefined>[];
128
+ protected isPrivateWsKey(wsKey: TWSKey): boolean;
129
+ /** Returns auto-incrementing request ID, used to track promise references for async requests */
130
+ protected getNewRequestId(): number;
131
+ protected abstract sendWSAPIRequest(wsKey: TWSKey, channel: string, params?: any): Promise<unknown>;
132
+ protected abstract sendWSAPIRequest(wsKey: TWSKey, channel: string, params: any): Promise<unknown>;
133
+ getTimeOffsetMs(): number;
134
+ setTimeOffsetMs(newOffset: number): void;
135
+ /**
136
+ * Don't call directly! Use subscribe() instead!
137
+ *
138
+ * Subscribe to one or more topics on a WS connection (identified by WS Key).
139
+ *
140
+ * - Topics are automatically cached
141
+ * - Connections are automatically opened, if not yet connected
142
+ * - Authentication is automatically handled
143
+ * - Topics are automatically resubscribed to, if something happens to the connection, unless you call unsubsribeTopicsForWsKey(topics, key).
144
+ *
145
+ * @param wsRequests array of topics to subscribe to
146
+ * @param wsKey ws key referring to the ws connection these topics should be subscribed on
147
+ */
148
+ protected subscribeTopicsForWsKey(wsTopicRequests: WsTopicRequestOrStringTopic<string>[], wsKey: TWSKey): Promise<unknown>;
149
+ protected unsubscribeTopicsForWsKey(wsTopicRequests: WsTopicRequestOrStringTopic<string>[], wsKey: TWSKey): Promise<unknown>;
150
+ /**
151
+ * Splits topic requests into two groups, public & private topic requests
152
+ */
153
+ private sortTopicRequestsIntoPublicPrivate;
154
+ /** Get the WsStore that tracks websockets & topics */
155
+ getWsStore(): WsStore<TWSKey, WsTopicRequest<string>>;
156
+ close(wsKey: TWSKey, force?: boolean): void;
157
+ closeAll(force?: boolean): void;
158
+ isConnected(wsKey: TWSKey): boolean;
159
+ /**
160
+ * Request connection to a specific websocket, instead of waiting for automatic connection.
161
+ */
162
+ connect(wsKey: TWSKey, customUrl?: string | undefined, throwOnError?: boolean): Promise<WSConnectedResult | undefined>;
163
+ private connectToWsUrl;
164
+ private onWsPing;
165
+ private onWsPong;
166
+ private parseWsError;
167
+ /** Get a signature, build the auth request and send it */
168
+ private sendAuthRequest;
169
+ private reconnectWithDelay;
170
+ private ping;
171
+ /**
172
+ * Closes a connection, if it's even open. If open, this will trigger a reconnect asynchronously.
173
+ * If closed, trigger a reconnect immediately
174
+ */
175
+ protected executeReconnectableClose(wsKey: TWSKey, reason: string): void;
176
+ private clearTimers;
177
+ private clearPingTimer;
178
+ private clearPongTimer;
179
+ private clearReconnectTimer;
180
+ /**
181
+ * Returns a list of string events that can be individually sent upstream to complete subscribing/unsubscribing/etc to these topics
182
+ *
183
+ * If events are an object, these should be stringified (`return JSON.stringify(event);`)
184
+ * Each event returned by this will be sent one at a time
185
+ *
186
+ * Events are automatically split into smaller batches, by this method, if needed.
187
+ */
188
+ protected getWsOperationEventsForTopics(topics: WsTopicRequest<string>[], wsKey: TWSKey, operation: WsOperation): Promise<MidflightWsRequestEvent<TWSRequestEvent>[]>;
189
+ /**
190
+ * Simply builds and sends subscribe events for a list of topics for a ws key
191
+ *
192
+ * @private Use the `subscribe(topics)` or `subscribeTopicsForWsKey(topics, wsKey)` method to subscribe to topics.
193
+ */
194
+ private requestSubscribeTopics;
195
+ /**
196
+ * Simply builds and sends unsubscribe events for a list of topics for a ws key
197
+ *
198
+ * @private Use the `unsubscribe(topics)` method to unsubscribe from topics. Send WS message to unsubscribe from topics.
199
+ */
200
+ private requestUnsubscribeTopics;
201
+ /**
202
+ * Try sending a string event on a WS connection (identified by the WS Key)
203
+ */
204
+ tryWsSend(wsKey: TWSKey, wsMessage: string, throwExceptions?: boolean): void;
205
+ private onWsOpen;
206
+ /**
207
+ * Handle subscription to private topics _after_ authentication successfully completes asynchronously.
208
+ *
209
+ * Only used for exchanges that require auth before sending private topic subscription requests
210
+ */
211
+ private onWsAuthenticated;
212
+ private onWsMessage;
213
+ private onWsClose;
214
+ private getWs;
215
+ private setWsState;
216
+ /**
217
+ * Promise-driven method to assert that a ws has successfully connected (will await until connection is open)
218
+ */
219
+ assertIsConnected(wsKey: TWSKey): Promise<unknown>;
220
+ /**
221
+ * Promise-driven method to assert that a ws has been successfully authenticated (will await until auth is confirmed)
222
+ */
223
+ assertIsAuthenticated(wsKey: TWSKey): Promise<unknown>;
224
+ }
225
+ export {};