Skip to content

Instantly share code, notes, and snippets.

var Pool = Base(EventEmitter, {
init: function(getConnection, size) {
this._super();
this._pool = [];
this._queue = [];
this.size = size;
var me = this;
getConnection(size, function(conn) {
me.emit('back', conn);
});
function() {
var me = this,
writeHead = me.flaker.writeHead,
write = me.flaker.write,
contentType = mime.lookup(extname(filePath));
fs.stat(filePath, function(err, stat) {
if (err || !stat.isFile()) {
errback ? errback(err) : me.error(404, 'File not found');
return;
}
@lsm
lsm / long-string-cipher-base64.js
Created October 29, 2010 15:46
long string cipher->base64 and decipher
var c = require('crypto');
// cipher to binary
var cipher = c.createCipher('aes192', 'hello');
var en = cipher.update('你好你好你好你好你好dfferfdgwefdv4t5g56euhergy56yhe54wtfgq34tg你好你好你好你好你好你好你好erwgw43etr', 'utf8', 'binary') + cipher.final('binary');
console.log(en);
// binary to base64
var buff = new Buffer(en, 'binary');
var base64 = buff.toString('base64');
@lsm
lsm / pure_javascript_hash-ring.js
Created December 14, 2010 14:03
copied from node-memcached
var CreateHash = require('crypto').createHash,
StringDecoder = require('string_decoder').StringDecoder,
Bisection = require('./utils').Bisection;
/*
example usage:
var sys = require( 'sys' ),
hashing = require( '../lib/hashring' ),
hashring = new hashing.hashRing(
[ '192.168.0.102:11212', '192.168.0.103:11212', '192.168.0.104:11212' ],
@lsm
lsm / TiOverLay.js
Created February 15, 2011 19:16
ti background gradient
// https://github.com/sspinc/OverlayTest
// Whip up a basic UI ...
Titanium.UI.setBackgroundColor('#fff');
var nav = Titanium.UI.createTabGroup();
var window = Ti.UI.createWindow({ title: 'Web View Test', tabBarHidden: true, navBarHidden: true, backgroundColor: '#c0c0c0' });
var mainTab = Titanium.UI.createTab({
icon: 'KS_nav_views.png',
@lsm
lsm / Validator.js
Created April 11, 2011 16:57
email,nickname,mobile,password
// RegExp copied from sina weibo;)
function makeReFn(regStr) {
var re = typeof regStr == 'string' ? new RegExp(regStr) : regStr;
return function (beRegStr) {
if (re.test(beRegStr)) {
return true;
} else {
return false;
}
@lsm
lsm / export_gridfs.sh
Created June 30, 2011 11:20
Export files from MongoDB GridFS with directory paths
#!/bin/bash
# http://www.vladimirm.com/blog/2011/06/export-files-from-mongodb-gridfs-with-directory-paths/
_host="${1:?Usage: gridfs host db}"
_db="${2:?Usage: gridfs host db}"
while read -r line; do
file=$(echo "$line" | awk -F'\t' '{ print $1 }')
[[ $file == 'connected to'* ]] && continue
directory=${file%/*}
mkdir -p $directory
@lsm
lsm / gsfixer.js
Created July 1, 2011 05:24
Exporter/importer to fix crapped gridfs files for node-mongodb-native < 0.9.4.4
var mongodbSrc = require('./mongodb-0.9.4-3');
var mongodbDst = require('./mongodb-0.9.6-1');
/**** Edit start ****/
var SRC_HOST = '10.0.0.31';
var SRC_PORT = 27017;
var SRC_DB_NAME = 'mydb';
var SRC_ROOT_COLLECTION = 'images';
var FILE_QUERY = {}; // default to all
@lsm
lsm / background_demo.js
Created August 5, 2011 11:07 — forked from kosso/background_demo.js
Background Service notification for Titanium
/* Kosso : March 12th 2011
This the only way I managed to do this without the app crashing on resume.
Done slightly differently to the KS example, since they unregister the service and
do not use a setInterval timer.
*/
//############ in app.js :
// test for iOS 4+