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
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 / worker.js
Created January 15, 2018 07:44
A simple worker
function worker (key, options = {}, work) {
global.queues = global.queues || {}
global.queues[key] = global.queues[key] || []
global.queues[`${key}:active`] = global.queues[`${key}:active`] || []
global.queues[`${key}:completed`] = global.queues[`${key}:completed`] || []
global.queues[`${key}:failed`] = global.queues[`${key}:failed`] || []
const queue = global.queues[key]
this.jobs = this.jobs || 0
@cggaurav
cggaurav / s.json
Created November 4, 2017 02:26
Example from Stylinity
{
"_id": {
"$oid": "59fcba895d47ce19e4e653e0"
},
"AdvertiserName": "Century21",
"Active": null,
"Availability": "in-stock",
"Category": "Men's Dress Shirt",
"Currency": null,
"EndDate": null,