From 31e678397a5e077604d6926ea0cdb20dba7f07f4 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Mon, 21 Jun 2021 11:34:48 +0800 Subject: [PATCH 1/2] lib: make lazyDOMException more common --- lib/buffer.js | 15 ++++++--------- lib/internal/crypto/aes.js | 5 ++++- lib/internal/crypto/diffiehellman.js | 5 ++++- lib/internal/crypto/dsa.js | 5 ++++- lib/internal/crypto/ec.js | 5 ++++- lib/internal/crypto/hkdf.js | 5 ++++- lib/internal/crypto/mac.js | 5 ++++- lib/internal/crypto/pbkdf2.js | 5 ++++- lib/internal/crypto/random.js | 2 +- lib/internal/crypto/rsa.js | 5 ++++- lib/internal/crypto/scrypt.js | 5 ++++- lib/internal/crypto/util.js | 9 +-------- lib/internal/crypto/webcrypto.js | 5 ++++- lib/internal/fs/promises.js | 5 ++--- lib/internal/util.js | 9 +++++++++ 15 files changed, 59 insertions(+), 31 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 18b90f40527921..63dbf05d411fad 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -75,6 +75,7 @@ const { const { customInspectSymbol, isInsideNodeModules, + lazyDOMException, normalizeEncoding, kIsEncodingSymbol } = require('internal/util'); @@ -1208,13 +1209,9 @@ if (internalBinding('config').hasIntl) { }; } -let DOMException; - -const lazyInvalidCharError = hideStackFrames((message, name) => { - if (DOMException === undefined) - DOMException = internalBinding('messaging').DOMException; - throw new DOMException('Invalid character', 'InvalidCharacterError'); -}); +const invalidCharError = () => { + return lazyDOMException('Invalid character', 'InvalidCharacterError'); +}; function btoa(input) { // The implementation here has not been performance optimized in any way and @@ -1223,7 +1220,7 @@ function btoa(input) { input = `${input}`; for (let n = 0; n < input.length; n++) { if (input[n].charCodeAt(0) > 0xff) - lazyInvalidCharError(); + invalidCharError(); } const buf = Buffer.from(input, 'latin1'); return buf.toString('base64'); @@ -1239,7 +1236,7 @@ function atob(input) { input = `${input}`; for (let n = 0; n < input.length; n++) { if (!kBase64Digits.includes(input[n])) - lazyInvalidCharError(); + invalidCharError(); } return Buffer.from(input, 'base64').toString('latin1'); } diff --git a/lib/internal/crypto/aes.js b/lib/internal/crypto/aes.js index dd6aa49ff454ab..0675c59ec368ca 100644 --- a/lib/internal/crypto/aes.js +++ b/lib/internal/crypto/aes.js @@ -36,7 +36,6 @@ const { getArrayBufferOrView, hasAnyNotIn, jobPromise, - lazyDOMException, validateByteLength, validateKeyOps, validateMaxBufferLength, @@ -45,6 +44,10 @@ const { kKeyObject, } = require('internal/crypto/util'); +const { + lazyDOMException, +} = require('internal/util'); + const { PromiseReject } = primordials; const { diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js index 2e38e95ea1d774..2efdbdd5bac9d3 100644 --- a/lib/internal/crypto/diffiehellman.js +++ b/lib/internal/crypto/diffiehellman.js @@ -47,6 +47,10 @@ const { isAnyArrayBuffer, } = require('internal/util/types'); +const { + lazyDOMException, +} = require('internal/util'); + const { KeyObject, InternalCryptoKey, @@ -66,7 +70,6 @@ const { getUsagesUnion, hasAnyNotIn, jobPromise, - lazyDOMException, toBuf, kHandle, kKeyObject, diff --git a/lib/internal/crypto/dsa.js b/lib/internal/crypto/dsa.js index b615c3a9cb932f..54bd70d9e2eac5 100644 --- a/lib/internal/crypto/dsa.js +++ b/lib/internal/crypto/dsa.js @@ -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) { diff --git a/lib/internal/crypto/ec.js b/lib/internal/crypto/ec.js index ee14eed7d083a7..8bc7d9a28a42ea 100644 --- a/lib/internal/crypto/ec.js +++ b/lib/internal/crypto/ec.js @@ -38,7 +38,6 @@ const { getUsagesUnion, hasAnyNotIn, jobPromise, - lazyDOMException, normalizeHashName, validateKeyOps, kHandle, @@ -46,6 +45,10 @@ const { kNamedCurveAliases, } = require('internal/crypto/util'); +const { + lazyDOMException, +} = require('internal/util'); + const { generateKeyPair, } = require('internal/crypto/keygen'); diff --git a/lib/internal/crypto/hkdf.js b/lib/internal/crypto/hkdf.js index 8cff0489a553e0..84d39ac61b2b2e 100644 --- a/lib/internal/crypto/hkdf.js +++ b/lib/internal/crypto/hkdf.js @@ -23,7 +23,6 @@ const { kMaxLength } = require('buffer'); const { getArrayBufferOrView, - lazyDOMException, normalizeHashName, toBuf, validateByteSource, @@ -35,6 +34,10 @@ const { isKeyObject, } = require('internal/crypto/keys'); +const { + lazyDOMException, +} = require('internal/util'); + const { isAnyArrayBuffer, isArrayBufferView, diff --git a/lib/internal/crypto/mac.js b/lib/internal/crypto/mac.js index 5ee1f0918db7e1..61fcc88a923dad 100644 --- a/lib/internal/crypto/mac.js +++ b/lib/internal/crypto/mac.js @@ -18,7 +18,6 @@ const { getHashLength, hasAnyNotIn, jobPromise, - lazyDOMException, normalizeHashName, validateBitLength, validateKeyOps, @@ -26,6 +25,10 @@ const { kKeyObject, } = require('internal/crypto/util'); +const { + lazyDOMException, +} = require('internal/util'); + const { codes: { ERR_MISSING_OPTION, diff --git a/lib/internal/crypto/pbkdf2.js b/lib/internal/crypto/pbkdf2.js index d600f8f036284b..753c4f2d9da597 100644 --- a/lib/internal/crypto/pbkdf2.js +++ b/lib/internal/crypto/pbkdf2.js @@ -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; diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index cf84507515a505..5ce158324851d4 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -27,7 +27,7 @@ const { const { lazyDOMException, -} = require('internal/crypto/util'); +} = require('internal/util'); const { Buffer, kMaxLength } = require('buffer'); diff --git a/lib/internal/crypto/rsa.js b/lib/internal/crypto/rsa.js index e7b793fa184817..a0d27f3715e211 100644 --- a/lib/internal/crypto/rsa.js +++ b/lib/internal/crypto/rsa.js @@ -40,7 +40,6 @@ const { getUsagesUnion, hasAnyNotIn, jobPromise, - lazyDOMException, normalizeHashName, validateKeyOps, validateMaxBufferLength, @@ -48,6 +47,10 @@ const { kKeyObject, } = require('internal/crypto/util'); +const { + lazyDOMException, +} = require('internal/util'); + const { isUint8Array, } = require('internal/util/types'); diff --git a/lib/internal/crypto/scrypt.js b/lib/internal/crypto/scrypt.js index 45a04905bfd447..63a5547e4cbf79 100644 --- a/lib/internal/crypto/scrypt.js +++ b/lib/internal/crypto/scrypt.js @@ -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, diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index 8343940b99e169..eafcc3d9669288 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -51,6 +51,7 @@ const { Buffer } = require('buffer'); const { cachedResult, filterDuplicateStrings, + lazyDOMException, } = require('internal/util'); const { @@ -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) { @@ -428,7 +422,6 @@ module.exports = { normalizeAlgorithm, normalizeHashName, hasAnyNotIn, - lazyDOMException, validateBitLength, validateByteLength, validateByteSource, diff --git a/lib/internal/crypto/webcrypto.js b/lib/internal/crypto/webcrypto.js index 900ac7a6e8ad7b..0dcdc28a6c2f63 100644 --- a/lib/internal/crypto/webcrypto.js +++ b/lib/internal/crypto/webcrypto.js @@ -49,7 +49,6 @@ const { const { getArrayBufferOrView, hasAnyNotIn, - lazyDOMException, lazyRequire, normalizeAlgorithm, normalizeHashName, @@ -59,6 +58,10 @@ const { kKeyObject, } = require('internal/crypto/util'); +const { + lazyDOMException, +} = require('internal/util'); + const { getRandomValues, } = require('internal/crypto/random'); diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index b30af8fdf5514a..dbe7e02b7a64fb 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -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'); @@ -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'); } diff --git a/lib/internal/util.js b/lib/internal/util.js index f0a04f61818392..101fbec67775b0 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -28,6 +28,7 @@ const { } = primordials; const { + hideStackFrames, codes: { ERR_INVALID_ARG_TYPE, ERR_NO_CRYPTO, @@ -441,6 +442,13 @@ function createDeferredPromise() { return { promise, resolve, reject }; } +let DOMException; +const lazyDOMException = hideStackFrames((message, name) => { + if (DOMException === undefined) + DOMException = internalBinding('messaging').DOMException; + return new DOMException(message, name); +}); + module.exports = { assertCrypto, cachedResult, @@ -457,6 +465,7 @@ module.exports = { isError, isInsideNodeModules, join, + lazyDOMException, normalizeEncoding, once, promisify, From fd00900fb74e539f327f31a8920e42f50c249a53 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Mon, 21 Jun 2021 15:15:12 +0800 Subject: [PATCH 2/2] f --- lib/buffer.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 63dbf05d411fad..278a67cbbfb37e 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1209,10 +1209,6 @@ if (internalBinding('config').hasIntl) { }; } -const invalidCharError = () => { - return lazyDOMException('Invalid character', 'InvalidCharacterError'); -}; - function btoa(input) { // The implementation here has not been performance optimized in any way and // should not be. @@ -1220,7 +1216,7 @@ function btoa(input) { input = `${input}`; for (let n = 0; n < input.length; n++) { if (input[n].charCodeAt(0) > 0xff) - invalidCharError(); + throw lazyDOMException('Invalid character', 'InvalidCharacterError'); } const buf = Buffer.from(input, 'latin1'); return buf.toString('base64'); @@ -1236,7 +1232,7 @@ function atob(input) { input = `${input}`; for (let n = 0; n < input.length; n++) { if (!kBase64Digits.includes(input[n])) - invalidCharError(); + throw lazyDOMException('Invalid character', 'InvalidCharacterError'); } return Buffer.from(input, 'base64').toString('latin1'); }