Treasury & Bank Transfer Management System — سامانه مدیریت خزانهداری و حوالههای بانکی
dev/v7-production-failclosed-20261021
ci/public-windows-bridge-probe-20261017
ci/windows-dispatch-bootstrap-20261016
ci/windows-dual-app-lab-20261016
dev/v7-production-failclosed-20261021
feature/reconciliation-guard-20261013
main
phase-1-foundation
phase-1-implementation
phase-2-treasury-core
phase-3-banking-reconciliation
phase-4-operations-enterprise-ux
phase-5-hardening-release
phase-6-production-platform
phase-7-production-infrastructure-bank-golive
phase-7-sql-readonly-ux-20261015
transfer-exe-to-drive-20261006
| 1 | const { app, BrowserWindow, shell } = require('electron'); |
| 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 | |
| 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 | }; |
| 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'); |
| 64 | } |
| 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, |
| 73 | stdio: vmDiagnostics ? ['ignore', 'ignore', 'pipe'] : 'ignore', |
| 74 | env: { |
| 75 | ...process.env, |
| 76 | ELECTRON_RUN_AS_NODE: '1', |
| 77 | NODE_ENV: 'production', |
| 78 | HOSTNAME: '127.0.0.1', |
| 79 | PORT: String(PORT), |
| 80 | TREASURYFLOW_DESKTOP_LOCAL: '1', |
| 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`); |
| 101 | } |
| 102 | |
| 103 | async function createWindow() { |
| 104 | await startServer(); |
| 105 | const win = new BrowserWindow({ |
| 106 | width: 1440, |
| 107 | height: 920, |
| 108 | minWidth: 1024, |
| 109 | minHeight: 700, |
| 110 | show: false, |
| 111 | autoHideMenuBar: true, |
| 112 | webPreferences: { nodeIntegration: false, contextIsolation: true, sandbox: true } |
| 113 | }); |
| 114 | win.webContents.setWindowOpenHandler(({ url }) => { |
| 115 | if (/^https?:\/\//i.test(url)) shell.openExternal(url); |
| 116 | return { action: 'deny' }; |
| 117 | }); |
| 118 | win.webContents.on('will-navigate', (event, url) => { |
| 119 | if (!url.startsWith(`http://127.0.0.1:${PORT}`)) event.preventDefault(); |
| 120 | }); |
| 121 | await win.loadURL(`http://127.0.0.1:${PORT}`); |
| 122 | win.once('ready-to-show', () => win.show()); |
| 123 | } |
| 124 | |
| 125 | app.whenReady().then(createWindow).catch(err => { |
| 126 | console.error(err); |
| 127 | app.quit(); |
| 128 | }); |
| 129 | app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); }); |
| 130 | app.on('before-quit', () => { app.isQuitting = true; if (serverProcess) serverProcess.kill(); }); |