Skip to content

Commit c936a51

Browse files
committed
move cleanParam logic to _wrap
1 parent 40bf0bf commit c936a51

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

src/PromisifiedFS.js

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Mutex2 = require("./Mutex2.js");
1212
const path = require("./path.js");
1313
const clock = require("./clock.js");
1414

15-
function cleanParams(filepath, opts) {
15+
function cleanParamsFilepathOpts(filepath, opts, ...rest) {
1616
// normalize paths
1717
filepath = path.normalize(filepath);
1818
// strip out callbacks
@@ -25,30 +25,46 @@ function cleanParams(filepath, opts) {
2525
encoding: opts,
2626
};
2727
}
28-
return [filepath, opts];
28+
return [filepath, opts, ...rest];
2929
}
3030

31-
function cleanParams2(oldFilepath, newFilepath) {
31+
function cleanParamsFilepathDataOpts(filepath, data, opts, ...rest) {
3232
// 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];
3450
}
3551

3652
module.exports = class PromisifiedFS {
3753
constructor(name, options) {
3854
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);
5268

5369
this.saveSuperblock = debounce(() => {
5470
this._saveSuperblock();
@@ -111,9 +127,10 @@ module.exports = class PromisifiedFS {
111127
this._gracefulShutdownResolve = null
112128
}
113129
}
114-
_wrap (fn, mutating) {
130+
_wrap (fn, paramCleaner, mutating) {
115131
let i = 0
116132
return async (...args) => {
133+
args = paramCleaner(...args)
117134
let op = {
118135
name: fn.name,
119136
args,
@@ -209,7 +226,6 @@ module.exports = class PromisifiedFS {
209226
return this._cache.writeStat(filepath, size, opts)
210227
}
211228
async readFile(filepath, opts) {
212-
;[filepath, opts] = cleanParams(filepath, opts);
213229
const { encoding } = opts;
214230
if (encoding && encoding !== 'utf8') throw new Error('Only "utf8" encoding is supported in readFile');
215231
let data = null, stat = null
@@ -240,7 +256,6 @@ module.exports = class PromisifiedFS {
240256
return data;
241257
}
242258
async writeFile(filepath, data, opts) {
243-
;[filepath, opts] = cleanParams(filepath, opts);
244259
const { mode, encoding = "utf8" } = opts;
245260
if (typeof data === "string") {
246261
if (encoding !== "utf8") {
@@ -253,7 +268,6 @@ module.exports = class PromisifiedFS {
253268
return null
254269
}
255270
async unlink(filepath, opts) {
256-
;[filepath, opts] = cleanParams(filepath, opts);
257271
const stat = this._cache.lstat(filepath);
258272
this._cache.unlink(filepath);
259273
if (stat.type !== 'symlink') {
@@ -262,17 +276,14 @@ module.exports = class PromisifiedFS {
262276
return null
263277
}
264278
async readdir(filepath, opts) {
265-
;[filepath, opts] = cleanParams(filepath, opts);
266279
return this._cache.readdir(filepath);
267280
}
268281
async mkdir(filepath, opts) {
269-
;[filepath, opts] = cleanParams(filepath, opts);
270282
const { mode = 0o777 } = opts;
271283
await this._cache.mkdir(filepath, { mode });
272284
return null
273285
}
274286
async rmdir(filepath, opts) {
275-
;[filepath, opts] = cleanParams(filepath, opts);
276287
// Never allow deleting the root directory.
277288
if (filepath === "/") {
278289
throw new ENOTEMPTY();
@@ -281,31 +292,25 @@ module.exports = class PromisifiedFS {
281292
return null;
282293
}
283294
async rename(oldFilepath, newFilepath) {
284-
;[oldFilepath, newFilepath] = cleanParams2(oldFilepath, newFilepath);
285295
this._cache.rename(oldFilepath, newFilepath);
286296
return null;
287297
}
288298
async stat(filepath, opts) {
289-
;[filepath, opts] = cleanParams(filepath, opts);
290299
const data = this._cache.stat(filepath);
291300
return new Stat(data);
292301
}
293302
async lstat(filepath, opts) {
294-
;[filepath, opts] = cleanParams(filepath, opts);
295303
let data = this._cache.lstat(filepath);
296304
return new Stat(data);
297305
}
298306
async readlink(filepath, opts) {
299-
;[filepath, opts] = cleanParams(filepath, opts);
300307
return this._cache.readlink(filepath);
301308
}
302309
async symlink(target, filepath) {
303-
;[target, filepath] = cleanParams2(target, filepath);
304310
this._cache.symlink(target, filepath);
305311
return null;
306312
}
307313
async backFile(filepath, opts) {
308-
;[filepath, opts] = cleanParams(filepath, opts);
309314
let size = await this._http.sizeFile(filepath)
310315
await this._writeStat(filepath, size, opts)
311316
return null

0 commit comments

Comments
 (0)