NodeJs writeStream storage in memory

603 Views Asked by At

I need to compress some files with archiver and then send them by email. In archiver examples shows how to storage the created file to the fs but i need to temporarily storage it in memory to could send it.

Is there any way to do that?

sendEmail() {

...
  if (filesSize > 5) {
            const archive = archiver.create('zip', { zlib: { level: 9 } });
            const output = fs.createWriteStream(dest) // Destination should be an in memory variable;
    
            archive.pipe(output)
            
            // Code to subscribe to output and archive events
            attachments.forEach(file=> {
                archive.file(file.path as string, { name: file.filename as string });
            })
            
            archive.finalize();
        }

...

}
1

There are 1 best solutions below

0
On

The way a found to solve this as a workaround was to save the file, send it by email and once the operation has finish delete the file.