Skip to content

Instantly share code, notes, and snippets.

View fed135's full-sized avatar
🗜️
Optimizing...

Frederic Charette fed135

🗜️
Optimizing...
View GitHub Profile
// This middleware example takes in the entire swagger spec and tries to find the route in it.
const validate = require('swagger-route-validator');
const spec = require('./my-spec');
function validateRequest(req, res, next) {
const layer = app._router.stack.find(bloc => bloc.route && bloc.regexp.exec(req.originalUrl) !== null);
// Check if route exists
if (!layer) return next();
@fed135
fed135 / to.ts
Last active September 15, 2021 19:54
Functional await spread
export function to<T = any>(promise: Promise<T>): Promise<[null, T] | [any, null?]> {
return promise
.then((data) => [null, data] as [null, T])
.catch((err) => [err] as [any]);
}
@fed135
fed135 / deferred.ts
Created June 22, 2021 13:11
Deferred Promise
export function deferred<T>() {
let resolve: (value?: T | PromiseLike<T>) => void;
let reject: (reason?: any) => void;
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}
@fed135
fed135 / poolSize-calculator.md
Created October 8, 2020 14:49
Ideal database client pool size calculator
  1. Identify total average [R]equests per second
  2. Identify average response [T]ime per request
  3. Calculate:

((R * T) / 1000) * 1.2

Ex:

((200 RPM * 200ms) / 1000) * 1.2 = 48 connections required

@fed135
fed135 / install-gvm-go
Created July 28, 2020 21:31
Installing GVM and Go on Debian-based linux
#!/bin/bash
# Go setup:
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
source ~/.gvm/scripts/gvm
sudo apt-get install bison -y
gvm install go1.14 -B
gvm use go1.14
@fed135
fed135 / rom2int.js
Created March 24, 2020 19:48
Roman numeral to integer parser
function romanToNum(str) {
const symbols = 'IVXLCDM';
let acc = '';
// Reverse iterate through the provided characters
for (let i = str.length - 1; i >= 0; i--) {
// Get the associated roman character symbol's order in the list
let index = symbols.indexOf(str[i]);
// Calculate the value for the symbol 1 || 5 ^ 10 based on the previous index value
let value = (index % 2 === 0 ? 1 : 5) * (10**(index>>1));
@fed135
fed135 / helper.js
Last active February 19, 2020 16:36
Mongo stream helper
const { promisify } = require('util');
const stream = collection.find();
const next = promisify(stream.nextObject);
let row;
while(null !== (row = await next(stream))) {
// do stuff with your row
}
@fed135
fed135 / Dockerfile
Last active June 9, 2021 19:26
Docker commands to shed some extra weight from your node modules
# Final production image
# ===============================
FROM node:14 as final
RUN mkdir /app && chown node:node /app
RUN wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.2/dumb-init_1.2.2_amd64
RUN chmod +x /usr/local/bin/dumb-init
USER node
@fed135
fed135 / latencies.js
Last active January 22, 2024 20:28
Calculate best case scenario unidirectional latency and ping between AWS DCs
const toRadians = num => num * Math.PI / 180;
// Get the long, lat values for the desired dc
module.exports.datacenters = {
'ap-east-1': [22.29, 114.16 ],
'ap-northeast-1': [35.41, 139.42 ],
'ap-northeast-2': [37.56, 126.98 ],
'ap-northeast-3': [34.65, 136 ],
'ap-south-1': [19.08, 72.88 ],
'ap-southeast-1': [1.37, 103.8 ],
@fed135
fed135 / lib.js
Created July 18, 2019 19:13
Forked HTTP
/**
* Multi-process request
*/
/* Requires ------------------------------------------------------------------*/
const crypto = require('crypto');
const path = require('path');
const fork = require('child_process').fork;