◐ Shell
clean mode source ↗

zlib: use modern class syntax for zstd classes · nodejs/node@8e6c191

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

}

856854857-

const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined;

855+

const handle = mode === ZSTD_COMPRESS ?

856+

new binding.ZstdCompress() : new binding.ZstdDecompress();

858857859-

this._writeState = new Uint32Array(2);

860-

handle.init(

861-

initParamsArray,

862-

pledgedSrcSize,

863-

this._writeState,

864-

processCallback,

865-

);

858+

const pledgedSrcSize = opts?.pledgedSrcSize ?? undefined;

866859867-

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-872871873872

const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map(

874873

(key) => (key.startsWith('ZSTD_c_') ?

@@ -878,16 +877,11 @@ const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map(

878877879878

const zstdInitCParamsArray = new Uint32Array(kMaxZstdCParam + 1);

880879881-

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-891885892886

const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map(

893887

(key) => (key.startsWith('ZSTD_d_') ?

@@ -897,16 +891,11 @@ const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map(

897891898892

const zstdInitDParamsArray = new Uint32Array(kMaxZstdDParam + 1);

899893900-

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-910899911900

function createProperty(ctor) {

912901

return {