Skip to content

Commit

Permalink
fs: reduced duplicate code in fs.write()
Browse files Browse the repository at this point in the history
PR-URL: #2947
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
ronkorving authored and trevnorris committed Oct 27, 2015
1 parent 53e64bb commit c339fa3
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,25 +662,21 @@ 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') {
callback = position;
position = null;
}
callback = maybeCallback(callback);
req.oncomplete = strWrapper;
return binding.writeBuffer(fd, buffer, offset, length, position, req);
}

Expand All @@ -696,7 +692,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);
};

Expand Down

0 comments on commit c339fa3

Please sign in to comment.