Skip to content

Commit e74e917

Browse files
committed
zlib: use new.target to check for instantiation
1 parent 2f0b371 commit e74e917

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

lib/zlib.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -706,57 +706,64 @@ Zlib.prototype.params = function params(level, strategy, callback) {
706706
// generic zlib
707707
// minimal 2-byte header
708708
function Deflate(opts) {
709-
if (!(this instanceof Deflate))
709+
if (!new.target) {
710710
return new Deflate(opts);
711+
}
711712
ReflectApply(Zlib, this, [opts, DEFLATE]);
712713
}
713714
ObjectSetPrototypeOf(Deflate.prototype, Zlib.prototype);
714715
ObjectSetPrototypeOf(Deflate, Zlib);
715716

716717
function Inflate(opts) {
717-
if (!(this instanceof Inflate))
718+
if (!new.target) {
718719
return new Inflate(opts);
720+
}
719721
ReflectApply(Zlib, this, [opts, INFLATE]);
720722
}
721723
ObjectSetPrototypeOf(Inflate.prototype, Zlib.prototype);
722724
ObjectSetPrototypeOf(Inflate, Zlib);
723725

724726
function Gzip(opts) {
725-
if (!(this instanceof Gzip))
727+
if (!new.target) {
726728
return new Gzip(opts);
729+
}
727730
ReflectApply(Zlib, this, [opts, GZIP]);
728731
}
729732
ObjectSetPrototypeOf(Gzip.prototype, Zlib.prototype);
730733
ObjectSetPrototypeOf(Gzip, Zlib);
731734

732735
function Gunzip(opts) {
733-
if (!(this instanceof Gunzip))
736+
if (!new.target) {
734737
return new Gunzip(opts);
738+
}
735739
ReflectApply(Zlib, this, [opts, GUNZIP]);
736740
}
737741
ObjectSetPrototypeOf(Gunzip.prototype, Zlib.prototype);
738742
ObjectSetPrototypeOf(Gunzip, Zlib);
739743

740744
function DeflateRaw(opts) {
741-
if (opts && opts.windowBits === 8) opts.windowBits = 9;
742-
if (!(this instanceof DeflateRaw))
745+
if (opts?.windowBits === 8) opts.windowBits = 9;
746+
if (!new.target) {
743747
return new DeflateRaw(opts);
748+
}
744749
ReflectApply(Zlib, this, [opts, DEFLATERAW]);
745750
}
746751
ObjectSetPrototypeOf(DeflateRaw.prototype, Zlib.prototype);
747752
ObjectSetPrototypeOf(DeflateRaw, Zlib);
748753

749754
function InflateRaw(opts) {
750-
if (!(this instanceof InflateRaw))
755+
if (!new.target) {
751756
return new InflateRaw(opts);
757+
}
752758
ReflectApply(Zlib, this, [opts, INFLATERAW]);
753759
}
754760
ObjectSetPrototypeOf(InflateRaw.prototype, Zlib.prototype);
755761
ObjectSetPrototypeOf(InflateRaw, Zlib);
756762

757763
function Unzip(opts) {
758-
if (!(this instanceof Unzip))
764+
if (!new.target) {
759765
return new Unzip(opts);
766+
}
760767
ReflectApply(Zlib, this, [opts, UNZIP]);
761768
}
762769
ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype);
@@ -831,16 +838,18 @@ ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype);
831838
ObjectSetPrototypeOf(Brotli, Zlib);
832839

833840
function BrotliCompress(opts) {
834-
if (!(this instanceof BrotliCompress))
841+
if (!new.target) {
835842
return new BrotliCompress(opts);
843+
}
836844
ReflectApply(Brotli, this, [opts, BROTLI_ENCODE]);
837845
}
838846
ObjectSetPrototypeOf(BrotliCompress.prototype, Brotli.prototype);
839847
ObjectSetPrototypeOf(BrotliCompress, Brotli);
840848

841849
function BrotliDecompress(opts) {
842-
if (!(this instanceof BrotliDecompress))
850+
if (!new.target) {
843851
return new BrotliDecompress(opts);
852+
}
844853
ReflectApply(Brotli, this, [opts, BROTLI_DECODE]);
845854
}
846855
ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);

test/parallel/test-zlib-deflate-raw-inherits.js

-27
This file was deleted.

0 commit comments

Comments
 (0)