A Readable stream does exactly what it states, it reads from a streaming source. It outputs data based on some criteria. Our example of this is a take on the simple example that is shown in the Node.js documentation.
We are going to take our example of counting the number of lorem in the text file, but we are going to output the location in the file that we found lorem:
- Import the Readable class and the createReadStream method from their respective modules:
import { Readable } from 'stream'
import { createReadStream } from 'fs'
- Create a class that extends the Readable class and set up some private variables to track the internal state:
class LoremFinder extends Readable {
#lorem = Buffer.from('lorem');
#found = 0;
#totalCount = 0;
#startByteLoc = -1;
#file = null;
}
- Add a constructor that initializes...