Skip to content

Instantly share code, notes, and snippets.

var zmq = require("zmq");
var node_uuid = require('node-uuid');
var arrayUnique = function(a) {
return a.reduce(function(p, c) {
if (p.indexOf(c) < 0) p.push(c);
return p;
}, []);
};
@rehanift
rehanift / server.js
Created October 5, 2012 14:13
Mixing callbacks with events
var util = require("util");
var events = require("events");
var AsyncServer = function(){
this.request_count = 0;
};
util.inherits(AsyncServer, events.EventEmitter);
AsyncServer.make = function(){
var server = new AsyncServer();
@rehanift
rehanift / gist:14cda03c79b1ae33c020
Created July 17, 2012 01:19
Javascript Secure Sandbox Notes

https://groups.google.com/d/msg/nodejs/P-gUjRun2Ek/hDXdS704w_MJ

  1. Use a child process to run the code. This process can be a node process that is running another VM inside of it for user code to be run in (it both the process and separated VM).
  2. Chroot the child process / Jail it / Run as Nobody:Nobody / run it in a new session / run it with empty environmental variables / remove ALL globals from node by setting them to undefined (not null) / everything reasonable to lock down the environment.
  3. Use a serialization channel when talking to user code, never ever directly share objects.
  4. Never reuse a child process.
  5. ANY variable given to a child process for interaction with a parent should be through a strict mode function that can talk to code outside of our VM, never give direct references to objects from the privileged vm. This function should be generated inside of the user code context prior to executing any user code and should not use eval(). All references to objects including functions from th
@rehanift
rehanift / fb-reinit.js
Created June 15, 2012 17:58 — forked from anonymous/fb-reinit.js
Issues with FB.init called inside a FbReady callback
var getFriendsInApp = function (callback) {
FB.login(function (response) { // Force FB client to re-authorize
var friends = "SELECT uid, name, pic_square FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())",
friends_in_app = "SELECT uid, name, pic_square FROM user WHERE is_app_user AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"
FB.api({
method: 'fql.query',
query: friends_in_app
}, callback);
});
};
@rehanift
rehanift / vm.js
Created April 29, 2012 21:23
Using contextify to replace vm second version
var vm = function(){
//avoid contextify's js wrapper
var contextifyPath = require('path').resolve(require.resolve('contextify'), '..', '..');
var contextify = require('bindings')({
module_root: contextifyPath,
bindings: 'contextify.node'
});
// basic WeakMap shim if not available
var WM = typeof WeakMap !== 'undefined' ? WeakMap : function WeakMap(){
var keys = [], values = [];
@rehanift
rehanift / user-authenticator.js
Created March 24, 2012 19:50
Using Mock Objects with Node.js and Jasmine
var events = require("events"),
util = require("util");
var UserAuthenticator = function(user_finder){
this.user_finder = user_finder;
};
util.inherits(UserAuthenticator, events.EventEmitter);
UserAuthenticator.make = function(dependencies){
var authenticator = new UserAuthenticator(dependencies.user_finder);
@rehanift
rehanift / gist:1954089
Created March 1, 2012 23:48
SML Stories Application Example
{% contest name:"stories-example" %}
{% partial name:"entry-form" %}
{% contest_form %}
<div>
<label>
Name:
<input type="text" name="registration[name]"/>
</label>
@rehanift
rehanift / acme_service_client.rb
Created February 15, 2012 14:43
Decoupled dependency injection
class AcmeServiceClient
def initialize(http_client, acme_response_translator)
self.http_client = http_client
self.acme_response_translator = acme_response_translator
end
def self.make(dependencies)
instance = self.new(dependencies["http_client"], dependencies["acme_response_translator"])
return instance
end
@rehanift
rehanift / .emacs
Created December 18, 2011 06:14
dot-emacs
(if (< emacs-major-version 24)
(load "~/.emacs_23")
(load "~/.emacs24.d/init.el"))
@rehanift
rehanift / vm-events.js
Created December 10, 2011 17:12
Node VM waiting for event listeners to fire
var vm = require("vm"),
util = require("util"),
events = require("events");
var fooClass = function(){};
util.inherits(fooClass, events.EventEmitter);
fooClass.prototype.run = function(){
this.emit('myevent', "Hello!");
};