@@ -72,16 +72,28 @@ class OperationMergerBackend {
72
72
73
73
this . provide = this . _provider
74
74
? /**
75
- * @param {string } path path
76
- * @param {any } options options
77
- * @param {function } callback callback
75
+ * @param {PathLike | PathOrFileDescriptor } path path
76
+ * @param {object | FileSystemCallback< any> | undefined } options options
77
+ * @param {FileSystemCallback<any>= } callback callback
78
78
* @returns {any } result
79
79
*/
80
80
( path , options , callback ) => {
81
81
if ( typeof options === "function" ) {
82
- callback = options ;
82
+ callback = /** @type { FileSystemCallback<any> } */ ( options ) ;
83
83
options = undefined ;
84
84
}
85
+ if (
86
+ typeof path !== "string" &&
87
+ ! Buffer . isBuffer ( path ) &&
88
+ ! ( path instanceof URL ) &&
89
+ typeof path !== "number"
90
+ ) {
91
+ /** @type {Function } */
92
+ ( callback ) (
93
+ new TypeError ( "path must be a string, Buffer, URL or number" )
94
+ ) ;
95
+ return ;
96
+ }
85
97
if ( options ) {
86
98
return /** @type {Function } */ ( this . _provider ) . call (
87
99
this . _providerContext ,
@@ -90,10 +102,6 @@ class OperationMergerBackend {
90
102
callback
91
103
) ;
92
104
}
93
- if ( typeof path !== "string" ) {
94
- callback ( new TypeError ( "path must be a string" ) ) ;
95
- return ;
96
- }
97
105
let callbacks = this . _activeAsyncOperations . get ( path ) ;
98
106
if ( callbacks ) {
99
107
callbacks . push ( callback ) ;
@@ -116,8 +124,8 @@ class OperationMergerBackend {
116
124
: null ;
117
125
this . provideSync = this . _syncProvider
118
126
? /**
119
- * @param {string } path path
120
- * @param {any } options options
127
+ * @param {PathLike | PathOrFileDescriptor } path path
128
+ * @param {object= } options options
121
129
* @returns {any } result
122
130
*/
123
131
( path , options ) => {
@@ -213,10 +221,16 @@ class CacheBackend {
213
221
callback = options ;
214
222
options = undefined ;
215
223
}
216
- if ( typeof path !== "string" ) {
217
- callback ( new TypeError ( "path must be a string" ) ) ;
224
+ if (
225
+ typeof path !== "string" &&
226
+ ! Buffer . isBuffer ( path ) &&
227
+ ! ( path instanceof URL ) &&
228
+ typeof path !== "number"
229
+ ) {
230
+ callback ( new TypeError ( "path must be a string, Buffer, URL or number" ) ) ;
218
231
return ;
219
232
}
233
+ const strPath = typeof path !== "string" ? path . toString ( ) : path ;
220
234
if ( options ) {
221
235
return /** @type {Function } */ ( this . _provider ) . call (
222
236
this . _providerContext ,
@@ -232,19 +246,19 @@ class CacheBackend {
232
246
}
233
247
234
248
// Check in cache
235
- let cacheEntry = this . _data . get ( path ) ;
249
+ let cacheEntry = this . _data . get ( strPath ) ;
236
250
if ( cacheEntry !== undefined ) {
237
251
if ( cacheEntry . err ) return nextTick ( callback , cacheEntry . err ) ;
238
252
return nextTick ( callback , null , cacheEntry . result ) ;
239
253
}
240
254
241
255
// Check if there is already the same operation running
242
- let callbacks = this . _activeAsyncOperations . get ( path ) ;
256
+ let callbacks = this . _activeAsyncOperations . get ( strPath ) ;
243
257
if ( callbacks !== undefined ) {
244
258
callbacks . push ( callback ) ;
245
259
return ;
246
260
}
247
- this . _activeAsyncOperations . set ( path , ( callbacks = [ callback ] ) ) ;
261
+ this . _activeAsyncOperations . set ( strPath , ( callbacks = [ callback ] ) ) ;
248
262
249
263
// Run the operation
250
264
/** @type {Function } */
@@ -256,8 +270,8 @@ class CacheBackend {
256
270
* @param {any } [result] result
257
271
*/
258
272
( err , result ) => {
259
- this . _activeAsyncOperations . delete ( path ) ;
260
- this . _storeResult ( path , err , result ) ;
273
+ this . _activeAsyncOperations . delete ( strPath ) ;
274
+ this . _storeResult ( strPath , err , result ) ;
261
275
262
276
// Enter async mode if not yet done
263
277
this . _enterAsyncMode ( ) ;
@@ -277,9 +291,15 @@ class CacheBackend {
277
291
* @returns {any } result
278
292
*/
279
293
provideSync ( path , options ) {
280
- if ( typeof path !== "string" ) {
294
+ if (
295
+ typeof path !== "string" &&
296
+ ! Buffer . isBuffer ( path ) &&
297
+ ! ( path instanceof URL ) &&
298
+ typeof path !== "number"
299
+ ) {
281
300
throw new TypeError ( "path must be a string" ) ;
282
301
}
302
+ const strPath = typeof path !== "string" ? path . toString ( ) : path ;
283
303
if ( options ) {
284
304
return /** @type {Function } */ ( this . _syncProvider ) . call (
285
305
this . _providerContext ,
@@ -294,16 +314,16 @@ class CacheBackend {
294
314
}
295
315
296
316
// Check in cache
297
- let cacheEntry = this . _data . get ( path ) ;
317
+ let cacheEntry = this . _data . get ( strPath ) ;
298
318
if ( cacheEntry !== undefined ) {
299
319
if ( cacheEntry . err ) throw cacheEntry . err ;
300
320
return cacheEntry . result ;
301
321
}
302
322
303
323
// Get all active async operations
304
324
// This sync operation will also complete them
305
- const callbacks = this . _activeAsyncOperations . get ( path ) ;
306
- this . _activeAsyncOperations . delete ( path ) ;
325
+ const callbacks = this . _activeAsyncOperations . get ( strPath ) ;
326
+ this . _activeAsyncOperations . delete ( strPath ) ;
307
327
308
328
// Run the operation
309
329
// When in idle mode, we will enter sync mode
@@ -314,14 +334,14 @@ class CacheBackend {
314
334
path
315
335
) ;
316
336
} catch ( err ) {
317
- this . _storeResult ( path , /** @type {Error } */ ( err ) , undefined ) ;
337
+ this . _storeResult ( strPath , /** @type {Error } */ ( err ) , undefined ) ;
318
338
this . _enterSyncModeWhenIdle ( ) ;
319
339
if ( callbacks ) {
320
340
runCallbacks ( callbacks , /** @type {Error } */ ( err ) , undefined ) ;
321
341
}
322
342
throw err ;
323
343
}
324
- this . _storeResult ( path , null , result ) ;
344
+ this . _storeResult ( strPath , null , result ) ;
325
345
this . _enterSyncModeWhenIdle ( ) ;
326
346
if ( callbacks ) {
327
347
runCallbacks ( callbacks , null , result ) ;
@@ -330,7 +350,7 @@ class CacheBackend {
330
350
}
331
351
332
352
/**
333
- * @param {string | string[] | Set<string> } [what] what to purge
353
+ * @param {string | Buffer | URL | number | ( string | URL | Buffer | number) [] | Set<string | URL | Buffer | number > } [what] what to purge
334
354
*/
335
355
purge ( what ) {
336
356
if ( ! what ) {
@@ -341,9 +361,15 @@ class CacheBackend {
341
361
}
342
362
this . _enterIdleMode ( ) ;
343
363
}
344
- } else if ( typeof what === "string" ) {
364
+ } else if (
365
+ typeof what === "string" ||
366
+ Buffer . isBuffer ( what ) ||
367
+ what instanceof URL ||
368
+ typeof what === "number"
369
+ ) {
370
+ const strWhat = typeof what !== "string" ? what . toString ( ) : what ;
345
371
for ( let [ key , data ] of this . _data ) {
346
- if ( key . startsWith ( what ) ) {
372
+ if ( key . startsWith ( strWhat ) ) {
347
373
this . _data . delete ( key ) ;
348
374
data . level . delete ( key ) ;
349
375
}
@@ -354,7 +380,8 @@ class CacheBackend {
354
380
} else {
355
381
for ( let [ key , data ] of this . _data ) {
356
382
for ( const item of what ) {
357
- if ( key . startsWith ( item ) ) {
383
+ const strItem = typeof item !== "string" ? item . toString ( ) : item ;
384
+ if ( key . startsWith ( strItem ) ) {
358
385
this . _data . delete ( key ) ;
359
386
data . level . delete ( key ) ;
360
387
break ;
@@ -368,17 +395,24 @@ class CacheBackend {
368
395
}
369
396
370
397
/**
371
- * @param {string| string[]| Set<string> } [what] what to purge
398
+ * @param {string | Buffer | URL | number | ( string | URL | Buffer | number)[] | Set<string | URL | Buffer | number > } [what] what to purge
372
399
*/
373
400
purgeParent ( what ) {
374
401
if ( ! what ) {
375
402
this . purge ( ) ;
376
- } else if ( typeof what === "string" ) {
377
- this . purge ( dirname ( what ) ) ;
403
+ } else if (
404
+ typeof what === "string" ||
405
+ Buffer . isBuffer ( what ) ||
406
+ what instanceof URL ||
407
+ typeof what === "number"
408
+ ) {
409
+ const strWhat = typeof what !== "string" ? what . toString ( ) : what ;
410
+ this . purge ( dirname ( strWhat ) ) ;
378
411
} else {
379
412
const set = new Set ( ) ;
380
413
for ( const item of what ) {
381
- set . add ( dirname ( item ) ) ;
414
+ const strItem = typeof item !== "string" ? item . toString ( ) : item ;
415
+ set . add ( dirname ( strItem ) ) ;
382
416
}
383
417
this . purge ( set ) ;
384
418
}
@@ -616,7 +650,7 @@ module.exports = class CachedInputFileSystem {
616
650
}
617
651
618
652
/**
619
- * @param {string| string[]| Set<string> } [what] what to purge
653
+ * @param {string | Buffer | URL | number | ( string | URL | Buffer | number)[] | Set<string | URL | Buffer | number > } [what] what to purge
620
654
*/
621
655
purge ( what ) {
622
656
this . _statBackend . purge ( what ) ;
0 commit comments