Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's my take on your issue.</p> <p>The basic idea is to create a <code>Transform</code> stream, which will allow us to execute your custom buffering logic before sending the data on the output of the stream:</p> <pre><code>var util = require('util') var stream = require('stream') var BufferStream = function (streamOptions) { stream.Transform.call(this, streamOptions) this.buffer = new Buffer('') } util.inherits(BufferStream, stream.Transform) BufferStream.prototype._transform = function (chunk, encoding, done) { // custom buffering logic // ie. add chunk to this.buffer, check buffer size, etc. this.buffer = new Buffer(chunk) this.push(chunk) done() } </code></pre> <p>Then, we need to override the <code>.pipe()</code> method so that we are are notified when the <code>BufferStream</code> is piped into a stream, which allows us to automatically write data to it:</p> <pre><code>BufferStream.prototype.pipe = function (destination, options) { var res = BufferStream.super_.prototype.pipe.call(this, destination, options) res.write(this.buffer) return res } </code></pre> <p>In this way, when we write <code>buffer.pipe(someStream)</code>, we perform the pipe as intended and write the internal buffer to the output stream. After that, the <code>Transform</code> class takes care of everything, while keeping track of the backpressure and whatnot.</p> <p>Here is a <a href="https://gist.github.com/PaulMougel/7961469" rel="noreferrer">working gist</a>. Please note that I didn't bother writing a correct buffering logic (ie. I don't care about the size of the internal buffer), but this should be easy to fix.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload