From ddec44481fd744641f608ffeddaf336ceadbeb34 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 1 Sep 2024 12:14:04 -0400 Subject: [PATCH] zlib: use new.target to check for instantiation --- lib/zlib.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/zlib.js b/lib/zlib.js index a2c092f1037261..0f700de07a03c9 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -706,57 +706,64 @@ Zlib.prototype.params = function params(level, strategy, callback) { // generic zlib // minimal 2-byte header function Deflate(opts) { - if (!(this instanceof Deflate)) + if (!new.target) { return new Deflate(opts); + } ReflectApply(Zlib, this, [opts, DEFLATE]); } ObjectSetPrototypeOf(Deflate.prototype, Zlib.prototype); ObjectSetPrototypeOf(Deflate, Zlib); function Inflate(opts) { - if (!(this instanceof Inflate)) + if (!new.target) { return new Inflate(opts); + } ReflectApply(Zlib, this, [opts, INFLATE]); } ObjectSetPrototypeOf(Inflate.prototype, Zlib.prototype); ObjectSetPrototypeOf(Inflate, Zlib); function Gzip(opts) { - if (!(this instanceof Gzip)) + if (!new.target) { return new Gzip(opts); + } ReflectApply(Zlib, this, [opts, GZIP]); } ObjectSetPrototypeOf(Gzip.prototype, Zlib.prototype); ObjectSetPrototypeOf(Gzip, Zlib); function Gunzip(opts) { - if (!(this instanceof Gunzip)) + if (!new.target) { return new Gunzip(opts); + } ReflectApply(Zlib, this, [opts, GUNZIP]); } ObjectSetPrototypeOf(Gunzip.prototype, Zlib.prototype); ObjectSetPrototypeOf(Gunzip, Zlib); function DeflateRaw(opts) { - if (opts && opts.windowBits === 8) opts.windowBits = 9; - if (!(this instanceof DeflateRaw)) + if (opts?.windowBits === 8) opts.windowBits = 9; + if (!new.target) { return new DeflateRaw(opts); + } ReflectApply(Zlib, this, [opts, DEFLATERAW]); } ObjectSetPrototypeOf(DeflateRaw.prototype, Zlib.prototype); ObjectSetPrototypeOf(DeflateRaw, Zlib); function InflateRaw(opts) { - if (!(this instanceof InflateRaw)) + if (!new.target) { return new InflateRaw(opts); + } ReflectApply(Zlib, this, [opts, INFLATERAW]); } ObjectSetPrototypeOf(InflateRaw.prototype, Zlib.prototype); ObjectSetPrototypeOf(InflateRaw, Zlib); function Unzip(opts) { - if (!(this instanceof Unzip)) + if (!new.target) { return new Unzip(opts); + } ReflectApply(Zlib, this, [opts, UNZIP]); } ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype); @@ -831,16 +838,18 @@ ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype); ObjectSetPrototypeOf(Brotli, Zlib); function BrotliCompress(opts) { - if (!(this instanceof BrotliCompress)) + if (!new.target) { return new BrotliCompress(opts); + } ReflectApply(Brotli, this, [opts, BROTLI_ENCODE]); } ObjectSetPrototypeOf(BrotliCompress.prototype, Brotli.prototype); ObjectSetPrototypeOf(BrotliCompress, Brotli); function BrotliDecompress(opts) { - if (!(this instanceof BrotliDecompress)) - return new BrotliDecompress(opts); + if (!new.target) { + return new BrotliCompress(opts); + } ReflectApply(Brotli, this, [opts, BROTLI_DECODE]); } ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);