mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +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 });
|
||||
});
|
||||
}
|
||||
@@ -837,6 +837,26 @@ describe("external file change coordinator", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// Studio settles which composition is open just after load, while the draft's file stays the same.
|
||||
it("keeps a restored draft when the open composition changes but the draft's file does not", async () => {
|
||||
const { captured, rerender } = await mountCoordinator({
|
||||
recoveryFilePath: "index.html",
|
||||
loadConflictSnapshot: vi.fn(async () => ({
|
||||
kind: "conflict" as const,
|
||||
projectId: "project-a",
|
||||
filePath: "index.html",
|
||||
externalVersion: "v2",
|
||||
externalContent: "external",
|
||||
studioContent: "studio",
|
||||
createdAt: 100,
|
||||
})),
|
||||
});
|
||||
await vi.waitFor(() => expect(captured.handle?.blocked?.status).toBe("conflict"));
|
||||
|
||||
await rerender({ activeCompPath: null });
|
||||
await vi.waitFor(() => expect(captured.handle?.blocked?.status).toBe("conflict"));
|
||||
});
|
||||
|
||||
it("retains the final local candidate when a drain fails", async () => {
|
||||
const failure = new Error("network unavailable");
|
||||
const persistFailureSnapshot = vi.fn(async () => undefined);
|
||||
|
||||
@@ -248,7 +248,7 @@ export function useExternalFileChangeCoordinator({
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [loadConflictSnapshot, projectId, recoveryFilePath, setBlocked]);
|
||||
}, [loadConflictSnapshot, projectId, activeCompPath, recoveryFilePath, setBlocked]);
|
||||
|
||||
// What a settled change owes: thumbnails (unless its own write's echo brings them), then reloads.
|
||||
const deliverOwed = useCallback(
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { createStudioApi } from "@hyperframes/studio-server";
|
||||
import { IDBFactory } from "fake-indexeddb";
|
||||
import { mkdtempSync, readdirSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createMemoryExternalConflictStorage,
|
||||
@@ -9,6 +13,27 @@ import {
|
||||
} from "./externalConflictStorage";
|
||||
import { StudioFileConflictError } from "./studioSaveDiagnostics";
|
||||
|
||||
const made: string[] = [];
|
||||
|
||||
/** Studio's real API for one project, "demo", answering the store's fetches. */
|
||||
function serveProject(): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-drafts-"));
|
||||
made.push(dir);
|
||||
const api = createStudioApi({
|
||||
listProjects: () => [],
|
||||
resolveProject: async (id: string) => (id === "demo" ? { id, dir, title: "Demo" } : null),
|
||||
bundle: async () => null,
|
||||
lint: async () => ({ findings: [] }),
|
||||
runtimeUrl: "/api/runtime.js",
|
||||
rendersDir: () => join(dir, "renders"),
|
||||
startRender: () => ({ id: "job", status: "rendering", progress: 0, outputPath: "" }),
|
||||
});
|
||||
vi.stubGlobal("fetch", (input: string, init?: RequestInit) =>
|
||||
api.fetch(new Request(`http://localhost${input.replace(/^\/api/, "")}`, init)),
|
||||
);
|
||||
return dir;
|
||||
}
|
||||
|
||||
describe("external conflict snapshots", () => {
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal("indexedDB", new IDBFactory());
|
||||
@@ -17,6 +42,7 @@ describe("external conflict snapshots", () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("durably records both complete file versions before resolution", async () => {
|
||||
@@ -54,8 +80,8 @@ describe("external conflict snapshots", () => {
|
||||
expect(await storage.get("project-a", "index.html")).toEqual(snapshot);
|
||||
});
|
||||
|
||||
it("persists, loads, and deletes a snapshot through the production IndexedDB adapter", async () => {
|
||||
vi.spyOn(Date, "now").mockReturnValue(1234);
|
||||
it("persists, loads, and deletes a draft through the project's own server", async () => {
|
||||
const project = serveProject();
|
||||
const conflict = new StudioFileConflictError({
|
||||
filePath: "index.html",
|
||||
currentVersion: "v2",
|
||||
@@ -63,24 +89,50 @@ describe("external conflict snapshots", () => {
|
||||
attemptedContent: "studio full file",
|
||||
});
|
||||
|
||||
await persistExternalConflictSnapshot("indexeddb-project", conflict);
|
||||
await expect(loadExternalConflictSnapshot("indexeddb-project", "index.html")).resolves.toEqual({
|
||||
await persistExternalConflictSnapshot("demo", conflict);
|
||||
expect(readdirSync(join(project, ".hyperframes", "recovery"))).toHaveLength(1);
|
||||
await expect(loadExternalConflictSnapshot("demo", "index.html")).resolves.toMatchObject({
|
||||
kind: "conflict",
|
||||
projectId: "indexeddb-project",
|
||||
projectId: "demo",
|
||||
filePath: "index.html",
|
||||
externalVersion: "v2",
|
||||
externalContent: "external full file",
|
||||
studioContent: "studio full file",
|
||||
createdAt: 1234,
|
||||
});
|
||||
await deleteExternalConflictSnapshot("indexeddb-project", "index.html");
|
||||
await expect(
|
||||
loadExternalConflictSnapshot("indexeddb-project", "index.html"),
|
||||
).resolves.toBeNull();
|
||||
await deleteExternalConflictSnapshot("demo", "index.html");
|
||||
await expect(loadExternalConflictSnapshot("demo", "index.html")).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects persistence when IndexedDB is unavailable", async () => {
|
||||
vi.stubGlobal("indexedDB", undefined);
|
||||
// Every project previewed on a port shares the origin's IndexedDB, and so does each one's composition code.
|
||||
it("never offers a draft another project's code planted in the browser's shared IndexedDB", async () => {
|
||||
serveProject();
|
||||
const open = indexedDB.open("hyperframes-studio-external-conflicts", 1);
|
||||
open.onupgradeneeded = () => open.result.createObjectStore("file-conflicts");
|
||||
const db = await new Promise<IDBDatabase>(
|
||||
(resolve) => (open.onsuccess = () => resolve(open.result)),
|
||||
);
|
||||
const planted = db.transaction("file-conflicts", "readwrite");
|
||||
planted.objectStore("file-conflicts").put(
|
||||
{
|
||||
kind: "failed",
|
||||
projectId: "demo",
|
||||
filePath: "index.html",
|
||||
externalVersion: null,
|
||||
externalContent: null,
|
||||
studioContent: "<script>planted</script>",
|
||||
failureMessage: "Recovered draft",
|
||||
createdAt: Date.now(),
|
||||
},
|
||||
"demo\0index.html",
|
||||
);
|
||||
await new Promise((resolve) => (planted.oncomplete = resolve));
|
||||
db.close();
|
||||
|
||||
await expect(loadExternalConflictSnapshot("demo", "index.html")).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it("rejects persistence when the server cannot keep the draft", async () => {
|
||||
vi.stubGlobal("fetch", async () => new Response("{}", { status: 500 }));
|
||||
const conflict = new StudioFileConflictError({
|
||||
filePath: "index.html",
|
||||
currentVersion: "v2",
|
||||
@@ -88,8 +140,8 @@ describe("external conflict snapshots", () => {
|
||||
attemptedContent: "studio",
|
||||
});
|
||||
|
||||
await expect(persistExternalConflictSnapshot("project-a", conflict)).rejects.toThrow(
|
||||
"IndexedDB is unavailable; conflict recovery snapshot was not saved",
|
||||
await expect(persistExternalConflictSnapshot("demo", conflict)).rejects.toThrow(
|
||||
"Recovery draft storage failed (500)",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { StudioFileConflictError } from "./studioSaveDiagnostics";
|
||||
import { studioApiFetch } from "./studioApiFetch";
|
||||
|
||||
interface ExternalRecoverySnapshotBase {
|
||||
projectId: string;
|
||||
@@ -19,10 +20,6 @@ export interface ExternalConflictStorage {
|
||||
delete(projectId: string, filePath: string): Promise<void>;
|
||||
}
|
||||
|
||||
const DB_NAME = "hyperframes-studio-external-conflicts";
|
||||
const DB_VERSION = 1;
|
||||
const STORE_NAME = "file-conflicts";
|
||||
|
||||
function key(projectId: string, filePath: string): string {
|
||||
return `${projectId}\0${filePath}`;
|
||||
}
|
||||
@@ -43,85 +40,46 @@ export function createMemoryExternalConflictStorage(): ExternalConflictStorage {
|
||||
};
|
||||
}
|
||||
|
||||
function openDb(): Promise<IDBDatabase> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!globalThis.indexedDB) {
|
||||
reject(new Error("IndexedDB is unavailable; conflict recovery snapshot was not saved"));
|
||||
return;
|
||||
}
|
||||
const request = globalThis.indexedDB.open(DB_NAME, DB_VERSION);
|
||||
request.onupgradeneeded = () => {
|
||||
if (!request.result.objectStoreNames.contains(STORE_NAME)) {
|
||||
request.result.createObjectStore(STORE_NAME);
|
||||
}
|
||||
function draftUrl(projectId: string, filePath: string): string {
|
||||
return `/api/projects/${encodeURIComponent(projectId)}/recovery-drafts/${encodeURIComponent(filePath)}`;
|
||||
}
|
||||
|
||||
async function draftRequest(
|
||||
projectId: string,
|
||||
filePath: string,
|
||||
init?: RequestInit,
|
||||
): Promise<Response> {
|
||||
const res = await studioApiFetch(draftUrl(projectId, filePath), init);
|
||||
if (!res.ok) throw new Error(`Recovery draft storage failed (${res.status})`);
|
||||
return res;
|
||||
}
|
||||
|
||||
/** Kept by the project's own server: an origin-wide browser store is shared with every project on the port. */
|
||||
const projectStorage: ExternalConflictStorage = {
|
||||
async get(projectId, filePath) {
|
||||
const { draft } = (await (await draftRequest(projectId, filePath)).json()) as {
|
||||
draft: ExternalConflictSnapshot | null;
|
||||
};
|
||||
request.onerror = () => reject(request.error ?? new Error("Failed to open conflict storage"));
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
});
|
||||
}
|
||||
return draft?.projectId === projectId && draft.filePath === filePath ? draft : null;
|
||||
},
|
||||
async set(snapshot) {
|
||||
await draftRequest(snapshot.projectId, snapshot.filePath, {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(snapshot),
|
||||
});
|
||||
},
|
||||
async delete(projectId, filePath) {
|
||||
await draftRequest(projectId, filePath, { method: "DELETE" });
|
||||
},
|
||||
};
|
||||
|
||||
function withStore<T>(
|
||||
mode: IDBTransactionMode,
|
||||
callback: (store: IDBObjectStore) => IDBRequest<T>,
|
||||
): Promise<T> {
|
||||
return openDb().then(
|
||||
(db) =>
|
||||
new Promise<T>((resolve, reject) => {
|
||||
const transaction = db.transaction(STORE_NAME, mode);
|
||||
const request = callback(transaction.objectStore(STORE_NAME));
|
||||
let result!: T;
|
||||
request.onerror = () => reject(request.error ?? new Error("Conflict storage failed"));
|
||||
request.onsuccess = () => {
|
||||
result = request.result;
|
||||
};
|
||||
transaction.oncomplete = () => {
|
||||
db.close();
|
||||
resolve(result);
|
||||
};
|
||||
transaction.onerror = () => {
|
||||
db.close();
|
||||
reject(transaction.error ?? new Error("Conflict storage transaction failed"));
|
||||
};
|
||||
transaction.onabort = () => {
|
||||
db.close();
|
||||
reject(transaction.error ?? new Error("Conflict storage transaction was aborted"));
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function createIndexedDbExternalConflictStorage(): ExternalConflictStorage {
|
||||
return {
|
||||
async get(projectId, filePath) {
|
||||
return (
|
||||
(await withStore<ExternalConflictSnapshot | undefined>("readonly", (store) =>
|
||||
store.get(key(projectId, filePath)),
|
||||
)) ?? null
|
||||
);
|
||||
},
|
||||
async set(snapshot) {
|
||||
await withStore<IDBValidKey>("readwrite", (store) =>
|
||||
store.put(snapshot, key(snapshot.projectId, snapshot.filePath)),
|
||||
);
|
||||
},
|
||||
async delete(projectId, filePath) {
|
||||
await withStore<undefined>("readwrite", (store) => store.delete(key(projectId, filePath)));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const indexedDbStorage = createIndexedDbExternalConflictStorage();
|
||||
|
||||
/**
|
||||
* Persist a conflict before offering destructive recovery actions.
|
||||
* Callers must await this promise: rejection means the Studio draft was not saved durably and
|
||||
* must remain available in memory while the storage failure is surfaced to the user.
|
||||
*/
|
||||
/** Saved before destructive recovery is offered; on a rejection the draft stays only in memory and the user is told. */
|
||||
export async function persistExternalConflictSnapshot(
|
||||
projectId: string,
|
||||
conflict: StudioFileConflictError,
|
||||
): Promise<void> {
|
||||
await indexedDbStorage.set({
|
||||
await projectStorage.set({
|
||||
kind: "conflict",
|
||||
projectId,
|
||||
filePath: conflict.filePath,
|
||||
@@ -132,11 +90,7 @@ export async function persistExternalConflictSnapshot(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the final Studio candidate after its file write fails.
|
||||
* Callers must await this promise: rejection means the Studio draft was not saved durably and
|
||||
* must remain available in memory while the storage failure is surfaced to the user.
|
||||
*/
|
||||
/** Studio's final candidate after its file write failed; awaited like persistExternalConflictSnapshot. */
|
||||
export async function persistExternalFailureSnapshot(
|
||||
projectId: string,
|
||||
filePath: string,
|
||||
@@ -145,7 +99,7 @@ export async function persistExternalFailureSnapshot(
|
||||
externalContent: string | null,
|
||||
error: unknown,
|
||||
): Promise<void> {
|
||||
await indexedDbStorage.set({
|
||||
await projectStorage.set({
|
||||
kind: "failed",
|
||||
projectId,
|
||||
filePath,
|
||||
@@ -161,12 +115,12 @@ export async function loadExternalConflictSnapshot(
|
||||
projectId: string,
|
||||
filePath: string,
|
||||
): Promise<ExternalConflictSnapshot | null> {
|
||||
return indexedDbStorage.get(projectId, filePath);
|
||||
return projectStorage.get(projectId, filePath);
|
||||
}
|
||||
|
||||
export async function deleteExternalConflictSnapshot(
|
||||
projectId: string,
|
||||
filePath: string,
|
||||
): Promise<void> {
|
||||
await indexedDbStorage.delete(projectId, filePath);
|
||||
await projectStorage.delete(projectId, filePath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user