mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(cli): a media file referenced by its filesystem path goes into the cloud archive (#7381)
* fix(cli): a media file referenced by its filesystem path goes into the cloud archive * fix(cli): an absolute media path keeps its fragment and query, and the resolver is split * fix(cli): a Windows drive-letter media path is a filesystem path too
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { stagePublishFiles } from "./publishProject.js";
|
||||
|
||||
const dirs: string[] = [];
|
||||
const temp = () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-abs-"));
|
||||
dirs.push(dir);
|
||||
return dir;
|
||||
};
|
||||
afterEach(() => dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true })));
|
||||
|
||||
/** A project whose index.html is `html(external, project)`, beside a folder holding song.mp3. */
|
||||
function stage(html: (external: string, project: string) => string) {
|
||||
const project = temp();
|
||||
const external = temp();
|
||||
writeFileSync(join(external, "song.mp3"), "audio");
|
||||
mkdirSync(join(project, "media"));
|
||||
writeFileSync(join(project, "media", "inside.mp3"), "audio");
|
||||
writeFileSync(join(project, "index.html"), html(external, project));
|
||||
const files = stagePublishFiles(project, Infinity);
|
||||
const index = files.get("index.html")!.toString("utf-8");
|
||||
return { files, index, external };
|
||||
}
|
||||
|
||||
describe("a media file referenced by its filesystem path", () => {
|
||||
it("outside the project is archived under _ext and the reference points at it", () => {
|
||||
const { files, index, external } = stage(
|
||||
(ext) => `<html><audio id="music" src="${join(ext, "song.mp3")}"></audio></html>`,
|
||||
);
|
||||
expect([...files.keys()].filter((key) => key.startsWith("_ext/"))).toHaveLength(1);
|
||||
expect(index).toMatch(/src="_ext\/[^"]*song\.mp3"/);
|
||||
expect(index).not.toContain(external);
|
||||
});
|
||||
|
||||
it("inside the project is rewritten to its path in the archive", () => {
|
||||
const { files, index } = stage(
|
||||
(_ext, project) =>
|
||||
`<html><audio id="music" src="${join(project, "media/inside.mp3")}"></audio></html>`,
|
||||
);
|
||||
expect(files.has("media/inside.mp3")).toBe(true);
|
||||
expect(index).toContain('src="media/inside.mp3"');
|
||||
});
|
||||
|
||||
it("keeps its #fragment and ?query after the archive path", () => {
|
||||
const inside = stage(
|
||||
(_ext, project) =>
|
||||
`<html><audio id="a" src="${join(project, "media/inside.mp3")}#t=1"></audio></html>`,
|
||||
);
|
||||
expect(inside.index).toContain('src="media/inside.mp3#t=1"');
|
||||
const outside = stage(
|
||||
(ext) => `<html><audio id="a" src="${join(ext, "song.mp3")}?v=2"></audio></html>`,
|
||||
);
|
||||
expect(outside.index).toMatch(/src="_ext\/[^"]*song\.mp3\?v=2"/);
|
||||
});
|
||||
|
||||
it("that does not exist is left as written, like any root-relative URL", () => {
|
||||
const { index } = stage(() => `<html><img src="/assets/none.png"></html>`);
|
||||
expect(index).toContain('src="/assets/none.png"');
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { envProxyEnabled } from "@hyperframes/core/env-proxy";
|
||||
import { basename, dirname, join, posix, relative, resolve } from "node:path";
|
||||
import { basename, dirname, isAbsolute, join, posix, relative, resolve } from "node:path";
|
||||
import {
|
||||
closeSync,
|
||||
existsSync,
|
||||
@@ -440,25 +440,43 @@ function includeReferencedProjectFile(
|
||||
/** References with no fallback: a missing file here cannot render. */
|
||||
const HARD_REFERENCES = new Set(["video[src]", "audio[src]", "img[src]", "video[poster]"]);
|
||||
|
||||
const relativeToProject = (ctx: ExternalAssetContext, absPath: string) =>
|
||||
encodeUrlPath(relative(ctx.absProjectDir, absPath).replaceAll("\\", "/"));
|
||||
|
||||
function archiveTargetOf(
|
||||
ctx: ExternalAssetContext,
|
||||
absPath: string,
|
||||
rawPath: string,
|
||||
options: { requireOnDisk: boolean; filesystemPath: boolean },
|
||||
): string | null | undefined {
|
||||
if (isPathInside(absPath, ctx.absProjectDir)) {
|
||||
includeReferencedProjectFile(ctx, absPath, rawPath, options.requireOnDisk);
|
||||
return options.filesystemPath ? relativeToProject(ctx, absPath) : null;
|
||||
}
|
||||
try {
|
||||
if (!existsSync(absPath) || !statSync(absPath).isFile()) return undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return encodeUrlPath(addExternalAsset(ctx, absPath));
|
||||
}
|
||||
|
||||
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)) {
|
||||
includeReferencedProjectFile(ctx, absPath, rawPath, requireOnDisk);
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
if (!existsSync(absPath) || !statSync(absPath).isFile()) continue;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
return encodeUrlPath(addExternalAsset(ctx, absPath));
|
||||
const filesystemPath = isAbsolute(rawPath) && !rawPath.startsWith("//");
|
||||
if (isNonRelativeUrl(rawPath) && !filesystemPath) return null;
|
||||
const cut = filesystemPath ? rawPath.search(/[?#]/) : -1;
|
||||
const [path, suffix] = cut < 0 ? [rawPath, ""] : [rawPath.slice(0, cut), rawPath.slice(cut)];
|
||||
for (const variant of decodeUrlPathVariants(path)) {
|
||||
const target = archiveTargetOf(ctx, resolve(referrerAbsDir, variant), rawPath, {
|
||||
requireOnDisk,
|
||||
filesystemPath,
|
||||
});
|
||||
if (target !== undefined) return target && target + suffix;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user