fs: reduced duplicate code in fs.write()

PR-URL: https://github.com/nodejs/node/pull/2947
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
ronkorving authored and James M Snell committed 2015-10-29 08:38:42 -07:00
1 parent a04408acce
commit 46c8c94055
1 file changed
+3 -8
+3 -8
View File
@@ -614,17 +614,14 @@ fs.readSync = function(fd, buffer, offset, length, position) {
// OR
// fs.write(fd, string[, position[, encoding]], callback);
fs.write = function(fd, buffer, offset, length, position, callback) {
function strWrapper(err, written) {
function wrapper(err, written) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback(err, written || 0, buffer);
}
function bufWrapper(err, written) {
// retain reference to string in case it's external
callback(err, written || 0, buffer);
}
var req = new FSReqWrap();
req.oncomplete = wrapper;
if (buffer instanceof Buffer) {
// if no position is passed then assume null
if (typeof position === 'function') {
@@ -632,7 +629,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
position = null;
}
callback = maybeCallback(callback);
req.oncomplete = strWrapper;
return binding.writeBuffer(fd, buffer, offset, length, position, req);
}
@@ -648,7 +644,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
length = 'utf8';
}
callback = maybeCallback(position);
req.oncomplete = bufWrapper;
return binding.writeString(fd, buffer, offset, length, req);
};