Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* 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)
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;
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};
});
// 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') {
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': '*',
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();
}
}
}
}
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;
}
}
}
constructor() {
sharp.concurrency(1);
}