Skip to content

Commit 4afbe3d

Browse files
committed
Fix issue 'Maximum call stack size exceeded' with playground share with large content.
1 parent b7b63a5 commit 4afbe3d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/playground/src/utils/base64.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const base64 = (function () {
1616
const { TextEncoder } = require('util');
1717
encoder = new TextEncoder();
1818
}
19-
return btoa(String.fromCharCode(...encoder.encode(text)));
19+
return btoa(safeFromCharCode(encoder, text));
2020
},
2121
decode(text: string): string {
2222
let decoder: any;
@@ -31,4 +31,21 @@ const base64 = (function () {
3131
};
3232
})();
3333

34+
/**
35+
* This function is a workaround for the fact that the String.fromCharCode method can throw a "Maximum call stack size exceeded" error if you try to pass too many arguments to it at once.
36+
* This is because String.fromCharCode expects individual character codes as arguments and javascript has a limit on the number of arguments that can be passed to a function.
37+
*/
38+
function safeFromCharCode(encoder: any, text: string): string {
39+
const codes = encoder.encode(text);
40+
const CHUNK_SIZE = 0x9000; // 36864
41+
let result = '';
42+
43+
for (let i = 0; i < codes.length; i += CHUNK_SIZE) {
44+
const chunk = codes.slice(i, i + CHUNK_SIZE);
45+
result += String.fromCharCode(...chunk);
46+
}
47+
48+
return result;
49+
}
50+
3451
export default base64;

0 commit comments

Comments
 (0)