2

Is it possible to extend / initialize the require('stream').Stream; object ? I would like to be able to create a stream object that i can push data to and have all the listeners notified when new data arrive.

I tried the following:

var stream = require('stream');
var test = new stream.Stream();
test.write(new Buffer('mads'));

But i get the following error:

TypeError: Object [object Object] has no method 'write'
    at repl:1:6
    at Interface.<anonymous> (repl.js:168:22)
    at Interface.emit (events.js:67:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:88:20)
    at ReadStream._emitKey (tty_posix.js:306:10)
    at ReadStream.onData (tty_posix.js:69:12)
    at ReadStream.emit (events.js:67:17)

2 Answers 2

13

For anyone who stumbles on this question today (node v0.10.x), this can be easily achieved using the PassThrough stream introduced with streams2.

var stream = require('stream');
var test = stream.PassThrough();
test.write('mads');
test.end();
Sign up to request clarification or add additional context in comments.

Comments

3

You have to create those methods yourself and also set the writable property to true, and don't forget to emit the events in those methods you override as well so that you can use it as a regular event emitter. Check this question for more information.

3 Comments

I want to use such custom stream as stdout and stderr for a child_process. But its saying Incorrect value for stdio stream: [object Object]. What could be wrong here?
@Салман same problem here. Have you found solution?
I actually wanted to redirect output of a child_process to a file, but it should be decided by the child itself, (unlike the parent opens an fd and give pass it while spawning). I couldn't figure it out, so had to open the fd in parent only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.