@hyperframes/producer 0.9.64 → 0.9.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/distributed.js +190 -79
- package/dist/hyperframe.manifest.json +1 -1
- package/dist/hyperframe.runtime.iife.js +72 -71
- package/dist/index.js +190 -79
- package/dist/public-server.js +190 -79
- package/dist/services/compositionSizeLimit.d.ts +5 -1
- package/dist/services/htmlCompiler.d.ts +1 -6
- package/package.json +7 -7
package/dist/distributed.js
CHANGED
|
@@ -2472,9 +2472,35 @@ var fileTooLarge = (file, bytes) => new CompositionTooLargeError(
|
|
|
2472
2472
|
`Composition file "${file}" is ${megabytes(bytes)}, over the ${megabytes(MAX_COMPOSITION_HTML_BYTES)} limit for one composition HTML file. Keep large data such as images, fonts and JSON in separate asset files and reference them by path instead of inlining them.`,
|
|
2473
2473
|
{ bytes, limitBytes: MAX_COMPOSITION_HTML_BYTES }
|
|
2474
2474
|
);
|
|
2475
|
+
var limitText = `One render generates at most ${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)}`;
|
|
2476
|
+
function generatedTooLarge(file, bytes) {
|
|
2477
|
+
if (file === "composition variables") {
|
|
2478
|
+
return `Composition variable styles add up to ${megabytes(bytes)}. ${limitText}. Shorten the variable values or declare them on fewer elements.`;
|
|
2479
|
+
}
|
|
2480
|
+
if (file === "variable media sources") {
|
|
2481
|
+
return `Variable-bound media sources add up to ${megabytes(bytes)}. ${limitText}. Use shorter URLs or bind fewer elements.`;
|
|
2482
|
+
}
|
|
2483
|
+
return `Scripts, styles and rebased asset paths generated for compositions, ending at "${file}", add up to ${megabytes(bytes)}. One render generates at most ${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)} for its mounts. Merge small inline scripts and style rules, keep data-composition-id values short, or mount scenes fewer times.`;
|
|
2484
|
+
}
|
|
2475
2485
|
var CompositionExpansionBudget = class {
|
|
2476
2486
|
bytes = 0;
|
|
2477
2487
|
mounts = 0;
|
|
2488
|
+
generated = 0;
|
|
2489
|
+
/** `bytes` may be negative when a reserved upper bound is settled to the real size. */
|
|
2490
|
+
chargeGenerated(file, bytes) {
|
|
2491
|
+
this.generated += bytes;
|
|
2492
|
+
if (this.generated <= MAX_EXPANDED_COMPOSITION_BYTES) return;
|
|
2493
|
+
throw new CompositionTooLargeError(generatedTooLarge(file, this.generated), {
|
|
2494
|
+
bytes: this.generated,
|
|
2495
|
+
limitBytes: MAX_EXPANDED_COMPOSITION_BYTES,
|
|
2496
|
+
exceeded: "generated"
|
|
2497
|
+
});
|
|
2498
|
+
}
|
|
2499
|
+
chargeGeneratedIfItFits(bytes) {
|
|
2500
|
+
if (this.generated + bytes > MAX_EXPANDED_COMPOSITION_BYTES) return false;
|
|
2501
|
+
this.generated += bytes;
|
|
2502
|
+
return true;
|
|
2503
|
+
}
|
|
2478
2504
|
charge(file, bytes) {
|
|
2479
2505
|
this.bytes += bytes;
|
|
2480
2506
|
this.mounts += 1;
|
|
@@ -4185,7 +4211,7 @@ function coalesceHeadStylesAndBodyScripts(html) {
|
|
|
4185
4211
|
}
|
|
4186
4212
|
return document2.toString();
|
|
4187
4213
|
}
|
|
4188
|
-
function inlineSubCompositions(html, subCompositions, projectDir, variableOverrides, assetRoot, outsideFiles) {
|
|
4214
|
+
function inlineSubCompositions(html, subCompositions, projectDir, variableOverrides, assetRoot, outsideFiles, budget) {
|
|
4189
4215
|
const { document: document2 } = parseHTML5(html);
|
|
4190
4216
|
const head = document2.querySelector("head");
|
|
4191
4217
|
const body = document2.querySelector("body");
|
|
@@ -4194,11 +4220,11 @@ function inlineSubCompositions(html, subCompositions, projectDir, variableOverri
|
|
|
4194
4220
|
const emitted = emitRootCompositionVariableStyles(
|
|
4195
4221
|
document2,
|
|
4196
4222
|
{},
|
|
4197
|
-
variableOverrides
|
|
4223
|
+
variableOverrides,
|
|
4224
|
+
{ chargeBytes: (bytes) => budget.chargeGenerated("composition variables", bytes) }
|
|
4198
4225
|
);
|
|
4199
4226
|
return { html: emitted ? document2.toString() : html, variablesByComp: {} };
|
|
4200
4227
|
}
|
|
4201
|
-
const budget = new CompositionExpansionBudget();
|
|
4202
4228
|
const readOnce = /* @__PURE__ */ new Map();
|
|
4203
4229
|
const readMountHtml = (srcPath) => {
|
|
4204
4230
|
const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot);
|
|
@@ -4233,11 +4259,16 @@ function inlineSubCompositions(html, subCompositions, projectDir, variableOverri
|
|
|
4233
4259
|
hostIdentityMap: hostIdentityByElement,
|
|
4234
4260
|
readVariableDefaults: readDeclaredDefaults,
|
|
4235
4261
|
parseHostVariables: parseHostVariableValues,
|
|
4262
|
+
resolveFilePath: (srcPath) => {
|
|
4263
|
+
const file = resolveCompositionFile(projectDir, srcPath, assetRoot);
|
|
4264
|
+
return file && relative2(projectDir, file).split(sep3).join("/");
|
|
4265
|
+
},
|
|
4236
4266
|
resolveHtml: (srcPath) => {
|
|
4237
4267
|
const compHtml = resolveMountHtml(srcPath);
|
|
4238
4268
|
if (compHtml) budget.charge(srcPath, Buffer.byteLength(compHtml));
|
|
4239
4269
|
return compHtml;
|
|
4240
4270
|
},
|
|
4271
|
+
chargeEmitted: (srcPath, bytes) => budget.chargeGenerated(srcPath, bytes),
|
|
4241
4272
|
parseHtml: (htmlStr) => parseHTML5(htmlStr).document,
|
|
4242
4273
|
// Mirrors the preview bundler: a sub-composition's SIBLING assets resolve
|
|
4243
4274
|
// against its own directory, project-root refs stay as authored.
|
|
@@ -4322,7 +4353,8 @@ function inlineSubCompositions(html, subCompositions, projectDir, variableOverri
|
|
|
4322
4353
|
emitRootCompositionVariableStyles(
|
|
4323
4354
|
document2,
|
|
4324
4355
|
result.variablesByComp,
|
|
4325
|
-
variableOverrides
|
|
4356
|
+
variableOverrides,
|
|
4357
|
+
{ chargeBytes: (bytes) => budget.chargeGenerated("composition variables", bytes) }
|
|
4326
4358
|
);
|
|
4327
4359
|
assignMediaRenderIds(document2);
|
|
4328
4360
|
return { html: document2.toString(), variablesByComp: result.variablesByComp };
|
|
@@ -4744,10 +4776,6 @@ function localizeFontFaceUrls(html, downloadDir) {
|
|
|
4744
4776
|
(h, url, relPath) => h.replaceAll(`url(${url})`, `url("${relPath}")`)
|
|
4745
4777
|
);
|
|
4746
4778
|
}
|
|
4747
|
-
function urlOccurrenceRe(localPath) {
|
|
4748
|
-
const escaped = localPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4749
|
-
return new RegExp(`url\\((["']?)${escaped}\\1\\)`, "g");
|
|
4750
|
-
}
|
|
4751
4779
|
var MAX_LOCAL_FONT_DATA_URI_BYTES = 5 * 1024 * 1024;
|
|
4752
4780
|
async function readLocalFont(absPath) {
|
|
4753
4781
|
const chunks = [];
|
|
@@ -4762,59 +4790,83 @@ async function readLocalFont(absPath) {
|
|
|
4762
4790
|
}
|
|
4763
4791
|
return { kind: "inline", buffer: Buffer.concat(chunks, totalBytes) };
|
|
4764
4792
|
}
|
|
4765
|
-
async function
|
|
4766
|
-
const
|
|
4767
|
-
|
|
4768
|
-
|
|
4793
|
+
async function localFontDataUri(localPath, projectDir, assetRoot, dataUriByAbsolutePath, log) {
|
|
4794
|
+
const absPath = localPath.startsWith("/") ? localPath : resolve6(projectDir, localPath);
|
|
4795
|
+
if (!isPathInside(absPath, projectDir) || escapesAssetRoot(absPath, assetRoot)) return void 0;
|
|
4796
|
+
if (refusesOutsideLink(projectDir, absPath, (line) => log.warn(`[Compiler] ${line}`))) {
|
|
4797
|
+
return void 0;
|
|
4798
|
+
}
|
|
4799
|
+
if (isPrivateCompositionFile(projectDir, absPath)) return void 0;
|
|
4800
|
+
if (dataUriByAbsolutePath.has(absPath)) return dataUriByAbsolutePath.get(absPath);
|
|
4801
|
+
const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf";
|
|
4802
|
+
try {
|
|
4803
|
+
const font = await readLocalFont(absPath);
|
|
4804
|
+
if (font.kind === "file-backed") {
|
|
4805
|
+
dataUriByAbsolutePath.set(absPath, void 0);
|
|
4806
|
+
defaultLogger.debug(
|
|
4807
|
+
`[Compiler] Kept large local font file-backed: ${localPath} (> ${(MAX_LOCAL_FONT_DATA_URI_BYTES / 1024 / 1024).toFixed(1)} MB)`
|
|
4808
|
+
);
|
|
4809
|
+
return void 0;
|
|
4810
|
+
}
|
|
4811
|
+
const { fontToDataUri } = await import("@hyperframes/core/fonts/embed");
|
|
4812
|
+
const dataUri = await fontToDataUri(font.buffer, ext, { compressToWoff2: false });
|
|
4813
|
+
dataUriByAbsolutePath.set(absPath, dataUri);
|
|
4814
|
+
defaultLogger.debug(
|
|
4815
|
+
`[Compiler] Embedded local font file: ${localPath} (${(font.buffer.length / 1024).toFixed(0)} KB \u2192 data URI)`
|
|
4816
|
+
);
|
|
4817
|
+
return dataUri;
|
|
4818
|
+
} catch (error) {
|
|
4819
|
+
defaultLogger.warn(
|
|
4820
|
+
`[Compiler] Could not embed local font ${localPath}: ${error instanceof Error ? error.message : String(error)}`
|
|
4821
|
+
);
|
|
4822
|
+
return void 0;
|
|
4823
|
+
}
|
|
4824
|
+
}
|
|
4825
|
+
var replaceInner = (whole, inner, tail, next) => whole.slice(0, whole.length - tail - inner.length) + next + whole.slice(whole.length - tail);
|
|
4826
|
+
async function embedLocalFontFaces(html, projectDir, assetRoot, budget, log = defaultLogger) {
|
|
4827
|
+
const dataUriByPath = /* @__PURE__ */ new Map();
|
|
4769
4828
|
const dataUriByAbsolutePath = /* @__PURE__ */ new Map();
|
|
4770
|
-
const fileBackedAbsolutePaths = /* @__PURE__ */ new Set();
|
|
4771
4829
|
for (const { parts: style } of styleBlockMatches(html)) {
|
|
4772
4830
|
for (const { parts: face } of fontFaceBlockMatches(style[1])) {
|
|
4773
|
-
const
|
|
4774
|
-
for (const { parts } of localFontFaceUrlMatches(block)) {
|
|
4831
|
+
for (const { parts } of localFontFaceUrlMatches(face[1])) {
|
|
4775
4832
|
const localPath = parts[1];
|
|
4776
|
-
if (!localPath ||
|
|
4777
|
-
|
|
4778
|
-
|
|
4779
|
-
|
|
4780
|
-
|
|
4781
|
-
continue;
|
|
4782
|
-
}
|
|
4783
|
-
if (isPrivateCompositionFile(projectDir, absPath)) continue;
|
|
4784
|
-
const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf";
|
|
4785
|
-
try {
|
|
4786
|
-
if (fileBackedAbsolutePaths.has(absPath)) {
|
|
4787
|
-
embeddedPaths.add(localPath);
|
|
4788
|
-
continue;
|
|
4789
|
-
}
|
|
4790
|
-
let dataUri = dataUriByAbsolutePath.get(absPath);
|
|
4791
|
-
if (!dataUri) {
|
|
4792
|
-
const font = await readLocalFont(absPath);
|
|
4793
|
-
if (font.kind === "file-backed") {
|
|
4794
|
-
fileBackedAbsolutePaths.add(absPath);
|
|
4795
|
-
defaultLogger.debug(
|
|
4796
|
-
`[Compiler] Kept large local font file-backed: ${localPath} (> ${(MAX_LOCAL_FONT_DATA_URI_BYTES / 1024 / 1024).toFixed(1)} MB)`
|
|
4797
|
-
);
|
|
4798
|
-
embeddedPaths.add(localPath);
|
|
4799
|
-
continue;
|
|
4800
|
-
}
|
|
4801
|
-
dataUri = await toDataUri(font.buffer, ext, { compressToWoff2: false });
|
|
4802
|
-
dataUriByAbsolutePath.set(absPath, dataUri);
|
|
4803
|
-
defaultLogger.debug(
|
|
4804
|
-
`[Compiler] Embedded local font file: ${localPath} (${(font.buffer.length / 1024).toFixed(0)} KB \u2192 data URI)`
|
|
4805
|
-
);
|
|
4806
|
-
}
|
|
4807
|
-
result = result.replace(urlOccurrenceRe(localPath), `url("${dataUri}")`);
|
|
4808
|
-
embeddedPaths.add(localPath);
|
|
4809
|
-
} catch (error) {
|
|
4810
|
-
defaultLogger.warn(
|
|
4811
|
-
`[Compiler] Could not embed local font ${localPath}: ${error instanceof Error ? error.message : String(error)}`
|
|
4812
|
-
);
|
|
4813
|
-
}
|
|
4833
|
+
if (!localPath || dataUriByPath.has(localPath)) continue;
|
|
4834
|
+
dataUriByPath.set(
|
|
4835
|
+
localPath,
|
|
4836
|
+
await localFontDataUri(localPath, projectDir, assetRoot, dataUriByAbsolutePath, log)
|
|
4837
|
+
);
|
|
4814
4838
|
}
|
|
4815
4839
|
}
|
|
4816
4840
|
}
|
|
4817
|
-
return
|
|
4841
|
+
if (![...dataUriByPath.values()].some(Boolean)) return html;
|
|
4842
|
+
let warned = false;
|
|
4843
|
+
const embed = (source, localPath) => {
|
|
4844
|
+
const dataUri = dataUriByPath.get(localPath);
|
|
4845
|
+
if (!dataUri) return source;
|
|
4846
|
+
const inlined = `url("${dataUri}")`;
|
|
4847
|
+
if (budget.chargeGeneratedIfItFits(inlined.length - source.length)) return inlined;
|
|
4848
|
+
if (!warned) {
|
|
4849
|
+
warned = true;
|
|
4850
|
+
log.warn(
|
|
4851
|
+
`[Compiler] Kept local font ${localPath} file-backed: the page reached its inline size limit`
|
|
4852
|
+
);
|
|
4853
|
+
}
|
|
4854
|
+
return source;
|
|
4855
|
+
};
|
|
4856
|
+
return replaceMatches(
|
|
4857
|
+
html,
|
|
4858
|
+
styleBlockMatches(html),
|
|
4859
|
+
(style, css) => replaceInner(
|
|
4860
|
+
style,
|
|
4861
|
+
css,
|
|
4862
|
+
"</style>".length,
|
|
4863
|
+
replaceMatches(
|
|
4864
|
+
css,
|
|
4865
|
+
fontFaceBlockMatches(css),
|
|
4866
|
+
(face, body) => replaceInner(face, body, 1, replaceMatches(body, localFontFaceUrlMatches(body), embed))
|
|
4867
|
+
)
|
|
4868
|
+
)
|
|
4869
|
+
);
|
|
4818
4870
|
}
|
|
4819
4871
|
function renderGsapDist() {
|
|
4820
4872
|
try {
|
|
@@ -4840,32 +4892,42 @@ function rewriteUnresolvableGsapToCdn(html, projectDir, compSrcPath) {
|
|
|
4840
4892
|
return `${prefix}${gsapDist}${file}${suffix}`;
|
|
4841
4893
|
});
|
|
4842
4894
|
}
|
|
4843
|
-
function rebaseDirectEntryAssetPaths(html, projectDir, htmlPath) {
|
|
4895
|
+
function rebaseDirectEntryAssetPaths(html, projectDir, htmlPath, budget) {
|
|
4844
4896
|
if (!isPathInside(htmlPath, projectDir)) return html;
|
|
4845
4897
|
const entryPath = relative2(projectDir, htmlPath).replace(/\\/g, "/");
|
|
4846
4898
|
if (!entryPath.includes("/")) return html;
|
|
4847
4899
|
const { document: document2 } = parseHTML5(html);
|
|
4848
4900
|
const assetExists = projectAssetExists(projectDir);
|
|
4901
|
+
const charge = (bytes) => budget.chargeGenerated(entryPath, bytes);
|
|
4849
4902
|
rewriteAssetPaths(
|
|
4850
4903
|
document2.querySelectorAll("[src], [href], [srcset]"),
|
|
4851
4904
|
entryPath,
|
|
4852
4905
|
(el, attr) => el.getAttribute(attr),
|
|
4853
4906
|
(el, attr, value) => el.setAttribute(attr, value),
|
|
4854
|
-
assetExists
|
|
4907
|
+
assetExists,
|
|
4908
|
+
charge
|
|
4855
4909
|
);
|
|
4856
4910
|
rewriteInlineStyleAssetUrls(
|
|
4857
4911
|
document2.querySelectorAll("[style]"),
|
|
4858
4912
|
entryPath,
|
|
4859
4913
|
(el) => el.getAttribute("style"),
|
|
4860
4914
|
(el, value) => el.setAttribute("style", value),
|
|
4861
|
-
assetExists
|
|
4915
|
+
assetExists,
|
|
4916
|
+
charge
|
|
4862
4917
|
);
|
|
4863
4918
|
for (const style of document2.querySelectorAll("style")) {
|
|
4864
|
-
style.textContent = rewriteCssAssetUrls(
|
|
4919
|
+
style.textContent = rewriteCssAssetUrls(
|
|
4920
|
+
style.textContent || "",
|
|
4921
|
+
entryPath,
|
|
4922
|
+
assetExists,
|
|
4923
|
+
charge
|
|
4924
|
+
);
|
|
4865
4925
|
}
|
|
4866
4926
|
return document2.toString();
|
|
4867
4927
|
}
|
|
4868
|
-
|
|
4928
|
+
var MAX_VARIABLE_MEDIA_SRC_BYTES = 8 * 1024;
|
|
4929
|
+
var MEDIA_SRC_CONTROL_CHARS = /[\u0000-\u001f\u007f]/;
|
|
4930
|
+
function bindVariableMediaSources(html, variables, budget, log) {
|
|
4869
4931
|
if (!html.includes("data-var-src")) return html;
|
|
4870
4932
|
const { document: document2 } = parseHTML5(html);
|
|
4871
4933
|
const declared = {};
|
|
@@ -4876,8 +4938,17 @@ function bindVariableMediaSources(html, variables) {
|
|
|
4876
4938
|
let changed = false;
|
|
4877
4939
|
for (const el of document2.querySelectorAll("video[data-var-src], audio[data-var-src]")) {
|
|
4878
4940
|
if (el.closest("[data-variable-values]")) continue;
|
|
4879
|
-
const
|
|
4880
|
-
if (
|
|
4941
|
+
const bound = boundMediaSrc(el, values);
|
|
4942
|
+
if (bound === null) continue;
|
|
4943
|
+
const src = bound.trim();
|
|
4944
|
+
if (src.length > MAX_VARIABLE_MEDIA_SRC_BYTES || MEDIA_SRC_CONTROL_CHARS.test(src)) {
|
|
4945
|
+
log?.warn("[Compiler] Refused a variable media source that cannot be a URL", {
|
|
4946
|
+
length: src.length
|
|
4947
|
+
});
|
|
4948
|
+
continue;
|
|
4949
|
+
}
|
|
4950
|
+
if (el.getAttribute("src") === src) continue;
|
|
4951
|
+
budget.chargeGenerated("variable media sources", src.length);
|
|
4881
4952
|
el.setAttribute("src", src);
|
|
4882
4953
|
changed = true;
|
|
4883
4954
|
}
|
|
@@ -4897,10 +4968,18 @@ async function compileForRender(projectDir, htmlPath, downloadDir, options = {})
|
|
|
4897
4968
|
throw new Error(`Entry file ${htmlPath} is not an .html file, so it is refused`);
|
|
4898
4969
|
}
|
|
4899
4970
|
assertCompositionFileSize(htmlPath, basename3(htmlPath));
|
|
4900
|
-
const
|
|
4971
|
+
const budget = new CompositionExpansionBudget();
|
|
4972
|
+
const entryHtml = rebaseDirectEntryAssetPaths(
|
|
4973
|
+
readTextFile(htmlPath),
|
|
4974
|
+
projectDir,
|
|
4975
|
+
htmlPath,
|
|
4976
|
+
budget
|
|
4977
|
+
);
|
|
4901
4978
|
const rawHtml = bindVariableMediaSources(
|
|
4902
4979
|
normalizeCompositionSrcs(rewriteUnresolvableGsapToCdn(entryHtml, projectDir), projectDir),
|
|
4903
|
-
options.variables ?? {}
|
|
4980
|
+
options.variables ?? {},
|
|
4981
|
+
budget,
|
|
4982
|
+
options.log
|
|
4904
4983
|
);
|
|
4905
4984
|
assertSubCompositionsUsable(rawHtml, projectDir, assetRoot, outsideFiles);
|
|
4906
4985
|
const probes = /* @__PURE__ */ new Map();
|
|
@@ -4931,7 +5010,8 @@ async function compileForRender(projectDir, htmlPath, downloadDir, options = {})
|
|
|
4931
5010
|
projectDir,
|
|
4932
5011
|
options.variables ?? {},
|
|
4933
5012
|
assetRoot,
|
|
4934
|
-
outsideFiles
|
|
5013
|
+
outsideFiles,
|
|
5014
|
+
budget
|
|
4935
5015
|
);
|
|
4936
5016
|
const sanitizedHtml = stripPreloadNone(inlinedHtml);
|
|
4937
5017
|
const renderModeHints = detectRenderModeHints(sanitizedHtml, options.pageMotionBlur);
|
|
@@ -5010,6 +5090,7 @@ async function compileForRender(projectDir, htmlPath, downloadDir, options = {})
|
|
|
5010
5090
|
htmlWithPreparedGifs,
|
|
5011
5091
|
projectDir,
|
|
5012
5092
|
assetRoot,
|
|
5093
|
+
budget,
|
|
5013
5094
|
options.log
|
|
5014
5095
|
);
|
|
5015
5096
|
const { html, externalAssets } = options.allowOutsideProjectAssets ? collectExternalAssets(
|
|
@@ -5812,21 +5893,50 @@ function reconcileBrowserMediaEnd(existingEnd, projectedEnd, sourceChanged, dura
|
|
|
5812
5893
|
return existingEnd <= 0 ? projectedEnd : Math.min(existingEnd, projectedEnd);
|
|
5813
5894
|
}
|
|
5814
5895
|
var MAX_MEDIA_SELECTOR_WORK = 1e6;
|
|
5815
|
-
var
|
|
5816
|
-
|
|
5896
|
+
var ATTRIBUTE_SELECTOR = /\[\s*[\w-]+\s*(?:[~|^$*]?=\s*(?:"[^"\\]*"|'[^'\\]*'|[\w-]+)\s*(?:[is]\s*)?)?\]/g;
|
|
5897
|
+
var COMPOUND_SELECTORS = /^[\w\s#.*>,@-]+$/;
|
|
5898
|
+
function cheapSelectorShape(selector) {
|
|
5899
|
+
const shape = selector.replace(ATTRIBUTE_SELECTOR, "@").trim();
|
|
5900
|
+
if (!COMPOUND_SELECTORS.test(shape)) return null;
|
|
5901
|
+
const collapsed = shape.replace(/\s+/g, " ").replace(/ ?([>,]) ?/g, "$1");
|
|
5902
|
+
const parts = collapsed.split(",");
|
|
5903
|
+
return parts.every((part) => part !== "" && part.split(" ").length <= 2) ? collapsed : null;
|
|
5904
|
+
}
|
|
5905
|
+
var attributeChars = (element) => Array.from(element.attributes).reduce((sum2, a) => sum2 + a.name.length + a.value.length + 1, 0);
|
|
5906
|
+
function managedMediaElements(document2, measure) {
|
|
5817
5907
|
const managed = [];
|
|
5818
|
-
const pending =
|
|
5819
|
-
|
|
5908
|
+
const pending = Array.from(document2.children, (element) => ({
|
|
5909
|
+
element,
|
|
5910
|
+
inMedia: false,
|
|
5911
|
+
depth: 0,
|
|
5912
|
+
above: 0
|
|
5913
|
+
}));
|
|
5820
5914
|
for (let next = pending.pop(); next; next = pending.pop()) {
|
|
5821
|
-
const
|
|
5915
|
+
const { element, depth } = next;
|
|
5916
|
+
const chainAttributeChars = measure ? next.above + attributeChars(element) : 0;
|
|
5917
|
+
const name = element.tagName.toLowerCase();
|
|
5822
5918
|
const isMedia = name === "video" || name === "audio";
|
|
5823
|
-
if (isMedia || next.inMedia && name === "source")
|
|
5824
|
-
|
|
5825
|
-
|
|
5919
|
+
if (isMedia || next.inMedia && name === "source") {
|
|
5920
|
+
managed.push({ element, depth, chainAttributeChars });
|
|
5921
|
+
}
|
|
5922
|
+
for (const child3 of Array.from(element.children)) {
|
|
5923
|
+
const inMedia = next.inMedia || isMedia;
|
|
5924
|
+
pending.push({ element: child3, inMedia, depth: depth + 1, above: chainAttributeChars });
|
|
5826
5925
|
}
|
|
5827
5926
|
}
|
|
5828
5927
|
return managed;
|
|
5829
5928
|
}
|
|
5929
|
+
function selectorCheckCost(selector, measured) {
|
|
5930
|
+
const shape = cheapSelectorShape(selector);
|
|
5931
|
+
if (shape === null) return Infinity;
|
|
5932
|
+
const tests = (shape.match(/[.#@]/g)?.length ?? 0) + shape.split(/[ >,]/).length;
|
|
5933
|
+
let cost = 0;
|
|
5934
|
+
for (const media of measured) {
|
|
5935
|
+
cost += selector.length * (media.depth + 1) + tests * media.chainAttributeChars;
|
|
5936
|
+
if (cost > MAX_MEDIA_SELECTOR_WORK) return cost;
|
|
5937
|
+
}
|
|
5938
|
+
return cost;
|
|
5939
|
+
}
|
|
5830
5940
|
function hasRuntimeMediaChanges(html) {
|
|
5831
5941
|
const { document: document2 } = parseHTML6(html);
|
|
5832
5942
|
const scriptBodies = [...document2.querySelectorAll("script")].map(
|
|
@@ -5836,11 +5946,12 @@ function hasRuntimeMediaChanges(html) {
|
|
|
5836
5946
|
(script) => /\bcreateElement\s*\(\s*["'`](?:video|audio)["'`]\s*\)/i.test(script) || /\bnew\s+(?:Audio|Video)\s*\(/i.test(script) || hasClosedMediaTag(script)
|
|
5837
5947
|
);
|
|
5838
5948
|
if (insertedMedia) return true;
|
|
5839
|
-
const managed = managedMediaElements(document2);
|
|
5949
|
+
const managed = managedMediaElements(document2, false);
|
|
5840
5950
|
if (managed.length === 0) return false;
|
|
5841
|
-
const managedIds = new Set(managed.map((
|
|
5951
|
+
const managedIds = new Set(managed.map((m) => m.element.getAttribute("id")).filter(Boolean));
|
|
5842
5952
|
const selectors = /* @__PURE__ */ new Set();
|
|
5843
|
-
let
|
|
5953
|
+
let work = 0;
|
|
5954
|
+
let measured;
|
|
5844
5955
|
for (const script of scriptBodies) {
|
|
5845
5956
|
for (const mutation of extractMediaSrcMutations(script)) {
|
|
5846
5957
|
const id = /^#[A-Za-z_][\w-]*$/.test(mutation.selector) ? mutation.selector.slice(1) : null;
|
|
@@ -5849,15 +5960,15 @@ function hasRuntimeMediaChanges(html) {
|
|
|
5849
5960
|
continue;
|
|
5850
5961
|
}
|
|
5851
5962
|
if (selectors.has(mutation.selector)) continue;
|
|
5852
|
-
if (SIBLING_COMBINATOR.test(mutation.selector)) return true;
|
|
5853
5963
|
selectors.add(mutation.selector);
|
|
5854
|
-
|
|
5855
|
-
|
|
5964
|
+
measured ??= managedMediaElements(document2, true);
|
|
5965
|
+
work += selectorCheckCost(mutation.selector, measured);
|
|
5966
|
+
if (work > MAX_MEDIA_SELECTOR_WORK) return true;
|
|
5856
5967
|
}
|
|
5857
5968
|
}
|
|
5858
5969
|
for (const selector of selectors) {
|
|
5859
5970
|
try {
|
|
5860
|
-
if (managed.some((
|
|
5971
|
+
if (managed.some((media) => media.element.matches(selector))) return true;
|
|
5861
5972
|
} catch {
|
|
5862
5973
|
}
|
|
5863
5974
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": "0.1.0",
|
|
3
3
|
"buildId": "dev",
|
|
4
|
-
"sha256": "
|
|
4
|
+
"sha256": "2a9e26f5b780e1f414573d4a7d513e409ef718f508650c18f6e32a52069b68a1",
|
|
5
5
|
"artifacts": {
|
|
6
6
|
"iife": "hyperframe.runtime.iife.js",
|
|
7
7
|
"esm": "hyperframe.runtime.mjs"
|