Skip to content

Instantly share code, notes, and snippets.

@jonpaul
Created August 22, 2012 14:06
Show Gist options
  • Save jonpaul/3425938 to your computer and use it in GitHub Desktop.
Save jonpaul/3425938 to your computer and use it in GitHub Desktop.
var A, E, J, M, WS, async, duc, e, ejs, euc, fs, haml, http, mime, n, qs, url, w, _ref, _ref1;
_ref = [
JSON, Math, decodeURIComponent, encodeURIComponent, (function(p) {
return p.replace(/\/*$/, '').replace(/^\/?/, '/');
}), (function(d, s) {
var k, v;
for (k in s) {
v = s[k];
d[k] = (d[k] instanceof Array ? d[k].concat(v) : v);
}
return d;
}), function(s, c) {
return s.split(/\s+/).map(c);
}
], J = _ref[0], M = _ref[1], duc = _ref[2], euc = _ref[3], n = _ref[4], e = _ref[5], w = _ref[6];
_ref1 = w('http url querystring fs async faye-websocket vault/node/aes haml ejs mime', require), http = _ref1[0], url = _ref1[1], qs = _ref1[2], fs = _ref1[3], async = _ref1[4], WS = _ref1[5], E = _ref1[6], haml = _ref1[7], ejs = _ref1[8], mime = _ref1[9];
A = (function() {
function A(request, response, _m) {
var c, h, _ref2,
_this = this;
this.request = request;
this.response = response;
this._m = _m;
_ref2 = [{}, (this.request.headers.cookie || '').split(/\s*;\s*/)], h = _ref2[0], c = _ref2[1];
c.map(function(p) {
var q;
q = p.split('=');
return h[duc(q[0])] = duc(q[1]);
});
A.aes().decrypt(h.session || '', function(x, j) {
var d, i, k, v;
delete h.session;
_this.params = {
splat: []
};
_this.cookies = h;
_this.session = (x ? {} : J.parse(j));
_this._u = url.parse(_this.request.url, true);
_this._h = {
'Set-Cookie': []
};
e(_this, A.h);
_this._b = A.r.filter(function(r) {
return r[0] === _this._m && r[2].test(_this._u.pathname);
})[0];
return _this._c((function() {
var _i, _len, _ref3, _ref4;
if (this._b) {
_ref3 = [[], this._u.pathname.match(this._b[2])], this._a = _ref3[0], d = _ref3[1];
_ref4 = this._b[1];
for (i = _i = 0, _len = _ref4.length; _i < _len; i = ++_i) {
k = _ref4[i];
v = duc(d[i + 1]);
this._a.push(v);
if (k === '*') {
this.params.splat.push(v);
} else {
this.params[k] = v;
}
}
return e(this.params, (/\bapplication\/x-www-form-urlencoded\b/.test(this.request.headers['content-type']) ? qs.parse(this.request.body) : this._u.query));
}
}).call(_this));
});
}
A.prototype._x = function(c) {
var _this = this;
return this._c = function() {
var p;
p = _this._u.pathname;
if (p.split('/').indexOf('..') >= 0) {
_this.response.writeHead(400, {});
return _this.response.end();
} else {
return async.series(A.b.filter(function(f) {
return p.substr(0, f.c.length) === f.c;
}).map(function(f) {
return function() {
return f.apply(_this, arguments);
};
}).concat(function() {
if (_this._b) {
return c.call(_this, (function() {
return _this._b[3].apply(_this, _this._a);
}));
} else {
return fs.readFile((A["public"] || './public') + p, function(x, f) {
if (x) {
_this.response.writeHead(404, {});
return _this.response.end();
} else {
_this.response.writeHead(200, {
'Content-Length': f.length,
'Content-Type': mime.lookup(p) + '; charset=utf-8'
});
return _this.response.end(f);
}
});
}
}));
}
};
};
A.prototype.render = function(s) {
var _this = this;
s = (s === void 0 ? '' : String(s));
return A.aes().encrypt(J.stringify(this.session), function(x, j) {
var h;
h = {};
if (s) {
e(h, {
'Set-Cookie': ['session=' + euc(j) + '; Path=/; HttpOnly'],
'Content-Type': 'text/html; charset=utf-8',
'Content-Length': new Buffer(s, 'utf8').length
});
}
e(h, _this._h);
_this.response.writeHead(_this._s || 200, h);
return _this.response.end(s);
});
};
A.prototype.cookie = function(c) {
var k, s, v, _results;
_results = [];
for (k in c) {
v = c[k];
_results.push(v = typeof v === 'string' ? {
value: v
} : (v, s = euc(k) + '=' + euc(v.value) + '; Path=' + v.path || '/', v.domain ? s += '; Domain=' + v.domain : void 0, v.expires ? s += '; Expires=' + v.expires.toGMTString() : void 0, v.http ? s += '; HttpOnly' : void 0, v.secure ? s += '; Secure' : void 0, e(this._h, {
'Set-Cookie': s
})));
}
return _results;
};
return A;
})();
e(A.prototype, {
headers: (function(o) {
return e(this._h, o);
}),
status: (function(n) {
return this._s = parseInt(n, 10);
}),
redirect: (function(u, s) {
if (s == null) {
s = 303;
}
this.status(s);
this.headers({
'Location': u
});
return this.render();
}),
haml: (function(n) {
var _this = this;
return A.v('haml', n, function(x, t) {
return _this.render(haml(t)(_this));
});
}),
ejs: (function(n, o) {
var _this = this;
return A.v('ejs', n, function(x, t) {
return _this.render(ejs.render(t, e((o != null ? o.locals : void 0) || {}, A.h)));
});
}),
puts: function(s) {
return console.log(s);
}
});
e(A, {
r: [],
c: [''],
context: (function(p, f) {
this.c.push(n(p));
f(this);
return this.c.pop();
}),
b: [],
before: (function(b) {
b.c = this.c.join('');
return this.b.push(b);
}),
h: {},
helpers: (function(o) {
return e(this.h, o);
}),
t: {},
template: (function(n, t) {
return this.t[n] = t;
}),
v: (function(t, n, c) {
var f;
if (f = this.t[n + '.' + t]) {
return c(null, f);
} else {
return fs.readFile((this.views || './views') + '/' + n + '.' + t, function(x, f) {
return c(null, f.toString());
});
}
}),
run: (function(q) {
return http.createServer(this.call).on('upgrade', this.ws).listen(q || 4567);
}),
aes: function() {
return new E(this.session_secret);
}
});
w('get post put delete patch head options websocket eventsource', function(v) {
return A[v] = function(p, f) {
var m, o;
p = n(this.c.join('') + n(p));
o = (p.match(/[\/\.](\*|:[a-z\_\$][a-z0-9\_\$]*)/g) || []).map(function(s) {
return s.replace(/^[^a-z0-9\_\$\*]*/, '');
});
m = new RegExp('^' + p.replace(/([\/\.])/g, '\\$1').replace(/\*|:[a-z\_\$][a-z0-9\_\$]*/ig, '(.+?)') + '$');
return this.r.push([v.toUpperCase(), o, m, f]);
};
});
A.call = function(req, res) {
var b;
req.setEncoding('utf8');
b = '';
req.on('data', function(s) {
return b += s;
});
return req.on('end', function() {
var ES, a, k;
req.body = b;
ES = WS.EventSource;
k = ES.isEventSource(req);
a = new A(req, res, k ? 'EVENTSOURCE' : req.method);
if (k) {
return a._x(function(h) {
var s;
s = a.socket = new ES(req, res);
h();
return s.addEventListener('close', function() {
return s = null;
});
});
} else {
return a._x(function(h) {
var r;
r = h();
if (typeof r === 'string') {
return this.render(r);
}
});
}
});
};
A.ws = function(r, s, h) {
var a;
a = new A(r, s, 'WEBSOCKET');
return a._x(function(H) {
w = a.socket = new WS(r, s, h);
H();
return w.addEventListener('close', function() {
return w = null;
});
});
};
module.exports = A;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment