Skip to content

Instantly share code, notes, and snippets.

View TooTallNate's full-sized avatar

Nathan Rajlich TooTallNate

View GitHub Profile
@TooTallNate
TooTallNate / gist:404593
Created May 18, 2010 04:08
A quick and dirty function that breaks apart a (long) String based on full words and a maximum line width (in characters).
// Adds a 'fitTextToWidth' function. Which essentially accepts a String and
// an Number (int) parameters. The String is a long string of text (i.e.
// dialoge in a RPG) and adds '\n' characters throughout the String.
//
// The placement of '\n' characters is determined by the second param, which
// should be the maximum width of a single line of text.
//
// Depends on Prototype's Language extensions ($w, Enumberable, etc.). Meant
// to be used with my Simple Game Framework.
@TooTallNate
TooTallNate / anotherScript.js
Created June 10, 2010 19:15
"getScriptName" is a cross-browser function to retrieve the absolute URL of the currently executing JavaScript file.
// If no exception is passed into 'getScriptName', it will only
// retrieve the URL of the script where 'getScriptName' is defined.
// In order to get the URL of a different script file, you must
// pass an exception generated in this script file to 'getScriptName'.
try {
(0)();
} catch(ex) {
getScriptName(ex, onScriptNameKnown);
}
@TooTallNate
TooTallNate / gist:443040
Created June 18, 2010 00:46
Scripts to be placed in /usr/bin/ on your jailbroken iPhone. They automatically run "ldid -S" on the output file.
We couldn’t find that file to show.
@TooTallNate
TooTallNate / webfont-manual.js
Created July 29, 2010 18:20
Adds a "manual" module to the WebFontLoader JS library, to dynamically create @font-face nodes.
webfont.DomHelper.prototype.setCssStyle = function(styleNode, body) {
if (styleNode.styleSheet) {
styleNode.styleSheet.cssText = body;
} else {
styleNode.appendChild(this.document_.createTextNode(body));
}
return styleNode;
}
/**
@TooTallNate
TooTallNate / redir.js
Created August 17, 2010 19:26
An attempt to use the iTunes radio playlist redirecting service with NodeJS.
var http = require('http');
var r = http.createClient(80, 'pri.kts-af.net');
var request = r.request('GET', '/redir/index.pls?esid=cb07887ce881c6164c3ae52a58c55361&url_no=1&client_id=7&uid=68efed4d03ec7e45fd3978262c107180&clicksrc=xml', {
'Host': 'pri.kts-af.net'
});
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
});
/* node UPNP port forwarding PoC
This is a simple way to forward ports on NAT routers with UPNP.
This is a not-for-production hack that I found useful when testing apps
on my home network behind ny NAT router.
-satori
usage:
@TooTallNate
TooTallNate / getip.js
Created October 17, 2010 20:39 — forked from isaacs/getip.js
var child_process = require("child_process");
function getIPs (cb) {
child_process.exec("ifconfig -a", function (er, o) {
if (er) return cb(er);
cb(null, o.match(/inet (?:addr:)?([0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3})/g).map(function (i) {
return i.replace(/^inet (?:addr:)?/, '');
}));
})
}
@TooTallNate
TooTallNate / netStreamDump.js
Created November 13, 2010 20:09
Dumps individual TCP 'requests' to files. I use this for creating test files when I'm implementing protocols in Node.
var port = 8080;
require('net').createServer(function(stream) {
var id = Date.now();
var filename = stream.remoteAddress + '.' + id + '.dump';
var dumpStream = require('fs').createWriteStream(filename);
dumpStream.on('close', function() {
console.error('File "' + filename + '" closed');
});
stream.on('timeout', function() {
console.error('Closing connection id ' + id + ', due to inactivity (timeout after 5 seconds)');
@TooTallNate
TooTallNate / spawn-response.js
Created November 30, 2010 02:31
Example of a Node HTTP server who's response body is 'stdout' of a `echo hello world` spawn.
var spawn = require('child_process').spawn;
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Connection':'close',
'Transfer-Encoding':'identity' // 'chunked' can't be used unfortunately
// since the child_process won't know how
@TooTallNate
TooTallNate / iphone4.2-node-configure
Created December 23, 2010 06:12
Runs "configure" for Node.js, with the intent of cross-compiling Node for ARM on an Apple iOS device.
#! /bin/bash
#
# Program : iphone4.2-node-configure
# Authors : Nathan Rajlich (nathan@tootallnate.net)
# Michael Aaron Safyan (michaelsafyan@gmail.com)
# Synopsis : This program runs the "configure" script for Node.js.
# An install prefix of "/opt/iphone-4.2/" is used.
unset CPATH