Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting array buffer to string - Maximum call stack size exceeded

Our app downloads a zip file, but the response is in binary.

So what I did is to convert it to base64. It works when the size is 87.7KB but an error occurs when the response size is 183KB.

The error is Uncaught RangeError: Maximum call stack size exceeded

The line in question is

btoa(String.fromCharCode.apply(null, new Uint8Array(blob)))

According to this answer, the String.fromCharCode.apply() must be replaced with TextEncoder.

So I changed it to

btoa(new TextDecoder('utf-8').decode(new Uint8Array(blob)))

but I get an error.

Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

I changed it again using the top most snippet of this answer

The new code is now

btoa(unescape(encodeURIComponent(new TextDecoder('utf-8').decode(new Uint8Array(blob)))))

The download now works but the download zip file is corrupted.

The whole code can be seen here

like image 280
Dev Avatar asked Mar 06 '18 04:03

Dev


People also ask

How do I fix error max call stack size exceeded?

The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is not being met and is, therefore, calling the function again and again until you hit the call stack limit.

What does it mean when maximum call stack size exceeded?

The JavaScript exception "too much recursion" or "Maximum call stack size exceeded" occurs when there are too many function calls, or a function is missing a base case.

What is maximum call size exceeded?

It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you hit the call stack limit. This is almost always because of a recursive function with a base case that isn't being met.

What is the maximum call stack size in JavaScript?

Without any local variables, each function call takes up 48 bytes during the execution, and you are limited to less than 1MB for all local function frames. Each boolean and number variable takes 8 bytes of memory.


1 Answers

I got my answer from another question

btoa(new Uint8Array(blob).reduce(function (data, byte) {
    return data + String.fromCharCode(byte);
}, ''));

Source

like image 164
Dev Avatar answered Sep 20 '22 08:09

Dev