@tradejs/infra 1.0.2 → 1.0.3

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/dist/files.d.mts CHANGED
@@ -1,9 +1,10 @@
1
1
  interface Options {
2
2
  stringify?: boolean;
3
3
  lock?: boolean;
4
+ projectRoot?: string;
4
5
  }
5
- declare const getFiles: (dir: string) => Promise<string[]>;
6
- declare const getFile: (dir: string, file: string, fallback?: never[]) => Promise<any>;
6
+ declare const getFiles: (dir: string, projectRoot?: string) => Promise<string[]>;
7
+ declare const getFile: (dir: string, file: string, fallback?: never[], projectRoot?: string) => Promise<any>;
7
8
  declare const setFile: <T>(dir: string, file: string, data: T, options?: Options) => Promise<void>;
8
9
 
9
10
  export { getFile, getFiles, setFile };
package/dist/files.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  interface Options {
2
2
  stringify?: boolean;
3
3
  lock?: boolean;
4
+ projectRoot?: string;
4
5
  }
5
- declare const getFiles: (dir: string) => Promise<string[]>;
6
- declare const getFile: (dir: string, file: string, fallback?: never[]) => Promise<any>;
6
+ declare const getFiles: (dir: string, projectRoot?: string) => Promise<string[]>;
7
+ declare const getFile: (dir: string, file: string, fallback?: never[], projectRoot?: string) => Promise<any>;
7
8
  declare const setFile: <T>(dir: string, file: string, data: T, options?: Options) => Promise<void>;
8
9
 
9
10
  export { getFile, getFiles, setFile };
package/dist/files.js CHANGED
@@ -46,23 +46,30 @@ var toJson = (value, pretty = false) => JSON.stringify(value, null, pretty ? 2 :
46
46
  var logError = (message, ...args) => {
47
47
  console.error(`[infra:files] ${message}`, ...args);
48
48
  };
49
- var getPath = (dir, file, lock = false) => {
49
+ var resolveProjectRoot = (projectRoot) => {
50
+ const explicit = String(projectRoot || "").trim();
51
+ if (explicit) {
52
+ return import_path.default.resolve(explicit);
53
+ }
54
+ const fromEnv = String(process.env.PROJECT_CWD || "").trim();
55
+ if (fromEnv) {
56
+ return import_path.default.resolve(fromEnv);
57
+ }
58
+ return process.cwd();
59
+ };
60
+ var getPath = (dir, file, lock = false, projectRoot) => {
61
+ const root = resolveProjectRoot(projectRoot);
50
62
  if (!lock) {
51
- return import_path.default.join(process.cwd(), dir, `${file}.json`);
63
+ return import_path.default.join(root, dir, `${file}.json`);
52
64
  }
53
- return import_path.default.join(
54
- process.cwd(),
55
- "data",
56
- "cache",
57
- `${file}.lock.${(0, import_crypto.randomUUID)()}.json`
58
- );
65
+ return import_path.default.join(root, "data", "cache", `${file}.lock.${(0, import_crypto.randomUUID)()}.json`);
59
66
  };
60
- var getDir = (dir) => import_path.default.join(process.cwd(), dir);
61
- var getFiles = async (dir) => {
62
- return await import_promises.default.readdir(getDir(dir));
67
+ var getDir = (dir, projectRoot) => import_path.default.join(resolveProjectRoot(projectRoot), dir);
68
+ var getFiles = async (dir, projectRoot) => {
69
+ return await import_promises.default.readdir(getDir(dir, projectRoot));
63
70
  };
64
- var getFile = async (dir, file, fallback = []) => {
65
- const fullPath = getPath(dir, file);
71
+ var getFile = async (dir, file, fallback = [], projectRoot) => {
72
+ const fullPath = getPath(dir, file, false, projectRoot);
66
73
  try {
67
74
  await import_promises.default.access(fullPath);
68
75
  } catch {
@@ -78,12 +85,12 @@ var getFile = async (dir, file, fallback = []) => {
78
85
  }
79
86
  };
80
87
  var setFile = async (dir, file, data, options = {}) => {
81
- const { stringify, lock } = {
88
+ const { stringify, lock, projectRoot } = {
82
89
  ...DEFAULT_OPTIONS,
83
90
  ...options
84
91
  };
85
- const fullPath = getPath(dir, file);
86
- const lockFullPath = getPath(dir, file, true);
92
+ const fullPath = getPath(dir, file, false, projectRoot);
93
+ const lockFullPath = getPath(dir, file, true, projectRoot);
87
94
  try {
88
95
  if (!lock) {
89
96
  await import_promises.default.writeFile(fullPath, toJson(data, stringify));
package/dist/files.mjs CHANGED
@@ -10,23 +10,30 @@ var toJson = (value, pretty = false) => JSON.stringify(value, null, pretty ? 2 :
10
10
  var logError = (message, ...args) => {
11
11
  console.error(`[infra:files] ${message}`, ...args);
12
12
  };
13
- var getPath = (dir, file, lock = false) => {
13
+ var resolveProjectRoot = (projectRoot) => {
14
+ const explicit = String(projectRoot || "").trim();
15
+ if (explicit) {
16
+ return path.resolve(explicit);
17
+ }
18
+ const fromEnv = String(process.env.PROJECT_CWD || "").trim();
19
+ if (fromEnv) {
20
+ return path.resolve(fromEnv);
21
+ }
22
+ return process.cwd();
23
+ };
24
+ var getPath = (dir, file, lock = false, projectRoot) => {
25
+ const root = resolveProjectRoot(projectRoot);
14
26
  if (!lock) {
15
- return path.join(process.cwd(), dir, `${file}.json`);
27
+ return path.join(root, dir, `${file}.json`);
16
28
  }
17
- return path.join(
18
- process.cwd(),
19
- "data",
20
- "cache",
21
- `${file}.lock.${randomUUID()}.json`
22
- );
29
+ return path.join(root, "data", "cache", `${file}.lock.${randomUUID()}.json`);
23
30
  };
24
- var getDir = (dir) => path.join(process.cwd(), dir);
25
- var getFiles = async (dir) => {
26
- return await fs.readdir(getDir(dir));
31
+ var getDir = (dir, projectRoot) => path.join(resolveProjectRoot(projectRoot), dir);
32
+ var getFiles = async (dir, projectRoot) => {
33
+ return await fs.readdir(getDir(dir, projectRoot));
27
34
  };
28
- var getFile = async (dir, file, fallback = []) => {
29
- const fullPath = getPath(dir, file);
35
+ var getFile = async (dir, file, fallback = [], projectRoot) => {
36
+ const fullPath = getPath(dir, file, false, projectRoot);
30
37
  try {
31
38
  await fs.access(fullPath);
32
39
  } catch {
@@ -42,12 +49,12 @@ var getFile = async (dir, file, fallback = []) => {
42
49
  }
43
50
  };
44
51
  var setFile = async (dir, file, data, options = {}) => {
45
- const { stringify, lock } = {
52
+ const { stringify, lock, projectRoot } = {
46
53
  ...DEFAULT_OPTIONS,
47
54
  ...options
48
55
  };
49
- const fullPath = getPath(dir, file);
50
- const lockFullPath = getPath(dir, file, true);
56
+ const fullPath = getPath(dir, file, false, projectRoot);
57
+ const lockFullPath = getPath(dir, file, true, projectRoot);
51
58
  try {
52
59
  if (!lock) {
53
60
  await fs.writeFile(fullPath, toJson(data, stringify));
package/dist/ml.d.mts CHANGED
@@ -28,9 +28,11 @@ type MlPredictParams = {
28
28
  features: Record<string, number>;
29
29
  threshold: number;
30
30
  grpcAddress?: string;
31
+ protoPath?: string;
32
+ projectRoot?: string;
31
33
  };
32
34
  declare const buildMlFeatures: (row: Record<string, unknown>) => Record<string, number>;
33
- declare const fetchMlThreshold: ({ strategy, features, threshold, grpcAddress, }: MlPredictParams) => Promise<MlPredictResponse | null>;
35
+ declare const fetchMlThreshold: ({ strategy, features, threshold, grpcAddress, protoPath, projectRoot, }: MlPredictParams) => Promise<MlPredictResponse | null>;
34
36
 
35
37
  type MlSignalRecord = {
36
38
  signal: MlSignalPayload;
package/dist/ml.d.ts CHANGED
@@ -28,9 +28,11 @@ type MlPredictParams = {
28
28
  features: Record<string, number>;
29
29
  threshold: number;
30
30
  grpcAddress?: string;
31
+ protoPath?: string;
32
+ projectRoot?: string;
31
33
  };
32
34
  declare const buildMlFeatures: (row: Record<string, unknown>) => Record<string, number>;
33
- declare const fetchMlThreshold: ({ strategy, features, threshold, grpcAddress, }: MlPredictParams) => Promise<MlPredictResponse | null>;
35
+ declare const fetchMlThreshold: ({ strategy, features, threshold, grpcAddress, protoPath, projectRoot, }: MlPredictParams) => Promise<MlPredictResponse | null>;
34
36
 
35
37
  type MlSignalRecord = {
36
38
  signal: MlSignalPayload;
package/dist/ml.js CHANGED
@@ -215,11 +215,16 @@ var logger = (0, import_winston.createLogger)({
215
215
 
216
216
  // src/mlGrpc.ts
217
217
  var clientCache = /* @__PURE__ */ new Map();
218
- var resolveProtoPath = () => {
218
+ var resolveProtoPath = (projectRoot, protoPath) => {
219
+ if (protoPath && import_fs2.default.existsSync(protoPath)) {
220
+ return protoPath;
221
+ }
222
+ const explicitRoot = String(projectRoot || "").trim();
223
+ const root = explicitRoot ? import_path2.default.resolve(explicitRoot) : String(process.env.PROJECT_CWD || "").trim() ? import_path2.default.resolve(String(process.env.PROJECT_CWD || "").trim()) : process.cwd();
219
224
  const candidates = [
220
225
  import_path2.default.resolve(__dirname, "../proto/ml_infer.proto"),
221
226
  import_path2.default.resolve(__dirname, "../../proto/ml_infer.proto"),
222
- import_path2.default.resolve(process.cwd(), "proto/ml_infer.proto")
227
+ import_path2.default.resolve(root, "proto/ml_infer.proto")
223
228
  ];
224
229
  for (const candidate of candidates) {
225
230
  if (import_fs2.default.existsSync(candidate)) {
@@ -228,11 +233,12 @@ var resolveProtoPath = () => {
228
233
  }
229
234
  return candidates[candidates.length - 1];
230
235
  };
231
- var getClient = (address) => {
232
- const cached = clientCache.get(address);
236
+ var getClient = (address, projectRoot, protoPath) => {
237
+ const resolvedProtoPath = resolveProtoPath(projectRoot, protoPath);
238
+ const cacheKey = `${address}::${resolvedProtoPath}`;
239
+ const cached = clientCache.get(cacheKey);
233
240
  if (cached) return cached;
234
- const protoPath = resolveProtoPath();
235
- const packageDefinition = protoLoader.loadSync(protoPath, {
241
+ const packageDefinition = protoLoader.loadSync(resolvedProtoPath, {
236
242
  keepCase: true,
237
243
  longs: String,
238
244
  enums: String,
@@ -244,7 +250,7 @@ var getClient = (address) => {
244
250
  address,
245
251
  grpc.credentials.createInsecure()
246
252
  );
247
- clientCache.set(address, client);
253
+ clientCache.set(cacheKey, client);
248
254
  return client;
249
255
  };
250
256
  var toFiniteNumber = (value) => {
@@ -269,11 +275,13 @@ var fetchMlThreshold = async ({
269
275
  strategy,
270
276
  features,
271
277
  threshold,
272
- grpcAddress
278
+ grpcAddress,
279
+ protoPath,
280
+ projectRoot
273
281
  }) => {
274
282
  try {
275
283
  const address = grpcAddress || process.env.ML_GRPC_ADDRESS || "localhost:50051";
276
- const client = getClient(address);
284
+ const client = getClient(address, projectRoot, protoPath);
277
285
  return await new Promise((resolve, reject) => {
278
286
  client.Predict(
279
287
  {
package/dist/ml.mjs CHANGED
@@ -124,11 +124,16 @@ import * as grpc from "@grpc/grpc-js";
124
124
  import * as protoLoader from "@grpc/proto-loader";
125
125
  import fs2 from "fs";
126
126
  var clientCache = /* @__PURE__ */ new Map();
127
- var resolveProtoPath = () => {
127
+ var resolveProtoPath = (projectRoot, protoPath) => {
128
+ if (protoPath && fs2.existsSync(protoPath)) {
129
+ return protoPath;
130
+ }
131
+ const explicitRoot = String(projectRoot || "").trim();
132
+ const root = explicitRoot ? path2.resolve(explicitRoot) : String(process.env.PROJECT_CWD || "").trim() ? path2.resolve(String(process.env.PROJECT_CWD || "").trim()) : process.cwd();
128
133
  const candidates = [
129
134
  path2.resolve(__dirname, "../proto/ml_infer.proto"),
130
135
  path2.resolve(__dirname, "../../proto/ml_infer.proto"),
131
- path2.resolve(process.cwd(), "proto/ml_infer.proto")
136
+ path2.resolve(root, "proto/ml_infer.proto")
132
137
  ];
133
138
  for (const candidate of candidates) {
134
139
  if (fs2.existsSync(candidate)) {
@@ -137,11 +142,12 @@ var resolveProtoPath = () => {
137
142
  }
138
143
  return candidates[candidates.length - 1];
139
144
  };
140
- var getClient = (address) => {
141
- const cached = clientCache.get(address);
145
+ var getClient = (address, projectRoot, protoPath) => {
146
+ const resolvedProtoPath = resolveProtoPath(projectRoot, protoPath);
147
+ const cacheKey = `${address}::${resolvedProtoPath}`;
148
+ const cached = clientCache.get(cacheKey);
142
149
  if (cached) return cached;
143
- const protoPath = resolveProtoPath();
144
- const packageDefinition = protoLoader.loadSync(protoPath, {
150
+ const packageDefinition = protoLoader.loadSync(resolvedProtoPath, {
145
151
  keepCase: true,
146
152
  longs: String,
147
153
  enums: String,
@@ -153,7 +159,7 @@ var getClient = (address) => {
153
159
  address,
154
160
  grpc.credentials.createInsecure()
155
161
  );
156
- clientCache.set(address, client);
162
+ clientCache.set(cacheKey, client);
157
163
  return client;
158
164
  };
159
165
  var toFiniteNumber = (value) => {
@@ -178,11 +184,13 @@ var fetchMlThreshold = async ({
178
184
  strategy,
179
185
  features,
180
186
  threshold,
181
- grpcAddress
187
+ grpcAddress,
188
+ protoPath,
189
+ projectRoot
182
190
  }) => {
183
191
  try {
184
192
  const address = grpcAddress || process.env.ML_GRPC_ADDRESS || "localhost:50051";
185
- const client = getClient(address);
193
+ const client = getClient(address, projectRoot, protoPath);
186
194
  return await new Promise((resolve, reject) => {
187
195
  client.Predict(
188
196
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tradejs/infra",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Server-only infrastructure adapters for Redis, Timescale, ML, logging, and IO.",
5
5
  "keywords": [
6
6
  "tradejs",
@@ -50,7 +50,7 @@
50
50
  "dependencies": {
51
51
  "@grpc/grpc-js": "^1.10.7",
52
52
  "@grpc/proto-loader": "^0.7.13",
53
- "@tradejs/types": "^1.0.2",
53
+ "@tradejs/types": "^1.0.3",
54
54
  "chalk": "4.1.2",
55
55
  "ioredis": "5.8.0",
56
56
  "pg": "8.16.3",