Browse Source

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>
process-exit-stdio-flushing
ronkorving 10 years ago
committed by Trevor Norris
parent
commit
c339fa36f5
  1. 11
      lib/fs.js

11
lib/fs.js

@ -662,17 +662,14 @@ fs.readSync = function(fd, buffer, offset, length, position) {
// OR // OR
// fs.write(fd, string[, position[, encoding]], callback); // fs.write(fd, string[, position[, encoding]], callback);
fs.write = function(fd, buffer, offset, length, position, 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. // Retain a reference to buffer so that it can't be GC'ed too soon.
callback(err, written || 0, buffer); 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(); var req = new FSReqWrap();
req.oncomplete = wrapper;
if (buffer instanceof Buffer) { if (buffer instanceof Buffer) {
// if no position is passed then assume null // if no position is passed then assume null
if (typeof position === 'function') { if (typeof position === 'function') {
@ -680,7 +677,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
position = null; position = null;
} }
callback = maybeCallback(callback); callback = maybeCallback(callback);
req.oncomplete = strWrapper;
return binding.writeBuffer(fd, buffer, offset, length, position, req); return binding.writeBuffer(fd, buffer, offset, length, position, req);
} }
@ -696,7 +692,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
length = 'utf8'; length = 'utf8';
} }
callback = maybeCallback(position); callback = maybeCallback(position);
req.oncomplete = bufWrapper;
return binding.writeString(fd, buffer, offset, length, req); return binding.writeString(fd, buffer, offset, length, req);
}; };

Loading…
Cancel
Save