Skip to content

Instantly share code, notes, and snippets.

View jpillora's full-sized avatar
👶

Jaime Pillora jpillora

👶
View GitHub Profile
@jpillora
jpillora / my-session-store.js
Last active December 17, 2015 08:39
Express/Connect Session Store Template
// http://www.senchalabs.org/connect/middleware-session.html
// Use with express like so:
//
// app.use(express.session({
// store: new MyStore(),
// secret: 'secret'
// }));
var connect = require('connect');
@jpillora
jpillora / uglifyjs2-browser-minify.js
Created May 26, 2013 12:20
UglifyJS2 browser build - Minify function.
// add missing minify function into UglifyJS2 browser build
// note: source maps removed, original function here:
// https://github.com/mishoo/UglifyJS2/blob/master/tools/node.js#L52
//
// usage:
// UglifyJS.minify(['var foo = 42;', 'function bar() { return 21; }', 'var baz = 7;']);
UglifyJS.minify = function(codes, options) {
@jpillora
jpillora / saveAs.js
Created May 28, 2013 02:45
Chrome text download (<a> download attribute) example For use with forced-server-download method as fallback
// Chrome text download (<a> download attribute) example
// For use with forced-server-download method as fallback
// extracted from https://github.com/eligrey/FileSaver.js
function saveAs(name, text) {
var a = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
if(!("download" in a))
return false;
var blob = new window.Blob([text], {type: "text/plain;charset=utf8"});
a.href = window.URL.createObjectURL(blob);
@jpillora
jpillora / di.coffee
Last active April 30, 2018 01:21
Micro JavaScript Dependancy Injection.Written in CoffeeScript / JS.This is *approximately* how AngularJS works.
# Micro JavaScript Dependancy Injection
define = (name, val) ->
#init modules map if missing
define.mods = define.mods or {}
#store 'name'
define.mods[name] = val
inject = (fn) ->
#regex match on function.toString()
@jpillora
jpillora / render-ast.coffee
Last active June 5, 2019 18:55
Acorn (SpiderMonkey) AST Renderer [Work in progress - Not fully tested]
#render function (ast-object) -> code-string
render = (->
#handlers for each node type
handlers =
Literal: (n) ->
n.raw
Identifier: (n) ->
n.name
ThisExpression: (n) ->
"this"
@jpillora
jpillora / flatten-object.js
Created July 1, 2013 16:42
Flatten Object
//converts this:
//{ counters:
// { 'statsdbad_lines_seen': 0,
// 'statsdpackets_received': 98,
// bucket: 26 },
// timers: {},
// gauges: { gaugor: 303 },
// timer_data: {},
@jpillora
jpillora / install-node.sh
Last active March 20, 2020 01:33
One liner to install the newest version Node.js for Ubuntu (Tested on Mini Ubuntu 13.04)
# visit https://blog.jpillora.com/one-line-install-node-js-on-linux-5964a8a5d40a
@jpillora
jpillora / peer-data-sync.md
Last active April 12, 2017 13:48
A write up on Synchronising Data across a set of Networked Peers

Peer Data Synchronisation

One-way data synchronisation is trivial. Forward all mutating operations to your slave store to also perform them. Master-Slave replication complete.

Two-way data synchronisation - on the other hand - is hard. It involves conflict resolution, varying of complexity, from choosing the newest operation, most relavant operation, or using operational transforms. All of these require vaguely accurate time synchronisation which then produce another set of problems, solvable with comparing timers, using a shared time server, or using [atomic clocks][1].

The current problem

Currently, I'm trying to synchronise session data across multiple authentication servers. I've added to the complexity of the problem by assuming that each node is transient and may only exist for a limited amount of time (i.e. AWS deciding they need to unplug my instance). Also, the connections between each node may fail.

@jpillora
jpillora / join-api-async.coffee
Last active December 22, 2023 22:57
Join API
"use strict"
api1 =
a: (x,y,z,cb) -> setTimeout cb.bind(null, null, x+y+z+1), 500
b: (x,y,z,cb) -> setTimeout cb.bind(null, null, x+y+z+2), 500
api2 =
b: (x,y,z,cb) -> setTimeout cb.bind(null, null, x+y+z+3), 500
c: (x,y,z,cb) -> setTimeout cb.bind(null, null, x+y+z+4), 500
@jpillora
jpillora / file-click-track.js
Last active December 21, 2015 17:09
Track file clicks with jQuery and Google Analytics (gs.js)
//File Click Tracker (jpillora@think.edu.au)
$(document).ready(function() {
//show event instead of sending to GA (modifiable)
var debug = false;
//snippet variables (non-modifiable)
var loc = window.location;
//watch all form submissions with a 'data-track' attribute
$(document).on("click", "a", function(e) {
var a = $(this);