Skip to content

Commit

Permalink
zlib: runtime deprecate initializing without new qualifier
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 5, 2024
1 parent fff1549 commit c9307a2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
37 changes: 28 additions & 9 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,57 +706,72 @@ 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('Initializing Deflate without "new" qualifier is deprecated', '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('Initializing Inflate without "new" qualifier is deprecated', '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('Initializing Inflate without "new" qualifier is deprecated', '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('Initializing Gunzip without "new" qualifier is deprecated', '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('Initializing DeflateRaw without "new" qualifier is deprecated', '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('Initializing InflateRaw without "new" qualifier is deprecated', '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('Initializing Unzip without "new" qualifier is deprecated', 'DEP0184');
return new Unzip(opts);

}
ReflectApply(Zlib, this, [opts, UNZIP]);
}
ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype);
Expand Down Expand Up @@ -831,16 +846,20 @@ ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype);
ObjectSetPrototypeOf(Brotli, Zlib);

function BrotliCompress(opts) {
if (!(this instanceof BrotliCompress))
if (!(this instanceof BrotliCompress)) {
process.emitWarning('Initializing BrotliCompress without "new" qualifier is deprecated', '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('Initializing BrotliDecompress without "new" qualifier is deprecated', 'DEP0184');
return new BrotliDecompress(opts);
}
ReflectApply(Brotli, this, [opts, BROTLI_DECODE]);
}
ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-zlib-deflate-constructors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --no-deprecation
'use strict';

require('../common');
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-zlib-deflate-raw-inherits.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --no-deprecation
'use strict';

require('../common');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ const opts = {
}
};

assert.throws(() => BrotliCompress(opts), {
assert.throws(() => new BrotliCompress(opts), {
code: 'ERR_INVALID_ARG_TYPE'
});
10 changes: 5 additions & 5 deletions test/parallel/test-zlib-invalid-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const nonStringInputs = [

// zlib.Unzip classes need to get valid data, or else they'll throw.
const unzips = [
zlib.Unzip(),
zlib.Gunzip(),
zlib.Inflate(),
zlib.InflateRaw(),
zlib.BrotliDecompress(),
new zlib.Unzip(),
new zlib.Gunzip(),
new zlib.Inflate(),
new zlib.InflateRaw(),
new zlib.BrotliDecompress(),
];

nonStringInputs.forEach(common.mustCall((input) => {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-zlib-zero-byte.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const { test } = require('node:test');
test('zlib should properly handle zero byte input', async () => {
for (const Compressor of [zlib.Gzip, zlib.BrotliCompress]) {
const { promise, resolve, reject } = Promise.withResolvers();
const gz = Compressor();
const gz = new Compressor();
const emptyBuffer = Buffer.alloc(0);
let received = 0;
gz.on('data', function(c) {
Expand Down

0 comments on commit c9307a2

Please sign in to comment.