Skip to content

Commit 0e55cb7

Browse files
XadillaXtargos
authored andcommitted
lib: make lazyDOMException more common
PR-URL: #39105 Reviewed-By: Antoine du Hamel <[email protected]>
1 parent a669a19 commit 0e55cb7

15 files changed

+56
-32
lines changed

Diff for: lib/buffer.js

+3-10
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
}

Diff for: lib/internal/crypto/aes.js

+4-1
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 {

Diff for: lib/internal/crypto/diffiehellman.js

+4-1
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,

Diff for: lib/internal/crypto/dsa.js

+4-1
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) {

Diff for: lib/internal/crypto/ec.js

+4-1
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');

Diff for: lib/internal/crypto/hkdf.js

+4-1
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,

Diff for: lib/internal/crypto/mac.js

+4-1
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,

Diff for: lib/internal/crypto/pbkdf2.js

+4-1
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;

Diff for: lib/internal/crypto/random.js

+1-1
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

Diff for: lib/internal/crypto/rsa.js

+4-1
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');

Diff for: lib/internal/crypto/scrypt.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ const {
3030
const {
3131
getArrayBufferOrView,
3232
getDefaultEncoding,
33-
lazyDOMException,
3433
kKeyObject,
3534
} = require('internal/crypto/util');
3635

36+
const {
37+
lazyDOMException,
38+
} = require('internal/util');
39+
3740
const defaults = {
3841
N: 16384,
3942
r: 8,

Diff for: lib/internal/crypto/util.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const { Buffer } = require('buffer');
5151
const {
5252
cachedResult,
5353
filterDuplicateStrings,
54+
lazyDOMException,
5455
} = require('internal/util');
5556

5657
const {
@@ -70,13 +71,6 @@ function lazyRequire(name) {
7071
return ret;
7172
}
7273

73-
let DOMException;
74-
const lazyDOMException = hideStackFrames((message, name) => {
75-
if (DOMException === undefined)
76-
DOMException = internalBinding('messaging').DOMException;
77-
return new DOMException(message, name);
78-
});
79-
8074
var defaultEncoding = 'buffer';
8175

8276
function setDefaultEncoding(val) {
@@ -428,7 +422,6 @@ module.exports = {
428422
normalizeAlgorithm,
429423
normalizeHashName,
430424
hasAnyNotIn,
431-
lazyDOMException,
432425
validateBitLength,
433426
validateByteLength,
434427
validateByteSource,

Diff for: lib/internal/crypto/webcrypto.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const {
4949
const {
5050
getArrayBufferOrView,
5151
hasAnyNotIn,
52-
lazyDOMException,
5352
lazyRequire,
5453
normalizeAlgorithm,
5554
normalizeHashName,
@@ -59,6 +58,10 @@ const {
5958
kKeyObject,
6059
} = require('internal/crypto/util');
6160

61+
const {
62+
lazyDOMException,
63+
} = require('internal/util');
64+
6265
const {
6366
getRandomValues,
6467
} = require('internal/crypto/random');

Diff for: lib/internal/fs/promises.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const {
7474
validateString,
7575
} = require('internal/validators');
7676
const pathModule = require('path');
77-
const { promisify } = require('internal/util');
77+
const { lazyDOMException, promisify } = require('internal/util');
7878
const { EventEmitterMixin } = require('internal/event_target');
7979
const { watch } = require('internal/fs/watchers');
8080
const { isIterable } = require('internal/streams/utils');
@@ -209,8 +209,7 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
209209

210210
[kTransfer]() {
211211
if (this[kClosePromise] || this[kRefs] > 1) {
212-
const DOMException = internalBinding('messaging').DOMException;
213-
throw new DOMException('Cannot transfer FileHandle while in use',
212+
throw lazyDOMException('Cannot transfer FileHandle while in use',
214213
'DataCloneError');
215214
}
216215

Diff for: lib/internal/util.js

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
} = primordials;
2929

3030
const {
31+
hideStackFrames,
3132
codes: {
3233
ERR_INVALID_ARG_TYPE,
3334
ERR_NO_CRYPTO,
@@ -441,6 +442,13 @@ function createDeferredPromise() {
441442
return { promise, resolve, reject };
442443
}
443444

445+
let DOMException;
446+
const lazyDOMException = hideStackFrames((message, name) => {
447+
if (DOMException === undefined)
448+
DOMException = internalBinding('messaging').DOMException;
449+
return new DOMException(message, name);
450+
});
451+
444452
module.exports = {
445453
assertCrypto,
446454
cachedResult,
@@ -457,6 +465,7 @@ module.exports = {
457465
isError,
458466
isInsideNodeModules,
459467
join,
468+
lazyDOMException,
460469
normalizeEncoding,
461470
once,
462471
promisify,

0 commit comments

Comments
 (0)