Skip to content

Commit 0c7ff7f

Browse files
committed
stream: simplify Writable.write
Slightly refactors Writable.write for minor perf and readability improvements. PR-URL: #31146 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent 0875837 commit 0c7ff7f

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

lib/_stream_writable.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,6 @@ Writable.prototype.pipe = function() {
262262

263263
Writable.prototype.write = function(chunk, encoding, cb) {
264264
const state = this._writableState;
265-
const isBuf = !state.objectMode && Stream._isUint8Array(chunk);
266-
267-
// Do not use Object.getPrototypeOf as it is slower since V8 7.3.
268-
if (isBuf && !(chunk instanceof Buffer)) {
269-
chunk = Stream._uint8ArrayToBuffer(chunk);
270-
}
271265

272266
if (typeof encoding === 'function') {
273267
cb = encoding;
@@ -279,34 +273,38 @@ Writable.prototype.write = function(chunk, encoding, cb) {
279273
cb = nop;
280274
}
281275

282-
if (isBuf)
283-
encoding = 'buffer';
284-
285276
let err;
286277
if (state.ending) {
287278
err = new ERR_STREAM_WRITE_AFTER_END();
288279
} else if (state.destroyed) {
289280
err = new ERR_STREAM_DESTROYED('write');
290281
} else if (chunk === null) {
291282
err = new ERR_STREAM_NULL_VALUES();
292-
} else {
293-
if (!isBuf && !state.objectMode) {
294-
if (typeof chunk !== 'string') {
295-
err = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
296-
} else if (encoding !== 'buffer' && state.decodeStrings !== false) {
283+
} else if (!state.objectMode) {
284+
if (typeof chunk === 'string') {
285+
if (state.decodeStrings !== false) {
297286
chunk = Buffer.from(chunk, encoding);
298287
encoding = 'buffer';
299288
}
300-
}
301-
if (err === undefined) {
302-
state.pendingcb++;
303-
return writeOrBuffer(this, state, chunk, encoding, cb);
289+
} else if (chunk instanceof Buffer) {
290+
encoding = 'buffer';
291+
} else if (Stream._isUint8Array(chunk)) {
292+
chunk = Stream._uint8ArrayToBuffer(chunk);
293+
encoding = 'buffer';
294+
} else {
295+
err = new ERR_INVALID_ARG_TYPE(
296+
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
304297
}
305298
}
306299

307-
process.nextTick(cb, err);
308-
errorOrDestroy(this, err, true);
309-
return false;
300+
if (err) {
301+
process.nextTick(cb, err);
302+
errorOrDestroy(this, err, true);
303+
return false;
304+
} else {
305+
state.pendingcb++;
306+
return writeOrBuffer(this, state, chunk, encoding, cb);
307+
}
310308
};
311309

312310
Writable.prototype.cork = function() {

test/parallel/test-net-write-arguments.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ socket.on('error', common.expectsError({
3333
code: 'ERR_INVALID_ARG_TYPE',
3434
name: 'TypeError',
3535
message: 'The "chunk" argument must be of type string or an instance of ' +
36-
`Buffer.${common.invalidArgTypeHelper(value)}`
36+
`Buffer or Uint8Array.${common.invalidArgTypeHelper(value)}`
3737
}));
3838
});

0 commit comments

Comments
 (0)