important offline fixes & assumption/default changes

This commit is contained in:
Mark Nadal 2020-04-05 17:41:51 -07:00
parent ec8808735b
commit fc58d3552a
3 changed files with 156 additions and 24 deletions

46
gun.js
View File

@ -50,6 +50,17 @@
if(u !== o['<'] && t <= o['<']){ return true }
return false;
}
Type.text.hash = function(s, c){ // via SO
if(typeof s !== 'string'){ return }
c = c || 0;
if(!s.length){ return c }
for(var i=0,l=s.length,n; i<l; ++i){
n = s.charCodeAt(i);
c = ((c<<5)-c)+n;
c |= 0;
}
return c;
}
Type.list = {is: function(l){ return (l instanceof Array) }}
Type.list.slit = Array.prototype.slice;
Type.list.sort = function(k){ // creates a new sort function based off some key
@ -956,7 +967,7 @@
obj_map(v, each, this[k]);
}, at.opt);
Gun.on('opt', at);
at.opt.uuid = at.opt.uuid || function(){ return state_lex() + text_rand(12) }
//at.opt.uuid = at.opt.uuid || function(){ return state_lex() + text_rand(12) }
Gun.obj.native();
return gun;
}
@ -1402,7 +1413,7 @@
if(cat.jam){ return cat.jam.push([cb, as]) }
cat.jam = [[cb,as]];
gun.get(function go(msg, eve){
if(u === msg.put && (tmp = Object.keys(cat.root.opt.peers).length) && ++acks < tmp){
if(u === msg.put && (tmp = Object.keys(cat.root.opt.peers).length) && ++acks <= tmp){
return;
}
eve.rid(msg);
@ -1488,7 +1499,7 @@
if(as.res){ as.res() }
return gun;
}
as.soul = as.soul || (as.not = Gun.node.soul(as.data) || (as.via.back('opt.uuid') || Gun.text.random)());
as.soul = as.soul || (as.not = Gun.node.soul(as.data) || (as.via.back('opt.uuid') /*|| Gun.text.random*/)());
if(!as.soul){ // polyfill async uuid for SEA
as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
if(err){ return Gun.log(err) } // TODO: Handle error!
@ -1613,11 +1624,13 @@
}, {as: as, at: at});
//if(is){ return {} }
}
var G = String.fromCharCode(31);
function soul(id, as, msg, eve){
var as = as.as, cat = as.at; as = as.as;
var as = as.as, path = as.p, cat = as.at; as = as.as;
var at = ((msg || {}).$ || {})._ || {};
id = at.dub = at.dub || id || Gun.node.soul(cat.obj) || Gun.node.soul(msg.put || at.put) || Gun.val.link.is(msg.put || at.put) || (as.via.back('opt.uuid') || Gun.text.random)(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous?
id = at.dub = at.dub || id || Gun.node.soul(cat.obj) || Gun.node.soul(msg.put || at.put) || Gun.val.link.is(msg.put || at.put) || (as.via.back('opt.uuid') || function(){
return (as.soul+'.')+Gun.text.hash(path.join(G)).toString(32);
})(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous? What about copy operations?
if(eve){ eve.stun = true }
if(!id){ // polyfill async uuid for SEA
as.via.back('opt.uuid')(function(err, id){ // TODO: improve perf without anonymous callback
@ -1671,13 +1684,13 @@
}
if(!as.not && !(as.soul = as.soul || soul)){
if(as.path && obj_is(as.data)){
as.soul = (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)();
as.soul = (opt.uuid || as.via.back('opt.uuid') /*|| Gun.text.random*/)();
} else {
//as.data = obj_put({}, as.$._.get, as.data);
if(node_ == at.get){
as.soul = (at.put||empty)['#'] || at.dub;
}
as.soul = as.soul || at.soul || at.link || (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)();
as.soul = as.soul || at.soul || at.link || (opt.uuid || as.via.back('opt.uuid') /*|| Gun.text.random*/)();
}
if(!as.soul){ // polyfill async uuid for SEA
as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
@ -1891,9 +1904,9 @@
if(soul = Gun.node.soul(item)){ item = Gun.obj.put({}, soul, Gun.val.link.ify(soul)) }
if(!Gun.is(item)){
if(Gun.obj.is(item)){;
item = gun.back(-1).get(soul = soul || Gun.node.soul(item) || gun.back('opt.uuid')()).put(item);
item = gun.back(-1).get(soul = soul || Gun.node.soul(item) || (gun.back('opt.uuid') || uuid)()).put(item);
}
return gun.get(soul || (Gun.state.lex() + Gun.text.random(7))).put(item, cb, opt);
return gun.get(soul || uuid()).put(item, cb, opt);
}
item.get(function(soul, o, msg){
if(!soul){ return cb.call(gun, {err: Gun.log('Only a node can be linked! Not "' + msg.put + '"!')}) }
@ -1901,6 +1914,7 @@
},true);
return item;
}
function uuid(){ return Gun.state.lex() + Gun.text.random(7) }
})(USE, './set');
;USE(function(module){
@ -2325,18 +2339,6 @@
}
;(function(){
Type.text.hash = function(s){ // via SO
if(typeof s !== 'string'){ return {err: 1} }
var c = 0;
if(!s.length){ return c }
for(var i=0,l=s.length,n; i<l; ++i){
n = s.charCodeAt(i);
c = ((c<<5)-c)+n;
c |= 0;
}
return c; // Math.abs(c);
}
var $ = JSON.stringify, u;
Type.obj.hash = function(obj, hash){

4
sea.js
View File

@ -734,13 +734,13 @@
if(user = root.back('user')){ return user }
var root = (root._), at = root, uuid = at.opt.uuid || Gun.state.lex;
(at = (user = at.user = gun.chain(new User))._).opt = {};
at.opt.uuid = function(cb){
/*at.opt.uuid = function(cb){
var id = uuid(), pub = root.user;
if(!pub || !(pub = pub.is) || !(pub = pub.pub)){ return id }
id = id + '~' + pub + '.';
if(cb && cb.call){ cb(null, id) }
return id;
}
}*/
return user;
}
Gun.User = User;

130
test/panic/no-override.js Normal file
View File

@ -0,0 +1,130 @@
var config = {
IP: require('ip').address(),
port: 8765,
servers: 1,
browsers: 1,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
'/jquery.js': __dirname + '/../../examples/jquery.js'
}
}
var panic = require('panic-server');
panic.server().on('request', function(req, res){
config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res);
}).listen(config.port);
var clients = panic.clients;
var manager = require('panic-manager')();
manager.start({
clients: Array(config.servers).fill().map(function(u, i){
return {
type: 'node',
port: config.port + (i + 1)
}
}),
panic: 'http://' + config.IP + ':' + config.port
});
var servers = clients.filter('Node.js');
var server = servers.pluck(1);
var spawn = servers.excluding(server).pluck(1);
var browsers = clients.excluding(servers);
var alice = browsers.pluck(1);
var again = {};
describe("No Empty Object on .Once", function(){
//this.timeout(5 * 60 * 1000);
this.timeout(10 * 60 * 1000);
it("Servers have joined!", function(){
return servers.atLeast(config.servers);
});
it("GUN started!", function(){
return server.run(function(test){
var env = test.props;
test.async();
try{ require('fs').unlinkSync(env.i+'data') }catch(e){}
try{ require('fs').unlinkSync((env.i+1)+'data') }catch(e){}
try{ require('gun/lib/fsrm')(env.i+'data') }catch(e){}
try{ require('gun/lib/fsrm')((env.i+1)+'data') }catch(e){}
var port = env.config.port + env.i;
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
});
var Gun = require('gun');
var gun = Gun({file: env.i+'data', web: server});
server.listen(port, function(){});
setTimeout(function(){
gun.get('survey').get('231119').get('x').put({z:1})
gun.get('survey').get('231119').get('y').put({z:1})
gun.get('survey').get('231119').once(function(data){
console.log(data);
if(!data.x || !data.y){
no_data_explode;
}
test.done();
});
},500);
}, {i: 1, config: config});
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
return browsers.atLeast(config.browsers);
});
it("Browsers initialized gun!", function(){
var tests = [], i = 0;
browsers.each(function(client, id){
tests.push(client.run(function(test){
localStorage.clear();
var env = test.props;
var gun = Gun('http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun');
window.ref = gun.get('survey').get('231119');
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
it("Write initial value", function(){
return alice.run(function(test){
test.async();
console.log("I AM ALICE");
ref.get('z').put({z:1});
setTimeout(function(){
ref.once(function(data){
console.log("DOES X Y & Z EXIST?", data);
if(!data.x || !data.y || !data.z){
test.fail("overwrote old object!");
return;
}
test.done();
})
}, 500);
});
});
it("All finished!", function(done){
console.log("Done! Cleaning things up...");
setTimeout(function(){
done();
},1000);
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
return servers.run(function(){
process.exit();
});
});
});