Multiplayer agent harness for work.
import type { IncomingMessage, ServerResponse } from "node:http";
import { gzip } from "node:zlib";
const COMPRESS_MIN_BYTES = 1024;
export class PayloadTooLargeError extends Error {
constructor() {
super("request body too large");
this.name = "PayloadTooLargeError";
}
}
export function gzipAccepted(req: IncomingMessage | undefined): boolean {
const header = req?.headers["accept-encoding"];
if (header === undefined) return false;
let explicit: boolean | undefined;
let wildcard: boolean | undefined;
for (const part of (Array.isArray(header) ? header.join(",") : header).split(",")) {
const [name, ...params] = part.split(";");
const token = (name ?? "").trim().toLowerCase();
if (token !== "gzip" && token !== "*") continue;
const q = params.map((p) => p.trim().toLowerCase()).find((p) => p.startsWith("q="));
const weight = q === undefined ? 1 : Number.parseFloat(q.slice(2));
const allowed = Number.isFinite(weight) && weight > 0;
if (token === "*") wildcard = allowed;
else explicit = allowed;
}
return explicit ?? wildcard ?? false;
}
export function withVary(res: ServerResponse, value: string): string {
const existing = res.getHeader("vary");
const tokens = [...(existing === undefined ? [] : String(existing).split(",")), ...value.split(",")]
.map((token) => token.trim())
.filter(Boolean);
return [...new Map(tokens.map((token) => [token.toLowerCase(), token])).values()].join(", ");
}
export function sendBuffered(res: ServerResponse, status: number, headers: Record<string, string>, body: string): void {
const out = { ...headers, vary: withVary(res, "accept-encoding") };
if (Buffer.byteLength(body) < COMPRESS_MIN_BYTES || !gzipAccepted(res.req)) {
res.writeHead(status, out);
res.end(body);
return;
}
gzip(body, (err, packed) => {
if (res.headersSent || res.writableEnded || res.destroyed) return;
try {
if (err) {
res.writeHead(status, out);
res.end(body);
return;
}
res.writeHead(status, { ...out, "content-encoding": "gzip", "content-length": String(packed.length) });
res.end(packed);
} catch {
res.destroy();
}
});
}
export function json(res: ServerResponse, status: number, body: unknown): void {
sendBuffered(res, status, { "content-type": "application/json" }, JSON.stringify(body));
}
export async function readBodyBytes(req: IncomingMessage, maxBytes = Infinity): Promise<Buffer> {
const chunks: Buffer[] = [];
let size = 0;
for await (const c of req) {
size += (c as Buffer).length;
if (size > maxBytes) throw new PayloadTooLargeError();
chunks.push(c as Buffer);
}
return Buffer.concat(chunks);
}
export async function readBody(req: IncomingMessage, maxBytes = Infinity): Promise<string> {
return (await readBodyBytes(req, maxBytes)).toString("utf8");
}
export function sameOriginRequest(req: IncomingMessage, expectedOrigin: string): boolean {
const origin = req.headers.origin;
const originMatches =
typeof origin === "string" &&
(() => {
try {
return new URL(origin).origin === expectedOrigin;
} catch {
return false;
}
})();
const site = req.headers["sec-fetch-site"];
if (typeof site !== "string") return originMatches;
return site === "same-origin" && (originMatches || origin === undefined || origin === "null");
}
export function cookie(req: IncomingMessage, name: string): string | null {
const m = (req.headers.cookie ?? "").match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
return m ? decodeURIComponent(m[1] ?? "") || null : null;
}
export function escapeHtml(s: string): string {
return s.replace(
/[&<>"']/g,
(c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c] ?? c,
);
}
const SVG_DOCUMENT = /^<svg[\s>][\s\S]*<\/svg>$/i;
function faviconSvg(icon: { svg?: string; emoji: string }): string {
const custom = icon.svg?.trim() ?? "";
if (SVG_DOCUMENT.test(custom)) return custom;
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90" text-anchor="middle" x="50">${icon.emoji}</text></svg>`;
}
export function serveFavicon(res: ServerResponse, icon: { svg?: string; emoji: string }, cacheControl: string): void {
res.writeHead(200, {
"content-type": "image/svg+xml; charset=utf-8",
"cache-control": cacheControl,
"x-content-type-options": "nosniff",
"content-security-policy": "default-src 'none'; style-src 'unsafe-inline'",
});
res.end(faviconSvg(icon));
}