mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
fix(engine): read ffconcat lists only as ffmpeg reads them (#8714)
* fix(engine): read ffconcat lists only as ffmpeg reads them The playlist folder check now refuses an ffconcat list with a lone CR or NUL, a file value in any form but a bare name or one fully single-quoted name, text after the name, or an option line. A nested entry with no name that exists no longer passes. Applies to project and downloaded media. * test(engine): reach the ffconcat lone CR, NUL and backslash guards The lone CR and NUL cases now sit after a comment line, so the trailing-text rule no longer refuses them first, and a bare name with a backslash is refused even when a file by that name exists. * test(engine): keep the walk's entry budget test fast; fit the engine budget The under-budget half now writes 100 files instead of 1000, since the walk opens every entry and the test timed out under the flake gate's load. The engine's packed budget rises by 360 bytes for the stricter ffconcat reader. * chore(engine): leave headroom in the engine budget after main's engine changes * test(cli): write the segment a kept snapshot playlist names
This commit is contained in:
@@ -188,6 +188,7 @@ describe("projectVideoFile", () => {
|
||||
writeFileSync(join(dir, "escape.m3u8"), list("../outside/secret.mp4"));
|
||||
writeFileSync(join(dir, "escape.mp4"), list(join(root, "outside", "secret.mp4")));
|
||||
writeFileSync(join(dir, "stream.m3u8"), list("seg0.ts"));
|
||||
writeFileSync(join(dir, "seg0.ts"), "segment");
|
||||
afterAll(() => rmSync(root, { recursive: true, force: true }));
|
||||
beforeEach(() => {
|
||||
run.mockReset();
|
||||
|
||||
@@ -29,6 +29,15 @@ function writeTree(files: Record<string, string>): string {
|
||||
return join(dir, Object.keys(files)[0]!);
|
||||
}
|
||||
|
||||
/** The list at `list`, with a media file at each name beside it. */
|
||||
function withBeside(list: string, ...names: string[]): string {
|
||||
for (const name of names) {
|
||||
mkdirSync(join(list, "..", name, ".."), { recursive: true });
|
||||
writeFileSync(join(list, "..", name), "media");
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
describe("isNotMediaPayload", () => {
|
||||
it.each([
|
||||
["doctype", "<!DOCTYPE html>\n<html><body></body></html>"],
|
||||
@@ -324,12 +333,12 @@ describe("playlistLeavesItsFolder", () => {
|
||||
expect(await isNotMediaPayload(file)).toBe(true);
|
||||
});
|
||||
|
||||
it("refuses a list whose nested entry is a manifest, and keeps one that is not there", async () => {
|
||||
it("refuses a list whose nested entry is a manifest or is not there", async () => {
|
||||
const list = "ffconcat version 1.0\nfile manifest.mpd\n";
|
||||
expect(
|
||||
await playlistLeavesItsFolder(writeTree({ "clip.mp4": list, "manifest.mpd": MPD })),
|
||||
).toBe(true);
|
||||
expect(await playlistLeavesItsFolder(writeTree({ "clip.mp4": list }))).toBe(false);
|
||||
expect(await playlistLeavesItsFolder(writeTree({ "clip.mp4": list }))).toBe(true);
|
||||
const encrypted = hls('#EXT-X-KEY:METHOD=AES-128,URI="key.bin"', "#EXTINF:1,", "seg.ts");
|
||||
expect(
|
||||
await playlistLeavesItsFolder(
|
||||
@@ -380,12 +389,17 @@ describe("playlistLeavesItsFolder", () => {
|
||||
});
|
||||
|
||||
it.each([
|
||||
["segments beside it", hls("#EXTINF:1,", "seg0.ts", "#EXTINF:1,", "parts/seg1.ts")],
|
||||
["https segments", hls("#EXTINF:1,", "https://cdn.example.com/seg0.ts")],
|
||||
["a concat entry beside it", concat("'part 1.mp4'")],
|
||||
["a media file", Buffer.from([0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70])],
|
||||
])("keeps %s", async (_name, contents) => {
|
||||
expect(await playlistLeavesItsFolder(writeFixture("clip.m3u8", contents))).toBe(false);
|
||||
[
|
||||
"segments beside it",
|
||||
hls("#EXTINF:1,", "seg0.ts", "#EXTINF:1,", "parts/seg1.ts"),
|
||||
["seg0.ts", "parts/seg1.ts"],
|
||||
],
|
||||
["https segments", hls("#EXTINF:1,", "https://cdn.example.com/seg0.ts"), []],
|
||||
["a concat entry beside it", concat("'part 1.mp4'"), ["part 1.mp4"]],
|
||||
["a media file", Buffer.from([0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70]), []],
|
||||
])("keeps %s", async (_name, contents, beside) => {
|
||||
const list = withBeside(writeFixture("clip.m3u8", contents), ...beside);
|
||||
expect(await playlistLeavesItsFolder(list)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -409,8 +423,9 @@ describe("playlistLeavesItsFolder takes time linear in the list, and bounds the
|
||||
});
|
||||
|
||||
it("reads a line as long as ffmpeg reads, and refuses one byte longer", async () => {
|
||||
// 4095 bytes is ffmpeg's line limit; neither name exists, so only the length decides.
|
||||
const atLimit = writeFixture("clip.m3u8", segments(`${"a".repeat(4092)}.ts`));
|
||||
// 4095 bytes is ffmpeg's line limit. A name is at most 255 bytes, so the line walks into folders that exist.
|
||||
const name = `${"./".repeat(2044)}abcd.ts`;
|
||||
const atLimit = withBeside(writeFixture("clip.m3u8", segments(name)), "abcd.ts");
|
||||
expect(await playlistLeavesItsFolder(atLimit)).toBe(false);
|
||||
const over = writeFixture("clip.m3u8", segments(`${"a".repeat(4093)}.ts`));
|
||||
expect(await playlistLeavesItsFolder(over)).toBe(true);
|
||||
@@ -421,21 +436,12 @@ describe("playlistLeavesItsFolder takes time linear in the list, and bounds the
|
||||
expect(await playlistLeavesItsFolder(concatOver)).toBe(true);
|
||||
});
|
||||
|
||||
it("reads an ffconcat file line as before: blanks and carriage returns between file and the name", async () => {
|
||||
const list = (line: string) => writeFixture("clip.ffconcat", `ffconcat version 1.0\n${line}\n`);
|
||||
expect(await playlistLeavesItsFolder(list(" file \r\t../other/out.mp4"))).toBe(true);
|
||||
expect(await playlistLeavesItsFolder(list("file\t'../other/out.mp4' "))).toBe(true);
|
||||
expect(await playlistLeavesItsFolder(list("file'../other/out.mp4'"))).toBe(false);
|
||||
expect(await playlistLeavesItsFolder(list("files ../other/out.mp4"))).toBe(false);
|
||||
expect(await playlistLeavesItsFolder(list("file part.mp4"))).toBe(false);
|
||||
});
|
||||
|
||||
it("refuses a walk past its entry budget, before it opens any entry", async () => {
|
||||
const names = Array.from({ length: 50_001 }, (_, i) => `s${i}.ts`);
|
||||
expect(await playlistLeavesItsFolder(writeFixture("clip.m3u8", segments(...names)))).toBe(true);
|
||||
expect(
|
||||
await playlistLeavesItsFolder(writeFixture("clip.m3u8", segments(...names.slice(0, 1000)))),
|
||||
).toBe(false);
|
||||
const under = names.slice(0, 100);
|
||||
const list = withBeside(writeFixture("clip.m3u8", segments(...under)), ...under);
|
||||
expect(await playlistLeavesItsFolder(list)).toBe(false);
|
||||
});
|
||||
|
||||
it("refuses a walk past its byte budget across nested lists", async () => {
|
||||
@@ -447,6 +453,7 @@ describe("playlistLeavesItsFolder takes time linear in the list, and bounds the
|
||||
["a", "b", "c"].map((n) => `#EXT-X-STREAM-INF:BANDWIDTH=1\n${n}.m3u8\n`).join(""),
|
||||
};
|
||||
for (const n of ["a", "b", "c"]) files[`${n}.m3u8`] = `#EXTM3U\n${padding}#EXTINF:1,\nseg.ts\n`;
|
||||
files["seg.ts"] = "segment";
|
||||
expect(await playlistLeavesItsFolder(writeTree(files))).toBe(true);
|
||||
delete files["c.m3u8"];
|
||||
files["master.m3u8"] =
|
||||
@@ -455,6 +462,60 @@ describe("playlistLeavesItsFolder takes time linear in the list, and bounds the
|
||||
});
|
||||
});
|
||||
|
||||
describe("playlistLeavesItsFolder reads an ffconcat list only as ffmpeg reads it", () => {
|
||||
const list = (body: string, ...beside: string[]) =>
|
||||
withBeside(writeFixture("clip.ffconcat", `ffconcat version 1.0\n${body}`), ...beside);
|
||||
|
||||
it.each([
|
||||
["a file line after a comment and a lone carriage return", "#\rfile ../other/out.mp4\n"],
|
||||
["a file line after a comment and a NUL", "#\0file ../other/out.mp4\n"],
|
||||
["a carriage return between file and the name", " file \r\t../other/out.mp4\n"],
|
||||
["a quote inside a bare name", "file part'.mp4' x\n"],
|
||||
["a quoted run followed by more name", "file 'part'.mp4\n"],
|
||||
["a backslash escape", "file part\\.mp4\n"],
|
||||
["text after the name", "file part.mp4 ../other/out.mp4\n"],
|
||||
["an empty name", "file ''\n"],
|
||||
["an option line", "file part.mp4\noption safe 0\n"],
|
||||
["an option line before any file", "option protocol_whitelist file\nfile part.mp4\n"],
|
||||
["a name that is not there", "file gone.mp4\n"],
|
||||
])("refuses %s", async (_name, body) => {
|
||||
const file = list(body, "part.mp4");
|
||||
expect(await playlistLeavesItsFolder(file)).toBe(true);
|
||||
expect(await isNotMediaPayload(file)).toBe(true);
|
||||
});
|
||||
|
||||
it("refuses a name outside the folder written in each form", async () => {
|
||||
expect(await playlistLeavesItsFolder(list("file\t'../other/out.mp4' \n"))).toBe(true);
|
||||
expect(await playlistLeavesItsFolder(list("file ../other/out.mp4\r\n"))).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["a bare name", "file part.mp4\n"],
|
||||
["a quoted name with a space", "file 'part 1.mp4'\n"],
|
||||
["tabs and blanks around the name", " file\tpart.mp4 \t\n"],
|
||||
["CR LF line ends", "file part.mp4\r\nduration 1\r\nfile 'part 1.mp4'\r\n"],
|
||||
["comments and other directives", "# file ../other/out.mp4\nfile part.mp4\ninpoint 0.5\n"],
|
||||
["lines whose keyword is not file", "file'../other/out.mp4'\nfiles ../other/out.mp4\n"],
|
||||
])("keeps %s", async (_name, body) => {
|
||||
expect(await playlistLeavesItsFolder(list(body, "part.mp4", "part 1.mp4"))).toBe(false);
|
||||
});
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"refuses a backslash in a bare name even when that name is there",
|
||||
async () => {
|
||||
expect(await playlistLeavesItsFolder(list("file a\\b.mp4\n", "a\\b.mp4"))).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"keeps a backslash inside quotes as written, as ffmpeg does",
|
||||
async () => {
|
||||
expect(await playlistLeavesItsFolder(list("file 'a\\b.mp4'\n", "a\\b.mp4"))).toBe(false);
|
||||
expect(await playlistLeavesItsFolder(list("file 'a\\b.mp4'\n", "ab.mp4"))).toBe(true);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("playlistLeavesItsFolder reads entries and folders as ffmpeg resolves them", () => {
|
||||
const hls = (...lines: string[]) => ["#EXTM3U", ...lines, "#EXT-X-ENDLIST"].join("\n");
|
||||
const listIn = (folder: string, contents: string | Buffer, name = "list.m3u8"): string => {
|
||||
@@ -500,7 +561,8 @@ describe("playlistLeavesItsFolder reads entries and folders as ffmpeg resolves t
|
||||
|
||||
it("keeps entries beside the list, https entries, and a media file in a folder with # in its name", async () => {
|
||||
const beside = hls("seg.ts", "sub/seg.ts", "seg#1.ts", "https://cdn.example.com/seg.ts");
|
||||
expect(await playlistLeavesItsFolder(writeFixture("clip.m3u8", beside))).toBe(false);
|
||||
const list = withBeside(writeFixture("clip.m3u8", beside), "seg.ts", "sub/seg.ts", "seg#1.ts");
|
||||
expect(await playlistLeavesItsFolder(list)).toBe(false);
|
||||
const httpsOnly = listIn("sub#x", hls("https://cdn.example.com/a.ts"));
|
||||
expect(await playlistLeavesItsFolder(httpsOnly)).toBe(false);
|
||||
const mp4 = Buffer.from([0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, ...Array(24).fill(0)]);
|
||||
@@ -510,7 +572,9 @@ describe("playlistLeavesItsFolder reads entries and folders as ffmpeg resolves t
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"keeps a list in a folder with : in its name",
|
||||
async () => {
|
||||
expect(await playlistLeavesItsFolder(listIn("c:d", hls("seg.ts")))).toBe(false);
|
||||
expect(
|
||||
await playlistLeavesItsFolder(withBeside(listIn("c:d", hls("seg.ts")), "seg.ts")),
|
||||
).toBe(false);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -588,12 +652,14 @@ describe("playlistLeavesItsFolder reads a list as ffprobe does", () => {
|
||||
expect(probe.stderr).toMatch(/outside\/secret\.mp4' for reading/);
|
||||
});
|
||||
|
||||
it("refuses a nested name it cannot read, and keeps one that does not exist", async () => {
|
||||
it("refuses a nested name it cannot read, and one that is not there", async () => {
|
||||
const list = (entry: string) => `${head}#EXTINF:1,\n${entry}\n#EXT-X-ENDLIST\n`;
|
||||
const unreadable = writeTree({ "clip.m3u8": list("sub"), "sub/seg.ts": "segment" });
|
||||
expect(await playlistLeavesItsFolder(unreadable)).toBe(true);
|
||||
const missing = writeTree({ "clip.m3u8": list("gone.ts") });
|
||||
expect(await playlistLeavesItsFolder(missing)).toBe(false);
|
||||
expect(await playlistLeavesItsFolder(missing)).toBe(true);
|
||||
const missingFolder = writeTree({ "clip.m3u8": list("gone/seg.ts") });
|
||||
expect(await playlistLeavesItsFolder(missingFolder)).toBe(true);
|
||||
});
|
||||
|
||||
it.skipIf(process.platform !== "linux")(
|
||||
@@ -618,7 +684,8 @@ describe("playlistLeavesItsFolder reads a list as ffprobe does", () => {
|
||||
|
||||
it("keeps a list with the same tags naming files beside it", async () => {
|
||||
const list = `${head}#EXT-X-MAP:URI=init.mp4\r#EXT-X-KEY:METHOD=AES-128,URI="k\\ey.bin"\r${tail}`;
|
||||
expect(await playlistLeavesItsFolder(writeFixture("clip.m3u8", list))).toBe(false);
|
||||
const file = withBeside(writeFixture("clip.m3u8", list), "init.mp4", "key.bin", "seg.ts");
|
||||
expect(await playlistLeavesItsFolder(file)).toBe(false);
|
||||
});
|
||||
|
||||
it("refuses a file behind more ID3 tags than it skips", async () => {
|
||||
|
||||
@@ -323,7 +323,6 @@ const FOLDER_CUT_AT_QUERY_OR_FRAGMENT = /[?#]/;
|
||||
// ffmpeg reads a list line into a 4096-byte buffer, so a longer line would name something else: it is refused unread.
|
||||
const MAX_LIST_LINE_BYTES = 4095;
|
||||
const ATTRIBUTE_GAP = /[\t\n\v\f\r ,]/;
|
||||
const SPACE = /\s/;
|
||||
const TRAILING_HLS_BLANK = /[\t\v\f ]/;
|
||||
|
||||
function withoutTrailing(line: string, isBlank: RegExp): string {
|
||||
@@ -332,32 +331,53 @@ function withoutTrailing(line: string, isBlank: RegExp): string {
|
||||
return line.slice(0, end);
|
||||
}
|
||||
|
||||
/** The names one line can open: ffmpeg opens one, the walk checks all, and refuses the list when none is there. */
|
||||
type Entry = string[];
|
||||
|
||||
/**
|
||||
* Every name an HLS list can open, read as ffmpeg's hls demuxer reads it; true when a line is too long to read
|
||||
* Every entry an HLS list can open, read as ffmpeg's hls demuxer reads it; true when a line is too long to read
|
||||
* whole. The text is latin1, one character per byte, so a line's length is its length in bytes.
|
||||
*/
|
||||
function hlsEntries(body: string): string[] | true {
|
||||
const names = new Set<string>();
|
||||
function hlsEntries(body: string): Entry[] | true {
|
||||
const entries = new Map<string, Entry>();
|
||||
for (const raw of body.replaceAll("\0", "\n").split(/[\r\n]/)) {
|
||||
if (raw.length > MAX_LIST_LINE_BYTES) return true;
|
||||
const line = withoutTrailing(raw, TRAILING_HLS_BLANK);
|
||||
const found = line.startsWith("#") ? attributeUris(line) : [line, line.trim()];
|
||||
for (const name of found) if (name) names.add(name);
|
||||
const found = line.startsWith("#")
|
||||
? attributeUris(line).map((uri) => [uri])
|
||||
: [[line, line.trim()]];
|
||||
for (const names of found.map((entry) => [...new Set(entry.filter(Boolean))])) {
|
||||
if (names.length > 0) entries.set(names.join("\n"), names);
|
||||
}
|
||||
}
|
||||
return [...names];
|
||||
return [...entries.values()];
|
||||
}
|
||||
|
||||
/** The name an ffconcat `file` line gives, as `/^\s*file\s+(.*)$/` would match it, in one pass; undefined for any other line. */
|
||||
function concatFileName(raw: string): string | undefined {
|
||||
let at = 0;
|
||||
while (at < raw.length && SPACE.test(raw[at]!)) at++;
|
||||
if (!raw.startsWith("file", at)) return undefined;
|
||||
let rest = at + 4;
|
||||
while (rest < raw.length && SPACE.test(raw[rest]!)) rest++;
|
||||
// `\s+` needs one blank, and `.` matches no carriage return, so one left past the blanks fails the match.
|
||||
if (rest === at + 4 || raw.includes("\r", rest)) return undefined;
|
||||
const value = raw.slice(rest).trim();
|
||||
return value.startsWith("'") ? value.slice(1, -1).replace(/\\(.)/g, "$1") : value;
|
||||
// ffmpeg's concat demuxer ends a line at LF, CR or NUL; only LF and CR LF end one here, so any other CR or NUL refuses.
|
||||
const LONE_CR = /\r(?!\n)/;
|
||||
const CONCAT_KEYWORD = /^[\t ]*([^\t ]*)[\t ]*/;
|
||||
// The `file` values `av_get_token` reads as written: a bare run with no quote or backslash, or one single-quoted run.
|
||||
const CONCAT_FILE_VALUE = /^(?:'([^']+)'|([^\t '\\]+))[\t ]*$/;
|
||||
|
||||
/** The name an ffconcat line opens; undefined for a line that opens none; true for one ffmpeg may read otherwise. */
|
||||
function concatFileName(line: string): string | true | undefined {
|
||||
const keyword = CONCAT_KEYWORD.exec(line)!;
|
||||
if (keyword[1] === "option") return true;
|
||||
if (keyword[1] !== "file") return undefined;
|
||||
const value = CONCAT_FILE_VALUE.exec(line.slice(keyword[0].length));
|
||||
return value ? (value[1] ?? value[2]!) : true;
|
||||
}
|
||||
|
||||
function concatEntries(body: string): Entry[] | true {
|
||||
if (LONE_CR.test(body) || body.includes("\0")) return true;
|
||||
const entries: Entry[] = [];
|
||||
for (const line of body.split(/\r?\n/)) {
|
||||
if (line.length > MAX_LIST_LINE_BYTES) return true;
|
||||
const name = concatFileName(line);
|
||||
if (name === true) return true;
|
||||
if (name !== undefined) entries.push([name]);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/** `URI=` values of a tag, split as ffmpeg's `ff_parse_key_value` splits them. */
|
||||
@@ -396,19 +416,11 @@ function attributeValue(line: string, at: number): [string, number] {
|
||||
|
||||
const UTF8_BOM = "\xEF\xBB\xBF";
|
||||
|
||||
/** The names a list can open; true when a line is too long to read whole; undefined when the text is no list. */
|
||||
function playlistEntries(text: string): string[] | true | undefined {
|
||||
/** The entries a list can open; true when it must be refused unread; undefined when the text is no list. */
|
||||
function playlistEntries(text: string): Entry[] | true | undefined {
|
||||
const body = (text.startsWith(UTF8_BOM) ? text.slice(UTF8_BOM.length) : text).trimStart();
|
||||
if (body.startsWith("#EXTM3U")) return hlsEntries(body);
|
||||
if (/^ffconcat\s+version/i.test(body)) {
|
||||
const names: string[] = [];
|
||||
for (const raw of body.split(/\r?\n/)) {
|
||||
if (raw.length > MAX_LIST_LINE_BYTES) return true;
|
||||
const name = concatFileName(raw);
|
||||
if (name !== undefined) names.push(name);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
if (/^ffconcat\s+version/i.test(body)) return concatEntries(body);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -435,10 +447,14 @@ function percentDecoded(value: string): string {
|
||||
/** Paths are walked as latin1 strings, one character per byte, and opened by those bytes. */
|
||||
const asBytes = (path: string): string => Buffer.from(path).toString("latin1");
|
||||
|
||||
/** A nested name that is not there: ffmpeg would open nothing by it, but the walk may have computed the wrong name. */
|
||||
const MISSING = Symbol("missing");
|
||||
type Verdict = boolean | typeof MISSING;
|
||||
|
||||
interface Walk {
|
||||
folder: string;
|
||||
seen: Set<string>;
|
||||
verdicts: Map<string, boolean>;
|
||||
verdicts: Map<string, Verdict>;
|
||||
bytes: number;
|
||||
entries: number;
|
||||
}
|
||||
@@ -460,7 +476,7 @@ export async function playlistLeavesItsFolder(
|
||||
entries: 0,
|
||||
};
|
||||
try {
|
||||
return await listLeaves(asBytes(resolve(filePath)), placed, walk, 0);
|
||||
return (await listLeaves(asBytes(resolve(filePath)), placed, walk, 0)) === true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
@@ -529,7 +545,7 @@ const isMissing = (error: unknown): boolean =>
|
||||
);
|
||||
|
||||
/** A list's entries, counted against the walk's budget; true when the list or the walk must be refused. */
|
||||
function budgetedEntries(text: string, walk: Walk): string[] | true {
|
||||
function budgetedEntries(text: string, walk: Walk): Entry[] | true {
|
||||
walk.bytes += text.length;
|
||||
const entries = playlistEntries(text) ?? [];
|
||||
if (entries === true) return true;
|
||||
@@ -537,17 +553,18 @@ function budgetedEntries(text: string, walk: Walk): string[] | true {
|
||||
return walk.bytes > MAX_WALK_BYTES || walk.entries > MAX_WALK_ENTRIES ? true : entries;
|
||||
}
|
||||
|
||||
/** Every list a list names is read too, against the top list's folder. A nested file that exists but cannot be read is refused. */
|
||||
/** Every list a list names is read too, against the top list's folder. A nested file that cannot be read is refused. */
|
||||
async function listLeaves(
|
||||
readPath: string,
|
||||
path: string,
|
||||
walk: Walk,
|
||||
depth: number,
|
||||
): Promise<boolean> {
|
||||
): Promise<Verdict> {
|
||||
const text = await readList(readPath, depth > 0).catch((error: unknown) =>
|
||||
depth > 0 && !isMissing(error) ? true : null,
|
||||
depth === 0 ? null : isMissing(error) ? MISSING : true,
|
||||
);
|
||||
if (text === null) return false;
|
||||
if (text === MISSING) return MISSING;
|
||||
if (text === true || depth >= MAX_PLAYLIST_DEPTH) return true;
|
||||
if (walk.seen.has(path)) return false;
|
||||
walk.seen.add(path);
|
||||
@@ -560,22 +577,29 @@ async function listLeaves(
|
||||
}
|
||||
|
||||
async function entryLeaves(
|
||||
entry: string,
|
||||
entry: Entry,
|
||||
base: string,
|
||||
walk: Walk,
|
||||
depth: number,
|
||||
): Promise<boolean> {
|
||||
if (entryLeavesFolder(entry, base, walk.folder)) return true;
|
||||
if (HTTP_URL.test(entry)) return false;
|
||||
for (const local of new Set([entry, percentDecoded(entry)])) {
|
||||
const nested = resolve(base, local);
|
||||
if (isPrivateFileUnder(walk.folder, nested, isStudioPrivatePath)) return true;
|
||||
let verdict = walk.verdicts.get(nested);
|
||||
if (verdict === undefined) {
|
||||
verdict = await listLeaves(nested, nested, walk, depth + 1);
|
||||
walk.verdicts.set(nested, verdict);
|
||||
}
|
||||
if (verdict) return true;
|
||||
if (entry.some((name) => entryLeavesFolder(name, base, walk.folder))) return true;
|
||||
const local = entry.filter((name) => !HTTP_URL.test(name));
|
||||
let found = local.length === 0;
|
||||
for (const nested of new Set(local.flatMap((name) => [name, percentDecoded(name)]))) {
|
||||
const path = resolve(base, nested);
|
||||
if (isPrivateFileUnder(walk.folder, path, isStudioPrivatePath)) return true;
|
||||
const verdict = await nestedVerdict(path, walk, depth);
|
||||
if (verdict === true) return true;
|
||||
found ||= verdict === false;
|
||||
}
|
||||
return false;
|
||||
return !found;
|
||||
}
|
||||
|
||||
async function nestedVerdict(path: string, walk: Walk, depth: number): Promise<Verdict> {
|
||||
let verdict = walk.verdicts.get(path);
|
||||
if (verdict === undefined) {
|
||||
verdict = await listLeaves(path, path, walk, depth + 1);
|
||||
walk.verdicts.set(path, verdict);
|
||||
}
|
||||
return verdict;
|
||||
}
|
||||
|
||||
@@ -925,8 +925,27 @@ describe("downloadToTemp atomic publication and bounded retry", () => {
|
||||
expect(temporaryDownloadEntries(dir)).toEqual([]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["names a segment not beside it", "#EXTM3U\n#EXTINF:1,\ngone.ts\n"],
|
||||
["carries an option line", "ffconcat version 1.0\nfile seg0.ts\noption safe 0\n"],
|
||||
[
|
||||
"hides a line behind a lone carriage return",
|
||||
"ffconcat version 1.0\nfile seg0.ts\rfile /x.mp4\n",
|
||||
],
|
||||
])("never publishes a downloaded playlist that %s", async (_name, body) => {
|
||||
const dir = makeTempDir();
|
||||
writeFileSync(join(dir, "seg0.ts"), "segment");
|
||||
stubFetch(vi.fn().mockResolvedValue(new Response(body)));
|
||||
|
||||
await expect(downloadToTemp("https://cdn.example/clip.m3u8", dir, 1_000)).rejects.toMatchObject(
|
||||
{ kind: "invalid_payload", retryable: false },
|
||||
);
|
||||
expect(readdirSync(dir)).toEqual(["seg0.ts"]);
|
||||
});
|
||||
|
||||
it("publishes a downloaded playlist whose segments sit beside it", async () => {
|
||||
const dir = makeTempDir();
|
||||
writeFileSync(join(dir, "seg0.ts"), "segment");
|
||||
stubFetch(vi.fn().mockResolvedValue(new Response("#EXTM3U\n#EXTINF:1,\nseg0.ts\n")));
|
||||
|
||||
const path = await downloadToTemp("https://cdn.example/clip.m3u8", dir, 1_000);
|
||||
|
||||
@@ -275,6 +275,26 @@ describe("preflightCompositionAssetMediaTypes", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
["an option line", "ffconcat version 1.0\nfile clip.mp4\noption safe 0\n"],
|
||||
["an entry that is not there", "ffconcat version 1.0\nfile gone.mp4\n"],
|
||||
[
|
||||
"a line hidden behind a lone carriage return",
|
||||
"ffconcat version 1.0\nfile a.mp4\rfile b.mp4\n",
|
||||
],
|
||||
])("refuses a project playlist with %s", async (_name, list) => {
|
||||
writeFileSync(join(projectDir, "a.mp4"), "media");
|
||||
writeFileSync(join(projectDir, "b.mp4"), "media");
|
||||
writeFileSync(join(projectDir, "list.ffconcat"), list);
|
||||
await expect(run({ videoSrc: "list.ffconcat" })).rejects.toBeInstanceOf(NotMediaPayloadError);
|
||||
});
|
||||
|
||||
it("keeps a project playlist naming media beside it", async () => {
|
||||
writeFileSync(join(projectDir, "a.mp4"), "media");
|
||||
writeFileSync(join(projectDir, "list.ffconcat"), "ffconcat version 1.0\nfile a.mp4\n");
|
||||
await expect(run({ videoSrc: "list.ffconcat" })).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it.each(["data:x/../streamed-preview.html", "BLOB:a/../streamed-preview.html"])(
|
||||
"sniffs a project file named behind a URL scheme: %s",
|
||||
async (videoSrc) => {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
"unpacked": 4898942
|
||||
},
|
||||
"@hyperframes/engine": {
|
||||
"packed": 511040,
|
||||
"packed": 511900,
|
||||
"unpacked": 1742862
|
||||
},
|
||||
"@hyperframes/gcp-cloud-run": {
|
||||
|
||||
Reference in New Issue
Block a user