import type { ErrorCode } from "./error"; import { ArchiveExtractionError, runArchiveExtractionTransaction as runArchiveExtractionTransactionBase, type ArchiveExtractionEntryReader, type ArchiveExtractionReceiptIR, type ArchiveExtractionStorage, } from "./archive-extraction-transaction"; export type ArchiveExtractionResourceFaultCode = Extract; /** Maps platform-specific allocation failures to the stable archive error contract. */ export function archiveExtractionResourceFaultCode(error: unknown): ArchiveExtractionResourceFaultCode | undefined { if (typeof error !== "object" || error === null) return undefined; const candidate = error as { code?: unknown; name?: unknown; message?: unknown }; if (candidate.code === "STORAGE_QUOTA" || candidate.code === "WASM_OUT_OF_MEMORY") return candidate.code; if (candidate.name === "QuotaExceededError" || candidate.name === "NotEnoughSpaceError") return "STORAGE_QUOTA"; if (candidate.name === "OutOfMemoryError") return "WASM_OUT_OF_MEMORY"; if (candidate.name === "RangeError" && typeof candidate.message === "string" && /out[ -]?of[ -]?memory|oom/i.test(candidate.message)) { return "WASM_OUT_OF_MEMORY"; } return undefined; } /** * Runs the existing atomic extraction transaction and normalizes resource failures only after * the base transaction has removed staging and revalidated the committed identity. */ export async function runArchiveExtractionTransaction( value: unknown, storage: ArchiveExtractionStorage, readEntry: ArchiveExtractionEntryReader, signal: AbortSignal, ): Promise { try { return await runArchiveExtractionTransactionBase(value, storage, readEntry, signal); } catch (error) { const code = archiveExtractionResourceFaultCode(error); if (code) throw new ArchiveExtractionError(code, "archive extraction released staging after a resource fault"); throw error; } } export type { ArchiveExtractionEntryIR, ArchiveExtractionRequestIR, ArchiveExtractionReceiptIR, ArchiveExtractionStorage, } from "./archive-extraction-transaction";