Skip to content

Instantly share code, notes, and snippets.

Gaurav C cggaurav

Block or report user

Report or block cggaurav

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:1de5c0d92e2b900bb0d5f7067cc22e5c
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 Jan 7, 2019
webhooks + id for sparkpost
View sparkpost.js
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 Dec 31, 2018 — forked from guilhermepontes/shuffle.js
Shuffle Array - JavaScript ES2015, ES6
View shuffle.js
// 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 Dec 28, 2018
Encrypt + Decrypt 101 with aes-256-cbc
View email.service.js
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')}`
}
View 0-rate-limiters.md

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.

View dropbox.js
// 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 Aug 25, 2018 — forked from grimen/generators-test.js
Experiments with Node.js Generator API - partially requires Node 10.
View generators-test.js
/* =========================================
IMPORTS
-------------------------------------- */
const fs = require('fs')
const { promisify } = require('util')
sleep = promisify(setTimeout)
@cggaurav
cggaurav / streams-test.js
Created Jun 18, 2018 — forked from grimen/streams-test.js
Experiments with Node.js Stream API.
View streams-test.js
/* =============================================
Dependencies
------------------------------------------ */
const debug = require('debug')
const { Readable, Writable, Transform } = require('stream')
const JSONStream = require('JSONStream')
@cggaurav
cggaurav / worker.js
Created Jan 15, 2018
A simple worker
View worker.js
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 Nov 4, 2017
Example from Stylinity
View s.json
{
"_id": {
"$oid": "59fcba895d47ce19e4e653e0"
},
"AdvertiserName": "Century21",
"Active": null,
"Availability": "in-stock",
"Category": "Men's Dress Shirt",
"Currency": null,
"EndDate": null,
You can’t perform that action at this time.