This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {HttpResponse} from "../utils"; | |
import {get} from "lodash"; | |
// compute cache key | |
function getCacheKey(req, options) { | |
if (typeof options.hashFn === 'function') { | |
return options.hashFn(req); | |
} | |
const {body, query} = req; | |
const keys = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function ControllerDecorator() { | |
// returns a function which takes constructor of class as argument | |
return constructor => { | |
// apply custom logic to each function of the controller class | |
constructor.elements.forEach(element => { | |
applyDecorator(element.descriptor); | |
}); | |
return constructor; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require('child_process'); | |
async function spawnChild(command) { | |
return new Promise((resolve, reject) => { | |
const proc = spawn(command, [], { | |
shell: true, | |
}); | |
proc.stdout.pipe(process.stdout); | |
proc.stderr.pipe(process.stderr); | |
proc.on('close', (code) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WorkerManager { | |
_started = false; | |
_workers = []; | |
constructor(concurrency, fn, data = []) { | |
this._channel = new Channel(data); | |
this._concurrency = concurrency; | |
this._fn = fn; | |
this._workers = this._createWorkers(concurrency); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Worker { | |
_stopped = false; | |
_started = false; | |
_onStop = noOp; | |
constructor(channel, fn) { | |
this._channel = channel; | |
this._fn = fn; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Channel { | |
_results = []; | |
_index = 0; | |
constructor(list = []) { | |
this._list = list; | |
} | |
push(...items) { | |
this._list.push(...items); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function inParallelWithLimit(list, concurrency, fn) { | |
// chunk is imported from here :- https://gist.github.com/Deepak13245/129ecdbb72c9f0f81827eb421260a8da | |
const batches = chunk(list, concurrency); | |
// Process batches in series | |
// inSeries :- https://gist.github.com/Deepak13245/c39c67c45abbf5d2a357fba5beeabbde | |
// inParallel :- https://gist.github.com/Deepak13245/e49c1853ebceacbd3ab6f725c42b4ba6 | |
const data = await inSeries(batches, async (items, batch) => { | |
// Process items in a batch in parallel | |
return inParallel(items, fn); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const imagemin = require('imagemin'); | |
const imageminJpegTran = require('imagemin-jpegtran'); | |
const imageminPngQuant = require('imagemin-pngquant'); | |
const imageminJpegRecompress = require('imagemin-jpeg-recompress'); | |
const fs = require('fs'); | |
async function optimizeImageBuffer(buffer) { | |
return imagemin.buffer(buffer, { | |
plugins: [ | |
imageminJpegRecompress({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inParallel(list, fn) { | |
return Promise.all( | |
list.map(async (...args) => { | |
try{ | |
return { | |
status: true, | |
result: await fn(...args), | |
}; | |
}catch(error){ | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function measure(promise) { | |
const start = Date.now(); | |
try { | |
const result = await promise; | |
return result; | |
} catch(e) { | |
throw e; | |
} finally { | |
const time = Date.now() - start; | |
console.log(`Time taken ${time} ms`); |
NewerOlder