Skip to content

Commit 4b4caad

Browse files
aduh95danielleadams
authored andcommitted
zlib: refactor to use more primordials
PR-URL: #36347 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent cc18907 commit 4b4caad

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

‎lib/zlib.js‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323

2424
const{
2525
ArrayBuffer,
26+
ArrayPrototypeMap,
27+
ArrayPrototypePush,
2628
Error,
29+
FunctionPrototypeBind,
2730
MathMax,
2831
NumberIsFinite,
2932
NumberIsNaN,
@@ -33,7 +36,9 @@ const{
3336
ObjectGetPrototypeOf,
3437
ObjectKeys,
3538
ObjectSetPrototypeOf,
39+
ReflectApply,
3640
Symbol,
41+
TypedArrayPrototypeFill,
3742
Uint32Array,
3843
}=primordials;
3944

@@ -124,7 +129,7 @@ function zlibBufferOnData(chunk){
124129
if(!this.buffers)
125130
this.buffers=[chunk];
126131
else
127-
this.buffers.push(chunk);
132+
ArrayPrototypePush(this.buffers,chunk);
128133
this.nread+=chunk.length;
129134
if(this.nread>this._maxOutputLength){
130135
this.close();
@@ -268,7 +273,7 @@ function ZlibBase(opts, mode, handle,{flush, finishFlush, fullFlush }){
268273
}
269274
}
270275

271-
Transform.call(this,{autoDestroy: true, ...opts});
276+
ReflectApply(Transform,this,[{autoDestroy: true, ...opts}]);
272277
this[kError]=null;
273278
this.bytesWritten=0;
274279
this._handle=handle;
@@ -458,7 +463,7 @@ function processChunkSync(self, chunk, flushFlag){
458463
if(!buffers)
459464
buffers=[out];
460465
else
461-
buffers.push(out);
466+
ArrayPrototypePush(buffers,out);
462467
nread+=out.byteLength;
463468

464469
if(nread>self._maxOutputLength){
@@ -671,7 +676,7 @@ function Zlib(opts, mode){
671676
processCallback,
672677
dictionary);
673678

674-
ZlibBase.call(this,opts,mode,handle,zlibDefaultOpts);
679+
ReflectApply(ZlibBase,this,[opts,mode,handle,zlibDefaultOpts]);
675680

676681
this._level=level;
677682
this._strategy=strategy;
@@ -699,7 +704,8 @@ Zlib.prototype.params = function params(level, strategy, callback){
699704

700705
if(this._level!==level||this._strategy!==strategy){
701706
this.flush(Z_SYNC_FLUSH,
702-
paramsAfterFlushCallback.bind(this,level,strategy,callback));
707+
FunctionPrototypeBind(paramsAfterFlushCallback,this,
708+
level,strategy,callback));
703709
}else{
704710
process.nextTick(callback);
705711
}
@@ -710,31 +716,31 @@ Zlib.prototype.params = function params(level, strategy, callback){
710716
functionDeflate(opts){
711717
if(!(thisinstanceofDeflate))
712718
returnnewDeflate(opts);
713-
Zlib.call(this,opts,DEFLATE);
719+
ReflectApply(Zlib,this,[opts,DEFLATE]);
714720
}
715721
ObjectSetPrototypeOf(Deflate.prototype,Zlib.prototype);
716722
ObjectSetPrototypeOf(Deflate,Zlib);
717723

718724
functionInflate(opts){
719725
if(!(thisinstanceofInflate))
720726
returnnewInflate(opts);
721-
Zlib.call(this,opts,INFLATE);
727+
ReflectApply(Zlib,this,[opts,INFLATE]);
722728
}
723729
ObjectSetPrototypeOf(Inflate.prototype,Zlib.prototype);
724730
ObjectSetPrototypeOf(Inflate,Zlib);
725731

726732
functionGzip(opts){
727733
if(!(thisinstanceofGzip))
728734
returnnewGzip(opts);
729-
Zlib.call(this,opts,GZIP);
735+
ReflectApply(Zlib,this,[opts,GZIP]);
730736
}
731737
ObjectSetPrototypeOf(Gzip.prototype,Zlib.prototype);
732738
ObjectSetPrototypeOf(Gzip,Zlib);
733739

734740
functionGunzip(opts){
735741
if(!(thisinstanceofGunzip))
736742
returnnewGunzip(opts);
737-
Zlib.call(this,opts,GUNZIP);
743+
ReflectApply(Zlib,this,[opts,GUNZIP]);
738744
}
739745
ObjectSetPrototypeOf(Gunzip.prototype,Zlib.prototype);
740746
ObjectSetPrototypeOf(Gunzip,Zlib);
@@ -743,23 +749,23 @@ function DeflateRaw(opts){
743749
if(opts&&opts.windowBits===8)opts.windowBits=9;
744750
if(!(thisinstanceofDeflateRaw))
745751
returnnewDeflateRaw(opts);
746-
Zlib.call(this,opts,DEFLATERAW);
752+
ReflectApply(Zlib,this,[opts,DEFLATERAW]);
747753
}
748754
ObjectSetPrototypeOf(DeflateRaw.prototype,Zlib.prototype);
749755
ObjectSetPrototypeOf(DeflateRaw,Zlib);
750756

751757
functionInflateRaw(opts){
752758
if(!(thisinstanceofInflateRaw))
753759
returnnewInflateRaw(opts);
754-
Zlib.call(this,opts,INFLATERAW);
760+
ReflectApply(Zlib,this,[opts,INFLATERAW]);
755761
}
756762
ObjectSetPrototypeOf(InflateRaw.prototype,Zlib.prototype);
757763
ObjectSetPrototypeOf(InflateRaw,Zlib);
758764

759765
functionUnzip(opts){
760766
if(!(thisinstanceofUnzip))
761767
returnnewUnzip(opts);
762-
Zlib.call(this,opts,UNZIP);
768+
ReflectApply(Zlib,this,[opts,UNZIP]);
763769
}
764770
ObjectSetPrototypeOf(Unzip.prototype,Zlib.prototype);
765771
ObjectSetPrototypeOf(Unzip,Zlib);
@@ -779,9 +785,10 @@ function createConvenienceMethod(ctor, sync){
779785
};
780786
}
781787

782-
constkMaxBrotliParam=MathMax(...ObjectKeys(constants).map((key)=>{
783-
returnkey.startsWith('BROTLI_PARAM_') ? constants[key] : 0;
784-
}));
788+
constkMaxBrotliParam=MathMax(...ArrayPrototypeMap(
789+
ObjectKeys(constants),
790+
(key)=>(key.startsWith('BROTLI_PARAM_') ? constants[key] : 0)
791+
));
785792

786793
constbrotliInitParamsArray=newUint32Array(kMaxBrotliParam+1);
787794

@@ -793,7 +800,7 @@ const brotliDefaultOpts ={
793800
functionBrotli(opts,mode){
794801
assert(mode===BROTLI_DECODE||mode===BROTLI_ENCODE);
795802

796-
brotliInitParamsArray.fill(-1);
803+
TypedArrayPrototypeFill(brotliInitParamsArray,-1);
797804
if(opts&&opts.params){
798805
for(constorigKeyofObjectKeys(opts.params)){
799806
constkey=+origKey;
@@ -824,23 +831,23 @@ function Brotli(opts, mode){
824831
thrownewERR_ZLIB_INITIALIZATION_FAILED();
825832
}
826833

827-
ZlibBase.call(this,opts,mode,handle,brotliDefaultOpts);
834+
ReflectApply(ZlibBase,this,[opts,mode,handle,brotliDefaultOpts]);
828835
}
829836
ObjectSetPrototypeOf(Brotli.prototype,Zlib.prototype);
830837
ObjectSetPrototypeOf(Brotli,Zlib);
831838

832839
functionBrotliCompress(opts){
833840
if(!(thisinstanceofBrotliCompress))
834841
returnnewBrotliCompress(opts);
835-
Brotli.call(this,opts,BROTLI_ENCODE);
842+
ReflectApply(Brotli,this,[opts,BROTLI_ENCODE]);
836843
}
837844
ObjectSetPrototypeOf(BrotliCompress.prototype,Brotli.prototype);
838845
ObjectSetPrototypeOf(BrotliCompress,Brotli);
839846

840847
functionBrotliDecompress(opts){
841848
if(!(thisinstanceofBrotliDecompress))
842849
returnnewBrotliDecompress(opts);
843-
Brotli.call(this,opts,BROTLI_DECODE);
850+
ReflectApply(Brotli,this,[opts,BROTLI_DECODE]);
844851
}
845852
ObjectSetPrototypeOf(BrotliDecompress.prototype,Brotli.prototype);
846853
ObjectSetPrototypeOf(BrotliDecompress,Brotli);

0 commit comments

Comments
(0)