So I have wrote a few posts on streams when it comes to the create read stream and create write stream file system module methods, as well as many other such methods in various native nodejs modules. However I have not wrote much on the node stream module by itself, and how to go about using that module to make my own custom streams. Also it is important to know a thing or two about this module and the nature of streams in general when it comes to working on nodejs projects. So I thought I would put together a piece of content in which I am focusing on the node stream module and custom made streams, rather than something else in nodejs that inherits from the base classes in this module.
So there are writable streams and duplex streams that can both read and write, but for starters readable streams might be a good place to begin with streams in nodejs. So I made a quick example that can be used to stream random letters to a writable stream that can console the data. This might not be the most piratical example of a readable stream, but the basic concepts can surly be applied to a stream that might have some kind of piratical application. So in this section I will be writing about using the node stream module to make a custom readable stream, and some basic examples that make use of it.
So I started out by making a readable node stream module that exports a method that can be used to create an instance of the Random Letters readable stream.
So by default I am using a hard coded stop if method that will push null ending the readable stream once the current byte index reaches a total bytes value. I can set what that total byes value is via an option when creating the readable node stream. So for the first example that makes use of this random letters readable stream example, I am just piping to the standard output.
Using a pipe is one way to go about setting the stream into flowing mode, so the stream will just keep flowing non stop into the standard output until it is stopped somehow. In this case the stream stops because there is a stop if method that is called in the body of the read method. By default the hard coded stop if method will push null if the byte index equals or surpasses the totalBytes value.
One of the options that is possible with this module I put together is the option to give a custom stop if method that can be used to define the condition required to push null ending the stream, or not. In this example I am not defining any condition to push null, I just keep pushing the current buffer value created in the read method.
So this example results in data being generated and spit out to the standard output non stop the only way is to break out some how in the console by pressing ctr + C for example.
So I have wrote a few examples that involve piping the readable node stream to another node stream that is writable such as the standard output. By using the readable.pipe method this places the stream in the flowing state, but what if I want to pause and resume that state from some reason so that I have fine grain control over the process. For this there is the resume, read, and puse methods.
In this section I will be going over a simple example of making a custom writable node stream. This example involves making a function that returns a writable stream that can encode text as base64, and another that can decode it back to plan text.