From cb2b51525effe2947bf8d3d38d870174a24e05d3 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Tue, 17 Nov 2026 23:27:17 +0000 Subject: [PATCH] fix(cli): a rename keeps the received-files record listing the moved file (#8112) * fix(cli): a rename keeps the received-files record listing the moved file On a disk that tells letter case or Unicode forms apart, a Studio rename to a name differing only in case or form followed the old spelling, so the record dropped the file's only line while the file moved to the new name. - onDiskPath folds a requested name onto an existing entry only when the disk opens the same file under both spellings, so the record follows the exact path the rename creates, spelled as the move spells it. - After a move, an old non-media line goes only when the file left its old path, sits at the planned path (same identity or content), and another line lists that path. Otherwise the line stays, as it does for a delete. - A refused move keeps a new line for a file another move already put there. * docs(cli): word a rename helper comment as what it checks now * test(cli): build the rename test folder by its native real path On Windows the temp folder can carry an 8.3 short name, which onDiskPath spells in full, as the native real path does. --- .../cli/src/server/followReceivedRecords.ts | 4 +- .../studioServer.receivedRecord.test.ts | 97 ++++++++++++++- packages/cli/src/utils/privateProjectFile.ts | 2 +- .../src/utils/receivedRecordFollow.test.ts | 112 ++++++++++++++++++ .../cli/src/utils/receivedRecordFollow.ts | 108 +++++++++++++---- 5 files changed, 298 insertions(+), 25 deletions(-) create mode 100644 packages/cli/src/utils/receivedRecordFollow.test.ts diff --git a/packages/cli/src/server/followReceivedRecords.ts b/packages/cli/src/server/followReceivedRecords.ts index 97a5322c71..6c06d66bd9 100644 --- a/packages/cli/src/server/followReceivedRecords.ts +++ b/packages/cli/src/server/followReceivedRecords.ts @@ -71,8 +71,8 @@ async function startedChange( /** * Follows a Studio rename, move or delete in the received-files records. Before the files move, every file listed in * a record inside a moved or deleted folder is listed in the project's own record, the received content leaving a - * listed path is kept, and a renamed file is listed under its new path; when that fails, the request is refused. - * Afterwards old lines go, but a non-media file deleted or renamed to a media name stays listed, still received. + * listed path is kept, and a renamed file is listed under the path it moves to; when that fails, the request is refused. + * Afterwards an old line goes once its file is listed where it sits; a non-media file deleted or made media stays. */ function followMovesAndDeletes(app: Hono, projectDir: string): void { app.on(["PATCH", "DELETE"], "/api/projects/:id/files/*", async (c, next) => { diff --git a/packages/cli/src/server/studioServer.receivedRecord.test.ts b/packages/cli/src/server/studioServer.receivedRecord.test.ts index 192093eab3..4ca5e47e9f 100644 --- a/packages/cli/src/server/studioServer.receivedRecord.test.ts +++ b/packages/cli/src/server/studioServer.receivedRecord.test.ts @@ -56,6 +56,17 @@ function caseInsensitiveDisk(): boolean { } } +/** Whether a name in composed Unicode reaches a file named in decomposed Unicode, as on macOS and Windows. */ +function unicodeFormsFolded(): boolean { + const probe = mkdtempSync(join(tmpdir(), "hf-unicode-probe-")); + try { + writeFileSync(join(probe, "caf\u00e9"), ""); + return existsSync(join(probe, "cafe\u0301")); + } finally { + rmSync(probe, { recursive: true, force: true }); + } +} + function write(path: string, text: string | Buffer): void { mkdirSync(join(path, ".."), { recursive: true }); writeFileSync(path, text); @@ -379,6 +390,88 @@ describe("a Studio rename, move or delete in a project with a received-files rec }, ); + it.runIf(caseInsensitiveDisk())( + "refuses a rename that changes only the letter case on a disk that folds case, keeping the line", + async () => { + const { projectDir, app } = studio(); + withRecord(projectDir); + expect((await move(app, "scenes/intro.html", "scenes/Intro.html")).status).toBe(409); + expect(listed(projectDir)).toEqual(["scenes/intro.html", "scenes/logo.png"]); + expect(allowed(projectDir)).toBe(false); + }, + ); + + it.skipIf(caseInsensitiveDisk())( + "lists a received page under its new name after a rename that changes only its letter case", + async () => { + const { projectDir, app } = studio(); + withRecord(projectDir); + expect((await move(app, "scenes/intro.html", "scenes/Intro.html")).status).toBe(200); + expect(listed(projectDir)).toEqual(["scenes/Intro.html", "scenes/logo.png"]); + expect(allowed(projectDir)).toBe(false); + }, + ); + + it.skipIf(unicodeFormsFolded())( + "lists a received page under its new name after a rename from composed to decomposed Unicode", + async () => { + const { projectDir, app } = studio(); + withRecord(projectDir); + write(join(projectDir, "scenes", "caf\u00e9.html"), ``); + write(record(projectDir), JSON.stringify({ files: ["scenes/caf\u00e9.html"] })); + expect((await move(app, "scenes/caf\u00e9.html", "scenes/cafe\u0301.html")).status).toBe(200); + expect(listed(projectDir)).toEqual(["scenes/cafe\u0301.html"]); + expect(allowed(projectDir)).toBe(false); + }, + ); + + it.skipIf(unicodeFormsFolded())( + "keeps a received page listed when a file named like it in another Unicode form is renamed", + async () => { + const { projectDir, app } = studio(); + withRecord(projectDir); + write(join(projectDir, "scenes", "caf\u00e9.html"), ``); + write(join(projectDir, "scenes", "cafe\u0301.html"), "

"); + write(record(projectDir), JSON.stringify({ files: ["scenes/caf\u00e9.html"] })); + expect((await move(app, "scenes/cafe\u0301.html", "scenes/draft.html")).status).toBe(200); + expect(listed(projectDir)).toEqual(["scenes/caf\u00e9.html", "scenes/draft.html"]); + }, + ); + + it.skipIf(caseInsensitiveDisk())( + "lists a received page under its new name when that name differs only in letter case from another listed file", + async () => { + const { projectDir, app } = studio(); + withRecord(projectDir); + write(join(projectDir, "scenes", "Opening.html"), "

other

"); + write( + record(projectDir), + JSON.stringify({ files: ["scenes/Opening.html", "scenes/intro.html"] }), + ); + expect((await move(app, "scenes/intro.html", "scenes/opening.html")).status).toBe(200); + expect(listed(projectDir)).toEqual(["scenes/Opening.html", "scenes/opening.html"]); + expect(allowed(projectDir)).toBe(false); + }, + ); + + it.skipIf(caseInsensitiveDisk())( + "keeps refusing outside files after a received page is renamed in another letter case, deleted and made again", + async () => { + const { projectDir, app } = studio(); + withRecord(projectDir); + expect((await move(app, "scenes/intro.html", "scenes/Intro.html")).status).toBe(200); + const page = fileUrl("scenes/Intro.html"); + expect((await app.request(page, { method: "DELETE" })).status).toBe(200); + const made = await app.request(page, { + method: "PUT", + headers: { "If-None-Match": "*" }, + body: `

new

`, + }); + expect(made.ok).toBe(true); + expect(allowed(projectDir)).toBe(false); + }, + ); + it("answers other requests while it waits for the record's lock, then refuses the rename", async () => { const { projectDir, app } = studio(); withRecord(projectDir); @@ -563,13 +656,13 @@ describe("a Studio rename, move or delete in a project with a received-files rec expect(receivedFiles(projectDir).gone).toBe("scenes/intro.html"); }); - it("moves a line its record spells in decomposed Unicode", async () => { + it("moves a line its record spells in decomposed Unicode, spelled as the move spells it", async () => { const { projectDir, app } = studio(); const folder = "cafe\u0301"; write(join(projectDir, folder, "intro.html"), "

"); write(record(projectDir), JSON.stringify({ files: [`${folder}/intro.html`] })); expect((await move(app, folder, `parts/${folder}`)).status).toBe(200); - expect(listed(projectDir)).toEqual(["parts/caf\u00e9/intro.html"]); + expect(listed(projectDir)).toEqual([`parts/${folder}/intro.html`]); expect(receivedFiles(projectDir)("parts/caf\u00e9/intro.html")).toBe(true); }); }); diff --git a/packages/cli/src/utils/privateProjectFile.ts b/packages/cli/src/utils/privateProjectFile.ts index 4853cda8c3..cb4ee055df 100644 --- a/packages/cli/src/utils/privateProjectFile.ts +++ b/packages/cli/src/utils/privateProjectFile.ts @@ -500,7 +500,7 @@ function receivedRecords(projectDir: string): ReceivedRecord[] | null { return records; } -function fileIdentity(path: string): string | null { +export function fileIdentity(path: string): string | null { try { const stat = lstatSync(path, { bigint: true }); return stat.ino === 0n ? null : `${stat.dev}:${stat.ino}`; diff --git a/packages/cli/src/utils/receivedRecordFollow.test.ts b/packages/cli/src/utils/receivedRecordFollow.test.ts new file mode 100644 index 0000000000..1397a8e07a --- /dev/null +++ b/packages/cli/src/utils/receivedRecordFollow.test.ts @@ -0,0 +1,112 @@ +import { + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + realpathSync, + renameSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { + dropReceivedArrivals, + dropReceivedLeavers, + onDiskPath, + plannedReceivedChange, + recordReceivedArrivals, +} from "./receivedRecordFollow.js"; + +let projectDir: string; + +afterEach(() => rmSync(projectDir, { recursive: true, force: true })); + +function project(listed: string[]): string { + projectDir = realpathSync.native(mkdtempSync(join(tmpdir(), "hf-record-follow-"))); + mkdirSync(join(projectDir, ".hyperframes")); + writeFileSync( + join(projectDir, ".hyperframes", "received-files.json"), + JSON.stringify({ files: listed }), + ); + for (const line of listed) writeFileSync(join(projectDir, line), "

received

"); + return projectDir; +} + +const listed = (dir: string): string[] => + JSON.parse(readFileSync(join(dir, ".hyperframes", "received-files.json"), "utf-8")).files; + +async function followRename(dir: string, planned: string, actual: string): Promise { + const change = await plannedReceivedChange(dir, join(dir, "intro.html"), join(dir, planned)); + const started = await recordReceivedArrivals(change); + renameSync(join(dir, "intro.html"), join(dir, actual)); + await dropReceivedLeavers(started); +} + +describe("following a rename in the received-files record", () => { + it("drops the old line once the file sits at the path the record now lists", async () => { + const dir = project(["intro.html"]); + await followRename(dir, "opening.html", "opening.html"); + expect(listed(dir)).toEqual(["opening.html"]); + }); + + it("keeps the old line when the file is not at the path the record now lists", async () => { + const dir = project(["intro.html"]); + await followRename(dir, "opening.html", "elsewhere.html"); + expect(listed(dir)).toEqual(["intro.html", "opening.html"]); + }); + + it("keeps the old line when the file stays put and a copy of it appears at the planned path", async () => { + const dir = project(["intro.html"]); + const change = await plannedReceivedChange( + dir, + join(dir, "intro.html"), + join(dir, "opening.html"), + ); + const started = await recordReceivedArrivals(change); + writeFileSync(join(dir, "opening.html"), "

received

"); + await dropReceivedLeavers(started); + expect(listed(dir)).toEqual(["intro.html", "opening.html"]); + }); + + it("keeps the new line a refused rename listed when another rename moved the file there", async () => { + const dir = project(["intro.html"]); + const plan = () => + plannedReceivedChange(dir, join(dir, "intro.html"), join(dir, "opening.html")); + const refused = await recordReceivedArrivals(await plan()); + const done = await recordReceivedArrivals(await plan()); + renameSync(join(dir, "intro.html"), join(dir, "opening.html")); + await dropReceivedLeavers(done); + await dropReceivedArrivals(refused); + expect(listed(dir)).toEqual(["opening.html"]); + }); + + it("takes back the new line of a refused rename when the file did not move", async () => { + const dir = project(["intro.html"]); + const change = await plannedReceivedChange( + dir, + join(dir, "intro.html"), + join(dir, "opening.html"), + ); + await dropReceivedArrivals(await recordReceivedArrivals(change)); + expect(listed(dir)).toEqual(["intro.html"]); + }); + + it("keeps the old line when the planned path is the old line itself", async () => { + const dir = project(["intro.html"]); + await followRename(dir, "intro.html", "elsewhere.html"); + expect(listed(dir)).toEqual(["intro.html"]); + }); +}); + +describe("onDiskPath", () => { + it("spells a path as the folder lists it only when the disk opens one file under both spellings", () => { + const dir = project(["intro.html"]); + const foldsCase = existsSync(join(dir, "INTRO.html")); + expect(onDiskPath(join(dir, "INTRO.html"))).toBe( + join(dir, foldsCase ? "intro.html" : "INTRO.html"), + ); + expect(onDiskPath(join(dir, "intro.html"))).toBe(join(dir, "intro.html")); + }); +}); diff --git a/packages/cli/src/utils/receivedRecordFollow.ts b/packages/cli/src/utils/receivedRecordFollow.ts index 9603f13467..e73752e7e7 100644 --- a/packages/cli/src/utils/receivedRecordFollow.ts +++ b/packages/cli/src/utils/receivedRecordFollow.ts @@ -5,19 +5,22 @@ import { realFilePath, resolveWithinProject } from "@hyperframes/studio-server/s import { registryRoot } from "../registry/publication.js"; import { changeReceivedRecordAsync, + fileIdentity, filesListedInFolder, receivedLinesAtOrAbove, } from "./privateProjectFile.js"; -import { contentHashAsync, hashable, readReceivedContent } from "./receivedContent.js"; +import { contentHash, contentHashAsync, hashable, readReceivedContent } from "./receivedContent.js"; import { isMediaFontOrImage, isMediaName } from "./receivedRender.js"; type Lines = Map; -/** A listed file a Studio rename, move or delete takes off its path, and its sha256 when its content is checked. */ +/** A listed file a Studio rename, move or delete takes off its path, its sha256 when checked, and where a move puts it. */ interface Leaving { line: string; hash?: string; stays?: boolean; + identity?: string | null; + landsAt?: string; } /** Worked out while the files are on disk. A line whose file is already gone stays, as the sign it is gone. */ @@ -36,24 +39,26 @@ function caseInsensitive(folder: string): boolean { return swapped !== folder && existsSync(swapped); } -/** `path` spelled as its folder lists it, so a request in another letter case or Unicode form names the listed file. */ +/** `path` spelled as its folder lists it, when the disk opens that same file under the spelling `path` asks for. */ export function onDiskPath(path: string): string { const folder = realFilePath(dirname(path)); const name = basename(path); + const asked = fileIdentity(join(folder, name)); + if (asked === null) return join(folder, name); const folded = name.normalize("NFC").toLowerCase(); + const sameFile = (entry: string) => + entry.normalize("NFC").toLowerCase() === folded && fileIdentity(join(folder, entry)) === asked; try { const names = readdirSync(folder); - const found = - names.find((entry) => entry === name) ?? - names.find((entry) => entry.normalize("NFC").toLowerCase() === folded); - return join(folder, found ?? name); + return join(folder, names.includes(name) ? name : (names.find(sameFile) ?? name)); } catch { return join(folder, name); } } -const keyIn = (folder: string, path: string) => - relative(folder, path).split(sep).join("/").normalize("NFC"); +const pathIn = (folder: string, path: string) => relative(folder, path).split(sep).join("/"); + +const keyIn = (folder: string, path: string) => pathIn(folder, path).normalize("NFC"); const leavesFolder = (key: string) => key === ".." || key.startsWith("../") || isAbsolute(key); @@ -118,6 +123,38 @@ async function leaving(line: string, path: string): Promise { return hash === null ? null : { line, hash, stays: true }; } +interface Arrival { + folder: string; + line: string; +} + +function arrivalOfMovedLine( + root: string, + folder: string, + fromKey: string, + line: string, + to: string, +): Arrival { + const rest = line + .split("/") + .slice(fromKey.split("/").length) + .map((segment) => `/${segment}`) + .join(""); + const toKey = pathIn(folder, to); + if (leavesFolder(toKey)) return { folder: root, line: pathIn(root, to) + rest }; + return { folder, line: toKey + rest }; +} + +function leaverStayingUntilListedWhereItLands( + folder: string, + left: Leaving, + arrived: Arrival, +): Leaving { + const landsAt = join(arrived.folder, arrived.line); + const stays = left.stays === true && !isMediaName(left.line) && isMediaName(landsAt); + return { ...left, stays, identity: fileIdentity(join(folder, left.line)), landsAt }; +} + export async function plannedReceivedChange( projectDir: string, from: string, @@ -135,14 +172,9 @@ export async function plannedReceivedChange( push(change.leaving, folder, left); continue; } - const rest = line.normalize("NFC").slice(fromKey.length); - const toKey = keyIn(folder, to); - push(change.leaving, folder, { - ...left, - stays: left.stays === true && !isMediaName(line) && isMediaName(toKey + rest), - }); - if (leavesFolder(toKey)) push(change.arriving, root, keyIn(root, to) + rest); - else push(change.arriving, folder, toKey + rest); + const arrived = arrivalOfMovedLine(root, folder, fromKey, line, to); + push(change.leaving, folder, leaverStayingUntilListedWhereItLands(folder, left, arrived)); + push(change.arriving, arrived.folder, arrived.line); } } return change; @@ -176,18 +208,54 @@ export async function recordReceivedArrivals(change: ReceivedChange): Promise + (!!left.identity && fileIdentity(path) === left.identity) || + (left.hash !== undefined && contentHash(path) === left.hash); + +/** Whether the file `left` lists has left its old path for the one its move planned. */ +const movedAsPlanned = (folder: string, left: Leaving): left is Leaving & { landsAt: string } => + left.landsAt !== undefined && + !holdsLeaver(join(folder, left.line), left) && + holdsLeaver(left.landsAt, left); + +/** Whether a non-media file sits at its planned destination and a record lists it there by another line. */ +function listedWhereItLands(folder: string, left: Leaving): boolean { + if (left.landsAt === undefined || left.hash === undefined) return true; + if (!movedAsPlanned(folder, left)) return false; + const { landsAt } = left; + return receivedLinesAtOrAbove(dirname(landsAt)).some(({ folder: at, lines }) => + lines.some((line) => join(at, line) === landsAt && (at !== folder || line !== left.line)), + ); +} + export const dropReceivedLeavers = (change: ReceivedChange): Promise => dropLines( new Map( [...change.leaving].map(([folder, lines]) => [ folder, - lines.flatMap((left) => (left.stays ? [] : [left.line])), + lines.flatMap((left) => + !left.stays && listedWhereItLands(folder, left) ? [left.line] : [], + ), ]), ), ); -export const dropReceivedArrivals = (change: ReceivedChange): Promise => - dropLines(change.arriving); +/** A refused move's new lines go, but for a file another move already put there. */ +export function dropReceivedArrivals(change: ReceivedChange): Promise { + const landed = new Set( + [...change.leaving].flatMap(([folder, lines]) => + lines.filter((left) => movedAsPlanned(folder, left)).flatMap((left) => left.landsAt ?? []), + ), + ); + return dropLines( + new Map( + [...change.arriving].map(([folder, lines]) => [ + folder, + lines.filter((line) => !landed.has(join(folder, line))), + ]), + ), + ); +} /** * Lists in the project's own record each of `paths` that no record lists and that holds content kept as received, so