A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

[Proposal] Simple blob decompression method

joe
2022-03-17

If I have a .json.gz file that I’d like to decompress, it currently requires code that looks something like this:

let stream = blob.stream().readable.pipeThrough(new DecompressionStream("gzip")).pipeThrough(new TextDecoderStream());
let reader = stream.getReader();
let jsonText = "";
while(1) {
  let {value, done} = await reader.read();
  if(value) jsonText += value;
  if(done) break;
}
let json = JSON.parse(jsonText);

I’m sure there are ways to simplify this a bit, but unless I’m mistaken (which I could be), it’s still going to be a bit of a mess.

It would be lovely if that code could be replaced by something like this:

let jsonText = await blob.decompress("gzip").then(b => b.text());
let json = JSON.parse(jsonText);

In the above example, blob.decompress("gzip") returns a Promise that resolves to a decompressed blob, but please ignore the specifics of the above syntax/API - the main point I’m intending to get across here is that decompression of blobs could/should be a simple one-liner.

Thoughts?

jimmywarting
2022-03-31

with some response tricks you can shorten the code to something like

// Just to simulate getting a .json.gz
const chunks = [ `{"a": "${'a'.repeat(1024)}" }` ]
const blob = new Blob(chunks)
const ts = new CompressionStream('gzip')
const blobGZ = await new Response(blob.stream().pipeThrough(ts)).blob()

// To uncompress and parse it to json in one go
const ts = blobGZ.stream().pipeThrough(new DecompressionStream("gzip"))
const json = await new Response(ts).json()

But i must admit, it feels abusive to use Response for this kind of conversion things…

easrng
2022-05-09

Note that DecompressionStream is not supported by non-Chromium browsers.