Skip to content

Instantly share code, notes, and snippets.

@totherik
totherik / crawl.js
Last active August 29, 2015 14:02
Crawl express 4.x application to build middleware structure and route mappings.
'use strict';
/**
* In order for this to work correctly, `this.path = path;` needs to be added in
* the Layer constructor function here:
* https://github.com/visionmedia/express/blob/7df7f7a575df4cf26c1d43535a6a1a6b8778933c/lib/router/layer.js#L21
*/
module.exports = function crawl(obj, path, tree) {
path = path || '';
@totherik
totherik / httprx.js
Last active August 29, 2015 14:02
http server as Rx observable
'use strict';
var http = require('http');
var Rx = require('rx');
var qs = require('qs');
var server, source, subscription;
server = http.createServer();
@totherik
totherik / nil.js
Last active August 29, 2015 14:02
Naïve approximation of Swift's nil-safe property access operator `?` - based on Zakas' "Defensive Objects", but kinder.
import nil from 'optionil'
let obj = {
str: 'literal',
obj: {
num: 5
}
};
obj = nil(obj);
@totherik
totherik / gist:3a4432f26eea1224ceeb
Last active April 9, 2024 00:46
v8 --allow-natives-syntax RuntimeFunctions
Per https://code.google.com/p/v8/codesearch#v8/trunk/src/runtime.cc
%CreateSymbol
%CreatePrivateSymbol
%CreateGlobalPrivateSymbol
%NewSymbolWrapper
%SymbolDescription
%SymbolRegistry
%SymbolIsPrivate
@totherik
totherik / reverse.js
Created March 11, 2014 16:15
Reverse Express routes.
'use strict';
var assert = require('assert');
var path2regex = require('path-to-regexp');
function reverse(route, obj) {
var keys;
keys = [];
'use strict';
module.exports = function (router) {
// Register function under slugified function name: `my-foo-route`
// Similar to `named.js`, the more I think about it not sure how this would work.
router.get('/foo', function myFooRoute(req, res) {
res.send('ok');
});
@totherik
totherik / npmjs.ini
Last active August 29, 2015 13:56
npmjs.org registry install script
; CouchDB Config
; Drop in PREFIX/local.d/npmjs.ini
[couch_httpd_auth]
public_fields = appdotnet, avatar, avatarMedium, avatarLarge, date, email, fields, freenode, fullname, github, homepage, name, roles, twitter, type, _id, _rev
users_db_public = true
[httpd]
secure_rewrites = false
@totherik
totherik / travesty.js
Last active August 29, 2015 13:56
Load dust helpers in Node.js without making them global.
var vm = require('vm');
var env, script;
env = { dust: { helpers: {} } };
script = vm.createScript(fs.readFileSync(require.resolve('dustjs-helpers'), { encoding: 'utf8' }));
script.runInNewContext(env);
env.dust.helpers // [object Object] with all the helpers.
@totherik
totherik / fresh.js
Created February 13, 2014 15:24
Get a unique instance of any module. Simply call `getFresh(module);`
function getFresh(name) {
var orig, fresh;
orig = snapshot(name);
invalidate(name);
fresh = require(name);
invalidate(name);
restore(orig);
@totherik
totherik / mjson.js
Created February 6, 2014 19:36
A naïve msjon impl for node.js.
#!/usr/bin/env node
var chunks = [];
process.stdin.on('data', chunks.push.bind(chunks));
process.stdin.on('end', function () {
var json;
json = Buffer.concat(chunks).toString('utf8');
json = JSON.parse(json);
json = JSON.stringify(json, null, 2);
process.stdout.write(json);