Skip to content

Instantly share code, notes, and snippets.

Avatar

Nathan Rajlich TooTallNate

View GitHub Profile
@TooTallNate
TooTallNate / gist:404593
Created May 18, 2010
A quick and dirty function that breaks apart a (long) String based on full words and a maximum line width (in characters).
View gist:404593
// 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 Jun 10, 2010
"getScriptName" is a cross-browser function to retrieve the absolute URL of the currently executing JavaScript file.
View anotherScript.js
// 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 Jun 18, 2010
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 Jul 29, 2010
Adds a "manual" module to the WebFontLoader JS library, to dynamically create @font-face nodes.
View webfont-manual.js
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 Aug 17, 2010
An attempt to use the iTunes radio playlist redirecting service with NodeJS.
View redir.js
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);
});
View upnp.js
/* 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:
View 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 Nov 13, 2010
Dumps individual TCP 'requests' to files. I use this for creating test files when I'm implementing protocols in Node.
View netStreamDump.js
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 Nov 30, 2010
Example of a Node HTTP server who's response body is 'stdout' of a `echo hello world` spawn.
View spawn-response.js
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 Dec 23, 2010
Runs "configure" for Node.js, with the intent of cross-compiling Node for ARM on an Apple iOS device.
View iphone4.2-node-configure
#! /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
You can’t perform that action at this time.