Skip to content

Commit

Permalink
zlib: deprecate classes usage without new
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 4, 2024
1 parent bdc2662 commit dbfed33
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
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');
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

0 comments on commit dbfed33

Please sign in to comment.