eye_mew
eye_mew

Reputation: 9133

Node transform stream: append string to end

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

Answers (1)

Luka
Luka

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

Related Questions