Skip to content

Commit 32e10f3

Browse files
aduh95targos
authored andcommitted
zlib: refactor to use more primordials
PR-URL: #36347 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 1aae572 commit 32e10f3

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

lib/zlib.js

+26-19
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

@@ -123,7 +128,7 @@ function zlibBufferOnData(chunk) {
123128
if (!this.buffers)
124129
this.buffers = [chunk];
125130
else
126-
this.buffers.push(chunk);
131+
ArrayPrototypePush(this.buffers, chunk);
127132
this.nread += chunk.length;
128133
if (this.nread > this._maxOutputLength) {
129134
this.close();
@@ -267,7 +272,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
267272
}
268273
}
269274

270-
Transform.call(this, { autoDestroy: true, ...opts });
275+
ReflectApply(Transform, this, [{ autoDestroy: true, ...opts }]);
271276
this[kError] = null;
272277
this.bytesWritten = 0;
273278
this._handle = handle;
@@ -452,7 +457,7 @@ function processChunkSync(self, chunk, flushFlag) {
452457
if (!buffers)
453458
buffers = [out];
454459
else
455-
buffers.push(out);
460+
ArrayPrototypePush(buffers, out);
456461
nread += out.byteLength;
457462

458463
if (nread > self._maxOutputLength) {
@@ -665,7 +670,7 @@ function Zlib(opts, mode) {
665670
processCallback,
666671
dictionary);
667672

668-
ZlibBase.call(this, opts, mode, handle, zlibDefaultOpts);
673+
ReflectApply(ZlibBase, this, [opts, mode, handle, zlibDefaultOpts]);
669674

670675
this._level = level;
671676
this._strategy = strategy;
@@ -693,7 +698,8 @@ Zlib.prototype.params = function params(level, strategy, callback) {
693698

694699
if (this._level !== level || this._strategy !== strategy) {
695700
this.flush(Z_SYNC_FLUSH,
696-
paramsAfterFlushCallback.bind(this, level, strategy, callback));
701+
FunctionPrototypeBind(paramsAfterFlushCallback, this,
702+
level, strategy, callback));
697703
} else {
698704
process.nextTick(callback);
699705
}
@@ -704,31 +710,31 @@ Zlib.prototype.params = function params(level, strategy, callback) {
704710
function Deflate(opts) {
705711
if (!(this instanceof Deflate))
706712
return new Deflate(opts);
707-
Zlib.call(this, opts, DEFLATE);
713+
ReflectApply(Zlib, this, [opts, DEFLATE]);
708714
}
709715
ObjectSetPrototypeOf(Deflate.prototype, Zlib.prototype);
710716
ObjectSetPrototypeOf(Deflate, Zlib);
711717

712718
function Inflate(opts) {
713719
if (!(this instanceof Inflate))
714720
return new Inflate(opts);
715-
Zlib.call(this, opts, INFLATE);
721+
ReflectApply(Zlib, this, [opts, INFLATE]);
716722
}
717723
ObjectSetPrototypeOf(Inflate.prototype, Zlib.prototype);
718724
ObjectSetPrototypeOf(Inflate, Zlib);
719725

720726
function Gzip(opts) {
721727
if (!(this instanceof Gzip))
722728
return new Gzip(opts);
723-
Zlib.call(this, opts, GZIP);
729+
ReflectApply(Zlib, this, [opts, GZIP]);
724730
}
725731
ObjectSetPrototypeOf(Gzip.prototype, Zlib.prototype);
726732
ObjectSetPrototypeOf(Gzip, Zlib);
727733

728734
function Gunzip(opts) {
729735
if (!(this instanceof Gunzip))
730736
return new Gunzip(opts);
731-
Zlib.call(this, opts, GUNZIP);
737+
ReflectApply(Zlib, this, [opts, GUNZIP]);
732738
}
733739
ObjectSetPrototypeOf(Gunzip.prototype, Zlib.prototype);
734740
ObjectSetPrototypeOf(Gunzip, Zlib);
@@ -737,23 +743,23 @@ function DeflateRaw(opts) {
737743
if (opts && opts.windowBits === 8) opts.windowBits = 9;
738744
if (!(this instanceof DeflateRaw))
739745
return new DeflateRaw(opts);
740-
Zlib.call(this, opts, DEFLATERAW);
746+
ReflectApply(Zlib, this, [opts, DEFLATERAW]);
741747
}
742748
ObjectSetPrototypeOf(DeflateRaw.prototype, Zlib.prototype);
743749
ObjectSetPrototypeOf(DeflateRaw, Zlib);
744750

745751
function InflateRaw(opts) {
746752
if (!(this instanceof InflateRaw))
747753
return new InflateRaw(opts);
748-
Zlib.call(this, opts, INFLATERAW);
754+
ReflectApply(Zlib, this, [opts, INFLATERAW]);
749755
}
750756
ObjectSetPrototypeOf(InflateRaw.prototype, Zlib.prototype);
751757
ObjectSetPrototypeOf(InflateRaw, Zlib);
752758

753759
function Unzip(opts) {
754760
if (!(this instanceof Unzip))
755761
return new Unzip(opts);
756-
Zlib.call(this, opts, UNZIP);
762+
ReflectApply(Zlib, this, [opts, UNZIP]);
757763
}
758764
ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype);
759765
ObjectSetPrototypeOf(Unzip, Zlib);
@@ -773,9 +779,10 @@ function createConvenienceMethod(ctor, sync) {
773779
};
774780
}
775781

776-
const kMaxBrotliParam = MathMax(...ObjectKeys(constants).map((key) => {
777-
return key.startsWith('BROTLI_PARAM_') ? constants[key] : 0;
778-
}));
782+
const kMaxBrotliParam = MathMax(...ArrayPrototypeMap(
783+
ObjectKeys(constants),
784+
(key) => (key.startsWith('BROTLI_PARAM_') ? constants[key] : 0)
785+
));
779786

780787
const brotliInitParamsArray = new Uint32Array(kMaxBrotliParam + 1);
781788

@@ -787,7 +794,7 @@ const brotliDefaultOpts = {
787794
function Brotli(opts, mode) {
788795
assert(mode === BROTLI_DECODE || mode === BROTLI_ENCODE);
789796

790-
brotliInitParamsArray.fill(-1);
797+
TypedArrayPrototypeFill(brotliInitParamsArray, -1);
791798
if (opts && opts.params) {
792799
for (const origKey of ObjectKeys(opts.params)) {
793800
const key = +origKey;
@@ -818,23 +825,23 @@ function Brotli(opts, mode) {
818825
throw new ERR_ZLIB_INITIALIZATION_FAILED();
819826
}
820827

821-
ZlibBase.call(this, opts, mode, handle, brotliDefaultOpts);
828+
ReflectApply(ZlibBase, this, [opts, mode, handle, brotliDefaultOpts]);
822829
}
823830
ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype);
824831
ObjectSetPrototypeOf(Brotli, Zlib);
825832

826833
function BrotliCompress(opts) {
827834
if (!(this instanceof BrotliCompress))
828835
return new BrotliCompress(opts);
829-
Brotli.call(this, opts, BROTLI_ENCODE);
836+
ReflectApply(Brotli, this, [opts, BROTLI_ENCODE]);
830837
}
831838
ObjectSetPrototypeOf(BrotliCompress.prototype, Brotli.prototype);
832839
ObjectSetPrototypeOf(BrotliCompress, Brotli);
833840

834841
function BrotliDecompress(opts) {
835842
if (!(this instanceof BrotliDecompress))
836843
return new BrotliDecompress(opts);
837-
Brotli.call(this, opts, BROTLI_DECODE);
844+
ReflectApply(Brotli, this, [opts, BROTLI_DECODE]);
838845
}
839846
ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);
840847
ObjectSetPrototypeOf(BrotliDecompress, Brotli);

0 commit comments

Comments
 (0)