-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
1123 lines (1003 loc) · 35.7 KB
/
index.ts
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
import { Utils, UtilsConstants } from "./utils/util";
import path from "path";
//handle setupevents as quickly as possible
const setupEvents = require("./../installers/setupEvents");
var mainWindow: any;
// local constants
module Constants {
export const RES_PATH = "srv";
}
// global variables
// var projectRootPath: string;
// var projectPath: string;
// var rPath: string;
// var rStoxFtpPath: string;
var simpleNodeLogger = require("simple-node-logger");
var express = require("express");
var bodyParser = require("body-parser");
var child_process = require("child_process");
var fs = require("fs");
var cors = require("cors");
var callr_evaluate: boolean[] = [];
var rPackagesIsInstalling = false;
var isClosing = false;
const net = require("net");
let client: any = null;
var properties: any = null;
var activeProjectSaved: boolean = true;
var log: any = null;
var server: any = null;
var rAvailable: boolean = false;
let serverStarted = false;
var versionRstoxFramework = "";
var loadStatusRstoxFramework = "";
var officialRstoxPackages: String[] = [];
var versionR = "";
var backendProcess: any; // Backend process
require("pkginfo")(module, "version");
var stoxVersion = module.exports.version;
var isOfficialStoXVersion: boolean = false;
var RstoxPackageStatus: number;
// Modules to control application life and create native browser window
const { app, BrowserWindow, Menu } = require("electron");
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
app.on("ready", async () => {
if (setupEvents.handleSquirrelEvent()) {
return;
}
logInfo("stox version: " + stoxVersion);
setupLogger();
logInfo("lifecycle: ready");
setupServer();
await startNodeServer();
readPropertiesFromFile();
// Extract resources
Utils.extractResourceFile(
UtilsConstants.RES_SERVER_OFFICIALRSTOXFRAMEWORKVERSIONS,
"stox"
);
Utils.extractResourceFile(
UtilsConstants.RES_SERVER_OFFICIASTOXVERSIONS,
"stox"
);
Utils.extractResourceFile(UtilsConstants.RES_SERVER_FILENAME, "stox");
logInfo(">>> startBackendServer");
await startBackendServer(true);
logInfo(">>> End startBackendServer");
logInfo(">>> checkLoadStatusRstoxFramework");
await checkLoadStatusRstoxFramework();
logInfo(">>> End checkLoadStatusRstoxFramework");
createWindow();
});
/*// Quit when all windows are closed. // AS added Break the OS convention and close the process when window closes
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit()
})*/
app.on("activate", function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow();
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("quit", function () {
// Write app properties file to disc here.
logInfo("ev:app quit");
writePropertiesToFile();
});
function logInfo(str: string) {
log?.info(str);
console.log("> " + str);
}
function setupLogger() {
var logDir = require("temp-dir") + "/stox";
logInfo("Logging to " + logDir);
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const opts = {
errorEventName: "error",
logDirectory: logDir, // NOTE: folder must exist and be writable... executable bin directory
fileNamePattern: "node-<DATE>.log",
dateFormat: "YYYY.MM.DD",
};
log = simpleNodeLogger.createRollingFileLogger(opts);
}
async function startNodeServer() {
// start server
var port = 3000;
logInfo("Starting Node Server at port " + port + "...");
await new Promise((r) => {
server.listen(port, () => {
logInfo("Node Server started at http://localhost:" + port);
r(null);
});
});
}
function createWindow() {
if (process.argv.filter((arg) => arg == "--server").length > 0) {
logInfo("No window created due to --server flag");
return;
}
logInfo("creating window");
app.allowRendererProcessReuse = true; // required or forced by electron 9
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
title: "StoX 3.0",
webPreferences: {
nodeIntegration: true,
},
});
if (mainWindow == null) {
return;
}
Menu.setApplicationMenu(null);
// and load the index.html of the app.
//mainWindow.loadFile(`../frontend/dist/stox/index.html`)
logInfo(`log to file://${__dirname}/`);
mainWindow.loadURL(`file://${__dirname}/../frontend/dist/stox/index.html`);
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on("close", async (e: any) => {
await onClosed(e);
});
// Emitted when the window is closed.
mainWindow.on("closed", function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
// rspawn = null;
child_process = null;
});
}
function showElectronMessageBox(
title: string,
message: string,
buttons: string[]
): number {
return require("electron").dialog.showMessageBoxSync(null, {
type: "question",
buttons: buttons,
title: title,
message: message,
});
}
async function onClosed(e: any) {
if (properties != null && properties.activeProject !== null && !isClosing) {
logInfo(
"close - saved:" +
activeProjectSaved +
" project: " +
(properties != null && properties.activeProject != null
? properties.activeProject
: "")
);
// need to stop application from quitting to let Promise work, and then re-close. with properties.activeProject = null
e.preventDefault();
let save = false;
if (!activeProjectSaved) {
var choice = showElectronMessageBox(
"Save project",
"Save changes to project before closing?",
["Yes", "No", "Cancel"]
);
if (choice == 0) {
save = true;
}
if (choice == 2) {
// cancel
return;
}
}
let cmd =
"RstoxFramework::runFunction.JSON(" +
JSON.stringify(
JSON.stringify({
what: "closeProject",
args: JSON.stringify({
projectPath: properties.activeProject,
save: save,
}),
package: "RstoxFramework",
})
) +
")";
let s = await callR(cmd);
isClosing = true;
mainWindow.close(); // re-close
}
}
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
function rScriptBin() {
return (
(properties.rPath == "" || properties.rPath == null
? ""
: properties.rPath + "/") + "Rscript"
);
}
async function checkRAvailable(): Promise<boolean> {
var rscriptBin = rScriptBin();
logInfo("Rscript bin " + rscriptBin);
let p1 = child_process.spawnSync(rscriptBin, ["--no-environ", "-e", "TRUE"]);
logInfo("Check Rscript availability " + p1.stdout);
if (p1.stdout == null || !p1.stdout.includes("TRUE")) {
rAvailable = false;
logInfo('Rscript is not available. Set R path in the properties."');
serverStarted = false;
return false;
}
logInfo("Rscript is available.");
return true;
}
async function startBackendServer(checkLoadStatus: boolean): Promise<string> {
logInfo("Starting backend");
rAvailable = await checkRAvailable();
if (!rAvailable) {
logInfo("R is not available- quitting startBackendServer");
// Set the status of the Rstox packages according to missing R:
RstoxPackageStatus = 3;
return "";
}
var rscriptBin = rScriptBin();
// spawn a process instead of exec (this will not include a intermediate hidden shell process cmd)
let fileName = Utils.getTempResFileName(
UtilsConstants.RES_SERVER_FILENAME,
"stox"
);
logInfo("Spawning " + rscriptBin + " " + fileName);
backendProcess = await child_process.spawn(rscriptBin, [fileName], {
stdio: ["pipe", "ignore", "ignore"],
});
backendProcess.on("error", (er: any) => {
logInfo("Spawning error " + er);
});
// logInfo("Process " + backendProcess.pid + " started with " + serverCmd)
logInfo("Backend started.");
//logInfo(backendProcess.pid);
//rserve_client = await connectRserve(rserve);
await createClient();
if (serverStarted) {
let cmd =
'paste(R.Version()$major, gsub("(.+?)([.].*)", "\\\\1", R.Version()$minor), sep = ".")';
versionR = ((await callR(cmd)) as any).result;
logInfo("R version " + versionR);
let firstLibPath = ((await callR(".libPaths()[1]")) as any).result;
logInfo("First of .libPaths: " + firstLibPath);
await callR(
'# Check that we are on Windows:\nif (.Platform$OS.type == "windows") {\n\t# If no non-programfiles libraries, create the same that Rstudio creates:\n\tlib <- .libPaths()\n\twritable <- file.access(lib, mode = 2) == 0\n\tif(!writable[1]) {\n\t\thomeFolder <- utils::readRegistry(key="Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Explorer\\\\Shell Folders", hive="HCU")$Personal\n\t\t# As of R 4.2.0 the folder for the packages changed:\n\t\ttwoDigitRVersion <- paste(R.Version()$major, strsplit(R.Version()$minor, ".", fixed = TRUE)[[1L]][1L], sep = ".")\n\t\tif(getRversion() >= "4.2.0") {\n\t\t\tnewLib <- paste(Sys.getenv("USERPROFILE"), "AppData", "Local", "R", "win-library", twoDigitRVersion, sep="/")\n\t\t}\n\t\telse {\n\t\t\tnewLib <- paste(homeFolder, "R", "win-library", twoDigitRVersion, sep="/")\n\t\t}\n\t\t# Add the local library as the first:\n\t\tif(!dir.exists(newLib)) {\n\t\t\tdir.create(newLib, recursive = TRUE)\n\t\t}\n\t\t# Add the local library in this session:\n\t\t.libPaths(newLib)\n\t}\n}'
);
logInfo(
"R packages installed in: " +
((await callR(".libPaths()[1]")) as any).result
);
let officialsRFTmpFile = Utils.getTempResFileName(
UtilsConstants.RES_SERVER_OFFICIALRSTOXFRAMEWORKVERSIONS,
"stox"
);
let officialStoXVersionTmpFile = Utils.getTempResFileName(
UtilsConstants.RES_SERVER_OFFICIASTOXVERSIONS,
"stox"
);
//let versionsTmpFile = Utils.getTempResFileName(UtilsConstants.RES_SERVER_VERSIONS);
//logInfo(versionsTmpFile);
//let StoXGUIInternalFile = 'srv/StoXGUIInternal_0.1.tar.gz';
//let StoXGUIInternalFile = path.join(path.join(__dirname, "../.."), 'srv/StoXGUIInternal_0.1.tar.gz')
//let StoXGUIInternalFile = Utils.getTempResFileName(UtilsConstants.RES_SERVER_STOXGUIINTERNAL);
//let StoXGUIInternalFile = path.join(path.join(__dirname, ".."), Constants.RES_PATH, UtilsConstants.RES_SERVER_STOXGUIINTERNAL);
let StoXGUIInternalFolder = Utils.getTempResFileName(
"StoXGUIInternal",
"stox"
);
StoXGUIInternalFolder = StoXGUIInternalFolder.replace(/\\/g, "/"); // convert backslash to forward
/*cmd = "paste(.libPaths(), collapse=\",\")";
versionR = (await callR(cmd) as any).result;
logInfo("Lib paths " + versionR);*/
//cmd = "if(!suppressWarnings(require(RCurl, quietly = TRUE))) install.packages(\"RCurl\", quiet = TRUE, repos = \"https://cloud.r-project.org\")";
//logInfo(cmd);
//let res22 = (await callR(cmd) as any).result;
// With R 4.3.0 the source() no longer works with callR. It simplly froze. Building a package instead:
// cmd = "source(\"" + versionsTmpFile + "\")";
// logInfo(cmd);
// let res = (await callR(cmd) as any).result;
// Install StoXGUIInternal every time for safety:
// Create the pakcage:
Utils.createStoXGUIInternal();
//cmd = "if(!require(StoXGUIInternal, quietly = TRUE)) utils::install.packages(\"" + StoXGUIInternalFile + "\", repos = NULL, type = \"source\", lib = .libPaths()[1])";
cmd =
'utils::install.packages("' +
StoXGUIInternalFolder +
'", repos = NULL, type = "source", lib = .libPaths()[1])';
logInfo(cmd);
let res = ((await callR(cmd)) as any).result;
logInfo(res);
// Initiate a local library
//logInfo("libPaths before initiation" + (await callR(".libPaths()[1]") as any).result);
//logInfo("StoXGUIInternal::initLocalLibrary(): " + (await callR("StoXGUIInternal::initLocalLibrary()") as any).result);
// Test whether RstoxFramework is installed:
if (checkLoadStatus) {
await checkLoadStatusRstoxFramework();
}
// This vector holds the PackageName_PackageVersion of all installed Rstox packages that are listed in the officialsRFTmpFile.
// The vector is however only used as a list of package names, when checking the status of the packages in the
// server.get('/getRstoxPackageVersions'...
// below on line 800-ish:
cmd =
'StoXGUIInternal::getOfficialRstoxPackageVersion("' +
stoxVersion +
'", "' +
officialsRFTmpFile +
'", optionalDependencies = TRUE, toJSON = T)';
logInfo(cmd);
officialRstoxPackages = JSON.parse(((await callR(cmd)) as any).result);
logInfo(JSON.stringify(officialRstoxPackages));
cmd =
'tryCatch(paste0("RstoxFramework_", as.character(packageVersion("RstoxFramework"))),error = function(e) {""})';
versionRstoxFramework = ((await callR(cmd)) as any).result;
logInfo(versionRstoxFramework);
// cmd = "StoXGUIInternal::isOfficialStoXVersion(\"" + stoxVersion + "\", \"" + officialsRFTmpFile + "\")";
// logInfo(cmd);
// isOfficialStoXVersion = (await callR(cmd) as any).result == "TRUE";
// logInfo("isOfficialStoXVersion" + isOfficialStoXVersion.toString());
let officialStoXVersions = fs.readFile(
officialStoXVersionTmpFile,
function (err: any, data: any) {
if (err) throw err;
isOfficialStoXVersion = data.includes(stoxVersion);
}
);
}
return "ok";
}
async function checkLoadStatusRstoxFramework(): Promise<string> {
if (!rAvailable) {
return "";
}
let cmd =
'tryCatch({library("RstoxFramework"); ""} ,error = function(e) {e})';
loadStatusRstoxFramework = ((await callR(cmd)) as any).result.trim();
logInfo("Load status RstoxFramework: " + loadStatusRstoxFramework);
return "ok";
}
async function getPackageStatus(
packageName: string,
stoxVersion: string,
officialsRFTmpFile: string
) {
let cmd =
'StoXGUIInternal::RstoxPackageStatus("' +
packageName +
'", "' +
stoxVersion +
'", "' +
officialsRFTmpFile +
'")';
RstoxPackageStatus = await (callR(cmd) as any);
return RstoxPackageStatus;
}
async function getVersionStringOfPackage(packageName: string) {
// 2023-04-25 (Work to separate the GUI from the Versions.R in RstoxFramework): Changed this to use utils directly, since the getVersionStringOfPackage is used in RstoxFramework:
//let cmd = "StoXGUIInternal::getVersionStringOfPackage(\"" + packageName + "\")";
let cmd = 'utils::installed.packages()[, "Version"]["' + packageName + '"]';
return await (callR(cmd) as any);
}
/**
* Create a safe connection to socket with timeout loop. Either success or failure in each try.
*/
async function createClient() {
while (callr_evaluate.length > 0) {
// pause while client is busy
await new Promise((r) =>
setTimeout(() => {
r(null);
}, 50 /*ms*/)
);
}
if (client != null) {
logInfo("Close existing client");
client.destroy();
logInfo("Client closed");
}
logInfo("Create client socket");
const localhost = "127.0.0.1";
const port = 6312;
client = null;
for (let i = 1; i <= 10000; i++) {
await new Promise((r) =>
setTimeout(() => {
r(null);
}, 400 /*ms*/)
); // createConnection synchr. takes however 1 sec.
logInfo("Connecting to " + localhost + ":" + port + " try " + i);
serverStarted = false;
await new Promise((resolve) => {
client = new net.createConnection(port, localhost)
.on("connect", function () {
serverStarted = true;
logInfo("Connected in try " + i);
resolve(null);
})
.on("error", function (err: any) {
logInfo("Error connecting client: " + err);
resolve(null);
});
});
if (serverStarted) {
break;
}
}
if (client != null) {
client.on("data", function (data: any) {
if (client.handle != null) {
client.handle(data);
}
});
client.setEncoding("utf8"); // to get chunks read as strings
return "";
}
}
const getPropertiesFileName = function getPropertiesFileName() {
return require("os").homedir() + "/.stox.properties." + stoxVersion + ".json";
};
const readPropertiesFromFile = function readPropertiesFromFile() {
let propFileName = getPropertiesFileName();
logInfo("Properties read from file: ");
try {
if (fs.existsSync(propFileName)) {
try {
properties = JSON.parse(
fs.readFileSync(propFileName, { encoding: "utf-8", flag: "r" })
);
} catch (err) {
properties = null;
logInfo("Properties not read due to error " + err);
}
logInfo("Properties read from file: " + propFileName);
}
if (properties == null) {
// Properties not read properly from file, or the file doesnt exist.
logInfo("create initial properties");
properties = {
projectRootPath: "",
activeProject: "",
rPath: "",
rStoxFtpPath: "",
};
logInfo("Properties initialized.");
}
if (
properties.projectRootPath == null ||
properties.projectRootPath == ""
) {
console.log("> " + "Electron Home: " + app.getPath("home"));
console.log("> " + "Node Home: " + require("os").homedir());
properties.projectRootPath = app.getPath("home");
}
// STOX-576 Windows home directory is not a good place for project files
if (properties?.projectRootPath?.includes("Program Files")) {
properties.projectRootPath = "";
}
if (properties.mapInfo == null) {
properties.mapInfo = {
projection: "StoX_001_LAEA",
zoom: 4.3,
origin: [10.01, 60.01],
};
//{projection:'StoX_001_LAEA', zoom:4.3, origin:[10,60]}
}
} catch (err) {
logInfo("Error reading properties: " + err);
}
};
const writePropertiesToFile = function writePropertiesToFile() {
logInfo("writePropertiesToFile... ");
if (properties == null) {
return; // Prevent properties to be reset.
}
let resourcefile = getPropertiesFileName();
logInfo("resourcefile... " + resourcefile);
try {
let options = { encoding: "utf-8", flag: "w" };
/*if (properties.projectRootPath) {
properties.projectRootPath = require('os').homedir();
*/
let str = JSON.stringify(properties, null, 2);
//logInfo("jsonString : " + jsonString);
fs.writeFileSync(resourcefile, str, options);
} catch (err) {
logInfo("Error writing properties " + err);
}
};
function callR(arg: string) {
const startTime = process.hrtime();
return new Promise(async (resolve) => {
while (callr_evaluate.length > 0) {
// pause when client is busy.
await new Promise((r) =>
setTimeout(() => {
r(null);
}, 50 /*ms*/)
);
}
callr_evaluate.push(true); // make my self ready. lock the other calls out if they appear at same time.
while (callr_evaluate.length > 1) {
// pause if 2 calls are released asynchronously from the first pause. (it could maybe happen) only one at the time.
await new Promise((r) =>
setTimeout(() => {
r(null);
}, 50 /*ms*/)
);
}
let ans: any = await evaluate(client, arg);
callr_evaluate.splice(callr_evaluate.length - 1, 1);
let diff = process.hrtime(startTime);
resolve({ time: diff[0] + diff[1] / 1000000000, result: ans });
});
}
async function evaluate(client: any, cmd: string) {
if (client == null) {
logInfo("client=NULL: " + cmd);
return null;
}
//console.log("> " + "evaluate: " + cmd);
let s = Buffer.from(cmd, "utf8").toString("hex"); // Encode command as hex
let lens = "" + s.length;
await new Promise(async (resolve) => {
client.handle = (data: any) => {
if (data == lens) {
// The length is send forth and back, we an proceed
resolve(null);
//console.log("> " + "length received: " + data);
} /*else {
console.log("> " + "length error: " + data);
}*/
};
//console.log("> " + "write length: " + lens);
await client.write("" + s.length);
});
let nResp = 0;
await new Promise(async (resolve) => {
client.handle = (data: any) => {
nResp = Number(data);
if (nResp != null) {
// recevied response length
resolve(null);
//console.log("> " + "write data nChar = " + nResp);
} /*else {
console.log("> " + "write data error nChar = null");
}*/
};
//console.log("> " + "write cmd hex: " + s);
await client.write(s); // may lead to throttling on the server side, but the server uses length info to get the string
});
// Total buffered preallocated before throttling, to avoid dynamic allocation time loss
let buf = Buffer.alloc(nResp);
let bufLen = 0;
let nChunks = 0;
await new Promise(async (resolve) => {
// calling handler from socket data event, registered once upon connection
client.handle = (data: any) => {
// Writing chunks (throttling) into total buffer
buf.write(data, bufLen);
bufLen = bufLen + data.length;
nChunks++;
if (bufLen == nResp) {
// test on length
// The response is received - finish
//console.log("> " + "throttling bufLen == nResp " + bufLen);
resolve(null);
} /*else if(bufLen < nResp) {
console.log("> " + "throttling bufLen, nResp " + bufLen + "," + nResp);
} else {
console.log("> " + "throttling overflow error bufLen=" + bufLen, ", nResp=" + nResp);
}*/
};
//console.log("> " + "write response length " + nResp);
await client.write("" + nResp); // handshake response length
});
return Buffer.from(buf.toString(), "hex").toString("utf8");
}
function setupServer() {
server = express();
//server.use(bodyParser.json())
//server.use(bodyParser.json({limit: '50mb'}));
server.use(bodyParser.text({limit: '50mb'}));
//server.use('/static', express.static('srv'))
server.use(cors()); // enable cors in header (http call from static resources)
//server.options(cors());
// Global content-type handler for all responses. text/plain is trusted by cors
server.use(function (req: any, res: any, next: any) {
res.type("text/plain");
next();
});
server.post("/updateactiveproject", function (req: any, res: any) {
properties.activeProject = req.body;
properties.projectRootPath = require("path").resolve(
properties.activeProject,
".."
);
logInfo("update active project: " + properties.activeProject);
res.send("ok");
});
server.post("/updateactiveprojectsavedstatus", function (req: any, res: any) {
activeProjectSaved = req.body === "true";
logInfo("update active project saved status: " + activeProjectSaved);
res.send("ok");
});
server.get("/readactiveproject", function (req: any, res: any) {
logInfo("read active project: " + properties.activeProject);
res.send(properties.activeProject);
});
server.post("/updateprojectrootpath", function (req: any, res: any) {
let projectRootPath = req.body;
//logInfo("in updateprojectrootpath jsonString : " + jsonString);
properties.projectRootPath = projectRootPath;
res.send("project root path updated");
});
// modify rpath in backend
server.post("/rpath", async (req: any, res: any) => {
properties.rPath = req.body;
logInfo("set rpath " + properties.rPath);
let resultstr: string = await startBackendServer(true);
res.send("post /rpath result:" + resultstr);
});
server.post("/mapInfo", async (req: any, res: any) => {
properties.mapInfo = JSON.parse(req.body);
//logInfo('set mapInfo ' + properties.mapInfo);
res.send("mapInfo updated");
});
// observe project root path
server.get("/projectrootpath", function (req: any, res: any) {
logInfo('get project root path ' + properties.projectRootPath);
res.send(properties.projectRootPath);
});
// observe rpath in backend
server.get("/", function (req: any, res: any) {
//logInfo('get rpath ' + properties.rPath);
res.send("Node server started");
});
// observe rpath in backend
server.get("/rpath", function (req: any, res: any) {
logInfo("get rpath " + properties.rPath);
res.send(properties.rPath);
});
server.get("/mapInfo", function (req: any, res: any) {
logInfo("get mapInfo " + properties.mapInfo);
res.send(properties.mapInfo);
});
server.get("/stoxversion", function (req: any, res: any) {
logInfo("get stoxVersion " + stoxVersion);
res.send(stoxVersion);
});
// observe rpath in backend
server.post("/callR", async (req: any, res: any) => {
while (rPackagesIsInstalling) {
// pause when installing packages
await new Promise((r) =>
setTimeout(() => {
r(null);
}, 50 /*ms*/)
);
}
if (client == null || !rAvailable) {
res.send({
value: null,
message: [],
warning: [],
error: [
"R is not available. Set path to R bin in Tools->R connection.",
],
});
return;
}
if (versionRstoxFramework == "") {
res.send({
value: null,
message: [],
warning: [],
error: [
"RstoxFramework is not installed. Install from Tools->Install R packages.",
],
});
return;
}
if (loadStatusRstoxFramework != "") {
res.send({
value: null,
message: [],
warning: [],
error: [
'RstoxFramework is not loaded properly due to message:\n"' +
loadStatusRstoxFramework +
'". \nTry to reinstall from Tools->Install R packages. (Remember also to close other R applications first)',
],
});
return;
}
// console.log("> " + "cmd: \"" + s.replace(/"/g, '\\"') + "\"");
//logInfo(req.body);
// this is a timout for a specific route for node express server.
// This will make long r calls like bootstrapping work.
req.setTimeout(2073600000); // use 24days proxy timeout.
let cmd: string =
"RstoxFramework::runFunction.JSON(" + JSON.stringify(req.body) + ")";
//cmd = cmd.replace(/\"/g, "\\\"");
//logInfo("cmd:" + cmd)
// The server is parsing the expression, a need for stringify to have 2^n-1 escapes where n is
// starting at 1 and going to 2 (because of parse). stringify and parse operates oppocite ways.
// The args in the body is already stringified (because of need to do do.call)
//cmd = JSON.stringify(cmd);
let s: any = await callR(cmd);
//logInfo('call R result ' + JSON.stringify(s));
// res.type('text/plain');
res.send(s.result);
});
server.post("/stopR", async (req: any, res: any) => {
const { modelName } = JSON.parse(req.body);
logInfo("Stopping R process by creating stopfile for model: " + modelName);
const stopFileName = (modelName ?? "") + "Stop";
const statusDir = "/process/projectSession/status/";
const stopFile =
properties.activeProject + statusDir + stopFileName + ".txt";
try {
fs.writeFileSync(stopFile, "stop", {
encoding: "utf-8",
flag: "w",
});
} catch (err) {
logInfo("Error writing stop file " + err);
}
});
server.post("/installRstoxFramework", async (req: any, res: any) => {
try {
let s: string = "R is not available";
rPackagesIsInstalling = true;
logInfo("/installRstoxFramework");
// Install remotes and data.table.
let cmd =
'if(!suppressWarnings(require(remotes, quietly = TRUE))) install.packages("remotes", type = "binary", quiet = TRUE, repos = "https://cloud.r-project.org")';
logInfo(cmd);
let res11 = ((await callR(cmd)) as any).result;
cmd =
'if(!suppressWarnings(require(data.table, quietly = TRUE))) install.packages("data.table", type = "binary", quiet = TRUE, repos = "https://cloud.r-project.org")';
logInfo(cmd);
let res12 = ((await callR(cmd)) as any).result;
await startBackendServer(false);
logInfo("server started: " + serverStarted + ", client: " + client);
if (serverStarted) {
let officialsRFTmpFile = Utils.getTempResFileName(
UtilsConstants.RES_SERVER_OFFICIALRSTOXFRAMEWORKVERSIONS,
"stox"
);
let cmd =
'StoXGUIInternal::installOfficialRstoxPackagesWithDependencies("' +
stoxVersion +
'", "' +
officialsRFTmpFile +
'", quiet = TRUE, toJSON = TRUE)';
logInfo(cmd);
let res = ((await callR(cmd)) as any).result;
// Build a string naming the packages that were installed, or an error message:
if (res.startsWith("Error")) {
s = res;
} else {
s = "Installed packages: " + res;
}
await startBackendServer(true);
await checkLoadStatusRstoxFramework();
logInfo(s);
}
res.send(s);
} finally {
rPackagesIsInstalling = false;
}
});
server.get("/getRstoxPackageVersions", async (req: any, res: any) => {
let packages: {
packageName: string;
version: string;
status: number;
}[] = [];
if (serverStarted) {
// logInfo("iterating through officialRstoxPackages " + officialRstoxPackages);
let officialsRFTmpFile = Utils.getTempResFileName(
UtilsConstants.RES_SERVER_OFFICIALRSTOXFRAMEWORKVERSIONS,
"stox"
);
let packagesStatus = officialRstoxPackages.map(async (s) => {
let elms: string[] = s.split("_");
// Determine status of each official package
let thisStatus = (
(await getPackageStatus(
elms[0],
stoxVersion,
officialsRFTmpFile
)) as any
).result;
let version = ((await getVersionStringOfPackage(elms[0])) as any)
.result;
return {
packageName: elms[0],
version: thisStatus == "3" ? " not installed" : version,
status: thisStatus,
};
});
packages = await Promise.all(packagesStatus);
} else {
packages.push({ packageName: "R disconnected", version: "", status: 3 });
}
logInfo("Rstox-packages: " + JSON.stringify(packages));
res.send(packages);
});
server.get("/getIsOfficialStoXVersion", function (req: any, res: any) {
logInfo("Is the StoX version official?: " + isOfficialStoXVersion);
res.send(isOfficialStoXVersion);
});
function resolveDefaultPath(defPath: string): string {
// electron showOpenDialog defaultPath requires c:\\temp\\test on win32, otherwise c:/temp/test on mac/linux
return defPath == null
? require("os").homedir()
: process.platform == "win32"
? defPath.replace(/\//g, "\\")
: defPath.replace(/\\/g, "/");
}
server.post("/browse", function (req: any, res: any) {
logInfo("select a folder... wait");
const defaultPath = resolveDefaultPath(req.body); // correct slashes in default path
logInfo("default folder " + defaultPath);
require("electron")
.dialog.showOpenDialog(mainWindow != null ? mainWindow : null, {
title: "Select a folder",
defaultPath,
properties: ["openDirectory"],
})
.then(
(object: {
canceled: boolean;
filePaths: string[];
bookmarks: string[];
}) => {
if (!object.filePaths || !object.filePaths.length) {
logInfo("You didn't select a folder");
return;
}
logInfo("You did select a folder");
logInfo(object.filePaths[0]);
res.send(object.filePaths[0]);
}
);
});
server.post("/browsePath", function (req: any, res: any) {
logInfo("select a file/folder path(s)");
let options: any = JSON.parse(req.body);
if (Object.keys(options).length) {
const defaultPath = resolveDefaultPath(options.defaultPath); // correct slashes in default path
logInfo("default folder " + defaultPath);
require("electron")
.dialog.showOpenDialog(mainWindow != null ? mainWindow : null, {
title: options.title,
defaultPath,
properties: options.properties,
})
.then(
(object: {
canceled: boolean;
filePaths: string[];