@@ -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 ) ;
@@ -368,7 +388,7 @@ class CacheBackend {
368
388
}
369
389
370
390
/**
371
- * @param {string| string[]| Set<string> } [what] what to purge
391
+ * @param {string | string[] | Set<string> } [what] what to purge
372
392
*/
373
393
purgeParent ( what ) {
374
394
if ( ! what ) {
0 commit comments