1

I'm trying to compress js file on server, then decompress and run it on the client. Code on my server:

const deflateString = pako.deflate(codeText, { to: 'string' });
const indexTemplate = fs.readFileSync('./index.ejs', 'utf-8');
const deflateString = pako.deflate(codeText, { to: 'string' });
fs.writeFileSync(`${__dirname}/index.html`, ejs.render(indexTemplate, {bundle: deflateString}));

ejs template:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script src="./pako_inflate.js"></script>
    <script id="deflateString" type="text/plain"><%- bundle %></script>
    <script>
        var deflatedBundle = document.querySelector('script#deflateString').innerHTML;
        var inflatedBundle = pako.inflate(deflatedBundle, { to: 'string' });    
        var s = document.createElement('script');
        s.innerText = inflatedBundle;
        document.body.appendChild(s);
    </script>
</body>
</html>

Error on client appears: "Uncaught invalid distance code" if I deflate this code:

alert(1);

But if I trying to deflate this:

!(function() {

    alert(1);

})()

It works correctly. Moreover if I trying to deflate this:

!(function() {

    alert(1);

})()
alert(2);

Chrome throws me an error: "Uncaught SyntaxError: Unexpected identifier", because pako gives invalid code. So, perhaps, problem with charset, any ideas? Pako version: 1.0.10

Max
  • 11
  • 3

0 Answers0