@hyperframes/studio 0.9.54 → 0.9.56

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.
@@ -1,4 +1,4 @@
1
- import{n as an}from"./index-B0VEzzD2.js";/*!
1
+ import{n as an}from"./index-CgyBYe2L.js";/*!
2
2
  * Copyright (c) 2026-present, Vanilagy and contributors
3
3
  *
4
4
  * This Source Code Form is subject to the terms of the Mozilla Public
@@ -1 +1 @@
1
- import{Input as V,ALL_FORMATS as G,UrlSource as K,VideoSampleSink as Q}from"./index-XEm_N45R.js";import{l as X}from"./index-B0VEzzD2.js";const x=1e-5,k=1e-9,T=h=>!h.variableFrameRate&&(h.frameRate??0)>0;function Y(h,s){const{sourceLead:r,inputLead:l,timeResolution:d,frameRate:p=0}=s;if(!T(s))return Math.max(l,h-r+l)+k;const w=Math.max(0,Math.floor((h-r)*p+k)),f=l+w/p;return(d>0?Math.round(f*d)/d:f)+x}function J(h,s,r){const{frameRate:l=0,sourceLead:d,inputLead:p}=r,w=T(r)?Math.round((h-p)*l)/l+d:h-p+d,f=Math.min(s,w);return{timestamp:f,duration:Math.max(T(r)?1/l:0,s-f+x)}}const E=64*1024*1024,M=4,Z=.5,$=2e3;async function q(h,s){if(!h.transparent)return h.sink.getSample(s);let r=null;try{for await(const l of h.sink.samplesAtTimestamps([s]))r==null||r.close(),r=l;return r}catch(l){throw r==null||r.close(),l}}function ot(h){const s=new Map,r=new Map,l=new Map,d=new Map,p=new Map;let w=0,f=!1;const _=t=>{var o,n;let e=new URL(t,location.href).pathname;try{e=decodeURIComponent(e)}catch{}return(n=Object.entries(((o=h.contentWindow)==null?void 0:o.__HF_MEDIA_SOURCE_FACTS__)??{}).filter(([i])=>e.endsWith(i)).sort(([i],[a])=>a.length-i.length)[0])==null?void 0:n[1]},b=(t=E)=>{for(const[e,o]of p){if(w<=t)break;e.pins>0||(p.delete(e),o.cache.delete(e.timestamp),w-=e.bytes,e.image.close())}},v=async t=>{if(t.disposed)return;t.disposed=!0,clearTimeout(t.idleTimer),r.get(t.key)===t&&r.delete(t.key);const e=(async()=>{t.transparent&&await t.tail,t.cancelReads(),t.input.dispose(),await y(t).catch(()=>{});for(const o of t.cache.values())p.delete(o),w-=o.bytes,o.image.close();t.cache.clear(),t.canvas.width=t.canvas.height=0})();return t.transparent?new Promise((o,n)=>{const i=setTimeout(()=>{t.cancelReads(),o()},$);e.then(o,n).finally(()=>clearTimeout(i))}):e},P=t=>{clearTimeout(t.idleTimer),!(t.disposed||t.users>0||[...t.cache.values()].some(e=>e.pins>0))&&(t.idleTimer=setTimeout(()=>{t.users>0||[...t.cache.values()].some(e=>e.pins>0)||r.get(t.key)===t&&(s.delete(t.key),v(t))},1e4))},D=async t=>{const e=X("thumbnail"),o=new V({source:new K(t,e),formats:G});l.set(o,e.cancelReads);try{const n=await o.getPrimaryVideoTrack();if(!n||f)throw new Error("Preview video unavailable");const[i,a,u,c,m,S,F,B,j]=await Promise.all([n.getDisplayWidth(),n.getDisplayHeight(),n.getFirstTimestamp(),n.getTimeResolution(),n.getSquarePixelWidth(),n.getSquarePixelHeight(),n.getRotation(),n.getFlip(),n.canBeTransparent()]),g=document.createElement("canvas");g.width=Math.max(1,Math.ceil(i));const z=F%180===0?m/S:S/m;return g.height=Math.max(1,Math.round(g.width/z)),{key:t,input:o,cancelReads:e.cancelReads,sink:new Q(n),canvas:g,rotation:F,flip:B,width:i,height:a,lead:u,ticks:c,transparent:j,tail:Promise.resolve(),iterator:null,iteratorEnd:0,lastTime:-1/0,cache:new Map,users:0,disposed:!1}}catch(n){throw o.dispose(),e.cancelReads(),n}finally{l.delete(o)}},I=()=>{for(const[t]of s){if(s.size<M)break;const e=r.get(t);e&&e.users===0&&![...e.cache.values()].some(o=>o.pins>0)&&(s.delete(t),v(e))}if(s.size>=M)throw new Error("Preview decoder budget reached")},L=async t=>{const e=t;if((d.get(e)??0)>performance.now())throw new Error("Preview decoder cooling down");let o=s.get(e);o||(I(),o=D(t),s.set(e,o)),s.delete(e),s.set(e,o);let n;try{n=await o}catch(i){throw s.get(e)===o&&(s.delete(e),d.set(e,performance.now()+5e3)),i}if(f||n.disposed)throw new DOMException("Preview disposed","AbortError");return r.set(e,n),n.users++,clearTimeout(n.idleTimer),n},y=async t=>{const e=t.iterator;t.iterator=null,await(e==null?void 0:e.return())},R=(t,e)=>{if(e.throwIfAborted(),f||t.disposed)throw new Error("Preview disposed")},O=async(t,e,o)=>{for(t.iterator||(t.iteratorEnd=e+8,t.iterator=t.sink.samples(e,t.iteratorEnd));;){R(t,o);const n=await t.iterator.next();if(n.done)return null;const i=n.value;let a=!1;try{if(R(t,o),i.timestamp>e+1e-6)return null;if(a=i.timestamp+i.duration>e+1e-6,a)return i}finally{a||i.close()}}},A=(t,e)=>(e.pins++,p.delete(e),p.set(e,t),e),W=async(t,e,o,n,i)=>{const a=t.canvas.getContext("2d",{alpha:!0});if(!a)throw new Error("Preview canvas unavailable");a.clearRect(0,0,t.canvas.width,t.canvas.height),a.imageSmoothingQuality="high",e.drawWithFit(a,{fit:"fill",rotation:t.rotation,flip:t.flip});const u=await createImageBitmap(t.canvas);if(i.aborted||f||t.disposed)throw u.close(),i.throwIfAborted(),new Error("Preview disposed");const c={image:u,...J(e.timestamp,o,n),bytes:u.width*u.height*4,pins:1},m=t.cache.get(c.timestamp);if(m)return m.duration=Math.max(m.duration,c.duration),u.close(),A(t,m);if(b(E-c.bytes),w+c.bytes>E)throw u.close(),new Error("Preview frame budget reached");return t.cache.set(c.timestamp,c),p.set(c,t),w+=c.bytes,c},U=(t,e,o)=>!t.transparent&&!o.variableFrameRate&&(o.frameRate??0)>0&&e>t.lastTime+1e-6&&e-t.lastTime<=Z,C=async(t,e,o,n)=>{R(t,n);const i=[...t.cache.values()].find(m=>e>=m.timestamp-1e-6&&e<m.timestamp+m.duration-1e-6);if(i)return A(t,i);const a=Y(e,o),u=U(t,e,o);let c=null;try{if((!u||a>=t.iteratorEnd)&&await y(t),c=u?await O(t,a,n):null,c||(await y(t),c=await q(t,a)),!c)throw new Error("No preview frame at requested time");return R(t,n),t.lastTime=e,await W(t,c,e,o,n)}finally{c==null||c.close(),(n.aborted||f)&&await y(t).catch(()=>{})}},H=(t,e)=>{const o=_(t.source);return{frameRate:o==null?void 0:o.frameRate,variableFrameRate:o==null?void 0:o.variableFrameRate,sourceLead:e.lead,inputLead:e.lead,timeResolution:e.ticks}},N=async(t,e)=>{e.throwIfAborted();const o=t.source;if(!o)throw new Error("Preview source unavailable");const n=await L(o);try{const i=H(t,n),a=n.tail.then(()=>C(n,t.time,i,e));n.tail=a.then(()=>{},()=>{});const u=await a;let c=!1;return b(),{source:u.image,timestamp:u.timestamp,duration:u.duration,width:n.width,height:n.height,release(){c||(c=!0,u.pins--,b(),P(n))}}}finally{n.users--,P(n)}};return{async decode(t,e){if(f)throw new DOMException("Preview disposed","AbortError");if(new Set(t.map(({source:a})=>a)).size>M)throw new Error("Preview decoder budget reached");const o=await Promise.allSettled(t.map(a=>N(a,e))),n=o.find(a=>a.status==="rejected"),i=o.flatMap(a=>a.status==="fulfilled"?[a.value]:[]);if(n||f||e.aborted){for(const a of i)a.release();throw n?n.reason:new DOMException("Preview cancelled","AbortError")}return i},async suspend(){const t=[...s.values()].map(e=>e.then(v).catch(()=>{}));s.clear();for(const[e,o]of l)o(),e.dispose();await Promise.all(t)},dispose(){if(!f){f=!0;for(const[t,e]of l)e(),t.dispose();for(const t of s.values())t.then(v).catch(()=>{});s.clear(),d.clear()}}}}export{ot as createPausedVideoDecoder};
1
+ import{Input as V,ALL_FORMATS as G,UrlSource as K,VideoSampleSink as Q}from"./index-yuN1pZrv.js";import{l as X}from"./index-CgyBYe2L.js";const x=1e-5,k=1e-9,T=h=>!h.variableFrameRate&&(h.frameRate??0)>0;function Y(h,s){const{sourceLead:r,inputLead:l,timeResolution:d,frameRate:p=0}=s;if(!T(s))return Math.max(l,h-r+l)+k;const w=Math.max(0,Math.floor((h-r)*p+k)),f=l+w/p;return(d>0?Math.round(f*d)/d:f)+x}function J(h,s,r){const{frameRate:l=0,sourceLead:d,inputLead:p}=r,w=T(r)?Math.round((h-p)*l)/l+d:h-p+d,f=Math.min(s,w);return{timestamp:f,duration:Math.max(T(r)?1/l:0,s-f+x)}}const E=64*1024*1024,M=4,Z=.5,$=2e3;async function q(h,s){if(!h.transparent)return h.sink.getSample(s);let r=null;try{for await(const l of h.sink.samplesAtTimestamps([s]))r==null||r.close(),r=l;return r}catch(l){throw r==null||r.close(),l}}function ot(h){const s=new Map,r=new Map,l=new Map,d=new Map,p=new Map;let w=0,f=!1;const _=t=>{var o,n;let e=new URL(t,location.href).pathname;try{e=decodeURIComponent(e)}catch{}return(n=Object.entries(((o=h.contentWindow)==null?void 0:o.__HF_MEDIA_SOURCE_FACTS__)??{}).filter(([i])=>e.endsWith(i)).sort(([i],[a])=>a.length-i.length)[0])==null?void 0:n[1]},b=(t=E)=>{for(const[e,o]of p){if(w<=t)break;e.pins>0||(p.delete(e),o.cache.delete(e.timestamp),w-=e.bytes,e.image.close())}},v=async t=>{if(t.disposed)return;t.disposed=!0,clearTimeout(t.idleTimer),r.get(t.key)===t&&r.delete(t.key);const e=(async()=>{t.transparent&&await t.tail,t.cancelReads(),t.input.dispose(),await y(t).catch(()=>{});for(const o of t.cache.values())p.delete(o),w-=o.bytes,o.image.close();t.cache.clear(),t.canvas.width=t.canvas.height=0})();return t.transparent?new Promise((o,n)=>{const i=setTimeout(()=>{t.cancelReads(),o()},$);e.then(o,n).finally(()=>clearTimeout(i))}):e},P=t=>{clearTimeout(t.idleTimer),!(t.disposed||t.users>0||[...t.cache.values()].some(e=>e.pins>0))&&(t.idleTimer=setTimeout(()=>{t.users>0||[...t.cache.values()].some(e=>e.pins>0)||r.get(t.key)===t&&(s.delete(t.key),v(t))},1e4))},D=async t=>{const e=X("thumbnail"),o=new V({source:new K(t,e),formats:G});l.set(o,e.cancelReads);try{const n=await o.getPrimaryVideoTrack();if(!n||f)throw new Error("Preview video unavailable");const[i,a,u,c,m,S,F,B,j]=await Promise.all([n.getDisplayWidth(),n.getDisplayHeight(),n.getFirstTimestamp(),n.getTimeResolution(),n.getSquarePixelWidth(),n.getSquarePixelHeight(),n.getRotation(),n.getFlip(),n.canBeTransparent()]),g=document.createElement("canvas");g.width=Math.max(1,Math.ceil(i));const z=F%180===0?m/S:S/m;return g.height=Math.max(1,Math.round(g.width/z)),{key:t,input:o,cancelReads:e.cancelReads,sink:new Q(n),canvas:g,rotation:F,flip:B,width:i,height:a,lead:u,ticks:c,transparent:j,tail:Promise.resolve(),iterator:null,iteratorEnd:0,lastTime:-1/0,cache:new Map,users:0,disposed:!1}}catch(n){throw o.dispose(),e.cancelReads(),n}finally{l.delete(o)}},I=()=>{for(const[t]of s){if(s.size<M)break;const e=r.get(t);e&&e.users===0&&![...e.cache.values()].some(o=>o.pins>0)&&(s.delete(t),v(e))}if(s.size>=M)throw new Error("Preview decoder budget reached")},L=async t=>{const e=t;if((d.get(e)??0)>performance.now())throw new Error("Preview decoder cooling down");let o=s.get(e);o||(I(),o=D(t),s.set(e,o)),s.delete(e),s.set(e,o);let n;try{n=await o}catch(i){throw s.get(e)===o&&(s.delete(e),d.set(e,performance.now()+5e3)),i}if(f||n.disposed)throw new DOMException("Preview disposed","AbortError");return r.set(e,n),n.users++,clearTimeout(n.idleTimer),n},y=async t=>{const e=t.iterator;t.iterator=null,await(e==null?void 0:e.return())},R=(t,e)=>{if(e.throwIfAborted(),f||t.disposed)throw new Error("Preview disposed")},O=async(t,e,o)=>{for(t.iterator||(t.iteratorEnd=e+8,t.iterator=t.sink.samples(e,t.iteratorEnd));;){R(t,o);const n=await t.iterator.next();if(n.done)return null;const i=n.value;let a=!1;try{if(R(t,o),i.timestamp>e+1e-6)return null;if(a=i.timestamp+i.duration>e+1e-6,a)return i}finally{a||i.close()}}},A=(t,e)=>(e.pins++,p.delete(e),p.set(e,t),e),W=async(t,e,o,n,i)=>{const a=t.canvas.getContext("2d",{alpha:!0});if(!a)throw new Error("Preview canvas unavailable");a.clearRect(0,0,t.canvas.width,t.canvas.height),a.imageSmoothingQuality="high",e.drawWithFit(a,{fit:"fill",rotation:t.rotation,flip:t.flip});const u=await createImageBitmap(t.canvas);if(i.aborted||f||t.disposed)throw u.close(),i.throwIfAborted(),new Error("Preview disposed");const c={image:u,...J(e.timestamp,o,n),bytes:u.width*u.height*4,pins:1},m=t.cache.get(c.timestamp);if(m)return m.duration=Math.max(m.duration,c.duration),u.close(),A(t,m);if(b(E-c.bytes),w+c.bytes>E)throw u.close(),new Error("Preview frame budget reached");return t.cache.set(c.timestamp,c),p.set(c,t),w+=c.bytes,c},U=(t,e,o)=>!t.transparent&&!o.variableFrameRate&&(o.frameRate??0)>0&&e>t.lastTime+1e-6&&e-t.lastTime<=Z,C=async(t,e,o,n)=>{R(t,n);const i=[...t.cache.values()].find(m=>e>=m.timestamp-1e-6&&e<m.timestamp+m.duration-1e-6);if(i)return A(t,i);const a=Y(e,o),u=U(t,e,o);let c=null;try{if((!u||a>=t.iteratorEnd)&&await y(t),c=u?await O(t,a,n):null,c||(await y(t),c=await q(t,a)),!c)throw new Error("No preview frame at requested time");return R(t,n),t.lastTime=e,await W(t,c,e,o,n)}finally{c==null||c.close(),(n.aborted||f)&&await y(t).catch(()=>{})}},H=(t,e)=>{const o=_(t.source);return{frameRate:o==null?void 0:o.frameRate,variableFrameRate:o==null?void 0:o.variableFrameRate,sourceLead:e.lead,inputLead:e.lead,timeResolution:e.ticks}},N=async(t,e)=>{e.throwIfAborted();const o=t.source;if(!o)throw new Error("Preview source unavailable");const n=await L(o);try{const i=H(t,n),a=n.tail.then(()=>C(n,t.time,i,e));n.tail=a.then(()=>{},()=>{});const u=await a;let c=!1;return b(),{source:u.image,timestamp:u.timestamp,duration:u.duration,width:n.width,height:n.height,release(){c||(c=!0,u.pins--,b(),P(n))}}}finally{n.users--,P(n)}};return{async decode(t,e){if(f)throw new DOMException("Preview disposed","AbortError");if(new Set(t.map(({source:a})=>a)).size>M)throw new Error("Preview decoder budget reached");const o=await Promise.allSettled(t.map(a=>N(a,e))),n=o.find(a=>a.status==="rejected"),i=o.flatMap(a=>a.status==="fulfilled"?[a.value]:[]);if(n||f||e.aborted){for(const a of i)a.release();throw n?n.reason:new DOMException("Preview cancelled","AbortError")}return i},async suspend(){const t=[...s.values()].map(e=>e.then(v).catch(()=>{}));s.clear();for(const[e,o]of l)o(),e.dispose();await Promise.all(t)},dispose(){if(!f){f=!0;for(const[t,e]of l)e(),t.dispose();for(const t of s.values())t.then(v).catch(()=>{});s.clear(),d.clear()}}}}export{ot as createPausedVideoDecoder};
@@ -1994,6 +1994,10 @@ function previewStageSize(iframe) {
1994
1994
  function frameOrigins(iframe) {
1995
1995
  const sandbox = iframe.getAttribute("sandbox")?.toLowerCase().split(/\s+/);
1996
1996
  if (sandbox && !sandbox.includes("allow-same-origin")) return ["null"];
1997
+ const loaded = loadedOrigins(iframe);
1998
+ return previewFrameOpaque(iframe) && !loaded.includes("null") ? [...loaded, "null"] : loaded;
1999
+ }
2000
+ function loadedOrigins(iframe) {
1997
2001
  const src = iframe.getAttribute("src");
1998
2002
  if (iframe.hasAttribute("srcdoc") || !src) return [window.location.origin];
1999
2003
  try {
@@ -2069,7 +2073,7 @@ function bridgePlaybackAdapter(iframe) {
2069
2073
  }
2070
2074
 
2071
2075
  // src/player/lib/frameToken.ts
2072
- var PREVIEW_PATH = /^(\/api\/projects\/[^/?#]+)\/preview(?=[/?#]|$)/;
2076
+ var PREVIEW_PATH = /^(\/api\/projects\/[^/?#]+)\/preview(?:\/~[\w-]{43})?(?=[/?#]|$)/;
2073
2077
  function fetchFrameToken(frameTokenPath) {
2074
2078
  return studioApiFetch(frameTokenPath).then((res) => res.ok ? res.json() : null).then(
2075
2079
  (body) => typeof body?.token === "string" ? body.token : null
@@ -2303,6 +2307,7 @@ var Player = forwardRef(
2303
2307
  player.addEventListener("ready", handleReady);
2304
2308
  player.addEventListener("painted", handlePainted);
2305
2309
  player.addEventListener("error", handleError);
2310
+ if (opaque) player.toggleAttribute("sandbox-origin", true);
2306
2311
  if (typeof ref === "function") {
2307
2312
  ref(iframe);
2308
2313
  } else if (ref) {
@@ -2312,7 +2317,6 @@ var Player = forwardRef(
2312
2317
  player.setAttribute("shader-loading", "player");
2313
2318
  player.setAttribute("low-power-idle", "");
2314
2319
  player.toggleAttribute("no-load-timeout", noLoadTimeout === true);
2315
- player.toggleAttribute("sandbox-origin", opaque);
2316
2320
  player.setAttribute("width", String(portrait ? 1080 : 1920));
2317
2321
  player.setAttribute("height", String(portrait ? 1920 : 1080));
2318
2322
  player.style.width = "100%";
package/dist/index.html CHANGED
@@ -14,7 +14,7 @@
14
14
  document.documentElement.dataset.theme = "paper";
15
15
  }
16
16
  </script>
17
- <script type="module" crossorigin src="/assets/index-B0VEzzD2.js"></script>
17
+ <script type="module" crossorigin src="/assets/index-CgyBYe2L.js"></script>
18
18
  <link rel="stylesheet" crossorigin href="/assets/index-CRiuMxyq.css">
19
19
  </head>
20
20
  <body>
package/dist/index.js CHANGED
@@ -91,7 +91,7 @@ import {
91
91
  whileScriptWrites,
92
92
  writeStudioUiPreferences,
93
93
  zoomToSelectedClips
94
- } from "./chunk-K3IOYISK.js";
94
+ } from "./chunk-5EEM5WO4.js";
95
95
  import {
96
96
  Button,
97
97
  ContextMenu,
@@ -32539,12 +32539,13 @@ import {
32539
32539
  parseMediaLoadFailure,
32540
32540
  RUNTIME_MEDIA_LOAD_FAILED
32541
32541
  } from "@hyperframes/core/runtime/protocol";
32542
- function isForeignSource(e, iframe) {
32543
- return Boolean(e.source && iframe && e.source !== iframe.contentWindow);
32542
+ function fromOurPreview(e, iframe) {
32543
+ if (!iframe || !e.source || e.source !== iframe.contentWindow) return false;
32544
+ return frameOrigins(iframe).includes(e.origin);
32544
32545
  }
32545
32546
  function acceptedPreviewMessage(e, iframe) {
32546
32547
  if (hasTimelinePerformanceFixtureLease()) return null;
32547
- if (isForeignSource(e, iframe)) return null;
32548
+ if (!fromOurPreview(e, iframe)) return null;
32548
32549
  const data = e.data;
32549
32550
  if (data?.source !== "hf-preview") return null;
32550
32551
  return acceptStudioRuntimeMessage(data) ? data : null;
package/dist/player.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Player
3
- } from "./chunk-K3IOYISK.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.54",
3
+ "version": "0.9.56",
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/core": "0.9.54",
68
- "@hyperframes/parsers": "0.9.54",
69
- "@hyperframes/player": "0.9.54",
70
- "@hyperframes/sdk": "0.9.54",
71
- "@hyperframes/shader-transitions": "0.9.54",
72
- "@hyperframes/studio-server": "0.9.54"
67
+ "@hyperframes/core": "0.9.56",
68
+ "@hyperframes/parsers": "0.9.56",
69
+ "@hyperframes/sdk": "0.9.56",
70
+ "@hyperframes/shader-transitions": "0.9.56",
71
+ "@hyperframes/studio-server": "0.9.56",
72
+ "@hyperframes/player": "0.9.56"
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.54"
89
+ "@hyperframes/producer": "0.9.56"
90
90
  },
91
91
  "peerDependencies": {
92
92
  "react": "19",
@@ -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);
@@ -218,6 +220,12 @@ describe("a host's sandbox origin", () => {
218
220
  `/api/projects/demo/preview/~${token}/comp/scene.html`,
219
221
  ],
220
222
  ["/api/projects/demo/preview?_t=1", `/api/projects/demo/preview/~${token}?_t=1`],
223
+ // A reload starts from the frame's own src, which already carries a token, maybe a stale one.
224
+ [`/api/projects/demo/preview/~${token}?_t=2`, `/api/projects/demo/preview/~${token}?_t=2`],
225
+ [
226
+ `/api/projects/demo/preview/~${"S".repeat(43)}/comp/scene.html`,
227
+ `/api/projects/demo/preview/~${token}/comp/scene.html`,
228
+ ],
221
229
  ])(
222
230
  "seals an opaque preview of %s and loads it under its project's frame token",
223
231
  async (directUrl, sealed) => {
@@ -237,6 +245,14 @@ describe("a host's sandbox origin", () => {
237
245
  expect(player.getAttribute("src")).toBe("/api/projects/demo/preview");
238
246
  });
239
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
+
240
256
  it("leaves a same-origin preview on its plain path without asking for a token", async () => {
241
257
  const fetch = vi.spyOn(globalThis, "fetch");
242
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%";
@@ -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;
@@ -91,6 +91,7 @@ it("publishes resolved metadata only for clips that still need it", async () =>
91
91
  window.dispatchEvent(
92
92
  new MessageEvent("message", {
93
93
  source: frame,
94
+ origin: window.location.origin,
94
95
  data: { source: "hf-preview", type: "timeline", clips, durationInFrames: 18000, fps: 30 },
95
96
  }),
96
97
  );
@@ -64,6 +64,7 @@ function postFromRuntime(win: object, data: Record<string, unknown>) {
64
64
  window.dispatchEvent(
65
65
  new MessageEvent("message", {
66
66
  source: win as unknown as Window,
67
+ origin: window.location.origin,
67
68
  data: { source: "hf-preview", ...data },
68
69
  }),
69
70
  );
@@ -87,6 +87,7 @@ describe("useTimelinePlayer seek hydration", () => {
87
87
  window.dispatchEvent(
88
88
  new MessageEvent("message", {
89
89
  source: iframeWindow,
90
+ origin: window.location.origin,
90
91
  data: {
91
92
  source: "hf-preview",
92
93
  type: "timeline",
@@ -164,6 +165,7 @@ describe("useTimelinePlayer seek hydration", () => {
164
165
  window.dispatchEvent(
165
166
  new MessageEvent("message", {
166
167
  source: iframeWindow as unknown as Window,
168
+ origin: window.location.origin,
167
169
  data: { source: "hf-preview", type: "state", protocolVersion: 999 },
168
170
  }),
169
171
  );
@@ -174,6 +176,7 @@ describe("useTimelinePlayer seek hydration", () => {
174
176
  window.dispatchEvent(
175
177
  new MessageEvent("message", {
176
178
  source: iframeWindow as unknown as Window,
179
+ origin: window.location.origin,
177
180
  data: { source: "hf-preview", type: "state" },
178
181
  }),
179
182
  );
@@ -3,6 +3,7 @@
3
3
  // because a runtime posts its first `timeline` while it boots.
4
4
  import type { PlaybackAdapter } from "./playbackTypes";
5
5
  import { inspectStudioRuntimeMessage, postRuntimeControlMessage } from "./runtimeProtocol";
6
+ import { previewFrameOpaque } from "./previewChannel";
6
7
 
7
8
  interface PlaybackMirror {
8
9
  iframe: HTMLIFrameElement;
@@ -56,10 +57,15 @@ export function previewStageSize(
56
57
  return (frame && mirrors.get(frame)?.stageSize) ?? null;
57
58
  }
58
59
 
59
- /** The origins a post from this frame can carry, given its sandbox and what it loads. */
60
+ /** The origins a post from this frame can carry; a page its server sandboxed posts as `null` without the attribute. */
60
61
  export function frameOrigins(iframe: HTMLIFrameElement): string[] {
61
62
  const sandbox = iframe.getAttribute("sandbox")?.toLowerCase().split(/\s+/);
62
63
  if (sandbox && !sandbox.includes("allow-same-origin")) return ["null"];
64
+ const loaded = loadedOrigins(iframe);
65
+ return previewFrameOpaque(iframe) && !loaded.includes("null") ? [...loaded, "null"] : loaded;
66
+ }
67
+
68
+ function loadedOrigins(iframe: HTMLIFrameElement): string[] {
63
69
  const src = iframe.getAttribute("src");
64
70
  if (iframe.hasAttribute("srcdoc") || !src) return [window.location.origin];
65
71
  try {
@@ -1,6 +1,6 @@
1
1
  import { studioApiFetch } from "../../utils/studioApiFetch";
2
2
 
3
- const PREVIEW_PATH = /^(\/api\/projects\/[^/?#]+)\/preview(?=[/?#]|$)/;
3
+ const PREVIEW_PATH = /^(\/api\/projects\/[^/?#]+)\/preview(?:\/~[\w-]{43})?(?=[/?#]|$)/;
4
4
 
5
5
  export function fetchFrameToken(frameTokenPath: string): Promise<string | null> {
6
6
  return studioApiFetch(frameTokenPath)
@@ -219,6 +219,19 @@ describe("bridge playback for a frame Studio cannot script", () => {
219
219
  expect(own.getTime()).toBe(5);
220
220
  });
221
221
 
222
+ it("accepts a frame its server sandboxed, which posts as null though its frame has no sandbox", () => {
223
+ const { iframe, frame } = opaqueFrame();
224
+ iframe.removeAttribute("sandbox");
225
+ iframe.setAttribute("src", "/api/projects/demo/preview");
226
+ watchPreviewFrame(iframe);
227
+ const adapter = bridgePlaybackAdapter(iframe)!;
228
+ fromFrame(frame, { type: "state", currentTime: 6, isPlaying: false }, "https://example.com");
229
+ fromFrame({}, { type: "state", currentTime: 7, isPlaying: false }, "null");
230
+ expect(adapter.getTime()).toBe(0);
231
+ fromFrame(frame, { type: "state", currentTime: 6, isPlaying: false }, "null");
232
+ expect(adapter.getTime()).toBe(6);
233
+ });
234
+
222
235
  it.each([
223
236
  ["srcdoc", (f: HTMLIFrameElement) => f.setAttribute("srcdoc", "<p>x</p>"), location.origin],
224
237
  [