11'use strict' ;
22
3- // Adapted from the following sources
4- // - https://github.com/jsdom/webidl-conversions
5- // Copyright Domenic Denicola. Licensed under BSD-2-Clause License.
6- // Original license at https://github.com/jsdom/webidl-conversions/blob/master/LICENSE.md.
7- // - https://github.com/denoland/deno
8- // Copyright Deno authors. Licensed under MIT License.
9- // Original license at https://github.com/denoland/deno/blob/main/LICENSE.md.
10- // Changes include using primordials and stripping the code down to only what
11- // WebCryptoAPI needs.
12-
133const {
14- ArrayBufferIsView,
154 ArrayPrototypeIncludes,
16- ArrayPrototypePush,
17- ArrayPrototypeSort,
185 MathPow,
19- MathTrunc,
20- Number,
21- NumberIsFinite,
226 NumberParseInt,
237 ObjectPrototypeHasOwnProperty,
24- ObjectPrototypeIsPrototypeOf,
258 SafeArrayIterator,
26- String,
279 StringPrototypeStartsWith,
2810 StringPrototypeToLowerCase,
29- TypedArrayPrototypeGetBuffer,
30- TypedArrayPrototypeGetSymbolToStringTag,
3111} = primordials ;
3212
33- const {
34- converters : sharedConverters ,
35- makeException,
36- createEnumConverter,
37- createSequenceConverter,
38- } = require ( 'internal/webidl' ) ;
39-
4013const {
4114 lazyDOMException,
4215 kEmptyObject,
43- setOwnProperty,
4416} = require ( 'internal/util' ) ;
4517const { CryptoKey } = require ( 'internal/crypto/webcrypto' ) ;
4618const {
4719 validateMaxBufferLength,
4820 kNamedCurveAliases,
4921} = require ( 'internal/crypto/util' ) ;
50- const { isSharedArrayBuffer } = require ( 'internal/util/types' ) ;
51-
52- // https://tc39.es/ecma262/#sec-tonumber
53- function toNumber ( value , opts = kEmptyObject ) {
54- switch ( typeof value ) {
55- case 'number' :
56- return value ;
57- case 'bigint' :
58- throw makeException (
59- 'is a BigInt and cannot be converted to a number.' ,
60- opts ) ;
61- case 'symbol' :
62- throw makeException (
63- 'is a Symbol and cannot be converted to a number.' ,
64- opts ) ;
65- default :
66- return Number ( value ) ;
67- }
68- }
69-
70- function type ( V ) {
71- if ( V === null )
72- return 'Null' ;
73-
74- switch ( typeof V ) {
75- case 'undefined' :
76- return 'Undefined' ;
77- case 'boolean' :
78- return 'Boolean' ;
79- case 'number' :
80- return 'Number' ;
81- case 'string' :
82- return 'String' ;
83- case 'symbol' :
84- return 'Symbol' ;
85- case 'bigint' :
86- return 'BigInt' ;
87- case 'object' : // Fall through
88- case 'function' : // Fall through
89- default :
90- // Per ES spec, typeof returns an implementation-defined value that is not
91- // any of the existing ones for uncallable non-standard exotic objects.
92- // Yet Type() which the Web IDL spec depends on returns Object for such
93- // cases. So treat the default case as an object.
94- return 'Object' ;
95- }
96- }
97-
98- const integerPart = MathTrunc ;
22+ const {
23+ converters : webidl ,
24+ createDictionaryConverter,
25+ createEnumConverter,
26+ createInterfaceConverter,
27+ createSequenceConverter,
28+ requiredArguments,
29+ type,
30+ } = require ( 'internal/webidl' ) ;
9931
10032function validateByteLength ( buf , name , target ) {
10133 if ( buf . byteLength !== target ) {
@@ -119,79 +51,7 @@ function namedCurveValidator(V, dict) {
11951 'NotSupportedError' ) ;
12052}
12153
122- // This was updated to only consider bitlength up to 32 used by WebCryptoAPI
123- function createIntegerConversion ( bitLength ) {
124- const lowerBound = 0 ;
125- const upperBound = MathPow ( 2 , bitLength ) - 1 ;
126-
127- const twoToTheBitLength = MathPow ( 2 , bitLength ) ;
128-
129- return ( V , opts = kEmptyObject ) => {
130- let x = toNumber ( V , opts ) ;
131-
132- if ( opts . enforceRange ) {
133- if ( ! NumberIsFinite ( x ) ) {
134- throw makeException (
135- 'is not a finite number.' ,
136- opts ) ;
137- }
138-
139- x = integerPart ( x ) ;
140-
141- if ( x < lowerBound || x > upperBound ) {
142- throw makeException (
143- `is outside the expected range of ${ lowerBound } to ${ upperBound } .` ,
144- { __proto__ : null , ...opts , code : 'ERR_OUT_OF_RANGE' } ,
145- ) ;
146- }
147-
148- return x ;
149- }
150-
151- if ( ! NumberIsFinite ( x ) || x === 0 ) {
152- return 0 ;
153- }
154-
155- x = integerPart ( x ) ;
156-
157- if ( x >= lowerBound && x <= upperBound ) {
158- return x ;
159- }
160-
161- x = x % twoToTheBitLength ;
162-
163- return x ;
164- } ;
165- }
166-
167- const converters = { } ;
168-
169- converters . boolean = ( val ) => ! ! val ;
170- converters . octet = createIntegerConversion ( 8 ) ;
171- converters [ 'unsigned short' ] = createIntegerConversion ( 16 ) ;
172- converters [ 'unsigned long' ] = createIntegerConversion ( 32 ) ;
173-
174- converters . DOMString = function ( V , opts = kEmptyObject ) {
175- if ( typeof V === 'string' ) {
176- return V ;
177- } else if ( typeof V === 'symbol' ) {
178- throw makeException (
179- 'is a Symbol and cannot be converted to a string.' ,
180- opts ) ;
181- }
182-
183- return String ( V ) ;
184- } ;
185-
186- converters . object = ( V , opts ) => {
187- if ( type ( V ) !== 'Object' ) {
188- throw makeException (
189- 'is not an object.' ,
190- opts ) ;
191- }
192-
193- return V ;
194- } ;
54+ const converters = { ...webidl } ;
19555
19656/**
19757 * @param {string | object } V - The hash algorithm identifier (string or object).
@@ -205,117 +65,6 @@ function ensureSHA(V, label) {
20565 `Only SHA hashes are supported in ${ label } ` , 'NotSupportedError' ) ;
20666}
20767
208- converters . Uint8Array = ( V , opts = kEmptyObject ) => {
209- if ( ! ArrayBufferIsView ( V ) ||
210- TypedArrayPrototypeGetSymbolToStringTag ( V ) !== 'Uint8Array' ) {
211- throw makeException (
212- 'is not an Uint8Array object.' ,
213- opts ) ;
214- }
215- if ( isSharedArrayBuffer ( TypedArrayPrototypeGetBuffer ( V ) ) ) {
216- throw makeException (
217- 'is a view on a SharedArrayBuffer, which is not allowed.' ,
218- opts ) ;
219- }
220-
221- return V ;
222- } ;
223-
224- converters . BufferSource = sharedConverters . BufferSource ;
225-
226- converters [ 'sequence<DOMString>' ] = createSequenceConverter (
227- converters . DOMString ) ;
228-
229- function requiredArguments ( length , required , opts = kEmptyObject ) {
230- if ( length < required ) {
231- throw makeException (
232- `${ required } argument${
233- required === 1 ? '' : 's'
234- } required, but only ${ length } present.`,
235- { __proto__ : null , ...opts , context : '' , code : 'ERR_MISSING_ARGS' } ) ;
236- }
237- }
238-
239- function createDictionaryConverter ( name , dictionaries ) {
240- let hasRequiredKey = false ;
241- const allMembers = [ ] ;
242- for ( let i = 0 ; i < dictionaries . length ; i ++ ) {
243- const member = dictionaries [ i ] ;
244- if ( member . required ) {
245- hasRequiredKey = true ;
246- }
247- ArrayPrototypePush ( allMembers , member ) ;
248- }
249- ArrayPrototypeSort ( allMembers , ( a , b ) => {
250- if ( a . key === b . key ) {
251- return 0 ;
252- }
253- return a . key < b . key ? - 1 : 1 ;
254- } ) ;
255-
256- return function ( V , opts = kEmptyObject ) {
257- const typeV = type ( V ) ;
258- switch ( typeV ) {
259- case 'Undefined' :
260- case 'Null' :
261- case 'Object' :
262- break ;
263- default :
264- throw makeException (
265- 'can not be converted to a dictionary' ,
266- opts ) ;
267- }
268- const esDict = V ;
269- const idlDict = { } ;
270-
271- // Fast path null and undefined.
272- if ( V == null && ! hasRequiredKey ) {
273- return idlDict ;
274- }
275-
276- for ( const member of new SafeArrayIterator ( allMembers ) ) {
277- const key = member . key ;
278-
279- let esMemberValue ;
280- if ( typeV === 'Undefined' || typeV === 'Null' ) {
281- esMemberValue = undefined ;
282- } else {
283- esMemberValue = esDict [ key ] ;
284- }
285-
286- if ( esMemberValue !== undefined ) {
287- const context = `'${ key } ' of '${ name } '${
288- opts . context ? ` (${ opts . context } )` : ''
289- } `;
290- const idlMemberValue = member . converter ( esMemberValue , {
291- __proto__ : null ,
292- ...opts ,
293- context,
294- } ) ;
295- member . validator ?. ( idlMemberValue , esDict ) ;
296- setOwnProperty ( idlDict , key , idlMemberValue ) ;
297- } else if ( member . required ) {
298- throw makeException (
299- `can not be converted to '${ name } ' because '${ key } ' is required in '${ name } '.` ,
300- { __proto__ : null , ...opts , code : 'ERR_MISSING_OPTION' } ) ;
301- }
302- }
303-
304- return idlDict ;
305- } ;
306- }
307-
308- function createInterfaceConverter ( name , prototype ) {
309- return ( V , opts ) => {
310- if ( ! ObjectPrototypeIsPrototypeOf ( prototype , V ) ) {
311- throw makeException (
312- `is not of type ${ name } .` ,
313- opts ) ;
314- }
315- return V ;
316- } ;
317- }
318-
31968converters . AlgorithmIdentifier = ( V , opts ) => {
32069 // Union for (object or DOMString)
32170 if ( type ( V ) === 'Object' ) {
@@ -365,7 +114,27 @@ const dictAlgorithm = [
365114converters . Algorithm = createDictionaryConverter (
366115 'Algorithm' , dictAlgorithm ) ;
367116
368- converters . BigInteger = converters . Uint8Array ;
117+ // TODO(panva): Reject resizable backing stores in a semver-major with:
118+ // converters.BigInteger = webidl.Uint8Array;
119+ converters . BigInteger = ( V , opts = kEmptyObject ) => {
120+ return webidl . Uint8Array ( V , {
121+ __proto__ : null ,
122+ ...opts ,
123+ allowResizable : true ,
124+ allowShared : false ,
125+ } ) ;
126+ } ;
127+
128+ // TODO(panva): Reject resizable backing stores in a semver-major by
129+ // removing this altogether.
130+ converters . BufferSource = ( V , opts = kEmptyObject ) => {
131+ return webidl . BufferSource ( V , {
132+ __proto__ : null ,
133+ ...opts ,
134+ allowResizable : opts . allowResizable === undefined ?
135+ true : opts . allowResizable ,
136+ } ) ;
137+ } ;
369138
370139const dictRsaKeyGenParams = [
371140 ...new SafeArrayIterator ( dictAlgorithm ) ,
0 commit comments