Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go from string to DecompressionStream?

Tags:

javascript

I have a string that is a base64 encoded, gzip compressed JSON string. I am able to turn the base64 encoded string to the bytes. There's the DecompressionStream API that I can use to decompress the bytes.

My problem is that I have not found a way to convert the bytes, which I put in a Buffer, to a n object that implements the stream() function, to piece this all together. This is what I have so far. Send help.

const bpString = "0eJztvVlvJEmS...SPUfzw=="
const buf = Buffer.from(bpString, "base64")
//HOW DO I GET FROM `buf` TO `data` ???
const ds = new DecompressionStream("gzip")
const decompressedStream = data.stream().pipeThrough(ds)
const decoded = await new Response(decompressedStream).blob()

Ps, I also tried so use fetch with a data url like so: const ret = await fetch("data:application/octet-stream;base64," + bpString)
But the await never returned and didn't throw an error. No idea, what goes on behind the scenes, but the string can be quite large, so maybe it just fails silently?! Anyway, I need a reliable method. And it must run in the browser.

like image 214
Daniel Avatar asked Aug 06 '26 15:08

Daniel


1 Answers

For this you can use the from factory of ReadableStream:

 const stream = ReadableStream.from(buf);

That way you get a stream directly, instead of having an intermediate object used only to get a stream out of.

If you absolutely need to, you can use a Blob

const data = new Blob([buf]);
like image 181
Vivick Avatar answered Aug 09 '26 05:08

Vivick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!