Skip to content

Commit f525369

Browse files
committed
test(cli): generated media in the offload tests carries its container header
1 parent 0c69168 commit f525369

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎packages/cli/src/cloud/offload.test.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,24 @@ const HEADERS: Record<string, Buffer> = {
4444
".mov": Buffer.concat([Buffer.from([0, 0, 0, 20]), Buffer.from("ftypqt ")]),
4545
".png": Buffer.from([0x89, 0x50, 0x4e, 0x47, 13, 10, 26, 10]),
4646
".mp3": Buffer.from("ID3\u0004\u0000"),
47+
".webm": Buffer.from([0x1a, 0x45, 0xdf, 0xa3]),
4748
};
4849

50+
function honest(path: string): Buffer {
51+
const header = HEADERS[extname(path).toLowerCase()] ?? Buffer.alloc(0);
52+
return Buffer.concat([header, HEAVY.subarray(header.length)]);
53+
}
54+
4955
function project(files: Record<string, string | Buffer>): string {
5056
const dir = mkdtempSync(join(tmpdir(), "hf-offload-test-"));
5157
dirs.push(dir);
5258
for (const [path, content] of Object.entries(files)) {
5359
mkdirSync(dirname(join(dir, path)), { recursive: true });
5460
const header = content === HEAVY ? HEADERS[extname(path).toLowerCase()] : undefined;
55-
writeFileSync(join(dir, path), header ? Buffer.concat([header, content.subarray(header.length)]) : content);
61+
writeFileSync(
62+
join(dir, path),
63+
header ? Buffer.concat([header, content.subarray(header.length)]) : content,
64+
);
5665
}
5766
return dir;
5867
}
@@ -357,7 +366,10 @@ describe("offloadHeavyMedia", () => {
357366
});
358367

359368
expect(report[0]).toMatchObject({ path: "song.mp3", action: "reencode" });
360-
expect(upload.mock.calls[0]![0]).toMatchObject({ filename: "song.wav", contentType: "audio/wav" });
369+
expect(upload.mock.calls[0]![0]).toMatchObject({
370+
filename: "song.wav",
371+
contentType: "audio/wav",
372+
});
361373
});
362374

363375
it("only takes bytes as-is when they are the container the extension names", () => {
@@ -468,7 +480,7 @@ describe("offloadHeavyMedia", () => {
468480
});
469481
const gifPrep = vi.fn<GifPrep>(async (input, options) => {
470482
const outputPath = join(options.outputDir, "hfgif-v1-abc.webm");
471-
writeFileSync(outputPath, HEAVY);
483+
writeFileSync(outputPath, honest(outputPath));
472484
return {
473485
html: input.replace(
474486
`<img src="media/loop.gif" data-loop="true" data-duration="4" />`,
@@ -515,7 +527,7 @@ describe("offloadHeavyMedia", () => {
515527
const gifPrep = vi.fn<GifPrep>(async (input, options) => {
516528
prepared = readFileSync(options.sourceAssets!.get("logo.gif")!);
517529
const outputPath = join(options.outputDir, "hfgif-v1-abc.webm");
518-
writeFileSync(outputPath, HEAVY);
530+
writeFileSync(outputPath, honest(outputPath));
519531
return {
520532
html: input.replace(
521533
`<img src="logo.gif" />`,

‎packages/cli/src/cloud/offloadSniff.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Whether a file's first bytes are the container its extension claims, for the types the asset API takes as-is. */
22
export function bytesMatchExtension(ext: string, head: Buffer): boolean {
3-
const at = (offset: number, text: string) => head.toString("latin1", offset, offset + text.length) === text;
3+
const at = (offset: number, text: string) =>
4+
head.toString("latin1", offset, offset + text.length) === text;
45
switch (ext) {
56
case ".mp3":
67
return at(0, "ID3") || (head[0] === 0xff && ((head[1] ?? 0) & 0xe6) === 0xe2);

0 commit comments

Comments
 (0)