@@ -25,6 +25,19 @@ function shouldDoTest(testPath) {
25
25
return args . indexOf ( testPath ) !== - 1 ;
26
26
}
27
27
28
+ var openFunctions = [
29
+ function ( zipfilePath , testId , options , callback ) { yauzl . open ( zipfilePath , options , callback ) ; } ,
30
+ function ( zipfilePath , testId , options , callback ) { yauzl . fromBuffer ( fs . readFileSync ( zipfilePath ) , options , callback ) ; } ,
31
+ function ( zipfilePath , testId , options , callback ) { openWithRandomAccess ( zipfilePath , options , true , testId , callback ) ; } ,
32
+ function ( zipfilePath , testId , options , callback ) { openWithRandomAccess ( zipfilePath , options , false , testId , callback ) ; } ,
33
+ ] ;
34
+ var openFunctionNames = [
35
+ "fd" ,
36
+ "buffer" ,
37
+ "randomAccess" ,
38
+ "minimalRandomAccess" ,
39
+ ] ;
40
+
28
41
// success tests
29
42
listZipFiles ( [ path . join ( __dirname , "success" ) , path . join ( __dirname , "wrong-entry-sizes" ) ] ) . forEach ( function ( zipfilePath ) {
30
43
if ( ! shouldDoTest ( zipfilePath ) ) return ;
@@ -38,15 +51,9 @@ listZipFiles([path.join(__dirname, "success"), path.join(__dirname, "wrong-entry
38
51
options . validateEntrySizes = false ;
39
52
} ) ;
40
53
}
41
- var openFunctions = [
42
- function ( testId , options , callback ) { yauzl . open ( zipfilePath , options , callback ) ; } ,
43
- function ( testId , options , callback ) { yauzl . fromBuffer ( fs . readFileSync ( zipfilePath ) , options , callback ) ; } ,
44
- function ( testId , options , callback ) { openWithRandomAccess ( zipfilePath , options , true , testId , callback ) ; } ,
45
- function ( testId , options , callback ) { openWithRandomAccess ( zipfilePath , options , false , testId , callback ) ; } ,
46
- ] ;
47
54
openFunctions . forEach ( function ( openFunction , i ) {
48
55
optionConfigurations . forEach ( function ( options , j ) {
49
- var testId = zipfilePath + "(" + [ "fd" , "buffer" , "randomAccess" , "minimalRandomAccess" ] [ i ] + "," + j + "): " ;
56
+ var testId = zipfilePath + "(" + openFunctionNames [ i ] + "," + j + "): " ;
50
57
var expectedPathPrefix = zipfilePath . replace ( / \. z i p $ / , "" ) ;
51
58
var expectedArchiveContents = { } ;
52
59
var DIRECTORY = 1 ; // not a string
@@ -77,7 +84,7 @@ listZipFiles([path.join(__dirname, "success"), path.join(__dirname, "wrong-entry
77
84
}
78
85
}
79
86
pend . go ( function ( zipfileCallback ) {
80
- openFunction ( testId , options , function ( err , zipfile ) {
87
+ openFunction ( zipfilePath , testId , options , function ( err , zipfile ) {
81
88
if ( err ) throw err ;
82
89
zipfile . readEntry ( ) ;
83
90
zipfile . on ( "entry" , function ( entry ) {
@@ -163,68 +170,77 @@ listZipFiles([path.join(__dirname, "success"), path.join(__dirname, "wrong-entry
163
170
listZipFiles ( [ path . join ( __dirname , "failure" ) ] ) . forEach ( function ( zipfilePath ) {
164
171
if ( ! shouldDoTest ( zipfilePath ) ) return ;
165
172
var expectedErrorMessage = path . basename ( zipfilePath ) . replace ( / ( _ \d + ) ? \. z i p $ / , "" ) ;
166
- var failedYet = false ;
167
- var emittedError = false ;
168
- pend . go ( function ( cb ) {
169
- var operationsInProgress = 0 ;
170
- if ( / i n v a l i d c h a r a c t e r s i n f i l e N a m e / . test ( zipfilePath ) ) {
171
- // this error can only happen when you specify an option
172
- yauzl . open ( zipfilePath , { strictFileNames : true } , onZipFile ) ;
173
- } else {
174
- yauzl . open ( zipfilePath , onZipFile ) ;
175
- }
176
- return ;
177
-
178
- function onZipFile ( err , zipfile ) {
179
- if ( err ) return checkErrorMessage ( err ) ;
180
- zipfile . on ( "error" , function ( err ) {
181
- noEventsAllowedAfterError ( ) ;
182
- emittedError = true ;
183
- checkErrorMessage ( err ) ;
184
- } ) ;
185
- zipfile . on ( "entry" , function ( entry ) {
186
- noEventsAllowedAfterError ( ) ;
187
- // let's also try to read directories, cuz whatever.
188
- operationsInProgress += 1 ;
189
- zipfile . openReadStream ( entry , function ( err , stream ) {
190
- if ( err ) return checkErrorMessage ( err ) ;
191
- stream . on ( "error" , function ( err ) {
192
- checkErrorMessage ( err ) ;
193
- } ) ;
194
- stream . on ( "data" , function ( data ) {
195
- // ignore
196
- } ) ;
197
- stream . on ( "end" , function ( ) {
198
- doneWithSomething ( ) ;
173
+
174
+ openFunctions . forEach ( function ( openFunction , i ) {
175
+ var testId = zipfilePath + "(" + openFunctionNames [ i ] + "): " ;
176
+ var failedYet = false ;
177
+ var emittedError = false ;
178
+ pend . go ( function ( cb ) {
179
+ var operationsInProgress = 0 ;
180
+ var options = null ;
181
+ if ( / i n v a l i d c h a r a c t e r s i n f i l e N a m e / . test ( zipfilePath ) ) {
182
+ // this error can only happen when you specify an option
183
+ options = { strictFileNames : true } ;
184
+ }
185
+ openFunction ( zipfilePath , testId , options , onZipFile ) ;
186
+
187
+ function onZipFile ( err , zipfile ) {
188
+ if ( err ) return checkErrorMessage ( err ) ;
189
+ zipfile . on ( "error" , function ( err ) {
190
+ noEventsAllowedAfterError ( ) ;
191
+ emittedError = true ;
192
+ checkErrorMessage ( err ) ;
193
+ } ) ;
194
+ zipfile . on ( "entry" , function ( entry ) {
195
+ noEventsAllowedAfterError ( ) ;
196
+ // let's also try to read directories, cuz whatever.
197
+ operationsInProgress += 1 ;
198
+ zipfile . openReadStream ( entry , function ( err , stream ) {
199
+ if ( err ) return checkErrorMessage ( err ) ;
200
+ stream . on ( "error" , function ( err ) {
201
+ checkErrorMessage ( err ) ;
202
+ } ) ;
203
+ stream . on ( "data" , function ( data ) {
204
+ // ignore
205
+ } ) ;
206
+ stream . on ( "end" , function ( ) {
207
+ doneWithSomething ( ) ;
208
+ } ) ;
199
209
} ) ;
200
210
} ) ;
201
- } ) ;
202
- operationsInProgress += 1 ;
203
- zipfile . on ( "end" , function ( ) {
204
- noEventsAllowedAfterError ( ) ;
205
- doneWithSomething ( ) ;
206
- } ) ;
207
- function doneWithSomething ( ) {
208
- operationsInProgress -= 1 ;
209
- if ( operationsInProgress !== 0 ) return ;
210
- if ( ! failedYet ) {
211
- throw new Error ( zipfilePath + ": expected failure" ) ;
211
+ operationsInProgress += 1 ;
212
+ zipfile . on ( "end" , function ( ) {
213
+ noEventsAllowedAfterError ( ) ;
214
+ doneWithSomething ( ) ;
215
+ } ) ;
216
+ function doneWithSomething ( ) {
217
+ operationsInProgress -= 1 ;
218
+ if ( operationsInProgress !== 0 ) return ;
219
+ if ( ! failedYet ) {
220
+ throw new Error ( testId + "expected failure" ) ;
221
+ }
212
222
}
213
223
}
214
- }
215
- function checkErrorMessage ( err ) {
216
- var actualMessage = err . message . replace ( / [ ^ 0 - 9 A - Z a - z - ] + / g, " " ) . trimRight ( ) ;
217
- if ( actualMessage !== expectedErrorMessage ) {
218
- throw new Error ( zipfilePath + ": wrong error message: " + actualMessage ) ;
224
+ function checkErrorMessage ( err ) {
225
+ var actualMessage = err . message . replace ( / [ ^ 0 - 9 A - Z a - z - ] + / g, " " ) . trimRight ( ) ;
226
+ if ( actualMessage !== expectedErrorMessage ) {
227
+ if ( i !== 0 ) {
228
+ // The error messages are tuned for the common case.
229
+ // Sometimes other open functions give slightly different error messages, and that's ok,
230
+ // as long as we're still getting some error.
231
+ } else {
232
+ throw new Error ( testId + "wrong error message: " + actualMessage ) ;
233
+ }
234
+ }
235
+ console . log ( testId + "pass" ) ;
236
+ failedYet = true ;
237
+ operationsInProgress = - Infinity ;
238
+ cb ( ) ;
219
239
}
220
- console . log ( zipfilePath + ": pass" ) ;
221
- failedYet = true ;
222
- operationsInProgress = - Infinity ;
223
- cb ( ) ;
224
- }
225
- function noEventsAllowedAfterError ( ) {
226
- if ( emittedError ) throw new Error ( "events emitted after error event" ) ;
227
- }
240
+ function noEventsAllowedAfterError ( ) {
241
+ if ( emittedError ) throw new Error ( testId + "events emitted after error event" ) ;
242
+ }
243
+ } ) ;
228
244
} ) ;
229
245
} ) ;
230
246
@@ -471,11 +487,11 @@ function openWithRandomAccess(zipfilePath, options, implementRead, testId, callb
471
487
if ( implementRead ) {
472
488
InefficientRandomAccessReader . prototype . read = function ( buffer , offset , length , position , callback ) {
473
489
fs . open ( zipfilePath , "r" , function ( err , fd ) {
474
- if ( err ) throw err ;
490
+ if ( err ) return callback ( err ) ;
475
491
fs . read ( fd , buffer , offset , length , position , function ( err , bytesRead ) {
476
- if ( bytesRead < length ) throw new Error ( "unexpected EOF" ) ;
492
+ if ( bytesRead < length ) return callback ( new Error ( "unexpected EOF" ) ) ;
477
493
fs . close ( fd , function ( err ) {
478
- if ( err ) throw err ;
494
+ if ( err ) return callback ( err ) ;
479
495
callback ( ) ;
480
496
} ) ;
481
497
} ) ;
@@ -488,10 +504,10 @@ function openWithRandomAccess(zipfilePath, options, implementRead, testId, callb
488
504
} ;
489
505
490
506
fs . stat ( zipfilePath , function ( err , stats ) {
491
- if ( err ) throw err ;
507
+ if ( err ) return callback ( err ) ;
492
508
var reader = new InefficientRandomAccessReader ( ) ;
493
509
yauzl . fromRandomAccessReader ( reader , stats . size , options , function ( err , zipfile ) {
494
- if ( err ) throw err ;
510
+ if ( err ) return callback ( err ) ;
495
511
callback ( null , zipfile ) ;
496
512
} ) ;
497
513
} ) ;
0 commit comments