From adfddff16e30418f0c749f4b6fc932a23fa93d22 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Mon, 17 Aug 2026 16:55:28 -0400 Subject: [PATCH] fix(audio): let the volume probe run where there is no DOM The unclamped-volume shim reached for `HTMLMediaElement.prototype` before checking that a DOM exists. The renderer's probe body is serialized into the page in production but executed directly by tests that stand in for a Page, so under the bun lane it threw `ReferenceError: HTMLMediaElement is not defined` instead of falling back. Both copies now degrade to the clamped path when the constructor is absent, which is the only correct answer off-browser. --- packages/core/src/audioGain.ts | 8 +++++++- packages/producer/src/services/htmlCompiler.ts | 11 +++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/core/src/audioGain.ts b/packages/core/src/audioGain.ts index cb19e7d30a..542890ca54 100644 --- a/packages/core/src/audioGain.ts +++ b/packages/core/src/audioGain.ts @@ -52,7 +52,13 @@ export function formatAudioGain(gain: number): string { * probe observes an out-of-range volume, and the shadow is removed afterwards. */ export function withUnclampedVolume(el: HTMLMediaElement, probe: () => T): T { - const descriptor = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "volume"); + // Guarded for non-DOM runtimes: the probe that calls this is also reachable + // from tests and tools that run outside a browser, where the clamped path is + // the right (and only) answer. + const descriptor = + typeof HTMLMediaElement === "undefined" + ? undefined + : Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "volume"); const nativeGet = descriptor?.get; const nativeSet = descriptor?.set; if (!nativeGet || !nativeSet) return probe(); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 164c546b71..a08b55b91d 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -2215,10 +2215,13 @@ export async function discoverAudioVolumeAutomationFromTimeline( // the clamped value. Mirrors `withUnclampedVolume` in // packages/core/src/audioGain.ts, which the preview probe uses; this copy // exists only because the probe body is serialized into the page. - const volumeDescriptor = Object.getOwnPropertyDescriptor( - HTMLMediaElement.prototype, - "volume", - ); + // Guarded because this body is also executed directly by tests that + // stand in for a Page, where there is no DOM and no HTMLMediaElement. + // Without the descriptor the probe simply keeps the clamped path. + const volumeDescriptor = + typeof HTMLMediaElement === "undefined" + ? undefined + : Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "volume"); const nativeVolumeGet = volumeDescriptor?.get; const nativeVolumeSet = volumeDescriptor?.set; const withUnclampedVolume = (el: HTMLMediaElement, probe: () => T): T => {