Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
http: code cleanup & dry
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
commit 6acff8f77f63788de4771f2bf93f3f7db2fcdb19
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