@@ -11,7 +11,7 @@ import {
1111 writeFileSync ,
1212} from "node:fs" ;
1313import { tmpdir } from "node:os" ;
14- import { dirname , join } from "node:path" ;
14+ import { dirname , extname , join } from "node:path" ;
1515import { buildPublishFileMap , stagePublishFiles } from "../utils/publishProject.js" ;
1616import {
1717 type GifPrep ,
@@ -20,6 +20,7 @@ import {
2020 type Reencoder ,
2121 type UploadableFile ,
2222} from "./offload.js" ;
23+ import { bytesMatchExtension } from "./offloadSniff.js" ;
2324
2425vi . mock ( "node:fs" , async ( importOriginal ) => {
2526 const actual = await importOriginal < typeof import ( "node:fs" ) > ( ) ;
@@ -37,12 +38,30 @@ afterEach(() => {
3738 for ( const dir of dirs . splice ( 0 ) ) rmSync ( dir , { recursive : true , force : true } ) ;
3839} ) ;
3940
41+ // The first bytes a real file of each extension starts with; HEAVY stands in for an honest file.
42+ const HEADERS : Record < string , Buffer > = {
43+ ".mp4" : Buffer . concat ( [ Buffer . from ( [ 0 , 0 , 0 , 24 ] ) , Buffer . from ( "ftypisom" ) ] ) ,
44+ ".mov" : Buffer . concat ( [ Buffer . from ( [ 0 , 0 , 0 , 20 ] ) , Buffer . from ( "ftypqt " ) ] ) ,
45+ ".png" : Buffer . from ( [ 0x89 , 0x50 , 0x4e , 0x47 , 13 , 10 , 26 , 10 ] ) ,
46+ ".mp3" : Buffer . from ( "ID3\u0004\u0000" ) ,
47+ ".webm" : Buffer . from ( [ 0x1a , 0x45 , 0xdf , 0xa3 ] ) ,
48+ } ;
49+
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+
4055function project ( files : Record < string , string | Buffer > ) : string {
4156 const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-offload-test-" ) ) ;
4257 dirs . push ( dir ) ;
4358 for ( const [ path , content ] of Object . entries ( files ) ) {
4459 mkdirSync ( dirname ( join ( dir , path ) ) , { recursive : true } ) ;
45- writeFileSync ( join ( dir , path ) , content ) ;
60+ const header = content === HEAVY ? HEADERS [ extname ( path ) . toLowerCase ( ) ] : undefined ;
61+ writeFileSync (
62+ join ( dir , path ) ,
63+ header ? Buffer . concat ( [ header , HEAVY . subarray ( header . length ) ] ) : content ,
64+ ) ;
4665 }
4766 return dir ;
4867}
@@ -319,6 +338,82 @@ describe("offloadHeavyMedia", () => {
319338 expect ( fileMap . has ( "take.mkv" ) ) . toBe ( false ) ;
320339 } ) ;
321340
341+ it ( "re-encodes an AAC file named .mp3 instead of uploading it as audio/mpeg" , async ( ) => {
342+ const m4a = Buffer . concat ( [
343+ Buffer . from ( [ 0 , 0 , 0 , 28 ] ) ,
344+ Buffer . from ( "ftypM4A " ) ,
345+ Buffer . alloc ( OFFLOAD_MIN_BYTES , 1 ) ,
346+ ] ) ;
347+ const dir = project ( {
348+ "index.html" : html ( `<audio src="song.mp3"></audio>` ) ,
349+ "song.mp3" : m4a ,
350+ } ) ;
351+ const reencoder : Reencoder = {
352+ choose : vi . fn ( async ( ) => "audio" as const ) ,
353+ run : vi . fn ( async ( ) => ( {
354+ bytes : Buffer . from ( "wav" ) ,
355+ filename : "song.wav" ,
356+ contentType : "audio/wav" ,
357+ } ) ) ,
358+ } ;
359+ const upload = fakeUpload ( ) ;
360+
361+ const report = await offloadHeavyMedia ( dir , buildPublishFileMap ( dir ) , {
362+ apiBaseUrl : "https://api.example" ,
363+ upload,
364+ reencoder,
365+ cachePath : cachePath ( ) ,
366+ } ) ;
367+
368+ expect ( report [ 0 ] ) . toMatchObject ( { path : "song.mp3" , action : "reencode" } ) ;
369+ expect ( upload . mock . calls [ 0 ] ! [ 0 ] ) . toMatchObject ( {
370+ filename : "song.wav" ,
371+ contentType : "audio/wav" ,
372+ } ) ;
373+ } ) ;
374+
375+ it ( "treats an audio-only MP4 named .mp4 as audio, whatever brand its ftyp box has" , async ( ) => {
376+ const audioOnly = Buffer . concat ( [
377+ HEADERS [ ".mp4" ] ! ,
378+ Buffer . alloc ( OFFLOAD_MIN_BYTES - HEADERS [ ".mp4" ] ! . length , 1 ) ,
379+ ] ) ;
380+ const dir = project ( {
381+ "index.html" : html ( `<audio src="voice.mp4"></audio>` ) ,
382+ "voice.mp4" : audioOnly ,
383+ } ) ;
384+ const reencoder : Reencoder = {
385+ hasVideo : async ( ) => false ,
386+ choose : vi . fn ( async ( _path , kind ) => ( kind === "audio" ? ( "audio" as const ) : null ) ) ,
387+ run : vi . fn ( async ( ) => ( {
388+ bytes : Buffer . from ( "wav" ) ,
389+ filename : "voice.wav" ,
390+ contentType : "audio/wav" ,
391+ } ) ) ,
392+ } ;
393+ const upload = fakeUpload ( ) ;
394+
395+ const report = await offloadHeavyMedia ( dir , buildPublishFileMap ( dir ) , {
396+ apiBaseUrl : "https://api.example" ,
397+ upload,
398+ reencoder,
399+ cachePath : cachePath ( ) ,
400+ } ) ;
401+
402+ expect ( reencoder . choose ) . toHaveBeenCalledWith ( expect . any ( String ) , "audio" ) ;
403+ expect ( report [ 0 ] ) . toMatchObject ( { path : "voice.mp4" , action : "reencode" } ) ;
404+ expect ( upload . mock . calls [ 0 ] ! [ 0 ] ) . toMatchObject ( { filename : "voice.wav" } ) ;
405+ } ) ;
406+
407+ it ( "only takes bytes as-is when they are the container the extension names" , ( ) => {
408+ const wave = Buffer . from ( "RIFF\0\0\0\0WAVEfmt " ) ;
409+ expect ( bytesMatchExtension ( ".wav" , wave ) ) . toBe ( true ) ;
410+ expect ( bytesMatchExtension ( ".mp3" , wave ) ) . toBe ( false ) ;
411+ expect ( bytesMatchExtension ( ".mp3" , Buffer . from ( [ 0xff , 0xfb , 0x90 , 0x00 ] ) ) ) . toBe ( true ) ;
412+ expect ( bytesMatchExtension ( ".mp4" , HEADERS [ ".mp4" ] ! ) ) . toBe ( true ) ;
413+ expect ( bytesMatchExtension ( ".mp4" , Buffer . from ( "\0\0\0\x1cftypM4A " ) ) ) . toBe ( false ) ;
414+ expect ( bytesMatchExtension ( ".jpg" , HEADERS [ ".png" ] ! ) ) . toBe ( false ) ;
415+ } ) ;
416+
322417 it ( "fails naming the file when a re-encode is still over the asset cap" , async ( ) => {
323418 const dir = project ( {
324419 "index.html" : html ( `<video src="long.mkv"></video>` ) ,
@@ -417,7 +512,7 @@ describe("offloadHeavyMedia", () => {
417512 } ) ;
418513 const gifPrep = vi . fn < GifPrep > ( async ( input , options ) => {
419514 const outputPath = join ( options . outputDir , "hfgif-v1-abc.webm" ) ;
420- writeFileSync ( outputPath , HEAVY ) ;
515+ writeFileSync ( outputPath , honest ( outputPath ) ) ;
421516 return {
422517 html : input . replace (
423518 `<img src="media/loop.gif" data-loop="true" data-duration="4" />` ,
@@ -464,7 +559,7 @@ describe("offloadHeavyMedia", () => {
464559 const gifPrep = vi . fn < GifPrep > ( async ( input , options ) => {
465560 prepared = readFileSync ( options . sourceAssets ! . get ( "logo.gif" ) ! ) ;
466561 const outputPath = join ( options . outputDir , "hfgif-v1-abc.webm" ) ;
467- writeFileSync ( outputPath , HEAVY ) ;
562+ writeFileSync ( outputPath , honest ( outputPath ) ) ;
468563 return {
469564 html : input . replace (
470565 `<img src="logo.gif" />` ,
0 commit comments