mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 19:46:11 +00:00
* feat(core): a composition's script can load its own files with __hyperframes.assetUrl * test(core): a template composition inside a mounted file resolves assets from that file * docs: assetUrl is for paths beside the composition, not project-root paths * docs: say assetUrl misses a project-root path * fix(core): a mounted composition's import map and module scripts run as such * refactor(core): scope a mounted module script under the same condition as a classic one * fix(core): a mounted module script loaded by src stays a module * fix(core): a mounted import map keeps the trailing slash of a prefix address * chore(core): keep the module-script helpers' comment share at the base
53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
export interface ExternalScriptAttributes {
|
|
integrity?: string;
|
|
crossorigin?: string;
|
|
type?: "module";
|
|
}
|
|
|
|
export function readExternalScriptAttributes(el: Element): ExternalScriptAttributes {
|
|
const attributes: ExternalScriptAttributes = {};
|
|
if (el.hasAttribute("integrity")) attributes.integrity = el.getAttribute("integrity") || "";
|
|
if (el.hasAttribute("crossorigin")) attributes.crossorigin = el.getAttribute("crossorigin") || "";
|
|
if ((el.getAttribute("type") || "").trim().toLowerCase() === "module") attributes.type = "module";
|
|
return attributes;
|
|
}
|
|
|
|
/** Deduplicate scripts without discarding a nested composition's integrity requirement. */
|
|
export function ensureExternalScriptTag(
|
|
doc: Document,
|
|
src: string,
|
|
attributes: ExternalScriptAttributes = {},
|
|
): void {
|
|
const existing = [...doc.querySelectorAll("script[src]")].filter(
|
|
(el) => el.getAttribute("src")?.trim() === src,
|
|
);
|
|
const requirements = new Set(
|
|
[attributes.integrity, ...existing.map((el) => el.getAttribute("integrity"))]
|
|
.map((value) =>
|
|
value
|
|
?.trim()
|
|
.replace(
|
|
/(^|[\t\n\f\r ])(sha256|sha384|sha512)-/gi,
|
|
(_match, space: string, algorithm: string) => `${space}${algorithm.toLowerCase()}-`,
|
|
),
|
|
)
|
|
.filter((value): value is string => Boolean(value)),
|
|
);
|
|
if (requirements.size > 1) {
|
|
throw new Error(`Conflicting script integrity requirements for ${src}`);
|
|
}
|
|
const integrity = [...requirements][0];
|
|
const elements = existing.length ? existing : [doc.createElement("script")];
|
|
for (const el of elements) {
|
|
if (integrity) el.setAttribute("integrity", integrity);
|
|
if (attributes.crossorigin !== undefined)
|
|
el.setAttribute("crossorigin", attributes.crossorigin);
|
|
if (attributes.type) el.setAttribute("type", attributes.type);
|
|
}
|
|
if (!existing.length) {
|
|
const el = elements[0]!;
|
|
el.setAttribute("src", src);
|
|
doc.body.appendChild(el);
|
|
}
|
|
}
|