@hyperframes/studio 0.9.55 → 0.9.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/assets/{hyperframes-player-BroIQA4E.js → hyperframes-player-B78JDyI_.js} +1 -1
  2. package/dist/assets/{index-Be7DZynj.js → index-D7ICtobF.js} +190 -190
  3. package/dist/assets/{index-CiF0D62S.js → index-ww7-RmuM.js} +1 -1
  4. package/dist/assets/{pausedVideoDecoder-RXcvblHS.js → pausedVideoDecoder-Dbe8Cmab.js} +1 -1
  5. package/dist/{chunk-JMM2CSEL.js → chunk-5EEM5WO4.js} +5 -1
  6. package/dist/index.d.ts +87 -3
  7. package/dist/index.html +1 -1
  8. package/dist/index.js +872 -778
  9. package/dist/player.js +1 -1
  10. package/package.json +8 -8
  11. package/src/components/nle/NLEPreview.test.ts +35 -3
  12. package/src/components/nle/NLEPreview.tsx +5 -4
  13. package/src/hooks/gsapRuntimePatch.ts +2 -3
  14. package/src/hooks/gsapRuntimeReaders.test.ts +5 -40
  15. package/src/hooks/gsapRuntimeReaders.ts +3 -27
  16. package/src/hooks/useExternalFileChangeCoordinator.test.tsx +20 -0
  17. package/src/hooks/useExternalFileChangeCoordinator.ts +1 -1
  18. package/src/index.ts +17 -0
  19. package/src/player/components/Player.test.ts +10 -0
  20. package/src/player/components/Player.tsx +2 -2
  21. package/src/player/components/studioShortcuts.ts +3 -2
  22. package/src/player/hooks/previewMessageRouter.manifest.test.ts +1 -0
  23. package/src/player/hooks/previewMessageRouter.test.ts +90 -12
  24. package/src/player/hooks/previewMessageRouter.ts +6 -4
  25. package/src/player/hooks/timelinePlayerTestHarness.ts +2 -0
  26. package/src/player/hooks/timelineSyncHydration.test.ts +28 -2
  27. package/src/player/hooks/timelineSyncHydration.ts +3 -7
  28. package/src/player/hooks/usePlaybackKeyboard.test.ts +107 -2
  29. package/src/player/hooks/usePlaybackKeyboard.ts +38 -1
  30. package/src/player/hooks/useShadowPreviewReload.ts +131 -14
  31. package/src/player/hooks/useTimelinePlayer.metadata.test.ts +1 -0
  32. package/src/player/hooks/useTimelinePlayer.play.test.ts +1 -0
  33. package/src/player/hooks/useTimelinePlayer.reloadStill.test.ts +241 -0
  34. package/src/player/hooks/useTimelinePlayer.seek.test.ts +3 -0
  35. package/src/player/hooks/useTimelinePlayer.ts +13 -1
  36. package/src/player/hooks/useTimelineSyncCallbacks.ts +4 -0
  37. package/src/player/lib/bridgePlayback.ts +7 -1
  38. package/src/player/lib/previewChannel.test.ts +13 -0
  39. package/src/sealedPreviewExports.test.ts +20 -0
  40. package/src/utils/externalConflictStorage.test.ts +66 -14
  41. package/src/utils/externalConflictStorage.ts +36 -82
  42. package/src/utils/gsapSoftReload.ts +2 -14
package/dist/player.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Player
3
- } from "./chunk-JMM2CSEL.js";
3
+ } from "./chunk-5EEM5WO4.js";
4
4
  import "./chunk-G6FJSL7X.js";
5
5
  import "./chunk-OOGQ5KCC.js";
6
6
  import "./chunk-MLKGABMK.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperframes/studio",
3
- "version": "0.9.55",
3
+ "version": "0.9.58",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -64,12 +64,12 @@
64
64
  "dockview-react": "8.3.1",
65
65
  "gsap": "^3.13.0",
66
66
  "mediabunny": "^1.66.0",
67
- "@hyperframes/player": "0.9.55",
68
- "@hyperframes/core": "0.9.55",
69
- "@hyperframes/sdk": "0.9.55",
70
- "@hyperframes/shader-transitions": "0.9.55",
71
- "@hyperframes/parsers": "0.9.55",
72
- "@hyperframes/studio-server": "0.9.55"
67
+ "@hyperframes/parsers": "0.9.58",
68
+ "@hyperframes/player": "0.9.58",
69
+ "@hyperframes/sdk": "0.9.58",
70
+ "@hyperframes/shader-transitions": "0.9.58",
71
+ "@hyperframes/studio-server": "0.9.58",
72
+ "@hyperframes/core": "0.9.58"
73
73
  },
74
74
  "devDependencies": {
75
75
  "@tailwindcss/vite": "^4.3.3",
@@ -86,7 +86,7 @@
86
86
  "vite": "^6.4.5",
87
87
  "vitest": "^4.1.12",
88
88
  "zustand": "^5.0.0",
89
- "@hyperframes/producer": "0.9.55"
89
+ "@hyperframes/producer": "0.9.58"
90
90
  },
91
91
  "peerDependencies": {
92
92
  "react": "19",
@@ -56,10 +56,9 @@ class MockResizeObserver {
56
56
  constructor(cb: ResizeObserverCallback) {
57
57
  this.cb = cb;
58
58
  }
59
+ // A browser calls back at its next frame, never inside observe(); renderPreview fires them.
59
60
  observe() {
60
- const fire = () => this.cb([], this as unknown as ResizeObserver);
61
- resizeCallbacks.push(fire);
62
- fire();
61
+ resizeCallbacks.push(() => this.cb([], this as unknown as ResizeObserver));
63
62
  }
64
63
  disconnect() {}
65
64
  }
@@ -67,6 +66,8 @@ class MockResizeObserver {
67
66
  const originalResizeObserver = globalThis.ResizeObserver;
68
67
 
69
68
  function setRect(node: Element, rect: { width: number; height: number }) {
69
+ Object.defineProperty(node, "clientWidth", { configurable: true, value: rect.width });
70
+ Object.defineProperty(node, "clientHeight", { configurable: true, value: rect.height });
70
71
  Object.defineProperty(node, "getBoundingClientRect", {
71
72
  configurable: true,
72
73
  value: () => ({
@@ -493,6 +494,37 @@ describe("NLEPreview", () => {
493
494
  filled.cleanup();
494
495
  });
495
496
 
497
+ it("sizes the stage within the resize callback, so the frame painted after a resize is never at the old size", () => {
498
+ const view = renderPreview(undefined, { box: { width: 640, height: 360 }, fillBox: true });
499
+ setRect(view.viewport, { width: 1920, height: 1080 });
500
+ // Outside act: the browser paints right after this callback returns.
501
+ for (const fire of resizeCallbacks) fire();
502
+ expect([view.stage.style.width, view.stage.style.height]).toEqual(["1920px", "1080px"]);
503
+ view.cleanup();
504
+ });
505
+
506
+ it("sizes the stage from the box's own size while a host scales the box on screen", () => {
507
+ const view = renderPreview(undefined, { box: { width: 640, height: 360 }, fillBox: true });
508
+ setRect(view.viewport, { width: 1920, height: 1080 });
509
+ // A host growing the preview from a quarter of its size: on screen the box is 480 x 270 for now.
510
+ Object.defineProperty(view.viewport, "getBoundingClientRect", {
511
+ configurable: true,
512
+ value: () => ({
513
+ x: 0,
514
+ y: 0,
515
+ left: 0,
516
+ top: 0,
517
+ right: 480,
518
+ bottom: 270,
519
+ width: 480,
520
+ height: 270,
521
+ }),
522
+ });
523
+ for (const fire of resizeCallbacks) fire();
524
+ expect([view.stage.style.width, view.stage.style.height]).toEqual(["1920px", "1080px"]);
525
+ view.cleanup();
526
+ });
527
+
496
528
  it("takes the host's composition size as the stage's shape until the preview measures its own", () => {
497
529
  const box = { width: 800, height: 600 };
498
530
  const plain = renderPreview(undefined, { box });
@@ -11,6 +11,7 @@ import {
11
11
  useState,
12
12
  type RefObject,
13
13
  } from "react";
14
+ import { flushSync } from "react-dom";
14
15
  import { Player } from "../../player";
15
16
  import { postFrameDisplayScale } from "../../player/lib/runtimeProtocol";
16
17
  import type { NLEContextValue } from "./NLEContext";
@@ -148,11 +149,10 @@ export const NLEPreview = memo(function NLEPreview({
148
149
  if (!viewport) return;
149
150
 
150
151
  const updateStageSize = () => {
151
- const rect = viewport.getBoundingClientRect();
152
152
  setStageSize(
153
153
  resolvePreviewStageSize(
154
- rect.width,
155
- rect.height,
154
+ viewport.clientWidth,
155
+ viewport.clientHeight,
156
156
  compositionSize ??
157
157
  (hintWidth && hintHeight ? { width: hintWidth, height: hintHeight } : null),
158
158
  portrait,
@@ -163,7 +163,8 @@ export const NLEPreview = memo(function NLEPreview({
163
163
  };
164
164
 
165
165
  updateStageSize();
166
- const observer = new ResizeObserver(updateStageSize);
166
+ const resizeBeforePaint = () => flushSync(updateStageSize);
167
+ const observer = new ResizeObserver(resizeBeforePaint);
167
168
  observer.observe(viewport);
168
169
  return () => observer.disconnect();
169
170
  }, [compositionSize, hintWidth, hintHeight, portrait, gutterPx, insetPx]);
@@ -250,9 +250,8 @@ function changeTouchesOpacity(change: RuntimeTweenChange): boolean {
250
250
 
251
251
  /**
252
252
  * A tween re-initialization (invalidate, or kill+recreate for keyframe-rebuild)
253
- * captures opacity from the element's CURRENT inline style — for a color-graded
254
- * source (hidden with `opacity: 0 !important`) or a mid-flight tween that's a
255
- * runtime transient, not the authored value, and the capture makes it permanent.
253
+ * captures opacity from the element's CURRENT inline style — for a mid-flight
254
+ * tween that's a runtime transient, not the authored value, and the capture makes it permanent.
256
255
  * Restore the runtime's parse-time authored capture (data-hf-authored-opacity)
257
256
  * first; the re-seek after the patch re-renders the animated value anyway.
258
257
  * Duck-typed (no instanceof): the targets live in the preview iframe's realm.
@@ -2,10 +2,7 @@
2
2
  import { afterEach, describe, expect, it } from "vitest";
3
3
  import { gsap } from "gsap";
4
4
  import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
5
- import {
6
- COLOR_GRADING_SOURCE_HIDDEN_ATTR,
7
- HF_COLOR_GRADING_CANVAS_ID_PREFIX,
8
- } from "@hyperframes/core/color-grading";
5
+ import { COLOR_GRADING_SOURCE_HIDDEN_ATTR } from "@hyperframes/core/color-grading";
9
6
  import { readAllAnimatedProperties, readGsapProperty } from "./gsapRuntimeReaders";
10
7
 
11
8
  /**
@@ -68,46 +65,14 @@ describe("readAllAnimatedProperties group filter", () => {
68
65
  });
69
66
 
70
67
  describe("color-grading opacity truth", () => {
71
- function gradedElement(): HTMLElement {
68
+ it("reads a graded source's opacity from GSAP, as for any element", () => {
72
69
  const el = document.createElement("img");
73
70
  el.id = "clip";
74
- el.setAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR, "");
75
- const canvas = document.createElement("canvas");
76
- canvas.id = `${HF_COLOR_GRADING_CANVAS_ID_PREFIX}clip`;
77
- canvas.style.opacity = "0.98";
78
- document.body.append(el, canvas);
79
- return el;
80
- }
81
-
82
- it("resolves opacity from the grading canvas, not the runtime hide", () => {
83
- const el = gradedElement();
84
- const iframe = fakeIframe(el, { gsapValues: { opacity: 0 } });
85
- const anim = {
86
- id: "#clip-from-200-visual",
87
- targetSelector: "#clip",
88
- method: "from",
89
- properties: { opacity: 0.5 },
90
- } as unknown as GsapAnimation;
91
-
92
- const result = readAllAnimatedProperties(iframe, "#clip", anim);
93
-
94
- expect(result.opacity).toBe(0.98);
95
- });
96
-
97
- it("readGsapProperty takes the same detour", () => {
98
- const el = gradedElement();
99
- const iframe = fakeIframe(el, { gsapValues: { opacity: 0 } });
100
-
101
- expect(readGsapProperty(iframe, "#clip", "opacity")).toBe(0.98);
102
- });
103
-
104
- it("reads GSAP directly when the source is not grading-hidden", () => {
105
- const el = document.createElement("div");
106
- el.id = "clip";
71
+ el.setAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR, "true");
107
72
  document.body.appendChild(el);
108
- const iframe = fakeIframe(el, { gsapValues: { opacity: 0.3 } });
73
+ const iframe = fakeIframe(el, { gsapValues: { opacity: 0.98 } });
109
74
 
110
- expect(readGsapProperty(iframe, "#clip", "opacity")).toBe(0.3);
75
+ expect(readGsapProperty(iframe, "#clip", "opacity")).toBe(0.98);
111
76
  });
112
77
  });
113
78
 
@@ -3,34 +3,10 @@
3
3
  */
4
4
  import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
5
5
  import { classifyPropertyGroup, type PropertyGroupName } from "@hyperframes/core/gsap-parser";
6
- import {
7
- COLOR_GRADING_SOURCE_HIDDEN_ATTR,
8
- HF_COLOR_GRADING_CANVAS_ID_PREFIX,
9
- } from "@hyperframes/core/color-grading";
10
- import { getIframeGsap, queryIframeElement, type IframeGsap } from "./gsapShared";
6
+ import { getIframeGsap, queryIframeElement } from "./gsapShared";
11
7
  import { gsapWritesChannels } from "./gsapRuntimeKeyframes";
12
8
  import { roundTo3 } from "../utils/rounding";
13
9
 
14
- /**
15
- * The element's live value for `prop` as GSAP drives it. Opacity on a
16
- * color-grading-hidden source needs a detour: the runtime hides the source
17
- * with inline `opacity: 0 !important`, so computed opacity is the hide, not
18
- * the animated value. The grading canvas mirrors the source's effective
19
- * opacity every frame, so it is the truth for that one property — reading the
20
- * raw 0 here is what bakes `opacity: 0` into committed keyframes.
21
- */
22
- function readLiveGsapValue(gsap: IframeGsap, el: Element, prop: string): number {
23
- if (prop === "opacity" && el.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR) != null && el.id) {
24
- const canvas = el.ownerDocument.getElementById(HF_COLOR_GRADING_CANVAS_ID_PREFIX + el.id);
25
- const win = el.ownerDocument.defaultView;
26
- if (canvas && win) {
27
- const val = Number(win.getComputedStyle(canvas).opacity);
28
- if (Number.isFinite(val)) return val;
29
- }
30
- }
31
- return Number(gsap.getProperty(el, prop));
32
- }
33
-
34
10
  export function readGsapProperty(
35
11
  iframe: HTMLIFrameElement | null,
36
12
  selector: string | null,
@@ -42,7 +18,7 @@ export function readGsapProperty(
42
18
  const el = queryIframeElement(iframe, selector);
43
19
  if (!el) return null;
44
20
  try {
45
- const val = readLiveGsapValue(gsap, el, prop);
21
+ const val = Number(gsap.getProperty(el, prop));
46
22
  if (!Number.isFinite(val)) return null;
47
23
  return POSITION_PROPS.has(prop) ? Math.round(val) : roundTo3(val);
48
24
  } catch {
@@ -89,7 +65,7 @@ export function readAllAnimatedProperties(
89
65
  const groupedPropKeys = new Set([...propKeys].filter(inGroup));
90
66
 
91
67
  for (const prop of groupedPropKeys) {
92
- const val = readLiveGsapValue(gsap, el, prop);
68
+ const val = Number(gsap.getProperty(el, prop));
93
69
  if (Number.isFinite(val)) {
94
70
  result[prop] = POSITION_PROPS.has(prop) ? Math.round(val) : roundTo3(val);
95
71
  }
@@ -837,6 +837,26 @@ describe("external file change coordinator", () => {
837
837
  });
838
838
  });
839
839
 
840
+ // Studio settles which composition is open just after load, while the draft's file stays the same.
841
+ it("keeps a restored draft when the open composition changes but the draft's file does not", async () => {
842
+ const { captured, rerender } = await mountCoordinator({
843
+ recoveryFilePath: "index.html",
844
+ loadConflictSnapshot: vi.fn(async () => ({
845
+ kind: "conflict" as const,
846
+ projectId: "project-a",
847
+ filePath: "index.html",
848
+ externalVersion: "v2",
849
+ externalContent: "external",
850
+ studioContent: "studio",
851
+ createdAt: 100,
852
+ })),
853
+ });
854
+ await vi.waitFor(() => expect(captured.handle?.blocked?.status).toBe("conflict"));
855
+
856
+ await rerender({ activeCompPath: null });
857
+ await vi.waitFor(() => expect(captured.handle?.blocked?.status).toBe("conflict"));
858
+ });
859
+
840
860
  it("retains the final local candidate when a drain fails", async () => {
841
861
  const failure = new Error("network unavailable");
842
862
  const persistFailureSnapshot = vi.fn(async () => undefined);
@@ -248,7 +248,7 @@ export function useExternalFileChangeCoordinator({
248
248
  return () => {
249
249
  cancelled = true;
250
250
  };
251
- }, [loadConflictSnapshot, projectId, recoveryFilePath, setBlocked]);
251
+ }, [loadConflictSnapshot, projectId, activeCompPath, recoveryFilePath, setBlocked]);
252
252
 
253
253
  // What a settled change owes: thumbnails (unless its own write's echo brings them), then reloads.
254
254
  const deliverOwed = useCallback(
package/src/index.ts CHANGED
@@ -46,6 +46,23 @@ export {
46
46
  } from "./player";
47
47
  export { usePreviewIframeStore } from "./player/store/previewIframeStore";
48
48
  export { previewEditingClient, previewFrameOpaque } from "./player/lib/previewChannel";
49
+ export {
50
+ describePreviewElements,
51
+ previewAncestors,
52
+ previewElementsAtPoint,
53
+ previewGeometry,
54
+ previewLayers,
55
+ previewPointerTargets,
56
+ queryPreviewElements,
57
+ type PreviewElement,
58
+ type PreviewElementHit,
59
+ } from "./player/lib/previewElements";
60
+ export {
61
+ findPreviewElementForSelection,
62
+ findPreviewElementForTimelineElement,
63
+ readable,
64
+ standInFor,
65
+ } from "./components/editor/previewSelection";
49
66
  export { PREVIEW_QUALITY_LEVELS, type PreviewQuality } from "@hyperframes/core/preview-quality";
50
67
  export type {
51
68
  PlaybackHealth,
@@ -18,12 +18,14 @@ Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
18
18
 
19
19
  let root: Root | null = null;
20
20
  let lifecycleLog: string[] = [];
21
+ const playersMade: HTMLElement[] = [];
21
22
 
22
23
  class TestHyperframesPlayer extends HTMLElement {
23
24
  readonly iframeElement = document.createElement("iframe");
24
25
 
25
26
  constructor() {
26
27
  super();
28
+ playersMade.push(this);
27
29
  this.attachShadow({ mode: "open" });
28
30
 
29
31
  const addIframeListener = this.iframeElement.addEventListener.bind(this.iframeElement);
@@ -243,6 +245,14 @@ describe("a host's sandbox origin", () => {
243
245
  expect(player.getAttribute("src")).toBe("/api/projects/demo/preview");
244
246
  });
245
247
 
248
+ it("keeps the seal a host puts on the player from its ref", async () => {
249
+ const seal = (iframe: HTMLIFrameElement | null) =>
250
+ iframe && playersMade.at(-1)?.setAttribute("sandbox-origin", "opaque");
251
+ const { player } = await mountPlayer({ ref: seal });
252
+ await settle();
253
+ expect(player.getAttribute("sandbox-origin")).toBe("opaque");
254
+ });
255
+
246
256
  it("leaves a same-origin preview on its plain path without asking for a token", async () => {
247
257
  const fetch = vi.spyOn(globalThis, "fetch");
248
258
  const { player } = await mountPlayer();
@@ -341,7 +341,8 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
341
341
  player.addEventListener("painted", handlePainted);
342
342
  player.addEventListener("error", handleError);
343
343
 
344
- // Bridge the inner iframe to the forwarded ref for useTimelinePlayer.
344
+ // Sealed before the ref (useTimelinePlayer's bridge) sees the frame, never unsealed: a host may seal it too.
345
+ if (opaque) player.toggleAttribute("sandbox-origin", true);
345
346
  if (typeof ref === "function") {
346
347
  ref(iframe);
347
348
  } else if (ref) {
@@ -352,7 +353,6 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
352
353
  player.setAttribute("shader-loading", "player");
353
354
  player.setAttribute("low-power-idle", "");
354
355
  player.toggleAttribute("no-load-timeout", noLoadTimeout === true);
355
- player.toggleAttribute("sandbox-origin", opaque);
356
356
  player.setAttribute("width", String(portrait ? 1080 : 1920));
357
357
  player.setAttribute("height", String(portrait ? 1920 : 1080));
358
358
  player.style.width = "100%";
@@ -86,12 +86,13 @@ export function setStudioKeys(overrides: Partial<Record<StudioKeyAction, string>
86
86
  export const getStudioKeys = (): StudioKeys => current;
87
87
  export const getStudioKey = (action: StudioKeyAction): string => current[action];
88
88
 
89
- function subscribe(listener: () => void) {
89
+ export function subscribeStudioKeys(listener: () => void) {
90
90
  listeners.add(listener);
91
91
  return () => listeners.delete(listener);
92
92
  }
93
93
 
94
- export const useStudioKeys = (): StudioKeys => useSyncExternalStore(subscribe, getStudioKeys);
94
+ export const useStudioKeys = (): StudioKeys =>
95
+ useSyncExternalStore(subscribeStudioKeys, getStudioKeys);
95
96
 
96
97
  export function useShortcutSections(host?: readonly ShortcutSection[]): readonly ShortcutSection[] {
97
98
  const keys = useStudioKeys();
@@ -41,6 +41,7 @@ function routeStateTicks(opaque: boolean) {
41
41
  handle(
42
42
  new MessageEvent("message", {
43
43
  source: frame as unknown as Window,
44
+ origin: window.location.origin,
44
45
  data: { source: "hf-preview", type: "state", isPlaying: false },
45
46
  }),
46
47
  );
@@ -1,18 +1,34 @@
1
+ // @vitest-environment happy-dom
2
+
1
3
  import { describe, expect, it, vi } from "vitest";
2
4
  import { usePlayerStore } from "../store/playerStore";
3
5
  import { createPreviewMessageHandler } from "./previewMessageRouter";
6
+ import { detachedFakeIframe } from "./timelinePlayerTestHarness";
7
+
8
+ /** A preview frame and a post from its window, from the origin it loads at. */
9
+ function previewFrame(frame: Record<string, unknown> = {}) {
10
+ const iframe = detachedFakeIframe(frame);
11
+ const post = (data: unknown) =>
12
+ new MessageEvent("message", {
13
+ source: frame as unknown as Window,
14
+ origin: window.location.origin,
15
+ data,
16
+ });
17
+ return { iframe, post };
18
+ }
4
19
 
5
20
  function route(isPlaying: boolean) {
6
21
  const enrich = vi.fn();
22
+ const { iframe, post } = previewFrame();
7
23
  const handle = createPreviewMessageHandler({
8
- iframeRef: { current: null },
24
+ iframeRef: { current: iframe },
9
25
  processTimelineMessageRef: { current: vi.fn() },
10
26
  enrichMissingCompositionsRef: { current: enrich },
11
27
  lastTimelineMessageRef: { current: 0 },
12
28
  getAdapter: () => null,
13
29
  syncTimelineElements: vi.fn(),
14
30
  });
15
- handle(new MessageEvent("message", { data: { source: "hf-preview", type: "state", isPlaying } }));
31
+ handle(post({ source: "hf-preview", type: "state", isPlaying }));
16
32
  return enrich;
17
33
  }
18
34
 
@@ -29,20 +45,16 @@ describe("preview state messages while a scrub posts them", () => {
29
45
  try {
30
46
  vi.setSystemTime(10_000);
31
47
  const enrich = vi.fn();
48
+ const { iframe, post } = previewFrame();
32
49
  const handle = createPreviewMessageHandler({
33
- iframeRef: { current: null },
50
+ iframeRef: { current: iframe },
34
51
  processTimelineMessageRef: { current: vi.fn() },
35
52
  enrichMissingCompositionsRef: { current: enrich },
36
53
  lastTimelineMessageRef: { current: 0 },
37
54
  getAdapter: () => null,
38
55
  syncTimelineElements: vi.fn(),
39
56
  });
40
- const state = () =>
41
- handle(
42
- new MessageEvent("message", {
43
- data: { source: "hf-preview", type: "state", isPlaying: false },
44
- }),
45
- );
57
+ const state = () => handle(post({ source: "hf-preview", type: "state", isPlaying: false }));
46
58
  state();
47
59
  vi.advanceTimersByTime(100);
48
60
  state();
@@ -58,8 +70,8 @@ describe("preview state messages while a scrub posts them", () => {
58
70
  });
59
71
 
60
72
  describe("preview quality messages", () => {
61
- function routeMessage(data: unknown, frame?: { postMessage: (message: unknown) => void }) {
62
- const iframe = frame ? ({ contentWindow: frame } as unknown as HTMLIFrameElement) : null;
73
+ function routeMessage(data: unknown, frame: Record<string, unknown> = {}) {
74
+ const { iframe, post } = previewFrame(frame);
63
75
  createPreviewMessageHandler({
64
76
  iframeRef: { current: iframe },
65
77
  processTimelineMessageRef: { current: vi.fn() },
@@ -67,7 +79,7 @@ describe("preview quality messages", () => {
67
79
  lastTimelineMessageRef: { current: 0 },
68
80
  getAdapter: () => null,
69
81
  syncTimelineElements: vi.fn(),
70
- })(new MessageEvent("message", { data }));
82
+ })(post(data));
71
83
  }
72
84
 
73
85
  it("keeps what the preview says about its lighter copies, and clears it when it says none", () => {
@@ -182,3 +194,69 @@ describe("preview quality messages", () => {
182
194
  usePlayerStore.getState().setPreviewQuality(1);
183
195
  });
184
196
  });
197
+
198
+ describe("messages that do not come from the preview frame", () => {
199
+ const facts = {
200
+ sceneCount: 1,
201
+ mountedElementCount: 1,
202
+ durationSeconds: 1,
203
+ videos: [],
204
+ lighterAt: [],
205
+ copiesReady: true,
206
+ };
207
+ const report = { source: "hf-preview", type: "preview-facts", facts };
208
+
209
+ function deliver(iframe: HTMLIFrameElement | null, event: MessageEvent) {
210
+ usePlayerStore.getState().setPreviewFacts(null);
211
+ createPreviewMessageHandler({
212
+ iframeRef: { current: iframe },
213
+ processTimelineMessageRef: { current: vi.fn() },
214
+ enrichMissingCompositionsRef: { current: vi.fn() },
215
+ lastTimelineMessageRef: { current: 0 },
216
+ getAdapter: () => null,
217
+ syncTimelineElements: vi.fn(),
218
+ })(event);
219
+ return usePlayerStore.getState().previewFacts;
220
+ }
221
+
222
+ const from = (source: unknown, origin: string) =>
223
+ new MessageEvent("message", {
224
+ source: source as Window,
225
+ origin,
226
+ data: report,
227
+ });
228
+
229
+ it("ignores a report with no sender window", () => {
230
+ const { iframe } = previewFrame();
231
+ expect(deliver(iframe, from(null, window.location.origin))).toBeNull();
232
+ });
233
+
234
+ it("ignores a report that arrives before the preview frame exists", () => {
235
+ expect(deliver(null, from({}, window.location.origin))).toBeNull();
236
+ });
237
+
238
+ it("ignores a report from a window that is not the preview frame", () => {
239
+ const { iframe } = previewFrame();
240
+ expect(deliver(iframe, from({}, window.location.origin))).toBeNull();
241
+ });
242
+
243
+ it("ignores a report from the preview frame's window at an origin that frame cannot have", () => {
244
+ const frame = {};
245
+ const { iframe } = previewFrame(frame);
246
+ expect(deliver(iframe, from(frame, "https://another.example"))).toBeNull();
247
+ });
248
+
249
+ it("accepts a sealed preview frame's report only from its opaque origin", () => {
250
+ const frame = {};
251
+ const { iframe } = previewFrame(frame);
252
+ iframe.setAttribute("sandbox", "allow-scripts");
253
+ expect(deliver(iframe, from(frame, window.location.origin))).toBeNull();
254
+ expect(deliver(iframe, from(frame, "null"))).toEqual(facts);
255
+ });
256
+
257
+ it("accepts the preview frame's report from the origin it loads at", () => {
258
+ const frame = {};
259
+ const { iframe } = previewFrame(frame);
260
+ expect(deliver(iframe, from(frame, window.location.origin))).toEqual(facts);
261
+ });
262
+ });
@@ -24,6 +24,7 @@ import {
24
24
  } from "../store/previewQualitySlice";
25
25
  import { parseTimelineFromDOM } from "../lib/timelineDOM";
26
26
  import { previewClipManifest, previewFrameOpaque } from "../lib/previewChannel";
27
+ import { frameOrigins } from "../lib/bridgePlayback";
27
28
 
28
29
  /** What `processTimelineMessage` accepts — the clip-manifest postMessage. */
29
30
  export interface ClipManifestMessage {
@@ -57,9 +58,10 @@ export interface PreviewMessageRouterDeps {
57
58
  syncTimelineElements: (elements: TimelineElement[]) => void;
58
59
  }
59
60
 
60
- /** True when the message did not come from OUR preview iframe. */
61
- function isForeignSource(e: MessageEvent, iframe: HTMLIFrameElement | null): boolean {
62
- return Boolean(e.source && iframe && e.source !== iframe.contentWindow);
61
+ /** True only for a post by our preview iframe's window, from an origin that frame can have. */
62
+ function fromOurPreview(e: MessageEvent, iframe: HTMLIFrameElement | null): boolean {
63
+ if (!iframe || !e.source || e.source !== iframe.contentWindow) return false;
64
+ return frameOrigins(iframe).includes(e.origin);
63
65
  }
64
66
 
65
67
  /** A preview message worth acting on, or null: the fixture lease, the sender
@@ -70,7 +72,7 @@ function acceptedPreviewMessage(
70
72
  iframe: HTMLIFrameElement | null,
71
73
  ): PreviewMessage | null {
72
74
  if (hasTimelinePerformanceFixtureLease()) return null;
73
- if (isForeignSource(e, iframe)) return null;
75
+ if (!fromOurPreview(e, iframe)) return null;
74
76
  const data = e.data as PreviewMessage | null;
75
77
  if (data?.source !== "hf-preview") return null;
76
78
  return acceptStudioRuntimeMessage(data) ? data : null;
@@ -157,12 +157,14 @@ export async function paintShadow(getApi: () => TimelinePlayerApi, shadow: HTMLI
157
157
  export function serveEditingRequests(
158
158
  frame: Record<string, unknown>,
159
159
  answer: (kind: string, params: unknown) => unknown,
160
+ onPort?: (port: MessagePort) => void,
160
161
  ): () => void {
161
162
  const ports: MessagePort[] = [];
162
163
  frame.postMessage = (message: { type?: string } | null) => {
163
164
  if (message?.type !== "editing-connect") return;
164
165
  const { port1, port2 } = new MessageChannel();
165
166
  ports.push(port1);
167
+ onPort?.(port1);
166
168
  port1.onmessage = (event: MessageEvent<{ id: number; kind: string; params: unknown }>) => {
167
169
  const { id, kind, params } = event.data;
168
170
  port1.postMessage({ v: 1, id, ok: true, result: answer(kind, params) });
@@ -1,11 +1,12 @@
1
1
  // @vitest-environment happy-dom
2
2
 
3
- import { describe, expect, it } from "vitest";
3
+ import { describe, expect, it, vi } from "vitest";
4
4
  import {
5
5
  collectSubCompositionDomChildren,
6
6
  collectSubCompositionHostState,
7
+ hydrateTimelineFromPreview,
7
8
  } from "./timelineSyncHydration";
8
- import type { ClipManifestClip } from "../lib/playbackTypes";
9
+ import type { ClipManifestClip, PlaybackAdapter } from "../lib/playbackTypes";
9
10
 
10
11
  const clip = (over: Partial<ClipManifestClip>): ClipManifestClip => ({
11
12
  id: "x",
@@ -138,3 +139,28 @@ describe("collectSubCompositionHostState", () => {
138
139
  expect(state.size).toBe(0);
139
140
  });
140
141
  });
142
+
143
+ describe("hydrateTimelineFromPreview", () => {
144
+ it("attaches the shortcut listeners to a frame whose document it cannot read", () => {
145
+ const frame = {};
146
+ Object.defineProperty(frame, "document", {
147
+ get: () => {
148
+ throw new DOMException("blocked", "SecurityError");
149
+ },
150
+ });
151
+ const iframe = document.createElement("iframe");
152
+ Object.defineProperty(iframe, "contentWindow", { value: frame });
153
+ Object.defineProperty(iframe, "contentDocument", { value: null });
154
+ const attachIframeShortcutListeners = vi.fn();
155
+ hydrateTimelineFromPreview({
156
+ iframe,
157
+ adapter: { getDuration: () => 1 } as unknown as PlaybackAdapter,
158
+ processTimelineMessage: vi.fn(),
159
+ enrichMissingCompositions: vi.fn(),
160
+ applyPreviewAudioState: vi.fn(),
161
+ attachIframeShortcutListeners,
162
+ syncTimelineElements: vi.fn(),
163
+ });
164
+ expect(attachIframeShortcutListeners).toHaveBeenCalledOnce();
165
+ });
166
+ });