@@ -12,7 +12,7 @@ const Mutex2 = require("./Mutex2.js");
12
12
const path = require ( "./path.js" ) ;
13
13
const clock = require ( "./clock.js" ) ;
14
14
15
- function cleanParams ( filepath , opts ) {
15
+ function cleanParamsFilepathOpts ( filepath , opts , ... rest ) {
16
16
// normalize paths
17
17
filepath = path . normalize ( filepath ) ;
18
18
// strip out callbacks
@@ -25,30 +25,46 @@ function cleanParams(filepath, opts) {
25
25
encoding : opts ,
26
26
} ;
27
27
}
28
- return [ filepath , opts ] ;
28
+ return [ filepath , opts , ... rest ] ;
29
29
}
30
30
31
- function cleanParams2 ( oldFilepath , newFilepath ) {
31
+ function cleanParamsFilepathDataOpts ( filepath , data , opts , ... rest ) {
32
32
// normalize paths
33
- return [ path . normalize ( oldFilepath ) , path . normalize ( newFilepath ) ] ;
33
+ filepath = path . normalize ( filepath ) ;
34
+ // strip out callbacks
35
+ if ( typeof opts === "undefined" || typeof opts === "function" ) {
36
+ opts = { } ;
37
+ }
38
+ // expand string options to encoding options
39
+ if ( typeof opts === "string" ) {
40
+ opts = {
41
+ encoding : opts ,
42
+ } ;
43
+ }
44
+ return [ filepath , data , opts , ...rest ] ;
45
+ }
46
+
47
+ function cleanParamsFilepathFilepath ( oldFilepath , newFilepath , ...rest ) {
48
+ // normalize paths
49
+ return [ path . normalize ( oldFilepath ) , path . normalize ( newFilepath ) , ...rest ] ;
34
50
}
35
51
36
52
module . exports = class PromisifiedFS {
37
53
constructor ( name , options ) {
38
54
this . init = this . init . bind ( this )
39
- this . readFile = this . _wrap ( this . readFile , false )
40
- this . writeFile = this . _wrap ( this . writeFile , true )
41
- this . unlink = this . _wrap ( this . unlink , true )
42
- this . readdir = this . _wrap ( this . readdir , false )
43
- this . mkdir = this . _wrap ( this . mkdir , true )
44
- this . rmdir = this . _wrap ( this . rmdir , true )
45
- this . rename = this . _wrap ( this . rename , true )
46
- this . stat = this . _wrap ( this . stat , false )
47
- this . lstat = this . _wrap ( this . lstat , false )
48
- this . readlink = this . _wrap ( this . readlink , false )
49
- this . symlink = this . _wrap ( this . symlink , true )
50
- this . backFile = this . _wrap ( this . backFile , true )
51
- this . du = this . _wrap ( this . du , false ) ;
55
+ this . readFile = this . _wrap ( this . readFile , cleanParamsFilepathOpts , false )
56
+ this . writeFile = this . _wrap ( this . writeFile , cleanParamsFilepathDataOpts , true )
57
+ this . unlink = this . _wrap ( this . unlink , cleanParamsFilepathOpts , true )
58
+ this . readdir = this . _wrap ( this . readdir , cleanParamsFilepathOpts , false )
59
+ this . mkdir = this . _wrap ( this . mkdir , cleanParamsFilepathOpts , true )
60
+ this . rmdir = this . _wrap ( this . rmdir , cleanParamsFilepathOpts , true )
61
+ this . rename = this . _wrap ( this . rename , cleanParamsFilepathFilepath , true )
62
+ this . stat = this . _wrap ( this . stat , cleanParamsFilepathOpts , false )
63
+ this . lstat = this . _wrap ( this . lstat , cleanParamsFilepathOpts , false )
64
+ this . readlink = this . _wrap ( this . readlink , cleanParamsFilepathOpts , false )
65
+ this . symlink = this . _wrap ( this . symlink , cleanParamsFilepathFilepath , true )
66
+ this . backFile = this . _wrap ( this . backFile , cleanParamsFilepathOpts , true )
67
+ this . du = this . _wrap ( this . du , cleanParamsFilepathOpts , false ) ;
52
68
53
69
this . saveSuperblock = debounce ( ( ) => {
54
70
this . _saveSuperblock ( ) ;
@@ -111,9 +127,10 @@ module.exports = class PromisifiedFS {
111
127
this . _gracefulShutdownResolve = null
112
128
}
113
129
}
114
- _wrap ( fn , mutating ) {
130
+ _wrap ( fn , paramCleaner , mutating ) {
115
131
let i = 0
116
132
return async ( ...args ) => {
133
+ args = paramCleaner ( ...args )
117
134
let op = {
118
135
name : fn . name ,
119
136
args,
@@ -209,7 +226,6 @@ module.exports = class PromisifiedFS {
209
226
return this . _cache . writeStat ( filepath , size , opts )
210
227
}
211
228
async readFile ( filepath , opts ) {
212
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
213
229
const { encoding } = opts ;
214
230
if ( encoding && encoding !== 'utf8' ) throw new Error ( 'Only "utf8" encoding is supported in readFile' ) ;
215
231
let data = null , stat = null
@@ -240,7 +256,6 @@ module.exports = class PromisifiedFS {
240
256
return data ;
241
257
}
242
258
async writeFile ( filepath , data , opts ) {
243
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
244
259
const { mode, encoding = "utf8" } = opts ;
245
260
if ( typeof data === "string" ) {
246
261
if ( encoding !== "utf8" ) {
@@ -253,7 +268,6 @@ module.exports = class PromisifiedFS {
253
268
return null
254
269
}
255
270
async unlink ( filepath , opts ) {
256
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
257
271
const stat = this . _cache . lstat ( filepath ) ;
258
272
this . _cache . unlink ( filepath ) ;
259
273
if ( stat . type !== 'symlink' ) {
@@ -262,17 +276,14 @@ module.exports = class PromisifiedFS {
262
276
return null
263
277
}
264
278
async readdir ( filepath , opts ) {
265
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
266
279
return this . _cache . readdir ( filepath ) ;
267
280
}
268
281
async mkdir ( filepath , opts ) {
269
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
270
282
const { mode = 0o777 } = opts ;
271
283
await this . _cache . mkdir ( filepath , { mode } ) ;
272
284
return null
273
285
}
274
286
async rmdir ( filepath , opts ) {
275
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
276
287
// Never allow deleting the root directory.
277
288
if ( filepath === "/" ) {
278
289
throw new ENOTEMPTY ( ) ;
@@ -281,31 +292,25 @@ module.exports = class PromisifiedFS {
281
292
return null ;
282
293
}
283
294
async rename ( oldFilepath , newFilepath ) {
284
- ; [ oldFilepath , newFilepath ] = cleanParams2 ( oldFilepath , newFilepath ) ;
285
295
this . _cache . rename ( oldFilepath , newFilepath ) ;
286
296
return null ;
287
297
}
288
298
async stat ( filepath , opts ) {
289
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
290
299
const data = this . _cache . stat ( filepath ) ;
291
300
return new Stat ( data ) ;
292
301
}
293
302
async lstat ( filepath , opts ) {
294
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
295
303
let data = this . _cache . lstat ( filepath ) ;
296
304
return new Stat ( data ) ;
297
305
}
298
306
async readlink ( filepath , opts ) {
299
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
300
307
return this . _cache . readlink ( filepath ) ;
301
308
}
302
309
async symlink ( target , filepath ) {
303
- ; [ target , filepath ] = cleanParams2 ( target , filepath ) ;
304
310
this . _cache . symlink ( target , filepath ) ;
305
311
return null ;
306
312
}
307
313
async backFile ( filepath , opts ) {
308
- ; [ filepath , opts ] = cleanParams ( filepath , opts ) ;
309
314
let size = await this . _http . sizeFile ( filepath )
310
315
await this . _writeStat ( filepath , size , opts )
311
316
return null
0 commit comments