From 07b30ed602a767e227e489046b164bc602d26a57 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Tue, 9 Aug 2022 15:33:22 -0700 Subject: [PATCH] unbuild - PUBLISHED! 1238 --- gun.min.js | 2 +- sea.js | 9 +++++++- src/localStorage.js | 7 ++++--- src/mesh.js | 50 +++++++++++++++++++++++++++++++-------------- src/put.js | 7 +++++-- src/root.js | 18 ++++++++++------ src/shim.js | 18 ++++++++++------ src/websocket.js | 6 +++--- 8 files changed, 80 insertions(+), 37 deletions(-) diff --git a/gun.min.js b/gun.min.js index e162d1ba..83be9594 100644 --- a/gun.min.js +++ b/gun.min.js @@ -1 +1 @@ -!function(){function f(e,t){return t?require(e):e.slice?f[o(e)]:function(t,n){e(t={exports:{}}),f[o(n)]=t.exports};function o(t){return t.split("/").slice(-1).toString().replace(".js","")}}if("undefined"!=typeof module)var n=module;f(function(t){String.random=function(t,n){var e="";for(t=t||24,n=n||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";0"]||n["<"])||o===n["="]&&(e=n["*"]||n[">"],t.slice(0,(e||"").length)===e||o===n["*"]&&(o!==n[">"]&&o!==n["<"]?t>=n[">"]&&t<=n["<"]:o!==n[">"]&&t>=n[">"]||o!==n["<"]&&t<=n["<"])))},String.hash=function(t,n){if("string"==typeof t){if(n=n||0,!t.length)return n;for(var e=0,o=t.length;e"]||e;if(o)return"number"==typeof(o=o[n])?o:-1/0},n.ify=function(t,n,e,o,i){(t=t||{})._=t._||{},i&&(t._["#"]=i);var r=t._[">"]||(t._[">"]={});return void 0!==n&&"_"!==n&&("number"==typeof e&&(r[n]=e),void 0!==o&&(t[n]=o)),t},t.exports=n})(f,"./state"),f(function(t){f("./shim"),t.exports=function(o){var i={s:{}},r=i.s;o=o||{max:999,age:9e3},i.check=function(t){return!!r[t]&&n(t)};var n=i.track=function(t){var n=r[t]||(r[t]={});return n.was=i.now=+new Date,i.to||(i.to=setTimeout(i.drop,o.age+9)),n};return i.drop=function(e){i.to=null,i.now=+new Date;var t=Object.keys(r);console.STAT&&console.STAT(i.now,+new Date-i.now,"dup drop keys"),setTimeout.each(t,function(t){var n=r[t];n&&(e||o.age)>i.now-n.was||delete r[t]},0,99)},i}})(f,"./dup"),f(function(t){f("./onto"),t.exports=function(t,n){if(this.on){var e=(this.opt||{}).lack||9e3;if("function"!=typeof t){if(!t)return;var o=t["#"]||t,i=(this.tag||"")[o];if(!i)return;return n&&(i=this.on(o,n),clearTimeout(i.err),i.err=setTimeout(function(){i.off()},e)),!0}o=n&&n["#"]||a(9);if(!t)return o;var r=this.on(o,t,n);return r.err=r.err||setTimeout(function(){r.off(),r.next({err:"Error: No ACK yet.",lack:!0})},e),o}};var a=String.random||function(){return Math.random().toString(36).slice(2)}})(f,"./ask"),f(function(t){function c(t){return t instanceof c?(this._={$:this}).$:this instanceof c?c.create(this._={$:this,opt:t}):new c(t)}function u(t,a){var s=+new Date,n=t._||{},u=n.DBG=t.DBG,c=t["#"],f=g(9),l=Object.keys(a||"").sort(),h=((a||"")._||"")["#"],p=(l.length,t.$._.root),d=a===p.graph[h];console.STAT&&console.STAT(s,((u||n).gk=+new Date)-s,"got keys"),a&&function t(){s=+new Date;for(var n,e,o,i=0,r={};i<9&&(n=l[i++]);)O(r,n,m(a,n),a[n],h);l=l.slice(i),(e={})[h]=r,r=e,d&&((o=function(){}).ram=o.faith=!0),e=l.length,console.STAT&&console.STAT(s,-(s-(s=+new Date)),"got copied some"),u&&(u.ga=+new Date),p.on("in",{"@":c,"#":f,put:r,"%":e?f=g(9):T,$:p.$,_:o,DBG:u}),console.STAT&&console.STAT(s,+new Date-s,"got in"),e&&setTimeout.turn(t)}(),a||p.on("in",{"@":t["#"]})}c.is=function(t){return t instanceof c||t&&t._&&t===t._.$||!1},c.version=.202,(c.chain=c.prototype).toJSON=function(){},f("./shim"),c.valid=f("./valid"),c.state=f("./state"),c.on=f("./onto"),c.dup=f("./dup"),c.ask=f("./ask"),function(){function b(t){if(t)if(t.out!==b){var n,e=this.as,o=e.at||e,i=o.$,r=o.dup,a=t.DBG;if((n=t["#"])||(n=t["#"]=g(9)),!r.check(n)){if(r.track(n),n=t._,t._="function"==typeof n?n:function(){},t.$&&t.$===(t.$._||"").$||(t.$=i),t["@"]&&!t.put&&function(t){var n,e=t["@"]||"";if(!(n=e._))return;n.acks=(n.acks||0)+1,(n.err=t.err)&&(t["@"]=n["#"],$(n));n.stop||n.crack||(n.crack=n.match&&n.match.push(function(){u(n)}));u(n)}(t),!o.ask(t["@"],t)){if(a&&(a.u=+new Date),t.put)return void s(t);t.get&&c.on.get(t,i)}a&&(a.uc=+new Date),this.to.next(t),a&&(a.ua=+new Date),t.nts||t.NTS||(t.out=b,o.on("out",t),a&&(a.ue=+new Date))}}else this.to.next(t)}function s(a){if(a){var s=a._||"",t=s.root=((s.$=a.$||"")._||"").root;if(a["@"]&&s.faith&&!s.miss)return a.out=b,void t.on("out",a);s.latch=t.hatch,s.match=t.hatch=[];var u=a.put,c=s.DBG=a.DBG,f=+new Date;if(!u["#"]||!u["."]){c&&(c.p=f),s["#"]=a["#"],s.msg=a,s.all=0,s.stun=1;var l=Object.keys(u);console.STAT&&console.STAT(f,((c||s).pk=+new Date)-f,"put sort");var h,p,d,g,v,y,k,m=0;!function t(n){if(h!=m){if(!(d=l[h=m]))return console.STAT&&console.STAT(f,((c||s).pd=+new Date)-f,"put"),void $(s);(g=u[d])?(k=g._)?d!==k["#"]?y=w+j(d)+"soul not same.":(v=k[">"])||(y=w+j(d)+"no state."):y=w+j(d)+"no meta.":y=w+j(d)+"no node.",p=Object.keys(g||{})}if(y)return a.err=s.err=y,void $(s);var e,o=0;for(n=n||0;n++<9&&(e=p[o++]);)if("_"!==e){var i=g[e],r=v[e];if(T===r){y=w+j(e)+"on"+j(d)+"no state.";break}if(!S(i)){y=w+j(e)+"on"+j(d)+"bad "+typeof i+j(i);break}_(i,e,d,r,a)}(p=p.slice(o)).length?x(t):(++m,p=null,t(n))}()}}}function _(t,n,e,o,i){var r,a=i._||"",s=a.root,u=s.graph,c=u[e]||D,f=m(c,n,1),l=c[n],h=a.DBG;(r=console.STAT)&&(u[e]&&l||(r.has=(r.has||0)+1));var p=k();if(py?y:r),void(console.STAT&&console.STAT((h||a).Hf=+new Date,r,"future"));if((!(o":o},_:a})}}function e(t){var n;(n=(t._||"").DBG)&&(n.pa=+new Date,n.pm=n.pm||+new Date);var e,o=this.as,i=o.graph,r=t._,a=t.put,s=a["#"],u=a["."],c=a[":"],f=a[">"];t["#"];(e=r.msg)&&(e=e.put)&&(e=e[s])&&O(e,u,f,c,s),i[s]=O(i[s],u,f,c,s),(e=(o.next||"")[s])&&e.on("in",t),$(r),this.to.next(t)}function $(t,n){var e;if(!t.stop&&(t.err||!(0<--t.stun))&&(t.stop=1,e=t.root)){var o=t.match;o.end=1,o===e.hatch&&(!(o=t.latch)||o.end?delete e.hatch:e.hatch=o),t.hatch&&t.hatch(),setTimeout.each(t.match,function(t){t&&t()}),!(n=t.msg)||t.err||n.err||(n.out=b,t.root.on("out",n))}}function u(t){t&&t.root&&(t.stun||t.acks!==t.all||t.root.on("in",{"@":t["#"],err:t.err,ok:t.err?T:{"":1}}))}c.create=function(t){t.root=t.root||t,t.graph=t.graph||{},t.on=t.on||c.on,t.ask=t.ask||c.ask,t.dup=t.dup||c.dup();var n=t.$.opt(t.opt);return t.once||(t.on("in",b,t),t.on("out",b,t),t.on("put",e,t),c.on("create",t),t.on("create",t)),t.once=1,n},c.on.put=s,console.log("BEWARE: BETA VERSION OF NEW GUN! NOT ALL FEATURES FINISHED!");var w="Error: Invalid graph!",j=function(t){return" '"+(""+t).slice(0,9)+"...' "},v=JSON.stringify,y=2147483647,k=c.state}(),c.on.get=function(t,n){var e=n._,o=t.get,i=o["#"],r=e.graph[i],a=o["."],s=((e.next||(e.next={}))[i],(t._||{}).DBG=t.DBG);if(s&&(s.g=+new Date),!r)return e.on("get",t);if(a){if("string"!=typeof a||T===r[a])return e.on("get",t);r=O({},a,m(r,a),r[a],i)}r&&u(t,r),e.on("get",t)},c.on.get.ack=u,c.chain.opt=function(t){t=t||{};var e=this._,n=t.peers||t;return Object.plain(t)||(t={}),Object.plain(e.opt)||(e.opt=t),"string"==typeof n&&(n=[n]),n instanceof Array&&(Object.plain(e.opt.peers)||(e.opt.peers={}),n.forEach(function(t){var n={};n.id=n.url=t,e.opt.peers[t]=e.opt.peers[t]||n})),e.opt.peers=e.opt.peers||{},o(t,function t(n){var e=this[n];this&&this.hasOwnProperty(n)||"string"==typeof e||Object.empty(e)?this[n]=e:(!e||e.constructor===Object||e instanceof Array)&&o(e,t)}),c.on("opt",e),e.opt.uuid=e.opt.uuid||function(t){return c.state().toString(36).replace(".","")+String.random(t||12)},this};var T,e,o=function(t,n){Object.keys(t).forEach(n,t)},g=String.random,x=setTimeout.turn,S=c.valid,m=c.state.is,O=c.state.ify,D={};(c.log=function(){return c.log.off||e.log.apply(e,arguments),[].slice.call(arguments).join(" ")}).once=function(t,n,e){return(e=c.log.once)[t]=e[t]||0,e[t]++||c.log(n)},"undefined"!=typeof window&&((window.GUN=window.Gun=c).window=window);try{void 0!==n&&(n.exports=c)}catch(t){}((t.exports=c).window||{}).console=(c.window||{}).console||{log:function(){}},(e=console).only=function(t,n){return e.only.i&&t===e.only.i&&e.only.i++&&(e.log.apply(e,arguments)||n)},c.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!")})(f,"./root"),f(function(t){f("./root").chain.back=function(t,n){if(-1===(t=t||1)||1/0===t)return this._.root.$;if(1===t)return(this._.back||this._).$;var e=this._;if("string"==typeof t&&(t=t.split(".")),t instanceof Array){for(var o=0,i=t.length,r=e;o":v(r.graph[i.soul],n)}}),e))return}else{if(e=i.ask&&i.ask[""],(i.ask||(i.ask={}))[""]=i,p!==i.put&&(i.on("in",i),e))return;t.$=i.$}return r.ask(a,t),r.on("in",t)}if(n["."])return o.get?(t={get:{".":o.get},$:o.$},(i.ask||(i.ask={}))[o.get]=t.$._):t={get:o.lex?t.get:{},$:o.$},i.on("out",t);if(((o.ask||(o.ask={}))[""]=o).get)return n["."]=o.get,(i.ask||(i.ask={}))[o.get]=t.$._,i.on("out",t)}return i.on("out",t)}o.on("in",{put:o.put=p,$:o.$})},u.on.in=function(n,e){var t,o=(e=e||this.as).root,i=n.$||(n.$=e.$),r=(i||"")._||d,a=n.put||"",s=a["#"],u=a["."],c=p!==a["="]?a["="]:a[":"],f=a[">"]||-1/0;if(p!==n.put&&(p===a["#"]||p===a["."]||p===a[":"]&&p===a["="]||p===a[">"]))return g(a)?void e.on("in",{$:r.back.$,put:{"#":s=r.back.soul,".":u=r.has||r.get,"=":a,">":v(r.back.put,u)},via:n}):(s=((a||"")._||"")["#"])?(i=e.root.$.get(s),setTimeout.each(Object.keys(a).sort(),function(t){"_"!=t&&p!==(f=v(a,t))&&e.on("in",{$:i,put:{"#":s,".":t,"=":a[t],">":f},VIA:n})})):void console.log("chain not yet supported for",a,"...",n,e);(n.seen||"")[e.id]||(((n.seen||(n.seen=function(){}))[e.id]=e)!==r&&(Object.keys(n).forEach(function(t){a[t]=n[t]},a={}),a.get=e.get||a.get,e.soul||e.has?r.soul&&(a.$=e.$,a.$$=a.$$||r.$):a.$$$=a.$$$||e.$,n=a),h(n,e),(e.soul||n.$$)&&f>=v(o.graph[s],u)&&((a=o.$.get(s)._).put=y(a.put,u,f,c,s)),!r.soul&&f>=v(o.graph[s],u)&&(t=(o.$.get(s)._.next||"")[u])&&(t.put=c,"string"==typeof(a=g(c))&&(t.put=o.$.get(a)._.put||c)),this.to&&this.to.next(n),e.any&&setTimeout.each(Object.keys(e.any),function(t){(t=e.any[t])&&t(n)},0,99),e.echo&&setTimeout.each(Object.keys(e.echo),function(t){(t=e.echo[t])&&t.on("in",n)},0,99),((n.$$||"")._||r).soul&&(t=e.next)&&(t=t[u])&&(a={},Object.keys(n).forEach(function(t){a[t]=n[t]}),a.$=(n.$$||n.$).get(a.get=u),delete a.$$,delete a.$$$,t.on("in",a)),l(n,e))},u.on.link=l,u.on.unlink=h;var p,d={},s=String.random,g=u.valid,c=function(t,n){return t&&Object.prototype.hasOwnProperty.call(t,n)},n=u.state,v=n.is,y=n.ify})(f,"./chain"),f(function(t){var g=f("./root");function r(t){var n=this.at||this.on;if(!t||n.soul||n.has)return this.off();if(t=(t=(t=t.$||t)._||t).id){var e;n.map;if((e=this.seen||(this.seen={}))[t])return!0;e[t]=!0}}g.chain.get=function(t,n,e){var o,i;if("string"==typeof t){if(0==t.length)return(o=this.chain())._.err={err:g.log("0 length key!",t)},n&&n.call(o,o._.err),o;(o=((f=this._).next||a)[t])||(o=t&&function(t,n){var e=n._,o=e.next,i=n.chain()._;o=o||(e.next={});o[i.get=t]=i,n===e.root.$?i.soul=t:(e.soul||e.has)&&(i.has=t);return i}(t,this)),o=o&&o.$}else{if("function"==typeof t){if(!0===n)return function(t,n,e,o){var a,s=t._,u=0;if(a=s.soul||s.link)return n(a,o,s);if(s.jam)return s.jam.push([n,o]);s.jam=[[n,o]],t.get(function(t,n){if(!(v===t.put&&!s.root.opt.super&&(a=Object.keys(s.root.opt.peers).length)&&++u<=a)){n.rid(t);var e,o=(o=t.$)&&o._||{},i=0;for(a=s.jam,delete s.jam;e=a[i++];){var r=e[0];e=e[1],r&&r(o.link||o.soul||g.valid(t.put)||((t.put||{})._||{})["#"],e,t,n)}}},{out:{get:{".":!0}}})}(this,t,0,e),this;var c,f=(o=this)._,l=n||{},h=f.root;l.at=f,l.ok=t;var p={};function d(n,t,e){if(!d.stun&&(!(o=h.pass)||o[c])){var o,i=n.$._,r=(n.$$||"")._,a=(r||i).put,s=!i.has&&!i.soul,u={};if(!s&&v!==a||(a=v===((o=n.put)||"")["="]?v===(o||"")[":"]?o:o[":"]:o["="]),"string"==typeof(o=g.valid(a))&&(a=v===(o=h.$.get(o)._.put)?l.not?v:a:o),!l.not||v!==a){if(v===l.stun){if((o=h.stun)&&o.on&&(f.$.back(function(t){if(o.on(""+t.id,u={}),(u.run||0)u.run&&(u.stun&&!u.stun.end||(u.stun=o.on("stun"),u.stun=u.stun&&u.stun.last),u.stun&&!u.stun.end)))return void((u.stun.add||(u.stun.add={}))[c]=function(){d(n,t,1)});if(v===a&&(e=0),(o=h.hatch)&&!o.end&&v===l.hatch&&!e){if(p[i.$._.id])return;return p[i.$._.id]=1,void o.push(function(){d(n,t,1)})}p={}}if(h.pass){if(h.pass[c+i.id])return;h.pass[c+i.id]=1}l.on?l.ok.call(i.$,a,i.get,n,t||d):l.v2020?l.ok(n,t||d):(Object.keys(n).forEach(function(t){o[t]=n[t]},o={}),(n=o).put=a,l.ok.call(l.as,n,t||d))}}}return(((d.at=f).any||(f.any={}))[c=String.random(7)]=d).off=function(){d.stun=1,f.any&&delete f.any[c]},d.rid=r,d.id=l.run||++h.once,i=h.pass,(h.pass={})[c]=1,l.out=l.out||{get:{}},f.on("out",l.out),h.pass=i,o}if("number"==typeof t)return this.get(""+t,n,e);if("string"==typeof(i=s(t)))return this.get(i,n,e);(i=this.get.next)&&(o=i(this,t))}return o?n&&"function"==typeof n&&o.get(n,e):((o=this.chain())._.err={err:g.log("Invalid get request!",t)},n&&n.call(o,o._.err)),o};var v,a={},s=g.valid})(f,"./get"),f(function(t){var g=f("./root");function v(n,t){if(t){t=(t._||"").id||t;var e,o=n.root.stun||(n.root.stun={on:g.on}),i={};n.stun||(n.stun=o.on("stun",function(){})),(e=o.on(""+t))&&e.the.last.next(i),i.run>=n.run||o.on(""+t,function(t){if(n.stun.end)return this.off(),void this.to.next(t);t.run=t.run||n.run,t.stun=t.stun||n.stun})}}function s(n){if(n.err)s.end(n.stun,n.root);else if(!n.todo.length&&!n.end&&Object.empty(n.wait)){n.end=1;var t,e=n.$.back(-1)._,o=e.root,i=e.ask(function(t){o.on("ack",t),t.err&&g.log(t),++r>(n.acks||0)&&this.off(),n.ack&&n.ack(t,this)},n.opt),r=0,a=n.stun;(t=function(){a&&(s.end(a,o),setTimeout.each(Object.keys(a=a.add||""),function(t){(t=a[t])&&t()}))}).hatch=t,n.via._.on("out",{put:n.out=n.graph,opt:n.opt,"#":i,_:t})}}g.chain.put=function(t,n,p){var e=this,o=e._,i=o.root;(p=p||{}).root=o.root,p.run||(p.run=i.once),v(p,o.id),p.ack=p.ack||n,p.via=p.via||e,p.data=p.data||t,p.soul||(p.soul=o.soul||"string"==typeof n&&n);var d=p.state=p.state||g.state();return"function"==typeof t?t(function(t){p.data=t,e.put(void 0,void 0,p)}):p.soul?(p.$=i.$.get(p.soul),p.todo=[{it:p.data,ref:p.$}],p.turn=p.turn||r,p.ran=p.ran||s,function t(){var n,e,i,r,a,o=p.todo,s=o.pop(),u=s.it;s.ref&&s.ref._.id;if(v(p,s.ref),(r=s.todo)&&(u=u[e=r.pop()],r.length&&o.push(s)),e&&(o.path||(o.path=[])).push(e),!(n=k(u))&&!(a=g.is(u))){if(!Object.plain(u))return(p.ack||y).call(p,p.out={err:p.err=g.log("Invalid data: "+(u&&(r=u.constructor)&&r.name||typeof u)+" at "+(p.via.back(function(t){t.get&&r.push(t.get)},r=[])||r.join("."))+"."+(o.path||[]).join("."))}),void p.ran(p);for(var c=p.seen||(p.seen=[]),f=c.length;f--;)if(u===(r=c[f]).it){n=u=r.link;break}}if(e&&n)s.node=m(s.node,e,d,u);else{p.seen.push(i={it:u,link:{},todo:a?[]:Object.keys(u).sort().reverse(),path:(o.path||[]).slice(),up:s}),s.node=m(s.node,e,d,i.link),!a&&i.todo.length&&o.push(i);var l=p.seen.length;function h(t,n){var e=i.link["#"];n&&(n.off(),n.rid(t));var o=e||t.soul||(r=(t.$$||t.$)._||"").soul||r.link||((r=r.put||"")._||"")["#"]||r["#"]||((r=t.put||"")&&t.$$?r["#"]:(r["="]||r[":"]||"")["#"]);e||v(p,t.$),o||s.link["#"]?(o||(o=[],(t.$$||t.$).back(function(t){if(r=t.soul||t.link)return o.push(r);o.push(t.get)}),o=o.reverse().join("/")),i.link["#"]=o,a||(((p.graph||(p.graph={}))[o]=i.node||(i.node={_:{}}))._["#"]=o),delete p.wait[l],i.wait&&setTimeout.each(i.wait,function(t){t&&t()}),p.ran(p)):(s.wait||(s.wait=[])).push(function(){h(t,n)})}(p.wait||(p.wait={}))[l]="",r=(i.ref=a?u:e?s.ref.get(e):s.ref)._,(r=u&&(u._||"")["#"]||r.soul||r.link)?h({soul:r}):i.ref.get(h,{run:p.run,v2020:1,out:{get:{".":" "}}})}if(!o.length)return p.ran(p);p.turn(t)}()):function(n){var e,t=n.via._;n.via=n.via.back(function(t){if(t.soul||!t.get)return t.$;e=n.data,(n.data={})[t.get]=e}),n.via&&n.via._.soul||(n.via=t.root.$.get(((n.data||"")._||"")["#"]||t.$.back("opt.uuid")()));n.via.put(n.data,n.ack,n)}(p),e},s.end=function(t,n){t.end=y,t.the.to===t&&t===t.the.last&&delete n.stun,t.off()};var y=function(){},r=setTimeout.turn,k=g.valid,m=g.state.ify})(f,"./put"),f(function(t){var n=f("./root");f("./chain"),f("./back"),f("./put"),f("./get"),t.exports=n})(f,"./index"),f(function(t){var d=f("./index");d.chain.on=function(t,n,e,o){var i,r=this._;r.root;if("string"==typeof t)return n?(i=r.on(t,n,e||r,o),e&&e.$&&(e.subs||(e.subs=[])).push(i),this):r.on(t);var a=n;(a=!0===a?{change:!0}:a||{}).not=1,a.on=1;return this.get(t,a),this},d.chain.once=function(u,c){if(c=c||{},!u)return function(t,n,e){return d.log.once("valonce","Chainable val is experimental, its behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it."),(e=t.chain())._.nix=t.once(function(t,n){e._.on("in",this._)}),e._.lex=t._.lex,e}(this);var f,l=this._,h=l.root,p=(l.put,String.random(7));return this.get(function(t,n,e,o){var i=this,r=i._,a=r.one||(r.one={});function s(){r.has||r.soul||(r={put:t,get:n}),void 0===(f=r.put)&&(f=((e.$$||"")._||"").put),"string"==typeof d.valid(f)&&void 0===(f=h.$.get(f)._.put)||o.stun||""!==a[p]&&(a[p]="",(l.soul||l.has)&&o.off(),u.call(i,f,r.get))}o.stun||""!==a[p]&&(!0!==(f=d.valid(t))?"string"!=typeof f&&(clearTimeout(a[p]),a[p]=setTimeout(s,c.wait||99)):s())},{on:1}),this},d.chain.off=function(){var e,t=this._,o=t.back;if(o)return t.ack=0,(e=o.next)&&e[t.get]&&delete e[t.get],(e=o.ask)&&delete e[t.get],(e=o.put)&&delete e[t.get],(e=t.soul)&&delete o.root.graph[e],(e=t.map)&&Object.keys(e).forEach(function(t,n){(n=e[t]).link&&o.root.$.get(n.link).off()}),(e=t.next)&&Object.keys(e).forEach(function(t,n){e[t].$.off()}),t.on("off",{}),this}})(f,"./on"),f(function(t){var u=f("./index"),o=u.chain.get.next;function r(t){this.to.next(t);var n,e=this.as,o=t.$._,i=t.put;(o.soul||t.$$)&&((n=e.lex)&&!String.match(t.get||(i||"")["."],n["."]||n["#"]||n)||u.on.link(t,e))}u.chain.get.next=function(t,n){var e;return Object.plain(n)?(e=((e=n["#"])||"")["="]||e)?t.get(e):((e=t.chain()._).lex=n,t.on("in",function(t){String.match(t.get||(t.put||"")["."],n["."]||n["#"]||n)&&e.on("in",t),this.to.next(t)}),e.$):(o||c)(t,n)},u.chain.map=function(a,t,n){var e,s,o=this,i=o._;return Object.plain(a)&&(e=a["."]?a:{".":a},a=void 0),a?(u.log.once("mapfn","Map functions are experimental, their behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it."),s=o.chain(),o.map().on(function(t,n,e,o){var i=(a||c).call(this,t,n,e,o);if(void 0!==i){if(t===i)return s._.on("in",e);if(u.is(i))return s._.on("in",i._);var r={};Object.keys(e.put).forEach(function(t){r[t]=e.put[t]},r),r["="]=i,s._.on("in",{get:n,put:r})}})):(s=i.each)||((i.each=s=o.chain())._.lex=e||s._.lex||i.lex,s._.nix=o.back("nix"),o.on("in",r,s._)),s};var c=function(){}})(f,"./map"),f(function(t){var s=f("./index");s.chain.set=function(t,i,n){var e,r,a=this,o=a.back(-1);return i=i||function(){},(n=n||{}).item=n.item||t,(e=((t||"")._||"")["#"])&&((t={})["#"]=e),"string"==typeof(r=s.valid(t))?a.get(e=r).put(t,i,n):s.is(t)?(a.put(function(o){t.get(function(t,n,e){if(!t)return i.call(a,{err:s.log('Only a node can be linked! Not "'+e.put+'"!')});(r={})[t]={"#":t},o(r)},!0)}),t):(Object.plain(t)&&(t=o.get(e=a.back("opt.uuid")()).put(t)),a.get(e||o.back("opt.uuid")(7)).put(t,i,n))}})(f,"./set"),f(function(t){f("./shim");var j;try{t.exports=function(u){var g=function(){},v=u.opt||{};v.log=v.log||console.log,v.gap=v.gap||v.wait||0,v.max=v.max||.3*(v.memory?999*v.memory*999:3e8),v.pack=v.pack||.01*v.max*.01,v.puff=v.puff||9;var y=setTimeout.turn||setTimeout,i=JSON.parseAsync||function(t,n,e){try{n(void 0,JSON.parse(t,e))}catch(t){n(t)}},k=JSON.stringifyAsync||function(t,n,e,o){try{n(void 0,JSON.stringify(t,e,o))}catch(t){n(t)}},m=u.dup,b=m.check,_=m.track,s=(new Date,g.hear=function(t,a){if(t){if(v.max<=t.length)return g.say({dam:"!",err:"Message too big!"},a);g===this&&(s.d+=t.length||0,++s.c);var n,e=a.SH=+new Date,o=t[0];return"["===o?(i(t,function(t,i){if(t||!i)return g.say({dam:"!",err:"DAM JSON parse error."},a);console.STAT&&console.STAT(+new Date,i.length,"# on hear batch");var r=v.puff;!function t(){for(var n,e=+new Date,o=0;o<"])&&"string"==typeof r&&r.slice(0,99).split(",").forEach(function(t){this[t]=1},t._.yo={}),r=t.dam)return(r=g.hear[r])&&r(t,n,u),void _(o);e=+new Date;s&&(s.is=e),n.SI=o,u.on("in",g.last=t),s&&(s.hd=+new Date),console.STAT&&console.STAT(e,+new Date-e,t.get?"msg get":t.put?"msg put":"msg"),(r=_(o)).via=n,t.get&&(r.it=t),a&&_(a),g.leap=g.last=null}},s.c=s.d=0,function(){var h,p=0;function t(t,n){var e;return n instanceof Object?(Object.keys(n).sort().forEach(o,{to:e={},on:n}),e):n}function o(t){this.to[t]=this.on[t]}g.hash=function(i,r){var a,s,u,c=+new Date;k(i.put,function t(n,e){var o=(s=s||(u=e||"")).slice(0,32768);a=String.hash(o,a),(s=s.slice(32768))?y(t,0):(console.STAT&&console.STAT(c,+new Date-c,"say json+hash"),i._.$put=u,i["##"]=a,d(i,r),delete i._.$put)},t)};var d=g.say=function(r,t){var n;if((n=this)&&(n=n.to)&&n.next&&n.next(r),!r)return!1;var e,a,s=r["@"],u=r._||(r._=function(){}),o=r.DBG,i=+new Date;if(u.y=u.y||i,t||o&&(o.y=i),(e=r["#"])||(e=r["#"]=String.random(9)),h||_(e),r.put&&(r.err||(m.s[e]||"").err))return!1;if(r["##"]||j===r.put||u.via||!s){if(!t&&s&&(t=(n=m.s[s])&&(n.via||(n=n.it)&&(n=n._)&&n.via)||(n=g.last)&&s===n["#"]&&g.leap),!t&&s){if(m.s[s])return;return console.STAT&&console.STAT(+new Date,++p,"total no peer to ack to"),!1}if(!t&&g.way)return g.way(r);if(o&&(o.yh=+new Date),a=u.raw){if(o&&(o.yr=+new Date),!t||!t.id){if(!Object.plain(t||v.peers))return!1;i=+new Date,v.puff;var c=v.peers,f=Object.keys(t||v.peers||{});return console.STAT&&console.STAT(i,+new Date-i,"peer keys"),void function t(){var n=+new Date;h=1;var e=u.raw;u.raw=a;for(var o,i=0;i<9&&(o=(f||"")[i++]);)(o=c[o])&&d(r,o);u.raw=e,h=0,f=f.slice(i),console.STAT&&console.STAT(n,+new Date-n,"say loop"),f.length&&(y(t,0),s&&_(s))}()}if(!t.wire&&g.wire&&g.wire(t),e!==t.last){if(t.last=e,t===u.via)return!1;if((n=u.yo)&&(n[t.url]||n[t.pid]||n[t.id]))return!1;if(console.STAT&&console.STAT(i,((o||u).yp=+new Date)-(u.y||i),"say prep"),!h&&s&&_(s),t.batch){if(t.tail=(n=t.tail||0)+a.length,t.tail<=v.pack)return void(t.batch+=(n?",":"")+a);$(t)}t.batch="[";var l=+new Date;setTimeout(function(){console.STAT&&console.STAT(l,+new Date-l,"0ms TO"),$(t)},v.gap),w(a,t),console.STAT&&s===t.SI&&console.STAT(i,+new Date-t.SH,"say ack")}}else g.raw(r,t)}else g.hash(r,t)};g.say.c=g.say.d=0,g.raw=function(e,o){if(!e)return"";var i,r,a=e._||{};if(r=a.raw)return r;if("string"==typeof e)return e;var t=e["##"],n=e["@"];if(t&&n){if(!a.via&&b(n+t))return!1;if((r=(m.s[n]||"").it)||(r=g.last)&&n===r["#"]){if(t===r["##"])return!1;r["##"]||(r["##"]=t)}}if(!e.dam){var s=0,u=[];for(var c in r=v.peers){var f=r[c];if(u.push(f.url||f.pid||f.id),6<++s)break}1<"]=u.join())}if(i=a.$put)return r={},Object.keys(e).forEach(function(t){r[t]=e[t]}),r.put=":])([:",void k(r,function(t,n){if(!t){var e=+new Date;r=n.indexOf('"put":":])([:"'),l(j,n=n.slice(0,r+6)+i+n.slice(r+14)),console.STAT&&console.STAT(e,+new Date-e,"say slice")}});function l(t,n){t||(a.raw=n,d(e,o))}k(e,l)}}(),g.hi=function(n){var t=n.wire||{};n.id?v.peers[n.url||n.id]=n:(t=n.id=n.id||String.random(9),g.say({dam:"?",pid:u.opt.pid},v.peers[t]=n),delete m.s[n.last]),n.met=n.met||+new Date,t.hied||u.on(t.hied="hi",n),t=n.queue,n.queue=[],setTimeout.each(t||[],function(t){w(t,n)},0,9)},g.bye=function(t){u.on("bye",t);var n=+new Date;n-=t.met||n,g.bye.time=((g.bye.time||n)+n)/2},g.hear["!"]=function(t,n){v.log("Error:",t.err)},g.hear["?"]=function(t,n){t.pid&&(n.pid||(n.pid=t.pid),t["@"])||(g.say({dam:"?",pid:v.pid,"@":t["#"]},n),delete m.s[n.last])},u.on("create",function(t){t.opt.pid=t.opt.pid||String.random(9),this.to.next(t),t.on("out",g.say)}),u.on("bye",function(t,n){t=v.peers[t.id||t]||t,this.to.next(t),t.bye?t.bye():(n=t.wire)&&n.close&&n.close(),delete v.peers[t.id],t.wire=null});var o={};return u.on("bye",function(t,n){this.to.next(t),(n=console.STAT)&&(n.peers=(n.peers||0)-1),(n=t.url)&&(o[n]=!0,setTimeout(function(){delete o[n]},v.lack||9e3))}),u.on("hi",function(n,e){this.to.next(n),(e=console.STAT)&&(e.peers=(e.peers||0)+1),(e=n.url)&&o[e]&&(delete o[e],v.super||setTimeout.each(Object.keys(u.next),function(t){u.next[t];(e={})[t]=u.graph[t],e=String.hash(e),g.say({"##":e,get:{"#":t}},n)}))}),g}}catch(t){}})(f,"./mesh"),f(function(t){var c=f("../index");c.Mesh=f("./mesh"),c.on("opt",function(t){if(this.to.next(t),!t.once){var o=t.opt;if(!1!==o.WebSocket){var n=c.window||{},e=o.WebSocket||n.WebSocket||n.webkitWebSocket||n.mozWebSocket;if(e){o.WebSocket=e;var i=o.mesh=o.mesh||c.Mesh(t);i.wire||o.wire;i.wire=o.wire=s,setTimeout(function(){o.super||t.on("out",{dam:"hi"})},1);var r=1998,a=""+void 0!=typeof document&&document}}}function s(n){try{if(!n||!n.url)return e&&e(n);var t=n.url.replace(/^http/,"ws"),e=n.wire=new o.WebSocket(t);return e.onclose=function(){o.mesh.bye(n),u(n)},e.onerror=function(t){u(n)},e.onopen=function(){o.mesh.hi(n)},e.onmessage=function(t){t&&o.mesh.hear(t.data||t,n)},e}catch(t){}}function u(n){clearTimeout(n.defer),a&&n.retry<=0||(n.retry=(n.retry||o.retry+1||60)-(-n.tried+(n.tried=+new Date)<4*r?1:0),n.defer=setTimeout(function t(){if(a&&a.hidden)return setTimeout(t,r);s(n)},r))}})})(f,"./websocket"),f(function(t){if("undefined"!=typeof Gun){var o;try{o=(Gun.window||function(){}).localStorage}catch(t){}o||(Gun.log("Warning: No localStorage exists to persist data to!"),o={setItem:function(t,n){this[t]=n},removeItem:function(t){delete this[t]},getItem:function(t){return this[t]}}),Gun.on("create",function n(e){this.to.next(e);var r,i,a=e.opt,s=(e.graph,[]);if(!1!==a.localStorage){a.prefix=a.file||"gun/";try{r=n[a.prefix]=n[a.prefix]||JSON.parse(o.getItem(a.prefix))||{}}catch(t){r=n[a.prefix]={}}e.on("get",function(t){this.to.next(t);var n,e,o,i=t.get;i&&(n=i["#"])&&((e=r[n]||void 0)&&(o=i["."])&&!Object.plain(o)&&(e=Gun.state.ify({},o,Gun.state.is(e,o),e[o],n)),Gun.on.get.ack(t,e))}),e.on("put",function(t){this.to.next(t);var n=t.put,e=n["#"],o=n["."];r[e]=Gun.state.ify(r[e],o,n[">"],n[":"],e),t["@"]||s.push(t["#"]),i=i||setTimeout(u,a.wait||1)})}function u(){var n,t=s;clearTimeout(i),i=!1,s=[];try{o.setItem(a.prefix,JSON.stringify(r))}catch(t){Gun.log((n=t||"localStorage failure")+" Consider using GUN's IndexedDB plugin for RAD for more storage space, https://gun.eco/docs/RAD#install"),e.on("localStorage:error",{err:n,get:a.prefix,put:r})}(n||Object.empty(a.peers))&&setTimeout.each(t,function(t){e.on("in",{"@":t,err:n,ok:0})})}})}})(f,"./localStorage")}(),function(){if(""+_!=typeof Gun){function l(t){console.log("Warning! Deprecated internal utility will break in next version:",t)}var n,h,p=Gun;p.fn=p.fn||{is:function(t){return l("fn"),!!t&&"function"==typeof t}},p.bi=p.bi||{is:function(t){return l("bi"),t instanceof Boolean||"boolean"==typeof t}},p.num=p.num||{is:function(t){return l("num"),!d(t)&&(0<=t-parseFloat(t)+1||1/0===t||-1/0===t)}},p.text=p.text||{is:function(t){return l("text"),"string"==typeof t}},p.text.ify=p.text.ify||function(t){return l("text.ify"),p.text.is(t)?t:"undefined"!=typeof JSON?JSON.stringify(t):t&&t.toString?t.toString():t},p.text.random=p.text.random||function(t,n){l("text.random");var e="";for(t=t||24,n=n||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";0"]||n["<"])||o===n["="]&&(e=n["*"]||n[">"]||n["<"],t.slice(0,(e||"").length)===e||o===n["*"]&&(o!==n[">"]&&o!==n["<"]?t>=n[">"]&&t<=n["<"]:o!==n[">"]&&t>=n[">"]||o!==n["<"]&&t<=n["<"])))},p.text.hash=p.text.hash||function(t,n){if(l("text.hash"),"string"==typeof t){if(n=n||0,!t.length)return n;for(var e=0,o=t.length;e"]||n["<"])||o===n["="]&&(e=n["*"]||n[">"],t.slice(0,(e||"").length)===e||o===n["*"]&&(o!==n[">"]&&o!==n["<"]?t>=n[">"]&&t<=n["<"]:o!==n[">"]&&t>=n[">"]||o!==n["<"]&&t<=n["<"])))},String.hash=function(t,n){if("string"==typeof t){if(n=n||0,!t.length)return n;for(var e=0,o=t.length;e=s.now()-i&&r++<3333?t():a(function(){i=s.now(),t()},r=0)},g=setTimeout,f=(c=g.turn=g.turn||function(t){1==f.push(t)&&l(p)}).s=[],l=g.poll,h=0,p=function(){(u=f[h++])&&u(),h!=f.length&&99!=h||(f=c.s=f.slice(h),h=0),f.length&&l(p)},g=setTimeout,v=g.turn,(g.each=g.each||function(r,a,s,u){u=u||9,function t(n,e,o){if(e=(n=(r||[]).splice(0,u)).length){for(var i=0;i"]||e;if(e)return"number"==typeof(e=e[n])?e:-1/0},n.ify=function(t,n,e,o,i){(t=t||{})._=t._||{},i&&(t._["#"]=i);i=t._[">"]||(t._[">"]={});return void 0!==n&&"_"!==n&&("number"==typeof e&&(i[n]=e),void 0!==o&&(t[n]=o)),t},t.exports=n})(T,"./state"),T(function(t){T("./shim"),t.exports=function(o){var i={s:{}},r=i.s;o=o||{max:999,age:9e3},i.check=function(t){return!!r[t]&&n(t)};var n=i.track=function(t){t=r[t]||(r[t]={});return t.was=i.now=+new Date,i.to||(i.to=setTimeout(i.drop,o.age+9)),t};return i.drop=function(e){i.to=null,i.now=+new Date;var t=Object.keys(r);console.STAT&&console.STAT(i.now,+new Date-i.now,"dup drop keys"),setTimeout.each(t,function(t){var n=r[t];n&&(e||o.age)>i.now-n.was||delete r[t]},0,99)},i}})(T,"./dup"),T(function(t){T("./onto"),t.exports=function(t,n){if(this.on){var e=(this.opt||{}).lack||9e3;if("function"!=typeof t){if(!t)return;var o=t["#"]||t,i=(this.tag||"")[o];return i?(n&&(i=this.on(o,n),clearTimeout(i.err),i.err=setTimeout(function(){i.off()},e)),!0):void 0}o=n&&n["#"]||a(9);if(!t)return o;var r=this.on(o,t,n);return r.err=r.err||setTimeout(function(){r.off(),r.next({err:"Error: No ACK yet.",lack:!0})},e),o}};var a=String.random||function(){return Math.random().toString(36).slice(2)}})(T,"./ask"),T(function(t){function f(t){return t instanceof f?(this._={$:this}).$:this instanceof f?f.create(this._={$:this,opt:t}):new f(t)}function a(t,a){var s=+new Date,n=t._||{},u=n.DBG=t.DBG,c=t["#"],f=g(9),l=Object.keys(a||"").sort(),h=((a||"")._||"")["#"],p=(l.length,t.$._.root),d=a===p.graph[h];console.STAT&&console.STAT(s,((u||n).gk=+new Date)-s,"got keys"),a&&function t(){s=+new Date;for(var n,e,o,i=0,r={};i<9&&(n=l[i++]);)v(r,n,G(a,n),a[n],h);l=l.slice(i),(e={})[h]=r,r=e,d&&((o=function(){}).ram=o.faith=!0),e=l.length,console.STAT&&console.STAT(s,-(s-(s=+new Date)),"got copied some"),u&&(u.ga=+new Date),p.on("in",{"@":c,"#":f,put:r,"%":e?f=g(9):O,$:p.$,_:o,DBG:u}),console.STAT&&console.STAT(s,+new Date-s,"got in"),e&&setTimeout.turn(t)}(),a||p.on("in",{"@":t["#"]})}f.is=function(t){return t instanceof f||t&&t._&&t===t._.$||!1},f.version=.202,(f.chain=f.prototype).toJSON=function(){},T("./shim"),f.valid=T("./valid"),f.state=T("./state"),f.on=T("./onto"),f.dup=T("./dup"),f.ask=T("./ask"),function(){function b(t){if(t)if(t.out!==b){var n,e,o=this.as,i=o.at||o,r=i.$,a=i.dup,s=t.DBG;if((e=t["#"])||(e=t["#"]=g(9)),!a.check(e)){if(a.track(e),e=t._,t._="function"==typeof e?e:function(){},t.$&&t.$===(t.$._||"").$||(t.$=r),t["@"]&&!t.put&&(a=(o=t)["@"]||"",(n=a._)?(n.acks=(n.acks||0)+1,(n.err=o.err)&&(o["@"]=n["#"],_(n)),n.ok=o.ok||n.ok,n.stop||n.crack||(n.crack=n.match&&n.match.push(function(){c(n)})),c(n)):(e=(e=(e=o.$)&&(e=e._)&&(e=e.root)&&e.dup).check(a))&&(o["@"]=e["#"]||o["@"])),!i.ask(t["@"],t)){if(s&&(s.u=+new Date),t.put)return void u(t);t.get&&f.on.get(t,r)}s&&(s.uc=+new Date),this.to.next(t),s&&(s.ua=+new Date),t.nts||t.NTS||(t.out=b,i.on("out",t),s&&(s.ue=+new Date))}}else this.to.next(t)}function u(a){if(a){var s=a._||"",t=s.root=((s.$=a.$||"")._||"").root;if(a["@"]&&s.faith&&!s.miss)return a.out=b,void t.on("out",a);s.latch=t.hatch,s.match=t.hatch=[];var u,c,f,l,h,p,d,g,v,y=a.put,k=s.DBG=a.DBG,m=+new Date;n=n||m,y["#"]&&y["."]||(k&&(k.p=m),s["#"]=a["#"],s.msg=a,s.all=0,s.stun=1,u=Object.keys(y),console.STAT&&console.STAT(m,((k||s).pk=+new Date)-m,"put sort"),c=0,function t(n){if(f!=c){if(!(h=u[f=c]))return console.STAT&&console.STAT(m,((k||s).pd=+new Date)-m,"put"),void _(s);(p=y[h])?(v=p._)?h!==v["#"]?g=w+$(h)+"soul not same.":(d=v[">"])||(g=w+$(h)+"no state."):g=w+$(h)+"no meta.":g=w+$(h)+"no node.",l=Object.keys(p||{})}if(g)return a.err=s.err=g,void _(s);var e,o=0;for(n=n||0;n++<9&&(e=l[o++]);)if("_"!==e){var i=p[e],r=d[e];if(O===r){g=w+$(e)+"on"+$(h)+"no state.";break}if(!A(i)){g=w+$(e)+"on"+$(h)+"bad "+typeof i+$(i);break}!function t(n,e,o,i,r){var a=r._||"",s=a.root,u=s.graph;var c=u[o]||N,f=G(c,e,1),l=c[e];var h=a.DBG;(c=console.STAT)&&(u[o]&&l||(c.has=(c.has||0)+1));u=x();if(uj?j:c),void(console.STAT&&console.STAT((h||a).Hf=+new Date,c,"future"));if(i":i},ok:r.ok,_:a})}(i,e,h,r,a),++S}(l=l.slice(o)).length?D(t):(++c,l=null,t(n))}())}}function e(t){(u=(t._||"").DBG)&&(u.pa=+new Date,u.pm=u.pm||+new Date);var n=this.as,e=n.graph,o=t._,i=t.put,r=i["#"],a=i["."],s=i[":"],u=i[">"];t["#"];(i=o.msg)&&(i=i.put)&&(i=i[r])&&v(i,a,u,s,r),e[r]=v(e[r],a,u,s,r),(i=(n.next||"")[r])&&i.on("in",t),_(o),this.to.next(t)}function _(t,n){var e,o;t.stop||!t.err&&0<--t.stun||(t.stop=1,(e=t.root)&&((o=t.match).end=1,o===e.hatch&&(!(o=t.latch)||o.end?delete e.hatch:e.hatch=o),t.hatch&&t.hatch(),setTimeout.each(t.match,function(t){t&&t()}),!(n=t.msg)||t.err||n.err||(n.out=b,t.root.on("out",n),i())))}function c(t){t&&t.root&&(t.stun||t.acks!==t.all||t.root.on("in",{"@":t["#"],err:t.err,ok:t.err?O:t.ok||{"":1}}))}f.create=function(t){t.root=t.root||t,t.graph=t.graph||{},t.on=t.on||f.on,t.ask=t.ask||f.ask,t.dup=t.dup||f.dup();var n=t.$.opt(t.opt);return t.once||(t.on("in",b,t),t.on("out",b,t),t.on("put",e,t),f.on("create",t),t.on("create",t)),t.once=1,n},f.on.put=u;var n,w="Error: Invalid graph!",$=function(t){return" '"+(""+t).slice(0,9)+"...' "},T=JSON.stringify,j=2147483647,x=f.state,S=0,i=function(){999":v(r.graph[i.soul],n)}}),e))return}else{if(e=i.ask&&i.ask[""],(i.ask||(i.ask={}))[""]=i,p!==i.put&&(i.on("in",i),e))return;t.$=i.$}return r.ask(a,t),r.on("in",t)}if(n["."])return o.get?(t={get:{".":o.get},$:o.$},(i.ask||(i.ask={}))[o.get]=t.$._):t={get:o.lex?t.get:{},$:o.$},i.on("out",t);if(((o.ask||(o.ask={}))[""]=o).get)return n["."]=o.get,(i.ask||(i.ask={}))[o.get]=t.$._,i.on("out",t)}return i.on("out",t)}o.on("in",{put:o.put=p,$:o.$})},r.on.in=function(n,e){var o,t,i=(e=e||this.as).root,r=((o=n.$||(n.$=e.$))||"")._||d,a=n.put||"",s=a["#"],u=a["."],c=p!==a["="]?a["="]:a[":"],f=a[">"]||-1/0;if(p!==n.put&&(p===a["#"]||p===a["."]||p===a[":"]&&p===a["="]||p===a[">"]))return g(a)?void e.on("in",{$:r.back.$,put:{"#":s=r.back.soul,".":u=r.has||r.get,"=":a,">":v(r.back.put,u)},via:n}):(s=((a||"")._||"")["#"])?(o=e.root.$.get(s),setTimeout.each(Object.keys(a).sort(),function(t){"_"!=t&&p!==(f=v(a,t))&&e.on("in",{$:o,put:{"#":s,".":t,"=":a[t],">":f},VIA:n})})):void console.log("chain not yet supported for",a,"...",n,e);(n.seen||"")[e.id]||(((n.seen||(n.seen=function(){}))[e.id]=e)!==r&&(Object.keys(n).forEach(function(t){a[t]=n[t]},a={}),a.get=e.get||a.get,e.soul||e.has?r.soul&&(a.$=e.$,a.$$=a.$$||r.$):a.$$$=a.$$$||e.$,n=a),h(n,e),(e.soul||n.$$)&&f>=v(i.graph[s],u)&&((a=i.$.get(s)._).put=y(a.put,u,f,c,s)),!r.soul&&f>=v(i.graph[s],u)&&(t=(i.$.get(s)._.next||"")[u])&&(t.put=c,"string"==typeof(a=g(c))&&(t.put=i.$.get(a)._.put||c)),this.to&&this.to.next(n),e.any&&setTimeout.each(Object.keys(e.any),function(t){(t=e.any[t])&&t(n)},0,99),e.echo&&setTimeout.each(Object.keys(e.echo),function(t){(t=e.echo[t])&&t.on("in",n)},0,99),((n.$$||"")._||r).soul&&(t=e.next)&&(t=t[u])&&(a={},Object.keys(n).forEach(function(t){a[t]=n[t]}),a.$=(n.$$||n.$).get(a.get=u),delete a.$$,delete a.$$$,t.on("in",a)),l(n,e))},r.on.link=l,r.on.unlink=h;var p,d={},s=String.random,g=r.valid,u=function(t,n){return t&&Object.prototype.hasOwnProperty.call(t,n)},n=r.state,v=n.is,y=n.ify})(T,"./chain"),T(function(t){var g=T("./root");function r(t){var n=this.at||this.on;if(!t||n.soul||n.has)return this.off();if(t=(t=(t=t.$||t)._||t).id){n.map;if((n=this.seen||(this.seen={}))[t])return!0;n[t]=!0}}g.chain.get=function(t,n,e){var o;if("string"==typeof t){if(0==t.length)return(i=this.chain())._.err={err:g.log("0 length key!",t)},n&&n.call(i,i._.err),i;var i=(i=!(i=((f=this._).next||a)[t])?t&&function(t,n){var e=n._,o=e.next,i=n.chain()._;o=o||(e.next={});o[i.get=t]=i,n===e.root.$?i.soul=t:(e.soul||e.has)&&(i.has=t);return i}(t,this):i)&&i.$}else{if("function"==typeof t){if(!0===n)return function(t,n,e){var a,s=t._,u=0;if(a=s.soul||s.link)return n(a,e,s);if(s.jam)return s.jam.push([n,e]);s.jam=[[n,e]],t.get(function(t,n){if(!(v===t.put&&!s.root.opt.super&&(a=Object.keys(s.root.opt.peers).length)&&++u<=a)){n.rid(t);var e=(e=t.$)&&e._||{},o=0;for(a=s.jam,delete s.jam;r=a[o++];){var i=r[0],r=r[1];i&&i(e.link||e.soul||g.valid(t.put)||((t.put||{})._||{})["#"],r,t,n)}}},{out:{get:{".":!0}}})}(this,t,e),this;var c,f=(i=this)._,l=n||{},h=f.root;l.at=f,l.ok=t;var p={};function d(n,t,e){if(!d.stun&&(!(o=h.pass)||o[c])){var o,i=n.$._,r=(n.$$||"")._,a=(r||i).put,s=!i.has&&!i.soul,u={};if(!s&&v!==a||(a=v===((o=n.put)||"")["="]?v===(o||"")[":"]?o:o[":"]:o["="]),"string"==typeof(o=g.valid(a))&&(a=v===(o=h.$.get(o)._.put)?l.not?v:a:o),!l.not||v!==a){if(v===l.stun){if((o=h.stun)&&o.on&&(f.$.back(function(t){if(o.on(""+t.id,u={}),(u.run||0)u.run&&(u.stun&&!u.stun.end||(u.stun=o.on("stun"),u.stun=u.stun&&u.stun.last),u.stun&&!u.stun.end)))return void((u.stun.add||(u.stun.add={}))[c]=function(){d(n,t,1)});if(v===a&&(e=0),(o=h.hatch)&&!o.end&&v===l.hatch&&!e)return p[i.$._.id]?void 0:(p[i.$._.id]=1,void o.push(function(){d(n,t,1)}));p={}}if(h.pass){if(h.pass[c+i.id])return;h.pass[c+i.id]=1}l.on?l.ok.call(i.$,a,i.get,n,t||d):l.v2020?l.ok(n,t||d):(Object.keys(n).forEach(function(t){o[t]=n[t]},o={}),(n=o).put=a,l.ok.call(l.as,n,t||d))}}}return(((d.at=f).any||(f.any={}))[c=String.random(7)]=d).off=function(){d.stun=1,f.any&&delete f.any[c]},d.rid=r,d.id=l.run||++h.once,o=h.pass,(h.pass={})[c]=1,l.out=l.out||{get:{}},f.on("out",l.out),h.pass=o,i}if("number"==typeof t)return this.get(""+t,n,e);if("string"==typeof(o=s(t)))return this.get(o,n,e);(o=this.get.next)&&(i=o(this,t))}return i?n&&"function"==typeof n&&i.get(n,e):((i=this.chain())._.err={err:g.log("Invalid get request!",t)},n&&n.call(i,i._.err)),i};var v,a={},s=g.valid})(T,"./get"),T(function(t){var y=T("./root");function k(n,t){var e,o,i;t&&(t=(t._||"").id||t,e=n.root.stun||(n.root.stun={on:y.on}),o={},n.stun||(n.stun=e.on("stun",function(){})),(i=e.on(""+t))&&i.the.last.next(o),o.run>=n.run||e.on(""+t,function(t){return n.stun.end?(this.off(),void this.to.next(t)):(t.run=t.run||n.run,void(t.stun=t.stun||n.stun))}))}function m(n){var e,t,o,i,r;n.err?m.end(n.stun,n.root):n.todo.length||n.end||!Object.empty(n.wait)||(n.end=1,r=n.$.back(-1)._,e=r.root,t=r.ask(function(t){e.on("ack",t),t.err&&!t.lack&&y.log(t),++o>(n.acks||0)&&this.off(),n.ack&&n.ack(t,this)},n.opt),o=0,i=n.stun,(r=function(){i&&(m.end(i,e),setTimeout.each(Object.keys(i=i.add||""),function(t){(t=i[t])&&t()}))}).hatch=r,n.ack&&!n.ok&&(n.ok=n.acks||9),n.via._.on("out",{put:n.out=n.graph,ok:n.ok&&{"@":n.ok+1},opt:n.opt,"#":t,_:r}))}y.chain.put=function(t,n,g){var e=this,o=e._,i=o.root;(g=g||{}).root=o.root,g.run||(g.run=i.once),k(g,o.id),g.ack=g.ack||n,g.via=g.via||e,g.data=g.data||t,g.soul||(g.soul=o.soul||"string"==typeof n&&n);var v=g.state=g.state||y.state();return"function"==typeof t?t(function(t){g.data=t,e.put(void 0,void 0,g)}):g.soul?(g.$=i.$.get(g.soul),g.todo=[{it:g.data,ref:g.$}],g.turn=g.turn||r,g.ran=g.ran||m,function t(){var n,e,i,r,a,o,s,u=g.todo,c=u.pop(),f=c.it;c.ref&&c.ref._.id;if(k(g,c.ref),(r=c.todo)&&(f=f[e=r.pop()],r.length&&u.push(c)),e&&(u.path||(u.path=[])).push(e),!(n=b(f))&&!(a=y.is(f))){if(!Object.plain(f))return void m.err(g,"Invalid data: "+((o=f)&&(s=o.constructor)&&s.name||typeof o)+" at "+(g.via.back(function(t){t.get&&r.push(t.get)},r=[])||r.join("."))+"."+(u.path||[]).join("."));for(var l=g.seen||(g.seen=[]),h=l.length;h--;)if(f===(r=l[h]).it){n=f=r.link;break}}if(e&&n)c.node=_(c.node,e,v,f);else{if(!g.seen)return void m.err(g,"Data at root of graph must be a node (an object).");g.seen.push(i={it:f,link:{},todo:a?[]:Object.keys(f).sort().reverse(),path:(u.path||[]).slice(),up:c}),c.node=_(c.node,e,v,i.link),!a&&i.todo.length&&u.push(i);var p=g.seen.length;function d(t,n){var e=i.link["#"];n&&(n.off(),n.rid(t));var o=e||t.soul||(r=(t.$$||t.$)._||"").soul||r.link||((r=r.put||"")._||"")["#"]||r["#"]||((r=t.put||"")&&t.$$?r:r["="]||r[":"]||"")["#"];e||k(g,t.$),o||c.link["#"]?(o||(o=[],(t.$$||t.$).back(function(t){return(r=t.soul||t.link)?o.push(r):void o.push(t.get)}),o=o.reverse().join("/")),i.link["#"]=o,a||(((g.graph||(g.graph={}))[o]=i.node||(i.node={_:{}}))._["#"]=o),delete g.wait[p],i.wait&&setTimeout.each(i.wait,function(t){t&&t()}),g.ran(g)):(c.wait||(c.wait=[])).push(function(){d(t,n)})}(g.wait||(g.wait={}))[p]="",r=(i.ref=a?f:e?c.ref.get(e):c.ref)._,(r=f&&(f._||"")["#"]||r.soul||r.link)?d({soul:r}):i.ref.get(d,{run:g.run,v2020:1,out:{get:{".":" "}}})}if(!u.length)return g.ran(g);g.turn(t)}()):function(n){var e,t=n.via._;n.via=n.via.back(function(t){return t.soul||!t.get?t.$:(e=n.data,void((n.data={})[t.get]=e))}),n.via&&n.via._.soul||(n.via=t.root.$.get(((n.data||"")._||"")["#"]||t.$.back("opt.uuid")()));n.via.put(n.data,n.ack,n)}(g),e},m.end=function(t,n){t.end=e,t.the.to===t&&t===t.the.last&&delete n.stun,t.off()},m.err=function(t,n){(t.ack||e).call(t,t.out={err:t.err=y.log(n)}),t.ran(t)};var e=function(){},r=setTimeout.turn,b=y.valid,_=y.state.ify})(T,"./put"),T(function(t){var n=T("./root");T("./chain"),T("./back"),T("./put"),T("./get"),t.exports=n})(T,"./index"),T(function(t){var g=T("./index");g.chain.on=function(t,n,e,o){var i=this._;i.root;if("string"==typeof t)return n?(o=i.on(t,n,e||i,o),e&&e.$&&(e.subs||(e.subs=[])).push(o),this):i.on(t);(n=!0===n?{change:!0}:n||{}).not=1,n.on=1;return this.get(t,n),this},g.chain.once=function(c,f){if(f=f||{},!c)return t=this,g.log.once("valonce","Chainable val is experimental, its behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it."),(e=t.chain())._.nix=t.once(function(t,n){e._.on("in",this._)}),e._.lex=t._.lex,e;var t,e,l,h=this._,p=h.root,d=(h.put,String.random(7));return this.get(function(n,e,o,i){var r=this,a=r._,s=a.one||(a.one={});function u(t){a.has||a.soul||(a={put:n,get:e}),void 0===(l=a.put)&&(l=((o.$$||"")._||"").put),"string"!=typeof g.valid(l)||void 0!==(l=p.$.get(l)._.put)||t?i.stun||""!==s[d]&&(s[d]="",(h.soul||h.has)&&i.off(),c.call(r,l,a.get),clearTimeout(s[d])):s[d]=setTimeout(function(){u(1)},f.wait||99)}i.stun||""!==s[d]&&(!0!==(l=g.valid(n))?"string"!=typeof l&&(clearTimeout((h.one||"")[d]),clearTimeout(s[d]),s[d]=setTimeout(u,f.wait||99)):u())},{on:1}),this},g.chain.off=function(){var e,t=this._,o=t.back;if(o)return t.ack=0,(e=o.next)&&e[t.get]&&delete e[t.get],(e=o.ask)&&delete e[t.get],(e=o.put)&&delete e[t.get],(e=t.soul)&&delete o.root.graph[e],(e=t.map)&&Object.keys(e).forEach(function(t,n){(n=e[t]).link&&o.root.$.get(n.link).off()}),(e=t.next)&&Object.keys(e).forEach(function(t,n){e[t].$.off()}),t.on("off",{}),this}})(T,"./on"),T(function(t){var s=T("./index"),o=s.chain.get.next;function u(t){this.to.next(t);var n=this.as,e=t.$._,o=t.put;(e.soul||t.$$)&&((e=n.lex)&&!String.match(t.get||(o||"")["."],e["."]||e["#"]||e)||s.on.link(t,n))}s.chain.get.next=function(t,n){var e;return Object.plain(n)?(e=((e=n["#"])||"")["="]||e)?t.get(e):((e=t.chain()._).lex=n,t.on("in",function(t){String.match(t.get||(t.put||"")["."],n["."]||n["#"]||n)&&e.on("in",t),this.to.next(t)}),e.$):(o||c)(t,n)},s.chain.map=function(r,t,n){var e,a,o=this,i=o._;return Object.plain(r)&&(e=r["."]?r:{".":r},r=void 0),r?(s.log.once("mapfn","Map functions are experimental, their behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it."),a=o.chain(),o.map().on(function(t,n,e,o){o=(r||c).call(this,t,n,e,o);if(void 0!==o){if(t===o)return a._.on("in",e);if(s.is(o))return a._.on("in",o._);var i={};Object.keys(e.put).forEach(function(t){i[t]=e.put[t]},i),i["="]=o,a._.on("in",{get:n,put:i})}})):(a=i.each)||((i.each=a=o.chain())._.lex=e||a._.lex||i.lex,a._.nix=o.back("nix"),o.on("in",u,a._)),a};var c=function(){}})(T,"./map"),T(function(t){var s=T("./index");s.chain.set=function(t,i,n){var e,r,a=this,o=a.back(-1);return i=i||function(){},(n=n||{}).item=n.item||t,(e=((t||"")._||"")["#"])&&((t={})["#"]=e),"string"==typeof(r=s.valid(t))?a.get(e=r).put(t,i,n):s.is(t)?(a.put(function(o){t.get(function(t,n,e){return t?((r={})[t]={"#":t},void o(r)):i.call(a,{err:s.log('Only a node can be linked! Not "'+e.put+'"!')})},!0)}),t):(Object.plain(t)&&(t=o.get(e=a.back("opt.uuid")()).put(t)),a.get(e||o.back("opt.uuid")(7)).put(t,i,n))}})(T,"./set"),T(function(t){T("./shim");function n(){}var r=JSON.parseAsync||function(t,n,e){var o=+new Date;try{n(void 0,JSON.parse(t,e),w.sucks(+new Date-o))}catch(t){n(t)}},w=JSON.stringifyAsync||function(t,n,e,o){var i=+new Date;try{n(void 0,JSON.stringify(t,e,o),w.sucks(+new Date-i))}catch(t){n(t)}};function e(u){var d=function(){},g=u.opt||{};g.log=g.log||console.log,g.gap=g.gap||g.wait||0,g.max=g.max||.3*(g.memory?999*g.memory*999:3e8),g.pack=g.pack||.01*g.max*.01,g.puff=g.puff||9;var v=setTimeout.turn||setTimeout,y=u.dup,k=y.check,m=y.track,i=(new Date,d.hear=function(t,a){if(t){if(g.max<=t.length)return d.say({dam:"!",err:"Message too big!"},a);d===this&&(i.d+=t.length||0,++i.c);var n,e=a.SH=+new Date,o=t[0];if("["===o)return r(t,function(t,i){if(t||!i)return d.say({dam:"!",err:"DAM JSON parse error."},a);console.STAT&&console.STAT(+new Date,i.length,"# on hear batch");var r=g.puff;!function t(){for(var n,e=+new Date,o=0;o<"])&&"string"==typeof r&&r.slice(0,99).split(",").forEach(function(t){this[t]=1},t._.yo={}),r=t.dam)return(r=d.hear[r])&&r(t,n,u),void m(o);(r=t.ok)&&(t._.near=r["/"]);e=+new Date;s&&(s.is=e),n.SI=o,u.on("in",d.last=t),s&&(s.hd=+new Date),console.STAT&&console.STAT(e,+new Date-e,t.get?"msg get":t.put?"msg put":"msg"),(r=m(o)).via=n,t.get&&(r.it=t),a&&m(a),d.leap=d.last=null}};function b(t){var n=t.batch,e="string"==typeof n;if(e&&(n+="]"),t.batch=t.tail=null,n&&(e?!(n.length<3):n.length)){if(!e)try{n=1===n.length?n[0]:JSON.stringify(n)}catch(t){return g.log("DAM JSON stringify error",t)}n&&_(n,t)}}function _(n,e){try{var t=e.wire;e.say?e.say(n):t.send&&t.send(n),d.say.d+=n.length||0,++d.say.c}catch(t){(e.queue=e.queue||[]).push(n)}}i.c=i.d=0,function(){var h,p=0;function t(t,n){var e;return n instanceof Object?(Object.keys(n).sort().forEach(o,{to:e={},on:n}),e):n}function o(t){this.to[t]=this.on[t]}d.hash=function(o,i){var r,a,s,u=+new Date;w(o.put,function t(n,e){e=(a=a||(s=e||"")).slice(0,32768);r=String.hash(e,r),(a=a.slice(32768))?v(t,0):(console.STAT&&console.STAT(u,+new Date-u,"say json+hash"),o._.$put=s,o["##"]=r,d.say(o,i),delete o._.$put)},t)};d.say=function(r,a){var t;if((t=this)&&(t=t.to)&&t.next&&t.next(r),!r)return!1;var n,s,u=r["@"],c=r._||(r._=function(){}),e=r.DBG,o=+new Date;if(c.y=c.y||o,a||e&&(e.y=o),(n=r["#"])||(n=r["#"]=String.random(9)),h||m(n),r["##"]||$===r.put||c.via||!u){if(!(a=!a&&u?(t=y.s[u])&&(t.via||(t=t.it)&&(t=t._)&&t.via)||(t=d.last)&&u===t["#"]&&d.leap:a)&&u)return y.s[u]?void 0:(console.STAT&&console.STAT(+new Date,++p,"total no peer to ack to"),!1);if(!a&&d.way)return d.way(r);if(e&&(e.yh=+new Date),s=c.raw){if(e&&(e.yr=+new Date),!a||!a.id){if(!Object.plain(a||g.peers))return!1;var o=+new Date,f=(g.puff,g.peers),l=Object.keys(a||g.peers||{});return console.STAT&&console.STAT(o,+new Date-o,"peer keys"),void function t(){var n=+new Date;h=1;var e=c.raw;c.raw=s;for(var o,i=0;i<9&&(o=(l||"")[i++]);)(o=f[o]||(a||"")[o])&&d.say(r,o);c.raw=e,h=0,l=l.slice(i),console.STAT&&console.STAT(n,+new Date-n,"say loop"),l.length&&(v(t,0),u&&m(u))}()}if(!a.wire&&d.wire&&d.wire(a),n!==a.last){if(a.last=n,a===c.via)return!1;if((t=c.yo)&&(t[a.url]||t[a.pid]||t[a.id]))return!1;if(console.STAT&&console.STAT(o,((e||c).yp=+new Date)-(c.y||o),"say prep"),!h&&u&&m(u),a.batch){if(a.tail=(t=a.tail||0)+s.length,a.tail<=g.pack)return void(a.batch+=(t?",":"")+s);b(a)}a.batch="[";var i=+new Date;setTimeout(function(){console.STAT&&console.STAT(i,+new Date-i,"0ms TO"),b(a)},g.gap),_(s,a),console.STAT&&u===a.SI&&console.STAT(o,+new Date-a.SH,"say ack")}}else d.raw(r,a)}else d.hash(r,a)};d.say.c=d.say.d=0,d.raw=function(e,o){if(!e)return"";var i,r=e._||{};if(s=r.raw)return s;if("string"==typeof e)return e;var t=e["##"],n=e["@"];if(t&&n){if(!r.via&&k(n+t))return!1;if((s=(y.s[n]||"").it)||(s=d.last)&&n===s["#"]){if(t===s["##"])return!1;s["##"]||(s["##"]=t)}}if(!e.dam&&!e["@"]){var a,s,u=0,c=[];for(a in s=g.peers){var f=s[a];if(c.push(f.url||f.pid||f.id),6<++u)break}1<"]=c.join())}if(e.put&&(s=e.ok)&&(e.ok={"@":(s["@"]||1)-1,"/":s["/"]==e._.near?d.near:s["/"]}),i=r.$put)return s={},Object.keys(e).forEach(function(t){s[t]=e[t]}),s.put=":])([:",void w(s,function(t,n){t||(t=+new Date,s=n.indexOf('"put":":])([:"'),l($,n=n.slice(0,s+6)+i+n.slice(s+14)),console.STAT&&console.STAT(t,+new Date-t,"say slice"))});function l(t,n){t||(r.raw=n,d.say(e,o))}w(e,l)}}(),d.near=0,d.hi=function(n){var t;n.wire?(n.id?g.peers[n.url||n.id]=n:(t=n.id=n.id||n.url||String.random(9),d.say({dam:"?",pid:u.opt.pid},g.peers[t]=n),delete y.s[n.last]),n.met||(d.near++,n.met=+new Date,u.on("hi",n)),t=n.queue,n.queue=[],setTimeout.each(t||[],function(t){_(t,n)},0,9)):d.wire(n.length?{url:n,id:n}:n)},d.bye=function(t){t.met&&--d.near,delete t.met,u.on("bye",t);var n=+new Date;n-=t.met||n,d.bye.time=((d.bye.time||n)+n)/2},d.hear["!"]=function(t,n){g.log("Error:",t.err)},d.hear["?"]=function(t,n){t.pid&&(n.pid||(n.pid=t.pid),t["@"])||(d.say({dam:"?",pid:g.pid,"@":t["#"]},n),delete y.s[n.last])},d.hear.mob=function(t,n){!t.peers||(t=(t=Object.keys(t.peers))[Math.random()*t.length>>0])&&(d.bye(n),d.hi(t))},u.on("create",function(t){t.opt.pid=t.opt.pid||String.random(9),this.to.next(t),t.on("out",d.say)}),u.on("bye",function(t,n){t=g.peers[t.id||t]||t,this.to.next(t),t.bye?t.bye():(n=t.wire)&&n.close&&n.close(),delete g.peers[t.id],t.wire=null});var e={};return u.on("bye",function(t,n){this.to.next(t),(n=console.STAT)&&(n.peers=d.near),(n=t.url)&&(e[n]=!0,setTimeout(function(){delete e[n]},g.lack||9e3))}),u.on("hi",function(e,t){this.to.next(e),(t=console.STAT)&&(t.peers=d.near),g.super||(9999<(t=Object.keys(u.next||"")).length&&!console.SUBS&&console.log(console.SUBS="Warning: You have more than 10K live GETs, which might use more bandwidth than your screen can show - consider `.off()`."),setTimeout.each(t,function(n){var t=u.next[n];g.super||(t.ask||"")[""]?d.say({get:{"#":n}},e):setTimeout.each(Object.keys(t.ask||""),function(t){t&&d.say({"##":String.hash((u.graph[n]||"")[t]),get:{"#":n,".":t}},e)})}))}),d}w.sucks=function(t){99"],n[":"],e),f&&4999880"]||n["<"])||o===n["="]&&(e=n["*"]||n[">"]||n["<"],t.slice(0,(e||"").length)===e||o===n["*"]&&(o!==n[">"]&&o!==n["<"]?t>=n[">"]&&t<=n["<"]:o!==n[">"]&&t>=n[">"]||o!==n["<"]&&t<=n["<"])))},h.text.hash=h.text.hash||function(t,n){if(l("text.hash"),"string"==typeof t){if(n=n||0,!t.length)return n;for(var e=0,o=t.length;e'], put[':'], soul); // merge into disk object if(stop && size > (4999880)){ root.on('in', {'@': id, err: "localStorage max!"}); return; } - if(!msg['@']){ acks.push(id) } // then ack any non-ack write. // TODO: use batch id. + //if(!msg['@']){ acks.push(id) } // then ack any non-ack write. // TODO: use batch id. + if(!msg['@'] && (!msg._.via || Math.random() < (ok['@'] / ok['/']))){ acks.push(id) } // then ack any non-ack write. // TODO: use batch id. if(to){ return } to = setTimeout(flush, 9+(size / 333)); // 0.1MB = 0.3s, 5MB = 15s }); @@ -55,7 +56,7 @@ Gun.on('create', function lg(root){ } size = tmp.length; - if(!err && !Object.empty(opt.peers)){ return } // only ack if there are no peers. // Switch this to probabilistic mode + //if(!err && !Object.empty(opt.peers)){ return } // only ack if there are no peers. // Switch this to probabilistic mode setTimeout.each(ack, function(id){ root.on('in', {'@': id, err: err, ok: 0}); // localStorage isn't reliable, so make its `ok` code be a low number. },0,99); diff --git a/src/mesh.js b/src/mesh.js index 27644552..dc0e5370 100644 --- a/src/mesh.js +++ b/src/mesh.js @@ -81,6 +81,7 @@ function Mesh(root){ dup_track(id); return; } + if(tmp = msg.ok){ msg._.near = tmp['/'] } var S = +new Date; DBG && (DBG.is = S); peer.SI = id; root.on('in', mesh.last = msg); @@ -126,12 +127,12 @@ function Mesh(root){ var DBG = msg.DBG, S = +new Date; meta.y = meta.y || S; if(!peer){ DBG && (DBG.y = S) } if(!(id = msg['#'])){ id = msg['#'] = String.random(9) } !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(!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){ // still no peer, then ack daisy chain lost. - if(dup.s[ack]){ return } // in dups but no peer hints that this was ack to self, ignore. - console.STAT && console.STAT(+new Date, ++SMIA, 'total no peer to ack to'); + 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. + 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; } // 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(!peer && mesh.way){ return mesh.way(msg) } @@ -148,7 +149,7 @@ function Mesh(root){ //Type.obj.map(peer || opt.peers, each); // in case peer is a peer list. loop = 1; var wr = meta.raw; meta.raw = raw; // quick perf hack var i = 0, p; while(i < 9 && (p = (pl||'')[i++])){ - if(!(p = ps[p])){ continue } + if(!(p = ps[p] || (peer||'')[p])){ continue } mesh.say(msg, p); } meta.raw = wr; loop = 0; @@ -199,7 +200,7 @@ function Mesh(root){ 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(!msg.dam){ + if(!msg.dam && !msg['@']){ var i = 0, to = []; tmp = opt.peers; for(var k in tmp){ var p = tmp[k]; // TODO: Make it up peers instead! to.push(p.url || p.pid || p.id); @@ -207,6 +208,7 @@ function Mesh(root){ } if(i > 1){ msg['><'] = to.join() } // TODO: BUG! This gets set regardless of peers sent to! Detect? } + if(msg.put && (tmp = msg.ok)){ msg.ok = {'@':(tmp['@']||1)-1, '/': (tmp['/']==msg._.near)? mesh.near : tmp['/']}; } if(put = meta.$put){ tmp = {}; Object.keys(msg).forEach(function(k){ tmp[k] = msg[k] }); tmp.put = ':])([:'; @@ -253,18 +255,22 @@ function Mesh(root){ (peer.queue = peer.queue || []).push(raw); }} + mesh.near = 0; mesh.hi = function(peer){ var wire = peer.wire, tmp; if(!wire){ mesh.wire((peer.length && {url: peer, id: peer}) || peer); return } if(peer.id){ opt.peers[peer.url || peer.id] = peer; } else { - tmp = peer.id = peer.id || String.random(9); + tmp = peer.id = peer.id || peer.url || String.random(9); mesh.say({dam: '?', pid: root.opt.pid}, opt.peers[tmp] = peer); delete dup.s[peer.last]; // IMPORTANT: see https://gun.eco/docs/DAM#self } - peer.met = peer.met || +(new Date); - if(!wire.hied){ root.on(wire.hied = 'hi', peer) } + if(!peer.met){ + mesh.near++; + peer.met = +(new Date); + root.on('hi', peer) + } // @rogowski I need this here by default for now to fix go1dfish's bug tmp = peer.queue; peer.queue = []; setTimeout.each(tmp||[],function(msg){ @@ -273,6 +279,8 @@ function Mesh(root){ //Type.obj.native && Type.obj.native(); // dirty place to check if other JS polluted. } mesh.bye = function(peer){ + peer.met && --mesh.near; + delete peer.met; root.on('bye', peer); var tmp = +(new Date); tmp = (tmp - (peer.met||tmp)); mesh.bye.time = ((mesh.bye.time || tmp) + tmp) / 2; @@ -286,6 +294,13 @@ function Mesh(root){ mesh.say({dam: '?', pid: opt.pid, '@': msg['#']}, peer); delete dup.s[peer.last]; // IMPORTANT: see https://gun.eco/docs/DAM#self } + mesh.hear['mob'] = function(msg, peer){ // NOTE: AXE will overload this with better logic. + if(!msg.peers){ return } + var peers = Object.keys(msg.peers), one = peers[(Math.random()*peers.length) >> 0]; + if(!one){ return } + mesh.bye(peer); + mesh.hi(one); + } root.on('create', function(root){ root.opt.pid = root.opt.pid || String.random(9); @@ -303,17 +318,22 @@ function Mesh(root){ var gets = {}; root.on('bye', function(peer, tmp){ this.to.next(peer); - if(tmp = console.STAT){ tmp.peers = (tmp.peers || 0) - 1; } + if(tmp = console.STAT){ tmp.peers = mesh.near; } if(!(tmp = peer.url)){ return } gets[tmp] = true; setTimeout(function(){ delete gets[tmp] },opt.lack || 9000); }); root.on('hi', function(peer, tmp){ this.to.next(peer); - if(tmp = console.STAT){ tmp.peers = (tmp.peers || 0) + 1 } - if(!(tmp = peer.url) || !gets[tmp]){ return } delete gets[tmp]; + if(tmp = console.STAT){ tmp.peers = mesh.near } if(opt.super){ return } // temporary (?) until we have better fix/solution? - setTimeout.each(Object.keys(root.next), function(soul){ var node = root.next[soul]; // TODO: .keys( is slow - tmp = {}; tmp[soul] = root.graph[soul]; tmp = String.hash(tmp); // TODO: BUG! This is broken. - mesh.say({'##': tmp, get: {'#': soul}}, peer); + var souls = Object.keys(root.next||''); // TODO: .keys( is slow + if(souls.length > 9999 && !console.SUBS){ console.log(console.SUBS = "Warning: You have more than 10K live GETs, which might use more bandwidth than your screen can show - consider `.off()`.") } + setTimeout.each(souls, function(soul){ var node = root.next[soul]; + if(opt.super || (node.ask||'')['']){ mesh.say({get: {'#': soul}}, peer); return } + setTimeout.each(Object.keys(node.ask||''), function(key){ if(!key){ return } + // is the lack of ## a !onion hint? + mesh.say({'##': String.hash((root.graph[soul]||'')[key]), get: {'#': soul, '.': key}}, peer); + // TODO: Switch this so Book could route? + }) }); }); diff --git a/src/put.js b/src/put.js index 54bd2c78..d0c95553 100644 --- a/src/put.js +++ b/src/put.js @@ -101,9 +101,10 @@ function stun(as, id){ function ran(as){ if(as.err){ ran.end(as.stun, as.root); return } // move log handle here. if(as.todo.length || as.end || !Object.empty(as.wait)){ return } as.end = 1; + //(as.retry = function(){ as.acks = 0; var cat = (as.$.back(-1)._), root = cat.root, ask = cat.ask(function(ack){ root.on('ack', ack); - if(ack.err){ Gun.log(ack) } + if(ack.err && !ack.lack){ Gun.log(ack) } if(++acks > (as.acks || 0)){ this.off() } // Adjustable ACKs! Only 1 by default. if(!as.ack){ return } as.ack(ack, this); @@ -114,7 +115,9 @@ function ran(as){ setTimeout.each(Object.keys(stun = stun.add||''), function(cb){ if(cb = stun[cb]){cb()} }); // resume the stunned reads // Any perf reasons to CPU schedule this .keys( ? }).hatch = tmp; // this is not official yet ^ //console.log(1, "PUT", as.run, as.graph); - (as.via._).on('out', {put: as.out = as.graph, ok: as.ok || as.opt, opt: as.opt, '#': ask, _: tmp}); + if(as.ack && !as.ok){ as.ok = as.acks || 9 } // TODO: In future! Remove this! This is just old API support. + (as.via._).on('out', {put: as.out = as.graph, ok: as.ok && {'@': as.ok+1}, opt: as.opt, '#': ask, _: tmp}); + //})(); }; ran.end = function(stun,root){ stun.end = noop; // like with the earlier id, cheaper to make this flag a function so below callbacks do not have to do an extra type check. if(stun.the.to === stun && stun === stun.the.last){ delete root.stun } diff --git a/src/root.js b/src/root.js index 7a3bb29f..f0ef4c03 100644 --- a/src/root.js +++ b/src/root.js @@ -128,7 +128,7 @@ Gun.ask = require('./ask'); console.STAT && console.STAT(((DBG||ctx).Hf = +new Date), tmp, 'future'); return; } - if(state < was){ /*old;*/ if(!ctx.miss){ return } } // but some chains have a cache miss that need to re-fire. // TODO: Improve in future. // for AXE this would reduce rebroadcast, but GUN does it on message forwarding. + if(state < was){ /*old;*/ if(true || !ctx.miss){ return } } // but some chains have a cache miss that need to re-fire. // TODO: Improve in future. // for AXE this would reduce rebroadcast, but GUN does it on message forwarding. // TURNS OUT CACHE MISS WAS NOT NEEDED FOR NEW CHAINS ANYMORE!!! DANGER DANGER DANGER, ALWAYS RETURN! (or am I missing something?) if(!ctx.faith){ // TODO: BUG? Can this be used for cache miss as well? // Yes this was a bug, need to check cache miss for RAD tests, but should we care about the faith check now? Probably not. if(state === was && (val === known || L(val) <= L(known))){ /*console.log("same");*/ /*same;*/ if(!ctx.miss){ return } } // same } @@ -136,14 +136,19 @@ Gun.ask = require('./ask'); var aid = msg['#']+ctx.all++, id = {toString: function(){ return aid }, _: ctx}; id.toJSON = id.toString; // this *trick* makes it compatible between old & new versions. root.dup.track(id)['#'] = msg['#']; // fixes new OK acks for RPC like RTC. DBG && (DBG.ph = DBG.ph || +new Date); - root.on('put', {'#': id, '@': msg['@'], put: {'#': soul, '.': key, ':': val, '>': state}, _: ctx}); + root.on('put', {'#': id, '@': msg['@'], put: {'#': soul, '.': key, ':': val, '>': state}, ok: msg.ok, _: ctx}); } function map(msg){ var DBG; if(DBG = (msg._||'').DBG){ DBG.pa = +new Date; DBG.pm = DBG.pm || +new Date} var eve = this, root = eve.as, graph = root.graph, ctx = msg._, put = msg.put, soul = put['#'], key = put['.'], val = put[':'], state = put['>'], id = msg['#'], tmp; if((tmp = ctx.msg) && (tmp = tmp.put) && (tmp = tmp[soul])){ state_ify(tmp, key, state, val, soul) } // necessary! or else out messages do not get SEA transforms. + //var bytes = ((graph[soul]||'')[key]||'').length||1; graph[soul] = state_ify(graph[soul], key, state, val, soul); - if(tmp = (root.next||'')[soul]){ tmp.on('in', msg) } + if(tmp = (root.next||'')[soul]){ + //tmp.bytes = (tmp.bytes||0) + ((val||'').length||1) - bytes; + //if(tmp.bytes > 2**13){ Gun.log.once('byte-limit', "Note: In the future, GUN peers will enforce a ~4KB query limit. Please see https://gun.eco/docs/Page") } + tmp.on('in', msg) + } fire(ctx); eve.to.next(msg); } @@ -163,11 +168,11 @@ Gun.ask = require('./ask'); CF(); // courtesy check; } function ack(msg){ // aggregate ACKs. - var id = msg['@'] || '', ctx; + var id = msg['@'] || '', ctx, ok, tmp; if(!(ctx = id._)){ var dup = (dup = msg.$) && (dup = dup._) && (dup = dup.root) && (dup = dup.dup); if(!(dup = dup.check(id))){ return } - msg['@'] = dup['#'] || msg['@']; + msg['@'] = dup['#'] || msg['@']; // This doesn't do anything anymore, backtrack it to something else? return; } ctx.acks = (ctx.acks||0) + 1; @@ -175,13 +180,14 @@ Gun.ask = require('./ask'); msg['@'] = ctx['#']; fire(ctx); // TODO: BUG? How it skips/stops propagation of msg if any 1 item is error, this would assume a whole batch/resync has same malicious intent. } + ctx.ok = msg.ok || ctx.ok; if(!ctx.stop && !ctx.crack){ ctx.crack = ctx.match && ctx.match.push(function(){back(ctx)}) } // handle synchronous acks. NOTE: If a storage peer ACKs synchronously then the PUT loop has not even counted up how many items need to be processed, so ctx.STOP flags this and adds only 1 callback to the end of the PUT loop. back(ctx); } function back(ctx){ if(!ctx || !ctx.root){ return } if(ctx.stun || ctx.acks !== ctx.all){ return } - ctx.root.on('in', {'@': ctx['#'], err: ctx.err, ok: ctx.err? u : {'':1}}); + ctx.root.on('in', {'@': ctx['#'], err: ctx.err, ok: ctx.err? u : ctx.ok || {'':1}}); } var ERR = "Error: Invalid graph!"; diff --git a/src/shim.js b/src/shim.js index a7a6b15a..59963495 100644 --- a/src/shim.js +++ b/src/shim.js @@ -46,12 +46,18 @@ Object.keys = Object.keys || function(o){ for(var k in o){ if(has.call(o, k)){ l.push(k) } } return l; } -;(function(){ // max ~1ms or before stack overflow - var u, sT = setTimeout, l = 0, c = 0, sI = (typeof setImmediate !== ''+u && setImmediate) || sT; // queueMicrotask faster but blocks UI - sT.hold = sT.hold || 9; - sT.poll = sT.poll || function(f){ //f(); return; // for testing - if((sT.hold >= (+new Date - l)) && c++ < 3333){ f(); return } - sI(function(){ l = +new Date; f() },c=0) +;(function(){ + var u, sT = setTimeout, l = 0, c = 0 + , sI = (typeof setImmediate !== ''+u && setImmediate) || (function(c,f){ + if(typeof MessageChannel == ''+u){ return sT } + (c = new MessageChannel()).port1.onmessage = function(e){ ''==e.data && f() } + return function(q){ f=q;c.port2.postMessage('') } + }()), check = sT.check = sT.check || (typeof performance !== ''+u && performance) + || {now: function(){ return +new Date }}; + sT.hold = sT.hold || 9; // half a frame benchmarks faster than < 1ms? + sT.poll = sT.poll || function(f){ + if((sT.hold >= (check.now() - l)) && c++ < 3333){ f(); return } + sI(function(){ l = check.now(); f() },c=0) } }()); ;(function(){ // Too many polls block, this "threads" them in turns over a single thread in time. diff --git a/src/websocket.js b/src/websocket.js index 7d0bd7b9..ee3531df 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -25,10 +25,10 @@ Gun.on('opt', function(root){ var url = peer.url.replace(/^http/, 'ws'); var wire = peer.wire = new opt.WebSocket(url); wire.onclose = function(){ - opt.mesh.bye(peer); reconnect(peer); + opt.mesh.bye(peer); }; - wire.onerror = function(error){ + wire.onerror = function(err){ reconnect(peer); }; wire.onopen = function(){ @@ -39,7 +39,7 @@ Gun.on('opt', function(root){ opt.mesh.hear(msg.data || msg, peer); }; return wire; - }catch(e){}} + }catch(e){ opt.mesh.bye(peer) }} setTimeout(function(){ !opt.super && root.on('out', {dam:'hi'}) },1); // it can take a while to open a socket, so maybe no longer lazy load for perf reasons?