Skip to content

Commit

Permalink
Chunk fromUint8Array to avoid RangeError exception with large arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen committed Feb 9, 2024
1 parent 6fd611e commit cd1727a
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mesop/web/src/services/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,16 @@ function generatePayloadString(request: UiRequest): string {
}

function fromUint8Array(array: Uint8Array): string {
return String.fromCodePoint(...(array as unknown as number[]));
// Chunk this to avoid RangeError: Maximum call stack size exceeded
let result = '';
const chunkSize = 16384; // This size can be adjusted

for (let i = 0; i < array.length; i += chunkSize) {
const chunk = array.subarray(i, i + chunkSize);
result += String.fromCodePoint(...(chunk as unknown as number[]));
}

return result;
}

function toUint8Array(byteString: string): Uint8Array {
Expand Down

0 comments on commit cd1727a

Please sign in to comment.