Skip to content

Commit abf1089

Browse files
committed
fix: TS definitions work with latest version
1 parent 94dd65f commit abf1089

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/utils/base64url.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@
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
/**

0 commit comments

Comments
 (0)