Something went wrong. Try again.
A reusable desktop shell: your sidecar, your index, safe APIs, installers for every platform
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677// Runs the app's server. With `node: true` the script is forked onto Electron's// own Node (a utility process), so installers carry no runtime; otherwise// `command` is spawned as is. Output goes to the sidecar log, and the shell// waits for `ready` to answer before it opens a window.import { spawn } from 'node:child_process';import { resolve } from 'node:path';import { utilityProcess } from 'electron';import { resolveCommand } from './launch.js';
export async function startSidecar ({ sidecar, appDir, env, log, name = 'sidecar' }) { const child = fork(sidecar, appDir, env); child.stdout?.on('data', (chunk) => log.raw(chunk)); child.stderr?.on('data', (chunk) => log.raw(chunk, true)); const exited = new Promise((res) => { child.once('exit', (code, signal) => res({ code, signal: signal || null })); child.once?.('error', (err) => res({ code: null, signal: null, error: err })); }); exited.then(({ code, signal, error }) => log.info(error ? `${name} failed to start: ${error.message}` : `${name} exited (${signal || code})`)); const handle = { child, exited, kill () { if (sidecar.node) child.kill(); else { child.kill('SIGTERM'); const hard = setTimeout(() => child.kill('SIGKILL'), 3000); exited.then(() => clearTimeout(hard)); } return exited; }, }; try { if (sidecar.ready) await waitForReady(sidecar.ready, { timeout: sidecar.readyTimeout, exited, name }); else { // Nothing to wait for; still catch one that falls over at once. const early = await Promise.race([exited, new Promise((res) => setTimeout(() => res(null), 300))]); if (early) throw new Error(early.error ? `The ${name} could not start: ${early.error.message}` : `The ${name} exited at once (${early.signal || early.code})`); } } catch (err) { handle.kill(); throw err; } return handle;}
function fork (sidecar, appDir, env) { // The sidecar does the app's networking, so it trusts what the machine // trusts: Node's bundled roots alone would refuse a LAN box behind a // locally issued certificate that the OS keychain already knows. const processEnv = { NODE_USE_SYSTEM_CA: '1', ...env }; if (sidecar.node) { const [script, ...args] = sidecar.args; return utilityProcess.fork(resolve(appDir, script), args, { cwd: appDir, env: processEnv, stdio: 'pipe', serviceName: 'mollusc sidecar' }); } return spawn(resolveCommand(sidecar.command, appDir), sidecar.args, { cwd: appDir, env: processEnv, stdio: ['ignore', 'pipe', 'pipe'] });}
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
// Resolves once `url` answers at all (any status: listening is what counts);// rejects if the process exits first or the timeout passes.export async function waitForReady (url, { timeout = 30000, exited, interval = 150, name = 'sidecar' }) { const deadline = Date.now() + timeout; let done = false; const died = exited.then((r) => { if (!done) throw new Error(r.error ? `The ${name} could not start: ${r.error.message}` : `The ${name} exited (${r.signal || r.code}) before ${url} answered`); }); died.catch(() => {}); while (Date.now() < deadline) { const res = await Promise.race([fetch(url, { signal: AbortSignal.timeout(interval * 4) }).catch(() => null), died]); if (res) { done = true; return res; } await Promise.race([sleep(interval), died]); } throw new Error(`Timed out after ${timeout} ms waiting for ${url}`);}