File tree Expand file tree Collapse file tree 1 file changed +18
-10
lines changed Expand file tree Collapse file tree 1 file changed +18
-10
lines changed Original file line number Diff line number Diff line change 33 * @param data - The data to encode.
44 * @returns The base64url encoded string.
55 */
6- export function toBase64Url < T extends ArrayBufferLike | string > (
6+ export function toBase64Url < T extends string | ArrayBuffer | ArrayBufferView > (
77 data : T ,
88) : string {
9- const base64 = btoa (
9+ const bytes =
1010 typeof data === "string"
11- ? data
12- : String . fromCharCode ( ...new Uint8Array ( data ) ) ,
13- ) ;
14- const base64Url = base64
15- . replace ( / \+ / g, "-" )
16- . replace ( / \/ / g, "_" )
17- . replace ( / = / g, "" ) ;
11+ ? new TextEncoder ( ) . encode ( data ) // Convert string to Uint8Array
12+ : data instanceof ArrayBuffer
13+ ? new Uint8Array ( data )
14+ : new Uint8Array ( data . buffer , data . byteOffset , data . byteLength ) ;
1815
19- return base64Url ;
16+ // Convert bytes to string safely (avoiding call stack overflow)
17+ let binaryString = "" ;
18+ const chunkSize = 8192 ; // Process in chunks to avoid call stack limits
19+
20+ for ( let i = 0 ; i < bytes . length ; i += chunkSize ) {
21+ const chunk = bytes . subarray ( i , i + chunkSize ) ;
22+ binaryString += String . fromCharCode ( ...chunk ) ;
23+ }
24+
25+ const base64 = btoa ( binaryString ) ;
26+
27+ return base64 . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" ) . replace ( / = + $ / g, "" ) ;
2028}
2129
2230/**
You can’t perform that action at this time.
0 commit comments