66import { execFile } from "node:child_process" ;
77import { createHash } from "node:crypto" ;
88import {
9+ copyFileSync ,
910 createReadStream ,
1011 mkdirSync ,
1112 mkdtempSync ,
@@ -457,11 +458,19 @@ async function stagedSha256(file: StagedFile): Promise<string> {
457458 * is used in place (checked unchanged after use), one staged in memory gets a temp copy.
458459 */
459460function snapshotPath ( path : string , file : StagedFile , tempFiles : string [ ] ) : string {
460- if ( ! Buffer . isBuffer ( file ) ) return file . absPath ;
461+ return Buffer . isBuffer ( file ) ? stagedCopy ( path , file , tempFiles ) : file . absPath ;
462+ }
463+
464+ /** A private copy of the staged bytes, for readers that cache what they read before any check runs. */
465+ function stagedCopy ( path : string , file : StagedFile , tempFiles : string [ ] ) : string {
461466 const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-offload-src-" ) ) ;
462467 tempFiles . push ( dir ) ;
463468 const copy = join ( dir , basename ( path ) ) ;
464- writeFileSync ( copy , file ) ;
469+ if ( Buffer . isBuffer ( file ) ) writeFileSync ( copy , file ) ;
470+ else {
471+ copyFileSync ( file . absPath , copy ) ;
472+ assertUnchanged ( file ) ;
473+ }
465474 return copy ;
466475}
467476
@@ -470,6 +479,21 @@ function textOf(path: string, file: StagedFile | undefined): string | undefined
470479 return TEXT_FILE_EXTS . test ( path ) && Buffer . isBuffer ( file ) ? file . toString ( "utf-8" ) : undefined ;
471480}
472481
482+ /** What the page's scripts can read: inline scripts of HTML files, and script and data files. */
483+ function scriptCode ( fileMap : Map < string , StagedFile > ) : string {
484+ const parts : string [ ] = [ ] ;
485+ for ( const [ path , content ] of fileMap ) {
486+ const text = textOf ( path , content ) ;
487+ if ( text === undefined ) continue ;
488+ if ( / \. h t m l ? $ / i. test ( path ) ) {
489+ for ( const script of parseHTML ( text ) . document . querySelectorAll ( "script:not([src])" ) ) {
490+ parts . push ( script . textContent ?? "" ) ;
491+ }
492+ } else if ( / \. ( m ? j s | c j s | j s o n ) $ / i. test ( path ) ) parts . push ( text ) ;
493+ }
494+ return parts . join ( "\n" ) ;
495+ }
496+
473497function assertFitsAsset ( path : string , filename : string , size : number ) : void {
474498 const maxBytes = ACCEPTED [ extname ( filename ) . toLowerCase ( ) ] ?. maxBytes ?? 0 ;
475499 if ( size > maxBytes ) {
@@ -545,6 +569,7 @@ async function planFileTrim(
545569 ( [ other , content ] ) =>
546570 other !== entryPath && other !== path && ! ! textOf ( other , content ) ?. includes ( name ) ,
547571 ) ,
572+ scriptCode : scriptCode ( ctx . fileMap ) ,
548573 } ) ;
549574 if ( ! plan ) return null ;
550575 ctx . fileMap . set ( entryPath , Buffer . from ( plan . html , "utf-8" ) ) ;
@@ -650,7 +675,7 @@ function entryGifSources(
650675 const key = htmlSrcKey ( ctx . absDir , entryPath , src , ctx . fileMap ) ;
651676 if ( ! key ?. toLowerCase ( ) . endsWith ( ".gif" ) ) continue ;
652677 const file = ctx . fileMap . get ( key ) ! ;
653- sourceAssets . set ( src , snapshotPath ( key , file , ctx . tempFiles ) ) ;
678+ sourceAssets . set ( src , stagedCopy ( key , file , ctx . tempFiles ) ) ;
654679 heavy ||= stagedSize ( file ) >= OFFLOAD_MIN_BYTES ;
655680 }
656681 return heavy ? sourceAssets : null ;
@@ -707,28 +732,36 @@ function dropReplacedFiles(
707732 preparedGifSources : string [ ] ,
708733 assetUrls : string [ ] ,
709734) : void {
735+ // Asset URLs end in names like original.mp4 that can collide with a project file's name.
736+ const withoutUrls = ( text : string ) =>
737+ assetUrls . reduce ( ( acc , url ) => acc . replaceAll ( url , "" ) , text ) ;
738+ const code = withoutUrls ( scriptCode ( fileMap ) ) ;
710739 for ( const item of report ) {
711- if ( isStillMentioned ( fileMap , item . path , assetUrls ) ) item . kept_in_archive = true ;
740+ if ( isStillMentioned ( fileMap , item . path , code , withoutUrls ) ) item . kept_in_archive = true ;
712741 else fileMap . delete ( item . path ) ;
713742 }
714743 for ( const gif of preparedGifSources ) {
715- if ( ! isStillMentioned ( fileMap , gif , assetUrls ) ) fileMap . delete ( gif ) ;
744+ if ( ! isStillMentioned ( fileMap , gif , code , withoutUrls ) ) fileMap . delete ( gif ) ;
716745 }
717746}
718747
719- // A file still named after rewriting (say, built by string concatenation) keeps a zip copy.
748+ /**
749+ * Whether a reference rewriting could not replace may still load the file: its name anywhere, or its
750+ * stem and extension both in script code, which can join them at runtime ("clip" + ".mp4").
751+ */
720752function isStillMentioned (
721753 fileMap : Map < string , StagedFile > ,
722754 path : string ,
723- assetUrls : string [ ] ,
755+ code : string ,
756+ withoutUrls : ( text : string ) => string ,
724757) : boolean {
758+ const ext = extname ( path ) ;
759+ if ( code . includes ( basename ( path , ext ) ) && code . includes ( ext . slice ( 1 ) ) ) return true ;
725760 const name = basename ( path ) ;
726761 for ( const [ entryPath , content ] of fileMap ) {
727762 const contentText = textOf ( entryPath , content ) ;
728763 if ( entryPath === path || ! contentText ?. includes ( name ) ) continue ;
729- // Asset URLs end in names like original.mp4 that can collide with a project file's name.
730- const text = assetUrls . reduce ( ( acc , url ) => acc . replaceAll ( url , "" ) , contentText ) ;
731- if ( text . includes ( name ) ) return true ;
764+ if ( withoutUrls ( contentText ) . includes ( name ) ) return true ;
732765 }
733766 return false ;
734767}
0 commit comments