zlib: refactor to use more primordials

PR-URL: https://github.com/nodejs/node/pull/36347
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Antoine du Hamel authored and Michaël Zasso committed 2021-06-11 09:29:46 +02:00
1 parent 1aae572220
commit 32e10f388c
1 file changed
+26 -19
+26 -19
View File
@@ -23,7 +23,10 @@
const {
ArrayBuffer,
ArrayPrototypeMap,
ArrayPrototypePush,
Error,
FunctionPrototypeBind,
MathMax,
NumberIsFinite,
NumberIsNaN,
@@ -33,7 +36,9 @@ const {
ObjectGetPrototypeOf,
ObjectKeys,
ObjectSetPrototypeOf,
ReflectApply,
Symbol,
TypedArrayPrototypeFill,
Uint32Array,
} = primordials;
@@ -123,7 +128,7 @@ function zlibBufferOnData(chunk) {
if (!this.buffers)
this.buffers = [chunk];
else
this.buffers.push(chunk);
ArrayPrototypePush(this.buffers, chunk);
this.nread += chunk.length;
if (this.nread > this._maxOutputLength) {
this.close();
@@ -267,7 +272,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
}
}
Transform.call(this, { autoDestroy: true, ...opts });
ReflectApply(Transform, this, [{ autoDestroy: true, ...opts }]);
this[kError] = null;
this.bytesWritten = 0;
this._handle = handle;
@@ -452,7 +457,7 @@ function processChunkSync(self, chunk, flushFlag) {
if (!buffers)
buffers = [out];
else
buffers.push(out);
ArrayPrototypePush(buffers, out);
nread += out.byteLength;
if (nread > self._maxOutputLength) {
@@ -665,7 +670,7 @@ function Zlib(opts, mode) {
processCallback,
dictionary);
ZlibBase.call(this, opts, mode, handle, zlibDefaultOpts);
ReflectApply(ZlibBase, this, [opts, mode, handle, zlibDefaultOpts]);
this._level = level;
this._strategy = strategy;
@@ -693,7 +698,8 @@ Zlib.prototype.params = function params(level, strategy, callback) {
if (this._level !== level || this._strategy !== strategy) {
this.flush(Z_SYNC_FLUSH,
paramsAfterFlushCallback.bind(this, level, strategy, callback));
FunctionPrototypeBind(paramsAfterFlushCallback, this,
level, strategy, callback));
} else {
process.nextTick(callback);
}
@@ -704,7 +710,7 @@ Zlib.prototype.params = function params(level, strategy, callback) {
function Deflate(opts) {
if (!(this instanceof Deflate))
return new Deflate(opts);
Zlib.call(this, opts, DEFLATE);
ReflectApply(Zlib, this, [opts, DEFLATE]);
}
ObjectSetPrototypeOf(Deflate.prototype, Zlib.prototype);
ObjectSetPrototypeOf(Deflate, Zlib);
@@ -712,7 +718,7 @@ ObjectSetPrototypeOf(Deflate, Zlib);
function Inflate(opts) {
if (!(this instanceof Inflate))
return new Inflate(opts);
Zlib.call(this, opts, INFLATE);
ReflectApply(Zlib, this, [opts, INFLATE]);
}
ObjectSetPrototypeOf(Inflate.prototype, Zlib.prototype);
ObjectSetPrototypeOf(Inflate, Zlib);
@@ -720,7 +726,7 @@ ObjectSetPrototypeOf(Inflate, Zlib);
function Gzip(opts) {
if (!(this instanceof Gzip))
return new Gzip(opts);
Zlib.call(this, opts, GZIP);
ReflectApply(Zlib, this, [opts, GZIP]);
}
ObjectSetPrototypeOf(Gzip.prototype, Zlib.prototype);
ObjectSetPrototypeOf(Gzip, Zlib);
@@ -728,7 +734,7 @@ ObjectSetPrototypeOf(Gzip, Zlib);
function Gunzip(opts) {
if (!(this instanceof Gunzip))
return new Gunzip(opts);
Zlib.call(this, opts, GUNZIP);
ReflectApply(Zlib, this, [opts, GUNZIP]);
}
ObjectSetPrototypeOf(Gunzip.prototype, Zlib.prototype);
ObjectSetPrototypeOf(Gunzip, Zlib);
@@ -737,7 +743,7 @@ function DeflateRaw(opts) {
if (opts && opts.windowBits === 8) opts.windowBits = 9;
if (!(this instanceof DeflateRaw))
return new DeflateRaw(opts);
Zlib.call(this, opts, DEFLATERAW);
ReflectApply(Zlib, this, [opts, DEFLATERAW]);
}
ObjectSetPrototypeOf(DeflateRaw.prototype, Zlib.prototype);
ObjectSetPrototypeOf(DeflateRaw, Zlib);
@@ -745,7 +751,7 @@ ObjectSetPrototypeOf(DeflateRaw, Zlib);
function InflateRaw(opts) {
if (!(this instanceof InflateRaw))
return new InflateRaw(opts);
Zlib.call(this, opts, INFLATERAW);
ReflectApply(Zlib, this, [opts, INFLATERAW]);
}
ObjectSetPrototypeOf(InflateRaw.prototype, Zlib.prototype);
ObjectSetPrototypeOf(InflateRaw, Zlib);
@@ -753,7 +759,7 @@ ObjectSetPrototypeOf(InflateRaw, Zlib);
function Unzip(opts) {
if (!(this instanceof Unzip))
return new Unzip(opts);
Zlib.call(this, opts, UNZIP);
ReflectApply(Zlib, this, [opts, UNZIP]);
}
ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype);
ObjectSetPrototypeOf(Unzip, Zlib);
@@ -773,9 +779,10 @@ function createConvenienceMethod(ctor, sync) {
};
}
const kMaxBrotliParam = MathMax(...ObjectKeys(constants).map((key) => {
return key.startsWith('BROTLI_PARAM_') ? constants[key] : 0;
}));
const kMaxBrotliParam = MathMax(...ArrayPrototypeMap(
ObjectKeys(constants),
(key) => (key.startsWith('BROTLI_PARAM_') ? constants[key] : 0)
));
const brotliInitParamsArray = new Uint32Array(kMaxBrotliParam + 1);
@@ -787,7 +794,7 @@ const brotliDefaultOpts = {
function Brotli(opts, mode) {
assert(mode === BROTLI_DECODE || mode === BROTLI_ENCODE);
brotliInitParamsArray.fill(-1);
TypedArrayPrototypeFill(brotliInitParamsArray, -1);
if (opts && opts.params) {
for (const origKey of ObjectKeys(opts.params)) {
const key = +origKey;
@@ -818,7 +825,7 @@ function Brotli(opts, mode) {
throw new ERR_ZLIB_INITIALIZATION_FAILED();
}
ZlibBase.call(this, opts, mode, handle, brotliDefaultOpts);
ReflectApply(ZlibBase, this, [opts, mode, handle, brotliDefaultOpts]);
}
ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype);
ObjectSetPrototypeOf(Brotli, Zlib);
@@ -826,7 +833,7 @@ ObjectSetPrototypeOf(Brotli, Zlib);
function BrotliCompress(opts) {
if (!(this instanceof BrotliCompress))
return new BrotliCompress(opts);
Brotli.call(this, opts, BROTLI_ENCODE);
ReflectApply(Brotli, this, [opts, BROTLI_ENCODE]);
}
ObjectSetPrototypeOf(BrotliCompress.prototype, Brotli.prototype);
ObjectSetPrototypeOf(BrotliCompress, Brotli);
@@ -834,7 +841,7 @@ ObjectSetPrototypeOf(BrotliCompress, Brotli);
function BrotliDecompress(opts) {
if (!(this instanceof BrotliDecompress))
return new BrotliDecompress(opts);
Brotli.call(this, opts, BROTLI_DECODE);
ReflectApply(Brotli, this, [opts, BROTLI_DECODE]);
}
ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);
ObjectSetPrototypeOf(BrotliDecompress, Brotli);