The backend server responds with a gzip file but without the Content-Encoding: gzip header
. And I do not have control over the server so can't handle the issue server side.
What I need now is to decompress the gzipped file client side using javascript.
I have found this excellent library which can help me do this: http://nodeca.github.io/pako/
But I don't want to add additional library just to un-gzip a file. I feel that there should be a way to use the browser's native functionality to un-gzip. Am I correct? If I am wrong can someone explain why this browser functionality is not exposed as a javascript API? And is there a way to un-gzip a file in javascript without adding an additional library?
The Compression Streams API is a new web standard and is currently available in Chrome ( since v80 ). Other browsers will eventually add it, but in the mean time the best bet is a WASM implementation . Apparently WASM implementations can approach 90% of the performance of a native implementation (and ~20x the speed of a JS implementation).
Some example usage of the Compression Streams API:
async function decompressBlob(blob) {
let ds = new DecompressionStream("gzip");
let decompressedStream = blob.stream().pipeThrough(ds);
return await new Response(decompressedStream).blob();
}
const compressedReadableStream = inputReadableStream.pipeThrough(new CompressionStream('gzip'));
More info:
I feel that there should be a way to use the browser's native functionality to un-gzip. If I am wrong can someone explain why this browser functionality is not exposed as a javascript API?
No, there shouldn't be, unless it is in w3c standard and it is not. The only standard which says something about gzip compression is an HTTP standard .
I really believe it won't become standard because there are thousands of algorithms (for compression, encryption, etc.) which you may want to use and browsers cannot handle them all; it would also be unfair to create interfaces for one algorithm while not creating them for another.
HTTP protocol is a kind of exception. The compression here is done to make the life of millions of people easier. The HTTP is a bottle neck in web performance, so as long as the compression is available there I cannot imagine any case when you elsewhere need to use a compression in JavaScript. The only case I know is compressing items in a localStorage
/ indexedDB
, but even there gzip won't work because it is not producing UTF-16
output.
That is why this is not in the standard and this is why it most likely won't appear there.
Your particular case is a server side implementation error. Using compressed output without a proper header really smells. Either don't use the compression or do it right.
And is there a way to un-gzip a file in javascript without adding an additional library?
Actually other than that there is a possible solution: create a browser extension which injects a proper header in a server response and you won't need a library but will need to distribute the extension to the users. Could be even worse, but still may work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.