1+ import { randomFillSync } from 'crypto' ;
12import JSZip from 'jszip' ;
23import { cleanFile } from '../clean' ;
34
5+ function readBytes ( blob ) {
6+ return new Promise ( ( resolve , reject ) => {
7+ const reader = new FileReader ( ) ;
8+ reader . onload = ( ) => resolve ( new Uint8Array ( reader . result ) ) ;
9+ reader . onerror = reject ;
10+ reader . readAsArrayBuffer ( blob ) ;
11+ } ) ;
12+ }
13+
414describe ( 'cleanFile' , ( ) => {
515 let mockZip ;
616 let originalCreateElement ;
@@ -19,10 +29,14 @@ describe('cleanFile', () => {
1929
2030 // Helper function to create a zip file with given contents
2131 async function createTestZip ( files , options = { } ) {
32+ const zip = new JSZip ( ) ;
2233 for ( const [ path , content ] of Object . entries ( files ) ) {
23- mockZip . file ( path , content ) ;
34+ zip . file ( path , content , { date : options . date } ) ;
2435 }
25- const zipContent = await mockZip . generateAsync ( { type : 'blob' } ) ;
36+ const zipContent = await zip . generateAsync ( {
37+ type : 'blob' ,
38+ compression : options . compression || 'STORE' ,
39+ } ) ;
2640 return new File ( [ zipContent ] , options . filename || 'test.zip' , {
2741 type : 'application/zip' ,
2842 lastModified : options . lastModified || Date . now ( ) ,
@@ -85,6 +99,61 @@ describe('cleanFile', () => {
8599 expect ( cleanedPaths ) . toContain ( 'index.html' ) ;
86100 expect ( cleanedPaths ) . toContain ( 'assets/style.css' ) ;
87101 } ) ;
102+
103+ it ( 'should preserve file contents' , async ( ) => {
104+ const binary = new Uint8Array ( [ 0x00 , 0xff , 0x10 , 0x80 , 0x7f ] ) ;
105+ const file = await createTestZip ( {
106+ 'dist/index.html' : '<html>ünïcödé</html>' ,
107+ 'dist/model/weights.bin' : binary ,
108+ } ) ;
109+ const cleanedZip = await JSZip . loadAsync ( await cleanFile ( file ) ) ;
110+
111+ expect ( await cleanedZip . file ( 'index.html' ) . async ( 'string' ) ) . toBe ( '<html>ünïcödé</html>' ) ;
112+ expect ( await cleanedZip . file ( 'model/weights.bin' ) . async ( 'uint8array' ) ) . toEqual ( binary ) ;
113+ } ) ;
114+
115+ it ( 'should produce identical bytes for the same files zipped differently' , async ( ) => {
116+ const files = {
117+ 'index.html' : '<html></html>' ,
118+ 'js/main.js' : "console.log('hello')" ,
119+ } ;
120+ const nestedReversed = {
121+ 'build/js/main.js' : files [ 'js/main.js' ] ,
122+ 'build/index.html' : files [ 'index.html' ] ,
123+ } ;
124+ const first = await cleanFile ( await createTestZip ( files , { date : new Date ( 2020 , 1 , 1 ) } ) ) ;
125+ const second = await cleanFile (
126+ await createTestZip ( nestedReversed , {
127+ compression : 'DEFLATE' ,
128+ date : new Date ( 2024 , 5 , 5 ) ,
129+ } ) ,
130+ ) ;
131+
132+ expect ( await readBytes ( second ) ) . toEqual ( await readBytes ( first ) ) ;
133+ } ) ;
134+
135+ it ( 'should keep the original file name, type and modification time' , async ( ) => {
136+ const file = await createTestZip (
137+ { 'dist/index.html' : '<html></html>' } ,
138+ { filename : 'course.zip' , lastModified : 1234567890 } ,
139+ ) ;
140+ const cleanedFile = await cleanFile ( file ) ;
141+
142+ expect ( cleanedFile . name ) . toBe ( 'course.zip' ) ;
143+ expect ( cleanedFile . type ) . toBe ( 'application/zip' ) ;
144+ expect ( cleanedFile . lastModified ) . toBe ( 1234567890 ) ;
145+ } ) ;
146+
147+ it ( 'should clean zips of incompressible content larger than 100 MB' , async ( ) => {
148+ const size = 110 * 1024 * 1024 ;
149+ const file = await createTestZip ( {
150+ 'index.html' : '<html></html>' ,
151+ 'model/weights.bin' : randomFillSync ( new Uint8Array ( size ) ) ,
152+ } ) ;
153+ const cleanedFile = await cleanFile ( file ) ;
154+
155+ expect ( cleanedFile . size ) . toBeGreaterThan ( size ) ;
156+ } , 120000 ) ;
88157 } ) ;
89158
90159 describe ( 'Error handling' , ( ) => {
0 commit comments