fix(cli): the check cache is written only as a project record, never through a link (#7884)

This commit is contained in:
Miguel Ángel
2026-11-16 00:51:21 +00:00
committed by GitHub
parent f5a74a5678
commit 7801e3ff34
3 changed files with 97 additions and 44 deletions
+58
View File
@@ -0,0 +1,58 @@
import {
mkdirSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { readCachedPass, storePass } from "./checkCache.js";
import type { CheckReport } from "./checkTypes.js";
const PASS = { ok: true, findings: [] } as unknown as CheckReport;
const made: string[] = [];
const temp = (prefix: string) => {
const dir = mkdtempSync(join(tmpdir(), prefix));
made.push(dir);
return dir;
};
afterEach(() => {
for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true });
});
describe("the check cache", () => {
it("stores a pass and replays it under the same key", () => {
const project = temp("hf-check-cache-");
storePass(project, "key", PASS);
expect(readCachedPass(project, "key")?.ok).toBe(true);
expect(readCachedPass(project, "other")).toBeUndefined();
});
// A received project can carry a link at the record or at a folder above it.
it.skipIf(process.platform === "win32").each([
["check.json", [".hyperframes", "cache"], "check.json"],
["cache", [".hyperframes"], "cache"],
[".hyperframes", [], ".hyperframes"],
])("never writes through a link at %s", (_what, folders, linked) => {
const project = temp("hf-check-cache-");
const outside = temp("hf-check-outside-");
// The link's target holds what the write would land on, at each depth.
mkdirSync(join(outside, "cache"), { recursive: true });
for (const file of ["victim", join("cache", "check.json"), "check.json"])
writeFileSync(join(outside, file), "the person's file");
mkdirSync(join(project, ...folders), { recursive: true });
const target = linked === "check.json" ? join(outside, "victim") : outside;
symlinkSync(target, join(project, ...folders, linked));
const before = readdirSync(outside, { recursive: true }).sort();
storePass(project, "key", PASS);
for (const file of ["victim", join("cache", "check.json"), "check.json"])
expect(readFileSync(join(outside, file), "utf8")).toBe("the person's file");
expect(readdirSync(outside, { recursive: true }).sort()).toEqual(before);
});
});
+14 -24
View File
@@ -1,16 +1,9 @@
import { createHash } from "node:crypto";
import {
closeSync,
fstatSync,
mkdirSync,
openSync,
readFileSync,
readdirSync,
writeFileSync,
} from "node:fs";
import { closeSync, fstatSync, openSync, readFileSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { CACHE_DIR_NAME as PROXY_CACHE_DIR_NAME } from "@hyperframes/studio-server/proxy-transcoder";
import type { CheckOptions, CheckReport } from "./checkTypes.js";
import { readRecordTail, writeRecord } from "./projectRecords.js";
const PROJECT_STATE_DIR = ".hyperframes";
// What the CLI itself writes under .hyperframes; anything else there (a script the page loads) is project input.
@@ -29,9 +22,8 @@ const VERDICT_ENV = /^(HYPERFRAMES?|PRODUCER|HF)_/;
class Uncacheable extends Error {}
function cachePath(projectDir: string): string {
return join(projectDir, ".hyperframes", "cache", "check.json");
}
const CACHE_RECORD = "cache/check.json";
const CACHE_RECORD_MAX_BYTES = 16 * 1024 * 1024;
/** The browser binary a check would launch, by path and file identity; undefined when none is installed. */
export async function browserIdentity(): Promise<string | undefined> {
@@ -115,7 +107,9 @@ export function checkCacheable(options: CheckOptions): boolean {
export function readCachedPass(projectDir: string, key: string): CheckReport | undefined {
try {
const stored = JSON.parse(readFileSync(cachePath(projectDir), "utf8"));
const record = readRecordTail(projectDir, CACHE_RECORD, CACHE_RECORD_MAX_BYTES);
if (!record?.whole) return undefined;
const stored = JSON.parse(record.text);
if (stored.key !== key || !stored.report?.ok) return undefined;
return { ...stored.report, cached: { at: stored.at } };
} catch {
@@ -123,17 +117,13 @@ export function readCachedPass(projectDir: string, key: string): CheckReport | u
}
}
/** Only a clean pass that ran the browser is stored; a failure is always re-run. */
/** Only a clean pass that ran the browser is stored when it can be (else the next run re-checks); a failure re-runs. */
export function storePass(projectDir: string, key: string, report: CheckReport): void {
if (!report.ok || report.browserSkipped) return;
try {
mkdirSync(join(projectDir, ".hyperframes", "cache"), { recursive: true });
const { saved: _saved, ...clean } = report as CheckReport & { saved?: unknown };
writeFileSync(
cachePath(projectDir),
JSON.stringify({ key, at: new Date().toISOString(), report: clean }),
);
} catch {
// A cache that cannot be written only costs the next run its speed-up.
}
const { saved: _saved, ...clean } = report as CheckReport & { saved?: unknown };
writeRecord(
projectDir,
CACHE_RECORD,
JSON.stringify({ key, at: new Date().toISOString(), report: clean }),
);
}
+25 -20
View File
@@ -16,22 +16,27 @@ import { replaceFileAtomically } from "@hyperframes/core/atomic-file";
const NO_FOLLOW = constants.O_NOFOLLOW ?? 0;
const NON_BLOCK = constants.O_NONBLOCK ?? 0;
/** The folder, made when missing and asked for; null when something other than a real folder holds its name. */
function recordsDir(projectDir: string, create = false): string | null {
const dir = join(projectDir, ".hyperframes");
try {
return lstatSync(dir).isDirectory() ? dir : null;
} catch {
if (!create) return null;
/** A record's folder, each level made if asked; null when anything but a real folder holds a level's name. */
function recordsDir(projectDir: string, name: string, create = false): string | null {
let dir = projectDir;
for (const part of [".hyperframes", ...name.split("/").slice(0, -1)]) {
dir = join(dir, part);
try {
mkdirSync(dir);
return dir;
if (!lstatSync(dir).isDirectory()) return null;
} catch {
return null;
if (!create) return null;
try {
mkdirSync(dir);
} catch {
return null;
}
}
}
return dir;
}
const recordFile = (name: string) => name.split("/").at(-1) ?? name;
/** The last `maxBytes` of a plain file, and whether that is all of it; null for a link, a device, or a missing file.
* The file is opened first and read only when `lstat` then finds the same plain file at the path, so this holds
* where O_NOFOLLOW doesn't exist (Windows), and nothing path-based follows the check. */
@@ -67,8 +72,8 @@ export function readRecordTail(
name: string,
maxBytes: number,
): { text: string; whole: boolean } | null {
const dir = recordsDir(projectDir);
return dir ? readPlainTail(join(dir, name), maxBytes) : null;
const dir = recordsDir(projectDir, name);
return dir ? readPlainTail(join(dir, recordFile(name)), maxBytes) : null;
}
export function readRecord(projectDir: string, name: string, maxBytes: number): string {
@@ -77,9 +82,9 @@ export function readRecord(projectDir: string, name: string, maxBytes: number):
/** When a record last changed; 0 when it is missing or not a plain file. */
export function recordChangedAt(projectDir: string, name: string): number {
const dir = recordsDir(projectDir);
const dir = recordsDir(projectDir, name);
try {
const stats = dir ? lstatSync(join(dir, name)) : null;
const stats = dir ? lstatSync(join(dir, recordFile(name))) : null;
return stats?.isFile() ? stats.mtimeMs : 0;
} catch {
return 0;
@@ -97,18 +102,18 @@ export function replaceFile(path: string, data: string, mode: number): boolean {
}
}
/** A record written whole and owner-only, in a real .hyperframes folder. */
/** A record written whole and owner-only, in real folders all the way from .hyperframes. */
export function writeRecord(projectDir: string, name: string, data: string): boolean {
const dir = recordsDir(projectDir, true);
return dir !== null && replaceFile(join(dir, name), data, 0o600);
const dir = recordsDir(projectDir, name, true);
return dir !== null && replaceFile(join(dir, recordFile(name)), data, 0o600);
}
/** Deletes a record that is a plain file; false when there is none to delete. */
export function removeRecord(projectDir: string, name: string): boolean {
const dir = recordsDir(projectDir);
const dir = recordsDir(projectDir, name);
try {
if (!dir || !lstatSync(join(dir, name)).isFile()) return false;
unlinkSync(join(dir, name));
if (!dir || !lstatSync(join(dir, recordFile(name))).isFile()) return false;
unlinkSync(join(dir, recordFile(name)));
return true;
} catch {
return false;