Skip to content
This repository was archived by the owner on May 27, 2019. It is now read-only.

Commit 0455848

Browse files
thefourtheyekevinoid
authored andcommitted
fs: refactor "options" processing as a function
As it is, the "options" processing is repeated in all the functions which need it. That introduces checks which are inconsistent with other functions and produces slightly different error messages. This patch moves the basic "options" validation and processing to a seperate function. PR-URL: nodejs/node#7165 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Nicu Micleușanu <[email protected]> Reviewed-By: Yorkie Liu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8387613 commit 0455848

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

fs-file-sync-fd.js

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,24 @@ var util = require('util');
1818

1919
var fsFileSyncFD = {};
2020

21-
function throwOptionsError(options) {
22-
throw new TypeError('Expected options to be either an object or a string, ' +
23-
'but got ' + typeof options + ' instead');
21+
function getOptions(options, defaultOptions) {
22+
if (options === null || options === undefined ||
23+
typeof options === 'function') {
24+
return defaultOptions;
25+
}
26+
27+
if (typeof options === 'string') {
28+
defaultOptions = util._extend({}, defaultOptions);
29+
defaultOptions.encoding = options;
30+
options = defaultOptions;
31+
} else if (typeof options !== 'object') {
32+
throw new TypeError('"options" must be a string or an object, got ' +
33+
typeof options + ' instead.');
34+
}
35+
36+
if (options.encoding !== 'buffer')
37+
assertEncoding(options.encoding);
38+
return options;
2439
}
2540

2641
function assertEncoding(encoding) {
@@ -70,20 +85,9 @@ function tryReadSync(fd, isUserFd, buffer, pos, len) {
7085
}
7186

7287
fsFileSyncFD.readFileSync = function(path, options) {
73-
if (!options) {
74-
options = { encoding: null, flag: 'r' };
75-
} else if (typeof options === 'string') {
76-
options = { encoding: options, flag: 'r' };
77-
} else if (typeof options !== 'object') {
78-
throwOptionsError(options);
79-
}
80-
81-
var encoding = options.encoding;
82-
assertEncoding(encoding);
83-
84-
var flag = options.flag || 'r';
88+
options = getOptions(options, { flag: 'r' });
8589
var isUserFd = isFd(path); // file descriptor ownership
86-
var fd = isUserFd ? path : fs.openSync(path, flag, 438 /*=0o666*/);
90+
var fd = isUserFd ? path : fs.openSync(path, options.flag || 'r', 438);
8791

8892
var st = tryStatSync(fd, isUserFd);
8993
var size = st.isFile() ? st.size : 0;
@@ -127,22 +131,14 @@ fsFileSyncFD.readFileSync = function(path, options) {
127131
buffer = buffer.slice(0, pos);
128132
}
129133

130-
if (encoding) buffer = buffer.toString(encoding);
134+
if (options.encoding) buffer = buffer.toString(options.encoding);
131135
return buffer;
132136
};
133137

134138
fsFileSyncFD.writeFileSync = function(path, data, options) {
135-
if (!options) {
136-
options = { encoding: 'utf8', mode: 438 /*=0o666*/, flag: 'w' };
137-
} else if (typeof options === 'string') {
138-
options = { encoding: options, mode: 438 /*=0o666*/, flag: 'w' };
139-
} else if (typeof options !== 'object') {
140-
throwOptionsError(options);
141-
}
142-
143-
assertEncoding(options.encoding);
144-
139+
options = getOptions(options, { encoding: 'utf8', mode: 438, flag: 'w' });
145140
var flag = options.flag || 'w';
141+
146142
var isUserFd = isFd(path); // file descriptor ownership
147143
var fd = isUserFd ? path : fs.openSync(path, flag, options.mode);
148144

@@ -167,13 +163,7 @@ fsFileSyncFD.writeFileSync = function(path, data, options) {
167163
};
168164

169165
fsFileSyncFD.appendFileSync = function(path, data, options) {
170-
if (!options) {
171-
options = { encoding: 'utf8', mode: 438 /*=0o666*/, flag: 'a' };
172-
} else if (typeof options === 'string') {
173-
options = { encoding: options, mode: 438 /*=0o666*/, flag: 'a' };
174-
} else if (typeof options !== 'object') {
175-
throwOptionsError(options);
176-
}
166+
options = getOptions(options, { encoding: 'utf8', mode: 438, flag: 'a' });
177167

178168
if (!options.flag)
179169
options = util._extend({ flag: 'a' }, options);

0 commit comments

Comments
 (0)