This commit is contained in:
theory 2014-03-17 03:04:59 -06:00
commit f4285a7328
15 changed files with 7280 additions and 0 deletions

1
Procfile Normal file
View File

@ -0,0 +1 @@
web: node init.js

928
deps/theory.js vendored Normal file
View File

@ -0,0 +1,928 @@
/** THEORY **/
;var theory=theory||null;if(theory){root.init()}else{
theory=(function(b,c,fn){
function theory(b,c){
var a = (function(b,c){
var a = a||theory, l = arguments.length;
if(l == 1){
if(a.text.is(b)){
return a.obj.get(a,b);
}
} if(l == 2){
if(a.text.is(c)){
return a.obj.get(b,c);
}
}
});
if(this && theory.bi.is(this)){ return theorize(a) }
return a(b,c);
} var $, _;
function theorize(a){
var $=undefined,_=undefined;
a.log = (function(s){
//console.log(s);
return a.log;
});
a.fns = (function(){
function fns($){
fns.$_ = $ !== undefined? $ : _;
return fns;
} var $;
fns.is = (function(fn){
$ = fns.$_;fns.$_=_;fn = $||fn;
return (fn instanceof Function)? true : false;
});
fns.flow = (function(s,f){ // TODO: BUG: Seriously reconsider then().done() because they fail on .end() after a synchronous callback, provide no doc or support for it until you do.
var t = (function(){
var args = a.list.slit.call(arguments,0), n;
args.push(t);
n = (function(){
(t.list[t.i++] || t.end).apply(t,args);
})();
return t;
}), list = a.list.is(s)? s : a.list.is(f)? f : 0;
f = a.fns.is(f)? f : a.fns.is(s)? s : function(){};
t.end = list? f : function(){}; // TODO: Receives `next` as param, is this desirable?
t.then = (function(fn){
if(a.fns.is(fn)){ t.list.push(fn) }
return t;
});
t.done = (function(fn){
t.end = a.fns.is(fn)? fn : t.end;
return t;
});
t.list = list || [];
t.i = 0;
if(list){ t() }
else{ f(t) }
return t;
});
fns.sort = (function(args){
if(!args){ return {e:"Empty"} }
var args = a.list.slit.call(args, 0), r = {b:[],n:[],t:[],l:[],o:[],f:[]};
for(var i = 0; i < args.length; i++){
if (fns.is(args[i])){
r.f.push(args[i]);
} else if(a.list.is(args[i])){
r.l.push(args[i]);
} else if(a.obj.is(args[i])){
r.o.push(args[i]);
} else if(a.num.is(args[i])){
r.n.push(args[i]);
} else if(a.text.is(args[i])){
r.t.push(args[i]);
} else if(a.bi.is(args[i])){
r.b.push(args[i]);
}
}
return r;
});
fns.$ = (function(t,v){
v = t.$;
t.$=_;
return v;
});
fns.pass = (function(fn,o){
$ = fns.$_;fns.$_=_;if($){ o=fn;fn=$ }
if(a.text.is(o)){ var tmp = a(fn,o); o = fn; fn = tmp }
if(!fns.is(fn)){ return _ }
return (function(){
return fn.apply(o, a.list.slit.call(arguments));
});
});
return fns;
})();
a.list = (function(){
function list($){
list.$ = $ !== undefined? $ : _;
return list;
} var $;
list.is = (function(l){
l = a.fns.$(list)||l;
return (l instanceof Array)? true : false;
});
list.slit = Array.prototype.slice;
list.at = (function(l,i,opt){
var r;
if($=a.fns.$(list)){ opt=i;i=l;l=$ }
if(!l||!i){ return undefined }
if(a.text.is(l)){ l = l.split('') }
if(i < 0){
r = l.slice().reverse();
i = Math.abs(i);
} opt = opt || {};
if(opt.ebb){
for(--i; 0 <= i; i--){ // upgrade to functionalize
if(r && r[i] !== undefined){ return r[i] }
else if(l[i] !== undefined){ return l[i] }
} return undefined;
}
return (r||l)[--i];
});
list.ify = (function(l,opt){
if($=a.fns.$(list)){ opt=l;l=$ }
opt=opt||{};
opt.wedge = opt.wedge||':';
opt.split = opt.split||',';
var r = [];
if(a.list.is(l)){
return l;
} else
if(a.text.is(l)){
var r = new RegExp("\\s*\\"+opt.split+"\\s*",'ig');
return l.split(r);
} else
if(a.obj.is(l)){
a.obj(l).each(function(v,i){
r.push(i+opt.wedge+(a.obj.is(v)? a.text.ify(v) : v));
});
}
return r;
});
list.fuse = (function(l){
var args = a.list.slit.call(arguments, 0), ll;
l = ($=a.fns.$(list))||l;
ll = $? a.fns.sort(args).l : a.fns.sort(args).l.slice(1);
return Array.prototype.concat.apply(l,ll);
});
list.union = list.u = (function(l,ll){ //[1,2,3,4,5] u [3,5,6,7,8] = [1,2,3,4,5,6,7,8]
return not_implemented_yet;
if($=a.fns.$(list)){ ll=l;l=$ }
// yeaaah, try again.
return r;
});
list.intersect = list.n = (function(l,ll){ //[1,2,3,4,5] n [3,5,6,7,8] = [3,5]
return not_implemented_yet;
if($=a.fns.$(list)){ ll=l;l=$ }
// yeaah, try again.
});
list.less = (function(l,s){ // ToDo: Add ability to use a function to determine what is removed.
var args = a.list.slit.call(arguments, 0), sl = s, ls = l;
l = ($=a.fns.$(list))||l;
s = $? args : args.slice(1);
if($ === args.length){ l=ls;s=sl }
sl = s.length;
return a.list(l).each(function(v,i,t){
if(1 == sl && a.test.is(v,s[0])){ return } else
if(a.list(s).each(function(w,j){
if(a.test.is(v,w)){ return true }
})){ return }
t(v);
})||[];
});
list.each = list.find = (function(l,c,t){
if($=a.fns.$(list)){ t=c;c=l;l=$ }
return a.obj.each(l,c,t);
});
list.copy = (function(l){
return a.obj.copy( ($=a.fns.$(list))||l );
});
list.index = 1;
return list;
})();
a.obj = (function(){
function obj($){
obj.$ = $ !== undefined? $ : _;
return obj;
} var $;
obj.is = (function(o){
o = a.fns.$(obj)||o;
return (o instanceof Object && !a.list.is(o) && !a.fns.is(o))? true : false;
});
obj.ify = (function(o){
o = a.fns.$(obj)||o;
if(a.obj.is(o)){ return o }
try{
o = JSON.parse(o);
}catch(e){o={}};
return o;
});
obj.empty = (function(o){
if(!(o = a.fns.$(obj)||o)){ return true }
return obj.each(o,function(v,i){
if(i){ return true }
})? false : true;
});
obj.copy = (function(o,r,l){
if(!r){
o = a.fns.$(obj) || o;
} l = a.list.is(o);
if(r && !a.obj.is(o) && !l){ return o }
r = {}; o = a.obj.each(o,function(v,i,t){
l? t(obj.copy(v,true)) : (r[i] = obj.copy(v,true));
})||[];
return l? o : r;
});
obj.union = obj.u = (function(x,y){
var args = a.list.slit.call(arguments, 0), r = {};
if($=a.fns.$(obj)){ y=x;x=$ }
if(a.list.is(x)){ y = x } else
if(a.list.is(y)){ } else {
y = $? args : args.slice(1);
y.splice(0,0,x);
}
a.list(y).each(function(v,i){
a.obj(v).each(function(w,j){
if(a.obj(r).has(j)){ return }
r[j] = w;
});
});
return r;
});
obj.has = (function(o,k){
if($=a.fns.$(obj)){ k=o;o=$ }
return Object.prototype.hasOwnProperty.call(o, k);
});
obj.each = (function(l,c,_){
if($=a.fns.$(obj)){ _=c;c=l;l=$ }
var i = 0, ii = 0, x, r, rr, f = a.fns.is(c),
t = (function(k,v){
if(v !== undefined){
rr = rr || {};
rr[k] = v;
return;
} rr = rr || [];
rr.push(k);
});
if(a.list.is(l)){
x = l.length;
for(;i < x; i++){
ii = (i + a.list.index);
if(f){
r = _? c.call(_, l[i], ii, t) : c(l[i], ii, t);
if(r !== undefined){ return r }
} else {
if(a.test.is(c,l[i])){ return ii }
}
}
} else if(a.obj.is(l)){
for(i in l){
if(f){
if(a.obj(l).has(i)){
r = _? c.call(_, l[i], i, t) : c(l[i], i, t);
if(r !== undefined){ return r }
}
} else {
if(a.test.is(c,l[i])){ return i }
}
}
}
return f? rr : a.list.index? 0 : -1;
});
obj.get = (function(o,l,opt,f){
if($=a.fns.$(obj)){ l=o;o=$ }
if(a.num.is(l)){ l = a.text.ify(l) }
if(a.list.is(l)){ l = l.join('.') }
if(a.text.is(l)){
f = (l.length == (l = l.replace(a.text.find.__.fn,'')).length)?
undefined : function(){}; l = l.split(a.text.find.__.dot);
} if(!l){ return }
var x = (l||[]).length, r,
deep = (function(o,v){
return a.list(o).each(function(w,j){
if(a.obj(w||{}).has(v)){ return w }
if(a.list.is(w)){ return deep(w,v) }
});
}), get = (function(v,i,t,n){
if(a.list.is(o)){
if(/^\-?\d+$/.test(v)){
n = a.list.index;
v = a.num.ify(v);
} else {
o = deep(o,v);
}
}
if(n || a.obj(o||{}).has(v)){
o = n? a.list(o).at(v) : o[v];
if(i === x - (a.list.index? 0 : 1)){
return f? a.fns.is(o)? o : f : o;
} return;
}
return f || a.test.nil;
}); r = a.list(l).each(get);
return r === a.test.nil? undefined : r;
});
return obj;
})();
a.text = (function(){
function text($){
text.$ = $ !== undefined? $ : _;
return text;
} var $;
text.is = (function(t){
t = (($=a.fns.$(text))!==_)?$:t;
return typeof t == 'string'?true:false;
});
text.get = (function(q){ return });
text.ify = (function(t){
t = (($=a.fns.$(text))!==_)?$:t;
if(JSON){ return JSON.stringify(t) }
return t.toString? t.toString():t;
});
text.random = text.r = (function(l,c){
if($=a.fns.$(text)){ c=l;l=$ } var $ = $||l, s = '';
l = a.num.is($)? $ : a.num.is(c)? c : 16;
c = a.text.is($)? $ : a.text.is(c)? c : '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
while(l>0){ s += c.charAt(Math.floor(Math.random()*c.length)); l-- }
return s;
});
text.clip = (function(t,r,s,e){
if($=a.fns.$(text)){ e=s;s=r;r=t;t=$ } // IE6 fails if e === undefined with Mocha
return t = (t||'').split(r), t=a.num.is(e)?t.slice(s,e):t.slice(s), t.join(r);
});
text.find = (function(t){
var regex = {};
a.log(regex.name = t.name+'.find');
regex.is = /[\.\\\?\*\[\]\{\}\(\)\^\$\+\|\,]/ig
regex.special = {'.':1,'\\':1,'?':1,'*':1,'[':1,']':1,'{':1,'}':1,'(':1,')':1,'^':1,'$':1,'+':1,'|':1,',':1}
regex.mail = /^(("[\w-\s]+")|([\w-]+(?:[\.\+][\w-]+)*)|("[\w-\s]+")([\w-]+(?:[\.\+][\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
regex.base64 = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
regex.list = /(,\s|;\s|,|;|\s)/ig;
regex.css = /(.+?):(.+?);/ig;
regex.url = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
regex.ext = /\.([^\.]+)$/i;
regex.ws_ = /\-/ig;
regex.space = /\s/ig;
regex.num = /(\-\d+\.\d+|\d+\.\d+|\-\d+|\d+)/g;
regex['int'] = /(\-\d+|\d+)/g;
regex.__ = { fn: /\-\>$/, dot: /\./ };
return regex;
})(text);
text.caps = (function(t){
t = a.fns.$(text)||t;
t = (text.is(t))?t:"";
return t.toUpperCase();
});
text.low = (function(t){
t = a.fns.$(text)||t;
t = (text.is(t))?t:"";
return t.toLowerCase();
});
return text;
})();
a.num = (function(){
function num($){
num.$ = ($ !== undefined? $ : _);
return num;
} var $;
num.is = (function(n){
n = (($=a.fns.$(num))!==_)?$:n;
return ( (n===0)? true : (!isNaN(n) && !a.bi.is(n) && !a.list.is(n) && !a.text.is(n))? true : false);
});
num.i = (function(n){return parseInt(a.fns.$(num)||n,10)});
num.dec = (function(n){return parseFloat(a.fns.$(num)||n)});
num.ify = (function(n,o){
if(($=a.fns.$(num))!==_){ o=n;n=$ }
var r, l = a.list.is(o);
if(a.list.is(o)){
if(a.num.is(n)){
return [n];
} if(a.text.is(n)){
r = n.match(a.text.find.num) || [];
return a.list(r).each(function(v,i,t){
t(a.num.ify(v));
});
}
}
r = num.dec(n);
if(a.num.is(r)){ return r }
if(!n){ return }
if(a.text.is(n)){
return a.num.ify( (n.match(a.text.find.num)||[])[0] );
}
});
num.random = num.r = (function(l){
l = ((($=a.fns.$(num))!==_)?$:l)||6;
if(a.list.is(l)){ return (Math.floor(Math.random() * (l[1] - l[0] + 1)) + l[0]) }
l = (l<=14)? l : 14;
var n = '9';
for(var i = 0; i < l-1; i++){ n += '0' }
n = a.num.ify(n);
var r = function(){return Math.floor(Math.random()*10)||(l==1?0:r())};
n = Math.floor(r() + Math.pow(Math.random(),Math.random()) * (n));
if(n.toString().length != l){ return num.r(l) }
return n;
});
return num;
})();
a.bi = (function(){
function bi($){
bi.$ = $ !== undefined? $ : _;
return bi;
} var $;
bi.is = (function(b){
b = (($=a.fns.$(bi))!==_)?$:b;
return (b instanceof Boolean || typeof b == 'boolean')?true:false;
});
return bi;
})();
a.on = (function(){
function on($){
on.$ = $ !== undefined? $ : _;
return on;
} var $, events = {}, sort = (function(A,B){
if(!A || !B){ return 0 } A = A.i; B = B.i;
if(A < B){ return -1 }else if(A > B){ return 1 }
else { return 0 }
});
on.emit = (function(){
if(!a.text.is($ = a.fns.$(on))) return;
var e = events[$] = events[$] || (events[$] = []), args = arguments;
if(!(events[$] = a.list(e).each(function(hear, i, t){
if(!hear.fn) return; t(hear);
hear.fn.apply(hear, args);
}))){ delete events[$]; }
});
on.event = (function(fn, i){
if(!a.text.is($ = a.fns.$(on))) return;
var $ = events[$] = events[$] || (events[$] = [])
, e = {fn: fn, i: i || 0, off: function(){ return !(e.fn = false); }};
return $.push(e), $.sort(sort), e;
});
return on;
})();
a.time = (function(){
function time($){
time.$ = $ !== undefined? $ : _;
return time;
}
time.is = (function(t){
t = ($=a.fns.$(time))||t;
return t? t instanceof Date : (+new Date().getTime());
});
time.now = (function(){
var n = a.num.ify((a.time.is().toString())+'.'+a.num.r(4));
return (theory.time.now.last||0) < n? (theory.time.now.last = n) : time.now();
});
time.loop = (function(fn,d){
var args = a.fns.sort(a.list.slit.call(arguments, 0));
return (args.f.length)?setInterval(a.list(args.f).at(1),a.list(args.n).at(1)):_;
});
time.wait = (function(fn,d){
var args = a.fns.sort(a.list.slit.call(arguments, 0));
return (args.f.length)?setTimeout(a.list(args.f).at(1),a.list(args.n).at(1)):_;
});
time.stop = (function(i){
i = ($=a.fns.$(time))||i;
return (clearTimeout(i)&&clearInterval(i))||true;
});
return time;
})();
a.com = (function($){
var com = a.com;
com.$ = $ !== undefined? $ : _;
com.way = com.way||$;$=_;
com.queue = [];
theory.com.queue = theory.com.queue||[];
com.dc = [theory.time.now()];
com.node = (function(opt){
if(!process._events){ process._events = {} }
if(process.send && !process._events.theory){
process._events.theory = (function(m){
com.msg(a.obj.ify(m));
}); process.on('message',process._events.theory);
process.send({onOpen:{readyState:(process.readyState = 1)},mod:module.theory[opt.way]});
com.wire = process;
return;
}
});
com.page = (function(){
com.src = com.src||(window.location.protocol +'//'+ window.location.hostname)
+ ((window.location.port)?':'+window.location.port:'')
+ (com.path||'/com');
var municate = (function(){
if(!window.SockJS){ return }
theory.com.wire = new window.SockJS(com.src);
theory.com.wire.onopen = function(){
theory.com.open&&theory.com.open();
console.log("Communication initiated at "+com.src+" with "+com.wire.protocol+".");
com.drain();
};
theory.com.wire.onmessage = theory.com.municate||function(m){
m = a.obj.ify(m.data||m);
if(theory.com.asked[m.when]){
a(theory.com.asked,m.when+"->")(m);
delete theory.com.asked[m.when];
return;
}
com.msg(m);
};
theory.com.wire.onclose = function(m){
console.log('close');
theory.com.close&&theory.com.close(m);
};
});
if(theory.com.off || root.opts.com === false){ return }
if(window.SockJS){
municate();
} else {
module.ajax.load(com.url||(location.local+'//cdn.sockjs.org/sockjs-0.3.min.js')
,function(d){municate()});
}
});
com.drain = (function(){
while(theory.com.queue.length > 0){
com.write(theory.com.queue.shift());
}
});
com.write = (function(m,c){
c = c||theory.com.wire;
if(!c || c.readyState !== 1){
theory.com.queue.push(m);
return;
}
if(a.obj.is(m)){
m = a.text(m).ify();
}
//console.log("send --> "+m);
c.send(m);
});
com.init = (function(c){
if(root.node){ com.node({way:c}) }
if(c){ return }
if(root.page){ com.page() }
return com;
});
/** Helpers **/
com.msg = (function(m,c){
theory.obj.get(theory,theory.obj.get(m,'how.way')+'->')(m,c);
});
com.ways = (function(m,w){
var way = w||a.obj.get(m,'how.way')||com.way;
if($=a.fns.$(com)){
way = ($.charAt(0)=='.')?com.way+$:$;
} return m = com.meta(m,way);
});
com.ask = (function(m,f){
if(!a.fns.is(f)){ return }
m = com.ways(m);
delete m.where;
theory.com.asked[m.when] = f;
com.write(m);
});theory.com.asked = theory.com.asked||{};
com.reply = (function(m){
m = com.ways(m);
if(m.how.web){
m.how.way = 'web.reply';
} m.who = m.who||{};
m.who.to = m.who.to||m.who.tid;
com.write(m);
});
com.send = (function(m){
m = com.ways(m);
com.write(m);
});
com.meta = (function(m,opt){
if(!a.obj.is(m)){ m = {what:m} }
var n = {what: (m.what = m.what||{}) };
opt = opt||{c:{}};
if(a.text.is(opt)){ opt = {w:opt,c:{}} }
if(opt.protocol){ opt.c = opt }
a.obj(m).each(function(v,i){
if( i == 'how' || i == 'who' || i == 'what' ||
i == 'when'|| i == 'where'){ return }
n.what[i] = m.what[i] = v; delete m[i];
});
if(!m.how){ n.how={way: opt.w||com.way} }else{
n.how = m.how;
n.how.way = opt.w||m.how.way||com.way;
delete m.how;
} m.how = n.how;
if(!m.when){ n.when=a.time.now() }else{
n.when = m.when;
delete m.when;
} m.when = n.when;
if(!m.who){
if(root.page && !com.who){
n.who = { tid: (com.who=root.who) }
} if(root.node){ n.who = {} }
}else{
if(a.obj.is(m.who)){ n.who=m.who }else{
n.who = {to: m.who}
} if(root.node){
} if(root.page && !com.who){
n.who.tid = com.who = root.who;
} delete m.who;
} m.who = n.who;
if(!m.where){
if(root.page){ if(a.text.is(m.where)){}else{};
} if(root.node){ n.where={pid: process.pid} }
}else{
if(a.obj.is(m.where)){ n.where = m.where }else{
n.where = {at: m.where};
} if(root.node){
if(!a.obj(m.where).has('pid')){ n.where.pid=process.pid }
} delete m.where;
} m.where = n.where;
return n;
}); /** END HELPERS **/
return com;
});
a.test = (function(){
function test($){
if($===undefined && a.fns.is(test.$)){ try{return test.$()}catch(e){return e} }
test.$ = arguments.length? $ : test.nil;
return test;
} test.nil = test.$ = 'ThEoRy.TeSt.NiL-VaLuE';
test._ = (function(r){ r = a.fns.$(test); test.$ = test.nil; return r; });
test.of = (function(t,f){
if(($=test._()) !== test.nil){ f=t;t=$ }
return t instanceof f;
});
test.is = (function(a, b, aStack, bStack){ // modified Underscore's to fix flaws
if(($=test._()) !== test.nil){ b=a;a=$ }
var _ = {isFunction:theory.fns.is
,has:theory.obj.has}, eq = test.is;
aStack = aStack||[]; bStack = bStack||[];
// Identical objects are equal. `0 === -0`, but they aren't identical.
if(a === b){ return a !== 0 || 1 / a == 1 / b }
if(a == null || b == null){ return a === b }
var className = Object.prototype.toString.apply(a);
if(className != Object.prototype.toString.apply(b)){ return false }
switch(className){
case '[object String]': return a == String(b);
case '[object Number]': return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
case '[object Function]': return a.name === b.name && a.toString() === b.toString();
case '[object Date]':
case '[object Boolean]': return +a == +b;
case '[object RegExp]': return a.source == b.source && a.global == b.global &&
a.multiline == b.multiline && a.ignoreCase == b.ignoreCase;
}
if(typeof a != 'object' || typeof b != 'object'){ return false }
var length = aStack.length;
while(length--){ if(aStack[length] == a){ return bStack[length] == b} }
aStack.push(a); bStack.push(b);
var size = 0, result = true;
if(className == '[object Array]'){
size = a.length; result = size == b.length;
if(result){
while(size--){
if(!(result = eq(a[size], b[size], aStack, bStack))){ break }
}
}
}else{
var aCtor = a.constructor, bCtor = b.constructor;
if(aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
_.isFunction(bCtor) && (bCtor instanceof bCtor))){ return false }
for(var key in a){
if(_.has(a, key)){
size++;
if(!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))){ break }
}
} if(result){
for(key in b){
if(_.has(b, key) && !(size--)){ break }
} result = !size;
}
}
aStack.pop(); bStack.pop();
return result;
});
return test;
})();
return a;
}
theory.Name = 'theory';
theory.version = 2.5;
theorize(theory);
return theory;
})(true);
/**
BASE
**/
(function(r){
var root = root||{}, a = theory;
root.opts = root.opts || {};
root.deps = {loaded:{},alias:{},all:{},wait:{}};
root.pollute = ((typeof GLOBAL !== 'undefined' && GLOBAL.global && GLOBAL.process &&
GLOBAL.process.env && GLOBAL.process.pid && GLOBAL.process.execPath)?
(function(){
global.node = root.node = true;
global.opts = root.opts;
global.theory = theory;
module.theory = module.theory||{}
process.env.totheory = __filename;
if(process.env.NODE_ENV==='production'){process.env.LIVE = true};
module.path = require('path');
require.sep = module.path.sep;
module.exports=(function(cb,deps,name){
if(!arguments.length) return theory;
var args = a.fns.sort(a.list.slit.call(arguments, 0)), r
,m = util.require.apply({},arguments);
args.file = root.submodule||(module.parent||{}).filename;
global.aname = global.aname||m.name;
a.obj(util.deps(m.dependencies,{flat:{},src:args.file})).each(function(name,path){
var p = require(root.submodule=path=util.resolve(path,path));
m.theory[name] = (theory.obj.is(p) && theory.obj.empty(p))? undefined : p;
});
module.theory[m.name] = a.obj.ify(a.text.ify(m));
var mod = (theory[m.name] = m.init(m.theory));
if(global.aname === m.name && theory.com) theory.com(theory.Name).init(m.name);
return mod;
});
return;
}) : (function(){
root = window.root = window.root||root;
root.page = true;
root.who = root.who||a.list((document.cookie+';').match(/tid=(.+?);/)||[]).at(-1)||'';
window.console = window.console||{log:function(s){return s}};
console.saw = (function(s){console.log(a.text(s).ify())});
location.local=(location.protocol==='file:'?'http:':'');
var noConflict={__dirname: window.__dirname,module:window.module,exports:window.exports,require:window.require};
window.__dirname = '';
window.module = {exports: (window.exports = {})};
window.module.ajax = {load:(function(b,c){
var d=document,j="script",s=d.createElement(j); module.sync=(s.onload===null||!s.readyState)?0:1; // IE6+
var e=2166136261,g=b.length,h=c,i=/=\?/,w=window.setTimeout,x,y,a=function(z){
document.body&&(z=z||x)&&s&&document.body[z]?document.body[y=z](s):w(a,0);
};if(i.test(b)){for(;g--;)e=e*16777619^b.charCodeAt(g);
window[j+=e<0?-e:e]=function(){h.apply(h,arguments);delete window[j]};b=b.replace(i,"="+j);c=0
};s.onload=s.onreadystatechange=function(){if(y&&/de|m/.test(s.readyState||"m")){
c&&c();a(x='removeChild');try{for(c in s)delete s[c]}catch(b){}
}};s.src=b;c&&a(x='appendChild');
})};module.ajax.load('#');
window.module.ajax.code = util.load;
window.onerror = (function(e,w,l){
console.log(e + " at line "+ l +" on "+ w);
//if(theory.com){ theory.com.send({e:e,url:w,line:l}) }
});
window.require = module.require = function require(p){
if(!p){ return require }
if(util.stripify(p) == util.stripify(theory.Name)){
return util.require;
} var fn, c = 0, cb = function(f){ fn = f; };
theory.list((p = theory.list.is(p)? p : [p])).each(function(v){
window.module.ajax.code(v,function(d){++c && (p.length <= c) && fn && fn(d)});
}); return cb;
}; window.require.sep = '/'; require.resolve = util.resolve; require.cache = {};
util.init();
if(root.opts.amd === false){theory.obj(noConflict).each(function(v,i){window[i]=v});}
if(theory.com){ theory.com(theory.Name).init() }
})
);
var util = {};
util.theorize = (function(mod){
mod.theory = theory.call(true);
if(mod.theory.com){ mod.theory.com(mod.name) }
return mod.theory;
});
util.require = (function(){
var mod, args = a.fns.sort(a.list.slit.call(arguments,0))
, fail = {name:'fail',init:(function(){console.log('module failed to load')})};
if(args.o.length === 1 && !args.t.length && !args.l.length){
mod = a.list(args.o).at(1);
} else {
if(args.f.length){
mod = {
name: a.list(args.t).at(1)
,init: a.list(args.f).at(1)
,dependencies: a.list(args.l).at(1) || a.list(args.o).at(1)
}
}
} mod.name = mod.name||fail.name;
mod.init = mod.init||mod.main||mod.start||mod.boot||mod.cb||mod.fn||fail.init;
mod.dependencies = mod.dependencies||mod.require||mod.deps||mod.dep;
mod.dependencies = a.list.is(mod.dependencies)?
a.list(mod.dependencies).each(function(v,i,t){t(v,1)}) : mod.dependencies;
mod.theory = util.theorize(mod);
if(root.node){ return mod }
args = {cb:function(p, opt){
if(args.launched
|| a.list(util.deps(mod.dependencies,{flat:{}})).each(function(v,j){
if(!(i = root.deps.loaded[j])){ return true }
if(i === 2){ return true }
if(i && i.launch && a.text.is(v) && mod.theory[v] === undefined){ mod.theory[v] = i.launch; }
})){ return }
args.on.off();
args.launched = {launch: (theory[mod.name] = mod.init(mod.theory||theory)), n:mod.name};
module.exports = exports = args.launched.launch;
if(mod.src){
root.deps.loaded[mod.src] = args.launched;
theory.on('ThEoRy_DePs').emit();
} return args.launched.launch;
}}; args.on = theory.on('ThEoRy_DePs').event(args.cb);
args.start = function(){util.deps(mod.dependencies,args); return args.cb()}
args.name = function(src){
module.on = args.name = false;
root.deps.alias[args.src = mod.src = src] = mod.name;
if((root.deps.all[src] = mod.dependencies)){
root.deps.loaded[src] = 2;
} if(!window.JSON){module.ajax.load(root.opts.JSON||location.local // JSON shim when needed
+"//ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js",args.start)
} else { return args.start() };
}; module.on = (!(require||{}).ing)? args.name(util.src(1))||false : args.name;
});
util.deps = (function(deps, opt){
opt = opt || {};
a.obj(deps).each(function(v,i){
var path = i, dopt = {p:i};
if(opt.src){
delete deps[i];
deps[path = util.resolve(opt.src, util.pathify(path))] = v;
} if(a.list.is(v)){
delete deps[i];
v = deps[path] = a.list(v).each(function(w,i,t){t(util.resolve(opt.src,util.pathify(w)),1)})
} if(a.obj.is(v)){
dopt.defer = v;
opt.flat && util.deps(v,{flat: opt.flat});
} if(v && a.text.is(v)){
dopt.name = v;
} if(opt.flat){
var url = util.urlify(util.pathify(path));
if((i = opt.flat[url]) && i !== 1){ return }
opt.flat[url] = (opt.sub? 1 : dopt.name) || util.stripify(path);
if(i !== 1 && url && a.text.is(url) && (v = root.deps.all[url])){
(a.obj.is(v) || a.list.is(v)) && util.deps(v,{flat: opt.flat, sub:1});
} return;
} return util.load(path, dopt);
});
return opt.flat;
});
util.urlify = (function(url){ // via SO, IE6+ safe
if(!root.page){ return url; }
var el= document.createElement('div');
el.innerHTML= '<a href="'+url+'">x</a>';
return el.firstChild.href;
});
util.pathify = (function(p){
if(!root.page){ return p; }
return p = (/\.js$/i.test(p))? p : p+'.js';
});
util.stripify = (function(p){
if(!a.text.is(p)){ return ''; } p=p.replace(/^\./,'');
return (p.split(require.sep).reverse()[0]).replace(/\.js$/i,'');
});
util.resolve = (function(p1, p2){ // via browserify
if('.' != p2.charAt(0)){ return p2.replace('/',require.sep) }
var path = p1.replace('/',require.sep).split(require.sep)
, segs = p2.replace('/',require.sep).split(require.sep)
path.pop();
for(var i=0;i<segs.length;i++){
var seg = segs[i];
if('..' == seg){ path.pop() }
else if('.' != seg){ path.push(seg) }
} return path.join(require.sep);
});
util.load = (function(p, opt ,z){
if(util.stripify(p) == util.stripify(theory.Name)){
return util.require;
} opt = opt || {};
{var w=root.deps.wait;if(module.sync){if(!z && !a.obj.empty(w)){
w[p] = opt;if(opt.defer){w=root.deps.wait = a.obj(w).u(opt.defer)}return;
}w[p] = opt;if(opt.defer){w=root.deps.wait = a.obj(w).u(opt.defer)}}}
var path = util.pathify(p), url = util.urlify(path)
, cb = (function(d){
if(false !== d){
console.log(opt.p||p, ' loaded');
root.deps.loaded[url] = 1;
module.on && module.on(url);
theory.fns.is(opt) && opt(d);
} theory.on('ThEoRy_DePs').emit();
!module.sync && opt.defer && util.deps(opt.defer, opt);
{if(module.sync){delete w[p];if(!a.obj(w).each(function(v,i,t){
delete w[i];util.load(i,v,1);return 1;})){w=root.deps.wait = false}}}
}); if(root.deps.loaded[url]
|| root.deps.loaded[url] === 0){
return cb(false);
} root.deps.loaded[url] = 0;
(require||{}).ing=true;
try{window.module.ajax.load(path,cb);}
catch(e){console.log("Network error.")};
console.log('loading', opt.p||p);
});
util.sandbox = (function(s,n){
try{ // via jQuery
(window.execScript || function(s){
window["eval"].call(window, s);
})(s);
}catch(e){
console.log("sandbox fail: "+n);
console.log(e, s);
}
});
util.theorycount = 0;
util.src = (function(){
var s = document.getElementsByTagName('script');
s = (s[s.length-1]||{}).src;
return util.stripify(s) === theory.Name? location : s||location;
});
util.init = (function(r){
if(!root.page){ return }
var z='', s = document.getElementsByTagName('script'), t;
for(var i in s){var v = s[i]; // IE6 fails on each, use for instead
r = v.src||r;
if(v.id || !v.innerHTML || util.stripify(v.src)
!== util.stripify(theory.Name)){ false;
} else { t = v }
} if(t){
util.sandbox(t.innerHTML,'Theory Configuration');
t.id = "theory"+util.theorycount++;
}
return r;
});
root.init = (function(){
root.pollute();
return util.init;
})();
})()};

10
gate/amplify.store.js Normal file
View File

@ -0,0 +1,10 @@
/*!
* Amplify Store - Persistent Client-Side Storage 1.1.0
*
* Copyright 2011 appendTo LLC. (http://appendto.com/team)
* Dual licensed under the MIT or GPL licenses.
* http://appendto.com/open-source-licenses
*
* http://amplifyjs.com
*/
(function(a,b){function e(a,e){c.addType(a,function(f,g,h){var i,j,k,l,m=g,n=(new Date).getTime();if(!f){m={},l=[],k=0;try{f=e.length;while(f=e.key(k++))d.test(f)&&(j=JSON.parse(e.getItem(f)),j.expires&&j.expires<=n?l.push(f):m[f.replace(d,"")]=j.data);while(f=l.pop())e.removeItem(f)}catch(o){}return m}f="__amplify__"+f;if(g===b){i=e.getItem(f),j=i?JSON.parse(i):{expires:-1};if(j.expires&&j.expires<=n)e.removeItem(f);else return j.data}else if(g===null)e.removeItem(f);else{j=JSON.stringify({data:g,expires:h.expires?n+h.expires:null});try{e.setItem(f,j)}catch(o){c[a]();try{e.setItem(f,j)}catch(o){throw c.error()}}}return m})}var c=a.store=function(a,b,d,e){var e=c.type;d&&d.type&&d.type in c.types&&(e=d.type);return c.types[e](a,b,d||{})};c.types={},c.type=null,c.addType=function(a,b){c.type||(c.type=a),c.types[a]=b,c[a]=function(b,d,e){e=e||{},e.type=a;return c(b,d,e)}},c.error=function(){return"amplify.store quota exceeded"};var d=/^__amplify__/;for(var f in{localStorage:1,sessionStorage:1})try{window[f].getItem&&e(f,window[f])}catch(g){}if(window.globalStorage)try{e("globalStorage",window.globalStorage[window.location.hostname]),c.type==="sessionStorage"&&(c.type="globalStorage")}catch(g){}(function(){if(!c.types.localStorage){var a=document.createElement("div"),d="amplify";a.style.display="none",document.getElementsByTagName("head")[0].appendChild(a);try{a.addBehavior("#default#userdata"),a.load(d)}catch(e){a.parentNode.removeChild(a);return}c.addType("userData",function(e,f,g){a.load(d);var h,i,j,k,l,m=f,n=(new Date).getTime();if(!e){m={},l=[],k=0;while(h=a.XMLDocument.documentElement.attributes[k++])i=JSON.parse(h.value),i.expires&&i.expires<=n?l.push(h.name):m[h.name]=i.data;while(e=l.pop())a.removeAttribute(e);a.save(d);return m}e=e.replace(/[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g,"-");if(f===b){h=a.getAttribute(e),i=h?JSON.parse(h):{expires:-1};if(i.expires&&i.expires<=n)a.removeAttribute(e);else return i.data}else f===null?a.removeAttribute(e):(j=a.getAttribute(e),i=JSON.stringify({data:f,expires:g.expires?n+g.expires:null}),a.setAttribute(e,i));try{a.save(d)}catch(o){j===null?a.removeAttribute(e):a.setAttribute(e,j),c.userData();try{a.setAttribute(e,i),a.save(d)}catch(o){j===null?a.removeAttribute(e):a.setAttribute(e,j);throw c.error()}}return m})}})(),function(){function e(a){return a===b?b:JSON.parse(JSON.stringify(a))}var a={},d={};c.addType("memory",function(c,f,g){if(!c)return e(a);if(f===b)return e(a[c]);d[c]&&(clearTimeout(d[c]),delete d[c]);if(f===null){delete a[c];return null}a[c]=f,g.expires&&(d[c]=setTimeout(function(){delete a[c],delete d[c]},g.expires));return f})}()})(this.amplify=this.amplify||{})

27
gate/sock.js Normal file

File diff suppressed because one or more lines are too long

344
gun.js Normal file
View File

@ -0,0 +1,344 @@
/**** The Abstact Structure
A JSON graph structure that is lightweight and flexible to describe anything.
The current goal, however, is limited to encompassing HTML and CSS for starters.
Immediately after that is describing code as a state.
A node is something which has relationships and or value.
Relationships and values aren't any different, other than that
a relationship is a reference and a value is embedded.
*****/
module.exports = require('theory')
('gun',function(a){
function gun(url){
var u
, db = {_id: url || gun.id() }
, g = function(n,p,v,w){
var args = arguments.length
, bd = this._db || db, t;
w = w || a.time.now();
if(this._id){
v = p;
p = n;
n = this;
args += 1;
}
if(n){
if(n._id && a.obj(bd).has(n._id)){
if(p === u){
console.log('gun delete', n._id);
delete bd[n._id];
theory.on(gun.event).emit({n:n._id,d:1,w:w}, bd._id);
return;
}
if(args >= 3){
var o = {ref:n};
o.p = a.text(p).clip('.',0,-1);
t = o.p? g.at.call(o,n,o.p) : n;
o.p = a.text(p).clip('.',-1);
p = o.path? o.path+'.'+o.p : o.p;
n = o.ref;
o.q = n._id+'.'+p;
if(gun.ham && gun.ham.call(g,n,p,v,w,g.at(n,p))){
return;
}
if(t){
if(v === u){
if(a.list.is(t)){
t = o.val || t;
var j = t.indexOf(o.p);
if(0 <= j){
t.splice(j,1);
} else {
j = a.list(t).find({_ref: o.p});
if(j){
t.splice(--j,1);
}
}
} else {
delete t[o.p];
}
theory.on(gun.event).emit({d:o.q,w:w});
} else {
v = g.at(v);
if(v._id){
v = {_ref: v._id};
} else {
v = g.ify.be(v);
}
var j;
if(a.list.is(v)){
t = o.val || t;
j = a.list.is(t[o.p])? t[o.p].concat(v) : v;
j = a.list(j).each(function(r,i,t){t(r,1)})||{};
t[o.p] = j = a.obj(j).each(function(w,r,t){t(r)})||[];
} else {
t[o.p] = v;
}
theory.on(gun.event).emit({p:o.q,v:v,w:w}, bd._id);
v = j || v;
}
}
return v;
} else
if(args >= 2){
v = g.at(n,p);
return v;
}
return;
}
if(a.obj.is(n)){
n._id = n._id || gun.id();
} else
if(a.text.is(n)){
n = {_id: n};
}
if(n._id){
if(a.obj(bd).has(n._id)){
n = bd[n._id];
} else {
bd[n._id] = n;
theory.on(gun.event).emit({n:n,w:w}, bd._id);
}
return function(p,v,w){
var args = a.list.slit.call(arguments);
return !args.length? n : g.apply(n,args);
}
}
} else {
return bd;
}
}
g.at = function(n,p,o){
if(n === u) return db;
if(p === u) return a.fns.is(n)? n() : n;
var c = this.ref || this.db? this : {}
, pp = a.list.is(p)? p : p.split('.')
, i = 0, l = pp.length, t
, v = a.fns.is(n)? n() : n;
c.db = c.db||db; c.ref = n; c.path = p;
while(i < l && v !== u){
t = pp[i++];
if(a.obj.is(v) && a.obj(v).has(t)){
v = v[t];
if(v && v._ref){
v = a.obj(c.db).has(v._ref)? c.db[v._ref] : u;
c.ref = n; c.path = pp.slice(i).join('.');
}
} else
if(a.list.is(v)){
return a.list(v).each(function(w,j){
if(!w) return;
if(!p) return;
w = a.obj(c.db).has(w._ref||w)? c.db[w._ref||w] : u;
if(!w) return;
if(t === w._id){
i += 1;
p = false;
}
return g.at.call(c,g.at(w),pp.slice(i-1));
});
} else {
v = u;
}
}
if(a.list.is(v)){
c.val = v;
v = a.list(v).each(function(w,j,t){
if(w){
if(a.obj(c.db).has(w._ref||w)) t(c.db[w._ref||w]);
}
}) || [];
}
return i < l? u : v;
}
g.ify = function(o, h, k){
if(!a.obj.is(o)) return;
var hold = h || {}, know = k || [], t;
var n = {_id: o._id || gun.id()};
function assign(o,i,f){
if(a.list.is(f)){
f.push(o);
} else {
f[i] = o;
}
}
function absorb(o,i,f,p,hold,know){
var n = g.ify(o, hold, know);
if(!n._id){
n = n.n;
if(n === p){
hold[f._id = f._id || gun.id()] = f;
}
}
return {_ref: n._id};
}
function be(o,i,f,p){
if(a.obj.is(o)){
if(o._id) return;
if(t=a.list(know).each(function(v,j){
if(v.o===o){
v.at = j;
return v;
}
})){
var r = absorb(o,i,f,p,hold,know);
assign(r,i,f);
assign(r,t.i,t.f);
return;
}
f[i] = {};
know.push({f:f,i:i,o:o,n:f[i]});
a.obj(o).each(function(v,j){
be(v,j,f[i],f);
});
if(f[i]._id){
f[i] = {_ref: f[i]._id};
}
return;
}
if(a.list.is(o)){
f[i] = [];
a.list(o).each(function(v,j){
if(a.obj.is(v)){
t = absorb(v,--j,f[i],p,hold,know);
assign(t, --j, f[i]);
} else {
be(v,--j,f[i]);
}
});
return;
}
if(g.ify.is(o)){
assign(o,i,f);
}
};
if(t=a.list(know).each(function(v,j){
if(v.o===o){
return v;
}
})){
return t;
};
know.push({f:know,i:n._id,o:o,n:n});
hold[n._id] = n;
a.obj(o).each(function(v,i){
be(v,i,n);
});
return h && k? n : hold;
}
g.ify.be = function(v,bd){
var r;
bd = bd || db;
if(a.obj.is(v)){
r = {};
a.obj(v).each(function(w,i){
w = g.ify.be(w);
if(w === u){ return }
r[i] = w;
});
} else
if(a.list.is(v)){ // references only
r = a.list(v).each(function(w,i,t){
w = g.at(w);
if(w._id){
t(w._id);
} else
if(w._ref){
t(w._ref);
} else
if(a.obj(bd).has(w)){
t(w);
}
}) || [];
} else
if(g.ify.is(v)){
r = v;
}
return r;
}
g.ify.is = function(v){ // inull, binary, number (!Infinity), or text.
if(v === Infinity) return false;
if(v === null
|| a.bi.is(v)
|| a.num.is(v)
|| a.text.is(v)){
return true;
}
return false;
}
g.ify.path = function(p){
return !(/[\.\_\$]/ig).test(p);
}
g.on = function(p){
}
return g;
}; gun.event = 'gun';
gun.id = function(){
return a.text.r(9);
}
/* Hypothetical Amnesia Machine
A thought experiment in efficient cause, linear time, and knowledge.
Suppose everything you will ever know in your life was already be stored
in your brain. Now suppose we have some machine, which delicately traverses
your mind and gives you amnesia about all these facts. You now no longer can
recall any of this knowledge because that information is disconnected from
all other pieces of knowledge - making it impossible for your mind to then
associate and thus remember things. But the curious fact is that all this
knowledge is still stored within your mind, it is just inaccessible.
Now suppose, this amnesia machine is designed to unlock various bits of
that knowledge, making it connected again to other related tidbits and thus
making it accessible to you. This unlocking process is activated at some pre-
determined value, such as a timestamp. Can it really be said that this is
indistinguishable from the supposed flow of past, present, and future?
Such that future information is not just unknown, but fundamentally does
not exist, and then by actions taken in the present is caused to be. As a
result of your senses, you then experience this effect, and thus 'learning'
that knowledge. Could we truly build a proof that reality is one way or the
other? But if we did, wouldn't that scurrying little machine just race across
our minds and assure it induces amnesia into our remembrance of its existence?
Nay, we cannot. We can only hypothesize about the existence of this crafty
device. For the blindness that it does shed upon us captivates our perception
of how the world really is, us forever duped into thinking time is linear.
And here, in write and code, is this machine used and exploited. Holding
in its power the ability to quarantine and keep secret, until upon some
value, some condition, some scheme or rendition, does it raise its mighty
clutch of deception and expose the truth, shining in its radiance and glory,
all at the ease of making a single connection. Whereupon we do assure that
all conscious actors are agreed upon in a unified spot, synchronized in the
capacity to realize such beautiful information.
*/
gun.ham = function(n,p,v,w,cv){
// console.log('ham',cv,v);
var g = a.fns.is(this)? this : {}, now, u;
p = p.replace('.',':');
n._age = n._age || {};
if(w < (n._age[p]||0)){
return true;
} else
if(w === (n._age[p]||0)){
if(cv === v || a.test.is(cv,v) || a.text.ify(cv) < a.text.ify(v)){
return true;
}
} else
if((now = a.time.now() + 1) < w){ // tolerate a threshold of 1ms.
/* Amnesia Quarantine */
a.time.wait(function(){
g(n,p,v,w);
}, Math.ceil(w - now)); // crude implementation for now.
return true;
}
if(v === u){
delete n._age[p];
} else {
n._age[p] = w;
}
}
return gun;
});

19
init.js Normal file
View File

@ -0,0 +1,19 @@
process.env.rootdir = __dirname;
var LIVE = process.env.LIVE || (process.env.NODE_ENV === 'production')
, web = require(process.env.COALESCEPATH = LIVE?'coalesce':process.env.rootdir+'/../coalesce/coalesce')
, opt = {};
opt.port = LIVE? 80 : 8888 ;
process.env.domain = LIVE? 'http://gunjs.herokuapp.com' :
(function(){require('child_process').exec('ifconfig',function(e,r){
console.log('on',process.env.domain='http://'+ r.match(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/)[0] +':'+opt.port)
})})();
opt.hook = {
pre: (function(req,res){
console.log("--------- "+req.flow+" : "+req.url.pathname +" ---------------");
})
};
opt.run = [];
web(opt);
console.log("Gun @ "+ opt.port);

14
package.json Normal file
View File

@ -0,0 +1,14 @@
{ "name": "gun"
, "version": "0.0.1"
, "author": "Mark Nadal"
, "description": "Graph engine."
, "engines": ["node >= 0.6.6"]
, "dependencies": {
"theory": "~>0.2.5",
"coalesce": "~>0.1.9b",
"aws-sdk": "~>2.0.0",
"redis": "~>0.10.1",
"mime": "~>1.2.11"
}
, "main": "gun"
}

82
shot.js Normal file
View File

@ -0,0 +1,82 @@
module.exports = require('theory')
('shot',function(a){
return function(opt){
opt = opt || {};
opt.src = opt.src || '';
opt.path = opt.path || '.'
opt.batch = opt.batch || 0;
opt.throttle = opt.throttle || 0;
if(root.node){ return require(opt.path+'/shots')(opt); }
var u, shot = {},
store = window.amplify && window.amplify.store? amplify.store
: function(src, data){
if(data === u){ return store[src] }
return store[src] = data;
}
theory.on(a.gun.event).event(shot.fire = function(m, db){
console.log('shot', m, db);
var w;
if(m){ shot.add(m, db, shot.batch) }
if(shot.lock){ return }
if((w = a.time.now()) - shot.last < opt.throttle
&& shot.batch.length < opt.batch){ return }
$.post(opt.src, {b:a.text.ify(shot.batch)}, function(e,r){
console.log('reply', e,r);
shot.del(m, db);
});
shot.last = w;
shot.batch = [];
});
shot.batch = [];
shot.last = a.time.now();
shot.list = function(m){
var g = store(a.gun.event) || {};
a.obj(g).each(function(l,db){
a.list(l).each(function(v,i,t){
theory.on(a.gun.event).emit(v, db);
});
});
}
shot.add = function(m, db, b){
if(a.list.is(b)) b.push(m);
var g = store(a.gun.event) || {};
g[db] = g[db] || [];
if(a.list(g[db]).each(function(v){
if(v && v.w === m.w){
return true;
}
})){ return g[db]; } // already added
g[db].push(m);
store(a.gun.event, g);
return g[db];
}
shot.del = function(m, db){
var g = store(a.gun.event) || {};
g[db] = g[db] || [];
g[db] = a.list(g[db]).each(function(v,i,t){
if(v && v.w === m.w){ // && a.test.is(m,v)){
return;
}
t(v);
});
store(a.gun.event, g);
}
shot.sort = function(A,B){
if(!A || !B){ return 0 }
A = A.w; B = B.w;
if(A < B){ return -1 }
else if(A > B){ return 1 }
else { return 0 }
}
shot.list();
return {gun: a.gun
,wait: function(){
shot.lock = 1;
}
,go: function(){
shot.last = shot.lock = 0;
shot.fire();
}
};
}
},['./gun'])

82
shots.js Normal file
View File

@ -0,0 +1,82 @@
module.exports = require('theory')
('shot',function(a){
var redis = a.redis
, client = redis.createClient();
return function(opt){
opt = opt || {};
opt.src = opt.src || '';
opt.path = opt.path || '.'
opt.batch = opt.batch || 0;
opt.throttle = opt.throttle || 0;
opt.redis = opt.redis || {};
var u, shot = {},
store = function(src, data){
if(data === u){ return store[src] }
return store[src] = data;
}
theory.on(a.gun.event).event(function(m, db){
console.log('shot', m, db);
shot.add(m, db, shot.batch);
if(a.time.now() - shot.last < opt.throttle
&& shot.batch.length < opt.batch){ return }
$.post(opt.src, shot.batch, function(e,r){
console.log('reply', e,r);
shot.del(m, db);
});
shot.batch = [];
});
shot.batch = [];
shot.last = a.time.now();
shot.list = function(m){
var g = store(a.gun.event) || {};
a.obj(g).each(function(l,db){
a.list(l).each(function(v,i,t){
theory.on(a.gun.event).emit(v, db);
});
});
}
shot.add = function(m, db, b){
if(a.list.is(b)) b.push(m);
var g = store(a.gun.event) || {};
g[db] = g[db] || [];
if(a.list(g[db]).each(function(v){
if(v && v.w === m.w){
return true;
}
})){ return g[db]; } // already added
g[db].push(m);
store(a.gun.event, g);
return g[db];
}
shot.del = function(m, db){
var g = store(a.gun.event) || {};
g[db] = g[db] || [];
g[db] = a.list(g[db]).each(function(v,i,t){
if(v && v.w === m.w){ // && a.test.is(m,v)){
return;
}
t(v);
});
store(a.gun.event, g);
}
shot.sort = function(A,B){
if(!A || !B){ return 0 }
A = A.w; B = B.w;
if(A < B){ return -1 }
else if(A > B){ return 1 }
else { return 0 }
}
shot.start = function(){
client.on('error', function(e){
if(!(/ECONNREFUSED/).test(e)){ return }
console.log("gun starting redis");
var path = 'redis-server';
if(!(require('fs').existsSync||require('path').existsSync)('/usr/local/bin/redis-server')){
path =
}
});
};
shot.start();
return {gun: a.gun};
}
},[__dirname+ '/gun', 'redis'])

1284
test/expect.js Normal file

File diff suppressed because it is too large Load Diff

182
test/mocha.css Normal file
View File

@ -0,0 +1,182 @@
body {
font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif;
padding: 60px 50px;
}
#mocha ul, #mocha li {
margin: 0;
padding: 0;
}
#mocha ul {
list-style: none;
}
#mocha h1, #mocha h2 {
margin: 0;
}
#mocha h1 {
margin-top: 15px;
font-size: 1em;
font-weight: 200;
}
#mocha h1 a {
text-decoration: none;
color: inherit;
}
#mocha h1 a:hover {
text-decoration: underline;
}
#mocha .suite .suite h1 {
margin-top: 0;
font-size: .8em;
}
#mocha h2 {
font-size: 12px;
font-weight: normal;
cursor: pointer;
}
#mocha .suite {
margin-left: 15px;
}
#mocha .test {
margin-left: 15px;
}
#mocha .test:hover h2::after {
position: relative;
top: 0;
right: -10px;
content: '(view source)';
font-size: 12px;
font-family: arial;
color: #888;
}
#mocha .test.pending:hover h2::after {
content: '(pending)';
font-family: arial;
}
#mocha .test.pass.medium .duration {
background: #C09853;
}
#mocha .test.pass.slow .duration {
background: #B94A48;
}
#mocha .test.pass::before {
content: '?';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #00d6b2;
}
#mocha .test.pass .duration {
font-size: 9px;
margin-left: 5px;
padding: 2px 5px;
color: white;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
}
#mocha .test.pass.fast .duration {
display: none;
}
#mocha .test.pending {
color: #0b97c4;
}
#mocha .test.pending::before {
content: '?';
color: #0b97c4;
}
#mocha .test.fail {
color: #c00;
}
#mocha .test.fail pre {
color: black;
}
#mocha .test.fail::before {
content: '?';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #c00;
}
#mocha .test pre.error {
color: #c00;
}
#mocha .test pre {
display: inline-block;
font: 12px/1.5 monaco, monospace;
margin: 5px;
padding: 15px;
border: 1px solid #eee;
border-bottom-color: #ddd;
-webkit-border-radius: 3px;
-webkit-box-shadow: 0 1px 3px #eee;
}
#error {
color: #c00;
font-size: 1.5 em;
font-weight: 100;
letter-spacing: 1px;
}
#stats {
position: fixed;
top: 15px;
right: 10px;
font-size: 12px;
margin: 0;
color: #888;
}
#stats .progress {
float: right;
padding-top: 0;
}
#stats em {
color: black;
}
#stats li {
display: inline-block;
margin: 0 5px;
list-style: none;
padding-top: 11px;
}
code .comment { color: #ddd }
code .init { color: #2F6FAD }
code .string { color: #5890AD }
code .keyword { color: #8A6343 }
code .number { color: #2F6FAD }

27
test/mocha.html Normal file
View File

@ -0,0 +1,27 @@
<html>
<head>
<title>Gun Tests</title>
<link rel="stylesheet" href="./mocha.css"/>
<script src="./mocha.js"></script>
<script>mocha.setup('bdd');</script>
<script src="./expect.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="../gate/amplify.store.js"></script>
<!--
<script src="../gate/sock.js"></script>
<script src="../../jquery.js"></script>
-->
<script src="../deps/theory.js"></script>
<script src="./server.js"></script>
<script src="../shot.js"></script>
<script src="./test.js"></script>
<style>
</style>
</head>
</head>
<body>
Gun Tests
<div id="debug"></div>
<div id="mocha"></div>
</body>
</html>

4228
test/mocha.js Normal file

File diff suppressed because it is too large Load Diff

25
test/server.js Normal file
View File

@ -0,0 +1,25 @@
module.exports=require('theory')
({name: 'echo'
, state: {way:'state', flow:-1}
, invincible: true
//, dep: ['../gun']
, init: function(a){
var echo = {};
echo.put = function(m){
console.log('stream', m);
}
echo.state = function(m){
var w = echo.get(m);
console.log('state', w);
if(w){
m.what.body = 1;
}
a.com.reply(m);
};
echo.get = function(m){
return !a.obj.empty(a(m,'what.form'))? a(m,'what.form')
: !a.obj.empty(a(m,'what.url.query'))? a(m,'what.url.query')
: false ;
};
return echo;
}});

27
test/test.js Normal file
View File

@ -0,0 +1,27 @@
$(function(){
/*
Tests not finished!
How does one automate testing of human input?
*/
describe('Start',function(){
it('tests',function(){
//return;
var s = theory.shot({src: '/test/server.js'})
, g = s.gun('gunjs.herokuapp.com/state.json');
s.wait();
var b = g({});
var c = g({});
b('name','Bob');
c('name','Calvin');
c('brother',b);
s.go();
console.log('first test done');
});
});
(function run(){
if(theory && theory.shot){
return mocha.run();
}
theory.time.wait(run,10);
})();
});