import { OwnedCommandFailure } from "./OwnedCommand.js"; /** Decode a complete stdout answer when the command exited successfully before finalization failed. */ export const readCompletedCommandAnswer = ( cause: unknown, decode: (stdout: string) => Answer, ): Answer | undefined => { if ( !(cause instanceof OwnedCommandFailure) || cause.collectionFailure || cause.snapshot?.exitCode !== 0 || cause.snapshot.outputIncomplete === true ) return undefined; const stdout = cause.snapshot.stdout; if (stdout.invalidUtf8 || stdout.bytes !== stdout.observedBytes) return undefined; try { return decode(stdout.text); } catch { return undefined; } };