mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 06:25:44 +00:00
* perf(javascript): avoid redundant semantic graph validation and node hashing * perf(resources): deduplicate directory containment checks and root lstat calls * perf(ci): trace script fixture modules, sparse-checkout gate jobs, and parallelize built doc checks * perf(tests): memoize live MCP doctor expectations and reuse CLI and verifier compilation * fix(ci): enforce containment after skipped entries, validate interned contexts, and supervise doc and verifier teardowns * fix(docs): decode buffered check output once to preserve multi-byte UTF-8
140 lines
4.1 KiB
TypeScript
140 lines
4.1 KiB
TypeScript
import type { BigIntStats } from "node:fs";
|
|
import { constants } from "node:fs";
|
|
import { lstat, realpath } from "node:fs/promises";
|
|
|
|
import { err, ok } from "../domain/result.js";
|
|
import {
|
|
cancelled,
|
|
failure,
|
|
filesystemFailureDetail,
|
|
} from "./ReferenceSourceReaderErrors.js";
|
|
import { isPathWithinRoot } from "../domain/localPath.js";
|
|
import type {
|
|
ReferenceSourceFailureCode,
|
|
ReferenceSourceResult,
|
|
} from "./ReferenceSourceReaderTypes.js";
|
|
import { rootFilesystemFailure } from "./ReferenceSourceFailureBuilders.js";
|
|
|
|
export const bigLstat = (path: string): Promise<BigIntStats> =>
|
|
lstat(path, { bigint: true });
|
|
|
|
export const sameFile = (left: BigIntStats, right: BigIntStats): boolean =>
|
|
left.dev === right.dev &&
|
|
left.ino === right.ino &&
|
|
left.size === right.size &&
|
|
left.mtimeNs === right.mtimeNs &&
|
|
left.ctimeNs === right.ctimeNs;
|
|
|
|
export const isAborted = (signal?: AbortSignal): boolean =>
|
|
signal?.aborted === true;
|
|
|
|
export const noFollowOpenSupported = (): boolean =>
|
|
Number.isSafeInteger(constants.O_NOFOLLOW) && constants.O_NOFOLLOW !== 0;
|
|
|
|
/** A directory's identity admission: its metadata, or why it was refused. */
|
|
export type DirectoryValidation =
|
|
| { readonly ok: true; readonly stats: BigIntStats }
|
|
| {
|
|
readonly ok: false;
|
|
readonly code: ReferenceSourceFailureCode;
|
|
readonly message: string;
|
|
readonly localDiagnostic?: string;
|
|
};
|
|
|
|
export const validateDirectory = async (
|
|
root: string,
|
|
rootIdentity: BigIntStats,
|
|
path: string,
|
|
signal?: AbortSignal,
|
|
): Promise<DirectoryValidation> => {
|
|
if (isAborted(signal))
|
|
return {
|
|
ok: false,
|
|
code: "cancelled",
|
|
message: "Reference source traversal cancelled",
|
|
};
|
|
try {
|
|
const rootNow = await bigLstat(root);
|
|
if (!sameFile(rootIdentity, rootNow))
|
|
return { ok: false, code: "changed", message: "Reference root changed" };
|
|
const stats = path === root ? rootNow : await bigLstat(path);
|
|
if (stats.isSymbolicLink())
|
|
return {
|
|
ok: false,
|
|
code: "symlink",
|
|
message: "Symbolic links are not followed",
|
|
};
|
|
if (!stats.isDirectory())
|
|
return {
|
|
ok: false,
|
|
code: "changed",
|
|
message: "Directory identity changed",
|
|
};
|
|
const canonical = await realpath(path);
|
|
if (canonical !== path || !isPathWithinRoot(root, canonical))
|
|
return {
|
|
ok: false,
|
|
code: "changed",
|
|
message: "Directory escaped the reference root",
|
|
};
|
|
return { ok: true, stats };
|
|
} catch (cause: unknown) {
|
|
const detail = filesystemFailureDetail(
|
|
cause,
|
|
"Directory identity could not be verified",
|
|
);
|
|
if (detail === undefined) throw cause;
|
|
return {
|
|
ok: false,
|
|
code: "io",
|
|
...detail,
|
|
};
|
|
}
|
|
};
|
|
|
|
export const prepareRoot = async (
|
|
root: string,
|
|
signal?: AbortSignal,
|
|
): Promise<
|
|
ReferenceSourceResult<{
|
|
readonly canonicalRoot: string;
|
|
readonly rootIdentity: BigIntStats;
|
|
}>
|
|
> => {
|
|
if (isAborted(signal)) return err(cancelled(signal));
|
|
try {
|
|
const metadata = await bigLstat(root);
|
|
if (metadata.isSymbolicLink() || !metadata.isDirectory())
|
|
return err(
|
|
failure(
|
|
"invalid-root",
|
|
`Reference source root is not a directory: ${root}`,
|
|
),
|
|
);
|
|
const canonicalRoot = await realpath(root);
|
|
if (isAborted(signal)) return err(cancelled(signal));
|
|
const canonicalMetadata = await bigLstat(canonicalRoot);
|
|
if (!sameFile(metadata, canonicalMetadata))
|
|
return err(
|
|
failure(
|
|
"invalid-root",
|
|
`Reference source root changed during resolution: ${root}`,
|
|
),
|
|
);
|
|
return ok({ canonicalRoot, rootIdentity: canonicalMetadata });
|
|
} catch (cause: unknown) {
|
|
const rootFailure = rootFilesystemFailure(
|
|
cause,
|
|
"Reference source root could not be resolved",
|
|
);
|
|
if (rootFailure === undefined) throw cause;
|
|
return err({
|
|
...failure(rootFailure.code, `${rootFailure.message}: ${root}`),
|
|
...(rootFailure.remediationAction === undefined
|
|
? {}
|
|
: { remediationAction: rootFailure.remediationAction }),
|
|
cause,
|
|
});
|
|
}
|
|
};
|