hughsk / vinyl-transform

Use standard text transform streams to write fewer gulp plugins
MIT License
55 stars 7 forks source link

change README example map-stream to through2 #4

Open olsonpm opened 9 years ago

olsonpm commented 9 years ago

So I just spent all today learning what I could about streams as a result of my transform not working properly. To get straight to the code, the following prints an empty file:

// given a file foo.txt and destination directory tmp

var vinylFs = require('vinyl-fs')
    , fs = require('fs')
    , map = require('map-stream')
    , vinylTransform = require('vinyl-transform')
    , vinylSourceStream = require('vinyl-source-stream');

var uppercaser = vinylTransform(function(filename) {
    return map(function(chunk, cb) {
        cb(null, chunk.toString().toUpperCase());
    });
});

fs.createReadStream('foo.txt')
    .pipe(vinylSourceStream('foo.txt'))
    .pipe(uppercaser)
    .pipe(vinylFs.dest('tmp'));

Whereas if I use a through2 stream in lieu of map, it works fine. I'm giving up looking into the exact line of code causing the premature 'end' emit as I've already spent far too long looking into it, but my higher-level understanding is that map-stream must be based off an older version of node-streams and doesn't follow the same protocol of emitting data when properly asked. In the above code specifically, the data is sent and the stream ends sometime before vinyl-fs' mkdirp enters its callback (i.e. before async io finishes).