Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(pages): keep the previous owner page when a replacement cannot be installed

erywim19967f1d1b3a
diff --git a/server/pages/owner-files.test.ts b/server/pages/owner-files.test.ts
new file mode 100644
index 0000000000..9cab946cda
--- /dev/null
+++ b/server/pages/owner-files.test.ts
@@ -0,0 +1,49 @@
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const control = vi.hoisted(() => ({ failInstalling: false }));
+vi.mock("../atomic.ts", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("../atomic.ts")>();
+ return {
+ ...actual,
+ renameWithRetry: (from: string, to: string) => {
+ if (control.failInstalling && from.endsWith(".tmp")) throw new Error("the replacement could not be installed");
+ actual.renameWithRetry(from, to);
+ },
+ };
+});
+
+import { PublishedPageFiles } from "./owner-files.ts";
+
+const PAGE = "page0000000000000000000001";
+const index = (text: string) => [{ path: "index.html", data: Buffer.from(text) }];
+
+describe("an owner-only page's files", () => {
+ let dir: string;
+ beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "omb-owner-files-")); });
+ afterEach(() => rmSync(dir, { recursive: true, force: true }));
+
+ it("swaps in a new version whole, and restores the old one when installing the replacement fails", () => {
+ const files = new PublishedPageFiles(dir);
+ files.save(PAGE, index("old"));
+ expect(files.read(PAGE, "")?.toString()).toBe("old");
+ control.failInstalling = true;
+ try {
+ expect(() => files.save(PAGE, index("new"))).toThrow("the replacement could not be installed");
+ } finally {
+ control.failInstalling = false;
+ }
+ expect(files.read(PAGE, "")?.toString()).toBe("old");
+ files.save(PAGE, index("newer"));
+ expect(files.read(PAGE, "")?.toString()).toBe("newer");
+ });
+
+ it("keeps the old version when the new one is incomplete", () => {
+ const files = new PublishedPageFiles(dir);
+ files.save(PAGE, index("old"));
+ expect(() => files.save(PAGE, [{ path: "style.css", data: Buffer.from("h1{}") }])).toThrow("the page has no index.html");
+ expect(files.read(PAGE, "")?.toString()).toBe("old");
+ });
+});
diff --git a/server/pages/owner-files.ts b/server/pages/owner-files.ts
index 72f24fa4fb..846022ff6b 100644
--- a/server/pages/owner-files.ts
+++ b/server/pages/owner-files.ts
@@ -19,7 +19,7 @@ export class PublishedPageFiles {
}
save(pageId: string, files: readonly PageFile[]): void {
- const target = join(this.dir, pageId), staging = `${target}.${randomBytes(6).toString("hex")}.tmp`;
+ const target = join(this.dir, pageId), staging = `${target}.${randomBytes(6).toString("hex")}.tmp`, backup = `${target}.old`;
rmSync(staging, { recursive: true, force: true });
try {
mkdirSync(staging, { recursive: true, mode: DIR_MODE });
@@ -31,8 +31,20 @@ export class PublishedPageFiles {
writeFileSync(full, file.data, { mode: FILE_MODE });
}
if (!existsSync(join(staging, "index.html"))) throw new Error("the page has no index.html");
- rmSync(target, { recursive: true, force: true });
- renameWithRetry(staging, target);
+ // A save that stopped between the two renames left the page under the backup name.
+ if (!existsSync(target) && existsSync(backup)) renameWithRetry(backup, target);
+ rmSync(backup, { recursive: true, force: true });
+ if (!existsSync(target)) renameWithRetry(staging, target);
+ else {
+ renameWithRetry(target, backup);
+ try {
+ renameWithRetry(staging, target);
+ } catch (error) {
+ try { renameWithRetry(backup, target); } catch { /* an unrestored backup is unserved; the error below still names the failure */ }
+ throw error;
+ }
+ try { rmSync(backup, { recursive: true, force: true }); } catch { /* the old files stay unserved beside the page */ }
+ }
} catch (error) {
rmSync(staging, { recursive: true, force: true });
throw error;