Merge pull request #196 from amark/develop

Develop
This commit is contained in:
Mark Nadal 2016-06-22 18:04:53 -06:00 committed by GitHub
commit 9863840284
5 changed files with 91 additions and 42 deletions

30
examples/express-auth.js Normal file
View File

@ -0,0 +1,30 @@
console.log("If modules not found, run `npm install` in /example folder!"); // git subtree push -P examples heroku master // OR // git subtree split -P examples master && git push heroku [['HASH']]:master --force
var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 80;
var express = require('express');
var app = express();
var Gun = require('gun');
var gun = Gun({
file: 'data.json',
s3: {
key: '', // AWS Access Key
secret: '', // AWS Secret Token
bucket: '' // The bucket you want to save into
}
});
gun.wsp(app/*, function(req, res, next){
console.log("auth!", req, req.body['#']);
if('get' === req.method){
if('example/todo/data' === req.body['#']){
next(req, res);
}
}
if('put' === req.method){
res({body: {err: "Permission denied!"}});
}
}*/);
app.use(express.static(__dirname)).listen(port);
console.log('Server started on port ' + port + ' with /gun');

33
gun.js
View File

@ -1173,7 +1173,8 @@
var soul = lex[Gun._.soul];
if(!soul){ return }
cb = cb || function(){};
(opt.headers = Gun.obj.copy(tab.headers)).id = tab.msg();
var ropt = {};
(ropt.headers = Gun.obj.copy(tab.headers)).id = tab.msg();
(function local(soul, cb){
tab.store.get(tab.prefix + soul, function(err, data){
if(!data){ return } // let the peers handle no data.
@ -1184,11 +1185,11 @@
});
}(soul, cb));
if(!(cb.local = opt.local)){
tab.request.s[opt.headers.id] = tab.error(cb, "Error: Get failed!", function(reply){
tab.request.s[ropt.headers.id] = tab.error(cb, "Error: Get failed!", function(reply){
setTimeout(function(){ tab.put(Gun.is.graph.ify(reply.body), function(){}, {local: true, peers: {}}) },1); // and flush the in memory nodes of this graph to localStorage after we've had a chance to union on it.
});
Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){ var p = {};
tab.request(url, lex, tab.request.s[opt.headers.id], opt);
tab.request(url, lex, tab.request.s[ropt.headers.id], ropt);
cb.peers = true;
});
var node = gun.__.graph[soul];
@ -1198,18 +1199,18 @@
} tab.peers(cb);
}
tab.put = tab.put || function(graph, cb, opt){
//console.log("SAVE", graph);
cb = cb || function(){};
opt = opt || {};
(opt.headers = Gun.obj.copy(tab.headers)).id = tab.msg();
var ropt = {};
(ropt.headers = Gun.obj.copy(tab.headers)).id = tab.msg();
Gun.is.graph(graph, function(node, soul){
if(!gun.__.graph[soul]){ return }
tab.store.put(tab.prefix + soul, gun.__.graph[soul], function(err){if(err){ cb({err: err}) }});
});
if(!(cb.local = opt.local)){
tab.request.s[opt.headers.id] = tab.error(cb, "Error: Put failed!");
tab.request.s[ropt.headers.id] = tab.error(cb, "Error: Put failed!");
Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){
tab.request(url, graph, tab.request.s[opt.headers.id], opt);
tab.request(url, graph, tab.request.s[ropt.headers.id], ropt);
cb.peers = true;
});
} tab.peers(cb);
@ -1287,16 +1288,21 @@
gun.__.opt.wire.get = gun.__.opt.wire.get || tab.get;
gun.__.opt.wire.put = gun.__.opt.wire.put || tab.put;
gun.__.opt.wire.key = gun.__.opt.wire.key || tab.key;
Tab.request = request;
Gun.Tab = Tab;
});
var request = (function(){
function r(base, body, cb, opt){
opt = opt || (base.length? {base: base} : base);
opt.base = opt.base || base;
opt.body = opt.body || body;
var o = base.length? {base: base} : {};
o.base = opt.base || base;
o.body = opt.body || body;
o.headers = opt.headers;
o.url = opt.url;
cb = cb || function(){};
if(!opt.base){ return }
r.transport(opt, cb);
if(!o.base){ return }
r.transport(o, cb);
}
r.createServer = function(fn){ r.createServer.s.push(fn) }
r.createServer.ing = function(req, cb){
@ -1328,7 +1334,8 @@
return true;
}
if(ws === false){ return }
ws = r.ws.peers[opt.base] = new WS(opt.base.replace('http','ws'));
try{ws = r.ws.peers[opt.base] = new WS(opt.base.replace('http','ws'));
}catch(e){}
ws.onopen = function(o){ r.back = 2; r.ws(opt, cb) };
ws.onclose = window.onbeforeunload = function(c){
if(!c){ return }

View File

@ -23,7 +23,8 @@
});
gun.__.opt.ws.port = gun.__.opt.ws.port || opt.ws.port || port || 80;
}
var wsp = gun.wsp = gun.wsp || function(server){
var wsp = gun.wsp = gun.wsp || function(server, auth){
gun.wsp.auth = auth;
if(!server){ return gun }
if(Gun.fns.is(server.address)){
if(server.address()){
@ -112,15 +113,24 @@
// all streams, technically PATCH but implemented as PUT or POST, are forwarded to other trusted peers
// except for the ones that are listed in the message as having already been sending to.
// all states, implemented with GET, are replied to the source that asked for it.
function tran(req, res){
if(!req || !res || !req.body || !req.headers || !req.headers.id){ return }
if(gun.wsp.msg(req.headers.id)){ return }
req.method = req.body? 'put' : 'get';
function flow(req, res){
gun.wsp.on('network').emit(Gun.obj.copy(req));
if(req.headers.rid){ return } // no need to process.
if(Gun.is.lex(req.body)){ return tran.get(req, res) }
else { return tran.put(req, res) }
cb({body: {hello: 'world'}});
}
function tran(req, res){
if(!req || !res || !req.body || !req.headers || !req.headers.id){ return }
if(gun.wsp.msg(req.headers.id)){ return }
req.method = (req.body && !Gun.is.lex(req.body))? 'put' : 'get';
if(gun.wsp.auth){ return gun.wsp.auth(req, function(reply){
if(!reply.headers){ reply.headers = {} }
if(!reply.headers['Content-Type']){ reply.headers['Content-Type'] = tran.json }
if(!reply.rid){ reply.headers.rid = req.headers.id }
if(!reply.id){ reply.headers.id = gun.wsp.msg() }
res(reply);
}, flow) }
else { return flow(req, res) }
}
tran.get = function(req, cb){
var key = req.url.key

View File

@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.3.92",
"version": "0.3.93",
"description": "Graph engine",
"main": "index.js",
"scripts": {

View File

@ -3799,7 +3799,7 @@ describe('Gun', function(){
});
it("get context", function(done){ // TODO: HUH?????? This was randomly causing errors?
var gun = Gun();
var gun = Gun();
var ref = gun.get('ctx/lol').get('ctx/foo').put({hello: 'world'});
gun.get('ctx/lol').val(function(implicit){
done.fail = true;
@ -3975,29 +3975,31 @@ describe('Gun', function(){
it("Don't put on parents", function(done){ // TODO: ADD TO 0.5 BRANCH! // Another Stefdv find.
var test = gun.get('test');
test.path('try.this.at.lvl4').put({msg:'hoi'})
test.val(function(node,b){
delete node._;
expect(Gun.obj.empty(node, 'try')).to.be.ok();
node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.try)]);
test.path('try.this.at.lvl4').put({msg:'hoi'});
setTimeout(function(){ // TODO: Is this cheating??
test.val(function(node,b){
delete node._;
expect(Gun.obj.empty(node, 'try')).to.be.ok();
node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.try)]);
delete node._;
expect(Gun.obj.empty(node, 'this')).to.be.ok();
node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.this)]);
delete node._;
expect(Gun.obj.empty(node, 'this')).to.be.ok();
node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.this)]);
delete node._;
expect(Gun.obj.empty(node, 'at')).to.be.ok();
node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.at)]);
delete node._;
expect(Gun.obj.empty(node, 'at')).to.be.ok();
node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.at)]);
delete node._;
expect(Gun.obj.empty(node, 'lvl4')).to.be.ok();
node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.lvl4)]);
delete node._;
expect(Gun.obj.empty(node, 'lvl4')).to.be.ok();
node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.lvl4)]);
delete node._;
expect(Gun.obj.empty(node, 'msg')).to.be.ok();
expect(node.msg).to.be('hoi');
done();
});
delete node._;
expect(Gun.obj.empty(node, 'msg')).to.be.ok();
expect(node.msg).to.be('hoi');
done();
});
},100);
});
});