mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(studio): recovery drafts are kept by each project's own server, never the browser (#7904)
* fix(studio): recovery drafts are kept by each project's own server, never the browser * fix(studio): a restored draft stays offered when Studio settles which composition is open * fix(studio): a draft read never waits on a pipe or loads an oversized record * test(studio): the draft read's child fixture is an entry point, and its note sits in the test * chore(studio-server): size budget covers the recovery-draft route * chore(cli): unpacked size budget covers the recovery-draft route
This commit is contained in:
@@ -16,6 +16,7 @@ import { registerPeakRoutes } from "./routes/peaks.js";
|
||||
import { registerFontRoutes } from "./routes/fonts.js";
|
||||
import { registerRegistryRoutes } from "./routes/registry.js";
|
||||
import { registerSelectionRoutes } from "./routes/selection.js";
|
||||
import { registerRecoveryDraftRoutes } from "./routes/recoveryDrafts.js";
|
||||
import { registerMediaRoutes } from "./routes/media.js";
|
||||
import { registerGlobalAssetRoutes } from "./routes/globalAssets.js";
|
||||
import { registerHistoryRoutes } from "./routes/history.js";
|
||||
@@ -222,6 +223,7 @@ export function createStudioApi(adapter: StudioApiAdapter, options: StudioApiOpt
|
||||
registerThumbnailRoutes(api, adapter);
|
||||
registerImageThumbnailRoutes(api, adapter);
|
||||
registerSelectionRoutes(api, adapter);
|
||||
registerRecoveryDraftRoutes(api, adapter);
|
||||
registerMediaRoutes(api, adapter);
|
||||
registerWaveformRoutes(api, adapter);
|
||||
registerFreezeFrameRoutes(api, adapter);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Hono } from "hono";
|
||||
import type { StudioApiAdapter } from "../types.js";
|
||||
import { registerRecoveryDraftRoutes } from "./recoveryDrafts.js";
|
||||
|
||||
const [dir, ...files] = process.argv.slice(2);
|
||||
const api = new Hono();
|
||||
registerRecoveryDraftRoutes(api, {
|
||||
resolveProject: async (id: string) => (id === "demo" ? { id, dir, title: "Demo" } : null),
|
||||
} as unknown as StudioApiAdapter);
|
||||
for (const file of files) {
|
||||
const res = await api.request(`/projects/demo/recovery-drafts/${encodeURIComponent(file)}`);
|
||||
console.log(JSON.stringify({ file, status: res.status, draft: (await res.json()).draft }));
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import { Hono } from "hono";
|
||||
import { execFileSync, spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import {
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readdirSync,
|
||||
rmSync,
|
||||
symlinkSync,
|
||||
truncateSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import type { StudioApiAdapter } from "../types";
|
||||
import { registerRecoveryDraftRoutes } from "./recoveryDrafts";
|
||||
|
||||
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 });
|
||||
});
|
||||
|
||||
function draftsApi(dir: string): Hono {
|
||||
const api = new Hono();
|
||||
registerRecoveryDraftRoutes(api, {
|
||||
resolveProject: async (id: string) => (id === "demo" ? { id, dir, title: "Demo" } : null),
|
||||
} as unknown as StudioApiAdapter);
|
||||
return api;
|
||||
}
|
||||
|
||||
const put = (api: Hono, path: string, body: unknown) =>
|
||||
api.request(`/projects/demo/recovery-drafts/${encodeURIComponent(path)}`, {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
const DRAFT = {
|
||||
kind: "failed",
|
||||
studioContent: "draft",
|
||||
externalVersion: null,
|
||||
externalContent: null,
|
||||
failureMessage: "disk full",
|
||||
};
|
||||
|
||||
describe("recovery drafts", () => {
|
||||
it("answers with the project and path asked for, whatever the stored body named", async () => {
|
||||
const api = draftsApi(temp("hf-drafts-"));
|
||||
await put(api, "a/index.html", { ...DRAFT, projectId: "other", filePath: "x.html" });
|
||||
|
||||
const res = await api.request(
|
||||
`/projects/demo/recovery-drafts/${encodeURIComponent("a/index.html")}`,
|
||||
);
|
||||
expect((await res.json()).draft).toMatchObject({
|
||||
...DRAFT,
|
||||
projectId: "demo",
|
||||
filePath: "a/index.html",
|
||||
});
|
||||
});
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"never writes a draft through a link at the project's recovery folder",
|
||||
async () => {
|
||||
const project = temp("hf-drafts-");
|
||||
const outside = temp("hf-drafts-outside-");
|
||||
mkdirSync(join(project, ".hyperframes"));
|
||||
symlinkSync(outside, join(project, ".hyperframes", "recovery"));
|
||||
|
||||
expect((await put(draftsApi(project), "index.html", DRAFT)).status).toBe(409);
|
||||
expect(readdirSync(outside)).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"reads a draft without waiting on a pipe and without loading an oversized record",
|
||||
() => {
|
||||
const project = temp("hf-drafts-");
|
||||
const drafts = join(project, ".hyperframes", "recovery");
|
||||
mkdirSync(drafts, { recursive: true });
|
||||
const record = (file: string) =>
|
||||
join(drafts, `${createHash("sha256").update(file).digest("hex")}.json`);
|
||||
writeFileSync(record("kept.html"), JSON.stringify({ ...DRAFT, createdAt: 1 }));
|
||||
execFileSync("mkfifo", [record("pipe.html")]);
|
||||
writeFileSync(record("huge.html"), "");
|
||||
truncateSync(record("huge.html"), 200 * 1024 * 1024);
|
||||
|
||||
// In a child: a read that blocks stalls only that process, which the deadline then kills.
|
||||
const fixture = join(dirname(fileURLToPath(import.meta.url)), "recoveryDrafts.fixture.ts");
|
||||
const child = spawnSync(
|
||||
process.execPath,
|
||||
["--import", "tsx", fixture, project, "kept.html", "pipe.html", "huge.html"],
|
||||
{ encoding: "utf8", timeout: 20_000, killSignal: "SIGKILL" },
|
||||
);
|
||||
|
||||
expect(child.signal, child.stderr).toBeNull();
|
||||
const answers = child.stdout
|
||||
.trim()
|
||||
.split("\n")
|
||||
.map((line) => JSON.parse(line));
|
||||
expect(answers).toMatchObject([
|
||||
{ file: "kept.html", status: 200, draft: { ...DRAFT, filePath: "kept.html" } },
|
||||
{ file: "pipe.html", status: 200, draft: null },
|
||||
{ file: "huge.html", status: 200, draft: null },
|
||||
]);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,100 @@
|
||||
import type { Context, Hono } from "hono";
|
||||
import { createHash } from "node:crypto";
|
||||
import { closeSync, constants, fstatSync, openSync, readFileSync, rmSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { replaceFileAtomically } from "@hyperframes/core/atomic-file";
|
||||
import { requestSubPath } from "../helpers/requestSubPath.js";
|
||||
import { isLinkFreeWithinProject, mkdirOwnedWithinProject } from "../helpers/safePath.js";
|
||||
import type { StudioApiAdapter } from "../types.js";
|
||||
|
||||
const ROUTE = "projects/:id/recovery-drafts";
|
||||
const DRAFTS = join(".hyperframes", "recovery");
|
||||
|
||||
const isText = (value: unknown): value is string => typeof value === "string";
|
||||
const isTextOrNull = (value: unknown): value is string | null => value === null || isText(value);
|
||||
|
||||
/** The fields a draft keeps; its project, path and time come from the request, never the body. */
|
||||
function draftFields(body: unknown): Record<string, unknown> | null {
|
||||
const { kind, studioContent, externalVersion, externalContent, failureMessage } = (body ??
|
||||
{}) as Record<string, unknown>;
|
||||
const message = kind === "failed" ? failureMessage : kind === "conflict" ? "" : null;
|
||||
const valid =
|
||||
[studioContent, message].every(isText) &&
|
||||
[externalVersion, externalContent].every(isTextOrNull);
|
||||
return valid ? { kind, studioContent, externalVersion, externalContent, failureMessage } : null;
|
||||
}
|
||||
|
||||
/** A draft holds two copies of a file the files route reads up to 32 MB of. */
|
||||
const MAX_DRAFT_BYTES = 96 * 1024 * 1024;
|
||||
|
||||
function readDraft(file: string): unknown {
|
||||
let fd: number;
|
||||
try {
|
||||
// Nonblocking: a FIFO in a received project would otherwise hold the open, and the server, for a writer.
|
||||
fd = openSync(
|
||||
file,
|
||||
constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0),
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const stat = fstatSync(fd);
|
||||
if (!stat.isFile() || stat.size > MAX_DRAFT_BYTES) return null;
|
||||
return JSON.parse(readFileSync(fd, "utf-8"));
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
type Target = {
|
||||
projectId: string;
|
||||
projectDir: string;
|
||||
filePath: string;
|
||||
dir: string;
|
||||
file: string;
|
||||
};
|
||||
|
||||
async function draftTarget(c: Context, adapter: StudioApiAdapter): Promise<Target | Response> {
|
||||
const project = await adapter.resolveProject(c.req.param("id") ?? "");
|
||||
if (!project) return c.json({ error: "not found" }, 404);
|
||||
const filePath = requestSubPath(c.req.url, ROUTE);
|
||||
if (!filePath || filePath.includes("\0")) return c.json({ error: "bad path" }, 400);
|
||||
const dir = join(project.dir, DRAFTS);
|
||||
if (!isLinkFreeWithinProject(project.dir, dir)) return c.json({ error: "not a folder" }, 409);
|
||||
const file = join(dir, `${createHash("sha256").update(filePath).digest("hex")}.json`);
|
||||
return { projectId: project.id, projectDir: project.dir, filePath, dir, file };
|
||||
}
|
||||
|
||||
function storedDraft({ projectId, filePath, file }: Target) {
|
||||
const stored = readDraft(file);
|
||||
const fields = draftFields(stored);
|
||||
const at = (stored as { createdAt?: unknown } | null)?.createdAt;
|
||||
return fields && { ...fields, projectId, filePath, createdAt: typeof at === "number" ? at : 0 };
|
||||
}
|
||||
|
||||
/** Studio's unsaved draft of one file, kept in that project's own `.hyperframes`, so only its own session made it. */
|
||||
export function registerRecoveryDraftRoutes(api: Hono, adapter: StudioApiAdapter): void {
|
||||
const path = `/${ROUTE}/*`;
|
||||
api.get(path, async (c) => {
|
||||
const target = await draftTarget(c, adapter);
|
||||
return target instanceof Response ? target : c.json({ draft: storedDraft(target) });
|
||||
});
|
||||
api.delete(path, async (c) => {
|
||||
const target = await draftTarget(c, adapter);
|
||||
if (target instanceof Response) return target;
|
||||
rmSync(target.file, { force: true });
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
api.put(path, async (c) => {
|
||||
const target = await draftTarget(c, adapter);
|
||||
if (target instanceof Response) return target;
|
||||
const fields = draftFields(await c.req.json().catch(() => null));
|
||||
if (!fields) return c.json({ error: "invalid draft" }, 400);
|
||||
mkdirOwnedWithinProject(target.projectDir, target.dir);
|
||||
replaceFileAtomically(target.file, JSON.stringify({ ...fields, createdAt: Date.now() }), 0o600);
|
||||
return c.json({ ok: true });
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user