Skip to content

Commit 59bebd4

Browse files
anonrigRafaelGSS
authored andcommitted
zlib: use modern class syntax for zstd classes
PR-URL: #56965 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent f6c7e1c commit 59bebd4

File tree

3 files changed

+44
-55
lines changed

3 files changed

+44
-55
lines changed

lib/zlib.js

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -851,45 +851,44 @@ const zstdDefaultOpts = {
851851
finishFlush: ZSTD_e_end,
852852
fullFlush: ZSTD_e_flush,
853853
};
854-
function Zstd(opts, mode, initParamsArray, maxParam) {
855-
assert(mode === ZSTD_COMPRESS || mode === ZSTD_DECOMPRESS);
856-
857-
initParamsArray.fill(-1);
858-
if (opts?.params) {
859-
ObjectKeys(opts.params).forEach((origKey) => {
860-
const key = +origKey;
861-
if (NumberIsNaN(key) || key < 0 || key > maxParam ||
862-
(initParamsArray[key] | 0) !== -1) {
863-
throw new ERR_ZSTD_INVALID_PARAM(origKey);
864-
}
865-
866-
const value = opts.params[origKey];
867-
if (typeof value !== 'number' && typeof value !== 'boolean') {
868-
throw new ERR_INVALID_ARG_TYPE('options.params[key]',
869-
'number', opts.params[origKey]);
870-
}
871-
initParamsArray[key] = value;
872-
});
873-
}
854+
class Zstd extends ZlibBase {
855+
constructor(opts, mode, initParamsArray, maxParam) {
856+
assert(mode === ZSTD_COMPRESS || mode === ZSTD_DECOMPRESS);
857+
858+
initParamsArray.fill(-1);
859+
if (opts?.params) {
860+
ObjectKeys(opts.params).forEach((origKey) => {
861+
const key = +origKey;
862+
if (NumberIsNaN(key) || key < 0 || key > maxParam ||
863+
(initParamsArray[key] | 0) !== -1) {
864+
throw new ERR_ZSTD_INVALID_PARAM(origKey);
865+
}
866+
867+
const value = opts.params[origKey];
868+
if (typeof value !== 'number' && typeof value !== 'boolean') {
869+
throw new ERR_INVALID_ARG_TYPE('options.params[key]',
870+
'number', opts.params[origKey]);
871+
}
872+
initParamsArray[key] = value;
873+
});
874+
}
874875

875-
const handle = mode === ZSTD_COMPRESS ?
876-
new binding.ZstdCompress() : new binding.ZstdDecompress();
876+
const handle = mode === ZSTD_COMPRESS ?
877+
new binding.ZstdCompress() : new binding.ZstdDecompress();
877878

878-
const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined;
879+
const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined;
879880

880-
this._writeState = new Uint32Array(2);
881-
handle.init(
882-
initParamsArray,
883-
pledgedSrcSize,
884-
this._writeState,
885-
processCallback,
886-
);
887-
888-
ReflectApply(ZlibBase, this, [opts, mode, handle, zstdDefaultOpts]);
881+
const writeState = new Uint32Array(2);
882+
handle.init(
883+
initParamsArray,
884+
pledgedSrcSize,
885+
writeState,
886+
processCallback,
887+
);
888+
super(opts, mode, handle, zstdDefaultOpts);
889+
this._writeState = writeState;
890+
}
889891
}
890-
ObjectSetPrototypeOf(Zstd.prototype, ZlibBase.prototype);
891-
ObjectSetPrototypeOf(Zstd, ZlibBase);
892-
893892

894893
const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map(
895894
(key) => (key.startsWith('ZSTD_c_') ?
@@ -899,16 +898,11 @@ const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map(
899898

900899
const zstdInitCParamsArray = new Uint32Array(kMaxZstdCParam + 1);
901900

902-
function ZstdCompress(opts) {
903-
if (!(this instanceof ZstdCompress))
904-
return new ZstdCompress(opts);
905-
906-
ReflectApply(Zstd, this,
907-
[opts, ZSTD_COMPRESS, zstdInitCParamsArray, kMaxZstdCParam]);
901+
class ZstdCompress extends Zstd {
902+
constructor(opts) {
903+
super(opts, ZSTD_COMPRESS, zstdInitCParamsArray, kMaxZstdCParam);
904+
}
908905
}
909-
ObjectSetPrototypeOf(ZstdCompress.prototype, Zstd.prototype);
910-
ObjectSetPrototypeOf(ZstdCompress, Zstd);
911-
912906

913907
const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map(
914908
(key) => (key.startsWith('ZSTD_d_') ?
@@ -918,16 +912,11 @@ const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map(
918912

919913
const zstdInitDParamsArray = new Uint32Array(kMaxZstdDParam + 1);
920914

921-
function ZstdDecompress(opts) {
922-
if (!(this instanceof ZstdDecompress))
923-
return new ZstdDecompress(opts);
924-
925-
ReflectApply(Zstd, this,
926-
[opts, ZSTD_DECOMPRESS, zstdInitDParamsArray, kMaxZstdDParam]);
915+
class ZstdDecompress extends Zstd {
916+
constructor(opts) {
917+
super(opts, ZSTD_DECOMPRESS, zstdInitDParamsArray, kMaxZstdDParam);
918+
}
927919
}
928-
ObjectSetPrototypeOf(ZstdDecompress.prototype, Zstd.prototype);
929-
ObjectSetPrototypeOf(ZstdDecompress, Zstd);
930-
931920

932921
function createProperty(ctor) {
933922
return {

test/parallel/test-zlib-invalid-input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const unzips = [
4040
zlib.Inflate(),
4141
zlib.InflateRaw(),
4242
zlib.BrotliDecompress(),
43-
zlib.ZstdDecompress(),
43+
new zlib.ZstdDecompress(),
4444
];
4545

4646
nonStringInputs.forEach(common.mustCall((input) => {

test/parallel/test-zlib-zero-byte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test('zlib should properly handle zero byte input', async () => {
3636

3737
for (const [Compressor, expected] of compressors) {
3838
const { promise, resolve, reject } = Promise.withResolvers();
39-
const gz = Compressor();
39+
const gz = new Compressor();
4040
const emptyBuffer = Buffer.alloc(0);
4141
let received = 0;
4242
gz.on('data', function(c) {

0 commit comments

Comments
 (0)