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: refactor writable _write #50198

Merged
merged 1 commit into from
Oct 24, 2023
Merged
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
29 changes: 17 additions & 12 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,22 +443,21 @@ Writable.prototype.pipe = function() {
function _write(stream, chunk, encoding, cb) {
const state = stream._writableState;

if (typeof encoding === 'function') {
cb = encoding;
encoding = null;
}

if (!encoding)
encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state[kDefaultEncodingValue];
else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding))
throw new ERR_UNKNOWN_ENCODING(encoding);

if (cb == null || typeof cb !== 'function')
if (cb == null || typeof cb !== 'function') {
cb = nop;
}

if (chunk === null) {
throw new ERR_STREAM_NULL_VALUES();
} else if ((state[kState] & kObjectMode) === 0) {
}

if ((state[kState] & kObjectMode) === 0) {
if (!encoding) {
encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state.defaultEncoding;
} else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding)) {
throw new ERR_UNKNOWN_ENCODING(encoding);
}

if (typeof chunk === 'string') {
if ((state[kState] & kDecodeStrings) !== 0) {
chunk = Buffer.from(chunk, encoding);
Expand Down Expand Up @@ -487,11 +486,17 @@ function _write(stream, chunk, encoding, cb) {
errorOrDestroy(stream, err, true);
return err;
}

state.pendingcb++;
return writeOrBuffer(stream, state, chunk, encoding, cb);
}

Writable.prototype.write = function(chunk, encoding, cb) {
if (encoding != null && typeof encoding === 'function') {
cb = encoding;
encoding = null;
}

return _write(this, chunk, encoding, cb) === true;
};

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-uint8array.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const GHI = new Uint8Array([0x47, 0x48, 0x49]);
assert(!(chunk instanceof Buffer));
assert(chunk instanceof Uint8Array);
assert.strictEqual(chunk, ABC);
assert.strictEqual(encoding, 'utf8');
assert.strictEqual(encoding, undefined);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina wdyt?

cb();
})
});
Expand Down