@@ -267,35 +267,8 @@ Writable.prototype.pipe = function() {
267
267
} ;
268
268
269
269
270
- function writeAfterEnd ( stream , cb ) {
271
- const er = new ERR_STREAM_WRITE_AFTER_END ( ) ;
272
- // TODO: defer error events consistently everywhere, not just the cb
273
- errorOrDestroy ( stream , er ) ;
274
- process . nextTick ( cb , er ) ;
275
- }
276
-
277
- // Checks that a user-supplied chunk is valid, especially for the particular
278
- // mode the stream is in. Currently this means that `null` is never accepted
279
- // and undefined/non-string values are only allowed in object mode.
280
- function validChunk ( stream , state , chunk , cb ) {
281
- var er ;
282
-
283
- if ( chunk === null ) {
284
- er = new ERR_STREAM_NULL_VALUES ( ) ;
285
- } else if ( typeof chunk !== 'string' && ! state . objectMode ) {
286
- er = new ERR_INVALID_ARG_TYPE ( 'chunk' , [ 'string' , 'Buffer' ] , chunk ) ;
287
- }
288
- if ( er ) {
289
- errorOrDestroy ( stream , er ) ;
290
- process . nextTick ( cb , er ) ;
291
- return false ;
292
- }
293
- return true ;
294
- }
295
-
296
270
Writable . prototype . write = function ( chunk , encoding , cb ) {
297
271
const state = this . _writableState ;
298
- var ret = false ;
299
272
const isBuf = ! state . objectMode && Stream . _isUint8Array ( chunk ) ;
300
273
301
274
// Do not use Object.getPrototypeOf as it is slower since V8 7.3.
@@ -305,29 +278,42 @@ Writable.prototype.write = function(chunk, encoding, cb) {
305
278
306
279
if ( typeof encoding === 'function' ) {
307
280
cb = encoding ;
308
- encoding = null ;
281
+ encoding = state . defaultEncoding ;
282
+ } else {
283
+ if ( ! encoding )
284
+ encoding = state . defaultEncoding ;
285
+ if ( typeof cb !== 'function' )
286
+ cb = nop ;
309
287
}
310
288
311
289
if ( isBuf )
312
290
encoding = 'buffer' ;
313
- else if ( ! encoding )
314
- encoding = state . defaultEncoding ;
315
-
316
- if ( typeof cb !== 'function' )
317
- cb = nop ;
318
291
292
+ let err ;
319
293
if ( state . ending ) {
320
- writeAfterEnd ( this , cb ) ;
294
+ err = new ERR_STREAM_WRITE_AFTER_END ( ) ;
321
295
} else if ( state . destroyed ) {
322
- const err = new ERR_STREAM_DESTROYED ( 'write' ) ;
323
- process . nextTick ( cb , err ) ;
324
- errorOrDestroy ( this , err ) ;
325
- } else if ( isBuf || validChunk ( this , state , chunk , cb ) ) {
326
- state . pendingcb ++ ;
327
- ret = writeOrBuffer ( this , state , chunk , encoding , cb ) ;
296
+ err = new ERR_STREAM_DESTROYED ( 'write' ) ;
297
+ } else if ( chunk === null ) {
298
+ err = new ERR_STREAM_NULL_VALUES ( ) ;
299
+ } else {
300
+ if ( ! isBuf && ! state . objectMode ) {
301
+ if ( typeof chunk !== 'string' ) {
302
+ err = new ERR_INVALID_ARG_TYPE ( 'chunk' , [ 'string' , 'Buffer' ] , chunk ) ;
303
+ } else if ( encoding !== 'buffer' && state . decodeStrings !== false ) {
304
+ chunk = Buffer . from ( chunk , encoding ) ;
305
+ encoding = 'buffer' ;
306
+ }
307
+ }
308
+ if ( err === undefined ) {
309
+ state . pendingcb ++ ;
310
+ return writeOrBuffer ( this , state , chunk , encoding , cb ) ;
311
+ }
328
312
}
329
313
330
- return ret ;
314
+ process . nextTick ( cb , err ) ;
315
+ errorOrDestroy ( this , err , true ) ;
316
+ return false ;
331
317
} ;
332
318
333
319
Writable . prototype . cork = function ( ) {
@@ -402,13 +388,6 @@ ObjectDefineProperty(Writable.prototype, 'writableCorked', {
402
388
// in the queue, and wait our turn. Otherwise, call _write
403
389
// If we return false, then we need a drain event, so set that flag.
404
390
function writeOrBuffer ( stream , state , chunk , encoding , cb ) {
405
- if ( ! state . objectMode &&
406
- state . decodeStrings !== false &&
407
- encoding !== 'buffer' &&
408
- typeof chunk === 'string' ) {
409
- chunk = Buffer . from ( chunk , encoding ) ;
410
- encoding = 'buffer' ;
411
- }
412
391
const len = state . objectMode ? 1 : chunk . length ;
413
392
414
393
state . length += len ;
0 commit comments