mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(studio): a sealed preview's element reads are bounded, and a frame with no agent answers at once (#8055)
* fix(studio): a sealed preview's element reads are bounded, and a frame with no agent answers at once * fix(studio): a sealed preview's sources and style names are bounded too, never cut
This commit is contained in:
@@ -164,6 +164,14 @@ describe("element handles over the editing port", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("leaves out an id, selector or attribute name it would have to cut, and says so", async () => {
|
||||
const long = "x".repeat(MAX_ELEMENT_VALUE + 1);
|
||||
document.body.innerHTML = `<div id="${long}" data-${long}="1" data-kept="2"></div>`;
|
||||
const described = await describeOne(document.querySelector("div")!);
|
||||
expect(described).toMatchObject({ id: null, selector: null, truncated: true });
|
||||
expect(described.attributes.map(([name]) => name)).toEqual(["id", "data-kept"]);
|
||||
});
|
||||
|
||||
it("cuts long values, keeps only a long data: value's header, and leaves script and style text out", async () => {
|
||||
const long = "x".repeat(MAX_ELEMENT_VALUE + 50);
|
||||
document.body.innerHTML = `<div class="card" title="${long}" style="--note: ${long}">
|
||||
@@ -246,6 +254,28 @@ describe("element handles over the editing port", () => {
|
||||
expect(top!.parent).not.toBeNull();
|
||||
});
|
||||
|
||||
it("describes no element whose inherited source it would have to cut, and leaves out a style name it would", async () => {
|
||||
const long = "x".repeat(MAX_ELEMENT_VALUE + 1);
|
||||
document.body.innerHTML = `<div data-composition-file="${long}.html"><p class="t"></p></div>
|
||||
<div data-composition-id="${long}"><p class="t"></p></div>
|
||||
<div data-hf-original-composition-id="${long}" data-composition-id="c"><p class="t"></p></div>`;
|
||||
for (const p of document.querySelectorAll("p")) expect(await describeOne(p)).toBeNull();
|
||||
const { result } = await ask<never>("query", { selector: ".t" });
|
||||
expect(result).toEqual({ matches: [], truncated: true });
|
||||
|
||||
document.body.innerHTML = `<div data-composition-file="${long}.html"><i class="u"></i></div><i class="u"></i>`;
|
||||
const later = document.querySelectorAll("i")[1]!;
|
||||
later.style.setProperty(`--${long}`, "1");
|
||||
later.style.setProperty("left", "4px");
|
||||
const described = await describeOne(later);
|
||||
expect(described).toMatchObject({
|
||||
selector: ".u",
|
||||
matchesBefore: null,
|
||||
inlineStyles: { left: "4px" },
|
||||
truncated: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("answers a query with each match's handle and source, in document order", async () => {
|
||||
document.body.innerHTML = `<p class="t" id="a"></p>
|
||||
<div data-composition-file="cards.html"><p class="t" id="b"></p></div>`;
|
||||
|
||||
@@ -65,6 +65,15 @@ const sameSource = (a: EditingElementSource, b: EditingElementSource) =>
|
||||
a.compositionId === b.compositionId &&
|
||||
a.originalCompositionId === b.originalCompositionId;
|
||||
|
||||
/** The element's source when every field fits the port; null otherwise, since a cut path or id names another file. */
|
||||
function boundedSource(element: Element): EditingElementSource | null {
|
||||
const source = readElementSource(element);
|
||||
const fields = [source.file, source.compositionId, source.originalCompositionId];
|
||||
return fields.every((field) => field === null || field.length <= MAX_ELEMENT_VALUE)
|
||||
? source
|
||||
: null;
|
||||
}
|
||||
|
||||
function matchesBefore(element: Element, selector: string | undefined) {
|
||||
if (!selector?.startsWith(".")) return null;
|
||||
const groups: Array<{ source: EditingElementSource; count: number }> = [];
|
||||
@@ -76,7 +85,8 @@ function matchesBefore(element: Element, selector: string | undefined) {
|
||||
}
|
||||
for (const match of matches) {
|
||||
if (match === element) return groups;
|
||||
const source = readElementSource(match);
|
||||
const source = boundedSource(match);
|
||||
if (!source) return null;
|
||||
const group = groups.find((entry) => sameSource(entry.source, source));
|
||||
if (group) group.count += 1;
|
||||
else if (groups.push({ source, count: 1 }) > MAX_SOURCE_GROUPS) return null;
|
||||
@@ -105,14 +115,19 @@ function inlineStyles(element: Element, state: Cut): Record<string, string> {
|
||||
if (declared.length > MAX_ELEMENT_ENTRIES) state.truncated = true;
|
||||
for (let index = 0; index < Math.min(declared.length, MAX_ELEMENT_ENTRIES); index += 1) {
|
||||
const name = declared.item(index);
|
||||
if (name.length > MAX_ELEMENT_VALUE) {
|
||||
state.truncated = true;
|
||||
continue;
|
||||
}
|
||||
styles[name] = cut(declared.getPropertyValue(name), MAX_ELEMENT_VALUE, state, true);
|
||||
}
|
||||
return styles;
|
||||
}
|
||||
|
||||
function attributes(element: Element, state: Cut): EditingElementDescription["attributes"] {
|
||||
const kept = Array.from(element.attributes).filter((attribute) => attribute.name !== "style");
|
||||
if (kept.length > MAX_ELEMENT_ENTRIES) state.truncated = true;
|
||||
const named = Array.from(element.attributes).filter((attribute) => attribute.name !== "style");
|
||||
const kept = named.filter((attribute) => attribute.name.length <= MAX_ELEMENT_VALUE);
|
||||
if (kept.length < named.length || kept.length > MAX_ELEMENT_ENTRIES) state.truncated = true;
|
||||
return kept
|
||||
.slice(0, MAX_ELEMENT_ENTRIES)
|
||||
.map((attribute) => [attribute.name, cut(attribute.value, MAX_ELEMENT_VALUE, state, true)]);
|
||||
@@ -137,22 +152,27 @@ export function elementHit(element: Element): EditingElementHit {
|
||||
const rect = element.getBoundingClientRect();
|
||||
return {
|
||||
handle: handleFor(element),
|
||||
tag: element.tagName.toLowerCase(),
|
||||
tag: element.tagName.toLowerCase().slice(0, MAX_ELEMENT_VALUE),
|
||||
rect: { x: rect.x, y: rect.y, width: rect.width, height: rect.height },
|
||||
};
|
||||
}
|
||||
|
||||
function describeElement(element: Element): EditingElementDescription {
|
||||
const selector = buildStableSelector(element);
|
||||
const whole = (value: string | undefined) =>
|
||||
value && value.length <= MAX_ELEMENT_VALUE ? value : undefined;
|
||||
|
||||
function describeElement(element: Element): EditingElementDescription | null {
|
||||
const source = boundedSource(element);
|
||||
if (!source) return null;
|
||||
const selector = whole(buildStableSelector(element));
|
||||
const state: Cut = { truncated: false };
|
||||
return {
|
||||
...elementHit(element),
|
||||
label: buildElementLabel(element),
|
||||
label: buildElementLabel(element).slice(0, MAX_ELEMENT_VALUE),
|
||||
parent: element.parentElement ? handleFor(element.parentElement) : null,
|
||||
id: element.id || null,
|
||||
id: whole(element.id) ?? null,
|
||||
selector: selector ?? null,
|
||||
matchesBefore: matchesBefore(element, selector),
|
||||
source: readElementSource(element),
|
||||
source,
|
||||
locked: element.closest("[data-timeline-locked]") !== null,
|
||||
attributes: attributes(element, state),
|
||||
inlineStyles: inlineStyles(element, state),
|
||||
@@ -195,9 +215,12 @@ export function queryElements(selector: string): {
|
||||
truncated: boolean;
|
||||
} {
|
||||
const matches: EditingElementMatch[] = [];
|
||||
let truncated = false;
|
||||
for (const element of document.querySelectorAll(selector)) {
|
||||
if (matches.length === MAX_QUERY_MATCHES) return { matches, truncated: true };
|
||||
matches.push({ handle: handleFor(element), source: readElementSource(element) });
|
||||
const source = boundedSource(element);
|
||||
if (source) matches.push({ handle: handleFor(element), source });
|
||||
else truncated = true;
|
||||
}
|
||||
return { matches, truncated: false };
|
||||
return { matches, truncated };
|
||||
}
|
||||
|
||||
@@ -120,6 +120,16 @@ describe("previewEditingClient", () => {
|
||||
expect(Date.now() - started).toBeLessThan(AGENT_PROBE_MS + 500);
|
||||
});
|
||||
|
||||
it("answers a frame whose runtime never came at once, rather than after another probe", async () => {
|
||||
const { iframe } = opaqueFrame();
|
||||
const started = Date.now();
|
||||
await previewSeekPainted(iframe, 15_000);
|
||||
expect(Date.now() - started).toBeGreaterThanOrEqual(AGENT_PROBE_MS - 50);
|
||||
const again = Date.now();
|
||||
await previewSeekPainted(iframe, 15_000);
|
||||
expect(Date.now() - again).toBeLessThan(100);
|
||||
});
|
||||
|
||||
it("returns the runtime's manifest only when its reply has the manifest's shape", async () => {
|
||||
const manifest = { clips: [], durationInFrames: 30, scenes: [] };
|
||||
let reply: unknown = { clipManifest: manifest };
|
||||
|
||||
@@ -54,7 +54,15 @@ export function setFrameLoopStart(iframe: HTMLIFrameElement, seconds: number | n
|
||||
|
||||
export const AGENT_PROBE_MS = 1_000;
|
||||
|
||||
const agents = new WeakMap<EditingPortClient, boolean>();
|
||||
|
||||
async function agentConnected(client: EditingPortClient): Promise<boolean> {
|
||||
const known = agents.get(client);
|
||||
if (known !== undefined) return known;
|
||||
client.ready.then(
|
||||
() => agents.set(client, true),
|
||||
() => agents.set(client, false),
|
||||
);
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
const probe = new Promise<boolean>(
|
||||
(resolve) => (timer = setTimeout(resolve, AGENT_PROBE_MS, false)),
|
||||
@@ -67,6 +75,7 @@ async function agentConnected(client: EditingPortClient): Promise<boolean> {
|
||||
probe,
|
||||
]);
|
||||
clearTimeout(timer);
|
||||
if (!agents.has(client)) agents.set(client, connected);
|
||||
return connected;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { EditingElementDescription } from "@hyperframes/core/runtime/editing-port";
|
||||
import {
|
||||
MAX_ELEMENT_ENTRIES,
|
||||
MAX_ELEMENT_TEXT,
|
||||
MAX_ELEMENT_VALUE,
|
||||
type EditingElementDescription,
|
||||
} from "@hyperframes/core/runtime/editing-port";
|
||||
import {
|
||||
describePreviewElements,
|
||||
previewElementSelectorIndex,
|
||||
@@ -93,6 +98,34 @@ describe("readPreviewElement", () => {
|
||||
expect(readPreviewElement(bad)).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("refuses a description longer anywhere than the runtime ever sends one", () => {
|
||||
const value = "x".repeat(MAX_ELEMENT_VALUE);
|
||||
const atLimit = described(1, { selector: value, id: value, attributes: [["src", value]] });
|
||||
expect(readPreviewElement(atLimit)).toEqual(atLimit);
|
||||
const over = `${value}x`;
|
||||
const text = "y".repeat(MAX_ELEMENT_TEXT + 1);
|
||||
const many = Array.from({ length: MAX_ELEMENT_ENTRIES + 1 }, (_, i) => [`data-${i}`, ""]);
|
||||
for (const bad of [
|
||||
described(1, { selector: over }),
|
||||
described(1, { id: over }),
|
||||
described(1, { tag: over }),
|
||||
described(1, { label: over }),
|
||||
described(1, { attributes: [["src", over]] }),
|
||||
described(1, { attributes: [[`data-${over}`, "1"]] }),
|
||||
described(1, { attributes: many as never }),
|
||||
described(1, { inlineStyles: { left: over } }),
|
||||
described(1, { inlineStyles: { [`--${over}`]: "1" } }),
|
||||
described(1, { source: { ...NO_SOURCE, file: over } }),
|
||||
described(1, { source: { ...NO_SOURCE, compositionId: over } }),
|
||||
described(1, { source: { ...NO_SOURCE, originalCompositionId: over } }),
|
||||
described(1, { matchesBefore: [{ source: { ...NO_SOURCE, file: over }, count: 1 }] }),
|
||||
described(1, { text }),
|
||||
described(1, { children: [{ text }] }),
|
||||
]) {
|
||||
expect(readPreviewElement(bad)).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("element requests over an opaque frame's port", () => {
|
||||
|
||||
@@ -7,9 +7,11 @@ import {
|
||||
MAX_DESCRIBE_HANDLES,
|
||||
MAX_DRAFT_PX,
|
||||
MAX_ELEMENT_ENTRIES,
|
||||
MAX_ELEMENT_TEXT,
|
||||
MAX_ELEMENT_VALUE,
|
||||
MAX_GEOMETRY_CHILDREN,
|
||||
MAX_SNAP_TARGETS,
|
||||
MAX_SOURCE_GROUPS,
|
||||
type EditingElementBox,
|
||||
type EditingElementDescription,
|
||||
type EditingElementGeometry,
|
||||
@@ -33,29 +35,38 @@ export const isFields = (value: unknown): value is Fields =>
|
||||
export const isHandle = (value: unknown) =>
|
||||
typeof value === "string" && EDITING_ELEMENT_HANDLE.test(value);
|
||||
const isText = (value: unknown) => typeof value === "string";
|
||||
const isTextOrNull = (value: unknown) => value === null || typeof value === "string";
|
||||
const isValue = (value: unknown) => isText(value) && (value as string).length <= MAX_ELEMENT_VALUE;
|
||||
const isLongText = (value: unknown) =>
|
||||
isText(value) && (value as string).length <= MAX_ELEMENT_TEXT;
|
||||
const isValueOrNull = (value: unknown) => value === null || isValue(value);
|
||||
const isEntries = (value: unknown): value is unknown[] =>
|
||||
Array.isArray(value) && value.length <= MAX_ELEMENT_ENTRIES;
|
||||
const isCount = (value: unknown) => Number.isSafeInteger(value) && (value as number) >= 0;
|
||||
|
||||
const isSource = (value: unknown): value is EditingElementSource =>
|
||||
isFields(value) &&
|
||||
isTextOrNull(value.file) &&
|
||||
isTextOrNull(value.compositionId) &&
|
||||
isTextOrNull(value.originalCompositionId);
|
||||
isValueOrNull(value.file) &&
|
||||
isValueOrNull(value.compositionId) &&
|
||||
isValueOrNull(value.originalCompositionId);
|
||||
|
||||
const isTextRecord = (value: unknown) => isFields(value) && Object.values(value).every(isText);
|
||||
const isStyles = (value: unknown) =>
|
||||
isFields(value) &&
|
||||
isEntries(Object.keys(value)) &&
|
||||
Object.entries(value).every(([name, style]) => isValue(name) && isValue(style));
|
||||
|
||||
function hasPlainFields(value: Fields): boolean {
|
||||
const { matchesBefore, attributes, children } = value;
|
||||
const groupsOk =
|
||||
matchesBefore === null ||
|
||||
(Array.isArray(matchesBefore) &&
|
||||
matchesBefore.length <= MAX_SOURCE_GROUPS &&
|
||||
matchesBefore.every((g) => isFields(g) && isSource(g.source) && isCount(g.count)));
|
||||
const attributesOk =
|
||||
Array.isArray(attributes) &&
|
||||
attributes.every((a) => Array.isArray(a) && a.length === 2 && a.every(isText));
|
||||
isEntries(attributes) &&
|
||||
attributes.every((a) => Array.isArray(a) && a.length === 2 && a.every(isValue));
|
||||
const childrenOk =
|
||||
Array.isArray(children) &&
|
||||
children.every((c) => isFields(c) && (isHandle(c.handle) || isText(c.text)));
|
||||
isEntries(children) &&
|
||||
children.every((c) => isFields(c) && (isHandle(c.handle) || isLongText(c.text)));
|
||||
return groupsOk && attributesOk && childrenOk && typeof value.truncated === "boolean";
|
||||
}
|
||||
|
||||
@@ -63,7 +74,7 @@ const isRect = (value: unknown) =>
|
||||
isFields(value) && [value.x, value.y, value.width, value.height].every(Number.isFinite);
|
||||
|
||||
function readHit(value: unknown): PreviewElementHit | null {
|
||||
if (!isFields(value) || !isHandle(value.handle) || !isText(value.tag)) return null;
|
||||
if (!isFields(value) || !isHandle(value.handle) || !isValue(value.tag)) return null;
|
||||
return isRect(value.rect) ? (value as unknown as PreviewElementHit) : null;
|
||||
}
|
||||
|
||||
@@ -71,10 +82,10 @@ function readHit(value: unknown): PreviewElementHit | null {
|
||||
export function readPreviewElement(value: unknown): PreviewElement | null {
|
||||
if (!readHit(value) || !isFields(value)) return null;
|
||||
if (value.parent !== null && !isHandle(value.parent)) return null;
|
||||
if (value.label !== undefined && !isText(value.label)) return null;
|
||||
if (!isTextOrNull(value.id) || !isTextOrNull(value.selector)) return null;
|
||||
if (value.label !== undefined && !isValue(value.label)) return null;
|
||||
if (!isValueOrNull(value.id) || !isValueOrNull(value.selector)) return null;
|
||||
if (!isSource(value.source) || typeof value.locked !== "boolean") return null;
|
||||
if (!isTextRecord(value.inlineStyles) || !isText(value.text)) return null;
|
||||
if (!isStyles(value.inlineStyles) || !isLongText(value.text)) return null;
|
||||
return hasPlainFields(value) ? (value as unknown as PreviewElement) : null;
|
||||
}
|
||||
|
||||
@@ -183,8 +194,7 @@ const isSourceBox = (value: unknown) =>
|
||||
isSizeOrNull(value.declaredWidth) &&
|
||||
isSizeOrNull(value.declaredHeight));
|
||||
|
||||
const isClipPath = (value: unknown) =>
|
||||
value === null || (isText(value) && (value as string).length <= MAX_ELEMENT_VALUE);
|
||||
const isClipPath = isValueOrNull;
|
||||
|
||||
const isBox = (value: unknown): value is EditingElementBox =>
|
||||
isFields(value) &&
|
||||
|
||||
Reference in New Issue
Block a user