From b2f437cb302b8dfb42bf470ba857d15f4d1035d9 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 8 Feb 2025 14:31:57 -0500 Subject: [PATCH] zlib: use modern class syntax for zstd classes --- lib/zlib.js | 95 +++++++++++------------- test/parallel/test-zlib-invalid-input.js | 2 +- test/parallel/test-zlib-zero-byte.js | 2 +- 3 files changed, 44 insertions(+), 55 deletions(-) diff --git a/lib/zlib.js b/lib/zlib.js index 536c7e559f1e2e..058e03c7180620 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -842,45 +842,44 @@ const zstdDefaultOpts = { finishFlush: ZSTD_e_end, fullFlush: ZSTD_e_flush, }; -function Zstd(opts, mode, initParamsArray, maxParam) { - assert(mode === ZSTD_COMPRESS || mode === ZSTD_DECOMPRESS); - - initParamsArray.fill(-1); - if (opts?.params) { - ObjectKeys(opts.params).forEach((origKey) => { - const key = +origKey; - if (NumberIsNaN(key) || key < 0 || key > maxParam || - (initParamsArray[key] | 0) !== -1) { - throw new ERR_ZSTD_INVALID_PARAM(origKey); - } - - const value = opts.params[origKey]; - if (typeof value !== 'number' && typeof value !== 'boolean') { - throw new ERR_INVALID_ARG_TYPE('options.params[key]', - 'number', opts.params[origKey]); - } - initParamsArray[key] = value; - }); - } +class Zstd extends ZlibBase { + constructor(opts, mode, initParamsArray, maxParam) { + assert(mode === ZSTD_COMPRESS || mode === ZSTD_DECOMPRESS); + + initParamsArray.fill(-1); + if (opts?.params) { + ObjectKeys(opts.params).forEach((origKey) => { + const key = +origKey; + if (NumberIsNaN(key) || key < 0 || key > maxParam || + (initParamsArray[key] | 0) !== -1) { + throw new ERR_ZSTD_INVALID_PARAM(origKey); + } + + const value = opts.params[origKey]; + if (typeof value !== 'number' && typeof value !== 'boolean') { + throw new ERR_INVALID_ARG_TYPE('options.params[key]', + 'number', opts.params[origKey]); + } + initParamsArray[key] = value; + }); + } - const handle = mode === ZSTD_COMPRESS ? - new binding.ZstdCompress() : new binding.ZstdDecompress(); + const handle = mode === ZSTD_COMPRESS ? + new binding.ZstdCompress() : new binding.ZstdDecompress(); - const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined; + const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined; - this._writeState = new Uint32Array(2); - handle.init( - initParamsArray, - pledgedSrcSize, - this._writeState, - processCallback, - ); - - ReflectApply(ZlibBase, this, [opts, mode, handle, zstdDefaultOpts]); + const writeState = new Uint32Array(2); + handle.init( + initParamsArray, + pledgedSrcSize, + writeState, + processCallback, + ); + super(opts, mode, handle, zstdDefaultOpts); + this._writeState = writeState; + } } -ObjectSetPrototypeOf(Zstd.prototype, ZlibBase.prototype); -ObjectSetPrototypeOf(Zstd, ZlibBase); - const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map( (key) => (key.startsWith('ZSTD_c_') ? @@ -890,16 +889,11 @@ const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map( const zstdInitCParamsArray = new Uint32Array(kMaxZstdCParam + 1); -function ZstdCompress(opts) { - if (!(this instanceof ZstdCompress)) - return new ZstdCompress(opts); - - ReflectApply(Zstd, this, - [opts, ZSTD_COMPRESS, zstdInitCParamsArray, kMaxZstdCParam]); +class ZstdCompress extends Zstd { + constructor(opts) { + super(opts, ZSTD_COMPRESS, zstdInitCParamsArray, kMaxZstdCParam); + } } -ObjectSetPrototypeOf(ZstdCompress.prototype, Zstd.prototype); -ObjectSetPrototypeOf(ZstdCompress, Zstd); - const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map( (key) => (key.startsWith('ZSTD_d_') ? @@ -909,16 +903,11 @@ const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map( const zstdInitDParamsArray = new Uint32Array(kMaxZstdDParam + 1); -function ZstdDecompress(opts) { - if (!(this instanceof ZstdDecompress)) - return new ZstdDecompress(opts); - - ReflectApply(Zstd, this, - [opts, ZSTD_DECOMPRESS, zstdInitDParamsArray, kMaxZstdDParam]); +class ZstdDecompress extends Zstd { + constructor(opts) { + super(opts, ZSTD_DECOMPRESS, zstdInitDParamsArray, kMaxZstdDParam); + } } -ObjectSetPrototypeOf(ZstdDecompress.prototype, Zstd.prototype); -ObjectSetPrototypeOf(ZstdDecompress, Zstd); - function createProperty(ctor) { return { diff --git a/test/parallel/test-zlib-invalid-input.js b/test/parallel/test-zlib-invalid-input.js index 7aea0efa06dbcf..3ec7a15e0bb9ad 100644 --- a/test/parallel/test-zlib-invalid-input.js +++ b/test/parallel/test-zlib-invalid-input.js @@ -40,7 +40,7 @@ const unzips = [ zlib.Inflate(), zlib.InflateRaw(), zlib.BrotliDecompress(), - zlib.ZstdDecompress(), + new zlib.ZstdDecompress(), ]; nonStringInputs.forEach(common.mustCall((input) => { diff --git a/test/parallel/test-zlib-zero-byte.js b/test/parallel/test-zlib-zero-byte.js index f75e1d05e9995d..a6120d4b92ed29 100644 --- a/test/parallel/test-zlib-zero-byte.js +++ b/test/parallel/test-zlib-zero-byte.js @@ -36,7 +36,7 @@ test('zlib should properly handle zero byte input', async () => { for (const [Compressor, expected] of compressors) { const { promise, resolve, reject } = Promise.withResolvers(); - const gz = Compressor(); + const gz = new Compressor(); const emptyBuffer = Buffer.alloc(0); let received = 0; gz.on('data', function(c) {