diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index b8f8b72ce8d113..19814ea8d3d730 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -445,16 +445,17 @@ function _write(stream, chunk, encoding, cb) { if (typeof encoding === 'function') { cb = encoding; - encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state.defaultEncoding; - } else { - 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 cb !== 'function') - cb = nop; + 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') + cb = nop; + if (chunk === null) { throw new ERR_STREAM_NULL_VALUES(); } else if ((state[kState] & kObjectMode) === 0) {