mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
* feat(player): a composition can no longer read the page that embeds the player BREAKING CHANGE: the player's composition frame no longer shares the embedding page's origin by default. Add sandbox-origin="same-origin" to keep reading the composition's DOM or runtime directly (editors, recorders, custom timelines), only for compositions you trust. * chore(skills): refresh the slideshow skill's manifest hash * fix(player): a sealed player's timeout says how to play a composition without the runtime A sealed player cannot add the runtime to a composition, so one with a GSAP timeline and no runtime never became ready and failed with a bare timeout. The timeout now names the two fixes, the README says a sealed composition brings its own runtime, and a browser test covers the sealed, trusted and runtime-bundled paths. * test(player): the heavy-films bench embeds the trusted player, since it reads the film from inside * test(player): the bench host stays sealed; only the bench that reads the film asks for trust * test(player): the perf server answers CORS, so a sealed player's film loads its own media * test(player): a sealed film on a host without CORS must still play its videos * test(core): a sealed video whose CORS load fails loads again once without crossorigin * fix(core): a sealed film's video loads once more without CORS when its host refuses CORS * fix(core): the sealed media fallback reads its event target without a realm-bound instanceof * fix(player): keep the player's origin sharing as today; sealing stays opt-in with sandbox-origin * test(core): a failed sealed media source spares its siblings; a refused URL drops the helper's CORS * fix(core): a sealed film tries every CORS source before falling back and drops CORS for refused URLs * fix(cli): the play page shares its origin as today; docs say sealing is opt-in * refactor(core): split the sealed media fallback into small steps * test(player): sealed media decides CORS per element in Chrome; probe advice follows the policy * fix(core): each sealed media element owns its CORS choice; probe advice follows the policy * test(player): a sealed player's late timeline expects the sealed advice; media cases await each read * test(player): sealed media rows for a skipped first source, author writes and a recovered URL * fix(core): sealed media falls back only when the browser has no source left, once per URL * test(player): the skipped-source row's loads are never cached, so each one is counted * test(player): the skipped-source row bounds the video's load attempts as well as its requests * test(player): sealed media rows for a mismatched-source reload, an outage and two refusing sources * fix(core): a sealed media element owns one fallback state; only a played fallback is remembered * test(player): a generated table of sealed media edits, each element checked on its own * fix(core): sealed media CORS is decided per element and chosen source, with no shared cache * test(player): the media table counts the runtime's loads against other selections * fix(core): a sealed media element guesses the source it last played * test(player): the media table attributes loads exactly and lets a failing host stay down * test: sealed media rows check per-element CORS; the generated table replaces the hand-picked ones * fix(core): a media element's own src counts as the source that failed * fix(core): starting sealed media CORS twice adds no second set of listeners * test(player): judge the sealed media table against frames without the runtime * test(player): compare the sealed media table with the same film unsealed * test(player): run the unsealed control film before the sealed one in one tab * test(player): judge an authored crossorigin only on the composition's value * test(player): an all-own list returning to an upgraded URL keeps its pixels * test(player): source and playback against the sealed film with the helper off * test(player): count every runtime load of a table video, net of the film without the helper * test(player): the all-own return row plays the other clip alone before the URL comes back * test(player): judge every round of the sealed media table, not only the end * fix(core): sealed media keeps the browser's source choice and its playback across our reloads * test(player): the table plays a video only after the runtime has bound it * fix(core): a sealed video drops a stale crossorigin its source already refused * test(player): the table exempts foreign hosts from pixels and allows a range request per load * test(core): unit rows for the source choice, down hosts and playback across reloads * refactor(core): sealed media restores time through the media clock, and its failure path is split * test(player): the table compares play() outcomes, recovers a down host, and reads a seek at seeked * test(player): the table judges a seek by where it settles and lets a composition pause on rejection * test(player): the table holds the helper to a play() the film without it resolved * test(player): pin the generated sequences that found the recovery and play() faults * test(player): a slow host keeps the composition's play() pending when its source fails * test(player): pin the sequences that find the play() and recovery faults with the slow host * test(player): the table can start its generated sequences at any seed * test(player): pin the generated sequences that find the recovered source skipped * test(player): keep the pinned sequences that are red in every run in both browsers * test(player): a looping video starting over is not a seek after the composition's * fix(core): a sealed video retries a recovered source and keeps the composition's play() alive * test(core): the playback row watches the play() the runtime's retry makes * test(player): a slow host delays its first answer only; the composition's own abort is skipped * test(player): a loop restart is told from a seek by where the seek began * test(player): restarts are read at metadata, and a buffering control judges no playback * test(player): the table counts and limits the comparisons it skips * test(player): our restore after a composition seek stays within a frame; all selections count * test(player): a playhead counts as moving if it moves within a second * fix(core): only the composition's own load() makes a sealed video retry a down source * fix(core): a sealed video's down source stays down through the runtime's preload load * test(player): judge sealed media rows by what each step makes possible, not page timing * test(player): print sealed and unsealed seek landings each sealed media run * test(player): read a looping clip after its loop restart in every film alike * fix(core): the runtime's preload loads a media element once per source list * fix(core): the runtime's prefetch shares the preload's one load per source list * test(player): a table step interrupts the CORS handling's own reload, not the runtime's preload * test(core): an undo puts a clip's colour grade back in a sealed preview in place * fix(core): an undo of a colour grade applies in place in a sealed preview * test(player): a second table seek lands during the CORS reload of a playing clip * test(core): an undo in a sealed preview keeps the media attributes the runtime added * fix(core): an undo in a sealed preview keeps the media attributes the runtime added * test(player): the playing-clip seek row plays a clip that starts past 0, as a trimmed one does * test(player): the playing-clip seek is armed with its video, before the play step waits * test(player): the down-then-back row reloads again, so every film's control has a frame * test(player): the playing-clip seek row's host answers late, so the clip plays before its reload * fix(player): a sealed player plays a same-site composition with the runtime it ships * fix(player): a switch to video unloads a sealed composition shown as srcdoc * test(core): the runtime's preload mid-selection keeps a down source's mark * fix(core): a sealed film's down source keeps its mark through the runtime's own loads * chore(player): fallow reads the cjs and iife runtime file as an entry * fix(core): declare the html-tokens subpath where package exports are generated * fix(player): a sealed composition that brings its runtime loads by src under its own headers * test(player): expect the sealed player's own timeout message in the late-runtime tests * test(core): a grade undo on a far clip whose length is loading applies in place * fix(core): a far clip's length probe records the preload it sets, so a grade undo applies * fix(player): a sealed film shown as srcdoc keeps its URL, so its sound still plays * test(player): the late-timeline switch uses a film from another origin, which must still fail * fix(player): a sealed Studio preview keeps its own runtime and loads by src * fix(player): the player package carries the runtime once, and Studio none * fix(core): a sealed video reads its own source's pixels in a list that names another host * fix(player): a sealed player with no bundler still gets the runtime, only when it needs it * fix(core): keep the mixed-list change within the comment budget * fix(core): opt a mixed list in only when its first source plays, so a reload keeps that video * fix(core): a CORS reload keeps the source that was playing * fix(core): the composition's own load() picks from its full source list again * fix(core): a reload's pin keeps a source type the composition wrote meanwhile * fix(core): a reload's pin stops at the source the browser picked, not a skipped copy of its URL * fix(core): a reload's pin stops at the source element that played, not the first copy of its URL * fix(core): a reload's pin keeps the source element that played or refused * style(core): format the source pin * style(player): keep the runtime source comment under the line limit * fix(player): only a real </script> closing tag ends a script in the runtime check * test(player): count the sealed helper's loads by the crossorigin they change * fix(core): a sealed video asks its host once before treating a failed CORS load as a refusal * test(player): the sealed table expects pixels from a host that answers CORS after a failed load * test(core): each refusal row waits for the CORS question it starts * test(player): the sealed media table waits on each clip's own events * refactor(core): a pinned-out source's error gets its own check in the sealed media helper * test(player): the loop restart wait keeps its two-second window * style(player): format the table's event wait * chore(fallow): resolve workspace exports to source as bun does, so no false package cycle * refactor(player): read script tag attributes in small steps, the same result * refactor(player): the shader loading attribute reads its words from one table * refactor(player): the attribute helpers' names say what their comments did * fix(core): the CORS question never follows a redirect, and stays quiet while it waits * style(core): format the CORS question's rows * test(player): a sealed same-site page keeps its own policy; a stuck fetch reports the limit * fix(player): a sealed same-site page keeps its own policy, and a stuck fetch ends at the limit * fix(player): a sealed page whose headers can't all be read, as after a redirect, loads by src * style(player): format the sealed player and name the probe's preparation state
197 lines
7.7 KiB
TypeScript
197 lines
7.7 KiB
TypeScript
import { RUNTIME_BOOTSTRAP_ATTR } from "@hyperframes/core/compiler/html-tokens";
|
|
|
|
/**
|
|
* Put the runtime in the document's head, before anything in the body runs.
|
|
*
|
|
* The probe injects the runtime by appending a `<script src>` to an already
|
|
* loaded document, and only once it has a reason to: a nested composition, or
|
|
* five polls with a timeline present. Both are far too late for a COMPONENT.
|
|
*
|
|
* A component is markup pasted into a composition, and it reads its values in
|
|
* an inline IIFE that runs while the body is being parsed:
|
|
*
|
|
* var vars = window.__hyperframes && window.__hyperframes.getVariables
|
|
* ? window.__hyperframes.getVariables()
|
|
* : {};
|
|
*
|
|
* With the runtime arriving afterwards, that guard always took the empty
|
|
* branch, so the component fell back to the defaults hardcoded in its own
|
|
* script and every chosen value was ignored. On the catalog page that looked
|
|
* like the customise panel doing nothing: badge-pop with count 10 and a green
|
|
* accent rendered 3, in red.
|
|
*
|
|
* A classic external `<script src>` in `<head>` is parser-blocking, so moving
|
|
* the same URL into the srcdoc fixes the ordering without changing what is
|
|
* loaded. A CLI render never had this problem because the engine already puts
|
|
* the runtime ahead of body scripts.
|
|
*/
|
|
|
|
/** Already carrying the runtime: a CLI-rendered page, or a second preparation. */
|
|
function alreadyHasRuntime(html: string, runtimeUrl: string | null): boolean {
|
|
if (runtimeUrl && html.includes(runtimeUrl)) return true;
|
|
// The engine inlines the runtime rather than linking it, and it assigns this global on the way in.
|
|
return /__hyperframes\s*=(?!=)/.test(html) || linksRuntime(html);
|
|
}
|
|
|
|
/** A page that brings the runtime itself, which a sealed player then loads by `src` as before. */
|
|
export const carriesRuntime = (html: string): boolean => alreadyHasRuntime(html, null);
|
|
|
|
function linksRuntime(html: string): boolean {
|
|
for (
|
|
let tag = findOpeningTag(html, "script");
|
|
tag;
|
|
tag = findOpeningTag(html, "script", afterScript(html, tag.end))
|
|
) {
|
|
const attributes = tagAttributes(html.slice(tag.index + "<script".length, tag.end - 1));
|
|
if (attributes.has(RUNTIME_BOOTSTRAP_ATTR)) return true;
|
|
if (attributes.get("src")?.includes("hyperframe.runtime.iife.js")) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function afterScript(html: string, from: number): number {
|
|
const lower = html.toLowerCase();
|
|
for (
|
|
let close = lower.indexOf("</script", from);
|
|
close >= 0;
|
|
close = lower.indexOf("</script", close + 1)
|
|
)
|
|
if (CLOSING_TAG_BOUNDARIES.has(lower[close + "</script".length])) return close;
|
|
return html.length;
|
|
}
|
|
|
|
const isSpace = (c: string) => c === " " || c === "\t" || c === "\n" || c === "\r" || c === "\f";
|
|
const isGap = (c: string) => isSpace(c) || c === "/";
|
|
const inName = (c: string) => !isGap(c) && c !== "=";
|
|
const notSpace = (c: string) => !isSpace(c);
|
|
|
|
function skip(text: string, from: number, more: (c: string) => boolean): number {
|
|
let i = from;
|
|
while (i < text.length && more(text.charAt(i))) i++;
|
|
return i;
|
|
}
|
|
|
|
function attributeValue(text: string, from: number): { value: string; end: number } {
|
|
const quote = text.charAt(from);
|
|
if (quote !== '"' && quote !== "'") {
|
|
const end = skip(text, from, notSpace);
|
|
return { value: text.slice(from, end), end };
|
|
}
|
|
const close = text.indexOf(quote, from + 1);
|
|
const end = close < 0 ? text.length : close;
|
|
return { value: text.slice(from + 1, end), end: end + 1 };
|
|
}
|
|
|
|
function tagAttributes(text: string): Map<string, string> {
|
|
const attributes = new Map<string, string>();
|
|
for (let i = skip(text, 0, isGap); i < text.length; i = skip(text, i, isGap)) {
|
|
const start = i;
|
|
i = skip(text, i, inName);
|
|
if (i === start) break;
|
|
const name = text.slice(start, i).toLowerCase();
|
|
i = skip(text, i, isSpace);
|
|
let value = "";
|
|
if (text.charAt(i) === "=")
|
|
({ value, end: i } = attributeValue(text, skip(text, i + 1, isSpace)));
|
|
attributes.set(name, value);
|
|
}
|
|
return attributes;
|
|
}
|
|
|
|
type OpeningTag = { index: number; end: number };
|
|
const OPENING_TAG_BOUNDARIES = new Set<string | undefined>([">", " ", "\t", "\n", "\r", "\f"]);
|
|
const CLOSING_TAG_BOUNDARIES = new Set<string | undefined>([...OPENING_TAG_BOUNDARIES, "/"]);
|
|
|
|
/** Find an opening tag in one linear pass, without a backtracking regex over caller-owned HTML. */
|
|
function findOpeningTag(html: string, tagName: string, start = 0): OpeningTag | null {
|
|
const lower = html.toLowerCase();
|
|
const prefix = `<${tagName}`;
|
|
let from = start;
|
|
while (from < lower.length) {
|
|
const index = lower.indexOf(prefix, from);
|
|
if (index < 0) return null;
|
|
const comment = lower.indexOf("<!--", from);
|
|
if (comment >= 0 && comment < index) {
|
|
const closed = lower.indexOf("-->", comment + 4);
|
|
if (closed < 0) return null;
|
|
from = closed + 3;
|
|
continue;
|
|
}
|
|
const boundary = lower[index + prefix.length];
|
|
if (OPENING_TAG_BOUNDARIES.has(boundary)) {
|
|
const close = lower.indexOf(">", index + prefix.length);
|
|
if (close < 0) return null;
|
|
return { index, end: close + 1 };
|
|
}
|
|
from = index + prefix.length;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function ensureRuntimeBeforeBodyScripts(html: string, runtimeUrl: string): string {
|
|
if (!html || alreadyHasRuntime(html, runtimeUrl)) return html;
|
|
return insertAtHeadStart(html, `<script src="${runtimeUrl}"></script>`);
|
|
}
|
|
|
|
/** The same placement with the runtime's own text, for a sealed frame: it then loads nothing from another origin. */
|
|
export function inlineRuntimeBeforeBodyScripts(html: string, escapedSource: string): string {
|
|
if (!html || alreadyHasRuntime(html, null)) return html;
|
|
return insertAtHeadStart(html, `<script>${escapedSource}</script>`);
|
|
}
|
|
|
|
/**
|
|
* A document fetched from `url` and shown as srcdoc still resolves its relative paths there. The first
|
|
* `<base href>` wins, so the page's own one is resolved against `url` and put first.
|
|
*/
|
|
export function withBaseHref(html: string, url: string): string {
|
|
return insertAtHeadStart(html, `<base href="${escapeAttr(baseOf(html, url))}">`);
|
|
}
|
|
|
|
function baseOf(html: string, url: string): string {
|
|
for (let tag = findOpeningTag(html, "base"); tag; tag = findOpeningTag(html, "base", tag.end)) {
|
|
const href = /\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(
|
|
html.slice(tag.index, tag.end),
|
|
);
|
|
if (!href) continue;
|
|
try {
|
|
return new URL(href[1] ?? href[2] ?? href[3] ?? "", url).href;
|
|
} catch {
|
|
return url;
|
|
}
|
|
}
|
|
return url;
|
|
}
|
|
|
|
/** The bytes of a fetched page as the browser would read them: the response's charset, else the page's own. */
|
|
export function decodePage(bytes: ArrayBuffer, contentType: string | null): string {
|
|
const declared =
|
|
/charset=["']?([\w-]+)/i.exec(contentType ?? "")?.[1] ??
|
|
/<meta[^>]{0,200}charset=["']?([\w-]+)/i.exec(
|
|
new TextDecoder("latin1").decode(bytes.slice(0, 1024)),
|
|
)?.[1];
|
|
try {
|
|
return new TextDecoder(declared ?? "utf-8").decode(bytes);
|
|
} catch {
|
|
return new TextDecoder().decode(bytes);
|
|
}
|
|
}
|
|
|
|
const escapeAttr = (value: string): string =>
|
|
value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
|
|
function insertAtHeadStart(html: string, tag: string): string {
|
|
const head = findOpeningTag(html, "head");
|
|
if (head) {
|
|
return html.slice(0, head.end) + tag + html.slice(head.end);
|
|
}
|
|
// No head: get in before <body> so body scripts still see the runtime. A
|
|
// fragment with neither lands at the front, which is the same guarantee.
|
|
const body = findOpeningTag(html, "body");
|
|
if (body) return html.slice(0, body.index) + tag + html.slice(body.index);
|
|
const htmlTag = findOpeningTag(html, "html");
|
|
if (htmlTag) {
|
|
return html.slice(0, htmlTag.end) + tag + html.slice(htmlTag.end);
|
|
}
|
|
return tag + html;
|
|
}
|