diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 38e1b6efc9aafd..85e47f692665d5 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3731,6 +3731,9 @@ and [`crypto.setEngine()`][] all depend on this functionality from OpenSSL. -Type: Documentation-only +Type: Runtime Instantiating classes without the `new` qualifier exported by the `node:zlib` module is deprecated. It is recommended to use the `new` qualifier instead. This applies to all Zlib classes, such as `Deflate`, diff --git a/lib/zlib.js b/lib/zlib.js index d79781de481b76..0132a25fb1f8b9 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -686,32 +686,44 @@ Zlib.prototype.params = function params(level, strategy, callback) { // generic zlib // minimal 2-byte header function Deflate(opts) { - if (!(this instanceof Deflate)) + if (!(this instanceof Deflate)) { + process.emitWarning(`Instantiating Deflate class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); 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 (!(this instanceof Inflate)) { + process.emitWarning(`Instantiating Inflate class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); 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 (!(this instanceof Gzip)) { + process.emitWarning(`Instantiating Gzip class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); 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 (!(this instanceof Gunzip)) { + process.emitWarning(`Instantiating Gunzip class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); return new Gunzip(opts); + } ReflectApply(Zlib, this, [opts, GUNZIP]); } ObjectSetPrototypeOf(Gunzip.prototype, Zlib.prototype); @@ -719,24 +731,33 @@ ObjectSetPrototypeOf(Gunzip, Zlib); function DeflateRaw(opts) { if (opts && opts.windowBits === 8) opts.windowBits = 9; - if (!(this instanceof DeflateRaw)) + if (!(this instanceof DeflateRaw)) { + process.emitWarning(`Instantiating DeflateRaw class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); 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 (!(this instanceof InflateRaw)) { + process.emitWarning(`Instantiating InflateRaw class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); 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 (!(this instanceof Unzip)) { + process.emitWarning(`Instantiating Unzip class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); return new Unzip(opts); + } ReflectApply(Zlib, this, [opts, UNZIP]); } ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype); @@ -801,16 +822,22 @@ ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype); ObjectSetPrototypeOf(Brotli, Zlib); function BrotliCompress(opts) { - if (!(this instanceof BrotliCompress)) + if (!(this instanceof BrotliCompress)) { + process.emitWarning(`Instantiating BrotliCompress class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); 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)) + if (!(this instanceof BrotliDecompress)) { + process.emitWarning(`Instantiating BrotliDecompress class without 'new' is deprecated.`, + 'DeprecationWarning', 'DEP0184'); return new BrotliDecompress(opts); + } ReflectApply(Brotli, this, [opts, BROTLI_DECODE]); } ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);