Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(web): write a recording source's manifest copy alongside its plan and inventory

Richie McIlroy5c87529dcbd8
diff --git a/apps/web/__tests__/unit/desktop-recording-source.test.ts b/apps/web/__tests__/unit/desktop-recording-source.test.ts
index 15e7cbf0d..e7e3b6a0a 100644
--- a/apps/web/__tests__/unit/desktop-recording-source.test.ts
+++ b/apps/web/__tests__/unit/desktop-recording-source.test.ts
@@ -1409,6 +1409,67 @@ describe("durable desktop recording source commit", () => {
),
).toBe(false);
});
+ it.each([
+ ["plan", "/plans/", "/plan.json"],
+ ["finalize", "/commits/", "/inventory.json"],
+ ])(
+ "writes the %s manifest copy alongside its sibling file",
+ async (_phase, directory, sibling) => {
+ const storage = storageFixture("googleDrive");
+ seedSegments(storage);
+ const put = storage.bucket.putObject.getMockImplementation();
+ if (!put) throw new Error("Missing fixture operation");
+ let manifestPending = false;
+ let overlapped = false;
+ let siblingStarted: () => void = () => {};
+ const siblingWrite = new Promise<void>((resolve) => {
+ siblingStarted = resolve;
+ });
+ storage.bucket.putObject.mockImplementation((key, body) => {
+ if (key.includes(directory) && key.endsWith(sibling)) {
+ if (manifestPending) overlapped = true;
+ siblingStarted();
+ }
+ if (key.includes(directory) && key.endsWith("/manifest.json")) {
+ manifestPending = true;
+ return Effect.promise(() =>
+ Promise.race([
+ siblingWrite,
+ new Promise<void>((resolve) => setTimeout(resolve, 1_000)),
+ ]),
+ ).pipe(
+ Effect.zipRight(put(key, body)),
+ Effect.ensuring(
+ Effect.sync(() => {
+ manifestPending = false;
+ }),
+ ),
+ );
+ }
+ return put(key, body);
+ });
+ await commitDesktopRecordingSource(recording(), generation);
+ expect(overlapped).toBe(true);
+ },
+ );
+
+ it.each(["/plans/", "/commits/"])(
+ "does not commit when the manifest copy under %s fails",
+ async (directory) => {
+ const storage = storageFixture("googleDrive");
+ seedSegments(storage);
+ const put = storage.bucket.putObject.getMockImplementation();
+ if (!put) throw new Error("Missing fixture operation");
+ storage.bucket.putObject.mockImplementation((key, body) =>
+ key.includes(directory) && key.endsWith("/manifest.json")
+ ? Effect.fail(new Error("Manifest copy unavailable"))
+ : put(key, body),
+ );
+ await expect(
+ commitDesktopRecordingSource(recording(), generation),
+ ).rejects.toThrow();
+ },
+ );
});
describe("large desktop recording snapshots", () => {
diff --git a/apps/web/lib/desktop-recording-source.ts b/apps/web/lib/desktop-recording-source.ts
index 423176f39..f3b4d3d86 100644
--- a/apps/web/lib/desktop-recording-source.ts
+++ b/apps/web/lib/desktop-recording-source.ts
@@ -616,18 +616,11 @@ async function createSourcePlan(
"The recording source inventory exceeds the supported object count",
);
const manifestKey = `${directory}/manifest.json`;
- await writeSourceText(context, manifestKey, content);
- plan = {
- version: 1,
- kind: "segments",
- manifestSha256: manifest.manifestSha256,
- manifestKey,
- originalManifestKey,
- requiredAudio: manifest.hasAudio,
- videoCount,
- audioCount,
- objectCount: videoCount + audioCount,
- sourcePreparation: await context
+ // The plan is only referenced once both files are durable, so the manifest copy can be written
+ // alongside the plan; a failure leaves both unreferenced in this plan's own directory.
+ const [, planReference] = await Promise.all([
+ writeSourceText(context, manifestKey, content),
+ context
.run(
context.bucket
.getObject(preparationMarkerKey(context.video))
@@ -640,8 +633,27 @@ async function createSourcePlan(
.object({ version: z.literal(1) })
.safeParse(JSON.parse(marker.value)).success,
)
- .catch(() => false),
- };
+ .catch(() => false)
+ .then((sourcePreparation) =>
+ writeSourceText(
+ context,
+ `${directory}/plan.json`,
+ JSON.stringify({
+ version: 1,
+ kind: "segments",
+ manifestSha256: manifest.manifestSha256,
+ manifestKey,
+ originalManifestKey,
+ requiredAudio: manifest.hasAudio,
+ videoCount,
+ audioCount,
+ objectCount: videoCount + audioCount,
+ sourcePreparation,
+ } satisfies SourcePlan),
+ ),
+ ),
+ ]);
+ return planReference;
} else {
const video = context.video;
const publishedKey = getPublishedRecordingOutputKey(video);
@@ -1417,6 +1429,7 @@ async function advanceSourceSnapshot(
let inventoryKey: string;
let inventory: unknown;
let mp4: DesktopRecordingSource["mp4"];
+ let manifestCopy: Promise<unknown> | undefined;
if (plan.kind === "mp4") {
const [object] = await readTree(
context,
@@ -1451,7 +1464,6 @@ async function advanceSourceSnapshot(
"Recording manifest changed while its durable snapshot was being saved",
);
const directory = `${context.prefix}/commits/${randomUUID()}`;
- await writeSourceText(context, `${directory}/manifest.json`, saved);
inventoryKey = `${directory}/inventory.json`;
inventory = {
version: 2,
@@ -1461,12 +1473,17 @@ async function advanceSourceSnapshot(
scope,
roots: checkpoint.receiptRoots,
};
+ // The commit directory is new and only referenced once both writes are durable.
+ manifestCopy = writeSourceText(
+ context,
+ `${directory}/manifest.json`,
+ saved,
+ );
}
- const saved = await writeSourceText(
- context,
- inventoryKey,
- JSON.stringify(inventory),
- );
+ const [saved] = await Promise.all([
+ writeSourceText(context, inventoryKey, JSON.stringify(inventory)),
+ manifestCopy,
+ ]);
return {
source: desktopRecordingSourceSchema.parse({
version: 1,