Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: refactor to use more primordials #36347

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -710,31 +716,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 @@ -743,23 +749,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 @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -824,23 +831,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