Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: reuse retained transcodes without requiring raw uploads

Richie McIlroyeb544f4d5176
diff --git a/apps/render-farm/src/coordinator.test.ts b/apps/render-farm/src/coordinator.test.ts
index 5850941f4..bfc822c07 100644
--- a/apps/render-farm/src/coordinator.test.ts
+++ b/apps/render-farm/src/coordinator.test.ts
@@ -6,6 +6,7 @@ import {
timingSafeEqual,
} from "node:crypto";
import { readFileSync } from "node:fs";
+import { ANNEX_B_PARAMETER_SETS } from "./boxes.test-util";
import type { Job, TaskState } from "./coordinator";
import * as fmp4 from "./fmp4";
import * as hls from "./hls";
@@ -702,6 +703,54 @@ describe("transcodes", () => {
});
});
+ test("a fresh index can reuse a retained transcode after its raw upload is removed", async () => {
+ const h = harness();
+ const prefix = "owner/video/project";
+ const header = mp4.buildHeader({
+ width: 128,
+ height: 72,
+ fps: 30,
+ video: {
+ sizes: Uint32Array.of(1),
+ runs: [{ first: 0, count: 1, offset: 0 }],
+ keyframes: Uint32Array.of(0),
+ avcC: mp4.avcC(ANNEX_B_PARAMETER_SETS),
+ },
+ audio: null,
+ payloadSize: 1,
+ minimumSize: 0,
+ });
+ const output = new Uint8Array(header.byteLength + 1);
+ output.set(header);
+ h.objects.set(request.output, output);
+ h.objects.set(
+ `${prefix}/recording-meta.json`,
+ new TextEncoder().encode("{}"),
+ );
+ h.objects.set(
+ `${prefix}/manifest.json`,
+ new TextEncoder().encode(
+ JSON.stringify({
+ files: [
+ { path: "recording-meta.json", size: 2 },
+ {
+ path: "display.mp4",
+ key: request.output,
+ transcodeFrom: request.source,
+ },
+ ],
+ }),
+ ),
+ );
+ await expect(
+ h.sourceIndex(prefix, request.sourceRoot),
+ ).resolves.toBeDefined();
+ expect(await (await call(h, "/transcodes", request)).json()).toMatchObject({
+ status: "ready",
+ size: output.byteLength,
+ });
+ });
+
test("standalone transcodes reject missing and oversized raw sources before dispatch", async () => {
const h = harness({ RF_MAX_SOURCE_BYTES: "1000" });
for (const size of [0, 1001]) {
diff --git a/apps/render-farm/src/coordinator.ts b/apps/render-farm/src/coordinator.ts
index 13a3ccef8..495aeb437 100644
--- a/apps/render-farm/src/coordinator.ts
+++ b/apps/render-farm/src/coordinator.ts
@@ -553,7 +553,8 @@ async function sourceIndex(
size:
file.transcodeFrom === undefined
? file.size
- : await transcodeSourceSize(file.transcodeFrom),
+ : ((await storedTranscodeSize(keyOf(file))) ??
+ (await transcodeSourceSize(file.transcodeFrom))),
})),
);
const sourceBounds = checkManifestBounds(
@@ -1429,6 +1430,19 @@ type Transcode = {
/** By output key: one transcode per target, shared by every job needing it. */
const transcodes = new Map<string, Transcode>();
+async function storedTranscodeSize(output: string) {
+ const head = await s3.head(output);
+ if (!head) return null;
+ if (
+ !Number.isSafeInteger(head.size) ||
+ head.size <= 0 ||
+ head.size > SOURCE_LIMITS.sourceBytes
+ ) {
+ throw new Error("stored transcode output has an invalid size");
+ }
+ return head.size;
+}
+
async function transcodeSourceSize(source: string) {
const head = await s3.head(source);
if (!head || !Number.isSafeInteger(head.size) || head.size <= 0) {
@@ -1465,11 +1479,9 @@ async function ensureTranscode(source: string, output: string) {
};
transcodes.set(output, transcode);
try {
- const head = await s3.head(output);
- if (head && head.size > 0 && head.size <= SOURCE_LIMITS.sourceBytes) {
- settleTranscode(transcode, "ready", head.size);
- } else if (head) {
- throw new Error("stored transcode output has an invalid size");
+ const size = await storedTranscodeSize(output);
+ if (size !== null) {
+ settleTranscode(transcode, "ready", size);
} else {
await transcodeSourceSize(source);
transcode.state = "queued";