websocket-relay
Version:
Easily set up communication channels between browser clients
1 lines • 4.87 kB
JavaScript
;var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e};!function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return e[r].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){var r=n(1);"undefined"==typeof window?e.exports=r:window.WebSocketRelay=r},function(e,t,n){(function(e){function t(e,t,n){i(e,t);var u={},f={},c=new a("ws://"+e),l=this;c.onopen=function(){"function"==typeof n&&n(),o(c,{authentication:{clientId:t.clientId,token:t.token}}),l.emit("open")},this.createChannel=function(e){var t=u[e],n=new r(c,e,t);return f[e]=n,n},c.onmessage=function(e){if(s(e.data)){var t=JSON.parse(e.data);if(!t.relay)throw"unrecognized ws message";var n=f[t.relay.senderId];if(n)n.emit("message",t.relay.message);else{var r=u[t.relay.senderId];void 0===r?u[t.relay.senderId]=[t.relay.message]:r.push(t.relay.message)}}}}function r(e,t,n){var r=this;this.send=function(n){o(e,{relay:{targetId:t,message:n}})},this.emitQueuedMessages=function(){if(n)for(;n.length>0;)r.emit("message",n.pop())}}function i(e,t){if(void 0===e)throw new Error("first parameter is required");if(void 0===t)throw new Error("second parameter is required");if(void 0===t.clientId)throw new Error("clientId property is required in authentication object");if(void 0===t.token)throw new Error("token property is required in authentication object")}function s(e){try{return JSON.parse(e),!0}catch(t){return!1}}function o(e,t){e.send(JSON.stringify(t))}var u=n(3);if("undefined"==typeof a)var a=e.require("ws");t.prototype=Object.create(u.prototype),r.prototype=Object.create(u.prototype),e.exports=t}).call(t,n(2)(e))},function(e,t){e.exports=function(e){return e.webpackPolyfill||(e.deprecate=function(){},e.paths=[],e.children=[],e.webpackPolyfill=1),e}},function(e,t,n){var r;(function(){function t(){}function i(e,t){for(var n=e.length;n--;)if(e[n].listener===t)return n;return-1}function s(e){return function(){return this[e].apply(this,arguments)}}var o=t.prototype,u=this,a=u.EventEmitter;o.getListeners=function(e){var t,n,r=this._getEvents();if(e instanceof RegExp){t={};for(n in r)r.hasOwnProperty(n)&&e.test(n)&&(t[n]=r[n])}else t=r[e]||(r[e]=[]);return t},o.flattenListeners=function(e){var t,n=[];for(t=0;t<e.length;t+=1)n.push(e[t].listener);return n},o.getListenersAsObject=function(e){var t,n=this.getListeners(e);return n instanceof Array&&(t={},t[e]=n),t||n},o.addListener=function(e,t){var n,r=this.getListenersAsObject(e),s="object"===("undefined"==typeof t?"undefined":_typeof(t));for(n in r)r.hasOwnProperty(n)&&-1===i(r[n],t)&&r[n].push(s?t:{listener:t,once:!1});return this},o.on=s("addListener"),o.addOnceListener=function(e,t){return this.addListener(e,{listener:t,once:!0})},o.once=s("addOnceListener"),o.defineEvent=function(e){return this.getListeners(e),this},o.defineEvents=function(e){for(var t=0;t<e.length;t+=1)this.defineEvent(e[t]);return this},o.removeListener=function(e,t){var n,r,s=this.getListenersAsObject(e);for(r in s)s.hasOwnProperty(r)&&(n=i(s[r],t),-1!==n&&s[r].splice(n,1));return this},o.off=s("removeListener"),o.addListeners=function(e,t){return this.manipulateListeners(!1,e,t)},o.removeListeners=function(e,t){return this.manipulateListeners(!0,e,t)},o.manipulateListeners=function(e,t,n){var r,i,s=e?this.removeListener:this.addListener,o=e?this.removeListeners:this.addListeners;if("object"!==("undefined"==typeof t?"undefined":_typeof(t))||t instanceof RegExp)for(r=n.length;r--;)s.call(this,t,n[r]);else for(r in t)t.hasOwnProperty(r)&&(i=t[r])&&("function"==typeof i?s.call(this,r,i):o.call(this,r,i));return this},o.removeEvent=function(e){var t,n="undefined"==typeof e?"undefined":_typeof(e),r=this._getEvents();if("string"===n)delete r[e];else if(e instanceof RegExp)for(t in r)r.hasOwnProperty(t)&&e.test(t)&&delete r[t];else delete this._events;return this},o.removeAllListeners=s("removeEvent"),o.emitEvent=function(e,t){var n,r,i,s,o,u=this.getListenersAsObject(e);for(s in u)if(u.hasOwnProperty(s))for(n=u[s].slice(0),i=0;i<n.length;i++)r=n[i],r.once===!0&&this.removeListener(e,r.listener),o=r.listener.apply(this,t||[]),o===this._getOnceReturnValue()&&this.removeListener(e,r.listener);return this},o.trigger=s("emitEvent"),o.emit=function(e){var t=Array.prototype.slice.call(arguments,1);return this.emitEvent(e,t)},o.setOnceReturnValue=function(e){return this._onceReturnValue=e,this},o._getOnceReturnValue=function(){return this.hasOwnProperty("_onceReturnValue")?this._onceReturnValue:!0},o._getEvents=function(){return this._events||(this._events={})},t.noConflict=function(){return u.EventEmitter=a,t},r=function(){return t}.call(u,n,u,e),!(void 0!==r&&(e.exports=r))}).call(this)}]);