pipe to and from (in other words, to be a duplex stream). I couldn’t find sufficient examples of implementing streams (probably because the node API has changed so many times). The current node stream docs are pretty clear, but miss a couple points.
After solving the problem, I created this Node Stream Inheritance sample project with code to share. The project contains sample code showing how to subclass node’s Readable, Writeable, Transform, and Duplex streams as they exist in node v0.12.2.
The node streaming API has a long history. For the purposes of this project, I wanted to make my class support streaming in the simplest way possible without any external dependencies.
I implemented this project to demonstrate the following:
- What event should I wait for to know stream is complete? This depends on the stream you are piping to (the sink). In the case of a FileStream, handle the
- What events should I implement (emit) to indicate my writable stream has received everything it can handle? In this case I opted to implement my own
'full'event – indicating that my custom stream had no room for further input.
- How do I handle errors?
_transformprovide callbacks. If you pass your error out, the underlying implementation will automatically emit an ‘error’ event. In the case of
_read, you need to use
Here is some sample code from the project.
Related: Top 6 problems with node.js