gun/gun.min.js
2021-08-21 21:27:55 -07:00

1 line
31 KiB
JavaScript

!function(){function f(n,t){return t?require(n):n.slice?f[o(n)]:function(t,e){n(t={exports:{}}),f[o(e)]=t.exports};function o(t){return t.split("/").slice(-1).toString().replace(".js","")}}if("undefined"!=typeof module)var e=module;f(function(t){String.random=function(t,e){var n="";for(t=t||24,e=e||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";0<t--;)n+=e.charAt(Math.floor(Math.random()*e.length));return n},String.match=function(t,e){var n,o;return"string"==typeof t&&("string"==typeof e&&(e={"=":e}),t===(n=(e=e||{})["="]||e["*"]||e[">"]||e["<"])||o===e["="]&&(n=e["*"]||e[">"],t.slice(0,(n||"").length)===n||o===e["*"]&&(o!==e[">"]&&o!==e["<"]?t>=e[">"]&&t<=e["<"]:o!==e[">"]&&t>=e[">"]||o!==e["<"]&&t<=e["<"])))},String.hash=function(t,e){if("string"==typeof t){if(e=e||0,!t.length)return e;for(var n=0,o=t.length;n<o;++n)e=(e<<5)-e+t.charCodeAt(n),e|=0;return e}};var e,n,o,i,r,a,u,s,c,f,l,h,p,d,g=Object.prototype.hasOwnProperty;Object.plain=function(t){return!!t&&(t instanceof Object&&t.constructor===Object||"Object"===Object.prototype.toString.call(t).match(/^\[object (\w+)\]$/)[1])},Object.empty=function(t,e){for(var n in t)if(g.call(t,n)&&(!e||-1==e.indexOf(n)))return!1;return!0},Object.keys=Object.keys||function(t){var e=[];for(var n in t)g.call(t,n)&&e.push(n);return e},e=setTimeout,o=n=0,i=typeof setImmediate!=""+void 0&&setImmediate||e,e.poll=e.poll||function(t){+new Date-n<=1&&o++<3333?t():i(function(){n=+new Date,t()},o=0)},a=setTimeout,s=(u=a.turn=a.turn||function(t){1==s.push(t)&&c(l)}).s=[],c=a.poll,f=0,l=function(){(r=s[f++])&&r(),f!=s.length&&99!=f||(s=u.s=s.slice(f),f=0),s.length&&c(l)},p=setTimeout,d=p.turn,p.each=p.each||function(r,a,u,s){s=s||9,function t(e,n,o){if(n=(e=(r||[]).splice(0,s)).length){for(var i=0;i<n&&h===(o=a(e[i]));i++);if(h===o)return void d(t)}u&&u(o)}()}})(f,"./shim"),f(function(t){t.exports=function t(e,n,o){if(!e)return{to:t};var i="function"==typeof n;e=(this.tag||(this.tag={}))[e]||i&&(this.tag[e]={tag:e,to:t._={next:function(t){var e;(e=this.to)&&e.next(t)}}});if(i){var r={off:t.off||(t.off=function(){if(this.next===t._.next)return!0;this===this.the.last&&(this.the.last=this.back),this.to.back=this.back,this.next=t._.next,this.back.to=this.to,this.the.last===this.the&&delete this.on.tag[this.the.tag]}),to:t._,next:n,the:e,on:this,as:o};return(r.back=e.last||e).to=r,e.last=r}return(e=e.to)&&void 0!==n&&e.next(n),e}})(f,"./onto"),f(function(t){f("./shim"),t.exports=function(t){return void 0!==t&&(null===t||t!==1/0&&(t==t&&("string"==typeof t||"boolean"==typeof t||"number"==typeof t||!(!t||"string"!=typeof(t["#"]||0)||!Object.empty(t,["#"]))&&t["#"])))}})(f,"./valid"),f(function(t){function e(){var t=+new Date;return i=i<t?(n=0,t+e.drift):t+(n+=1)/o+e.drift}f("./shim");e.drift=0;var n=0,o=999,i=-1/0;e.is=function(t,e,n){var o=e&&t&&t._&&t._[">"]||n;if(o)return"number"==typeof(o=o[e])?o:-1/0},e.ify=function(t,e,n,o,i){(t=t||{})._=t._||{},i&&(t._["#"]=i);var r=t._[">"]||(t._[">"]={});return void 0!==e&&"_"!==e&&("number"==typeof n&&(r[e]=n),void 0!==o&&(t[e]=o)),t},t.exports=e})(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]&&e(t)};var e=i.track=function(t){var e=r[t]||(r[t]={});return e.was=i.now=+new Date,i.to||(i.to=setTimeout(i.drop,o.age+9)),e};return i.drop=function(n){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 e=r[t];e&&(n||o.age)>i.now-e.was||delete r[t]},0,99)},i}})(f,"./dup"),f(function(t){f("./onto"),t.exports=function(t,e){if(this.on){var n=(this.opt||{}).lack||9e3;if("function"!=typeof t){if(!t)return;var o=t["#"]||t,i=(this.tag||"")[o];if(!i)return;return e&&(i=this.on(o,e),clearTimeout(i.err),i.err=setTimeout(function(){i.off()},n)),!0}o=e&&e["#"]||Math.random().toString(36).slice(2);if(!t)return o;var r=this.on(o,t,e);return r.err=r.err||setTimeout(function(){r.off(),r.next({err:"Error: No ACK yet.",lack:!0})},n),o}}})(f,"./ask"),f(function(t){function s(t){return t instanceof s?(this._={$:this}).$:this instanceof s?s.create(this._={$:this,opt:t}):new s(t)}function c(t,a){var u=+new Date,e=t._||{},s=e.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(u,((s||e).gk=+new Date)-u,"got keys"),a&&function t(){u=+new Date;for(var e,n,o,i=0,r={};i<9&&(e=l[i++]);)O(r,e,m(a,e),a[e],h);l=l.slice(i),(n={})[h]=r,r=n,d&&((o=function(){}).ram=o.faith=!0),n=l.length,console.STAT&&console.STAT(u,-(u-(u=+new Date)),"got copied some"),s&&(s.ga=+new Date),p.on("in",{"@":c,"#":f,put:r,"%":n?f=g(9):S,$:p.$,_:o,DBG:s}),console.STAT&&console.STAT(u,+new Date-u,"got in"),n&&setTimeout.turn(t)}(),a||p.on("in",{"@":t["#"]})}s.is=function(t){return t instanceof s||t&&t._&&t===t._.$||!1},s.version=.202,(s.chain=s.prototype).toJSON=function(){},f("./shim"),s.valid=f("./valid"),s.state=f("./state"),s.on=f("./onto"),s.dup=f("./dup"),s.ask=f("./ask"),function(){function $(t){if(t)if(t.out!==$){var e,n=this.as,o=n.at||n,i=o.$,r=o.dup,a=t.DBG;if((e=t["#"])||(e=t["#"]=g(9)),!r.check(e)){if(r.track(e),e=t._,t._="function"==typeof e?e:function(){},t.$&&t.$===(t.$._||"").$||(t.$=i),t["@"]&&!t.put&&function(t){var e,n=t["@"]||"",o=(t.$._||"").root;if(!(e=n._))return;e.acks=(e.acks||0)+1,(e.err=t.err)&&(t["@"]=e["#"],--e.stun);if(0==e.stun&&e.acks==e.all)o&&o.on("in",{"@":e["#"],err:t.err,ok:t.err?S:"shard"}),t.err&&b(e)}(t),!o.ask(t["@"],t)){if(a&&(a.u=+new Date),t.put)return void u(t);t.get&&s.on.get(t,i)}a&&(a.uc=+new Date),this.to.next(t),a&&(a.ua=+new Date),t.nts||t.NTS||(t.out=$,o.on("out",t),a&&(a.ue=+new Date))}}else this.to.next(t)}function u(a){if(a){var u=a._||"",t=u.root=((u.$=a.$||"")._||"").root;if(a["@"]&&u.faith&&!u.miss)return a.out=$,void t.on("out",a);u.latch=t.hatch,u.match=t.hatch=[];var s=a.put,c=u.DBG=a.DBG,f=+new Date;if(!s["#"]||!s["."]){c&&(c.p=f),u["#"]=a["#"],u.msg=a,u.all=0,u.stun=1;var l=Object.keys(s);console.STAT&&console.STAT(f,((c||u).pk=+new Date)-f,"put sort");var h,p,d,g,v,y,k,m=0;!function t(e){if(h!=m){if(!(d=l[h=m]))return console.STAT&&console.STAT(f,((c||u).pd=+new Date)-f,"put"),void b(u);(g=s[d])?(k=g._)?d!==k["#"]?y=_+T(d)+"soul not same.":(v=k[">"])||(y=_+T(d)+"no state."):y=_+T(d)+"no meta.":y=_+T(d)+"no node.",p=Object.keys(g||{})}if(y)return a.err=u.err=y,void b(u);var n,o=0;for(e=e||0;e++<9&&(n=p[o++]);)if("_"!==n){var i=g[n],r=v[n];if(S===r){y=_+T(n)+"on"+T(d)+"no state.";break}if(!D(i)){y=_+T(n)+"on"+T(d)+"bad "+typeof i+T(i);break}w(i,n,d,r,a)}(p=p.slice(o)).length?x(t):(++m,p=null,t(e))}()}}}function w(t,e,n,o,i){var r,a=i._||"",u=a.root,s=u.graph,c=s[n]||j,f=m(c,e,1),l=c[e],h=a.DBG;(r=console.STAT)&&(s[n]&&l||(r.has=(r.has||0)+1));var p=k();if(p<o)return setTimeout(function(){w(t,e,n,o,i)},(r=o-p)>y?y:r),void(console.STAT&&console.STAT((h||a).Hf=+new Date,r,"future"));if((!(o<f)||a.miss)&&(a.faith||o!==f||!(t===l||v(t)<=v(l))||a.miss)){a.stun++;var d=i["#"]+a.all++,g={toString:function(){return d},_:a};g.toJSON=g.toString,h&&(h.ph=h.ph||+new Date),u.on("put",{"#":g,"@":i["@"],put:{"#":n,".":e,":":t,">":o},_:a})}}function n(t){var e;(e=(t._||"").DBG)&&(e.pa=+new Date,e.pm=e.pm||+new Date);var n,o=this.as,i=o.graph,r=t._,a=t.put,u=a["#"],s=a["."],c=a[":"],f=a[">"];t["#"];(n=r.msg)&&(n=n.put)&&(n=n[u])&&O(n,s,f,c,u),i[u]=O(i[u],s,f,c,u),(n=(o.next||"")[u])&&n.on("in",t),this.to.next(t),b(r)}function b(t,e){var n;if(!t.stop&&(t.err||!(0<--t.stun))&&(t.stop=1,n=t.root)){var o=t.match;o.end=1,o===n.hatch&&(!(o=t.latch)||o.end?delete n.hatch:n.hatch=o),t.hatch&&t.hatch(),setTimeout.each(t.match,function(t){t&&t()}),!(e=t.msg)||t.err||e.err||(e.out=$,t.root.on("out",e))}}s.create=function(t){t.root=t.root||t,t.graph=t.graph||{},t.on=t.on||s.on,t.ask=t.ask||s.ask,t.dup=t.dup||s.dup();var e=t.$.opt(t.opt);return t.once||(t.on("in",$,t),t.on("out",$,t),t.on("put",n,t),s.on("create",t),t.on("create",t)),t.once=1,e},s.on.put=u,console.log("BEWARE: BETA VERSION OF NEW GUN! NOT ALL FEATURES FINISHED!");var _="Error: Invalid graph!",T=function(t){return" '"+(""+t).slice(0,9)+"...' "},v=JSON.stringify,y=2147483647,k=s.state}(),s.on.get=function(t,e){var n=e._,o=t.get,i=o["#"],r=n.graph[i],a=o["."],u=((n.next||(n.next={}))[i],(t._||{}).DBG=t.DBG);if(u&&(u.g=+new Date),!r)return n.on("get",t);if(a){if("string"!=typeof a||S===r[a])return n.on("get",t);r=O({},a,m(r,a),r[a],i)}r&&c(t,r),n.on("get",t)},s.on.get.ack=c,s.chain.opt=function(t){t=t||{};var n=this._,e=t.peers||t;return Object.plain(t)||(t={}),Object.plain(n.opt)||(n.opt=t),"string"==typeof e&&(e=[e]),e instanceof Array&&(Object.plain(n.opt.peers)||(n.opt.peers={}),e.forEach(function(t){var e={};e.id=e.url=t,n.opt.peers[t]=n.opt.peers[t]||e})),n.opt.peers=n.opt.peers||{},o(t,function t(e){var n=this[e];this&&this.hasOwnProperty(e)||"string"==typeof n||Object.empty(n)?this[e]=n:(!n||n.constructor===Object||n instanceof Array)&&o(n,t)}),s.on("opt",n),n.opt.uuid=n.opt.uuid||function(t){return s.state().toString(36).replace(".","")+String.random(t||12)},this};var S,n,o=function(t,e){Object.keys(t).forEach(e,t)},g=String.random,x=setTimeout.turn,D=s.valid,m=s.state.is,O=s.state.ify,j={};(s.log=function(){return s.log.off||n.log.apply(n,arguments),[].slice.call(arguments).join(" ")}).once=function(t,e,n){return(n=s.log.once)[t]=n[t]||0,n[t]++||s.log(e)},"undefined"!=typeof window&&((window.GUN=window.Gun=s).window=window);try{void 0!==e&&(e.exports=s)}catch(t){}((t.exports=s).window||{}).console=(s.window||{}).console||{log:function(){}},(n=console).only=function(t,e){return n.only.i&&t===n.only.i&&n.only.i++&&(n.log.apply(n,arguments)||e)},s.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,e){if(-1===(t=t||1)||1/0===t)return this._.root.$;if(1===t)return(this._.back||this._).$;var n=this._;if("string"==typeof t&&(t=t.split(".")),t instanceof Array){for(var o=0,i=t.length,r=n;o<i;o++)r=(r||u)[t[o]];return void 0!==r?e?this:r:(r=n.back)?r.$.back(t,e):void 0}if("function"!=typeof t)return"number"==typeof t?(n.back||n).$.back(t-1):this;var a;for(r={back:n};(r=r.back)&&void 0===(a=t(r,e)););return a};var u={}})(f,"./back"),f(function(t){var s=f("./root");function l(t,e){if(e=e||this.as||t.$._,(!t.$$||this===s.on)&&t.put&&!e.soul){var n=t.put||"",o=n["="]||n[":"],i=e.root,r=i.$.get(n["#"]).get(n["."])._;if("string"==typeof(o=g(o))){if(!(r.echo||(r.echo={}))[e.id]||(i.pass||"")[e.id]){if(a=i.pass){if(a[o+e.id])return;a[o+e.id]=1}((r.echo||(r.echo={}))[e.id]=e).has&&(e.link=o);var a,u=i.$.get(r.link=o)._;(u.echo||(u.echo={}))[r.id]=r,((a=e.ask||"")[""]||e.lex)&&u.on("out",{get:{"#":o}}),setTimeout.each(Object.keys(a),function(t,e){t&&(e=a[t])&&e.on("out",{get:{"#":o,".":t}})},0,99)}}else this===s.on&&((r.echo||(r.echo={}))[e.id]=e)}}function h(t,n){var o,e,i=t.put||"",r=p!==i["="]?i["="]:i[":"],a=n.root;if(p===r){if(n.soul&&p!==n.put)return;if(e=(t.$$||t.$||"")._||"",t["@"]&&(p!==e.put||p!==n.put))return;return(o=n.link||t.linked)&&delete(a.$.get(o)._.echo||"")[n.id],n.has&&(n.link=null),n.put=p,void setTimeout.each(Object.keys(n.next||""),function(t,e){(e=n.next[t])&&(o&&delete(a.$.get(o).get(t)._.echo||"")[e.id],e.on("in",{get:t,put:p,$:e.$}))},0,99)}n.soul||t.$$||(o=g(r),e=t.$._||"",(o!==e.link&&(!n.has||e.link)||(a.pass||"")[n.id]&&"string"!=typeof o)&&(delete(e.echo||"")[n.id],h({get:n.get,put:p,$:t.$,linked:t.linked=t.linked||e.link},n)))}function a(t,e){var n=this.as,o=n.$._,i=(o.root,n.get||""),r=(t.put||"")[i["#"]]||"";if(!t.put||"string"==typeof i["."]&&p===r[i["."]]){if(p!==o.put)return;if(!o.soul&&!o.has)return;return o.ack=(o.ack||0)+1,void o.on("in",{get:o.get,put:o.put=p,$:o.$,"@":t["@"]})}(t._||{}).miss=1,s.on.put(t)}s.chain.chain=function(t){var e,n=this._,o=new(t||this).constructor(this),i=o._;return i.root=e=n.root,i.id=++e.once,i.back=this._,i.on=s.on,i.on("in",s.on.in,i),i.on("out",s.on.out,i),o},s.on.out=function(t){var e,n,o=this.as,i=o.back,r=o.root;if(t.$||(t.$=o.$),this.to.next(t),!o.err){if(e=t.get){if(r.pass&&(r.pass[o.id]=o),o.lex&&Object.keys(o.lex).forEach(function(t){n[t]=o.lex[t]},n=t.get=t.get||{}),e["#"]||o.soul){if(e["#"]=e["#"]||o.soul,t["#"]||(t["#"]=u(9)),i=r.$.get(e["#"])._,e=e["."]){if(c(i.put,e)&&(n=i.ask&&i.ask[e],(i.ask||(i.ask={}))[e]=i.$.get(e)._,i.on("in",{get:e,put:{"#":i.soul,".":e,":":i.put[e],">":v(r.graph[i.soul],e)}}),n))return}else{if(n=i.ask&&i.ask[""],(i.ask||(i.ask={}))[""]=i,p!==i.put&&(i.on("in",i),n))return;t.$=i.$}return r.ask(a,t),r.on("in",t)}if(e["."])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 e["."]=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.$})},s.on.in=function(e,n){var t,o=(n=n||this.as).root,i=e.$||(e.$=n.$),r=(i||"")._||d,a=e.put||"",u=a["#"],s=a["."],c=p!==a["="]?a["="]:a[":"],f=a[">"]||-1/0;if(p!==e.put&&(p===a["#"]||p===a["."]||p===a[":"]&&p===a["="]||p===a[">"]))return g(a)?void n.on("in",{$:r.back.$,put:{"#":u=r.back.soul,".":s=r.has||r.get,"=":a,">":v(r.back.put,s)},via:e}):(u=((a||"")._||"")["#"])?(i=n.root.$.get(u),setTimeout.each(Object.keys(a).sort(),function(t){"_"!=t&&p!==(f=v(a,t))&&n.on("in",{$:i,put:{"#":u,".":t,"=":a[t],">":f},VIA:e})})):void console.log("chain not yet supported for",a,"...",e,n);(e.seen||"")[n.id]||(((e.seen||(e.seen=function(){}))[n.id]=n)!==r&&(Object.keys(e).forEach(function(t){a[t]=e[t]},a={}),a.get=n.get||a.get,n.soul||n.has?r.soul&&(a.$=n.$,a.$$=a.$$||r.$):a.$$$=a.$$$||n.$,e=a),h(e,n),(n.soul||e.$$)&&f>=v(o.graph[u],s)&&((a=o.$.get(u)._).put=y(a.put,s,f,c,u)),!r.soul&&f>=v(o.graph[u],s)&&(t=(o.$.get(u)._.next||"")[s])&&(t.put=c,"string"==typeof(a=g(c))&&(t.put=o.$.get(a)._.put||c)),this.to&&this.to.next(e),n.any&&setTimeout.each(Object.keys(n.any),function(t){(t=n.any[t])&&t(e)},0,99),n.echo&&setTimeout.each(Object.keys(n.echo),function(t){(t=n.echo[t])&&t.on("in",e)},0,99),((e.$$||"")._||r).soul&&(t=n.next)&&(t=t[s])&&(a={},Object.keys(e).forEach(function(t){a[t]=e[t]}),a.$=(e.$$||e.$).get(a.get=s),delete a.$$,delete a.$$$,t.on("in",a)),l(e,n))},s.on.link=l,s.on.unlink=h;var p,d={},u=String.random,g=s.valid,c=function(t,e){return t&&Object.prototype.hasOwnProperty.call(t,e)},e=s.state,v=e.is,y=e.ify})(f,"./chain"),f(function(t){var g=f("./root");function r(t){var e=this.at||this.on;if(!t||e.soul||e.has)return this.off();if(t=(t=(t=t.$||t)._||t).id){var n;e.map;if((n=this.seen||(this.seen={}))[t])return!0;n[t]=!0}}g.chain.get=function(t,e,n){var o,i;if("string"==typeof t){if(0==t.length)return(o=this.chain())._.err={err:g.log("0 length key!",t)},e&&e.call(o,o._.err),o;(o=((f=this._).next||a)[t])||(o=t&&function(t,e){var n=e._,o=n.next,i=e.chain()._;o=o||(n.next={});o[i.get=t]=i,e===n.root.$?i.soul=t:(n.soul||n.has)&&(i.has=t);return i}(t,this)),o=o&&o.$}else{if("function"==typeof t){if(!0===e)return function(t,e,n,o){var a,u=t._,s=0;if(a=u.soul||u.link)return e(a,o,u);if(u.jam)return u.jam.push([e,o]);u.jam=[[e,o]],t.get(function(t,e){if(!(v===t.put&&!u.root.opt.super&&(a=Object.keys(u.root.opt.peers).length)&&++s<=a)){e.rid(t);var n,o=(o=t.$)&&o._||{},i=0;for(a=u.jam,delete u.jam;n=a[i++];){var r=n[0];n=n[1],r&&r(o.link||o.soul||g.valid(t.put)||((t.put||{})._||{})["#"],n,t,e)}}},{out:{get:{".":!0}}})}(this,t,0,n),this;var c,f=(o=this)._,l=e||{},h=f.root;l.at=f,l.ok=t;var p={};function d(e,t,n){if(!d.stun&&(!(o=h.pass)||o[c])){var o,i=e.$._,r=(e.$$||"")._,a=(r||i).put,u=!i.has&&!i.soul,s={};if(!u&&v!==a||(a=v===((o=e.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,s={}),(s.run||0)<d.id)return s}),s.run||o.on(""+i.id,s={}),!s.run&&r&&o.on(""+r.id,s={}),d.id>s.run&&(s.stun&&!s.stun.end||(s.stun=o.on("stun"),s.stun=s.stun&&s.stun.last),s.stun&&!s.stun.end)))return void((s.stun.add||(s.stun.add={}))[c]=function(){d(e,t,1)});if(v===a&&(n=0),(o=h.hatch)&&!o.end&&v===l.hatch&&!n){if(p[i.$._.id])return;return p[i.$._.id]=1,void o.push(function(){d(e,t,1)})}p={}}l.on?l.ok.call(i.$,a,i.get,e,t||d):l.v2020?l.ok(e,t||d):(Object.keys(e).forEach(function(t){o[t]=e[t]},o={}),(e=o).put=a,l.ok.call(l.as,e,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,e,n);if("string"==typeof(i=u(t)))return this.get(i,e,n);(i=this.get.next)&&(o=i(this,t))}return o?e&&"function"==typeof e&&o.get(e,n):((o=this.chain())._.err={err:g.log("Invalid get request!",t)},e&&e.call(o,o._.err)),o};var v,a={},u=g.valid})(f,"./get"),f(function(t){var g=f("./root");function v(e,t){if(t){t=(t._||"").id||t;var n,o=e.root.stun||(e.root.stun={on:g.on}),i={};e.stun||(e.stun=o.on("stun",function(){})),(n=o.on(""+t))&&n.the.last.next(i),i.run>=e.run||o.on(""+t,function(t){if(e.stun.end)return this.off(),void this.to.next(t);t.run=t.run||e.run,t.stun=t.stun||e.stun})}}function u(e){if(e.err)u.end(e.stun,e.root);else if(!e.todo.length&&!e.end&&Object.empty(e.wait)){e.end=1;var t,n=e.$.back(-1)._,o=n.root,i=n.ask(function(t){o.on("ack",t),t.err&&g.log(t),++r>(e.acks||0)&&this.off(),e.ack&&e.ack(t,this)},e.opt),r=0,a=e.stun;(t=function(){a&&(u.end(a,o),setTimeout.each(Object.keys(a=a.add||""),function(t){(t=a[t])&&t()}))}).hatch=t,e.via._.on("out",{put:e.out=e.graph,opt:e.opt,"#":i,_:t})}}g.chain.put=function(t,e,p){var n=this,o=n._,i=o.root;(p=p||{}).root=o.root,p.run||(p.run=i.once),v(p,o.id),p.ack=p.ack||e,p.via=p.via||n,p.data=p.data||t,p.soul||(p.soul=o.soul||"string"==typeof e&&e);var d=p.state=p.state||g.state();return"function"==typeof t?t(function(t){p.data=t,n.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||u,function t(){var e,n,o,i,r,a=p.todo,u=a.pop(),s=u.it;u.ref&&u.ref._.id;if(v(p,u.ref),(i=u.todo)&&(s=s[n=i.pop()],i.length&&a.push(u)),n&&(a.path||(a.path=[])).push(n),!(e=k(s))&&!(r=g.is(s))){if(!Object.plain(s))return(p.ack||y).call(p,p.out={err:p.err=g.log("Invalid data: "+(s&&(i=s.constructor)&&i.name||typeof s)+" at "+(p.via.back(function(t){t.get&&i.push(t.get)},i=[])||i.join("."))+"."+(a.path||[]).join("."))}),void p.ran(p);for(var c=p.seen||(p.seen=[]),f=c.length;f--;)if(s===(i=c[f]).it){e=s=i.link;break}}if(n&&e)u.node=m(u.node,n,d,s);else{p.seen.push(o={it:s,link:{},todo:r?[]:Object.keys(s).sort().reverse(),path:(a.path||[]).slice(),up:u}),u.node=m(u.node,n,d,o.link),!r&&o.todo.length&&a.push(o);var l=p.seen.length;function h(t,e){if(o.link["#"])return p.ran(p);e&&(e.off(),e.rid(t));var n=t.soul||(i=(t.$$||t.$)._||"").soul||i.link||((i=i.put||"")._||"")["#"]||i["#"]||((i=t.put||"")&&t.$$?i["#"]:(i["="]||i[":"]||"")["#"]);v(p,t.$),n||u.link["#"]?(n||(n=[],(t.$$||t.$).back(function(t){if(i=t.soul||t.link)return n.push(i);n.push(t.get)}),n=n.reverse().join("/")),o.link["#"]=n,r||(((p.graph||(p.graph={}))[n]=o.node||(o.node={_:{}}))._["#"]=n),delete p.wait[l],o.wait&&setTimeout.each(o.wait,function(t){t&&t()}),p.ran(p)):(u.wait||(u.wait=[])).push(function(){h(t,e)})}(p.wait||(p.wait={}))[l]="",i=(o.ref=r?s:n?u.ref.get(n):u.ref)._,(i=s&&(s._||"")["#"]||i.soul||i.link)?h({soul:i}):o.ref.get(h,{run:p.run,v2020:1,out:{get:{".":" "}}})}if(!a.length)return p.ran(p);p.turn(t)}()):function(e){var n,t=e.via._;e.via=e.via.back(function(t){if(t.soul||!t.get)return t.$;n=e.data,(e.data={})[t.get]=n}),e.via&&e.via._.soul||(e.via=t.root.$.get(((e.data||"")._||"")["#"]||t.$.back("opt.uuid")()));e.via.put(e.data,e.ack,e)}(p),n},u.end=function(t,e){t.end=y,t.the.to===t&&t===t.the.last&&delete e.stun,t.off()};var y=function(){},r=setTimeout.turn,k=g.valid,m=g.state.ify})(f,"./put"),f(function(t){var e=f("./root");f("./chain"),f("./back"),f("./put"),f("./get"),t.exports=e})(f,"./index"),f(function(t){var d=f("./index");d.chain.on=function(t,e,n,o){var i,r=this._;r.root;if("string"==typeof t)return e?(i=r.on(t,e,n||r,o),n&&n.$&&(n.subs||(n.subs=[])).push(i),this):r.on(t);var a=e;(a=!0===a?{change:!0}:a||{}).not=1,a.on=1;return this.get(t,a),this},d.chain.once=function(s,c){if(c=c||{},!s)return function(t,e,n){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."),(n=t.chain())._.nix=t.once(function(t,e){n._.on("in",this._)}),n._.lex=t._.lex,n}(this);var f,l=this._,h=l.root,p=(l.put,String.random(7));return this.get(function(t,e,n,o){var i=this,r=i._,a=r.one||(r.one={});function u(){r.has||r.soul||(r={put:t,get:e}),void 0===(f=r.put)&&(f=((n.$$||"")._||"").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(),s.call(i,f,r.get))}o.stun||""!==a[p]&&(!0!==(f=d.valid(t))?"string"!=typeof f&&(clearTimeout(a[p]),a[p]=setTimeout(u,c.wait||99)):u())},{on:1}),this},d.chain.off=function(){var n,t=this._,o=t.back;if(o)return t.ack=0,(n=o.next)&&n[t.get]&&delete n[t.get],(n=o.ask)&&delete n[t.get],(n=o.put)&&delete n[t.get],(n=t.soul)&&delete o.root.graph[n],(n=t.map)&&Object.keys(n).forEach(function(t,e){(e=n[t]).link&&o.root.$.get(e.link).off()}),(n=t.next)&&Object.keys(n).forEach(function(t,e){n[t].$.off()}),t.on("off",{}),this}})(f,"./on"),f(function(t){var s=f("./index"),o=s.chain.get.next;function r(t){this.to.next(t);var e,n=this.as,o=t.$._,i=t.put;(o.soul||t.$$)&&((e=n.lex)&&!String.match(t.get||(i||"")["."],e["."]||e["#"]||e)||s.on.link(t,n))}s.chain.get.next=function(t,e){var n;return Object.plain(e)?(n=((n=e["#"])||"")["="]||n)?t.get(n):((n=t.chain()._).lex=e,t.on("in",function(t){String.match(t.get||(t.put||"")["."],e["."]||e["#"]||e)&&n.on("in",t),this.to.next(t)}),n.$):(o||c)(t,e)},s.chain.map=function(a,t,e){var n,u,o=this,i=o._;return Object.plain(a)&&(n=a["."]?a:{".":a},a=void 0),a?(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."),u=o.chain(),o.map().on(function(t,e,n,o){var i=(a||c).call(this,t,e,n,o);if(void 0!==i){if(t===i)return u._.on("in",n);if(s.is(i))return u._.on("in",i._);var r={};Object.keys(n.put).forEach(function(t){r[t]=n.put[t]},r),r["="]=i,u._.on("in",{get:e,put:r})}})):(u=i.each)||((i.each=u=o.chain())._.lex=n||u._.lex||i.lex,u._.nix=o.back("nix"),o.on("in",r,u._)),u};var c=function(){}})(f,"./map"),f(function(t){var u=f("./index");u.chain.set=function(t,i,e){var n,r,a=this,o=a.back(-1);return i=i||function(){},(e=e||{}).item=e.item||t,(n=((t||"")._||"")["#"])&&((t={})["#"]=n),"string"==typeof(r=u.valid(t))?a.get(n=r).put(t,i,e):u.is(t)?(a.put(function(o){t.get(function(t,e,n){if(!t)return i.call(a,{err:u.log('Only a node can be linked! Not "'+n.put+'"!')});(r={})[t]={"#":t},o(r)},!0)}),t):(Object.plain(t)&&(t=o.get(n=a.back("opt.uuid")()).put(t)),a.get(n||o.back("opt.uuid")(7)).put(t,i,e))}})(f,"./set"),f(function(t){f("./shim");var T;try{t.exports=function(s){var g=function(){},v=s.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,e,n){try{e(void 0,JSON.parse(t,n))}catch(t){e(t)}},k=JSON.stringifyAsync||function(t,e,n,o){try{e(void 0,JSON.stringify(t,n,o))}catch(t){e(t)}},m=s.dup,$=m.check,w=m.track,u=(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&&(u.d+=t.length||0,++u.c);var e,n=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 e,n=+new Date,o=0;o<r&&(e=i[o++]);)u(e,a);i=i.slice(o),console.STAT&&console.STAT(n,+new Date-n,"hear loop"),b(a),i.length&&y(t,0)}()}),void(t="")):"{"===o||(t["#"]||Object.plain(t))&&(e=t)?e?u.one(e,a,n):void i(t,function(t,e){if(t||!e)return g.say({dam:"!",err:"DAM JSON parse error."},a);u.one(e,a,n)}):void 0}});function b(t){var e=t.batch,n="string"==typeof e;if(n&&(e+="]"),t.batch=t.tail=null,e&&(n?!(e.length<3):e.length)){if(!n)try{e=1===e.length?e[0]:JSON.stringify(e)}catch(t){return v.log("DAM JSON stringify error",t)}e&&_(e,t)}}function _(e,n){try{var t=n.wire;n.say?n.say(e):t.send&&t.send(e),g.say.d+=e.length||0,++g.say.c}catch(t){(n.queue=n.queue||[]).push(e)}}u.one=function(t,e,n){var o,i,r,a,u;if(t.DBG&&(t.DBG=u={DBG:t.DBG}),u&&(u.h=n),u&&(u.hp=+new Date),(o=t["#"])||(o=t["#"]=String.random(9)),!((r=$(o))||(i=t["##"])&&(r=t["@"]||t.get&&o)&&m.check(a=r+i))){if((t._=function(){}).via=g.leap=e,(r=t["><"])&&"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,e,s),void w(o);n=+new Date;u&&(u.is=n),e.SI=o,s.on("in",g.last=t),u&&(u.hd=+new Date),console.STAT&&console.STAT(n,+new Date-n,t.get?"msg get":t.put?"msg put":"msg"),(r=w(o)).via=e,t.get&&(r.it=t),a&&w(a),g.leap=g.last=null}},u.c=u.d=0,function(){var h,p=0;function t(t,e){var n;return e instanceof Object?(Object.keys(e).sort().forEach(o,{to:n={},on:e}),n):e}function o(t){this.to[t]=this.on[t]}g.hash=function(i,r){var a,u,s,c=+new Date;k(i.put,function t(e,n){var o=(u=u||(s=n||"")).slice(0,32768);a=String.hash(o,a),(u=u.slice(32768))?y(t,0):(console.STAT&&console.STAT(c,+new Date-c,"say json+hash"),i._.$put=s,i["##"]=a,d(i,r),delete i._.$put)},t)};var d=g.say=function(r,t){var e;if((e=this)&&(e=e.to)&&e.next&&e.next(r),!r)return!1;var n,a,u=r["@"],s=r._||(r._=function(){}),o=r.DBG,i=+new Date;if(s.y=s.y||i,t||o&&(o.y=i),(n=r["#"])||(n=r["#"]=String.random(9)),h||w(n),r.put&&(r.err||(m.s[n]||"").err))return!1;if(r["##"]||T===r.put||s.via||!u){if(!t&&u&&(t=(e=m.s[u])&&(e.via||(e=e.it)&&(e=e._)&&e.via)||(e=g.last)&&u===e["#"]&&g.leap),!t&&u){if(m.s[u])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=s.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 e=+new Date;h=1;var n=s.raw;s.raw=a;for(var o,i=0;i<9&&(o=(f||"")[i++]);)(o=c[o])&&d(r,o);s.raw=n,h=0,f=f.slice(i),console.STAT&&console.STAT(e,+new Date-e,"say loop"),f.length&&(y(t,0),u&&w(u))}()}if(!t.wire&&g.wire&&g.wire(t),n!==t.last){if(t.last=n,t===s.via)return!1;if((e=s.yo)&&(e[t.url]||e[t.pid]||e[t.id]))return!1;if(console.STAT&&console.STAT(i,((o||s).yp=+new Date)-(s.y||i),"say prep"),!h&&u&&w(u),t.batch){if(t.tail=(e=t.tail||0)+a.length,t.tail<=v.pack)return void(t.batch+=(e?",":"")+a);b(t)}t.batch="[";var l=+new Date;setTimeout(function(){console.STAT&&console.STAT(l,+new Date-l,"0ms TO"),b(t)},v.gap),_(a,t),console.STAT&&u===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(n,o){if(!n)return"";var i,r,a=n._||{};if(r=a.raw)return r;if("string"==typeof n)return n;var t=n["##"],e=n["@"];if(t&&e){if(!a.via&&$(e+t))return!1;if((r=(m.s[e]||"").it)||(r=g.last)&&e===r["#"]){if(t===r["##"])return!1;r["##"]||(r["##"]=t)}}if(!n.dam){var u=0,s=[];for(var c in r=v.peers){var f=r[c];if(s.push(f.url||f.pid||f.id),6<++u)break}1<u&&(n["><"]=s.join())}if(i=a.$put)return r={},Object.keys(n).forEach(function(t){r[t]=n[t]}),r.put=":])([:",void k(r,function(t,e){if(!t){var n=+new Date;r=e.indexOf('"put":":])([:"'),l(T,e=e.slice(0,r+6)+i+e.slice(r+14)),console.STAT&&console.STAT(n,+new Date-n,"say slice")}});function l(t,e){t||(a.raw=e,d(n,o))}k(n,l)}}(),g.hi=function(e){var t=e.wire||{};e.id?v.peers[e.url||e.id]=e:(t=e.id=e.id||String.random(9),g.say({dam:"?",pid:s.opt.pid},v.peers[t]=e),delete m.s[e.last]),e.met=e.met||+new Date,t.hied||s.on(t.hied="hi",e),t=e.queue,e.queue=[],setTimeout.each(t||[],function(t){_(t,e)},0,9)},g.bye=function(t){s.on("bye",t);var e=+new Date;e-=t.met||e,g.bye.time=((g.bye.time||e)+e)/2},g.hear["!"]=function(t,e){v.log("Error:",t.err)},g.hear["?"]=function(t,e){t.pid&&(e.pid||(e.pid=t.pid),t["@"])||(g.say({dam:"?",pid:v.pid,"@":t["#"]},e),delete m.s[e.last])},s.on("create",function(t){t.opt.pid=t.opt.pid||String.random(9),this.to.next(t),t.on("out",g.say)}),s.on("bye",function(t,e){t=v.peers[t.id||t]||t,this.to.next(t),t.bye?t.bye():(e=t.wire)&&e.close&&e.close(),delete v.peers[t.id],t.wire=null});var o={};return s.on("bye",function(t,e){this.to.next(t),(e=console.STAT)&&(e.peers=(e.peers||0)-1),(e=t.url)&&(o[e]=!0,setTimeout(function(){delete o[e]},v.lack||9e3))}),s.on("hi",function(e,n){this.to.next(e),(n=console.STAT)&&(n.peers=(n.peers||0)+1),(n=e.url)&&o[n]&&(delete o[n],v.super||setTimeout.each(Object.keys(s.next),function(t){s.next[t];(n={})[t]=s.graph[t],n=String.hash(n),g.say({"##":n,get:{"#":t}},e)}))}),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 e=c.window||{},n=o.WebSocket||e.WebSocket||e.webkitWebSocket||e.mozWebSocket;if(n){o.WebSocket=n;var i=o.mesh=o.mesh||c.Mesh(t);i.wire||o.wire;i.wire=o.wire=u,setTimeout(function(){o.super||t.on("out",{dam:"hi"})},1);var r=1998,a=""+void 0!=typeof document&&document}}}function u(e){try{if(!e||!e.url)return n&&n(e);var t=e.url.replace(/^http/,"ws"),n=e.wire=new o.WebSocket(t);return n.onclose=function(){o.mesh.bye(e),s(e)},n.onerror=function(t){s(e)},n.onopen=function(){o.mesh.hi(e)},n.onmessage=function(t){t&&o.mesh.hear(t.data||t,e)},n}catch(t){}}function s(e){clearTimeout(e.defer),a&&e.retry<=0||(e.retry=(e.retry||o.retry+1||60)-(-e.tried+(e.tried=+new Date)<4*r?1:0),e.defer=setTimeout(function t(){if(a&&a.hidden)return setTimeout(t,r);u(e)},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,e){this[t]=e},removeItem:function(t){delete this[t]},getItem:function(t){return this[t]}}),Gun.on("create",function e(n){this.to.next(n);var r,i,a=n.opt,u=(n.graph,[]);if(!1!==a.localStorage){a.prefix=a.file||"gun/";try{r=e[a.prefix]=e[a.prefix]||JSON.parse(o.getItem(a.prefix))||{}}catch(t){r=e[a.prefix]={}}n.on("get",function(t){this.to.next(t);var e,n,o,i=t.get;i&&(e=i["#"])&&((n=r[e]||void 0)&&(o=i["."])&&!Object.plain(o)&&(n=Gun.state.ify({},o,Gun.state.is(n,o),n[o],e)),Gun.on.get.ack(t,n))}),n.on("put",function(t){this.to.next(t);var e=t.put,n=e["#"],o=e["."];r[n]=Gun.state.ify(r[n],o,e[">"],e[":"],n),t["@"]||u.push(t["#"]),i=i||setTimeout(s,a.wait||1)})}function s(){var e,t=u;clearTimeout(i),i=!1,u=[];try{o.setItem(a.prefix,JSON.stringify(r))}catch(t){Gun.log((e=t||"localStorage failure")+" Consider using GUN's IndexedDB plugin for RAD for more storage space, https://gun.eco/docs/RAD#install"),n.on("localStorage:error",{err:e,get:a.prefix,put:r})}(e||Object.empty(a.peers))&&setTimeout.each(t,function(t){n.on("in",{"@":t,err:e,ok:0})})}})}})(f,"./localStorage")}();