@ -35,6 +35,10 @@ const isWindows = process.platform === 'win32';
const DEBUG = process . env . NODE_DEBUG && /fs/ . test ( process . env . NODE_DEBUG ) ;
const errnoException = util . _ errnoException ;
function throwOptionsError ( options ) {
throw new TypeError ( 'Expected options to be either an object or a string, ' +
'but got ' + typeof options + ' instead' ) ;
}
function rethrow ( ) {
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
@ -226,12 +230,13 @@ fs.existsSync = function(path) {
fs . readFile = function ( path , options , callback_ ) {
var callback = maybeCallback ( arguments [ arguments . length - 1 ] ) ;
if ( ! options || typeof options === 'function' )
if ( ! options || typeof options === 'function' ) {
options = { encoding : null , flag : 'r' } ;
else if ( typeof options === 'string' )
} else if ( typeof options === 'string' ) {
options = { encoding : options , flag : 'r' } ;
else if ( typeof options !== 'object' )
throw new TypeError ( 'Bad arguments' ) ;
} else if ( typeof options !== 'object' ) {
throwOptionsError ( options ) ;
}
var encoding = options . encoding ;
assertEncoding ( encoding ) ;
@ -389,7 +394,7 @@ fs.readFileSync = function(path, options) {
} else if ( typeof options === 'string' ) {
options = { encoding : options , flag : 'r' } ;
} else if ( typeof options !== 'object' ) {
throw new TypeError ( 'Bad arguments' ) ;
throwOptionsError ( options ) ;
}
var encoding = options . encoding ;
@ -1119,7 +1124,7 @@ fs.writeFile = function(path, data, options, callback) {
} else if ( typeof options === 'string' ) {
options = { encoding : options , mode : 0o666 , flag : 'w' } ;
} else if ( typeof options !== 'object' ) {
throw new TypeError ( 'Bad arguments' ) ;
throwOptionsError ( options ) ;
}
assertEncoding ( options . encoding ) ;
@ -1143,7 +1148,7 @@ fs.writeFileSync = function(path, data, options) {
} else if ( typeof options === 'string' ) {
options = { encoding : options , mode : 0o666 , flag : 'w' } ;
} else if ( typeof options !== 'object' ) {
throw new TypeError ( 'Bad arguments' ) ;
throwOptionsError ( options ) ;
}
assertEncoding ( options . encoding ) ;
@ -1178,7 +1183,7 @@ fs.appendFile = function(path, data, options, callback_) {
} else if ( typeof options === 'string' ) {
options = { encoding : options , mode : 0o666 , flag : 'a' } ;
} else if ( typeof options !== 'object' ) {
throw new TypeError ( 'Bad arguments' ) ;
throwOptionsError ( options ) ;
}
if ( ! options . flag )
@ -1192,7 +1197,7 @@ fs.appendFileSync = function(path, data, options) {
} else if ( typeof options === 'string' ) {
options = { encoding : options , mode : 0o666 , flag : 'a' } ;
} else if ( typeof options !== 'object' ) {
throw new TypeError ( 'Bad arguments' ) ;
throwOptionsError ( options ) ;
}
if ( ! options . flag )
options = util . _ extend ( { flag : 'a' } , options ) ;