-
Notifications
You must be signed in to change notification settings - Fork 3
/
subetha.min.js
28 lines (28 loc) · 12.4 KB
/
subetha.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* SubEtha v0.0.2-alpha, Morus v1.0.0, SubEtha Client v0.0.1-alpha, Peer-Events v0.0.0-alpha, Ad Hoc-Exchange v0.0.2-alpha | github.com/bemson | (c) 2014, ACPACHE & MIT */
!function(r,x,n,B){function h(){function n(){for(var f={},t=d.concat(),k,g,a,e=95;e--;)a=r(A()*e),g=e+32,k=t[a],f[g+"c"]=k,f[k]=g,t.splice(a,1);return f}function u(){this.randomize()}var h={},d=[],v,f=JSON.stringify,J=JSON.parse,A=Math.random,r=Math.round,w=/[ -~]/g,x=/^(\d+)({.+})$/;!function(){for(var f=95,t,k;f--;)t=f+32,k=String.fromCharCode(t),h[t+"c"]=d[f]=k,h[k]=t;v=d.join("")}();u.prototype.encode=function(d){var f=this.key,k=this.index,g;return d.replace(w,function(a){g=h[a];g-=32-k;g%=95;
g+=32;k++;return f[g+"c"]})};u.prototype.decode=function(d){var f=this.key,k=this.index,g;return d.replace(w,function(a){g=f[a];g-=32+k;0>g&&(g%=95)&&(g=95+g);g+=32;k++;return h[g+"c"]})};u.prototype.randomize=function(){this.key=n();this.index=r(95*A());return this};u.prototype.cipher=function(d){var t;return arguments.length?"string"==typeof d&&(t=x.exec(d))?(this.index=+t[1],this.key=J(t[2]),!0):!1:this.index+f(this.key)};u.genKey=n;u.ASCII=v;return u}r?define(h):x?module.exports=h():n.Morus||
(n.Morus=h())}("function"==typeof define,"undefined"!=typeof exports,this);
!function(N,O,A,G,ba,P,ca,la,f,ma){function p(){function B(a){for(var b=1,c,d;c=arguments[b];b++)for(d in c)k.call(c,d)&&(a[d]=c[d]);return a}function p(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(da,ea)}function ea(a){var b=16*Q()|0;return("x"===a?b:b&3|8).toString(16)}function q(a){return a&&"string"===typeof a}function H(){v.contains(m)&&v.removeChild(m)}function R(a){var b=a.data,c,d,e,h;if(!S&&"string"===typeof b&&"[]"===b.charAt(0)+b.charAt(b.length-1))try{b=T(b)}catch(n){return}I(b)&&
"se-0"==b[0]&&3===b.length&&k.call(r,b[1])&&0<(d=(c=r[b[1]]).state)&&4>d&&(e=c.decode(b[2]))&&q(e.mid)&&k.call(e,"sent")&&k.call(e,"msg")&&k.call(U,h=e.type)&&(e.sent=new G(e.sent),U[h](c,e.msg,e,a))}function w(){var a=C,b;v=D.body;x(f,"DOMContentLoaded",w);x(f,"load",w);J=function(a){var b=a.url;(k.call(r,b)?r[b]:r[b]=new V(b)).addClient(a)};W=function(a){var b=a._bridge(K);b&&b.removeClient(a)};C=0;for(b in a)k.call(a,b)&&J(a[b])}function X(a,b,c,d,e,h,n,g){var l;k.call(a,b)&&k.call((l=a[b]).peers,
c)&&(a=l.peers[c],d(l,a,e,{id:n.mid,peer:a,sent:new G(n.sent),timeStamp:g.timeStamp}))}function t(a,b){var c=a.state;b!==c&&(a.state=b,a.fire("::readyStateChange",b,c),a.state===b&&(0===b&&(a.peers={},2<c&&a.fire("::disconnect")),3===b&&a.fire("::connect")))}function L(){}function V(a){var b=this,c=D.createElement("iframe"),d=new fa;c.setAttribute("sandbox","allow-same-origin allow-scripts");b.iframe=c;b.id=a;b.cipher=d;b.clients={};b.channels={};b.channelCnts={};b.ping=["se-0",Y,a,d.cipher()].join("`");
Z++||(y(f,"message",R),y(f,"unload",H));b.onLoad=function(){3>b.state?(b.iframe.contentWindow.postMessage(b.ping,"*"),b.state=2):b.destroy()};y(c,"load",b.onLoad);k.call(z.urls,a)?c.src=z.urls[a]:c.src=a;b.dDay(z.bridgeTimeout);m.appendChild(c);b.inDom()||v.appendChild(m)}function E(){this.peers={};this.credentials=[]}function F(a,b){this._client=b;a&&(this.id=a.id,this.origin=a.origin,this.start=new G(a.start),this.channel=a.channel)}var fa=O||N?require("morus"):f.Morus,ga=P.stringify,T=P.parse,
Q=ba.random,k=ca.prototype.hasOwnProperty,aa=A.prototype.slice,D=document,v,da=/[xy]/g,Y=Q(),u={},ha=/^([\w\-]+\.)+[conmi]\w{1,2}\b/,ia=/^[^{]{0,40}({.+})[^}]{0,40}$/,M="f"==location.protocol.charAt(0),ja=M?"http://":"//",m=D.createElement("div"),r={},Z=0,C={},K={},J=function(a){C[a.id]=a},W=function(a){delete C[a.id];t(a,0)},S=!!function(){var a=1;try{f.postMessage({toString:function(){a=0}},"*")}catch(b){}return a}(),ka=S?function(a,b){a.postMessage(b,"*")}:function(a,b){a.postMessage(ga(b),"*")},
y=f.attachEvent?function(a,b,c){a.attachEvent("on"+b,c)}:function(a,b,c){a.addEventListener(b,c,!1)},x=f.attachEvent?function(a,b,c){a.detachEvent("on"+b,c)}:function(a,b,c){a.removeEventListener(b,c,!1)},I="function"===typeof A.isArray?A.isArray:function(a){return a instanceof A},U={ready:function(a,b){var c=a.clients,d;a.dDay();a.origin=b;a.state=3;for(d in c)k.call(c,d)&&a.auth(c[d])},auth:function(a,b){var c=a.channels,d=a.clients,e=b.id,h,n,g,l;if(k.call(d,e))if(d=d[e],g=b.peers,b.ok&&2===d.state){n=
d.peers={};for(l in g)if(k.call(g,l)){h=1;var f=g[l];d.peers[f.id]=new F(f,d)}g=d.channel;k.call(c,g)||(c[g]={},a.channelCnts[g]=0);c[g][e]=d;a.channelCnts[g]++;t(d,3);if(h&&3===d.state)for(l in n)k.call(n,l)&&d.fire("::join",n[l],!0)}else a.remove(d)},net:function(a,b){var c,d,e,h=b.joins,n=b.drops,g,l,f,m;for(f=h.length;f--;){g=h[f];l=g.id;c=a.channels[g.channel];m=[];for(d in c)d!=l&&k.call(c,d)&&!k.call((e=c[d]).peers,l)&&(e.peers[g.id]=new F(g,e),m.push(e));for(c=m.length;c--;)e=m[c],e.fire("::join",
e.peers[l])}for(f=n.length;f--;){h=[];g=n[f];l=g.id;c=a.channels[g.channel];for(d in c)d!=l&&k.call(c,d)&&(e=c[d],g=e.peers[l],h.push([e,g]),g.state=0,delete e.peers[l]);for(c=h.length;c--;)e=h[c][0],e.id!=l&&e.fire("::drop",h[c][1])}},die:function(a){a.deref();a.destroy()},client:function(a,b,c,d){a=a.clients;var e=b.from,h=z.msgType,f,g,l;if("object"===typeof h&&k.call(h,b.type))if(h=h[b.type],l=b.to,f=b.data,l)for(g=l.length;g--;)X(a,l[g],e,h,f,b,c,d);else for(g in a)k.call(a,g)&&X(a,g,e,h,f,b,
c,d)}},z={bridgeTimeout:1E4,guid:p,Client:E,Peer:F,EventEmitter:L,protocol:"se-0",urls:{"public":(M?"http:":"")+"//rawgit.com/bemson/subetha-bridge/master/public_bridge.html",local:"javascript:'<script src=\""+(M?"http:":"")+"//rawgit.com/bemson/subetha-bridge/master/subetha-bridge.min.js\">\x3c/script>'"},msgType:{}};u["::drop"]=1;u["::join"]=1;u["::readyStateChange"]=1;u["::connect"]=1;u["::disconnect"]=1;m.style.display="none";m.setAttribute("aria-hidden","true");m.setAttribute("hidden","hidden");
m.setAttribute("data-owner","subetha");B(L.prototype,{on:function(a,b){q(a)&&"function"===typeof b&&(k.call(this,"_evts")||(this._evts={}),k.call(this._evts,a)||(this._evts[a]=[]),this._evts[a].push(b));return this},off:function(a,b){var c,d,e=arguments.length;if(!k.call(this,"_evts")||!e)this._evts={};else if(q(a)&&k.call(this._evts,a)){c=this._evts[a];if("function"==typeof b)for(d=c.length;d--;)if(c[d]===b){c.splice(d,1);break}(2>e||!c.length)&&delete this._evts[a]}return this},fire:function(a){var b=
this,c,d,e,h,f;if(q(a)&&k.call(b,"_evts")&&k.call(b._evts,a)&&(e=(d=b._evts[a]).length))for(c=aa.call(arguments,1),f=c.length?function(a){a.apply(b,c)}:function(a){a.call(b)},h=0;h<e;h++)f(d[h]);return b}});B(V.prototype,{failures:0,state:1,cnt:0,addClient:function(a){var b=this,c=b.clients,d=a.id;k.call(c,d)||(b.cnt++,a._bridge=function(a){return a===K&&b},c[d]=a);3==b.state&&b.auth(a)},removeClient:function(a){var b=a.id,c=a.channel;--this.cnt?(delete this.clients[b],1<a.state&&(delete this.channels[c][b],
1==this.channelCnts[c]--&&delete this.channelCnts[c]),this.drop(a)):this.destroy()},auth:function(a){var b;t(a,2);2==a.state&&(b=a.credentials,I(b)||(b=[b]),this.send("auth",{id:a.id,channel:a.channel,creds:b}))},destroy:function(){var a=this.state,b=this.iframe,c=this.clients,d;this.dDay();this.state=4;this.clients={};this.cnt=0;for(d in c)k.call(c,d)&&this.drop(c[d]);this.cnt?this.state=a:(this.deref(),x(b,"load",this.onLoad),--Z||(x(f,"message",R),x(f,"unload",H),H()),m.contains(b)&&m.removeChild(b))},
deref:function(){delete r[this.id]},drop:function(a){var b=a.state;delete a._bridge;1<b&&4>b&&(this.send("drop",a.id),3==b&&t(a,4));a.peers={};4==a.state&&t(a,0)},decode:function(a){var b=this.cipher,c;if(q(a)&&(c=b.decode(a))&&(c=ia.exec(c)))try{return c=T(c[1]),b.shift++,c}catch(d){}3===++this.failures&&this.destroy();return 0},inDom:function(){var a=this.iframe;return m.contains(a)&&v.contains(a)},dDay:function(a){var b=this;clearTimeout(b.timer);a&&(b.timer=setTimeout(function(){b.destroy()},
a))},send:function(a,b){var c;if(3!=this.state)return 0;c=p();ka(this.iframe.contentWindow,{key:Y,mid:c,type:a,msg:b});return c}});E.prototype=new L;B(E.prototype,{id:"",channel:"lobby",url:"local",state:0,_bridge:function(){return!1},open:function(a){var b=this.channel,c=this.url,d=arguments,e=this.state,h;q(a)&&(h=a.indexOf("@"),~h?(b=a.substring(0,h)||b,c=a.substring(h+1)||c):b=a,1<d.length&&(this.credentials=aa.call(d,1)));ha.test(c)&&(c=ja+c);if(2<e){if(b==this.channel&&c==this.url)return;this.close();
e=this.state}this.channel=b;this.url=c;2>e&&(this.id=p(),t(this,1),1==this.state&&J(this));return this},close:function(){var a=this.state;0<a&&4>a&&W(this);return this},_transmit:function(a,b,c){var d=this._bridge(K),e;if((e=d&&3==d.state&&3==this.state&&q(a)&&!k.call(u,a))&&!(e=!b)){a:{e=this.peers;var h=[],f,g;I(b)||(b=[b]);for(g=b.length;g--;){f=b[g];f instanceof Subetha.Peer&&(f=f.id);if(!k.call(e,f)){e=0;break a}h.push(f)}e=h}e=b=e}return e?d.send("client",{type:a,from:this.id,to:b?[].concat(b):
0,data:c}):!1}});B(F.prototype,{state:3});"function"!==typeof f.postMessage?E.prototype.open=function(){return this}:D.body?w():(y(f,"DOMContentLoaded",w),y(f,"load",w));return z}N?define(p):O?module.exports=p():f.Subetha||(f.Subetha=p())}("function"===typeof define,"undefined"!=typeof exports,Array,Date,Math,JSON,Object,RegExp,this);
!function(r,x,n,B){function h(){function h(d,v,f,n){return v&&"string"===typeof v&&"::"!=v.substring(0,2)&&d._transmit("subetha/event",n,{type:v,data:1<f.length?I.call(f,1):[]})}var u=x||r?require("subetha-client"):n.Subetha,I=Array.prototype.slice;u.Client.prototype.emit=function(d){return h(this,d,arguments,0)};u.Peer.prototype.send=function(d){return this.state?h(this._client,d,arguments,this.id):!1};u.msgType["subetha/event"]=function(d,v,f,n){var h=f.data;(f=f.type)&&"string"===typeof f&&"::"!=
f.substring(0,2)&&(v={data:[].concat(h),id:n.id,peer:v,timeStamp:n.timeStamp,type:f},h.length?d.fire.apply(d,[f,v].concat(h)):d.fire(f,v))};return u}r?define(h):x?module.exports=h():n.Subetha&&h()}("function"==typeof define,"undefined"!=typeof exports,this);
!function(r,x,n,B,h,E){function u(){function u(a){return a&&"string"===typeof a}function d(a,e,d){var f,g=e[0],k,h;if(!g||"string"!=typeof g)return!1;if(d)k=[d];else for(h in f=a.peers,k=[],f)k.push(h);if(h=k.length){f=w();e=e.length?t.call(e,1):[];if(!a._transmit("subetha/exchange",d,{xid:f,idx:0,phrase:g,data:e}))return!1;for(;h--;)v(a,f,k[h]).push(g);return f}}function v(a,e,d){var g,q;k.call(a,"_ax")||(g=a._ax={xids:{},cbs:{}},a.on("::disconnect",f));q=a._ax;e&&!k.call(q.xids,e)&&(g=q.xids[e]=
{pids:{},cnt:0});d&&!k.call(q.xids[e].pids,d)&&(q.xids[e].cnt++,g=[],g.endFn=function(){return!!A(a,e,d)},g=q.xids[e].pids[d]=g);return g}function f(){var a=this._ax.xids,e;this.off("::disconnect",f);for(e in a)J(this,e,a[e],1);delete this._ax}function J(a,e,d){var f,g,h;if(k.call(a,"_ax")&&(f=a._ax.xids,(g=f[e])&&k.call(g.pids,d)))return h=g.pids[d],delete g.pids[d],--g.cnt||delete f[e],a=a.peers[d],a._client.fire("::exchange-end",a,e,h.concat()),1}function A(a,e,d){a._transmit("subetha/exchange",
d,{xid:e,xkill:1});J(a,e,d)}var F=x||r?require("subetha"):h.Subetha,w=F.guid,C=F.Client,E=F.Peer,t=n.prototype.slice,k=Object.prototype.hasOwnProperty,g="function"===typeof n.isArray?n.isArray:function(a){return a instanceof n};C.prototype.adhoc=function(){var a=arguments,e=a[a.length-1];1<a.length&&"function"==typeof e&&(v(this),this._ax.cbs["="+t.call(a,0,-1).join()]=e);return this};C.prototype.unhoc=function(){var a,e;if(k.call(this,"_ax"))for(e in a=t.call(arguments),"function"==typeof a[a.length-
1]&&(a=a.slice(0,-1)),a="="+a.join(),chainRxp=new B("^"+a),a=this._ax.cbs,a)chainRxp.test(e)&&delete a[e];return this};C.prototype.ask=function(){return d(this,arguments)};E.prototype.ask=function(){return d(this._client,arguments,this.id)};E.prototype.endExchange=function(a){var e=this.id,d=this._client,f=this._ax,g=0,k=!a,h,n;if(f)for(n in f)if(h=n==a,k||h||f[n].chain[0]==a)if(A(d,e,n),g++,h)break;return g};Subetha.msgType["subetha/exchange"]=function(a,e,d,f){var h=e.id,n,r,x,m,w,z;"object"==typeof d&&
u(r=d.xid)&&(k.call(a,"_ax")?k.call(d,"xkill")?J(a,r,h):"number"==typeof(x=d.idx)&&u(z=d.phrase)&&g(n=d.data)?(v(a,r,h),m=a._ax,w=m.xids[r].pids[h],d="="+w.concat(z).join(),k.call(m.cbs,d)&&w.length==x?(w.push(z),e={end:w.endFn,reply:function(){var d,e;return k.call(m.xids,r)&&w.length==x+1?(d=arguments,e=d[0],w.push(e),d=t.call(d,1),a._transmit("subetha/exchange",h,{xid:r,idx:x+1,phrase:e,data:d})):!1},thread:w.concat(),data:n,id:f.id,peer:e,timeStamp:f.timeStamp,phrase:z,xid:r},cb=m.cbs[d],n.length?
cb.apply(a,[e].concat(n)):cb.call(a,e)):A(a,r,h)):A(a,r,h):A(a,r,h))};return F}r?define(u):x?module.exports=u():h.Subetha&&u()}("function"===typeof define,"undefined"!=typeof exports,Array,RegExp,this);