From abd765892807499103800f6f23e44ef2a5a68c0b Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 13 Dec 2017 07:34:51 +0100 Subject: [PATCH 1/2] stream: remove `undefined` check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `validChunk` allowed `undefined` as a chunk in object mode; however, this was redundant, since: - `validChunk()` is only used by `.write()` - If the `validChunk()` check passes for `undefined`, `.write()` calls `writeOrBuffer()` - If `writeOrBuffer()` does not receive a Buffer, it calls `decodeChunk()` - `decodeChunk()` ignores `undefined` because it checks `typeof chunk === 'string'` - After that call, `chunk.length` is accessed, which throws an error if `chunk` is undefined`. This “fixes” a bug in the sense that `state.pendingcb` is no longer incremented for write attempts that fail like this. --- lib/_stream_writable.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 02481739250635..f5f05486d233a9 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -249,9 +249,7 @@ function validChunk(stream, state, chunk, cb) { if (chunk === null) { er = new errors.TypeError('ERR_STREAM_NULL_VALUES'); - } else if (typeof chunk !== 'string' && - chunk !== undefined && - !state.objectMode) { + } else if (typeof chunk !== 'string' && !state.objectMode) { er = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'chunk', ['string', 'Buffer']); } From 1a7918ca9ec42fd0be63f05b1ffe446a8a7794c7 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 13 Dec 2017 07:39:29 +0100 Subject: [PATCH 2/2] net: remove Socket.prototype.write This is superfluous now that typechecking in `net` and `stream` are aligned. --- lib/net.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lib/net.js b/lib/net.js index f962a848fad662..5562db6ed61e38 100644 --- a/lib/net.js +++ b/lib/net.js @@ -701,17 +701,6 @@ protoGetter('localPort', function localPort() { }); -Socket.prototype.write = function(chunk, encoding, cb) { - if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'chunk', - ['string', 'Buffer'], - chunk); - } - return stream.Duplex.prototype.write.apply(this, arguments); -}; - - Socket.prototype._writeGeneric = function(writev, data, encoding, cb) { // If we are still connecting, then buffer this for later. // The Writable logic will buffer up any more writes while