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

Commit

Permalink
fs: refactor "options" processing as a function
Browse files Browse the repository at this point in the history
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 <benjamingr@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Nicu Micleușanu <micnic90@gmail.com>
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
thefourtheye authored and kevinoid committed Dec 13, 2016
1 parent 8387613 commit 0455848
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions fs-file-sync-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,24 @@ var util = require('util');

var fsFileSyncFD = {};

function throwOptionsError(options) {
throw new TypeError('Expected options to be either an object or a string, ' +
'but got ' + typeof options + ' instead');
function getOptions(options, defaultOptions) {
if (options === null || options === undefined ||
typeof options === 'function') {
return defaultOptions;
}

if (typeof options === 'string') {
defaultOptions = util._extend({}, defaultOptions);
defaultOptions.encoding = options;
options = defaultOptions;
} else if (typeof options !== 'object') {
throw new TypeError('"options" must be a string or an object, got ' +
typeof options + ' instead.');
}

if (options.encoding !== 'buffer')
assertEncoding(options.encoding);
return options;
}

function assertEncoding(encoding) {
Expand Down Expand Up @@ -70,20 +85,9 @@ function tryReadSync(fd, isUserFd, buffer, pos, len) {
}

fsFileSyncFD.readFileSync = function(path, options) {
if (!options) {
options = { encoding: null, flag: 'r' };
} else if (typeof options === 'string') {
options = { encoding: options, flag: 'r' };
} else if (typeof options !== 'object') {
throwOptionsError(options);
}

var encoding = options.encoding;
assertEncoding(encoding);

var flag = options.flag || 'r';
options = getOptions(options, { flag: 'r' });
var isUserFd = isFd(path); // file descriptor ownership
var fd = isUserFd ? path : fs.openSync(path, flag, 438 /*=0o666*/);
var fd = isUserFd ? path : fs.openSync(path, options.flag || 'r', 438);

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

if (encoding) buffer = buffer.toString(encoding);
if (options.encoding) buffer = buffer.toString(options.encoding);
return buffer;
};

fsFileSyncFD.writeFileSync = function(path, data, options) {
if (!options) {
options = { encoding: 'utf8', mode: 438 /*=0o666*/, flag: 'w' };
} else if (typeof options === 'string') {
options = { encoding: options, mode: 438 /*=0o666*/, flag: 'w' };
} else if (typeof options !== 'object') {
throwOptionsError(options);
}

assertEncoding(options.encoding);

options = getOptions(options, { encoding: 'utf8', mode: 438, flag: 'w' });
var flag = options.flag || 'w';

var isUserFd = isFd(path); // file descriptor ownership
var fd = isUserFd ? path : fs.openSync(path, flag, options.mode);

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

fsFileSyncFD.appendFileSync = function(path, data, options) {
if (!options) {
options = { encoding: 'utf8', mode: 438 /*=0o666*/, flag: 'a' };
} else if (typeof options === 'string') {
options = { encoding: options, mode: 438 /*=0o666*/, flag: 'a' };
} else if (typeof options !== 'object') {
throwOptionsError(options);
}
options = getOptions(options, { encoding: 'utf8', mode: 438, flag: 'a' });

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

0 comments on commit 0455848

Please sign in to comment.