How to use the sharp.concurrency function in sharp

To help you get started, we’ve selected a few sharp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gymnastjs / gymnast / test / screenshot / commands / compareScreenshot.js View on Github external
/* eslint-disable no-console, prefer-arrow-callback, prefer-rest-params */
const { existsSync } = require('fs')
const { resolve } = require('path')
const { moveSync } = require('fs-extra')
const sharp = require('sharp')
const { noop } = require('lodash')
const { getBrowserData } = require('../shared')

sharp.concurrency(1)

function isValid(
  assert,
  screenshot,
  filename,
  baseline,
  sessionId,
  browserName,
  callback = noop
) {
  if (screenshot.status !== 0) {
    console.log('Error saving screenshot...', screenshot)
    callback(false)
  }
  assert.compareScreenshot(filename, baseline, browserName, result => {
    callback(!!result)
github MarcusCemes / responsive-image-builder / lib / worker.js View on Github external
const sharp   = require('sharp');
const path    = require('path');
const fs      = require('fs-extra');
const Export  = require(path.join(__dirname, 'export.js'));
const imagemin = require('imagemin');
const pngquant = require('imagemin-pngquant');
const mozjpeg = require('imagemin-mozjpeg');
const cluster = require('cluster');
let debug; try { debug = require('debug')('RIBworker:' + cluster.worker.id) } catch (err) { debug = () => {} };

let config;
let uncompressed_bytes = 0;
let compressed_bytes = 0;
let raw_bytes = 0;
sharp.concurrency(1);  // Only permit one thread

process.on('message', async function (packet) {
  if (packet.code === 'EXIT') {
    // Send worker stats and exit
    process.send({code: 'SUM', data: {uncompressed_bytes: uncompressed_bytes, compressed_bytes: compressed_bytes, raw_bytes: raw_bytes}});
    process.exit(0);

  } else if (packet.code === 'SETUP') {

    config = packet.data;

  } else if (packet.code === 'ACCEL') {
    sharp.concurrency(config.max_threads);  // Speed up the end, as there are fewer cores

  } else if (packet.code === 'TASK') {
    let result;
github MarcusCemes / responsive-image-builder / lib / worker.js View on Github external
process.on('message', async function (packet) {
  if (packet.code === 'EXIT') {
    // Send worker stats and exit
    process.send({code: 'SUM', data: {uncompressed_bytes: uncompressed_bytes, compressed_bytes: compressed_bytes, raw_bytes: raw_bytes}});
    process.exit(0);

  } else if (packet.code === 'SETUP') {

    config = packet.data;

  } else if (packet.code === 'ACCEL') {
    sharp.concurrency(config.max_threads);  // Speed up the end, as there are fewer cores

  } else if (packet.code === 'TASK') {
    let result;

    if (!config) {
      result = {code: 'NOPT', data: packet.data};
    } else {

      try {

        exp = await process_image(packet.data, config);

        result = { code: 'OK', data: exp };
      } catch (err) {
        if (err instanceof Error) err = new_error(err);
        result = {code: 'ERROR', data: packet.data, error: err};
github sozialhelden / accessibility-cloud / server / image-scaling.js View on Github external
});

  // setup pipes
  originalImageStream
    .pipe(resizeTask)
    .pipe(task.outputStream);

  return {
    cancel: () => {
      originalImageStream.destroy();
      resizeTask = null;
    },
  };
};

sharp.concurrency(3);
const imageProcessingQueue = new Queue(imageProcessingHandler, { concurrent: 3 });

function respondWithError(res, code, reason) {
  res.writeHead(code, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ error: { reason } }));
}

WebApp.connectHandlers.use('/images/scale/', (req: http.IncomingMessage, res: http.ServerResponse) => {
  setAccessControlHeaders(res, ['GET', 'HEAD', 'OPTIONS']);

  if (req.method === 'OPTIONS') {
    res.end();
    return;
  }

  if (req.method !== 'GET' && req.method !== 'HEAD') {
github IBM / taxinomitis / src / lib / utils / download.ts View on Github external
const log = loggerSetup();

// number of times an image download has been attempted
let numDownloads = 0;
// number of failures to download an image
let numErrors = 0;


type IErrCallback = (err?: Error) => void;


// disable aggressive use of memory for caching
sharp.cache(false);
// prevent sharp using multiple cores in parallel to reduce memory use
sharp.concurrency(1);

// standard options for downloading images
const REQUEST_OPTIONS = {
    timeout : 20000,
    rejectUnauthorized : false,
    strictSSL : false,
    gzip : true,
    headers : {
        // identify source of the request
        //  partly as it's polite and good practice,
        //  partly as some websites block requests that don't specify a user-agent
        'User-Agent': 'machinelearningforkids.co.uk',
        // prefer images if we have a choice
        'Accept': 'image/png,image/jpeg,image/*,*/*',
        // some servers block requests that don't include this
        'Accept-Language': '*',
github MarcusCemes / responsive-image-builder / src / ProcessThread.ts View on Github external
private handleCommand(message: any): void {
    if (typeof message === "object" && message.cmd) {
      switch (message.cmd) {
        case "INIT":
          this.config = message.config || {};
          break;
        case "FILE":
          if (message.accelerate === true) {
            sharp.concurrency(0);
          }
          this.convert(message.file);
          break;
        case "KILL":
          if (this.activeJobs > 0) {
            this.queueExit = true;
          } else {
            this.exit();
          }
      }
    }
  }
github asilvas / node-image-steam / lib / processor / processor.js View on Github external
function Processor(options) {
  if (!(this instanceof Processor)) {
    return new Processor(options);
  }

  EventEmitter.call(this);

  this.options = _.merge({}, defaults, options || {});

  if (this.options.sharp) {
    if ('cache' in this.options.sharp) {
      sharp.cache(this.options.sharp.cache);
    }
    if ('concurrency' in this.options.sharp) {
      sharp.concurrency(this.options.sharp.concurrency);
    }
    if ('simd' in this.options.sharp) {
      sharp.concurrency(this.options.sharp.simd);
    }
    if ('defaults' in this.options.sharp) {
      this.defaults = this.options.sharp.defaults;
    }
  }
}

sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, GIF, AVIF and TIFF images

Apache-2.0
Latest version published 1 month ago

Package Health Score

94 / 100
Full package analysis