Is there a way to use the built-in gzip decompression in the browser using Javascript?

The backend server responds with a gzip file, but without Content-Encoding: gzip header . And I do not control the server, so I can not cope with the server problem.

Now I need to unzip the client side of the gzipped file using javascript.

I found this great library that can help me: http://nodeca.imtqy.com/pako/

But I do not want to add an additional library to just unzip the file. I believe that there should be a way to use the built-in browser functionality for un-gzip. I'm right? If I am mistaken, can someone explain why this browser function is not displayed as JavaScript API? And is there a way to un-gzip a file in javascript without adding an extra library?

+5
source share
1 answer

I feel that there should be a way to use the built-in browser functionality for un-gzip. If I am wrong, can someone explain why this browser function is not showing as javascript API?

No, it should not be if it is not a w3c standard, and it is not. The only standard that says something about gzip compression is the HTTP standard .

I really believe that it will not become standard, because there are thousands of algorithms (for compression, encryption, etc.) that you might want to use, and browsers cannot handle them; It would also be unfair to create interfaces for one algorithm without creating them for another.

The HTTP protocol is a kind of exception. Compression is done here to make life easier for millions of people. HTTP is the neck of the bottle in web performance, so while I have access to compression, I can’t imagine a single case where you need to use compression in JavaScript elsewhere. The only case I know is element compression in localStorage / indexedDB , but even there gzip will not work because it does not produce UTF-16 output.

This is why it is not in the standard, and therefore it is most likely not to appear there.

Your specific case is a server-side implementation error. Using compressed output without the proper header really smells. Either do not use compression, or do it correctly.

And is there a way to un-gzip a file in javascript without adding an extra library?

In fact, in addition to this, there is a possible solution: create a browser extension that enters the correct header in the server response, and you do not need a library, but you will need to distribute the extension to users. It could be worse, but it can still work.

0
source

Source: https://habr.com/ru/post/1245648/


All Articles