Reputation: 9133
How do I create a transform stream, where the only change it will effect, is appending a string to the end of a the incoming readable stream.
For example, let's say input.txt contains abcdef
.
fs.createReadStream('input.txt', {encoding: 'utf8'})
.pipe(appendTransform)
.pipe(fs.createWriteStream('output.txt', {encoding: 'utf8'}));
What can I use for appendTransform
, such that output.txt contains abcdefghi
.
Upvotes: 7
Views: 2601
Reputation: 3089
Create a transform stream:
var Transform = require('stream').Transform;
var appendTransform = new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk);
},
flush(callback) {
this.push('ghi');
callback();
}
});
Upvotes: 12