Comparing version 0.3.4 to 0.3.5
# PeerJS Changelog | ||
## Version 0.3.5 beta (26 Nov 2013) | ||
* Fix bug where chunks were being emitted. | ||
## Version 0.3.4 beta (11 Nov 2013) | ||
@@ -4,0 +7,0 @@ * Fix file transfer issue in Chrome by chunking for data over 120KB. |
@@ -1,1 +0,1 @@ | ||
/*! peerjs.min.js build:0.3.4, production. Copyright(c) 2013 Michelle Bu <michelle@michellebu.com> */(function(e){function n(){this._pieces=[],this._parts=[]}function r(e){this.index=0,this.dataBuffer=e,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function i(e){this.utf8=e,this.bufferBuilder=new n}function s(){this._events={}}function u(e,t){if(!(this instanceof u))return new u(e);this._dc=e,l.debug=t,this._outgoing={},this._incoming={},this._received={},this._window=1e3,this._mtu=500,this._interval=0,this._count=0,this._queue=[],this._setupDC()}function c(e,t){if(!(this instanceof c))return new c(e,t);s.call(this),e&&e.constructor==Object?(t=e,e=undefined):e&&(e=e.toString()),t=l.extend({debug:0,host:l.CLOUD_HOST,port:l.CLOUD_PORT,key:"peerjs",config:l.defaultConfig},t),this.options=t,t.host==="/"&&(t.host=window.location.hostname),t.secure===undefined&&t.host!==l.CLOUD_HOST&&(t.secure=l.isSecure()),t.logFunction&&l.setLogFunction(t.logFunction),l.setLogLevel(t.debug);if(!l.supports.audioVideo&&!l.supports.data){this._delayedAbort("browser-incompatible","The current browser does not support WebRTC");return}if(!l.validateId(e)){this._delayedAbort("invalid-id",'ID "'+e+'" is invalid');return}if(!l.validateKey(t.key)){this._delayedAbort("invalid-key",'API KEY "'+t.key+'" is invalid');return}if(t.secure&&t.host==="0.peerjs.com"){this._delayedAbort("ssl-unavailable","The cloud server currently does not support HTTPS. Please run your own PeerServer to use HTTPS.");return}this.destroyed=!1,this.disconnected=!1,this.open=!1,this.connections={},this._lostMessages={};var n=this;this.socket=new v(this.options.secure,this.options.host,this.options.port,this.options.key),this.socket.on("message",function(e){n._handleMessage(e)}),this.socket.on("error",function(e){n._abort("socket-error",e)}),this.socket.on("close",function(){n.disconnected||n._abort("socket-closed","Underlying socket is already closed.")}),e?this._initialize(e):this._retrieveId()}function h(e,t,n){if(!(this instanceof h))return new h(e,t,n);s.call(this),this.options=l.extend({serialization:"binary",reliable:!1},n),this.open=!1,this.type="data",this.peer=e,this.provider=t,this.id=this.options.connectionId||h._idPrefix+l.randomToken(),this.label=this.options.label||this.id,this.metadata=this.options.metadata,this.serialization=this.options.serialization,this.reliable=this.options.reliable,this._chunkedData={},d.startConnection(this,this.options._payload||{originator:!0})}function p(e,t,n){if(!(this instanceof p))return new p(e,t,n);s.call(this),this.options=l.extend({},n),this.open=!1,this.type="media",this.peer=e,this.provider=t,this.metadata=this.options.metadata,this.localStream=this.options._stream,this.id=this.options.connectionId||p._idPrefix+l.randomToken(),this.localStream&&d.startConnection(this,{_stream:this.localStream,originator:!0})}function v(e,t,n,r){if(!(this instanceof v))return new v(e,t,n,r);s.call(this),this.disconnected=!1,this._queue=[];var i=e?"https://":"http://",o=e?"wss://":"ws://";this._httpUrl=i+t+":"+n+"/"+r,this._wsUrl=o+t+":"+n+"/peerjs?key="+r}var t={};t.useBlobBuilder=function(){try{return new Blob([]),!1}catch(e){return!0}}(),t.useArrayBufferView=!t.useBlobBuilder&&function(){try{return(new Blob([new Uint8Array([])])).size===0}catch(e){return!0}}(),e.binaryFeatures=t,e.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder,n.prototype.append=function(e){typeof e=="number"?this._pieces.push(e):(this.flush(),this._parts.push(e))},n.prototype.flush=function(){if(this._pieces.length>0){var e=new Uint8Array(this._pieces);t.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},n.prototype.getBuffer=function(){this.flush();if(t.useBlobBuilder){var e=new BlobBuilder;for(var n=0,r=this._parts.length;n<r;n++)e.append(this._parts[n]);return e.getBlob()}return new Blob(this._parts)},e.BinaryPack={unpack:function(e){var t=new r(e);return t.unpack()},pack:function(e,t){var n=new i(t);n.pack(e);var r=n.getBuffer();return r}},r.prototype.unpack=function(){var e=this.unpack_uint8();if(e<128){var t=e;return t}if((e^224)<32){var n=(e^224)-32;return n}var r;if((r=e^160)<=15)return this.unpack_raw(r);if((r=e^176)<=15)return this.unpack_string(r);if((r=e^144)<=15)return this.unpack_array(r);if((r=e^128)<=15)return this.unpack_map(r);switch(e){case 192:return null;case 193:return undefined;case 194:return!1;case 195:return!0;case 202:return this.unpack_float();case 203:return this.unpack_double();case 204:return this.unpack_uint8();case 205:return this.unpack_uint16();case 206:return this.unpack_uint32();case 207:return this.unpack_uint64();case 208:return this.unpack_int8();case 209:return this.unpack_int16();case 210:return this.unpack_int32();case 211:return this.unpack_int64();case 212:return undefined;case 213:return undefined;case 214:return undefined;case 215:return undefined;case 216:return r=this.unpack_uint16(),this.unpack_string(r);case 217:return r=this.unpack_uint32(),this.unpack_string(r);case 218:return r=this.unpack_uint16(),this.unpack_raw(r);case 219:return r=this.unpack_uint32(),this.unpack_raw(r);case 220:return r=this.unpack_uint16(),this.unpack_array(r);case 221:return r=this.unpack_uint32(),this.unpack_array(r);case 222:return r=this.unpack_uint16(),this.unpack_map(r);case 223:return r=this.unpack_uint32(),this.unpack_map(r)}},r.prototype.unpack_uint8=function(){var e=this.dataView[this.index]&255;return this.index++,e},r.prototype.unpack_uint16=function(){var e=this.read(2),t=(e[0]&255)*256+(e[1]&255);return this.index+=2,t},r.prototype.unpack_uint32=function(){var e=this.read(4),t=((e[0]*256+e[1])*256+e[2])*256+e[3];return this.index+=4,t},r.prototype.unpack_uint64=function(){var e=this.read(8),t=((((((e[0]*256+e[1])*256+e[2])*256+e[3])*256+e[4])*256+e[5])*256+e[6])*256+e[7];return this.index+=8,t},r.prototype.unpack_int8=function(){var e=this.unpack_uint8();return e<128?e:e-256},r.prototype.unpack_int16=function(){var e=this.unpack_uint16();return e<32768?e:e-65536},r.prototype.unpack_int32=function(){var e=this.unpack_uint32();return e<Math.pow(2,31)?e:e-Math.pow(2,32)},r.prototype.unpack_int64=function(){var e=this.unpack_uint64();return e<Math.pow(2,63)?e:e-Math.pow(2,64)},r.prototype.unpack_raw=function(e){if(this.length<this.index+e)throw new Error("BinaryPackFailure: index is out of range "+this.index+" "+e+" "+this.length);var t=this.dataBuffer.slice(this.index,this.index+e);return this.index+=e,t},r.prototype.unpack_string=function(e){var t=this.read(e),n=0,r="",i,s;while(n<e)i=t[n],i<128?(r+=String.fromCharCode(i),n++):(i^192)<32?(s=(i^192)<<6|t[n+1]&63,r+=String.fromCharCode(s),n+=2):(s=(i&15)<<12|(t[n+1]&63)<<6|t[n+2]&63,r+=String.fromCharCode(s),n+=3);return this.index+=e,r},r.prototype.unpack_array=function(e){var t=new Array(e);for(var n=0;n<e;n++)t[n]=this.unpack();return t},r.prototype.unpack_map=function(e){var t={};for(var n=0;n<e;n++){var r=this.unpack(),i=this.unpack();t[r]=i}return t},r.prototype.unpack_float=function(){var e=this.unpack_uint32(),t=e>>31,n=(e>>23&255)-127,r=e&8388607|8388608;return(t==0?1:-1)*r*Math.pow(2,n-23)},r.prototype.unpack_double=function(){var e=this.unpack_uint32(),t=this.unpack_uint32(),n=e>>31,r=(e>>20&2047)-1023,i=e&1048575|1048576,s=i*Math.pow(2,r-20)+t*Math.pow(2,r-52);return(n==0?1:-1)*s},r.prototype.read=function(e){var t=this.index;if(t+e<=this.length)return this.dataView.subarray(t,t+e);throw new Error("BinaryPackFailure: read index out of range")},i.prototype.getBuffer=function(){return this.bufferBuilder.getBuffer()},i.prototype.pack=function(e){var n=typeof e;if(n=="string")this.pack_string(e);else if(n=="number")Math.floor(e)===e?this.pack_integer(e):this.pack_double(e);else if(n=="boolean")e===!0?this.bufferBuilder.append(195):e===!1&&this.bufferBuilder.append(194);else if(n=="undefined")this.bufferBuilder.append(192);else{if(n!="object")throw new Error('Type "'+n+'" not yet supported');if(e===null)this.bufferBuilder.append(192);else{var r=e.constructor;if(r==Array)this.pack_array(e);else if(r==Blob||r==File)this.pack_bin(e);else if(r==ArrayBuffer)t.useArrayBufferView?this.pack_bin(new Uint8Array(e)):this.pack_bin(e);else if("BYTES_PER_ELEMENT"in e)t.useArrayBufferView?this.pack_bin(e):this.pack_bin(e.buffer);else if(r==Object)this.pack_object(e);else if(r==Date)this.pack_string(e.toString());else{if(typeof e.toBinaryPack!="function")throw new Error('Type "'+r.toString()+'" not yet supported');this.bufferBuilder.append(e.toBinaryPack())}}}this.bufferBuilder.flush()},i.prototype.pack_bin=function(e){var t=e.length||e.byteLength||e.size;if(t<=15)this.pack_uint8(160+t);else if(t<=65535)this.bufferBuilder.append(218),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_string=function(e){var t;if(this.utf8){var n=new Blob([e]);t=n.size}else t=e.length;if(t<=15)this.pack_uint8(176+t);else if(t<=65535)this.bufferBuilder.append(216),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_array=function(e){var t=e.length;if(t<=15)this.pack_uint8(144+t);else if(t<=65535)this.bufferBuilder.append(220),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(t)}for(var n=0;n<t;n++)this.pack(e[n])},i.prototype.pack_integer=function(e){if(-32<=e&&e<=127)this.bufferBuilder.append(e&255);else if(0<=e&&e<=255)this.bufferBuilder.append(204),this.pack_uint8(e);else if(-128<=e&&e<=127)this.bufferBuilder.append(208),this.pack_int8(e);else if(0<=e&&e<=65535)this.bufferBuilder.append(205),this.pack_uint16(e);else if(-32768<=e&&e<=32767)this.bufferBuilder.append(209),this.pack_int16(e);else if(0<=e&&e<=4294967295)this.bufferBuilder.append(206),this.pack_uint32(e);else if(-2147483648<=e&&e<=2147483647)this.bufferBuilder.append(210),this.pack_int32(e);else if(-0x8000000000000000<=e&&e<=0x8000000000000000)this.bufferBuilder.append(211),this.pack_int64(e);else{if(!(0<=e&&e<=0x10000000000000000))throw new Error("Invalid integer");this.bufferBuilder.append(207),this.pack_uint64(e)}},i.prototype.pack_double=function(e){var t=0;e<0&&(t=1,e=-e);var n=Math.floor(Math.log(e)/Math.LN2),r=e/Math.pow(2,n)-1,i=Math.floor(r*Math.pow(2,52)),s=Math.pow(2,32),o=t<<31|n+1023<<20|i/s&1048575,u=i%s;this.bufferBuilder.append(203),this.pack_int32(o),this.pack_int32(u)},i.prototype.pack_object=function(e){var t=Object.keys(e),n=t.length;if(n<=15)this.pack_uint8(128+n);else if(n<=65535)this.bufferBuilder.append(222),this.pack_uint16(n);else{if(!(n<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(223),this.pack_uint32(n)}for(var r in e)e.hasOwnProperty(r)&&(this.pack(r),this.pack(e[r]))},i.prototype.pack_uint8=function(e){this.bufferBuilder.append(e)},i.prototype.pack_uint16=function(e){this.bufferBuilder.append(e>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_uint32=function(e){var t=e&4294967295;this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255)},i.prototype.pack_uint64=function(e){var t=e/Math.pow(2,32),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)},i.prototype.pack_int8=function(e){this.bufferBuilder.append(e&255)},i.prototype.pack_int16=function(e){this.bufferBuilder.append((e&65280)>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int32=function(e){this.bufferBuilder.append(e>>>24&255),this.bufferBuilder.append((e&16711680)>>>16),this.bufferBuilder.append((e&65280)>>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int64=function(e){var t=Math.floor(e/Math.pow(2,32)),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)};var o=Array.isArray;s.prototype.addListener=function(e,t,n,r){if("function"!=typeof t)throw new Error("addListener only takes instances of Function");return this.emit("newListener",e,typeof t.listener=="function"?t.listener:t),this._events[e]?o(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t,this},s.prototype.on=s.prototype.addListener,s.prototype.once=function(e,t,n){function i(){r.removeListener(e,i),t.apply(this,arguments)}if("function"!=typeof t)throw new Error(".once only takes instances of Function");var r=this;return i.listener=t,r.on(e,i),this},s.prototype.removeListener=function(e,t,n){if("function"!=typeof t)throw new Error("removeListener only takes instances of Function");if(!this._events[e])return this;var r=this._events[e];if(o(r)){var i=-1;for(var s=0,u=r.length;s<u;s++)if(r[s]===t||r[s].listener&&r[s].listener===t){i=s;break}if(i<0)return this;r.splice(i,1),r.length==0&&delete this._events[e]}else(r===t||r.listener&&r.listener===t)&&delete this._events[e];return this},s.prototype.off=s.prototype.removeListener,s.prototype.removeAllListeners=function(e){return arguments.length===0?(this._events={},this):(e&&this._events&&this._events[e]&&(this._events[e]=null),this)},s.prototype.listeners=function(e){return this._events[e]||(this._events[e]=[]),o(this._events[e])||(this._events[e]=[this._events[e]]),this._events[e]},s.prototype.emit=function(e){var e=arguments[0],t=this._events[e];if(!t)return!1;if(typeof t=="function"){switch(arguments.length){case 1:t.call(this);break;case 2:t.call(this,arguments[1]);break;case 3:t.call(this,arguments[1],arguments[2]);break;default:var n=arguments.length,r=new Array(n-1);for(var i=1;i<n;i++)r[i-1]=arguments[i];t.apply(this,r)}return!0}if(o(t)){var n=arguments.length,r=new Array(n-1);for(var i=1;i<n;i++)r[i-1]=arguments[i];var s=t.slice();for(var i=0,n=s.length;i<n;i++)s[i].apply(this,r);return!0}return!1},u.prototype.send=function(e){var t=l.pack(e);if(t.size<this._mtu){this._handleSend(["no",t]);return}this._outgoing[this._count]={ack:0,chunks:this._chunk(t)},l.debug&&(this._outgoing[this._count].timer=new Date),this._sendWindowedChunks(this._count),this._count+=1},u.prototype._setupInterval=function(){var e=this;this._timeout=setInterval(function(){var t=e._queue.shift();if(t._multiple)for(var n=0,r=t.length;n<r;n+=1)e._intervalSend(t[n]);else e._intervalSend(t)},this._interval)},u.prototype._intervalSend=function(e){var t=this;e=l.pack(e),l.blobToBinaryString(e,function(e){t._dc.send(e)}),t._queue.length===0&&(clearTimeout(t._timeout),t._timeout=null)},u.prototype._processAcks=function(){for(var e in this._outgoing)this._outgoing.hasOwnProperty(e)&&this._sendWindowedChunks(e)},u.prototype._handleSend=function(e){var t=!0;for(var n=0,r=this._queue.length;n<r;n+=1){var i=this._queue[n];i===e?t=!1:i._multiple&&i.indexOf(e)!==-1&&(t=!1)}t&&(this._queue.push(e),this._timeout||this._setupInterval())},u.prototype._setupDC=function(){var e=this;this._dc.onmessage=function(t){var n=t.data,r=n.constructor;if(r===String){var i=l.binaryStringToArrayBuffer(n);n=l.unpack(i),e._handleMessage(n)}}},u.prototype._handleMessage=function(e){var t=e[1],n=this._incoming[t],r=this._outgoing[t],i;switch(e[0]){case"no":var s=t;!s||this.onmessage(l.unpack(s));break;case"end":i=n,this._received[t]=e[2];if(!i)break;this._ack(t);break;case"ack":i=r;if(!!i){var o=e[2];i.ack=Math.max(o,i.ack),i.ack>=i.chunks.length?(l.log("Time: ",new Date-i.timer),delete this._outgoing[t]):this._processAcks()}break;case"chunk":i=n;if(!i){var u=this._received[t];if(u===!0)break;i={ack:["ack",t,0],chunks:[]},this._incoming[t]=i}var a=e[2],f=e[3];i.chunks[a]=new Uint8Array(f),a===i.ack[2]&&this._calculateNextAck(t),this._ack(t);break;default:this._handleSend(e)}},u.prototype._chunk=function(e){var t=[],n=e.size,r=0;while(r<n){var i=Math.min(n,r+this._mtu),s=e.slice(r,i),o={payload:s};t.push(o),r=i}return l.log("Created",t.length,"chunks."),t},u.prototype._ack=function(e){var t=this._incoming[e].ack;this._received[e]===t[2]&&(this._complete(e),this._received[e]=!0),this._handleSend(t)},u.prototype._calculateNextAck=function(e){var t=this._incoming[e],n=t.chunks;for(var r=0,i=n.length;r<i;r+=1)if(n[r]===undefined){t.ack[2]=r;return}t.ack[2]=n.length},u.prototype._sendWindowedChunks=function(e){l.log("sendWindowedChunks for: ",e);var t=this._outgoing[e],n=t.chunks,r=[],i=Math.min(t.ack+this._window,n.length);for(var s=t.ack;s<i;s+=1)if(!n[s].sent||s===t.ack)n[s].sent=!0,r.push(["chunk",e,s,n[s].payload]);t.ack+this._window>=n.length&&r.push(["end",e,n.length]),r._multiple=!0,this._handleSend(r)},u.prototype._complete=function(e){l.log("Completed called for",e);var t=this,n=this._incoming[e].chunks,r=new Blob(n);l.blobToArrayBuffer(r,function(e){t.onmessage(l.unpack(e))}),delete this._incoming[e]},u.higherBandwidthSDP=function(e){var t=navigator.appVersion.match(/Chrome\/(.*?) /);if(t){t=parseInt(t[1].split(".").shift());if(t<31){var n=e.split("b=AS:30"),r="b=AS:102400";if(n.length>1)return n[0]+r+n[1]}}return e},u.prototype.onmessage=function(e){},e.Reliable=u,e.RTCSessionDescription=window.mozRTCSessionDescription||window.RTCSessionDescription,e.RTCPeerConnection=window.mozRTCPeerConnection||window.webkitRTCPeerConnection||window.RTCPeerConnection,e.RTCIceCandidate=window.mozRTCIceCandidate||window.RTCIceCandidate;var a={iceServers:[{url:"stun:stun.l.google.com:19302"}]},f=1,l={noop:function(){},CLOUD_HOST:"0.peerjs.com",CLOUD_PORT:9e3,chunkedMTU:12e4,logLevel:0,setLogLevel:function(e){var t=parseInt(e,10);isNaN(parseInt(e,10))?l.logLevel=e?3:0:l.logLevel=t,l.log=l.warn=l.error=l.noop,l.logLevel>0&&(l.error=l._printWith("ERROR")),l.logLevel>1&&(l.warn=l._printWith("WARNING")),l.logLevel>2&&(l.log=l._print)},setLogFunction:function(e){e.constructor!==Function?l.warn("The log function you passed in is not a function. Defaulting to regular logs."):l._print=e},_printWith:function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e),l._print.apply(l,t)}},_print:function(){var e=!1,t=Array.prototype.slice.call(arguments);t.unshift("PeerJS: ");for(var n=0,r=t.length;n<r;n++)t[n]instanceof Error&&(t[n]="("+t[n].name+") "+t[n].message,e=!0);e?console.error.apply(console,t):console.log.apply(console,t)},defaultConfig:a,browser:function(){return window.mozRTCPeerConnection?"Firefox":window.webkitRTCPeerConnection?"Chrome":window.RTCPeerConnection?"Supported":"Unsupported"}(),supports:function(){if(typeof RTCPeerConnection=="undefined")return{};var e=!0,t=!0,n=!1,r=!1,i=!!window.webkitRTCPeerConnection,s,o;try{s=new RTCPeerConnection(a,{optional:[{RtpDataChannels:!0}]})}catch(u){e=!1,t=!1}if(e)try{o=s.createDataChannel("_PEERJSTEST")}catch(u){e=!1}if(e){try{o.binaryType="blob",n=!0}catch(u){}var f=new RTCPeerConnection(a,{});try{var c=f.createDataChannel("_PEERJSRELIABLETEST",{});r=c.reliable}catch(u){}f.close()}t&&(t=!!s.addStream);if(!i&&e){var h=new RTCPeerConnection(a,{optional:[{RtpDataChannels:!0}]});h.onnegotiationneeded=function(){i=!0,l&&l.supports&&(l.supports.onnegotiationneeded=!0)};var p=h.createDataChannel("_PEERJSNEGOTIATIONTEST");setTimeout(function(){h.close()},1e3)}return s&&s.close(),{audioVideo:t,data:e,binaryBlob:n,binary:r,reliable:r,sctp:r,onnegotiationneeded:i}}(),validateId:function(e){return!e||/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(e)},validateKey:function(e){return!e||/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(e)},debug:!1,inherits:function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})},extend:function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e},pack:BinaryPack.pack,unpack:BinaryPack.unpack,log:function(){if(l.debug){var e=!1,t=Array.prototype.slice.call(arguments);t.unshift("PeerJS: ");for(var n=0,r=t.length;n<r;n++)t[n]instanceof Error&&(t[n]="("+t[n].name+") "+t[n].message,e=!0);e?console.error.apply(console,t):console.log.apply(console,t)}},setZeroTimeout:function(e){function r(r){t.push(r),e.postMessage(n,"*")}function i(r){r.source==e&&r.data==n&&(r.stopPropagation&&r.stopPropagation(),t.length&&t.shift()())}var t=[],n="zero-timeout-message";return e.addEventListener?e.addEventListener("message",i,!0):e.attachEvent&&e.attachEvent("onmessage",i),r}(this),chunk:function(e){var t=[],n=e.size,r=index=0,i=Math.ceil(n/l.chunkedMTU);while(r<n){var s=Math.min(n,r+l.chunkedMTU),o=e.slice(r,s),u={__peerData:f,n:index,data:o,total:i};t.push(u),r=s,index+=1}return f+=1,t},blobToArrayBuffer:function(e,t){var n=new FileReader;n.onload=function(e){t(e.target.result)},n.readAsArrayBuffer(e)},blobToBinaryString:function(e,t){var n=new FileReader;n.onload=function(e){t(e.target.result)},n.readAsBinaryString(e)},binaryStringToArrayBuffer:function(e){var t=new Uint8Array(e.length);for(var n=0;n<e.length;n++)t[n]=e.charCodeAt(n)&255;return t.buffer},randomToken:function(){return Math.random().toString(36).substr(2)},isSecure:function(){return location.protocol==="https:"}};e.util=l,l.inherits(c,s),c.prototype._retrieveId=function(e){var t=this,n=new XMLHttpRequest,r=this.options.secure?"https://":"http://",i=r+this.options.host+":"+this.options.port+"/"+this.options.key+"/id",s="?ts="+(new Date).getTime()+""+Math.random();i+=s,n.open("get",i,!0),n.onerror=function(e){l.error("Error retrieving ID",e),t._abort("server-error","Could not get an ID from the server")},n.onreadystatechange=function(){if(n.readyState!==4)return;if(n.status!==200){n.onerror();return}t._initialize(n.responseText)},n.send(null)},c.prototype._initialize=function(e){var t=this;this.id=e,this.socket.start(this.id)},c.prototype._handleMessage=function(e){var t=e.type,n=e.payload,r=e.src;switch(t){case"OPEN":this.emit("open",this.id),this.open=!0;break;case"ERROR":this._abort("server-error",n.msg);break;case"ID-TAKEN":this._abort("unavailable-id","ID `"+this.id+"` is taken");break;case"INVALID-KEY":this._abort("invalid-key",'API KEY "'+this._key+'" is invalid');break;case"LEAVE":l.log("Received leave message from",r),this._cleanupPeer(r);break;case"EXPIRE":this.emit("error",new Error("Could not connect to peer "+r));break;case"OFFER":var i=n.connectionId,s=this.getConnection(r,i);if(s)l.warn("Offer received for existing Connection ID:",i);else{if(n.type==="media"){var s=new p(r,this,{connectionId:i,_payload:n,metadata:n.metadata});this._addConnection(r,s),this.emit("call",s)}else{if(n.type!=="data"){l.warn("Received malformed connection type:",n.type);return}s=new h(r,this,{connectionId:i,_payload:n,metadata:n.metadata,label:n.label,serialization:n.serialization,reliable:n.reliable}),this._addConnection(r,s),this.emit("connection",s)}var o=this._getMessages(i);for(var u=0,a=o.length;u<a;u+=1)s.handleMessage(o[u])}break;default:if(!n){l.warn("You received a malformed message from "+r+" of type "+t);return}var f=n.connectionId,s=this.getConnection(r,f);s&&s.pc?s.handleMessage(e):f?this._storeMessage(f,e):l.warn("You received an unrecognized message:",e)}},c.prototype._storeMessage=function(e,t){this._lostMessages[e]||(this._lostMessages[e]=[]),this._lostMessages[e].push(t)},c.prototype._getMessages=function(e){var t=this._lostMessages[e];return t?(delete this._lostMessages[e],t):[]},c.prototype.connect=function(e,t){if(this.disconnected){l.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect."),this.emit("error",new Error("Cannot connect to new Peer after disconnecting from server."));return}var n=new h(e,this,t);return this._addConnection(e,n),n},c.prototype.call=function(e,t,n){if(this.disconnected){l.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect."),this.emit("error",new Error("Cannot connect to new Peer after disconnecting from server."));return}if(!t){l.error("To call a peer, you must provide a stream from your browser's `getUserMedia`.");return}n=n||{},n._stream=t;var r=new p(e,this,n);return this._addConnection(e,r),r},c.prototype._addConnection=function(e,t){this.connections[e]||(this.connections[e]=[]),this.connections[e].push(t)},c.prototype.getConnection=function(e,t){var n=this.connections[e];if(!n)return null;for(var r=0,i=n.length;r<i;r++)if(n[r].id===t)return n[r];return null},c.prototype._delayedAbort=function(e,t){var n=this;l.setZeroTimeout(function(){n._abort(e,t)})},c.prototype._abort=function(e,t){l.error("Aborting. Error:",t);var n=new Error(t);n.type=e,this.destroy(),this.emit("error",n)},c.prototype.destroy=function(){this.destroyed||(this._cleanup(),this.disconnect(),this.destroyed=!0)},c.prototype._cleanup=function(){if(this.connections){var e=Object.keys(this.connections);for(var t=0,n=e.length;t<n;t++)this._cleanupPeer(e[t])}this.emit("close")},c.prototype._cleanupPeer=function(e){var t=this.connections[e];for(var n=0,r=t.length;n<r;n+=1)t[n].close()},c.prototype.disconnect=function(){var e=this;l.setZeroTimeout(function(){e.disconnected||(e.disconnected=!0,e.open=!1,e.socket&&e.socket.close(),e.id=null)})},e.Peer=c,l.inherits(h,s),h._idPrefix="dc_",h.prototype.initialize=function(e){this._dc=e,this._configureDataChannel()},h.prototype._configureDataChannel=function(){var e=this;l.supports.sctp&&(this._dc.binaryType="arraybuffer"),this._dc.onopen=function(){l.log("Data channel connection success"),e.open=!0,e.emit("open")},!l.supports.sctp&&this.reliable&&(this._reliable=new u(this._dc,l.debug)),this._reliable?this._reliable.onmessage=function(t){e.emit("data",t)}:this._dc.onmessage=function(t){e._handleDataMessage(t)},this._dc.onclose=function(t){l.log("DataChannel closed for:",e.peer),e.close()}},h.prototype._handleDataMessage=function(e){var t=this,n=e.data,r=n.constructor;if(this.serialization==="binary"||this.serialization==="binary-utf8"){if(r===Blob){l.blobToArrayBuffer(n,function(e){n=l.unpack(e),t.emit("data",n)});return}if(r===ArrayBuffer)n=l.unpack(n);else if(r===String){var i=l.binaryStringToArrayBuffer(n);n=l.unpack(i)}}else this.serialization==="json"&&(n=JSON.parse(n));if(n.__peerData){var s=n.__peerData,o=this._chunkedData[s]||{data:[],count:0,total:n.total};o.data[n.n]=n.data,o.count+=1;if(o.total===o.count){n=new Blob(o.data),this._handleDataMessage({data:n}),delete this._chunkedData[s];return}this._chunkedData[s]=o}this.emit("data",n)},h.prototype.close=function(){if(!this.open)return;this.open=!1,d.cleanup(this),this.emit("close")},h.prototype.send=function(e,t){if(!this.open){this.emit("error",new Error("Connection is not open. You should listen for the `open` event before sending messages."));return}if(this._reliable){this._reliable.send(e);return}var n=this;if(this.serialization==="json")this._dc.send(JSON.stringify(e));else if("binary-utf8".indexOf(this.serialization)!==-1){var r=this.serialization==="binary-utf8",i=l.pack(e,r);if(l.browser!=="Firefox"&&!t&&i.size>l.chunkedMTU){this._sendChunks(i);return}l.supports.sctp?l.supports.binaryBlob?this._dc.send(i):l.blobToArrayBuffer(i,function(e){n._dc.send(e)}):l.blobToBinaryString(i,function(e){n._dc.send(e)})}else this._dc.send(e)},h.prototype._sendChunks=function(e){var t=l.chunk(e);for(var n=0,r=t.length;n<r;n+=1){var e=t[n];this.send(e,!0)}},h.prototype.handleMessage=function(e){var t=e.payload;switch(e.type){case"ANSWER":d.handleSDP(e.type,this,t.sdp);break;case"CANDIDATE":d.handleCandidate(this,t.candidate);break;default:l.warn("Unrecognized message type:",e.type,"from peer:",this.peer)}},l.inherits(p,s),p._idPrefix="mc_",p.prototype.addStream=function(e){l.log("Receiving stream",e),this.remoteStream=e,this.emit("stream",e)},p.prototype.handleMessage=function(e){var t=e.payload;switch(e.type){case"ANSWER":d.handleSDP(e.type,this,t.sdp),this.open=!0;break;case"CANDIDATE":d.handleCandidate(this,t.candidate);break;default:l.warn("Unrecognized message type:",e.type,"from peer:",this.peer)}},p.prototype.answer=function(e){if(this.localStream){l.warn("Local stream already exists on this MediaConnection. Are you answering a call twice?");return}this.options._payload._stream=e,this.localStream=e,d.startConnection(this,this.options._payload);var t=this.provider._getMessages(this.id);for(var n=0,r=t.length;n<r;n+=1)this.handleMessage(t[n]);this.open=!0},p.prototype.close=function(){if(!this.open)return;this.open=!1,d.cleanup(this),this.emit("close")};var d={pcs:{data:{},media:{}},queue:[]};d._idPrefix="pc_",d.startConnection=function(e,t){var n=d._getPeerConnection(e,t);e.type==="media"&&t._stream&&n.addStream(t._stream),e.pc=n;if(t.originator){if(e.type==="data"){var r={};l.supports.sctp||(r={reliable:t.reliable});var i=n.createDataChannel(e.label,r);e.initialize(i)}l.supports.onnegotiationneeded||d._makeOffer(e)}else d.handleSDP("OFFER",e,t.sdp)},d._getPeerConnection=function(e,t){d.pcs[e.type]||l.error(e.type+" is not a valid connection type. Maybe you overrode the `type` property somewhere."),d.pcs[e.type][e.peer]||(d.pcs[e.type][e.peer]={});var n=d.pcs[e.type][e.peer],r;t.pc&&(r=d.pcs[e.type][e.peer][t.pc]);if(!r||r.signalingState!=="stable")r=d._startPeerConnection(e);return r},d._startPeerConnection=function(e){l.log("Creating RTCPeerConnection.");var t=d._idPrefix+l.randomToken(),n={};e.type==="data"&&!l.supports.sctp?n={optional:[{RtpDataChannels:!0}]}:e.type==="media"&&(n={optional:[{DtlsSrtpKeyAgreement:!0}]});var r=new RTCPeerConnection(e.provider.options.config,n);return d.pcs[e.type][e.peer][t]=r,d._setupListeners(e,r,t),r},d._setupListeners=function(e,t,n){var r=e.peer,i=e.id,s=e.provider;l.log("Listening for ICE candidates."),t.onicecandidate=function(t){t.candidate&&(l.log("Received ICE candidates for:",e.peer),s.socket.send({type:"CANDIDATE",payload:{candidate:t.candidate,type:e.type,connectionId:e.id},dst:r}))},t.oniceconnectionstatechange=function(){switch(t.iceConnectionState){case"disconnected":case"failed":l.log("iceConnectionState is disconnected, closing connections to "+r),e.close();break;case"completed":t.onicecandidate=l.noop}},t.onicechange=t.oniceconnectionstatechange,l.log("Listening for `negotiationneeded`"),t.onnegotiationneeded=function(){l.log("`negotiationneeded` triggered"),t.signalingState=="stable"?d._makeOffer(e):l.log("onnegotiationneeded triggered when not stable. Is another connection being established?")},l.log("Listening for data channel"),t.ondatachannel=function(e){l.log("Received data channel");var t=e.channel,n=s.getConnection(r,i);n.initialize(t)},l.log("Listening for remote stream"),t.onaddstream=function(e){l.log("Received remote stream");var t=e.stream;s.getConnection(r,i).addStream(t)}},d.cleanup=function(e){l.log("Cleaning up PeerConnection to "+e.peer);var t=e.pc;!!t&&(t.readyState!=="closed"||t.signalingState!=="closed")&&(t.close(),e.pc=null)},d._makeOffer=function(e){var t=e.pc;t.createOffer(function(n){l.log("Created offer."),!l.supports.sctp&&e.type==="data"&&e.reliable&&(n.sdp=u.higherBandwidthSDP(n.sdp)),t.setLocalDescription(n,function(){l.log("Set localDescription: offer","for:",e.peer),e.provider.socket.send({type:"OFFER",payload:{sdp:n,type:e.type,label:e.label,reliable:e.reliable,serialization:e.serialization,metadata:e.metadata,connectionId:e.id,sctp:l.supports.sctp},dst:e.peer})},function(t){e.provider.emit("error",t),l.log("Failed to setLocalDescription, ",t)})},function(t){e.provider.emit("error",t),l.log("Failed to createOffer, ",t)},e.options.constraints)},d._makeAnswer=function(e){var t=e.pc;t.createAnswer(function(n){l.log("Created answer."),!l.supports.sctp&&e.type==="data"&&e.reliable&&(n.sdp=u.higherBandwidthSDP(n.sdp)),t.setLocalDescription(n,function(){l.log("Set localDescription: answer","for:",e.peer),e.provider.socket.send({type:"ANSWER",payload:{sdp:n,type:e.type,connectionId:e.id},dst:e.peer})},function(t){e.provider.emit("error",t),l.log("Failed to setLocalDescription, ",t)})},function(t){e.provider.emit("error",t),l.log("Failed to create answer, ",t)})},d.handleSDP=function(e,t,n){n=new RTCSessionDescription(n);var r=t.pc;l.log("Setting remote description",n),r.setRemoteDescription(n,function(){l.log("Set remoteDescription:",e,"for:",t.peer),e==="OFFER"&&d._makeAnswer(t)},function(e){t.provider.emit("error",e),l.log("Failed to setRemoteDescription, ",e)})},d.handleCandidate=function(e,t){var n=t.candidate,r=t.sdpMLineIndex;e.pc.addIceCandidate(new RTCIceCandidate({sdpMLineIndex:r,candidate:n})),l.log("Added ICE candidate for:",e.peer)},l.inherits(v,s),v.prototype.start=function(e){this.id=e;var t=l.randomToken();this._httpUrl+="/"+e+"/"+t,this._wsUrl+="&id="+e+"&token="+t,this._startXhrStream(),this._startWebSocket()},v.prototype._startWebSocket=function(e){var t=this;if(this._socket)return;this._socket=new WebSocket(this._wsUrl),this._socket.onmessage=function(e){var n;try{n=JSON.parse(e.data)}catch(r){l.log("Invalid server message",e.data);return}t.emit("message",n)},this._socket.onopen=function(){t._timeout&&(clearTimeout(t._timeout),setTimeout(function(){t._http.abort(),t._http=null},5e3)),t._sendQueuedMessages(),l.log("Socket open")}},v.prototype._startXhrStream=function(e){try{var t=this;this._http=new XMLHttpRequest,this._http._index=1,this._http._streamIndex=e||0,this._http.open("post",this._httpUrl+"/id?i="+this._http._streamIndex,!0),this._http.onreadystatechange=function(){this.readyState==2&&this.old&&(this.old.abort(),delete this.old),this.readyState>2&&this.status==200&&this.responseText&&t._handleStream(this)},this._http.send(null),this._setHTTPTimeout()}catch(n){l.log("XMLHttpRequest not available; defaulting to WebSockets")}},v.prototype._handleStream=function(e){var t=e.responseText.split("\n");if(e._buffer)while(e._buffer.length>0){var n=e._buffer.shift(),r=t[n];try{r=JSON.parse(r)}catch(i){e._buffer.shift(n);break}this.emit("message",r)}var s=t[e._index];if(s){e._index+=1;if(e._index===t.length)e._buffer||(e._buffer=[]),e._buffer.push(e._index-1);else{try{s=JSON.parse(s)}catch(i){l.log("Invalid server message",s);return}this.emit("message",s)}}},v.prototype._setHTTPTimeout=function(){var e=this;this._timeout=setTimeout(function(){var t=e._http;e._wsOpen()?t.abort():(e._startXhrStream(t._streamIndex+1),e._http.old=t)},25e3)},v.prototype._wsOpen=function(){return this._socket&&this._socket.readyState==1},v.prototype._sendQueuedMessages=function(){for(var e=0,t=this._queue.length;e<t;e+=1)this.send(this._queue[e])},v.prototype.send=function(e){if(this.disconnected)return;if(!this.id){this._queue.push(e);return}if(!e.type){this.emit("error","Invalid message");return}var t=JSON.stringify(e);if(this._wsOpen())this._socket.send(t);else{var n=new XMLHttpRequest,r=this._httpUrl+"/"+e.type.toLowerCase();n.open("post",r,!0),n.setRequestHeader("Content-Type","application/json"),n.send(t)}},v.prototype.close=function(){!this.disconnected&&this._wsOpen()&&(this._socket.close(),this.disconnected=!0)}})(this) | ||
/*! peerjs.min.js build:0.3.5, production. Copyright(c) 2013 Michelle Bu <michelle@michellebu.com> */(function(e){function n(){this._pieces=[],this._parts=[]}function r(e){this.index=0,this.dataBuffer=e,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function i(e){this.utf8=e,this.bufferBuilder=new n}function s(){this._events={}}function u(e,t){if(!(this instanceof u))return new u(e);this._dc=e,l.debug=t,this._outgoing={},this._incoming={},this._received={},this._window=1e3,this._mtu=500,this._interval=0,this._count=0,this._queue=[],this._setupDC()}function c(e,t){if(!(this instanceof c))return new c(e,t);s.call(this),e&&e.constructor==Object?(t=e,e=undefined):e&&(e=e.toString()),t=l.extend({debug:0,host:l.CLOUD_HOST,port:l.CLOUD_PORT,key:"peerjs",config:l.defaultConfig},t),this.options=t,t.host==="/"&&(t.host=window.location.hostname),t.secure===undefined&&t.host!==l.CLOUD_HOST&&(t.secure=l.isSecure()),t.logFunction&&l.setLogFunction(t.logFunction),l.setLogLevel(t.debug);if(!l.supports.audioVideo&&!l.supports.data){this._delayedAbort("browser-incompatible","The current browser does not support WebRTC");return}if(!l.validateId(e)){this._delayedAbort("invalid-id",'ID "'+e+'" is invalid');return}if(!l.validateKey(t.key)){this._delayedAbort("invalid-key",'API KEY "'+t.key+'" is invalid');return}if(t.secure&&t.host==="0.peerjs.com"){this._delayedAbort("ssl-unavailable","The cloud server currently does not support HTTPS. Please run your own PeerServer to use HTTPS.");return}this.destroyed=!1,this.disconnected=!1,this.open=!1,this.connections={},this._lostMessages={};var n=this;this.socket=new v(this.options.secure,this.options.host,this.options.port,this.options.key),this.socket.on("message",function(e){n._handleMessage(e)}),this.socket.on("error",function(e){n._abort("socket-error",e)}),this.socket.on("close",function(){n.disconnected||n._abort("socket-closed","Underlying socket is already closed.")}),e?this._initialize(e):this._retrieveId()}function h(e,t,n){if(!(this instanceof h))return new h(e,t,n);s.call(this),this.options=l.extend({serialization:"binary",reliable:!1},n),this.open=!1,this.type="data",this.peer=e,this.provider=t,this.id=this.options.connectionId||h._idPrefix+l.randomToken(),this.label=this.options.label||this.id,this.metadata=this.options.metadata,this.serialization=this.options.serialization,this.reliable=this.options.reliable,this._chunkedData={},d.startConnection(this,this.options._payload||{originator:!0})}function p(e,t,n){if(!(this instanceof p))return new p(e,t,n);s.call(this),this.options=l.extend({},n),this.open=!1,this.type="media",this.peer=e,this.provider=t,this.metadata=this.options.metadata,this.localStream=this.options._stream,this.id=this.options.connectionId||p._idPrefix+l.randomToken(),this.localStream&&d.startConnection(this,{_stream:this.localStream,originator:!0})}function v(e,t,n,r){if(!(this instanceof v))return new v(e,t,n,r);s.call(this),this.disconnected=!1,this._queue=[];var i=e?"https://":"http://",o=e?"wss://":"ws://";this._httpUrl=i+t+":"+n+"/"+r,this._wsUrl=o+t+":"+n+"/peerjs?key="+r}var t={};t.useBlobBuilder=function(){try{return new Blob([]),!1}catch(e){return!0}}(),t.useArrayBufferView=!t.useBlobBuilder&&function(){try{return(new Blob([new Uint8Array([])])).size===0}catch(e){return!0}}(),e.binaryFeatures=t,e.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder,n.prototype.append=function(e){typeof e=="number"?this._pieces.push(e):(this.flush(),this._parts.push(e))},n.prototype.flush=function(){if(this._pieces.length>0){var e=new Uint8Array(this._pieces);t.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},n.prototype.getBuffer=function(){this.flush();if(t.useBlobBuilder){var e=new BlobBuilder;for(var n=0,r=this._parts.length;n<r;n++)e.append(this._parts[n]);return e.getBlob()}return new Blob(this._parts)},e.BinaryPack={unpack:function(e){var t=new r(e);return t.unpack()},pack:function(e,t){var n=new i(t);n.pack(e);var r=n.getBuffer();return r}},r.prototype.unpack=function(){var e=this.unpack_uint8();if(e<128){var t=e;return t}if((e^224)<32){var n=(e^224)-32;return n}var r;if((r=e^160)<=15)return this.unpack_raw(r);if((r=e^176)<=15)return this.unpack_string(r);if((r=e^144)<=15)return this.unpack_array(r);if((r=e^128)<=15)return this.unpack_map(r);switch(e){case 192:return null;case 193:return undefined;case 194:return!1;case 195:return!0;case 202:return this.unpack_float();case 203:return this.unpack_double();case 204:return this.unpack_uint8();case 205:return this.unpack_uint16();case 206:return this.unpack_uint32();case 207:return this.unpack_uint64();case 208:return this.unpack_int8();case 209:return this.unpack_int16();case 210:return this.unpack_int32();case 211:return this.unpack_int64();case 212:return undefined;case 213:return undefined;case 214:return undefined;case 215:return undefined;case 216:return r=this.unpack_uint16(),this.unpack_string(r);case 217:return r=this.unpack_uint32(),this.unpack_string(r);case 218:return r=this.unpack_uint16(),this.unpack_raw(r);case 219:return r=this.unpack_uint32(),this.unpack_raw(r);case 220:return r=this.unpack_uint16(),this.unpack_array(r);case 221:return r=this.unpack_uint32(),this.unpack_array(r);case 222:return r=this.unpack_uint16(),this.unpack_map(r);case 223:return r=this.unpack_uint32(),this.unpack_map(r)}},r.prototype.unpack_uint8=function(){var e=this.dataView[this.index]&255;return this.index++,e},r.prototype.unpack_uint16=function(){var e=this.read(2),t=(e[0]&255)*256+(e[1]&255);return this.index+=2,t},r.prototype.unpack_uint32=function(){var e=this.read(4),t=((e[0]*256+e[1])*256+e[2])*256+e[3];return this.index+=4,t},r.prototype.unpack_uint64=function(){var e=this.read(8),t=((((((e[0]*256+e[1])*256+e[2])*256+e[3])*256+e[4])*256+e[5])*256+e[6])*256+e[7];return this.index+=8,t},r.prototype.unpack_int8=function(){var e=this.unpack_uint8();return e<128?e:e-256},r.prototype.unpack_int16=function(){var e=this.unpack_uint16();return e<32768?e:e-65536},r.prototype.unpack_int32=function(){var e=this.unpack_uint32();return e<Math.pow(2,31)?e:e-Math.pow(2,32)},r.prototype.unpack_int64=function(){var e=this.unpack_uint64();return e<Math.pow(2,63)?e:e-Math.pow(2,64)},r.prototype.unpack_raw=function(e){if(this.length<this.index+e)throw new Error("BinaryPackFailure: index is out of range "+this.index+" "+e+" "+this.length);var t=this.dataBuffer.slice(this.index,this.index+e);return this.index+=e,t},r.prototype.unpack_string=function(e){var t=this.read(e),n=0,r="",i,s;while(n<e)i=t[n],i<128?(r+=String.fromCharCode(i),n++):(i^192)<32?(s=(i^192)<<6|t[n+1]&63,r+=String.fromCharCode(s),n+=2):(s=(i&15)<<12|(t[n+1]&63)<<6|t[n+2]&63,r+=String.fromCharCode(s),n+=3);return this.index+=e,r},r.prototype.unpack_array=function(e){var t=new Array(e);for(var n=0;n<e;n++)t[n]=this.unpack();return t},r.prototype.unpack_map=function(e){var t={};for(var n=0;n<e;n++){var r=this.unpack(),i=this.unpack();t[r]=i}return t},r.prototype.unpack_float=function(){var e=this.unpack_uint32(),t=e>>31,n=(e>>23&255)-127,r=e&8388607|8388608;return(t==0?1:-1)*r*Math.pow(2,n-23)},r.prototype.unpack_double=function(){var e=this.unpack_uint32(),t=this.unpack_uint32(),n=e>>31,r=(e>>20&2047)-1023,i=e&1048575|1048576,s=i*Math.pow(2,r-20)+t*Math.pow(2,r-52);return(n==0?1:-1)*s},r.prototype.read=function(e){var t=this.index;if(t+e<=this.length)return this.dataView.subarray(t,t+e);throw new Error("BinaryPackFailure: read index out of range")},i.prototype.getBuffer=function(){return this.bufferBuilder.getBuffer()},i.prototype.pack=function(e){var n=typeof e;if(n=="string")this.pack_string(e);else if(n=="number")Math.floor(e)===e?this.pack_integer(e):this.pack_double(e);else if(n=="boolean")e===!0?this.bufferBuilder.append(195):e===!1&&this.bufferBuilder.append(194);else if(n=="undefined")this.bufferBuilder.append(192);else{if(n!="object")throw new Error('Type "'+n+'" not yet supported');if(e===null)this.bufferBuilder.append(192);else{var r=e.constructor;if(r==Array)this.pack_array(e);else if(r==Blob||r==File)this.pack_bin(e);else if(r==ArrayBuffer)t.useArrayBufferView?this.pack_bin(new Uint8Array(e)):this.pack_bin(e);else if("BYTES_PER_ELEMENT"in e)t.useArrayBufferView?this.pack_bin(e):this.pack_bin(e.buffer);else if(r==Object)this.pack_object(e);else if(r==Date)this.pack_string(e.toString());else{if(typeof e.toBinaryPack!="function")throw new Error('Type "'+r.toString()+'" not yet supported');this.bufferBuilder.append(e.toBinaryPack())}}}this.bufferBuilder.flush()},i.prototype.pack_bin=function(e){var t=e.length||e.byteLength||e.size;if(t<=15)this.pack_uint8(160+t);else if(t<=65535)this.bufferBuilder.append(218),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_string=function(e){var t;if(this.utf8){var n=new Blob([e]);t=n.size}else t=e.length;if(t<=15)this.pack_uint8(176+t);else if(t<=65535)this.bufferBuilder.append(216),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_array=function(e){var t=e.length;if(t<=15)this.pack_uint8(144+t);else if(t<=65535)this.bufferBuilder.append(220),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(t)}for(var n=0;n<t;n++)this.pack(e[n])},i.prototype.pack_integer=function(e){if(-32<=e&&e<=127)this.bufferBuilder.append(e&255);else if(0<=e&&e<=255)this.bufferBuilder.append(204),this.pack_uint8(e);else if(-128<=e&&e<=127)this.bufferBuilder.append(208),this.pack_int8(e);else if(0<=e&&e<=65535)this.bufferBuilder.append(205),this.pack_uint16(e);else if(-32768<=e&&e<=32767)this.bufferBuilder.append(209),this.pack_int16(e);else if(0<=e&&e<=4294967295)this.bufferBuilder.append(206),this.pack_uint32(e);else if(-2147483648<=e&&e<=2147483647)this.bufferBuilder.append(210),this.pack_int32(e);else if(-0x8000000000000000<=e&&e<=0x8000000000000000)this.bufferBuilder.append(211),this.pack_int64(e);else{if(!(0<=e&&e<=0x10000000000000000))throw new Error("Invalid integer");this.bufferBuilder.append(207),this.pack_uint64(e)}},i.prototype.pack_double=function(e){var t=0;e<0&&(t=1,e=-e);var n=Math.floor(Math.log(e)/Math.LN2),r=e/Math.pow(2,n)-1,i=Math.floor(r*Math.pow(2,52)),s=Math.pow(2,32),o=t<<31|n+1023<<20|i/s&1048575,u=i%s;this.bufferBuilder.append(203),this.pack_int32(o),this.pack_int32(u)},i.prototype.pack_object=function(e){var t=Object.keys(e),n=t.length;if(n<=15)this.pack_uint8(128+n);else if(n<=65535)this.bufferBuilder.append(222),this.pack_uint16(n);else{if(!(n<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(223),this.pack_uint32(n)}for(var r in e)e.hasOwnProperty(r)&&(this.pack(r),this.pack(e[r]))},i.prototype.pack_uint8=function(e){this.bufferBuilder.append(e)},i.prototype.pack_uint16=function(e){this.bufferBuilder.append(e>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_uint32=function(e){var t=e&4294967295;this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255)},i.prototype.pack_uint64=function(e){var t=e/Math.pow(2,32),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)},i.prototype.pack_int8=function(e){this.bufferBuilder.append(e&255)},i.prototype.pack_int16=function(e){this.bufferBuilder.append((e&65280)>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int32=function(e){this.bufferBuilder.append(e>>>24&255),this.bufferBuilder.append((e&16711680)>>>16),this.bufferBuilder.append((e&65280)>>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int64=function(e){var t=Math.floor(e/Math.pow(2,32)),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)};var o=Array.isArray;s.prototype.addListener=function(e,t,n,r){if("function"!=typeof t)throw new Error("addListener only takes instances of Function");return this.emit("newListener",e,typeof t.listener=="function"?t.listener:t),this._events[e]?o(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t,this},s.prototype.on=s.prototype.addListener,s.prototype.once=function(e,t,n){function i(){r.removeListener(e,i),t.apply(this,arguments)}if("function"!=typeof t)throw new Error(".once only takes instances of Function");var r=this;return i.listener=t,r.on(e,i),this},s.prototype.removeListener=function(e,t,n){if("function"!=typeof t)throw new Error("removeListener only takes instances of Function");if(!this._events[e])return this;var r=this._events[e];if(o(r)){var i=-1;for(var s=0,u=r.length;s<u;s++)if(r[s]===t||r[s].listener&&r[s].listener===t){i=s;break}if(i<0)return this;r.splice(i,1),r.length==0&&delete this._events[e]}else(r===t||r.listener&&r.listener===t)&&delete this._events[e];return this},s.prototype.off=s.prototype.removeListener,s.prototype.removeAllListeners=function(e){return arguments.length===0?(this._events={},this):(e&&this._events&&this._events[e]&&(this._events[e]=null),this)},s.prototype.listeners=function(e){return this._events[e]||(this._events[e]=[]),o(this._events[e])||(this._events[e]=[this._events[e]]),this._events[e]},s.prototype.emit=function(e){var e=arguments[0],t=this._events[e];if(!t)return!1;if(typeof t=="function"){switch(arguments.length){case 1:t.call(this);break;case 2:t.call(this,arguments[1]);break;case 3:t.call(this,arguments[1],arguments[2]);break;default:var n=arguments.length,r=new Array(n-1);for(var i=1;i<n;i++)r[i-1]=arguments[i];t.apply(this,r)}return!0}if(o(t)){var n=arguments.length,r=new Array(n-1);for(var i=1;i<n;i++)r[i-1]=arguments[i];var s=t.slice();for(var i=0,n=s.length;i<n;i++)s[i].apply(this,r);return!0}return!1},u.prototype.send=function(e){var t=l.pack(e);if(t.size<this._mtu){this._handleSend(["no",t]);return}this._outgoing[this._count]={ack:0,chunks:this._chunk(t)},l.debug&&(this._outgoing[this._count].timer=new Date),this._sendWindowedChunks(this._count),this._count+=1},u.prototype._setupInterval=function(){var e=this;this._timeout=setInterval(function(){var t=e._queue.shift();if(t._multiple)for(var n=0,r=t.length;n<r;n+=1)e._intervalSend(t[n]);else e._intervalSend(t)},this._interval)},u.prototype._intervalSend=function(e){var t=this;e=l.pack(e),l.blobToBinaryString(e,function(e){t._dc.send(e)}),t._queue.length===0&&(clearTimeout(t._timeout),t._timeout=null)},u.prototype._processAcks=function(){for(var e in this._outgoing)this._outgoing.hasOwnProperty(e)&&this._sendWindowedChunks(e)},u.prototype._handleSend=function(e){var t=!0;for(var n=0,r=this._queue.length;n<r;n+=1){var i=this._queue[n];i===e?t=!1:i._multiple&&i.indexOf(e)!==-1&&(t=!1)}t&&(this._queue.push(e),this._timeout||this._setupInterval())},u.prototype._setupDC=function(){var e=this;this._dc.onmessage=function(t){var n=t.data,r=n.constructor;if(r===String){var i=l.binaryStringToArrayBuffer(n);n=l.unpack(i),e._handleMessage(n)}}},u.prototype._handleMessage=function(e){var t=e[1],n=this._incoming[t],r=this._outgoing[t],i;switch(e[0]){case"no":var s=t;!s||this.onmessage(l.unpack(s));break;case"end":i=n,this._received[t]=e[2];if(!i)break;this._ack(t);break;case"ack":i=r;if(!!i){var o=e[2];i.ack=Math.max(o,i.ack),i.ack>=i.chunks.length?(l.log("Time: ",new Date-i.timer),delete this._outgoing[t]):this._processAcks()}break;case"chunk":i=n;if(!i){var u=this._received[t];if(u===!0)break;i={ack:["ack",t,0],chunks:[]},this._incoming[t]=i}var a=e[2],f=e[3];i.chunks[a]=new Uint8Array(f),a===i.ack[2]&&this._calculateNextAck(t),this._ack(t);break;default:this._handleSend(e)}},u.prototype._chunk=function(e){var t=[],n=e.size,r=0;while(r<n){var i=Math.min(n,r+this._mtu),s=e.slice(r,i),o={payload:s};t.push(o),r=i}return l.log("Created",t.length,"chunks."),t},u.prototype._ack=function(e){var t=this._incoming[e].ack;this._received[e]===t[2]&&(this._complete(e),this._received[e]=!0),this._handleSend(t)},u.prototype._calculateNextAck=function(e){var t=this._incoming[e],n=t.chunks;for(var r=0,i=n.length;r<i;r+=1)if(n[r]===undefined){t.ack[2]=r;return}t.ack[2]=n.length},u.prototype._sendWindowedChunks=function(e){l.log("sendWindowedChunks for: ",e);var t=this._outgoing[e],n=t.chunks,r=[],i=Math.min(t.ack+this._window,n.length);for(var s=t.ack;s<i;s+=1)if(!n[s].sent||s===t.ack)n[s].sent=!0,r.push(["chunk",e,s,n[s].payload]);t.ack+this._window>=n.length&&r.push(["end",e,n.length]),r._multiple=!0,this._handleSend(r)},u.prototype._complete=function(e){l.log("Completed called for",e);var t=this,n=this._incoming[e].chunks,r=new Blob(n);l.blobToArrayBuffer(r,function(e){t.onmessage(l.unpack(e))}),delete this._incoming[e]},u.higherBandwidthSDP=function(e){var t=navigator.appVersion.match(/Chrome\/(.*?) /);if(t){t=parseInt(t[1].split(".").shift());if(t<31){var n=e.split("b=AS:30"),r="b=AS:102400";if(n.length>1)return n[0]+r+n[1]}}return e},u.prototype.onmessage=function(e){},e.Reliable=u,e.RTCSessionDescription=window.mozRTCSessionDescription||window.RTCSessionDescription,e.RTCPeerConnection=window.mozRTCPeerConnection||window.webkitRTCPeerConnection||window.RTCPeerConnection,e.RTCIceCandidate=window.mozRTCIceCandidate||window.RTCIceCandidate;var a={iceServers:[{url:"stun:stun.l.google.com:19302"}]},f=1,l={noop:function(){},CLOUD_HOST:"0.peerjs.com",CLOUD_PORT:9e3,chunkedMTU:6e4,logLevel:0,setLogLevel:function(e){var t=parseInt(e,10);isNaN(parseInt(e,10))?l.logLevel=e?3:0:l.logLevel=t,l.log=l.warn=l.error=l.noop,l.logLevel>0&&(l.error=l._printWith("ERROR")),l.logLevel>1&&(l.warn=l._printWith("WARNING")),l.logLevel>2&&(l.log=l._print)},setLogFunction:function(e){e.constructor!==Function?l.warn("The log function you passed in is not a function. Defaulting to regular logs."):l._print=e},_printWith:function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e),l._print.apply(l,t)}},_print:function(){var e=!1,t=Array.prototype.slice.call(arguments);t.unshift("PeerJS: ");for(var n=0,r=t.length;n<r;n++)t[n]instanceof Error&&(t[n]="("+t[n].name+") "+t[n].message,e=!0);e?console.error.apply(console,t):console.log.apply(console,t)},defaultConfig:a,browser:function(){return window.mozRTCPeerConnection?"Firefox":window.webkitRTCPeerConnection?"Chrome":window.RTCPeerConnection?"Supported":"Unsupported"}(),supports:function(){if(typeof RTCPeerConnection=="undefined")return{};var e=!0,t=!0,n=!1,r=!1,i=!!window.webkitRTCPeerConnection,s,o;try{s=new RTCPeerConnection(a,{optional:[{RtpDataChannels:!0}]})}catch(u){e=!1,t=!1}if(e)try{o=s.createDataChannel("_PEERJSTEST")}catch(u){e=!1}if(e){try{o.binaryType="blob",n=!0}catch(u){}var f=new RTCPeerConnection(a,{});try{var c=f.createDataChannel("_PEERJSRELIABLETEST",{});r=c.reliable}catch(u){}f.close()}t&&(t=!!s.addStream);if(!i&&e){var h=new RTCPeerConnection(a,{optional:[{RtpDataChannels:!0}]});h.onnegotiationneeded=function(){i=!0,l&&l.supports&&(l.supports.onnegotiationneeded=!0)};var p=h.createDataChannel("_PEERJSNEGOTIATIONTEST");setTimeout(function(){h.close()},1e3)}return s&&s.close(),{audioVideo:t,data:e,binaryBlob:n,binary:r,reliable:r,sctp:r,onnegotiationneeded:i}}(),validateId:function(e){return!e||/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(e)},validateKey:function(e){return!e||/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(e)},debug:!1,inherits:function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})},extend:function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e},pack:BinaryPack.pack,unpack:BinaryPack.unpack,log:function(){if(l.debug){var e=!1,t=Array.prototype.slice.call(arguments);t.unshift("PeerJS: ");for(var n=0,r=t.length;n<r;n++)t[n]instanceof Error&&(t[n]="("+t[n].name+") "+t[n].message,e=!0);e?console.error.apply(console,t):console.log.apply(console,t)}},setZeroTimeout:function(e){function r(r){t.push(r),e.postMessage(n,"*")}function i(r){r.source==e&&r.data==n&&(r.stopPropagation&&r.stopPropagation(),t.length&&t.shift()())}var t=[],n="zero-timeout-message";return e.addEventListener?e.addEventListener("message",i,!0):e.attachEvent&&e.attachEvent("onmessage",i),r}(this),chunk:function(e){var t=[],n=e.size,r=index=0,i=Math.ceil(n/l.chunkedMTU);while(r<n){var s=Math.min(n,r+l.chunkedMTU),o=e.slice(r,s),u={__peerData:f,n:index,data:o,total:i};t.push(u),r=s,index+=1}return f+=1,t},blobToArrayBuffer:function(e,t){var n=new FileReader;n.onload=function(e){t(e.target.result)},n.readAsArrayBuffer(e)},blobToBinaryString:function(e,t){var n=new FileReader;n.onload=function(e){t(e.target.result)},n.readAsBinaryString(e)},binaryStringToArrayBuffer:function(e){var t=new Uint8Array(e.length);for(var n=0;n<e.length;n++)t[n]=e.charCodeAt(n)&255;return t.buffer},randomToken:function(){return Math.random().toString(36).substr(2)},isSecure:function(){return location.protocol==="https:"}};e.util=l,l.inherits(c,s),c.prototype._retrieveId=function(e){var t=this,n=new XMLHttpRequest,r=this.options.secure?"https://":"http://",i=r+this.options.host+":"+this.options.port+"/"+this.options.key+"/id",s="?ts="+(new Date).getTime()+""+Math.random();i+=s,n.open("get",i,!0),n.onerror=function(e){l.error("Error retrieving ID",e),t._abort("server-error","Could not get an ID from the server")},n.onreadystatechange=function(){if(n.readyState!==4)return;if(n.status!==200){n.onerror();return}t._initialize(n.responseText)},n.send(null)},c.prototype._initialize=function(e){var t=this;this.id=e,this.socket.start(this.id)},c.prototype._handleMessage=function(e){var t=e.type,n=e.payload,r=e.src;switch(t){case"OPEN":this.emit("open",this.id),this.open=!0;break;case"ERROR":this._abort("server-error",n.msg);break;case"ID-TAKEN":this._abort("unavailable-id","ID `"+this.id+"` is taken");break;case"INVALID-KEY":this._abort("invalid-key",'API KEY "'+this._key+'" is invalid');break;case"LEAVE":l.log("Received leave message from",r),this._cleanupPeer(r);break;case"EXPIRE":this.emit("error",new Error("Could not connect to peer "+r));break;case"OFFER":var i=n.connectionId,s=this.getConnection(r,i);if(s)l.warn("Offer received for existing Connection ID:",i);else{if(n.type==="media"){var s=new p(r,this,{connectionId:i,_payload:n,metadata:n.metadata});this._addConnection(r,s),this.emit("call",s)}else{if(n.type!=="data"){l.warn("Received malformed connection type:",n.type);return}s=new h(r,this,{connectionId:i,_payload:n,metadata:n.metadata,label:n.label,serialization:n.serialization,reliable:n.reliable}),this._addConnection(r,s),this.emit("connection",s)}var o=this._getMessages(i);for(var u=0,a=o.length;u<a;u+=1)s.handleMessage(o[u])}break;default:if(!n){l.warn("You received a malformed message from "+r+" of type "+t);return}var f=n.connectionId,s=this.getConnection(r,f);s&&s.pc?s.handleMessage(e):f?this._storeMessage(f,e):l.warn("You received an unrecognized message:",e)}},c.prototype._storeMessage=function(e,t){this._lostMessages[e]||(this._lostMessages[e]=[]),this._lostMessages[e].push(t)},c.prototype._getMessages=function(e){var t=this._lostMessages[e];return t?(delete this._lostMessages[e],t):[]},c.prototype.connect=function(e,t){if(this.disconnected){l.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect."),this.emit("error",new Error("Cannot connect to new Peer after disconnecting from server."));return}var n=new h(e,this,t);return this._addConnection(e,n),n},c.prototype.call=function(e,t,n){if(this.disconnected){l.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect."),this.emit("error",new Error("Cannot connect to new Peer after disconnecting from server."));return}if(!t){l.error("To call a peer, you must provide a stream from your browser's `getUserMedia`.");return}n=n||{},n._stream=t;var r=new p(e,this,n);return this._addConnection(e,r),r},c.prototype._addConnection=function(e,t){this.connections[e]||(this.connections[e]=[]),this.connections[e].push(t)},c.prototype.getConnection=function(e,t){var n=this.connections[e];if(!n)return null;for(var r=0,i=n.length;r<i;r++)if(n[r].id===t)return n[r];return null},c.prototype._delayedAbort=function(e,t){var n=this;l.setZeroTimeout(function(){n._abort(e,t)})},c.prototype._abort=function(e,t){l.error("Aborting. Error:",t);var n=new Error(t);n.type=e,this.destroy(),this.emit("error",n)},c.prototype.destroy=function(){this.destroyed||(this._cleanup(),this.disconnect(),this.destroyed=!0)},c.prototype._cleanup=function(){if(this.connections){var e=Object.keys(this.connections);for(var t=0,n=e.length;t<n;t++)this._cleanupPeer(e[t])}this.emit("close")},c.prototype._cleanupPeer=function(e){var t=this.connections[e];for(var n=0,r=t.length;n<r;n+=1)t[n].close()},c.prototype.disconnect=function(){var e=this;l.setZeroTimeout(function(){e.disconnected||(e.disconnected=!0,e.open=!1,e.socket&&e.socket.close(),e.id=null)})},e.Peer=c,l.inherits(h,s),h._idPrefix="dc_",h.prototype.initialize=function(e){this._dc=e,this._configureDataChannel()},h.prototype._configureDataChannel=function(){var e=this;l.supports.sctp&&(this._dc.binaryType="arraybuffer"),this._dc.onopen=function(){l.log("Data channel connection success"),e.open=!0,e.emit("open")},!l.supports.sctp&&this.reliable&&(this._reliable=new u(this._dc,l.debug)),this._reliable?this._reliable.onmessage=function(t){e.emit("data",t)}:this._dc.onmessage=function(t){e._handleDataMessage(t)},this._dc.onclose=function(t){l.log("DataChannel closed for:",e.peer),e.close()}},h.prototype._handleDataMessage=function(e){var t=this,n=e.data,r=n.constructor;if(this.serialization==="binary"||this.serialization==="binary-utf8"){if(r===Blob){l.blobToArrayBuffer(n,function(e){n=l.unpack(e),t.emit("data",n)});return}if(r===ArrayBuffer)n=l.unpack(n);else if(r===String){var i=l.binaryStringToArrayBuffer(n);n=l.unpack(i)}}else this.serialization==="json"&&(n=JSON.parse(n));if(n.__peerData){var s=n.__peerData,o=this._chunkedData[s]||{data:[],count:0,total:n.total};o.data[n.n]=n.data,o.count+=1,o.total===o.count&&(n=new Blob(o.data),this._handleDataMessage({data:n}),delete this._chunkedData[s]),this._chunkedData[s]=o;return}this.emit("data",n)},h.prototype.close=function(){if(!this.open)return;this.open=!1,d.cleanup(this),this.emit("close")},h.prototype.send=function(e,t){if(!this.open){this.emit("error",new Error("Connection is not open. You should listen for the `open` event before sending messages."));return}if(this._reliable){this._reliable.send(e);return}var n=this;if(this.serialization==="json")this._dc.send(JSON.stringify(e));else if("binary-utf8".indexOf(this.serialization)!==-1){var r=this.serialization==="binary-utf8",i=l.pack(e,r);if(l.browser!=="Firefox"&&!t&&i.size>l.chunkedMTU){this._sendChunks(i);return}l.supports.sctp?l.supports.binaryBlob?this._dc.send(i):l.blobToArrayBuffer(i,function(e){n._dc.send(e)}):l.blobToBinaryString(i,function(e){n._dc.send(e)})}else this._dc.send(e)},h.prototype._sendChunks=function(e){var t=l.chunk(e);for(var n=0,r=t.length;n<r;n+=1){var e=t[n];this.send(e,!0)}},h.prototype.handleMessage=function(e){var t=e.payload;switch(e.type){case"ANSWER":d.handleSDP(e.type,this,t.sdp);break;case"CANDIDATE":d.handleCandidate(this,t.candidate);break;default:l.warn("Unrecognized message type:",e.type,"from peer:",this.peer)}},l.inherits(p,s),p._idPrefix="mc_",p.prototype.addStream=function(e){l.log("Receiving stream",e),this.remoteStream=e,this.emit("stream",e)},p.prototype.handleMessage=function(e){var t=e.payload;switch(e.type){case"ANSWER":d.handleSDP(e.type,this,t.sdp),this.open=!0;break;case"CANDIDATE":d.handleCandidate(this,t.candidate);break;default:l.warn("Unrecognized message type:",e.type,"from peer:",this.peer)}},p.prototype.answer=function(e){if(this.localStream){l.warn("Local stream already exists on this MediaConnection. Are you answering a call twice?");return}this.options._payload._stream=e,this.localStream=e,d.startConnection(this,this.options._payload);var t=this.provider._getMessages(this.id);for(var n=0,r=t.length;n<r;n+=1)this.handleMessage(t[n]);this.open=!0},p.prototype.close=function(){if(!this.open)return;this.open=!1,d.cleanup(this),this.emit("close")};var d={pcs:{data:{},media:{}},queue:[]};d._idPrefix="pc_",d.startConnection=function(e,t){var n=d._getPeerConnection(e,t);e.type==="media"&&t._stream&&n.addStream(t._stream),e.pc=n;if(t.originator){if(e.type==="data"){var r={};l.supports.sctp||(r={reliable:t.reliable});var i=n.createDataChannel(e.label,r);e.initialize(i)}l.supports.onnegotiationneeded||d._makeOffer(e)}else d.handleSDP("OFFER",e,t.sdp)},d._getPeerConnection=function(e,t){d.pcs[e.type]||l.error(e.type+" is not a valid connection type. Maybe you overrode the `type` property somewhere."),d.pcs[e.type][e.peer]||(d.pcs[e.type][e.peer]={});var n=d.pcs[e.type][e.peer],r;t.pc&&(r=d.pcs[e.type][e.peer][t.pc]);if(!r||r.signalingState!=="stable")r=d._startPeerConnection(e);return r},d._startPeerConnection=function(e){l.log("Creating RTCPeerConnection.");var t=d._idPrefix+l.randomToken(),n={};e.type==="data"&&!l.supports.sctp?n={optional:[{RtpDataChannels:!0}]}:e.type==="media"&&(n={optional:[{DtlsSrtpKeyAgreement:!0}]});var r=new RTCPeerConnection(e.provider.options.config,n);return d.pcs[e.type][e.peer][t]=r,d._setupListeners(e,r,t),r},d._setupListeners=function(e,t,n){var r=e.peer,i=e.id,s=e.provider;l.log("Listening for ICE candidates."),t.onicecandidate=function(t){t.candidate&&(l.log("Received ICE candidates for:",e.peer),s.socket.send({type:"CANDIDATE",payload:{candidate:t.candidate,type:e.type,connectionId:e.id},dst:r}))},t.oniceconnectionstatechange=function(){switch(t.iceConnectionState){case"disconnected":case"failed":l.log("iceConnectionState is disconnected, closing connections to "+r),e.close();break;case"completed":t.onicecandidate=l.noop}},t.onicechange=t.oniceconnectionstatechange,l.log("Listening for `negotiationneeded`"),t.onnegotiationneeded=function(){l.log("`negotiationneeded` triggered"),t.signalingState=="stable"?d._makeOffer(e):l.log("onnegotiationneeded triggered when not stable. Is another connection being established?")},l.log("Listening for data channel"),t.ondatachannel=function(e){l.log("Received data channel");var t=e.channel,n=s.getConnection(r,i);n.initialize(t)},l.log("Listening for remote stream"),t.onaddstream=function(e){l.log("Received remote stream");var t=e.stream;s.getConnection(r,i).addStream(t)}},d.cleanup=function(e){l.log("Cleaning up PeerConnection to "+e.peer);var t=e.pc;!!t&&(t.readyState!=="closed"||t.signalingState!=="closed")&&(t.close(),e.pc=null)},d._makeOffer=function(e){var t=e.pc;t.createOffer(function(n){l.log("Created offer."),!l.supports.sctp&&e.type==="data"&&e.reliable&&(n.sdp=u.higherBandwidthSDP(n.sdp)),t.setLocalDescription(n,function(){l.log("Set localDescription: offer","for:",e.peer),e.provider.socket.send({type:"OFFER",payload:{sdp:n,type:e.type,label:e.label,reliable:e.reliable,serialization:e.serialization,metadata:e.metadata,connectionId:e.id,sctp:l.supports.sctp},dst:e.peer})},function(t){e.provider.emit("error",t),l.log("Failed to setLocalDescription, ",t)})},function(t){e.provider.emit("error",t),l.log("Failed to createOffer, ",t)},e.options.constraints)},d._makeAnswer=function(e){var t=e.pc;t.createAnswer(function(n){l.log("Created answer."),!l.supports.sctp&&e.type==="data"&&e.reliable&&(n.sdp=u.higherBandwidthSDP(n.sdp)),t.setLocalDescription(n,function(){l.log("Set localDescription: answer","for:",e.peer),e.provider.socket.send({type:"ANSWER",payload:{sdp:n,type:e.type,connectionId:e.id},dst:e.peer})},function(t){e.provider.emit("error",t),l.log("Failed to setLocalDescription, ",t)})},function(t){e.provider.emit("error",t),l.log("Failed to create answer, ",t)})},d.handleSDP=function(e,t,n){n=new RTCSessionDescription(n);var r=t.pc;l.log("Setting remote description",n),r.setRemoteDescription(n,function(){l.log("Set remoteDescription:",e,"for:",t.peer),e==="OFFER"&&d._makeAnswer(t)},function(e){t.provider.emit("error",e),l.log("Failed to setRemoteDescription, ",e)})},d.handleCandidate=function(e,t){var n=t.candidate,r=t.sdpMLineIndex;e.pc.addIceCandidate(new RTCIceCandidate({sdpMLineIndex:r,candidate:n})),l.log("Added ICE candidate for:",e.peer)},l.inherits(v,s),v.prototype.start=function(e){this.id=e;var t=l.randomToken();this._httpUrl+="/"+e+"/"+t,this._wsUrl+="&id="+e+"&token="+t,this._startXhrStream(),this._startWebSocket()},v.prototype._startWebSocket=function(e){var t=this;if(this._socket)return;this._socket=new WebSocket(this._wsUrl),this._socket.onmessage=function(e){var n;try{n=JSON.parse(e.data)}catch(r){l.log("Invalid server message",e.data);return}t.emit("message",n)},this._socket.onopen=function(){t._timeout&&(clearTimeout(t._timeout),setTimeout(function(){t._http.abort(),t._http=null},5e3)),t._sendQueuedMessages(),l.log("Socket open")}},v.prototype._startXhrStream=function(e){try{var t=this;this._http=new XMLHttpRequest,this._http._index=1,this._http._streamIndex=e||0,this._http.open("post",this._httpUrl+"/id?i="+this._http._streamIndex,!0),this._http.onreadystatechange=function(){this.readyState==2&&this.old&&(this.old.abort(),delete this.old),this.readyState>2&&this.status==200&&this.responseText&&t._handleStream(this)},this._http.send(null),this._setHTTPTimeout()}catch(n){l.log("XMLHttpRequest not available; defaulting to WebSockets")}},v.prototype._handleStream=function(e){var t=e.responseText.split("\n");if(e._buffer)while(e._buffer.length>0){var n=e._buffer.shift(),r=t[n];try{r=JSON.parse(r)}catch(i){e._buffer.shift(n);break}this.emit("message",r)}var s=t[e._index];if(s){e._index+=1;if(e._index===t.length)e._buffer||(e._buffer=[]),e._buffer.push(e._index-1);else{try{s=JSON.parse(s)}catch(i){l.log("Invalid server message",s);return}this.emit("message",s)}}},v.prototype._setHTTPTimeout=function(){var e=this;this._timeout=setTimeout(function(){var t=e._http;e._wsOpen()?t.abort():(e._startXhrStream(t._streamIndex+1),e._http.old=t)},25e3)},v.prototype._wsOpen=function(){return this._socket&&this._socket.readyState==1},v.prototype._sendQueuedMessages=function(){for(var e=0,t=this._queue.length;e<t;e+=1)this.send(this._queue[e])},v.prototype.send=function(e){if(this.disconnected)return;if(!this.id){this._queue.push(e);return}if(!e.type){this.emit("error","Invalid message");return}var t=JSON.stringify(e);if(this._wsOpen())this._socket.send(t);else{var n=new XMLHttpRequest,r=this._httpUrl+"/"+e.type.toLowerCase();n.open("post",r,!0),n.setRequestHeader("Content-Type","application/json"),n.send(t)}},v.prototype.close=function(){!this.disconnected&&this._wsOpen()&&(this._socket.close(),this.disconnected=!0)}})(this) |
@@ -119,6 +119,6 @@ /** | ||
delete this._chunkedData[id]; | ||
return; | ||
} | ||
this._chunkedData[id] = chunkInfo; | ||
return; | ||
} | ||
@@ -125,0 +125,0 @@ |
@@ -9,3 +9,3 @@ var defaultConfig = {'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]}; | ||
CLOUD_PORT: 9000, | ||
chunkedMTU: 120000, // 120KB | ||
chunkedMTU: 60000, // 60KB | ||
@@ -12,0 +12,0 @@ // Logging logic |
{ | ||
"name": "peerjs", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "PeerJS client", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
4
1
426632
56
7700