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 <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 a3ca7f3 commit 8e6c191

3 files changed

Lines changed: 44 additions & 55 deletions

File tree

‎lib/zlib.js‎

Lines changed: 42 additions & 53 deletions
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-
functionZstd(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-
constkey=+origKey;
840-
if(NumberIsNaN(key)||key<0||key>maxParam||
841-
(initParamsArray[key]|0)!==-1){
842-
thrownewERR_ZSTD_INVALID_PARAM(origKey);
843-
}
844-
845-
constvalue=opts.params[origKey];
846-
if(typeofvalue!=='number'&&typeofvalue!=='boolean'){
847-
thrownewERR_INVALID_ARG_TYPE('options.params[key]',
848-
'number',opts.params[origKey]);
849-
}
850-
initParamsArray[key]=value;
851-
});
852-
}
853-
854-
consthandle=mode===ZSTD_COMPRESS ?
855-
newbinding.ZstdCompress() : newbinding.ZstdDecompress();
833+
classZstdextendsZlibBase{
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+
constkey=+origKey;
841+
if(NumberIsNaN(key)||key<0||key>maxParam||
842+
(initParamsArray[key]|0)!==-1){
843+
thrownewERR_ZSTD_INVALID_PARAM(origKey);
844+
}
845+
846+
constvalue=opts.params[origKey];
847+
if(typeofvalue!=='number'&&typeofvalue!=='boolean'){
848+
thrownewERR_INVALID_ARG_TYPE('options.params[key]',
849+
'number',opts.params[origKey]);
850+
}
851+
initParamsArray[key]=value;
852+
});
853+
}
856854

857-
constpledgedSrcSize=opts?.pledgedSrcSize??undefined;
855+
consthandle=mode===ZSTD_COMPRESS ?
856+
newbinding.ZstdCompress() : newbinding.ZstdDecompress();
858857

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

867-
ReflectApply(ZlibBase,this,[opts,mode,handle,zstdDefaultOpts]);
860+
constwriteState=newUint32Array(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
constkMaxZstdCParam=MathMax(...ObjectKeys(constants).map(
874873
(key)=>(key.startsWith('ZSTD_c_') ?
@@ -878,16 +877,11 @@ const kMaxZstdCParam = MathMax(...ObjectKeys(constants).map(
878877

879878
constzstdInitCParamsArray=newUint32Array(kMaxZstdCParam+1);
880879

881-
functionZstdCompress(opts){
882-
if(!(thisinstanceofZstdCompress))
883-
returnnewZstdCompress(opts);
884-
885-
ReflectApply(Zstd,this,
886-
[opts,ZSTD_COMPRESS,zstdInitCParamsArray,kMaxZstdCParam]);
880+
classZstdCompressextendsZstd{
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
constkMaxZstdDParam=MathMax(...ObjectKeys(constants).map(
893887
(key)=>(key.startsWith('ZSTD_d_') ?
@@ -897,16 +891,11 @@ const kMaxZstdDParam = MathMax(...ObjectKeys(constants).map(
897891

898892
constzstdInitDParamsArray=newUint32Array(kMaxZstdDParam+1);
899893

900-
functionZstdDecompress(opts){
901-
if(!(thisinstanceofZstdDecompress))
902-
returnnewZstdDecompress(opts);
903-
904-
ReflectApply(Zstd,this,
905-
[opts,ZSTD_DECOMPRESS,zstdInitDParamsArray,kMaxZstdDParam]);
894+
classZstdDecompressextendsZstd{
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
functioncreateProperty(ctor){
912901
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+
newzlib.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]ofcompressors){
3838
const{ promise, resolve, reject }=Promise.withResolvers();
39-
constgz=Compressor();
39+
constgz=newCompressor();
4040
constemptyBuffer=Buffer.alloc(0);
4141
letreceived=0;
4242
gz.on('data',function(c){

0 commit comments

Comments
 (0)