Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a Readable stream from a Writable stream? (Node.js)

Tags:

stream

node.js

I'm creating Excel data on the fly using Exceljs

I want to buffer this thing in memory, then send it off to the user using Koa.

The write method for Exceljs expects a writableStream:

workbook.xlsx.write(writableStream, options)

BUT Koa expects a readable stream:

response.body = readableStream

I know I can pipe a readable stream into a writable stream, but how do I do the opposite? I want Exceljs to write into the writable stream and have Koa read from the same stream. I'm so frustrated with the streams API!

Among 20 other things, I tried this:

const ReadableStream = require("memory-streams").ReadableStream

const reader = new ReadableStream()

const writer = new stream.Writable({
    write: function(chunk, encoding, next) {
        console.log(chunk.toString())
        // reader.push(chunk, encoding)
        next()
    }
})

const reader = new MemoryStream(null, {readable: true})
// reader.write = reader.unshift
const writer = reader

workbook.xlsx.write(writer, {})
return reader

But it doesn't work, I get some weird error about not being able to write to a stream that is closed. Even if I handle the error, however, my Excel file doesn't open.

So how can I make a readable stream out of a writable stream?

like image 292
Max Heiber Avatar asked Feb 22 '16 21:02

Max Heiber


People also ask

How do I create a readable stream in node?

To implement a readable stream, we require the Readable interface, and construct an object from it, and implement a read() method in the stream's configuration parameter: const { Readable } = require('stream'); const inStream = new Readable({ read() {} }); There is a simple way to implement readable streams.

Can a output of readable stream be an input to the writable stream?

B - Output of readable stream can be input to a writable stream.

Which stream can be used for both read and write operation in node js?

Duplex − Stream which can be used for both read and write operation.

How do you switch between modes in readable stream mode?

One of the ways of switching the mode of a stream to flowing is to attach a 'data' event listener. A way to switch the readable stream to a flowing mode manually is to call the stream. resume method.


2 Answers

You need a transform stream. It's both readable and writable.

stream = new require('stream').Transform()
stream._transform = function (chunk,encoding,done) 
{
    this.push(chunk)
    done()
}
like image 111
user619271 Avatar answered Oct 02 '22 20:10

user619271


Have you considered Passthrough stream? That's a simple transform stream that passes all data passed to it, so you can read from a writable stream.

like image 21
Javad M. Amiri Avatar answered Oct 02 '22 22:10

Javad M. Amiri