Skip to content

Commit c9307a2

Browse files
committed
zlib: runtime deprecate initializing without new qualifier
1 parent fff1549 commit c9307a2

6 files changed

+37
-16
lines changed

lib/zlib.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -706,57 +706,72 @@ Zlib.prototype.params = function params(level, strategy, callback) {
706706
// generic zlib
707707
// minimal 2-byte header
708708
function Deflate(opts) {
709-
if (!(this instanceof Deflate))
709+
if (!(this instanceof Deflate)) {
710+
process.emitWarning('Initializing Deflate without "new" qualifier is deprecated', 'DEP0184');
710711
return new Deflate(opts);
712+
}
711713
ReflectApply(Zlib, this, [opts, DEFLATE]);
712714
}
713715
ObjectSetPrototypeOf(Deflate.prototype, Zlib.prototype);
714716
ObjectSetPrototypeOf(Deflate, Zlib);
715717

716718
function Inflate(opts) {
717-
if (!(this instanceof Inflate))
719+
if (!(this instanceof Inflate)) {
720+
process.emitWarning('Initializing Inflate without "new" qualifier is deprecated', 'DEP0184');
718721
return new Inflate(opts);
722+
}
719723
ReflectApply(Zlib, this, [opts, INFLATE]);
720724
}
721725
ObjectSetPrototypeOf(Inflate.prototype, Zlib.prototype);
722726
ObjectSetPrototypeOf(Inflate, Zlib);
723727

724728
function Gzip(opts) {
725-
if (!(this instanceof Gzip))
729+
if (!(this instanceof Gzip)) {
730+
process.emitWarning('Initializing Inflate without "new" qualifier is deprecated', 'DEP0184');
726731
return new Gzip(opts);
732+
}
727733
ReflectApply(Zlib, this, [opts, GZIP]);
728734
}
729735
ObjectSetPrototypeOf(Gzip.prototype, Zlib.prototype);
730736
ObjectSetPrototypeOf(Gzip, Zlib);
731737

732738
function Gunzip(opts) {
733-
if (!(this instanceof Gunzip))
739+
if (!(this instanceof Gunzip)) {
740+
process.emitWarning('Initializing Gunzip without "new" qualifier is deprecated', 'DEP0184');
734741
return new Gunzip(opts);
742+
}
735743
ReflectApply(Zlib, this, [opts, GUNZIP]);
736744
}
737745
ObjectSetPrototypeOf(Gunzip.prototype, Zlib.prototype);
738746
ObjectSetPrototypeOf(Gunzip, Zlib);
739747

740748
function DeflateRaw(opts) {
741749
if (opts && opts.windowBits === 8) opts.windowBits = 9;
742-
if (!(this instanceof DeflateRaw))
750+
if (!(this instanceof DeflateRaw)) {
751+
process.emitWarning('Initializing DeflateRaw without "new" qualifier is deprecated', 'DEP0184');
743752
return new DeflateRaw(opts);
753+
}
744754
ReflectApply(Zlib, this, [opts, DEFLATERAW]);
745755
}
746756
ObjectSetPrototypeOf(DeflateRaw.prototype, Zlib.prototype);
747757
ObjectSetPrototypeOf(DeflateRaw, Zlib);
748758

749759
function InflateRaw(opts) {
750-
if (!(this instanceof InflateRaw))
760+
if (!(this instanceof InflateRaw)) {
761+
process.emitWarning('Initializing InflateRaw without "new" qualifier is deprecated', 'DEP0184');
751762
return new InflateRaw(opts);
763+
}
752764
ReflectApply(Zlib, this, [opts, INFLATERAW]);
753765
}
754766
ObjectSetPrototypeOf(InflateRaw.prototype, Zlib.prototype);
755767
ObjectSetPrototypeOf(InflateRaw, Zlib);
756768

757769
function Unzip(opts) {
758-
if (!(this instanceof Unzip))
770+
if (!(this instanceof Unzip)) {
771+
process.emitWarning('Initializing Unzip without "new" qualifier is deprecated', 'DEP0184');
759772
return new Unzip(opts);
773+
774+
}
760775
ReflectApply(Zlib, this, [opts, UNZIP]);
761776
}
762777
ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype);
@@ -831,16 +846,20 @@ ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype);
831846
ObjectSetPrototypeOf(Brotli, Zlib);
832847

833848
function BrotliCompress(opts) {
834-
if (!(this instanceof BrotliCompress))
849+
if (!(this instanceof BrotliCompress)) {
850+
process.emitWarning('Initializing BrotliCompress without "new" qualifier is deprecated', 'DEP0184');
835851
return new BrotliCompress(opts);
852+
}
836853
ReflectApply(Brotli, this, [opts, BROTLI_ENCODE]);
837854
}
838855
ObjectSetPrototypeOf(BrotliCompress.prototype, Brotli.prototype);
839856
ObjectSetPrototypeOf(BrotliCompress, Brotli);
840857

841858
function BrotliDecompress(opts) {
842-
if (!(this instanceof BrotliDecompress))
859+
if (!(this instanceof BrotliDecompress)) {
860+
process.emitWarning('Initializing BrotliDecompress without "new" qualifier is deprecated', 'DEP0184');
843861
return new BrotliDecompress(opts);
862+
}
844863
ReflectApply(Brotli, this, [opts, BROTLI_DECODE]);
845864
}
846865
ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);

test/parallel/test-zlib-deflate-constructors.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-deprecation
12
'use strict';
23

34
require('../common');

test/parallel/test-zlib-deflate-raw-inherits.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-deprecation
12
'use strict';
23

34
require('../common');

test/parallel/test-zlib-invalid-arg-value-brotli-compress.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ const opts = {
1515
}
1616
};
1717

18-
assert.throws(() => BrotliCompress(opts), {
18+
assert.throws(() => new BrotliCompress(opts), {
1919
code: 'ERR_INVALID_ARG_TYPE'
2020
});

test/parallel/test-zlib-invalid-input.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ const nonStringInputs = [
3535

3636
// zlib.Unzip classes need to get valid data, or else they'll throw.
3737
const unzips = [
38-
zlib.Unzip(),
39-
zlib.Gunzip(),
40-
zlib.Inflate(),
41-
zlib.InflateRaw(),
42-
zlib.BrotliDecompress(),
38+
new zlib.Unzip(),
39+
new zlib.Gunzip(),
40+
new zlib.Inflate(),
41+
new zlib.InflateRaw(),
42+
new zlib.BrotliDecompress(),
4343
];
4444

4545
nonStringInputs.forEach(common.mustCall((input) => {

test/parallel/test-zlib-zero-byte.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const { test } = require('node:test');
3030
test('zlib should properly handle zero byte input', async () => {
3131
for (const Compressor of [zlib.Gzip, zlib.BrotliCompress]) {
3232
const { promise, resolve, reject } = Promise.withResolvers();
33-
const gz = Compressor();
33+
const gz = new Compressor();
3434
const emptyBuffer = Buffer.alloc(0);
3535
let received = 0;
3636
gz.on('data', function(c) {

0 commit comments

Comments
 (0)