diff --git a/packages/cli/src/commands/render.test.ts b/packages/cli/src/commands/render.test.ts index 348fed694b..fa9ae29ada 100644 --- a/packages/cli/src/commands/render.test.ts +++ b/packages/cli/src/commands/render.test.ts @@ -543,6 +543,32 @@ describe("renderLocal browser GPU config", () => { expect(cpuSamplerState.samplers.map((s) => s.stopped)).toEqual([true, true]); }); + it("shows control characters in a failed render's message as escapes, one line each", async () => { + producerState.executeImpl = async () => { + throw new Error("FFmpeg could not read clip\u009b\b\r.mp4\nsecond\u0007 line"); + }; + const errors = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + await expect( + renderLocal(PROJECT_DIR, "/tmp/out.mp4", { + fps: { num: 30, den: 1 }, + quality: "standard", + format: "mp4", + gpu: false, + browserGpuMode: "software", + hdrMode: "auto", + quiet: true, + }), + ).rejects.toMatchObject({ name: "CliRuntimeError" }); + const output = errors.mock.calls.map((call) => String(call[0])).join("\n"); + expect(output).toContain("clip\\x9b\\x08\\x0d.mp4"); + expect(output).toMatch(/\n\s*.*second\\x07 line/); + for (const control of ["\u009b", "\b", "\r", "\u0007"]) expect(output).not.toContain(control); + } finally { + errors.mockRestore(); + } + }); + it.each([ { kind: "solid", rgb: [10, 46, 138] }, { kind: "none" }, diff --git a/packages/cli/src/commands/render.ts b/packages/cli/src/commands/render.ts index 00ee1cd983..ea9aaa3989 100644 --- a/packages/cli/src/commands/render.ts +++ b/packages/cli/src/commands/render.ts @@ -161,6 +161,7 @@ import { checkOutputResolutionCompatibility, suggestMatchingPreset, fpsToNumber, + terminalText, type CanvasResolution, type OutputResolutionIssueKind, type Fps, @@ -1965,12 +1966,13 @@ function handleRenderError( if (options.throwOnError) { throw new Error(message); } + const shown = message.split(/\r?\n/).map(terminalText).join("\n"); const launch = chromeLaunchFailure(message); if (launch) { - errorBox(launch.title, message, launch.remediation); + errorBox(launch.title, shown, launch.remediation); failCommand(); } - errorBox("Render failed", message, hint); + errorBox("Render failed", shown, hint); failCommand(); } diff --git a/packages/cli/src/commands/snapshot.test.ts b/packages/cli/src/commands/snapshot.test.ts index 185e944aa4..5accb0ecb8 100644 --- a/packages/cli/src/commands/snapshot.test.ts +++ b/packages/cli/src/commands/snapshot.test.ts @@ -536,6 +536,20 @@ describe("extractVideoFrameToBuffer", () => { !noProbe && execFileSync(ffmpeg!, ["-hide_banner", "-encoders"], { encoding: "utf-8" }).includes("libx265"); + it.skipIf(noProbe)("shows control characters in a failed probe's path as escapes", async () => { + const dir = mkdtempSync(join(tmpdir(), "hf-snapshot-probe-text-")); + try { + const clip = join(dir, "clip\u0007\b\u009b.mp4"); + const probe = probeContainingSourceFrameSeek(clip, 0.5, false); + await expect(probe).rejects.toThrow("clip\\x07\\x08\\x9b.mp4"); + const message = await probe.catch((error: Error) => error.message); + for (const control of ["\u0007", "\b", "\u009b", "\r"]) + expect(message).not.toContain(control); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + it.skipIf(noProbe)("selects containing frames for snapshots and reference pairs", async () => { const dir = mkdtempSync(join(tmpdir(), "hf-snapshot-containing-")); try { diff --git a/packages/cli/src/commands/snapshot.ts b/packages/cli/src/commands/snapshot.ts index 87f87f613a..3eb6bb3fc3 100644 --- a/packages/cli/src/commands/snapshot.ts +++ b/packages/cli/src/commands/snapshot.ts @@ -413,7 +413,11 @@ export function containingSourceFrameIndex(timestamps: readonly number[], time: return index; } -class FrameExtractError extends Error {} +class FrameExtractError extends Error { + constructor(message: string) { + super(terminalText(message)); + } +} const lastLine = (text: string) => text.trim().split("\n").at(-1) ?? ""; diff --git a/packages/cli/src/commands/snapshotHeldVideos.test.ts b/packages/cli/src/commands/snapshotHeldVideos.test.ts index 77c0b721b3..e278d5e2a1 100644 --- a/packages/cli/src/commands/snapshotHeldVideos.test.ts +++ b/packages/cli/src/commands/snapshotHeldVideos.test.ts @@ -1,7 +1,7 @@ import { execFileSync } from "node:child_process"; -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { afterAll, beforeEach, describe, expect, it, vi } from "vitest"; const run = vi.hoisted(() => vi.fn()); @@ -63,6 +63,29 @@ describe("projectLocalFile", () => { ])("gives null for %s", (_label, src) => { expect(projectLocalFile(dir, src)).toBeNull(); }); + + it.each([".hyperframes/handoff.mp4", ".git/clip.mp4", ".env", "keys/id_ed25519"])( + "gives null for the private project file %s", + (rel) => { + mkdirSync(dirname(join(dir, rel)), { recursive: true }); + writeFileSync(join(dir, rel), "x"); + expect(projectLocalFile(dir, `http://127.0.0.1:3000/${rel}`)).toBeNull(); + }, + ); + + it("gives null for a link whose target is a private project file", () => { + mkdirSync(join(dir, ".hyperframes"), { recursive: true }); + writeFileSync(join(dir, ".hyperframes", "record.mp4"), "x"); + symlinkSync(join(dir, ".hyperframes", "record.mp4"), join(dir, "clips", "link.mp4")); + expect(projectLocalFile(dir, "clips/link.mp4")).toBeNull(); + }); + + it("names a file in Studio's own prepared-assets folder", () => { + const rel = ".hyperframes/prepared-assets/clip.mp4"; + mkdirSync(dirname(join(dir, rel)), { recursive: true }); + writeFileSync(join(dir, rel), "x"); + expect(projectLocalFile(dir, rel)).toBe(join(dir, rel)); + }); }); const probeJson = (stream: object, duration = "12.480000") => @@ -141,6 +164,16 @@ describe("heldVideoHints", () => { { id: "wide", width: 1280, height: 720 }, ]); }); + + it("never probes a private project file", async () => { + mkdirSync(join(dir, ".hyperframes"), { recursive: true }); + writeFileSync(join(dir, ".hyperframes", "record.mp4"), "x"); + run.mockResolvedValue({ stdout: probeJson({ width: 1280, height: 720 }) }); + const videos = [{ id: "kept", src: ".hyperframes/record.mp4" }]; + + await expect(heldVideoHints(dir, videos, new Map(), "ffprobe")).resolves.toEqual([]); + expect(run).not.toHaveBeenCalled(); + }); }); describe("projectVideoFile", () => { diff --git a/packages/cli/src/commands/snapshotHeldVideos.ts b/packages/cli/src/commands/snapshotHeldVideos.ts index f1f7d2b55a..b06ae99003 100644 --- a/packages/cli/src/commands/snapshotHeldVideos.ts +++ b/packages/cli/src/commands/snapshotHeldVideos.ts @@ -2,7 +2,11 @@ import { existsSync } from "node:fs"; import { isAbsolute, relative, resolve } from "node:path"; import { parseHTML } from "linkedom"; import { MEDIA_RENDER_ID_ATTR } from "@hyperframes/core"; -import { isNotMediaPayload, type CaptureVideoMetadataHint } from "@hyperframes/engine"; +import { + isNotMediaPayload, + isPrivateUnderAssetRoots, + type CaptureVideoMetadataHint, +} from "@hyperframes/engine"; import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; import { findFFprobe } from "../browser/ffmpeg.js"; import { runCancellableProcess } from "../utils/cancellableProcess.js"; @@ -22,7 +26,7 @@ export function localVideos(html: string): { id: string; src: string }[] { export const localVideoRenderIds = (html: string): string[] => localVideos(html).map((v) => v.id); -/** The project file a page URL names, or null when it is remote or outside the project. */ +/** The project file a page URL names, or null when it is remote, outside the project or private. */ export function projectLocalFile(projectDir: string, src: string): string | null { try { const path = decodeURIComponent(new URL(src, "http://project.invalid/").pathname).replace( @@ -31,7 +35,8 @@ export function projectLocalFile(projectDir: string, src: string): string | null ); const file = resolve(projectDir, path); const rel = relative(projectDir, file); - return !rel.startsWith("..") && !isAbsolute(rel) && existsSync(file) ? file : null; + const inside = !rel.startsWith("..") && !isAbsolute(rel) && existsSync(file); + return inside && !isPrivateUnderAssetRoots(file, [projectDir]) ? file : null; } catch { return null; } diff --git a/packages/cli/src/registry/resolver.test.ts b/packages/cli/src/registry/resolver.test.ts index 8ee896e337..ba4e448902 100644 --- a/packages/cli/src/registry/resolver.test.ts +++ b/packages/cli/src/registry/resolver.test.ts @@ -131,6 +131,19 @@ describe("registry resolver", () => { expect(warnings.length).toBeGreaterThan(0); expect(warnings.some((w) => w.includes("beta"))).toBe(true); }); + + it("writes a skipped item's name to stderr with control characters escaped", async () => { + const write = vi.spyOn(process.stderr, "write").mockImplementation(() => true); + try { + const entries = [{ name: "be\u001b]0;t\u0007ta", type: "hyperframes:example" as const }]; + await loadAllItems(entries, { baseUrl: uniqueBaseUrl() }); + const line = write.mock.calls.map(([chunk]) => String(chunk)).join(""); + expect(line).toContain("be\\x1b]0;t\\x07ta"); + for (const control of ["\u001b", "\u0007"]) expect(line).not.toContain(control); + } finally { + write.mockRestore(); + } + }); }); describe("loadAllItems on a slow registry", () => { diff --git a/packages/cli/src/registry/resolver.ts b/packages/cli/src/registry/resolver.ts index fe59b3a0b4..80465eb21c 100644 --- a/packages/cli/src/registry/resolver.ts +++ b/packages/cli/src/registry/resolver.ts @@ -4,7 +4,12 @@ * when blocks/components need it for the `add` command. */ -import type { ItemType, RegistryItem, RegistryManifestEntry } from "@hyperframes/core"; +import { + terminalText, + type ItemType, + type RegistryItem, + type RegistryManifestEntry, +} from "@hyperframes/core"; import { fetchItemManifest, fetchRegistryManifest, DEFAULT_REGISTRY_URL } from "./remote.js"; import { runWithConcurrencyLimit } from "../utils/concurrency.js"; import { closestNames } from "../utils/didYouMean.js"; @@ -22,7 +27,7 @@ export interface ResolveOptions { } function defaultWarn(message: string): void { - process.stderr.write(`hyperframes:registry ${message}\n`); + process.stderr.write(`hyperframes:registry ${terminalText(message)}\n`); } /** diff --git a/packages/cli/src/server/studioServer.registryInstall.test.ts b/packages/cli/src/server/studioServer.registryInstall.test.ts index 067d94867b..3766bb640d 100644 --- a/packages/cli/src/server/studioServer.registryInstall.test.ts +++ b/packages/cli/src/server/studioServer.registryInstall.test.ts @@ -37,6 +37,7 @@ const block = (name: string, dependencies?: string[]) => ({ }); const ITEMS = [ block("studio-drop-block"), + { ...block("studio-drop-retired"), deprecated: "use \u001b]0;t\u0007other" }, block("studio-drop-other"), block("studio-drop-parent", ["studio-drop-part"]), { @@ -125,6 +126,19 @@ function installer(link: string) { } describe("Studio catalog install", () => { + it("writes a registry warning to stderr with control characters escaped", async () => { + const { link } = projectWithRegistry(); + const write = vi.spyOn(process.stderr, "write").mockImplementation(() => true); + try { + await installer(link)("studio-drop-retired"); + const line = write.mock.calls.map(([chunk]) => String(chunk)).join(""); + expect(line).toContain("use \\x1b]0;t\\x07other"); + for (const control of ["\u001b", "\u0007"]) expect(line).not.toContain(control); + } finally { + write.mockRestore(); + } + }); + it("answers that the project folder is gone when it is renamed while the item downloads", async () => { let real = ""; ({ real } = projectWithRegistry(() => renameSync(real, `${real}-renamed`))); diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts index 0082e4562c..47eb6d375f 100644 --- a/packages/cli/src/server/studioServer.ts +++ b/packages/cli/src/server/studioServer.ts @@ -9,7 +9,7 @@ import { PREVIEW_NONCE_HEADER, previewProof } from "@hyperframes/core/preview-pr import type { HttpBindings } from "@hono/node-server"; import { Hono, type Context } from "hono"; import { streamSSE } from "hono/streaming"; -import { realpath } from "@hyperframes/core"; +import { realpath, terminalText } from "@hyperframes/core"; import { existsSync, readFileSync, statSync, writeFileSync, unlinkSync } from "node:fs"; import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { createHash } from "node:crypto"; @@ -925,7 +925,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { source: "studio", }); for (const warning of result.warnings) { - process.stderr.write(`hyperframes:registry ${warning}\n`); + process.stderr.write(`hyperframes:registry ${terminalText(warning)}\n`); } const written = result.written; diff --git a/packages/cli/src/templates/remote.test.ts b/packages/cli/src/templates/remote.test.ts index 18232c03b5..8547a22c41 100644 --- a/packages/cli/src/templates/remote.test.ts +++ b/packages/cli/src/templates/remote.test.ts @@ -1,4 +1,7 @@ -import { describe, expect, it } from "vitest"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { describe, expect, it, onTestFinished, vi } from "vitest"; import { MANIFEST_FILENAME, TEMPLATES_DIR } from "./remote.js"; // These constants construct the GitHub URL that installed CLIs use to fetch @@ -13,3 +16,32 @@ describe("remote template path constants", () => { expect(MANIFEST_FILENAME).toBe("templates.json"); }); }); + +describe("fetchRemoteTemplate", () => { + it("writes a registry warning to stderr with control characters escaped", async () => { + const destDir = mkdtempSync(join(tmpdir(), "hf-remote-template-")); + const write = vi.spyOn(process.stderr, "write").mockImplementation(() => true); + onTestFinished(() => { + write.mockRestore(); + vi.doUnmock("../registry/resolver.js"); + vi.doUnmock("../registry/index.js"); + rmSync(destDir, { recursive: true, force: true }); + }); + vi.resetModules(); + vi.doMock("../registry/resolver.js", () => ({ + resolveItemWithDependencies: async () => [ + { name: "starter", type: "hyperframes:example", deprecated: "use \u001b]0;t\u0007x" }, + ], + })); + vi.doMock("../registry/index.js", () => ({ + installItem: async () => writeFileSync(join(destDir, "index.html"), ""), + })); + const { fetchRemoteTemplate } = await import("./remote.js"); + + await fetchRemoteTemplate("starter", destDir); + + const line = write.mock.calls.map(([chunk]) => String(chunk)).join(""); + expect(line).toContain("use \\x1b]0;t\\x07x"); + for (const control of ["\u001b", "\u0007"]) expect(line).not.toContain(control); + }); +}); diff --git a/packages/cli/src/templates/remote.ts b/packages/cli/src/templates/remote.ts index 5bd3bd64ca..c1f1ff7dff 100644 --- a/packages/cli/src/templates/remote.ts +++ b/packages/cli/src/templates/remote.ts @@ -5,6 +5,7 @@ import { existsSync } from "node:fs"; import { join } from "node:path"; +import { terminalText } from "@hyperframes/core"; import { installItem, listRegistryItems, loadAllItems } from "../registry/index.js"; import { resolveItemWithDependencies } from "../registry/resolver.js"; import { gateRegistryItemsCompatibility } from "../registry/compatibility.js"; @@ -53,7 +54,7 @@ export async function fetchRemoteTemplate(templateId: string, destDir: string): const items = await resolveItemWithDependencies(templateId); const warnings = gateRegistryItemsCompatibility(items); for (const warning of warnings) { - process.stderr.write(`hyperframes:registry ${warning}\n`); + process.stderr.write(`hyperframes:registry ${terminalText(warning)}\n`); } for (const item of items) { await installItem(item, { destDir }); diff --git a/packages/engine/src/services/videoFrameExtractor.test.ts b/packages/engine/src/services/videoFrameExtractor.test.ts index f821ee8b96..440693cc75 100644 --- a/packages/engine/src/services/videoFrameExtractor.test.ts +++ b/packages/engine/src/services/videoFrameExtractor.test.ts @@ -708,6 +708,29 @@ describe("video extraction failure taxonomy and bounded retry", () => { } }); + it("warns about a missing source with control characters shown as visible escapes", async () => { + const dir = mkdtempSync(join(tmpdir(), "hf-extract-missing-")); + const write = vi.spyOn(process.stderr, "write").mockImplementation(() => true); + try { + const src = "missing\x1b[2J\x07\x9b.mp4"; + const video = { id: "v1", src, start: 0, end: 1, mediaStart: 0, loop: false }; + const result = await extractAllVideoFrames([{ ...video, hasAudio: false }], dir, { + fps: 30, + outputDir: join(dir, "out"), + assetRoots: [dir], + }); + expect(result.errors).toEqual([expect.objectContaining({ kind: "source_missing" })]); + const warning = write.mock.calls + .map(([chunk]) => String(chunk)) + .find((chunk) => chunk.includes("could not be resolved on disk")); + expect(warning).toContain('src="missing\\x1b[2J\\x07\\x9b.mp4"'); + for (const control of ["\u001b", "\u0007", "\u009b"]) expect(warning).not.toContain(control); + } finally { + write.mockRestore(); + rmSync(dir, { recursive: true, force: true }); + } + }); + it.each([".git/clip.mp4", ".hyperframes/clip.mp4", ".ssh/clip.mp4"])( "treats the private project file %s as a missing source", async (src) => { diff --git a/packages/engine/src/services/videoFrameExtractor.ts b/packages/engine/src/services/videoFrameExtractor.ts index e65e91eca9..94f23ce3e7 100644 --- a/packages/engine/src/services/videoFrameExtractor.ts +++ b/packages/engine/src/services/videoFrameExtractor.ts @@ -45,6 +45,7 @@ import { hdrToSdrToneMapFilter, parseFirstFrameColour, readsKeyframeTransfers, + terminalText, } from "@hyperframes/core"; import { resolveReferencedStart, type RefResolverEl } from "./referenceResolver.js"; import { isKnownInactiveTimelineWindow } from "./mediaTimelineWindow.js"; @@ -2307,8 +2308,8 @@ export async function extractAllVideoFrames( if (!warnedSrcs.has(video.src)) { warnedSrcs.add(video.src); process.stderr.write( - `[hyperframes:render] WARNING: video src="${video.src}" ` + - `could not be resolved on disk (looked for ${videoPath}). ` + + `[hyperframes:render] WARNING: video src="${terminalText(video.src)}" ` + + `could not be resolved on disk (looked for ${terminalText(videoPath)}). ` + `The rendered output will show this video's first frame for the entire clip duration. ` + `If your