On the readable object _node.js in the stream module of Node.js

Source: Internet
Author: User
Tags readable

I have always been reluctant to pull the nodejs, because from the first time I saw it, I think its design is really disgusting. But there is no way, the stream specification has not yet popularized, and indeed there are many things are dependent on the flow of Nodejs to achieve, so I can only pinch the nose to pull the bite of this smelly and hard Nodejs stream object.


Nodejs has a module called stream, which can be used to get a set of flow object constructors. Now all I'm saying is the simplest stream. Readable.


In fact, used Nodejs almost all of the readable of the case, but usually not too concerned about it. A very typical example is when we process each request in the HTTP module with req and res objects, req is actually a readable object. We can read the entity part of the HTTP request on this req as a stream.


So the question is, why is the HTTP module designed to flow in this way? Or ask this question from another dimension is "nodejs if you get the content of the POST request? 」。 Students who know how to use search engines can easily find such an answer: Listen to the Data event collection, the end of the event to merge the data collected. Yes, this is the way to solve the problem. But why is it so designed? How good is it to get POST content directly like PHP? In fact, this design is good, if we receive the data is illegal, I can immediately detect, and then respond and disconnect. This avoids some unnecessary transmission costs. For example, upload a picture, perhaps the user mistakenly chose a very large executable file, we do not need to wait until the file is completely uploaded, as long as a file head of a number of bytes can determine whether a file is a picture. You can use the design of the stream here to read out the first few bytes before using it.


The data and end events mentioned above are all readable events, which represent the receipt and the completion of data received by each of the two events. So in fact, we already know the use of readable, but many people do not know it is readable object.


But these two events are merely events for readable consumers. How does the interior push a data into a readable object so that readable triggers these events? So it's the push method. Here is an example that creates a readable object that flows out an incremented number (used Babel-node here)


Import stream from ' stream ';

var r = new stream. readable;

R.on (' data ', data => {
 console.log (data + ');
});

R.on (' End ', data => {
 console.log (' End ');
});

R._read = () => {
 //Console.log (' before read ');

void function callee (i) {
 if (I <) {
  R.push (i + ');//Only pass in string or Buffre object
 } else {
  r.push (null); When a null is entered, the stream transfer completes, triggering the End event 
 }
 settimeout (callee, i + 1);
} (0);

If you look closely at the code above, you will find a magical place, this code is written in the _read method, what is this ghost? In fact, I also think this is a pit, this private naming style does not spit, why do you have to overwrite this method to realize it? If this method is not overridden, an exception is thrown when the push is invoked:

 Error:not implemented at
  Readable._read (_stream_readable.js:464:22) at
  Readable.read (_stream_ READABLE.JS:341:10)

These are the basic usages of the readable object. But there are more pits to step on, this article is just a simple introduction, let everybody learn how to create a readable object that can output data. As for some basic methods such as read, these are unscientific designs anyway.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.