mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
fix(media-use): never write recipe files through links (#8257)
* fix(media-use): never write recipe files through links A project folder can carry symbolic links inside .media/recipes/<name>/. When recipe.json there did not parse, freezing a recipe under that name reused the folder and wrote each recipe file through the link, which could create or overwrite files outside the project. The user-tier copy then carried those links into ~/.media/recipes, and using the recipe copied them into other projects. Freezing and using a recipe now refuse a link at .media, .media/recipes or the recipe folder before anything moves or is written. Each recipe file is written as a new file and renamed into place, so a link at its name is replaced, never followed. Only the files a freeze just wrote reach the user tier, and using a recipe imports only its regular recipe files. Using a recipe also refuses a recipe whose frame.md is a link, and replaces a frame.md link in the project instead of writing through it. * fix(media-use): read user-tier recipe files through one no-follow handle Checking a file with lstat and then reading it by path leaves a gap in which the file can be swapped for a link. Importing a recipe from the user tier and copying its frame.md now open each file once with O_NOFOLLOW, and check that the open handle is a regular file before reading it. * fix(media-use): refuse recipe file links where Node has no O_NOFOLLOW Node has no O_NOFOLLOW on Windows, so opening a recipe file there followed a link and read its target. readRegularFile now checks the path with lstat first and refuses anything but a regular file. After the open, the handle must be a regular file with the same device and inode that lstat saw, so a link swapped in between is refused on every platform. O_NOFOLLOW stays as an extra guard where it exists, and O_NONBLOCK keeps a FIFO from blocking the open. A test passes 0 as the no-follow flag to check the Windows path on any platform, and another checks that a FIFO is refused without blocking. * refactor(media-use): compare the path with the handle after opening readRegularFile now opens first, then requires that lstat of the path shows a regular file with the same device and inode as the handle. A link has its own inode, so it never matches the file an open reached through it, with or without O_NOFOLLOW. O_NONBLOCK keeps a FIFO from blocking the open. Checking after the open, instead of before it, leaves no check-then-open gap on the path.
This commit is contained in:
@@ -89,7 +89,8 @@ export function ownMediaDir(projectDir) {
|
||||
return dir;
|
||||
}
|
||||
|
||||
function refuseLink(path) {
|
||||
/** Throws when `path` is a link. */
|
||||
export function refuseLink(path) {
|
||||
if (lstatSync(path, { throwIfNoEntry: false })?.isSymbolicLink()) {
|
||||
throw new Error(`${path} is a link; nothing was written through it`);
|
||||
}
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
import {
|
||||
cpSync,
|
||||
closeSync,
|
||||
constants,
|
||||
existsSync,
|
||||
fstatSync,
|
||||
lstatSync,
|
||||
mkdirSync,
|
||||
openSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { basename, join, resolve } from "node:path";
|
||||
import { globalMediaDir } from "./media-home.mjs";
|
||||
import { appendRecord, mediaDir, nextId } from "./manifest.mjs";
|
||||
import { appendRecord, mediaDir, nextId, refuseLink, replaceFile } from "./manifest.mjs";
|
||||
import { regenerateIndex } from "./index-gen.mjs";
|
||||
import { mergedPreferences } from "./prefs-store.mjs";
|
||||
|
||||
// A filter keeps Node 22 off its native copy, which writes nothing under a non-ASCII path on Windows.
|
||||
const NON_ASCII_SAFE_DIR_COPY = { recursive: true, force: true, filter: () => true };
|
||||
/** The files a recipe folder holds. Nothing else in a recipe folder is copied between tiers. */
|
||||
const RECIPE_FILES = ["recipe.json", "frame.md", "storyboard-skeleton.md", "brief-skeleton.md"];
|
||||
|
||||
/**
|
||||
* Recipes — the heavyweight tier of HyperFrames user memory.
|
||||
@@ -212,6 +215,56 @@ function prefValue(prefs, key) {
|
||||
return prefs[key]?.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws, before anything is moved or written, when `.media`, `.media/recipes` or the recipe's own
|
||||
* folder is a link. A project from someone else could carry such a link to send recipe writes
|
||||
* outside the project.
|
||||
*/
|
||||
function refuseLinkedRecipeDir(root, slug) {
|
||||
for (const path of [
|
||||
mediaDir(root),
|
||||
projectRecipesDir(root),
|
||||
join(projectRecipesDir(root), slug),
|
||||
]) {
|
||||
refuseLink(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The content of the regular file at `path`, or undefined when it is missing, a link or not a file.
|
||||
* After the open, `path` must still be a regular file with the device and inode the handle has. A
|
||||
* link has its own inode, so it never matches the file an open reached through it, even where Node
|
||||
* has no `O_NOFOLLOW` (Windows); pass 0 as `noFollow` to check that path. `O_NONBLOCK` keeps a FIFO
|
||||
* from blocking the open.
|
||||
*/
|
||||
export function readRegularFile(path, noFollow = constants.O_NOFOLLOW ?? 0) {
|
||||
let fd;
|
||||
try {
|
||||
fd = openSync(path, constants.O_RDONLY | noFollow | (constants.O_NONBLOCK ?? 0));
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
const opened = fstatSync(fd, { bigint: true });
|
||||
const atPath = lstatSync(path, { bigint: true, throwIfNoEntry: false });
|
||||
const same =
|
||||
opened.isFile() && atPath?.isFile() && opened.dev === atPath.dev && opened.ino === atPath.ino;
|
||||
return same ? readFileSync(fd) : undefined;
|
||||
} finally {
|
||||
closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes each named file into `dir` as a new regular file, replacing a link at that name instead
|
||||
* of following it, and removes a leftover brief skeleton the bundle does not have.
|
||||
*/
|
||||
function writeRecipeFiles(dir, files) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
for (const [name, content] of Object.entries(files)) replaceFile(join(dir, name), content);
|
||||
if (!("brief-skeleton.md" in files)) rmSync(join(dir, "brief-skeleton.md"), { force: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Freeze the current project's approved run as a named recipe. Writes the
|
||||
* project-tier folder + a manifest record, then copies to the user tier (a
|
||||
@@ -232,6 +285,7 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) {
|
||||
if (!existsSync(frameSpec)) throw new Error("no frame.md to freeze — run the design step first");
|
||||
if (!existsSync(storyboard)) throw new Error("no STORYBOARD.md to freeze");
|
||||
|
||||
refuseLinkedRecipeDir(root, slug);
|
||||
const dir = join(projectRecipesDir(root), slug);
|
||||
let version = 1;
|
||||
const previous = existsSync(dir) ? readRecipeJson(dir) : null;
|
||||
@@ -252,7 +306,6 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) {
|
||||
}
|
||||
renameSync(dir, `${dir}@v${slot}`);
|
||||
}
|
||||
mkdirSync(dir, { recursive: true });
|
||||
|
||||
const prefs = mergedPreferences(root);
|
||||
const recipe = {
|
||||
@@ -272,23 +325,19 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) {
|
||||
blocks: Array.isArray(blocks) && blocks.length > 0 ? blocks : undefined,
|
||||
};
|
||||
|
||||
writeFileSync(join(dir, "recipe.json"), `${JSON.stringify(recipe, null, 2)}\n`);
|
||||
cpSync(frameSpec, join(dir, "frame.md"));
|
||||
writeFileSync(
|
||||
join(dir, "storyboard-skeleton.md"),
|
||||
`${skeletonizeStoryboard(readFileSync(storyboard, "utf8")).trimEnd()}\n`,
|
||||
);
|
||||
|
||||
const files = {
|
||||
"recipe.json": `${JSON.stringify(recipe, null, 2)}\n`,
|
||||
"frame.md": readFileSync(frameSpec),
|
||||
"storyboard-skeleton.md": `${skeletonizeStoryboard(readFileSync(storyboard, "utf8")).trimEnd()}\n`,
|
||||
};
|
||||
// Best-effort fourth artifact — projects briefed before BRIEF.md existed
|
||||
// (or by workflows that don't write one) freeze fine without it.
|
||||
const brief = join(root, "BRIEF.md");
|
||||
const briefSkeleton = existsSync(brief);
|
||||
if (briefSkeleton) {
|
||||
writeFileSync(
|
||||
join(dir, "brief-skeleton.md"),
|
||||
`${skeletonizeBrief(readFileSync(brief, "utf8")).trimEnd()}\n`,
|
||||
);
|
||||
files["brief-skeleton.md"] = `${skeletonizeBrief(readFileSync(brief, "utf8")).trimEnd()}\n`;
|
||||
}
|
||||
writeRecipeFiles(dir, files);
|
||||
|
||||
const id = nextId(root, "recipe");
|
||||
appendRecord(root, {
|
||||
@@ -301,12 +350,9 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) {
|
||||
});
|
||||
regenerateIndex(root);
|
||||
|
||||
// User tier — best-effort, like every other promotion.
|
||||
// User tier — best-effort, like every other promotion. Only the files this freeze wrote go there.
|
||||
try {
|
||||
const userDir = join(userRecipesDir(), slug);
|
||||
mkdirSync(userDir, { recursive: true });
|
||||
cpSync(dir, userDir, NON_ASCII_SAFE_DIR_COPY);
|
||||
if (!briefSkeleton) rmSync(join(userDir, "brief-skeleton.md"), { force: true });
|
||||
writeRecipeFiles(join(userRecipesDir(), slug), files);
|
||||
} catch {
|
||||
// The project-tier freeze already landed.
|
||||
}
|
||||
@@ -355,7 +401,8 @@ export function listRecipes({ projectDir, workflow }) {
|
||||
export function useRecipe({ projectDir, name }) {
|
||||
const slug = slugifyRecipeName(name);
|
||||
const root = resolve(projectDir);
|
||||
let dir = join(projectRecipesDir(root), slug);
|
||||
refuseLinkedRecipeDir(root, slug);
|
||||
const dir = join(projectRecipesDir(root), slug);
|
||||
|
||||
if (!readRecipeJson(dir)) {
|
||||
const userDir = join(userRecipesDir(), slug);
|
||||
@@ -365,8 +412,18 @@ export function useRecipe({ projectDir, name }) {
|
||||
`no recipe named "${slug}"${known.length ? ` (known: ${known.join(", ")})` : ""}`,
|
||||
);
|
||||
}
|
||||
mkdirSync(dir, { recursive: true });
|
||||
cpSync(userDir, dir, NON_ASCII_SAFE_DIR_COPY);
|
||||
// Only regular recipe files come over; a link or anything else in the user-tier folder stays there.
|
||||
const files = {};
|
||||
for (const file of RECIPE_FILES) {
|
||||
const content = readRegularFile(join(userDir, file));
|
||||
if (content !== undefined) files[file] = content;
|
||||
}
|
||||
if (!files["recipe.json"]) {
|
||||
throw new Error(
|
||||
`${join(userDir, "recipe.json")} is not a regular file; nothing was imported`,
|
||||
);
|
||||
}
|
||||
writeRecipeFiles(dir, files);
|
||||
const imported = readRecipeJson(dir);
|
||||
appendRecord(root, {
|
||||
id: nextId(root, "recipe"),
|
||||
@@ -380,7 +437,12 @@ export function useRecipe({ projectDir, name }) {
|
||||
}
|
||||
|
||||
const recipe = readRecipeJson(dir);
|
||||
cpSync(join(dir, "frame.md"), join(root, "frame.md"));
|
||||
const frameSpec = join(dir, "frame.md");
|
||||
const frame = readRegularFile(frameSpec);
|
||||
if (frame === undefined) {
|
||||
throw new Error(`${frameSpec} is missing, a link or not a regular file; nothing was read`);
|
||||
}
|
||||
replaceFile(join(root, "frame.md"), frame);
|
||||
return {
|
||||
recipe,
|
||||
dir,
|
||||
|
||||
@@ -2,18 +2,22 @@ import { strict as assert } from "node:assert";
|
||||
import { test } from "node:test";
|
||||
import {
|
||||
existsSync,
|
||||
lstatSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
symlinkSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
freezeRecipe,
|
||||
listRecipes,
|
||||
readRegularFile,
|
||||
skeletonizeBrief,
|
||||
skeletonizeStoryboard,
|
||||
slugifyRecipeName,
|
||||
@@ -487,3 +491,156 @@ test("use with an unknown name lists what exists", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
const RECIPE_FILE_NAMES = [
|
||||
"recipe.json",
|
||||
"frame.md",
|
||||
"storyboard-skeleton.md",
|
||||
"brief-skeleton.md",
|
||||
];
|
||||
|
||||
/** A file outside the project with known content, and a link to it under each recipe file name. */
|
||||
function plantRecipeLinks(root, dir) {
|
||||
const target = join(root, "outside.txt");
|
||||
writeFileSync(target, "keep me\n");
|
||||
mkdirSync(dir, { recursive: true });
|
||||
for (const name of RECIPE_FILE_NAMES) symlinkSync(target, join(dir, name));
|
||||
return target;
|
||||
}
|
||||
|
||||
for (const recipeJson of ["missing", "unparseable"]) {
|
||||
test(`freeze replaces links in a recipe folder whose recipe.json is ${recipeJson}`, () => {
|
||||
withSandbox(({ project, root, home }) => {
|
||||
writeFileSync(join(project, "BRIEF.md"), BRIEF);
|
||||
const dir = join(project, ".media/recipes/promo");
|
||||
const target = plantRecipeLinks(root, dir);
|
||||
if (recipeJson === "missing") rmSync(join(dir, "recipe.json"));
|
||||
writeFileSync(join(dir, "leftover.sh"), "echo planted\n");
|
||||
|
||||
const frozen = freezeRecipe({ projectDir: project, name: "promo" });
|
||||
assert.equal(frozen.version, 1);
|
||||
assert.equal(readFileSync(target, "utf8"), "keep me\n");
|
||||
for (const name of RECIPE_FILE_NAMES) {
|
||||
assert.ok(lstatSync(join(dir, name)).isFile(), `${name} is a regular file`);
|
||||
assert.ok(lstatSync(join(home, ".media/recipes/promo", name)).isFile());
|
||||
}
|
||||
assert.equal(JSON.parse(readFileSync(join(dir, "recipe.json"), "utf8")).name, "promo");
|
||||
assert.deepEqual(
|
||||
readdirSync(join(home, ".media/recipes/promo")).sort(),
|
||||
[...RECIPE_FILE_NAMES].sort(),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("freeze without a BRIEF.md removes a planted brief-skeleton link instead of keeping it", () => {
|
||||
withSandbox(({ project, root, home }) => {
|
||||
const dir = join(project, ".media/recipes/promo");
|
||||
const target = plantRecipeLinks(root, dir);
|
||||
freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" });
|
||||
assert.equal(readFileSync(target, "utf8"), "keep me\n");
|
||||
assert.equal(lstatSync(join(dir, "brief-skeleton.md"), { throwIfNoEntry: false }), undefined);
|
||||
assert.ok(!existsSync(join(home, ".media/recipes/promo/brief-skeleton.md")));
|
||||
});
|
||||
});
|
||||
|
||||
for (const linked of [".media", ".media/recipes", ".media/recipes/promo"]) {
|
||||
test(`freeze refuses, with nothing written, when ${linked} is a link`, () => {
|
||||
withSandbox(({ project, root, home }) => {
|
||||
const outside = join(root, "outside-dir");
|
||||
mkdirSync(outside);
|
||||
const parent = join(project, linked, "..");
|
||||
mkdirSync(parent, { recursive: true });
|
||||
symlinkSync(outside, join(project, linked));
|
||||
assert.throws(
|
||||
() =>
|
||||
freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" }),
|
||||
/is a link; nothing was written through it/,
|
||||
);
|
||||
assert.deepEqual(readdirSync(outside), []);
|
||||
assert.ok(!existsSync(join(home, ".media/recipes/promo")));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("use imports only regular recipe files from the user tier, never links or leftovers", () => {
|
||||
withSandbox(({ project, root, home }) => {
|
||||
freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" });
|
||||
const userDir = join(home, ".media/recipes/promo");
|
||||
const target = join(root, "secret.txt");
|
||||
writeFileSync(target, "secret\n");
|
||||
symlinkSync(target, join(userDir, "brief-skeleton.md"));
|
||||
writeFileSync(join(userDir, "leftover.sh"), "echo planted\n");
|
||||
|
||||
const fresh = join(root, "fresh-project");
|
||||
mkdirSync(fresh, { recursive: true });
|
||||
const used = useRecipe({ projectDir: fresh, name: "promo" });
|
||||
assert.equal(used.briefSkeletonPath, undefined);
|
||||
assert.deepEqual(readdirSync(join(fresh, ".media/recipes/promo")).sort(), [
|
||||
"frame.md",
|
||||
"recipe.json",
|
||||
"storyboard-skeleton.md",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
test("use replaces a frame.md link in the project instead of writing through it", () => {
|
||||
withSandbox(({ project, root }) => {
|
||||
freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" });
|
||||
const fresh = join(root, "fresh-project");
|
||||
mkdirSync(fresh, { recursive: true });
|
||||
const target = join(root, "outside.txt");
|
||||
writeFileSync(target, "keep me\n");
|
||||
symlinkSync(target, join(fresh, "frame.md"));
|
||||
|
||||
useRecipe({ projectDir: fresh, name: "promo" });
|
||||
assert.equal(readFileSync(target, "utf8"), "keep me\n");
|
||||
assert.ok(lstatSync(join(fresh, "frame.md")).isFile());
|
||||
assert.match(readFileSync(join(fresh, "frame.md"), "utf8"), /background: #101014/);
|
||||
});
|
||||
});
|
||||
|
||||
test("use refuses a recipe whose frame.md is a link, so no file is read through it", () => {
|
||||
withSandbox(({ project, root }) => {
|
||||
const dir = join(project, ".media/recipes/promo");
|
||||
mkdirSync(dir, { recursive: true });
|
||||
writeFileSync(join(dir, "recipe.json"), JSON.stringify({ name: "promo", version: 1 }));
|
||||
const target = join(root, "secret.txt");
|
||||
writeFileSync(target, "secret\n");
|
||||
symlinkSync(target, join(dir, "frame.md"));
|
||||
|
||||
assert.throws(
|
||||
() => useRecipe({ projectDir: project, name: "promo" }),
|
||||
/a link or not a regular file/,
|
||||
);
|
||||
assert.equal(
|
||||
readFileSync(join(project, "frame.md"), "utf8"),
|
||||
"# Frame spec\nbackground: #101014\n",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test("readRegularFile refuses a link even without O_NOFOLLOW, as on Windows", () => {
|
||||
withSandbox(({ root }) => {
|
||||
const target = join(root, "secret.txt");
|
||||
writeFileSync(target, "secret\n");
|
||||
const link = join(root, "frame.md");
|
||||
symlinkSync(target, link);
|
||||
assert.equal(readRegularFile(link, 0), undefined);
|
||||
assert.equal(readRegularFile(link), undefined);
|
||||
assert.equal(readRegularFile(target, 0).toString(), "secret\n");
|
||||
assert.equal(readRegularFile(join(root, "missing.md"), 0), undefined);
|
||||
});
|
||||
});
|
||||
|
||||
test(
|
||||
"readRegularFile refuses a FIFO without blocking on it",
|
||||
{ skip: process.platform === "win32" },
|
||||
() => {
|
||||
withSandbox(({ root }) => {
|
||||
const fifo = join(root, "frame.md");
|
||||
execFileSync("mkfifo", [fifo]);
|
||||
assert.equal(readRegularFile(fifo), undefined);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
"files": 13
|
||||
},
|
||||
"media-use": {
|
||||
"hash": "d979128f61f6ab95",
|
||||
"hash": "23ee5ebbc42b963a",
|
||||
"files": 131
|
||||
},
|
||||
"remotion-to-hyperframes": {
|
||||
|
||||
@@ -89,7 +89,8 @@ export function ownMediaDir(projectDir) {
|
||||
return dir;
|
||||
}
|
||||
|
||||
function refuseLink(path) {
|
||||
/** Throws when `path` is a link. */
|
||||
export function refuseLink(path) {
|
||||
if (lstatSync(path, { throwIfNoEntry: false })?.isSymbolicLink()) {
|
||||
throw new Error(`${path} is a link; nothing was written through it`);
|
||||
}
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
import {
|
||||
cpSync,
|
||||
closeSync,
|
||||
constants,
|
||||
existsSync,
|
||||
fstatSync,
|
||||
lstatSync,
|
||||
mkdirSync,
|
||||
openSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { basename, join, resolve } from "node:path";
|
||||
import { globalMediaDir } from "./media-home.mjs";
|
||||
import { appendRecord, mediaDir, nextId } from "./manifest.mjs";
|
||||
import { appendRecord, mediaDir, nextId, refuseLink, replaceFile } from "./manifest.mjs";
|
||||
import { regenerateIndex } from "./index-gen.mjs";
|
||||
import { mergedPreferences } from "./prefs-store.mjs";
|
||||
|
||||
// A filter keeps Node 22 off its native copy, which writes nothing under a non-ASCII path on Windows.
|
||||
const NON_ASCII_SAFE_DIR_COPY = { recursive: true, force: true, filter: () => true };
|
||||
/** The files a recipe folder holds. Nothing else in a recipe folder is copied between tiers. */
|
||||
const RECIPE_FILES = ["recipe.json", "frame.md", "storyboard-skeleton.md", "brief-skeleton.md"];
|
||||
|
||||
/**
|
||||
* Recipes — the heavyweight tier of HyperFrames user memory.
|
||||
@@ -212,6 +215,56 @@ function prefValue(prefs, key) {
|
||||
return prefs[key]?.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws, before anything is moved or written, when `.media`, `.media/recipes` or the recipe's own
|
||||
* folder is a link. A project from someone else could carry such a link to send recipe writes
|
||||
* outside the project.
|
||||
*/
|
||||
function refuseLinkedRecipeDir(root, slug) {
|
||||
for (const path of [
|
||||
mediaDir(root),
|
||||
projectRecipesDir(root),
|
||||
join(projectRecipesDir(root), slug),
|
||||
]) {
|
||||
refuseLink(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The content of the regular file at `path`, or undefined when it is missing, a link or not a file.
|
||||
* After the open, `path` must still be a regular file with the device and inode the handle has. A
|
||||
* link has its own inode, so it never matches the file an open reached through it, even where Node
|
||||
* has no `O_NOFOLLOW` (Windows); pass 0 as `noFollow` to check that path. `O_NONBLOCK` keeps a FIFO
|
||||
* from blocking the open.
|
||||
*/
|
||||
export function readRegularFile(path, noFollow = constants.O_NOFOLLOW ?? 0) {
|
||||
let fd;
|
||||
try {
|
||||
fd = openSync(path, constants.O_RDONLY | noFollow | (constants.O_NONBLOCK ?? 0));
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
const opened = fstatSync(fd, { bigint: true });
|
||||
const atPath = lstatSync(path, { bigint: true, throwIfNoEntry: false });
|
||||
const same =
|
||||
opened.isFile() && atPath?.isFile() && opened.dev === atPath.dev && opened.ino === atPath.ino;
|
||||
return same ? readFileSync(fd) : undefined;
|
||||
} finally {
|
||||
closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes each named file into `dir` as a new regular file, replacing a link at that name instead
|
||||
* of following it, and removes a leftover brief skeleton the bundle does not have.
|
||||
*/
|
||||
function writeRecipeFiles(dir, files) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
for (const [name, content] of Object.entries(files)) replaceFile(join(dir, name), content);
|
||||
if (!("brief-skeleton.md" in files)) rmSync(join(dir, "brief-skeleton.md"), { force: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Freeze the current project's approved run as a named recipe. Writes the
|
||||
* project-tier folder + a manifest record, then copies to the user tier (a
|
||||
@@ -232,6 +285,7 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) {
|
||||
if (!existsSync(frameSpec)) throw new Error("no frame.md to freeze — run the design step first");
|
||||
if (!existsSync(storyboard)) throw new Error("no STORYBOARD.md to freeze");
|
||||
|
||||
refuseLinkedRecipeDir(root, slug);
|
||||
const dir = join(projectRecipesDir(root), slug);
|
||||
let version = 1;
|
||||
const previous = existsSync(dir) ? readRecipeJson(dir) : null;
|
||||
@@ -252,7 +306,6 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) {
|
||||
}
|
||||
renameSync(dir, `${dir}@v${slot}`);
|
||||
}
|
||||
mkdirSync(dir, { recursive: true });
|
||||
|
||||
const prefs = mergedPreferences(root);
|
||||
const recipe = {
|
||||
@@ -272,23 +325,19 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) {
|
||||
blocks: Array.isArray(blocks) && blocks.length > 0 ? blocks : undefined,
|
||||
};
|
||||
|
||||
writeFileSync(join(dir, "recipe.json"), `${JSON.stringify(recipe, null, 2)}\n`);
|
||||
cpSync(frameSpec, join(dir, "frame.md"));
|
||||
writeFileSync(
|
||||
join(dir, "storyboard-skeleton.md"),
|
||||
`${skeletonizeStoryboard(readFileSync(storyboard, "utf8")).trimEnd()}\n`,
|
||||
);
|
||||
|
||||
const files = {
|
||||
"recipe.json": `${JSON.stringify(recipe, null, 2)}\n`,
|
||||
"frame.md": readFileSync(frameSpec),
|
||||
"storyboard-skeleton.md": `${skeletonizeStoryboard(readFileSync(storyboard, "utf8")).trimEnd()}\n`,
|
||||
};
|
||||
// Best-effort fourth artifact — projects briefed before BRIEF.md existed
|
||||
// (or by workflows that don't write one) freeze fine without it.
|
||||
const brief = join(root, "BRIEF.md");
|
||||
const briefSkeleton = existsSync(brief);
|
||||
if (briefSkeleton) {
|
||||
writeFileSync(
|
||||
join(dir, "brief-skeleton.md"),
|
||||
`${skeletonizeBrief(readFileSync(brief, "utf8")).trimEnd()}\n`,
|
||||
);
|
||||
files["brief-skeleton.md"] = `${skeletonizeBrief(readFileSync(brief, "utf8")).trimEnd()}\n`;
|
||||
}
|
||||
writeRecipeFiles(dir, files);
|
||||
|
||||
const id = nextId(root, "recipe");
|
||||
appendRecord(root, {
|
||||
@@ -301,12 +350,9 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) {
|
||||
});
|
||||
regenerateIndex(root);
|
||||
|
||||
// User tier — best-effort, like every other promotion.
|
||||
// User tier — best-effort, like every other promotion. Only the files this freeze wrote go there.
|
||||
try {
|
||||
const userDir = join(userRecipesDir(), slug);
|
||||
mkdirSync(userDir, { recursive: true });
|
||||
cpSync(dir, userDir, NON_ASCII_SAFE_DIR_COPY);
|
||||
if (!briefSkeleton) rmSync(join(userDir, "brief-skeleton.md"), { force: true });
|
||||
writeRecipeFiles(join(userRecipesDir(), slug), files);
|
||||
} catch {
|
||||
// The project-tier freeze already landed.
|
||||
}
|
||||
@@ -355,7 +401,8 @@ export function listRecipes({ projectDir, workflow }) {
|
||||
export function useRecipe({ projectDir, name }) {
|
||||
const slug = slugifyRecipeName(name);
|
||||
const root = resolve(projectDir);
|
||||
let dir = join(projectRecipesDir(root), slug);
|
||||
refuseLinkedRecipeDir(root, slug);
|
||||
const dir = join(projectRecipesDir(root), slug);
|
||||
|
||||
if (!readRecipeJson(dir)) {
|
||||
const userDir = join(userRecipesDir(), slug);
|
||||
@@ -365,8 +412,18 @@ export function useRecipe({ projectDir, name }) {
|
||||
`no recipe named "${slug}"${known.length ? ` (known: ${known.join(", ")})` : ""}`,
|
||||
);
|
||||
}
|
||||
mkdirSync(dir, { recursive: true });
|
||||
cpSync(userDir, dir, NON_ASCII_SAFE_DIR_COPY);
|
||||
// Only regular recipe files come over; a link or anything else in the user-tier folder stays there.
|
||||
const files = {};
|
||||
for (const file of RECIPE_FILES) {
|
||||
const content = readRegularFile(join(userDir, file));
|
||||
if (content !== undefined) files[file] = content;
|
||||
}
|
||||
if (!files["recipe.json"]) {
|
||||
throw new Error(
|
||||
`${join(userDir, "recipe.json")} is not a regular file; nothing was imported`,
|
||||
);
|
||||
}
|
||||
writeRecipeFiles(dir, files);
|
||||
const imported = readRecipeJson(dir);
|
||||
appendRecord(root, {
|
||||
id: nextId(root, "recipe"),
|
||||
@@ -380,7 +437,12 @@ export function useRecipe({ projectDir, name }) {
|
||||
}
|
||||
|
||||
const recipe = readRecipeJson(dir);
|
||||
cpSync(join(dir, "frame.md"), join(root, "frame.md"));
|
||||
const frameSpec = join(dir, "frame.md");
|
||||
const frame = readRegularFile(frameSpec);
|
||||
if (frame === undefined) {
|
||||
throw new Error(`${frameSpec} is missing, a link or not a regular file; nothing was read`);
|
||||
}
|
||||
replaceFile(join(root, "frame.md"), frame);
|
||||
return {
|
||||
recipe,
|
||||
dir,
|
||||
|
||||
Reference in New Issue
Block a user