biz-a-cli 2.3.78 → 2.3.79-15211

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.
@@ -1,6 +1,7 @@
1
1
  import workerpool from "workerpool";
2
2
  import { loadCliScript, extractFunctionScript } from "../scheduler/datalib.js";
3
3
  import { createLogger, transports, format } from "winston";
4
+ import * as db from "../db/db.js";
4
5
 
5
6
  const logger = createLogger({
6
7
  // logger must used worker thread context
@@ -22,6 +23,37 @@ if (process.env.NODE_ENV !== "production") {
22
23
  );
23
24
  }
24
25
 
26
+ // export const run = async (apiConfig, scriptIdentifier, scriptData) => {
27
+ // const searchBy =
28
+ // typeof scriptIdentifier === "object"
29
+ // ? scriptIdentifier.searchBy
30
+ // : "SCRIPT_NAME";
31
+ // const searchValue =
32
+ // typeof scriptIdentifier === "object"
33
+ // ? scriptIdentifier.searchValue
34
+ // : scriptIdentifier;
35
+ // try {
36
+ // const functions = await extractFunctionScript(
37
+ // apiConfig,
38
+ // await loadCliScript(apiConfig, searchBy, searchValue),
39
+ // );
40
+ // const result =
41
+ // functions && functions.onInit
42
+ // ? await functions.onInit(scriptData)
43
+ // : "'onInit' not found. Nothing to execute";
44
+
45
+ // logger.info(
46
+ // `CLI script with ${searchBy} = ${searchValue} success. Result : ${result}`,
47
+ // );
48
+ // return result;
49
+ // } catch (err) {
50
+ // logger.error(
51
+ // `CLI script with ${searchBy} = ${searchValue} fail. Error : ${err.message || err}`,
52
+ // );
53
+ // throw new Error(err);
54
+ // }
55
+ // };
56
+
25
57
  export const run = async (apiConfig, scriptIdentifier, scriptData) => {
26
58
  const searchBy =
27
59
  typeof scriptIdentifier === "object"
@@ -31,18 +63,70 @@ export const run = async (apiConfig, scriptIdentifier, scriptData) => {
31
63
  typeof scriptIdentifier === "object"
32
64
  ? scriptIdentifier.searchValue
33
65
  : scriptIdentifier;
66
+
67
+ const injectESBMethods = (lib) => {
68
+ // 1. System Choreography (Fire-and-forget internal services)
69
+ lib.emitCLI = (topic, payload) => {
70
+ workerpool.workerEmit({ type: "IPC_RELAY", topic, payload });
71
+ };
72
+
73
+ // 2. Targeted Client UI Update
74
+ lib.emitClient = (clientSockId, eventUUIDName, payload) => {
75
+ workerpool.workerEmit({
76
+ type: "IPC_RELAY",
77
+ topic: "emitClient",
78
+ payload: { clientSockId, eventUUIDName, payload },
79
+ });
80
+ };
81
+
82
+ // 3. Broadcast Client UI Update
83
+ lib.broadcastClient = (topic, payload) => {
84
+ workerpool.workerEmit({
85
+ type: "IPC_RELAY",
86
+ topic: "broadcastClient",
87
+ payload: { topic, payload },
88
+ });
89
+ };
90
+ };
91
+
92
+ const injectDBMethods = (lib, baseConfig) => {
93
+ const mergeConfig = (customCfg) =>
94
+ customCfg ? { ...baseConfig, ...customCfg } : baseConfig;
95
+
96
+ lib.db = {
97
+ queryData: (payload, customCfg) =>
98
+ db.queryData(payload, mergeConfig(customCfg)),
99
+ save: (payload, customCfg) =>
100
+ db.save(payload, mergeConfig(customCfg)),
101
+ remove: (payload, customCfg) =>
102
+ db.remove(payload, mergeConfig(customCfg)),
103
+ deleteMD: (id, master, details, customCfg) =>
104
+ db.deleteMD(id, master, details, mergeConfig(customCfg)),
105
+ list: (payload, customCfg) =>
106
+ db.list(payload, mergeConfig(customCfg)),
107
+ executeBlock: (sql, customCfg) =>
108
+ db.executeBlock(sql, mergeConfig(customCfg)),
109
+ };
110
+ };
111
+
34
112
  try {
113
+ const extraLib = {};
114
+ injectESBMethods(extraLib);
115
+ injectDBMethods(extraLib, apiConfig);
116
+
35
117
  const functions = await extractFunctionScript(
36
118
  apiConfig,
37
119
  await loadCliScript(apiConfig, searchBy, searchValue),
120
+ extraLib,
38
121
  );
122
+
39
123
  const result =
40
124
  functions && functions.onInit
41
125
  ? await functions.onInit(scriptData)
42
126
  : "'onInit' not found. Nothing to execute";
43
127
 
44
128
  logger.info(
45
- `CLI script with ${searchBy} = ${searchValue} success. Result : ${result}`,
129
+ `CLI script with ${searchBy} = ${searchValue} success. Result : ${typeof result === "object" ? JSON.stringify(result) : result}`,
46
130
  );
47
131
  return result;
48
132
  } catch (err) {
@@ -1,9 +1,11 @@
1
1
  import workerpool from "workerpool";
2
2
  import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
+ import { EventEmitter } from "node:events";
4
5
 
5
6
  const __filename = fileURLToPath(import.meta.url);
6
7
  const __dirname = path.dirname(__filename);
8
+ export const workerEvents = new EventEmitter();
7
9
 
8
10
  export const cliScriptWorkerPool = workerpool.pool(
9
11
  // "./worker/cliScriptWorker.js",
@@ -58,7 +60,20 @@ export const execCLIScriptWorker = async (
58
60
  // Worker threads can only accept strings, numbers, booleans, and pure JSON objects/arrays.
59
61
  // It cannot serialize live functions, object or DOM nodes
60
62
  const safeData = JSON.parse(JSON.stringify(scriptData));
61
- return cliScriptWorkerPool.exec("run", [apiConfig, scriptName, safeData]);
63
+
64
+ if (!apiConfig.url) {
65
+ apiConfig.url = `${apiConfig["secure"] == true ? "https://" : "http://"}${apiConfig["hostname"]}:${apiConfig["port"]}`; // FINA API url, needed by datalib.js
66
+ }
67
+ const safeConfig = JSON.parse(JSON.stringify(apiConfig || {}));
68
+
69
+ return cliScriptWorkerPool.exec("run", [safeConfig, scriptName, safeData], {
70
+ on: (message) => {
71
+ // Catch the relay and bounce it to the main thread
72
+ if (message && message.type === "IPC_RELAY") {
73
+ workerEvents.emit("IPC_RELAY", message.topic, message.payload);
74
+ }
75
+ },
76
+ });
62
77
  };
63
78
 
64
79
  export const workerStats = () => {