import { parentPort } from 'node:worker_threads'; import { posix } from 'node:path'; import { db } from '../db/index.js'; import { relDestForKind } from '../config.js'; import { captureRecipe, summarizeRecipe } from '../fleet/launch-recipes.js'; import { sha256Buffer } from '../fleet/files.js'; import type { NativeLaunchInput } from './launch-preparation.js'; import { choosePlacement, type PlacementOptions } from '../hosts/capacity.js'; parentPort!.on('message', (message: { id: number; kind: 'launch'; input: NativeLaunchInput } | { id: number; kind: 'placement'; input: PlacementOptions }) => { const { id } = message; try { if (message.kind === 'placement') { const decision = db().transaction(() => choosePlacement(message.input))(); parentPort!.postMessage({ id, value: { hostId: decision.host.id, lastHeartbeat: decision.host.lastHeartbeat } }); return; } const input = message.input; // All file versions and composed configuration belong to one snapshot. const recipe = db().transaction(() => ({ ...captureRecipe(input.game, input.mapSlug), native: input.native, image: input.instance.image }))(); const instance = input.instance; const payload = { name: instance.server, game: recipe.game, libraryId: instance.library_id, image: instance.image, port: instance.port, bindAddress: instance.bind_address, rconPassword: input.rconPassword, env: { PLUTAINER_SERVER_NAME: instance.server, PLUTAINER_PORT: String(instance.port), PLUTAINER_GAME: recipe.game === 't6' ? 't6zm' : 't5sp', PLUTAINER_CONFIG_FILE: recipe.configFile, PLUTAINER_MAP_ROTATE: 'false', PLUTAINER_EXTRA_ARGS: `+exec electric_cherry_map.cfg +set tws_server_id ${instance.server} +map_rotate`, PLUTO_SERVER_KEY: input.serverKey }, files: recipe.files.map(file => ({ path: posix.join(relDestForKind(file.kind, recipe.game), file.path), contentBase64: file.content, sha256: file.sha256 })), ...(input.rebuild ? { nextGeneration: instance.generation + 1, ...(input.safetyOverride ? { safetyOverride: true } : {}) } : {}) }; const encode = (value: unknown) => new TextEncoder().encode(JSON.stringify(value)); const recipeJson = encode(recipe); const payloadJson = encode(payload); const plan = { ...recipe, files: recipe.files.map(({ content: _content, ...file }) => file) }; const recipeSummary = JSON.stringify(summarizeRecipe(plan, sha256Buffer(Buffer.from(recipeJson.buffer)))); // Transfer ownership of the encoded bytes; do not clone giant strings or objects. parentPort!.postMessage({ id, value: { recipe: plan, recipeSummary, recipeJson, payloadJson } }, [recipeJson.buffer, payloadJson.buffer]); } catch (error) { parentPort!.postMessage({ id, error: error instanceof Error ? error.message : 'Launch preparation failed.' }); } });