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

fix(desktop): fail closed on unexpected localhost service

davoodmehraban89 committed Oct 16, 2026 at 22:42 UTC 63a06ccc5038d98390201ea22d34419be33f21a3
1 file changed +49 -16
electron/main.cjs
+49 -16
@@ -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');
@@ -47,6 +79,7 @@ async function startServer() {
79 BANK_ADAPTER_MODE: 'disabled'
80 }
81 });
82 + serverProcess.on('error', err => { serverStartError = err; });
83 serverProcess.on('exit', code => {
84 if (!app.isQuitting && code !== 0) console.error('TreasuryFlow server exited', code);
85 });