Skip to content

Commit f87921d

Browse files
ronagUlisesGascon
authored andcommitted
stream: refactor writable _write
PR-URL: #50198 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 8a89642 commit f87921d

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

‎lib/internal/streams/writable.js‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -443,22 +443,21 @@ Writable.prototype.pipe = function(){
443443
function_write(stream,chunk,encoding,cb){
444444
conststate=stream._writableState;
445445

446-
if(typeofencoding==='function'){
447-
cb=encoding;
448-
encoding=null;
449-
}
450-
451-
if(!encoding)
452-
encoding=(state[kState]&kDefaultUTF8Encoding)!==0 ? 'utf8' : state[kDefaultEncodingValue];
453-
elseif(encoding!=='buffer'&&!Buffer.isEncoding(encoding))
454-
thrownewERR_UNKNOWN_ENCODING(encoding);
455-
456-
if(cb==null||typeofcb!=='function')
446+
if(cb==null||typeofcb!=='function'){
457447
cb=nop;
448+
}
458449

459450
if(chunk===null){
460451
thrownewERR_STREAM_NULL_VALUES();
461-
}elseif((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+
}elseif(encoding!=='buffer'&&!Buffer.isEncoding(encoding)){
458+
thrownewERR_UNKNOWN_ENCODING(encoding);
459+
}
460+
462461
if(typeofchunk==='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
returnerr;
489488
}
489+
490490
state.pendingcb++;
491491
returnwriteOrBuffer(stream,state,chunk,encoding,cb);
492492
}
493493

494494
Writable.prototype.write=function(chunk,encoding,cb){
495+
if(encoding!=null&&typeofencoding==='function'){
496+
cb=encoding;
497+
encoding=null;
498+
}
499+
495500
return_write(this,chunk,encoding,cb)===true;
496501
};
497502

‎test/parallel/test-stream-uint8array.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const GHI = new Uint8Array([0x47, 0x48, 0x49]);
3838
assert(!(chunkinstanceofBuffer));
3939
assert(chunkinstanceofUint8Array);
4040
assert.strictEqual(chunk,ABC);
41-
assert.strictEqual(encoding,'utf8');
41+
assert.strictEqual(encoding,undefined);
4242
cb();
4343
})
4444
});

0 commit comments

Comments
(0)