title: Proper way to consume NodeJS stream into buffer and write stream
body: <p>I have a need to pipe a readable stream into both a buffer (to be converted into a string) and a file. The stream is coming from <code>node-fetch</code>.</p>
  
  <p>NodeJS streams have two states: paused and flowing. From what I understand, as soon as a <code>'data'</code> listener is attached, the stream will change to flowing mode. I want to make sure the way I am reading a stream will not lose any bytes.</p>
  
  <p>Method 1: piping and reading from <code>'data'</code>:</p>
  
  <pre><code>fetch(url).then(
    response =&gt;
      new Promise(resolve =&gt; {
        const buffers = []
        const dest = fs.createWriteStream(filename)
        response.body.pipe(dest)
        response.body.on('data', chunk =&gt; buffers.push(chunk))
        dest.on('close', () =&gt; resolve(Buffer.concat(buffers).toString())
      })
  )
  </code></pre>
  
  <p>Method 2: using passthrough streams:</p>
  
  <pre><code>const { PassThrough } = require('stream')
  fetch(url).then(
    response =&gt;
      new Promise(resolve =&gt; {
        const buffers = []
        const dest = fs.createWriteStream(filename)
        const forFile = new PassThrough()
        const forBuffer = new PassThrough()
        response.body.pipe(forFile).pipe(dest)
        response.body.pipe(forBuffer)
        forBuffer.on('data', chunk =&gt; buffers.push(chunk))
        dest.on('close', () =&gt; resolve(Buffer.concat(buffers).toString())
      })
  )
  </code></pre>
  
  <p>Is the second method required so there is no lost data? Is the second method wasteful since two more streams could be buffered? Or, is there another way to fill a buffer and write stream simultaneously?</p>