Skip to content

Instantly share code, notes, and snippets.

View codeofnode's full-sized avatar

Ramesh Kumar codeofnode

View GitHub Profile
@codeofnode
codeofnode / timeout.sh
Created November 27, 2019 08:09
timeout for child process with its exit code
#!/bin/sh
timeout() {
# Spawn a child process:
($1) & pid=$!
# in the background, sleep for x secs then kill that process
(sleep ${2:-'10'} && kill -9 $pid) & waiter=$!
# wait on our worker process and return the exitcode
wait $pid && exit_code=$?
# kill the waiter subshell, if it still runs
@codeofnode
codeofnode / gitconfig.ini
Last active August 1, 2019 04:15 — forked from tdd/gitconfig.ini
Nice, useful global Git configuration
# Put this in your ~/.gitconfig or ~/.config/git/config
# Windows users: "~" is your profile's home directory, e.g. C:\Users\<YourName>
[user]
name = Ramesh Kumar
email = codeofnode@gmail.com
[color]
# Enable colors in color-supporting terminals
ui = auto
[alias]
# List available aliases
@codeofnode
codeofnode / ddrequ.py
Last active February 26, 2019 09:06
Making http requests with data driven approach in python
import urllib2, json
reqopener = urllib2.build_opener(urllib2.HTTPHandler)
"""
To resolve template string from variable dictionary
Input:
@strvar: string on which replacement to be made
@varsdict: the dictionary that will be used to resolve the template object
Returns:
The resolved value, or the as it is string if not resolved by variable dictionary
@codeofnode
codeofnode / funnygit.sh
Created December 6, 2018 19:25
geeky bio for social profile
git fetch geeks
git checkout ramesh
git merge me
git add .coffee
git commit -am "some nerd made me nerd"
@codeofnode
codeofnode / server.js
Last active July 30, 2018 09:01
Pure node js server where routes represents your directories
const { createServer } = require('http')
const { createReadStream } = require('fs')
const { parse } = require('url')
const { extname, resolve } = require('path')
const PORT = 9000
const HOST = '0.0.0.0'
const DOC_HANDLER = process.env.J2S_DOC_HANDLER || '_doc'
const DOC_CHECK = process.env.J2S_DOC_CHECK
const BASE_PATH = process.env.J2S_BASE_PATH || '/v0'
@codeofnode
codeofnode / VERSIONING.md
Last active May 30, 2018 14:09
How to setup workspace tree in git repo
|-- master (latest version passed by QA)
|   |-- dev (latest version passed by developers, complete and working)
|
|-- v0 (version 0.x.x)
|   |-- i1 (addressing issue/task 1)
|   |-- i2 (addressing issue/task 2)
|   |-- i5 (addressing issue/task 5)
|   |-- new (fork from v0)
|
@codeofnode
codeofnode / extender.js
Last active July 7, 2017 13:28
JSON extender that works even with stringified son
/**
* clone a string or object
* @param {object|string} input - the input to the function.
* @return {object|string} output - the cloned data.
*/
const clone = function clone(input) {
if (typeof input === 'string') return input;
return Object.assign({}, input);
};
@codeofnode
codeofnode / stringify.js
Created July 7, 2017 13:25
stringify anything in javascript
/**
* stringify if not already string
* @param {*} input - the input to the function.
* @return {string} output - the stringified data.
*/
const stringify = function stringify(input) {
if (typeof input === 'string') return input;
if (typeof input === 'object') {
try {
return JSON.stringify(input);
@codeofnode
codeofnode / ipblock_awslamda.js
Last active June 1, 2017 14:58
A basic AWS lamda function in nodejs to block ip address is hitting more than a limit
'use strict';
console.log('--> Loading function');
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const waf = new aws.WAF({ apiVersion: '2015-08-24' });
class Police {
function isRightPattern (actual, expected){
if(expected instanceof RegExp){
return expected.test(actual);
} else {
return actual === expected;
}
}
function extractPatterns(obj,expected) {
if(!obj || typeof obj !== 'object') return {};