Skip to content

Commit

Permalink
zlib: use new.target to check for instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 1, 2024
1 parent 2f0b371 commit e74e917
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
29 changes: 19 additions & 10 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,57 +706,64 @@ Zlib.prototype.params = function params(level, strategy, callback) {
// generic zlib
// minimal 2-byte header
function Deflate(opts) {
if (!(this instanceof Deflate))
if (!new.target) {
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 (!new.target) {
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 (!new.target) {
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 (!new.target) {
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 (opts?.windowBits === 8) opts.windowBits = 9;
if (!new.target) {
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 (!new.target) {
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 (!new.target) {
return new Unzip(opts);
}
ReflectApply(Zlib, this, [opts, UNZIP]);
}
ObjectSetPrototypeOf(Unzip.prototype, Zlib.prototype);
Expand Down Expand Up @@ -831,16 +838,18 @@ ObjectSetPrototypeOf(Brotli.prototype, Zlib.prototype);
ObjectSetPrototypeOf(Brotli, Zlib);

function BrotliCompress(opts) {
if (!(this instanceof BrotliCompress))
if (!new.target) {
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 (!new.target) {
return new BrotliDecompress(opts);
}
ReflectApply(Brotli, this, [opts, BROTLI_DECODE]);
}
ObjectSetPrototypeOf(BrotliDecompress.prototype, Brotli.prototype);
Expand Down
27 changes: 0 additions & 27 deletions test/parallel/test-zlib-deflate-raw-inherits.js

This file was deleted.

0 comments on commit e74e917

Please sign in to comment.