节点:使用流和管道读取数据、转换数据并写入文件

Node: Read data, transform it and write into file using streams and pipe

我尝试从文本文件中读取字符串,对其进行编码并保存到文件中。我想使用 pipehashReadStream 转移到 WriteStream。但是我不知道如何转换更改的数据。我的代码:

const crypto = require('crypto');
const fs = require('fs');
let hash = crypto.createHash('md5');
var rs = fs.createReadStream('./passwords.txt');
var ws = fs.createWriteStream('./new_passwords.txt');


rs.on('data', function(d) {
  hash.update(d);
});
rs.on('end', function(d) {
    console.log(hash.digest('hex'))
});

根据 the documentation 应该很简单:

const fs = require('fs')
const crypto = require('crypto')
const hash = crypto.createHash('md5')
const rs = fs.createReadStream('./plain.txt')
const ws = fs.createWriteStream('./hashed.txt')

rs.pipe(hash).pipe(ws)
var rs = fs.createReadStream('./passwords.txt');
var ws = fs.createWriteStream('./new_passwords.txt');
var Transform = require('stream').Transform;
var transformer = new Transform();

transformer._transform = function(data, encoding, cb) {
 // do transformation
 cb();
}

    rs
    .pipe(transformer)
    .pipe(ws);