1

In node I can pipe to a file like this:

let outStream = fs.createWriteStream('./unzipped.html')
req.pipe(zlib.createGunzip()).pipe(outStream)

and read it back later with:

let data = fs.readFileSync('./unzipped.html')

But I don't want to create a file. What kind of stream am I looking for?

pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • Also, there are a lot of NPM packages providing utility pipes for this sort of thing. https://github.com/jasonpincin/stream-to-string – Brad Mar 27 '20 at 05:16
  • @Brad, Thanks but the other question / answers don't show how to create the stream (other than the fs stream which I'm trying to avoid.) I also want to avoid a new dependency. I'll stick with the temp file if there's no clean way to do this. – pguardiario Mar 27 '20 at 06:01
  • You *have* a stream, you're piping it to a file now. And, the accepted answer on that question shows how you can write this code manually. Just concatenate chunks to a buffer until the `end` event. – Brad Mar 27 '20 at 06:03
  • You're talking about stream.Readable ? My question is about how to implement that which the answer doesn't cover. – pguardiario Mar 27 '20 at 06:28
  • Am I not understanding what you're asking? Your question asks how to pipe data to a buffer/string. To do that, you simply consume the `data` and `end` events. On `data`, add it to your buffer/string. On `end`, you're done. If for some reason you want to add another pipe (which doesn't sound necessary), you could use the built-in Through pipe. – Brad Mar 27 '20 at 16:18
  • I'm asking what kind of stream to use. I ended up going with stream.PassThrough – pguardiario Mar 27 '20 at 23:20

0 Answers0