From aec2f733f97460ddde677b53bcc048b55878c7b3 Mon Sep 17 00:00:00 2001 From: isaacs Date: Thu, 27 Dec 2012 11:39:57 -0800 Subject: [PATCH] net: Move createWriteReq to separate function --- lib/net.js | 61 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/lib/net.js b/lib/net.js index 5df47a0..4a5fbe5 100644 --- a/lib/net.js +++ b/lib/net.js @@ -534,21 +534,16 @@ Socket.prototype.write = function(chunk, encoding, cb) { Socket.prototype._write = function(dataEncoding, cb) { - assert(Array.isArray(dataEncoding)); + // assert(Array.isArray(dataEncoding)); var data = dataEncoding[0]; var encoding = dataEncoding[1] || 'utf8'; - if (this !== process.stderr && this !== process.stdout) - debug('Socket._write'); - // If we are still connecting, then buffer this for later. // The Writable logic will buffer up any more writes while // waiting for this one to be done. if (this._connecting) { - debug('_write: waiting for connection'); this._pendingWrite = dataEncoding; this.once('connect', function() { - debug('_write: connected now, try again'); this._write(dataEncoding, cb); }); return; @@ -558,39 +553,12 @@ Socket.prototype._write = function(dataEncoding, cb) { timers.active(this); if (!this._handle) { - debug('already destroyed'); this._destroy(new Error('This socket is closed.'), cb); return false; } - var writeReq; - - if (Buffer.isBuffer(data)) { - writeReq = this._handle.writeBuffer(data); - - } else { - switch (encoding) { - case 'utf8': - case 'utf-8': - writeReq = this._handle.writeUtf8String(data); - break; - - case 'ascii': - writeReq = this._handle.writeAsciiString(data); - break; - - case 'ucs2': - case 'ucs-2': - case 'utf16le': - case 'utf-16le': - writeReq = this._handle.writeUcs2String(data); - break; - - default: - writeReq = this._handle.writeBuffer(new Buffer(data, encoding)); - break; - } - } + var enc = Buffer.isBuffer(data) ? 'buffer' : encoding; + var writeReq = createWriteReq(this._handle, data, enc); if (!writeReq || typeof writeReq !== 'object') return this._destroy(errnoException(errno, 'write'), cb); @@ -606,6 +574,29 @@ Socket.prototype._write = function(dataEncoding, cb) { writeReq.cb = cb; }; +function createWriteReq(handle, data, encoding) { + switch (encoding) { + case 'buffer': + return handle.writeBuffer(data); + + case 'utf8': + case 'utf-8': + return handle.writeUtf8String(data); + + case 'ascii': + return handle.writeAsciiString(data); + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return handle.writeUcs2String(data); + + default: + return handle.writeBuffer(new Buffer(data, encoding)); + } +} + Socket.prototype.__defineGetter__('bytesWritten', function() { var bytes = this._bytesDispatched, -- 2.7.4