Files
hyperframes/packages/player/src/runtime-message-handler.ts
T
xiayeandClaude Opus 4.7 5a0966dfeb fix(runtime,player): replay bridge state on iframe ready to repair race
The audio-locked attribute was correctly setting `muted = true` and posting
`set-muted` to the iframe runtime, but on warm-cache reloads of claude.ai
and inside the Claude desktop Electron client, the iframe finishes loading
*after* the parent has already sent control messages — the iframe runtime's
postMessage listener isn't installed yet, so the messages are silently
dropped. Audio plays unmuted with no UI to recover.

Confirmed via:
- "First open" on claude.ai: cold cache, iframe slow → listener up before
  `set-muted` lands → audio muted 
- "Hard refresh" on claude.ai: warm cache, iframe fast → listener up after
  message arrives → message lost → audio plays 
- Claude desktop: Electron renderer consistently fast → race always loses
  → audio plays 

Fix: add a `{source: "hf-preview", type: "ready"}` event the runtime emits
once `installRuntimeControlBridge` has registered the listener. The player
listens for it and replays current bridge state (`set-muted`, `set-volume`,
`set-playback-rate`). Pre-ready messages are now safe to send — they'll be
replayed once the runtime can receive them.

The replay is idempotent — re-asserting defaults is a no-op — so it's also
safe across iframe reloads (new runtime instance emits ready again).

Tests: 6 new (1 bridge: ready posted on install; 5 player: replays muted /
volume / playback-rate / audio-locked-forced-mute / handles second ready /
ignores ready from wrong source). Suites green: core 1387, player 137.

Refs:
- Investigation: heygen-com/hyperframes#1300 (UA-fallback attempt — unrelated
  to actual root cause)
- claude.ai-web.log analysis revealed cross-origin iframe + race condition,
  not attribute stripping as originally hypothesized

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-09 12:22:27 -07:00

104 lines
3.3 KiB
TypeScript

/**
* Routes postMessages from the composition iframe to the appropriate handlers.
*
* Accepts the raw MessageEvent and delegates through typed callbacks so the
* web component keeps its state fields private and this module stays stateless.
*/
import {
applyRuntimeStateMessage,
type PlaybackState,
type PlaybackStateCallbacks,
} from "./playback-state.js";
import type { ShaderLoaderState } from "./shader-loader-state.js";
import type { ShaderTransitionState } from "./shader-options.js";
const FPS = 30;
export interface MessageHandlerCallbacks extends PlaybackStateCallbacks {
getPlaybackState: () => PlaybackState;
setPlaybackState: (next: PlaybackState) => void;
getShaderLoadingMode: () => string;
shaderLoader: ShaderLoaderState;
setCompositionSize: (width: number, height: number) => void;
sendControl: (action: string, extra?: Record<string, unknown>) => void;
getIframeDoc: () => Document | null;
/** Invoked when the iframe runtime posts `{type: "ready"}` — the player
* uses it to replay current bridge state (mute, volume, playback rate) so
* control messages sent before the iframe's listener registered aren't lost. */
onRuntimeReady: () => void;
}
export function handleRuntimeMessage(
event: MessageEvent,
frameWindow: Window | null,
callbacks: MessageHandlerCallbacks,
): void {
if (event.source !== frameWindow) return;
const data = event.data as Record<string, unknown> | undefined;
if (!data || data["source"] !== "hf-preview") return;
if (data["type"] === "shader-transition-state") {
const state: ShaderTransitionState =
data["state"] && typeof data["state"] === "object"
? (data["state"] as ShaderTransitionState)
: {};
callbacks.shaderLoader.update(state, callbacks.getShaderLoadingMode());
callbacks.dispatchEvent(
new CustomEvent("shadertransitionstate", {
detail: { compositionId: data["compositionId"], state },
}),
);
return;
}
if (data["type"] === "ready") {
callbacks.onRuntimeReady();
return;
}
if (data["type"] === "state") {
callbacks.setPlaybackState(
applyRuntimeStateMessage(
{ frame: (data["frame"] as number) ?? 0, isPlaying: !!data["isPlaying"] },
FPS,
callbacks.getPlaybackState(),
callbacks,
),
);
return;
}
if (data["type"] === "media-autoplay-blocked") {
let iframeDoc: Document | null = null;
try {
iframeDoc = callbacks.getIframeDoc();
} catch {
/* cross-origin */
}
callbacks.media.promoteToParentProxy(iframeDoc, (t, opts) =>
callbacks.media.mirrorTime(t, opts),
);
callbacks.sendControl("set-media-output-muted", { muted: true });
return;
}
if (data["type"] === "timeline" && (data["durationInFrames"] as number) > 0) {
if (Number.isFinite(data["durationInFrames"])) {
const pb = callbacks.getPlaybackState();
const duration = (data["durationInFrames"] as number) / FPS;
callbacks.setPlaybackState({ ...pb, duration });
callbacks.updateControlsTime(pb.currentTime, duration);
}
return;
}
if (
data["type"] === "stage-size" &&
(data["width"] as number) > 0 &&
(data["height"] as number) > 0
) {
callbacks.setCompositionSize(data["width"] as number, data["height"] as number);
}
}