#javascript #node.js #stream #streaming #buffer
Вопрос:
Я создаю Buffer
и Readable
поток, с целью заставить Readable
потреблять Buffer
.
Код выглядит так:
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) =gt; { 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) =gt; { console.log('chnk:', chunk.toString()) }) // the 'end' event, currently is not emitted, and the reading is not stopping. read.on('and', (chunk) =gt; { console.log('--end--') })