diff --git a/packages/cli/src/utils/publishProject.test.ts b/packages/cli/src/utils/publishProject.test.ts index db1f771c2d..d93a6eca21 100644 --- a/packages/cli/src/utils/publishProject.test.ts +++ b/packages/cli/src/utils/publishProject.test.ts @@ -1504,3 +1504,59 @@ describe("readStaged", () => { expect(() => readStaged(staged)).toThrow(/changed on disk/); }); }); + +describe("createPublishArchive referenced dot-paths", () => { + const clip = ".hyperframes/prepared-assets/clip.mp4"; + + function projectWith(html: string, files: Record): string { + const dir = makeProjectDir(); + writeFileSync(join(dir, "index.html"), html, "utf-8"); + for (const [name, body] of Object.entries(files)) { + mkdirSync(dirname(join(dir, name)), { recursive: true }); + writeFileSync(join(dir, name), body, "utf-8"); + } + return dir; + } + + const names = (dir: string) => + new AdmZip(createPublishArchive(dir).buffer).getEntries().map((e) => e.entryName); + + it("carries a referenced file under a dot-folder", () => { + const dir = projectWith(``, { [clip]: "v" }); + expect(names(dir)).toContain(clip); + }); + + it("fails before upload when a referenced file is missing", () => { + const dir = projectWith(``, {}); + expect(() => createPublishArchive(dir)).toThrow(clip); + }); + + it("does not fail on a missing href, source fallback, srcset candidate or CSS url", () => { + const dir = projectWith( + `n` + + ``, + { "a.mp4": "v", "a.png": "p", "s.css": ".a{background:url(gone.jpg)}" }, + ); + expect(() => createPublishArchive(dir)).not.toThrow(); + expect(names(dir)).not.toContain("notes.html"); + }); + + it("fails on a missing video, audio, img src or poster, naming the file", () => { + for (const tag of [ + 'video src="gone.mp4"', + 'audio src="gone.mp3"', + 'img src="gone.png"', + 'video poster="gone.jpg"', + ]) { + const name = /"(gone\.[a-z0-9]+)"/.exec(tag)![1]!; + expect(() => + createPublishArchive(projectWith(`<${tag}>`, {})), + ).toThrow(name); + } + }); + + it("leaves out an unreferenced .env", () => { + const dir = projectWith(``, { [clip]: "v", ".env": "SECRET=1" }); + expect(names(dir)).not.toContain(".env"); + }); +}); diff --git a/packages/cli/src/utils/publishProject.ts b/packages/cli/src/utils/publishProject.ts index 0719428df1..78fd43088b 100644 --- a/packages/cli/src/utils/publishProject.ts +++ b/packages/cli/src/utils/publishProject.ts @@ -414,15 +414,45 @@ function addExternalAsset(ctx: ExternalAssetContext, absPath: string): string { return archivePath; } +/** Ships a referenced in-project file the ignore rules skipped, resolved from its folder or the root. */ +function includeReferencedProjectFile( + ctx: ExternalAssetContext, + resolvedPath: string, + rawPath: string, + requireOnDisk: boolean, +): void { + const fromRoot = resolve(ctx.absProjectDir, rawPath); + for (const candidate of [resolvedPath, fromRoot]) { + const absPath = candidate.replace(/[?#].*$/, ""); + if (!isPathInside(absPath, ctx.absProjectDir)) continue; + const rel = relative(ctx.absProjectDir, absPath).replaceAll("\\", "/"); + if (!rel || ctx.fileContents.has(rel) || rel.split("/").some(shouldIgnoreSegment)) return; + if (!existsSync(absPath)) continue; + if (statSync(absPath).isFile()) { + ctx.fileContents.set(rel, stageFile(absPath, ctx.onDiskMinBytes)); + } + return; + } + if (!requireOnDisk) return; + throw new Error(`The composition references "${rawPath}", which is not in the project folder.`); +} + +/** References with no fallback: a missing file here cannot render. */ +const HARD_REFERENCES = new Set(["video[src]", "audio[src]", "img[src]", "video[poster]"]); + function tryResolveExternal( ctx: ExternalAssetContext, rawPath: string, referrerAbsDir: string, + requireOnDisk = false, ): string | null { if (isNonRelativeUrl(rawPath)) return null; for (const path of decodeUrlPathVariants(rawPath)) { const absPath = resolve(referrerAbsDir, path); - if (isPathInside(absPath, ctx.absProjectDir)) return null; + if (isPathInside(absPath, ctx.absProjectDir)) { + includeReferencedProjectFile(ctx, absPath, rawPath, requireOnDisk); + return null; + } try { if (!existsSync(absPath) || !statSync(absPath).isFile()) continue; } catch { @@ -435,7 +465,11 @@ function tryResolveExternal( /** Resolves a raw attribute or CSS `url()` value to the archive path or absolute * URL it should point at, or `null` to leave it untouched. */ -export type HtmlAttributeResolver = (rawValue: string, referrerAbsDir: string) => string | null; +export type HtmlAttributeResolver = ( + rawValue: string, + referrerAbsDir: string, + ref?: { attr: string; tag: string }, +) => string | null; function referenceFrom(entryPath: string, target: string): string { return isNonRelativeUrl(target) ? target : posix.relative(posix.dirname(entryPath), target); @@ -490,7 +524,7 @@ export function rewriteHtmlAttributes( for (const attr of attrs) { const val = (el.getAttribute(attr) || "").trim(); if (!val) continue; - const target = resolveTarget(val, referrerAbsDir); + const target = resolveTarget(val, referrerAbsDir, { attr, tag: el.localName }); if (!target) continue; el.setAttribute(attr, referenceFrom(entryPath, target)); modified = true; @@ -551,7 +585,13 @@ function rewriteHtmlSrcsets( function localizeHtmlEntry(ctx: ExternalAssetContext, entryPath: string, content: Buffer): void { const referrerAbsDir = resolve(ctx.absProjectDir, dirname(entryPath)); const { document } = parseHTML(content.toString("utf-8")); - const resolveTarget: HtmlAttributeResolver = (val, dir) => tryResolveExternal(ctx, val, dir); + const resolveTarget: HtmlAttributeResolver = (val, dir, ref) => + tryResolveExternal( + ctx, + val, + dir, + ref !== undefined && HARD_REFERENCES.has(`${ref.tag}[${ref.attr}]`), + ); const attrsChanged = rewriteHtmlAttributes(document, referrerAbsDir, entryPath, resolveTarget, { attrs: ["src", "href", "poster"], });