blockbench/lib/peer.min.js

1 line
42 KiB
JavaScript
Raw Permalink Normal View History

2019-04-12 18:44:18 +02:00
!function o(s,a,u){function c(t,e){if(!a[t]){if(!s[t]){var i="function"==typeof require&&require;if(!e&&i)return i(t,!0);if(p)return p(t,!0);var n=new Error("Cannot find module '"+t+"'");throw n.code="MODULE_NOT_FOUND",n}var r=a[t]={exports:{}};s[t][0].call(r.exports,function(e){return c(s[t][1][e]||e)},r,r.exports,o,s,a,u)}return a[t].exports}for(var p="function"==typeof require&&require,e=0;e<u.length;e++)c(u[e]);return c}({1:[function(e,t,i){"use strict";Object.defineProperty(i,"__esModule",{value:!0}),i.RTCSessionDescription=window.RTCSessionDescription||window.mozRTCSessionDescription,i.RTCPeerConnection=window.RTCPeerConnection||window.mozRTCPeerConnection||window.webkitRTCPeerConnection,i.RTCIceCandidate=window.RTCIceCandidate||window.mozRTCIceCandidate},{}],2:[function(e,t,i){"use strict";Object.defineProperty(i,"__esModule",{value:!0});var a=e("./util"),n=e("eventemitter3"),r=e("./negotiator"),o=e("reliable");function s(e,t,i){if(!(this instanceof s))return new s(e,t,i);n.EventEmitter.call(this),this.options=a.util.extend({serialization:"binary",reliable:!1},i),this.open=!1,this.type="data",this.peer=e,this.provider=t,this.id=this.options.connectionId||s._idPrefix+a.util.randomToken(),this.label=this.options.label||this.id,this.metadata=this.options.metadata,this.serialization=this.options.serialization,this.reliable=this.options.reliable,this._buffer=[],this._buffering=!1,this.bufferSize=0,this._chunkedData={},this.options._payload&&(this._peerBrowser=this.options._payload.browser),r.Negotiator.startConnection(this,this.options._payload||{originator:!0})}i.DataConnection=s,a.util.inherits(s,n.EventEmitter),s._idPrefix="dc_",s.prototype.initialize=function(e){this._dc=this.dataChannel=e,this._configureDataChannel()},s.prototype._configureDataChannel=function(){var t=this;a.util.supports.sctp&&(this._dc.binaryType="arraybuffer"),this._dc.onopen=function(){a.util.log("Data channel connection success"),t.open=!0,t.emit("open")},!a.util.supports.sctp&&this.reliable&&(this._reliable=new o.Reliable(this._dc,a.util.debug)),this._reliable?this._reliable.onmessage=function(e){t.emit("data",e)}:this._dc.onmessage=function(e){t._handleDataMessage(e)},this._dc.onclose=function(e){a.util.log("DataChannel closed for:",t.peer),t.close()}},s.prototype._handleDataMessage=function(e){var t=this,i=e.data,n=i.constructor;if("binary"===this.serialization||"binary-utf8"===this.serialization){if(n===Blob)return void a.util.blobToArrayBuffer(i,function(e){i=a.util.unpack(e),t.emit("data",i)});if(n===ArrayBuffer)i=a.util.unpack(i);else if(n===String){var r=a.util.binaryStringToArrayBuffer(i);i=a.util.unpack(r)}}else"json"===this.serialization&&(i=JSON.parse(i));if(i.__peerData){var o=i.__peerData,s=this._chunkedData[o]||{data:[],count:0,total:i.total};return s.data[i.n]=i.data,s.count+=1,s.total===s.count&&(delete this._chunkedData[o],i=new Blob(s.data),this._handleDataMessage({data:i})),void(this._chunkedData[o]=s)}this.emit("data",i)},s.prototype.close=function(){this.open&&(this.open=!1,r.Negotiator.cleanup(this),this.emit("close"))},s.prototype.send=function(e,t){if(this.open)if(this._reliable)this._reliable.send(e);else{var i=this;if("json"===this.serialization)this._bufferedSend(JSON.stringify(e));else if("binary"===this.serialization||"binary-utf8"===this.serialization){var n=a.util.pack(e);if((a.util.chunkedBrowsers[this._peerBrowser]||a.util.chunkedBrowsers[a.util.browser])&&!t&&n.size>a.util.chunkedMTU)return void this._sendChunks(n);a.util.supports.sctp?a.util.supports.binaryBlob?this._bufferedSend(n):a.util.blobToArrayBuffer(n,function(e){i._bufferedSend(e)}):a.util.blobToBinaryString(n,function(e){i._bufferedSend(e)})}else this._bufferedSend(e)}else this.emit("error",new Error("Connection is not open. You should listen for the `open` event before sending messages."))},s.prototype._bufferedSend=function(e){!this._buffering&&this._trySend(e)||(this._buffer.push(e),this.bufferSize=this._buffer.length)},s.prototype._trySend=function(e){try{this._dc.send(e)}catch(e){this._buffering=!0;var t=this;return setTimeout(function(){t._