@@ -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 ;
@@ -366,8 +383,7 @@ const dictRsaKeyGenParams = [
366383 ...new SafeArrayIterator ( dictAlgorithm ) ,
367384 {
368385 key : 'modulusLength' ,
369- converter : ( V , opts ) =>
370- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
386+ converter : enforceRangeUnsignedLong ,
371387 required : true ,
372388 } ,
373389 {
@@ -431,8 +447,7 @@ converters.AesKeyGenParams = createDictionaryConverter(
431447 ...new SafeArrayIterator ( dictAlgorithm ) ,
432448 {
433449 key : 'length' ,
434- converter : ( V , opts ) =>
435- converters [ 'unsigned short' ] ( V , { ...opts , enforceRange : true } ) ,
450+ converter : enforceRangeUnsignedShort ,
436451 validator : AESLengthValidator ,
437452 required : true ,
438453 } ,
@@ -452,8 +467,7 @@ converters.RsaPssParams = createDictionaryConverter(
452467 ...new SafeArrayIterator ( dictAlgorithm ) ,
453468 {
454469 key : 'saltLength' ,
455- converter : ( V , opts ) =>
456- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
470+ converter : enforceRangeUnsignedLong ,
457471 required : true ,
458472 } ,
459473 ] ) ;
@@ -490,8 +504,7 @@ for (const { 0: name, 1: zeroError } of [['HmacKeyGenParams', 'OperationError'],
490504 } ,
491505 {
492506 key : 'length' ,
493- converter : ( V , opts ) =>
494- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
507+ converter : enforceRangeUnsignedLong ,
495508 validator : validateMacKeyLength ( `${ name } .length` , zeroError ) ,
496509 } ,
497510 ] ) ;
@@ -567,8 +580,7 @@ converters.CShakeParams = createDictionaryConverter(
567580 ...new SafeArrayIterator ( dictAlgorithm ) ,
568581 {
569582 key : 'outputLength' ,
570- converter : ( V , opts ) =>
571- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
583+ converter : enforceRangeUnsignedLong ,
572584 validator : ( V , opts ) => {
573585 // The Web Crypto spec allows for SHAKE output length that are not multiples of
574586 // 8. We don't.
@@ -600,8 +612,7 @@ converters.Pbkdf2Params = createDictionaryConverter(
600612 } ,
601613 {
602614 key : 'iterations' ,
603- converter : ( V , opts ) =>
604- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
615+ converter : enforceRangeUnsignedLong ,
605616 validator : ( V , dict ) => {
606617 if ( V === 0 )
607618 throw lazyDOMException ( 'iterations cannot be zero' , 'OperationError' ) ;
@@ -620,8 +631,7 @@ converters.AesDerivedKeyParams = createDictionaryConverter(
620631 ...new SafeArrayIterator ( dictAlgorithm ) ,
621632 {
622633 key : 'length' ,
623- converter : ( V , opts ) =>
624- converters [ 'unsigned short' ] ( V , { ...opts , enforceRange : true } ) ,
634+ converter : enforceRangeUnsignedShort ,
625635 validator : AESLengthValidator ,
626636 required : true ,
627637 } ,
@@ -665,8 +675,7 @@ converters.AeadParams = createDictionaryConverter(
665675 } ,
666676 {
667677 key : 'tagLength' ,
668- converter : ( V , opts ) =>
669- converters . octet ( V , { ...opts , enforceRange : true } ) ,
678+ converter : enforceRangeOctet ,
670679 validator : ( V , dict ) => {
671680 switch ( StringPrototypeToLowerCase ( dict . name ) ) {
672681 case 'chacha20-poly1305' :
@@ -711,8 +720,7 @@ converters.AesCtrParams = createDictionaryConverter(
711720 } ,
712721 {
713722 key : 'length' ,
714- converter : ( V , opts ) =>
715- converters . octet ( V , { ...opts , enforceRange : true } ) ,
723+ converter : enforceRangeOctet ,
716724 validator : ( V , dict ) => {
717725 if ( V === 0 || V > 128 )
718726 throw lazyDOMException (
@@ -776,8 +784,7 @@ converters.Argon2Params = createDictionaryConverter(
776784 } ,
777785 {
778786 key : 'parallelism' ,
779- converter : ( V , opts ) =>
780- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
787+ converter : enforceRangeUnsignedLong ,
781788 validator : ( V , dict ) => {
782789 if ( V === 0 || V > MathPow ( 2 , 24 ) - 1 ) {
783790 throw lazyDOMException (
@@ -789,8 +796,7 @@ converters.Argon2Params = createDictionaryConverter(
789796 } ,
790797 {
791798 key : 'memory' ,
792- converter : ( V , opts ) =>
793- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
799+ converter : enforceRangeUnsignedLong ,
794800 validator : ( V , dict ) => {
795801 if ( V < 8 * dict . parallelism ) {
796802 throw lazyDOMException (
@@ -802,14 +808,12 @@ converters.Argon2Params = createDictionaryConverter(
802808 } ,
803809 {
804810 key : 'passes' ,
805- converter : ( V , opts ) =>
806- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
811+ converter : enforceRangeUnsignedLong ,
807812 required : true ,
808813 } ,
809814 {
810815 key : 'version' ,
811- converter : ( V , opts ) =>
812- converters . octet ( V , { ...opts , enforceRange : true } ) ,
816+ converter : enforceRangeOctet ,
813817 validator : ( V , dict ) => {
814818 if ( V !== 0x13 ) {
815819 throw lazyDOMException (
@@ -845,8 +849,7 @@ for (const { 0: name, 1: zeroError } of [['KmacKeyGenParams', 'OperationError'],
845849 ...new SafeArrayIterator ( dictAlgorithm ) ,
846850 {
847851 key : 'length' ,
848- converter : ( V , opts ) =>
849- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
852+ converter : enforceRangeUnsignedLong ,
850853 validator : validateMacKeyLength ( `${ name } .length` , zeroError ) ,
851854 } ,
852855 ] ) ;
@@ -857,8 +860,7 @@ converters.KmacParams = createDictionaryConverter(
857860 ...new SafeArrayIterator ( dictAlgorithm ) ,
858861 {
859862 key : 'outputLength' ,
860- converter : ( V , opts ) =>
861- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
863+ converter : enforceRangeUnsignedLong ,
862864 validator : ( V , opts ) => {
863865 // The Web Crypto spec allows for KMAC output length that are not multiples of 8. We don't.
864866 if ( V % 8 )
@@ -877,8 +879,7 @@ converters.KangarooTwelveParams = createDictionaryConverter(
877879 ...new SafeArrayIterator ( dictAlgorithm ) ,
878880 {
879881 key : 'outputLength' ,
880- converter : ( V , opts ) =>
881- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
882+ converter : enforceRangeUnsignedLong ,
882883 validator : ( V , opts ) => {
883884 if ( V === 0 || V % 8 )
884885 throw lazyDOMException ( 'Invalid KangarooTwelveParams outputLength' , 'OperationError' ) ;
@@ -896,8 +897,7 @@ converters.TurboShakeParams = createDictionaryConverter(
896897 ...new SafeArrayIterator ( dictAlgorithm ) ,
897898 {
898899 key : 'outputLength' ,
899- converter : ( V , opts ) =>
900- converters [ 'unsigned long' ] ( V , { ...opts , enforceRange : true } ) ,
900+ converter : enforceRangeUnsignedLong ,
901901 validator : ( V , opts ) => {
902902 if ( V === 0 || V % 8 )
903903 throw lazyDOMException ( 'Invalid TurboShakeParams outputLength' , 'OperationError' ) ;
@@ -906,8 +906,7 @@ converters.TurboShakeParams = createDictionaryConverter(
906906 } ,
907907 {
908908 key : 'domainSeparation' ,
909- converter : ( V , opts ) =>
910- converters . octet ( V , { ...opts , enforceRange : true } ) ,
909+ converter : enforceRangeOctet ,
911910 validator : ( V ) => {
912911 if ( V < 0x01 || V > 0x7F ) {
913912 throw lazyDOMException (
0 commit comments