fix(filesystem): reject unstable artifact targets

This commit is contained in:
morluto
2026-10-10 04:14:54 +08:00
parent 66a20a2597
commit 7829b1aeef
11 changed files with 359 additions and 58 deletions
+2 -28
View File
@@ -1,38 +1,12 @@
import { constants } from "node:fs";
import { open } from "node:fs/promises";
/** The opened input cannot supply regular-file bytes. */
export class NonRegularFileReadError extends Error {
readonly code: "EISDIR" | "ENOTFILE";
constructor(
readonly path: string,
directory: boolean,
) {
super(`Selected input must be a regular file: ${path}`);
this.name = "NonRegularFileReadError";
this.code = directory ? "EISDIR" : "ENOTFILE";
}
}
import { openRegularFile } from "../filesystem/RegularFile.js";
/** Admit a regular file without waiting for a pipe, then read its verified handle. */
export const readRegularFile = async (
path: string,
signal?: AbortSignal,
): Promise<Buffer> => {
signal?.throwIfAborted();
const handle = await open(
path,
constants.O_RDONLY |
(constants.O_NONBLOCK ?? 0) |
(constants.O_NOCTTY ?? 0),
);
const handle = await openRegularFile(path, { symlinks: "follow", signal });
try {
signal?.throwIfAborted();
const stats = await handle.stat();
signal?.throwIfAborted();
if (!stats.isFile())
throw new NonRegularFileReadError(path, stats.isDirectory());
const bytes = await handle.readFile({ signal });
signal?.throwIfAborted();
return bytes;