mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): validate and keyframes print project text with control characters as visible escapes (#7958)
* fix(cli): validate and keyframes print project text with control characters as visible escapes * test(cli): the CSS case prints a payload the declaration split keeps whole * fix(cli): escape project-supplied object keys too, so keyframes prints tween property names safely
This commit is contained in:
@@ -501,3 +501,72 @@ describe("keyframes project compositions", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("keyframes text output", () => {
|
||||
const printed = async (html: string, extra: string[], file = "index.html") => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-keyframes-text-"));
|
||||
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
|
||||
const source = join(dir, file);
|
||||
writeFileSync(source, html);
|
||||
const log = vi.spyOn(console, "log").mockImplementation(() => {});
|
||||
onTestFinished(() => log.mockRestore());
|
||||
await runCommand(keyframesCommand, { rawArgs: [source, ...extra] });
|
||||
return log.mock.calls.map((args) => args.join(" ")).join("\n");
|
||||
};
|
||||
|
||||
// fails without the fix: a tween target or CSS rule from the project reached the terminal with its control bytes.
|
||||
it.each([
|
||||
[
|
||||
"a GSAP tween target",
|
||||
wrap(`
|
||||
const tl = gsap.timeline({ paused: true });
|
||||
tl.to("#dot\u001b]52;c;cGF5\u0007", { x: 10, duration: 1 });
|
||||
window.__timelines = [tl];
|
||||
`),
|
||||
[],
|
||||
"\\x1b]52;c;cGF5\\x07",
|
||||
],
|
||||
[
|
||||
"a GSAP tween property name",
|
||||
wrap(`
|
||||
const tl = gsap.timeline({ paused: true });
|
||||
tl.to("#dot", { "\u001b]52;c;cGF5\u0007": 1, duration: 1 });
|
||||
window.__timelines = [tl];
|
||||
`),
|
||||
[],
|
||||
"\\x1b]52;c;cGF5\\x07",
|
||||
],
|
||||
[
|
||||
"a CSS keyframes rule",
|
||||
`<!doctype html><html><head><style>
|
||||
.dot { animation: rise 1s both; }
|
||||
@keyframes rise { 0% { content: "\u001b[2J\u0007"; } 100% { opacity: 1; } }
|
||||
</style></head><body><div class="dot"></div></body></html>`,
|
||||
["--runtime", "css"],
|
||||
"\\x1b[2J\\x07",
|
||||
],
|
||||
])(
|
||||
"prints %s with its control characters as visible escapes",
|
||||
async (_what, html, extra, escaped) => {
|
||||
const output = await printed(html, extra);
|
||||
expect(output).toContain(escaped);
|
||||
expect(output).not.toContain("\u001b");
|
||||
expect(output).not.toContain("\u0007");
|
||||
},
|
||||
);
|
||||
|
||||
// Windows file names cannot hold control characters.
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"prints a file name with control characters as visible escapes",
|
||||
async () => {
|
||||
const output = await printed(
|
||||
`<!doctype html><html><body></body></html>`,
|
||||
[],
|
||||
"a\u001b]0;x\u0007.html",
|
||||
);
|
||||
expect(output).toContain("a\\x1b]0;x\\x07.html");
|
||||
expect(output).not.toContain("\u001b");
|
||||
expect(output).not.toContain("\u0007");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { terminalText } from "@hyperframes/core";
|
||||
import { terminalText, terminalTextDeep } from "@hyperframes/core";
|
||||
import { defineCommand } from "../utils/defineCommand.js";
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { isWithinProjectRoot, readProjectFile } from "@hyperframes/parsers/asset-resolution";
|
||||
@@ -1128,19 +1128,23 @@ function createKeyframesCommand(options: Partial<KeyframesCommandOptions> = {})
|
||||
console.log();
|
||||
}
|
||||
|
||||
const total = comps.reduce(
|
||||
const shown = terminalTextDeep(comps);
|
||||
const projectShown = terminalText(projectName);
|
||||
const total = shown.reduce(
|
||||
(n, cmp) => n + cmp.tweens.length + cmp.cssKeyframes.length + cmp.anime.length,
|
||||
0,
|
||||
);
|
||||
if (total === 0) {
|
||||
console.log(`${c.success("◇")} ${c.accent(projectName)} ${c.dim("— no keyframes found")}`);
|
||||
console.log(
|
||||
`${c.success("◇")} ${c.accent(projectShown)} ${c.dim("— no keyframes found")}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
console.log(
|
||||
`${c.success("◇")} ${c.accent(projectName)} ${c.dim("—")} ${c.dim(`${total} item${total === 1 ? "" : "s"}`)}`,
|
||||
`${c.success("◇")} ${c.accent(projectShown)} ${c.dim("—")} ${c.dim(`${total} item${total === 1 ? "" : "s"}`)}`,
|
||||
);
|
||||
console.log();
|
||||
for (const cmp of comps) printComposition(cmp);
|
||||
for (const cmp of shown) printComposition(cmp);
|
||||
console.log(
|
||||
c.dim(
|
||||
`Tip: edit the keyframes in source, then \`${commandOptions.invocation} --shot out.png\` to see the rendered motion.`,
|
||||
|
||||
@@ -14,6 +14,8 @@ import {
|
||||
auditClipDurations,
|
||||
extractCompositionErrorsFromLint,
|
||||
navigationTimeoutHint,
|
||||
emitFailureReport,
|
||||
printValidationResult,
|
||||
projectPathOfUrl,
|
||||
raceMediaReady,
|
||||
shouldIgnoreRequestFailure,
|
||||
@@ -404,3 +406,51 @@ describe("projectPathOfUrl", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("validate text report", () => {
|
||||
// fails without the fix: page console messages and contrast findings reached the terminal with raw control bytes.
|
||||
it("prints page messages and contrast findings with control characters as visible escapes", () => {
|
||||
const log = vi.spyOn(console, "log").mockImplementation(() => {});
|
||||
try {
|
||||
const planted = "\u001b]52;c;cGF5\u0007";
|
||||
printValidationResult(
|
||||
{
|
||||
errors: [{ level: "error", text: `boom${planted}`, line: 3 }],
|
||||
warnings: [{ level: "warning", text: `careful${planted}` }],
|
||||
contrast: [
|
||||
{
|
||||
time: 1,
|
||||
selector: `#title${planted}`,
|
||||
text: `Hello${planted}`,
|
||||
ratio: 1.2,
|
||||
wcagAA: false,
|
||||
large: false,
|
||||
fg: "#777",
|
||||
bg: "#888",
|
||||
},
|
||||
],
|
||||
},
|
||||
false,
|
||||
);
|
||||
const output = log.mock.calls.map((args) => args.join(" ")).join("\n");
|
||||
expect(output.split("\\x1b]52;c;cGF5\\x07").length - 1).toBe(4);
|
||||
expect(output).not.toContain("\u001b");
|
||||
expect(output).not.toContain("\u0007");
|
||||
} finally {
|
||||
log.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("prints a failure message with control characters as visible escapes", () => {
|
||||
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
try {
|
||||
emitFailureReport("page crashed\u001b]52;c;cGF5\u0007", false);
|
||||
const output = error.mock.calls.map((args) => args.join(" ")).join("\n");
|
||||
expect(output).toContain("page crashed\\x1b]52;c;cGF5\\x07");
|
||||
expect(output).not.toContain("\u001b");
|
||||
expect(output).not.toContain("\u0007");
|
||||
} finally {
|
||||
error.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,7 +15,11 @@ import { resolveProject, type ProjectDir } from "../utils/project.js";
|
||||
import { normalizeErrorMessage } from "../utils/errorMessage.js";
|
||||
import type { ProjectLintResult } from "../utils/lintProject.js";
|
||||
import { resolveCompositionViewportFromHtml } from "../utils/compositionViewport.js";
|
||||
import { resolveNaturalMediaTimelineDuration } from "@hyperframes/core";
|
||||
import {
|
||||
resolveNaturalMediaTimelineDuration,
|
||||
terminalText,
|
||||
terminalTextDeep,
|
||||
} from "@hyperframes/core";
|
||||
import { c } from "../ui/colors.js";
|
||||
import { decodeWellFormedEscapes } from "@hyperframes/studio-server";
|
||||
import { printDeprecationNotice, withMeta } from "../utils/updateCheck.js";
|
||||
@@ -639,7 +643,7 @@ function emitTextReport(
|
||||
console.log(`${c.accent("◇")} ${formatTotals(errors, warnings, contrastFailures)}`);
|
||||
}
|
||||
|
||||
function emitFailureReport(message: string, asJson: boolean): void {
|
||||
export function emitFailureReport(message: string, asJson: boolean): void {
|
||||
if (asJson) {
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
@@ -653,7 +657,7 @@ function emitFailureReport(message: string, asJson: boolean): void {
|
||||
);
|
||||
return;
|
||||
}
|
||||
console.error(`${c.error("✗")} ${message}`);
|
||||
console.error(`${c.error("✗")} ${terminalText(message)}`);
|
||||
}
|
||||
|
||||
export default defineCommand({
|
||||
@@ -691,7 +695,9 @@ Examples:
|
||||
const asJson = Boolean(args.json);
|
||||
|
||||
if (!asJson) {
|
||||
console.log(`${c.accent("◆")} Validating ${c.accent(project.name)} in headless Chrome`);
|
||||
console.log(
|
||||
`${c.accent("◆")} Validating ${c.accent(terminalText(project.name))} in headless Chrome`,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -707,7 +713,7 @@ Examples:
|
||||
},
|
||||
});
|
||||
|
||||
function printValidationResult(
|
||||
export function printValidationResult(
|
||||
result: { errors: ConsoleEntry[]; warnings: ConsoleEntry[]; contrast?: ContrastEntry[] },
|
||||
asJson: boolean,
|
||||
): number {
|
||||
@@ -718,7 +724,8 @@ function printValidationResult(
|
||||
if (asJson) {
|
||||
emitJsonReport(errors, warnings, contrast, contrastFailures);
|
||||
} else {
|
||||
emitTextReport(errors, warnings, contrastFailures, contrastPassed);
|
||||
const shown = terminalTextDeep({ errors, warnings, contrastFailures, contrastPassed });
|
||||
emitTextReport(shown.errors, shown.warnings, shown.contrastFailures, shown.contrastPassed);
|
||||
}
|
||||
return errors.length > 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
@@ -495,4 +495,4 @@ export {
|
||||
type MotionBlurFingerprint,
|
||||
type MotionBlurFingerprintField,
|
||||
} from "./motionBlurFingerprint.js";
|
||||
export { terminalText } from "./terminalText.js";
|
||||
export { terminalText, terminalTextDeep } from "./terminalText.js";
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { terminalText } from "./terminalText.js";
|
||||
import { terminalText, terminalTextDeep } from "./terminalText.js";
|
||||
|
||||
describe("terminalText", () => {
|
||||
it("leaves ordinary text and tabs alone", () => {
|
||||
@@ -12,3 +12,24 @@ describe("terminalText", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("terminalTextDeep", () => {
|
||||
it("escapes every string in nested arrays and objects and keeps other values", () => {
|
||||
const shown = terminalTextDeep({
|
||||
id: "a\u001b]52;c;x\u0007",
|
||||
list: [{ selector: "#b\u001b[2J", n: 3 }],
|
||||
ok: true,
|
||||
});
|
||||
expect(shown).toEqual({
|
||||
id: "a\\x1b]52;c;x\\x07",
|
||||
list: [{ selector: "#b\\x1b[2J", n: 3 }],
|
||||
ok: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("escapes object keys too", () => {
|
||||
expect(terminalTextDeep({ "x\u001b]52;c;cGF5\u0007": 1 })).toEqual({
|
||||
"x\\x1b]52;c;cGF5\\x07": 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,3 +9,19 @@ export function terminalText(text: string): string {
|
||||
}
|
||||
return shown;
|
||||
}
|
||||
|
||||
const isPlainObject = (value: object) => {
|
||||
const proto = Object.getPrototypeOf(value);
|
||||
return proto === Object.prototype || proto === null;
|
||||
};
|
||||
|
||||
export function terminalTextDeep<T>(value: T): T {
|
||||
if (typeof value === "string") return terminalText(value) as T;
|
||||
if (Array.isArray(value)) return value.map(terminalTextDeep) as T;
|
||||
if (value !== null && typeof value === "object" && isPlainObject(value)) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).map(([key, item]) => [terminalText(key), terminalTextDeep(item)]),
|
||||
) as T;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user