-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Create less IO errors to improve efficiency of require #8189
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,14 +114,16 @@ function assertEncoding(encoding) { | |
} | ||
} | ||
|
||
function nullCheck(path, callback) { | ||
function nullCheck(path, callback, throwSafe) { | ||
if (('' + path).indexOf('\u0000') !== -1) { | ||
var er = new Error('Path must be a string without null bytes.'); | ||
if (!callback) | ||
throw er; | ||
process.nextTick(function() { | ||
callback(er); | ||
}); | ||
if (!throwSafe) { | ||
var er = new Error('Path must be a string without null bytes.'); | ||
if (!callback) | ||
throw er; | ||
process.nextTick(function() { | ||
callback(er); | ||
}); | ||
} | ||
return false; | ||
} | ||
return true; | ||
|
@@ -162,21 +164,30 @@ fs.Stats.prototype.isSocket = function() { | |
}; | ||
|
||
fs.exists = function(path, callback) { | ||
if (!nullCheck(path, cb)) return; | ||
binding.stat(pathModule._makeLong(path), cb); | ||
function cb(err, stats) { | ||
if (callback) callback(err ? false : true); | ||
if (!nullCheck(path, undefined, true)) { | ||
process.nextTick(function() { | ||
cb(true, false); | ||
}); | ||
return; | ||
} | ||
binding.stat(pathModule._makeLong(path), cb, true); | ||
function cb(err, result) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like that we are creating a closure every time we enter this function. Could we just move the check above and use it right before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid I don't see how to create less closure without copying the body of the |
||
if (callback) { | ||
if (err) { | ||
callback(false); | ||
} else { | ||
callback(!!result); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove extraneous curly brackets in the inner |
||
} | ||
} | ||
}; | ||
|
||
fs.existsSync = function(path) { | ||
try { | ||
nullCheck(path); | ||
binding.stat(pathModule._makeLong(path)); | ||
return true; | ||
} catch (e) { | ||
if (!nullCheck(path, undefined, true)) | ||
return false; | ||
} | ||
if (!binding.stat(pathModule._makeLong(path), undefined, true)) | ||
return false; | ||
return true; | ||
}; | ||
|
||
fs.readFile = function(path, options, callback_) { | ||
|
@@ -675,10 +686,17 @@ fs.lstat = function(path, callback) { | |
binding.lstat(pathModule._makeLong(path), callback); | ||
}; | ||
|
||
fs.stat = function(path, callback) { | ||
fs.stat = function(path, callback, throwSafe) { | ||
callback = makeCallback(callback); | ||
if (!nullCheck(path, callback)) return; | ||
binding.stat(pathModule._makeLong(path), callback); | ||
if (!nullCheck(path, callback, throwSafe)) { | ||
if (throwSafe) { | ||
process.nextTick(function() { | ||
callback(true, false); | ||
}); | ||
} | ||
return; | ||
} | ||
binding.stat(pathModule._makeLong(path), callback, throwSafe); | ||
}; | ||
|
||
fs.fstatSync = function(fd) { | ||
|
@@ -690,9 +708,11 @@ fs.lstatSync = function(path) { | |
return binding.lstat(pathModule._makeLong(path)); | ||
}; | ||
|
||
fs.statSync = function(path) { | ||
nullCheck(path); | ||
return binding.stat(pathModule._makeLong(path)); | ||
fs.statSync = function(path, throwSafe) { | ||
if (!nullCheck(path, undefined, throwSafe)) | ||
return false; | ||
|
||
return binding.stat(pathModule._makeLong(path), undefined, throwSafe); | ||
}; | ||
|
||
fs.readlink = function(path, callback) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you setting
cb
asundefined
?fs.exists()
is async, and if the callback isn't passed then it should throw that the callback wasn't passed.