Skip to content

Commit f77caf7

Browse files
committed
zlib: deprecate classes usage without new
1 parent bdc2662 commit f77caf7

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3731,14 +3731,17 @@ and [`crypto.setEngine()`][] all depend on this functionality from OpenSSL.
37313731

37323732
<!-- YAML
37333733
changes:
3734+
- version: REPLACEME
3735+
pr-url: https://github.com/nodejs/node/pull/XXXXX
3736+
description: Runtime deprecation.
37343737
- version:
37353738
- v22.9.0
37363739
- v20.18.0
37373740
pr-url: https://github.com/nodejs/node/pull/54708
37383741
description: Documentation-only deprecation.
37393742
-->
37403743

3741-
Type: Documentation-only
3744+
Type: Runtime
37423745

37433746
Instantiating classes without the `new` qualifier exported by the `node:zlib` module is deprecated.
37443747
It is recommended to use the `new` qualifier instead. This applies to all Zlib classes, such as `Deflate`,

lib/zlib.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -686,57 +686,78 @@ Zlib.prototype.params = function params(level, strategy, callback) {
686686
// generic zlib
687687
// minimal 2-byte header
688688
function Deflate(opts) {
689-
if (!(this instanceof Deflate))
689+
if (!(this instanceof Deflate)) {
690+
process.emitWarning(`Instantiating Deflate class without 'new' is deprecated.`,
691+
'DeprecationWarning', 'DEP0184');
690692
return new Deflate(opts);
693+
}
691694
ReflectApply(Zlib, this, [opts, DEFLATE]);
692695
}
693696
ObjectSetPrototypeOf(Deflate.prototype, Zlib.prototype);
694697
ObjectSetPrototypeOf(Deflate, Zlib);
695698

696699
function Inflate(opts) {
697-
if (!(this instanceof Inflate))
700+
if (!(this instanceof Inflate)) {
701+
process.emitWarning(`Instantiating Inflate class without 'new' is deprecated.`,
702+
'DeprecationWarning', 'DEP0184');
698703
return new Inflate(opts);
704+
}
699705
ReflectApply(Zlib, this, [opts, INFLATE]);
700706
}
701707
ObjectSetPrototypeOf(Inflate.prototype, Zlib.prototype);
702708
ObjectSetPrototypeOf(Inflate, Zlib);
703709

704710
function Gzip(opts) {
705-
if (!(this instanceof Gzip))
711+
if (!(this instanceof Gzip)) {
712+
process.emitWarning(`Instantiating Gzip class without 'new' is deprecated.`,
713+
'DeprecationWarning', 'DEP0184');
706714
return new Gzip(opts);
715+
}
707716
ReflectApply(Zlib, this, [opts, GZIP]);
708717
}
709718
ObjectSetPrototypeOf(Gzip.prototype, Zlib.prototype);
710719
ObjectSetPrototypeOf(Gzip, Zlib);
711720

712721
function Gunzip(opts) {
713-
if (!(this instanceof Gunzip))
722+
if (!(this instanceof Gunzip)) {
723+
process.emitWarning(`Instantiating Gunzip class without 'new' is deprecated.`,
724+
'DeprecationWarning', 'DEP0184');
714725
return new Gunzip(opts);
726+
}
715727
ReflectApply(Zlib, this, [opts, GUNZIP]);
716728
}
717729
ObjectSetPrototypeOf(Gunzip.prototype, Zlib.prototype);
718730
ObjectSetPrototypeOf(Gunzip, Zlib);
719731

720732
function DeflateRaw(opts) {
721733
if (opts && opts.windowBits === 8) opts.windowBits = 9;
722-
if (!(this instanceof DeflateRaw))
734+
if (!(this instanceof DeflateRaw)) {
735+
process.emitWarning(`Instantiating DeflateRaw class without 'new' is deprecated.`,
736+
'DeprecationWarning', 'DEP0184');
723737
return new DeflateRaw(opts);
738+
}
724739
ReflectApply(Zlib, this, [opts, DEFLATERAW]);
725740
}
726741
ObjectSetPrototypeOf(DeflateRaw.prototype, Zlib.prototype);
727742
ObjectSetPrototypeOf(DeflateRaw, Zlib);
728743

729744
function InflateRaw(opts) {
730-
if (!(this instanceof InflateRaw))
745+
if (!(this instanceof InflateRaw)) {
746+
process.emitWarning(`Instantiating InflateRaw class without 'new' is deprecated.`,
747+
'DeprecationWarning', 'DEP0184');
731748
return new InflateRaw(opts);
749+
}
732750
ReflectApply(Zlib, this, [opts, INFLATERAW]);
733751
}
734752
ObjectSetPrototypeOf(InflateRaw.prototype, Zlib.prototype);
735753
ObjectSetPrototypeOf(InflateRaw, Zlib);
736754

737755
function Unzip(opts) {
738-
if (!(this instanceof Unzip))
756+
if (!(this instanceof Unzip)) {
757+
process.emitWarning(`Instantiating Unzip class without 'new' is deprecated.`,
758+
'DeprecationWarning', 'DEP0184');
739759
return new Unzip(opts);
760+
}
740761
ReflectApply(Zlib, this, [opts, UNZIP]);
741762
}
742763
ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype);
@@ -801,16 +822,22 @@ ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype);
801822
ObjectSetPrototypeOf(Brotli, Zlib);
802823

803824
function BrotliCompress(opts) {
804-
if (!(this instanceof BrotliCompress))
825+
if (!(this instanceof BrotliCompress)) {
826+
process.emitWarning(`Instantiating BrotliCompress class without 'new' is deprecated.`,
827+
'DeprecationWarning', 'DEP0184');
805828
return new BrotliCompress(opts);
829+
}
806830
ReflectApply(Brotli, this, [opts, BROTLI_ENCODE]);
807831
}
808832
ObjectSetPrototypeOf(BrotliCompress.prototype, Brotli.prototype);
809833
ObjectSetPrototypeOf(BrotliCompress, Brotli);
810834

811835
function BrotliDecompress(opts) {
812-
if (!(this instanceof BrotliDecompress))
836+
if (!(this instanceof BrotliDecompress)) {
837+
process.emitWarning(`Instantiating BrotliDecompress class without 'new' is deprecated.`,
838+
'DeprecationWarning', 'DEP0184');
813839
return new BrotliDecompress(opts);
840+
}
814841
ReflectApply(Brotli, this, [opts, BROTLI_DECODE]);
815842
}
816843
ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);

0 commit comments

Comments
 (0)