0

I am attempting to create a zip file containing a csv file and return it from a function as a buffer. The code I have is as follows:

const archiver = require('archiver');
const streamBuffers = require("stream-buffers");

createZipFromCsv(csv) {

  var myWritableStreamBuffer = new streamBuffers.WritableStreamBuffer({
    initialSize: (100 * 1024),   // start at 100 kilobytes.
    incrementAmount: (10 * 1024) // grow by 10 kilobytes each time buffer overflows.
  });

  myWritableStreamBuffer.on('finish', () => {
    console.log(myWritableStreamBuffer.getContents())
    return Promise.resolve(myWritableStreamBuffer.getContents());
  });

  const archive = archiver('zip');
  archive.pipe(myWritableStreamBuffer);
  archive.append(csv, { name: 'csvName.csv' });
  archive.finalize();

}

And the function call:

const buffer = await createZipFromCsv("some,csv,data");
console.log(buffer)

however, the buffer console log after the function call is undefined. The console log inside the on finish returns a good buffer, but only after the console log after the function call. I'm confused why the function is returning before the buffer has finished writing.

Any help appreciated!

1 Answers1

1

You need to wrap your code in a promise that you return and then resolve/reject appropriately. Here's one way to do that:

const archiver = require('archiver');
const streamBuffers = require("stream-buffers");

createZipFromCsv(csv) {

    return new Promise((resolve, reject) => {
        const myWritableStreamBuffer = new streamBuffers.WritableStreamBuffer({
            initialSize: (100 * 1024), // start at 100 kilobytes.
            incrementAmount: (10 * 1024) // grow by 10 kilobytes each time buffer overflows.
        });

        myWritableStreamBuffer.on('finish', () => {
            resolve(myWritableStreamBuffer.getContents());
        });

        myWritableStreamBuffer.on('error', reject);

        const archive = archiver('zip');
        archive.pipe(myWritableStreamBuffer);
        archive.append(csv, { name: 'csvName.csv' });
        archive.finalize();

    });
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you for your reply. After implementing this fix the console log after the function call is "false" – Edward Martin May 30 '22 at 15:49
  • Turns out you can't .getContents() more than once as it's a function with side effects! Removing the console log makes it work. Thank you so much again! – Edward Martin May 30 '22 at 15:52
  • @EdwardMartin - Note, I added a listener for the `error` event on myWritableStreamBuffer, It's possible, you should also have a listener for the `error` event on the archive` stream that also rejects for complete error handling. – jfriend00 May 30 '22 at 16:03