mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
fix(studio): an in-place tween patch that throws says why it fell back to a reload (#8454)
* test(studio): an in-place tween patch that throws reports the error's name * fix(studio): an in-place tween patch that throws says why it fell back to a reload * test(studio): a composition's own error class name reports as other * fix(studio): only built-in error names leave an in-place patch's report * test(studio): a built-in error from the preview frame's realm reports its name * fix(studio): an in-place patch reads a thrown error's name from any realm
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { runInNewContext } from "node:vm";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { patchRuntimeTweenInPlace } from "./gsapRuntimePatch";
|
||||
import { trackStudioEvent } from "../utils/studioTelemetry";
|
||||
|
||||
vi.mock("../utils/studioTelemetry", () => ({ trackStudioEvent: vi.fn() }));
|
||||
|
||||
/**
|
||||
* The helper patches ONE tween's values in `window.__timelines[compKey]` in place,
|
||||
@@ -566,3 +570,82 @@ describe("patchRuntimeTweenInPlace — deferSeek", () => {
|
||||
expect(rendered).toEqual({ a: 500, b: 600 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("patchRuntimeTweenInPlace — a throw inside the patch", () => {
|
||||
// fails without the fix: the reload fallback hid the error, so a broken patch path looked like a slow edit.
|
||||
// The composition's GSAP throws here, so the edit still falls back to a reload.
|
||||
it("falls back, and reports the error's name, never its message", () => {
|
||||
const logged = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
const el = { id: "box" };
|
||||
const iframe = {
|
||||
contentWindow: {
|
||||
gsap: {
|
||||
set: () => {
|
||||
throw new TypeError("user script text");
|
||||
},
|
||||
},
|
||||
},
|
||||
contentDocument: { querySelector: (sel: string) => (sel === "#box" ? el : null) },
|
||||
} as unknown as HTMLIFrameElement;
|
||||
|
||||
expect(patchRuntimeTweenInPlace(iframe, "#box", { kind: "global-set", props: { x: 1 } })).toBe(
|
||||
false,
|
||||
);
|
||||
expect(trackStudioEvent).toHaveBeenCalledWith("gsap_instant_patch_threw", {
|
||||
error_name: "TypeError",
|
||||
});
|
||||
expect(logged).toHaveBeenCalledTimes(1);
|
||||
logged.mockRestore();
|
||||
});
|
||||
|
||||
// fails without the fix: a TypeError from the preview frame's realm is not instanceof this realm's Error, so every
|
||||
// real throw reported 'other'.
|
||||
it("reports a built-in error thrown in the preview frame's realm by its name", () => {
|
||||
const logged = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
const el = { id: "box" };
|
||||
const iframe = {
|
||||
contentWindow: {
|
||||
gsap: {
|
||||
set: () => {
|
||||
throw runInNewContext("new TypeError('user script text')");
|
||||
},
|
||||
},
|
||||
},
|
||||
contentDocument: { querySelector: (sel: string) => (sel === "#box" ? el : null) },
|
||||
} as unknown as HTMLIFrameElement;
|
||||
|
||||
expect(patchRuntimeTweenInPlace(iframe, "#box", { kind: "global-set", props: { x: 1 } })).toBe(
|
||||
false,
|
||||
);
|
||||
expect(trackStudioEvent).toHaveBeenLastCalledWith("gsap_instant_patch_threw", {
|
||||
error_name: "TypeError",
|
||||
});
|
||||
logged.mockRestore();
|
||||
});
|
||||
|
||||
it("reports a name the composition gave its own error class as 'other'", () => {
|
||||
const logged = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
class FilmError extends Error {
|
||||
override name = "user script text";
|
||||
}
|
||||
const el = { id: "box" };
|
||||
const iframe = {
|
||||
contentWindow: {
|
||||
gsap: {
|
||||
set: () => {
|
||||
throw new FilmError();
|
||||
},
|
||||
},
|
||||
},
|
||||
contentDocument: { querySelector: (sel: string) => (sel === "#box" ? el : null) },
|
||||
} as unknown as HTMLIFrameElement;
|
||||
|
||||
expect(patchRuntimeTweenInPlace(iframe, "#box", { kind: "global-set", props: { x: 1 } })).toBe(
|
||||
false,
|
||||
);
|
||||
expect(trackStudioEvent).toHaveBeenLastCalledWith("gsap_instant_patch_threw", {
|
||||
error_name: "other",
|
||||
});
|
||||
logged.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,9 +11,31 @@ import {
|
||||
type RuntimeTweenChange,
|
||||
} from "@hyperframes/core/runtime/patch-tween";
|
||||
import { swapWaiting, type SoftReloadWindow } from "@hyperframes/core/runtime/soft-reload";
|
||||
import { trackStudioEvent } from "../utils/studioTelemetry";
|
||||
|
||||
export type { RuntimeTweenChange, SetPatchProps } from "@hyperframes/core/runtime/patch-tween";
|
||||
|
||||
// A composition can name its own error class anything, so only these names leave the machine.
|
||||
const REPORTED_ERROR_NAMES = new Set([
|
||||
"Error",
|
||||
"TypeError",
|
||||
"RangeError",
|
||||
"ReferenceError",
|
||||
"SyntaxError",
|
||||
"EvalError",
|
||||
"URIError",
|
||||
"AggregateError",
|
||||
"SecurityError",
|
||||
"InvalidStateError",
|
||||
"NotFoundError",
|
||||
"DataCloneError",
|
||||
]);
|
||||
// The error may come from the preview frame's realm, so its name is read, never checked with instanceof.
|
||||
function reportedErrorName(error: unknown): string {
|
||||
const name = (error as { name?: unknown } | null)?.name;
|
||||
return typeof name === "string" && REPORTED_ERROR_NAMES.has(name) ? name : "other";
|
||||
}
|
||||
|
||||
function reachablePreview(iframe: HTMLIFrameElement): PatchTarget {
|
||||
const win = iframe.contentWindow as
|
||||
| (Window & {
|
||||
@@ -48,7 +70,11 @@ export function patchRuntimeTweenInPlace(
|
||||
if (change.kind !== "global-set")
|
||||
return patchTween(target, selector, change, compositionId, deferSeek);
|
||||
return applyGlobalSet(target, selector, change.props) !== null;
|
||||
} catch {
|
||||
} catch (error) {
|
||||
trackStudioEvent("gsap_instant_patch_threw", {
|
||||
error_name: reportedErrorName(error),
|
||||
});
|
||||
console.error("[Studio] An in-place tween patch threw; reloading the preview instead", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user