Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
How to convert Nodejs counter readable stream example to simplified construction version?

I refer to this doc

https://nodejs.org/api/stream.html#simplified-construction

The _construct() method is never called in my version. Why?

Note: In simplified construction you provide the implementation of _construct() method in the constructor options {construct() {...}, ...}

This is a working class version of a readable stream counter example:

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

class Counter extends Readable {
  constructor(options) {
    super(options);
    this._max = 10;
    this._index = 1;
  }

  _read() {
    const i = this._index++;
    if (i > this._max) {
      this.push(null);
    } else {
      const str = String(i);
      const buf = Buffer.from(str, 'utf-8');
      this.push(buf);
    }
  }
}

const readable = new Counter();

readable.on('readable', function() {
  console.log('readable');

  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

readable.on('close', function() {
  console.log('close');
});

This is my not working simplified construction version:

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

const counter = new Readable({

  construct() {
    this._max = 10;
    this._index = 1;
    console.log(this._max); // This is never executed
  },

  read() {
    console.log(this._max); // This is undefined
    this.push(null);
  }

  // read() {
  //   const i = this._index++;
  //   if (i > this._max) {
  //     this.push(null);
  //   } else {
  //     const str = String(i);
  //     const buf = Buffer.from(str, 'utf-8');
  //     this.push(buf);
  //   }
  // }

});

counter.on('readable', function() {
  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

counter.on('close', function() {
  console.log('close');
});

The docs say:

const { Writable } = require('node:stream');

const myWritable = new Writable({
  construct(callback) {
    // Initialize state and load resources...
  },
  write(chunk, encoding, callback) {
    // ...
  },
  destroy() {
    // Free resources...
  }
});

Now I'm confused. Thought initialization code belongs into the _construct() method?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As stated in my comment above, _construct()is added in Node Version 15.x. For further reference, follows the working example:

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

const counter = new Readable({

  construct(callback) {
    this._max = 10;
    this._index = 1;
    callback(); // Signal completion of initialization
  },

  read() {
    const i = this._index++;
    if (i > this._max) {
      this.push(null);
    } else {
      const str = String(i);
      const buf = Buffer.from(str, 'utf-8');
      this.push(buf);
    }
  }

});

counter.on('readable', function() {
  let data;

  while ((data = this.read()) !== null) {
    console.log(String(data));
  }
});

counter.on('close', function() {
  console.log('close');
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda