USE the best, thanks to @d3x0r !

This commit is contained in:
Mark Nadal 2018-01-15 02:34:08 -08:00
parent 2b05fc805c
commit a5275172af
9 changed files with 105 additions and 92 deletions

154
gun.js
View File

@ -6,19 +6,19 @@
if(typeof global !== "undefined"){ root = global } if(typeof global !== "undefined"){ root = global }
root = root || {}; root = root || {};
var console = root.console || {log: function(){}}; var console = root.console || {log: function(){}};
function require(arg){ function USE(arg){
return arg.slice? require[resolve(arg)] : function(mod, path){ return arg.slice? USE[R(arg)] : function(mod, path){
arg(mod = {exports: {}}); arg(mod = {exports: {}});
require[resolve(path)] = mod.exports; USE[R(path)] = mod.exports;
} }
function resolve(path){ function R(p){
return path.split('/').slice(-1).toString().replace('.js',''); return p.split('/').slice(-1).toString().replace('.js','');
} }
} }
if(typeof module !== "undefined"){ var common = module } if(typeof module !== "undefined"){ var common = module }
/* UNBUILD */ /* UNBUILD */
;require(function(module){ ;USE(function(module){
// Generic javascript utilities. // Generic javascript utilities.
var Type = {}; var Type = {};
//Type.fns = Type.fn = {is: function(fn){ return (!!fn && fn instanceof Function) }} //Type.fns = Type.fn = {is: function(fn){ return (!!fn && fn instanceof Function) }}
@ -163,9 +163,9 @@
var list_is = Type.list.is; var list_is = Type.list.is;
var obj = Type.obj, obj_is = obj.is, obj_has = obj.has, obj_map = obj.map; var obj = Type.obj, obj_is = obj.is, obj_has = obj.has, obj_map = obj.map;
module.exports = Type; module.exports = Type;
})(require, './type'); })(USE, './type');
;require(function(module){ ;USE(function(module){
// On event emitter generic javascript utility. // On event emitter generic javascript utility.
module.exports = function onto(tag, arg, as){ module.exports = function onto(tag, arg, as){
if(!tag){ return {to: onto} } if(!tag){ return {to: onto} }
@ -203,9 +203,9 @@
(tag = tag.to).next(arg); (tag = tag.to).next(arg);
return tag; return tag;
}; };
})(require, './onto'); })(USE, './onto');
;require(function(module){ ;USE(function(module){
/* Based on the Hypothetical Amnesia Machine thought experiment */ /* Based on the Hypothetical Amnesia Machine thought experiment */
function HAM(machineState, incomingState, currentState, incomingValue, currentValue){ function HAM(machineState, incomingState, currentState, incomingValue, currentValue){
if(machineState < incomingState){ if(machineState < incomingState){
@ -250,10 +250,10 @@
} }
var Lexical = JSON.stringify, undefined; var Lexical = JSON.stringify, undefined;
module.exports = HAM; module.exports = HAM;
})(require, './HAM'); })(USE, './HAM');
;require(function(module){ ;USE(function(module){
var Type = require('./type'); var Type = USE('./type');
var Val = {}; var Val = {};
Val.is = function(v){ // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first. Val.is = function(v){ // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first.
if(v === u){ return false } if(v === u){ return false }
@ -294,11 +294,11 @@
var text_is = Type.text.is; var text_is = Type.text.is;
var obj = Type.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map; var obj = Type.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map;
module.exports = Val; module.exports = Val;
})(require, './val'); })(USE, './val');
;require(function(module){ ;USE(function(module){
var Type = require('./type'); var Type = USE('./type');
var Val = require('./val'); var Val = USE('./val');
var Node = {_: '_'}; var Node = {_: '_'};
Node.soul = function(n, o){ return (n && n._ && n._[o || soul_]) } // convenience function to check to see if there is a soul on a node and return it. Node.soul = function(n, o){ return (n && n._ && n._[o || soul_]) } // convenience function to check to see if there is a soul on a node and return it.
Node.soul.ify = function(n, o){ // put a soul on an object. Node.soul.ify = function(n, o){ // put a soul on an object.
@ -353,11 +353,11 @@
var soul_ = Node.soul._; var soul_ = Node.soul._;
var u; var u;
module.exports = Node; module.exports = Node;
})(require, './node'); })(USE, './node');
;require(function(module){ ;USE(function(module){
var Type = require('./type'); var Type = USE('./type');
var Node = require('./node'); var Node = USE('./node');
function State(){ function State(){
var t; var t;
if(perf){ if(perf){
@ -437,12 +437,12 @@
var fn = Type.fn, fn_is = fn.is; var fn = Type.fn, fn_is = fn.is;
var N_ = Node._, u; var N_ = Node._, u;
module.exports = State; module.exports = State;
})(require, './state'); })(USE, './state');
;require(function(module){ ;USE(function(module){
var Type = require('./type'); var Type = USE('./type');
var Val = require('./val'); var Val = USE('./val');
var Node = require('./node'); var Node = USE('./node');
var Graph = {}; var Graph = {};
;(function(){ ;(function(){
Graph.is = function(g, cb, fn, as){ // checks to see if an object is a valid graph. Graph.is = function(g, cb, fn, as){ // checks to see if an object is a valid graph.
@ -589,11 +589,11 @@
var obj = Type.obj, obj_is = obj.is, obj_del = obj.del, obj_has = obj.has, obj_empty = obj.empty, obj_put = obj.put, obj_map = obj.map, obj_copy = obj.copy; var obj = Type.obj, obj_is = obj.is, obj_del = obj.del, obj_has = obj.has, obj_empty = obj.empty, obj_put = obj.put, obj_map = obj.map, obj_copy = obj.copy;
var u; var u;
module.exports = Graph; module.exports = Graph;
})(require, './graph'); })(USE, './graph');
;require(function(module){ ;USE(function(module){
// request / response module, for asking and acking messages. // request / response module, for asking and acking messages.
require('./onto'); // depends upon onto! USE('./onto'); // depends upon onto!
module.exports = function ask(cb, as){ module.exports = function ask(cb, as){
if(!this.on){ return } if(!this.on){ return }
if(!(cb instanceof Function)){ if(!(cb instanceof Function)){
@ -613,10 +613,10 @@
}, (this.opt||{}).lack || 9000); }, (this.opt||{}).lack || 9000);
return id; return id;
} }
})(require, './ask'); })(USE, './ask');
;require(function(module){ ;USE(function(module){
var Type = require('./type'); var Type = USE('./type');
function Dup(opt){ function Dup(opt){
var dup = {s:{}}; var dup = {s:{}};
opt = opt || {max: 1000, age: 1000 * 9};//1000 * 60 * 2}; opt = opt || {max: 1000, age: 1000 * 9};//1000 * 60 * 2};
@ -640,9 +640,9 @@
} }
var time_is = Type.time.is; var time_is = Type.time.is;
module.exports = Dup; module.exports = Dup;
})(require, './dup'); })(USE, './dup');
;require(function(module){ ;USE(function(module){
function Gun(o){ function Gun(o){
if(o instanceof Gun){ return (this._ = {gun: this}).gun } if(o instanceof Gun){ return (this._ = {gun: this}).gun }
@ -657,16 +657,16 @@
Gun.chain = Gun.prototype; Gun.chain = Gun.prototype;
Gun.chain.toJSON = function(){}; Gun.chain.toJSON = function(){};
var Type = require('./type'); var Type = USE('./type');
Type.obj.to(Type, Gun); Type.obj.to(Type, Gun);
Gun.HAM = require('./HAM'); Gun.HAM = USE('./HAM');
Gun.val = require('./val'); Gun.val = USE('./val');
Gun.node = require('./node'); Gun.node = USE('./node');
Gun.state = require('./state'); Gun.state = USE('./state');
Gun.graph = require('./graph'); Gun.graph = USE('./graph');
Gun.on = require('./onto'); Gun.on = USE('./onto');
Gun.ask = require('./ask'); Gun.ask = USE('./ask');
Gun.dup = require('./dup'); Gun.dup = USE('./dup');
Gun._ = { // some reserved key words, these are not the only ones. Gun._ = { // some reserved key words, these are not the only ones.
node: Gun.node._ // all metadata of a node is stored in the meta property on the node. node: Gun.node._ // all metadata of a node is stored in the meta property on the node.
@ -868,10 +868,10 @@
},1); },1);
}); });
});*/ });*/
})(require, './root'); })(USE, './root');
;require(function(module){ ;USE(function(module){
var Gun = require('./root'); var Gun = USE('./root');
Gun.chain.back = function(n, opt){ var tmp; Gun.chain.back = function(n, opt){ var tmp;
n = n || 1; n = n || 1;
if(-1 === n || Infinity === n){ if(-1 === n || Infinity === n){
@ -910,13 +910,13 @@
return this; return this;
} }
var empty = {}, u; var empty = {}, u;
})(require, './back'); })(USE, './back');
;require(function(module){ ;USE(function(module){
// WARNING: GUN is very simple, but the JavaScript chaining API around GUN // WARNING: GUN is very simple, but the JavaScript chaining API around GUN
// is complicated and was extremely hard to build. If you port GUN to another // is complicated and was extremely hard to build. If you port GUN to another
// language, consider implementing an easier API to build. // language, consider implementing an easier API to build.
var Gun = require('./root'); var Gun = USE('./root');
Gun.chain.chain = function(){ Gun.chain.chain = function(){
var at = this._, chain = new this.constructor(this), cat = chain._, root; var at = this._, chain = new this.constructor(this), cat = chain._, root;
cat.root = root = at.root; cat.root = root = at.root;
@ -1191,10 +1191,10 @@
var obj = Gun.obj, obj_has = obj.has, obj_put = obj.put, obj_del = obj.del, obj_to = obj.to, obj_map = obj.map; var obj = Gun.obj, obj_has = obj.has, obj_put = obj.put, obj_del = obj.del, obj_to = obj.to, obj_map = obj.map;
var text_rand = Gun.text.random; var text_rand = Gun.text.random;
var _soul = Gun._.soul, _field = Gun._.field, node_ = Gun.node._; var _soul = Gun._.soul, _field = Gun._.field, node_ = Gun.node._;
})(require, './chain'); })(USE, './chain');
;require(function(module){ ;USE(function(module){
var Gun = require('./root'); var Gun = USE('./root');
Gun.chain.get = function(key, cb, as){ Gun.chain.get = function(key, cb, as){
if(typeof key === 'string'){ if(typeof key === 'string'){
var gun, back = this, cat = back._; var gun, back = this, cat = back._;
@ -1266,10 +1266,10 @@
var num_is = Gun.num.is; var num_is = Gun.num.is;
var rel = Gun.val.rel, node_ = Gun.node._; var rel = Gun.val.rel, node_ = Gun.node._;
var empty = {}, u; var empty = {}, u;
})(require, './get'); })(USE, './get');
;require(function(module){ ;USE(function(module){
var Gun = require('./root'); var Gun = USE('./root');
Gun.chain.put = function(data, cb, as){ Gun.chain.put = function(data, cb, as){
// #soul.field=value>state // #soul.field=value>state
// ~who#where.where=what>when@was // ~who#where.where=what>when@was
@ -1492,19 +1492,19 @@
var obj = Gun.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map; 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 u, empty = {}, noop = function(){}, iife = function(fn,as){fn.call(as||empty)};
var node_ = Gun.node._; var node_ = Gun.node._;
})(require, './put'); })(USE, './put');
;require(function(module){ ;USE(function(module){
var Gun = require('./root'); var Gun = USE('./root');
require('./chain'); USE('./chain');
require('./back'); USE('./back');
require('./put'); USE('./put');
require('./get'); USE('./get');
module.exports = Gun; module.exports = Gun;
})(require, './index'); })(USE, './index');
;require(function(module){ ;USE(function(module){
var Gun = require('./index'); var Gun = USE('./index');
Gun.chain.on = function(tag, arg, eas, as){ Gun.chain.on = function(tag, arg, eas, as){
var gun = this, at = gun._, tmp, act, off; var gun = this, at = gun._, tmp, act, off;
if(typeof tag === 'string'){ if(typeof tag === 'string'){
@ -1650,10 +1650,10 @@
var obj = Gun.obj, obj_map = obj.map, obj_has = obj.has, obj_del = obj.del, obj_to = obj.to; var obj = Gun.obj, obj_map = obj.map, obj_has = obj.has, obj_del = obj.del, obj_to = obj.to;
var rel = Gun.val.rel; var rel = Gun.val.rel;
var empty = {}, noop = function(){}, u; var empty = {}, noop = function(){}, u;
})(require, './on'); })(USE, './on');
;require(function(module){ ;USE(function(module){
var Gun = require('./index'); var Gun = USE('./index');
Gun.chain.map = function(cb, opt, t){ Gun.chain.map = function(cb, opt, t){
var gun = this, cat = gun._, chain; var gun = this, cat = gun._, chain;
if(!cb){ if(!cb){
@ -1689,10 +1689,10 @@
(tmp.echo || (tmp.echo = {}))[at.id] = at; (tmp.echo || (tmp.echo = {}))[at.id] = at;
} }
var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._, u; var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._, u;
})(require, './map'); })(USE, './map');
;require(function(module){ ;USE(function(module){
var Gun = require('./index'); var Gun = USE('./index');
Gun.chain.set = function(item, cb, opt){ Gun.chain.set = function(item, cb, opt){
var gun = this, soul; var gun = this, soul;
cb = cb || function(){}; cb = cb || function(){};
@ -1717,9 +1717,9 @@
},{wait:0}); },{wait:0});
return item; return item;
} }
})(require, './set'); })(USE, './set');
;require(function(module){ ;USE(function(module){
if(typeof Gun === 'undefined'){ return } // TODO: localStorage is Browser only. But it would be nice if it could somehow plugin into NodeJS compatible localStorage APIs? if(typeof Gun === 'undefined'){ return } // TODO: localStorage is Browser only. But it would be nice if it could somehow plugin into NodeJS compatible localStorage APIs?
var root, noop = function(){}, u; var root, noop = function(){}, u;
@ -1793,10 +1793,10 @@
}); });
} }
}); });
})(require, './adapters/localStorage'); })(USE, './adapters/localStorage');
;require(function(module){ ;USE(function(module){
var Gun = require('./index'); var Gun = USE('./index');
var websocket; var websocket;
if(typeof WebSocket !== 'undefined'){ if(typeof WebSocket !== 'undefined'){
websocket = WebSocket; websocket = WebSocket;
@ -1899,6 +1899,6 @@
} }
}); });
var noop = function(){}; var noop = function(){};
})(require, './adapters/websocket'); })(USE, './adapters/websocket');
}()); }());

2
gun.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -88,10 +88,11 @@ var undent = function(code, n){
write('src/polyfill/unbuild.js', undent(code, 1)); write('src/polyfill/unbuild.js', undent(code, 1));
(function recurse(c){ (function recurse(c){
code = next(";require(function(module){", "})(require"); code = next(";USE(function(module){", "})(USE");
if(!code){ return } if(!code){ return }
var file = path(); var file = path();
if(!file){ return } if(!file){ return }
code = code.replace(/\bUSE\(/g, 'require(');
write(file, undent(code)); write(file, undent(code));
recurse(); recurse();
}()); }());

View File

@ -250,9 +250,9 @@ function ask(at, soul){
Gun.obj.del(at, 'ask'); // TODO: PERFORMANCE? More elegant way? Gun.obj.del(at, 'ask'); // TODO: PERFORMANCE? More elegant way?
} }
function ack(msg, ev){ 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(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 } if(at.put !== u){ return }
//at.ack = 0; //at.ack = 0;
at.on('in', { at.on('in', {
@ -263,6 +263,10 @@ function ack(msg, ev){
}) })
return; 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 } //if(/*!msg.gun &&*/ !get['.'] && get['#']){ at.ack = (at.ack + 1) || 1 }
//msg = obj_to(msg); //msg = obj_to(msg);
msg.gun = at.root; msg.gun = at.root;

View File

@ -39,7 +39,7 @@ function cache(key, back){
var cat = back._, next = cat.next, gun = back.chain(), at = gun._; var cat = back._, next = cat.next, gun = back.chain(), at = gun._;
if(!next){ next = cat.next = {} } if(!next){ next = cat.next = {} }
next[at.get = key] = gun; next[at.get = key] = gun;
if(cat.root === back){ if(cat.root === back){
at.soul = key; at.soul = key;
} else } else
if(cat.soul || cat.field || cat.has){ // TODO: Convert field to has! if(cat.soul || cat.field || cat.has){ // TODO: Convert field to has!

View File

@ -4,13 +4,13 @@ if(typeof window !== "undefined"){ root = window }
if(typeof global !== "undefined"){ root = global } if(typeof global !== "undefined"){ root = global }
root = root || {}; root = root || {};
var console = root.console || {log: function(){}}; var console = root.console || {log: function(){}};
function require(arg){ function USE(arg){
return arg.slice? require[resolve(arg)] : function(mod, path){ return arg.slice? USE[R(arg)] : function(mod, path){
arg(mod = {exports: {}}); arg(mod = {exports: {}});
require[resolve(path)] = mod.exports; USE[R(path)] = mod.exports;
} }
function resolve(path){ function R(p){
return path.split('/').slice(-1).toString().replace('.js',''); return p.split('/').slice(-1).toString().replace('.js','');
} }
} }
if(typeof module !== "undefined"){ var common = module } if(typeof module !== "undefined"){ var common = module }

View File

@ -204,7 +204,10 @@ function any(at, ev){
as.soul = (opt.uuid || cat.root._.opt.uuid || Gun.text.random)(); as.soul = (opt.uuid || cat.root._.opt.uuid || Gun.text.random)();
} else { } else {
//as.data = obj_put({}, as.gun._.get, as.data); //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 if(!as.soul){ // polyfill async uuid for SEA
as.ref.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback as.ref.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
@ -218,4 +221,5 @@ function any(at, ev){
} }
var obj = Gun.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map; 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 u, empty = {}, noop = function(){}, iife = function(fn,as){fn.call(as||empty)};
var node_ = Gun.node._;

View File

@ -101,7 +101,10 @@ Gun._ = { // some reserved key words, these are not the only ones.
} }
function merge(node, soul){ function merge(node, soul){
var ctx = this, cat = ctx.gun._, at = (cat.next || empty)[soul]; var ctx = this, cat = ctx.gun._, at = (cat.next || empty)[soul];
if(!at){ return } if(!at){
ctx.souls[soul] = false;
return
}
var msg = ctx.map[soul] = { var msg = ctx.map[soul] = {
put: node, put: node,
get: soul, get: soul,
@ -139,7 +142,6 @@ Gun._ = { // some reserved key words, these are not the only ones.
function map(msg, soul){ function map(msg, soul){
if(!msg.gun){ return } if(!msg.gun){ return }
msg.gun._.root._.stop = {}; msg.gun._.root._.stop = {};
//console.log("map ->", soul, msg.put);
(msg.gun._).on('in', msg); (msg.gun._).on('in', msg);
msg.gun._.root._.stop = {}; msg.gun._.root._.stop = {};
} }
@ -220,6 +222,6 @@ module.exports = Gun;
//console.log("<<<<<", msg.put); //console.log("<<<<<", msg.put);
to.next(msg); to.next(msg);
},1); },1);
}) });
});*/ });*/

View File

@ -15,8 +15,10 @@ Gun.chain.set = function(item, cb, opt){
item.get('_').get(function(at, ev){ item.get('_').get(function(at, ev){
if(!at.gun || !at.gun._.back){ return } if(!at.gun || !at.gun._.back){ return }
ev.off(); ev.off();
var soul = (at.put||{})['#'];
at = (at.gun._.back._); 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 + '"!')}) } 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); gun.put(Gun.obj.put(put, soul, Gun.val.rel.ify(soul)), cb, opt);
},{wait:0}); },{wait:0});