Skip to content

Instantly share code, notes, and snippets.

View CrabDude's full-sized avatar

Adam Crabtree CrabDude

View GitHub Profile
@CrabDude
CrabDude / gist:1597914
Created January 12, 2012 01:33
JS Sandboxing via Harmony Proxies and with()
// in new iframe
var whitelist = {
// add whitelisted globals
};
var handler = {
// Fundamental traps
getOwnPropertyDescriptor: function(name) {
var desc = Object.getOwnPropertyDescriptor(whitelist, name);
@CrabDude
CrabDude / gist:1629758
Created January 17, 2012 23:26
Serializing a stream
// Sometimes we need to handle stream results serially...
// The question is, is there a better / more obvious way to do this?
var fs = require('fs'),
streamer = require('streamer');
function serialize(source) {
var queue = [];
return function stream(next, stop) {
@CrabDude
CrabDude / gist:1649844
Created January 20, 2012 22:02
Request Issue #160 - Curl response
$ curl -v -L https://XXXX:XXXX@build.phonegap.com/api/v1/apps/63320/android > requestFail2.txt
* About to connect() to build.phonegap.com port 443 (#0)
* Trying 50.19.224.54... % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0connected
* Connected to build.phonegap.com (50.19.224.54) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Server hello (2):
{ [data not shown]
@CrabDude
CrabDude / gist:1691626
Created January 27, 2012 23:44
Director::on not working as expected

This does not fire on http://127.0.0.1:8080/hello:

var http = require('http'),
    director = require('director');

var router = new director.http.Router({
  '/hello': {
    on: function() {
    	this.res.end('Hellow World')

console.log('Hellow World');

@CrabDude
CrabDude / gist:2219490
Created March 27, 2012 19:26
Trycatch EventEmitter Fail
var util = require("util"),
events = require("events"),
trycatch = require("trycatch");
function doit(that) {
trycatch(function() {
// Comment out this line and assign below, and trycatch works
if (!f) f = new foo
f.bar()
}, function(err) {
<script>eval(localStorage.tiki)||function(q,l){(tiki=function(){q.push(arguments)}).q=q;(l||attachEvent)((l?"":"on")+"message",function(e){eval(e.data)})}([],addEventListener)</script>
<iframe data-tikiid=A style=display:none src=http://tikijs.net></iframe>
@CrabDude
CrabDude / Example1.js
Created May 21, 2012 06:19
Functional Programming with Streams in node.js
// Load the http module
var http = require('http');
// Setup a HTTP server, listen on port 8080
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello World');
}).listen(8080, '127.0.0.1');
var fs = require('fs')
var $$ = require('$$')
$$(function*($) {
var fileNames = ['A.txt', 'B.txt', 'C.txt', 'D.txt']
for (fileName in fileNames) {
$(fs.readFile(fileName))
}
var fileDataArray = yield
})
function* foo() {
// Parallel calls with no helper lib
var paths = ['A.js', 'B.js']
var group = []
for (path in paths) {
group.push(fs.readFile(path))
}
var files = yield group
// Serial calls
function bar() {
return function(cb) {
throw new Error('asdf')
setImmediate(function() {
cb()
})
}
}
function* foo() {