Skip to content

Instantly share code, notes, and snippets.

View jhurliman's full-sized avatar
🐨

John Hurliman jhurliman

🐨
View GitHub Profile
// Load the Facebook API
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Browser-specific hacks to make FB Connect more reliable
function clientIP(req)
{
if (config.using_proxy) {
var forwardedFor = req.headers['x-forwarded-for'];
return forwardedFor ? forwardedFor : req.connection.remoteAddress;
}
return req.connection.remoteAddress;
}
@jhurliman
jhurliman / gist:1107975
Created July 26, 2011 20:49
node.js request statusCode
var status = res ? res.statusCode : (err && err.length === 3) ? err : '-1';
@jhurliman
jhurliman / express-error-handling.js
Created September 28, 2011 07:47
Basic error handling in Express
app.configure(function() {
// Setup a socket error handler for all requests
app.use(socketErrorHandler);
// Catch any attempts to call res.send multiple times for the same client
app.use(onlyOneResponse);
});
function socketErrorHandler(req, res, next) {
// Check if this socket is being reused and already has an error handler
if (!req.socket.listeners('error').length) {
@jhurliman
jhurliman / base64.js
Created September 29, 2011 06:39 — forked from Marak/base64.js
An extremely simple implementation of base64 encoding / decoding using node.js Buffers (plus url-safe versions)
/*
* base64.js: An extremely simple implementation of base64 encoding / decoding using node.js Buffers
*
* (C) 2010, Nodejitsu Inc.
* (C) 2011, Cull TV, Inc.
*
*/
var base64 = exports;
@jhurliman
jhurliman / gist:1271679
Created October 8, 2011 00:33
lockedStore.js
LockedStore = function() { };
LockedStore.prototype = {
table: [],
/**
* index - Index of the value to check out.
* callback(value, fn) - Fired when a lock is acquired on the requested value. First
* parameter is the requested value, second parameter is a callback that must be
* fired to check the value back in and expects the new value as a single parameter.
@jhurliman
jhurliman / gist:1303191
Created October 21, 2011 05:53
Add a custom timeout to FB.getLoginStatus()
/* Add this somewhere after FB.init() and before any calls to FB.getLoginStatus() */
// Add a timeout to FB.getLoginStatus()
var FB_STATUS_TIMEOUT = 1000 * 30;
var origGetLoginStatus = FB.getLoginStatus;
FB.getLoginStatus = function(callback, force) {
// Start a timer
var timedOut = false;
var timeout = setTimeout(function() {
timedOut = true;
@jhurliman
jhurliman / cache.js
Created October 28, 2011 00:30
A generic expiring cache
Cache = function(cleanupIntervalMS) {
if (typeof cleanupIntervalMS === 'undefined')
cleanupIntervalMS = 1000 * 60 * 5; // 5 minute default interval
if (cleanupIntervalMS)
this.cleanupTimer = setInterval(this.cleanup, cleanupIntervalMS);
};
Cache.prototype = {
store: {},
@jhurliman
jhurliman / textstreaming.js
Created December 12, 2011 20:57
Stream from one file to another one line at a time
var fs = require('fs');
var input = fs.createReadStream('input.txt');
var output = fs.createWriteStream('output.txt');
var buffer = '';
input.on('data', function(data) {
buffer += data;
var index = buffer.indexOf('\n');
while (index > -1) {
@jhurliman
jhurliman / audioExtractor.js
Created March 1, 2012 23:09
Read PCM (WAV) data with node.js
var spawn = require('child_process').spawn;
exports.getPcmData = function(filename, sampleCallback, endCallback) {
var outputStr = '';
var oddByte = null;
var channel = 0;
var gotData = false;
// Extract signed 16-bit little endian PCM data with ffmpeg and pipe to STDOUT
var ffmpeg = spawn('ffmpeg', ['-i',filename,'-f','s16le','-ac','2',