Skip to content

Commit cdc5d08

Browse files
committed
fix(producer): module and importing scripts still count as able to register a timeline
1 parent 6f3543e commit cdc5d08

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

‎packages/engine/src/services/frameCapture-subTimelinePoll.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ describe("authorScriptsMayRegisterTimelines", () => {
206206
).toBe(true);
207207
});
208208

209+
it("answers true for a module script or any code that imports another file", async () => {
210+
expect(await scan(`<body><script type="module">draw()</script></body>`)).toBe(true);
211+
expect(await scan(`<body><script>import("./scene.js").then(run)</script></body>`)).toBe(true);
212+
expect(
213+
await scan(`<body><script src="/a.js"></script></body>`, {
214+
"/a.js": 'import { x } from "./b.js"',
215+
}),
216+
).toBe(true);
217+
expect(await scan(`<body><script>const important = 1; draw(important)</script></body>`)).toBe(
218+
false,
219+
);
220+
});
221+
209222
it("reads same-origin scripts and answers true for ones it cannot read", async () => {
210223
expect(await scan(`<body><script src="/a.js"></script></body>`, { "/a.js": "draw()" })).toBe(
211224
false,

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,7 @@ export interface IsolatedComposition {
20652065
}
20662066

20672067
// Runs in the page. False once every inline, template and same-origin script is read and none
2068-
// mentions __timelines; an unreadable script or a page still loading answers true.
2068+
// mentions __timelines or imports code; an unreadable or module script, or a page still loading, answers true.
20692069
export async function authorScriptsMayRegisterTimelines(
20702070
runtimeAttr: string,
20712071
authoredBegin: string,
@@ -2078,7 +2078,11 @@ export async function authorScriptsMayRegisterTimelines(
20782078
Array.from(t.content.querySelectorAll("script")),
20792079
),
20802080
].filter((script) => !script.hasAttribute(runtimeAttr));
2081+
// Code that imports another file may register through it, which is not read here.
2082+
const mayRegister = (code: string) =>
2083+
code.includes("__timelines") || /\bimport\s*\(|^\s*import\s*[\w{*"']/m.test(code);
20812084
for (const script of scripts) {
2085+
if (script.type === "module") return true;
20822086
if (!script.src) {
20832087
const text = script.textContent ?? "";
20842088
const authored = text.includes(authoredBegin)
@@ -2088,13 +2092,13 @@ export async function authorScriptsMayRegisterTimelines(
20882092
.map((region) => region.split(authoredEnd)[0])
20892093
.join("\n")
20902094
: text;
2091-
if (authored.includes("__timelines")) return true;
2095+
if (mayRegister(authored)) return true;
20922096
continue;
20932097
}
20942098
const url = new URL(script.src, location.href);
20952099
if (url.origin !== location.origin) return true;
20962100
try {
2097-
if ((await (await fetch(url)).text()).includes("__timelines")) return true;
2101+
if (mayRegister(await (await fetch(url)).text())) return true;
20982102
} catch {
20992103
return true;
21002104
}

0 commit comments

Comments
 (0)