Skip to content

Commit

Permalink
support caching streaming files that have known checksums
Browse files Browse the repository at this point in the history
  • Loading branch information
jjclark1982 committed May 6, 2015
1 parent da0fdff commit aca0f44
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
26 changes: 14 additions & 12 deletions index.js
Expand Up @@ -12,19 +12,21 @@ var plugin = function(name, opt){
}

var stream = through.obj(function(file, enc, callback){
var contents = null;
var contents = file.checksum;

if (file.isStream()) {
this.push(file);
return callback();
}
if (file.isBuffer()) {
contents = file.contents.toString('utf8');

// slower for each file
// but good if you need to save on memory
if (opts.optimizeMemory) {
contents = crypto.createHash('md5').update(contents).digest('hex');
if (!contents) {
if (file.isStream()) {
this.push(file);
return callback();
}
if (file.isBuffer()) {
contents = file.contents.toString('utf8');

// slower for each file
// but good if you need to save on memory
if (opts.optimizeMemory) {
contents = crypto.createHash('md5').update(contents).digest('hex');
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/main.js
Expand Up @@ -151,4 +151,28 @@ describe('gulp-cached', function() {
stream.write(file);
stream.end();
});

it('should create a cache that only allows a hashed streaming file through once', function(done) {
var file = new gutil.File({
path: "/home/file.js",
contents: new PassThrough()
});
file.checksum = 'deadbeef';
var stream = cache('testyeah');
var count = 0;
stream.on('data', function(nfile){
count++;
nfile.path.should.equal(file.path);
});
stream.on('end', function(){
count.should.equal(1);
done();
});
stream.write(file);
stream.write(file);
stream.write(file);
stream.write(file);
stream.write(file);
stream.end();
});
});

0 comments on commit aca0f44

Please sign in to comment.