From 9d1b1a3fbdc5956659413cc540c8d050a81ced22 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 31 Dec 2019 12:43:54 +0100 Subject: [PATCH] stream: simplify Writable.write Slightly refactors Writable.write for minor perf and readability improvements. PR-URL: https://github.com/nodejs/node/pull/31146 Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: David Carlier --- lib/_stream_writable.js | 40 +++++++++++------------ test/parallel/test-net-write-arguments.js | 2 +- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 8e9fbe70388c9e..b1dca886bcffe0 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -270,12 +270,6 @@ Writable.prototype.pipe = function() { Writable.prototype.write = function(chunk, encoding, cb) { const state = this._writableState; - const isBuf = !state.objectMode && Stream._isUint8Array(chunk); - - // Do not use Object.getPrototypeOf as it is slower since V8 7.3. - if (isBuf && !(chunk instanceof Buffer)) { - chunk = Stream._uint8ArrayToBuffer(chunk); - } if (typeof encoding === 'function') { cb = encoding; @@ -287,9 +281,6 @@ Writable.prototype.write = function(chunk, encoding, cb) { cb = nop; } - if (isBuf) - encoding = 'buffer'; - let err; if (state.ending) { err = new ERR_STREAM_WRITE_AFTER_END(); @@ -297,24 +288,31 @@ Writable.prototype.write = function(chunk, encoding, cb) { err = new ERR_STREAM_DESTROYED('write'); } else if (chunk === null) { err = new ERR_STREAM_NULL_VALUES(); - } else { - if (!isBuf && !state.objectMode) { - if (typeof chunk !== 'string') { - err = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk); - } else if (encoding !== 'buffer' && state.decodeStrings !== false) { + } else if (!state.objectMode) { + if (typeof chunk === 'string') { + if (state.decodeStrings !== false) { chunk = Buffer.from(chunk, encoding); encoding = 'buffer'; } - } - if (err === undefined) { - state.pendingcb++; - return writeOrBuffer(this, state, chunk, encoding, cb); + } else if (chunk instanceof Buffer) { + encoding = 'buffer'; + } else if (Stream._isUint8Array(chunk)) { + chunk = Stream._uint8ArrayToBuffer(chunk); + encoding = 'buffer'; + } else { + err = new ERR_INVALID_ARG_TYPE( + 'chunk', ['string', 'Buffer', 'Uint8Array'], chunk); } } - process.nextTick(cb, err); - errorOrDestroy(this, err, true); - return false; + if (err) { + process.nextTick(cb, err); + errorOrDestroy(this, err, true); + return false; + } else { + state.pendingcb++; + return writeOrBuffer(this, state, chunk, encoding, cb); + } }; Writable.prototype.cork = function() { diff --git a/test/parallel/test-net-write-arguments.js b/test/parallel/test-net-write-arguments.js index d3dde36b02f852..0e9e30b41da286 100644 --- a/test/parallel/test-net-write-arguments.js +++ b/test/parallel/test-net-write-arguments.js @@ -29,6 +29,6 @@ assert.throws(() => socket.write(null), code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "chunk" argument must be of type string or an instance of ' + - `Buffer.${common.invalidArgTypeHelper(value)}` + `Buffer or Uint8Array.${common.invalidArgTypeHelper(value)}` })); });