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.
Files changed (76) hide show
  1. package/README.md +1 -1
  2. package/dist/boundedqueue.d.ts +19 -0
  3. package/dist/boundedqueue.d.ts.map +1 -0
  4. package/dist/boundedqueue.js +96 -0
  5. package/dist/boundedqueue.js.map +1 -0
  6. package/dist/combine.d.ts +6 -4
  7. package/dist/combine.d.ts.map +1 -1
  8. package/dist/combine.js +8 -30
  9. package/dist/combine.js.map +1 -1
  10. package/dist/computable/booksnapshot.js +5 -2
  11. package/dist/computable/booksnapshot.js.map +1 -1
  12. package/dist/computable/computable.d.ts.map +1 -1
  13. package/dist/computable/computable.js +6 -6
  14. package/dist/computable/computable.js.map +1 -1
  15. package/dist/handy.d.ts +2 -2
  16. package/dist/handy.d.ts.map +1 -1
  17. package/dist/handy.js +22 -17
  18. package/dist/handy.js.map +1 -1
  19. package/dist/orderbook.d.ts +2 -1
  20. package/dist/orderbook.d.ts.map +1 -1
  21. package/dist/orderbook.js +14 -4
  22. package/dist/orderbook.js.map +1 -1
  23. package/dist/realtimefeeds/binance.d.ts.map +1 -1
  24. package/dist/realtimefeeds/binance.js +9 -0
  25. package/dist/realtimefeeds/binance.js.map +1 -1
  26. package/dist/realtimefeeds/binancedex.d.ts.map +1 -1
  27. package/dist/realtimefeeds/binancedex.js +3 -0
  28. package/dist/realtimefeeds/binancedex.js.map +1 -1
  29. package/dist/realtimefeeds/bitstamp.d.ts.map +1 -1
  30. package/dist/realtimefeeds/bitstamp.js +3 -0
  31. package/dist/realtimefeeds/bitstamp.js.map +1 -1
  32. package/dist/realtimefeeds/huobi.d.ts.map +1 -1
  33. package/dist/realtimefeeds/huobi.js +3 -0
  34. package/dist/realtimefeeds/huobi.js.map +1 -1
  35. package/dist/realtimefeeds/index.d.ts +0 -1
  36. package/dist/realtimefeeds/index.d.ts.map +1 -1
  37. package/dist/realtimefeeds/index.js +0 -3
  38. package/dist/realtimefeeds/index.js.map +1 -1
  39. package/dist/realtimefeeds/kucoin.d.ts.map +1 -1
  40. package/dist/realtimefeeds/kucoin.js +3 -0
  41. package/dist/realtimefeeds/kucoin.js.map +1 -1
  42. package/dist/realtimefeeds/kucoinfutures.d.ts.map +1 -1
  43. package/dist/realtimefeeds/kucoinfutures.js +3 -0
  44. package/dist/realtimefeeds/kucoinfutures.js.map +1 -1
  45. package/dist/realtimefeeds/realtimefeed.d.ts +17 -8
  46. package/dist/realtimefeeds/realtimefeed.d.ts.map +1 -1
  47. package/dist/realtimefeeds/realtimefeed.js +187 -114
  48. package/dist/realtimefeeds/realtimefeed.js.map +1 -1
  49. package/dist/realtimefeeds/woox.d.ts.map +1 -1
  50. package/dist/realtimefeeds/woox.js +3 -0
  51. package/dist/realtimefeeds/woox.js.map +1 -1
  52. package/dist/realtimeiterator.d.ts +10 -0
  53. package/dist/realtimeiterator.d.ts.map +1 -0
  54. package/dist/realtimeiterator.js +106 -0
  55. package/dist/realtimeiterator.js.map +1 -0
  56. package/dist/stream.d.ts.map +1 -1
  57. package/dist/stream.js +50 -21
  58. package/dist/stream.js.map +1 -1
  59. package/package.json +6 -36
  60. package/src/boundedqueue.ts +118 -0
  61. package/src/combine.ts +15 -39
  62. package/src/computable/booksnapshot.ts +5 -2
  63. package/src/computable/computable.ts +6 -5
  64. package/src/handy.ts +24 -19
  65. package/src/orderbook.ts +15 -4
  66. package/src/realtimefeeds/binance.ts +10 -0
  67. package/src/realtimefeeds/binancedex.ts +3 -0
  68. package/src/realtimefeeds/bitstamp.ts +3 -0
  69. package/src/realtimefeeds/huobi.ts +4 -0
  70. package/src/realtimefeeds/index.ts +0 -4
  71. package/src/realtimefeeds/kucoin.ts +3 -0
  72. package/src/realtimefeeds/kucoinfutures.ts +3 -0
  73. package/src/realtimefeeds/realtimefeed.ts +231 -129
  74. package/src/realtimefeeds/woox.ts +3 -0
  75. package/src/realtimeiterator.ts +124 -0
  76. package/src/stream.ts +66 -33
package/src/handy.ts CHANGED
@@ -1,17 +1,14 @@
1
1
  import crypto, { createHash } from 'crypto'
2
2
  import { createWriteStream, mkdirSync, rmSync } from 'node:fs'
3
3
  import { rename } from 'node:fs/promises'
4
- import type { RequestOptions, Agent } from 'https'
4
+ import { Agent as HttpAgent } from 'node:http'
5
+ import { Agent as HttpsAgent, type RequestOptions } from 'node:https'
5
6
  import followRedirects from 'follow-redirects'
6
- import * as httpsProxyAgentPkg from 'https-proxy-agent'
7
7
  import path from 'path'
8
8
  import { debug } from './debug.ts'
9
9
  import { Mapper } from './mappers/index.ts'
10
10
  import { Disconnect, Exchange, Filter, FilterForExchange } from './types.ts'
11
- import * as socksProxyAgentPkg from 'socks-proxy-agent'
12
- const { http, https } = followRedirects
13
- const { HttpsProxyAgent } = httpsProxyAgentPkg
14
- const { SocksProxyAgent } = socksProxyAgentPkg
11
+ const { http: followRedirectsHttp, https: followRedirectsHttps } = followRedirects
15
12
 
16
13
  export function parseAsUTCDate(val: string) {
17
14
  // Treat date-only and minute-level strings as UTC instead of local time.
@@ -263,18 +260,26 @@ export function optimizeFilters(filters: Filter<any>[]) {
263
260
  return optimizedFilters
264
261
  }
265
262
 
266
- const httpsAgent = new https.Agent({
263
+ const httpsAgent = new HttpsAgent({
267
264
  keepAlive: true,
268
265
  keepAliveMsecs: 10 * ONE_SEC_IN_MS,
269
266
  maxSockets: 120
270
267
  })
271
268
 
272
- export const httpsProxyAgent: Agent | undefined =
273
- process.env.HTTP_PROXY !== undefined
274
- ? new HttpsProxyAgent(process.env.HTTP_PROXY)
275
- : process.env.SOCKS_PROXY !== undefined
276
- ? new SocksProxyAgent(process.env.SOCKS_PROXY)
277
- : undefined
269
+ const proxyEnv =
270
+ process.env.HTTP_PROXY === undefined
271
+ ? undefined
272
+ : {
273
+ HTTP_PROXY: process.env.HTTP_PROXY,
274
+ HTTPS_PROXY: process.env.HTTP_PROXY
275
+ }
276
+ const httpProxyAgent = proxyEnv === undefined ? undefined : new HttpAgent({ proxyEnv })
277
+ const httpsProxyAgent = proxyEnv === undefined ? undefined : new HttpsAgent({ proxyEnv })
278
+
279
+ export function getProxyAgent(url: string | URL) {
280
+ const protocol = new URL(url).protocol
281
+ return protocol === 'http:' || protocol === 'ws:' ? httpProxyAgent : httpsProxyAgent
282
+ }
278
283
 
279
284
  const DEFAULT_FETCH_RETRY_LIMIT = 2
280
285
 
@@ -452,7 +457,7 @@ async function requestViaFetch(method: string, url: string, options: HttpRequest
452
457
  }
453
458
 
454
459
  async function requestViaProxy(method: string, url: string, options: HttpRequestOptions): Promise<HttpResponse> {
455
- const requestClient = new URL(url).protocol === 'http:' ? http : https
460
+ const requestClient = new URL(url).protocol === 'http:' ? followRedirectsHttp : followRedirectsHttps
456
461
  const preparedRequest = prepareRequest(method, options)
457
462
 
458
463
  return await new Promise<HttpResponse>((resolve, reject) => {
@@ -461,7 +466,7 @@ async function requestViaProxy(method: string, url: string, options: HttpRequest
461
466
  url,
462
467
  {
463
468
  method,
464
- agent: httpsProxyAgent,
469
+ agent: getProxyAgent(url),
465
470
  headers: preparedRequest.headers,
466
471
  timeout: options.timeout
467
472
  },
@@ -495,7 +500,7 @@ async function request(method: string, url: string, options: HttpRequestOptions
495
500
  for (let attempt = 1; ; attempt += 1) {
496
501
  try {
497
502
  const response =
498
- httpsProxyAgent === undefined ? await requestViaFetch(method, url, options) : await requestViaProxy(method, url, options)
503
+ getProxyAgent(url) === undefined ? await requestViaFetch(method, url, options) : await requestViaProxy(method, url, options)
499
504
 
500
505
  if (response.statusCode >= 200 && response.statusCode < 300) {
501
506
  return response
@@ -556,7 +561,6 @@ export async function download({
556
561
  acceptEncoding?: string
557
562
  }) {
558
563
  const httpRequestOptions = {
559
- agent: httpsProxyAgent !== undefined ? httpsProxyAgent : httpsAgent,
560
564
  timeout: 90 * ONE_SEC_IN_MS,
561
565
  headers: {
562
566
  'Accept-Encoding': acceptEncoding,
@@ -641,9 +645,10 @@ async function _downloadFile(requestOptions: RequestOptions, url: string, downlo
641
645
  let responseHeaders: Record<string, string> = {}
642
646
  await new Promise<void>((resolve, reject) => {
643
647
  const protocol = new URL(url).protocol
644
- const requestClient = protocol === 'http:' ? http : https
648
+ const requestClient = protocol === 'http:' ? followRedirectsHttp : followRedirectsHttps
649
+ const agent = getProxyAgent(url) ?? (protocol === 'https:' ? httpsAgent : undefined)
645
650
  const req = requestClient
646
- .get(url, { ...requestOptions, ...(protocol === 'http:' ? { agent: undefined } : {}) }, (res) => {
651
+ .get(url, { ...requestOptions, agent }, (res) => {
647
652
  const { statusCode } = res
648
653
  if (statusCode !== 200) {
649
654
  // read the error response text and throw it as an HttpError
package/src/orderbook.ts CHANGED
@@ -13,7 +13,7 @@ export class OrderBook {
13
13
  private readonly _bids = new PriceLevelTree(-1)
14
14
  private readonly _asks = new PriceLevelTree(1)
15
15
  private readonly _removeCrossedLevels: boolean | undefined
16
- private readonly _onCrossedLevelRemoved: OnLevelRemovedCB | undefined
16
+ private _onCrossedLevelRemovedCallbacks: Set<OnLevelRemovedCB> | undefined
17
17
 
18
18
  private _receivedInitialSnapshot = false
19
19
 
@@ -22,7 +22,16 @@ export class OrderBook {
22
22
  onCrossedLevelRemoved
23
23
  }: { removeCrossedLevels?: boolean; onCrossedLevelRemoved?: OnLevelRemovedCB } = {}) {
24
24
  this._removeCrossedLevels = removeCrossedLevels
25
- this._onCrossedLevelRemoved = onCrossedLevelRemoved
25
+ if (onCrossedLevelRemoved !== undefined) {
26
+ this._onCrossedLevelRemovedCallbacks = new Set([onCrossedLevelRemoved])
27
+ }
28
+ }
29
+
30
+ public addOnCrossedLevelRemovedCallback(callback: OnLevelRemovedCB) {
31
+ if (this._onCrossedLevelRemovedCallbacks === undefined) {
32
+ this._onCrossedLevelRemovedCallbacks = new Set()
33
+ }
34
+ this._onCrossedLevelRemovedCallbacks.add(callback)
26
35
  }
27
36
 
28
37
  public update(bookChange: BookChange) {
@@ -88,8 +97,10 @@ export class OrderBook {
88
97
 
89
98
  const newBestBid = this.bestBid()
90
99
  const newBestAsk = this.bestAsk()
91
- if (this._onCrossedLevelRemoved !== undefined) {
92
- this._onCrossedLevelRemoved(bookChange, bestBid, newBestBid, bestAsk, newBestAsk)
100
+ if (this._onCrossedLevelRemovedCallbacks !== undefined) {
101
+ for (const callback of this._onCrossedLevelRemovedCallbacks) {
102
+ callback(bookChange, bestBid, newBestBid, bestAsk, newBestAsk)
103
+ }
93
104
  }
94
105
 
95
106
  bestBid = newBestBid
@@ -355,6 +355,9 @@ class BinanceSingleConnectionRealTimeFeed extends RealTimeFeedBase {
355
355
  }
356
356
 
357
357
  const exchangeInfoResponse = await getJSON<any>(`${this._httpURL}/exchangeInfo`, binanceHttpOptions)
358
+ if (shouldCancel()) {
359
+ return
360
+ }
358
361
  const exchangeInfo = exchangeInfoResponse.data
359
362
 
360
363
  const DELAY_ENV = `${this._exchange.toUpperCase().replace(/-/g, '_')}_SNAPSHOTS_DELAY_MS`
@@ -408,10 +411,17 @@ class BinanceSingleConnectionRealTimeFeed extends RealTimeFeedBase {
408
411
  await wait(delayMS)
409
412
  }
410
413
 
414
+ if (shouldCancel()) {
415
+ return 0
416
+ }
417
+
411
418
  const depthSnapshotResponse = await getJSON<any>(
412
419
  `${this._httpURL}/depth?symbol=${symbol.toUpperCase()}&limit=1000`,
413
420
  binanceHttpOptions
414
421
  )
422
+ if (shouldCancel()) {
423
+ return 0
424
+ }
415
425
 
416
426
  const snapshot = {
417
427
  stream: `${symbol}@depthSnapshot`,
@@ -43,6 +43,9 @@ export class BinanceDexRealTimeFeed extends RealTimeFeedBase {
43
43
  }
44
44
 
45
45
  const { data } = await getJSON<any>(`${this.httpURL}/depth?symbol=${symbol}&limit=1000`)
46
+ if (shouldCancel()) {
47
+ return
48
+ }
46
49
 
47
50
  const snapshot = {
48
51
  stream: `depthSnapshot`,
@@ -51,6 +51,9 @@ export class BitstampRealTimeFeed extends RealTimeFeedBase {
51
51
  }
52
52
 
53
53
  const { data } = await getJSON(`${this.httpURL}/order_book/${symbol}?group=1`)
54
+ if (shouldCancel()) {
55
+ return
56
+ }
54
57
 
55
58
  const snapshot = {
56
59
  data,
@@ -122,6 +122,10 @@ class HuobiMarketDataRealTimeFeed extends RealTimeFeedBase {
122
122
  }
123
123
 
124
124
  setTimeout(() => {
125
+ if (shouldCancel()) {
126
+ return
127
+ }
128
+
125
129
  this.send({
126
130
  id: '1',
127
131
  req: `market.${symbol}.mbp.400`
@@ -143,7 +143,3 @@ export function createRealTimeFeed(
143
143
 
144
144
  return new RealTimeFeedFactory(exchange, filters, timeoutIntervalMS, onError)
145
145
  }
146
-
147
- export function setRealTimeFeedFactory(exchange: Exchange, realTimeFeed: RealTimeFeed) {
148
- realTimeFeedsMap[exchange] = realTimeFeed
149
- }
@@ -45,6 +45,9 @@ export class KucoinRealTimeFeed extends RealTimeFeedBase {
45
45
  const { data } = await getJSON<any>(`${this._httpURL}/v1/market/orderbook/level2_100?symbol=${symbol}`, {
46
46
  timeout: 10000
47
47
  })
48
+ if (shouldCancel()) {
49
+ return
50
+ }
48
51
 
49
52
  const snapshot = {
50
53
  type: 'message',
@@ -77,6 +77,9 @@ export class KucoinFuturesSingleConnectionRealTimeFeed extends RealTimeFeedBase
77
77
  }
78
78
 
79
79
  const { data } = await getJSON<any>(`${this._httpURL}/v1/level2/snapshot?symbol=${symbol}`, kucoinHttpOptions)
80
+ if (shouldCancel()) {
81
+ return
82
+ }
80
83
 
81
84
  const snapshot = {
82
85
  type: 'message',