Skip to content

Commit

Permalink
http: code cleanup & dry
Browse files Browse the repository at this point in the history
The first change in `_writeRaw`:
This reduces drops an unnecessary if check (`outputLength`),
because it is redone in `_flushOutput`. It also changes an if/else
statement to an if statement, because the blocks were unrelated.

The second change in `write`:
This consolidates code in #write() that handled different
string encodings and Buffers. There was no reason to handle the
encodings differently, so after splitting them based on Buffer vs
encoding, the code is consolidated. This might see a speedup. Shoutout
to Ron Korving <ron@ronkorving.nl> for spotting this.
  • Loading branch information
brendanashworth committed Dec 14, 2016
1 parent cb96305 commit 6acff8f
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ function _writeRaw(data, encoding, callback) {
connection.writable &&
!connection.destroyed) {
// There might be pending data in the this.output buffer.
var outputLength = this.output.length;
if (outputLength > 0) {
this._flushOutput(connection);
} else if (data.length === 0) {
this._flushOutput(connection);

// Avoid writing empty messages, but trigger the callback.
if (data.length === 0) {
if (typeof callback === 'function')
process.nextTick(callback);
return true;
Expand Down Expand Up @@ -472,25 +472,16 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {

var len, ret;
if (this.chunkedEncoding) {
if (typeof chunk === 'string' &&
encoding !== 'hex' &&
encoding !== 'base64' &&
encoding !== 'latin1') {
if (typeof chunk === 'string') {
len = Buffer.byteLength(chunk, encoding);
chunk = len.toString(16) + CRLF + chunk + CRLF;
ret = this._send(chunk, encoding, callback);
} else {
// buffer, or a non-toString-friendly encoding
if (typeof chunk === 'string')
len = Buffer.byteLength(chunk, encoding);
else
len = chunk.length;

this._send(len.toString(16), 'latin1', null);
this._send(crlf_buf, null, null);
this._send(chunk, encoding, null);
ret = this._send(crlf_buf, null, callback);
len = chunk.length;
}

this._send(len.toString(16), 'latin1', null);
this._send(crlf_buf, null, null);
this._send(chunk, encoding, null);
ret = this._send(crlf_buf, null, callback);
} else {
ret = this._send(chunk, encoding, callback);
}
Expand Down

0 comments on commit 6acff8f

Please sign in to comment.