diff --git a/gun.js b/gun.js index cb1fbd014..311fae3a8 100644 --- a/gun.js +++ b/gun.js @@ -1003,7 +1003,7 @@ if(!obj_is(at.opt.peers)){ at.opt.peers = {}} at.opt.peers = obj_to(tmp, at.opt.peers); } - at.opt.wsc = at.opt.wsc || {protocols:''} + at.opt.wsc = at.opt.wsc || {protocols:[]} at.opt.peers = at.opt.peers || {}; obj_to(opt, at.opt); // copies options on to `at.opt` only if not already taken. Gun.on('opt', at); diff --git a/gun.min.js b/gun.min.js index e72e1bc30..460974e7c 100644 --- a/gun.min.js +++ b/gun.min.js @@ -1 +1 @@ -!function(){function t(n){function o(t){return t.split("/").slice(-1).toString().replace(".js","")}return n.slice?t[o(n)]:function(e,i){n(e={exports:{}}),t[o(i)]=e.exports}}var n;"undefined"!=typeof window&&(n=window),"undefined"!=typeof global&&(n=global),n=n||{};var o=n.console||{log:function(){}};if("undefined"!=typeof module)var e=module;t(function(t){var n={};n.fns=n.fn={is:function(t){return!!t&&"function"==typeof t}},n.bi={is:function(t){return t instanceof Boolean||"boolean"==typeof t}},n.num={is:function(t){return!e(t)&&(t-parseFloat(t)+1>=0||1/0===t||-1/0===t)}},n.text={is:function(t){return"string"==typeof t}},n.text.ify=function(t){return n.text.is(t)?t:"undefined"!=typeof JSON?JSON.stringify(t):t&&t.toString?t.toString():t},n.text.random=function(t,n){var o="";for(t=t||24,n=n||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";t>0;)o+=n.charAt(Math.floor(Math.random()*n.length)),t--;return o},n.text.match=function(t,o){var e=!1;if(t=t||"",o=n.text.is(o)?{"=":o}:o||{},n.obj.has(o,"~")&&(t=t.toLowerCase(),o["="]=(o["="]||o["~"]).toLowerCase()),n.obj.has(o,"="))return t===o["="];if(n.obj.has(o,"*")){if(t.slice(0,o["*"].length)!==o["*"])return!1;e=!0,t=t.slice(o["*"].length)}if(n.obj.has(o,"!")){if(t.slice(-o["!"].length)!==o["!"])return!1;e=!0}if(n.obj.has(o,"+")&&n.list.map(n.list.is(o["+"])?o["+"]:[o["+"]],function(n){if(!(t.indexOf(n)>=0))return!0;e=!0}))return!1;if(n.obj.has(o,"-")&&n.list.map(n.list.is(o["-"])?o["-"]:[o["-"]],function(n){if(!(t.indexOf(n)<0))return!0;e=!0}))return!1;if(n.obj.has(o,">")){if(!(t>o[">"]))return!1;e=!0}if(n.obj.has(o,"<")){if(!(to?1:0):0}},n.list.map=function(t,n,o){return a(t,n,o)},n.list.index=1,n.obj={is:function(t){return!!t&&(t instanceof Object&&t.constructor===Object||"Object"===Object.prototype.toString.call(t).match(/^\[object (\w+)\]$/)[1])}},n.obj.put=function(t,n,o){return(t||{})[n]=o,t},n.obj.has=function(t,n){return t&&Object.prototype.hasOwnProperty.call(t,n)},n.obj.del=function(t,n){if(t)return t[n]=null,delete t[n],t},n.obj.as=function(t,n,o,e){return t[n]=t[n]||(e===o?{}:o)},n.obj.ify=function(t){if(r(t))return t;try{t=JSON.parse(t)}catch(n){t={}}return t},function(){function t(t,n){u(this,n)&&o!==this[n]||(this[n]=t)}var o;n.obj.to=function(n,o){return o=o||{},a(n,t,o),o}}(),n.obj.copy=function(t){return t?JSON.parse(JSON.stringify(t)):t},function(){function t(t,n){var o=this.n;if(!o||!(n===o||r(o)&&u(o,n)))return!!n||void 0}n.obj.empty=function(n,o){return!n||!a(n,t,{n:o})}}(),function(){function t(n,o){if(2===arguments.length)return t.r=t.r||{},void(t.r[n]=o);t.r=t.r||[],t.r.push(n)}var i=Object.keys;n.obj.map=function(a,s,f){var c,l,p,g,h,d=0,v=o(s);if(t.r=null,i&&r(a)&&(g=Object.keys(a),h=!0),e(a)||g)for(l=(g||a).length;d",o.drift=0,o.is=function(t,n,e){var i=n&&t&&t[x]&&t[x][o._]||e;if(i)return m(i=i[n])?i:-1/0},o.ify=function(t,n,e,r,u){if(!t||!t[x]){if(!u)return;t=i.soul.ify(t,u)}var a=g(t[x],o._);return l!==n&&n!==x&&(m(e)&&(a[n]=e),l!==r&&(t[n]=r)),t},o.to=function(t,n,e){var r=t[n];return d(r)&&(r=_(r)),o.ify(e,n,o.is(t,n),r,i.soul(t))},function(){function t(t,n){x!==n&&o.ify(this.o,n,this.s)}o.map=function(n,e,i){var r,u=d(u=n||e)?u:null;return n=k(n=n||e)?n:null,u&&!n?(e=m(e)?e:o(),u[x]=u[x]||{},v(u,t,{o:u,s:e}),u):(i=i||d(e)?e:r,e=m(e)?e:o(),function(o,u,a,s){if(!n)return t.call({o:a,s:e},o,u),o;n.call(i||this||{},o,u,a,s),h(a,u)&&r===a[u]||t.call({o:a,s:e},o,u)})}}();var l,p=e.obj,g=p.as,h=p.has,d=p.is,v=p.map,_=p.copy,b=e.num,m=b.is,y=e.fn,k=y.is,x=i._;n.exports=o})(t,"./state"),t(function(n){var o=t("./type"),e=t("./val"),i=t("./node"),r={};!function(){function t(t,o){if(!t||o!==i.soul(t)||!i.is(t,this.fn,this.as))return!0;this.cb&&(n.n=t,n.as=this.as,this.cb.call(n.as,t,o,n))}function n(t){t&&i.is(n.n,t,n.as)}r.is=function(n,o,e,i){return!(!n||!s(n)||l(n))&&!g(n,t,{cb:o,fn:e,as:i})}}(),function(){function t(t,r){var u;return(u=l(t,r))?u:(r.env=t,r.soul=o,i.ify(r.obj,n,r)&&(t.graph[e.rel.is(r.rel)]=r.node),r)}function n(n,o,r){var s,l,p=this,g=p.env;if(i._===o&&c(n,e.rel._))return r._;if(s=a(n,o,r,p,g)){if(o||(p.node=p.node||r||{},c(n,i._)&&(p.node._=h(n._)),p.node=i.soul.ify(p.node,e.rel.is(p.rel)),p.rel=p.rel||e.rel.ify(i.soul(p.node))),(l=g.map)&&(l.call(g.as||{},n,o,r,p),c(r,o))){if(n=r[o],u===n)return void f(r,o);if(!(s=a(n,o,r,p,g)))return}if(!o)return p.node;if(!0===s)return n;if(l=t(g,{obj:n,path:p.path.concat(o)}),l.node)return l.rel}}function o(t){var n=this,o=e.rel.is(n.rel),r=n.env.graph;n.rel=n.rel||e.rel.ify(t),n.rel[e.rel._]=t,n.node&&n.node[i._]&&(n.node[i._][e.rel._]=t),c(r,o)&&(r[t]=r[o],f(r,o))}function a(t,n,o,i,r){var u;return!!e.is(t)||(s(t)?1:(u=r.invalid)?(t=u.call(r.as||{},t,n,o),a(t,n,o,i,r)):void(r.err="Invalid value at '"+i.path.concat(n).join(".")+"'!"))}function l(t,n){for(var o,e=t.seen,i=e.length;i--;)if(o=e[i],n.obj===o.obj)return o;e.push(n)}r.ify=function(n,o,i){var r={path:[],obj:n};return o?"string"==typeof o?o={soul:o}:o instanceof Function&&(o.map=o):o={},o.soul&&(r.rel=e.rel.ify(o.soul)),o.graph=o.graph||{},o.seen=o.seen||[],o.as=o.as||i,t(o,r),o.root=r.node,o.graph}}(),r.node=function(t){var n=i.soul(t);if(n)return p({},n,t)},function(){function t(t,n){var o,u;if(i._===n){if(l(t,e.rel._))return;return void(this.obj[n]=h(t))}return(o=e.rel.is(t))?(u=this.opt.seen[o])?void(this.obj[n]=u):void(this.obj[n]=this.opt.seen[o]=r.to(this.graph,o,this.opt)):void(this.obj[n]=t)}r.to=function(n,o,e){if(n){var i={};return e=e||{seen:{}},g(n[o],t,{obj:i,graph:n,opt:e}),i}}}();var u,a=(o.fn.is,o.obj),s=a.is,f=a.del,c=a.has,l=a.empty,p=a.put,g=a.map,h=a.copy;n.exports=r})(t,"./graph"),t(function(n){function o(){this.cache={}}var e=t("./type");o.prototype.track=function(t){return this.cache[t]=e.time.is(),this.to||this.gc(),t},o.prototype.check=function(t){return!!e.obj.has(this.cache,t)&&this.track(t)},o.prototype.gc=function(){var t=this,n=e.time.is(),o=n;if(e.obj.map(t.cache,function(i,r){o=Math.min(n,i),n-i<3e5||e.obj.del(t.cache,r)}),e.obj.empty(t.cache))return void(t.to=null);var i=n-o,r=3e5-i;t.to=setTimeout(function(){t.gc()},r)},n.exports=o})(t,"./dup"),t(function(n){function i(t){return t instanceof i?(this._={gun:this}).gun:this instanceof i?i.create(this._={gun:this,opt:t}):new i(t)}i.is=function(t){return t instanceof i},i.version=.7,i.chain=i.prototype,i.chain.toJSON=function(){};var r=t("./type");r.obj.to(r,i),i.HAM=t("./HAM"),i.val=t("./val"),i.node=t("./node"),i.state=t("./state"),i.graph=t("./graph"),i.dup=t("./dup"),i.schedule=t("./schedule"),i.on=t("./onify")(),i._={node:i.node._,soul:i.val.rel._,state:i.state._,field:".",value:"="},function(){function t(t){var n,o=this,e=o.as;if(t.gun||(t.gun=e.gun),t["#"]||(t["#"]=i.text.random()),!e.dup.check(t["#"])){if(t["@"]){if(e.ack(t["@"],t))return;return e.dup.track(t["#"]),void i.on("out",p(t,{gun:e.gun}))}e.dup.track(t["#"]),n=p(t,{gun:e.gun}),t.get&&i.on("get",n),t.put&&i.on("put",n),i.on("out",n)}}i.create=function(n){n.on=n.on||i.on,n.root=n.root||n.gun,n.graph=n.graph||{},n.dup=n.dup||new i.dup,n.ask=i.on.ask,n.ack=i.on.ack;var o=n.gun.opt(n.opt);return n.once||(n.on("in",t,n),n.on("out",t,n)),n.once=1,o}}(),function(){function t(t,n,o,e){var r=this,u=i.state.is(o,n);if(!u)return r.err="Error: No state on '"+n+"' in node '"+e+"'!";var a=r.graph[e]||v,s=i.state.is(a,n,!0),f=a[n],c=i.HAM(r.machine,u,s,t,f);c.incoming||c.defer&&(r.defer=u<(r.defer||1/0)?u:r.defer),r.put[e]=i.state.to(o,n,r.put[e]),(r.diff||(r.diff={}))[e]=i.state.to(o,n,r.diff[e])}function n(t,n){var e=(this.gun._.next||v)[n];if(e){var r=this.map[n]={put:this.node=t,get:this.soul=n,gun:this.ref=e};g(t,o,this),i.on("node",r)}}function o(t,n){var o=this.graph,e=this.soul,r=this.ref._;o[e]=i.state.to(this.node,n,o[e]),(r.put||(r.put={}))[n]=t}function e(t,n){t.gun&&t.gun._.on("in",t)}i.on("put",function(o){if(!o["#"])return this.to.next(o);var r=this,a={gun:o.gun,graph:o.gun._.graph,put:{},map:{},machine:i.state()};if(i.graph.is(o.put,null,t,a)||(a.err="Error: Invalid graph!"),a.err)return a.gun.on("in",{"@":o["#"],err:i.log(a.err)});g(a.put,n,a),g(a.map,e,a),u!==a.defer&&i.schedule(a.defer,function(){i.on("put",o)},i.state),a.diff&&r.to.next(p(o,{put:a.diff}))})}(),function(){i.on("get",function(t){var n,o=this,e=t.get[h],r=t.gun._,u=r.graph[e],a=t.get[d],s=r.next||(r.next={}),f=(s[e]||v)._;if(!u||!f)return o.to.next(t);if(a){if(!l(u,a))return o.to.next(t);u=i.state.to(u,a)}else u=i.obj.copy(u);u=i.graph.node(u),n=f.ack,r.on("in",{"@":t["#"],how:"mem",put:u,gun:f.gun}),0e.ack&&!t.ack&&!l.val.rel.is(c)&&(e.ack=1),e.get&&t.get!==e.get&&(t=b(t,{get:e.get})),e.field&&a!==e&&(t=b(t,{gun:e.gun}),a.ack&&(e.ack=e.ack||a.ack)),p===c){if(o.to.next(t),e.soul)return;return u(e,t,o),e.field&&f(e,t),_(a.echo,e.id),void _(e.map,a.id)}return e.soul?(e.root._.now&&(t=b(t,{put:c=a.put})),o.to.next(t),u(e,t,o),void m(c,s,{at:t,cat:e})):(n=l.val.rel.is(c))?(r(e,t,a,n),o.to.next(t),void u(e,t,o)):l.val.is(c)?(e.field||e.soul?f(e,t):(a.field||a.soul)&&((a.echo||(a.echo={}))[e.id]=e,(e.map||(e.map={}))[a.id]=e.map[a.id]||{at:a}),o.to.next(t),void u(e,t,o)):(e.field&&a!==e&&d(a,"put")&&(e.put=a.put),(n=l.node.soul(c))&&a.field&&(a.put=e.root.get(n)._.put),o.to.next(t),u(e,t,o),r(e,t,a,n),void m(c,s,{at:t,cat:e}))}function r(t,n,o,e){if(e&&x!==t.get){var i=t.root.get(e)._;t.field?o=i:o.field&&r(o,n,o,e),o!==t&&((o.echo||(o.echo={}))[t.id]=t,t.field&&!(t.map||g)[o.id]&&f(t,n),i=(t.map||(t.map={}))[o.id]=t.map[o.id]||{at:o},e!==i.rel&&c(t,i.rel=e))}}function u(t,n,o){t.echo&&(t.field&&(n=b(n,{event:o})),m(t.echo,a,n))}function a(t){t.on("in",this)}function s(t,n){var o,e,i,r=this.cat,u=r.next||g,a=this.at;(x!==n||u[n])&&(o=u[n])&&(i=o._,i.field?(t&&t[y]&&l.val.rel.is(t)===l.node.soul(i.put)||(i.put=t),e=o):e=a.gun.get(n),i.on("in",{put:t,get:n,gun:e,via:a}))}function f(t,n){if(t.field||t.soul){var o=t.map;t.map=null,null!==o&&(p===o&&t.put!==p||(m(o,function(n){(n=n.at)&&_(n.echo,t.id)}),m(t.next,function(t,n){var o=t._;o.put=p,o.ack&&(o.ack=-1),o.on("in",{get:n,gun:t,put:p})})))}}function c(t,n){var o=t.root.get(n)._;if(t.ack)return o.ack=o.ack||-1,void o.on("out",{get:o={"#":n,gun:o.gun},"#":t.root._.ask(l.HAM.synth,o)});m(t.next,function(o,e){o._.on("out",{get:o={"#":n,".":e,gun:o},"#":t.root._.ask(l.HAM.synth,o)})})}var l=t("./root");l.chain.chain=function(){var t=this._,o=new this.constructor(this),r=o._;return r.root=n=t.root,r.id=++n._.once,r.back=this,r.on=l.on,l.on("chain",r),r.on("in",i,r),r.on("out",e,r),o},l.chain.chain.input=i;var p,g={},h=l.obj,d=h.has,v=h.put,_=h.del,b=h.to,m=h.map,y=l._.soul,k=l._.field,x=l.node._})(t,"./chain"),t(function(n){function o(t,n){var o=n._,e=o.next,i=n.chain(),r=i._;return e||(e=o.next={}),e[r.get=t]=i,o.root===n?r.soul=t:(o.soul||o.field)&&(r.field=t),i}function e(t){var n,o=this,e=o.as,i=t.gun,u=i._,s=t.put;r===s&&(s=u.put),(n=s)&&n[f._]&&(n=f.is(n))&&(n=u.root.get(n)._,r!==n.put&&(t=a(t,{put:n.put}))),e.use(t,t.event||o),o.to.next(t)}var i=t("./root");i.chain.get=function(t,n,r){if("string"!=typeof t){if(t instanceof Function){var u=this,a=u._;return r=n||{},r.use=t,r.out=r.out||{cap:1},r.out.get=r.out.get||{},"_"!=a.get&&(a.root._.now=!0),a.on("in",e,r),a.on("out",r.out),a.root._.now=!1,u}return s(t)?this.get(""+t,n,r):((r=this.chain())._.err={err:i.log("Invalid get request!",t)},n&&n.call(r,r._.err),r)}var u,f,l=this,p=l._,g=p.next||c;return(u=g[t])||(u=o(t,l)),(f=p.stun)&&(u._.stun=u._.stun||f),n&&n instanceof Function&&u.get(n,r),u};var r,u=i.obj,a=(u.has,i.obj.to),s=i.num.is,f=i.val.rel,c=(i.node._,{})})(t,"./get"),t(function(n){function e(t){t.batch=i;var n=t.opt||{},o=t.env=f.state.map(u,n.state);if(o.soul=t.soul,t.graph=f.graph.ify(t.data,o,t),o.err)return(t.ack||v).call(t,t.out={err:f.log(o.err)}),void(t.res&&t.res());t.batch()}function i(){var t=this;t.graph&&!h(t.stun,r)&&((t.res||_)(function(){t.ref._.on("out",{cap:3,gun:t.ref,put:t.out=t.env.graph,opt:t.opt,"#":t.gun.back(-1)._.ask(function(n){this.off(),t.ack&&t.ack(n,this)},t.opt)})},t),t.res&&t.res())}function r(t,n){if(t)return!0}function u(t,n,o,e){var i=this;!n&&e.path.length&&(i.res||_)(function(){var t=e.path,n=i.ref,o=(i.opt,0),r=t.length;for(o;o":{}}},e.node.soul(o)))&&n._&&(i=a(i)?{machine:i}:{machine:e.state()},i.union=n||e.obj.copy(n),i.vertex=n,i.node=o,!l(o,t,i)))return i.union},e.HAM.delta=function(t,o,i){return i=a(i)?{machine:i}:{machine:e.state()},t?(i.soul=e.node.soul(i.vertex=t),i.soul?(i.delta=e.node.soul.ify({},i.soul),l(i.node=o,n,i),i.delta):void 0):e.obj.copy(o)},e.HAM.synth=function(t,n){var o=this.as,r=o.gun._;if(!t.put||o["."]&&!f(t.put[o["#"]],r.get)){if(r.put!==i)return;return void r.on("in",{get:r.get,put:r.put=i,gun:r.gun})}t.gun=r.root,e.on("put",t)},e.HAM.synth_=function(t,n,o){var r=this.as||o,u=r._,a=u.root._,s={};if(!t.put){if(u.put!==i)return;return void u.on("in",{get:u.get,put:u.put=i,gun:r,via:t})}l(t.put,function(t,n){var o=this.graph;s[n]=e.HAM.delta(o[n],t,{graph:o}),o[n]=e.HAM.union(o[n],t)||o[n]},a),t.gun!==a.gun&&(s=t.put),l(s,function(o,r){var a=this,s=a.next||(a.next={}),l=s[r]||(s[r]=a.gun.get(r)),p=l._;if(p.put=a.graph[r],u.field&&!f(o,u.field))return(t=c(t,{})).put=i,void e.HAM.synth(t,n,u.gun);p.on("in",{put:o,get:r,gun:l,via:t})},a)}}();var i,r=e,u=r.num,a=u.is,s=r.obj,f=s.has,c=(s.put,s.to),l=s.map,p=e.node,g=(p.soul,p.is,p.ify,e.state),h=g.is,d=g.ify,v=e.val,_=v.is;v.rel.is})(t,"./index"),t(function(n){var o=t("./root");t("./index"),t("./opt"),t("./chain"),t("./back"),t("./put"),t("./get"),n.exports=o})(t,"./core"),t(function(n){var o=t("./core");o.chain.path=function(t,n,e){var i,r=this,u=r;if(e=e||{},e.path=!0,o.log.once("pathing","Warning: `.path` to be removed from core (but available as an extension), use `.get` chains instead. If you are opposed to this, please voice your opinion in https://gitter.im/amark/gun and ask others."),u===u._.root)return n&&n({err:o.log("Can't do that on root instance.")}),u;if("string"==typeof t){if(i=t.split(e.split||"."),1===i.length)return u=r.get(t,n,e),u._.opt=e,u;t=i}if(t instanceof Array){if(t.length>1){u=r;var a=0,s=t.length;for(a;a=1e4)return n();e||(clearTimeout(e),e=setTimeout(n,1e3))}),Gun.on("get",function(t){this.to.next(t);var n,o,e,r=t.gun,a=t.get;if((e=t.opt||{}).prefix=e.prefix||t.gun.back("opt.prefix")||"gun/",a&&(n=a[Gun._.soul])){var s=a["."];o=Gun.obj.ify(i.getItem(e.prefix+n)||null)||u[n]||void 0,o&&s&&(o=Gun.state.to(o,s)),(o||Gun.obj.empty(r.back("opt.peers")))&&r.on("in",{"@":t["#"],put:Gun.graph.node(o),how:"lS"})}})}})(t,"./adapters/localStorage"),t(function(n){function o(t){var n=s,o=this,e=t.wire||i(t,o);if(o.wsp&&o.wsp.count++,e)return e.readyState===e.OPEN?void e.send(n):void(t.queue=t.queue||[]).push(n)}function e(t,n,o){if(o&&t){try{t=JSON.parse(t.data||t)}catch(t){}if(t instanceof Array)for(var i,r=0;i=t[r++];)e(i,n,o);else o.wsp&&1===o.wsp.count&&((t.body||t).wsp=c),o.gun.on("in",t.body||t)}}function i(t,n){if(t&&t.url){var i=t.url.replace("http","ws"),f=t.wire=new a(i,n.opt.wsc.protocols,n.opt.wsc);return f.onclose=function(){r(t,n)},f.onerror=function(o){r(t,n),o&&o.code},f.onopen=function(){var e=t.queue;t.queue=[],u.obj.map(e,function(e){s=e,o.call(n,t)})},f.onmessage=function(o){e(o,t,n)},f}}function r(t,n){clearTimeout(t.defer),t.defer=setTimeout(function(){i(t,n)},2e3)}var u=t("./core");if("undefined"==typeof JSON)throw new Error("Gun depends on JSON. Please load it first:\najax.cdnjs.com/ajax/libs/json2/20110223/json2.js");var a;if("undefined"!=typeof window){a=window.WebSocket||window.webkitWebSocket||window.mozWebSocket;var s,f,c=function(){};u.on("out",function(t){this.to.next(t);var n=t.gun._.root._,e=n.wsp||(n.wsp={});if(!t.wsp||1!==e.count){if(s=JSON.stringify(t),n.udrain)return void n.udrain.push(s);n.udrain=[],clearTimeout(f),f=setTimeout(function(){if(n.udrain){var t=n.udrain;n.udrain=null,t.length&&(s=JSON.stringify(t),u.obj.map(n.opt.peers,o,n))}},1),e.count=0,u.obj.map(n.opt.peers,o,n)}})}})(t,"./polyfill/request")}(); \ No newline at end of file +!function(){function t(n){function o(t){return t.split("/").slice(-1).toString().replace(".js","")}return n.slice?t[o(n)]:function(e,i){n(e={exports:{}}),t[o(i)]=e.exports}}var n;"undefined"!=typeof window&&(n=window),"undefined"!=typeof global&&(n=global),n=n||{};var o=n.console||{log:function(){}};if("undefined"!=typeof module)var e=module;t(function(t){var n={};n.fns=n.fn={is:function(t){return!!t&&"function"==typeof t}},n.bi={is:function(t){return t instanceof Boolean||"boolean"==typeof t}},n.num={is:function(t){return!e(t)&&(t-parseFloat(t)+1>=0||1/0===t||-1/0===t)}},n.text={is:function(t){return"string"==typeof t}},n.text.ify=function(t){return n.text.is(t)?t:"undefined"!=typeof JSON?JSON.stringify(t):t&&t.toString?t.toString():t},n.text.random=function(t,n){var o="";for(t=t||24,n=n||"0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";t>0;)o+=n.charAt(Math.floor(Math.random()*n.length)),t--;return o},n.text.match=function(t,o){var e=!1;if(t=t||"",o=n.text.is(o)?{"=":o}:o||{},n.obj.has(o,"~")&&(t=t.toLowerCase(),o["="]=(o["="]||o["~"]).toLowerCase()),n.obj.has(o,"="))return t===o["="];if(n.obj.has(o,"*")){if(t.slice(0,o["*"].length)!==o["*"])return!1;e=!0,t=t.slice(o["*"].length)}if(n.obj.has(o,"!")){if(t.slice(-o["!"].length)!==o["!"])return!1;e=!0}if(n.obj.has(o,"+")&&n.list.map(n.list.is(o["+"])?o["+"]:[o["+"]],function(n){if(!(t.indexOf(n)>=0))return!0;e=!0}))return!1;if(n.obj.has(o,"-")&&n.list.map(n.list.is(o["-"])?o["-"]:[o["-"]],function(n){if(!(t.indexOf(n)<0))return!0;e=!0}))return!1;if(n.obj.has(o,">")){if(!(t>o[">"]))return!1;e=!0}if(n.obj.has(o,"<")){if(!(to?1:0):0}},n.list.map=function(t,n,o){return a(t,n,o)},n.list.index=1,n.obj={is:function(t){return!!t&&(t instanceof Object&&t.constructor===Object||"Object"===Object.prototype.toString.call(t).match(/^\[object (\w+)\]$/)[1])}},n.obj.put=function(t,n,o){return(t||{})[n]=o,t},n.obj.has=function(t,n){return t&&Object.prototype.hasOwnProperty.call(t,n)},n.obj.del=function(t,n){if(t)return t[n]=null,delete t[n],t},n.obj.as=function(t,n,o,e){return t[n]=t[n]||(e===o?{}:o)},n.obj.ify=function(t){if(r(t))return t;try{t=JSON.parse(t)}catch(n){t={}}return t},function(){function t(t,n){u(this,n)&&o!==this[n]||(this[n]=t)}var o;n.obj.to=function(n,o){return o=o||{},a(n,t,o),o}}(),n.obj.copy=function(t){return t?JSON.parse(JSON.stringify(t)):t},function(){function t(t,n){var o=this.n;if(!o||!(n===o||r(o)&&u(o,n)))return!!n||void 0}n.obj.empty=function(n,o){return!n||!a(n,t,{n:o})}}(),function(){function t(n,o){if(2===arguments.length)return t.r=t.r||{},void(t.r[n]=o);t.r=t.r||[],t.r.push(n)}var i=Object.keys;n.obj.map=function(a,s,f){var c,l,p,g,h,d=0,v=o(s);if(t.r=null,i&&r(a)&&(g=Object.keys(a),h=!0),e(a)||g)for(l=(g||a).length;d",o.drift=0,o.is=function(t,n,e){var i=n&&t&&t[x]&&t[x][o._]||e;if(i)return m(i=i[n])?i:-1/0},o.ify=function(t,n,e,r,u){if(!t||!t[x]){if(!u)return;t=i.soul.ify(t,u)}var a=g(t[x],o._);return l!==n&&n!==x&&(m(e)&&(a[n]=e),l!==r&&(t[n]=r)),t},o.to=function(t,n,e){var r=t[n];return d(r)&&(r=_(r)),o.ify(e,n,o.is(t,n),r,i.soul(t))},function(){function t(t,n){x!==n&&o.ify(this.o,n,this.s)}o.map=function(n,e,i){var r,u=d(u=n||e)?u:null;return n=k(n=n||e)?n:null,u&&!n?(e=m(e)?e:o(),u[x]=u[x]||{},v(u,t,{o:u,s:e}),u):(i=i||d(e)?e:r,e=m(e)?e:o(),function(o,u,a,s){if(!n)return t.call({o:a,s:e},o,u),o;n.call(i||this||{},o,u,a,s),h(a,u)&&r===a[u]||t.call({o:a,s:e},o,u)})}}();var l,p=e.obj,g=p.as,h=p.has,d=p.is,v=p.map,_=p.copy,b=e.num,m=b.is,y=e.fn,k=y.is,x=i._;n.exports=o})(t,"./state"),t(function(n){var o=t("./type"),e=t("./val"),i=t("./node"),r={};!function(){function t(t,o){if(!t||o!==i.soul(t)||!i.is(t,this.fn,this.as))return!0;this.cb&&(n.n=t,n.as=this.as,this.cb.call(n.as,t,o,n))}function n(t){t&&i.is(n.n,t,n.as)}r.is=function(n,o,e,i){return!(!n||!s(n)||l(n))&&!g(n,t,{cb:o,fn:e,as:i})}}(),function(){function t(t,r){var u;return(u=l(t,r))?u:(r.env=t,r.soul=o,i.ify(r.obj,n,r)&&(t.graph[e.rel.is(r.rel)]=r.node),r)}function n(n,o,r){var s,l,p=this,g=p.env;if(i._===o&&c(n,e.rel._))return r._;if(s=a(n,o,r,p,g)){if(o||(p.node=p.node||r||{},c(n,i._)&&(p.node._=h(n._)),p.node=i.soul.ify(p.node,e.rel.is(p.rel)),p.rel=p.rel||e.rel.ify(i.soul(p.node))),(l=g.map)&&(l.call(g.as||{},n,o,r,p),c(r,o))){if(n=r[o],u===n)return void f(r,o);if(!(s=a(n,o,r,p,g)))return}if(!o)return p.node;if(!0===s)return n;if(l=t(g,{obj:n,path:p.path.concat(o)}),l.node)return l.rel}}function o(t){var n=this,o=e.rel.is(n.rel),r=n.env.graph;n.rel=n.rel||e.rel.ify(t),n.rel[e.rel._]=t,n.node&&n.node[i._]&&(n.node[i._][e.rel._]=t),c(r,o)&&(r[t]=r[o],f(r,o))}function a(t,n,o,i,r){var u;return!!e.is(t)||(s(t)?1:(u=r.invalid)?(t=u.call(r.as||{},t,n,o),a(t,n,o,i,r)):void(r.err="Invalid value at '"+i.path.concat(n).join(".")+"'!"))}function l(t,n){for(var o,e=t.seen,i=e.length;i--;)if(o=e[i],n.obj===o.obj)return o;e.push(n)}r.ify=function(n,o,i){var r={path:[],obj:n};return o?"string"==typeof o?o={soul:o}:o instanceof Function&&(o.map=o):o={},o.soul&&(r.rel=e.rel.ify(o.soul)),o.graph=o.graph||{},o.seen=o.seen||[],o.as=o.as||i,t(o,r),o.root=r.node,o.graph}}(),r.node=function(t){var n=i.soul(t);if(n)return p({},n,t)},function(){function t(t,n){var o,u;if(i._===n){if(l(t,e.rel._))return;return void(this.obj[n]=h(t))}return(o=e.rel.is(t))?(u=this.opt.seen[o])?void(this.obj[n]=u):void(this.obj[n]=this.opt.seen[o]=r.to(this.graph,o,this.opt)):void(this.obj[n]=t)}r.to=function(n,o,e){if(n){var i={};return e=e||{seen:{}},g(n[o],t,{obj:i,graph:n,opt:e}),i}}}();var u,a=(o.fn.is,o.obj),s=a.is,f=a.del,c=a.has,l=a.empty,p=a.put,g=a.map,h=a.copy;n.exports=r})(t,"./graph"),t(function(n){function o(){this.cache={}}var e=t("./type");o.prototype.track=function(t){return this.cache[t]=e.time.is(),this.to||this.gc(),t},o.prototype.check=function(t){return!!e.obj.has(this.cache,t)&&this.track(t)},o.prototype.gc=function(){var t=this,n=e.time.is(),o=n;if(e.obj.map(t.cache,function(i,r){o=Math.min(n,i),n-i<3e5||e.obj.del(t.cache,r)}),e.obj.empty(t.cache))return void(t.to=null);var i=n-o,r=3e5-i;t.to=setTimeout(function(){t.gc()},r)},n.exports=o})(t,"./dup"),t(function(n){function i(t){return t instanceof i?(this._={gun:this}).gun:this instanceof i?i.create(this._={gun:this,opt:t}):new i(t)}i.is=function(t){return t instanceof i},i.version=.7,i.chain=i.prototype,i.chain.toJSON=function(){};var r=t("./type");r.obj.to(r,i),i.HAM=t("./HAM"),i.val=t("./val"),i.node=t("./node"),i.state=t("./state"),i.graph=t("./graph"),i.dup=t("./dup"),i.schedule=t("./schedule"),i.on=t("./onify")(),i._={node:i.node._,soul:i.val.rel._,state:i.state._,field:".",value:"="},function(){function t(t){var n,o=this,e=o.as;if(t.gun||(t.gun=e.gun),t["#"]||(t["#"]=i.text.random()),!e.dup.check(t["#"])){if(t["@"]){if(e.ack(t["@"],t))return;return e.dup.track(t["#"]),void i.on("out",p(t,{gun:e.gun}))}e.dup.track(t["#"]),n=p(t,{gun:e.gun}),t.get&&i.on("get",n),t.put&&i.on("put",n),i.on("out",n)}}i.create=function(n){n.on=n.on||i.on,n.root=n.root||n.gun,n.graph=n.graph||{},n.dup=n.dup||new i.dup,n.ask=i.on.ask,n.ack=i.on.ack;var o=n.gun.opt(n.opt);return n.once||(n.on("in",t,n),n.on("out",t,n)),n.once=1,o}}(),function(){function t(t,n,o,e){var r=this,u=i.state.is(o,n);if(!u)return r.err="Error: No state on '"+n+"' in node '"+e+"'!";var a=r.graph[e]||v,s=i.state.is(a,n,!0),f=a[n],c=i.HAM(r.machine,u,s,t,f);c.incoming||c.defer&&(r.defer=u<(r.defer||1/0)?u:r.defer),r.put[e]=i.state.to(o,n,r.put[e]),(r.diff||(r.diff={}))[e]=i.state.to(o,n,r.diff[e])}function n(t,n){var e=(this.gun._.next||v)[n];if(e){var r=this.map[n]={put:this.node=t,get:this.soul=n,gun:this.ref=e};g(t,o,this),i.on("node",r)}}function o(t,n){var o=this.graph,e=this.soul,r=this.ref._;o[e]=i.state.to(this.node,n,o[e]),(r.put||(r.put={}))[n]=t}function e(t,n){t.gun&&t.gun._.on("in",t)}i.on("put",function(o){if(!o["#"])return this.to.next(o);var r=this,a={gun:o.gun,graph:o.gun._.graph,put:{},map:{},machine:i.state()};if(i.graph.is(o.put,null,t,a)||(a.err="Error: Invalid graph!"),a.err)return a.gun.on("in",{"@":o["#"],err:i.log(a.err)});g(a.put,n,a),g(a.map,e,a),u!==a.defer&&i.schedule(a.defer,function(){i.on("put",o)},i.state),a.diff&&r.to.next(p(o,{put:a.diff}))})}(),function(){i.on("get",function(t){var n,o=this,e=t.get[h],r=t.gun._,u=r.graph[e],a=t.get[d],s=r.next||(r.next={}),f=(s[e]||v)._;if(!u||!f)return o.to.next(t);if(a){if(!l(u,a))return o.to.next(t);u=i.state.to(u,a)}else u=i.obj.copy(u);u=i.graph.node(u),n=f.ack,r.on("in",{"@":t["#"],how:"mem",put:u,gun:f.gun}),0e.ack&&!t.ack&&!l.val.rel.is(c)&&(e.ack=1),e.get&&t.get!==e.get&&(t=b(t,{get:e.get})),e.field&&a!==e&&(t=b(t,{gun:e.gun}),a.ack&&(e.ack=e.ack||a.ack)),p===c){if(o.to.next(t),e.soul)return;return u(e,t,o),e.field&&f(e,t),_(a.echo,e.id),void _(e.map,a.id)}return e.soul?(e.root._.now&&(t=b(t,{put:c=a.put})),o.to.next(t),u(e,t,o),void m(c,s,{at:t,cat:e})):(n=l.val.rel.is(c))?(r(e,t,a,n),o.to.next(t),void u(e,t,o)):l.val.is(c)?(e.field||e.soul?f(e,t):(a.field||a.soul)&&((a.echo||(a.echo={}))[e.id]=e,(e.map||(e.map={}))[a.id]=e.map[a.id]||{at:a}),o.to.next(t),void u(e,t,o)):(e.field&&a!==e&&d(a,"put")&&(e.put=a.put),(n=l.node.soul(c))&&a.field&&(a.put=e.root.get(n)._.put),o.to.next(t),u(e,t,o),r(e,t,a,n),void m(c,s,{at:t,cat:e}))}function r(t,n,o,e){if(e&&x!==t.get){var i=t.root.get(e)._;t.field?o=i:o.field&&r(o,n,o,e),o!==t&&((o.echo||(o.echo={}))[t.id]=t,t.field&&!(t.map||g)[o.id]&&f(t,n),i=(t.map||(t.map={}))[o.id]=t.map[o.id]||{at:o},e!==i.rel&&c(t,i.rel=e))}}function u(t,n,o){t.echo&&(t.field&&(n=b(n,{event:o})),m(t.echo,a,n))}function a(t){t.on("in",this)}function s(t,n){var o,e,i,r=this.cat,u=r.next||g,a=this.at;(x!==n||u[n])&&(o=u[n])&&(i=o._,i.field?(t&&t[y]&&l.val.rel.is(t)===l.node.soul(i.put)||(i.put=t),e=o):e=a.gun.get(n),i.on("in",{put:t,get:n,gun:e,via:a}))}function f(t,n){if(t.field||t.soul){var o=t.map;t.map=null,null!==o&&(p===o&&t.put!==p||(m(o,function(n){(n=n.at)&&_(n.echo,t.id)}),m(t.next,function(t,n){var o=t._;o.put=p,o.ack&&(o.ack=-1),o.on("in",{get:n,gun:t,put:p})})))}}function c(t,n){var o=t.root.get(n)._;if(t.ack)return o.ack=o.ack||-1,void o.on("out",{get:o={"#":n,gun:o.gun},"#":t.root._.ask(l.HAM.synth,o)});m(t.next,function(o,e){o._.on("out",{get:o={"#":n,".":e,gun:o},"#":t.root._.ask(l.HAM.synth,o)})})}var l=t("./root");l.chain.chain=function(){var t=this._,o=new this.constructor(this),r=o._;return r.root=n=t.root,r.id=++n._.once,r.back=this,r.on=l.on,l.on("chain",r),r.on("in",i,r),r.on("out",e,r),o},l.chain.chain.input=i;var p,g={},h=l.obj,d=h.has,v=h.put,_=h.del,b=h.to,m=h.map,y=l._.soul,k=l._.field,x=l.node._})(t,"./chain"),t(function(n){function o(t,n){var o=n._,e=o.next,i=n.chain(),r=i._;return e||(e=o.next={}),e[r.get=t]=i,o.root===n?r.soul=t:(o.soul||o.field)&&(r.field=t),i}function e(t){var n,o=this,e=o.as,i=t.gun,u=i._,s=t.put;r===s&&(s=u.put),(n=s)&&n[f._]&&(n=f.is(n))&&(n=u.root.get(n)._,r!==n.put&&(t=a(t,{put:n.put}))),e.use(t,t.event||o),o.to.next(t)}var i=t("./root");i.chain.get=function(t,n,r){if("string"!=typeof t){if(t instanceof Function){var u=this,a=u._;return r=n||{},r.use=t,r.out=r.out||{cap:1},r.out.get=r.out.get||{},"_"!=a.get&&(a.root._.now=!0),a.on("in",e,r),a.on("out",r.out),a.root._.now=!1,u}return s(t)?this.get(""+t,n,r):((r=this.chain())._.err={err:i.log("Invalid get request!",t)},n&&n.call(r,r._.err),r)}var u,f,l=this,p=l._,g=p.next||c;return(u=g[t])||(u=o(t,l)),(f=p.stun)&&(u._.stun=u._.stun||f),n&&n instanceof Function&&u.get(n,r),u};var r,u=i.obj,a=(u.has,i.obj.to),s=i.num.is,f=i.val.rel,c=(i.node._,{})})(t,"./get"),t(function(n){function e(t){t.batch=i;var n=t.opt||{},o=t.env=f.state.map(u,n.state);if(o.soul=t.soul,t.graph=f.graph.ify(t.data,o,t),o.err)return(t.ack||v).call(t,t.out={err:f.log(o.err)}),void(t.res&&t.res());t.batch()}function i(){var t=this;t.graph&&!h(t.stun,r)&&((t.res||_)(function(){t.ref._.on("out",{cap:3,gun:t.ref,put:t.out=t.env.graph,opt:t.opt,"#":t.gun.back(-1)._.ask(function(n){this.off(),t.ack&&t.ack(n,this)},t.opt)})},t),t.res&&t.res())}function r(t,n){if(t)return!0}function u(t,n,o,e){var i=this;!n&&e.path.length&&(i.res||_)(function(){var t=e.path,n=i.ref,o=(i.opt,0),r=t.length;for(o;o":{}}},e.node.soul(o)))&&n._&&(i=a(i)?{machine:i}:{machine:e.state()},i.union=n||e.obj.copy(n),i.vertex=n,i.node=o,!l(o,t,i)))return i.union},e.HAM.delta=function(t,o,i){return i=a(i)?{machine:i}:{machine:e.state()},t?(i.soul=e.node.soul(i.vertex=t),i.soul?(i.delta=e.node.soul.ify({},i.soul),l(i.node=o,n,i),i.delta):void 0):e.obj.copy(o)},e.HAM.synth=function(t,n){var o=this.as,r=o.gun._;if(!t.put||o["."]&&!f(t.put[o["#"]],r.get)){if(r.put!==i)return;return void r.on("in",{get:r.get,put:r.put=i,gun:r.gun})}t.gun=r.root,e.on("put",t)},e.HAM.synth_=function(t,n,o){var r=this.as||o,u=r._,a=u.root._,s={};if(!t.put){if(u.put!==i)return;return void u.on("in",{get:u.get,put:u.put=i,gun:r,via:t})}l(t.put,function(t,n){var o=this.graph;s[n]=e.HAM.delta(o[n],t,{graph:o}),o[n]=e.HAM.union(o[n],t)||o[n]},a),t.gun!==a.gun&&(s=t.put),l(s,function(o,r){var a=this,s=a.next||(a.next={}),l=s[r]||(s[r]=a.gun.get(r)),p=l._;if(p.put=a.graph[r],u.field&&!f(o,u.field))return(t=c(t,{})).put=i,void e.HAM.synth(t,n,u.gun);p.on("in",{put:o,get:r,gun:l,via:t})},a)}}();var i,r=e,u=r.num,a=u.is,s=r.obj,f=s.has,c=(s.put,s.to),l=s.map,p=e.node,g=(p.soul,p.is,p.ify,e.state),h=g.is,d=g.ify,v=e.val,_=v.is;v.rel.is})(t,"./index"),t(function(n){var o=t("./root");t("./index"),t("./opt"),t("./chain"),t("./back"),t("./put"),t("./get"),n.exports=o})(t,"./core"),t(function(n){var o=t("./core");o.chain.path=function(t,n,e){var i,r=this,u=r;if(e=e||{},e.path=!0,o.log.once("pathing","Warning: `.path` to be removed from core (but available as an extension), use `.get` chains instead. If you are opposed to this, please voice your opinion in https://gitter.im/amark/gun and ask others."),u===u._.root)return n&&n({err:o.log("Can't do that on root instance.")}),u;if("string"==typeof t){if(i=t.split(e.split||"."),1===i.length)return u=r.get(t,n,e),u._.opt=e,u;t=i}if(t instanceof Array){if(t.length>1){u=r;var a=0,s=t.length;for(a;a=1e4)return n();e||(clearTimeout(e),e=setTimeout(n,1e3))}),Gun.on("get",function(t){this.to.next(t);var n,o,e,r=t.gun,a=t.get;if((e=t.opt||{}).prefix=e.prefix||t.gun.back("opt.prefix")||"gun/",a&&(n=a[Gun._.soul])){var s=a["."];o=Gun.obj.ify(i.getItem(e.prefix+n)||null)||u[n]||void 0,o&&s&&(o=Gun.state.to(o,s)),(o||Gun.obj.empty(r.back("opt.peers")))&&r.on("in",{"@":t["#"],put:Gun.graph.node(o),how:"lS"})}})}})(t,"./adapters/localStorage"),t(function(n){function o(t){var n=s,o=this,e=t.wire||i(t,o);if(o.wsp&&o.wsp.count++,e)return e.readyState===e.OPEN?void e.send(n):void(t.queue=t.queue||[]).push(n)}function e(t,n,o){if(o&&t){try{t=JSON.parse(t.data||t)}catch(t){}if(t instanceof Array)for(var i,r=0;i=t[r++];)e(i,n,o);else o.wsp&&1===o.wsp.count&&((t.body||t).wsp=c),o.gun.on("in",t.body||t)}}function i(t,n){if(t&&t.url){var i=t.url.replace("http","ws"),f=t.wire=new a(i,n.opt.wsc.protocols,n.opt.wsc);return f.onclose=function(){r(t,n)},f.onerror=function(o){r(t,n),o&&o.code},f.onopen=function(){var e=t.queue;t.queue=[],u.obj.map(e,function(e){s=e,o.call(n,t)})},f.onmessage=function(o){e(o,t,n)},f}}function r(t,n){clearTimeout(t.defer),t.defer=setTimeout(function(){i(t,n)},2e3)}var u=t("./core");if("undefined"==typeof JSON)throw new Error("Gun depends on JSON. Please load it first:\najax.cdnjs.com/ajax/libs/json2/20110223/json2.js");var a;if("undefined"!=typeof window){a=window.WebSocket||window.webkitWebSocket||window.mozWebSocket;var s,f,c=function(){};u.on("out",function(t){this.to.next(t);var n=t.gun._.root._,e=n.wsp||(n.wsp={});if(!t.wsp||1!==e.count){if(s=JSON.stringify(t),n.udrain)return void n.udrain.push(s);n.udrain=[],clearTimeout(f),f=setTimeout(function(){if(n.udrain){var t=n.udrain;n.udrain=null,t.length&&(s=JSON.stringify(t),u.obj.map(n.opt.peers,o,n))}},1),e.count=0,u.obj.map(n.opt.peers,o,n)}})}})(t,"./polyfill/request")}(); \ No newline at end of file diff --git a/src/root.js b/src/root.js index f11e9ad8d..0009c8071 100644 --- a/src/root.js +++ b/src/root.js @@ -189,7 +189,7 @@ Gun._ = { // some reserved key words, these are not the only ones. if(!obj_is(at.opt.peers)){ at.opt.peers = {}} at.opt.peers = obj_to(tmp, at.opt.peers); } - at.opt.wsc = at.opt.wsc || {protocols:''} + at.opt.wsc = at.opt.wsc || {protocols:[]} at.opt.peers = at.opt.peers || {}; obj_to(opt, at.opt); // copies options on to `at.opt` only if not already taken. Gun.on('opt', at);