@@ -443,22 +443,21 @@ Writable.prototype.pipe = function() {
443443function _write(stream, chunk, encoding, cb) {
444444 const state = stream._writableState;
445445
446- if (typeof encoding === 'function') {
447- cb = encoding;
448- encoding = null;
449- }
450-
451- if (!encoding)
452- encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state[kDefaultEncodingValue];
453- else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding))
454- throw new ERR_UNKNOWN_ENCODING(encoding);
455-
456- if (cb == null || typeof cb !== 'function')
446+ if (cb == null || typeof cb !== 'function') {
457447 cb = nop;
448+ }
458449
459450 if (chunk === null) {
460451 throw new ERR_STREAM_NULL_VALUES();
461- } else if ((state[kState] & kObjectMode) === 0) {
452+ }
453+
454+ if ((state[kState] & kObjectMode) === 0) {
455+ if (!encoding) {
456+ encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state.defaultEncoding;
457+ } else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding)) {
458+ throw new ERR_UNKNOWN_ENCODING(encoding);
459+ }
460+
462461 if (typeof chunk === 'string') {
463462 if ((state[kState] & kDecodeStrings) !== 0) {
464463 chunk = Buffer.from(chunk, encoding);
@@ -487,11 +486,17 @@ function _write(stream, chunk, encoding, cb) {
487486 errorOrDestroy(stream, err, true);
488487 return err;
489488 }
489+
490490 state.pendingcb++;
491491 return writeOrBuffer(stream, state, chunk, encoding, cb);
492492}
493493
494494Writable.prototype.write = function(chunk, encoding, cb) {
495+ if (encoding != null && typeof encoding === 'function') {
496+ cb = encoding;
497+ encoding = null;
498+ }
499+
495500 return _write(this, chunk, encoding, cb) === true;
496501};
497502
0 commit comments