@tradejs/infra 1.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.
@@ -0,0 +1,50 @@
1
+ import Redis from 'ioredis';
2
+
3
+ declare global {
4
+ var __redis__: Redis | undefined;
5
+ }
6
+ interface Options {
7
+ expire?: number;
8
+ }
9
+ interface DelKeyOptions {
10
+ raiseOnMisconf?: boolean;
11
+ }
12
+ declare const getKeys: (prefix: string) => Promise<string[]>;
13
+ declare const getData: (key: string, fallback?: any) => Promise<any>;
14
+ declare const delKey: (key: string) => Promise<boolean>;
15
+ declare class RedisWriteBlockedError extends Error {
16
+ constructor(message: string);
17
+ }
18
+ declare const delKeyWithOptions: (key: string, options?: DelKeyOptions) => Promise<boolean>;
19
+ declare const setData: <T>(key: string, data: T, options?: Options) => Promise<void>;
20
+ declare const redisKeys: {
21
+ users: () => string;
22
+ user: (userName: string) => string;
23
+ bots: (userName: string) => string;
24
+ botsPrefix: () => string;
25
+ bot: (userName: string, botId: string) => string;
26
+ backtestConfig: (userName: string, config: string) => string;
27
+ strategies: (userName: string) => string;
28
+ strategyConfig: (userName: string, strategyName: string) => string;
29
+ strategyResults: (userName: string, strategyName: string) => string;
30
+ tests: (userName: string, strategyName?: string) => string;
31
+ testOrders: (userName: string, strategyName: string, testName: string) => string;
32
+ testConfig: (userName: string, strategyName: string, testName: string) => string;
33
+ testStat: (userName: string, strategyName: string, testName: string) => string;
34
+ cacheChunk: (userName: string, chunkId: string) => string;
35
+ cacheOrders: (userName: string, orderLogId: string) => string;
36
+ cachePositions: (userName: string, orderLogId: string) => string;
37
+ signal: (symbol: string, signalId: string) => string;
38
+ signalsBySymbol: (symbol: string) => string;
39
+ storeSignal: (symbol: string, signalId: string) => string;
40
+ analysis: (symbol: string, signalId: string) => string;
41
+ backtestResults: (userName: string, config: string, timestamp: string) => string;
42
+ mlSignalsByStrategy: (strategyName: string) => string;
43
+ mlSignals: () => string;
44
+ mlSignal: (strategyName: string, signalId: string) => string;
45
+ mlResultsByStrategy: (strategyName: string) => string;
46
+ mlResults: () => string;
47
+ mlResult: (strategyName: string, signalId: string) => string;
48
+ };
49
+
50
+ export { RedisWriteBlockedError, delKey, delKeyWithOptions, getData, getKeys, redisKeys, setData };
@@ -0,0 +1,50 @@
1
+ import Redis from 'ioredis';
2
+
3
+ declare global {
4
+ var __redis__: Redis | undefined;
5
+ }
6
+ interface Options {
7
+ expire?: number;
8
+ }
9
+ interface DelKeyOptions {
10
+ raiseOnMisconf?: boolean;
11
+ }
12
+ declare const getKeys: (prefix: string) => Promise<string[]>;
13
+ declare const getData: (key: string, fallback?: any) => Promise<any>;
14
+ declare const delKey: (key: string) => Promise<boolean>;
15
+ declare class RedisWriteBlockedError extends Error {
16
+ constructor(message: string);
17
+ }
18
+ declare const delKeyWithOptions: (key: string, options?: DelKeyOptions) => Promise<boolean>;
19
+ declare const setData: <T>(key: string, data: T, options?: Options) => Promise<void>;
20
+ declare const redisKeys: {
21
+ users: () => string;
22
+ user: (userName: string) => string;
23
+ bots: (userName: string) => string;
24
+ botsPrefix: () => string;
25
+ bot: (userName: string, botId: string) => string;
26
+ backtestConfig: (userName: string, config: string) => string;
27
+ strategies: (userName: string) => string;
28
+ strategyConfig: (userName: string, strategyName: string) => string;
29
+ strategyResults: (userName: string, strategyName: string) => string;
30
+ tests: (userName: string, strategyName?: string) => string;
31
+ testOrders: (userName: string, strategyName: string, testName: string) => string;
32
+ testConfig: (userName: string, strategyName: string, testName: string) => string;
33
+ testStat: (userName: string, strategyName: string, testName: string) => string;
34
+ cacheChunk: (userName: string, chunkId: string) => string;
35
+ cacheOrders: (userName: string, orderLogId: string) => string;
36
+ cachePositions: (userName: string, orderLogId: string) => string;
37
+ signal: (symbol: string, signalId: string) => string;
38
+ signalsBySymbol: (symbol: string) => string;
39
+ storeSignal: (symbol: string, signalId: string) => string;
40
+ analysis: (symbol: string, signalId: string) => string;
41
+ backtestResults: (userName: string, config: string, timestamp: string) => string;
42
+ mlSignalsByStrategy: (strategyName: string) => string;
43
+ mlSignals: () => string;
44
+ mlSignal: (strategyName: string, signalId: string) => string;
45
+ mlResultsByStrategy: (strategyName: string) => string;
46
+ mlResults: () => string;
47
+ mlResult: (strategyName: string, signalId: string) => string;
48
+ };
49
+
50
+ export { RedisWriteBlockedError, delKey, delKeyWithOptions, getData, getKeys, redisKeys, setData };
package/dist/redis.js ADDED
@@ -0,0 +1,337 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/redis.ts
31
+ var redis_exports = {};
32
+ __export(redis_exports, {
33
+ RedisWriteBlockedError: () => RedisWriteBlockedError,
34
+ delKey: () => delKey,
35
+ delKeyWithOptions: () => delKeyWithOptions,
36
+ getData: () => getData,
37
+ getKeys: () => getKeys,
38
+ redisKeys: () => redisKeys,
39
+ setData: () => setData
40
+ });
41
+ module.exports = __toCommonJS(redis_exports);
42
+ var import_ioredis = __toESM(require("ioredis"));
43
+ var TTL_1D = 86400;
44
+ var toJson = (value) => JSON.stringify(value);
45
+ var logger = {
46
+ log: (level, message, ...args) => {
47
+ const method = level === "error" ? "error" : level === "warn" ? "warn" : "log";
48
+ console[method](`[infra:redis] ${message}`, ...args);
49
+ }
50
+ };
51
+ var redisConnectionWarningShown = false;
52
+ var redisUnavailable = false;
53
+ var isRedisConnectivityError = (error) => /ECONNREFUSED|ENOTFOUND|EAI_AGAIN|ETIMEDOUT|MaxRetriesPerRequestError|Connection is closed|Stream isn't writeable/i.test(
54
+ error.message
55
+ );
56
+ var toNonNegativeInt = (value, fallback) => {
57
+ const parsed = Number.parseInt(String(value ?? ""), 10);
58
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback;
59
+ };
60
+ var toPositiveInt = (value, fallback) => {
61
+ const parsed = Number.parseInt(String(value ?? ""), 10);
62
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
63
+ };
64
+ var markRedisUnavailable = (error) => {
65
+ redisUnavailable = true;
66
+ if (redisConnectionWarningShown) return;
67
+ redisConnectionWarningShown = true;
68
+ logger.log(
69
+ "warn",
70
+ "Redis is unavailable: %s. Cache-dependent features are temporarily disabled.",
71
+ error.message
72
+ );
73
+ };
74
+ var getRedis = () => {
75
+ if (!global.__redis__) {
76
+ const host = process.env.REDIS_HOST || "127.0.0.1";
77
+ const port = toPositiveInt(process.env.REDIS_PORT, 6379);
78
+ const connectTimeout = toPositiveInt(
79
+ process.env.REDIS_CONNECT_TIMEOUT_MS,
80
+ 3e3
81
+ );
82
+ const maxRetriesPerRequest = toNonNegativeInt(
83
+ process.env.REDIS_MAX_RETRIES_PER_REQUEST,
84
+ 1
85
+ );
86
+ global.__redis__ = new import_ioredis.default({
87
+ host,
88
+ port,
89
+ connectTimeout,
90
+ maxRetriesPerRequest,
91
+ enableOfflineQueue: false,
92
+ retryStrategy: (attempt) => Math.min(attempt * 200, 2e3)
93
+ });
94
+ global.__redis__.on("error", (error) => {
95
+ if (isRedisConnectivityError(error)) {
96
+ markRedisUnavailable(error);
97
+ return;
98
+ }
99
+ logger.log("error", "Redis client error: %s", String(error));
100
+ });
101
+ global.__redis__.on("ready", () => {
102
+ redisUnavailable = false;
103
+ if (redisConnectionWarningShown) {
104
+ redisConnectionWarningShown = false;
105
+ logger.log("info", "Redis connection restored");
106
+ }
107
+ });
108
+ }
109
+ return global.__redis__;
110
+ };
111
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
112
+ var getRedisStatus = (redis) => String(redis.status ?? "ready");
113
+ var waitForRedisReady = async (redis) => {
114
+ if (getRedisStatus(redis) === "ready") {
115
+ return true;
116
+ }
117
+ const readyTimeoutMs = toPositiveInt(
118
+ process.env.REDIS_READY_TIMEOUT_MS,
119
+ toPositiveInt(process.env.REDIS_CONNECT_TIMEOUT_MS, 3e3)
120
+ );
121
+ const startedAt = Date.now();
122
+ while (Date.now() - startedAt < readyTimeoutMs) {
123
+ if (getRedisStatus(redis) === "ready") {
124
+ return true;
125
+ }
126
+ if (getRedisStatus(redis) === "end") {
127
+ return false;
128
+ }
129
+ await sleep(50);
130
+ }
131
+ return getRedisStatus(redis) === "ready";
132
+ };
133
+ var getReadyRedis = async () => {
134
+ if (redisUnavailable) return null;
135
+ const redis = getRedis();
136
+ const ready = await waitForRedisReady(redis);
137
+ if (ready) {
138
+ return redis;
139
+ }
140
+ markRedisUnavailable(
141
+ new Error(`Redis is not ready (status=${getRedisStatus(redis)})`)
142
+ );
143
+ return null;
144
+ };
145
+ var toResultString = (result) => {
146
+ if (result == null) return null;
147
+ if (typeof result === "string") return result;
148
+ if (Buffer.isBuffer(result)) return result.toString("utf8");
149
+ return String(result);
150
+ };
151
+ var DEFAULT_OPTIONS = {
152
+ expire: TTL_1D
153
+ };
154
+ var parseJsonOrDeleteKey = async (redis, key, raw, fallback) => {
155
+ try {
156
+ return JSON.parse(raw);
157
+ } catch (e) {
158
+ logger.log("error", "failed JSON.parse(%s): %s", key, String(e));
159
+ await redis.del(key);
160
+ return fallback;
161
+ }
162
+ };
163
+ var getKeys = async (prefix) => {
164
+ if (redisUnavailable) return [];
165
+ const redis = await getReadyRedis();
166
+ if (!redis) return [];
167
+ const keys = [];
168
+ try {
169
+ let cursor = "0";
170
+ do {
171
+ const [nextCursor, batch] = await redis.scan(
172
+ cursor,
173
+ "MATCH",
174
+ `${prefix}*`,
175
+ "COUNT",
176
+ "200"
177
+ );
178
+ cursor = nextCursor;
179
+ for (const key of batch) {
180
+ if (key.startsWith(prefix)) {
181
+ keys.push(key);
182
+ }
183
+ }
184
+ } while (cursor !== "0");
185
+ } catch (e) {
186
+ if (e instanceof Error && isRedisConnectivityError(e)) {
187
+ markRedisUnavailable(e);
188
+ return [];
189
+ }
190
+ logger.log("warn", "failed SCAN for %s: %s", prefix, String(e));
191
+ return [];
192
+ }
193
+ return keys;
194
+ };
195
+ var getData = async (key, fallback = []) => {
196
+ if (redisUnavailable) return fallback;
197
+ const redis = await getReadyRedis();
198
+ if (!redis) return fallback;
199
+ try {
200
+ const rawJson = await redis.call("JSON.GET", key);
201
+ const raw = toResultString(rawJson);
202
+ if (raw == null) return fallback;
203
+ return parseJsonOrDeleteKey(redis, key, raw, fallback);
204
+ } catch (e) {
205
+ if (e instanceof Error && isRedisConnectivityError(e)) {
206
+ markRedisUnavailable(e);
207
+ return fallback;
208
+ }
209
+ logger.log(
210
+ "error",
211
+ "failed JSON.GET %s: %s (fallback to GET)",
212
+ key,
213
+ String(e)
214
+ );
215
+ }
216
+ try {
217
+ const raw = await redis.get(key);
218
+ if (raw == null) return fallback;
219
+ return parseJsonOrDeleteKey(redis, key, raw, fallback);
220
+ } catch (e) {
221
+ if (e instanceof Error && isRedisConnectivityError(e)) {
222
+ markRedisUnavailable(e);
223
+ return fallback;
224
+ }
225
+ logger.log("error", "failed GET %s: %s", key, String(e));
226
+ return fallback;
227
+ }
228
+ };
229
+ var delKey = async (key) => {
230
+ return delKeyWithOptions(key);
231
+ };
232
+ var RedisWriteBlockedError = class extends Error {
233
+ constructor(message) {
234
+ super(message);
235
+ this.name = "RedisWriteBlockedError";
236
+ }
237
+ };
238
+ var delKeyWithOptions = async (key, options = {}) => {
239
+ if (redisUnavailable) return false;
240
+ const { raiseOnMisconf = false } = options;
241
+ const redis = await getReadyRedis();
242
+ if (!redis) return false;
243
+ try {
244
+ const result = await redis.del(key);
245
+ if (result === 1) {
246
+ return true;
247
+ }
248
+ return false;
249
+ } catch (e) {
250
+ if (e instanceof Error && isRedisConnectivityError(e)) {
251
+ markRedisUnavailable(e);
252
+ return false;
253
+ }
254
+ const msg = String(e);
255
+ if (raiseOnMisconf && msg.includes("MISCONF")) {
256
+ throw new RedisWriteBlockedError(msg);
257
+ }
258
+ logger.log("error", "failed DEL %s: %s", key, String(e));
259
+ return false;
260
+ }
261
+ };
262
+ var setData = async (key, data, options = {}) => {
263
+ if (redisUnavailable) return;
264
+ const { expire } = { ...DEFAULT_OPTIONS, ...options };
265
+ const redis = await getReadyRedis();
266
+ if (!redis) return;
267
+ const value = toJson(data);
268
+ try {
269
+ await redis.call("JSON.SET", key, "$", value);
270
+ if (expire) {
271
+ await redis.expire(key, expire);
272
+ }
273
+ } catch (e) {
274
+ if (e instanceof Error && isRedisConnectivityError(e)) {
275
+ markRedisUnavailable(e);
276
+ return;
277
+ }
278
+ logger.log(
279
+ "error",
280
+ "failed JSON.SET %s: %s (fallback to SET)",
281
+ key,
282
+ String(e)
283
+ );
284
+ try {
285
+ if (expire) {
286
+ await redis.set(key, value, "EX", expire);
287
+ } else {
288
+ await redis.set(key, value);
289
+ }
290
+ } catch (e2) {
291
+ if (e2 instanceof Error && isRedisConnectivityError(e2)) {
292
+ markRedisUnavailable(e2);
293
+ return;
294
+ }
295
+ logger.log("error", "failed SET %s: %s", key, String(e2));
296
+ }
297
+ }
298
+ };
299
+ var redisKeys = {
300
+ users: () => "users:index:",
301
+ user: (userName) => `users:index:${userName}`,
302
+ bots: (userName) => `users:${userName}:bots`,
303
+ botsPrefix: () => "users:",
304
+ bot: (userName, botId) => `users:${userName}:bots:${botId}`,
305
+ backtestConfig: (userName, config) => `users:${userName}:backtests:configs:${config}`,
306
+ strategies: (userName) => `users:${userName}:strategies`,
307
+ strategyConfig: (userName, strategyName) => `users:${userName}:strategies:${strategyName}:config`,
308
+ strategyResults: (userName, strategyName) => `users:${userName}:strategies:${strategyName}:results`,
309
+ tests: (userName, strategyName) => strategyName ? `users:${userName}:tests:${strategyName}` : `users:${userName}:tests:`,
310
+ testOrders: (userName, strategyName, testName) => `users:${userName}:tests:${strategyName}:${testName}:orders`,
311
+ testConfig: (userName, strategyName, testName) => `users:${userName}:tests:${strategyName}:${testName}:config`,
312
+ testStat: (userName, strategyName, testName) => `users:${userName}:tests:${strategyName}:${testName}:stat`,
313
+ cacheChunk: (userName, chunkId) => `users:${userName}:cache:tests:chunks:${chunkId}`,
314
+ cacheOrders: (userName, orderLogId) => `users:${userName}:cache:tests:orders:${orderLogId}`,
315
+ cachePositions: (userName, orderLogId) => `users:${userName}:cache:tests:positions:${orderLogId}`,
316
+ signal: (symbol, signalId) => `signals:${symbol}:${signalId}`,
317
+ signalsBySymbol: (symbol) => `signals:${symbol}:`,
318
+ storeSignal: (symbol, signalId) => `store:signals:${symbol}:${signalId}`,
319
+ analysis: (symbol, signalId) => `analysis:${symbol}:${signalId}`,
320
+ backtestResults: (userName, config, timestamp) => `users:${userName}:backtests:results:${config}:${timestamp}`,
321
+ mlSignalsByStrategy: (strategyName) => `ml:${strategyName}:signals:`,
322
+ mlSignals: () => "ml:",
323
+ mlSignal: (strategyName, signalId) => `ml:${strategyName}:signals:${signalId}`,
324
+ mlResultsByStrategy: (strategyName) => `ml:${strategyName}:results:`,
325
+ mlResults: () => "ml:",
326
+ mlResult: (strategyName, signalId) => `ml:${strategyName}:results:${signalId}`
327
+ };
328
+ // Annotate the CommonJS export names for ESM import in node:
329
+ 0 && (module.exports = {
330
+ RedisWriteBlockedError,
331
+ delKey,
332
+ delKeyWithOptions,
333
+ getData,
334
+ getKeys,
335
+ redisKeys,
336
+ setData
337
+ });