Skip to content

Instantly share code, notes, and snippets.

View jhs's full-sized avatar

Jason Smith jhs

  • McKinsey & Company
  • New York, NY
View GitHub Profile
@jhs
jhs / normalize_path.erl
Created January 22, 2012 14:46
normalize_paths translated from Erlang to Javascript
% normalize_path experiments
-define(SEPARATOR, $\/).
main(_) -> ok
, Paths = [ ""
, "/"
, "/foo/bar/baz"
, "foo/one//two///three////"
, "foo/bar/..//baz"
@jhs
jhs / gist:1669518
Created January 24, 2012 10:31 — forked from fdmanana/gist:832610
The CouchDB replicator database

1. Introduction to the replicator database

A database where you PUT/POST documents to trigger replications and you DELETE to cancel ongoing replications. These documents have exactly the same content as the JSON objects we used to POST to /_replicate/ (fields "source", "target", "create_target", "continuous", "doc_ids", "filter", "query_params".

Replication documents can have a user defined "_id". Design documents (and _local documents) added to the replicator database are ignored.

The default name of this database is _replicator. The name can be changed in the .ini configuration, section [replicator], parameter db.

2. Basics

@jhs
jhs / With optimization.txt
Created February 28, 2012 00:37
Jason's view benchmark (11" MBA, 4GB)
$ docs=500000 batch=50000 ./bench.sh small_doc.tpl
Server: CouchDB/1.2.0a-f023052-git (Erlang OTP/R15B)
{"couchdb":"Welcome","version":"1.2.0a-f023052-git"}
[INFO] Created DB named `db1'
[INFO] Uploaded 50000 documents via _bulk_docs
[INFO] Uploaded 50000 documents via _bulk_docs
[INFO] Uploaded 50000 documents via _bulk_docs
[INFO] Uploaded 50000 documents via _bulk_docs
[INFO] Uploaded 50000 documents via _bulk_docs
@jhs
jhs / lease.js
Created March 3, 2012 13:58
Renewing CQS lease
message.on('heartbeat', function(percent) {
LOG.debug('heartbeat: ' + percent);
if(percent < 66)
return;
if(percent > 99) {
LOG.fatal('No more time for rake: ' + self.task);
return self.cancel();
}
@jhs
jhs / app.js
Created March 16, 2012 05:13
Failing node-couchapp Kanso application
// Example node-couchapp Kanso application.
//
var fs = require('fs')
var ddoc = module.exports = { 'views':{} }
fs.readdirSync(__dirname + '/views').forEach(function(file) {
var match = file.match(/^(.+)\.js$/)
, mod = match && match[1]
@jhs
jhs / hello.js
Created April 6, 2012 00:41
Safe Javascript semicolons
var http = require('http');;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
http.createServer(function (request, response) {;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
response.writeHead(200, {'Content-Type': 'text/plain'});;;;;;;;;;;;;;;;;;;;;;;
response.end('Hello World\n');;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
}).listen(8124);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
console.log('Server running at http://127.0.0.1:8124/');;;;;;;;;;;;;;;;;;;;;;;;;
@jhs
jhs / irccloud_search.js
Created May 25, 2012 10:39
Get irccloud backlog
var request = require('request')
var SINCE = +process.env.since
if(!SINCE)
throw new Error('Need $since')
get_lines(SINCE)
function get_lines(beforeid) {
var headers = { 'Cookie': process.env.cookie }
if(!headers.Cookie)
@jhs
jhs / iris-redis-example.js
Created August 4, 2012 02:00
Iris-Redis example
if(!process.env.redis_pass)
return console.error('Run this first: export redis_pass=<your password>')
var redis = require('iris-redis') // npm install iris-redis first, of course.
var client = redis.createClient(6379, 'redis.pouch.iriscouch.com')
client.auth(process.env.redis_pass)
client.on("ready", function() {
console.log('Connected %s at %s:%d', client.host, client.stream.remoteAddress, client.stream.remotePort)
client.iris_config(function(er, config) {
@jhs
jhs / for_in.js
Created August 10, 2012 16:34
Looping syntax
for (var thing in {'foo':1, 'bar':1, 'neat':1}) {
console.log('doing ' + thing)
doing(thing)
}
@jhs
jhs / example.js
Created December 10, 2012 03:16
My Node.js modules these days
// Short module explanation // Something to gather my thoughts
// // I think this looks cool.
//
module.exports = the_exported_function // Modules are a function. Always.
//
module.exports.extra = extra // Additional API entry points if
module.exports.other = other // desired.
//
var util = require('util') // Other packages from npm or core
var assert = require('assert') // No comma-first due to lots of