Skip to content

Instantly share code, notes, and snippets.

View Deepak13245's full-sized avatar
🎯
Focusing

Deepak Kumar Deepak13245

🎯
Focusing
View GitHub Profile
@Deepak13245
Deepak13245 / cache.js
Created January 6, 2024 10:53
cacheable
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 = [];
@Deepak13245
Deepak13245 / controller.js
Created January 6, 2024 10:36
Controller decorator
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;
}
@Deepak13245
Deepak13245 / spawnChild.js
Created June 7, 2020 22:34
Spawn a subprocess in nodejs
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) => {
@Deepak13245
Deepak13245 / workerManager.js
Created June 2, 2020 21:02
Manager implementation for async workers
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);
}
@Deepak13245
Deepak13245 / worker.js
Created June 2, 2020 20:43
Worker implementation for async functions
class Worker {
_stopped = false;
_started = false;
_onStop = noOp;
constructor(channel, fn) {
this._channel = channel;
this._fn = fn;
}
@Deepak13245
Deepak13245 / channel.js
Created June 2, 2020 20:38
Woker channel for parallel processing of async function
class Channel {
_results = [];
_index = 0;
constructor(list = []) {
this._list = list;
}
push(...items) {
this._list.push(...items);
}
@Deepak13245
Deepak13245 / inParallelWithLimit.js
Created June 2, 2020 20:17
Run async functions in parallel with limited amout of concurrency.
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);
@Deepak13245
Deepak13245 / optimizeImage.js
Last active June 3, 2020 21:16
Optimize jpeg and png images
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({
@Deepak13245
Deepak13245 / inParallel.js
Created June 2, 2020 19:17
Running async functions in parallel
function inParallel(list, fn) {
return Promise.all(
list.map(async (...args) => {
try{
return {
status: true,
result: await fn(...args),
};
}catch(error){
return {
@Deepak13245
Deepak13245 / utils.js
Last active July 7, 2021 19:22
Few js utilities
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`);