diff --git a/gate/s3.js b/gate/s3.js index 8815cada..b0bd2523 100644 --- a/gate/s3.js +++ b/gate/s3.js @@ -9,7 +9,6 @@ module.exports=require('theory') AWS.config.sslEnabled = conf.sslEnabled = false; } AWS.config.update(conf); - function s3(db){ db = db || conf.s3Bucket || (conf.s3Bucket = process.env.s3Bucket); if(dev){ @@ -73,7 +72,7 @@ module.exports=require('theory') ,Key: key }; if(S3.batch(m,cb)){ console.log('no!', m.Bucket + m.Key); return } - console.log('YES!', m.Bucket + m.Key); + console.log("s3 info:", m); S3().getObject(m, function(e,r){ var d, t, r = r || (this && this.httpResponse); if(e || !r){ return S3.batch.reply(m,e) } diff --git a/gun.js b/gun.js index 86e51622..c951d8ff 100644 --- a/gun.js +++ b/gun.js @@ -18,14 +18,14 @@ module.exports = require('theory') , g, n, w = w || a.time.now(); if(gun.is(this)){ n = this; - g = n._.graph || function(){ return {} }; + g = n._.clip || function(){ return {} }; if(a.text.is(p)){ if(args >= 2){ // set var ref = {} , val = gun.at(n,p,ref); - if(!ref.node || !ref.node._ || !ref.node._[gun._.id]){ + if(!ref.cartridge || !ref.cartridge._ || !ref.cartridge._[gun._.id]){ return; - } ref.id = ref.node._[gun._.id] +'.'+ ref.path; + } ref.id = ref.cartridge._[gun._.id] +'.'+ ref.path; if(a.gun.ham && a.gun.ham.call(g,n,p,v,w,val)){ console.log("HAM REJECTION", p, v, val); return; @@ -48,7 +48,7 @@ module.exports = require('theory') delete ref.at[ref.prop]; } var del = {}; del[ref.id] = null; - theory.on(gun.event).emit(w, del, g[gun._.id]); + gun.fire(del, g[gun._.id], w); v = ref.at; } else { v = gun.at(v); @@ -65,7 +65,7 @@ module.exports = require('theory') ref.at[ref.prop] = v; } var diff = {}; diff[ref.id] = v; - theory.on(gun.event).emit(w, diff, g[gun._.id]); + gun.fire(diff, g[gun._.id], w); v = j || v; } return v; @@ -81,9 +81,9 @@ module.exports = require('theory') } n = a.obj.is(v)? v : a.obj.is(p)? p : null; p = a.text.is(p)? p : gun.id(); - if(a.obj.is(n)){ // create a new graph from this object + if(a.obj.is(n)){ // create a clip from this object g = gun.ify(n); - var graph = gun.clip[p] = function(p,v,w){ + var clip = gun.magazine[p] = function(p,v,w){ var args, id, path, n, w = w || a.time.now(); if(a.text.is(p)){ id = a.text(p).clip('.',0,1); @@ -99,7 +99,7 @@ module.exports = require('theory') } if(path){ if(n){ - n._.graph = n._.graph || graph; + n._.clip = n._.clip || clip; return gun.apply(n, args); } return; @@ -111,13 +111,13 @@ module.exports = require('theory') } if(n){ if(args.length === 1){ - n._.graph = n._.graph || graph; + n._.clip = n._.clip || clip; return fn; } if(v === null){ delete g[n._[gun._.id]]; var del = {}; del[n._[gun._.id]] = n = null; - theory.on(gun.event).emit(w, del, g[gun._.id]); + gun.fire(del, g[gun._.id], w); return null; } return; @@ -130,16 +130,16 @@ module.exports = require('theory') v = p; } if(a.obj.is(v)){ - n = gun.ify(v,u,{}); // can only add one node with this method! + n = gun.ify(v,u,{}); // a clip cannot be created from this, only a single cartridge n._ = n._ || gun.id({}); - n._.graph = graph; // JSONifying excludes functions. + n._.clip = clip; // JSONifying excludes functions. var add = {}; add[n._[gun._.id]] = g[n._[gun._.id]] = n; - theory.on(gun.event).emit(w, add, graph[gun._.id]); + gun.fire(add, clip[gun._.id], w); return fn; } } - graph[gun._.id] = p; - return graph; + clip[gun._.id] = p; + return clip; } } var u; gun._ = { @@ -164,10 +164,10 @@ module.exports = require('theory') } ref = ref || {}; var pp = a.list.is(p)? p : (p||'').split('.') - , g = a.fns.is(n._.graph)? n._.graph() : this + , g = a.fns.is(n._.clip)? n._.clip() : this , i = 0, l = pp.length, v = n , x, y, z; - ref.node = n; + ref.cartridge = n; ref.prop = pp[l-1]; ref.path = pp.slice(i).join('.'); while(i < l && v !== u){ @@ -228,7 +228,7 @@ module.exports = require('theory') } else { f[i] = n; } - opt.seen.push({node: n, prop: i, from: f, src: o}); + opt.seen.push({cartridge: n, prop: i, from: f, src: o}); a.obj(o).each(function(v,j){ ify(v,j,n,{},f); }); @@ -247,7 +247,7 @@ module.exports = require('theory') if(a.obj.is(v)){ if(seen = ify.seen(v)){ ify.be(seen); - if(gun.is(seen.node)){ t(seen.node._[gun._.id]) } + if(gun.is(seen.cartridge)){ t(seen.cartridge._[gun._.id]) } } else { gun.ify(v, opt, n); if(gun.is(n)){ @@ -270,7 +270,7 @@ module.exports = require('theory') } } ify.be = function(seen){ - var n = seen.node; + var n = seen.cartridge; n._ = n._||{}; n._[gun._.id] = n._[gun._.id]||gun.id(); g[n._[gun._.id]] = n; @@ -283,14 +283,14 @@ module.exports = require('theory') if(v && v.src === o){ return v } }) || false; } - var is = true, node = n || {}; + var is = true, cartridge = n || {}; a.obj(o).each(function(v,i){ if(!gun.is(v)){ is = false } - ify(v, i, node, {}); + ify(v, i, cartridge, {}); }); if(!is){ - ify.be({node: node}); - g[node._[gun._.id]] = node; + ify.be({cartridge: cartridge}); + g[cartridge._[gun._.id]] = cartridge; } if(n){ return n; @@ -337,8 +337,21 @@ module.exports = require('theory') } return false; } + gun.fire = function(bullet,c,w,op){ + bullet = bullet.what? bullet : {what: bullet}; + bullet.where = c || bullet.where; + bullet.when = w || bullet.when; + if(!a.obj.is(bullet.what)){ return gun.fire.jam("No ammo.", bullet) } + if(!a.num.is(bullet.when)){ return gun.fire.jam("No time.", bullet) } + if(!a.text.is(bullet.where)){ return gun.fire.jam("No location.", bullet) } + bullet.how = bullet.how || {}; + bullet.how.gun = op || 1; + theory.on(gun.event).emit(bullet); + } + gun.fire.jam = function(s,b){ if(b){ return console.log("Gun jam:",s,b) } console.log("Gun jam:",s) } + gun.shots = function(hear,s){ return theory.on(gun.event+(s?'.'+s:'')).event(hear) } gun.event = 'gun'; - gun.clip = {}; + gun.magazine = {}; return gun; })(); /* Hypothetical Amnesia Machine diff --git a/init.js b/init.js index 94f8a09e..9ceb9b2d 100644 --- a/init.js +++ b/init.js @@ -1,17 +1,12 @@ (function(){ process.env.rootdir = __dirname; -var LIVE = process.env.LIVE || (process.env.NODE_ENV === 'production') -, web, opt = {} -opt.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || 8888; -opt.host = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0'; - - -opt.hook = { - pre: (function(req,res){ - //console.log("--------- "+req.flow+" : "+req.url.pathname +" ---------------"); - }) -}; +var LIVE = process.env.LIVE || (process.env.NODE_ENV === 'production'), web, opt = {}; +opt.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT; +opt.host = process.env.OPENSHIFT_NODEJS_IP; +if(opt.port || opt.host){ LIVE = true } +opt.port = opt.port || 8888; +opt.host = opt.host || '0.0.0.0'; if(LIVE){ //process.env['redis-install'] = '/tmp'; diff --git a/shot.js b/shot.js index ce8bcae8..753d6287 100644 --- a/shot.js +++ b/shot.js @@ -6,6 +6,7 @@ module.exports = require('theory') opt.batch = opt.batch || 0; opt.throttle = opt.throttle || 0; opt.src = opt.src || (this && this.com) || ''; + opt.cache = a.num.is(opt.cache)? opt.cache : opt.cache || 1; if(root.node){ return require(opt.path+'/shots')(opt); } var u, shot = {}, store = window.amplify && window.amplify.store? amplify.store @@ -13,51 +14,14 @@ module.exports = require('theory') if(data === u){ return store[src] } return store[src] = data; } - store.reply = {}; - shot.meta = function(m, w, g){ - if(!m){ return } - if(m.when && m.where){ return m } - if(!w || !g){ return } - m.when = w; - m.where = g; - m.how = {gun:1}; - return m; - } - theory.on(a.gun.event).event(shot.fire = function(w, m, g){ - if(m){ - m = shot.meta(m, w, g); - } - if(!m){ return } - if(w && g){ - if(shot.add(m, g, shot.batch) === 2){ - return; - } - } - if(opt.src && opt.src.send){ - if(!shot.meta(m)){ return } - console.log("to server!", m); - return opt.src.send(m); - } // below is fallback. TODO: Unfinished! - if(shot.lock){ return } - var now = a.time.now(); - if(now - shot.last < opt.throttle // this entire if satement is probably wrong. Redo entirely. - && shot.batch.length < opt.batch){ return } - console.log('sending!', shot.batch); - $.post(opt.src, {b:a.text.ify(shot.batch)}, function(e,r){ - console.log('reply', e,r); - shot.del(m, g); - }); - shot.last = w; - shot.batch = []; - }); - shot.batch = []; - shot.last = a.time.now(); - shot.list = function(){ + store.batch = []; + store.last = a.time.now(); + store.list = function(){ var g = store(a.gun.event) || {} , z = function(l,g,i){ if(i !== 0 && !i){ return } if(!l || !l[i]){ return } - shot.fire(null, l[i]); + shot.fire(l[i], 1); console.log("re-sent", l[i]); a.time.wait(function(){ z(l,g,i+1) },1); } @@ -65,13 +29,21 @@ module.exports = require('theory') z(l,g,0); }); } - shot.add = function(m, g, b){ + store.sort = function(A,B){ + if(!A || !B){ return 0 } + A = ((A||{})._||{})[a.gun._.ham]; B = ((B||{})._||{})[a.gun._.ham]; + if(A < B){ return -1 } + else if(A > B){ return 1 } + else { return 0 } + } + store.add = function(m, g, b){ if(!m){ return } + g = '_' + (a(g,'at') || g || a(m,'where.at') || m.where); var gs = store(a.gun.event) || {} - , when = m.when || a(m,'_.'+a.gun._.ham) || a(m,'what._.'+a.gun._.ham); + , when = shot.when(m); gs[g] = gs[g] || []; if(a.list(gs[g]).each(function(v){ - var w = v.when || a(v,'_.'+a.gun._.ham) || a(v,'what._.'+a.gun._.ham); + var w = shot.when(v); if(w === when){ return true; } @@ -81,15 +53,15 @@ module.exports = require('theory') store(a.gun.event, gs); return gs[g]; } - shot.del = function(m, g){ + store.del = function(m, g){ if(!m){ return } var gs = store(a.gun.event) || {} - , when = m.when || a(m,'_.'+a.gun._.ham) || a(m,'what._.'+a.gun._.ham); - g = m.where.at || m.where; + , when = shot.when(m); + g = '_'+(m.where.at || m.where || g); console.log("clear queue", g, m); gs[g] = gs[g] || []; gs[g] = a.list(gs[g]).each(function(v,i,t){ - var w = v.when || a(v,'_.'+a.gun._.ham) || a(v,'what._.'+a.gun._.ham); + var w = shot.when(v); if(w === when){ return; } @@ -97,20 +69,57 @@ module.exports = require('theory') }); store(a.gun.event, gs); } - shot.sort = function(A,B){ - if(!A || !B){ return 0 } - A = ((A||{})._||{})[a.gun._.ham]; B = ((B||{})._||{})[a.gun._.ham]; - if(A < B){ return -1 } - else if(A > B){ return 1 } - else { return 0 } + store.set = function(key, val){ + var s = store(a.gun.event) || {}; + s[key] = val; + store(a.gun.event, s); } + store.get = function(key, cb){ + var s = store(a.gun.event) || {}; + s = s[key]; + if(cb){ + return cb(null, s); + } + return s; + }; + a.gun.shots(shot.fire = function(m, r){ + if(!m || !m.where){ return } + if(!r){ + if(store.add(m, m.where, store.batch) === 2){ + return; + } + } + if(opt.src && opt.src.send){ + m = opt.src.meta(m); + if(!m.what || !m.where || !m.when){ return } + console.log("to server!", m); + return opt.src.send(m); + } + return; // below should be a fallback. TODO: Unfinished! + }); + shot.when = function(m){ return a(m,'what._.'+a.gun._.ham) || a(m,'_.'+a.gun._.ham) || m.when } shot.load = function(where,cb,o){ if(!where){ return } o = o || {}; - var m = {what: where, how: {gun:3}}; + var m = {what: where, how: {gun:3}} + , g = a.gun.magazine[where] || store.get(where); + g = a.fns.is(g)? g : a.obj.is(g)? a.gun(where, g) : null; + if(g){ + //cb(g); + } + //console.log("!!! ASK !!!"); if(opt.src && opt.src.ask){ opt.src.ask(m,function(m){ + if(g){ + //console.log("!!! double load !!!"); + //return; // prevent load from calling twice! Add sync comparison. + } if(!m || !m.what){ cb(null) } + if(o.cache !== 0){ + if(o.cache || opt.cache){ // make options more configurable. + store.set(where, m.what); + } + } m = a.gun(where, m.what); cb(m); }); @@ -130,21 +139,10 @@ module.exports = require('theory') shot.spray.action = function(m){ if(!m || !m.how || !m.how.gun){ return } if(m.how.gun === -1){ - shot.del(m); + store.del(m); } } - shot.list(); - return {gun: a.gun - ,spray: shot.spray - ,load: shot.load - ,fire: shot.fire - ,wait: function(){ - shot.lock = 1; - } - ,go: function(){ - shot.last = shot.lock = 0; - shot.fire(); - } - }; + store.list(); + return shot; } },['./gun']) \ No newline at end of file diff --git a/shots.js b/shots.js index 6961af86..70c2e803 100644 --- a/shots.js +++ b/shots.js @@ -3,6 +3,7 @@ module.exports = require('theory') var s3 = require(__dirname+'/gate/s3') , store = require(__dirname+'/gate/redis'); return function(opt){ + console.log("***** SHOTS *****"); opt = opt || {}; var u, shot = {}; opt.path = opt.path || '.' @@ -30,17 +31,23 @@ module.exports = require('theory') } } opt.s3 = opt.s3 || {}; - opt.s3.Bucket = a.text.is(opt.s3.bucket)? opt.s3.bucket : ''; + opt.s3.Bucket = a.text.is(opt.s3.bucket)? opt.s3.bucket : (process.env.s3Bucket || ''); opt.s3.bucket = a.fns.is(opt.s3.bucket)? opt.s3.bucket : function(key){ return opt.s3.Bucket || a.text(key).clip('/',0,1); } opt.s3.key = a.fns.is(opt.s3.key)? opt.s3.key : function(key){ - if(opt.s3.Bucket){ return key } - return a.text(key).clip('/',1); + if(key.slice(0, opt.s3.Bucket.length) === opt.s3.Bucket){ + key = key.slice(opt.s3.Bucket.length)||''; + if(key.charAt(0) === '/'){ + key = key.slice(1); + } + } + return key; } store.batch = []; store.last = a.time.now(); store.push = function(key, score, val, cb){ + if(!val){ return } store.client.zadd(key, score, val, function(e,r){ if(e){ store.clienf.zadd(key, score, val, cb); @@ -49,10 +56,11 @@ module.exports = require('theory') if(cb){ cb(e,r) } }); } + store.when = function(m){ return a(m,'what._.'+a.gun._.ham) || a(m,'_.'+a.gun._.ham) || m.when } store.add = function(m, g){ if(!m){ return } g = '_' + (g || a(m,'where.at') || m.where); - store.push(g, (m.when || a(m,'what._.'+a.gun._.ham) || 0), a.text.ify(m)); + store.push(g, store.when(m) || 0, a.text.ify(m)); } store.del = function(m, g){ @@ -86,12 +94,13 @@ module.exports = require('theory') store.wait = null; a.obj(store.batch).each(function(g,where){ if(!g || !where){ return } - console.log('*************** save', where, '*******************'); + //console.log('*************** save', where, '*******************'); s3(opt.s3.bucket(where)).put(opt.s3.key(where),g,function(e,r){ a.list(store.batched[where]).each(function(cb){ if(a.fns.is(cb)){ cb(e,r) } - console.log('*** end ***'); + console.log('*** saved ***'); }); + console.log(store.batched[where]); delete store.batched[where]; }); }); @@ -100,7 +109,7 @@ module.exports = require('theory') opt.redis.config(); var val = a.text.is(value)? value : a.text.ify(value); if(a.fns.is(fn)){ store.stay(key, value, fn) } - console.log("potential setex:", key, opt.redis.expire, val); + //console.log("potential setex:", key, opt.redis.expire, val); if(opt.redis.max){ store.client.set(key, val, function(e,r){ if(e){ @@ -126,17 +135,15 @@ module.exports = require('theory') }); } shot.load = function(where,cb,o){ - console.log("shot.load >", where); + //console.log("shot.load >", where); if(!where){ return } where = a.text.is(where)? where : (where.at || where); - console.log("shot.load?", where); if(!a.text.is(where)){ return } - if(a.fns.is(a.gun.clip[where])){ + if(a.fns.is(a.gun.magazine[where])){ console.log('via memory', where); - cb(a.gun.clip[where]); // TODO: Need to delete these at some point, too! + cb(a.gun.magazine[where]); // TODO: Need to delete these at some point, too! return; } - console.log("from external..."); store.get(where, function(e,r){ if(e || !r){ return s3(opt.s3.bucket(where)).get(opt.s3.key(where),function(e,r,t){ @@ -154,6 +161,7 @@ module.exports = require('theory') }); } shot.spray = function(m){ + console.log(">> shot.spray"); if(m && m.how){ shot.spray.action(m); return shot; @@ -166,15 +174,12 @@ module.exports = require('theory') } shot.spray.transform = function(g,m,d){if(d){d()}} shot.spray.action = function(m){ + console.log(">>> shot.spray.action"); if(!m || !m.how){ return } var where = a.text.is(m.where)? m.where : m.where.at; - console.log("spray", where, m); if(m.how.gun === 3){ - console.log("load..."); shot.load(m.what, function(g,e){ - console.log("got it!"); shot.pump.action(g, m, function(){ // receive custom edited copy here and send it down instead. - console.log("reply"); if(!opt.src || !opt.src.reply){ return } m.what = a.fns.is(g)? g() : {}; m.how.gun = -(m.how.gun||3); @@ -187,9 +192,9 @@ module.exports = require('theory') store.add(m); shot.load(where, function(g,e){ var done = function(){ - var u, s, w = m.when || 0, r = {}, cb; + var u, s, w = store.when(m) || 0, r = {}, cb; m.how.gun = -(m.how.gun||1); - g = a.fns.is(g)? g : (a.gun.clip[where] || function(){}); + g = a.fns.is(g)? g : (a.gun.magazine[where] || function(){}); a.obj(m.what).each(function(v,p){ if(g(p,v,w) === u){ r[p] = 0; // Error code @@ -243,6 +248,18 @@ module.exports = require('theory') : !a.obj.empty(a(m,'what.url.query'))? a(m,'what.url.query') : false ; }; + shot.chamber = function(){ + var index = process.env.gun_chamber; + if(!index){ + index = process.env.gun_chamber = a.text.r(12); + } + s3(opt.s3.bucket(index)).get(opt.s3.key(index),function(e,r,t){ + if(!r){ + + } + }); + } + shot.gun = a.gun; return shot; } },[__dirname+'/gun']) \ No newline at end of file diff --git a/test/shoot.html b/test/shoot.html index 95a3b1f5..6a5d5bd8 100644 --- a/test/shoot.html +++ b/test/shoot.html @@ -71,6 +71,13 @@