lostintranslation
lostintranslation

Reputation: 24583

Stream write stream to read stream

I am using a library in nodejs that can read from a read stream. I am currently opening a file read stream and passing that to the lib. However I also need to create the file on the filesystem that I am reading from. So step 1 is to create a write file stream and stream my data to the file.

  1. data write stream -----> file
  2. http response <------ read stream file

The thing I don't like about this process is that I have to clean up the file system when I am done. Is there a way the I can write directly to a read stream and do it all in one step?

EDIT: Concrete example (using https://github.com/ctalkington/node-archiver):

// some route
function(req, res, next) {
    var archive = archiver('zip');
    res.attachment("icons.zip");
    archive.pipe(res);

    // I have to create files on file system
    var stream1 = fs.createWriteStream("file1.txt");
    var stream2 = fs.createWriteStream("file2.txt");
    ...
    // all stuff is one streaming to file system

    // now stream stuff from file system to archive
    archive
      .append(fs.createReadStream("file1.txt"), { name: 'file1.txt' })
      .append(fs.createReadStream("file2.txt"), { name: 'file2.txt' })
      .finalize();

}

I have no idea how to avoid streaming to file system first.

EDIT:

Simple question is really this:

Can I write to a readable stream?

Upvotes: 3

Views: 11337

Answers (2)

lostintranslation
lostintranslation

Reputation: 24583

Seems like such an easy answer, can't believe I missed this.

Looks like I can just use PassThrough:

http://nodejs.org/api/stream.html#stream_class_stream_passthrough

Class: stream.PassThrough# This is a trivial implementation of a Transform stream that simply passes the input bytes across to the output. Its purpose is mainly for examples and testing, but there are occasionally use cases where it can come in handy as a building block for novel sorts of streams.

Upvotes: 1

jmingov
jmingov

Reputation: 14003

Pipe the readable stream to the response, or to a writable stream.

To pipe to the client you can do something like:

// the response object is a writable stream too.

// Send the headres first (Dont forget these, and change the content type.)

res.writeHead(200, {
  'Content-Type': 'application/pdf',
  'Access-Control-Allow-Origin': '*'
});
var resStream  = readableStm.pipe(res);  // readableStm should be a READABLE stream

// listen to the finish ev.
resStream.on('finish', function () {
  res.end();
});

Upvotes: 0

Related Questions