const fs = require('fs') const {Transform} = require('stream') class DoublingStream extends Transform { constructor() { // Process chunks as strings (instead of byte arrays) super({decodeStrings: false}) } // Specifies how to transform each chunk. // `callback` must be called when the chunk has been processed. _transform(chunk, encoding, callback) { // For each character in the chunk, emit it twice to the output for (const character of chunk) { this.push(character, encoding) this.push(character, encoding) } callback() // indicate we're done processing chunk } } // Read file.txt into string chunks fs.createReadStream('file.txt', 'utf8') // Transform each chunk with a DoublingStream. // Note that pipe() returns the destination stream. .pipe(new DoublingStream()) // Pipe the output of the DoublingStream to doubled.txt .pipe(fs.createWriteStream('doubled.txt'))