fix super serious put

This commit is contained in:
Mark Nadal 2018-01-15 00:27:12 -08:00
parent 6e1ef72a7b
commit a372951a53
18 changed files with 836 additions and 92 deletions

View File

@ -13,7 +13,8 @@
margin: 1em 0;
padding: 1em;
border: none;
background: white;
background: rgba(100%, 100%, 100%, 0.9);
back-ground: white;
}
.sigbut {
display: inline-block;
@ -56,7 +57,7 @@
<ul>
<a href="person" name="#">
<li>
<span name="profile">
<span name="who">
<b name="name"></b>
</span>
<i>~</i><i name="alias"></i>
@ -86,7 +87,7 @@
<div id="person" class="hue3 page">
<div class="pad">
<div name="profile">
<div name="who">
<!-- img src="" -->
<h2 name="name" contenteditable="true">Name</h2>
<i name="born"></i>
@ -97,7 +98,7 @@
</form>
<div>
<ul name="said">
<li name="#"><span name="what"></span></li>
<li name="#" class="shade rim"><div name="what" class="rim"></div></li>
</ul>
</div>
</div>
@ -109,15 +110,21 @@
<p>A mysterious new example app has appeared! It is not finished/ready yet.</p>
</div id="info">
<script src="../jquery.js"></script>
<script src="../../gun.js"></script>
<script src="../../lib/cryptomodules.js"></script>
<script src="../../sea.js"></script>
<script src="../../as.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/examples/jquery.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/gun.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/lib/cryptomodules.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/sea.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/as.js"></script>
<!-- script src="/jquery.js"></script>
<script src="/gun.js"></script>
<script src="/gun/lib/cryptomodules.js"></script>
<script src="/gun/sea.js"></script>
<script src="/gun/as.js"></script -->
<script>
//var gun = Gun('http://localhost:8080/gun'); // DOESN'T WORK YET :(
var gun = Gun();
var app = gun.get('example/contacts/4');
var gun = Gun('http://localhost:8080/gun'); // DOESN'T WORK YET :(
//var gun = Gun();
var app = gun.get('example/contacts/5');
var user = gun.user();
var c = window.c = {};
(function(){
@ -203,7 +210,7 @@
(function(){
$(document).on('submit', '#say', function(e){
e.preventDefault();
user.get('profile').get('said').set({
user.get('who').get('said').set({
what: $(this).find('.jot').val()
});
$(this).find('.jot').val('');
@ -219,7 +226,7 @@
as.route.page('person', function(){
if(!user.is){ return as.route('sign') }
var pub = location.hash.split('/').slice(-1)[0];
as('#person', gun.get('pub/'+pub));
as('#person', window.PUB = gun.get('pub/'+pub));
});

View File

@ -69,7 +69,7 @@
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://gunjs.herokuapp.com/gun.js"></script>
<script src="/gun.js"></script>
<script>
function Where(opt, cb){
// a small wrapper around Leaflet for map tracking.

View File

@ -62,6 +62,13 @@ input, textarea {
background: white;
}
.shade {
background: rgba(0%, 0%, 0%, 0.1);
}
.tint {
background: rgba(100%, 100%, 100%, 0.1);
}
.redt {
color: #ea3224;
}
@ -88,6 +95,10 @@ input, textarea {
max-width: 50em;
}
.rim {
margin: 2%;
}
.right {
float: right;
}
@ -125,17 +136,17 @@ input, textarea {
}
@keyframes hue {
0% {background: #4D79D8;}
25% {background: #33cc33;}
50% {background: #f2b919;}
75% {background: #ea3224;}
100% {background: #4D79D8;}
0% {background-color: #4D79D8;}
25% {background-color: #33cc33;}
50% {background-color: #f2b919;}
75% {background-color: #ea3224;}
100% {background-color: #4D79D8;}
} @-webkit-keyframes hue {
0% {background: #4D79D8;}
25% {background: #33cc33;}
50% {background: #f2b919;}
75% {background: #ea3224;}
100% {background: #4D79D8;}
0% {background-color: #4D79D8;}
25% {background-color: #33cc33;}
50% {background-color: #f2b919;}
75% {background-color: #ea3224;}
100% {background-color: #4D79D8;}
}
.huet {
@ -165,17 +176,17 @@ input, textarea {
}
@keyframes hue2 {
0% {background: #ea3224;}
25% {background: #4D79D8;}
50% {background: #33cc33;}
75% {background: #f2b919;}
100% {background: #ea3224;}
0% {background-color: #ea3224;}
25% {background-color: #4D79D8;}
50% {background-color: #33cc33;}
75% {background-color: #f2b919;}
100% {background-color: #ea3224;}
} @-webkit-keyframes hue2 {
0% {background: #ea3224;}
25% {background: #4D79D8;}
50% {background: #33cc33;}
75% {background: #f2b919;}
100% {background: #ea3224;}
0% {background-color: #ea3224;}
25% {background-color: #4D79D8;}
50% {background-color: #33cc33;}
75% {background-color: #f2b919;}
100% {background-color: #ea3224;}
}
.huet2 {
@ -205,17 +216,17 @@ input, textarea {
}
@keyframes hue3 {
0% {background: #33cc33;}
25% {background: #f2b919;}
50% {background: #ea3224;}
75% {background: #4D79D8;}
100% {background: #33cc33;}
0% {background-color: #33cc33;}
25% {background-color: #f2b919;}
50% {background-color: #ea3224;}
75% {background-color: #4D79D8;}
100% {background-color: #33cc33;}
} @-webkit-keyframes hue3 {
0% {background: #33cc33;}
25% {background: #f2b919;}
50% {background: #ea3224;}
75% {background: #4D79D8;}
100% {background: #33cc33;}
0% {background-color: #33cc33;}
25% {background-color: #f2b919;}
50% {background-color: #ea3224;}
75% {background-color: #4D79D8;}
100% {background-color: #33cc33;}
}
.huet3 {
@ -245,17 +256,17 @@ input, textarea {
}
@keyframes hue4 {
0% {background: #f2b919;}
25% {background: #ea3224;}
50% {background: #4D79D8;}
75% {background: #33cc33;}
100% {background: #f2b919;}
0% {background-color: #f2b919;}
25% {background-color: #ea3224;}
50% {background-color: #4D79D8;}
75% {background-color: #33cc33;}
100% {background-color: #f2b919;}
} @-webkit-keyframes hue4 {
0% {background: #f2b919;}
25% {background: #ea3224;}
50% {background: #4D79D8;}
75% {background: #33cc33;}
100% {background: #f2b919;}
0% {background-color: #f2b919;}
25% {background-color: #ea3224;}
50% {background-color: #4D79D8;}
75% {background-color: #33cc33;}
100% {background-color: #f2b919;}
}
.huet4 {

21
gun.js
View File

@ -1162,9 +1162,9 @@
Gun.obj.del(at, 'ask'); // TODO: PERFORMANCE? More elegant way?
}
function ack(msg, ev){
var as = this.as, get = as.get || empty, at = as.gun._;
var as = this.as, get = as.get || empty, at = as.gun._, tmp = (msg.put||empty)[get['#']];
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(tmp, at.get))){
if(at.put !== u){ return }
//at.ack = 0;
at.on('in', {
@ -1175,6 +1175,10 @@
})
return;
}
if(node_ == get['.']){ // is this a security concern?
at.on('in', {get: at.get, put: tmp[at.get], gun: at.gun, '@': msg['@']});
return;
}
//if(/*!msg.gun &&*/ !get['.'] && get['#']){ at.ack = (at.ack + 1) || 1 }
//msg = obj_to(msg);
msg.gun = at.root;
@ -1228,7 +1232,7 @@
var cat = back._, next = cat.next, gun = back.chain(), at = gun._;
if(!next){ next = cat.next = {} }
next[at.get = key] = gun;
if(cat.root === back){
if(cat.root === back){
at.soul = key;
} else
if(cat.soul || cat.field || cat.has){ // TODO: Convert field to has!
@ -1368,6 +1372,7 @@
var tmp = cat.root._.now; obj.del(cat.root._, 'now');
var tmp2 = cat.root._.stop;
(as.ref._).now = true;
//console.log('PUT!', as.env.graph);
(as.ref._).on('out', {
gun: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask
});
@ -1468,7 +1473,10 @@
as.soul = (opt.uuid || cat.root._.opt.uuid || Gun.text.random)();
} else {
//as.data = obj_put({}, as.gun._.get, as.data);
as.soul = at.soul || cat.soul || (opt.uuid || cat.root._.opt.uuid || Gun.text.random)();
if(node_ == at.get){
as.soul = (at.put||empty)['#'];
}
as.soul = as.soul || at.soul || cat.soul || (opt.uuid || cat.root._.opt.uuid || Gun.text.random)();
}
if(!as.soul){ // polyfill async uuid for SEA
as.ref.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
@ -1482,6 +1490,7 @@
}
var obj = Gun.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map;
var u, empty = {}, noop = function(){}, iife = function(fn,as){fn.call(as||empty)};
var node_ = Gun.node._;
})(require, './put');
;require(function(module){
@ -1698,8 +1707,10 @@
item.get('_').get(function(at, ev){
if(!at.gun || !at.gun._.back){ return }
ev.off();
var soul = (at.put||{})['#'];
at = (at.gun._.back._);
var put = {}, node = at.put, soul = Gun.node.soul(node);
var put = {}, node = at.put;
soul = at.soul || Gun.node.soul(node) || soul;
if(!soul){ return cb.call(gun, {err: Gun.log('Only a node can be linked! Not "' + node + '"!')}) }
gun.put(Gun.obj.put(put, soul, Gun.val.rel.ify(soul)), cb, opt);
},{wait:0});

2
gun.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.9.7",
"version": "0.9.8",
"description": "Graph engine",
"main": "index.js",
"browser": "gun.min.js",

13
sea.js
View File

@ -903,6 +903,12 @@
return;
}
check['any'+soul+key] = 1;
if((tmp = soul.split('~')) && 2 == tmp.length){
setTimeout(function(){ // hacky idea, what would be better?
each.any(val, key, node, soul);
},1);
return;
}
at.on('secure', function(msg){ this.off();
check['any'+soul+key] = 0;
each.end(msg || {err: "Data cannot be modified."});
@ -914,6 +920,13 @@
each.end({err: "Soul is not signed at '" + key + "'."});
return;
}
var other = Gun.obj.map(at.sea.own[soul], function(v, p){
if(user.pub !== p){ return p }
});
if(other){
each.any(val, key, node, soul);
return;
}
check['any'+soul+key] = 1;
SEA.verify(tmp[0], user.pub, tmp[1], function(ok){
if(!ok){ return each.end({err: "Signature did not match account at '" + key + "'."}) }

View File

@ -157,10 +157,22 @@ function relate(at, msg, from, rel){
}
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;
// NOW is a hack to get synchronous replies to correctly call.
// and STOP is a hack to get async behavior to correctly call.
// neither of these are ideal, need to be fixed without hacks,
// but for now, this works for current tests. :/
if(!now){
var stop = at.root._.stop;
if(!stop){ return }
if(stop[at.id] === rel){ return }
stop[at.id] = rel;
} else {
if(u === now[at.id]){ return }
if((now._ || (now._ = {}))[at.id] === rel){ return }
now._[at.id] = rel;
}
}
ask(at, tmp.rel = rel);
}
@ -240,7 +252,7 @@ function ask(at, soul){
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', {

View File

@ -44,9 +44,9 @@ function cache(key, back){
} else
if(cat.soul || cat.field || cat.has){ // TODO: Convert field to has!
at.field = at.has = key;
if(obj_has(cat.put, key)){
//if(obj_has(cat.put, key)){
//at.put = cat.put[key];
}
//}
}
return gun;
}

View File

@ -4,7 +4,10 @@ module.exports = function onto(tag, arg, as){
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(arg);
}}
}});
if(arg instanceof Function){
var be = {

View File

@ -97,13 +97,19 @@ function batch(){ var as = this;
if(!as.ack){ return }
as.ack(ack, this);
}, as.opt);
// NOW is a hack to get synchronous replies to correctly call.
// and STOP is a hack to get async behavior to correctly call.
// neither of these are ideal, need to be fixed without hacks,
// but for now, this works for current tests. :/
var tmp = cat.root._.now; obj.del(cat.root._, 'now');
var tmp2 = cat.root._.stop;
(as.ref._).now = true;
(as.ref._).on('out', {
gun: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask
});
obj.del((as.ref._), 'now');
cat.root._.now = tmp;
cat.root._.stop = tmp2;
}, as);
if(as.res){ as.res() }
} function no(v,f){ if(v){ return true } }

View File

@ -71,11 +71,11 @@ Gun._ = { // some reserved key words, these are not the only ones.
;(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);
@ -97,32 +97,51 @@ Gun._ = { // some reserved key words, these are not the only ones.
}
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 }
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._);
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._.root._.stop = {};
//console.log("map ->", soul, msg.put);
(msg.gun._).on('in', msg);
msg.gun._.root._.stop = {};
}
Gun.on.get = function(msg, gun){
@ -190,4 +209,17 @@ Gun.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, feel f
if(typeof window !== "undefined"){ window.Gun = Gun }
if(typeof common !== "undefined"){ common.exports = Gun }
module.exports = Gun;
/*Gun.on('opt', function(ctx){ // FOR TESTING PURPOSES
this.to.next(ctx);
if(ctx.once){ return }
ctx.on('node', function(msg){
var to = this.to;
//console.log(">>>", msg.put);
setTimeout(function(){
//console.log("<<<<<", msg.put);
to.next(msg);
},1);
})
});*/

View File

@ -6,9 +6,11 @@ Gun.chain.set = function(item, cb, opt){
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 }

27
test/debug/deep-set.html Normal file
View File

@ -0,0 +1,27 @@
<script src="../../gun.js"></script>
<script>
;(function(){
//localStorage.clear();
var gun = window.gun = Gun('http://localhost:8080/gun');
var user = window.user = gun.get('pub/alice');
return;
user.put({pub: 'alice'}, write);
function write(data){
console.log("write...");
user.get('profile').get('said').set({
what: "Hello world! " + Gun.text.random(3)
}, get);
}
function get(){
console.log("get...");
user.get('profile').get('said').map().val(function(data){
console.log("read...", data);
})
}
}());
</script>

201
test/panic/set.js Normal file
View File

@ -0,0 +1,201 @@
var config = {
IP: require('ip').address(),
port: 8080,
servers: 1,
browsers: 2,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
'/jquery.js': __dirname + '/../../examples/jquery.js',
'/cryptomodules.js': __dirname + '/../../lib/cryptomodules.js',
'/sea.js': __dirname + '/../../sea.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 bob = browsers.excluding(alice).pluck(1);
var again = {};
describe("Make sure SEA syncs correctly", 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){}
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(){
test.done();
});
}, {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 load SEA!", function(){
var tests = [], i = 0;
browsers.each(function(client, id){
tests.push(client.run(function(test){
test.async();
//console.log("load?");
function load(src, cb){
var script = document.createElement('script');
script.onload = cb; script.src = src;
document.head.appendChild(script);
}
/*load('cryptomodules.js', function(){
load('sea.js', function(){
test.done();
});
});*/
test.done();
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
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.gun = gun;
var user = window.user = gun.get('pub/alice');
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
it("Create Alice", function(){
return alice.run(function(test){
console.log("I AM ALICE");
test.async();
user.put({pub: 'alice'}, function(ack){
if(ack.err){ return }
console.log("write...");
user.get('who').get('said').set({
what: "Hello world!"
}, function(ack){
if(ack.err){ return }
test.done();
});
});
});
});
it("Have Bob listen", function(){
return bob.run(function(test){
test.async();
window.count = [];
user.get('who').get('said').map().val(function(data){
console.log("read...", data);
window.count.push(data);
if(window.count.length - 1){ return }
test.done();
});
});
});
it("Alice reloading.", function(){
return alice.run(function(test){
location.reload();
});
});
it("Got Alice.", function(){
again.alice = browsers.excluding(new panic.ClientList([alice, bob])).pluck(1);
return again.alice.atLeast(1);
});
it("Alice reloaded.", function(){
return again.alice.run(function(test){
var env = test.props;
var gun = Gun('http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun');
window.gun = gun;
var user = window.user = gun.get('pub/alice');
}, {i: 1, config: config})
});
it("Alice write.", function(){
return again.alice.run(function(test){
test.async();
console.log("write...");
user.get('who').get('said').set({
what: "AAA"
}, function(ack){
if(ack.err){ return }
test.done();
});
});
});
it("Bob got", function(){
return bob.run(function(test){
test.async();
console.log(window.count);
setTimeout(function(){
console.log("!!!!!!!!!!!!!!!!!!!!!!!!", window.count);
if('AAA' === window.count[1].what){
test.done();
}
}, 100);
});
});
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();
});
});
});

252
test/panic/who.js Normal file
View File

@ -0,0 +1,252 @@
var config = {
IP: require('ip').address(),
port: 8080,
servers: 1,
browsers: 2,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
'/jquery.js': __dirname + '/../../examples/jquery.js',
'/cryptomodules.js': __dirname + '/../../lib/cryptomodules.js',
'/sea.js': __dirname + '/../../sea.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 bob = browsers.excluding(alice).pluck(1);
var again = {};
describe("Make sure SEA syncs correctly", 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){}
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(){
test.done();
});
}, {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 load SEA!", function(){
var tests = [], i = 0;
browsers.each(function(client, id){
tests.push(client.run(function(test){
test.async();
//console.log("load?");
function load(src, cb){
var script = document.createElement('script');
script.onload = cb; script.src = src;
document.head.appendChild(script);
}
load('cryptomodules.js', function(){
load('sea.js', function(){
test.done();
});
});
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
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.gun = gun;
var user = window.user = gun.user();
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
it("Create Alice", function(){
return alice.run(function(test){
console.log("I AM ALICE");
test.async();
window.user.create('alice', 'xyzabcmnopq', function(ack){
if(ack.err || !ack.pub){ return }
window.user.auth('alice', 'xyzabcmnopq', function(ack){
if(ack.err || !ack.pub){ return }
test.done();
user.get('who').get('said').set({
what: "Hello world!"
}, function(ack){
if(ack.err){ return }
test.done();
});
});
});
});
});
it("Create Bob", function(){
return bob.run(function(test){
test.async();
window.user.create('bob', 'zyxcbaqponm', function(ack){
if(ack.err || !ack.pub){ return }
window.user.auth('bob', 'zyxcbaqponm', function(ack){
if(ack.err || !ack.pub){ return }
test.done();
});
});
});
});
it("Have Bob find Alice", function(){
return bob.run(function(test){
test.async();
window.gun.get('alias/alice').map().val(function(data){
window.ref = gun.get('pub/'+data.pub);
test.done();
});
});
});
it("Have Bob listen", function(){
return bob.run(function(test){
test.async();
window.count = [];
ref.get('who').get('said').map().val(function(data){
console.log("read...", data);
window.count.push(data);
if(window.count.length - 1){ return }
test.done();
});
});
});
it("Alice reloading.", function(){
return alice.run(function(test){
location.reload();
});
});
it("Got Alice.", function(){
again.alice = browsers.excluding(new panic.ClientList([alice, bob])).pluck(1);
return again.alice.atLeast(1);
});
it("Alice reloaded.", function(){
return again.alice.run(function(test){
test.async();
//console.log("load?");
function load(src, cb){
var script = document.createElement('script');
script.onload = cb; script.src = src;
document.head.appendChild(script);
}
load('cryptomodules.js', function(){
load('sea.js', function(){
test.done();
});
});
}, {i: 1, config: config});
});
it("Alice loaded.", function(){
return again.alice.run(function(test){
test.async();
var env = test.props;
var gun = Gun('http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun');
window.gun = gun;
var user = window.user = gun.user();
user.auth('alice', 'xyzabcmnopq', function(ack){
console.log("????", ack);
if(ack.err || !ack.pub){ return }
test.done();
});
}, {i: 1, config: config})
});
it("Alice write.", function(){
return again.alice.run(function(test){
test.async();
console.log("write...");
user.get('who').get('said').set({
what: "AAA"
}, function(ack){
if(ack.err){ return }
test.done();
});
});
});
it("Have Bob listen", function(){
return bob.run(function(test){
test.async();
console.log(window.count);
setTimeout(function(){
console.log("!!!!!!!!!!!!!!!!!!!!!!!!", window.count);
if('AAA' === window.count[1].what){
test.done();
}
}, 100);
});
});
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();
});
});
});

134
test/ptsd/streampipe.js Normal file
View File

@ -0,0 +1,134 @@
const stream = require('stream')
const Gun = require('gun')
const randomString = length => {
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
let text = ''
for (var i = 0; i < length; i++) {
text += possible[Math.floor(Math.random() * possible.length)]
}
return text
}
// Generator
// generates stream of json {id: "", name: "", description: ""}
class Generator extends stream.Readable {
constructor(n) {
super()
this._n = n
this._i = 0
}
_read(size) {
if (this._i < this._n) {
this.push(
JSON.stringify({
id: `${this._i}`,
name: randomString(20),
description: randomString(200)
})
)
this.push('\n')
this._i++
} else {
this.push(null)
}
}
}
// Line
// read line by line as stream comes through it
class Line extends stream.Transform {
constructor() {
super()
this.buff = ''
}
trySendLine() {
let index = this.buff.indexOf(`\n`)
while (index !== -1) {
const line = this.buff.slice(0, index)
this.buff = this.buff.slice(index + 1)
this.push(line)
index = this.buff.indexOf(`\n`)
}
}
_transform(chunk, enc, cb) {
this.buff += chunk.toString()
this.trySendLine()
cb()
}
end() {
this.trySendLine()
if (this.buff.length > 0) {
this.push(this.buff)
}
}
}
// Graph
// parse the chunk and tries to add it to table
class Graph extends stream.Transform {
constructor() {
super()
this.db = new Gun({
//file: 'graph.json'
localStorage: false
})
this.items = this.db.get('items')
}
_transform(chunk, enc, cb) {
const json = JSON.parse(chunk.toString())
const item = this.db.get(json.id)
item.put(
{
id: json.id,
name: json.name,
description: json.description
},
() => {
this.items.set(item, () => {
this.push(chunk)
cb()
})
}
)
}
}
// Report
// shows how many item has pass through the system
class Report extends stream.Transform {
constructor() {
super()
this._count = 0
}
_transform(chunk, enc, cb) {
this.push(`count: ${this._count++}\r`)
cb()
}
}
const generator = new Generator(5000)
const line = new Line()
const graph = new Graph()
const report = new Report()
generator
.pipe(line)
.pipe(graph)
.pipe(report)
.pipe(process.stdout)

33
test/sea-tmp.html Normal file
View File

@ -0,0 +1,33 @@
<script src="../gun.js"></script>
<script src="../lib/cryptomodules.js"></script>
<script src="../sea.js"></script>
<script>
;(function(){
localStorage.clear();
var gun = window.gun = Gun();
var user = gun.user();
user.create('alice', 'unsafepassword', login);
function login(ack){
console.log("login...");
user.auth('alice', 'unsafepassword', write);
}
function write(data){
console.log("write...");
user.get('profile').get('said').set({
what: "Hello world!"
}, get);
}
function get(){
console.log("get...");
user.get('profile').get('said').map().val(function(data){
console.log("read...", data);
})
}
}());
</script>