Streaming bz2 decompression of `window.fetch` response
Closed this issue · 0 comments
josephrocca commented
It took me an embarrassingly long time to do this (very little experience with streams), so I figured I'd share it in case anyone else finds themself in a similar position. It's not a complete example, but it's enough to get you started:
let bz2 = window.unbzip2Stream();
bz2.on('data', (d) => console.log(new TextDecoder('utf-8').decode(d)));
let reader = await fetch("https://files.pushshift.io/reddit/comments/RC_2017-11.bz2").then(r => r.body.getReader())
let r;
while(r = await reader.read(), !r.done) {
bz2.write( r.value );
}
I couldn't work out a way to use body.pipeThrough
because it expects a TransformStream
and there's no much info out there on how to create one. That's why I just ended up using getReader
and writing manually. If anyone comes up with a better way I'd love to see it.
Edit: Slightly more complete example here.