diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 7ac93bd3d8f498..f097bd4c8413e4 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -876,16 +876,15 @@ function write_(msg, chunk, encoding, callback, fromEnd) { return false; } - let len = msg.strictContentLength ? - typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength : null; + let len; - if (len != null) { - if ( - strictContentLength(msg) && - (fromEnd ? msg[kBytesWritten] + len !== msg._contentLength : msg[kBytesWritten] + len > msg._contentLength) - ) { + if (strictContentLength(msg)) { + len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength; + + if (fromEnd ? msg[kBytesWritten] + len !== msg._contentLength : msg[kBytesWritten] + len > msg._contentLength) { throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(len + msg[kBytesWritten], msg._contentLength); } + msg[kBytesWritten] += len; }