Implementing a Transform stream

Our Transform stream is going to take in string data and keep running a hashing algorithm. Once it has finished, it will output the final hash that was calculated. A hashing function is one where we take some form of input and output a unique piece of data. This unique piece of data (in our case, a number) should not be vulnerable to collisions. Collisions is the concept that two values that differ from each other could come to the exact same hash value. In our case, we are converting the string to a 32-bit integer in JavaScript so we have a low chance of collision, but not an impossible chance of it.

The example is as follows:

// implemented in stream form from 
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
export default class StreamHashCreator extends Transform {
#currHash = 0;
constructor(options={}) {
if( options.objectMode ) {
throw new Error("This stream does not support object mode!");
}
options.decodeStrings = true;
super(options);
}
_transform(chunk, encoding, callback) {
if( Buffer.isBuffer(chunk) ) {
const str = chunk.toString('utf8');
for(let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
this.#currHash = ((this.#currHash << 5) - this.#currHash )
+ char;
this.#currHash |= 0;
}
}
callback();
}
_flush(callback) {
const buf = Buffer.alloc(4);
buf.writeInt32BE(this.#currHash);
this.push(buf);
callback(null);
}
}

Each function of the previous stream is explained below:

  1. The only thing that we need to persist until the stream is destroyed is the current hash code. This will allow the hash function to keep track of what we have already passed into it and work off of the data after each write.
  2. We do a check here to see whether the chunk we received is a Buffer. Since we made sure to turn the option of decodeStrings on, this means that we should always get buffers, but it still helps to check.
  3. While the contents of the hash function can be seen at the URL provided, the only major thing that we need to worry about is that we call our callback, just like we had to do when we were implementing our Writable stream.
  1. Once we are ready to produce data, we utilize the push method, just like we did with the Readable stream. Remember, Transform streams are just special Duplex streams that allow us to manipulate the data that is being input and change it into something for the output. We can also change the last two lines of code to just do callback(null, buf); this is just the shorthand of what we've seen previously.

Now, if we run some test cases on the previous code, we will see that we do get a unique hash code for each unique string that we enter, but we get the same hash code when we input the exact same thing. This means that our hashing function is good and we can hook it up to a streaming application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.37.154