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

lib: make lazyDOMException more common #39105

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const {
const {
customInspectSymbol,
isInsideNodeModules,
lazyDOMException,
normalizeEncoding,
kIsEncodingSymbol
} = require('internal/util');
Expand Down Expand Up @@ -1208,22 +1209,14 @@ if (internalBinding('config').hasIntl) {
};
}

let DOMException;

const lazyInvalidCharError = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
throw new DOMException('Invalid character', 'InvalidCharacterError');
});

function btoa(input) {
// The implementation here has not been performance optimized in any way and
// should not be.
// Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932
input = `${input}`;
for (let n = 0; n < input.length; n++) {
if (input[n].charCodeAt(0) > 0xff)
lazyInvalidCharError();
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
}
const buf = Buffer.from(input, 'latin1');
return buf.toString('base64');
Expand All @@ -1239,7 +1232,7 @@ function atob(input) {
input = `${input}`;
for (let n = 0; n < input.length; n++) {
if (!kBase64Digits.includes(input[n]))
lazyInvalidCharError();
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
}
return Buffer.from(input, 'base64').toString('latin1');
}
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const {
getArrayBufferOrView,
hasAnyNotIn,
jobPromise,
lazyDOMException,
validateByteLength,
validateKeyOps,
validateMaxBufferLength,
Expand All @@ -45,6 +44,10 @@ const {
kKeyObject,
} = require('internal/crypto/util');

const {
lazyDOMException,
} = require('internal/util');

const { PromiseReject } = primordials;

const {
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const {
isAnyArrayBuffer,
} = require('internal/util/types');

const {
lazyDOMException,
} = require('internal/util');

const {
KeyObject,
InternalCryptoKey,
Expand All @@ -66,7 +70,6 @@ const {
getUsagesUnion,
hasAnyNotIn,
jobPromise,
lazyDOMException,
toBuf,
kHandle,
kKeyObject,
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/dsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ const {
getUsagesUnion,
hasAnyNotIn,
jobPromise,
lazyDOMException,
normalizeHashName,
validateKeyOps,
kKeyObject,
kHandle,
} = require('internal/crypto/util');

const {
lazyDOMException,
} = require('internal/util');

function verifyAcceptableDsaKeyUse(name, type, usages) {
let checkSet;
switch (type) {
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/ec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ const {
getUsagesUnion,
hasAnyNotIn,
jobPromise,
lazyDOMException,
normalizeHashName,
validateKeyOps,
kHandle,
kKeyObject,
kNamedCurveAliases,
} = require('internal/crypto/util');

const {
lazyDOMException,
} = require('internal/util');

const {
generateKeyPair,
} = require('internal/crypto/keygen');
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/hkdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const { kMaxLength } = require('buffer');

const {
getArrayBufferOrView,
lazyDOMException,
normalizeHashName,
toBuf,
validateByteSource,
Expand All @@ -35,6 +34,10 @@ const {
isKeyObject,
} = require('internal/crypto/keys');

const {
lazyDOMException,
} = require('internal/util');

const {
isAnyArrayBuffer,
isArrayBufferView,
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ const {
getHashLength,
hasAnyNotIn,
jobPromise,
lazyDOMException,
normalizeHashName,
validateBitLength,
validateKeyOps,
kHandle,
kKeyObject,
} = require('internal/crypto/util');

const {
lazyDOMException,
} = require('internal/util');

const {
codes: {
ERR_MISSING_OPTION,
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ const { ERR_MISSING_OPTION } = require('internal/errors').codes;
const {
getArrayBufferOrView,
getDefaultEncoding,
lazyDOMException,
normalizeHashName,
kKeyObject,
} = require('internal/crypto/util');

const {
lazyDOMException,
} = require('internal/util');

function pbkdf2(password, salt, iterations, keylen, digest, callback) {
if (typeof digest === 'function') {
callback = digest;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {

const {
lazyDOMException,
} = require('internal/crypto/util');
} = require('internal/util');

const { Buffer, kMaxLength } = require('buffer');

Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ const {
getUsagesUnion,
hasAnyNotIn,
jobPromise,
lazyDOMException,
normalizeHashName,
validateKeyOps,
validateMaxBufferLength,
kHandle,
kKeyObject,
} = require('internal/crypto/util');

const {
lazyDOMException,
} = require('internal/util');

const {
isUint8Array,
} = require('internal/util/types');
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/scrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ const {
const {
getArrayBufferOrView,
getDefaultEncoding,
lazyDOMException,
kKeyObject,
} = require('internal/crypto/util');

const {
lazyDOMException,
} = require('internal/util');

const defaults = {
N: 16384,
r: 8,
Expand Down
9 changes: 1 addition & 8 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const { Buffer } = require('buffer');
const {
cachedResult,
filterDuplicateStrings,
lazyDOMException,
} = require('internal/util');

const {
Expand All @@ -70,13 +71,6 @@ function lazyRequire(name) {
return ret;
}

let DOMException;
const lazyDOMException = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message, name);
});

var defaultEncoding = 'buffer';

function setDefaultEncoding(val) {
Expand Down Expand Up @@ -428,7 +422,6 @@ module.exports = {
normalizeAlgorithm,
normalizeHashName,
hasAnyNotIn,
lazyDOMException,
validateBitLength,
validateByteLength,
validateByteSource,
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const {
const {
getArrayBufferOrView,
hasAnyNotIn,
lazyDOMException,
lazyRequire,
normalizeAlgorithm,
normalizeHashName,
Expand All @@ -59,6 +58,10 @@ const {
kKeyObject,
} = require('internal/crypto/util');

const {
lazyDOMException,
} = require('internal/util');

const {
getRandomValues,
} = require('internal/crypto/random');
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const {
validateInteger,
} = require('internal/validators');
const pathModule = require('path');
const { promisify } = require('internal/util');
const { lazyDOMException, promisify } = require('internal/util');
const { EventEmitterMixin } = require('internal/event_target');
const { watch } = require('internal/fs/watchers');
const { isIterable } = require('internal/streams/utils');
Expand Down Expand Up @@ -211,8 +211,7 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {

[kTransfer]() {
if (this[kClosePromise] || this[kRefs] > 1) {
const DOMException = internalBinding('messaging').DOMException;
throw new DOMException('Cannot transfer FileHandle while in use',
throw lazyDOMException('Cannot transfer FileHandle while in use',
'DataCloneError');
}

Expand Down
9 changes: 9 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
} = primordials;

const {
hideStackFrames,
codes: {
ERR_INVALID_ARG_TYPE,
ERR_NO_CRYPTO,
Expand Down Expand Up @@ -441,6 +442,13 @@ function createDeferredPromise() {
return { promise, resolve, reject };
}

let DOMException;
const lazyDOMException = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
Comment on lines +447 to +448
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
DOMException ??= internalBinding('messaging').DOMException;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we use ??=?

Copy link
Contributor

@aduh95 aduh95 Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find ??= more readable and concise than the if statement, feel free to disagree and/or disregard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good to hoist this out of the crypto util and make it more universal in the code base, but I am wondering, why do we want to make this lazy?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably to avoid building the JS function until we're on the error path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return new DOMException(message, name);
});

module.exports = {
assertCrypto,
cachedResult,
Expand All @@ -457,6 +465,7 @@ module.exports = {
isError,
isInsideNodeModules,
join,
lazyDOMException,
normalizeEncoding,
once,
promisify,
Expand Down