@@ -149,21 +149,29 @@ function overrideAPI(module, name, arg = 0) {
149
149
// Override fs APIs.
150
150
exports . wrapFsWithAsar = function ( fs ) {
151
151
const { lstatSync} = fs
152
- fs . lstatSync = function ( p ) {
152
+ fs . lstatSync = function ( p , options ) {
153
153
const [ isAsar , filePath ] = splitPath ( p )
154
154
if ( ! isAsar )
155
- return lstatSync ( p )
155
+ return lstatSync ( p , options )
156
156
const stats = process . asarArchive . stat ( filePath )
157
- if ( ! stats )
158
- notFoundError ( filePath )
157
+ if ( ! stats ) {
158
+ if ( options ?. throwIfNoEntry )
159
+ notFoundError ( filePath )
160
+ else
161
+ return undefined
162
+ }
159
163
return generateStats ( stats )
160
164
}
161
165
162
166
const { lstat} = fs
163
- fs . lstat = function ( p , callback ) {
167
+ fs . lstat = function ( p , options , callback ) {
168
+ if ( typeof options == 'function' ) {
169
+ callback = options
170
+ options = { }
171
+ }
164
172
const [ isAsar , filePath ] = splitPath ( p )
165
173
if ( ! isAsar )
166
- return lstat ( p , callback )
174
+ return lstat ( p , options , callback )
167
175
const stats = process . asarArchive . stat ( filePath )
168
176
if ( ! stats )
169
177
return notFoundError ( filePath , callback )
@@ -175,29 +183,33 @@ exports.wrapFsWithAsar = function(fs) {
175
183
fs . promises . lstat = util . promisify ( fs . lstat )
176
184
177
185
const { statSync} = fs
178
- fs . statSync = function ( p ) {
186
+ fs . statSync = function ( p , options ) {
179
187
const [ isAsar ] = splitPath ( p )
180
188
if ( ! isAsar )
181
- return statSync ( p )
189
+ return statSync ( p , options )
182
190
// Do not distinguish links for now.
183
- return fs . lstatSync ( p )
191
+ return fs . lstatSync ( p , options )
184
192
}
185
193
186
194
const { stat} = fs
187
- fs . stat = function ( p , callback ) {
195
+ fs . stat = function ( p , options , callback ) {
196
+ if ( typeof options == 'function' ) {
197
+ callback = options
198
+ options = { }
199
+ }
188
200
const [ isAsar ] = splitPath ( p )
189
201
if ( ! isAsar )
190
- return stat ( p , callback )
202
+ return stat ( p , options , callback )
191
203
// Do not distinguish links for now.
192
204
process . nextTick ( function ( ) {
193
- fs . lstat ( p , callback )
205
+ fs . lstat ( p , options , callback )
194
206
} )
195
207
}
196
208
197
209
fs . promises . stat = util . promisify ( fs . lstat )
198
210
199
211
const wrapRealpathSync = function ( func ) {
200
- return function ( p ) {
212
+ return function ( p , options ) {
201
213
const [ isAsar , filePath ] = splitPath ( p )
202
214
if ( ! isAsar )
203
215
return func . apply ( this , arguments )
@@ -208,7 +220,7 @@ exports.wrapFsWithAsar = function(fs) {
208
220
if ( info . unpacked )
209
221
return real
210
222
else
211
- return path . join ( func ( process . execPath ) , 'asar' , real )
223
+ return path . join ( func ( process . execPath , options ) , 'asar' , real )
212
224
}
213
225
}
214
226
@@ -217,13 +229,13 @@ exports.wrapFsWithAsar = function(fs) {
217
229
fs . realpathSync . native = wrapRealpathSync ( realpathSync . native ) ;
218
230
219
231
const wrapRealpath = function ( func ) {
220
- return function ( p , cache , callback ) {
232
+ return function ( p , options , callback ) {
221
233
const [ isAsar , filePath ] = splitPath ( p )
222
234
if ( ! isAsar )
223
235
return func . apply ( this , arguments )
224
- if ( typeof cache === 'function' ) {
225
- callback = cache
226
- cache = undefined
236
+ if ( arguments . length < 3 ) {
237
+ callback = options
238
+ options = { }
227
239
}
228
240
const info = process . asarArchive . getFileInfo ( filePath )
229
241
if ( ! info )
@@ -232,7 +244,7 @@ exports.wrapFsWithAsar = function(fs) {
232
244
if ( info . unpacked ) {
233
245
callback ( null , real )
234
246
} else {
235
- func ( process . execPath , function ( err , p ) {
247
+ func ( process . execPath , options , function ( err , p ) {
236
248
if ( err )
237
249
return callback ( err )
238
250
return callback ( null , path . join ( p , 'asar' , real ) )
@@ -419,10 +431,16 @@ exports.wrapFsWithAsar = function(fs) {
419
431
}
420
432
421
433
const { readdir} = fs
422
- fs . readdir = function ( p , callback ) {
434
+ fs . readdir = function ( p , options , callback ) {
423
435
const [ isAsar , filePath ] = splitPath ( p )
424
436
if ( ! isAsar )
425
437
return readdir . apply ( this , arguments )
438
+ if ( typeof options == 'function' ) {
439
+ callback = options
440
+ options = { }
441
+ } else if ( typeof options == 'object' ) {
442
+ throw new Error ( 'fs.readdir with options is not supported for ASAR' )
443
+ }
426
444
const files = process . asarArchive . readdir ( filePath )
427
445
if ( ! files )
428
446
return notFoundError ( filePath , callback )
@@ -434,10 +452,12 @@ exports.wrapFsWithAsar = function(fs) {
434
452
fs . promises . readdir = util . promisify ( fs . readdir )
435
453
436
454
const { readdirSync} = fs
437
- fs . readdirSync = function ( p ) {
455
+ fs . readdirSync = function ( p , options ) {
438
456
const [ isAsar , filePath ] = splitPath ( p )
439
457
if ( ! isAsar )
440
458
return readdirSync . apply ( this , arguments )
459
+ if ( typeof options == 'object' )
460
+ throw new Error ( 'fs.readdir with options is not supported for ASAR' )
441
461
const files = process . asarArchive . readdir ( filePath )
442
462
if ( ! files )
443
463
notFoundError ( filePath )
@@ -482,22 +502,23 @@ exports.wrapFsWithAsar = function(fs) {
482
502
// widely used.
483
503
if ( process . platform === 'win32' ) {
484
504
const { mkdir} = fs
485
- fs . mkdir = function ( p , mode , callback ) {
486
- if ( typeof mode === 'function' ) {
487
- callback = mode
505
+ fs . mkdir = function ( p , options , callback ) {
506
+ if ( typeof options == 'function' ) {
507
+ callback = options
508
+ options = { }
488
509
}
489
510
const [ isAsar , filePath ] = splitPath ( p )
490
511
if ( isAsar && filePath . length )
491
512
return notDirError ( callback )
492
- mkdir ( p , mode , callback )
513
+ mkdir ( p , options , callback )
493
514
}
494
515
495
516
const { mkdirSync} = fs
496
- fs . mkdirSync = function ( p , mode ) {
517
+ fs . mkdirSync = function ( p , options ) {
497
518
const [ isAsar , filePath ] = splitPath ( p )
498
519
if ( isAsar && filePath . length )
499
520
return notDirError ( )
500
- return mkdirSync ( p , mode )
521
+ return mkdirSync ( p , options )
501
522
}
502
523
}
503
524
0 commit comments