Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: simplify Writable.write #31146

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,6 @@ Writable.prototype.pipe = function() {

Writable.prototype.write = function(chunk, encoding, cb) {
const state = this._writableState;
const isBuf = !state.objectMode && Stream._isUint8Array(chunk);
ronag marked this conversation as resolved.
Show resolved Hide resolved

// 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;
Expand All @@ -278,34 +272,38 @@ 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();
} else if (state.destroyed) {
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() {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-net-write-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ socket.on('error', common.expectsError({
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)}`
}));
});