lib: optimize webidl conversion options

Replace object spread in nested WebIDL conversion
options with stable-shape ordinary objects. This keeps
hot dictionary and sequence conversion paths from
allocating null-prototype spread results.

Apply the same pattern to Web Crypto converter wrappers
that override allowResizable or enable [EnforceRange].

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/62756
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
Filip Skokan authored and Antoine du Hamel committed 2026-07-30 13:51:28 +02:00
1 parent 8bb536066d
commit 1ebbbd59cf
2 files changed
+73 -37

No files matched your search

+41 -22
View File
@@ -106,6 +106,22 @@ converters['sequence<KeyUsage>'] = createSequenceConverter(converters.KeyUsage);
converters.HashAlgorithmIdentifier = converters.AlgorithmIdentifier;
/**
* Builds conversion options for Web Crypto integer members that use Web IDL
* [EnforceRange]. Keep this helper instead of spreading opts in each member
* converter so the hot dictionary paths allocate stable-shape objects.
* @param {object} opts Parent conversion options.
* @returns {object}
*/
function enforceRangeOptions(opts) {
return {
prefix: opts.prefix,
context: opts.context,
code: opts.code,
enforceRange: true,
};
}
const dictAlgorithm = [
{
key: 'name',
@@ -121,8 +137,9 @@ converters.Algorithm = createDictionaryConverter(
// converters.BigInteger = webidl.Uint8Array;
converters.BigInteger = (V, opts = kEmptyObject) => {
return webidl.Uint8Array(V, {
__proto__: null,
...opts,
prefix: opts.prefix,
context: opts.context,
code: opts.code,
allowResizable: true,
allowShared: false,
});
@@ -132,10 +149,12 @@ converters.BigInteger = (V, opts = kEmptyObject) => {
// removing this altogether.
converters.BufferSource = (V, opts = kEmptyObject) => {
return webidl.BufferSource(V, {
__proto__: null,
...opts,
prefix: opts.prefix,
context: opts.context,
code: opts.code,
allowResizable: opts.allowResizable === undefined ?
true : opts.allowResizable,
allowShared: opts.allowShared,
});
};
@@ -143,7 +162,7 @@ const dictRsaKeyGenParams = [
{
key: 'modulusLength',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
required: true,
},
{
@@ -221,7 +240,7 @@ converters.AesKeyGenParams = createDictionaryConverter(
{
key: 'length',
converter: (V, opts) =>
converters['unsigned short'](V, { ...opts, enforceRange: true }),
converters['unsigned short'](V, enforceRangeOptions(opts)),
validator: AESLengthValidator,
required: true,
},
@@ -244,7 +263,7 @@ converters.RsaPssParams = createDictionaryConverter(
{
key: 'saltLength',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
required: true,
},
],
@@ -288,7 +307,7 @@ for (const { 0: name, 1: zeroError } of [['HmacKeyGenParams', 'OperationError'],
{
key: 'length',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: validateMacKeyLength(`${name}.length`, zeroError),
},
],
@@ -370,7 +389,7 @@ converters.CShakeParams = createDictionaryConverter(
{
key: 'outputLength',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V, opts) => {
// The Web Crypto spec allows for SHAKE output length that are not multiples of
// 8. We don't.
@@ -404,7 +423,7 @@ converters.Pbkdf2Params = createDictionaryConverter(
{
key: 'iterations',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V, dict) => {
if (V === 0)
throw lazyDOMException('iterations cannot be zero', 'OperationError');
@@ -427,7 +446,7 @@ converters.AesDerivedKeyParams = createDictionaryConverter(
{
key: 'length',
converter: (V, opts) =>
converters['unsigned short'](V, { ...opts, enforceRange: true }),
converters['unsigned short'](V, enforceRangeOptions(opts)),
validator: AESLengthValidator,
required: true,
},
@@ -481,7 +500,7 @@ converters.AeadParams = createDictionaryConverter(
{
key: 'tagLength',
converter: (V, opts) =>
converters.octet(V, { ...opts, enforceRange: true }),
converters.octet(V, enforceRangeOptions(opts)),
validator: (V, dict) => {
switch (StringPrototypeToLowerCase(dict.name)) {
case 'chacha20-poly1305':
@@ -524,7 +543,7 @@ converters.AesCtrParams = createDictionaryConverter(
{
key: 'length',
converter: (V, opts) =>
converters.octet(V, { ...opts, enforceRange: true }),
converters.octet(V, enforceRangeOptions(opts)),
validator: (V, dict) => {
if (V === 0 || V > 128)
throw lazyDOMException(
@@ -604,7 +623,7 @@ converters.Argon2Params = createDictionaryConverter(
{
key: 'parallelism',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V, dict) => {
if (V === 0 || V > MathPow(2, 24) - 1) {
throw lazyDOMException(
@@ -617,7 +636,7 @@ converters.Argon2Params = createDictionaryConverter(
{
key: 'memory',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V, dict) => {
if (V < 8 * dict.parallelism) {
throw lazyDOMException(
@@ -630,7 +649,7 @@ converters.Argon2Params = createDictionaryConverter(
{
key: 'passes',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V) => {
if (V === 0) {
throw lazyDOMException('passes must be > 0', 'OperationError');
@@ -641,7 +660,7 @@ converters.Argon2Params = createDictionaryConverter(
{
key: 'version',
converter: (V, opts) =>
converters.octet(V, { ...opts, enforceRange: true }),
converters.octet(V, enforceRangeOptions(opts)),
validator: (V, dict) => {
if (V !== 0x13) {
throw lazyDOMException(
@@ -680,7 +699,7 @@ for (const { 0: name, 1: zeroError } of [['KmacKeyGenParams', 'OperationError'],
{
key: 'length',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: validateMacKeyLength(`${name}.length`, zeroError),
},
],
@@ -694,7 +713,7 @@ converters.KmacParams = createDictionaryConverter(
{
key: 'outputLength',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V, opts) => {
// The Web Crypto spec allows for KMAC output length that are not multiples of 8. We don't.
if (V % 8)
@@ -716,7 +735,7 @@ converters.KangarooTwelveParams = createDictionaryConverter(
{
key: 'outputLength',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V, opts) => {
if (V === 0 || V % 8)
throw lazyDOMException('Invalid KangarooTwelveParams outputLength', 'OperationError');
@@ -737,7 +756,7 @@ converters.TurboShakeParams = createDictionaryConverter(
{
key: 'outputLength',
converter: (V, opts) =>
converters['unsigned long'](V, { ...opts, enforceRange: true }),
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V, opts) => {
if (V === 0 || V % 8)
throw lazyDOMException('Invalid TurboShakeParams outputLength', 'OperationError');
@@ -747,7 +766,7 @@ converters.TurboShakeParams = createDictionaryConverter(
{
key: 'domainSeparation',
converter: (V, opts) =>
converters.octet(V, { ...opts, enforceRange: true }),
converters.octet(V, enforceRangeOptions(opts)),
validator: (V) => {
if (V < 0x01 || V > 0x7F) {
throw lazyDOMException(
+32 -15
View File
@@ -109,6 +109,28 @@ function makeException(message, options = kEmptyObject) {
);
}
/**
* Builds derived conversion options for nested converter calls and adjusted
* error codes. These objects are allocated on dictionary/sequence conversion
* hot paths, so keep their shape stable and avoid object spread and
* null-prototype objects.
* @param {ConversionOptions} options Parent conversion options.
* @param {string} [context] Replacement context.
* @param {string} [code] Replacement error code.
* @returns {ConversionOptions}
*/
function makeOptions(options, context = options.context, code = options.code) {
return {
prefix: options.prefix,
context,
code,
enforceRange: options.enforceRange,
clamp: options.clamp,
allowShared: options.allowShared,
allowResizable: options.allowResizable,
};
}
/**
* Returns the ECMAScript specification type of a JavaScript value.
* @see https://tc39.es/ecma262/#sec-ecmascript-data-types-and-values
@@ -376,7 +398,7 @@ function convertToInt(
if (integer < lowerBound || integer > upperBound) {
throw makeException(
`is outside the expected range of ${lowerBound} to ${upperBound}.`,
{ __proto__: null, ...options, code: 'ERR_OUT_OF_RANGE' });
makeOptions(options, options.context, 'ERR_OUT_OF_RANGE'));
}
return integer;
@@ -416,7 +438,7 @@ function convertToInt(
if (x < lowerBound || x > upperBound) {
throw makeException(
`is outside the expected range of ${lowerBound} to ${upperBound}.`,
{ __proto__: null, ...options, code: 'ERR_OUT_OF_RANGE' });
makeOptions(options, options.context, 'ERR_OUT_OF_RANGE'));
}
return x;
@@ -593,7 +615,7 @@ function requiredArguments(length, required, options = kEmptyObject) {
`${required} argument${
required === 1 ? '' : 's'
} required, but only ${length} present.`,
{ __proto__: null, ...options, context: '', code: 'ERR_MISSING_ARGS' });
makeOptions(options, '', 'ERR_MISSING_ARGS'));
}
}
@@ -615,7 +637,7 @@ function createEnumConverter(name, values) {
if (!E.has(S)) {
throw makeException(
`'${S}' is not a valid enum value of type ${name}.`,
{ __proto__: null, ...options, code: 'ERR_INVALID_ARG_VALUE' });
makeOptions(options, options.context, 'ERR_INVALID_ARG_VALUE'));
}
// Step 3: return the matching enumeration value.
@@ -715,11 +737,7 @@ function createDictionaryConverter(
// Step 4.1.4.1: convert the JavaScript value to IDL.
const idlMemberValue = converter(
jsMemberValue,
{
__proto__: null,
...options,
context: dictionaryMemberContext(key, options),
},
makeOptions(options, dictionaryMemberContext(key, options)),
);
// Validators are a Node.js extension after conversion. They let
// consumers reject known unsupported values while dictionary
@@ -736,7 +754,7 @@ function createDictionaryConverter(
// Step 4.1.6: required missing members throw.
throw makeException(
missingDictionaryMemberMessage(dictionaryName, key),
{ __proto__: null, ...options, code: 'ERR_MISSING_OPTION' });
makeOptions(options, options.context, 'ERR_MISSING_OPTION'));
}
}
}
@@ -794,11 +812,10 @@ function createSequenceConverter(converter) {
break;
}
// Step 3.3: convert next to an IDL value of type T.
const idlValue = converter(next.value, {
__proto__: null,
...options,
context: sequenceElementContext(idlSequence.length, options),
});
const idlValue = converter(
next.value,
makeOptions(options, sequenceElementContext(idlSequence.length, options)),
);
// Step 3.4: store the value and advance i.
ArrayPrototypePush(idlSequence, idlValue);
}