Skip to content

Commit 6acff8f

Browse files
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 <[email protected]> for spotting this.
1 parent cb96305 commit 6acff8f

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

lib/_http_outgoing.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ function _writeRaw(data, encoding, callback) {
155155
connection.writable &&
156156
!connection.destroyed) {
157157
// There might be pending data in the this.output buffer.
158-
var outputLength = this.output.length;
159-
if (outputLength > 0) {
160-
this._flushOutput(connection);
161-
} else if (data.length === 0) {
158+
this._flushOutput(connection);
159+
160+
// Avoid writing empty messages, but trigger the callback.
161+
if (data.length === 0) {
162162
if (typeof callback === 'function')
163163
process.nextTick(callback);
164164
return true;
@@ -472,25 +472,16 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
472472

473473
var len, ret;
474474
if (this.chunkedEncoding) {
475-
if (typeof chunk === 'string' &&
476-
encoding !== 'hex' &&
477-
encoding !== 'base64' &&
478-
encoding !== 'latin1') {
475+
if (typeof chunk === 'string') {
479476
len = Buffer.byteLength(chunk, encoding);
480-
chunk = len.toString(16) + CRLF + chunk + CRLF;
481-
ret = this._send(chunk, encoding, callback);
482477
} else {
483-
// buffer, or a non-toString-friendly encoding
484-
if (typeof chunk === 'string')
485-
len = Buffer.byteLength(chunk, encoding);
486-
else
487-
len = chunk.length;
488-
489-
this._send(len.toString(16), 'latin1', null);
490-
this._send(crlf_buf, null, null);
491-
this._send(chunk, encoding, null);
492-
ret = this._send(crlf_buf, null, callback);
478+
len = chunk.length;
493479
}
480+
481+
this._send(len.toString(16), 'latin1', null);
482+
this._send(crlf_buf, null, null);
483+
this._send(chunk, encoding, null);
484+
ret = this._send(crlf_buf, null, callback);
494485
} else {
495486
ret = this._send(chunk, encoding, callback);
496487
}

0 commit comments

Comments
 (0)