Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
Trying to return a stream-buffer from a function

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!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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();

    });
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda