From cf75dfd7af51ee6c690313874fb7dbe297fda184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 7 Nov 2026 08:55:14 +0000 Subject: [PATCH] fix(studio-server): a video with a codec nobody listed gets a playable preview copy (#7356) * fix(studio-server): a video with a codec nobody listed gets a playable preview copy * refactor(studio-server): drop a comment the codec names already carry --- .../src/helpers/mediaCodecMap.test.ts | 43 +++++++++++++------ .../src/helpers/mediaCodecMap.ts | 13 +++--- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/packages/studio-server/src/helpers/mediaCodecMap.test.ts b/packages/studio-server/src/helpers/mediaCodecMap.test.ts index ec7eaed442..c16d2691d4 100644 --- a/packages/studio-server/src/helpers/mediaCodecMap.test.ts +++ b/packages/studio-server/src/helpers/mediaCodecMap.test.ts @@ -269,24 +269,43 @@ describe("shouldPrewarmProxy", () => { ).toBe(false); }); - // `shouldPrewarmProxy` alone cannot catch a missing `Object.hasOwn` guard — - // `.prewarm === true` is already strict against `Object.prototype.constructor`. - // The input that misbehaves goes through `codecFactsFor`, which would report - // an ordinary asset as browser-hostile. - it("does not treat a codec named after an Object.prototype key as hostile", async () => { + it("treats a codec nobody listed as browser-hostile, so its file gets a playable copy", async () => { + const project = tmpProject(); + const videoPath = join(project, "clip.mp4"); + writeFileSync(videoPath, "fake video bytes"); + + const facts = await probeAssetCodec(videoPath, makeRunner({ [videoPath]: "wmv3" })); + + expect(facts).toEqual({ + codecName: "wmv3", + browserHostile: true, + representativeMime: null, + hasAlpha: false, + }); + expect(shouldPrewarmProxy(facts!)).toBe(true); + expect(decideMediaProxyEligibility(facts)).toEqual({ eligible: true }); + }); + + it.each(["h264", "vp8", "vp9", "av1"])("does not pre-warm %s", async (codecName) => { + const project = tmpProject(); + const videoPath = join(project, "clip.mp4"); + writeFileSync(videoPath, "fake video bytes"); + + const facts = await probeAssetCodec(videoPath, makeRunner({ [videoPath]: codecName })); + + expect(shouldPrewarmProxy(facts!)).toBe(false); + }); + + // A codec named after an Object.prototype key must not resolve against it: it is an unlisted codec like any other. + it("reads a codec named after an Object.prototype key as an unlisted codec", async () => { const project = tmpProject(); const videoPath = join(project, "clip.mp4"); writeFileSync(videoPath, "fake video bytes"); const facts = await probeAssetCodec(videoPath, makeRunner({ [videoPath]: "constructor" })); - expect(facts).toEqual({ - codecName: "constructor", - browserHostile: false, - representativeMime: null, - hasAlpha: false, - }); - expect(shouldPrewarmProxy(facts!)).toBe(false); + expect(facts?.browserHostile).toBe(true); + expect(facts?.representativeMime).toBeNull(); }); }); diff --git a/packages/studio-server/src/helpers/mediaCodecMap.ts b/packages/studio-server/src/helpers/mediaCodecMap.ts index 9b6f761f78..2825a3e0ae 100644 --- a/packages/studio-server/src/helpers/mediaCodecMap.ts +++ b/packages/studio-server/src/helpers/mediaCodecMap.ts @@ -55,8 +55,8 @@ export interface BrowserHostileCodec { } /** - * Browser-hostile codec table v1. One exported constant so extending it is a - * one-line change. `ffprobe` cannot emit exact RFC 6381 codec strings, so + * Browser-hostile codec table v1: the codecs with a known answer; one not here and not in + * `BROWSER_PLAYABLE_CODECS` is hostile too. `ffprobe` cannot emit exact RFC 6381 codec strings, so * these `representativeMime` values are deliberately coarse (a false * positive costs one proxy transcode, never correctness; a false negative is * rescued by the runtime's reactive zero-videoWidth swap). @@ -78,12 +78,15 @@ export const BROWSER_HOSTILE_CODECS: Record = { vp9: { representativeMime: 'video/webm; codecs="vp09.00.10.08"', prewarm: false }, }; +const BROWSER_PLAYABLE_CODECS: ReadonlySet = new Set(["h264", "vp8"]); + +const UNLISTED_HOSTILE: BrowserHostileCodec = { representativeMime: null, prewarm: true }; + /** `Object.hasOwn` rather than a bare index: a codec named `constructor` or * `toString` would otherwise resolve against `Object.prototype`. */ function hostileCodecEntry(codecName: string): BrowserHostileCodec | undefined { - return Object.hasOwn(BROWSER_HOSTILE_CODECS, codecName) - ? BROWSER_HOSTILE_CODECS[codecName] - : undefined; + if (Object.hasOwn(BROWSER_HOSTILE_CODECS, codecName)) return BROWSER_HOSTILE_CODECS[codecName]; + return BROWSER_PLAYABLE_CODECS.has(codecName) ? undefined : UNLISTED_HOSTILE; } /** The pre-warm gate: true only for codecs with no cross-platform browser