0

What is the best way to create an "intermediate" stream in Node.js? That is, a stream which just gathers information from another stream and pipes it out.

For instance, I might want to return a stream immediately from a function with asynchronous flow:

var streamingQuery = function (query) {
    var intermediateStream = new IntermediateStream();
    database.connect(function (client) {
        var queryStream = client.query(query);
        queryStream.pipe(intermediateStream);
    });
    return intermediateStream;
};

I think stream.DuplexStream might be what I need, but I'm not sure the best way of making that type of stream just pass all its data along to the next stream. Or maybe there is a handy helper function for accomplishing this particular task, and if so I'd like to know about it.

1 Answer 1

1

I discovered that stream.PassThrough is built-in and seems to do what I want.

var stream = require('stream');
var streamingQuery = function (query) {
    var intermediateStream = new stream.PassThrough();
    database.connect(function (client) {
        var queryStream = client.query(query);
        queryStream.pipe(intermediateStream);
    });
    return intermediateStream;
};
Sign up to request clarification or add additional context in comments.

Comments

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.