Skip to content

Commit 0e55cb7

Browse files
XadillaXtargos
authored andcommitted
lib: make lazyDOMException more common
PR-URL: #39105 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent a669a19 commit 0e55cb7

File tree

15 files changed

+56
-32
lines changed

15 files changed

+56
-32
lines changed

lib/buffer.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const {
7575
const {
7676
customInspectSymbol,
7777
isInsideNodeModules,
78+
lazyDOMException,
7879
normalizeEncoding,
7980
kIsEncodingSymbol
8081
} = require('internal/util');
@@ -1208,22 +1209,14 @@ if (internalBinding('config').hasIntl) {
12081209
};
12091210
}
12101211

1211-
let DOMException;
1212-
1213-
const lazyInvalidCharError = hideStackFrames((message, name) => {
1214-
if (DOMException === undefined)
1215-
DOMException = internalBinding('messaging').DOMException;
1216-
throw new DOMException('Invalid character', 'InvalidCharacterError');
1217-
});
1218-
12191212
function btoa(input) {
12201213
// The implementation here has not been performance optimized in any way and
12211214
// should not be.
12221215
// Refs: https://github.com/nodejs/node/pull/38433#issuecomment-828426932
12231216
input = `${input}`;
12241217
for (let n = 0; n < input.length; n++) {
12251218
if (input[n].charCodeAt(0) > 0xff)
1226-
lazyInvalidCharError();
1219+
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
12271220
}
12281221
const buf = Buffer.from(input, 'latin1');
12291222
return buf.toString('base64');
@@ -1239,7 +1232,7 @@ function atob(input) {
12391232
input = `${input}`;
12401233
for (let n = 0; n < input.length; n++) {
12411234
if (!kBase64Digits.includes(input[n]))
1242-
lazyInvalidCharError();
1235+
throw lazyDOMException('Invalid character', 'InvalidCharacterError');
12431236
}
12441237
return Buffer.from(input, 'base64').toString('latin1');
12451238
}

lib/internal/crypto/aes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const {
3636
getArrayBufferOrView,
3737
hasAnyNotIn,
3838
jobPromise,
39-
lazyDOMException,
4039
validateByteLength,
4140
validateKeyOps,
4241
validateMaxBufferLength,
@@ -45,6 +44,10 @@ const {
4544
kKeyObject,
4645
} = require('internal/crypto/util');
4746

47+
const {
48+
lazyDOMException,
49+
} = require('internal/util');
50+
4851
const { PromiseReject } = primordials;
4952

5053
const {

lib/internal/crypto/diffiehellman.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const {
4747
isAnyArrayBuffer,
4848
} = require('internal/util/types');
4949

50+
const {
51+
lazyDOMException,
52+
} = require('internal/util');
53+
5054
const {
5155
KeyObject,
5256
InternalCryptoKey,
@@ -66,7 +70,6 @@ const {
6670
getUsagesUnion,
6771
hasAnyNotIn,
6872
jobPromise,
69-
lazyDOMException,
7073
toBuf,
7174
kHandle,
7275
kKeyObject,

lib/internal/crypto/dsa.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ const {
4444
getUsagesUnion,
4545
hasAnyNotIn,
4646
jobPromise,
47-
lazyDOMException,
4847
normalizeHashName,
4948
validateKeyOps,
5049
kKeyObject,
5150
kHandle,
5251
} = require('internal/crypto/util');
5352

53+
const {
54+
lazyDOMException,
55+
} = require('internal/util');
56+
5457
function verifyAcceptableDsaKeyUse(name, type, usages) {
5558
let checkSet;
5659
switch (type) {

lib/internal/crypto/ec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ const {
3838
getUsagesUnion,
3939
hasAnyNotIn,
4040
jobPromise,
41-
lazyDOMException,
4241
normalizeHashName,
4342
validateKeyOps,
4443
kHandle,
4544
kKeyObject,
4645
kNamedCurveAliases,
4746
} = require('internal/crypto/util');
4847

48+
const {
49+
lazyDOMException,
50+
} = require('internal/util');
51+
4952
const {
5053
generateKeyPair,
5154
} = require('internal/crypto/keygen');

lib/internal/crypto/hkdf.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const { kMaxLength } = require('buffer');
2323

2424
const {
2525
getArrayBufferOrView,
26-
lazyDOMException,
2726
normalizeHashName,
2827
toBuf,
2928
validateByteSource,
@@ -35,6 +34,10 @@ const {
3534
isKeyObject,
3635
} = require('internal/crypto/keys');
3736

37+
const {
38+
lazyDOMException,
39+
} = require('internal/util');
40+
3841
const {
3942
isAnyArrayBuffer,
4043
isArrayBufferView,

lib/internal/crypto/mac.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ const {
1818
getHashLength,
1919
hasAnyNotIn,
2020
jobPromise,
21-
lazyDOMException,
2221
normalizeHashName,
2322
validateBitLength,
2423
validateKeyOps,
2524
kHandle,
2625
kKeyObject,
2726
} = require('internal/crypto/util');
2827

28+
const {
29+
lazyDOMException,
30+
} = require('internal/util');
31+
2932
const {
3033
codes: {
3134
ERR_MISSING_OPTION,

lib/internal/crypto/pbkdf2.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ const { ERR_MISSING_OPTION } = require('internal/errors').codes;
2525
const {
2626
getArrayBufferOrView,
2727
getDefaultEncoding,
28-
lazyDOMException,
2928
normalizeHashName,
3029
kKeyObject,
3130
} = require('internal/crypto/util');
3231

32+
const {
33+
lazyDOMException,
34+
} = require('internal/util');
35+
3336
function pbkdf2(password, salt, iterations, keylen, digest, callback) {
3437
if (typeof digest === 'function') {
3538
callback = digest;

lib/internal/crypto/random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const {
2727

2828
const {
2929
lazyDOMException,
30-
} = require('internal/crypto/util');
30+
} = require('internal/util');
3131

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

lib/internal/crypto/rsa.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ const {
4040
getUsagesUnion,
4141
hasAnyNotIn,
4242
jobPromise,
43-
lazyDOMException,
4443
normalizeHashName,
4544
validateKeyOps,
4645
validateMaxBufferLength,
4746
kHandle,
4847
kKeyObject,
4948
} = require('internal/crypto/util');
5049

50+
const {
51+
lazyDOMException,
52+
} = require('internal/util');
53+
5154
const {
5255
isUint8Array,
5356
} = require('internal/util/types');

0 commit comments

Comments
 (0)