-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnaf-firebase-adapter.min.js
2 lines (2 loc) · 12 KB
/
naf-firebase-adapter.min.js
1
2
!function(e){var t={};function n(a){if(t[a])return t[a].exports;var o=t[a]={i:a,l:!1,exports:{}};return e[a].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,a){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:a})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var a=Object.create(null);if(n.r(a),Object.defineProperty(a,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(a,o,function(t){return e[t]}.bind(null,o));return a},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){function a(e,t){for(var n=0;n<t.length;n++){var a=t[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}var o=n(1),i=n(2),r=function(){function e(t,n){if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.rootPath="networked-aframe",this.localId=null,this.appId=null,this.roomId=null,this.peers={},this.occupants={},this.serverTimeRequests=0,this.timeOffsets=[],this.avgTimeOffset=0,n=n||window.firebaseConfig,this.firebase=t||window.firebase,void 0===this.firebase)throw new Error("Import https://www.gstatic.com/firebasejs/x.x.x/firebase.js");this.authType=n.authType,this.apiKey=n.apiKey,this.authDomain=n.authDomain,this.databaseURL=n.databaseURL}var t,n,r;return t=e,(n=[{key:"setServerUrl",value:function(e){}},{key:"setApp",value:function(e){this.appId=e}},{key:"setRoom",value:function(e){this.roomId=e}},{key:"setWebRtcOptions",value:function(e){!1===e.datachannel&&NAF.log.warn("FirebaseWebRtcAdapter.setWebRtcOptions: datachannel must be true."),!0===e.audio&&NAF.log.warn("FirebaseWebRtcAdapter does not support audio yet."),!0===e.video&&NAF.log.warn("FirebaseWebRtcAdapter does not support video yet.")}},{key:"setServerConnectListeners",value:function(e,t){this.connectSuccess=e,this.connectFailure=t}},{key:"setRoomOccupantListener",value:function(e){this.occupantListener=e}},{key:"setDataChannelListeners",value:function(e,t,n){this.openListener=e,this.closedListener=t,this.messageListener=function(e,t,a){var i=o.deepDecode(a);n(e,t,i)}}},{key:"connect",value:function(){var e=this;this.initFirebase((function(t){e.updateTimeOffset(),e.localId=t;var n=e.firebaseApp;e.getTimestamp((function(t){e.myRoomJoinTime=t;var a=n.database().ref(e.getUserPath(e.localId));a.set({timestamp:t,signal:"",data:""}),a.onDisconnect().remove();var o=n.database().ref(e.getRoomPath());o.on("child_added",(function(a){var o=a.key;if(o!==e.localId&&"timestamp"!==o&&void 0===e.peers[o]){var r=a.val().timestamp,s=new i(e.localId,o,(function(t){n.database().ref(e.getSignalPath(e.localId)).set(t)}));s.setDatachannelListeners(e.openListener,e.closedListener,e.messageListener),e.peers[o]=s,e.occupants[o]=r,n.database().ref(e.getSignalPath(o)).on("value",(function(e){var t=e.val();null!==t&&""!==t&&s.handleSignal(t)})),n.database().ref(e.getDataPath(o)).on("value",(function(t){var n=t.val();null!==n&&""!==n&&n.to===e.localId&&e.messageListener(o,n.type,n.data)})),(t>r||t===r&&e.localId>o)&&s.offer(),e.occupantListener(e.occupants)}})),o.on("child_removed",(function(t){var n=t.key;n!==e.localId&&"timestamp"!==n&&void 0!==e.peers[n]&&(delete e.peers[n],delete e.occupants[n],e.occupantListener(e.occupants))})),e.connectSuccess(e.localId)}))}))}},{key:"shouldStartConnectionTo",value:function(e){return(this.myRoomJoinTime||0)<=(e?e.roomJoinTime:0)}},{key:"startStreamConnection",value:function(e){}},{key:"closeStreamConnection",value:function(e){}},{key:"sendData",value:function(e,t,n){this.peers[e].send(t,n)}},{key:"sendDataGuaranteed",value:function(e,t,n){var a=JSON.parse(JSON.stringify(n)),i=o.deepEncode(a);this.firebaseApp.database().ref(this.getDataPath(this.localId)).set({to:e,type:t,data:i})}},{key:"broadcastData",value:function(e,t){for(var n in this.peers)this.peers.hasOwnProperty(n)&&this.sendData(n,e,t)}},{key:"broadcastDataGuaranteed",value:function(e,t){for(var n in this.peers)this.peers.hasOwnProperty(n)&&this.sendDataGuaranteed(n,e,t)}},{key:"getConnectStatus",value:function(e){var t=this.peers[e];if(void 0===t)return NAF.adapters.NOT_CONNECTED;switch(t.getStatus()){case i.IS_CONNECTED:return NAF.adapters.IS_CONNECTED;case i.CONNECTING:return NAF.adapters.CONNECTING;case i.NOT_CONNECTED:default:return NAF.adapters.NOT_CONNECTED}}},{key:"getMediaStream",value:function(e){return Promise.reject("Interface method not implemented: getMediaStream")}},{key:"initFirebase",value:function(e){this.firebaseApp=this.firebase.initializeApp({apiKey:this.apiKey,authDomain:this.authDomain,databaseURL:this.databaseURL},this.appId),this.auth(this.authType,e)}},{key:"auth",value:function(e,t){switch(e){case"none":this.authNone(t);break;case"anonymous":this.authAnonymous(t);break;default:NAF.log.log("FirebaseWebRtcInterface.auth: Unknown authType "+e)}}},{key:"authNone",value:function(e){var t=this;requestAnimationFrame((function(){e(t.randomString())}))}},{key:"authAnonymous",value:function(e){var t=this,n=this.firebaseApp;n.auth().signInAnonymously().catch((function(e){NAF.log.error("FirebaseWebRtcInterface.authAnonymous: "+e),t.connectFailure(null,e)})),n.auth().onAuthStateChanged((function(t){null!==t&&e(t.uid)}))}},{key:"getRootPath",value:function(){return this.rootPath}},{key:"getAppPath",value:function(){return this.getRootPath()+"/"+this.appId}},{key:"getRoomPath",value:function(){return this.getAppPath()+"/"+this.roomId}},{key:"getUserPath",value:function(e){return this.getRoomPath()+"/"+e}},{key:"getSignalPath",value:function(e){return this.getUserPath(e)+"/signal"}},{key:"getDataPath",value:function(e){return this.getUserPath(e)+"/data"}},{key:"getTimestampGenerationPath",value:function(e){return this.getRoomPath()+"/timestamp/"+e}},{key:"randomString",value:function(){for(var e="ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz0123456789",t="",n=0;n<16;n++){var a=Math.floor(Math.random()*e.length);t+=e.substring(a,a+1)}return t}},{key:"getTimestamp",value:function(e){var t=this.firebaseApp.database().ref(this.getTimestampGenerationPath(this.localId));t.set(this.firebase.database.ServerValue.TIMESTAMP),t.once("value",(function(n){var a=n.val();t.remove(),e(a)})),t.onDisconnect().remove()}},{key:"updateTimeOffset",value:function(){var e=this;return this.firebaseApp.database().ref("/.info/serverTimeOffset").once("value").then((function(t){var n=t.val();e.serverTimeRequests++,e.serverTimeRequests<=10?e.timeOffsets.push(n):e.timeOffsets[e.serverTimeRequests%10]=n,e.avgTimeOffset=e.timeOffsets.reduce((function(e,t){return e+t}),0)/e.timeOffsets.length,e.serverTimeRequests>10?setTimeout((function(){return e.updateTimeOffset()}),3e5):e.updateTimeOffset()}))}},{key:"getServerTime",value:function(){return new Date+this.avgTimeOffset}}])&&a(t.prototype,n),r&&a(t,r),e}();NAF.adapters.register("firebase",r),e.exports=r},function(e,t){function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}e.exports={encode:function(e){return encodeURIComponent(e).replace(/\./g,"%2E")},decode:function(e){return decodeURIComponent(e.replace("%2E","."))},deepKeyReplace:function(e,t){var a=Object.assign({},e);return function e(t,a,o){if("object"===n(t))for(var i in t)null===t[i]||"object"!=n(t[i])&&!Array.isArray(t[i])||e(t[i],a[i],o),o.apply(this,[a,i,a[i]]);else if(Array.isArray(t))for(i=0;i<t.length;i++)null===t[i]||"object"!=n(t[i])&&!Array.isArray(t[i])||e(t[i],a[i],o)}(e,a,(function(e,n,a){delete e[n],e[t(n)]=a})),a},deepDecode:function(e){var t=this;return this.deepKeyReplace(e,(function(e){return t.decode(e)}))},deepEncode:function(e){var t=this;return this.deepKeyReplace(e,(function(e){return t.encode(e)}))}}},function(e,t){function n(e,t){for(var n=0;n<t.length;n++){var a=t[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}var a=function(){function e(t,n,a){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.localId=t,this.remoteId=n,this.sendSignalFunc=a,this.open=!1,this.channelLabel="networked-aframe-channel",this.pc=this.createPeerConnection(),this.channel=null}var t,a,o;return t=e,(a=[{key:"setDatachannelListeners",value:function(e,t,n){this.openListener=e,this.closedListener=t,this.messageListener=n}},{key:"offer",value:function(){var e=this;this.setupChannel(this.pc.createDataChannel(this.channelLabel,{reliable:!1})),this.pc.createOffer((function(t){e.handleSessionDescription(t)}),(function(e){console.error("WebRtcPeer.offer: "+e)}))}},{key:"handleSignal",value:function(e){if(this.localId===e.to&&this.remoteId===e.from)switch(e.type){case"offer":this.handleOffer(e);break;case"answer":this.handleAnswer(e);break;case"candidate":this.handleCandidate(e);break;default:console.error("WebRtcPeer.handleSignal: Unknown signal type "+e.type)}}},{key:"send",value:function(e,t){null!==this.channel&&"open"===this.channel.readyState&&this.channel.send(JSON.stringify({type:e,data:t}))}},{key:"getStatus",value:function(){if(null===this.channel)return e.NOT_CONNECTED;switch(this.channel.readyState){case"open":return e.IS_CONNECTED;case"connecting":return e.CONNECTING;case"closing":case"closed":default:return e.NOT_CONNECTED}}},{key:"createPeerConnection",value:function(){var t=this,n=window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection||window.msRTCPeerConnection;if(void 0===n)throw new Error("WebRtcPeer.createPeerConnection: This browser does not seem to support WebRTC.");var a=new n({iceServers:e.ICE_SERVERS});return a.onicecandidate=function(e){e.candidate&&t.sendSignalFunc({from:t.localId,to:t.remoteId,type:"candidate",sdpMLineIndex:e.candidate.sdpMLineIndex,candidate:e.candidate.candidate})},a.oniceconnectionstatechange=function(){t.open&&"disconnected"===a.iceConnectionState&&(t.open=!1,t.closedListener(t.remoteId))},a}},{key:"setupChannel",value:function(e){var t=this;this.channel=e,this.channel.onmessage=function(e){var n=JSON.parse(e.data);t.messageListener(t.remoteId,n.type,n.data)},this.channel.onopen=function(e){t.open=!0,t.openListener(t.remoteId)},this.channel.onclose=function(e){t.open&&(t.open=!1,t.closedListener(t.remoteId))},this.channel.onerror=function(e){console.error("WebRtcPeer.channel.onerror: "+e)}}},{key:"handleOffer",value:function(e){var t=this;this.pc.ondatachannel=function(e){t.setupChannel(e.channel)},this.setRemoteDescription(e),this.pc.createAnswer((function(e){t.handleSessionDescription(e)}),(function(e){console.error("WebRtcPeer.handleOffer: "+e)}))}},{key:"handleAnswer",value:function(e){this.setRemoteDescription(e)}},{key:"handleCandidate",value:function(e){var t=window.RTCIceCandidate||window.webkitRTCIceCandidate||window.mozRTCIceCandidate;this.pc.addIceCandidate(new t(e),(function(){}),(function(e){console.error("WebRtcPeer.handleCandidate: "+e)}))}},{key:"handleSessionDescription",value:function(e){this.pc.setLocalDescription(e,(function(){}),(function(e){console.error("WebRtcPeer.handleSessionDescription: "+e)})),this.sendSignalFunc({from:this.localId,to:this.remoteId,type:e.type,sdp:e.sdp})}},{key:"setRemoteDescription",value:function(e){var t=window.RTCSessionDescription||window.webkitRTCSessionDescription||window.mozRTCSessionDescription||window.msRTCSessionDescription;this.pc.setRemoteDescription(new t(e),(function(){}),(function(e){console.error("WebRtcPeer.setRemoteDescription: "+e)}))}}])&&n(t.prototype,a),o&&n(t,o),e}();a.IS_CONNECTED="IS_CONNECTED",a.CONNECTING="CONNECTING",a.NOT_CONNECTED="NOT_CONNECTED",a.ICE_SERVERS=[{urls:"stun:stun1.l.google.com:19302"},{urls:"stun:stun2.l.google.com:19302"},{urls:"stun:stun3.l.google.com:19302"},{urls:"stun:stun4.l.google.com:19302"}],e.exports=a}]);
//# sourceMappingURL=naf-firebase-adapter.min.js.map