src/processor/image/index.js

/* disabled flow because of deprecated signature type mismatch */
/**
 * Image processor module
 * @module
 */
const forEach = require('lodash/forEach');
const logger = require('../../logger').build('processor/image');

const processors = {
  sharp: require('./sharp')
};

if (require('optional')('gm') !== null) {
  processors.gm = require('./gm');
} else {
  logger.info('`gm` processor disabled, because `gm` isn\'t installed.');
}

/**
 * Function that takes an array with processing operations and returns a function that can be called with an stream.
 * This stream is converted using the given transformations array.
 *
 * @param {Object} operation flamingo operation
 * @returns {Function} function to convert a stream
 * @example
 * image([{ processor: 'sharp', pipe: (sharp) => sharp.toFormat('jpeg') }])(fs.createReadStream('sample.png')
 * // converted image stream
 */
module.exports = function (operation/*: FlamingoOperation */)/*: function */ {
  const transformations = operation.process;

  return function (stream) {
    forEach(transformations, transformation => {
      if (processors.hasOwnProperty(transformation.processor)) {
        stream = processors[transformation.processor](operation, transformation.pipe, stream);
      } else {
        logger.info(`Skipping transformation, unknown processor: ${transformation.processor}`);
      }
    });

    return stream;
  };
};