Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: deprecate classes usage without new #55718

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3731,14 +3731,17 @@ and [`crypto.setEngine()`][] all depend on this functionality from OpenSSL.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/55718
description: Runtime deprecation.
- version:
- v22.9.0
- v20.18.0
pr-url: https://github.com/nodejs/node/pull/54708
description: Documentation-only deprecation.
-->

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`,
Expand Down
45 changes: 36 additions & 9 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,57 +686,78 @@ 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');
Comment on lines +690 to +691
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW when #54869 lands there will be a helper utility for this warning

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);
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);
Expand Down Expand Up @@ -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);
Expand Down
Loading