| 1 | import { app, BrowserWindow, protocol, ipcMain, net } from "electron"; |
| 2 | import { readFileSync } from "node:fs"; |
| 3 | import { join } from "node:path"; |
| 4 | import { registerAppProtocol } from "../../src/main/protocol.js"; |
| 5 | import { registerRendererIpc } from "../../src/main/ipc.js"; |
| 6 | import { ProcessDiagnostics } from "../../src/main/processDiagnostics.js"; |
| 7 | import { createPerformanceHost } from "../../src/main/performanceHost.js"; |
| 8 | |
| 9 | const config = JSON.parse(readFileSync(join(__dirname, "performance-config.json"), "utf8")) as { |
| 10 | benchmark: boolean; monitor: boolean; workerPath: string; |
| 11 | }; |
| 12 | app.setPath("userData", join(__dirname, "profile")); |
| 13 | protocol.registerSchemesAsPrivileged([{ scheme: "reasonix", privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: false, stream: true } }]); |
| 14 | app.whenReady().then(async () => { |
| 15 | const log = { info() {}, warn() {}, error() {} }; |
| 16 | let copied = ""; |
| 17 | const clipboard = { writeText: (text: string) => { copied = text; }, readText: () => copied }; |
| 18 | registerAppProtocol({ protocol, fetch: net.fetch, distRoot: __dirname, resources: () => null, log }); |
| 19 | const win = new BrowserWindow({ |
| 20 | show: true, focusable: !config.benchmark, width: 1000, height: 750, |
| 21 | webPreferences: { backgroundThrottling: !config.benchmark, preload: join(__dirname, "preload.cjs"), sandbox: true, contextIsolation: true, nodeIntegration: false }, |
| 22 | }); |
| 23 | const diagnostics = new ProcessDiagnostics(() => app.getAppMetrics(), undefined, () => config.benchmark || (win.isVisible() && win.isFocused())); |
| 24 | if (config.monitor) { diagnostics.sample(); setInterval(() => diagnostics.sample(), 30000).unref(); } |
| 25 | const performanceHost = createPerformanceHost({ |
| 26 | window: () => win.isDestroyed() ? null : win, |
| 27 | workerPath: config.workerPath, |
| 28 | locale: () => "en", |
| 29 | isForeground: config.benchmark ? () => true : undefined, |
| 30 | dialog: { |
| 31 | showMessageBox: async () => ({ response: 1, checkboxChecked: false }), |
| 32 | showSaveDialog: async () => ({ canceled: false, filePath: join(__dirname, "fixture.heapsnapshot") }), |
| 33 | }, |
| 34 | }); |
| 35 | registerRendererIpc({ |
| 36 | ipcMain, contract: { protocolVersion: 1, digest: "test", commands: [], commandSet: new Set() }, |
| 37 | window: { isTrustedSender: (sender, frame) => sender === win.webContents && frame === sender.mainFrame } as Parameters<typeof registerRendererIpc>[0]["window"], |
| 38 | clipboard, invoke: async () => undefined, openExternal: async () => undefined, |
| 39 | serviceState: () => ({ phase: "ready", generation: "test" }), |
| 40 | processDiagnostics: () => diagnostics.snapshot(), performance: performanceHost, log, |
| 41 | }); |
| 42 | app.once("will-quit", () => performanceHost.dispose()); |
| 43 | const url = new URL("reasonix://app/index.html"); |
| 44 | url.searchParams.set("monitor", config.monitor ? "1" : "0"); |
| 45 | url.searchParams.set("benchmark", config.benchmark ? "1" : "0"); |
| 46 | await win.loadURL(url.href); |
| 47 | if (!config.benchmark) win.focus(); |
| 48 | }); |
| 49 |