mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
perf(parsers): judge private paths in linear time (#8691)
* perf(parsers): judge private paths in linear time isStudioPrivatePath decided whether each segment sat at the project root by copying and scanning every earlier segment, so its work grew with the square of the segment count. It now finds the first named segment once. The two media-use copies change the same way, and lint refuses a data-composition-src longer than any file path before resolving it. * chore(skills): regenerate the media-use audio-carve bundle It bundles the private-path rule, which this branch makes linear. * test: bound the long-path tests by input size, not wall time The isPrivateFileUnder test runs on 200,000 segments, and the lint test's 40,000-character source has no timer. CI runners took just over the old limits while the results stayed right. * chore(parsers): raise the unpacked size budget by 1,000 bytes The linear private-path check adds a few dozen bytes to @hyperframes/parsers, which put it 22 bytes past its budget's 1% margin (924,172 against 924,150).
This commit is contained in:
@@ -3,3 +3,4 @@ export function receivedFileState(
|
||||
path: string,
|
||||
options?: { atOrAboveOnly?: boolean },
|
||||
): "received" | "unchecked" | null;
|
||||
export function isStudioPrivatePath(relPath: string): boolean;
|
||||
|
||||
@@ -364,14 +364,15 @@ function isStudioOwnFile(path) {
|
||||
);
|
||||
}
|
||||
|
||||
function isStudioPrivatePath(relPath) {
|
||||
export function isStudioPrivatePath(relPath) {
|
||||
if (isVcsOrSecretPath(relPath) || isCredentialOrToolStatePath(relPath)) return true;
|
||||
const segments = pathSegments(relPath);
|
||||
const inside = !isAbsolute(relPath) && !/^\.\.[. ]*$/.test(relPath.split(/[\\/]/, 1)[0] ?? "");
|
||||
const name = segments.at(-1) ?? "";
|
||||
const loadedType = !name.startsWith(".") && LOADED_ASSET_TYPE.test(name);
|
||||
const firstNamed = segments.findIndex((segment) => segment !== "");
|
||||
return segments.some((segment, at) => {
|
||||
const atRoot = segments.slice(0, at).every((before) => before === "");
|
||||
const atRoot = firstNamed === -1 || at <= firstNamed;
|
||||
if (segment === ".hyperframes")
|
||||
return !atRoot || !isStudioOwnFile(segments.slice(at).join("/"));
|
||||
if (BUILD_OUTPUT_FOLDERS.has(segment)) return inside;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { isStudioPrivatePath } from "@hyperframes/parsers/private-path";
|
||||
import { isStudioPrivatePath as portedRule } from "./lib/received-env.mjs";
|
||||
|
||||
const MILLION = 1_000_000;
|
||||
const PATHS = [
|
||||
".hyperframes/studio-motion.json",
|
||||
".hyperframes/app-history.jsonl",
|
||||
"sub/.hyperframes/studio-motion.json",
|
||||
".media/take.mp4",
|
||||
"sub/.media/take.mp4",
|
||||
"/.hyperframes/studio-motion.json",
|
||||
"//.media/take.mp4",
|
||||
".next/server.js",
|
||||
"scenes/intro.html",
|
||||
".token",
|
||||
"/".repeat(MILLION),
|
||||
"a/".repeat(MILLION) + "clip.png",
|
||||
"/".repeat(MILLION) + ".hyperframes/studio-motion.json",
|
||||
"/".repeat(MILLION) + ".media/take.mp4",
|
||||
"a/".repeat(MILLION) + ".hyperframes/studio-motion.json",
|
||||
];
|
||||
|
||||
it("the media-use copy of the private-path rule agrees with it, in linear time", () => {
|
||||
for (const path of PATHS) {
|
||||
const started = performance.now();
|
||||
expect(portedRule(path), path.slice(0, 40)).toBe(isStudioPrivatePath(path));
|
||||
expect(performance.now() - started).toBeLessThan(4_000);
|
||||
}
|
||||
});
|
||||
@@ -6,12 +6,14 @@ import {
|
||||
mkdirSync,
|
||||
writeFileSync,
|
||||
symlinkSync,
|
||||
realpathSync,
|
||||
rmSync,
|
||||
} from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { basename, dirname, join } from "node:path";
|
||||
import {
|
||||
folderGone,
|
||||
isPrivateFileUnder,
|
||||
isProjectRootMissing,
|
||||
isSafePath,
|
||||
isStudioPrivatePath,
|
||||
@@ -281,6 +283,46 @@ describe("folderGone", () => {
|
||||
);
|
||||
});
|
||||
|
||||
describe("isStudioPrivatePath on very long paths", () => {
|
||||
const MILLION = 1_000_000;
|
||||
it.each([
|
||||
["a million empty segments", "/".repeat(MILLION), false],
|
||||
["a million named segments", "a/".repeat(MILLION) + "clip.png", false],
|
||||
[
|
||||
"Studio's own file after a million empty segments",
|
||||
"/".repeat(MILLION) + ".hyperframes/studio-motion.json",
|
||||
false,
|
||||
],
|
||||
[
|
||||
"the media folder after a million empty segments",
|
||||
"/".repeat(MILLION) + ".media/take.mp4",
|
||||
false,
|
||||
],
|
||||
[
|
||||
"a nested .hyperframes after a million named segments",
|
||||
"a/".repeat(MILLION) + ".hyperframes/studio-motion.json",
|
||||
true,
|
||||
],
|
||||
["a hidden file after a million named segments", "a/".repeat(MILLION) + ".token", true],
|
||||
])("judges %s in linear time, as before", (_, path, privateFile) => {
|
||||
const started = performance.now();
|
||||
expect(isStudioPrivatePath(path)).toBe(privateFile);
|
||||
expect(performance.now() - started).toBeLessThan(2_000);
|
||||
});
|
||||
|
||||
it("judges a file named past the platform's path limit in linear time", () => {
|
||||
const projectDir = realpathSync(mkdtempSync(join(tmpdir(), "hf-long-name-")));
|
||||
try {
|
||||
const started = performance.now();
|
||||
const file = join(projectDir, "a/".repeat(200_000) + "clip.png");
|
||||
expect(isPrivateFileUnder(projectDir, file, isStudioPrivatePath)).toBe(false);
|
||||
expect(performance.now() - started).toBeLessThan(2_000);
|
||||
} finally {
|
||||
rmSync(projectDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("isStudioPrivatePath", () => {
|
||||
it.each([
|
||||
".hyperframes",
|
||||
|
||||
@@ -308,6 +308,13 @@ describe("missing_or_empty_sub_composition", () => {
|
||||
expect(finding?.message).toContain("does not exist");
|
||||
});
|
||||
|
||||
it("errors on a source longer than any file path, before resolving it", async () => {
|
||||
const src = "a/".repeat(20_000) + "scene.html";
|
||||
const { finding } = await lintSubComp(src);
|
||||
expect(finding?.message).toContain("longer than any file path can be");
|
||||
expect(finding?.message.length).toBeLessThan(400);
|
||||
});
|
||||
|
||||
it("errors, instead of crashing, when the referenced sub-composition is a folder", async () => {
|
||||
const project = makeProject(htmlWithSubComp("compositions/scene-title"), {});
|
||||
mkdirSync(join(project, "compositions", "scene-title"));
|
||||
|
||||
@@ -336,6 +336,9 @@ function resolveCssAssetCandidates(
|
||||
return resolveLocalAssetCandidates(projectDir, url);
|
||||
}
|
||||
|
||||
const WINDOWS_LONG_PATH_LIMIT = 32_767;
|
||||
const longerThanAnyPath = (src: string) => src.length > WINDOWS_LONG_PATH_LIMIT;
|
||||
|
||||
function mountedSources(
|
||||
projectDir: string,
|
||||
rootHtml: string,
|
||||
@@ -347,7 +350,8 @@ function mountedSources(
|
||||
const { document } = parseHTML(source.html);
|
||||
for (const host of querySelectorAllIncludingTemplates(document, "[data-composition-src]")) {
|
||||
const src = host.getAttribute("data-composition-src")?.trim();
|
||||
if (!src || isUnresolvedAssetPlaceholder(src) || isRemoteOrInlineUrl(src)) continue;
|
||||
if (!src || longerThanAnyPath(src)) continue;
|
||||
if (isUnresolvedAssetPlaceholder(src) || isRemoteOrInlineUrl(src)) continue;
|
||||
const path = resolve(projectDir, src);
|
||||
if (seen.has(path) || !isWithinProjectRoot(projectDir, path)) continue;
|
||||
seen.add(path);
|
||||
@@ -1042,6 +1046,11 @@ function lintMissingOrEmptySubComposition(
|
||||
// Shared scanner — see collectSubCompositionSrcs for why this must be a
|
||||
// text scan rather than a DOM query (template content is inert).
|
||||
for (const srcPath of collectSubCompositionSrcs(html)) {
|
||||
if (longerThanAnyPath(srcPath)) {
|
||||
const shown = `${srcPath.slice(0, 80)}…`;
|
||||
checked.set(shown, { srcPath: shown, problem: "it is longer than any file path can be" });
|
||||
continue;
|
||||
}
|
||||
// data-composition-src is always written root-relative (even from a
|
||||
// nested sub-composition) — matches the resolution the renderer uses
|
||||
// in packages/producer/src/services/htmlCompiler.ts (parseSubCompositions
|
||||
|
||||
@@ -111,8 +111,9 @@ export function isStudioPrivatePath(relPath: string): boolean {
|
||||
const inside = !isAbsolute(relPath) && !/^\.\.[. ]*$/.test(relPath.split(/[\\/]/, 1)[0] ?? "");
|
||||
const name = segments.at(-1) ?? "";
|
||||
const loadedType = !name.startsWith(".") && LOADED_ASSET_TYPE.test(name);
|
||||
const firstNamed = segments.findIndex((segment) => segment !== "");
|
||||
return segments.some((segment, at) => {
|
||||
const atRoot = segments.slice(0, at).every((before) => before === "");
|
||||
const atRoot = firstNamed === -1 || at <= firstNamed;
|
||||
if (segment === ".hyperframes")
|
||||
return !atRoot || !isStudioOwnFile(segments.slice(at).join("/"));
|
||||
if (BUILD_OUTPUT_FOLDERS.has(segment)) return inside;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
"files": 13
|
||||
},
|
||||
"media-use": {
|
||||
"hash": "527b640341cdd6a2",
|
||||
"hash": "7aa1e3701f3a10c0",
|
||||
"files": 134
|
||||
},
|
||||
"remotion-to-hyperframes": {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -364,14 +364,15 @@ function isStudioOwnFile(path) {
|
||||
);
|
||||
}
|
||||
|
||||
function isStudioPrivatePath(relPath) {
|
||||
export function isStudioPrivatePath(relPath) {
|
||||
if (isVcsOrSecretPath(relPath) || isCredentialOrToolStatePath(relPath)) return true;
|
||||
const segments = pathSegments(relPath);
|
||||
const inside = !isAbsolute(relPath) && !/^\.\.[. ]*$/.test(relPath.split(/[\\/]/, 1)[0] ?? "");
|
||||
const name = segments.at(-1) ?? "";
|
||||
const loadedType = !name.startsWith(".") && LOADED_ASSET_TYPE.test(name);
|
||||
const firstNamed = segments.findIndex((segment) => segment !== "");
|
||||
return segments.some((segment, at) => {
|
||||
const atRoot = segments.slice(0, at).every((before) => before === "");
|
||||
const atRoot = firstNamed === -1 || at <= firstNamed;
|
||||
if (segment === ".hyperframes")
|
||||
return !atRoot || !isStudioOwnFile(segments.slice(at).join("/"));
|
||||
if (BUILD_OUTPUT_FOLDERS.has(segment)) return inside;
|
||||
|
||||
Reference in New Issue
Block a user