Evgeny Kobzev
Evgeny Kobzev

Reputation: 89

Cannot implement Transform stream

I need to create a program that reads doc, encrypts it and writes the result to another doc. All of that should be done using transform stream. Here´s my code:

"use strict";
const fs = require("fs");
const crypto = require("crypto");
const Transform = require("stream").Transform;

// Make read and write streams
const input = fs.createReadStream("./logs/data.txt");
let output = fs.createWriteStream("./logs/newdata.txt");

// 2 functions that encrypt data and transform it
class CT extends Transform {
  constructor() {
    super();
  }

  // Encryption function
  makeHash(err, data) {
    if (err) throw err;
    return crypto.createHash("md5").update(data).digest("hex")
  }

  // Encryption function will be passed as a callback
  _transform(data, encoding, callback) {
    callback(null, data);
  }
}

let data;

// Make transform stream
let tr = new CT;
input.on("readable", () => {
  while (data = input.read()) {
    tr._transform(data, "utf8", tr.makeHash);
    console.log(data.toString());
    console.log(tr.makeHash(null, data));
    output.write(data, "utf8", () => {
      output.end();
      console.log("Done writing the file");
    });
  }
});

input.on("error", (err) => {
  console.error(`Unexpected ${err}`);
});

input.on("end", () => {
  console.log("Done reading the file");
});

When I run this program, console shows me that:

TextInfo
a0b5dfe30f8028ab86b1bb48b8f42ebb
Done reading the file
Done writing the file

It means that it initially reads and writes docs, but the result is not encrypted ("TextInfo") - instead it should look like this "a0b5dfe30f8028ab86b1bb48b8f42ebb". I´m sure there´s a mistake in transform stream logic.

Any help will be appreciated!

Upvotes: 1

Views: 829

Answers (1)

Evgeny Kobzev
Evgeny Kobzev

Reputation: 89

My mistake #1 was that I created a read stream - instead I should just declare variable input that reads file and on callback excels a transform stream to write its contents.

Mistake #2 was that I incorrectly implemented function makeHash: now it perfectly encrypts and writes to the file. So this function is called as a callback of _transform function.

Here´s the working code:

"use strict";
const fs = require("fs");
const crypto = require("crypto");
const Transform = require("stream").Transform;

const input = fs.readFile("./logs/data.txt", "utf8", (err, content) => {
  if (err) throw err;
  tr.write(content);
});
let output = fs.createWriteStream("./logs/newdata.txt");

class CT extends Transform {
  constructor() {
    super();
  }
  // Function declares cData - encrypted input text and writes it to the file
  makeHash(err, data) {
    let cData = crypto.createHash("md5").update(data).digest("hex");
    output.write(cData, "utf8", () => {
      output.end();
      console.log("Done writing the file");
    });
  }

  _transform(chunk, encoding, callback) {
    callback(null, chunk);
  }
}

let tr = new CT;
tr.on("data", (chunk) => tr._transform(chunk, "utf8", tr.makeHash));

tr.on("error", (err) => {
  console.error(`Unexpected ${err}`);
});

Upvotes: 1

Related Questions