have unbuild function wrap to prevent scope leaks & allow RETURN hehehe so I can reject @bmatusiak 's lS change O:) O:) I love you you're a hero!

later with @bmatusiak check sea.then for '../gun.js' vs '../' vs ...
note: src/index -> core.js
TODO: something about WebRTC candidates hitting ack decrement limits?
This commit is contained in:
Mark Nadal 2024-01-17 10:13:00 -08:00
parent 5c52df2eee
commit 638c2c3c23
48 changed files with 216 additions and 78 deletions

6
gun.js
View File

@ -136,7 +136,7 @@
}; };
})(USE, './onto'); })(USE, './onto');
;USE(function(module){ ;(function(){ ;USE(function(module){
// TODO: BUG! Unbuild will make these globals... CHANGE unbuild to wrap files in a function. // TODO: BUG! Unbuild will make these globals... CHANGE unbuild to wrap files in a function.
// Book is a replacement for JS objects, maps, dictionaries. // Book is a replacement for JS objects, maps, dictionaries.
var sT = setTimeout, B = sT.Book || (sT.Book = function(text){ var sT = setTimeout, B = sT.Book || (sT.Book = function(text){
@ -348,7 +348,6 @@
} }
try{module.exports=B}catch(e){} try{module.exports=B}catch(e){}
}());//delete later.
})(USE, './book'); })(USE, './book');
;USE(function(module){ ;USE(function(module){
@ -716,7 +715,7 @@
tmp = keys.length; tmp = keys.length;
console.STAT && console.STAT(S, -(S - (S = +new Date)), 'got copied some'); console.STAT && console.STAT(S, -(S - (S = +new Date)), 'got copied some');
DBG && (DBG.ga = +new Date); DBG && (DBG.ga = +new Date);
root.on('in', {'@': to, '#': id, put: put, '%': (tmp? (id = text_rand(9)) : u), $: root.$, _: faith, DBG: DBG, FOO: 1}); root.on('in', {'@': to, '#': id, put: put, '%': (tmp? (id = text_rand(9)) : u), $: root.$, _: faith, DBG: DBG});
console.STAT && console.STAT(S, +new Date - S, 'got in'); console.STAT && console.STAT(S, +new Date - S, 'got in');
if(!tmp){ return } if(!tmp){ return }
setTimeout.turn(go); setTimeout.turn(go);
@ -1388,6 +1387,7 @@
var Gun = USE('./root'); var Gun = USE('./root');
USE('./shim'); USE('./shim');
USE('./onto'); USE('./onto');
USE('./book');
USE('./valid'); USE('./valid');
USE('./state'); USE('./state');
USE('./dup'); USE('./dup');

View File

@ -110,6 +110,7 @@ var undent = function(code, n){
if(rcode != code){ if(rcode != code){
console.log("unbuild:","update",file); console.log("unbuild:","update",file);
} }
code = ";(function(){\n"+code+"\n}());";
write(file, code); write(file, code);
recurse(); recurse();
}()); }());

View File

@ -1,3 +1,4 @@
;(function(){
var shim = require('./shim'); var shim = require('./shim');
var S = require('./settings'); var S = require('./settings');
@ -14,3 +15,4 @@
} }
module.exports = importGen; module.exports = importGen;
}());

View File

@ -1,3 +1,4 @@
;(function(){
require('./base64'); require('./base64');
// This is Array extended to have .toString(['utf8'|'hex'|'base64']) // This is Array extended to have .toString(['utf8'|'hex'|'base64'])
@ -23,3 +24,4 @@
} }
module.exports = SeaArray; module.exports = SeaArray;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var User = require('./user'), SEA = User.SEA, Gun = User.GUN, noop = function(){}; var User = require('./user'), SEA = User.SEA, Gun = User.GUN, noop = function(){};
// now that we have created a user, we want to authenticate them! // now that we have created a user, we want to authenticate them!
@ -7,6 +8,7 @@
var pass = (alias || (pair && !(pair.priv && pair.epriv))) && typeof args[1] === 'string' ? args[1] : null; var pass = (alias || (pair && !(pair.priv && pair.epriv))) && typeof args[1] === 'string' ? args[1] : null;
var cb = args.filter(arg => typeof arg === 'function')[0] || null; // cb now can stand anywhere, after alias/pass or pair var cb = args.filter(arg => typeof arg === 'function')[0] || null; // cb now can stand anywhere, after alias/pass or pair
var opt = args && args.length > 1 && typeof args[args.length-1] === 'object' ? args[args.length-1] : {}; // opt is always the last parameter which typeof === 'object' and stands after cb var opt = args && args.length > 1 && typeof args[args.length-1] === 'object' ? args[args.length-1] : {}; // opt is always the last parameter which typeof === 'object' and stands after cb
var retries = typeof opt.retries === 'number' ? opt.retries : 9;
var gun = this, cat = (gun._), root = gun.back(-1); var gun = this, cat = (gun._), root = gun.back(-1);
@ -30,6 +32,10 @@
var get = (act.list = (act.list||[]).concat(list||[])).shift(); var get = (act.list = (act.list||[]).concat(list||[])).shift();
if(u === get){ if(u === get){
if(act.name){ return act.err('Your user account is not published for dApps to access, please consider syncing it online, or allowing local access by adding your device as a peer.') } if(act.name){ return act.err('Your user account is not published for dApps to access, please consider syncing it online, or allowing local access by adding your device as a peer.') }
if(alias && retries--){
root.get('~@'+alias).once(act.a);
return;
}
return act.err('Wrong user or password.') return act.err('Wrong user or password.')
} }
root.get(get).once(act.a); root.get(get).once(act.a);
@ -74,7 +80,7 @@
if(SEA.window && ((gun.back('user')._).opt||opt).remember){ if(SEA.window && ((gun.back('user')._).opt||opt).remember){
// TODO: this needs to be modular. // TODO: this needs to be modular.
try{var sS = {}; try{var sS = {};
sS = window.sessionStorage; // TODO: FIX BUG putting on `.is`! sS = SEA.window.sessionStorage; // TODO: FIX BUG putting on `.is`!
sS.recall = true; sS.recall = true;
sS.pair = JSON.stringify(pair); // auth using pair is more reliable than alias/pass sS.pair = JSON.stringify(pair); // auth using pair is more reliable than alias/pass
}catch(e){} }catch(e){}
@ -155,3 +161,4 @@
return o; return o;
} }
}());

View File

@ -1,3 +1,4 @@
;(function(){
var u; var u;
if(u+''== typeof btoa){ if(u+''== typeof btoa){
@ -8,3 +9,4 @@
global.atob = function(data){ return Buffer.from(data, "base64").toString("binary") }; global.atob = function(data){ return Buffer.from(data, "base64").toString("binary") };
} }
}());

View File

@ -1,3 +1,4 @@
;(function(){
require('./base64'); require('./base64');
// This is Buffer implementation used in SEA. Functionality is mostly // This is Buffer implementation used in SEA. Functionality is mostly
@ -77,3 +78,4 @@
module.exports = SafeBuffer; module.exports = SafeBuffer;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
// This is to certify that a group of "certificants" can "put" anything at a group of matched "paths" to the certificate authority's graph // This is to certify that a group of "certificants" can "put" anything at a group of matched "paths" to the certificate authority's graph
@ -70,3 +71,4 @@
module.exports = SEA.certify; module.exports = SEA.certify;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var User = require('./user'), SEA = User.SEA, Gun = User.GUN, noop = function(){}; var User = require('./user'), SEA = User.SEA, Gun = User.GUN, noop = function(){};
@ -94,7 +95,7 @@
} }
if(SEA.window){ if(SEA.window){
try{var sS = {}; try{var sS = {};
sS = window.sessionStorage; sS = SEA.window.sessionStorage;
delete sS.recall; delete sS.recall;
delete sS.pair; delete sS.pair;
}catch(e){}; }catch(e){};
@ -102,3 +103,4 @@
return gun; return gun;
} }
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
var shim = require('./shim'); var shim = require('./shim');
@ -40,3 +41,4 @@
module.exports = SEA.decrypt; module.exports = SEA.decrypt;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
var shim = require('./shim'); var shim = require('./shim');
@ -38,3 +39,4 @@
module.exports = SEA.encrypt; module.exports = SEA.encrypt;
}());

View File

@ -1,12 +1,16 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
try{ if(SEA.window){ try{ if(SEA.window){
if(location.protocol.indexOf('s') < 0 if(location.protocol.indexOf('s') < 0
&& location.host.indexOf('localhost') < 0 && location.host.indexOf('localhost') < 0
&& ! /^127\.\d+\.\d+\.\d+$/.test(location.hostname) && ! /^127\.\d+\.\d+\.\d+$/.test(location.hostname)
&& location.protocol.indexOf('file:') < 0){ && location.protocol.indexOf('blob:') < 0
&& location.protocol.indexOf('file:') < 0
&& location.origin != 'null'){
console.warn('HTTPS needed for WebCrypto in SEA, redirecting...'); console.warn('HTTPS needed for WebCrypto in SEA, redirecting...');
location.protocol = 'https:'; // WebCrypto does NOT work without HTTPS! location.protocol = 'https:'; // WebCrypto does NOT work without HTTPS!
} }
} }catch(e){} } }catch(e){}
}());

View File

@ -1,6 +1,7 @@
;(function(){
var SEA = require('./sea'), S = require('./settings'), noop = function() {}, u; var SEA = require('./sea'), S = require('./settings'), noop = function() {}, u;
var Gun = (''+u != typeof window)? (window.Gun||{on:noop}) : require((''+u === typeof MODULE?'.':'')+'./gun', 1); var Gun = (SEA.window||'').GUN || require((''+u === typeof MODULE?'.':'')+'./gun', 1);
// After we have a GUN extension to make user registration/login easy, we then need to handle everything else. // After we have a GUN extension to make user registration/login easy, we then need to handle everything else.
// We do this with a GUN adapter, we first listen to when a gun instance is created (and when its options change) // We do this with a GUN adapter, we first listen to when a gun instance is created (and when its options change)
@ -66,7 +67,7 @@
check.any(eve, msg, val, key, soul, at, no, at.user||''); return; check.any(eve, msg, val, key, soul, at, no, at.user||''); return;
eve.to.next(msg); // not handled eve.to.next(msg); // not handled
} }
check.hash = function(eve, msg, val, key, soul, at, no){ check.hash = function(eve, msg, val, key, soul, at, no){ // mark unbuilt @i001962 's epic hex contrib!
SEA.work(val, null, function(data){ SEA.work(val, null, function(data){
function hexToBase64(hexStr) { function hexToBase64(hexStr) {
let base64 = ""; let base64 = "";
@ -246,3 +247,4 @@
// TODO: Potential bug? If pub/priv key starts with `-`? IDK how possible. // TODO: Potential bug? If pub/priv key starts with `-`? IDK how possible.
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
var shim = require('./shim'); var shim = require('./shim');
@ -71,3 +72,4 @@
module.exports = SEA.pair; module.exports = SEA.pair;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var User = require('./user'), SEA = User.SEA, Gun = User.GUN; var User = require('./user'), SEA = User.SEA, Gun = User.GUN;
User.prototype.recall = function(opt, cb){ User.prototype.recall = function(opt, cb){
@ -7,7 +8,7 @@
if(SEA.window){ if(SEA.window){
try{ try{
var sS = {}; var sS = {};
sS = window.sessionStorage; // TODO: FIX BUG putting on `.is`! sS = SEA.window.sessionStorage; // TODO: FIX BUG putting on `.is`!
if(sS){ if(sS){
(root._).opt.remember = true; (root._).opt.remember = true;
((gun.back('user')._).opt||opt).remember = true; ((gun.back('user')._).opt||opt).remember = true;
@ -25,3 +26,4 @@
return gun; return gun;
} }
}());

View File

@ -1,9 +1,11 @@
;(function(){
// Security, Encryption, and Authorization: SEA.js // Security, Encryption, and Authorization: SEA.js
// MANDATORY READING: https://gun.eco/explainers/data/security.html // MANDATORY READING: https://gun.eco/explainers/data/security.html
// IT IS IMPLEMENTED IN A POLYFILL/SHIM APPROACH. // IT IS IMPLEMENTED IN A POLYFILL/SHIM APPROACH.
// THIS IS AN EARLY ALPHA! // THIS IS AN EARLY ALPHA!
if(typeof self !== "undefined"){ module.window = self } // should be safe for at least browser/worker/nodejs, need to check other envs like RN etc.
if(typeof window !== "undefined"){ module.window = window } if(typeof window !== "undefined"){ module.window = window }
var tmp = module.window || module, u; var tmp = module.window || module, u;
@ -14,3 +16,4 @@
try{ if(u+'' !== typeof MODULE){ MODULE.exports = SEA } }catch(e){} try{ if(u+'' !== typeof MODULE){ MODULE.exports = SEA } }catch(e){}
module.exports = SEA; module.exports = SEA;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var shim = require('./shim'); var shim = require('./shim');
// Practical examples about usage found in tests. // Practical examples about usage found in tests.
@ -16,7 +17,7 @@
// For documentation see https://nodejs.org/api/buffer.html // For documentation see https://nodejs.org/api/buffer.html
SEA.Buffer = SEA.Buffer || require('./buffer'); SEA.Buffer = SEA.Buffer || require('./buffer');
// These SEA functions support now only Promises or // These SEA functions support now ony Promises or
// async/await (compatible) code, use those like Promises. // async/await (compatible) code, use those like Promises.
// //
// Creates a wrapper library around Web Crypto API // Creates a wrapper library around Web Crypto API
@ -45,7 +46,7 @@
// Obviously it is missing MANY necessary features. This is only an alpha release. // Obviously it is missing MANY necessary features. This is only an alpha release.
// Please experiment with it, audit what I've done so far, and complain about what needs to be added. // Please experiment with it, audit what I've done so far, and complain about what needs to be added.
// SEA should be a full suite that is easy and seamless to use. // SEA should be a full suite that is easy and seamless to use.
// Again, scroll near the top, where I provide an EXAMPLE of how to create a user and sign in. // Again, scroll naer the top, where I provide an EXAMPLE of how to create a user and sign in.
// Once logged in, the rest of the code you just read handled automatically signing/validating data. // Once logged in, the rest of the code you just read handled automatically signing/validating data.
// But all other behavior needs to be equally easy, like opinionated ways of // But all other behavior needs to be equally easy, like opinionated ways of
// Adding friends (trusted public keys), sending private messages, etc. // Adding friends (trusted public keys), sending private messages, etc.
@ -56,3 +57,4 @@
// -------------- END SEA MODULES -------------------- // -------------- END SEA MODULES --------------------
// -- BEGIN SEA+GUN MODULES: BUNDLED BY DEFAULT UNTIL OTHERS USE SEA ON OWN ------- // -- BEGIN SEA+GUN MODULES: BUNDLED BY DEFAULT UNTIL OTHERS USE SEA ON OWN -------
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
var shim = require('./shim'); var shim = require('./shim');
@ -51,3 +52,4 @@
module.exports = SEA.secret; module.exports = SEA.secret;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
var shim = require('./shim'); var shim = require('./shim');
@ -42,3 +43,4 @@
SEA.opt = s; SEA.opt = s;
module.exports = s module.exports = s
}());

View File

@ -1,3 +1,4 @@
;(function(){
// This internal func returns SHA-1 hashed data for KeyID generation // This internal func returns SHA-1 hashed data for KeyID generation
const __shim = require('./shim') const __shim = require('./shim')
@ -6,3 +7,4 @@
const sha1hash = (b) => ossl.digest({name: 'SHA-1'}, new ArrayBuffer(b)) const sha1hash = (b) => ossl.digest({name: 'SHA-1'}, new ArrayBuffer(b))
module.exports = sha1hash module.exports = sha1hash
}());

View File

@ -1,3 +1,4 @@
;(function(){
var shim = require('./shim'); var shim = require('./shim');
module.exports = async function(d, o){ module.exports = async function(d, o){
@ -6,3 +7,4 @@
return shim.Buffer.from(hash); return shim.Buffer.from(hash);
} }
}());

View File

@ -1,3 +1,4 @@
;(function(){
var User = require('./user'), SEA = User.SEA, Gun = User.GUN, noop = function(){}; var User = require('./user'), SEA = User.SEA, Gun = User.GUN, noop = function(){};
User.prototype.pair = function(){ User.prototype.pair = function(){
@ -134,3 +135,4 @@
*/ */
module.exports = User module.exports = User
}());

View File

@ -1,3 +1,4 @@
;(function(){
const SEA = require('./root') const SEA = require('./root')
const api = {Buffer: require('./buffer')} const api = {Buffer: require('./buffer')}
@ -15,10 +16,10 @@
})} })}
if(SEA.window){ if(SEA.window){
api.crypto = window.crypto || window.msCrypto api.crypto = SEA.window.crypto || SEA.window.msCrypto
api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle; api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle;
api.TextEncoder = window.TextEncoder; api.TextEncoder = SEA.window.TextEncoder;
api.TextDecoder = window.TextDecoder; api.TextDecoder = SEA.window.TextDecoder;
api.random = (len) => api.Buffer.from(api.crypto.getRandomValues(new Uint8Array(api.Buffer.alloc(len)))); api.random = (len) => api.Buffer.from(api.crypto.getRandomValues(new Uint8Array(api.Buffer.alloc(len))));
} }
if(!api.TextDecoder) if(!api.TextDecoder)
@ -45,3 +46,4 @@
module.exports = api module.exports = api
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
var shim = require('./shim'); var shim = require('./shim');
@ -42,3 +43,4 @@
module.exports = SEA.sign; module.exports = SEA.sign;
}());

View File

@ -1,5 +1,6 @@
;(function(){
var u, Gun = (''+u != typeof window)? (window.Gun||{chain:{}}) : require((''+u === typeof MODULE?'.':'')+'./gun', 1); var u, Gun = (''+u != typeof GUN)? (GUN||{chain:{}}) : require((''+u === typeof MODULE?'.':'')+'./gun', 1);
Gun.chain.then = function(cb, opt){ Gun.chain.then = function(cb, opt){
var gun = this, p = (new Promise(function(res, rej){ var gun = this, p = (new Promise(function(res, rej){
gun.once(res, opt); gun.once(res, opt);
@ -7,3 +8,4 @@
return cb? p.then(cb) : p; return cb? p.then(cb) : p;
} }
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./sea'), Gun, u; var SEA = require('./sea'), Gun, u;
if(SEA.window){ if(SEA.window){
@ -39,3 +40,4 @@
User.SEA = Gun.SEA = SEA; User.SEA = Gun.SEA = SEA;
module.exports = User; module.exports = User;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
var shim = require('./shim'); var shim = require('./shim');
@ -78,3 +79,4 @@
SEA.opt.fallback = 2; SEA.opt.fallback = 2;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var SEA = require('./root'); var SEA = require('./root');
var shim = require('./shim'); var shim = require('./shim');
@ -40,3 +41,4 @@
module.exports = SEA.work; module.exports = SEA.work;
}());

View File

@ -1,3 +1,4 @@
;(function(){
// request / response module, for asking and acking messages. // request / response module, for asking and acking messages.
require('./onto'); // depends upon onto! require('./onto'); // depends upon onto!
@ -25,3 +26,4 @@ module.exports = function ask(cb, as){
} }
var random = String.random || function(){ return Math.random().toString(36).slice(2) } var random = String.random || function(){ return Math.random().toString(36).slice(2) }
}());

View File

@ -1,3 +1,4 @@
;(function(){
var Gun = require('./root'); var Gun = require('./root');
Gun.chain.back = function(n, opt){ var tmp; Gun.chain.back = function(n, opt){ var tmp;
@ -38,3 +39,4 @@ Gun.chain.back = function(n, opt){ var tmp;
} }
var empty = {}, u; var empty = {}, u;
}());

View File

@ -1,4 +1,5 @@
;(function(){ ;(function(){
// TODO: BUG! Unbuild will make these globals... CHANGE unbuild to wrap files in a function. // TODO: BUG! Unbuild will make these globals... CHANGE unbuild to wrap files in a function.
// Book is a replacement for JS objects, maps, dictionaries. // Book is a replacement for JS objects, maps, dictionaries.
var sT = setTimeout, B = sT.Book || (sT.Book = function(text){ var sT = setTimeout, B = sT.Book || (sT.Book = function(text){
@ -210,5 +211,5 @@ function decord(t){
} }
try{module.exports=B}catch(e){} try{module.exports=B}catch(e){}
}());//delete later.
}());

View File

@ -1,3 +1,4 @@
;(function(){
// 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
@ -28,6 +29,7 @@ function output(msg){
if(at.lex){ Object.keys(at.lex).forEach(function(k){ tmp[k] = at.lex[k] }, tmp = msg.get = msg.get || {}) } if(at.lex){ Object.keys(at.lex).forEach(function(k){ tmp[k] = at.lex[k] }, tmp = msg.get = msg.get || {}) }
if(get['#'] || at.soul){ if(get['#'] || at.soul){
get['#'] = get['#'] || at.soul; get['#'] = get['#'] || at.soul;
//root.graph[get['#']] = root.graph[get['#']] || {_:{'#':get['#'],'>':{}}};
msg['#'] || (msg['#'] = text_rand(9)); // A3120 ? msg['#'] || (msg['#'] = text_rand(9)); // A3120 ?
back = (root.$.get(get['#'])._); back = (root.$.get(get['#'])._);
if(!(get = get['.'])){ // soul if(!(get = get['.'])){ // soul
@ -248,3 +250,4 @@ function ack(msg, ev){
var empty = {}, u, text_rand = String.random, valid = Gun.valid, obj_has = function(o, k){ return o && Object.prototype.hasOwnProperty.call(o, k) }, state = Gun.state, state_is = state.is, state_ify = state.ify; var empty = {}, u, text_rand = String.random, valid = Gun.valid, obj_has = function(o, k){ return o && Object.prototype.hasOwnProperty.call(o, k) }, state = Gun.state, state_is = state.is, state_ify = state.ify;
}());

10
src/core.js Normal file
View File

@ -0,0 +1,10 @@
;(function(){
var Gun = require('./root');
require('./chain');
require('./back');
require('./put');
require('./get');
module.exports = Gun;
}());

View File

@ -1,3 +1,4 @@
;(function(){
require('./shim'); require('./shim');
function Dup(opt){ function Dup(opt){
@ -11,6 +12,7 @@ function Dup(opt){
var it = s[id] || (s[id] = {}); var it = s[id] || (s[id] = {});
it.was = dup.now = +new Date; it.was = dup.now = +new Date;
if(!dup.to){ dup.to = setTimeout(dup.drop, opt.age + 9) } if(!dup.to){ dup.to = setTimeout(dup.drop, opt.age + 9) }
if(dt.ed){ dt.ed(id) }
return it; return it;
} }
dup.drop = function(age){ dup.drop = function(age){
@ -27,3 +29,4 @@ function Dup(opt){
} }
module.exports = Dup; module.exports = Dup;
}());

View File

@ -1,3 +1,4 @@
;(function(){
var Gun = require('./root'); var Gun = require('./root');
Gun.chain.get = function(key, cb, as){ Gun.chain.get = function(key, cb, as){
@ -110,6 +111,7 @@ function cache(key, back){
next[at.get = key] = at; next[at.get = key] = at;
if(back === cat.root.$){ if(back === cat.root.$){
at.soul = key; at.soul = key;
//at.put = {};
} else } else
if(cat.soul || cat.has){ if(cat.soul || cat.has){
at.has = key; at.has = key;
@ -154,3 +156,4 @@ function rid(at){
} }
var empty = {}, valid = Gun.valid, u; var empty = {}, valid = Gun.valid, u;
}());

View File

@ -1,8 +1,20 @@
;(function(){
var Gun = require('./root'); var Gun = require('./root');
require('./chain'); require('./shim');
require('./back'); require('./onto');
require('./put'); require('./book');
require('./get'); require('./valid');
require('./state');
require('./dup');
require('./ask');
require('./core');
require('./on');
require('./map');
require('./set');
require('./mesh');
require('./websocket');
require('./localStorage');
module.exports = Gun; module.exports = Gun;
}());

View File

@ -1,3 +1,4 @@
;(function(){
if(typeof Gun === 'undefined'){ return } if(typeof Gun === 'undefined'){ return }
@ -65,3 +66,4 @@ Gun.on('create', function lg(root){
}); });
}());

View File

@ -1,5 +1,6 @@
;(function(){
var Gun = require('./index'), next = Gun.chain.get.next; var Gun = require('./root'), next = Gun.chain.get.next;
Gun.chain.get.next = function(gun, lex){ var tmp; Gun.chain.get.next = function(gun, lex){ var tmp;
if(!Object.plain(lex)){ return (next||noop)(gun, lex) } if(!Object.plain(lex)){ return (next||noop)(gun, lex) }
if(tmp = ((tmp = lex['#'])||'')['='] || tmp){ return gun.get(tmp) } if(tmp = ((tmp = lex['#'])||'')['='] || tmp){ return gun.get(tmp) }
@ -42,3 +43,4 @@ function map(msg){ this.to.next(msg);
} }
var noop = function(){}, event = {stun: noop, off: noop}, u; var noop = function(){}, event = {stun: noop, off: noop}, u;
}());

View File

@ -1,3 +1,4 @@
;(function(){
require('./shim'); require('./shim');
@ -84,12 +85,17 @@ function Mesh(root){
if(tmp = msg.ok){ msg._.near = tmp['/'] } if(tmp = msg.ok){ msg._.near = tmp['/'] }
var S = +new Date; var S = +new Date;
DBG && (DBG.is = S); peer.SI = id; DBG && (DBG.is = S); peer.SI = id;
dup_track.ed = function(d){
if(id !== d){ return }
dup_track.ed = 0;
if(!(d = dup.s[id])){ return }
d.via = peer;
if(msg.get){ d.it = msg }
}
root.on('in', mesh.last = msg); root.on('in', mesh.last = msg);
//ECHO = msg.put || ECHO; !(msg.ok !== -3740) && mesh.say({ok: -3740, put: ECHO, '@': msg['#']}, peer);
DBG && (DBG.hd = +new Date); DBG && (DBG.hd = +new Date);
console.STAT && console.STAT(S, +new Date - S, msg.get? 'msg get' : msg.put? 'msg put' : 'msg'); console.STAT && console.STAT(S, +new Date - S, msg.get? 'msg get' : msg.put? 'msg put' : 'msg');
(tmp = dup_track(id)).via = peer; // don't dedup message ID till after, cause GUN has internal dedup check. dup_track(id); // in case 'in' does not call track.
if(msg.get){ tmp.it = msg }
if(ash){ dup_track(ash) } //dup.track(tmp+hash, true).it = it(msg); if(ash){ dup_track(ash) } //dup.track(tmp+hash, true).it = it(msg);
mesh.leap = mesh.last = null; // warning! mesh.leap could be buggy. mesh.leap = mesh.last = null; // warning! mesh.leap could be buggy.
} }
@ -129,12 +135,13 @@ function Mesh(root){
!loop && dup_track(id);//.it = it(msg); // track for 9 seconds, default. Earth<->Mars would need more! // always track, maybe move this to the 'after' logic if we split function. !loop && dup_track(id);//.it = it(msg); // track for 9 seconds, default. Earth<->Mars would need more! // always track, maybe move this to the 'after' logic if we split function.
//if(msg.put && (msg.err || (dup.s[id]||'').err)){ return false } // TODO: in theory we should not be able to stun a message, but for now going to check if it can help network performance preventing invalid data to relay. //if(msg.put && (msg.err || (dup.s[id]||'').err)){ return false } // TODO: in theory we should not be able to stun a message, but for now going to check if it can help network performance preventing invalid data to relay.
if(!(hash = msg['##']) && u !== msg.put && !meta.via && ack){ mesh.hash(msg, peer); return } // TODO: Should broadcasts be hashed? if(!(hash = msg['##']) && u !== msg.put && !meta.via && ack){ mesh.hash(msg, peer); return } // TODO: Should broadcasts be hashed?
if(!peer && ack){ peer = ((tmp = dup.s[ack]) && (tmp.via || ((tmp = tmp.it) && (tmp = tmp._) && tmp.via))) || ((tmp = mesh.last) && ack === tmp['#'] && mesh.leap) } // warning! mesh.leap could be buggy! mesh last check reduces this. if(!peer && ack){ peer = ((tmp = dup.s[ack]) && (tmp.via || ((tmp = tmp.it) && (tmp = tmp._) && tmp.via))) || ((tmp = mesh.last) && ack === tmp['#'] && mesh.leap) } // warning! mesh.leap could be buggy! mesh last check reduces this. // TODO: CLEAN UP THIS LINE NOW? `.it` should be reliable.
if(!peer && ack){ // still no peer, then ack daisy chain 'tunnel' got lost. if(!peer && ack){ // still no peer, then ack daisy chain 'tunnel' got lost.
if(dup.s[ack]){ return } // in dups but no peer hints that this was ack to ourself, ignore. if(dup.s[ack]){ return } // in dups but no peer hints that this was ack to ourself, ignore.
console.STAT && console.STAT(+new Date, ++SMIA, 'total no peer to ack to'); // TODO: Delete this now. Dropping lost ACKs is protocol fine now. console.STAT && console.STAT(+new Date, ++SMIA, 'total no peer to ack to'); // TODO: Delete this now. Dropping lost ACKs is protocol fine now.
return false; return false;
} // TODO: Temporary? If ack via trace has been lost, acks will go to all peers, which trashes browser bandwidth. Not relaying the ack will force sender to ask for ack again. Note, this is technically wrong for mesh behavior. } // TODO: Temporary? If ack via trace has been lost, acks will go to all peers, which trashes browser bandwidth. Not relaying the ack will force sender to ask for ack again. Note, this is technically wrong for mesh behavior.
if(ack && !msg.put && !hash && ((dup.s[ack]||'').it||'')['##']){ return false } // If we're saying 'not found' but a relay had data, do not bother sending our not found. // Is this correct, return false? // NOTE: ADD PANIC TEST FOR THIS!
if(!peer && mesh.way){ return mesh.way(msg) } if(!peer && mesh.way){ return mesh.way(msg) }
DBG && (DBG.yh = +new Date); DBG && (DBG.yh = +new Date);
if(!(raw = meta.raw)){ mesh.raw(msg, peer); return } if(!(raw = meta.raw)){ mesh.raw(msg, peer); return }
@ -195,7 +202,7 @@ function Mesh(root){
var hash = msg['##'], ack = msg['@']; var hash = msg['##'], ack = msg['@'];
if(hash && ack){ if(hash && ack){
if(!meta.via && dup_check(ack+hash)){ return false } // for our own out messages, memory & storage may ack the same thing, so dedup that. Tho if via another peer, we already tracked it upon hearing, so this will always trigger false positives, so don't do that! if(!meta.via && dup_check(ack+hash)){ return false } // for our own out messages, memory & storage may ack the same thing, so dedup that. Tho if via another peer, we already tracked it upon hearing, so this will always trigger false positives, so don't do that!
if((tmp = (dup.s[ack]||'').it) || ((tmp = mesh.last) && ack === tmp['#'])){ if(tmp = (dup.s[ack]||'').it){
if(hash === tmp['##']){ return false } // if ask has a matching hash, acking is optional. if(hash === tmp['##']){ return false } // if ask has a matching hash, acking is optional.
if(!tmp['##']){ tmp['##'] = hash } // if none, add our hash to ask so anyone we relay to can dedup. // NOTE: May only check against 1st ack chunk, 2nd+ won't know and still stream back to relaying peers which may then dedup. Any way to fix this wasted bandwidth? I guess force rate limiting breaking change, that asking peer has to ask for next lexical chunk. if(!tmp['##']){ tmp['##'] = hash } // if none, add our hash to ask so anyone we relay to can dedup. // NOTE: May only check against 1st ack chunk, 2nd+ won't know and still stream back to relaying peers which may then dedup. Any way to fix this wasted bandwidth? I guess force rate limiting breaking change, that asking peer has to ask for next lexical chunk.
} }
@ -344,3 +351,4 @@ function Mesh(root){
try{ module.exports = Mesh }catch(e){} try{ module.exports = Mesh }catch(e){}
}());

View File

@ -1,5 +1,6 @@
;(function(){
var Gun = require('./index'); var Gun = require('./root');
Gun.chain.on = function(tag, arg, eas, as){ // don't rewrite! Gun.chain.on = function(tag, arg, eas, as){ // don't rewrite!
var gun = this, cat = gun._, root = cat.root, act, off, id, tmp; var gun = this, cat = gun._, root = cat.root, act, off, id, tmp;
if(typeof tag === 'string'){ if(typeof tag === 'string'){
@ -103,6 +104,10 @@ Gun.chain.off = function(){
} }
} }
// TODO: delete cat.one[map.id]? // TODO: delete cat.one[map.id]?
if (tmp = cat.any) {
delete cat.any;
cat.any = {};
}
if(tmp = cat.ask){ if(tmp = cat.ask){
delete tmp[at.get]; delete tmp[at.get];
} }
@ -129,3 +134,4 @@ Gun.chain.off = function(){
} }
var empty = {}, noop = function(){}, u; var empty = {}, noop = function(){}, u;
}());

View File

@ -1,3 +1,4 @@
;(function(){
// 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){
@ -34,3 +35,4 @@ module.exports = function onto(tag, arg, as){
return tag; return tag;
}; };
}());

View File

@ -1,3 +1,4 @@
;(function(){
var Gun = require('./root'); var Gun = require('./root');
Gun.chain.put = function(data, cb, as){ // I rewrote it :) Gun.chain.put = function(data, cb, as){ // I rewrote it :)
@ -153,3 +154,4 @@ function check(d, tmp){ return ((d && (tmp = d.constructor) && tmp.name) || type
var u, empty = {}, noop = function(){}, turn = setTimeout.turn, valid = Gun.valid, state_ify = Gun.state.ify; var u, empty = {}, noop = function(){}, turn = setTimeout.turn, valid = Gun.valid, state_ify = Gun.state.ify;
var iife = function(fn,as){fn.call(as||empty)} var iife = function(fn,as){fn.call(as||empty)}
}());

View File

@ -1,3 +1,4 @@
;(function(){
function Gun(o){ function Gun(o){
@ -201,6 +202,9 @@ Gun.ask = require('./ask');
Gun.on.get = function(msg, gun){ Gun.on.get = function(msg, gun){
var root = gun._, get = msg.get, soul = get['#'], node = root.graph[soul], has = get['.']; var root = gun._, get = msg.get, soul = get['#'], node = root.graph[soul], has = get['.'];
var next = root.next || (root.next = {}), at = next[soul]; var next = root.next || (root.next = {}), at = next[soul];
// TODO: Azarattum bug, what is in graph is not same as what is in next. Fix!
// queue concurrent GETs? // queue concurrent GETs?
// TODO: consider tagging original message into dup for DAM. // TODO: consider tagging original message into dup for DAM.
// TODO: ^ above? In chat app, 12 messages resulted in same peer asking for `#user.pub` 12 times. (same with #user GET too, yipes!) // DAM note: This also resulted in 12 replies from 1 peer which all had same ##hash but none of them deduped because each get was different. // TODO: ^ above? In chat app, 12 messages resulted in same peer asking for `#user.pub` 12 times. (same with #user GET too, yipes!) // DAM note: This also resulted in 12 replies from 1 peer which all had same ##hash but none of them deduped because each get was different.
@ -221,10 +225,14 @@ Gun.ask = require('./ask');
}*/ }*/
var ctx = msg._||{}, DBG = ctx.DBG = msg.DBG; var ctx = msg._||{}, DBG = ctx.DBG = msg.DBG;
DBG && (DBG.g = +new Date); DBG && (DBG.g = +new Date);
//console.log("GET:", get, node, has); //console.log("GET:", get, node, has, at);
//if(!node && !at){ return root.on('get', msg) }
//if(has && node){ // replace 2 below lines to continue dev?
if(!node){ return root.on('get', msg) } if(!node){ return root.on('get', msg) }
if(has){ if(has){
if('string' != typeof has || u === node[has]){ return root.on('get', msg) } if('string' != typeof has || u === node[has]){
if(!((at||'').next||'')[has]){ root.on('get', msg); return }
}
node = state_ify({}, has, state_is(node, has), node[has], soul); node = state_ify({}, has, state_is(node, has), node[has], soul);
// If we have a key in-memory, do we really need to fetch? // If we have a key in-memory, do we really need to fetch?
// Maybe... in case the in-memory key we have is a local write // Maybe... in case the in-memory key we have is a local write
@ -302,3 +310,4 @@ module.exports = Gun;
;"Please do not remove welcome log unless you are paying for a monthly sponsorship, thanks!"; ;"Please do not remove welcome log unless you are paying for a monthly sponsorship, thanks!";
Gun.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!"); Gun.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!");
}());

View File

@ -1,5 +1,6 @@
;(function(){
var Gun = require('./index'); var Gun = require('./root');
Gun.chain.set = function(item, cb, opt){ Gun.chain.set = function(item, cb, opt){
var gun = this, root = gun.back(-1), soul, tmp; var gun = this, root = gun.back(-1), soul, tmp;
cb = cb || function(){}; cb = cb || function(){};
@ -21,3 +22,4 @@ Gun.chain.set = function(item, cb, opt){
return item; return item;
} }
}());

View File

@ -1,3 +1,4 @@
;(function(){
// Shim for generic javascript utilities. // Shim for generic javascript utilities.
String.random = function(l, c){ String.random = function(l, c){
@ -83,3 +84,4 @@ Object.keys = Object.keys || function(o){
}())})(); }())})();
}()); }());
}());

View File

@ -1,3 +1,4 @@
;(function(){
require('./shim'); require('./shim');
function State(){ function State(){
@ -26,3 +27,4 @@ State.ify = function(n, k, s, v, soul){ // put a key's state on a node.
} }
module.exports = State; module.exports = State;
}());

View File

@ -1,9 +1,10 @@
;(function(){
// Valid values are a subset of JSON: null, binary, number (!Infinity), text, // Valid values are a subset of JSON: null, binary, number (!Infinity), text,
// or a soul relation. Arrays need special algorithms to handle concurrency, // or a soul relation. Arrays need special algorithms to handle concurrency,
// so they are not supported directly. Use an extension that supports them if // so they are not supported directly. Use an extension that supports them if
// needed but research their problems first. // needed but research their problems first.
module.exports = function (v) { module.exports = function(v){
// "deletes", nulling out keys. // "deletes", nulling out keys.
return v === null || return v === null ||
"string" === typeof v || "string" === typeof v ||
@ -14,3 +15,4 @@ module.exports = function (v) {
(!!v && "string" == typeof v["#"] && Object.keys(v).length === 1 && v["#"]); (!!v && "string" == typeof v["#"] && Object.keys(v).length === 1 && v["#"]);
} }
}());

View File

@ -1,5 +1,6 @@
;(function(){
var Gun = require('./index'); var Gun = require('./root');
Gun.Mesh = require('./mesh'); Gun.Mesh = require('./mesh');
// TODO: resync upon reconnect online/offline // TODO: resync upon reconnect online/offline
@ -58,3 +59,4 @@ Gun.on('opt', function(root){
}); });
var noop = function(){}, u; var noop = function(){}, u;
}());