
How it works...
Streams are bundled with Node core as a core module (the stream module). Other parts of core, such as fs, rely on the stream module for their higher level interfaces.
The two main stream abstractions are a readable stream and a writable stream.
In our case, we use a readable stream (as provided by the fs module) to read our source file (index.js) a chunk at a time. Since our file is smaller than the maximum size per chunk (16 KB), only one chunk is read.
The data event is therefore only emitted once, and then the end event is emitted.
While the content of self-read/index.js isn't considered Big Data , this data processing approach can scale potentially infinitely because the amount of memory used by the process stays constant.
Never use the core stream module directly.
As a rule, when we create streams, we should avoid using the internal stream module directly. This is because the behavior (more so than the API) of streams can (and has) changed between Node versions. While this is expected to occur less, or in a less impacting way in future, it's still safer to always use the readable-stream module instead. The readable-stream module (for historical reasons) is very poorly named; it's the latest core stream module exposed as an npm module and it's compatible with all Node versions. This still doesn't quite cater to core modules (such as fs) that rely on the core stream module; nevertheless, it's a best-effort practice to avoid maintenance pain in the future.