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

Use new.target instead of instanceof to detect if its a constructor call #7431

Closed
wants to merge 12 commits into from
18 changes: 9 additions & 9 deletions src/js/node/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7461,7 +7461,7 @@ var require__ = __commonJS({
BlockHash = common.BlockHash,
sha1_K = [1518500249, 1859775393, 2400959708, 3395469782];
function SHA1() {
if (!(this instanceof SHA1)) return new SHA1();
if (!new.target) return new SHA1();
BlockHash.$call(this),
(this.h = [1732584193, 4023233417, 2562383102, 271733878, 3285377520]),
(this.W = new Array(80));
Expand Down Expand Up @@ -7526,7 +7526,7 @@ var require__2 = __commonJS({
3329325298,
];
function SHA256() {
if (!(this instanceof SHA256)) return new SHA256();
if (!new.target) return new SHA256();
BlockHash.$call(this),
(this.h = [1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225]),
(this.k = sha256_K),
Expand Down Expand Up @@ -7576,7 +7576,7 @@ var require__3 = __commonJS({
var utils = require_utils4(),
SHA256 = require__2();
function SHA224() {
if (!(this instanceof SHA224)) return new SHA224();
if (!new.target) return new SHA224();
SHA256.$call(this),
(this.h = [3238371032, 914150663, 812702999, 4144912697, 4290775857, 1750603025, 1694076839, 3204075428]);
}
Expand Down Expand Up @@ -7632,7 +7632,7 @@ var require__4 = __commonJS({
3409855158, 1501505948, 4234509866, 1607167915, 987167468, 1816402316, 1246189591,
];
function SHA512() {
if (!(this instanceof SHA512)) return new SHA512();
if (!new.target) return new SHA512();
BlockHash.$call(this),
(this.h = [
1779033703, 4089235720, 3144134277, 2227873595, 1013904242, 4271175723, 2773480762, 1595750129, 1359893119,
Expand Down Expand Up @@ -7812,7 +7812,7 @@ var require__5 = __commonJS({
var utils = require_utils4(),
SHA512 = require__4();
function SHA384() {
if (!(this instanceof SHA384)) return new SHA384();
if (!new.target) return new SHA384();
SHA512.$call(this),
(this.h = [
3418070365, 3238371032, 1654270250, 914150663, 2438529370, 812702999, 355462360, 4144912697, 1731405415,
Expand Down Expand Up @@ -7855,7 +7855,7 @@ var require_ripemd = __commonJS({
sum32_4 = utils.sum32_4,
BlockHash = common.BlockHash;
function RIPEMD160() {
if (!(this instanceof RIPEMD160)) return new RIPEMD160();
if (!new.target) return new RIPEMD160();
BlockHash.$call(this),
(this.h = [1732584193, 4023233417, 2562383102, 271733878, 3285377520]),
(this.endian = "little");
Expand Down Expand Up @@ -7954,7 +7954,7 @@ var require_hmac = __commonJS({
function Hmac(hash, key, enc) {
key = exportIfKeyObject(key);

if (!(this instanceof Hmac)) return new Hmac(hash, key, enc);
if (!new.target) return new Hmac(hash, key, enc);
(this.Hash = hash),
(this.blockSize = hash.blockSize / 8),
(this.outSize = hash.outSize / 8),
Expand Down Expand Up @@ -8965,7 +8965,7 @@ var require_hmac_drbg = __commonJS({
utils = require_utils2(),
assert = require_minimalistic_assert();
function HmacDRBG(options) {
if (!(this instanceof HmacDRBG)) return new HmacDRBG(options);
if (!new.target) return new HmacDRBG(options);
(this.hash = options.hash),
(this.predResist = !!options.predResist),
(this.outLen = this.hash.outSize),
Expand Down Expand Up @@ -9196,7 +9196,7 @@ var require_ec = __commonJS({
KeyPair = require_key(),
Signature = require_signature();
function EC(options) {
if (!(this instanceof EC)) return new EC(options);
if (!new.target) return new EC(options);
typeof options == "string" &&
(assert(Object.prototype.hasOwnProperty.$call(curves, options), "Unknown curve " + options),
(options = curves[options])),
Expand Down
47 changes: 35 additions & 12 deletions src/js/node/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function ERR_INVALID_ARG_VALUE(name, value, reason) {
return new Error(`The value '${value}' is invalid for argument '${name}'. Reason: ${reason}`);
}

var isCallingDuplexStreamConstructor = false;
// node_modules/readable-stream/lib/ours/primordials.js
var require_primordials = __commonJS({
"node_modules/readable-stream/lib/ours/primordials.js"(exports, module) {
Expand Down Expand Up @@ -2000,7 +2001,6 @@ var require_legacy = __commonJS({
var { ArrayIsArray, ObjectSetPrototypeOf } = require_primordials();

function Stream(options) {
if (!(this instanceof Stream)) return new Stream(options);
EE.$call(this, options);
}
Stream.prototype = {};
Expand Down Expand Up @@ -2265,22 +2265,42 @@ var require_readable = __commonJS({

function Readable(options) {
if (!(this instanceof Readable)) return new Readable(options);
const isDuplex = this instanceof require_duplex();
this._readableState = new ReadableState(options, this, isDuplex);
const wasCallingDuplexStreamConstructor = isCallingDuplexStreamConstructor;
isCallingDuplexStreamConstructor = false;

this._events ??= {
close: undefined,
error: undefined,
data: undefined,
end: undefined,
readable: undefined,
// Skip uncommon events...
// pause: undefined,
// resume: undefined,
// pipe: undefined,
// unpipe: undefined,
// [destroyImpl.kConstruct]: undefined,
// [destroyImpl.kDestroy]: undefined,
};

this._readableState = new ReadableState(options, this, wasCallingDuplexStreamConstructor);

if (options) {
const { read, destroy, construct, signal } = options;
if (typeof read === "function") this._read = read;
if (typeof destroy === "function") this._destroy = destroy;
if (typeof construct === "function") this._construct = construct;
if (signal && !isDuplex) addAbortSignal(signal, this);
if ($isCallable(read)) this._read = read;
if ($isCallable(destroy)) this._destroy = destroy;
if ($isCallable(construct)) this._construct = construct;
if (signal) addAbortSignal(signal, this);
}
Stream.$call(this, options);

destroyImpl.construct(this, () => {
if (this._readableState.needReadable) {
maybeReadMore(this, this._readableState);
}
});
if (!$isUndefinedOrNull(this._construct)) {
destroyImpl.construct(this, () => {
if (this._readableState.needReadable) {
maybeReadMore(this, this._readableState);
}
});
}
}
Readable.prototype = {};
ObjectSetPrototypeOf(Readable.prototype, Stream.prototype);
Expand Down Expand Up @@ -4387,7 +4407,10 @@ var require_duplex = __commonJS({

function Duplex(options) {
if (!(this instanceof Duplex)) return new Duplex(options);
const prevIsCallingDuplexStreamConstructor = isCallingDuplexStreamConstructor;
isCallingDuplexStreamConstructor = true;
Readable.$call(this, options);
isCallingDuplexStreamConstructor = prevIsCallingDuplexStreamConstructor;
Writable.$call(this, options);

if (options) {
Expand Down
14 changes: 7 additions & 7 deletions src/js/node/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4123,31 +4123,31 @@ var require_lib = __commonJS({
return engine._processChunk(buffer, flushFlag);
}
function Deflate(opts) {
if (!(this instanceof Deflate)) return new Deflate(opts);
if (!new.target) return new Deflate(opts);
Zlib.$call(this, opts, binding.DEFLATE);
}
function Inflate(opts) {
if (!(this instanceof Inflate)) return new Inflate(opts);
if (!new.target) return new Inflate(opts);
Zlib.$call(this, opts, binding.INFLATE);
}
function Gzip(opts) {
if (!(this instanceof Gzip)) return new Gzip(opts);
if (!new.target) return new Gzip(opts);
Zlib.$call(this, opts, binding.GZIP);
}
function Gunzip(opts) {
if (!(this instanceof Gunzip)) return new Gunzip(opts);
if (!new.target) return new Gunzip(opts);
Zlib.$call(this, opts, binding.GUNZIP);
}
function DeflateRaw(opts) {
if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
if (!new.target) return new DeflateRaw(opts);
Zlib.$call(this, opts, binding.DEFLATERAW);
}
function InflateRaw(opts) {
if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
if (!new.target) return new InflateRaw(opts);
Zlib.$call(this, opts, binding.INFLATERAW);
}
function Unzip(opts) {
if (!(this instanceof Unzip)) return new Unzip(opts);
if (!new.target) return new Unzip(opts);
Zlib.$call(this, opts, binding.UNZIP);
}
function isValidFlushFlag(flag) {
Expand Down
Loading