mirror of https://github.com/nodejs/node.git
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>pull/36347/head
parent
3604fadacf
commit
e4d570fe9a
45
lib/zlib.js
45
lib/zlib.js
|
@ -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;
|
||||
|
||||
|
@ -124,7 +129,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();
|
||||
|
@ -268,7 +273,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;
|
||||
|
@ -458,7 +463,7 @@ function processChunkSync(self, chunk, flushFlag) {
|
|||
if (!buffers)
|
||||
buffers = [out];
|
||||
else
|
||||
buffers.push(out);
|
||||
ArrayPrototypePush(buffers, out);
|
||||
nread += out.byteLength;
|
||||
|
||||
if (nread > self._maxOutputLength) {
|
||||
|
@ -671,7 +676,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;
|
||||
|
@ -699,7 +704,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);
|
||||
}
|
||||
|
@ -710,7 +716,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);
|
||||
|
@ -718,7 +724,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);
|
||||
|
@ -726,7 +732,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);
|
||||
|
@ -734,7 +740,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);
|
||||
|
@ -743,7 +749,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);
|
||||
|
@ -751,7 +757,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);
|
||||
|
@ -759,7 +765,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);
|
||||
|
@ -779,9 +785,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);
|
||||
|
||||
|
@ -793,7 +800,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;
|
||||
|
@ -824,7 +831,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);
|
||||
|
@ -832,7 +839,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);
|
||||
|
@ -840,7 +847,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);
|
||||
|
|
Loading…
Reference in New Issue