From 46c8c94055512dd1a501fa62d2bcbe90e61e2f73 Mon Sep 17 00:00:00 2001 From: ronkorving Date: Fri, 18 Sep 2015 17:36:57 +0900 Subject: [PATCH] fs: reduced duplicate code in fs.write() PR-URL: https://github.com/nodejs/node/pull/2947 Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Trevor Norris --- lib/fs.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index fef4f7ba501b50..cd39cc9d83fcc1 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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); };