Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

Harden V7 prototype Electron startup with proven strict readiness checks and opt-in bounded synthetic VM diagnostics

davoodmehraban89 committed Oct 26, 2026 at 08:03 UTC b4280b26faa57785fac21d0c5d7fe3459f9c23f3
1 file changed +64 -17
electron/main.cjs
+64 -17
@@ -2,30 +2,62 @@
2 const { spawn } = require('node:child_process');
3 const path = require('node:path');
4 const http = require('node:http');
5 +const { isExpectedReadiness } = require('./ready-check.cjs');
6
7 let serverProcess;
8 +let serverStartError = null;
9 const PORT = 3210;
10
9 -function waitForServer(url, timeoutMs = 30000) {
10 - const started = Date.now();
11 - return new Promise((resolve, reject) => {
12 - const attempt = () => {
13 - const req = http.get(url, res => {
14 - res.resume();
15 - if (res.statusCode && res.statusCode < 500) return resolve();
16 - retry();
17 - });
18 - req.on('error', retry);
19 - req.setTimeout(1500, () => { req.destroy(); retry(); });
20 - };
21 - const retry = () => {
22 - if (Date.now() - started > timeoutMs) return reject(new Error('TreasuryFlow local server did not start'));
23 - setTimeout(attempt, 300);
11 +function probeReadiness(url) {
12 + return new Promise(resolve => {
13 + let settled = false;
14 + const finish = value => {
15 + if (!settled) {
16 + settled = true;
17 + resolve(value);
18 + }
19 };
25 - attempt();
20 + const req = http.get(url, res => {
21 + const parts = [];
22 + let bytes = 0;
23 + res.on('data', chunk => {
24 + bytes += chunk.length;
25 + if (bytes > 4096) {
26 + req.destroy();
27 + finish(false);
28 + } else {
29 + parts.push(chunk);
30 + }
31 + });
32 + res.on('end', () => finish(isExpectedReadiness(res.statusCode, Buffer.concat(parts).toString('utf8'))));
33 + res.on('error', () => finish(false));
34 + });
35 + req.on('error', () => finish(false));
36 + req.setTimeout(1500, () => {
37 + req.destroy();
38 + finish(false);
39 + });
40 });
41 }
42
43 +async function waitForServer(url, timeoutMs = 30000) {
44 + const deadline = Date.now() + timeoutMs;
45 + while (Date.now() < deadline) {
46 + if (serverStartError || !serverProcess ||
47 + serverProcess.exitCode !== null || serverProcess.signalCode !== null) {
48 + throw new Error('TreasuryFlow local server exited before readiness');
49 + }
50 + if (await probeReadiness(url)) {
51 + if (serverStartError || serverProcess.exitCode !== null || serverProcess.signalCode !== null) {
52 + throw new Error('TreasuryFlow local server exited during readiness');
53 + }
54 + return;
55 + }
56 + await new Promise(resolve => setTimeout(resolve, 300));
57 + }
58 + throw new Error('TreasuryFlow local server did not reach authenticated application readiness');
59 +}
60 +
61 function serverPath() {
62 if (app.isPackaged) return path.join(process.resourcesPath, 'standalone', 'server.js');
63 return path.join(app.getAppPath(), '.next', 'standalone', 'server.js');
@@ -33,10 +65,12 @@ function serverPath() {
65
66 async function startServer() {
67 const script = serverPath();
68 + // Expose bounded diagnostic stderr only in a synthetic-data Windows VM.
69 + const vmDiagnostics = process.env.TREASURYFLOW_VM_DIAGNOSTICS === '1';
70 serverProcess = spawn(process.execPath, [script], {
71 cwd: path.dirname(script),
72 windowsHide: true,
39 - stdio: 'ignore',
73 + stdio: vmDiagnostics ? ['ignore', 'ignore', 'pipe'] : 'ignore',
74 env: {
75 ...process.env,
76 ELECTRON_RUN_AS_NODE: '1',
@@ -47,7 +81,20 @@ async function startServer() {
81 BANK_ADAPTER_MODE: 'disabled'
82 }
83 });
84 + let childStderr = '';
85 + if (vmDiagnostics && serverProcess.stderr) {
86 + serverProcess.stderr.on('data', chunk => {
87 + childStderr = (childStderr + chunk.toString('utf8')).slice(-2400);
88 + });
89 + }
90 + serverProcess.on('error', err => { serverStartError = err; });
91 serverProcess.on('exit', code => {
92 + if (vmDiagnostics && childStderr) {
93 + const scrubbed = childStderr
94 + .replace(/\b(password|token|secret|credential|authorization|api[_-]?key)\s*[:=]\s*\S+/gi, '$1=[REDACTED]')
95 + .replace(/https?:\/\/[^@\s]+@/gi, 'https://[REDACTED]@');
96 + console.error('TreasuryFlow child stderr (sanitized):', scrubbed);
97 + }
98 if (!app.isQuitting && code !== 0) console.error('TreasuryFlow server exited', code);
99 });
100 await waitForServer(`http://127.0.0.1:${PORT}/api/ready`);