mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
perf(core): a preview build scans a large page without copying it per pass (#7878)
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
insertRuntimeTag,
|
||||
hasCompositionOutsideTemplates,
|
||||
isFullHtmlDocument,
|
||||
mapStartTagsAndStyles,
|
||||
parseHTMLContent,
|
||||
stripEmbeddedRuntimeScripts,
|
||||
} from "./htmlDocument.js";
|
||||
@@ -63,7 +64,41 @@ function scannedChars(run: () => void): number {
|
||||
const workGrowth = (scan: (n: number) => void) =>
|
||||
scannedChars(() => scan(20_000)) / scannedChars(() => scan(10_000));
|
||||
|
||||
/** Lowercased copies `run` makes of any string at least `size` long. */
|
||||
function lowercasedCopies(size: number, run: () => void): number {
|
||||
const proto = String.prototype as unknown as Record<string, (...args: unknown[]) => unknown>;
|
||||
const originals = ["replace", "toLowerCase"].map((name) => [name, proto[name]!] as const);
|
||||
let copies = 0;
|
||||
for (const [name, original] of originals) {
|
||||
proto[name] = function (this: string, ...args: unknown[]) {
|
||||
if (this.length >= size) copies += 1;
|
||||
return original.apply(this, args);
|
||||
};
|
||||
}
|
||||
try {
|
||||
run();
|
||||
} finally {
|
||||
for (const [name, original] of originals) proto[name] = original;
|
||||
}
|
||||
return copies;
|
||||
}
|
||||
|
||||
describe("htmlDocument helpers", () => {
|
||||
it("finds a large page's tags without lowercasing a copy of the page for each pass", () => {
|
||||
const clip = '<VIDEO SRC="a.mov" DATA-HF-ID="v" MUTED></VIDEO>';
|
||||
const page = `<!DOCTYPE html><HTML><HEAD><TITLE>x</TITLE></HEAD><BODY>${clip.repeat(20_000)}<SCRIPT>window.x = 1;</SCRIPT></BODY></HTML>`;
|
||||
const copies = lowercasedCopies(page.length / 2, () => {
|
||||
insertBeforeCloseTag(page, "head", "<meta>");
|
||||
insertBeforeCloseTag(page, "body", "<i></i>");
|
||||
injectTagsAtHeadStart(page, "<meta>");
|
||||
findStartTags(page, "base");
|
||||
mapStartTagsAndStyles(page, { tag: (tag) => tag, style: (css) => css });
|
||||
hasCompositionOutsideTemplates(page);
|
||||
stripEmbeddedRuntimeScripts(page);
|
||||
});
|
||||
expect(copies).toBe(0);
|
||||
});
|
||||
|
||||
it("strips runtimes from large mixed-case base64 HTML within a bounded heap", () => {
|
||||
const moduleUrl = pathToFileURL(resolve(__dirname, "htmlDocument.ts")).href;
|
||||
const result = spawnSync(
|
||||
|
||||
@@ -65,14 +65,44 @@ function lowerAscii(text: string): string {
|
||||
);
|
||||
}
|
||||
|
||||
function startsWithLower(text: string, token: string, at: number): boolean {
|
||||
if (at < 0 || at + token.length > text.length) return false;
|
||||
for (let i = 0; i < token.length; i += 1) {
|
||||
const code = text.charCodeAt(at + i);
|
||||
if ((code >= 65 && code <= 90 ? code + 32 : code) !== token.charCodeAt(i)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function indexOfLower(text: string, token: string, from: number): number {
|
||||
for (
|
||||
let at = text.indexOf(token.charAt(0), from);
|
||||
at !== -1;
|
||||
at = text.indexOf(token.charAt(0), at + 1)
|
||||
) {
|
||||
if (startsWithLower(text, token, at)) return at;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function lastIndexOfLower(text: string, token: string): number {
|
||||
for (
|
||||
let at = text.lastIndexOf(token.charAt(0));
|
||||
at !== -1;
|
||||
at = at === 0 ? -1 : text.lastIndexOf(token.charAt(0), at - 1)
|
||||
) {
|
||||
if (startsWithLower(text, token, at)) return at;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
export function stripEmbeddedRuntimeScripts(html: string): string {
|
||||
if (!html) return html;
|
||||
const loweredHtml = lowerAscii(html);
|
||||
let output = "";
|
||||
let cursor = 0;
|
||||
|
||||
while (cursor < html.length) {
|
||||
const scriptStart = findScriptStart(loweredHtml, cursor);
|
||||
const scriptStart = findScriptStart(html, cursor);
|
||||
if (scriptStart === -1) {
|
||||
output += html.slice(cursor);
|
||||
break;
|
||||
@@ -85,7 +115,7 @@ export function stripEmbeddedRuntimeScripts(html: string): string {
|
||||
break;
|
||||
}
|
||||
|
||||
const closeTagEnd = findScriptCloseTagEnd(loweredHtml, startTagEnd + 1);
|
||||
const closeTagEnd = findScriptCloseTagEnd(html, startTagEnd + 1);
|
||||
const scriptEnd = closeTagEnd === -1 ? html.length : closeTagEnd;
|
||||
const block = html.slice(scriptStart, scriptEnd);
|
||||
if (!shouldStripRuntimeScriptBlock(block, html.slice(scriptStart, startTagEnd + 1))) {
|
||||
@@ -97,12 +127,12 @@ export function stripEmbeddedRuntimeScripts(html: string): string {
|
||||
return output;
|
||||
}
|
||||
|
||||
function findScriptStart(loweredHtml: string, from: number): number {
|
||||
let index = loweredHtml.indexOf("<script", from);
|
||||
function findScriptStart(html: string, from: number): number {
|
||||
let index = indexOfLower(html, "<script", from);
|
||||
while (index !== -1) {
|
||||
const next = loweredHtml[index + "<script".length] ?? "";
|
||||
const next = html[index + "<script".length] ?? "";
|
||||
if (isTagBoundary(next)) return index;
|
||||
index = loweredHtml.indexOf("<script", index + 1);
|
||||
index = indexOfLower(html, "<script", index + 1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -145,22 +175,22 @@ function stateAfterSlash(state: TagState): TagState {
|
||||
return state === "equals" || state === "value" ? "value" : "between";
|
||||
}
|
||||
|
||||
function findScriptCloseTagEnd(loweredHtml: string, from: number): number {
|
||||
let index = loweredHtml.indexOf("</script", from);
|
||||
function findScriptCloseTagEnd(html: string, from: number): number {
|
||||
let index = indexOfLower(html, "</script", from);
|
||||
while (index !== -1) {
|
||||
const closeTagEnd = findScriptCloseTagBoundary(loweredHtml, index + "</script".length);
|
||||
const closeTagEnd = findScriptCloseTagBoundary(html, index + "</script".length);
|
||||
if (closeTagEnd !== -1) return closeTagEnd;
|
||||
index = loweredHtml.indexOf("</script", index + 1);
|
||||
index = indexOfLower(html, "</script", index + 1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function findScriptCloseTagBoundary(loweredHtml: string, from: number): number {
|
||||
function findScriptCloseTagBoundary(html: string, from: number): number {
|
||||
let cursor = from;
|
||||
while (cursor < loweredHtml.length && isHtmlWhitespace(loweredHtml[cursor] ?? "")) {
|
||||
while (cursor < html.length && isHtmlWhitespace(html[cursor] ?? "")) {
|
||||
cursor += 1;
|
||||
}
|
||||
return loweredHtml[cursor] === ">" ? cursor + 1 : -1;
|
||||
return html[cursor] === ">" ? cursor + 1 : -1;
|
||||
}
|
||||
|
||||
function shouldStripRuntimeScriptBlock(block: string, startTag: string): boolean {
|
||||
@@ -183,8 +213,7 @@ export function isRuntimeFileUrl(src: string | null | undefined): boolean {
|
||||
function getScriptSource(block: string): string {
|
||||
const startTagEnd = findTagEnd(block, 1);
|
||||
if (startTagEnd === -1) return "";
|
||||
const loweredBlock = lowerAscii(block);
|
||||
const closeTagStart = loweredBlock.lastIndexOf("</script");
|
||||
const closeTagStart = lastIndexOfLower(block, "</script");
|
||||
const end = closeTagStart === -1 ? block.length : closeTagStart;
|
||||
return block.slice(startTagEnd + 1, end);
|
||||
}
|
||||
@@ -210,13 +239,12 @@ function escapeCaseInsensitiveToken(
|
||||
token: string,
|
||||
replacement: (match: string) => string,
|
||||
): string {
|
||||
const loweredSource = lowerAscii(source);
|
||||
const loweredToken = lowerAscii(token);
|
||||
let output = "";
|
||||
let cursor = 0;
|
||||
|
||||
while (cursor < source.length) {
|
||||
const tokenStart = loweredSource.indexOf(loweredToken, cursor);
|
||||
const tokenStart = indexOfLower(source, loweredToken, cursor);
|
||||
if (tokenStart === -1) {
|
||||
output += source.slice(cursor);
|
||||
break;
|
||||
@@ -242,21 +270,20 @@ const RAW_TEXT_TAGS = ["script", "style", "title", "textarea"] as const;
|
||||
type DocumentTag = "<head" | "</head" | "<body" | "</body" | "</template";
|
||||
const COMMENT_END = /--!?>/g;
|
||||
|
||||
function* markupStarts(lowered: string): Generator<number> {
|
||||
function* markupStarts(html: string): Generator<number> {
|
||||
const unclosedRawText = new Set<string>();
|
||||
let cursor = 0;
|
||||
while (cursor !== -1) {
|
||||
const open = lowered.indexOf("<", cursor);
|
||||
const open = html.indexOf("<", cursor);
|
||||
if (open === -1) return;
|
||||
yield open;
|
||||
cursor = skipMarkup(lowered, open, unclosedRawText);
|
||||
cursor = skipMarkup(html, open, unclosedRawText);
|
||||
}
|
||||
}
|
||||
|
||||
function findDocumentTag(html: string, tag: DocumentTag): number {
|
||||
const lowered = lowerAscii(html);
|
||||
for (const open of markupStarts(lowered)) {
|
||||
if (isTagAt(lowered, open, tag)) return open;
|
||||
for (const open of markupStarts(html)) {
|
||||
if (isTagAt(html, open, tag)) return open;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -265,17 +292,16 @@ export function mapStartTagsAndStyles(
|
||||
html: string,
|
||||
map: { tag: (tag: string) => string; style: (css: string) => string },
|
||||
): string {
|
||||
const lowered = lowerAscii(html);
|
||||
let out = "";
|
||||
let cursor = 0;
|
||||
for (const open of markupStarts(lowered)) {
|
||||
if (open < cursor || !/[a-z]/.test(lowered.charAt(open + 1))) continue;
|
||||
const end = findTagEnd(lowered, open + 1);
|
||||
for (const open of markupStarts(html)) {
|
||||
if (open < cursor || !/[a-z]/i.test(html.charAt(open + 1))) continue;
|
||||
const end = findTagEnd(html, open + 1);
|
||||
if (end === -1) break;
|
||||
out += html.slice(cursor, open) + map.tag(html.slice(open, end + 1));
|
||||
cursor = end + 1;
|
||||
if (!isTagAt(lowered, open, "<style")) continue;
|
||||
const close = findRawTextClose(lowered, "style", cursor);
|
||||
if (!isTagAt(html, open, "<style")) continue;
|
||||
const close = findRawTextClose(html, "style", cursor);
|
||||
if (close === -1) continue;
|
||||
out += map.style(html.slice(cursor, close));
|
||||
cursor = close;
|
||||
@@ -284,99 +310,100 @@ export function mapStartTagsAndStyles(
|
||||
}
|
||||
|
||||
export function findStartTags(html: string, name: string): number[] {
|
||||
const lowered = lowerAscii(html);
|
||||
const token = `<${lowerAscii(name)}`;
|
||||
const starts: number[] = [];
|
||||
let templateDepth = 0;
|
||||
for (const open of markupStarts(lowered)) {
|
||||
if (templateDepth === 0 && isTagAt(lowered, open, token)) starts.push(open);
|
||||
if (isTagAt(lowered, open, "<template")) templateDepth++;
|
||||
else if (templateDepth > 0 && isTagAt(lowered, open, "</template")) templateDepth--;
|
||||
for (const open of markupStarts(html)) {
|
||||
if (templateDepth === 0 && isTagAt(html, open, token)) starts.push(open);
|
||||
if (isTagAt(html, open, "<template")) templateDepth++;
|
||||
else if (templateDepth > 0 && isTagAt(html, open, "</template")) templateDepth--;
|
||||
}
|
||||
return starts;
|
||||
}
|
||||
|
||||
function isTagAt(lowered: string, at: number, token: string): boolean {
|
||||
return lowered.startsWith(token, at) && isTagBoundary(lowered.charAt(at + token.length));
|
||||
function isTagAt(html: string, at: number, token: string): boolean {
|
||||
return startsWithLower(html, token, at) && isTagBoundary(html.charAt(at + token.length));
|
||||
}
|
||||
|
||||
/** Cursor just past the markup that starts at `open`, or -1 when the document ends inside it. */
|
||||
function skipMarkup(lowered: string, open: number, unclosedRawText: Set<string>): number {
|
||||
if (lowered.startsWith("<!--", open)) return skipComment(lowered, open);
|
||||
const next = lowered.charAt(open + 1);
|
||||
if (next === "/" && !/[a-z]/.test(lowered.charAt(open + 2))) {
|
||||
const bogusEnd = lowered.indexOf(">", open);
|
||||
function skipMarkup(html: string, open: number, unclosedRawText: Set<string>): number {
|
||||
if (html.startsWith("<!--", open)) return skipComment(html, open);
|
||||
const next = html.charAt(open + 1);
|
||||
if (next === "/" && !/[a-z]/i.test(html.charAt(open + 2))) {
|
||||
const bogusEnd = html.indexOf(">", open);
|
||||
return bogusEnd === -1 ? -1 : bogusEnd + 1;
|
||||
}
|
||||
if (!/[a-z/]/.test(next)) return open + 1;
|
||||
const tagEnd = findTagEnd(lowered, open + 1);
|
||||
return tagEnd === -1 ? -1 : skipRawText(lowered, open, tagEnd, unclosedRawText);
|
||||
if (!/[a-z/]/i.test(next)) return open + 1;
|
||||
const tagEnd = findTagEnd(html, open + 1);
|
||||
return tagEnd === -1 ? -1 : skipRawText(html, open, tagEnd, unclosedRawText);
|
||||
}
|
||||
|
||||
function skipComment(lowered: string, open: number): number {
|
||||
if (lowered.startsWith("<!-->", open) || lowered.startsWith("<!--->", open)) {
|
||||
return lowered.indexOf(">", open) + 1;
|
||||
function skipComment(html: string, open: number): number {
|
||||
if (html.startsWith("<!-->", open) || html.startsWith("<!--->", open)) {
|
||||
return html.indexOf(">", open) + 1;
|
||||
}
|
||||
COMMENT_END.lastIndex = open + 4;
|
||||
const end = COMMENT_END.exec(lowered);
|
||||
const end = COMMENT_END.exec(html);
|
||||
return end ? end.index + end[0].length : -1;
|
||||
}
|
||||
|
||||
function skipRawText(
|
||||
lowered: string,
|
||||
html: string,
|
||||
open: number,
|
||||
tagEnd: number,
|
||||
unclosedRawText: Set<string>,
|
||||
): number {
|
||||
const rawText = RAW_TEXT_TAGS.find((name) => isTagAt(lowered, open, `<${name}`));
|
||||
const rawText = RAW_TEXT_TAGS.find((name) => isTagAt(html, open, `<${name}`));
|
||||
if (!rawText) return tagEnd + 1;
|
||||
const close = unclosedRawText.has(rawText) ? -1 : findRawTextClose(lowered, rawText, tagEnd + 1);
|
||||
const close = unclosedRawText.has(rawText) ? -1 : findRawTextClose(html, rawText, tagEnd + 1);
|
||||
if (close !== -1) return close;
|
||||
unclosedRawText.add(rawText);
|
||||
return lowered.charAt(tagEnd - 1) === "/" ? tagEnd + 1 : -1;
|
||||
return html.charAt(tagEnd - 1) === "/" ? tagEnd + 1 : -1;
|
||||
}
|
||||
|
||||
function findRawTextClose(lowered: string, name: string, from: number): number {
|
||||
function findRawTextClose(html: string, name: string, from: number): number {
|
||||
const close = `</${name}`;
|
||||
for (let at = lowered.indexOf(close, from); at !== -1; at = lowered.indexOf(close, at + 1)) {
|
||||
if (isTagAt(lowered, at, close)) return at;
|
||||
for (
|
||||
let at = indexOfLower(html, close, from);
|
||||
at !== -1;
|
||||
at = indexOfLower(html, close, at + 1)
|
||||
) {
|
||||
if (isTagAt(html, at, close)) return at;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function findOuterTemplateClose(html: string): number {
|
||||
const lowered = lowerAscii(html);
|
||||
let depth = 0;
|
||||
for (const open of markupStarts(lowered)) {
|
||||
if (isTagAt(lowered, open, "<template")) depth++;
|
||||
else if (isTagAt(lowered, open, "</template") && --depth === 0) return open;
|
||||
for (const open of markupStarts(html)) {
|
||||
if (isTagAt(html, open, "<template")) depth++;
|
||||
else if (isTagAt(html, open, "</template") && --depth === 0) return open;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
const COMPOSITION_ID_ATTR = /\sdata-composition-id\s*=/;
|
||||
const COMPOSITION_ID_ATTR = /\sdata-composition-id\s*=/i;
|
||||
|
||||
const isDocumentWrapper = (lowered: string, open: number): boolean =>
|
||||
isTagAt(lowered, open, "<html") || isTagAt(lowered, open, "<body");
|
||||
const isDocumentWrapper = (html: string, open: number): boolean =>
|
||||
isTagAt(html, open, "<html") || isTagAt(html, open, "<body");
|
||||
|
||||
function carriesCompositionId(lowered: string, open: number): boolean {
|
||||
const end = findTagEnd(lowered, open + 1);
|
||||
return end !== -1 && COMPOSITION_ID_ATTR.test(lowered.slice(open, end));
|
||||
function carriesCompositionId(html: string, open: number): boolean {
|
||||
const end = findTagEnd(html, open + 1);
|
||||
return end !== -1 && COMPOSITION_ID_ATTR.test(html.slice(open, end));
|
||||
}
|
||||
|
||||
export function hasCompositionOutsideTemplates(html: string): boolean {
|
||||
const lowered = lowerAscii(html);
|
||||
let depth = 0;
|
||||
let idOnWrapper = false;
|
||||
let idInTemplate = false;
|
||||
for (const open of markupStarts(lowered)) {
|
||||
if (isTagAt(lowered, open, "<template")) {
|
||||
for (const open of markupStarts(html)) {
|
||||
if (isTagAt(html, open, "<template")) {
|
||||
depth++;
|
||||
if (carriesCompositionId(lowered, open)) idInTemplate = true;
|
||||
} else if (isTagAt(lowered, open, "</template")) depth = Math.max(0, depth - 1);
|
||||
else if (/[a-z]/.test(lowered.charAt(open + 1)) && carriesCompositionId(lowered, open)) {
|
||||
if (carriesCompositionId(html, open)) idInTemplate = true;
|
||||
} else if (isTagAt(html, open, "</template")) depth = Math.max(0, depth - 1);
|
||||
else if (/[a-z]/i.test(html.charAt(open + 1)) && carriesCompositionId(html, open)) {
|
||||
if (depth > 0) idInTemplate = true;
|
||||
else if (isDocumentWrapper(lowered, open)) idOnWrapper = true;
|
||||
else if (isDocumentWrapper(html, open)) idOnWrapper = true;
|
||||
else return true;
|
||||
}
|
||||
}
|
||||
@@ -405,7 +432,7 @@ function insertTagInHead(html: string, tag: string): string {
|
||||
const insertPos = htmlOpenMatch.index + htmlOpenMatch[0].length;
|
||||
return `${html.slice(0, insertPos)}<head>${tag}</head>${html.slice(insertPos)}`;
|
||||
}
|
||||
const doctypeIdx = html.toLowerCase().indexOf("<!doctype");
|
||||
const doctypeIdx = indexOfLower(html, "<!doctype", 0);
|
||||
if (doctypeIdx >= 0) {
|
||||
const insertPos = html.indexOf(">", doctypeIdx) + 1;
|
||||
return html.slice(0, insertPos) + tag + html.slice(insertPos);
|
||||
|
||||
@@ -228,23 +228,14 @@ function emptyHfIdValue(html: string, tag: StartTag): number | null {
|
||||
}
|
||||
|
||||
function outsideQuotes(open: string): string {
|
||||
let quote = "";
|
||||
let out = "";
|
||||
for (const c of open) {
|
||||
out += quotedChar(quote, c);
|
||||
quote = quoteOf(quote, c);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function quoteOf(quote: string, c: string): string {
|
||||
if (quote) return c === quote ? "" : quote;
|
||||
return c === '"' || c === "'" ? c : "";
|
||||
}
|
||||
|
||||
function quotedChar(quote: string, c: string): string {
|
||||
if (!quote || c === quote) return c;
|
||||
return " ".repeat(c.length);
|
||||
return open.replace(/"[^"]*"?|'[^']*'?/g, (quoted) => {
|
||||
const closed = quoted.length > 1 && quoted.endsWith(quoted.charAt(0));
|
||||
return (
|
||||
quoted.charAt(0) +
|
||||
" ".repeat(quoted.length - (closed ? 2 : 1)) +
|
||||
(closed ? quoted.charAt(0) : "")
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function spliceIds(html: string, inserts: Insert[]): string {
|
||||
|
||||
Reference in New Issue
Block a user