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

363
Vistas
Nodejs, the Readable stream, does not emit 'end' event

I create a Buffer and a Readable stream, with the aim to make the Readable consume the Buffer.

The code is like so:

const { Readable } = require('stream');

// create a Buffer and allocate 200 positions to add the long text
const buf = Buffer.alloc(200, 'this is a loooong text..');


const readStream = (buf_data) => {
return new Readable({
  read(size){
    // I push the chunk data to the stream
    this.push(buf_data);

    // Here there MUST be a condition that will `push(null)` 
    // end emit the 'end' event
    // ????
  }
})
}

// Pass the Buffer to the readable stream
read = readStream(buf)


// the 'data' event is called to print the chunk data
read.on('data', (chunk) => {
  console.log('chnk:', chunk.toString())
})

// the 'end' event, currently is not emitted, and the reading is not stopping.
read.on('and', (chunk) => {
 console.log('--end--')
})

When i run the app (node <filename>), the application keeps reading and printing the text. There should be a condition that i can add into the read() method, to make end event trigger**

enter image description here

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

0

I ended up just manually emitting the end event after creating the stream

let buffer = Buffer.from(attach.base64Data, 'base64');
const bufferStream = Readable.from(buffer)
bufferStream.emit("end");

It's also worth noting that I was stumped for a while because I was listening for finished instead of end

.on('end',()=>{}) // this gets called
.on('finish',()=>{}) // this never gets called.
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