Skip to content

Instantly share code, notes, and snippets.

View harish2704's full-sized avatar

Harish Karumuthil harish2704

View GitHub Profile
@harish2704
harish2704 / sendmail.js
Last active May 14, 2022 10:55
Psudo sendmail script for testing
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const mailDir = path.join( __dirname, '..', '..', 'test-mails' );
const {stdin} = process;
async function getStdin() {
let result = '';
@harish2704
harish2704 / DataBuffer.js
Created July 7, 2017 20:59
An in-memory data buffer class for Sequelize models. Can be used for buffering findOrCreate / create / upsert queries
/* ഓം ബ്രഹ്മാർപ്പണം. */
/*
* DataBuffer.js
* Created: Fri Jul 07 2017 23:14:58 GMT+0530 (IST)
* Copyright 2017 Harish.K<harish2704@gmail.com>
*/
/**
* DataBuffer
@harish2704
harish2704 / sequelize-forEach-batch.js
Created July 7, 2017 21:09
Do any task using all the items in a Sequlize model. Just write the task alone. ItemMaper will take care about batch execution and limit
class ItemMaper{
constructor( { model, batchSize, limit }, sequelizeOpts ){
this.model = model;
this.batchSize = batchSize || 10;
this.limit = limit || Infinity;
this.sequelizeOpts = sequelizeOpts || {};
}
async forEachBatch( fn ){
let offset = -this.batchSize,
@harish2704
harish2704 / repl-log-promise.js
Created July 19, 2017 15:19
Log promise in Node js repl. This will add a command ".log" which can be used like ".log Promise.resolve(123)" Raw
function logSuccess( a, b, c ){ console.log('Success'); global.uu = arguments; global.aa = a; global.bb = b; global.cc = c; }
function logError( e ){ console.log( 'Error'); global.ee = e; }
function log( promise ){ promise.then(logSuccess).catch(logError); }
function doLog( task ){ var cmd =`log(${task})`; this.outputStream.write(cmd); this.context.vm.runInThisContext(cmd); this.displayPrompt();}
repl.repl.defineCommand('log', doLog);
@harish2704
harish2704 / reflection-middleware.js
Created August 7, 2017 07:09
Simple Nodejs-Express reflection API
module.exports = function( app ){
app.post('/__reflection', function( req, res, next ){
const ip = req.headers[ 'x-forwarded-for' ] || req.connection.remoteAddress;
console.log( 'IP', ip );
if( ip !== '::ffff:127.0.0.1' ){
return next();
}
let fn;
try {
eval( 'fn = ' + req.body.code );
@harish2704
harish2704 / dummy-express-server.js
Created August 7, 2017 11:06
Dummy express server for logging in comming request
var express = require('express');
var app = express();
var port = process.env.PORT || 4005;
app.use( function( req, res ){
[
'method',
'url',
'headers',
'query',
'body'
@harish2704
harish2704 / css-utils.js
Created September 1, 2017 11:43
Convert / extract all applied styles in DOM element convert it into inline css
function getCss( elem ){
var rules = [].slice.call(window.getMatchedCSSRules( elem ));
return rules.map( rule => rule.style.cssText ).join('');
}
function addInlineCss( elem, isRecurssive ){
elem.setAttribute( 'style', getCss( elem ) );
if( isRecurssive && elem.children.length ){
[].slice.call( elem.children ).forEach( v => addInlineCss(v, true ) )
@harish2704
harish2704 / server.vim
Last active October 30, 2017 07:00
Simple vimrc for server environments without external plugins
" Plane vimrmc file without any extra plugins. Useful for use in server env
" This file is inspired by spf13's vimrc
" https://gist.github.com/harish2704/7cbc767a61110fd8bbbd05d50a71ebe1
" My custom commands {{{
" Grep for a word
command! -nargs=1 Gr :execute 'grep -nr <f-args> ./ <CR>'
" Add file header to current buffer
@harish2704
harish2704 / Readme.md
Created December 1, 2017 15:54
Routing network traffic of specific process through specific interface in linux

Consider we have two working ether net connections. One will be set as default by system.

What if we want to route all the network traffic for a specific process through a specific network interface ? In Linux, we can do this easily

My current configuration is this.

enp0s26u1u2 Link encap:Ethernet  HWaddr 02:45:4a:37:5a:28  
file-roller --extract-here *
for i in $(ls -d */); do
extId=$( cat $i/metadata.json | grep uuid | sed 's/.*"uuid".*"\(.*\)".*/\1/' );
mv $i $extId;
done