101arrowz / fflate

High performance (de)compression in an 8kB package
https://101arrowz.github.io/fflate
MIT License
2.27k stars 79 forks source link

Uncaught InternalError: too much recursion #96

Closed ninjadynamics closed 3 years ago

ninjadynamics commented 3 years ago

Hi!

I'm getting a recursion overflow error when trying to create a zip file using zipSync.

Browser console:

fflate.zipSync({'test.txt': strToU8('Hey there!')})

Uncaught InternalError: too much recursion
    Rt https://unpkg.com/fflate@0.7.1/umd/index.js:1

My HTML file (served via https): <script src="https://unpkg.com/fflate@0.7.1/umd/index.js"></script>

101arrowz commented 3 years ago

This is quite weird, I have never seen an error remotely similar. This HTML file does not produce the error:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    <script src="https://unpkg.com/fflate@0.7.1/umd/index.js"></script>
    <script>
        const res = fflate.zipSync({'test.txt': fflate.strToU8('Hey there!')});
        console.log(res)
    </script>
</body>
</html>

Could you provide extra info about your environment?

ninjadynamics commented 3 years ago

Thank you for the quick response!

I found the problem. It seems that adding a method to Object.prototype causes the recursion to happen. You can try it.

console.log(fflate.zipSync({'test.txt': fflate.strToU8('TEST A')}));
Object.prototype.bananas = function() { return "bananas" };
console.log(fflate.zipSync({'test.txt': fflate.strToU8('TEST B')}));

image

101arrowz commented 3 years ago

Modifying the object prototype is bound to cause issues, I'm surprised fflate is the first library causing problems. The main issue with this is that the property is enumerable. If you set the property as non-enumerable, the problem does not occur.

Object.defineProperty(Object.prototype, 'x', {
  enumerable: false,
  value: function() {  }
})

Let me know if this works for you.

ninjadynamics commented 3 years ago

This works! Thank you very much! 👍 I'm closing this issue now! :)