@@ -261,7 +261,6 @@ Writable.prototype.pipe = function() {
261261
262262Writable . prototype . write = function ( chunk , encoding , cb ) {
263263 const state = this . _writableState ;
264- var ret = false ;
265264 const isBuf = ! state . objectMode && Stream . _isUint8Array ( chunk ) ;
266265
267266 // Do not use Object.getPrototypeOf as it is slower since V8 7.3.
@@ -271,16 +270,16 @@ Writable.prototype.write = function(chunk, encoding, cb) {
271270
272271 if ( typeof encoding === 'function' ) {
273272 cb = encoding ;
274- encoding = null ;
273+ encoding = state . defaultEncoding ;
274+ } else {
275+ if ( ! encoding )
276+ encoding = state . defaultEncoding ;
277+ if ( typeof cb !== 'function' )
278+ cb = nop ;
275279 }
276280
277281 if ( isBuf )
278282 encoding = 'buffer' ;
279- else if ( ! encoding )
280- encoding = state . defaultEncoding ;
281-
282- if ( typeof cb !== 'function' )
283- cb = nop ;
284283
285284 let err ;
286285 if ( state . ending ) {
@@ -289,19 +288,24 @@ Writable.prototype.write = function(chunk, encoding, cb) {
289288 err = new ERR_STREAM_DESTROYED ( 'write' ) ;
290289 } else if ( chunk === null ) {
291290 err = new ERR_STREAM_NULL_VALUES ( ) ;
292- } else if ( ! isBuf && typeof chunk !== 'string' && ! state . objectMode ) {
293- err = new ERR_INVALID_ARG_TYPE ( 'chunk' , [ 'string' , 'Buffer' ] , chunk ) ;
294291 } else {
295- state . pendingcb ++ ;
296- ret = writeOrBuffer ( this , state , chunk , encoding , cb ) ;
297- }
298-
299- if ( err ) {
300- process . nextTick ( cb , err ) ;
301- errorOrDestroy ( this , err , true ) ;
292+ if ( ! isBuf && ! state . objectMode ) {
293+ if ( typeof chunk !== 'string' ) {
294+ err = new ERR_INVALID_ARG_TYPE ( 'chunk' , [ 'string' , 'Buffer' ] , chunk ) ;
295+ } else if ( encoding !== 'buffer' && state . decodeStrings !== false ) {
296+ chunk = Buffer . from ( chunk , encoding ) ;
297+ encoding = 'buffer' ;
298+ }
299+ }
300+ if ( err === undefined ) {
301+ state . pendingcb ++ ;
302+ return writeOrBuffer ( this , state , chunk , encoding , cb ) ;
303+ }
302304 }
303305
304- return ret ;
306+ process . nextTick ( cb , err ) ;
307+ errorOrDestroy ( this , err , true ) ;
308+ return false ;
305309} ;
306310
307311Writable . prototype . cork = function ( ) {
@@ -376,13 +380,6 @@ ObjectDefineProperty(Writable.prototype, 'writableCorked', {
376380// in the queue, and wait our turn. Otherwise, call _write
377381// If we return false, then we need a drain event, so set that flag.
378382function writeOrBuffer ( stream , state , chunk , encoding , cb ) {
379- if ( ! state . objectMode &&
380- state . decodeStrings !== false &&
381- encoding !== 'buffer' &&
382- typeof chunk === 'string' ) {
383- chunk = Buffer . from ( chunk , encoding ) ;
384- encoding = 'buffer' ;
385- }
386383 const len = state . objectMode ? 1 : chunk . length ;
387384
388385 state . length += len ;
0 commit comments