Skip to content

Commit 9d1b1a3

Browse files
ronagMylesBorins
authored andcommitted
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 1c4f4cc commit 9d1b1a3

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
@@ -270,12 +270,6 @@ Writable.prototype.pipe = function() {
270270

271271
Writable.prototype.write = function(chunk, encoding, cb) {
272272
const state = this._writableState;
273-
const isBuf = !state.objectMode && Stream._isUint8Array(chunk);
274-
275-
// Do not use Object.getPrototypeOf as it is slower since V8 7.3.
276-
if (isBuf && !(chunk instanceof Buffer)) {
277-
chunk = Stream._uint8ArrayToBuffer(chunk);
278-
}
279273

280274
if (typeof encoding === 'function') {
281275
cb = encoding;
@@ -287,34 +281,38 @@ Writable.prototype.write = function(chunk, encoding, cb) {
287281
cb = nop;
288282
}
289283

290-
if (isBuf)
291-
encoding = 'buffer';
292-
293284
let err;
294285
if (state.ending) {
295286
err = new ERR_STREAM_WRITE_AFTER_END();
296287
} else if (state.destroyed) {
297288
err = new ERR_STREAM_DESTROYED('write');
298289
} else if (chunk === null) {
299290
err = new ERR_STREAM_NULL_VALUES();
300-
} else {
301-
if (!isBuf && !state.objectMode) {
302-
if (typeof chunk !== 'string') {
303-
err = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
304-
} else if (encoding !== 'buffer' && state.decodeStrings !== false) {
291+
} else if (!state.objectMode) {
292+
if (typeof chunk === 'string') {
293+
if (state.decodeStrings !== false) {
305294
chunk = Buffer.from(chunk, encoding);
306295
encoding = 'buffer';
307296
}
308-
}
309-
if (err === undefined) {
310-
state.pendingcb++;
311-
return writeOrBuffer(this, state, chunk, encoding, cb);
297+
} else if (chunk instanceof Buffer) {
298+
encoding = 'buffer';
299+
} else if (Stream._isUint8Array(chunk)) {
300+
chunk = Stream._uint8ArrayToBuffer(chunk);
301+
encoding = 'buffer';
302+
} else {
303+
err = new ERR_INVALID_ARG_TYPE(
304+
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
312305
}
313306
}
314307

315-
process.nextTick(cb, err);
316-
errorOrDestroy(this, err, true);
317-
return false;
308+
if (err) {
309+
process.nextTick(cb, err);
310+
errorOrDestroy(this, err, true);
311+
return false;
312+
} else {
313+
state.pendingcb++;
314+
return writeOrBuffer(this, state, chunk, encoding, cb);
315+
}
318316
};
319317

320318
Writable.prototype.cork = function() {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ assert.throws(() => socket.write(null),
2929
code: 'ERR_INVALID_ARG_TYPE',
3030
name: 'TypeError',
3131
message: 'The "chunk" argument must be of type string or an instance of ' +
32-
`Buffer.${common.invalidArgTypeHelper(value)}`
32+
`Buffer or Uint8Array.${common.invalidArgTypeHelper(value)}`
3333
}));
3434
});

0 commit comments

Comments
 (0)