Skip to content

Instantly share code, notes, and snippets.

@liyu1981
liyu1981 / mock.js
Last active December 30, 2015 21:08
Simple mock rest server of nodejs
var http = require('http');
var create = false;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
if (req.url === '/rpc/Cloud/Host/List') {
res.end(JSON.stringify({ Items: [ { StateType: 5 } ] }));
} else if (req.url === '/rpc/Cloud/Injections/CreateRequestWith') {
if (create) {
res.end(JSON.stringify({ Err: 'Request again!' }));
process.exit(1);
@liyu1981
liyu1981 / streamProgress.js
Created December 17, 2013 07:19
progress notification for nodejs streams
var util = require('util');
var Transform = require('stream').Transform;
util.inherits(StreamProgress, Transform);
function StreamProgress(options) {
if (!(this instanceof StreamProgress))
return new StreamProgress(options);
Transform.call(this, options);
}
@liyu1981
liyu1981 / cloudstack.js
Last active December 31, 2015 16:49
minimalistic wrapper for the CloudStack API in Node.js. original here -- https://github.com/Chatham/node-cloudstack
// taken from https://github.com/Chatham/node-cloudstack
// adapted by LI, Yu
var http = require('http');
var crypto = require('crypto');
var url = require('url');
module.exports = function cloudstack(options) {
if (!options) { options = {}; }
@liyu1981
liyu1981 / myopenvpn.sh
Last active January 3, 2016 02:28
my openvpn util script
THISDIR=`pwd`
CMD=$1
CONFNAME=$2
function usage {
echo "usage: sudo $0 start/status/stop <conf_name>"
}
function openvpn_start {
#echo "openvpn --daemon --writepid /var/run/openvpn/${CONFNAME}.pid --cd ${THISDIR}/${CONFNAME} --config ${CONFNAME}.ovpn"
@liyu1981
liyu1981 / bashrc
Created January 21, 2014 13:22
Setting for fcitx
#setup XIM environment
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS=@im=fcitx
if [ -z `pidof fcitx` ]; then
fcitx
fi
find dir -type f -print0 | xargs -0 md5sum | sort
@liyu1981
liyu1981 / ddprogress.sh
Created January 27, 2014 07:42
Show dd progress
# learned from http://bneijt.nl/blog/post/show-progress-of-dd-command/
# open another terminal tab and,
sudo watch -n 30 'pkill -USR1 -n -x dd'
@liyu1981
liyu1981 / hill.js
Last active August 29, 2015 13:55
Hill problem
/*
Challenge 2: Hill
Given an array of integer elements
Your task is to
* write a function that finds the minimum value X that makes possible the following: generate a new array that is sorted in strictly ascending order by increasing or decreasing each of the elements of the initial array with integer values in the [0, X] range.
Example: Having the initial array [5, 4, 3, 2, 8] the minimum value for X is 3. Decrease the first element (5) by 3, decrease the second one (4) by 1, increase the third one (3) by 1, increase the forth one (2) by 3 and do nothing to the last one (8). In the end we obtain the array [2, 3, 4, 5, 8] which is sorted in strictly ascending order.
;(function ($, window) {
var intervals = {};
var removeListener = function(selector) {
if (intervals[selector]) {
window.clearInterval(intervals[selector]);
intervals[selector] = null;
}
@liyu1981
liyu1981 / runCmd.js
Created March 26, 2014 01:53
simple wrapper of nodejs native spawn child process for a exec like func
function runCmd(cmd, args, callback) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var out = '';
var err = '';
child.stdout.on('data', function(buffer) { out += buffer.toString(); });
child.stderr.on('data', function(buffer) { err += buffer.toString(); });
child.on('close', function(code) { callback(code, out, err); });
}