tardis-dev 17.2.2 → 18.0.0
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.
- package/README.md +1 -1
- package/dist/boundedqueue.d.ts +19 -0
- package/dist/boundedqueue.d.ts.map +1 -0
- package/dist/boundedqueue.js +96 -0
- package/dist/boundedqueue.js.map +1 -0
- package/dist/combine.d.ts +6 -4
- package/dist/combine.d.ts.map +1 -1
- package/dist/combine.js +8 -30
- package/dist/combine.js.map +1 -1
- package/dist/computable/booksnapshot.js +5 -2
- package/dist/computable/booksnapshot.js.map +1 -1
- package/dist/computable/computable.d.ts.map +1 -1
- package/dist/computable/computable.js +6 -6
- package/dist/computable/computable.js.map +1 -1
- package/dist/handy.d.ts +2 -2
- package/dist/handy.d.ts.map +1 -1
- package/dist/handy.js +22 -17
- package/dist/handy.js.map +1 -1
- package/dist/orderbook.d.ts +2 -1
- package/dist/orderbook.d.ts.map +1 -1
- package/dist/orderbook.js +14 -4
- package/dist/orderbook.js.map +1 -1
- package/dist/realtimefeeds/binance.d.ts.map +1 -1
- package/dist/realtimefeeds/binance.js +9 -0
- package/dist/realtimefeeds/binance.js.map +1 -1
- package/dist/realtimefeeds/binancedex.d.ts.map +1 -1
- package/dist/realtimefeeds/binancedex.js +3 -0
- package/dist/realtimefeeds/binancedex.js.map +1 -1
- package/dist/realtimefeeds/bitstamp.d.ts.map +1 -1
- package/dist/realtimefeeds/bitstamp.js +3 -0
- package/dist/realtimefeeds/bitstamp.js.map +1 -1
- package/dist/realtimefeeds/huobi.d.ts.map +1 -1
- package/dist/realtimefeeds/huobi.js +3 -0
- package/dist/realtimefeeds/huobi.js.map +1 -1
- package/dist/realtimefeeds/index.d.ts +0 -1
- package/dist/realtimefeeds/index.d.ts.map +1 -1
- package/dist/realtimefeeds/index.js +0 -3
- package/dist/realtimefeeds/index.js.map +1 -1
- package/dist/realtimefeeds/kucoin.d.ts.map +1 -1
- package/dist/realtimefeeds/kucoin.js +3 -0
- package/dist/realtimefeeds/kucoin.js.map +1 -1
- package/dist/realtimefeeds/kucoinfutures.d.ts.map +1 -1
- package/dist/realtimefeeds/kucoinfutures.js +3 -0
- package/dist/realtimefeeds/kucoinfutures.js.map +1 -1
- package/dist/realtimefeeds/realtimefeed.d.ts +17 -8
- package/dist/realtimefeeds/realtimefeed.d.ts.map +1 -1
- package/dist/realtimefeeds/realtimefeed.js +187 -114
- package/dist/realtimefeeds/realtimefeed.js.map +1 -1
- package/dist/realtimefeeds/woox.d.ts.map +1 -1
- package/dist/realtimefeeds/woox.js +3 -0
- package/dist/realtimefeeds/woox.js.map +1 -1
- package/dist/realtimeiterator.d.ts +10 -0
- package/dist/realtimeiterator.d.ts.map +1 -0
- package/dist/realtimeiterator.js +106 -0
- package/dist/realtimeiterator.js.map +1 -0
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +50 -21
- package/dist/stream.js.map +1 -1
- package/package.json +6 -36
- package/src/boundedqueue.ts +118 -0
- package/src/combine.ts +15 -39
- package/src/computable/booksnapshot.ts +5 -2
- package/src/computable/computable.ts +6 -5
- package/src/handy.ts +24 -19
- package/src/orderbook.ts +15 -4
- package/src/realtimefeeds/binance.ts +10 -0
- package/src/realtimefeeds/binancedex.ts +3 -0
- package/src/realtimefeeds/bitstamp.ts +3 -0
- package/src/realtimefeeds/huobi.ts +4 -0
- package/src/realtimefeeds/index.ts +0 -4
- package/src/realtimefeeds/kucoin.ts +3 -0
- package/src/realtimefeeds/kucoinfutures.ts +3 -0
- package/src/realtimefeeds/realtimefeed.ts +231 -129
- package/src/realtimefeeds/woox.ts +3 -0
- package/src/realtimeiterator.ts +124 -0
- package/src/stream.ts +66 -33
|
@@ -2,8 +2,9 @@ import dbg from 'debug'
|
|
|
2
2
|
import WebSocket, { createWebSocketStream } from 'ws'
|
|
3
3
|
import type { ClientRequestArgs } from 'http'
|
|
4
4
|
import { PassThrough, Writable } from 'stream'
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { setTimeout as sleep } from 'node:timers/promises'
|
|
6
|
+
import { getProxyAgent, ONE_SEC_IN_MS, optimizeFilters } from '../handy.ts'
|
|
7
|
+
import { createManagedRealTimeIterator, mergeRealTime, type ManagedRealTimeIterator } from '../realtimeiterator.ts'
|
|
7
8
|
import { Exchange, Filter } from '../types.ts'
|
|
8
9
|
|
|
9
10
|
export type RealTimeFeed = {
|
|
@@ -19,9 +20,18 @@ let connectionCounter = 1
|
|
|
19
20
|
|
|
20
21
|
export type RealTimeFeedIterable = AsyncIterable<any>
|
|
21
22
|
|
|
23
|
+
type RealTimeFeedConnection = {
|
|
24
|
+
id: number
|
|
25
|
+
ws: WebSocket
|
|
26
|
+
controller: AbortController
|
|
27
|
+
receivedMessagesCount: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ABORTED = Symbol('aborted')
|
|
31
|
+
|
|
22
32
|
export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
23
|
-
[Symbol.asyncIterator]() {
|
|
24
|
-
return this._stream()
|
|
33
|
+
[Symbol.asyncIterator](): ManagedRealTimeIterator<any> {
|
|
34
|
+
return createManagedRealTimeIterator(this._stream(), () => this._close())
|
|
25
35
|
}
|
|
26
36
|
|
|
27
37
|
protected readonly debug: dbg.Debugger
|
|
@@ -29,10 +39,10 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
29
39
|
protected readonly throttleSubscribeMS: number = 0
|
|
30
40
|
protected readonly manualSnapshotsBuffer: any[] = []
|
|
31
41
|
private readonly _filters: Filter<string>[]
|
|
32
|
-
private _receivedMessagesCount = 0
|
|
33
|
-
private _ws?: WebSocket
|
|
34
|
-
private _connectionId = -1
|
|
35
42
|
private _wsClientOptions: WebSocket.ClientOptions | ClientRequestArgs
|
|
43
|
+
private _closed = false
|
|
44
|
+
private readonly _closeController = new AbortController()
|
|
45
|
+
private _connection?: RealTimeFeedConnection
|
|
36
46
|
protected readonly originHeader: string | undefined = undefined
|
|
37
47
|
protected readonly extraHeaders: Record<string, string> | undefined = undefined
|
|
38
48
|
|
|
@@ -54,10 +64,6 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
54
64
|
handshakeTimeout: 10 * ONE_SEC_IN_MS,
|
|
55
65
|
skipUTF8Validation: true
|
|
56
66
|
} as any
|
|
57
|
-
|
|
58
|
-
if (httpsProxyAgent !== undefined) {
|
|
59
|
-
this._wsClientOptions.agent = httpsProxyAgent
|
|
60
|
-
}
|
|
61
67
|
}
|
|
62
68
|
|
|
63
69
|
protected async getWebSocketUrl() {
|
|
@@ -67,22 +73,40 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
67
73
|
return finalWssUrl
|
|
68
74
|
}
|
|
69
75
|
|
|
76
|
+
private _close() {
|
|
77
|
+
if (this._closed) {
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this._closed = true
|
|
82
|
+
this._closeController.abort()
|
|
83
|
+
this._closeConnection(this._connection)
|
|
84
|
+
}
|
|
85
|
+
|
|
70
86
|
private async *_stream() {
|
|
71
|
-
let staleConnectionTimerId
|
|
72
|
-
let pingTimerId
|
|
73
87
|
let retries = 0
|
|
74
88
|
|
|
75
|
-
while (
|
|
89
|
+
while (this._closed === false) {
|
|
90
|
+
const connectionId = connectionCounter++
|
|
91
|
+
let connection: RealTimeFeedConnection | undefined
|
|
92
|
+
let staleConnectionTimerId: NodeJS.Timeout | undefined
|
|
93
|
+
let pingTimerId: NodeJS.Timeout | undefined
|
|
94
|
+
let connectionError: Error | undefined
|
|
95
|
+
|
|
76
96
|
try {
|
|
77
|
-
this.
|
|
97
|
+
this.manualSnapshotsBuffer.length = 0
|
|
78
98
|
const subscribeMessages = this.mapToSubscribeMessages(this._filters)
|
|
79
|
-
const finalWssUrl = await this.getWebSocketUrl()
|
|
99
|
+
const finalWssUrl = await resolveOrAbort(this.getWebSocketUrl(), this._closeController.signal)
|
|
80
100
|
|
|
81
|
-
|
|
101
|
+
if (finalWssUrl === ABORTED || this._closed) {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
this.debug('(connection id: %d) estabilishing connection to %s', connectionId, finalWssUrl)
|
|
82
106
|
|
|
83
107
|
this.debug(
|
|
84
108
|
'(connection id: %d) provided filters: %o mapped to subscribe messages: %j',
|
|
85
|
-
|
|
109
|
+
connectionId,
|
|
86
110
|
this._filters,
|
|
87
111
|
subscribeMessages
|
|
88
112
|
)
|
|
@@ -95,19 +119,32 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
95
119
|
Object.assign((this._wsClientOptions as any).headers, this.extraHeaders)
|
|
96
120
|
}
|
|
97
121
|
|
|
98
|
-
this.
|
|
99
|
-
|
|
100
|
-
|
|
122
|
+
this._wsClientOptions.agent = getProxyAgent(finalWssUrl)
|
|
123
|
+
const ws = new WebSocket(finalWssUrl, this._wsClientOptions)
|
|
124
|
+
const currentConnection: RealTimeFeedConnection = {
|
|
125
|
+
id: connectionId,
|
|
126
|
+
ws,
|
|
127
|
+
controller: new AbortController(),
|
|
128
|
+
receivedMessagesCount: 0
|
|
129
|
+
}
|
|
130
|
+
connection = currentConnection
|
|
131
|
+
this._connection = currentConnection
|
|
132
|
+
ws.onopen = () => this._onConnectionOpen(currentConnection, subscribeMessages)
|
|
133
|
+
ws.onclose = (event) => this._onConnectionClosed(currentConnection, event)
|
|
101
134
|
|
|
102
|
-
staleConnectionTimerId = this._monitorConnectionIfStale()
|
|
103
|
-
pingTimerId = this._sendPeriodicPing()
|
|
135
|
+
staleConnectionTimerId = this._monitorConnectionIfStale(currentConnection)
|
|
136
|
+
pingTimerId = this._sendPeriodicPing(currentConnection)
|
|
104
137
|
|
|
105
|
-
const realtimeMessagesStream = createWebSocketStream(
|
|
138
|
+
const realtimeMessagesStream = createWebSocketStream(ws, {
|
|
106
139
|
readableObjectMode: true, // othwerwise we may end up with multiple messages returned by stream in single iteration
|
|
107
140
|
readableHighWaterMark: 8096 // since we're in object mode, let's increase hwm a little from default of 16 messages buffered
|
|
108
141
|
}) as unknown as AsyncIterableIterator<Buffer>
|
|
109
142
|
|
|
110
143
|
for await (let message of realtimeMessagesStream) {
|
|
144
|
+
if (this._isConnectionActive(currentConnection) === false) {
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
|
|
111
148
|
if (this.decompress !== undefined) {
|
|
112
149
|
message = this.decompress(message)
|
|
113
150
|
}
|
|
@@ -132,7 +169,7 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
132
169
|
// exclude heaartbeat messages from received messages counter
|
|
133
170
|
// connection could still be stale even if only heartbeats are provided without any data
|
|
134
171
|
if (this.messageIsHeartbeat(messageDeserialized) === false) {
|
|
135
|
-
|
|
172
|
+
currentConnection.receivedMessagesCount++
|
|
136
173
|
}
|
|
137
174
|
|
|
138
175
|
this.onMessage(messageDeserialized)
|
|
@@ -153,80 +190,72 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
153
190
|
}
|
|
154
191
|
}
|
|
155
192
|
|
|
156
|
-
|
|
193
|
+
if (this._closed) {
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (this._closed) {
|
|
198
|
+
return
|
|
199
|
+
}
|
|
200
|
+
connectionError = error instanceof Error ? error : new Error(String(error))
|
|
201
|
+
} finally {
|
|
157
202
|
if (staleConnectionTimerId !== undefined) {
|
|
158
203
|
clearInterval(staleConnectionTimerId)
|
|
159
204
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
this._onError(error)
|
|
205
|
+
|
|
206
|
+
if (pingTimerId !== undefined) {
|
|
207
|
+
clearInterval(pingTimerId)
|
|
164
208
|
}
|
|
165
209
|
|
|
166
|
-
|
|
210
|
+
this._closeConnection(connection)
|
|
211
|
+
}
|
|
167
212
|
|
|
168
|
-
|
|
169
|
-
|
|
213
|
+
if (this._closed) {
|
|
214
|
+
return
|
|
215
|
+
}
|
|
170
216
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
delay = Math.pow(2, retries - 1) * 1000
|
|
217
|
+
let retryDelay: number | undefined
|
|
218
|
+
if (connectionError !== undefined) {
|
|
219
|
+
this._onError?.(connectionError)
|
|
220
|
+
retries++
|
|
176
221
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
222
|
+
const MAX_DELAY = 32 * 1000
|
|
223
|
+
const isRateLimited = connectionError.message.includes('429')
|
|
224
|
+
retryDelay = isRateLimited ? (MAX_DELAY / 2) * retries : Math.min(Math.pow(2, retries - 1) * 1000, MAX_DELAY)
|
|
181
225
|
|
|
182
226
|
this.debug(
|
|
183
227
|
'(connection id: %d) %s real-time feed connection error, retries count: %d, next retry delay: %dms, rate limited: %s error message: %o',
|
|
184
|
-
|
|
228
|
+
connectionId,
|
|
185
229
|
this._exchange,
|
|
186
230
|
retries,
|
|
187
|
-
|
|
231
|
+
retryDelay,
|
|
188
232
|
isRateLimited,
|
|
189
|
-
|
|
233
|
+
connectionError
|
|
190
234
|
)
|
|
235
|
+
}
|
|
191
236
|
|
|
192
|
-
|
|
193
|
-
if (staleConnectionTimerId !== undefined) {
|
|
194
|
-
clearInterval(staleConnectionTimerId)
|
|
195
|
-
}
|
|
196
|
-
yield { __disconnect__: true }
|
|
197
|
-
|
|
198
|
-
await wait(delay)
|
|
199
|
-
} finally {
|
|
200
|
-
// stop timers
|
|
201
|
-
if (staleConnectionTimerId !== undefined) {
|
|
202
|
-
clearInterval(staleConnectionTimerId)
|
|
203
|
-
}
|
|
237
|
+
yield { __disconnect__: true }
|
|
204
238
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
239
|
+
if (retryDelay !== undefined) {
|
|
240
|
+
await this._wait(retryDelay, this._closeController.signal)
|
|
208
241
|
}
|
|
209
242
|
}
|
|
210
243
|
}
|
|
211
244
|
|
|
212
245
|
protected send(msg: any) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
if (this._ws.readyState !== WebSocket.OPEN) {
|
|
246
|
+
const ws = this._connection?.ws
|
|
247
|
+
if (ws === undefined || ws.readyState !== WebSocket.OPEN) {
|
|
217
248
|
return
|
|
218
249
|
}
|
|
219
|
-
|
|
250
|
+
ws.send(JSON.stringify(msg))
|
|
220
251
|
}
|
|
221
252
|
|
|
222
253
|
protected sendRaw(msg: string | Buffer) {
|
|
223
|
-
|
|
254
|
+
const ws = this._connection?.ws
|
|
255
|
+
if (ws === undefined || ws.readyState !== WebSocket.OPEN) {
|
|
224
256
|
return
|
|
225
257
|
}
|
|
226
|
-
|
|
227
|
-
return
|
|
228
|
-
}
|
|
229
|
-
this._ws.send(msg)
|
|
258
|
+
ws.send(msg)
|
|
230
259
|
}
|
|
231
260
|
|
|
232
261
|
protected abstract mapToSubscribeMessages(filters: Filter<string>[]): any[]
|
|
@@ -255,48 +284,48 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
255
284
|
|
|
256
285
|
protected decompress?: (msg: any) => Buffer
|
|
257
286
|
|
|
258
|
-
private _monitorConnectionIfStale() {
|
|
287
|
+
private _monitorConnectionIfStale(connection: RealTimeFeedConnection) {
|
|
259
288
|
if (this._timeoutIntervalMS === undefined || this._timeoutIntervalMS === 0) {
|
|
260
289
|
return
|
|
261
290
|
}
|
|
262
291
|
|
|
263
292
|
// set up timer that checks against open, but stale connections that do not return any data
|
|
264
293
|
return setInterval(() => {
|
|
265
|
-
if (this.
|
|
294
|
+
if (this._isConnectionActive(connection) === false) {
|
|
266
295
|
return
|
|
267
296
|
}
|
|
268
297
|
|
|
269
|
-
if (
|
|
298
|
+
if (connection.receivedMessagesCount === 0) {
|
|
270
299
|
this.debug(
|
|
271
300
|
'(connection id: %d) did not received any messages within %d ms timeout, terminating connection...',
|
|
272
|
-
|
|
301
|
+
connection.id,
|
|
273
302
|
this._timeoutIntervalMS
|
|
274
303
|
)
|
|
275
|
-
|
|
304
|
+
connection.ws.terminate()
|
|
276
305
|
}
|
|
277
|
-
|
|
306
|
+
connection.receivedMessagesCount = 0
|
|
278
307
|
}, this._timeoutIntervalMS)
|
|
279
308
|
}
|
|
280
309
|
|
|
281
|
-
private _sendPeriodicPing() {
|
|
310
|
+
private _sendPeriodicPing(connection: RealTimeFeedConnection) {
|
|
282
311
|
return setInterval(() => {
|
|
283
|
-
if (this.
|
|
312
|
+
if (this._isConnectionOpen(connection) === false) {
|
|
284
313
|
return
|
|
285
314
|
}
|
|
286
315
|
|
|
287
316
|
if (this.sendCustomPing !== undefined) {
|
|
288
317
|
this.sendCustomPing()
|
|
289
318
|
} else {
|
|
290
|
-
|
|
319
|
+
connection.ws.ping()
|
|
291
320
|
}
|
|
292
321
|
}, 5 * ONE_SEC_IN_MS)
|
|
293
322
|
}
|
|
294
323
|
|
|
295
|
-
private
|
|
296
|
-
|
|
297
|
-
const subscribeMessages = this.mapToSubscribeMessages(this._filters)
|
|
324
|
+
private async _onConnectionEstablished(connection: RealTimeFeedConnection, subscribeMessages: any[]) {
|
|
325
|
+
const shouldCancel = () => this._isConnectionOpen(connection) === false
|
|
298
326
|
|
|
299
|
-
|
|
327
|
+
try {
|
|
328
|
+
const symbolsCount = this._filters.reduce((prev, curr) => {
|
|
300
329
|
if (curr.symbols !== undefined) {
|
|
301
330
|
for (const symbol of curr.symbols) {
|
|
302
331
|
prev.add(symbol)
|
|
@@ -307,78 +336,140 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
307
336
|
|
|
308
337
|
await this.onConnected()
|
|
309
338
|
|
|
339
|
+
if (shouldCancel()) {
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
|
|
310
343
|
for (const message of subscribeMessages) {
|
|
311
|
-
|
|
344
|
+
if (shouldCancel()) {
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
this._send(connection, message)
|
|
312
349
|
if (this.throttleSubscribeMS > 0) {
|
|
313
|
-
await
|
|
350
|
+
await this._wait(this.throttleSubscribeMS, connection.controller.signal)
|
|
314
351
|
}
|
|
315
352
|
}
|
|
316
353
|
|
|
317
|
-
this.debug('(connection id: %d) established connection',
|
|
354
|
+
this.debug('(connection id: %d) established connection', connection.id)
|
|
318
355
|
|
|
319
356
|
//wait before fetching snapshots until we're sure we've got proper connection estabilished (received some messages)
|
|
320
|
-
while (
|
|
321
|
-
await
|
|
357
|
+
while (shouldCancel() === false && connection.receivedMessagesCount < symbolsCount * 2) {
|
|
358
|
+
await this._wait(100, connection.controller.signal)
|
|
322
359
|
}
|
|
360
|
+
|
|
361
|
+
if (shouldCancel()) {
|
|
362
|
+
return
|
|
363
|
+
}
|
|
364
|
+
|
|
323
365
|
// wait a second just in case before starting fetching the snapshots
|
|
324
|
-
await
|
|
366
|
+
await this._wait(1 * ONE_SEC_IN_MS, connection.controller.signal)
|
|
325
367
|
|
|
326
|
-
if (
|
|
368
|
+
if (shouldCancel()) {
|
|
327
369
|
return
|
|
328
370
|
}
|
|
329
371
|
|
|
330
|
-
await this.provideManualSnapshots(this._filters,
|
|
372
|
+
await this.provideManualSnapshots(this._filters, shouldCancel)
|
|
331
373
|
} catch (e) {
|
|
332
|
-
this.debug('(connection id: %d) providing manual snapshots error: %o',
|
|
333
|
-
this.
|
|
374
|
+
this.debug('(connection id: %d) providing manual snapshots error: %o', connection.id, e)
|
|
375
|
+
if (this._isConnectionOpen(connection)) {
|
|
376
|
+
connection.ws.emit('error', e)
|
|
377
|
+
}
|
|
334
378
|
}
|
|
335
379
|
}
|
|
336
380
|
|
|
337
|
-
private _onConnectionClosed
|
|
338
|
-
this.debug('(connection id: %d) connection closed %s',
|
|
381
|
+
private _onConnectionClosed(connection: RealTimeFeedConnection, event: WebSocket.CloseEvent) {
|
|
382
|
+
this.debug('(connection id: %d) connection closed %s', connection.id, event.reason)
|
|
339
383
|
}
|
|
340
|
-
}
|
|
341
384
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
private readonly _timeoutIntervalMS: number | undefined,
|
|
347
|
-
private readonly _onError?: (error: Error) => void
|
|
348
|
-
) {}
|
|
385
|
+
private _onConnectionOpen(connection: RealTimeFeedConnection, subscribeMessages: any[]) {
|
|
386
|
+
if (this._isConnectionActive(connection) === false) {
|
|
387
|
+
return
|
|
388
|
+
}
|
|
349
389
|
|
|
350
|
-
|
|
351
|
-
return this._stream()
|
|
390
|
+
void this._onConnectionEstablished(connection, subscribeMessages)
|
|
352
391
|
}
|
|
353
392
|
|
|
354
|
-
private
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
393
|
+
private _send(connection: RealTimeFeedConnection, message: any) {
|
|
394
|
+
if (this._isConnectionOpen(connection)) {
|
|
395
|
+
connection.ws.send(JSON.stringify(message))
|
|
396
|
+
}
|
|
397
|
+
}
|
|
359
398
|
|
|
360
|
-
|
|
399
|
+
private _isConnectionActive(connection: RealTimeFeedConnection) {
|
|
400
|
+
return this._closed === false && this._connection === connection && connection.controller.signal.aborted === false
|
|
401
|
+
}
|
|
361
402
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
for await (const message of realTimeFeed) {
|
|
366
|
-
if (combinedStream.destroyed) {
|
|
367
|
-
return
|
|
368
|
-
}
|
|
403
|
+
private _isConnectionOpen(connection: RealTimeFeedConnection) {
|
|
404
|
+
return this._isConnectionActive(connection) && connection.ws.readyState === WebSocket.OPEN
|
|
405
|
+
}
|
|
369
406
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
}
|
|
374
|
-
})()
|
|
407
|
+
private _closeConnection(connection: RealTimeFeedConnection | undefined) {
|
|
408
|
+
if (connection === undefined) {
|
|
409
|
+
return
|
|
375
410
|
}
|
|
376
411
|
|
|
377
|
-
|
|
378
|
-
|
|
412
|
+
connection.controller.abort()
|
|
413
|
+
if (connection.ws.readyState !== WebSocket.CLOSED) {
|
|
414
|
+
connection.ws.terminate()
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (this._connection === connection) {
|
|
418
|
+
this._connection = undefined
|
|
419
|
+
this.manualSnapshotsBuffer.length = 0
|
|
379
420
|
}
|
|
380
421
|
}
|
|
381
422
|
|
|
423
|
+
private async _wait(delayMS: number, signal: AbortSignal) {
|
|
424
|
+
try {
|
|
425
|
+
await sleep(delayMS, undefined, { signal })
|
|
426
|
+
} catch (error) {
|
|
427
|
+
if (signal.aborted === false) {
|
|
428
|
+
throw error
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function resolveOrAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T | typeof ABORTED> {
|
|
435
|
+
if (signal.aborted) {
|
|
436
|
+
return Promise.resolve(ABORTED)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return new Promise((resolve, reject) => {
|
|
440
|
+
let settled = false
|
|
441
|
+
const onAbort = () => settle(() => resolve(ABORTED))
|
|
442
|
+
const settle = (callback: () => void) => {
|
|
443
|
+
if (settled) {
|
|
444
|
+
return
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
settled = true
|
|
448
|
+
signal.removeEventListener('abort', onAbort)
|
|
449
|
+
callback()
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
signal.addEventListener('abort', onAbort, { once: true })
|
|
453
|
+
void promise.then(
|
|
454
|
+
(value) => settle(() => resolve(value)),
|
|
455
|
+
(error) => settle(() => reject(error))
|
|
456
|
+
)
|
|
457
|
+
})
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export abstract class MultiConnectionRealTimeFeedBase implements RealTimeFeedIterable {
|
|
461
|
+
constructor(
|
|
462
|
+
private readonly _exchange: string,
|
|
463
|
+
private readonly _filters: Filter<string>[],
|
|
464
|
+
private readonly _timeoutIntervalMS: number | undefined,
|
|
465
|
+
private readonly _onError?: (error: Error) => void
|
|
466
|
+
) {}
|
|
467
|
+
|
|
468
|
+
[Symbol.asyncIterator](): ManagedRealTimeIterator<any> {
|
|
469
|
+
const realTimeFeeds = Array.from(this._getRealTimeFeeds(this._exchange, this._filters, this._timeoutIntervalMS, this._onError))
|
|
470
|
+
return mergeRealTime(realTimeFeeds)
|
|
471
|
+
}
|
|
472
|
+
|
|
382
473
|
protected abstract _getRealTimeFeeds(
|
|
383
474
|
exchange: string,
|
|
384
475
|
filters: Filter<string>[],
|
|
@@ -390,6 +481,7 @@ export abstract class MultiConnectionRealTimeFeedBase implements RealTimeFeedIte
|
|
|
390
481
|
export abstract class PoolingClientBase implements RealTimeFeedIterable {
|
|
391
482
|
protected readonly debug: dbg.Debugger
|
|
392
483
|
private _tid: NodeJS.Timeout | undefined = undefined
|
|
484
|
+
private _outputStream: PassThrough | undefined
|
|
393
485
|
|
|
394
486
|
constructor(
|
|
395
487
|
exchange: string,
|
|
@@ -399,8 +491,8 @@ export abstract class PoolingClientBase implements RealTimeFeedIterable {
|
|
|
399
491
|
this.debug = dbg(`tardis-dev:pooling-client:${exchange}`)
|
|
400
492
|
}
|
|
401
493
|
|
|
402
|
-
[Symbol.asyncIterator]() {
|
|
403
|
-
return this._stream()
|
|
494
|
+
[Symbol.asyncIterator](): ManagedRealTimeIterator<any> {
|
|
495
|
+
return createManagedRealTimeIterator(this._stream(), () => this._close())
|
|
404
496
|
}
|
|
405
497
|
|
|
406
498
|
protected abstract poolDataToStream(outputStream: Writable): Promise<void>
|
|
@@ -409,6 +501,14 @@ export abstract class PoolingClientBase implements RealTimeFeedIterable {
|
|
|
409
501
|
return this._poolingIntervalSeconds * ONE_SEC_IN_MS
|
|
410
502
|
}
|
|
411
503
|
|
|
504
|
+
private _close() {
|
|
505
|
+
if (this._tid !== undefined) {
|
|
506
|
+
clearTimeout(this._tid)
|
|
507
|
+
this._tid = undefined
|
|
508
|
+
}
|
|
509
|
+
this._outputStream?.destroy()
|
|
510
|
+
}
|
|
511
|
+
|
|
412
512
|
private async _startPooling(outputStream: Writable) {
|
|
413
513
|
const pool = async () => {
|
|
414
514
|
try {
|
|
@@ -439,6 +539,7 @@ export abstract class PoolingClientBase implements RealTimeFeedIterable {
|
|
|
439
539
|
objectMode: true,
|
|
440
540
|
highWaterMark: 1024
|
|
441
541
|
})
|
|
542
|
+
this._outputStream = stream
|
|
442
543
|
|
|
443
544
|
this._startPooling(stream)
|
|
444
545
|
|
|
@@ -449,8 +550,9 @@ export abstract class PoolingClientBase implements RealTimeFeedIterable {
|
|
|
449
550
|
yield message
|
|
450
551
|
}
|
|
451
552
|
} finally {
|
|
452
|
-
|
|
453
|
-
|
|
553
|
+
this._close()
|
|
554
|
+
if (this._outputStream === stream) {
|
|
555
|
+
this._outputStream = undefined
|
|
454
556
|
}
|
|
455
557
|
|
|
456
558
|
this.debug('pooling finished')
|
|
@@ -46,6 +46,9 @@ export class WooxRealTimeFeed extends RealTimeFeedBase {
|
|
|
46
46
|
const response = await getJSON<WooxOrderBookSnapshotResponse>(
|
|
47
47
|
`${this.httpURL}/v3/public/orderbook?symbol=${symbol}&maxLevel=${ORDER_BOOK_DEPTH}`
|
|
48
48
|
)
|
|
49
|
+
if (shouldCancel()) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
49
52
|
|
|
50
53
|
if (response.data.success === false) {
|
|
51
54
|
throw new Error(`Woox orderbook snapshot request failed: ${JSON.stringify(response.data)}`)
|