How do pipe (stream of Node.js) and bl (BufferList) work together? How do pipe (stream of Node.js) and bl (BufferList) work together? node.js node.js

How do pipe (stream of Node.js) and bl (BufferList) work together?


This portion of the bl github page more or less answers your question:

Give it a callback in the constructor and use it just like concat-stream:

const bl = require('bl'), fs = require('fs')fs.createReadStream('README.md')      .pipe(bl(function (err, data) { //  note 'new' isn't strictly required      // `data` is a complete Buffer object containing the full data      console.log(data.toString())   }))

Note that when you use the callback method like this, the resulting data parameter is a concatenation of all Buffer objects in the list. If you want to avoid the overhead of this concatenation (in cases of extreme performance consciousness), then avoid the callback method and just listen to 'end' instead, like a standard Stream.

You're passing a callback to bl, which is basically a function that it will call when it has a stream of data to do something with. Thus, data is undefined for now... it's just a parameter name that will later be used to pass the text from the GET call for printing.

I believe that bl.end() doesn't have be called because there's no real performance overhead to letting it run, but I could be wrong.


I have read the source code of bl library and node stream API.

BufferList is a custom duplex stream,that is both Readable and Writable.When you run readableStream.pipe(BufferList), by default end() is called on BufferList as the destination when the source stream emits end() which fires when there will be no more data to read.

See the implementation of BufferList.prorotype.end:

BufferList.prototype.end = function (chunk) {  DuplexStream.prototype.end.call(this, chunk)  if (this._callback) {    this._callback(null, this.slice())    this._callback = null  }}

So the callback passed to BufferList, will be called after BufferList received all data from the source stream, call this.slice() will return the result of concatenating all the Buffers in the BufferList where is the data parameter comes from.


var request=require('request')request(process.argv[2],function(err,response,body){console.log(body.length);console.log(body);})

you can have a look on this approach to solve the above exercise,p.s request is a third party module though