import { MARKER_PREFIX } from "./script"; type Marker = | { type: "step-start"; index: number } | { type: "step-end"; index: number; code: number } | { type: "step-skip"; index: number } | { type: "artifact"; name: string; path: string } | { type: "output"; name: string; value: string } | { type: "job-end"; failed: boolean } | { type: "setup-start" } | { type: "setup-end"; code: number }; function decodeBase64(value: string): string { try { return new TextDecoder().decode(Uint8Array.from(atob(value), (char) => char.charCodeAt(0))); } catch { return ""; } } export function parseMarker(line: string): Marker | null { if (!line.startsWith(MARKER_PREFIX)) return null; const [type, ...args] = line.slice(MARKER_PREFIX.length).trim().split(" "); const number = (value: string | undefined) => Number(value ?? "0"); switch (type) { case "step-start": return { type, index: number(args[0]) }; case "step-end": return { type, index: number(args[0]), code: number(args[1]) }; case "step-skip": return { type, index: number(args[0]) }; case "artifact": return { type, name: args[0] ?? "artifact", path: args.slice(1).join(" ").replace(/^"|"$/g, "") }; case "output": return { type, name: args[0] ?? "", value: decodeBase64(args[1] ?? "") }; case "job-end": return { type, failed: args[0] !== "0" }; case "setup-start": return { type }; case "setup-end": return { type, code: number(args[0]) }; default: return null; } } const MAX_JOB_OUTPUTS = 50; const MAX_OUTPUT_BYTES = 64 * 1024; /** Job outputs flow into other jobs' scripts, so their count and total size are bounded. */ export function withOutput(outputs: Record, name: string, value: string): Record { if (!(name in outputs) && Object.keys(outputs).length >= MAX_JOB_OUTPUTS) return outputs; const used = Object.entries(outputs).reduce((sum, [key, item]) => sum + (key === name ? 0 : item.length), 0); if (used + value.length > MAX_OUTPUT_BYTES) return outputs; return { ...outputs, [name]: value }; }