Skip to content

Instantly share code, notes, and snippets.

View Jimbly's full-sized avatar

Jimb Esser Jimbly

View GitHub Profile
@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');
}
@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 / 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: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 / 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 / test-http-deadsocket-client.js
Created September 7, 2012 20:57
Test case for getting a dead socket because of node timing issues.
var assert = require('assert');
var http = require('http');
var agent = new http.Agent({maxSockets: 1});
var headers = {'connection': 'keep-alive'};
var PORT = 8080;
var responses = 0;
var errors = 0;
@Jimbly
Jimbly / gist:3935714
Created October 23, 2012 00:07
Failed attempt at IP filtering.
var dns = require('dns');
var http = require('http');
var https = require('https');
var blocked = ['207.171.163.193'];
var user_supplied_hostname = 'www.cloudpartytime.com';
dns.lookup(user_supplied_hostname, function(err, address) {
console.log('DNS lookup returned ' + address);
@Jimbly
Jimbly / gist:3997436
Created November 1, 2012 23:20
Infinite HTTP server
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var count=0;
function doit() {
while (res.writable && res.write('Data!\r\n\r\n')) {
++count;
if (count % 1000 === 0) {
console.log('wrote ' + count + ' chunks');
return process.nextTick(doit);
@Jimbly
Jimbly / gist:5112692
Last active December 14, 2015 16:08
Logging to zlib log stream with periodic flushing.
var assert = require('assert');
var zlib = require('zlib');
var gzip = zlib.createGzip();
var fs = require('fs');
var FILENAME = 'log.txt.gz';
var out = fs.createWriteStream(FILENAME);
gzip.pipe(out);
function debug(msg) {
@Jimbly
Jimbly / gist:5408241
Last active December 16, 2015 08:48
Cloud Party: floating beach ball script
function tick() {
//error('test');
var z = getParam('ground_height');
var pos = getPos();
if (pos[2] < z) {
setGravity({ gravity: [0, 0, min(5, z - pos[2])] });
} else {
setGravity({ gravity: [0, 0, -9.8] });
}
}