Skip to content

Commit 8e6c191

Browse files
anonrigtargos
authored andcommitted
zlib: use modern class syntax for zstd classes
PR-URL: #56965 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent a3ca7f3 commit 8e6c191

File tree

3 files changed

+44
-55
lines changed

3 files changed

+44
-55
lines changed

lib/zlib.js

+42-53
Original file line numberDiff line numberDiff line change
@@ -830,45 +830,44 @@ const zstdDefaultOpts = {
830830
finishFlush: ZSTD_e_end,
831831
fullFlush: ZSTD_e_flush,
832832
};
833-
function Zstd(opts, mode, initParamsArray, maxParam) {
834-
assert(mode === ZSTD_COMPRESS || mode === ZSTD_DECOMPRESS);
835-
836-
initParamsArray.fill(-1);
837-
if (opts?.params) {
838-
ObjectKeys(opts.params).forEach((origKey) => {
839-
const key = +origKey;
840-
if (NumberIsNaN(key) || key < 0 || key > maxParam ||
841-
(initParamsArray[key] | 0) !== -1) {
842-
throw new ERR_ZSTD_INVALID_PARAM(origKey);
843-
}
844-
845-
const value = opts.params[origKey];
846-
if (typeof value !== 'number' && typeof value !== 'boolean') {
847-
throw new ERR_INVALID_ARG_TYPE('options.params[key]',
848-
'number', opts.params[origKey]);
849-
}
850-
initParamsArray[key] = value;
851-
});
852-
}
853-
854-
const handle = mode === ZSTD_COMPRESS ?
855-
new binding.ZstdCompress() : new binding.ZstdDecompress();
833+
class Zstd extends ZlibBase {
834+
constructor(opts, mode, initParamsArray, maxParam) {
835+
assert(mode === ZSTD_COMPRESS || mode === ZSTD_DECOMPRESS);
836+
837+
initParamsArray.fill(-1);
838+
if (opts?.params) {
839+
ObjectKeys(opts.params).forEach((origKey) => {
840+
const key = +origKey;
841+
if (NumberIsNaN(key) || key < 0 || key > maxParam ||
842+
(initParamsArray[key] | 0) !== -1) {
843+
throw new ERR_ZSTD_INVALID_PARAM(origKey);
844+
}
845+
846+
const value = opts.params[origKey];
847+
if (typeof value !== 'number' && typeof value !== 'boolean') {
848+
throw new ERR_INVALID_ARG_TYPE('options.params[key]',
849+
'number', opts.params[origKey]);
850+
}
851+
initParamsArray[key] = value;
852+
});
853+
}
856854

857-
const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined;
855+
const handle = mode === ZSTD_COMPRESS ?
856+
new binding.ZstdCompress() : new binding.ZstdDecompress();
858857

859-
this._writeState = new Uint32Array(2);
860-
handle.init(
861-
initParamsArray,
862-
pledgedSrcSize,
863-
this._writeState,
864-
processCallback,
865-
);
858+
const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined;
866859

867-
ReflectApply(ZlibBase, this, [opts, mode, handle, zstdDefaultOpts]);
860+
const writeState = new Uint32Array(2);
861+
handle.init(
862+
initParamsArray,
863+
pledgedSrcSize,
864+
writeState,
865+
processCallback,
866+
);
867+
super(opts, mode, handle, zstdDefaultOpts);
868+
this._writeState = writeState;
869+
}
868870
}
869-
ObjectSetPrototypeOf(Zstd.prototype, ZlibBase.prototype);
870-
ObjectSetPrototypeOf(Zstd, ZlibBase);
871-
872871

873872
const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map(
874873
(key) => (key.startsWith('ZSTD_c_') ?
@@ -878,16 +877,11 @@ const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map(
878877

879878
const zstdInitCParamsArray = new Uint32Array(kMaxZstdCParam + 1);
880879

881-
function ZstdCompress(opts) {
882-
if (!(this instanceof ZstdCompress))
883-
return new ZstdCompress(opts);
884-
885-
ReflectApply(Zstd, this,
886-
[opts, ZSTD_COMPRESS, zstdInitCParamsArray, kMaxZstdCParam]);
880+
class ZstdCompress extends Zstd {
881+
constructor(opts) {
882+
super(opts, ZSTD_COMPRESS, zstdInitCParamsArray, kMaxZstdCParam);
883+
}
887884
}
888-
ObjectSetPrototypeOf(ZstdCompress.prototype, Zstd.prototype);
889-
ObjectSetPrototypeOf(ZstdCompress, Zstd);
890-
891885

892886
const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map(
893887
(key) => (key.startsWith('ZSTD_d_') ?
@@ -897,16 +891,11 @@ const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map(
897891

898892
const zstdInitDParamsArray = new Uint32Array(kMaxZstdDParam + 1);
899893

900-
function ZstdDecompress(opts) {
901-
if (!(this instanceof ZstdDecompress))
902-
return new ZstdDecompress(opts);
903-
904-
ReflectApply(Zstd, this,
905-
[opts, ZSTD_DECOMPRESS, zstdInitDParamsArray, kMaxZstdDParam]);
894+
class ZstdDecompress extends Zstd {
895+
constructor(opts) {
896+
super(opts, ZSTD_DECOMPRESS, zstdInitDParamsArray, kMaxZstdDParam);
897+
}
906898
}
907-
ObjectSetPrototypeOf(ZstdDecompress.prototype, Zstd.prototype);
908-
ObjectSetPrototypeOf(ZstdDecompress, Zstd);
909-
910899

911900
function createProperty(ctor) {
912901
return {

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

+1-1
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

+1-1
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)