-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflow-manager.js
More file actions
1350 lines (1126 loc) · 52.9 KB
/
flow-manager.js
File metadata and controls
1350 lines (1126 loc) · 52.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const {log} = require('console');
const path = require('path'), fs = require('fs-extra'), bodyParser = require('body-parser'), log4js = require('log4js'),
os = require("os"), jsonata = require('jsonata'), eol = require('eol'), YAML = require('js-yaml'),
child_process = require('child_process'), crypto = require('crypto'), debounce = require('./debounce'),
axios = require('axios');
let alertStatus = true
function execShellCommand(cmd) {
return new Promise((resolve, reject) => {
child_process.exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(new Error(stderr || stdout));
}
resolve(stdout);
});
});
}
function encodeFileName(origName) {
return origName.replace(/\\/g, '%5C').replace(/\//g, '%2F').replace(/\:/g, '%3A').replace(/\*/g, '%2A').replace(/\?/g, '%3F').replace(/\""/g, '%22').replace(/\</g, '%3C').replace(/\>/g, '%3E').replace(/\|/g, '%7C')
// Characters not allowed: \ / : * ? " < > |
}
function changeTime(inputString) {
let timestampString = inputString.match(/\d+/)[0];
let timestamp = parseInt(timestampString, 10);
let date = new Date(timestamp);
return date.toISOString()
}
function stringifyFormattedFileJson(nodes) {
const str = JSON.stringify(nodes, undefined, 2);
return eol.auto(str);
}
// NOTE(alonam) a little trick to require the same "node-red" API to give private access to our own modulesContext.
const PRIVATERED = (function requireExistingNoderedInstance() {
for (const child of require.main.children) {
if (child.filename.endsWith('red.js')) {
return require(child.filename);
}
}
// In case node-red was not required before, just require it
return require('node-red');
})();
const nodeName = path.basename(__filename).split('.')[0];
const nodeLogger = log4js.getLogger('NodeRed FlowManager');
let RED;
function getNodesEnvConfigForNode(node) {
return (node.name && directories.nodesEnvConfig["name:" + node.name]) || (node.id && directories.nodesEnvConfig[node.id]);
}
async function getNodeList() {
return await PRIVATERED.runtime.nodes.getNodeList({});
}
let update_flag = false;
async function iinstallModeList(info) {
if (info !== "") {
let newModules = JSON.parse(info);
let installList = []
if (newModules?.libs?.length > 0) {
const oldModules = await getNodeList()
// 创建一个映射,用于存储旧模块的版本信息
const oldVersionMap = new Map(oldModules.map(m => [m.module, m.version]));
// 迭代新模块以找出新增或版本不同的模块
newModules = newModules.libs
const updates = newModules.filter(m => {
const isNewOrUpdated = !oldVersionMap.has(m.module) || oldVersionMap.get(m.module) !== m.version;
if (oldVersionMap.get(m.module) !== m.version && !update_flag) {
update_flag = true;
}
return isNewOrUpdated;
});
if (updates.length > 0) {
for (let i = 0; i < updates.length; i++) {
let item = updates[i];
console.log(`install or updat module: ${item.module},version : ${item.version}`);
await PRIVATERED.runtime.nodes.addModule({module: item.module, version: item.version});
}
}
}
}
}
async function allNpm(flows) {
const nodes = await getNodeList();
const now = new Date().valueOf()
let result = {
"id": `sensecraft${now}`,
"type": "comment",
"name": "sensecraft-libs",
"info": [],
"x": 2005,
"y": 7980,
"wires": [],
"l": false,
}
const flowFile = await readFlowByNameAndType("global");
// console.log(JSON.stringify(flowFile))
flows.forEach(flow => {
if (flow.type === "tab") {
result.z = flow.id
}
// const now = new Date();
nodes.forEach(node => {
node.types.forEach(type => {
if (flow.type === type && node.module !== "node-red") {
const flowRev = calculateRevision(node.version)
let nodeInfo = {
enabled: node.enabled,
local: node.local,
user: node.user,
version: node.version,
module: node.module, // rev: flowRev,
// mtime: now.toISOString()
}
result.info.push(nodeInfo);
}
})
})
})
const uniqueModules = [];
const seenModules = {};
result.info.forEach(item => {
if (!seenModules[item.module]) {
seenModules[item.module] = true;
uniqueModules.push(item);
}
});
const infoString = {
"global": JSON.parse(flowFile.str), "libs": uniqueModules,
}
result.info = JSON.stringify(infoString)
// 查找数组中 type 为 npm 的对象的索引
const npmIndex = flows.findIndex(item => item.name === "sensecraft-libs");
if (npmIndex !== -1) {
flows[npmIndex] = result;
} else {
flows.push(result);
console.log("npmIndex === -1")
}
return {flows: flows, sensecraft: result}
}
let lastFlows = {};
const originalSaveFlows = PRIVATERED.runtime.storage.saveFlows;
PRIVATERED.runtime.storage.saveFlows = async function newSaveFlows(data) {
if (data.flows && data.flows.length) {
// 从nodes 中提取所需的npm 包
let infoList = await allNpm(data.flows)
data.flows = infoList.flows
function getFlowFilePath(flowDetails) {
let folderName;
if (flowDetails.type === 'subflow') folderName = 'subflows'; else if (flowDetails.type === 'tab') folderName = 'flows'; else if (flowDetails.name = "sensecraft-libs") folderName = 'sensecraft-libs'; else folderName = '.';
const flowsDir = path.resolve(directories.basePath, folderName);
const flowName = flowDetails.type === 'global' ? 'config-nodes' : flowDetails.name; // if it's a subflow, then the correct property is 'name'
const flowFilePath = path.join(flowsDir, encodeFileName(flowName) + '.' + flowManagerSettings.fileFormat);
return flowFilePath;
}
const sensecraftPath = getFlowFilePath(infoList.sensecraft);
await writeFlowFile(sensecraftPath, infoList.sensecraft);
const loadedFlowAndSubflowNames = {};
const envNodePropsChangedByUser = {};
const allFlows = {};
const orderedNodeIds = [];
for (const node of data.flows) {
orderedNodeIds.push(node.id);
//
// Enforce envnodes consistency
//
const envConfig = getNodesEnvConfigForNode(node);
if (envConfig) {
for (const key in envConfig) {
const val = envConfig[key];
try {
if (JSON.stringify(node[key]) !== JSON.stringify(val)) {
if (!envNodePropsChangedByUser[node.id]) envNodePropsChangedByUser[node.id] = {};
envNodePropsChangedByUser[node.id][key] = val;
node[key] = val;
}
} catch (e) {
}
}
}
//
// Fill flows, subflows, config-nodes
if (node.type === 'tab' || node.type === 'subflow') {
if (!allFlows[node.id]) allFlows[node.id] = {nodes: []};
if (!allFlows[node.id].name) allFlows[node.id].name = node.label || node.name;
if (!allFlows[node.id].type) allFlows[node.id].type = node.type;
allFlows[node.id].nodes.push(node);
loadedFlowAndSubflowNames[node.id] = allFlows[node.id];
} else if (!node.z) {
// global (config-node)
if (!allFlows.global) allFlows.global = {name: "config-nodes", type: "global", nodes: []};
allFlows.global.nodes.push(node);
} else {
// simple node
if (!allFlows[node.z]) allFlows[node.z] = {nodes: []};
allFlows[node.z].nodes.push(node);
}
}
// save node ordering file
await fs.writeJson(directories.nodesOrderFilePath, orderedNodeIds);
// If envnode property changed, send back original values to revert on Node-RED UI as well.
if (Object.keys(envNodePropsChangedByUser).length > 0) {
RED.comms.publish('flow-manager/flow-manager-envnodes-override-attempt', envNodePropsChangedByUser);
}
deployedFlowNames.clear();
for (const flowId of Object.keys(allFlows)) {
const flowName = allFlows[flowId].name;
const flowType = allFlows[flowId].type;
if (flowType === 'tab' && (flowManagerSettings.filter.indexOf(flowName) !== -1 || flowManagerSettings.filter.length === 0 || onDemandFlowsManager.onDemandFlowsSet.has(flowName))) {
deployedFlowNames.add(flowName);
}
// Potentially we save different flow file than the one we deploy, like "credentials" property is deleted in flow file.
const flowNodes = JSON.parse(JSON.stringify(allFlows[flowId].nodes));
for (const node of flowNodes) {
// Set properties used by envnodes to mared them as flow-manager managed.
/*const foundNodeEnvConfig = getNodesEnvConfigForNode(node);
if (foundNodeEnvConfig) {
for (const prop in foundNodeEnvConfig) {
node[prop] = "flow-manager-managed";
}
}*/
// delete credentials
if (node.credentials != null) {
delete node.credentials;
}
}
const flowFilePath = getFlowFilePath({type: flowType, name: flowName});
try {
if (lastFlows[flowId] && allFlows[flowId] && lastFlows[flowId].name != allFlows[flowId].name) {
const flowsDir = path.dirname(flowFilePath);
const from = path.resolve(flowsDir, `${encodeFileName(lastFlows[flowId].name)}.${flowManagerSettings.fileFormat}`);
const to = path.resolve(flowFilePath);
try {
// First, try rename with git
await execShellCommand(`git mv -f "${from}" "${to}"`);
} catch (e) {
// if not working with git, do a normal "mv" command
try {
await fs.move(from, to, {overwrite: true});
} catch (e) {
}
}
}
// save flow file
const fileStr = await writeFlowFile(flowFilePath, flowNodes);
const stat = await fs.stat(flowFilePath);
// update revisions
const flowRev = calculateRevision(fileStr)
switch (flowType) {
case 'tab':
revisions.byFlowName[flowName] = {rev: flowRev, mtime: stat.mtime};
break
case 'subflow':
revisions.bySubflowName[flowName] = {rev: flowRev, mtime: stat.mtime};
break
case 'global':
revisions.global = {rev: flowRev, mtime: stat.mtime};
break
}
} catch (err) {
}
}
// Check which flows were removed, if any
for (const flowId in lastFlows) {
const flowName = lastFlows[flowId].name;
if (!allFlows[flowId] && ((flowManagerSettings.filter.indexOf(flowName) !== -1 || flowManagerSettings.filter.length === 0) || lastFlows[flowId].type === 'subflow')) {
const flowFilePath = getFlowFilePath(lastFlows[flowId]);
await fs.remove(flowFilePath);
}
}
lastFlows = loadedFlowAndSubflowNames;
}
return originalSaveFlows.apply(PRIVATERED.runtime.storage, arguments);
}
const flowManagerSettings = {};
async function writeFlowFile(filePath, flowStrOrObject) {
let str;
async function isReallyChanged(newObj) {
try {
const oldObj = (await readFlowFile(filePath)).obj;
const oldSet = new Set(oldObj.map(item => JSON.stringify(item)));
const newSet = new Set(newObj.map(item => JSON.stringify(item)));
if (oldSet.size !== newSet.size) return true;
newSet.forEach(function (item) {
if (!oldSet.has(item)) throw 'Found change';
})
} catch (e) {
return true;
}
}
let changed;
if (typeof flowStrOrObject === 'string' || flowStrOrObject instanceof String) {
changed = await isReallyChanged(filePath.endsWith('.yaml') ? YAML.safeLoad(flowStrOrObject) : JSON.parse(flowStrOrObject));
str = flowStrOrObject;
} else if (flowManagerSettings.fileFormat === 'yaml') {
changed = await isReallyChanged(flowStrOrObject);
str = YAML.safeDump(flowStrOrObject);
} else {
changed = await isReallyChanged(flowStrOrObject);
str = stringifyFormattedFileJson(flowStrOrObject);
}
if (changed) {
await fs.outputFile(filePath, str);
}
return str;
}
async function readFlowFile(filePath, ignoreObj) {
const retVal = {};
const fileContentsStr = await fs.readFile(filePath, 'utf-8');
retVal.str = fileContentsStr;
if (ignoreObj) {
retVal.mtime = (await fs.stat(filePath)).mtime
return retVal;
}
const indexOfExtension = filePath.lastIndexOf('.');
const fileExt = filePath.substring(indexOfExtension + 1).toLowerCase();
const finalObject = fileExt === 'yaml' ? YAML.safeLoad(fileContentsStr) : JSON.parse(fileContentsStr);
if (fileExt !== flowManagerSettings.fileFormat) {
// File needs conversion
const newFilePathWithNewExt = filePath.substring(0, indexOfExtension) + '.' + flowManagerSettings.fileFormat;
await writeFlowFile(newFilePathWithNewExt, finalObject);
// Delete old file
await fs.remove(filePath);
filePath = newFilePathWithNewExt;
}
retVal.mtime = (await fs.stat(filePath)).mtime
retVal.obj = finalObject;
return retVal;
}
async function readConfigFlowFile(ignoreObj) {
try {
return await readFlowFile(directories.configNodesFilePathWithoutExtension + '.json', ignoreObj);
} catch (e) {
return await readFlowFile(directories.configNodesFilePathWithoutExtension + '.yaml', ignoreObj);
}
}
async function readFlowByNameAndType(type, name, ignoreObj) {
const fileNameWithExt = `${name}.${flowManagerSettings.fileFormat}`;
if (type === 'flow') {
return await readFlowFile(path.join(directories.flowsDir, fileNameWithExt), ignoreObj);
} else if (type === 'subflow') {
return await readFlowFile(path.join(directories.subflowsDir, fileNameWithExt), ignoreObj);
} else if (type === 'global') {
return await (readConfigFlowFile(ignoreObj));
}
}
async function flowFileExists(flowName) {
return fs.exists(path.resolve(directories.flowsDir, `${flowName}.${flowManagerSettings.fileFormat}`));
}
const onDemandFlowsManager = {
onDemandFlowsSet: new Set(), updateOnDemandFlowsSet: async function (newSet) {
let totalSet;
//
// If flow file does not exist, or is already passed filtering settings, we don't consider it an on-demand flow
//
if (flowManagerSettings.filter.length === 0) {
totalSet = new Set();
} else {
totalSet = newSet;
for (const flowName of Array.from(totalSet)) {
if (!await flowFileExists(flowName) || flowManagerSettings.filter.indexOf(flowName) !== -1) {
totalSet.delete(flowName);
}
}
}
onDemandFlowsManager.onDemandFlowsSet = totalSet;
return onDemandFlowsManager.onDemandFlowsSet;
}
}
async function readProject() {
try {
// newer nodered versions
const newCfgPath = path.join(RED.settings.userDir, '.config.projects.json');
const newCfgPath_exists = fs.existsSync(newCfgPath);
if (newCfgPath_exists) {
const redConfig = await fs.readJson(newCfgPath);
return {path: newCfgPath, activeProject: redConfig.activeProject};
}
// older nodered versions
const oldCfgPath = path.join(RED.settings.userDir, '.config.json');
const oldCfgPath_exists = fs.existsSync(oldCfgPath);
if (oldCfgPath_exists) {
const redConfig = await fs.readJson(oldCfgPath);
return {path: oldCfgPath, activeProject: redConfig.projects.activeProject};
}
} catch (e) {
}
return null;
}
const revisions = {
byFlowName: {}, bySubflowName: {}, global: null // config nodes, etc..
}
let deployedFlowNames = new Set();
function calculateRevision(str) {
return crypto.createHash('md5').update(str.replace(/\s+/g, '')).digest("hex");
}
const directories = {};
async function main() {
let initialLoadPromise = (() => {
let res;
const p = new Promise((resolve, reject) => {
res = resolve;
});
p.resolve = res;
return p
})()
const originalGetFlows = PRIVATERED.runtime.storage.getFlows;
PRIVATERED.runtime.storage.getFlows = async function () {
if (initialLoadPromise) await initialLoadPromise;
const retVal = await originalGetFlows.apply(PRIVATERED.runtime.storage, arguments);
const flowsInfo = await loadFlows(null, true);
retVal.flows = flowsInfo.flows;
retVal.rev = calculateRevision(JSON.stringify(flowsInfo.flows));
revisions.byFlowName = flowsInfo.flowVersions;
revisions.bySubflowName = flowsInfo.subflowVersions;
revisions.global = flowsInfo.globalVersion;
deployedFlowNames = new Set(Object.values(flowsInfo.loadedFlowAndSubflowNames)
.filter(flow => flow.type === 'tab')
.map(flow => flow.name));
lastFlows = flowsInfo.loadedFlowAndSubflowNames;
return retVal;
}
async function refreshDirectories() {
let basePath, project = null;
if (RED.settings.editorTheme.projects.enabled) {
projectObj = await readProject();
if (projectObj?.activeProject) {
project = projectObj.activeProject;
const activeProjectPath = path.join(RED.settings.userDir, 'projects', project);
basePath = activeProjectPath;
} else {
basePath = RED.settings.userDir;
}
} else {
basePath = RED.settings.userDir;
}
Object.assign(directories, {
basePath: basePath,
subflowsDir: path.resolve(basePath, 'subflows'),
flowsDir: path.resolve(basePath, 'flows'),
envNodesDir: path.resolve(basePath, 'envnodes'),
flowFile: path.resolve(basePath, RED.settings.flowFile || 'flows_' + os.hostname() + '.json'),
project: project,
flowManagerCfg: path.resolve(basePath, 'flow-manager-cfg.json'),
configNodesFilePathWithoutExtension: path.resolve(basePath, 'config-nodes'),
nodesOrderFilePath: path.resolve(basePath, 'flow-manager-nodes-order.json'),
});
// Read flow-manager settings
let needToSaveFlowManagerSettings = false;
try {
const fileJson = await fs.readJson(directories.flowManagerCfg);
// Backwards compatibility
if (Array.isArray(fileJson)) {
needToSaveFlowManagerSettings = true;
flowManagerSettings.filter = fileJson;
} else if (typeof fileJson === 'object') {
Object.assign(flowManagerSettings, fileJson);
}
} catch (e) {
} finally {
if (!Array.isArray(flowManagerSettings.filter)) {
needToSaveFlowManagerSettings = true;
flowManagerSettings.filter = [];
}
if (!flowManagerSettings.fileFormat) {
needToSaveFlowManagerSettings = true;
flowManagerSettings.fileFormat = 'json';
}
}
if (needToSaveFlowManagerSettings) {
await fs.outputFile(directories.flowManagerCfg, stringifyFormattedFileJson(flowManagerSettings));
}
directories.configNodesFilePath = directories.configNodesFilePathWithoutExtension + '.' + flowManagerSettings.fileFormat;
// Loading ENV configuration for nodes
directories.nodesEnvConfig = {};
const envNodeResolutionPromises = [];
try { // envnodes dir is optional
for (const envNodeFileName of await fs.readdir(directories.envNodesDir)) {
const ext = envNodeFileName.substring(envNodeFileName.lastIndexOf('.') + 1);
const fileExtIndex = ["jsonata", "json", "js"].indexOf(ext);
if (fileExtIndex === -1) continue;
envNodeResolutionPromises.push((async () => {
const absoluteFilePath = path.resolve(directories.envNodesDir, envNodeFileName);
let result = null;
try {
switch (fileExtIndex) {
case 0: { // jsonata
const fileContents = await fs.readFile(absoluteFilePath, 'UTF-8');
result = jsonata(fileContents).evaluate({
require: require, basePath: directories.basePath
});
break
}
case 1: {
const fileContents = await fs.readFile(absoluteFilePath, 'UTF-8');
result = JSON.parse(fileContents);
break
}
case 2: {
const jsFile = require(absoluteFilePath);
if (isObject(jsFile)) {
result = jsFile;
} else if (typeof jsFile === 'function') {
const returnedVal = jsFile(RED);
if (returnedVal instanceof Promise) {
result = await returnedVal;
} else {
result = returnedVal;
}
}
break
}
}
} catch (e) {
nodeLogger.error('JSONata parsing failed for env nodes:\n', e);
}
return result;
})());
}
} catch (e) {
}
const results = await Promise.all(envNodeResolutionPromises);
results.forEach(result => {
Object.assign(directories.nodesEnvConfig, result)
});
await fs.ensureDir(directories.flowsDir);
await fs.ensureDir(directories.subflowsDir);
}
async function readAllFlowFileNames(type = 'subflow') {
const filesUnderFolder = (await fs.readdir(type === 'subflow' ? directories.subflowsDir : directories.flowsDir));
const relevantItems = filesUnderFolder.filter(item => {
const ext = path.extname(item).toLowerCase();
return ext === '.json' || ext === '.yaml';
});
return relevantItems;
}
async function readAllFlowFileNamesWithoutExt(type) {
return (await readAllFlowFileNames(type)).map(file => file.substring(0, file.lastIndexOf('.')));
}
async function loadFlows(flowsToShow = null, getMode = false) {
const retVal = {
flows: [],
rev: null,
flowVersions: {},
subflowVersions: {},
globalVersion: null,
loadedFlowAndSubflowNames: {}
}
let flowJsonSum = {
tabs: [], subflows: [], groups: [], global: [], groupedNodes: [], nodes: [], byNodeId: {}
};
let items = await readAllFlowFileNames('flow');
if (flowsToShow === null) {
flowsToShow = flowManagerSettings.filter;
}
nodeLogger.info('Flow filtering state:', flowsToShow);
// read flows
for (const algoFlowFileName of items) {
try {
const itemWithoutExt = algoFlowFileName.substring(0, algoFlowFileName.lastIndexOf('.'));
if (!algoFlowFileName.toLowerCase().match(/.*\.(json)|(yaml)$/g)) continue;
const flowJsonFile = await readFlowFile(path.join(directories.flowsDir, algoFlowFileName));
retVal.flowVersions[itemWithoutExt] = {
rev: calculateRevision(flowJsonFile.str), mtime: flowJsonFile.mtime
};
// Ignore irrelevant flows (filter flows functionality)
if (flowsToShow && flowsToShow.length && flowsToShow.indexOf(itemWithoutExt) === -1) {
continue;
}
// find tab node
let tab = null;
for (let i = flowJsonFile.obj.length - 1; i >= 0; i--) {
const node = flowJsonFile.obj[i];
flowJsonSum.byNodeId[node.id] = node;
if (node.type === 'tab') {
flowJsonSum.tabs.push(node);
flowJsonFile.obj.splice(i, 1);
tab = node;
} else if (node.type === 'group') {
flowJsonFile.obj.splice(i, 1);
flowJsonSum.groups.push(node);
} else if (typeof node.g === "string") {
flowJsonFile.obj.splice(i, 1);
flowJsonSum.groupedNodes.push(node);
}
}
if (!tab) {
throw new Error("Could not find tab node in flow file")
}
Array.prototype.push.apply(flowJsonSum.nodes, flowJsonFile.obj);
retVal.loadedFlowAndSubflowNames[tab.id] = {type: tab.type, name: tab.label};
} catch (e) {
nodeLogger.error('Could not load flow ' + algoFlowFileName + '\r\n' + e.stack || e);
}
}
// read subflows
const subflowItems = (await fs.readdir(directories.subflowsDir)).filter(item => {
const itemLC = item.toLowerCase();
return itemLC.endsWith('.yaml') || itemLC.endsWith('.json');
});
for (const subflowFileName of subflowItems) {
try {
const flowJsonFile = await readFlowFile(path.join(directories.subflowsDir, subflowFileName));
// find subflow node
let subflowNode = false;
for (let i = flowJsonFile.obj.length - 1; i >= 0; i--) {
const node = flowJsonFile.obj[i];
flowJsonSum.byNodeId[node.id] = node;
if (node.type === 'subflow') {
flowJsonSum.subflows.push(node);
flowJsonFile.obj.splice(i, 1);
subflowNode = node;
} else if (node.type === 'group') {
flowJsonFile.obj.splice(i, 1);
flowJsonSum.groups.push(node);
} else if (typeof node.g === "string") {
flowJsonFile.obj.splice(i, 1);
flowJsonSum.groupedNodes.push(node);
}
}
if (!subflowNode) {
throw new Error("Could not find subflow node in flow file")
}
Array.prototype.push.apply(flowJsonSum.nodes, flowJsonFile.obj);
const itemWithoutExt = subflowFileName.substring(0, subflowFileName.lastIndexOf('.'));
retVal.subflowVersions[itemWithoutExt] = {
rev: calculateRevision(flowJsonFile.str), mtime: flowJsonFile.mtime
};
retVal.loadedFlowAndSubflowNames[subflowNode.id] = {type: subflowNode.type, name: subflowNode.name};
} catch (e) {
nodeLogger.error('Could not load subflow ' + subflowFileName + '\r\n' + e.stack || e);
}
}
{
// read config nodes
let configFlowFile;
try {
configFlowFile = await readConfigFlowFile();
} catch (e) {
await writeFlowFile(directories.configNodesFilePathWithoutExtension + '.' + flowManagerSettings.fileFormat, []);
configFlowFile = await readConfigFlowFile();
}
retVal.globalVersion = {
rev: calculateRevision(configFlowFile.str), mtime: configFlowFile.mtime
};
for (const node of configFlowFile.obj) {
flowJsonSum.byNodeId[node.id] = node;
}
Array.prototype.push.apply(flowJsonSum.global, configFlowFile.obj);
}
let orderedNodes = null;
try {
const unorderedNodesLeft = new Set(Object.keys(flowJsonSum.byNodeId));
const nodesOrderArray = await fs.readJson(directories.nodesOrderFilePath);
orderedNodes = []
for (const nodeId of nodesOrderArray) {
const theNode = flowJsonSum.byNodeId[nodeId];
if (theNode) {
orderedNodes.push(theNode);
unorderedNodesLeft.delete(nodeId);
}
}
if (unorderedNodesLeft.size > 0) {
orderedNodes = null;
}
} catch (e) {
}
if (!orderedNodes) {
// No ordering exists, at least come up with a similar order
orderedNodes = [...flowJsonSum.tabs, ...flowJsonSum.subflows, ...flowJsonSum.groups, ...flowJsonSum.global, ...flowJsonSum.groupedNodes, ...flowJsonSum.nodes];
}
retVal.flows = orderedNodes;
const nodesEnvConfig = directories.nodesEnvConfig;
// If we have node config modifications for this ENV, iterate and apply node updates from ENV config.
if (nodesEnvConfig && Object.keys(nodesEnvConfig).length) {
for (const node of retVal.flows) {
// If no patch exists for this id
if (!node) continue;
const foundNodeEnvConfig = getNodesEnvConfigForNode(node);
if (foundNodeEnvConfig) {
Object.assign(node, foundNodeEnvConfig);
}
}
}
if (!getMode) {
nodeLogger.info('Loading flows:', items);
nodeLogger.info('Loading subflows:', subflowItems);
try {
retVal.rev = await PRIVATERED.nodes.setFlows(retVal.flows, null, 'flows');
revisions.byFlowName = retVal.flowVersions;
revisions.bySubflowName = retVal.subflowVersions;
revisions.global = retVal.globalVersion;
nodeLogger.info('Finished setting node-red nodes successfully.');
} catch (e) {
nodeLogger.error('Failed setting node-red nodes\r\n' + e.stack || e);
}
}
return retVal;
}
async function checkIfMigrationIsRequried() {
try {
// Check if we need to migrate from "fat" flow json file to managed mode.
if ((await fs.readdir(directories.flowsDir)).length === 0 && (await fs.readdir(directories.subflowsDir)).length === 0 && await fs.exists(directories.flowFile)) {
nodeLogger.info('First boot with flow-manager detected, starting migration process...');
return true;
}
} catch (e) {
}
return false;
}
async function startFlowManager() {
await refreshDirectories();
if (await checkIfMigrationIsRequried()) {
const masterFlowFile = await fs.readJson(directories.flowFile);
const flowNodes = {};
const globalConfigNodes = [], simpleNodes = [];
for (const node of masterFlowFile) {
if (node.type === 'tab' || node.type === 'subflow') flowNodes[node.id] = [node]; else if (!node.z || node.z.length === 0) globalConfigNodes.push(node); else simpleNodes.push(node);
}
for (const node of simpleNodes) {
if (flowNodes[node.z]) flowNodes[node.z].push(node);
}
// finally write files
const fileWritePromises = [writeFlowFile(directories.configNodesFilePath, globalConfigNodes)];
for (const flowId of Object.keys(flowNodes)) {
const nodesInFlow = flowNodes[flowId];
const topNode = nodesInFlow[0];
const flowName = topNode.label || topNode.name; // label on tabs ,
const destinationFile = path.resolve(directories.basePath, topNode.type === 'tab' ? 'flows' : 'subflows', encodeFileName(flowName) + '.' + flowManagerSettings.fileFormat);
fileWritePromises.push(writeFlowFile(destinationFile, nodesInFlow));
}
await Promise.all(fileWritePromises);
nodeLogger.info('flow-manager migration complete.');
}
}
await startFlowManager();
if (RED.settings.editorTheme.projects.enabled) {
let projFile = await readProject();
if (!projFile) return; // no cfg file, do nothing.
fs.watch(projFile.path, debounce(async () => {
const newProjFile = await readProject();
if (!projFile) return; // no cfg, do nothing
if (projFile?.activeProject != newProjFile?.activeProject) {
projFile = newProjFile;
await startFlowManager();
}
}, 500));
}
RED.httpAdmin.get('/' + nodeName + '/flow-names', RED.auth.needsPermission("flows.read"), async function (req, res) {
try {
let flowFiles = await fs.readdir(path.join(directories.basePath, "flows"));
flowFiles = flowFiles.filter(file => file.toLowerCase().match(/.*\.(json)|(yaml)$/g));
res.send(flowFiles);
} catch (e) {
res.status(404).send();
}
});
async function getFlowState(type, flowName) {
let flowFile
try {
flowFile = await readFlowByNameAndType(type, flowName, true);
} catch (e) {
return null
}
const retVal = {};
let lastLoadedFlowVersionInfo;
if (type === 'flow') {
lastLoadedFlowVersionInfo = revisions.byFlowName[flowName];
// If flow was not deployed either "on-demend" or passed filtering, we determine that it was not deployed.
const wasLoadedOnDemand = type === 'flow' && onDemandFlowsManager.onDemandFlowsSet.has(flowName)
retVal.deployed = deployedFlowNames.has(flowName);
retVal.onDemand = wasLoadedOnDemand;
} else if (type === 'subflow') {
lastLoadedFlowVersionInfo = revisions.bySubflowName[flowName];
retVal.deployed = true;
} else if (type === 'global') {
lastLoadedFlowVersionInfo = revisions.global;
retVal.deployed = true;
} else {
return null;
}
let strOld = flowFile.str
let strNew = JSON.parse(flowFile.str)
if (Array.isArray(strNew)) {
strNew = strNew.filter(item => item.name !== "sensecraft-libs")
strOld = JSON.stringify(strNew)
}
const fileRev = calculateRevision(strOld);
retVal.rev = fileRev
retVal.mtime = flowFile.mtime;
retVal.hasUpdate = !lastLoadedFlowVersionInfo || fileRev !== lastLoadedFlowVersionInfo.rev;
if (retVal.hasUpdate && lastLoadedFlowVersionInfo) {
retVal.oldRev = lastLoadedFlowVersionInfo.rev;
retVal.oldMtime = lastLoadedFlowVersionInfo.mtime
}
return retVal;
}
async function getFlowStateForType(type) {
if (type === 'flow' || type === 'subflow') {
const retVal = {};
const flowNames = await readAllFlowFileNamesWithoutExt(type);
for (const flowName of flowNames) {
retVal[flowName] = await getFlowState(type, flowName);
}
return retVal;
} else if (type === 'global') {
return await getFlowState(type);
}
}
RED.httpAdmin.get('/' + nodeName + '/states/:type/:flowName?', RED.auth.needsPermission("flows.read"), async function (req, res) {
try {
if (['flow', 'subflow', 'global'].indexOf(req.params.type) !== -1) {
let retVal;
if (req.params.flowName) {
retVal = await getFlowState(req.params.type, req.params.flowName);
} else {
retVal = await getFlowStateForType(req.params.type);
}
if (retVal) {
return res.send(retVal);
} else {
return res.status(404).send({error: `No such ${req.params.type} file`});
}
} else {
return res.status(404).send({error: `Unrecognised flow type: ${req.params.type}`});
}
} catch (e) {
return res.status(404).send();
}
});
RED.httpAdmin.get('/' + nodeName + '/states', RED.auth.needsPermission("flows.read"), async function (req, res) {
try {
const retVal = {};
for (const flowType of ['flow', 'subflow', 'global']) {
retVal[flowType] = await getFlowStateForType(flowType);
}
return res.send(retVal);
} catch (e) {
return res.status(404).send();
}