fix(cli): copy only media, font, CSS and JS files from outside the project (#7636)

* fix(cli): copy only media, font, CSS and JS files from outside the project

Publish, cloud render and feedback now copy an outside reference into _ext/
only when its extension is an image, video, audio, font, CSS or JS type,
taken from studio-server's MIME map (new ./mime subpath; .avif added).
Anything else outside the project is left out with a warning and a
left_out entry whose reason is file_type; telemetry counts it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): keep the new outside-type file under its package's comment share

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): copy the outside media cloud render re-encodes or converts

The outside allowlist now also takes the video, audio and still-image
extensions cloud render re-encodes or converts (.avi, .ogv, .aiff, .tif,
.bmp and the rest), from one shared list offload imports too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
James Russo
2026-11-13 03:24:23 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent b54e4981f2
commit 89a7aa3004
15 changed files with 320 additions and 58 deletions
+32
View File
@@ -0,0 +1,32 @@
export const VIDEO_EXTS = new Set([
".mp4",
".m4v",
".mov",
".webm",
".mkv",
".avi",
".mpg",
".mpeg",
".ts",
".m2ts",
".mts",
".flv",
".wmv",
".ogv",
".3gp",
]);
export const AUDIO_EXTS = new Set([
".wav",
".mp3",
".m4a",
".aac",
".ogg",
".oga",
".opus",
".flac",
".aif",
".aiff",
".wma",
]);
// Still images the API refuses; converted to PNG unless animated.
export const CONVERTIBLE_IMAGE_EXTS = new Set([".gif", ".avif", ".bmp", ".tif", ".tiff"]);
+1 -33
View File
@@ -33,6 +33,7 @@ import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg
import { probeAudio } from "../audio/analysis/decode.js";
import { formatBytes } from "../ui/format.js";
import { bytesMatchExtension } from "./offloadSniff.js";
import { AUDIO_EXTS, CONVERTIBLE_IMAGE_EXTS, VIDEO_EXTS } from "./mediaExtensions.js";
import { type PlayedSegment, planTrim } from "./offloadTrim.js";
import {
archiveKey,
@@ -70,39 +71,6 @@ const ACCEPTED: Record<string, { contentType: string; maxBytes: number }> = {
".mp3": { contentType: "audio/mpeg", maxBytes: AUDIO_MAX_BYTES },
};
const VIDEO_EXTS = new Set([
".mp4",
".m4v",
".mov",
".webm",
".mkv",
".avi",
".mpg",
".mpeg",
".ts",
".m2ts",
".mts",
".flv",
".wmv",
".ogv",
".3gp",
]);
const AUDIO_EXTS = new Set([
".wav",
".mp3",
".m4a",
".aac",
".ogg",
".oga",
".opus",
".flac",
".aif",
".aiff",
".wma",
]);
// Still images the API refuses; converted to PNG unless animated.
const CONVERTIBLE_IMAGE_EXTS = new Set([".gif", ".avif", ".bmp", ".tif", ".tiff"]);
/** Visually lossless re-encodes for media the asset API refuses or caps. Quality lives here. */
const REENCODE_PRESETS = {
video: {
@@ -7,7 +7,7 @@ import { fileURLToPath } from "node:url";
const cliEntry = resolve(fileURLToPath(import.meta.url), "..", "..", "..", "cli.ts");
/** `<root>/p` whose received index.html points at `<root>/notes.txt` and whose own page points at `<root>/.env`. */
/** `<root>/p` whose received index.html points at `<root>/notes.txt`; its own page points at `<root>/.env` and `<root>/data.json`. */
function importedProject(): string {
const root = mkdtempSync(join(tmpdir(), "hf-cloud-left-out-"));
onTestFinished(() => rmSync(root, { recursive: true, force: true }));
@@ -15,11 +15,12 @@ function importedProject(): string {
mkdirSync(join(dir, ".hyperframes"), { recursive: true });
writeFileSync(join(root, "notes.txt"), "outside");
writeFileSync(join(root, ".env"), "SECRET=1");
writeFileSync(join(root, "data.json"), "{}");
writeFileSync(
join(dir, "index.html"),
`<!doctype html><html><body><div data-composition-id="main" data-width="1920" data-height="1080"><link href="../notes.txt"></div></body></html>`,
);
writeFileSync(join(dir, "mine.html"), `<link href="../.env">`);
writeFileSync(join(dir, "mine.html"), `<link href="../.env"><link href="../data.json">`);
writeFileSync(
join(dir, ".hyperframes", "received-files.json"),
JSON.stringify({ files: ["index.html"] }),
@@ -64,8 +65,15 @@ describe("cloud render --dry-run --json lists what the upload leaves out", () =>
reason: "blocklisted",
levels_up: 1,
},
{
file: "mine.html",
reference: "../data.json",
path: "../data.json",
reason: "file_type",
levels_up: 1,
},
];
expect(archive.left_out).toHaveLength(2);
expect(archive.left_out).toHaveLength(3);
expect(archive.left_out).toEqual(expect.arrayContaining(expected));
const events = result.stderr
.split("\n")
@@ -18,19 +18,29 @@ describe("cli_upload_left_out", () => {
beforeEach(() => trackEvent.mockClear());
it("counts what was left out and how far above the project, and names nothing", () => {
trackUploadLeftOut("cloud_render", [at(1), at(1, "blocklisted"), at(2), at(5), at(null)], true);
const references = [
at(1),
at(1, "blocklisted"),
at(2),
at(5),
at(null),
at(1, "file_type"),
at(2, "file_type"),
];
trackUploadLeftOut("cloud_render", references, true);
expect(trackEvent).toHaveBeenCalledTimes(1);
const [event, properties] = trackEvent.mock.calls[0]!;
expect(event).toBe("cli_upload_left_out");
expect(properties).toEqual({
command: "cloud_render",
dry_run: true,
left_out_count: 5,
left_out_count: 7,
received_count: 4,
blocklisted_count: 1,
file_type_count: 2,
max_levels_up: 5,
levels_up_1: 2,
levels_up_2: 1,
levels_up_1: 3,
levels_up_2: 2,
levels_up_3_plus: 1,
other_drive: 1,
});
@@ -11,6 +11,7 @@ function leftOutCounts(references: readonly LeftOutReference[]) {
left_out_count: references.length,
received_count: references.filter((reference) => reference.reason === "received").length,
blocklisted_count: references.filter((reference) => reference.reason === "blocklisted").length,
file_type_count: references.filter((reference) => reference.reason === "file_type").length,
max_levels_up: Math.max(0, ...levels.map((n) => n ?? 0)),
levels_up_1: counted((n) => n === 1),
levels_up_2: counted((n) => n === 2),
+13 -2
View File
@@ -1,8 +1,8 @@
import { isAbsolute, relative } from "node:path";
import { realFilePath } from "@hyperframes/studio-server/safe-path";
/** `blocklisted`: a private location; `received`: reached from a file that came in with an imported project. */
export type LeftOutReason = "blocklisted" | "received";
/** `blocklisted`: a private location; `received`: reached from an imported file; `file_type`: not a type uploads copy. */
export type LeftOutReason = "blocklisted" | "received" | "file_type";
/** One file an upload left out. `path` is from the project folder; `levels_up` is null on another drive. */
export interface LeftOutReference {
@@ -15,6 +15,17 @@ export interface LeftOutReference {
export type OnLeftOut = (warning: string, reference: LeftOutReference) => void;
const WHY: Record<LeftOutReason, string> = {
blocklisted: "which is outside the project in a private location",
received: "which leads outside the project and came in with an imported project",
file_type: "which is outside the project and is not an image, video, audio, font, CSS or JS file",
};
export function leftOutWarning(file: string, reference: string, reason: LeftOutReason): string {
const warning = `${file} references ${reference}, ${WHY[reason]}; it was left out of the upload.`;
return reason === "file_type" ? `${warning} Move it into the project to include it.` : warning;
}
export function leftOutReference(
projectDir: string,
file: string,
+22
View File
@@ -0,0 +1,22 @@
import { extname } from "node:path";
import { MIME_TYPES } from "@hyperframes/studio-server/mime";
import { realFilePath } from "@hyperframes/studio-server/safe-path";
import { AUDIO_EXTS, CONVERTIBLE_IMAGE_EXTS, VIDEO_EXTS } from "../cloud/mediaExtensions.js";
const COPIED_TYPE = /^(?:image|video|audio|font)\/|^text\/(?:css|javascript);/;
/** What an upload copies from outside the project: the preview server's image, video, audio, font, CSS and JS types,
* and the media cloud render re-encodes or converts. */
export const OUTSIDE_FILE_EXTENSIONS: ReadonlySet<string> = new Set([
...Object.entries(MIME_TYPES)
.filter(([, type]) => COPIED_TYPE.test(type))
.map(([extension]) => extension),
...VIDEO_EXTS,
...AUDIO_EXTS,
...CONVERTIBLE_IMAGE_EXTS,
]);
const hasCopiedType = (path: string) => OUTSIDE_FILE_EXTENSIONS.has(extname(path).toLowerCase());
export const isCopiedOutsideFileType = (absPath: string): boolean =>
hasCopiedType(absPath) && hasCopiedType(realFilePath(absPath));
@@ -0,0 +1,179 @@
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import type { LeftOutReference } from "./leftOutReference.js";
import { OUTSIDE_FILE_EXTENSIONS } from "./outsideFileType.js";
import { stagePublishFiles } from "./publishProject.js";
const dirs: string[] = [];
afterEach(() => dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true })));
/** Writes `files` under a fresh folder, stages its `p` project, and collects what was left out. */
function stage(files: Record<string, string>) {
const root = mkdtempSync(join(tmpdir(), "hf-file-type-"));
dirs.push(root);
for (const [name, body] of Object.entries(files)) {
mkdirSync(dirname(join(root, name)), { recursive: true });
writeFileSync(join(root, name), body.replaceAll("<root>", root));
}
const warnings: string[] = [];
const leftOut: LeftOutReference[] = [];
const staged = stagePublishFiles(join(root, "p"), Infinity, (warning, reference) => {
warnings.push(warning);
leftOut.push(reference);
});
const ext = [...staged.keys()].filter((key) => key.startsWith("_ext/")).sort();
return { root, staged, warnings, leftOut, ext };
}
describe("a file outside the project", () => {
it("is still copied when it is an image, video, audio, font or stylesheet, without a warning", () => {
const { ext, warnings } = stage({
"shared/logo.png": "png",
"shared/LOUD.PNG": "png",
"shared/clip.mp4": "mp4",
"shared/brand.woff2": "woff2",
"shared/theme.css": "body {}",
"shared/old.avi": "avi",
"shared/scan.tif": "tif",
"shared/take.aiff": "aiff",
"p/index.html": [
`<img src="../shared/logo.png"><img src="../shared/LOUD.PNG"><img src="../shared/scan.tif">`,
`<video src="../shared/old.avi"></video><audio src="../shared/take.aiff"></audio>`,
`<video src="../shared/clip.mp4"></video><link rel="stylesheet" href="../shared/theme.css">`,
`<style>@font-face { font-family: B; src: url("../shared/brand.woff2"); }</style>`,
].join(""),
});
expect(ext).toEqual([
"_ext/shared/LOUD.PNG",
"_ext/shared/brand.woff2",
"_ext/shared/clip.mp4",
"_ext/shared/logo.png",
"_ext/shared/old.avi",
"_ext/shared/scan.tif",
"_ext/shared/take.aiff",
"_ext/shared/theme.css",
]);
expect(warnings).toEqual([]);
});
it("is left out when it is any other type, for ../, absolute and percent-encoded spellings", () => {
const { root, staged, ext, warnings, leftOut } = stage({
"shared/scene.json": "{}",
"shared/brief.pdf": "pdf",
"shared/notes": "text",
"p/index.html": [
`<lottie-player src="../shared/scene.json"></lottie-player>`,
`<a href="<root>/shared/brief.pdf"></a>`,
`<link href="%2e%2e/shared/notes">`,
].join(""),
});
expect(ext).toEqual([]);
const tail =
"which is outside the project and is not an image, video, audio, font, CSS or JS file; it was left out of the upload. Move it into the project to include it.";
expect(warnings.sort()).toEqual(
[
`index.html references ../shared/scene.json, ${tail}`,
`index.html references ${root}/shared/brief.pdf, ${tail}`,
`index.html references %2e%2e/shared/notes, ${tail}`,
].sort(),
);
const at = (reference: string, path: string) => ({
file: "index.html",
reference,
path,
reason: "file_type",
levels_up: 1,
});
expect(leftOut).toEqual(
expect.arrayContaining([
at("../shared/scene.json", "../shared/scene.json"),
at(`${root}/shared/brief.pdf`, "../shared/brief.pdf"),
at("%2e%2e/shared/notes", "../shared/notes"),
]),
);
expect(leftOut).toHaveLength(3);
const index = staged.get("index.html")!.toString("utf-8");
expect(index).toContain(`src="../shared/scene.json"`);
});
it("follows the same rule for CSS url() targets and @import", () => {
const { ext, warnings, leftOut } = stage({
"shared/theme.css": "body {}",
"shared/data.json": "{}",
"p/style.css": `@import url("../shared/theme.css"); body { background: url("../shared/data.json"); }`,
"p/index.html": `<link rel="stylesheet" href="style.css">`,
});
expect(ext).toEqual(["_ext/shared/theme.css"]);
expect(warnings).toEqual([
expect.stringMatching(/^style\.css references \.\.\/shared\/data\.json, which is outside/),
]);
expect(leftOut.map((one) => one.reason)).toEqual(["file_type"]);
});
it("is left out when its name looks like an image but it links to another type", () => {
const root = mkdtempSync(join(tmpdir(), "hf-file-type-"));
dirs.push(root);
mkdirSync(join(root, "p"));
writeFileSync(join(root, "data.json"), "{}");
symlinkSync(join(root, "data.json"), join(root, "logo.png"));
writeFileSync(join(root, "p", "index.html"), `<img src="../logo.png">`);
const reasons: string[] = [];
const staged = stagePublishFiles(join(root, "p"), Infinity, (_, one) =>
reasons.push(one.reason),
);
expect([...staged.keys()].some((key) => key.startsWith("_ext/"))).toBe(false);
expect(reasons).toEqual(["file_type"]);
});
it("is left out when its own name is another type, even if it links to an image", () => {
const root = mkdtempSync(join(tmpdir(), "hf-file-type-"));
dirs.push(root);
mkdirSync(join(root, "p"));
writeFileSync(join(root, "logo.png"), "png");
symlinkSync(join(root, "logo.png"), join(root, "scene.json"));
writeFileSync(join(root, "p", "index.html"), `<link href="../scene.json">`);
const reasons: string[] = [];
const staged = stagePublishFiles(join(root, "p"), Infinity, (_, one) =>
reasons.push(one.reason),
);
expect([...staged.keys()].some((key) => key.startsWith("_ext/"))).toBe(false);
expect(reasons).toEqual(["file_type"]);
});
it("keeps the private-location reason ahead of the file type", () => {
const { ext, leftOut } = stage({
"shared/.cache/state.json": "{}",
"p/index.html": `<link href="../shared/.cache/state.json">`,
});
expect(ext).toEqual([]);
expect(leftOut.map((one) => one.reason)).toEqual(["blocklisted"]);
});
it("does not change what is copied from inside the project", () => {
const { staged, warnings } = stage({
"p/data/scene.json": "{}",
"p/index.html": `<lottie-player src="data/scene.json"></lottie-player>`,
});
expect(staged.has("data/scene.json")).toBe(true);
expect(warnings).toEqual([]);
});
});
describe("the outside file types", () => {
it("are images, video, audio, fonts, CSS and JS, and nothing else", () => {
for (const extension of [".png", ".avif", ".svg", ".bmp", ".tif", ".mp4", ".mov", ".webm"]) {
expect(OUTSIDE_FILE_EXTENSIONS).toContain(extension);
}
for (const extension of [".avi", ".ogv", ".mp3", ".wav", ".aiff"]) {
expect(OUTSIDE_FILE_EXTENSIONS).toContain(extension);
}
for (const extension of [".woff", ".woff2", ".ttf", ".otf", ".css", ".js", ".mjs"]) {
expect(OUTSIDE_FILE_EXTENSIONS).toContain(extension);
}
for (const extension of [".json", ".html", ".txt", ".md", ".cube", ".pdf", ".key", ""]) {
expect(OUTSIDE_FILE_EXTENSIONS).not.toContain(extension);
}
});
});
+15 -9
View File
@@ -29,7 +29,13 @@ import {
linksToPrivateOutsideFile,
receivedFiles,
} from "./privateProjectFile.js";
import { leftOutReference, type OnLeftOut } from "./leftOutReference.js";
import {
leftOutReference,
leftOutWarning,
type LeftOutReason,
type OnLeftOut,
} from "./leftOutReference.js";
import { isCopiedOutsideFileType } from "./outsideFileType.js";
const IGNORED_DIRS = new Set([".git", "node_modules", "dist", ".next", "coverage"]);
const IGNORED_FILES = new Set([".DS_Store", "Thumbs.db"]);
@@ -449,7 +455,7 @@ function includeReferencedProjectFile(
if (staysOut(ctx, rel, absPath)) return;
if (!existsSync(absPath)) continue;
if (ctx.received(entryPath) && linksOutside(ctx.absProjectDir, absPath)) {
return leaveOut(ctx, entryPath, rawPath, absPath, RECEIVED);
return leaveOut(ctx, entryPath, rawPath, absPath, "received");
}
if (statSync(absPath).isFile()) {
ctx.fileContents.set(rel, stageFile(absPath, ctx.onDiskMinBytes));
@@ -466,20 +472,16 @@ const HARD_REFERENCES = new Set(["video[src]", "audio[src]", "img[src]", "video[
const relativeToProject = (ctx: ExternalAssetContext, absPath: string) =>
encodeUrlPath(relative(ctx.absProjectDir, absPath).replaceAll("\\", "/"));
const PRIVATE = "which is outside the project in a private location";
const RECEIVED = "which leads outside the project and came in with an imported project";
function leaveOut(
ctx: ExternalAssetContext,
entryPath: string,
rawPath: string,
absPath: string,
why = PRIVATE,
reason: LeftOutReason = "blocklisted",
): void {
const warning = `${entryPath} references ${rawPath}, ${why}; it was left out of the upload.`;
const warning = leftOutWarning(entryPath, rawPath, reason);
if (ctx.leftOut.has(warning)) return;
ctx.leftOut.add(warning);
const reason = why === RECEIVED ? "received" : "blocklisted";
ctx.onLeftOut?.(
warning,
leftOutReference(ctx.absProjectDir, entryPath, rawPath, absPath, reason),
@@ -499,13 +501,17 @@ function archiveTargetOf(
try {
if (!existsSync(absPath) || !statSync(absPath).isFile()) return undefined;
if (ctx.received(options.entryPath)) {
leaveOut(ctx, options.entryPath, rawPath, absPath, RECEIVED);
leaveOut(ctx, options.entryPath, rawPath, absPath, "received");
return undefined;
}
if (isPrivateOutsideFile(ctx.absProjectDir, absPath)) {
leaveOut(ctx, options.entryPath, rawPath, absPath);
return undefined;
}
if (!isCopiedOutsideFileType(absPath)) {
leaveOut(ctx, options.entryPath, rawPath, absPath, "file_type");
return undefined;
}
} catch {
return undefined;
}