Launcher release / release (push) Canceled after 0s
Add a quick ZIP log export button to the About page (also bundling the latest embedded browser -browser.log while excluding player saves) and add a runtime logging toggle under Settings -> Diagnostics that controls launcher file logs, local server traffic logs, and wto_ws.log via log=0 in wto_ws.cfg. Update the v0.1.2 release note accordingly.
97 lines
3.4 KiB
TypeScript
97 lines
3.4 KiB
TypeScript
import { once } from 'node:events';
|
|
import { join } from 'node:path';
|
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
import { utilityProcess } from 'electron';
|
|
import { z } from 'zod';
|
|
import { HEALTH_PATH, LOOPBACK, type ServerOptions } from '@wto/server';
|
|
import { AppError } from '@shared/errors.ts';
|
|
import type { ServerWorkerStatus } from '../../workers/server-worker.ts';
|
|
import { forwardProcessOutput } from './process-output.ts';
|
|
|
|
const Health = z.object({ app: z.literal('wto-server'), pid: z.number() });
|
|
|
|
interface LocalServer {
|
|
readonly exited: Promise<number>;
|
|
close(): Promise<void>;
|
|
}
|
|
|
|
export function localServerStatusUrl(port: number): string {
|
|
return `http://${LOOPBACK}:${port}${HEALTH_PATH}`;
|
|
}
|
|
|
|
export async function localServerReady(port: number, pid?: number, signal?: AbortSignal): Promise<boolean> {
|
|
const deadline = AbortSignal.timeout(900);
|
|
try {
|
|
const response = await fetch(localServerStatusUrl(port), {
|
|
redirect: 'error', signal: signal ? AbortSignal.any([signal, deadline]) : deadline,
|
|
});
|
|
if (!response.ok) return false;
|
|
const health = Health.parse(await response.json());
|
|
return pid === undefined || health.pid === pid;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function startLocalServer(
|
|
{ gameFolder, dataFolder, port, loggingEnabled = true }: Omit<ServerOptions, 'report'>, signal: AbortSignal,
|
|
): Promise<LocalServer> {
|
|
signal.throwIfAborted();
|
|
const worker = utilityProcess.fork(
|
|
join(import.meta.dirname, 'server-worker.js'),
|
|
[gameFolder, dataFolder, String(port), loggingEnabled ? '1' : '0'],
|
|
{
|
|
serviceName: 'WTOffline local server',
|
|
stdio: 'pipe',
|
|
},
|
|
);
|
|
forwardProcessOutput(worker.stdout, 'server', 'info');
|
|
forwardProcessOutput(worker.stderr, 'server', 'error');
|
|
|
|
let ended = false;
|
|
const exited = new Promise<number>((resolve) => worker.once('exit', (code) => {
|
|
ended = true;
|
|
resolve(code);
|
|
}));
|
|
worker.on('error', (type, location) => console.error('[server] utility process error:', type, location));
|
|
|
|
const stop = async (graceful: boolean): Promise<void> => {
|
|
if (!ended && graceful) {
|
|
worker.postMessage('close');
|
|
await Promise.race([exited, delay(3000, undefined, { ref: false })]);
|
|
}
|
|
if (!ended && !worker.kill() && worker.pid === undefined) {
|
|
worker.once('spawn', () => worker.kill());
|
|
}
|
|
await exited;
|
|
};
|
|
|
|
const startup = AbortSignal.any([signal, AbortSignal.timeout(60_000)]);
|
|
let listening = false;
|
|
try {
|
|
const reply: ServerWorkerStatus = await Promise.race([
|
|
once(worker, 'message', { signal: startup }).then(([status]) => status as ServerWorkerStatus),
|
|
exited.then(() => 'failed' as const),
|
|
]);
|
|
if (reply === 'port-busy') throw new AppError('launch_port_busy');
|
|
const pid = worker.pid;
|
|
if (reply !== 'listening' || pid === undefined) throw new AppError('launch_server_failed');
|
|
listening = true;
|
|
while (!ended) {
|
|
startup.throwIfAborted();
|
|
if (await localServerReady(port, pid, startup)) {
|
|
startup.throwIfAborted();
|
|
if (!ended) return { exited, close: () => stop(true) };
|
|
break;
|
|
}
|
|
await delay(150, undefined, { signal: startup });
|
|
}
|
|
throw new AppError('launch_server_failed');
|
|
} catch (error) {
|
|
await stop(listening);
|
|
signal.throwIfAborted();
|
|
console.error('[server] startup failed:', error);
|
|
throw error instanceof AppError ? error : new AppError('launch_server_failed');
|
|
}
|
|
}
|