This commit is contained in:
theory 2014-03-25 03:51:14 -06:00
parent 61c8424e28
commit 7f11a67f78
3 changed files with 306 additions and 306 deletions

View File

@ -1,57 +1,57 @@
module.exports = (function(){
var r = {}
, fs = require('fs')
, child = require('child_process')
, check = (fs.existsSync||require('path').existsSync)
, install = 'redis-install'
, server = 'redis-server';
r.redis = require('redis');
r.client = r.redis.createClient();
r.refis = require('fakeredis');
r.clienf = r.refis.createClient();
process.env[server] = process.env[server] || '/usr/local/bin/redis-server';
r.client.on('error', function(e){
console.log("redis error", e);
if(!(/ECONNREFUSED/).test(e)){ return }
r.start();
});
r.client.set('_gun_redis_init_', r.key = Math.random().toString().slice(2));
r.start = function(){
if(process.env[install]){
if(!check(process.env[server])){
return r.deploy(r.check);
}
}
if(!check(process.env[server])){
return; // never recover
}
if(process.env.gun_redis_lock){ return }
process.env.gun_redis_lock = process.pid;
console.log('gun', process.pid, 'starting redis');
require('child_process').spawn(process.env[server]).on('exit',function(){
if(process.env.gun_redis_lock == process.pid){
process.env.gun_redis_lock = ''; // 0 and false don't work, cause they are cast to strings!
}
});
r.client.get('_gun_redis_init_', function(e,r){
console.log(">>>> BOOM <<<<", e, r);
});
}
r.deploy = function(done){
var path = process.env[install];
if(!path){ return }
if(check(process.env[server])){
done(process.env[server]);
} else {
child.exec('cd ' + path
+ ' && ' + 'curl -O http://download.redis.io/redis-stable.tar.gz'
+ ' && ' + 'tar xvzf redis-stable.tar.gz'
+ ' && ' + 'cd redis-stable'
+ ' && ' + 'make'
, function(e, r){
done(process.env[server] = path + '/redis-stable/src/redis-server');
});
}
}
return r;
module.exports = (function(){
var r = {}
, fs = require('fs')
, child = require('child_process')
, check = (fs.existsSync||require('path').existsSync)
, install = 'redis-install'
, server = 'redis-server';
r.redis = require('redis');
r.client = r.redis.createClient();
r.refis = require('fakeredis');
r.clienf = r.refis.createClient();
process.env[server] = process.env[server] || '/usr/local/bin/redis-server';
r.client.on('error', function(e){
console.log("redis error", e);
if(!(/ECONNREFUSED/).test(e)){ return }
r.start();
});
r.client.set('_gun_redis_init_', r.key = Math.random().toString().slice(2));
r.start = function(){
if(process.env[install]){
if(!check(process.env[server])){
return r.deploy(r.check);
}
}
if(!check(process.env[server])){
return; // never recover
}
if(process.env.gun_redis_lock){ return }
process.env.gun_redis_lock = process.pid;
console.log('gun', process.pid, 'starting redis');
require('child_process').spawn(process.env[server]).on('exit',function(){
if(process.env.gun_redis_lock == process.pid){
process.env.gun_redis_lock = ''; // 0 and false don't work, cause they are cast to strings!
}
});
r.client.get('_gun_redis_init_', function(e,r){
console.log(">>>> BOOM <<<<", e, r);
});
}
r.deploy = function(done){
var path = process.env[install];
if(!path){ return }
if(check(process.env[server])){
done(process.env[server]);
} else {
child.exec('cd ' + path
+ ' && ' + 'curl -O http://download.redis.io/redis-stable.tar.gz'
+ ' && ' + 'tar xvzf redis-stable.tar.gz'
+ ' && ' + 'cd redis-stable'
+ ' && ' + 'make'
, function(e, r){
done(process.env[server] = path + '/redis-stable/src/redis-server');
});
}
}
return r;
})();

View File

@ -1,134 +1,134 @@
module.exports=require('theory')
('s3', function(a){
var AWS = a['aws-sdk'], conf = {}, dev = process.env.fakes3;
AWS.config.region = conf.region = process.env.AWS_REGION || "us-east-1";
AWS.config.accessKeyId = conf.accessKeyId = process.env.AWS_ACCESS_KEY_ID || 'abc';
AWS.config.secretAccessKey = conf.secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY || 'a1b2c3';
if(dev){
AWS.config.endpoint = conf.endpoint = dev;
AWS.config.sslEnabled = conf.sslEnabled = false;
}
AWS.config.update(conf);
function s3(db){
db = db || conf.s3Bucket || (conf.s3Bucket = process.env.s3Bucket);
if(dev){
db = db.replace('.','p');
}
s3.bucket = db;
return s3;
}; var S3 = function(){
var s = new AWS.S3();
if(dev){
s.endpoint = conf.endpoint;
}
return s;
}
S3.batch = function(m, cb){
var id = S3.batch.id(m);
if(a.list.is(S3.batch.list[id])){
S3.batch.list[id].push(cb);
return true;
}
S3.batch.list[id] = [cb];
}
S3.batch.list = {};
S3.batch.id = function(m){ return m.Bucket +'/'+ m.Key }
S3.batch.reply = function(m,e,d,t,r){
var id = S3.batch.id(m);
a.list(S3.batch.list[id]).each(function(cb){
if(a.fns.is(cb)){
try{
cb(e,d,t,r);
}catch(e){}
}
});
S3.batch.list[id] = null;
delete S3.batch.list[id];
}
s3.put = function(key, o, cb, m){
if(!key) return;
var m = m || {
Bucket: s3.bucket
,Key: key
}
if(a.obj.is(o) || a.list.is(o)){
m.Body = a.text.ify(o);
m.ContentType = a.mime.lookup('json')
} else {
m.Body = a.text.ify(o);
}
S3().putObject(m, function(e,r){
//console.log('saved', e,r);
if(!cb) return;
cb(e,r);
});
return s3;
}
s3.get = function(key, cb, o){
if(!key) return;
var m = {
Bucket: s3.bucket
,Key: key
};
if(S3.batch(m,cb)){ console.log('no!', m.Bucket + m.Key); return }
console.log('YES!', m.Bucket + m.Key);
S3().getObject(m, function(e,r){
var d, t, r = r || (this && this.httpResponse);
if(e || !r){ return S3.batch.reply(m,e) }
r.Text = r.text = t = (r.Body||r.body||'').toString('utf8');
r.Type = r.type = r.ContentType || (r.headers||{})['content-type'];
if(r.type && 'json' === a.mime.extension(r.type)){
d = a.obj.ify(t);
}
S3.batch.reply(m, e, d, t, r); // Warning about the r parameter, is is the raw response and may result in stupid SAX errors.
});
return s3;
}
s3.del = function(key, cb){
if(!key) return;
var m = {
Bucket: s3.bucket
,Key: key
}
S3().deleteObject(m, function(e,r){
if(!cb) return;
cb(e, r);
});
return s3;
}
s3.dbs = function(o, cb){
cb = cb || o;
var m = {}
S3().listBuckets(m, function(e,r){
//console.log('dbs',e);
a.list((r||{}).Contents).each(function(v){console.log(v);});
//console.log('---end list---');
if(!a.fns.is(cb)) return;
cb(e,r);
});
return s3;
}
s3.keys = function(from, upto, cb){
cb = cb || upto || from;
var m = {
Bucket: s3.bucket
}
if(a.text.is(from)){
m.Prefix = from;
}
if(a.text.is(upto)){
m.Delimiter = upto;
}
S3().listObjects(m, function(e,r){
//console.log('list',e);
a.list((r||{}).Contents).each(function(v){console.log(v);});
//console.log('---end list---');
if(!a.fns.is(cb)) return;
cb(e,r);
});
return s3;
}
return s3;
module.exports=require('theory')
('s3', function(a){
var AWS = a['aws-sdk'], conf = {}, dev = process.env.fakes3;
AWS.config.region = conf.region = process.env.AWS_REGION || "us-east-1";
AWS.config.accessKeyId = conf.accessKeyId = process.env.AWS_ACCESS_KEY_ID || 'abc';
AWS.config.secretAccessKey = conf.secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY || 'a1b2c3';
if(dev){
AWS.config.endpoint = conf.endpoint = dev;
AWS.config.sslEnabled = conf.sslEnabled = false;
}
AWS.config.update(conf);
function s3(db){
db = db || conf.s3Bucket || (conf.s3Bucket = process.env.s3Bucket);
if(dev){
db = db.replace('.','p');
}
s3.bucket = db;
return s3;
}; var S3 = function(){
var s = new AWS.S3();
if(dev){
s.endpoint = conf.endpoint;
}
return s;
}
S3.batch = function(m, cb){
var id = S3.batch.id(m);
if(a.list.is(S3.batch.list[id])){
S3.batch.list[id].push(cb);
return true;
}
S3.batch.list[id] = [cb];
}
S3.batch.list = {};
S3.batch.id = function(m){ return m.Bucket +'/'+ m.Key }
S3.batch.reply = function(m,e,d,t,r){
var id = S3.batch.id(m);
a.list(S3.batch.list[id]).each(function(cb){
if(a.fns.is(cb)){
try{
cb(e,d,t,r);
}catch(e){}
}
});
S3.batch.list[id] = null;
delete S3.batch.list[id];
}
s3.put = function(key, o, cb, m){
if(!key) return;
var m = m || {
Bucket: s3.bucket
,Key: key
}
if(a.obj.is(o) || a.list.is(o)){
m.Body = a.text.ify(o);
m.ContentType = a.mime.lookup('json')
} else {
m.Body = a.text.ify(o);
}
S3().putObject(m, function(e,r){
//console.log('saved', e,r);
if(!cb) return;
cb(e,r);
});
return s3;
}
s3.get = function(key, cb, o){
if(!key) return;
var m = {
Bucket: s3.bucket
,Key: key
};
if(S3.batch(m,cb)){ console.log('no!', m.Bucket + m.Key); return }
console.log('YES!', m.Bucket + m.Key);
S3().getObject(m, function(e,r){
var d, t, r = r || (this && this.httpResponse);
if(e || !r){ return S3.batch.reply(m,e) }
r.Text = r.text = t = (r.Body||r.body||'').toString('utf8');
r.Type = r.type = r.ContentType || (r.headers||{})['content-type'];
if(r.type && 'json' === a.mime.extension(r.type)){
d = a.obj.ify(t);
}
S3.batch.reply(m, e, d, t, r); // Warning about the r parameter, is is the raw response and may result in stupid SAX errors.
});
return s3;
}
s3.del = function(key, cb){
if(!key) return;
var m = {
Bucket: s3.bucket
,Key: key
}
S3().deleteObject(m, function(e,r){
if(!cb) return;
cb(e, r);
});
return s3;
}
s3.dbs = function(o, cb){
cb = cb || o;
var m = {}
S3().listBuckets(m, function(e,r){
//console.log('dbs',e);
a.list((r||{}).Contents).each(function(v){console.log(v);});
//console.log('---end list---');
if(!a.fns.is(cb)) return;
cb(e,r);
});
return s3;
}
s3.keys = function(from, upto, cb){
cb = cb || upto || from;
var m = {
Bucket: s3.bucket
}
if(a.text.is(from)){
m.Prefix = from;
}
if(a.text.is(upto)){
m.Delimiter = upto;
}
S3().listObjects(m, function(e,r){
//console.log('list',e);
a.list((r||{}).Contents).each(function(v){console.log(v);});
//console.log('---end list---');
if(!a.fns.is(cb)) return;
cb(e,r);
});
return s3;
}
return s3;
},['aws-sdk','mime']);

View File

@ -1,118 +1,118 @@
module.exports=require('theory')
('tests',function(a){
if(root.node){
require('../../../lushly/keys');
var shot = require('../shots')({src: a.com}).pump(function(g, m, done){
done();
});
return shot.spray(function(g, m, done){
console.log('>>>>>>>>>> gun');
done();
console.log(g());
});
}
var shot = a.shot();
describe('Gun',function(){
it('ify',function(){
return;
var graph = {
asdf: {
_: {$: 'asdf'}
,name: 'Alice'
,age: 23
,knows: {$: 'zyx' }
}
,fdsa: {
_: {$: 'fdsa'}
,name: 'Bob'
,age: 22
,friends: ['asdf','xyz','zyx']
}
,xyz: {
_: {$: 'xyz'}
,name: 'Carl'
,age: 32
}
,zyx: {
_: {$: 'zyx'}
,name: 'Dave'
,age: 22
,self: {$: 'zyx'}
,eye: {
color: 'blue'
}
}
}
var g = theory.gun(graph);
var e = g({name: 'Evan', age: 19, knows: {$: 'fdsa'}});
var f = g({name: 'Fred', age: 20, knows: {$: 'asdf'}});
expect(f('knows.age')).to.eql(23);
console.log(g());
console.log('---------------------');
expect(e('knows.friends.age')).to.eql(23);
var ref = {}, val = theory.gun.at(f(),'knows.knows.eye.color',ref);
expect(val).to.eql('blue');
expect(ref.prop).to.be('color');
expect(ref.path).to.be('eye.color');
expect(ref.node._.$).to.be('zyx');
expect(ref.at).to.eql({color:'blue'});
console.log(g());
});
it('ify',function(){
return;
var g = theory.gun('hi');
var z = {here:'we',are:'again',name:'z'};
var y = {to:z,what:'up',name:'y'};
var x = {from:y,here:'go',name:'x'};
var n = g.ify(x);
var q = g.at();
var p = g.ify(q);
console.log(g(), q, p);
expect(q).to.eql(p);
});
it('tests',function(){
return;
// run before shotgun, or we'll have mixed problems?
var g = theory.gun();
var z = g({});
var y = g({});
z('name','Zach');
y('name','Yvan');
z('list',[y]);
expect(a.list.is(z('list'))).to.be.ok();
expect(z('list').length).to.be(1);
expect(z('list.name')).to.be('Yvan');
z('list',[z]);
z('from','USA');
expect(a.list.is(z('list'))).to.be.ok();
expect(z('list').length).to.be(2);
expect(z('list.from')).to.be('USA');
});
});
describe('Shot',function(){
it('tests',function(){
//return;
//var s = theory.shot({src: '/test/server.js'})
var s = shot.shell('lushly.org/tests/package.json',function(g){
if(!g){ return }
console.log('graph', g());
var p = g('packageson');
var val = p('version');
console.log(val);
val = a.num.is(val)? (val + 1) : 0;
console.log(val);
p('version',val);
a.time.wait(function(){
shot.shell('lushly.org/tests/package.json',function(gg){
var pp = gg('packageson');
console.log('graph prime', gg());
expect(val).to.be(pp('version'));
console.log(pp('version'));
});
},1000);
});
});
});
a.time.wait(mocha.run,50);
return shot.spray;
module.exports=require('theory')
('tests',function(a){
if(root.node){
require('../../../lushly/keys');
var shot = require('../shots')({src: a.com}).pump(function(g, m, done){
done();
});
return shot.spray(function(g, m, done){
console.log('>>>>>>>>>> gun');
done();
console.log(g());
});
}
var shot = a.shot();
describe('Gun',function(){
it('ify',function(){
return;
var graph = {
asdf: {
_: {$: 'asdf'}
,name: 'Alice'
,age: 23
,knows: {$: 'zyx' }
}
,fdsa: {
_: {$: 'fdsa'}
,name: 'Bob'
,age: 22
,friends: ['asdf','xyz','zyx']
}
,xyz: {
_: {$: 'xyz'}
,name: 'Carl'
,age: 32
}
,zyx: {
_: {$: 'zyx'}
,name: 'Dave'
,age: 22
,self: {$: 'zyx'}
,eye: {
color: 'blue'
}
}
}
var g = theory.gun(graph);
var e = g({name: 'Evan', age: 19, knows: {$: 'fdsa'}});
var f = g({name: 'Fred', age: 20, knows: {$: 'asdf'}});
expect(f('knows.age')).to.eql(23);
console.log(g());
console.log('---------------------');
expect(e('knows.friends.age')).to.eql(23);
var ref = {}, val = theory.gun.at(f(),'knows.knows.eye.color',ref);
expect(val).to.eql('blue');
expect(ref.prop).to.be('color');
expect(ref.path).to.be('eye.color');
expect(ref.node._.$).to.be('zyx');
expect(ref.at).to.eql({color:'blue'});
console.log(g());
});
it('ify',function(){
return;
var g = theory.gun('hi');
var z = {here:'we',are:'again',name:'z'};
var y = {to:z,what:'up',name:'y'};
var x = {from:y,here:'go',name:'x'};
var n = g.ify(x);
var q = g.at();
var p = g.ify(q);
console.log(g(), q, p);
expect(q).to.eql(p);
});
it('tests',function(){
return;
// run before shotgun, or we'll have mixed problems?
var g = theory.gun();
var z = g({});
var y = g({});
z('name','Zach');
y('name','Yvan');
z('list',[y]);
expect(a.list.is(z('list'))).to.be.ok();
expect(z('list').length).to.be(1);
expect(z('list.name')).to.be('Yvan');
z('list',[z]);
z('from','USA');
expect(a.list.is(z('list'))).to.be.ok();
expect(z('list').length).to.be(2);
expect(z('list.from')).to.be('USA');
});
});
describe('Shot',function(){
it('tests',function(){
//return;
//var s = theory.shot({src: '/test/server.js'})
var s = shot.shell('lushly.org/tests/package.json',function(g){
if(!g){ return }
console.log('graph', g());
var p = g('packageson');
var val = p('version');
console.log(val);
val = a.num.is(val)? (val + 1) : 0;
console.log(val);
p('version',val);
a.time.wait(function(){
shot.shell('lushly.org/tests/package.json',function(gg){
var pp = gg('packageson');
console.log('graph prime', gg());
expect(val).to.be(pp('version'));
console.log(pp('version'));
});
},1000);
});
});
});
a.time.wait(mocha.run,50);
return shot.spray;
},['../shot']);