How to implement setEncoding in my own Node.JS readable Stream?

In my program, I have a readable Stream source which emits data events from a shared stream of byte data. I then interpret the data to extract Buffer objects, and then pass them to the appropriate function. The most elegant way of passing the Buffers to the rest of my program seems to be using a readable Stream object. That way my functions can read the stream the same way as if it were standalone stream.

Here is my current code:

var stream = new stream.Stream()
stream.readable = true
stream.pause = function () {
    source.pause()
}
stream.resume = function () {
    source.resume()
}
stream.readable = true
// in a loop, read the data from source stream, dissect the parts we want,
// and emit Buffer objects to the stream's data event.

How can I implement setEncoding?

Edit: I think the answer is related to this line in the stream.js source code

Stream.Readable = require('_stream_readable');

Furthermore, the correct answer may be to

  • replace new stream.Stream with new stream.Readable and to
  • replace stream.emit('data', buffer) with
    either stream.onread(buffer) or stream.push(buffer). The thing is, I don't know which one.

Edit: stream.Readable is not available in version 0.8. It is available in 0.9 beta, and will be avilable in v0.10 stable when that comes out. The ideal answer to this question will describe both the 0.8- solution and the 0.9+ solution.

use streams2. for 0.8, there's: https://github.com/isaacs/readable-stream