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?
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.
B - Output of readable stream can be input to a writable stream.
Duplex − Stream which can be used for both read and write operation.
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.
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()
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With