Skip to content

Commit 4c9abbd

Browse files
ronkorvingrvagg
ronkorving
authored andcommitted
fs: reduced duplicate code in fs.write()
PR-URL: #2947 Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 134a60c commit 4c9abbd

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

lib/fs.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -662,25 +662,21 @@ fs.readSync = function(fd, buffer, offset, length, position) {
662662
// OR
663663
// fs.write(fd, string[, position[, encoding]], callback);
664664
fs.write = function(fd, buffer, offset, length, position, callback) {
665-
function strWrapper(err, written) {
665+
function wrapper(err, written) {
666666
// Retain a reference to buffer so that it can't be GC'ed too soon.
667667
callback(err, written || 0, buffer);
668668
}
669669

670-
function bufWrapper(err, written) {
671-
// retain reference to string in case it's external
672-
callback(err, written || 0, buffer);
673-
}
674-
675670
var req = new FSReqWrap();
671+
req.oncomplete = wrapper;
672+
676673
if (buffer instanceof Buffer) {
677674
// if no position is passed then assume null
678675
if (typeof position === 'function') {
679676
callback = position;
680677
position = null;
681678
}
682679
callback = maybeCallback(callback);
683-
req.oncomplete = strWrapper;
684680
return binding.writeBuffer(fd, buffer, offset, length, position, req);
685681
}
686682

@@ -696,7 +692,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
696692
length = 'utf8';
697693
}
698694
callback = maybeCallback(position);
699-
req.oncomplete = bufWrapper;
700695
return binding.writeString(fd, buffer, offset, length, req);
701696
};
702697

0 commit comments

Comments
 (0)