Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node transform stream: append string to end

Tags:

stream

node.js

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.

like image 868
eye_mew Avatar asked Jan 11 '17 14:01

eye_mew


1 Answers

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();
    }
});
like image 57
Luka Avatar answered Oct 17 '22 22:10

Luka