mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(cli): add lists files from a received config's registry as received (#7820)
* fix(cli): add lists files from a received config's registry as received When a project's hyperframes.json is listed in .hyperframes/received-files.json and names a registry other than the public one, add now lists every file it is about to write in that record before writing any of them. This covers every caller of addToProject: add by name, by tag, several names, and Studio. The decision reads the config the install uses, so a byte-order mark or another spelling of the file on disk is handled the same way. A person's own config, the public registry and a record that cannot be read are left as they are. * fix(cli): list received files as they are written, under a shared lock add now lists each file in the received-files record just before it writes it, using the write-time decision, so a file kept because the person edited it during the download is never listed. When an install fails, it takes back the entries it listed but never wrote. Changes to the record take .hyperframes/received-files.json.lock, created exclusively. A writer retries for about 2 s, takes over a lock older than about 10 s, and always releases it. A writer that cannot take the lock in time fails with a clear error instead of writing past it. * fix(cli): an unreadable config counts as received; a failed install keeps what is on disk add now counts an install as received when hyperframes.json exists but cannot be read or parsed, since that config is not proven to be the person's own. When an install fails, it takes back only the entries it listed whose files are not on disk; files that landed stay listed. * fix(cli): the received-files lock resolves the project folder as the installer does The record's lock and write resolved the project folder with fs.realpathSync, while the containment check resolves targets with the native realpath. The two can spell the same folder differently: a Windows short name or a folder typed in another letter case. The target then looked outside the project and the change was refused. The record now resolves the folder with the installer's realProjectRoot, so the root and target compare in one spelling. The failed-install tests now fail the write by stubbing it, which works on every platform, instead of a read-only folder, which Windows ignores.
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
import { lstatSync, readFileSync, realpathSync, statSync } from "node:fs";
|
||||
import {
|
||||
closeSync,
|
||||
lstatSync,
|
||||
openSync,
|
||||
readFileSync,
|
||||
realpathSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
} from "node:fs";
|
||||
import { dirname, join, relative, resolve, sep } from "node:path";
|
||||
import {
|
||||
isEnvOrGitPath,
|
||||
@@ -6,6 +14,8 @@ import {
|
||||
linksOutside,
|
||||
} from "@hyperframes/studio-server/outside-file";
|
||||
import { isPrivateProjectFile, realFilePath } from "@hyperframes/studio-server/safe-path";
|
||||
import { mkdirWithinProject } from "@hyperframes/core";
|
||||
import { publishRegistryFile, registryRoot, registryTargetPath } from "../registry/publication.js";
|
||||
|
||||
export { isEnvOrGitPath, isPrivateOutsideFile, linksOutside };
|
||||
|
||||
@@ -14,6 +24,9 @@ export function linksToPrivateOutsideFile(projectDir: string, filePath: string):
|
||||
}
|
||||
|
||||
const RECEIVED_RECORD = join(".hyperframes", "received-files.json");
|
||||
const RECEIVED_RECORD_LOCK = `${RECEIVED_RECORD}.lock`;
|
||||
const LOCK_WAIT_MS = 2_000;
|
||||
const LOCK_STALE_MS = 10_000;
|
||||
|
||||
interface ReceivedRecord {
|
||||
root: string;
|
||||
@@ -39,6 +52,60 @@ function readRecord(dir: string, ownFolder: boolean): { listed: unknown } | null
|
||||
}
|
||||
}
|
||||
|
||||
function sleepSync(ms: number): void {
|
||||
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
||||
}
|
||||
|
||||
function takeRecordLock(lock: string): void {
|
||||
const deadline = Date.now() + LOCK_WAIT_MS;
|
||||
for (;;) {
|
||||
try {
|
||||
closeSync(openSync(lock, "wx"));
|
||||
return;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
||||
}
|
||||
const heldFor = Date.now() - (statSync(lock, { throwIfNoEntry: false })?.mtimeMs ?? Date.now());
|
||||
if (heldFor > LOCK_STALE_MS) rmSync(lock, { force: true });
|
||||
else if (Date.now() < deadline) sleepSync(25);
|
||||
else throw new Error(`Another program is changing ${RECEIVED_RECORD}; try again.`);
|
||||
}
|
||||
}
|
||||
|
||||
function changeReceivedRecord(projectDir: string, change: (files: Set<string>) => boolean): void {
|
||||
const root = registryRoot(projectDir);
|
||||
const lock = registryTargetPath(root, RECEIVED_RECORD_LOCK);
|
||||
mkdirWithinProject(root, dirname(lock));
|
||||
takeRecordLock(lock);
|
||||
try {
|
||||
const record = readRecord(root, true);
|
||||
if (record && !Array.isArray(record.listed)) return;
|
||||
const files = new Set(((record?.listed ?? []) as unknown[]).map(String));
|
||||
if (!change(files)) return;
|
||||
publishRegistryFile(root, RECEIVED_RECORD, `${JSON.stringify({ files: [...files].sort() })}\n`);
|
||||
} finally {
|
||||
rmSync(lock, { force: true });
|
||||
}
|
||||
}
|
||||
|
||||
export function recordReceivedFiles(root: string, paths: string[]): string[] {
|
||||
const added: string[] = [];
|
||||
if (paths.length === 0) return added;
|
||||
changeReceivedRecord(root, (files) => {
|
||||
for (const path of paths.map((one) => one.normalize("NFC"))) {
|
||||
if (!files.has(path)) added.push(path);
|
||||
files.add(path);
|
||||
}
|
||||
return added.length > 0;
|
||||
});
|
||||
return added;
|
||||
}
|
||||
|
||||
export function forgetReceivedFiles(root: string, paths: string[]): void {
|
||||
if (paths.length === 0) return;
|
||||
changeReceivedRecord(root, (files) => paths.map((path) => files.delete(path)).some(Boolean));
|
||||
}
|
||||
|
||||
function receivedRecordsAtOrAbove(projectDir: string): ReceivedRecord[] {
|
||||
let start: string;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user