Skip to content

Commit 4ee87b8

Browse files
authored
zlib: deprecate classes usage without new
PR-URL: #55718 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 22792b8 commit 4ee87b8

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

doc/api/deprecations.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3737,14 +3737,17 @@ and [`crypto.setEngine()`][] all depend on this functionality from OpenSSL.
37373737

37383738
<!-- YAML
37393739
changes:
3740+
- version: REPLACEME
3741+
pr-url: https://github.com/nodejs/node/pull/55718
3742+
description: Runtime deprecation.
37403743
- version:
37413744
- v22.9.0
37423745
- v20.18.0
37433746
pr-url: https://github.com/nodejs/node/pull/54708
37443747
description: Documentation-only deprecation.
37453748
-->
37463749

3747-
Type: Documentation-only
3750+
Type: Runtime
37483751

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

lib/zlib.js

+30-18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const {
4646
genericNodeError,
4747
} = require('internal/errors');
4848
const { Transform, finished } = require('stream');
49+
const {
50+
deprecateInstantiation,
51+
} = require('internal/util');
4952
const {
5053
isArrayBufferView,
5154
isAnyArrayBuffer,
@@ -686,57 +689,64 @@ Zlib.prototype.params = function params(level, strategy, callback) {
686689
// generic zlib
687690
// minimal 2-byte header
688691
function Deflate(opts) {
689-
if (!(this instanceof Deflate))
690-
return new Deflate(opts);
692+
if (!(this instanceof Deflate)) {
693+
return deprecateInstantiation(Deflate, 'DEP0184', opts);
694+
}
691695
ReflectApply(Zlib, this, [opts, DEFLATE]);
692696
}
693697
ObjectSetPrototypeOf(Deflate.prototype, Zlib.prototype);
694698
ObjectSetPrototypeOf(Deflate, Zlib);
695699

696700
function Inflate(opts) {
697-
if (!(this instanceof Inflate))
698-
return new Inflate(opts);
701+
if (!(this instanceof Inflate)) {
702+
return deprecateInstantiation(Inflate, 'DEP0184', opts);
703+
}
699704
ReflectApply(Zlib, this, [opts, INFLATE]);
700705
}
701706
ObjectSetPrototypeOf(Inflate.prototype, Zlib.prototype);
702707
ObjectSetPrototypeOf(Inflate, Zlib);
703708

704709
function Gzip(opts) {
705-
if (!(this instanceof Gzip))
706-
return new Gzip(opts);
710+
if (!(this instanceof Gzip)) {
711+
return deprecateInstantiation(Gzip, 'DEP0184', opts);
712+
}
707713
ReflectApply(Zlib, this, [opts, GZIP]);
708714
}
709715
ObjectSetPrototypeOf(Gzip.prototype, Zlib.prototype);
710716
ObjectSetPrototypeOf(Gzip, Zlib);
711717

712718
function Gunzip(opts) {
713-
if (!(this instanceof Gunzip))
714-
return new Gunzip(opts);
719+
if (!(this instanceof Gunzip)) {
720+
return deprecateInstantiation(Gunzip, 'DEP0184', opts);
721+
}
715722
ReflectApply(Zlib, this, [opts, GUNZIP]);
716723
}
717724
ObjectSetPrototypeOf(Gunzip.prototype, Zlib.prototype);
718725
ObjectSetPrototypeOf(Gunzip, Zlib);
719726

720727
function DeflateRaw(opts) {
721728
if (opts && opts.windowBits === 8) opts.windowBits = 9;
722-
if (!(this instanceof DeflateRaw))
723-
return new DeflateRaw(opts);
729+
if (!(this instanceof DeflateRaw)) {
730+
return deprecateInstantiation(DeflateRaw, 'DEP0184', opts);
731+
}
724732
ReflectApply(Zlib, this, [opts, DEFLATERAW]);
725733
}
726734
ObjectSetPrototypeOf(DeflateRaw.prototype, Zlib.prototype);
727735
ObjectSetPrototypeOf(DeflateRaw, Zlib);
728736

729737
function InflateRaw(opts) {
730-
if (!(this instanceof InflateRaw))
731-
return new InflateRaw(opts);
738+
if (!(this instanceof InflateRaw)) {
739+
return deprecateInstantiation(InflateRaw, 'DEP0184', opts);
740+
}
732741
ReflectApply(Zlib, this, [opts, INFLATERAW]);
733742
}
734743
ObjectSetPrototypeOf(InflateRaw.prototype, Zlib.prototype);
735744
ObjectSetPrototypeOf(InflateRaw, Zlib);
736745

737746
function Unzip(opts) {
738-
if (!(this instanceof Unzip))
739-
return new Unzip(opts);
747+
if (!(this instanceof Unzip)) {
748+
return deprecateInstantiation(Unzip, 'DEP0184', opts);
749+
}
740750
ReflectApply(Zlib, this, [opts, UNZIP]);
741751
}
742752
ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype);
@@ -801,16 +811,18 @@ ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype);
801811
ObjectSetPrototypeOf(Brotli, Zlib);
802812

803813
function BrotliCompress(opts) {
804-
if (!(this instanceof BrotliCompress))
805-
return new BrotliCompress(opts);
814+
if (!(this instanceof BrotliCompress)) {
815+
return deprecateInstantiation(BrotliCompress, 'DEP0184', opts);
816+
}
806817
ReflectApply(Brotli, this, [opts, BROTLI_ENCODE]);
807818
}
808819
ObjectSetPrototypeOf(BrotliCompress.prototype, Brotli.prototype);
809820
ObjectSetPrototypeOf(BrotliCompress, Brotli);
810821

811822
function BrotliDecompress(opts) {
812-
if (!(this instanceof BrotliDecompress))
813-
return new BrotliDecompress(opts);
823+
if (!(this instanceof BrotliDecompress)) {
824+
return deprecateInstantiation(BrotliDecompress, 'DEP0184', opts);
825+
}
814826
ReflectApply(Brotli, this, [opts, BROTLI_DECODE]);
815827
}
816828
ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);

test/parallel/test-zlib.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-warnings
12
// Copyright Joyent, Inc. and other Node contributors.
23
//
34
// Permission is hereby granted, free of charge, to any person obtaining a
@@ -230,3 +231,10 @@ testKeys.forEach(common.mustCall((file) => {
230231
}, trickle.length));
231232
}, chunkSize.length));
232233
}, testKeys.length));
234+
235+
{
236+
// Test instantiation without 'new'
237+
common.expectWarning('DeprecationWarning', `Instantiating Gzip without the 'new' keyword has been deprecated.`, 'DEP0184');
238+
const gzip = zlib.Gzip();
239+
assert.ok(gzip instanceof zlib.Gzip);
240+
}

0 commit comments

Comments
 (0)