Skip to content

Commit

Permalink
Bug: Issue 'Maximum call stack size exceeded' with playground share. (#…
Browse files Browse the repository at this point in the history
…4370)

* Fix issue 'Maximum call stack size exceeded' with playground share with large content.

* update changelog

---------

Co-authored-by: Abdallah Al-Soqatri <abdallah.al-soqatri@aspentech.com>
  • Loading branch information
abdalla-rko and Abdallah Al-Soqatri authored Nov 7, 2024
1 parent b7b63a5 commit b1cda9b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ should change the heading of the (upcoming) version to include a major version b
-->

# 5.22.4

## Dev / docs / playground

- Fix issue 'Maximum call stack size exceeded' with playground share with large content.

# 5.22.3

## @rjsf/utils
Expand Down
19 changes: 18 additions & 1 deletion packages/playground/src/utils/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const base64 = (function () {
const { TextEncoder } = require('util');
encoder = new TextEncoder();
}
return btoa(String.fromCharCode(...encoder.encode(text)));
return btoa(safeFromCharCode(encoder, text));
},
decode(text: string): string {
let decoder: any;
Expand All @@ -31,4 +31,21 @@ const base64 = (function () {
};
})();

/**
* 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.
* 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.
*/
function safeFromCharCode(encoder: any, text: string): string {
const codes = encoder.encode(text);
const CHUNK_SIZE = 0x9000; // 36864
let result = '';

for (let i = 0; i < codes.length; i += CHUNK_SIZE) {
const chunk = codes.slice(i, i + CHUNK_SIZE);
result += String.fromCharCode(...chunk);
}

return result;
}

export default base64;

0 comments on commit b1cda9b

Please sign in to comment.