Skip to content

Instantly share code, notes, and snippets.

// res.write is defined by the app framework to take callbacks; it stores the
// callbacks in a queue that ensures FIFO order, and when req.end is called it
// waits for all the write callbacks to finish before ending
var hello = function(req, res) {
res.writeHead(200, {});
res.write(function(){ this('Hello World'); };
res.end();
}
// Add some ruby-like methods to some of the builtins
Object.prototype.instance_eval = function (block) {
// Convert the function to a string so that we can rebind it
if (typeof block === 'function') {
block = "(" + block + ").call(this)";
}
// Eval using "this" as the "with" scope
return eval("with(this) { " + block + "}");
};
include("/utils.js");
// "this" scope is the object, closures work like normal. Basically this is a nowmal "call" use for functions
Object.prototype.instance_eval = function (input) {
// Convert the function to a string so that we can rebind it
if (typeof input === 'function') {
return input.call(this);
}
var Base = {
_ops: {},
_privateFnc: function () {},
abstract: function () {},
init: function (ops) {
this._ops = ops || {};
// INIT
}
};
@polotek
polotek / pipeaccept.js
Created May 18, 2011 01:16 — forked from heapwolf/pipeaccept.js
accepting input from a pipe, nodejs
var data;
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
data += chunk;
});
process.stdin.on('end', function() {
@polotek
polotek / clone.js
Created September 24, 2011 19:22 — forked from rwaldron/clone.js
Real, deep copied objects.
function hasOwnProperty(key) {
if(this[key]) {
var proto = this.prototype;
if(proto) {
return ((key in this.prototype) && (this[key] === this.prototype[key]));
}
return true;
} else {
return false;
}
@polotek
polotek / route.js
Created December 5, 2011 19:19 — forked from creationix/route.js
Sanity check for async template idea
Creationix.route("GET", "/", function (req, res, params, next) {
loadIndex(function (err, home) {
home.render("frontindex",
links: query("index", "links"),
articles: loadArticles()
}, function (err, html) {
if (err) return next(err);
res.writeHead(200, {
"Content-Length": Buffer.byteLength(html),
"Content-Type": "text/html; charset=utf-8"
@polotek
polotek / route.js
Created December 6, 2011 18:50 — forked from creationix/route.js
Sanity check for async template idea
Creationix.route("GET", "/", function (req, res, params, next) {
render("frontindex", {
title: query("index", "title"),
links: query("index", "links"),
articles: loadArticles
}, function (err, html) {
if (err) return next(err);
res.writeHead(200, {
"Content-Length": Buffer.byteLength(html),
"Content-Type": "text/html; charset=utf-8"
@polotek
polotek / benchCallbacks.js
Created April 12, 2012 00:24 — forked from bjouhier/benchCallbacks.js
streamline vs. callbacks bench
"use strict";
var fs = require('fs');
var cache = {}, hit = 0, missed = 0;
function load(name, cb) {
var res = cache[name];
if (res) {
process.nextTick(function() {
hit++;
cb(null, res);
@events =
events: {}
on: (topic, handler, context = this) ->
(@events[topic] or= []).push {handler, context}
trigger: (topic, args...) ->
return unless @events[topic]?
handler.apply(context, args) for {handler, context} in @events[topic]