Skip to content

Instantly share code, notes, and snippets.

View Jimbly's full-sized avatar

Jimb Esser Jimbly

View GitHub Profile
@Jimbly
Jimbly / gist:3429037
Created August 22, 2012 20:23
fs.exists is important
var fs = require('fs');
var path = require('path');
var getSetting;
if ("we can use exists") {
getSetting = function(fn, cb) {
(fs.exists || path.exists)(fn, function(exists) {
if (!exists) {
return cb(undefined, 'default value');
}
fs.readFile(fn, 'utf8', cb);
@Jimbly
Jimbly / gist:2899468
Created June 9, 2012 04:26
comparing callback binding, calling, closures
function nop() { }
// static functions, .bind them
function doop1(a, cb) { cb(); }
function doop2(a, b, cb) { cb(); }
function backupFile1(filename, cb) {
doop1(filename + '.bak', step2.bind(undefined, filename, cb));
}
function step2(filename, cb, err) {
doop2(filename, filename + '.bak', cb);
@Jimbly
Jimbly / NetTestClient.js
Created June 4, 2012 21:42
node.js echo/flood test
var Net = require('net');
var Assert = require('assert');
var socket;
function randomInt(max) {
return Math.min(Math.floor(Math.random() * max), max - 1);
}
console.log('Initializing test data...');
@Jimbly
Jimbly / gist:2841357
Created May 31, 2012 05:46
CRC32 for node.js
/**
* Calculates the CRC32 checksum of a Buffer
*
* From libGlov under the MIT license,
* http://www.opensource.org/licenses/mit-license.php
**/
/* Table of CRCs of all 8-bit messages. */
var _crc_table = new Array(256);
@Jimbly
Jimbly / gist:2346805
Created April 9, 2012 21:54
Adding keep alive for https connections
function addKeepAliveTimeout(agent) {
function fail(msg) {
console.warn(msg + ', node version: ' + process.version);
return agent;
}
// Check it's what we expect
var old_listeners = agent.listeners('free');
if (!old_listeners || old_listeners.length !== 1) {
return fail('Unexpected lacking "free" listener');
}