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:
parent
53e64bb29e
commit
c339fa36f5
11
lib/fs.js
11
lib/fs.js
@ -662,17 +662,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') {
|
||||
@ -680,7 +677,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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user