Skip to content

Instantly share code, notes, and snippets.

View cggaurav's full-sized avatar
🏠
Working from home

Gaurav cggaurav

🏠
Working from home
View GitHub Profile
@cggaurav
cggaurav / airports list.txt
Created November 30, 2016 22:39
Airports List Contact Form 7
"Aalborg,Denmark,AAL" "Aarhus,Denmark,AAR" "Aasiaat,Greenland,JEG" "Abadan,Iran,ABD" "Abaiang Atoll Airport,Kiribati,ABF" "Abakan,Russia,ABA" "Abbotsford,Canada,YXX" "Abdul Rachman Saleh,Indonesia,MLG" "Abeche,Chad,AEH" "Abel Santamaria,Cuba,SNU" "Abemama Atoll Airport,Kiribati,AEA" "Aberdeen Regional Airport,United States,ABR" "Abha,Saudi Arabia,AHB" "Abidjan Felix Houphouet Boigny Intl,Cote d'Ivoire,ABJ" "Abilene Rgnl,United States,ABI" "Abraham Gonzalez Intl,Mexico,CJS" "Abraham Lincoln Capital,United States,SPI" "Abu Dhabi Intl,United Arab Emirates,AUH" "Abu Simbel,Egypt,ABS" "Acadiana Rgnl,United States,ARA" "Achmad Yani,Indonesia,SRG" "Achutupo Airport,Panama,ACU" "Adak Airport,United States,ADK" "Adams Fld,United States,LIT" "Adana,Turkey,ADA" "Adana-Incirlik Airbase,Turkey,UAB" "Addison,United States,ADS" "Adelaide Intl,Australia,ADL" "Aden Adde International Airport,Somalia,MGQ" "Aden Intl,Yemen,ADE" "Adi Sumarmo Wiryokusumo,Indonesia,SOC" "Adi Sutjipto,Indonesia,JOG" "Adirondack Regional Airport,Uni
const encrypt = (text, email) => {
const iv = crypto.randomBytes(IV_LENGTH)
const cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv)
let encrypted = cipher.update(text)
encrypted = Buffer.concat([encrypted, cipher.final()])
return `${iv.toString('hex')}:${encrypted.toString('hex')}`
}
@cggaurav
cggaurav / sparkpost.js
Created January 7, 2019 09:27
webhooks + id for sparkpost
client.inboundDomains.list()
.then((data) => {
console.log('List of all inbound domains', data)
})
.catch((err) => {
console.log('Whoops! Something went wrong')
console.log(err)
})
client.inboundDomains.create({ domain: 'test.gratefuldiary.co'})
@cggaurav
cggaurav / shuffle.js
Created December 31, 2018 14:33 — forked from guilhermepontes/shuffle.js
Shuffle Array - JavaScript ES2015, ES6
// original gist
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
// fully random by @BetonMAN
const shuffleArray = arr => arr
.map(a => [Math.random(), a])
.sort((a, b) => a[0] - b[0])
.map(a => a[1]);
shuffleArray([1, 2, 3]) //[3, 1, 2]
@cggaurav
cggaurav / email.service.js
Created December 28, 2018 10:53
Encrypt + Decrypt 101 with aes-256-cbc
const encrypt = (text, email) => {
const iv = crypto.randomBytes(IV_LENGTH)
const cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv)
let encrypted = cipher.update(text)
encrypted = Buffer.concat([encrypted, cipher.final()])
return `${iv.toString('hex')}:${encrypted.toString('hex')}`
}

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@cggaurav
cggaurav / dropbox.js
Created December 7, 2018 03:01
Dropbox WebHook
// How Dropbox handles webhooks: https://www.dropbox.com/developers/reference/webhooks
router.get('/api/dropbox/webhook', function (ctx, next) {
return new HTTPRequest({ ctx }).handle((request) => {
ctx.set('Content-Type', 'text/plain')
ctx.set('X-Content-Type-Options', 'nosniff')
ctx.body = request.data.challenge
})
})
@cggaurav
cggaurav / generators-test.js
Created August 25, 2018 01:10 — forked from grimen/generators-test.js
Experiments with Node.js Generator API - partially requires Node 10.
/* =========================================
IMPORTS
-------------------------------------- */
const fs = require('fs')
const { promisify } = require('util')
sleep = promisify(setTimeout)
@cggaurav
cggaurav / streams-test.js
Created June 18, 2018 20:06 — forked from grimen/streams-test.js
Experiments with Node.js Stream API.
/* =============================================
Dependencies
------------------------------------------ */
const debug = require('debug')
const { Readable, Writable, Transform } = require('stream')
const JSONStream = require('JSONStream')
@cggaurav
cggaurav / server.js
Created April 7, 2017 16:25
Gisting!
// ---------------------------------
// Redis
// ------------------------------
import redis from 'redis'
import redisConfig from '../config/redis-config'
const redisArgs = [redisConfig.redis.port, redisConfig.redis.hostname, {auth_pass: redisConfig.redis.password}]
const r = redis.createClient(...redisArgs)