may need to revert some gun.js changes

This commit is contained in:
Mark Nadal 2018-01-08 02:59:02 -08:00
parent ca6eecffa5
commit 6d1d3bfb9f
5 changed files with 135 additions and 39 deletions

93
gun.js
View File

@ -171,7 +171,10 @@
if(!tag){ return {to: onto} }
var tag = (this.tag || (this.tag = {}))[tag] ||
(this.tag[tag] = {tag: tag, to: onto._ = {
next: function(){}
next: function(arg){ var tmp;
if((tmp = this.to) && tmp.next !== this.next){
tmp.next(arg);
}}
}});
if(arg instanceof Function){
var be = {
@ -712,11 +715,11 @@
;(function(){
Gun.on.put = function(msg, gun){
var at = gun._, ctx = {gun: gun, graph: at.graph, put: {}, map: {}, machine: Gun.state(), ack: msg['@']};
var at = gun._, ctx = {gun: gun, graph: at.graph, put: {}, map: {}, souls: {}, machine: Gun.state(), ack: msg['@']};
if(!Gun.graph.is(msg.put, null, verify, ctx)){ ctx.err = "Error: Invalid graph!" }
if(ctx.err){ return at.on('in', {'@': msg['#'], err: Gun.log(ctx.err) }) }
obj_map(ctx.put, merge, ctx);
obj_map(ctx.map, map, ctx);
if(!ctx.async){ obj_map(ctx.map, map, ctx) }
if(u !== ctx.defer){
setTimeout(function(){
Gun.on.put(msg, gun);
@ -738,32 +741,52 @@
}
ctx.put[soul] = Gun.state.to(node, key, ctx.put[soul]);
(ctx.diff || (ctx.diff = {}))[soul] = Gun.state.to(node, key, ctx.diff[soul]);
ctx.souls[soul] = true;
}
function merge(node, soul){
var cat = this.gun._, at = (cat.next || empty)[soul];
var ctx = this, cat = ctx.gun._, at = (cat.next || empty)[soul];
if(!at){ return }
var msg = this.map[soul] = {
put: this.node = node,
get: this.soul = soul,
gun: this.at = at
};
if(this.ack){ msg['@'] = this.ack }
obj_map(node, each, this);
var msg = ctx.map[soul] = {
put: node,
get: soul,
gun: at
}, as = {ctx: ctx, msg: msg};
ctx.async = !!cat.tag.node;
if(ctx.ack){ msg['@'] = ctx.ack }
obj_map(node, each, as);
if(!ctx.async){ return }
if(!ctx.and){
cat.on('node', function(m){
this.to.next(m);
if(m !== ctx.map[m.get]){ return }
ctx.souls[m.get] = false;
obj_map(m.put, aeach, m);
if(obj_map(ctx.souls, function(v){ if(v){ return v } })){ return }
if(ctx.c){ return } ctx.c = 1;
this.off();
obj_map(ctx.map, map, ctx);
});
}
ctx.and = true;
cat.on('node', msg);
}
function each(val, key){
var graph = this.graph, soul = this.soul, at = (this.at._), tmp;
graph[soul] = Gun.state.to(this.node, key, graph[soul]);
at.put = Gun.state.to(this.node, key, at.put);
var ctx = this.ctx, graph = ctx.graph, msg = this.msg, soul = msg.get, node = msg.put, at = (msg.gun._), tmp;
graph[soul] = Gun.state.to(node, key, graph[soul]);
if(ctx.async){ return }
//console.log("::::", at.put[key]);
at.put = Gun.state.to(node, key, at.put);
}
function map(msg, soul){ var tmp;
function aeach(val, key){
var msg = this, node = msg.put, at = (msg.gun._);
//console.log("::", at.put[key]);
at.put = Gun.state.to(node, key, at.put);
}
function map(msg, soul){
if(!msg.gun){ return }
if((tmp = this.gun._).tag.node){
tmp.on('node', function(msg){ this.off();
(msg.gun._).on('in', msg);
}).on.on('node', msg);
return;
}
msg.gun._.stop = {};
(msg.gun._).on('in', msg);
msg.gun._.stop = {};
}
Gun.on.get = function(msg, gun){
@ -831,6 +854,19 @@
if(typeof window !== "undefined"){ window.Gun = Gun }
if(typeof common !== "undefined"){ common.exports = Gun }
module.exports = Gun;
/*Gun.on('opt', function(ctx){
this.to.next(ctx);
if(ctx.once){ return }
ctx.on('node', function(msg){
var to = this.to;
//console.log(">>>>");
setTimeout(function(){
//console.log("<<<<", msg.put);
to.next(msg);
},1);
})
});*/
})(require, './root');
;require(function(module){
@ -1034,10 +1070,11 @@
}
tmp = (at.map || (at.map = {}))[from.id] = at.map[from.id] || {at: from};
var now = at.root._.now;
now = now || at.root._.stop;
if(rel === tmp.rel){
if(!now){ return }
if(u === now[at.id]){ return }
if((now._ || (now._ = {}))[at.id]){ return } now._[at.id] = true;
if((now._ || (now._ = {}))[at.id] === rel){ return } now._[at.id] = rel;
}
ask(at, tmp.rel = rel);
}
@ -1117,7 +1154,7 @@
function ack(msg, ev){
var as = this.as, get = as.get || empty, at = as.gun._;
if(at.ack){ at.ack = (at.ack + 1) || 1 }
if(!msg.put || node_ == get['.'] || (get['.'] && !obj_has(msg.put[get['#']], at.get))){
if(!msg.put /*|| node_ == get['.']*/ || (get['.'] && !obj_has(msg.put[get['#']], at.get))){
if(at.put !== u){ return }
//at.ack = 0;
at.on('in', {
@ -1380,6 +1417,7 @@
function any(at, ev){
var as = this.as;
//console.log('any', at);
if(!at.gun || !at.gun._){ return } // TODO: Handle
if(at.err){ // TODO: Handle
console.log("Please report this as an issue! Put.any.err");
@ -1604,6 +1642,7 @@
Gun.log.once("mapfn", "Map functions are experimental, their behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it.");
chain = gun.chain();
gun.map().on(function(data, key, at, ev){
//console.log("** MAP FN ** MAP FN **", key, data);
var next = (cb||noop).call(this, data, key, at, ev);
if(u === next){ return }
if(Gun.is(next)){
@ -1636,9 +1675,11 @@
opt = opt || {}; opt.item = opt.item || item;
if(soul = Gun.node.soul(item)){ return gun.set(gun.back(-1).get(soul), cb, opt) }
if(!Gun.is(item)){
var id = gun._.root._.opt.uuid() || (Gun.state.lex() + Gun.text.random(12));
if(Gun.obj.is(item)){ return gun.set(gun._.root.put(item, id), cb, opt) }
return gun.get(id).put(item);
var id = gun._.root._.opt.uuid();
if(id && Gun.obj.is(item)){
return gun.set(gun._.root.put(item, id), cb, opt);
}
return gun.get(id || (Gun.state.lex() + Gun.text.random(12))).put(item, cb, opt);
}
item.get('_').get(function(at, ev){
if(!at.gun || !at.gun._.back){ return }

View File

@ -33,6 +33,7 @@ Gun.on('opt', function mount(ctx){
ctx.on('bye', peer);
Gun.obj.del(opt.peers, wire.id);
});
wire.on('error', function(e){});
});
ctx.on('out', function(at){

View File

@ -49,7 +49,7 @@
},
"dependencies": {
"aws-sdk": ">=2.153.0",
"ws": "~>3.3.2"
"ws": "~>4.0.0"
},
"devDependencies": {
"spark-md5": "^3.0.0",

25
sea.js
View File

@ -166,7 +166,10 @@
.catch(function(e){ reject({err: 'Failed to create proof!'}) })
.then(function(proof){
var user = {pub: pub, proof: proof, at: at};
// the proof of work is evidence that we've spent some time/effort trying to log in, this slows brute force.
// the proof of work is evidence that we've spent some time/effort trying to log in, this slows brute force.
/*
MARK TO @mhelander : pub vs epub!???
*/
SEA.dec(auth.auth, {pub: pub, key: proof})
.catch(function(e){ reject({err: 'Failed to decrypt secret!'}) })
.then(function(sea){
@ -209,7 +212,8 @@
// persist authentication
return authpersist(user._, key.proof, opts).then(function(){
// emit an auth event, useful for page redirects and stuff.
root._.on('auth', user._);
try{root._.on('auth', user._);
}catch(e){console.log("Your 'auth' callback crashed with:", e)}
// returns success with the user data credentials.
return user._;
});
@ -747,14 +751,19 @@
function each(msg){ // TODO: Warning: Need to switch to `gun.on('node')`! Do not use `Gun.on('node'` in your apps!
// NOTE: THE SECURITY FUNCTION HAS ALREADY VERIFIED THE DATA!!!
// WE DO NOT NEED TO RE-VERIFY AGAIN, JUST TRANSFORM IT TO PLAINTEXT.
var to = this.to, vertex = (msg.gun._).put, c = 0;
var to = this.to, vertex = (msg.gun._).put, c = 0, d;
console.debug(7, 'seach', Gun.obj.copy(msg.put));
Gun.node.is(msg.put, function(val, key, node){ c++; // for each property on the node
console.debug(8, 'seach it', key, val);
SEA.read(val, false).then(function(data){ c--; // false just extracts the plain data.
console.log(10, 'seach got', key, data, c, vertex, node, '?????', msg);
vertex[key] = node[key] = val = data; // transform to plain value.
if(!c && (c = -1)){ to.next(msg) }
if(d && !c && (c = -1)){ to.next(msg) }
});
console.log("-----", key, "-----", val);
});
if(!c){ to.next(msg) }
d = true;
if(d && !c){ to.next(msg) }
return;
/*var to = this.to, ctx = this.as;
var own = ctx.sea.own, soul = msg.get, c = 0;
@ -906,7 +915,7 @@
return;
}
if(!(tmp = soul.split('~')) || 2 !== tmp.length){
each.end({err: "Soul is not signed at '" + key + "'."})
each.end({err: "Soul is not signed at '" + key + "'."});
return;
}
check['any'+soul+key] = 1;
@ -923,13 +932,14 @@
each.end = function(ctx){ // TODO: Can't you just switch this to each.end = cb?
if(each.err){ return }
if((each.err = ctx.err) || ctx.no){
console.log('NO!', each.err);
console.log('NO!', each.err, msg.put);
return;
}
if(!each.end.ed){ return }
if(Gun.obj.map(check, function(no){
if(no){ return true }
})){ return }
console.debug(2, 'security', msg);
to.next(msg);
};
Gun.obj.map(msg.put, each.node);
@ -1168,6 +1178,7 @@
m = m || '';
d = m[0];
try{ d = d.slice ? JSON.parse(d) : d }catch(e){}
console.debug(9, 'SEAread', p, m, d);
if(false === p){ resolve(d) }
SEA.verify(m[0], p, m[1]).then(function(ok){
if(!ok){ return resolve() }

View File

@ -3025,7 +3025,7 @@ describe('Gun', function(){
gun.map().val(function(v,f){
check[f] = v;
count[f] = (count[f] || 0) + 1;
// console.log("**************", f, v);
//console.log("**************", f, v);
if(check['1-1'] && check['2-2']){
clearTimeout(done.to);
done.to = setTimeout(function(){
@ -3038,6 +3038,7 @@ describe('Gun', function(){
}
});
setTimeout(function(){
//console.debug.i=1;console.log("-----------------");
gun.get('1-1').put({what: "hi"});
setTimeout(function(){
gun.get('2-2').put({what: "you."});
@ -3343,6 +3344,45 @@ describe('Gun', function(){
},400);
});
it('multiple times map', function(done){
var gun = Gun();
gun.get('usersMM').put({
'mark': {
fdsa: {
pub: 'fdsa',
name: "mark"
}
},
'amber': {
asdf: {
pub: 'asdf',
name: "amber"
}
}
});
var check = {A: {}, B: {}};
setTimeout(function(){
gun.get('usersMM').map().map().val(function(data){
//console.log('A', data);
check.A[data.pub] = true;
})
}, 900);
setTimeout(function(){
gun.get('usersMM').map().map().val(function(data){
//console.log('B', data, check);
check.B[data.pub] = true;
if(check.A['asdf'] && check.A['fdsa'] && check.B['asdf'] && check.B['fdsa']){
if(done.c){ return } done.c = 1;
done();
}
})
}, 1200);
});
it('multiple times', function(done){
var gun = Gun();
var app = gun.get('mult/times');
@ -3434,16 +3474,19 @@ describe('Gun', function(){
var list = app.get('list');
var check = {};
list.map(function(user){ return user.age === 27? user.name + "thezombie" : u }).on(function(data){
//console.log('data:', data);
list.map(function(user){ /*console.log("****", user);*/ return user.age === 27? user.name + "thezombie" : u }).on(function(data){
//console.log('+++++', data);
check[data] = true;
if(check.alicethezombie && check.bobthezombie){
if(done.c){return}done.c=1;
done();
}
});
//console.debug.i=1;console.log("----------------");
list.set({name: 'alice', age: 27}); // on put, table-scan flag doesn't get set, but is needed for initial!??
list.set({name: 'bob', age: 27});
//console.log("vvvvvvvvvvv");
//return;
list.set({name: 'carl', age: 29});
list.set({name: 'dave', age: 25});
});
@ -3925,11 +3968,11 @@ describe('Gun', function(){
Gun.on('put', {gun: gun, put: Gun.graph.ify({
here: "we go"
}, s)});
console.debug.i=1;console.log("---------------");
//console.debug.i=1;console.log("---------------");
gun.get('get/put/any')
.put({})
.any(function(err, data, field, at, ev){
console.log("***** 1", data);
//console.log("***** 1", data);
done();
});
});