Code coverage report for lib/tasks/compressor.js

Statements: 100% (22 / 22)      Branches: 100% (14 / 14)      Functions: 100% (3 / 3)      Lines: 100% (22 / 22)     

All files » lib/tasks/ » compressor.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51          1             14 14 14 11       1 25 3 2   3     25   25 11         11     14 14 14 14 14 14 1   13        
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
var compressor = require('yuicompressor'),
    compressing = false,
    queue = [],
    running = 0,
    maxCompressor = 8,
    log = require('../log'),
    runQueue = function () {
        compressing = false;
        var item = queue.pop();
        if (item) {
            exports.compressor(item.options, item.blob, item.done);
        }
    };
 
exports.compressor = function (options, blob, done) {
    if (process.env.SHIFTER_COMPRESSOR_TASKS) {
        if (parseInt(process.env.SHIFTER_COMPRESSOR_TASKS, 10) > 0) {
            maxCompressor = parseInt(process.env.SHIFTER_COMPRESSOR_TASKS, 10);
        }
        log.info('found env for compressor tasks, running ' + maxCompressor + ' concurrent compressor tasks');
    }
 
    options = options || {};
 
    if (compressing && running >= maxCompressor) {
        queue.push({
            options: options,
            blob: blob,
            done: done
        });
        return;
    }
 
    compressing = true;
    running = running + 1;
    compressor.compress(blob.result, options, function (err, data) {
        running = running - 1;
        runQueue();
        if (err) {
            done(err);
        } else {
            done(null, new blob.constructor(data, blob));
        }
    });
};