@@ -147,6 +147,23 @@ converters.octet = createIntegerConversion(8);
147147converters [ 'unsigned short' ] = createIntegerConversion ( 16 ) ;
148148converters [ 'unsigned long' ] = createIntegerConversion ( 32 ) ;
149149
150+ // Shared helpers for `[EnforceRange] unsigned short/long/octet` dictionary
151+ // members. Dictionary converters always call members with a freshly allocated
152+ // `{ __proto__: null, prefix, context }` options object, so it is safe to
153+ // mutate it to set `enforceRange` instead of spreading it into a new object.
154+ const enforceRangeOctet = ( V , opts ) => {
155+ opts . enforceRange = true ;
156+ return converters . octet ( V , opts ) ;
157+ } ;
158+ const enforceRangeUnsignedShort = ( V , opts ) => {
159+ opts . enforceRange = true ;
160+ return converters [ 'unsigned short' ] ( V , opts ) ;
161+ } ;
162+ const enforceRangeUnsignedLong = ( V , opts ) => {
163+ opts . enforceRange = true ;
164+ return converters [ 'unsigned long' ] ( V , opts ) ;
165+ } ;
166+
150167converters . DOMString = function ( V , opts = kEmptyObject ) {
151168 if ( typeof V === 'string' ) {
152169 return V ;
@@ -346,8 +363,7 @@ const dictRsaKeyGenParams = [
346363 ...new SafeArrayIterator ( dictAlgorithm ) ,
347364 {
348365 key : 'modulusLength' ,
349- converter : ( V , opts ) =>
350- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
366+ converter : enforceRangeUnsignedLong ,
351367 required : true ,
352368 } ,
353369 {
@@ -411,8 +427,7 @@ converters.AesKeyGenParams = createDictionaryConverter(
411427 ...new SafeArrayIterator ( dictAlgorithm ) ,
412428 {
413429 key : 'length' ,
414- converter : ( V , opts ) =>
415- converters [ 'unsigned short' ] ( V , { ...opts , enforceRange : true } ) ,
430+ converter : enforceRangeUnsignedShort ,
416431 validator : AESLengthValidator ,
417432 required : true ,
418433 } ,
@@ -432,8 +447,7 @@ converters.RsaPssParams = createDictionaryConverter(
432447 ...new SafeArrayIterator ( dictAlgorithm ) ,
433448 {
434449 key : 'saltLength' ,
435- converter : ( V , opts ) =>
436- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
450+ converter : enforceRangeUnsignedLong ,
437451 required : true ,
438452 } ,
439453 ] ) ;
@@ -470,8 +484,7 @@ for (const { 0: name, 1: zeroError } of [['HmacKeyGenParams', 'OperationError'],
470484 } ,
471485 {
472486 key : 'length' ,
473- converter : ( V , opts ) =>
474- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
487+ converter : enforceRangeUnsignedLong ,
475488 validator : validateMacKeyLength ( `${ name } .length` , zeroError ) ,
476489 } ,
477490 ] ) ;
@@ -547,8 +560,7 @@ converters.CShakeParams = createDictionaryConverter(
547560 ...new SafeArrayIterator ( dictAlgorithm ) ,
548561 {
549562 key : 'outputLength' ,
550- converter : ( V , opts ) =>
551- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
563+ converter : enforceRangeUnsignedLong ,
552564 validator : ( V , opts ) => {
553565 // The Web Crypto spec allows for SHAKE output length that are not multiples of
554566 // 8. We don't.
@@ -580,8 +592,7 @@ converters.Pbkdf2Params = createDictionaryConverter(
580592 } ,
581593 {
582594 key : 'iterations' ,
583- converter : ( V , opts ) =>
584- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
595+ converter : enforceRangeUnsignedLong ,
585596 validator : ( V , dict ) => {
586597 if ( V === 0 )
587598 throw lazyDOMException ( 'iterations cannot be zero' , 'OperationError' ) ;
@@ -600,8 +611,7 @@ converters.AesDerivedKeyParams = createDictionaryConverter(
600611 ...new SafeArrayIterator ( dictAlgorithm ) ,
601612 {
602613 key : 'length' ,
603- converter : ( V , opts ) =>
604- converters [ 'unsigned short' ] ( V , { ...opts , enforceRange : true } ) ,
614+ converter : enforceRangeUnsignedShort ,
605615 validator : AESLengthValidator ,
606616 required : true ,
607617 } ,
@@ -645,8 +655,7 @@ converters.AeadParams = createDictionaryConverter(
645655 } ,
646656 {
647657 key : 'tagLength' ,
648- converter : ( V , opts ) =>
649- converters . octet ( V , { ...opts , enforceRange : true } ) ,
658+ converter : enforceRangeOctet ,
650659 validator : ( V , dict ) => {
651660 switch ( StringPrototypeToLowerCase ( dict . name ) ) {
652661 case 'chacha20-poly1305' :
@@ -691,8 +700,7 @@ converters.AesCtrParams = createDictionaryConverter(
691700 } ,
692701 {
693702 key : 'length' ,
694- converter : ( V , opts ) =>
695- converters . octet ( V , { ...opts , enforceRange : true } ) ,
703+ converter : enforceRangeOctet ,
696704 validator : ( V , dict ) => {
697705 if ( V === 0 || V > 128 )
698706 throw lazyDOMException (
@@ -756,8 +764,7 @@ converters.Argon2Params = createDictionaryConverter(
756764 } ,
757765 {
758766 key : 'parallelism' ,
759- converter : ( V , opts ) =>
760- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
767+ converter : enforceRangeUnsignedLong ,
761768 validator : ( V , dict ) => {
762769 if ( V === 0 || V > MathPow ( 2 , 24 ) - 1 ) {
763770 throw lazyDOMException (
@@ -769,8 +776,7 @@ converters.Argon2Params = createDictionaryConverter(
769776 } ,
770777 {
771778 key : 'memory' ,
772- converter : ( V , opts ) =>
773- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
779+ converter : enforceRangeUnsignedLong ,
774780 validator : ( V , dict ) => {
775781 if ( V < 8 * dict . parallelism ) {
776782 throw lazyDOMException (
@@ -782,14 +788,12 @@ converters.Argon2Params = createDictionaryConverter(
782788 } ,
783789 {
784790 key : 'passes' ,
785- converter : ( V , opts ) =>
786- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
791+ converter : enforceRangeUnsignedLong ,
787792 required : true ,
788793 } ,
789794 {
790795 key : 'version' ,
791- converter : ( V , opts ) =>
792- converters . octet ( V , { ...opts , enforceRange : true } ) ,
796+ converter : enforceRangeOctet ,
793797 validator : ( V , dict ) => {
794798 if ( V !== 0x13 ) {
795799 throw lazyDOMException (
@@ -825,8 +829,7 @@ for (const { 0: name, 1: zeroError } of [['KmacKeyGenParams', 'OperationError'],
825829 ...new SafeArrayIterator ( dictAlgorithm ) ,
826830 {
827831 key : 'length' ,
828- converter : ( V , opts ) =>
829- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
832+ converter : enforceRangeUnsignedLong ,
830833 validator : validateMacKeyLength ( `${ name } .length` , zeroError ) ,
831834 } ,
832835 ] ) ;
@@ -837,8 +840,7 @@ converters.KmacParams = createDictionaryConverter(
837840 ...new SafeArrayIterator ( dictAlgorithm ) ,
838841 {
839842 key : 'outputLength' ,
840- converter : ( V , opts ) =>
841- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
843+ converter : enforceRangeUnsignedLong ,
842844 validator : ( V , opts ) => {
843845 // The Web Crypto spec allows for KMAC output length that are not multiples of 8. We don't.
844846 if ( V % 8 )
@@ -857,8 +859,7 @@ converters.KangarooTwelveParams = createDictionaryConverter(
857859 ...new SafeArrayIterator ( dictAlgorithm ) ,
858860 {
859861 key : 'outputLength' ,
860- converter : ( V , opts ) =>
861- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
862+ converter : enforceRangeUnsignedLong ,
862863 validator : ( V , opts ) => {
863864 if ( V === 0 || V % 8 )
864865 throw lazyDOMException ( 'Invalid KangarooTwelveParams outputLength' , 'OperationError' ) ;
@@ -876,8 +877,7 @@ converters.TurboShakeParams = createDictionaryConverter(
876877 ...new SafeArrayIterator ( dictAlgorithm ) ,
877878 {
878879 key : 'outputLength' ,
879- converter : ( V , opts ) =>
880- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
880+ converter : enforceRangeUnsignedLong ,
881881 validator : ( V , opts ) => {
882882 if ( V === 0 || V % 8 )
883883 throw lazyDOMException ( 'Invalid TurboShakeParams outputLength' , 'OperationError' ) ;
@@ -886,8 +886,7 @@ converters.TurboShakeParams = createDictionaryConverter(
886886 } ,
887887 {
888888 key : 'domainSeparation' ,
889- converter : ( V , opts ) =>
890- converters . octet ( V , { ...opts , enforceRange : true } ) ,
889+ converter : enforceRangeOctet ,
891890 validator : ( V ) => {
892891 if ( V < 0x01 || V > 0x7F ) {
893892 throw lazyDOMException (
0 commit comments