|
| 1 | +import * as path from "path" |
| 2 | +import * as fs from "fs/promises" |
| 3 | + |
| 4 | +import { createWriteStream } from "fs" |
| 5 | + |
| 6 | +export const PHASE_RESULT_VERSION = 1 as const |
| 7 | + |
| 8 | +export type RestartPhase = "create" | "verify" |
| 9 | +export type PhaseStatus = "passed" | "failed" |
| 10 | + |
| 11 | +export type PhaseError = { |
| 12 | + message: string |
| 13 | + stack?: string |
| 14 | +} |
| 15 | + |
| 16 | +export type PhaseResult = { |
| 17 | + version: typeof PHASE_RESULT_VERSION |
| 18 | + phase: RestartPhase |
| 19 | + status: PhaseStatus |
| 20 | + values?: Record<string, string> |
| 21 | + error?: PhaseError |
| 22 | +} |
| 23 | + |
| 24 | +const phaseResultNames: Record<RestartPhase, string> = { |
| 25 | + create: "01-create.json", |
| 26 | + verify: "02-verify.json", |
| 27 | +} |
| 28 | + |
| 29 | +export function getPhaseResultPath(resultsDir: string, phase: RestartPhase): string { |
| 30 | + const resolvedResultsDir = path.resolve(resultsDir) |
| 31 | + const resultPath = path.resolve(resolvedResultsDir, phaseResultNames[phase]) |
| 32 | + if (path.dirname(resultPath) !== resolvedResultsDir) { |
| 33 | + throw new Error(`Phase result path escaped the results directory: ${phase}`) |
| 34 | + } |
| 35 | + return resultPath |
| 36 | +} |
| 37 | + |
| 38 | +export function serializePhaseError(error: unknown): PhaseError { |
| 39 | + if (error instanceof Error) { |
| 40 | + return { |
| 41 | + message: error.message.slice(0, 2_000), |
| 42 | + ...(error.stack && { stack: error.stack.slice(0, 4_000) }), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return { message: String(error).slice(0, 2_000) } |
| 47 | +} |
| 48 | + |
| 49 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 50 | + return typeof value === "object" && value !== null |
| 51 | +} |
| 52 | + |
| 53 | +export function validatePhaseResult(value: unknown): asserts value is PhaseResult { |
| 54 | + if (!isRecord(value) || value.version !== PHASE_RESULT_VERSION) { |
| 55 | + throw new Error("Invalid phase result version") |
| 56 | + } |
| 57 | + if (value.phase !== "create" && value.phase !== "verify") { |
| 58 | + throw new Error("Invalid phase result phase") |
| 59 | + } |
| 60 | + if (value.status !== "passed" && value.status !== "failed") { |
| 61 | + throw new Error("Invalid phase result status") |
| 62 | + } |
| 63 | + if (value.values !== undefined) { |
| 64 | + if (!isRecord(value.values) || Object.values(value.values).some((entry) => typeof entry !== "string")) { |
| 65 | + throw new Error("Phase result values must be string-valued") |
| 66 | + } |
| 67 | + } |
| 68 | + if (value.error !== undefined) { |
| 69 | + if (!isRecord(value.error) || typeof value.error.message !== "string") { |
| 70 | + throw new Error("Invalid phase result error") |
| 71 | + } |
| 72 | + if (value.error.stack !== undefined && typeof value.error.stack !== "string") { |
| 73 | + throw new Error("Invalid phase result error stack") |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export async function writePhaseResult(resultsDir: string, result: PhaseResult): Promise<void> { |
| 79 | + validatePhaseResult(result) |
| 80 | + const targetPath = getPhaseResultPath(resultsDir, result.phase) |
| 81 | + const temporaryPath = `${targetPath}.${process.pid}.${Date.now()}.tmp` |
| 82 | + try { |
| 83 | + await writeJsonAtomically(temporaryPath, result) |
| 84 | + await fs.rename(temporaryPath, targetPath) |
| 85 | + } finally { |
| 86 | + await fs.rm(temporaryPath, { force: true }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +async function writeJsonAtomically(filePath: string, value: PhaseResult): Promise<void> { |
| 91 | + await fs.mkdir(path.dirname(filePath), { recursive: true }) |
| 92 | + await new Promise<void>((resolve, reject) => { |
| 93 | + const stream = createWriteStream(filePath, { encoding: "utf8" }) |
| 94 | + stream.once("error", reject) |
| 95 | + stream.once("finish", resolve) |
| 96 | + stream.end(JSON.stringify(value)) |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +export async function readPhaseResult(resultsDir: string, phase: RestartPhase): Promise<PhaseResult> { |
| 101 | + const result = JSON.parse(await fs.readFile(getPhaseResultPath(resultsDir, phase), "utf8")) as unknown |
| 102 | + validatePhaseResult(result) |
| 103 | + if (result.phase !== phase) { |
| 104 | + throw new Error(`Phase result does not match requested phase: ${phase}`) |
| 105 | + } |
| 106 | + return result |
| 107 | +} |
0 commit comments