Skip to content

Commit

Permalink
zlib: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36347
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
aduh95 authored and targos committed Jun 11, 2021
1 parent 1aae572 commit 32e10f3
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

const {
ArrayBuffer,
ArrayPrototypeMap,
ArrayPrototypePush,
Error,
FunctionPrototypeBind,
MathMax,
NumberIsFinite,
NumberIsNaN,
Expand All @@ -33,7 +36,9 @@ const {
ObjectGetPrototypeOf,
ObjectKeys,
ObjectSetPrototypeOf,
ReflectApply,
Symbol,
TypedArrayPrototypeFill,
Uint32Array,
} = primordials;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -704,31 +710,31 @@ 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);

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);

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);

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);
Expand All @@ -737,23 +743,23 @@ 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);

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);

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);
Expand All @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -818,23 +825,23 @@ 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);

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);

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);
Expand Down

0 comments on commit 32e10f3

Please sign in to comment.