In the last lesson, I mentioned transform streams. Transform streams are a special type of duplex stream. Instead of simply passing the data to the read in to the write in, transform streams change the data. Transform streams are the center pipe pieces that can be used to transform data from readable streams before they are sent to writable streams. So I’m in just a blank index.js file, this file is found inside of your start folder, under chapter two lesson eight, and I’m going to go ahead and just create a new transform stream by pulling out our transform type.
From our stream module, and what we’re going to go ahead and do, is create a new class called replace text. And this is going to be a transform stream, so we’re going to extend transform type. Transform streams have different methods, instead of write we are actually going to use the transform method. Now, it looks similar to the write method in that it gets a chunk in the encoding, of that chunk and a callback function.
And what we’re going to go ahead and do is also add a constructor. And in the constructor we’ll go ahead and get a character. Invoke the super, which is our actual transform stream type that we are invoking to set up the transform stream, and then we will set this dot replace Char to the character that’s coming in. So what we want to do is we’re actually going to transform the data chunk by chunk, so I’m going to create a new variable, called transform chunk, and we’re going to go ahead and take the chunk of data that’s sent to this stream, we’re going to turn it into a string, assuming that it already is a string, and then using the strings replace method, we’re going to go ahead and replace all of the text, with this dot replace Char.
And I’ll go ahead and I will put some of the stuff on separate lines so that we can see it, there we go. So we’re just chaining a two string, and then we are going to replace on that string. So I actually haven’t done the replace yet, notice these two slashes, they represent a regular expression, so basically I want to replace all the lower case alphabet, all the uppercase alphabet, and we’ll separate these with pipes, also any digits. And adding the G will make sure that we replace that globally.
So what we’re going to do is whatever replace character is sent to the constructor, when we create a new transform stream, will be used to replace any text found in these chunks. So then all I need to do is then push my transform chunk. And invoke the callback. So here, we have a chunk of data coming into the transform method and we’re changing it, before we push it out of this stream.
The last method that we can implement is the flush method. And the flush method will get a callback. So once our read stream has stopped, we can still push more stuff into a transform stream. So I’ll go ahead and say more stuff is being passed. And I’ll go ahead and invoke the callback. And then let me go ahead and place a semicolon at the end of this line. Great, so there’s our transform type for replace texts let’s go ahead and use it.
So I’m going to go ahead and create a variable called xStream. And we will set that to a new replace text. And we will go ahead and throw an X there, that’s why we call it the xStream. So any text should be replaced with X’s. And let’s just try this on process, dot standard input, and remember, we can add this transform stream to any pipeline. So we’ll just go ahead and pipe in the xStream, and we will pipe this to standard output.
A write stream, so here’s our little pipeline, so from standard input through the xStream transform stream, to standard output, so I’ll go ahead and save this, and come over to the terminal, and I will type node dot, and now when I type hello world, and send that through standard input, it gets piped through my transform stream, and all I can see is, are X’s. So you can see that only the characters or digits are being replaced, and again, this replace text stream can actually work for anything, I can go ahead and replace all of my text with two X’s.
We’ll actually do two capital X’s, and save that. Come out here to the terminal, stop the program. Node dot to run it again, now again, hello world. Now we replace each of those characters with two X’s. And we can see that happening here. Transform streams are the last stream type available on Node.js that we’ve covered. Now there are a lot of transform streams out there, Node.js comes with a zlib package, that’s a transform stream that can be used to zip incoming data from the read stream, and send compressed data to the write stream.
Crypto is an NPM package that has transform streams that can encrypt data chunk by chunk, and then pass encrypted data to the write stream, or decrypt data from a read stream, and pass the decrypted data to the write stream. So transform streams are an essential part of the stream family.