11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// Adapted from the following sources 41cb0ef41Sopenharmony_ci// - https://github.com/jsdom/webidl-conversions 51cb0ef41Sopenharmony_ci// Copyright Domenic Denicola. Licensed under BSD-2-Clause License. 61cb0ef41Sopenharmony_ci// Original license at https://github.com/jsdom/webidl-conversions/blob/master/LICENSE.md. 71cb0ef41Sopenharmony_ci// - https://github.com/denoland/deno 81cb0ef41Sopenharmony_ci// Copyright Deno authors. Licensed under MIT License. 91cb0ef41Sopenharmony_ci// Original license at https://github.com/denoland/deno/blob/main/LICENSE.md. 101cb0ef41Sopenharmony_ci// Changes include using primordials and stripping the code down to only what 111cb0ef41Sopenharmony_ci// WebCryptoAPI needs. 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst { 141cb0ef41Sopenharmony_ci ArrayBufferIsView, 151cb0ef41Sopenharmony_ci ArrayBufferPrototype, 161cb0ef41Sopenharmony_ci ArrayPrototypePush, 171cb0ef41Sopenharmony_ci ArrayPrototypeSort, 181cb0ef41Sopenharmony_ci MathPow, 191cb0ef41Sopenharmony_ci MathTrunc, 201cb0ef41Sopenharmony_ci Number, 211cb0ef41Sopenharmony_ci NumberIsFinite, 221cb0ef41Sopenharmony_ci ObjectAssign, 231cb0ef41Sopenharmony_ci ObjectPrototypeIsPrototypeOf, 241cb0ef41Sopenharmony_ci SafeArrayIterator, 251cb0ef41Sopenharmony_ci SafeSet, 261cb0ef41Sopenharmony_ci String, 271cb0ef41Sopenharmony_ci SymbolIterator, 281cb0ef41Sopenharmony_ci TypedArrayPrototypeGetBuffer, 291cb0ef41Sopenharmony_ci TypedArrayPrototypeGetSymbolToStringTag, 301cb0ef41Sopenharmony_ci TypeError, 311cb0ef41Sopenharmony_ci globalThis: { 321cb0ef41Sopenharmony_ci SharedArrayBuffer, 331cb0ef41Sopenharmony_ci }, 341cb0ef41Sopenharmony_ci} = primordials; 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciconst { 371cb0ef41Sopenharmony_ci kEmptyObject, 381cb0ef41Sopenharmony_ci setOwnProperty, 391cb0ef41Sopenharmony_ci} = require('internal/util'); 401cb0ef41Sopenharmony_ciconst { CryptoKey } = require('internal/crypto/webcrypto'); 411cb0ef41Sopenharmony_ciconst { getDataViewOrTypedArrayBuffer } = require('internal/crypto/util'); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_cifunction codedTypeError(message, errorProperties = kEmptyObject) { 441cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-syntax 451cb0ef41Sopenharmony_ci const err = new TypeError(message); 461cb0ef41Sopenharmony_ci ObjectAssign(err, errorProperties); 471cb0ef41Sopenharmony_ci return err; 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cifunction makeException(message, opts = kEmptyObject) { 511cb0ef41Sopenharmony_ci const prefix = opts.prefix ? opts.prefix + ': ' : ''; 521cb0ef41Sopenharmony_ci const context = opts.context?.length === 0 ? 531cb0ef41Sopenharmony_ci '' : (opts.context ?? 'Value') + ' '; 541cb0ef41Sopenharmony_ci return codedTypeError( 551cb0ef41Sopenharmony_ci `${prefix}${context}${message}`, 561cb0ef41Sopenharmony_ci { code: opts.code || 'ERR_INVALID_ARG_TYPE' }, 571cb0ef41Sopenharmony_ci ); 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci// https://tc39.es/ecma262/#sec-tonumber 611cb0ef41Sopenharmony_cifunction toNumber(value, opts = kEmptyObject) { 621cb0ef41Sopenharmony_ci switch (typeof value) { 631cb0ef41Sopenharmony_ci case 'number': 641cb0ef41Sopenharmony_ci return value; 651cb0ef41Sopenharmony_ci case 'bigint': 661cb0ef41Sopenharmony_ci throw makeException( 671cb0ef41Sopenharmony_ci 'is a BigInt and cannot be converted to a number.', 681cb0ef41Sopenharmony_ci opts); 691cb0ef41Sopenharmony_ci case 'symbol': 701cb0ef41Sopenharmony_ci throw makeException( 711cb0ef41Sopenharmony_ci 'is a Symbol and cannot be converted to a number.', 721cb0ef41Sopenharmony_ci opts); 731cb0ef41Sopenharmony_ci default: 741cb0ef41Sopenharmony_ci return Number(value); 751cb0ef41Sopenharmony_ci } 761cb0ef41Sopenharmony_ci} 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_cifunction type(V) { 791cb0ef41Sopenharmony_ci if (V === null) 801cb0ef41Sopenharmony_ci return 'Null'; 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci switch (typeof V) { 831cb0ef41Sopenharmony_ci case 'undefined': 841cb0ef41Sopenharmony_ci return 'Undefined'; 851cb0ef41Sopenharmony_ci case 'boolean': 861cb0ef41Sopenharmony_ci return 'Boolean'; 871cb0ef41Sopenharmony_ci case 'number': 881cb0ef41Sopenharmony_ci return 'Number'; 891cb0ef41Sopenharmony_ci case 'string': 901cb0ef41Sopenharmony_ci return 'String'; 911cb0ef41Sopenharmony_ci case 'symbol': 921cb0ef41Sopenharmony_ci return 'Symbol'; 931cb0ef41Sopenharmony_ci case 'bigint': 941cb0ef41Sopenharmony_ci return 'BigInt'; 951cb0ef41Sopenharmony_ci case 'object': // Fall through 961cb0ef41Sopenharmony_ci case 'function': // Fall through 971cb0ef41Sopenharmony_ci default: 981cb0ef41Sopenharmony_ci // Per ES spec, typeof returns an implemention-defined value that is not 991cb0ef41Sopenharmony_ci // any of the existing ones for uncallable non-standard exotic objects. 1001cb0ef41Sopenharmony_ci // Yet Type() which the Web IDL spec depends on returns Object for such 1011cb0ef41Sopenharmony_ci // cases. So treat the default case as an object. 1021cb0ef41Sopenharmony_ci return 'Object'; 1031cb0ef41Sopenharmony_ci } 1041cb0ef41Sopenharmony_ci} 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ciconst integerPart = MathTrunc; 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci// This was updated to only consider bitlength up to 32 used by WebCryptoAPI 1091cb0ef41Sopenharmony_cifunction createIntegerConversion(bitLength) { 1101cb0ef41Sopenharmony_ci const lowerBound = 0; 1111cb0ef41Sopenharmony_ci const upperBound = MathPow(2, bitLength) - 1; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci const twoToTheBitLength = MathPow(2, bitLength); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci return (V, opts = kEmptyObject) => { 1161cb0ef41Sopenharmony_ci let x = toNumber(V, opts); 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci if (opts.enforceRange) { 1191cb0ef41Sopenharmony_ci if (!NumberIsFinite(x)) { 1201cb0ef41Sopenharmony_ci throw makeException( 1211cb0ef41Sopenharmony_ci 'is not a finite number.', 1221cb0ef41Sopenharmony_ci opts); 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci x = integerPart(x); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci if (x < lowerBound || x > upperBound) { 1281cb0ef41Sopenharmony_ci throw makeException( 1291cb0ef41Sopenharmony_ci `is outside the expected range of ${lowerBound} to ${upperBound}.`, 1301cb0ef41Sopenharmony_ci { __proto__: null, ...opts, code: 'ERR_OUT_OF_RANGE' }, 1311cb0ef41Sopenharmony_ci ); 1321cb0ef41Sopenharmony_ci } 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci return x; 1351cb0ef41Sopenharmony_ci } 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci if (!NumberIsFinite(x) || x === 0) { 1381cb0ef41Sopenharmony_ci return 0; 1391cb0ef41Sopenharmony_ci } 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci x = integerPart(x); 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci if (x >= lowerBound && x <= upperBound) { 1441cb0ef41Sopenharmony_ci return x; 1451cb0ef41Sopenharmony_ci } 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci x = x % twoToTheBitLength; 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci return x; 1501cb0ef41Sopenharmony_ci }; 1511cb0ef41Sopenharmony_ci} 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ciconst converters = {}; 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ciconverters.boolean = (val) => !!val; 1561cb0ef41Sopenharmony_ciconverters.octet = createIntegerConversion(8); 1571cb0ef41Sopenharmony_ciconverters['unsigned short'] = createIntegerConversion(16); 1581cb0ef41Sopenharmony_ciconverters['unsigned long'] = createIntegerConversion(32); 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ciconverters.DOMString = function(V, opts = kEmptyObject) { 1611cb0ef41Sopenharmony_ci if (typeof V === 'string') { 1621cb0ef41Sopenharmony_ci return V; 1631cb0ef41Sopenharmony_ci } else if (typeof V === 'symbol') { 1641cb0ef41Sopenharmony_ci throw makeException( 1651cb0ef41Sopenharmony_ci 'is a Symbol and cannot be converted to a string.', 1661cb0ef41Sopenharmony_ci opts); 1671cb0ef41Sopenharmony_ci } 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ci return String(V); 1701cb0ef41Sopenharmony_ci}; 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ciconverters.object = (V, opts) => { 1731cb0ef41Sopenharmony_ci if (type(V) !== 'Object') { 1741cb0ef41Sopenharmony_ci throw makeException( 1751cb0ef41Sopenharmony_ci 'is not an object.', 1761cb0ef41Sopenharmony_ci opts); 1771cb0ef41Sopenharmony_ci } 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci return V; 1801cb0ef41Sopenharmony_ci}; 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_cifunction isNonSharedArrayBuffer(V) { 1831cb0ef41Sopenharmony_ci return ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V); 1841cb0ef41Sopenharmony_ci} 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_cifunction isSharedArrayBuffer(V) { 1871cb0ef41Sopenharmony_ci return ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V); 1881cb0ef41Sopenharmony_ci} 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ciconverters.Uint8Array = (V, opts = kEmptyObject) => { 1911cb0ef41Sopenharmony_ci if (!ArrayBufferIsView(V) || 1921cb0ef41Sopenharmony_ci TypedArrayPrototypeGetSymbolToStringTag(V) !== 'Uint8Array') { 1931cb0ef41Sopenharmony_ci throw makeException( 1941cb0ef41Sopenharmony_ci 'is not an Uint8Array object.', 1951cb0ef41Sopenharmony_ci opts); 1961cb0ef41Sopenharmony_ci } 1971cb0ef41Sopenharmony_ci if (isSharedArrayBuffer(TypedArrayPrototypeGetBuffer(V))) { 1981cb0ef41Sopenharmony_ci throw makeException( 1991cb0ef41Sopenharmony_ci 'is a view on a SharedArrayBuffer, which is not allowed.', 2001cb0ef41Sopenharmony_ci opts); 2011cb0ef41Sopenharmony_ci } 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci return V; 2041cb0ef41Sopenharmony_ci}; 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ciconverters.BufferSource = (V, opts = kEmptyObject) => { 2071cb0ef41Sopenharmony_ci if (ArrayBufferIsView(V)) { 2081cb0ef41Sopenharmony_ci if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) { 2091cb0ef41Sopenharmony_ci throw makeException( 2101cb0ef41Sopenharmony_ci 'is a view on a SharedArrayBuffer, which is not allowed.', 2111cb0ef41Sopenharmony_ci opts); 2121cb0ef41Sopenharmony_ci } 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ci return V; 2151cb0ef41Sopenharmony_ci } 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci if (!isNonSharedArrayBuffer(V)) { 2181cb0ef41Sopenharmony_ci throw makeException( 2191cb0ef41Sopenharmony_ci 'is not instance of ArrayBuffer, Buffer, TypedArray, or DataView.', 2201cb0ef41Sopenharmony_ci opts); 2211cb0ef41Sopenharmony_ci } 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci return V; 2241cb0ef41Sopenharmony_ci}; 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ciconverters['sequence<DOMString>'] = createSequenceConverter( 2271cb0ef41Sopenharmony_ci converters.DOMString); 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_cifunction requiredArguments(length, required, opts = kEmptyObject) { 2301cb0ef41Sopenharmony_ci if (length < required) { 2311cb0ef41Sopenharmony_ci throw makeException( 2321cb0ef41Sopenharmony_ci `${required} argument${ 2331cb0ef41Sopenharmony_ci required === 1 ? '' : 's' 2341cb0ef41Sopenharmony_ci } required, but only ${length} present.`, 2351cb0ef41Sopenharmony_ci { __proto__: null, ...opts, context: '', code: 'ERR_MISSING_ARGS' }); 2361cb0ef41Sopenharmony_ci } 2371cb0ef41Sopenharmony_ci} 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_cifunction createDictionaryConverter(name, dictionaries) { 2401cb0ef41Sopenharmony_ci let hasRequiredKey = false; 2411cb0ef41Sopenharmony_ci const allMembers = []; 2421cb0ef41Sopenharmony_ci for (let i = 0; i < dictionaries.length; i++) { 2431cb0ef41Sopenharmony_ci const member = dictionaries[i]; 2441cb0ef41Sopenharmony_ci if (member.required) { 2451cb0ef41Sopenharmony_ci hasRequiredKey = true; 2461cb0ef41Sopenharmony_ci } 2471cb0ef41Sopenharmony_ci ArrayPrototypePush(allMembers, member); 2481cb0ef41Sopenharmony_ci } 2491cb0ef41Sopenharmony_ci ArrayPrototypeSort(allMembers, (a, b) => { 2501cb0ef41Sopenharmony_ci if (a.key === b.key) { 2511cb0ef41Sopenharmony_ci return 0; 2521cb0ef41Sopenharmony_ci } 2531cb0ef41Sopenharmony_ci return a.key < b.key ? -1 : 1; 2541cb0ef41Sopenharmony_ci }); 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ci return function(V, opts = kEmptyObject) { 2571cb0ef41Sopenharmony_ci const typeV = type(V); 2581cb0ef41Sopenharmony_ci switch (typeV) { 2591cb0ef41Sopenharmony_ci case 'Undefined': 2601cb0ef41Sopenharmony_ci case 'Null': 2611cb0ef41Sopenharmony_ci case 'Object': 2621cb0ef41Sopenharmony_ci break; 2631cb0ef41Sopenharmony_ci default: 2641cb0ef41Sopenharmony_ci throw makeException( 2651cb0ef41Sopenharmony_ci 'can not be converted to a dictionary', 2661cb0ef41Sopenharmony_ci opts); 2671cb0ef41Sopenharmony_ci } 2681cb0ef41Sopenharmony_ci const esDict = V; 2691cb0ef41Sopenharmony_ci const idlDict = {}; 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci // Fast path null and undefined. 2721cb0ef41Sopenharmony_ci if (V == null && !hasRequiredKey) { 2731cb0ef41Sopenharmony_ci return idlDict; 2741cb0ef41Sopenharmony_ci } 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci for (const member of new SafeArrayIterator(allMembers)) { 2771cb0ef41Sopenharmony_ci const key = member.key; 2781cb0ef41Sopenharmony_ci 2791cb0ef41Sopenharmony_ci let esMemberValue; 2801cb0ef41Sopenharmony_ci if (typeV === 'Undefined' || typeV === 'Null') { 2811cb0ef41Sopenharmony_ci esMemberValue = undefined; 2821cb0ef41Sopenharmony_ci } else { 2831cb0ef41Sopenharmony_ci esMemberValue = esDict[key]; 2841cb0ef41Sopenharmony_ci } 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci if (esMemberValue !== undefined) { 2871cb0ef41Sopenharmony_ci const context = `'${key}' of '${name}'${ 2881cb0ef41Sopenharmony_ci opts.context ? ` (${opts.context})` : '' 2891cb0ef41Sopenharmony_ci }`; 2901cb0ef41Sopenharmony_ci const converter = member.converter; 2911cb0ef41Sopenharmony_ci const idlMemberValue = converter(esMemberValue, { 2921cb0ef41Sopenharmony_ci __proto__: null, 2931cb0ef41Sopenharmony_ci ...opts, 2941cb0ef41Sopenharmony_ci context, 2951cb0ef41Sopenharmony_ci }); 2961cb0ef41Sopenharmony_ci setOwnProperty(idlDict, key, idlMemberValue); 2971cb0ef41Sopenharmony_ci } else if (member.required) { 2981cb0ef41Sopenharmony_ci throw makeException( 2991cb0ef41Sopenharmony_ci `can not be converted to '${name}' because '${key}' is required in '${name}'.`, 3001cb0ef41Sopenharmony_ci { __proto__: null, ...opts, code: 'ERR_MISSING_OPTION' }); 3011cb0ef41Sopenharmony_ci } 3021cb0ef41Sopenharmony_ci } 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci return idlDict; 3051cb0ef41Sopenharmony_ci }; 3061cb0ef41Sopenharmony_ci} 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_cifunction createEnumConverter(name, values) { 3091cb0ef41Sopenharmony_ci const E = new SafeSet(values); 3101cb0ef41Sopenharmony_ci 3111cb0ef41Sopenharmony_ci return function(V, opts = kEmptyObject) { 3121cb0ef41Sopenharmony_ci const S = String(V); 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci if (!E.has(S)) { 3151cb0ef41Sopenharmony_ci throw makeException( 3161cb0ef41Sopenharmony_ci `value '${S}' is not a valid enum value of type ${name}.`, 3171cb0ef41Sopenharmony_ci { __proto__: null, ...opts, code: 'ERR_INVALID_ARG_VALUE' }); 3181cb0ef41Sopenharmony_ci } 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci return S; 3211cb0ef41Sopenharmony_ci }; 3221cb0ef41Sopenharmony_ci} 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_cifunction createSequenceConverter(converter) { 3251cb0ef41Sopenharmony_ci return function(V, opts = kEmptyObject) { 3261cb0ef41Sopenharmony_ci if (type(V) !== 'Object') { 3271cb0ef41Sopenharmony_ci throw makeException( 3281cb0ef41Sopenharmony_ci 'can not be converted to sequence.', 3291cb0ef41Sopenharmony_ci opts); 3301cb0ef41Sopenharmony_ci } 3311cb0ef41Sopenharmony_ci const iter = V?.[SymbolIterator]?.(); 3321cb0ef41Sopenharmony_ci if (iter === undefined) { 3331cb0ef41Sopenharmony_ci throw makeException( 3341cb0ef41Sopenharmony_ci 'can not be converted to sequence.', 3351cb0ef41Sopenharmony_ci opts); 3361cb0ef41Sopenharmony_ci } 3371cb0ef41Sopenharmony_ci const array = []; 3381cb0ef41Sopenharmony_ci while (true) { 3391cb0ef41Sopenharmony_ci const res = iter?.next?.(); 3401cb0ef41Sopenharmony_ci if (res === undefined) { 3411cb0ef41Sopenharmony_ci throw makeException( 3421cb0ef41Sopenharmony_ci 'can not be converted to sequence.', 3431cb0ef41Sopenharmony_ci opts); 3441cb0ef41Sopenharmony_ci } 3451cb0ef41Sopenharmony_ci if (res.done === true) break; 3461cb0ef41Sopenharmony_ci const val = converter(res.value, { 3471cb0ef41Sopenharmony_ci __proto__: null, 3481cb0ef41Sopenharmony_ci ...opts, 3491cb0ef41Sopenharmony_ci context: `${opts.context}, index ${array.length}`, 3501cb0ef41Sopenharmony_ci }); 3511cb0ef41Sopenharmony_ci ArrayPrototypePush(array, val); 3521cb0ef41Sopenharmony_ci } 3531cb0ef41Sopenharmony_ci return array; 3541cb0ef41Sopenharmony_ci }; 3551cb0ef41Sopenharmony_ci} 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_cifunction createInterfaceConverter(name, prototype) { 3581cb0ef41Sopenharmony_ci return (V, opts) => { 3591cb0ef41Sopenharmony_ci if (!ObjectPrototypeIsPrototypeOf(prototype, V)) { 3601cb0ef41Sopenharmony_ci throw makeException( 3611cb0ef41Sopenharmony_ci `is not of type ${name}.`, 3621cb0ef41Sopenharmony_ci opts); 3631cb0ef41Sopenharmony_ci } 3641cb0ef41Sopenharmony_ci return V; 3651cb0ef41Sopenharmony_ci }; 3661cb0ef41Sopenharmony_ci} 3671cb0ef41Sopenharmony_ci 3681cb0ef41Sopenharmony_ciconverters.AlgorithmIdentifier = (V, opts) => { 3691cb0ef41Sopenharmony_ci // Union for (object or DOMString) 3701cb0ef41Sopenharmony_ci if (type(V) === 'Object') { 3711cb0ef41Sopenharmony_ci return converters.object(V, opts); 3721cb0ef41Sopenharmony_ci } 3731cb0ef41Sopenharmony_ci return converters.DOMString(V, opts); 3741cb0ef41Sopenharmony_ci}; 3751cb0ef41Sopenharmony_ci 3761cb0ef41Sopenharmony_ciconverters.KeyFormat = createEnumConverter('KeyFormat', [ 3771cb0ef41Sopenharmony_ci 'raw', 3781cb0ef41Sopenharmony_ci 'pkcs8', 3791cb0ef41Sopenharmony_ci 'spki', 3801cb0ef41Sopenharmony_ci 'jwk', 3811cb0ef41Sopenharmony_ci]); 3821cb0ef41Sopenharmony_ci 3831cb0ef41Sopenharmony_ciconverters.KeyUsage = createEnumConverter('KeyUsage', [ 3841cb0ef41Sopenharmony_ci 'encrypt', 3851cb0ef41Sopenharmony_ci 'decrypt', 3861cb0ef41Sopenharmony_ci 'sign', 3871cb0ef41Sopenharmony_ci 'verify', 3881cb0ef41Sopenharmony_ci 'deriveKey', 3891cb0ef41Sopenharmony_ci 'deriveBits', 3901cb0ef41Sopenharmony_ci 'wrapKey', 3911cb0ef41Sopenharmony_ci 'unwrapKey', 3921cb0ef41Sopenharmony_ci]); 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ciconverters['sequence<KeyUsage>'] = createSequenceConverter(converters.KeyUsage); 3951cb0ef41Sopenharmony_ci 3961cb0ef41Sopenharmony_ciconverters.HashAlgorithmIdentifier = converters.AlgorithmIdentifier; 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_ciconst dictAlgorithm = [ 3991cb0ef41Sopenharmony_ci { 4001cb0ef41Sopenharmony_ci key: 'name', 4011cb0ef41Sopenharmony_ci converter: converters.DOMString, 4021cb0ef41Sopenharmony_ci required: true, 4031cb0ef41Sopenharmony_ci }, 4041cb0ef41Sopenharmony_ci]; 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ciconverters.Algorithm = createDictionaryConverter( 4071cb0ef41Sopenharmony_ci 'Algorithm', dictAlgorithm); 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ciconverters.BigInteger = converters.Uint8Array; 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ciconst dictRsaKeyGenParams = [ 4121cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 4131cb0ef41Sopenharmony_ci { 4141cb0ef41Sopenharmony_ci key: 'modulusLength', 4151cb0ef41Sopenharmony_ci converter: (V, opts) => 4161cb0ef41Sopenharmony_ci converters['unsigned long'](V, { ...opts, enforceRange: true }), 4171cb0ef41Sopenharmony_ci required: true, 4181cb0ef41Sopenharmony_ci }, 4191cb0ef41Sopenharmony_ci { 4201cb0ef41Sopenharmony_ci key: 'publicExponent', 4211cb0ef41Sopenharmony_ci converter: converters.BigInteger, 4221cb0ef41Sopenharmony_ci required: true, 4231cb0ef41Sopenharmony_ci }, 4241cb0ef41Sopenharmony_ci]; 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ciconverters.RsaKeyGenParams = createDictionaryConverter( 4271cb0ef41Sopenharmony_ci 'RsaKeyGenParams', dictRsaKeyGenParams); 4281cb0ef41Sopenharmony_ci 4291cb0ef41Sopenharmony_ciconverters.RsaHashedKeyGenParams = createDictionaryConverter( 4301cb0ef41Sopenharmony_ci 'RsaHashedKeyGenParams', [ 4311cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictRsaKeyGenParams), 4321cb0ef41Sopenharmony_ci { 4331cb0ef41Sopenharmony_ci key: 'hash', 4341cb0ef41Sopenharmony_ci converter: converters.HashAlgorithmIdentifier, 4351cb0ef41Sopenharmony_ci required: true, 4361cb0ef41Sopenharmony_ci }, 4371cb0ef41Sopenharmony_ci ]); 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_ciconverters.RsaHashedImportParams = createDictionaryConverter( 4401cb0ef41Sopenharmony_ci 'RsaHashedImportParams', [ 4411cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 4421cb0ef41Sopenharmony_ci { 4431cb0ef41Sopenharmony_ci key: 'hash', 4441cb0ef41Sopenharmony_ci converter: converters.HashAlgorithmIdentifier, 4451cb0ef41Sopenharmony_ci required: true, 4461cb0ef41Sopenharmony_ci }, 4471cb0ef41Sopenharmony_ci ]); 4481cb0ef41Sopenharmony_ci 4491cb0ef41Sopenharmony_ciconverters.NamedCurve = converters.DOMString; 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ciconverters.EcKeyImportParams = createDictionaryConverter( 4521cb0ef41Sopenharmony_ci 'EcKeyImportParams', [ 4531cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 4541cb0ef41Sopenharmony_ci { 4551cb0ef41Sopenharmony_ci key: 'namedCurve', 4561cb0ef41Sopenharmony_ci converter: converters.NamedCurve, 4571cb0ef41Sopenharmony_ci required: true, 4581cb0ef41Sopenharmony_ci }, 4591cb0ef41Sopenharmony_ci ]); 4601cb0ef41Sopenharmony_ci 4611cb0ef41Sopenharmony_ciconverters.EcKeyGenParams = createDictionaryConverter( 4621cb0ef41Sopenharmony_ci 'EcKeyGenParams', [ 4631cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 4641cb0ef41Sopenharmony_ci { 4651cb0ef41Sopenharmony_ci key: 'namedCurve', 4661cb0ef41Sopenharmony_ci converter: converters.NamedCurve, 4671cb0ef41Sopenharmony_ci required: true, 4681cb0ef41Sopenharmony_ci }, 4691cb0ef41Sopenharmony_ci ]); 4701cb0ef41Sopenharmony_ci 4711cb0ef41Sopenharmony_ciconverters.AesKeyGenParams = createDictionaryConverter( 4721cb0ef41Sopenharmony_ci 'AesKeyGenParams', [ 4731cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 4741cb0ef41Sopenharmony_ci { 4751cb0ef41Sopenharmony_ci key: 'length', 4761cb0ef41Sopenharmony_ci converter: (V, opts) => 4771cb0ef41Sopenharmony_ci converters['unsigned short'](V, { ...opts, enforceRange: true }), 4781cb0ef41Sopenharmony_ci required: true, 4791cb0ef41Sopenharmony_ci }, 4801cb0ef41Sopenharmony_ci ]); 4811cb0ef41Sopenharmony_ci 4821cb0ef41Sopenharmony_ciconverters.HmacKeyGenParams = createDictionaryConverter( 4831cb0ef41Sopenharmony_ci 'HmacKeyGenParams', [ 4841cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 4851cb0ef41Sopenharmony_ci { 4861cb0ef41Sopenharmony_ci key: 'hash', 4871cb0ef41Sopenharmony_ci converter: converters.HashAlgorithmIdentifier, 4881cb0ef41Sopenharmony_ci required: true, 4891cb0ef41Sopenharmony_ci }, 4901cb0ef41Sopenharmony_ci { 4911cb0ef41Sopenharmony_ci key: 'length', 4921cb0ef41Sopenharmony_ci converter: (V, opts) => 4931cb0ef41Sopenharmony_ci converters['unsigned long'](V, { ...opts, enforceRange: true }), 4941cb0ef41Sopenharmony_ci }, 4951cb0ef41Sopenharmony_ci ]); 4961cb0ef41Sopenharmony_ci 4971cb0ef41Sopenharmony_ciconverters.RsaPssParams = createDictionaryConverter( 4981cb0ef41Sopenharmony_ci 'RsaPssParams', [ 4991cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 5001cb0ef41Sopenharmony_ci { 5011cb0ef41Sopenharmony_ci key: 'saltLength', 5021cb0ef41Sopenharmony_ci converter: (V, opts) => 5031cb0ef41Sopenharmony_ci converters['unsigned long'](V, { ...opts, enforceRange: true }), 5041cb0ef41Sopenharmony_ci required: true, 5051cb0ef41Sopenharmony_ci }, 5061cb0ef41Sopenharmony_ci ]); 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ciconverters.RsaOaepParams = createDictionaryConverter( 5091cb0ef41Sopenharmony_ci 'RsaOaepParams', [ 5101cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 5111cb0ef41Sopenharmony_ci { 5121cb0ef41Sopenharmony_ci key: 'label', 5131cb0ef41Sopenharmony_ci converter: converters.BufferSource, 5141cb0ef41Sopenharmony_ci }, 5151cb0ef41Sopenharmony_ci ]); 5161cb0ef41Sopenharmony_ci 5171cb0ef41Sopenharmony_ciconverters.EcdsaParams = createDictionaryConverter( 5181cb0ef41Sopenharmony_ci 'EcdsaParams', [ 5191cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 5201cb0ef41Sopenharmony_ci { 5211cb0ef41Sopenharmony_ci key: 'hash', 5221cb0ef41Sopenharmony_ci converter: converters.HashAlgorithmIdentifier, 5231cb0ef41Sopenharmony_ci required: true, 5241cb0ef41Sopenharmony_ci }, 5251cb0ef41Sopenharmony_ci ]); 5261cb0ef41Sopenharmony_ci 5271cb0ef41Sopenharmony_ciconverters.HmacImportParams = createDictionaryConverter( 5281cb0ef41Sopenharmony_ci 'HmacImportParams', [ 5291cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 5301cb0ef41Sopenharmony_ci { 5311cb0ef41Sopenharmony_ci key: 'hash', 5321cb0ef41Sopenharmony_ci converter: converters.HashAlgorithmIdentifier, 5331cb0ef41Sopenharmony_ci required: true, 5341cb0ef41Sopenharmony_ci }, 5351cb0ef41Sopenharmony_ci { 5361cb0ef41Sopenharmony_ci key: 'length', 5371cb0ef41Sopenharmony_ci converter: (V, opts) => 5381cb0ef41Sopenharmony_ci converters['unsigned long'](V, { ...opts, enforceRange: true }), 5391cb0ef41Sopenharmony_ci }, 5401cb0ef41Sopenharmony_ci ]); 5411cb0ef41Sopenharmony_ci 5421cb0ef41Sopenharmony_ciconst simpleDomStringKey = (key) => ({ key, converter: converters.DOMString }); 5431cb0ef41Sopenharmony_ci 5441cb0ef41Sopenharmony_ciconverters.RsaOtherPrimesInfo = createDictionaryConverter( 5451cb0ef41Sopenharmony_ci 'RsaOtherPrimesInfo', [ 5461cb0ef41Sopenharmony_ci simpleDomStringKey('r'), 5471cb0ef41Sopenharmony_ci simpleDomStringKey('d'), 5481cb0ef41Sopenharmony_ci simpleDomStringKey('t'), 5491cb0ef41Sopenharmony_ci ]); 5501cb0ef41Sopenharmony_ciconverters['sequence<RsaOtherPrimesInfo>'] = createSequenceConverter( 5511cb0ef41Sopenharmony_ci converters.RsaOtherPrimesInfo); 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ciconverters.JsonWebKey = createDictionaryConverter( 5541cb0ef41Sopenharmony_ci 'JsonWebKey', [ 5551cb0ef41Sopenharmony_ci simpleDomStringKey('kty'), 5561cb0ef41Sopenharmony_ci simpleDomStringKey('use'), 5571cb0ef41Sopenharmony_ci { 5581cb0ef41Sopenharmony_ci key: 'key_ops', 5591cb0ef41Sopenharmony_ci converter: converters['sequence<DOMString>'], 5601cb0ef41Sopenharmony_ci }, 5611cb0ef41Sopenharmony_ci simpleDomStringKey('alg'), 5621cb0ef41Sopenharmony_ci { 5631cb0ef41Sopenharmony_ci key: 'ext', 5641cb0ef41Sopenharmony_ci converter: converters.boolean, 5651cb0ef41Sopenharmony_ci }, 5661cb0ef41Sopenharmony_ci simpleDomStringKey('crv'), 5671cb0ef41Sopenharmony_ci simpleDomStringKey('x'), 5681cb0ef41Sopenharmony_ci simpleDomStringKey('y'), 5691cb0ef41Sopenharmony_ci simpleDomStringKey('d'), 5701cb0ef41Sopenharmony_ci simpleDomStringKey('n'), 5711cb0ef41Sopenharmony_ci simpleDomStringKey('e'), 5721cb0ef41Sopenharmony_ci simpleDomStringKey('p'), 5731cb0ef41Sopenharmony_ci simpleDomStringKey('q'), 5741cb0ef41Sopenharmony_ci simpleDomStringKey('dp'), 5751cb0ef41Sopenharmony_ci simpleDomStringKey('dq'), 5761cb0ef41Sopenharmony_ci simpleDomStringKey('qi'), 5771cb0ef41Sopenharmony_ci { 5781cb0ef41Sopenharmony_ci key: 'oth', 5791cb0ef41Sopenharmony_ci converter: converters['sequence<RsaOtherPrimesInfo>'], 5801cb0ef41Sopenharmony_ci }, 5811cb0ef41Sopenharmony_ci simpleDomStringKey('k'), 5821cb0ef41Sopenharmony_ci ]); 5831cb0ef41Sopenharmony_ci 5841cb0ef41Sopenharmony_ciconverters.HkdfParams = createDictionaryConverter( 5851cb0ef41Sopenharmony_ci 'HkdfParams', [ 5861cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 5871cb0ef41Sopenharmony_ci { 5881cb0ef41Sopenharmony_ci key: 'hash', 5891cb0ef41Sopenharmony_ci converter: converters.HashAlgorithmIdentifier, 5901cb0ef41Sopenharmony_ci required: true, 5911cb0ef41Sopenharmony_ci }, 5921cb0ef41Sopenharmony_ci { 5931cb0ef41Sopenharmony_ci key: 'salt', 5941cb0ef41Sopenharmony_ci converter: converters.BufferSource, 5951cb0ef41Sopenharmony_ci required: true, 5961cb0ef41Sopenharmony_ci }, 5971cb0ef41Sopenharmony_ci { 5981cb0ef41Sopenharmony_ci key: 'info', 5991cb0ef41Sopenharmony_ci converter: converters.BufferSource, 6001cb0ef41Sopenharmony_ci required: true, 6011cb0ef41Sopenharmony_ci }, 6021cb0ef41Sopenharmony_ci ]); 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ciconverters.Pbkdf2Params = createDictionaryConverter( 6051cb0ef41Sopenharmony_ci 'Pbkdf2Params', [ 6061cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 6071cb0ef41Sopenharmony_ci { 6081cb0ef41Sopenharmony_ci key: 'hash', 6091cb0ef41Sopenharmony_ci converter: converters.HashAlgorithmIdentifier, 6101cb0ef41Sopenharmony_ci required: true, 6111cb0ef41Sopenharmony_ci }, 6121cb0ef41Sopenharmony_ci { 6131cb0ef41Sopenharmony_ci key: 'iterations', 6141cb0ef41Sopenharmony_ci converter: (V, opts) => 6151cb0ef41Sopenharmony_ci converters['unsigned long'](V, { ...opts, enforceRange: true }), 6161cb0ef41Sopenharmony_ci required: true, 6171cb0ef41Sopenharmony_ci }, 6181cb0ef41Sopenharmony_ci { 6191cb0ef41Sopenharmony_ci key: 'salt', 6201cb0ef41Sopenharmony_ci converter: converters.BufferSource, 6211cb0ef41Sopenharmony_ci required: true, 6221cb0ef41Sopenharmony_ci }, 6231cb0ef41Sopenharmony_ci ]); 6241cb0ef41Sopenharmony_ci 6251cb0ef41Sopenharmony_ciconverters.AesDerivedKeyParams = createDictionaryConverter( 6261cb0ef41Sopenharmony_ci 'AesDerivedKeyParams', [ 6271cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 6281cb0ef41Sopenharmony_ci { 6291cb0ef41Sopenharmony_ci key: 'length', 6301cb0ef41Sopenharmony_ci converter: (V, opts) => 6311cb0ef41Sopenharmony_ci converters['unsigned short'](V, { ...opts, enforceRange: true }), 6321cb0ef41Sopenharmony_ci required: true, 6331cb0ef41Sopenharmony_ci }, 6341cb0ef41Sopenharmony_ci ]); 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ciconverters.AesCbcParams = createDictionaryConverter( 6371cb0ef41Sopenharmony_ci 'AesCbcParams', [ 6381cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 6391cb0ef41Sopenharmony_ci { 6401cb0ef41Sopenharmony_ci key: 'iv', 6411cb0ef41Sopenharmony_ci converter: converters.BufferSource, 6421cb0ef41Sopenharmony_ci required: true, 6431cb0ef41Sopenharmony_ci }, 6441cb0ef41Sopenharmony_ci ]); 6451cb0ef41Sopenharmony_ci 6461cb0ef41Sopenharmony_ciconverters.AesGcmParams = createDictionaryConverter( 6471cb0ef41Sopenharmony_ci 'AesGcmParams', [ 6481cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 6491cb0ef41Sopenharmony_ci { 6501cb0ef41Sopenharmony_ci key: 'iv', 6511cb0ef41Sopenharmony_ci converter: converters.BufferSource, 6521cb0ef41Sopenharmony_ci required: true, 6531cb0ef41Sopenharmony_ci }, 6541cb0ef41Sopenharmony_ci { 6551cb0ef41Sopenharmony_ci key: 'tagLength', 6561cb0ef41Sopenharmony_ci converter: (V, opts) => 6571cb0ef41Sopenharmony_ci converters.octet(V, { ...opts, enforceRange: true }), 6581cb0ef41Sopenharmony_ci }, 6591cb0ef41Sopenharmony_ci { 6601cb0ef41Sopenharmony_ci key: 'additionalData', 6611cb0ef41Sopenharmony_ci converter: converters.BufferSource, 6621cb0ef41Sopenharmony_ci }, 6631cb0ef41Sopenharmony_ci ]); 6641cb0ef41Sopenharmony_ci 6651cb0ef41Sopenharmony_ciconverters.AesCtrParams = createDictionaryConverter( 6661cb0ef41Sopenharmony_ci 'AesCtrParams', [ 6671cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 6681cb0ef41Sopenharmony_ci { 6691cb0ef41Sopenharmony_ci key: 'counter', 6701cb0ef41Sopenharmony_ci converter: converters.BufferSource, 6711cb0ef41Sopenharmony_ci required: true, 6721cb0ef41Sopenharmony_ci }, 6731cb0ef41Sopenharmony_ci { 6741cb0ef41Sopenharmony_ci key: 'length', 6751cb0ef41Sopenharmony_ci converter: (V, opts) => 6761cb0ef41Sopenharmony_ci converters.octet(V, { ...opts, enforceRange: true }), 6771cb0ef41Sopenharmony_ci required: true, 6781cb0ef41Sopenharmony_ci }, 6791cb0ef41Sopenharmony_ci ]); 6801cb0ef41Sopenharmony_ci 6811cb0ef41Sopenharmony_ciconverters.CryptoKey = createInterfaceConverter( 6821cb0ef41Sopenharmony_ci 'CryptoKey', CryptoKey.prototype); 6831cb0ef41Sopenharmony_ci 6841cb0ef41Sopenharmony_ciconverters.EcdhKeyDeriveParams = createDictionaryConverter( 6851cb0ef41Sopenharmony_ci 'EcdhKeyDeriveParams', [ 6861cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 6871cb0ef41Sopenharmony_ci { 6881cb0ef41Sopenharmony_ci key: 'public', 6891cb0ef41Sopenharmony_ci converter: converters.CryptoKey, 6901cb0ef41Sopenharmony_ci required: true, 6911cb0ef41Sopenharmony_ci }, 6921cb0ef41Sopenharmony_ci ]); 6931cb0ef41Sopenharmony_ci 6941cb0ef41Sopenharmony_ciconverters.Ed448Params = createDictionaryConverter( 6951cb0ef41Sopenharmony_ci 'Ed448Params', [ 6961cb0ef41Sopenharmony_ci ...new SafeArrayIterator(dictAlgorithm), 6971cb0ef41Sopenharmony_ci { 6981cb0ef41Sopenharmony_ci key: 'context', 6991cb0ef41Sopenharmony_ci converter: converters.BufferSource, 7001cb0ef41Sopenharmony_ci required: false, 7011cb0ef41Sopenharmony_ci }, 7021cb0ef41Sopenharmony_ci ]); 7031cb0ef41Sopenharmony_ci 7041cb0ef41Sopenharmony_cimodule.exports = { 7051cb0ef41Sopenharmony_ci converters, 7061cb0ef41Sopenharmony_ci requiredArguments, 7071cb0ef41Sopenharmony_ci}; 708