Files
reverse-engineering-anything/src/application/RegularFileRead.ts
T
Ysopwnable 6ce3288f3b fix(cli): stream JSON inputs beyond the whole-document string limit (#1360)
Stream large CLI JSON files through a strict UTF-8 decoder and standard JSON parser using the same admitted regular-file handle. Preserve native parsing for small inputs, typed malformed-input/resource/access-denied errors, cancellation, and complete Evidence.

Verified with focused public CLI regressions, a 768 MB whitespace-padded valid request, and the Windows native decoder boundary. Individual strings and the assembled object graph remain subject to native runtime limits. All 27 final-head checks passed.

Refs #1331
2026-10-10 04:53:09 +08:00

30 lines
970 B
TypeScript

import type { Stats } from "node:fs";
import type { FileHandle } from "node:fs/promises";
import { openRegularFile } from "../filesystem/RegularFile.js";
/** Admit one regular-file handle, retain it through the consumer, and always close it. */
export const withRegularFile = async <Value>(
path: string,
read: (handle: FileHandle, stats: Stats) => Promise<Value>,
signal?: AbortSignal,
): Promise<Value> => {
const handle = await openRegularFile(path, { symlinks: "follow", signal });
try {
const stats = await handle.stat();
signal?.throwIfAborted();
const value = await read(handle, stats);
signal?.throwIfAborted();
return value;
} finally {
await handle.close();
}
};
/** Admit a regular file without waiting for a pipe, then read its verified handle. */
export const readRegularFile = (
path: string,
signal?: AbortSignal,
): Promise<Buffer> =>
withRegularFile(path, (handle) => handle.readFile({ signal }), signal);