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
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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
273
|
-
process.env.HTTP_PROXY
|
|
274
|
-
?
|
|
275
|
-
:
|
|
276
|
-
|
|
277
|
-
|
|
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:' ?
|
|
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:
|
|
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
|
-
|
|
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:' ?
|
|
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,
|
|
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
|
|
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
|
-
|
|
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.
|
|
92
|
-
|
|
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`,
|
|
@@ -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',
|