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?