regular / unbzip2-stream

streaming unbzip2 implementatio in pure javascript for node and browsers
Other
29 stars 23 forks source link

Streaming bz2 decompression of `window.fetch` response #18

Closed josephrocca closed 6 years ago

josephrocca commented 6 years ago

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.