Bound renderer memory use and configure worker heap ceiling

This commit is contained in:
2026-10-04 19:17:15 +05:00
parent a44e28c99f
commit 3b3cf6997c
4 changed files with 47 additions and 6 deletions

View File

@@ -19,6 +19,25 @@ export async function renderSnapshot(
const server = createServer(async (req, res) => {
try {
const url = new URL(req.url, "http://localhost");
if (url.pathname === "/terrain.json") {
res.setHeader("Content-Type", "application/json");
// Avoid Playwright's recursive protocol serialization of millions of
// block records. Keep serialization buffers bounded and honor backpressure.
const send = async (chunk) => {
if (res.destroyed) throw new Error("Terrain connection closed");
await new Promise((resolve, reject) => {
res.write(chunk, (error) => error ? reject(error) : resolve());
});
};
const { blocks, ...metadata } = snapshot;
await send(JSON.stringify(metadata).slice(0, -1) + ',"blocks":[');
for (let i = 0; i < blocks.length; i += 2048) {
if (signal?.aborted) throw new Error("Rendering cancelled");
await send((i ? "," : "") + JSON.stringify(blocks.slice(i, i + 2048)).slice(1, -1));
}
res.end("]}");
return;
}
let path =
url.pathname === "/three.js"
? "node_modules/three/build/three.module.js"
@@ -44,6 +63,7 @@ export async function renderSnapshot(
);
res.end(await readFile(path));
} catch {
if (res.headersSent) { res.destroy(); return; }
res.writeHead(404);
res.end();
}
@@ -59,7 +79,11 @@ export async function renderSnapshot(
page.on("pageerror", (err) => console.error("Renderer:", err.message));
await page.goto(`http://127.0.0.1:${server.address().port}`);
await page.waitForFunction(() => window.rendererReady);
const stats = await page.evaluate((s) => window.buildTerrain(s), snapshot);
const stats = await page.evaluate(async () => {
const response = await fetch("/terrain.json");
if (!response.ok) throw new Error("Unable to load terrain snapshot");
return window.buildTerrain(await response.json());
});
for (const p of viewpoints) {
if (signal?.aborted) throw new Error("Rendering cancelled");
await mkdir(`${output}/${p.id}`, { recursive: true });