suanpan_node_sdk 2.2.4 → 2.2.5-beta

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 (123) hide show
  1. package/dist/api/index.d.ts +10 -10
  2. package/dist/api/index.js +56 -52
  3. package/dist/api/index.js.map +1 -1
  4. package/dist/core/common/constant.d.ts +6 -6
  5. package/dist/core/common/constant.js +50 -50
  6. package/dist/core/common/exec.d.ts +1 -1
  7. package/dist/core/common/exec.js +53 -53
  8. package/dist/core/common/log.d.ts +3 -3
  9. package/dist/core/common/log.js +15 -15
  10. package/dist/core/event/index.d.ts +6 -6
  11. package/dist/core/event/index.js +10 -10
  12. package/dist/core/logkit/index.d.ts +33 -33
  13. package/dist/core/logkit/index.js +125 -125
  14. package/dist/core/logkit/index.js.map +1 -1
  15. package/dist/core/message/channel.d.ts +7 -7
  16. package/dist/core/message/channel.js +2 -2
  17. package/dist/core/message/channelFactory.d.ts +4 -4
  18. package/dist/core/message/channelFactory.js +10 -10
  19. package/dist/core/message/index.d.ts +6 -6
  20. package/dist/core/message/index.js +22 -18
  21. package/dist/core/message/index.js.map +1 -1
  22. package/dist/core/message/message.d.ts +19 -19
  23. package/dist/core/message/message.js +59 -59
  24. package/dist/core/message/message.js.map +1 -1
  25. package/dist/core/message/messageBuilder.d.ts +12 -12
  26. package/dist/core/message/messageBuilder.js +50 -50
  27. package/dist/core/message/messageBuilder.js.map +1 -1
  28. package/dist/core/message/messageChain.d.ts +9 -9
  29. package/dist/core/message/messageChain.js +29 -29
  30. package/dist/core/message/messageListener.d.ts +4 -4
  31. package/dist/core/message/messageListener.js +11 -11
  32. package/dist/core/message/mq/index.d.ts +9 -9
  33. package/dist/core/message/mq/index.js +50 -50
  34. package/dist/core/message/mq/redis.d.ts +12 -12
  35. package/dist/core/message/mq/redis.js +101 -97
  36. package/dist/core/message/mq/redis.js.map +1 -1
  37. package/dist/core/parameter/commandArgs.d.ts +2 -2
  38. package/dist/core/parameter/commandArgs.js +22 -22
  39. package/dist/core/parameter/commandArgs.js.map +1 -1
  40. package/dist/core/parameter/environment.d.ts +86 -86
  41. package/dist/core/parameter/environment.js +103 -103
  42. package/dist/core/parameter/index.d.ts +7 -7
  43. package/dist/core/parameter/index.js +33 -33
  44. package/dist/core/script/script-babel/babel.d.ts +1 -1
  45. package/dist/core/script/script-babel/babel.js +23 -23
  46. package/dist/core/script/script-babel/plugin.d.ts +7 -7
  47. package/dist/core/script/script-babel/plugin.js +16 -16
  48. package/dist/core/script/script-constant.d.ts +36 -36
  49. package/dist/core/script/script-constant.js +70 -70
  50. package/dist/core/script/script-context.d.ts +1 -1
  51. package/dist/core/script/script-context.js +295 -295
  52. package/dist/core/script/script-front.d.ts +1 -1
  53. package/dist/core/script/script-front.js +201 -201
  54. package/dist/core/script/script-insert.d.ts +18 -18
  55. package/dist/core/script/script-insert.js +43 -43
  56. package/dist/core/script/script-manager.d.ts +74 -74
  57. package/dist/core/script/script-manager.js +261 -261
  58. package/dist/core/script/script-storage.d.ts +22 -22
  59. package/dist/core/script/script-storage.js +241 -241
  60. package/dist/core/script/script-support.d.ts +11 -11
  61. package/dist/core/script/script-support.js +140 -140
  62. package/dist/core/script/script-template.d.ts +1 -1
  63. package/dist/core/script/script-template.js +6 -6
  64. package/dist/core/service/port.d.ts +5 -5
  65. package/dist/core/service/port.js +53 -53
  66. package/dist/core/service/port.js.map +1 -1
  67. package/dist/core/storage/index.d.ts +5 -5
  68. package/dist/core/storage/index.js +21 -17
  69. package/dist/core/storage/index.js.map +1 -1
  70. package/dist/core/storage/minioStorage.d.ts +31 -28
  71. package/dist/core/storage/minioStorage.js +224 -209
  72. package/dist/core/storage/minioStorage.js.map +1 -1
  73. package/dist/core/storage/objectStorage.d.ts +23 -19
  74. package/dist/core/storage/objectStorage.js +3 -3
  75. package/dist/core/storage/ossStorage.d.ts +29 -26
  76. package/dist/core/storage/ossStorage.js +215 -193
  77. package/dist/core/storage/ossStorage.js.map +1 -1
  78. package/dist/core/storage/storageFlusher.d.ts +14 -14
  79. package/dist/core/storage/storageFlusher.js +54 -54
  80. package/dist/core/storage/storageLogger.d.ts +16 -16
  81. package/dist/core/storage/storageLogger.js +100 -100
  82. package/dist/core/storage/storageLogger.js.map +1 -1
  83. package/dist/core/storage/storagePath.d.ts +16 -16
  84. package/dist/core/storage/storagePath.js +49 -49
  85. package/dist/core/storage/wrapperStorage.d.ts +17 -17
  86. package/dist/core/storage/wrapperStorage.js +47 -47
  87. package/dist/core/storage/wrapperStorage.js.map +1 -1
  88. package/dist/core/utils/flusher.d.ts +18 -18
  89. package/dist/core/utils/flusher.js +66 -62
  90. package/dist/core/utils/flusher.js.map +1 -1
  91. package/dist/core/utils/http.d.ts +3 -3
  92. package/dist/core/utils/http.js +35 -35
  93. package/dist/core/utils/http.js.map +1 -1
  94. package/dist/core/utils/index.d.ts +57 -56
  95. package/dist/core/utils/index.js +247 -243
  96. package/dist/core/utils/index.js.map +1 -1
  97. package/dist/core/utils/number.d.ts +2 -2
  98. package/dist/core/utils/number.js +19 -19
  99. package/dist/core/utils/retry.d.ts +1 -1
  100. package/dist/core/utils/retry.js +34 -34
  101. package/dist/core/utils/retry.js.map +1 -1
  102. package/dist/core/utils/suanpan.d.ts +21 -20
  103. package/dist/core/utils/suanpan.js +103 -92
  104. package/dist/core/utils/suanpan.js.map +1 -1
  105. package/dist/core/web/app.d.ts +2 -2
  106. package/dist/core/web/app.js +38 -38
  107. package/dist/core/web/app.js.map +1 -1
  108. package/dist/core/web/server.d.ts +3 -3
  109. package/dist/core/web/server.js +64 -60
  110. package/dist/core/web/server.js.map +1 -1
  111. package/dist/main.d.ts +5 -5
  112. package/dist/main.js +28 -24
  113. package/dist/main.js.map +1 -1
  114. package/dist/types/conmonTypes.d.ts +21 -21
  115. package/dist/types/conmonTypes.js +16 -16
  116. package/dist/types/index.d.ts +3 -3
  117. package/dist/types/index.js +19 -15
  118. package/dist/types/index.js.map +1 -1
  119. package/dist/types/storageTypes.d.ts +59 -58
  120. package/dist/types/storageTypes.js +9 -9
  121. package/dist/types/streamTypes.d.ts +21 -21
  122. package/dist/types/streamTypes.js +2 -2
  123. package/package.json +1 -1
@@ -1,296 +1,296 @@
1
- 'use strict';
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const _ = require('lodash');
16
- const is = require('is-type-of');
17
- const fs = require('fs-extra');
18
- const csv = require('csvtojson');
19
- const util = require('util');
20
- const path = require('path');
21
- const storage = require('./script-storage');
22
- const log_1 = __importDefault(require("../common/log"));
23
- const ScriptFront = require('./script-front');
24
- const EventEmitter = require('events');
25
- const { isTrue, getConfig, pathEndWith, stringifyIfNecessary } = require('../utils');
26
- const { OUT_TYPE, MAX_LENGTH, OUT_SUB_TYPE, TRIMIMMEDIATELY } = require('./script-constant');
27
- const { validate, buildSendData, getNodeQueueName, buildDashBoardData } = require('./script-support');
28
- class CommonScriptContext extends EventEmitter {
29
- constructor({ userId, appId, nodeId, portId, data, when, extra, portTypes, spmqClient, utils, builtInModules, manager }) {
30
- super();
31
- this.userId = userId;
32
- this.appId = appId;
33
- this.nodeId = nodeId;
34
- this.portId = portId;
35
- this.data = data || {};
36
- this.when = when || '';
37
- this.extra = extra || {};
38
- this.debug = (this.extra && this.extra.global && isTrue(this.extra.global.debug));
39
- this.portTypes = portTypes;
40
- this.spmqClient = spmqClient;
41
- this.utils = utils || {};
42
- this.emitEventToDashboard = this.utils.emitEventToDashboard;
43
- this.emitEventToSuanpanWeb = this.utils.emitEventToSuanpanWeb;
44
- this.modules = builtInModules;
45
- this.storage = storage;
46
- this.page = new ScriptFront(manager, this.extra, this.emitEventToDashboard, this);
47
- }
48
- emitToSuanpanWeb() {
49
- if (this.utils.emitEventToSuanpanWeb) {
50
- this.utils.emitEventToSuanpanWeb(...arguments);
51
- }
52
- }
53
- /**
54
- * built-in modules
55
- * @param {*} moduleName
56
- */
57
- require(moduleName) {
58
- if (!_.includes(Object.keys(this.modules), moduleName)) {
59
- throw new Error(`moduleName: ${moduleName} is not support now.`);
60
- }
61
- return this.modules[moduleName];
62
- }
63
- /**
64
- * Convert a csv file with csvtojson
65
- * @param {*} csvFilePath local csv file path or storage objectName
66
- * @returns JSON Array
67
- */
68
- csvToJson(csvFilePath) {
69
- return __awaiter(this, void 0, void 0, function* () {
70
- if (!csvFilePath) {
71
- throw new Error('csvFilePath is required.');
72
- }
73
- if (!is.string(csvFilePath)) {
74
- throw new Error('csvFilePath must be string.');
75
- }
76
- if (pathEndWith(csvFilePath, '.csv') && fs.pathExistsSync(csvFilePath)) {
77
- return yield csv().fromFile(csvFilePath);
78
- }
79
- const objectExist = yield this.storage.fileExist(csvFilePath);
80
- if (!objectExist) {
81
- throw new Error(`csvFilePath: ${csvFilePath} is not exist.`);
82
- }
83
- const csvStream = yield this.storage.getStream(csvFilePath);
84
- if (!is.readableStream(csvStream)) {
85
- throw new Error(`${csvStream} is not readableStream.`);
86
- }
87
- return yield csv().fromStream(csvStream);
88
- });
89
- }
90
- /**
91
- * Convert buffer to json object
92
- * @param {*} buffer
93
- */
94
- bufferToJsonObj(buffer) {
95
- if (!_.isBuffer(buffer)) {
96
- return buffer;
97
- }
98
- return JSON.parse(buffer.toString());
99
- }
100
- getCurrentUserDir() {
101
- return path.join('studio', this.userId);
102
- }
103
- getCurrentAppDir() {
104
- return path.join(this.getCurrentUserDir(), this.appId);
105
- }
106
- getDefaultStorageDir() {
107
- return path.join(this.getCurrentAppDir(), 'script');
108
- }
109
- /**
110
- * async delay
111
- * @param {*} ms unit millisecond
112
- */
113
- sleep(ms) {
114
- return __awaiter(this, void 0, void 0, function* () {
115
- return new Promise(resolve => setTimeout(resolve, ms));
116
- });
117
- }
118
- getDataType(nodeId, portId, type, subType) {
119
- const outType = `${portId}${OUT_TYPE}`;
120
- const outSubType = `${portId}${OUT_SUB_TYPE}`;
121
- return {
122
- [outType]: type || this.portTypes[nodeId][portId].type,
123
- [outSubType]: subType || this.portTypes[nodeId][portId].subType
124
- };
125
- }
126
- buildSender() {
127
- return (currentNodeId, targetNodeId, { data, queue, maxlen, trimImmediately }) => __awaiter(this, void 0, void 0, function* () {
128
- try {
129
- yield this.spmqClient.sendMessage({ queue, data, maxlen, trimImmediately });
130
- const msg = `sent message to queue ${queue} from ${currentNodeId || 'initializer hook'} to ${targetNodeId}, data: ${JSON.stringify(data)}.`;
131
- if (this.debug) {
132
- log_1.default.debug(msg);
133
- }
134
- }
135
- catch (ex) {
136
- log_1.default.error(ex);
137
- }
138
- });
139
- }
140
- /**
141
- * Sends a message to the port(in) of the specified node
142
- * All dashboard components do not receive array or objects and need to be converted to JSON string
143
- * @param {*} targetNodeId
144
- * @param {*} targetPortId
145
- * @param {*} sendValue
146
- * @param {*} type
147
- * @param {*} subType
148
- */
149
- sendMessage(targetNodeId, targetPortId, sendValue, type, subType) {
150
- return __awaiter(this, void 0, void 0, function* () {
151
- validate(this.when, targetNodeId, targetPortId);
152
- const queueName = getNodeQueueName(targetNodeId);
153
- const dataType = this.getDataType(targetNodeId, targetPortId, type, subType);
154
- try {
155
- if (!_.isEmpty(this.utils) && this.utils.isDashboardNode(targetNodeId)) {
156
- log_1.default.info(`sendMessage to dashboard, nodeId: ${targetNodeId}, portId: ${targetPortId}, data: ${sendValue}`);
157
- if (_.isEmpty(this.extra.global)) {
158
- this.extra.global = {
159
- currentUserId: this.userId
160
- };
161
- }
162
- const data = buildDashBoardData(targetPortId, sendValue, dataType);
163
- this.utils.sendMessageToDashboard(targetNodeId, data, this.extra);
164
- return;
165
- }
166
- const sendData = buildSendData(this.data, targetPortId, sendValue, dataType, this.extra);
167
- const sender = this.buildSender();
168
- const maxlen = getConfig(MAX_LENGTH) || 2000;
169
- const trimImmediately = getConfig(TRIMIMMEDIATELY) || true;
170
- yield sender(this.nodeId, targetNodeId, {
171
- data: sendData,
172
- queue: queueName,
173
- maxlen,
174
- trimImmediately
175
- });
176
- }
177
- catch (ex) {
178
- throw new Error(`sent message to queue ${queueName} from ${this.nodeId || 'initializer hook'} to ${targetNodeId} failed. \nError: ${ex}`);
179
- }
180
- });
181
- }
182
- /**
183
- * TODO: add coordinate options support
184
- * @param {*} array2D
185
- * @param {*} options
186
- */
187
- update2DArray(array2D, options) {
188
- if (_.isEmpty(options)) {
189
- return array2D;
190
- }
191
- const { icon = '*', columnsNames, columnsIndexs } = options;
192
- const columnsHeads = _.head(array2D);
193
- return array2D.reduce((acc, currentValue, currentIndex) => {
194
- if (currentIndex === 0) {
195
- return acc;
196
- }
197
- if (_.isEmpty(columnsIndexs) && _.isEmpty(columnsNames)) {
198
- acc.push(currentValue.map(m => icon));
199
- return acc;
200
- }
201
- if (_.isString(columnsIndexs) || _.isNumber(columnsIndexs)) {
202
- currentValue[columnsIndexs] = icon;
203
- }
204
- else if (_.isArray(columnsIndexs)) {
205
- _.forEach(columnsIndexs, columnsIndex => {
206
- if (currentValue[columnsIndex]) {
207
- currentValue[columnsIndex] = icon;
208
- }
209
- });
210
- }
211
- if (_.isString(columnsNames)) {
212
- const columnsHeadIndex = _.indexOf(columnsHeads, columnsNames);
213
- if (columnsHeadIndex !== -1) {
214
- currentValue[columnsHeadIndex] = icon;
215
- }
216
- }
217
- else if (_.isArray(columnsNames)) {
218
- _.forEach(columnsNames, columnsName => {
219
- const columnsHeadIndex = _.indexOf(columnsHeads, columnsName);
220
- if (columnsHeadIndex !== -1) {
221
- currentValue[columnsHeadIndex] = icon;
222
- }
223
- });
224
- }
225
- acc.push(currentValue);
226
- return acc;
227
- }, []);
228
- }
229
- }
230
- class AppScriptContext extends CommonScriptContext {
231
- constructor(userId, appId, portTypes, spmqClient, utils, builtInModules, manager) {
232
- super({ userId, appId, portTypes, spmqClient, utils, builtInModules, manager });
233
- this.g = {};
234
- }
235
- log(...args) {
236
- this.emitToSuanpanWeb('globalLogging', {
237
- userId: this.userId,
238
- appId: this.appId,
239
- content: util.format(...args)
240
- });
241
- }
242
- }
243
- class NodeScriptContext extends CommonScriptContext {
244
- constructor(userId, appId, nodeId, portId, when, data, extra, spmqClient, portTypes, utils, manager, globalVariable, builtInModules) {
245
- super({ userId, appId, nodeId, portId, data, when, extra, portTypes, spmqClient, utils, manager, builtInModules });
246
- this.logPrefix = `(${userId},${appId},${nodeId},${portId},${when})`;
247
- this.g = globalVariable;
248
- }
249
- /**
250
- * output of log in custom script
251
- * @param {...any} args
252
- */
253
- log(...args) {
254
- this.emitToSuanpanWeb('scriptLogging', {
255
- node: this.nodeId,
256
- port: this.portId,
257
- when: this.when,
258
- content: util.format(...args)
259
- });
260
- }
261
- /**
262
- * The data is converted to @param newValue and passed to the connected node
263
- * @param {*} newValue
264
- */
265
- resetData(newValue) {
266
- const oldValue = this.data[this.portId];
267
- try {
268
- this.data[this.portId] = _.isString(newValue) ? newValue : JSON.stringify(newValue);
269
- if (this.debug) {
270
- log_1.default.debug(`${this.logPrefix} script reseted data FROM ${oldValue} TO `, newValue);
271
- }
272
- }
273
- catch (ex) {
274
- log_1.default.error(`reset ${newValue} failed ${ex.stack} fallback to raw data`);
275
- this.data[this.portId] = oldValue;
276
- }
277
- }
278
- /**
279
- * script stopped data to downstream
280
- */
281
- stop() {
282
- log_1.default.info(`${this.logPrefix} script stopped data to downstream.`);
283
- delete this.data[this.portId];
284
- }
285
- getCurrentNodeDir() {
286
- return path.join(this.getCurrentAppDir(), this.nodeId);
287
- }
288
- getNodeDir(nodeId) {
289
- return path.join(this.getCurrentAppDir(), nodeId);
290
- }
291
- display(data) {
292
- this.log(stringifyIfNecessary(data));
293
- }
294
- }
295
- module.exports = { NodeScriptContext, AppScriptContext };
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const _ = require('lodash');
16
+ const is = require('is-type-of');
17
+ const fs = require('fs-extra');
18
+ const csv = require('csvtojson');
19
+ const util = require('util');
20
+ const path = require('path');
21
+ const storage = require('./script-storage');
22
+ const log_1 = __importDefault(require("../common/log"));
23
+ const ScriptFront = require('./script-front');
24
+ const EventEmitter = require('events');
25
+ const { isTrue, getConfig, pathEndWith, stringifyIfNecessary } = require('../utils');
26
+ const { OUT_TYPE, MAX_LENGTH, OUT_SUB_TYPE, TRIMIMMEDIATELY } = require('./script-constant');
27
+ const { validate, buildSendData, getNodeQueueName, buildDashBoardData } = require('./script-support');
28
+ class CommonScriptContext extends EventEmitter {
29
+ constructor({ userId, appId, nodeId, portId, data, when, extra, portTypes, spmqClient, utils, builtInModules, manager }) {
30
+ super();
31
+ this.userId = userId;
32
+ this.appId = appId;
33
+ this.nodeId = nodeId;
34
+ this.portId = portId;
35
+ this.data = data || {};
36
+ this.when = when || '';
37
+ this.extra = extra || {};
38
+ this.debug = (this.extra && this.extra.global && isTrue(this.extra.global.debug));
39
+ this.portTypes = portTypes;
40
+ this.spmqClient = spmqClient;
41
+ this.utils = utils || {};
42
+ this.emitEventToDashboard = this.utils.emitEventToDashboard;
43
+ this.emitEventToSuanpanWeb = this.utils.emitEventToSuanpanWeb;
44
+ this.modules = builtInModules;
45
+ this.storage = storage;
46
+ this.page = new ScriptFront(manager, this.extra, this.emitEventToDashboard, this);
47
+ }
48
+ emitToSuanpanWeb() {
49
+ if (this.utils.emitEventToSuanpanWeb) {
50
+ this.utils.emitEventToSuanpanWeb(...arguments);
51
+ }
52
+ }
53
+ /**
54
+ * built-in modules
55
+ * @param {*} moduleName
56
+ */
57
+ require(moduleName) {
58
+ if (!_.includes(Object.keys(this.modules), moduleName)) {
59
+ throw new Error(`moduleName: ${moduleName} is not support now.`);
60
+ }
61
+ return this.modules[moduleName];
62
+ }
63
+ /**
64
+ * Convert a csv file with csvtojson
65
+ * @param {*} csvFilePath local csv file path or storage objectName
66
+ * @returns JSON Array
67
+ */
68
+ csvToJson(csvFilePath) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ if (!csvFilePath) {
71
+ throw new Error('csvFilePath is required.');
72
+ }
73
+ if (!is.string(csvFilePath)) {
74
+ throw new Error('csvFilePath must be string.');
75
+ }
76
+ if (pathEndWith(csvFilePath, '.csv') && fs.pathExistsSync(csvFilePath)) {
77
+ return yield csv().fromFile(csvFilePath);
78
+ }
79
+ const objectExist = yield this.storage.fileExist(csvFilePath);
80
+ if (!objectExist) {
81
+ throw new Error(`csvFilePath: ${csvFilePath} is not exist.`);
82
+ }
83
+ const csvStream = yield this.storage.getStream(csvFilePath);
84
+ if (!is.readableStream(csvStream)) {
85
+ throw new Error(`${csvStream} is not readableStream.`);
86
+ }
87
+ return yield csv().fromStream(csvStream);
88
+ });
89
+ }
90
+ /**
91
+ * Convert buffer to json object
92
+ * @param {*} buffer
93
+ */
94
+ bufferToJsonObj(buffer) {
95
+ if (!_.isBuffer(buffer)) {
96
+ return buffer;
97
+ }
98
+ return JSON.parse(buffer.toString());
99
+ }
100
+ getCurrentUserDir() {
101
+ return path.join('studio', this.userId);
102
+ }
103
+ getCurrentAppDir() {
104
+ return path.join(this.getCurrentUserDir(), this.appId);
105
+ }
106
+ getDefaultStorageDir() {
107
+ return path.join(this.getCurrentAppDir(), 'script');
108
+ }
109
+ /**
110
+ * async delay
111
+ * @param {*} ms unit millisecond
112
+ */
113
+ sleep(ms) {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ return new Promise(resolve => setTimeout(resolve, ms));
116
+ });
117
+ }
118
+ getDataType(nodeId, portId, type, subType) {
119
+ const outType = `${portId}${OUT_TYPE}`;
120
+ const outSubType = `${portId}${OUT_SUB_TYPE}`;
121
+ return {
122
+ [outType]: type || this.portTypes[nodeId][portId].type,
123
+ [outSubType]: subType || this.portTypes[nodeId][portId].subType
124
+ };
125
+ }
126
+ buildSender() {
127
+ return (currentNodeId, targetNodeId, { data, queue, maxlen, trimImmediately }) => __awaiter(this, void 0, void 0, function* () {
128
+ try {
129
+ yield this.spmqClient.sendMessage({ queue, data, maxlen, trimImmediately });
130
+ const msg = `sent message to queue ${queue} from ${currentNodeId || 'initializer hook'} to ${targetNodeId}, data: ${JSON.stringify(data)}.`;
131
+ if (this.debug) {
132
+ log_1.default.debug(msg);
133
+ }
134
+ }
135
+ catch (ex) {
136
+ log_1.default.error(ex);
137
+ }
138
+ });
139
+ }
140
+ /**
141
+ * Sends a message to the port(in) of the specified node
142
+ * All dashboard components do not receive array or objects and need to be converted to JSON string
143
+ * @param {*} targetNodeId
144
+ * @param {*} targetPortId
145
+ * @param {*} sendValue
146
+ * @param {*} type
147
+ * @param {*} subType
148
+ */
149
+ sendMessage(targetNodeId, targetPortId, sendValue, type, subType) {
150
+ return __awaiter(this, void 0, void 0, function* () {
151
+ validate(this.when, targetNodeId, targetPortId);
152
+ const queueName = getNodeQueueName(targetNodeId);
153
+ const dataType = this.getDataType(targetNodeId, targetPortId, type, subType);
154
+ try {
155
+ if (!_.isEmpty(this.utils) && this.utils.isDashboardNode(targetNodeId)) {
156
+ log_1.default.info(`sendMessage to dashboard, nodeId: ${targetNodeId}, portId: ${targetPortId}, data: ${sendValue}`);
157
+ if (_.isEmpty(this.extra.global)) {
158
+ this.extra.global = {
159
+ currentUserId: this.userId
160
+ };
161
+ }
162
+ const data = buildDashBoardData(targetPortId, sendValue, dataType);
163
+ this.utils.sendMessageToDashboard(targetNodeId, data, this.extra);
164
+ return;
165
+ }
166
+ const sendData = buildSendData(this.data, targetPortId, sendValue, dataType, this.extra);
167
+ const sender = this.buildSender();
168
+ const maxlen = getConfig(MAX_LENGTH) || 2000;
169
+ const trimImmediately = getConfig(TRIMIMMEDIATELY) || true;
170
+ yield sender(this.nodeId, targetNodeId, {
171
+ data: sendData,
172
+ queue: queueName,
173
+ maxlen,
174
+ trimImmediately
175
+ });
176
+ }
177
+ catch (ex) {
178
+ throw new Error(`sent message to queue ${queueName} from ${this.nodeId || 'initializer hook'} to ${targetNodeId} failed. \nError: ${ex}`);
179
+ }
180
+ });
181
+ }
182
+ /**
183
+ * TODO: add coordinate options support
184
+ * @param {*} array2D
185
+ * @param {*} options
186
+ */
187
+ update2DArray(array2D, options) {
188
+ if (_.isEmpty(options)) {
189
+ return array2D;
190
+ }
191
+ const { icon = '*', columnsNames, columnsIndexs } = options;
192
+ const columnsHeads = _.head(array2D);
193
+ return array2D.reduce((acc, currentValue, currentIndex) => {
194
+ if (currentIndex === 0) {
195
+ return acc;
196
+ }
197
+ if (_.isEmpty(columnsIndexs) && _.isEmpty(columnsNames)) {
198
+ acc.push(currentValue.map(m => icon));
199
+ return acc;
200
+ }
201
+ if (_.isString(columnsIndexs) || _.isNumber(columnsIndexs)) {
202
+ currentValue[columnsIndexs] = icon;
203
+ }
204
+ else if (_.isArray(columnsIndexs)) {
205
+ _.forEach(columnsIndexs, columnsIndex => {
206
+ if (currentValue[columnsIndex]) {
207
+ currentValue[columnsIndex] = icon;
208
+ }
209
+ });
210
+ }
211
+ if (_.isString(columnsNames)) {
212
+ const columnsHeadIndex = _.indexOf(columnsHeads, columnsNames);
213
+ if (columnsHeadIndex !== -1) {
214
+ currentValue[columnsHeadIndex] = icon;
215
+ }
216
+ }
217
+ else if (_.isArray(columnsNames)) {
218
+ _.forEach(columnsNames, columnsName => {
219
+ const columnsHeadIndex = _.indexOf(columnsHeads, columnsName);
220
+ if (columnsHeadIndex !== -1) {
221
+ currentValue[columnsHeadIndex] = icon;
222
+ }
223
+ });
224
+ }
225
+ acc.push(currentValue);
226
+ return acc;
227
+ }, []);
228
+ }
229
+ }
230
+ class AppScriptContext extends CommonScriptContext {
231
+ constructor(userId, appId, portTypes, spmqClient, utils, builtInModules, manager) {
232
+ super({ userId, appId, portTypes, spmqClient, utils, builtInModules, manager });
233
+ this.g = {};
234
+ }
235
+ log(...args) {
236
+ this.emitToSuanpanWeb('globalLogging', {
237
+ userId: this.userId,
238
+ appId: this.appId,
239
+ content: util.format(...args)
240
+ });
241
+ }
242
+ }
243
+ class NodeScriptContext extends CommonScriptContext {
244
+ constructor(userId, appId, nodeId, portId, when, data, extra, spmqClient, portTypes, utils, manager, globalVariable, builtInModules) {
245
+ super({ userId, appId, nodeId, portId, data, when, extra, portTypes, spmqClient, utils, manager, builtInModules });
246
+ this.logPrefix = `(${userId},${appId},${nodeId},${portId},${when})`;
247
+ this.g = globalVariable;
248
+ }
249
+ /**
250
+ * output of log in custom script
251
+ * @param {...any} args
252
+ */
253
+ log(...args) {
254
+ this.emitToSuanpanWeb('scriptLogging', {
255
+ node: this.nodeId,
256
+ port: this.portId,
257
+ when: this.when,
258
+ content: util.format(...args)
259
+ });
260
+ }
261
+ /**
262
+ * The data is converted to @param newValue and passed to the connected node
263
+ * @param {*} newValue
264
+ */
265
+ resetData(newValue) {
266
+ const oldValue = this.data[this.portId];
267
+ try {
268
+ this.data[this.portId] = _.isString(newValue) ? newValue : JSON.stringify(newValue);
269
+ if (this.debug) {
270
+ log_1.default.debug(`${this.logPrefix} script reseted data FROM ${oldValue} TO `, newValue);
271
+ }
272
+ }
273
+ catch (ex) {
274
+ log_1.default.error(`reset ${newValue} failed ${ex.stack} fallback to raw data`);
275
+ this.data[this.portId] = oldValue;
276
+ }
277
+ }
278
+ /**
279
+ * script stopped data to downstream
280
+ */
281
+ stop() {
282
+ log_1.default.info(`${this.logPrefix} script stopped data to downstream.`);
283
+ delete this.data[this.portId];
284
+ }
285
+ getCurrentNodeDir() {
286
+ return path.join(this.getCurrentAppDir(), this.nodeId);
287
+ }
288
+ getNodeDir(nodeId) {
289
+ return path.join(this.getCurrentAppDir(), nodeId);
290
+ }
291
+ display(data) {
292
+ this.log(stringifyIfNecessary(data));
293
+ }
294
+ }
295
+ module.exports = { NodeScriptContext, AppScriptContext };
296
296
  //# sourceMappingURL=script-context.js.map
@@ -1 +1 @@
1
- export {};
1
+ export {};