Skip to content

Commit b1cda9b

Browse files
abdalla-rkoAbdallah Al-Soqatri
andauthored
Bug: Issue 'Maximum call stack size exceeded' with playground share. (#4370)
* Fix issue 'Maximum call stack size exceeded' with playground share with large content. * update changelog --------- Co-authored-by: Abdallah Al-Soqatri <[email protected]>
1 parent b7b63a5 commit b1cda9b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ should change the heading of the (upcoming) version to include a major version b
1616
1717
-->
1818

19+
# 5.22.4
20+
21+
## Dev / docs / playground
22+
23+
- Fix issue 'Maximum call stack size exceeded' with playground share with large content.
24+
1925
# 5.22.3
2026

2127
## @rjsf/utils

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)