Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(core): bind native window methods in the scoped sub-composition proxy (#3378)
Miguel Ángela1c1f519cb1d
diff --git a/packages/core/src/compiler/compositionScoping.test.ts b/packages/core/src/compiler/compositionScoping.test.tsindex b9d7e72d79..7ba39a5fbc 100644--- a/packages/core/src/compiler/compositionScoping.test.ts+++ b/packages/core/src/compiler/compositionScoping.test.ts@@ -169,6 +169,40 @@ body { margin: 0; }expect(fakeWindow.__captured).toEqual({ title: "Pro", price: "$29" });});+ it("hands native window methods back bound to the real window", () => {+ // Regression: the scoped `window` proxy returned natives UNBOUND, so `this`+ // at call time was the Proxy itself and Chrome rejected it with+ // "Illegal invocation" — breaking window.addEventListener, setTimeout,+ // matchMedia, getComputedStyle and requestAnimationFrame in every+ // sub-composition, including the window.addEventListener("hf-seek", ...)+ // form the Three.js and TypeGPU adapters document. The sibling document+ // and gsap proxies in this file always bound; this one did not.+ const { document } = parseHTML(`<div data-composition-id="scene"></div>`);+ const fakeWindow: Record<string, unknown> = { document, __timelines: {} };+ let boundToWindow = false;+ // Method shorthand, not `function () {}`: like a real native method it has+ // no `.prototype`, which is the property the proxy uses to tell a method+ // apart from a class. Stands in for the native brand check, which throws+ // when the method is invoked with anything else as `this`.+ const natives = {+ addEventListener(this: unknown) {+ if (this !== fakeWindow) throw new TypeError("Illegal invocation");+ boundToWindow = true;+ },+ };+ fakeWindow.addEventListener = natives.addEventListener;++ const wrapped = wrapScopedCompositionScript(+ `window.addEventListener("hf-seek", function () {});`,+ "scene",+ );+ new Function("window", wrapped)(fakeWindow);++ // Asserted on the call landing, not on throwing: the wrapper's error+ // boundary swallows the TypeError, which is why this shipped unnoticed.+ expect(boundToWindow).toBe(true);+ });+it("preserves non-getVariables members on window.__hyperframes (only getVariables is rescoped)", () => {const { document } = parseHTML(`<div data-composition-id="card-1"></div>`);let fitCalled = false;diff --git a/packages/core/src/compiler/compositionScoping.ts b/packages/core/src/compiler/compositionScoping.tsindex 795b801f08..c1fc996200 100644--- a/packages/core/src/compiler/compositionScoping.ts+++ b/packages/core/src/compiler/compositionScoping.ts@@ -485,7 +485,22 @@ export function wrapScopedCompositionScript(// (__hfScopedHyperframes is a hoisted var assigned below, before any// sub-comp script -- the only code that reads this -- runs.)if (prop === "__hyperframes") return __hfScopedHyperframes;- return Reflect.get(target, prop, target);+ // Native window methods must stay bound to the real window. Handed+ // back unbound, "this" at call time is this Proxy and Chrome rejects+ // it with "Illegal invocation", which broke window.addEventListener,+ // setTimeout, matchMedia and getComputedStyle inside every+ // sub-composition -- including the window.addEventListener("hf-seek",+ // ...) form the Three.js and TypeGPU adapters document. The sibling+ // document and gsap proxies here already bind.+ //+ // Only bind non-constructors. Function.prototype.bind drops static+ // members, so binding a class exposed on window (window.Texts and+ // friends) would silently strip its statics. Built-in methods have+ // no .prototype; classes and constructor functions do.+ var value = Reflect.get(target, prop, target);+ return typeof value === "function" && value.prototype === undefined+ ? value.bind(target)+ : value;},set: function(target, prop, value, receiver) {if (prop === "__timelines") {