mirror of
https://github.com/morluto/rea.git
synced 2026-10-30 19:35:56 +00:00
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
30 lines
970 B
TypeScript
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);
|