@@ -43,55 +43,55 @@ export function toBase64url(arr) {
4343// Unlike Uint8Array.fromBase64(), accepts both base64 and base64url
4444// NOTE: Always operates in strict mode for last chunk
4545
46- export function fromBase64 ( arg , format = 'uint8' ) {
47- if ( typeof arg !== 'string' ) throw new TypeError ( 'Input is not a string' )
46+ export function fromBase64 ( str , format = 'uint8' ) {
47+ if ( typeof str !== 'string' ) throw new TypeError ( 'Input is not a string' )
4848
4949 // These checks should be needed only for Buffer path, not Uint8Array.fromBase64 path, but JSC lacks proper checks
50- assert ( arg . length % 4 !== 1 , 'Invalid base64 length' ) // JSC misses this in fromBase64
51- if ( arg . endsWith ( '=' ) ) {
52- assert ( arg . length % 4 === 0 , 'Invalid padded length' ) // JSC misses this too
53- assert ( arg [ arg . length - 3 ] !== '=' , 'Excessive padding' ) // no more than two = at the end
50+ assert ( str . length % 4 !== 1 , 'Invalid base64 length' ) // JSC misses this in fromBase64
51+ if ( str . endsWith ( '=' ) ) {
52+ assert ( str . length % 4 === 0 , 'Invalid padded length' ) // JSC misses this too
53+ assert ( str [ str . length - 3 ] !== '=' , 'Excessive padding' ) // no more than two = at the end
5454 }
5555
56- assert ( ! / [ ^ 0 - 9 a - z = + / ] / iu. test ( arg ) , 'Invalid character in base64 input' )
57- return fromTypedArray ( fromBase64common ( arg , false ) , format )
56+ assert ( ! / [ ^ 0 - 9 a - z = + / ] / iu. test ( str ) , 'Invalid character in base64 input' )
57+ return fromTypedArray ( fromBase64common ( str , false ) , format )
5858}
5959
60- export function fromBase64url ( arg , format = 'uint8' ) {
61- if ( typeof arg !== 'string' ) throw new TypeError ( 'Input is not a string' )
60+ export function fromBase64url ( str , format = 'uint8' ) {
61+ if ( typeof str !== 'string' ) throw new TypeError ( 'Input is not a string' )
6262
6363 // These checks should be needed only for Buffer path, not Uint8Array.fromBase64 path, but JSC lacks proper checks
64- assert ( arg . length % 4 !== 1 , 'Invalid base64 length' ) // JSC misses this in fromBase64
65- assert ( ! arg . includes ( '=' ) , 'Did not expect padding in base64url input' )
64+ assert ( str . length % 4 !== 1 , 'Invalid base64 length' ) // JSC misses this in fromBase64
65+ assert ( ! str . includes ( '=' ) , 'Did not expect padding in base64url input' )
6666
67- assert ( ! / [ ^ 0 - 9 a - z _ - ] / iu. test ( arg ) , 'Invalid character in base64url input' )
68- return fromTypedArray ( fromBase64common ( arg , true ) , format )
67+ assert ( ! / [ ^ 0 - 9 a - z _ - ] / iu. test ( str ) , 'Invalid character in base64url input' )
68+ return fromTypedArray ( fromBase64common ( str , true ) , format )
6969}
7070
7171const { atob } = globalThis
7272
73- function fromBase64common ( arg , isBase64url ) {
73+ function fromBase64common ( str , isBase64url ) {
7474 if ( Uint8Array . fromBase64 ) {
7575 const options = { alphabet : isBase64url ? 'base64url' : 'base64' , lastChunkHandling : 'strict' }
76- const padded = arg . length % 4 > 0 ? `${ arg } ${ '=' . repeat ( 4 - ( arg . length % 4 ) ) } ` : arg
76+ const padded = str . length % 4 > 0 ? `${ str } ${ '=' . repeat ( 4 - ( str . length % 4 ) ) } ` : str
7777 return Uint8Array . fromBase64 ( padded , options )
7878 }
7979
8080 let arr
8181 if ( ! haveNativeBuffer && atob ) {
8282 // atob is faster than manual parsing on Hermes
83- const str = atob ( isBase64url ? arg . replaceAll ( '-' , '+' ) . replaceAll ( '_' , '/' ) : arg )
84- arr = new Uint8Array ( str . length )
85- for ( let i = 0 ; i < str . length ; i ++ ) arr [ i ] = str . charCodeAt ( i )
83+ const raw = atob ( isBase64url ? str . replaceAll ( '-' , '+' ) . replaceAll ( '_' , '/' ) : str )
84+ arr = new Uint8Array ( raw . length )
85+ for ( let i = 0 ; i < raw . length ; i ++ ) arr [ i ] = raw . charCodeAt ( i )
8686 } else {
87- assert ( ! arg . includes ( '=' ) || ! / = [ ^ = ] / iu. test ( arg ) , 'Invalid input after padding' )
88- arr = haveNativeBuffer ? Buffer . from ( arg , 'base64' ) : fromBase64js ( arg )
87+ assert ( ! str . includes ( '=' ) || ! / = [ ^ = ] / iu. test ( str ) , 'Invalid input after padding' )
88+ arr = haveNativeBuffer ? Buffer . from ( str , 'base64' ) : fromBase64js ( str )
8989 }
9090
9191 if ( arr . length % 3 !== 0 ) {
9292 // Check last chunk to be strict if it was incomplete
9393 const expected = toBase64 ( arr . subarray ( - ( arr . length % 3 ) ) )
94- const last = arg . length % 4 === 0 ? arg . slice ( - 4 ) : arg . slice ( - ( arg . length % 4 ) ) . padEnd ( 4 , '=' )
94+ const last = str . length % 4 === 0 ? str . slice ( - 4 ) : str . slice ( - ( str . length % 4 ) ) . padEnd ( 4 , '=' )
9595 const actual = isBase64url ? last . replaceAll ( '-' , '+' ) . replaceAll ( '_' , '/' ) : last
9696 if ( expected !== actual ) throw new Error ( 'Invalid last chunk' )
9797 }
0 commit comments