UNPKG

data-transport

Version:
3 lines (2 loc) 28.5 kB
"use strict";var e=Symbol("listener"),n=Symbol("listen"),t=Symbol("sender"),r=Symbol("requestsMap"),o=Symbol("listensMap"),i=Symbol("serializer"),s=Symbol("log"),a=Symbol("verbose"),c=Symbol("originalListensMap"),l=Symbol("callback"),u=Symbol("timeout"),d=Symbol("prefix"),f=Symbol("produce"),h=Symbol("beforeEmit"),p=Symbol("beforeEmitResolve"),v="__DATA_TRANSPORT_UUID__",m="request",y="response",b=function(e,n){return b=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t])},b(e,n)};function _(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function t(){this.constructor=e}b(e,n),e.prototype=null===n?Object.create(n):(t.prototype=n.prototype,new t)}var w=function(){return w=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var o in n=arguments[t])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},w.apply(this,arguments)};function g(e,n){var t={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&n.indexOf(r)<0&&(t[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)n.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(t[r[o]]=e[r[o]])}return t}function C(e,n,t,r){return new(t||(t=Promise))((function(o,i){function s(e){try{c(r.next(e))}catch(e){i(e)}}function a(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var n;e.done?o(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(s,a)}c((r=r.apply(e,n||[])).next())}))}function k(e,n){var t,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},s=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(a){return function(c){return function(a){if(t)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(i=0)),i;)try{if(t=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,r=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){i.label=a[1];break}if(6===a[0]&&i.label<o[1]){i.label=o[1],o=a;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(a);break}o[2]&&i.ops.pop(),i.trys.pop();continue}a=n.call(e,i)}catch(e){a=[6,e],r=0}finally{t=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function E(e){var n="function"==typeof Symbol&&Symbol.iterator,t=n&&e[n],r=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function S(e,n){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var r,o,i=t.call(e),s=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(t=i.return)&&t.call(i)}finally{if(o)throw o.error}}return s}"function"==typeof SuppressedError&&SuppressedError;let x;const T=new Uint8Array(16);function M(){if(!x&&(x="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!x))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return x(T)}const O=[];for(let e=0;e<256;++e)O.push((e+256).toString(16).slice(1));var I={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function L(e,n,t){if(I.randomUUID&&!e)return I.randomUUID();const r=(e=e||{}).random||(e.rng||M)();return r[6]=15&r[6]|64,r[8]=63&r[8]|128,function(e,n=0){return(O[e[n+0]]+O[e[n+1]]+O[e[n+2]]+O[e[n+3]]+"-"+O[e[n+4]]+O[e[n+5]]+"-"+O[e[n+6]]+O[e[n+7]]+"-"+O[e[n+8]]+O[e[n+9]]+"-"+O[e[n+10]]+O[e[n+11]]+O[e[n+12]]+O[e[n+13]]+O[e[n+14]]+O[e[n+15]]).toLowerCase()}(r)}var R,P=function(){return/^((?!chrome|android).)*safari/i.test(navigator.userAgent)},D=function(){return L({rng:function(){for(var e,n=new Array(16),t=0;t<16;t++)3&t||(e=4294967296*Math.random()),n[t]=e>>>((3&t)<<3)&255;return n}})},W=function(e,n){return"".concat(e,"-").concat(n.toString())},A=function(){function l(l){var h=l.listener,p=l.sender,b=l.timeout,_=void 0===b?6e4:b,g=l.verbose,C=void 0!==g&&g,k=l.prefix,E=void 0===k?"DataTransport":k,S=l.listenKeys,x=void 0===S?[]:S;l.checkListen;var T,M,O=l.serializer,I=l.logger,L=this;this[R]=new Map,this.id=D(),this[o]=null!==(T=this[o])&&void 0!==T?T:new Map,this[c]=null!==(M=this[c])&&void 0!==M?M:new Map,this[e]=h.bind(this),this[t]=p.bind(this),this[u]=_,this[d]=E,this[i]=O,this[a]=C,this[s]=I,new Set(x).forEach((function(e){var n,t=L[e];L[c].set(e,t),Object.assign(L,((n={})[e]=function(){},n))})),this[c].forEach((function(e,n){L[f](n,e)})),this[n]=function(e){var n,t;if(L[a]&&("function"==typeof L[s]&&e?L[s](e):console.info("DataTransport Receive: ",e)),null==e?void 0:e[v]){var c=function(e,n){return n.replace(new RegExp("^".concat(e,"-")),"")}(L[d],e.action);if(L[c],e.type===y){var l=L[r].get(e[v]);if(l){var u=e.response;l("string"==typeof u&&(null===(n=L[i])||void 0===n?void 0:n.parse)?L[i].parse(u):u)}}else if(e.type===m){var f=L[o].get(e.action);if("function"==typeof f){var h=e.request;f("string"==typeof h&&(null===(t=L[i])||void 0===t?void 0:t.parse)?L[i].parse(h):h,w(w({},e),{transportId:e[v],hasRespond:e.hasRespond}))}}}};var P=this[e](this[n]);this.dispose=function(){if("function"==typeof P)return L[r].clear(),L[o].clear(),L[c].clear(),P()}}return l.prototype[(R=r,f)]=function(e,n){var r=this,s=W(this[d],e);this[o].set(s,(function(o,a){return C(r,void 0,void 0,(function(){var r,c,l,u,d=a.hasRespond,f=a.transportId;a.request;var h=g(a,["hasRespond","transportId","request"]);return k(this,(function(a){switch(a.label){case 0:return"function"!=typeof n?[3,2]:[4,n.apply(this,o)];case 1:return r=a.sent(),d?(c=w(w({},h),((l={action:s,response:void 0!==r&&(null===(u=this[i])||void 0===u?void 0:u.stringify)?this[i].stringify(r):r,hasRespond:d})[v]=f,l.type=y,l.responseId=this.id,l)),this[t](c),[3,3]):[2];case 2:throw new Error("The listener for event ".concat(e," should be a function."));case 3:return[2]}}))}))}))},l.prototype.listen=function(e,n){var t=this;if("string"!=typeof e)throw new Error('The event name "'.concat(e.toString(),'" is not a string, it should be a string.'));if(this[c].get(e))throw new Error('Failed to listen to the event "'.concat(e,'", the event "').concat(e,'" is already listened to.'));if("function"!=typeof n)throw new Error("The listener for event ".concat(e," should be a function."));return this[c].set(e,n),this[f](e,n),function(){t[c].delete(e);var n=W(t[d],e);t[o].delete(n)}},l.prototype.emit=function(e){for(var n=[],o=1;o<arguments.length;o++)n[o-1]=arguments[o];return C(this,void 0,void 0,(function(){var o,c,l,f,p,y,b,_,g,E,S,x,T,M,O,I,L=this;return k(this,(function(R){switch(R.label){case 0:return c=null===(x=(o="object"==typeof e?e:{}).respond)||void 0===x||x,l=null!==(T=o.silent)&&void 0!==T&&T,f=null!==(M=o.timeout)&&void 0!==M?M:this[u],p=null!==(O=o.name)&&void 0!==O?O:e,y=D(),b=W(this[d],p),_=w(w({},o._extra?{_extra:o._extra}:{}),((S={type:m,action:b,request:void 0!==n&&(null===(I=this[i])||void 0===I?void 0:I.stringify)?this[i].stringify(n):n,hasRespond:c})[v]=y,S.requestId=this.id,S)),this[a]&&("function"==typeof this[s]?this[s](_):console.info("DataTransport Send: ",_)),c?[3,3]:!this[h]||o.skipBeforeEmit?[3,2]:[4,this[h]];case 1:R.sent(),R.label=2;case 2:return this[t](_),[2,Promise.resolve(void 0)];case 3:return E=Promise.race([new Promise((function(e){return C(L,void 0,void 0,(function(){return k(this,(function(n){switch(n.label){case 0:return!this[h]||o.skipBeforeEmit?[3,2]:[4,this[h]];case 1:n.sent(),n.label=2;case 2:return this[r].set(y,e),this[t](_),[2]}}))}))})),new Promise((function(e,n){g=setTimeout((function(){n()}),f)}))]),[2,E.then((function(e){return clearTimeout(g),L[r].delete(y),e})).catch((function(e){if(clearTimeout(g),L[r].delete(y),void 0===e){if(l)return;console.warn("The event '".concat(b,"' timed out for ").concat(f," seconds..."),_)}}))]}}))}))},l}(),q="iframe-connect",B=function(e){function n(n){var t=this,r=n.iframe,o=void 0===r?document.querySelector("iframe"):r,i=n.targetOrigin,s=void 0===i?"*":i,a=n.listener,c=void 0===a?function(e){var n=function(n){var t=n.data,r=n.source,i=o.contentWindow;if(i&&i===r)return e(t)};return window.addEventListener("message",n),function(){window.removeEventListener("message",n)}}:a,l=n.sender,u=void 0===l?function(e){o?o.contentWindow.postMessage(e,s):window.frames[0]?window.frames[0].postMessage(e,s):console.error("The current page does not have any iframe elements")}:l,d=n.skipConnectionCheck,f=g(n,["iframe","targetOrigin","listener","sender","skipConnectionCheck"]);if(t=e.call(this,w(w({},f),{listener:c,sender:u}))||this,!d){var v=function(){t.emit({name:q,silent:!0,skipBeforeEmit:!0}).then((function(e){e&&t[p]()}))};v(),t[h]=new Promise((function(e){t[p]=e})),t.listen(q,(function(){return C(t,void 0,void 0,(function(){return k(this,(function(e){return this[p](),[2,!0]}))}))})),null==o||o.addEventListener("load",(function(){t[h]=new Promise((function(e){t[p]=e})),v()}))}return t}return _(n,e),n}(A),U=function(e){function n(n){void 0===n&&(n={});var t=this,r=n.targetOrigin,o=void 0===r?"*":r,i=n.listener,s=void 0===i?function(e){var n=function(n){var t=n.data;return e(t)};return window.addEventListener("message",n),function(){window.removeEventListener("message",n)}}:i,a=n.sender,c=void 0===a?function(e){return window.parent.postMessage(e,o)}:a,l=n.skipConnectionCheck,u=g(n,["targetOrigin","listener","sender","skipConnectionCheck"]);return t=e.call(this,w(w({},u),{listener:s,sender:c}))||this,l||(t.emit({name:q,silent:!0}).then((function(e){e&&t[p]()})),t[h]=new Promise((function(e){t[p]=e})),t.listen(q,(function(){return C(t,void 0,void 0,(function(){return k(this,(function(e){return this[p](),[2,!0]}))}))}))),t}return _(n,e),n}(A),j={Main:B,IFrame:U},N="worker-connect",F=function(e){function n(n){var t=this,r=n.worker,o=n.listener,i=void 0===o?function(e){var n=function(n){var t=n.data;e(t)};return r.addEventListener("message",n),function(){r.removeEventListener("message",n)}}:o,s=n.sender,a=void 0===s?function(e){var n,t=null!==(n=e.transfer)&&void 0!==n?n:[];delete e.transfer,r.postMessage(e,t)}:s,c=g(n,["worker","listener","sender"]);return(t=e.call(this,w(w({},c),{listener:i,sender:a}))||this)._connected=!1,t._handleConnectCallbacks=function(){return C(t,void 0,void 0,(function(){return k(this,(function(e){return this._connected||(this._connected=!0,this._onConnectCallback.forEach((function(e){e()})),this._onConnectCallback.clear()),[2]}))}))},t._onConnectCallback=new Set,t.emit({name:N,respond:!0,silent:!0}).then(t._handleConnectCallbacks),t.listen(N,t._handleConnectCallbacks),t}return _(n,e),n.prototype.onConnect=function(e){var n=this;return this._connected?e():(this._onConnectCallback.add(e),function(){n._onConnectCallback.delete(e)})},n}(A),J=function(e){function n(n){void 0===n&&(n={});var t=this,r=n.listener,o=void 0===r?function(e){var n=function(n){var t=n.data;e(t)};return self.addEventListener("message",n),function(){self.removeEventListener("message",n)}}:r,i=n.sender,s=void 0===i?function(e){var n,t=null!==(n=e.transfer)&&void 0!==n?n:[];delete e.transfer,postMessage(e,t)}:i,a=g(n,["listener","sender"]);return(t=e.call(this,w(w({},a),{listener:o,sender:s}))||this)._handleConnectCallbacks=function(){return C(t,void 0,void 0,(function(){return k(this,(function(e){return this._connected||(this._connected=!0,this._onConnectCallback.forEach((function(e){e()})),this._onConnectCallback.clear()),[2]}))}))},t._connected=!1,t._onConnectCallback=new Set,t.emit({name:N,respond:!0,silent:!0}).then(t._handleConnectCallbacks),t.listen(N,t._handleConnectCallbacks),t}return _(n,e),n.prototype.onConnect=function(e){var n=this;return this._connected?e():(this._onConnectCallback.add(e),function(){n._onConnectCallback.delete(e)})},n}(A),$={Main:F,Worker:J},K="sharedworker-connect",V="sharedworker-disconnect",z=function(e){function n(n){var t=this,r=n.worker,o=n.listener,i=void 0===o?function(e){var n=function(n){var t=n.data;e(t)};return r.port.addEventListener("message",n),r.port.start(),function(){r.port.removeEventListener("message",n)}}:o,s=n.sender,a=void 0===s?function(e){var n,t=null!==(n=e.transfer)&&void 0!==n?n:[];delete e.transfer,r.port.postMessage(e,t)}:s,c=g(n,["worker","listener","sender"]);return(t=e.call(this,w(w({},c),{listener:i,sender:a}))||this)._connected=!1,t._onConnectCallback=new Set,window.addEventListener("pagehide",(function(){t.emit({name:V,respond:!1},t.id)})),t.listen(K,(function(){return C(t,void 0,void 0,(function(){var e=this;return k(this,(function(n){return Promise.resolve().then((function(){e._handleConnect()})),[2,this.id]}))}))})),t.emit({name:K,respond:!1,silent:!0}),t}return _(n,e),n.prototype._handleConnect=function(){this._connected||(this._connected=!0,this._onConnectCallback.forEach((function(e){e()})))},n.prototype.onConnect=function(e){var n=this;return this._onConnectCallback.add(e),function(){n._onConnectCallback.delete(e)}},n}(A),G=function(e){function n(n){void 0===n&&(n={});var t=this,r=n.listener,o=void 0===r?function(e){var n=this;return this[l]=e,function(){n.ports.forEach((function(e){e._handler&&e.removeEventListener("message",e._handler),delete e._handler})),self.close()}}:r,i=n.sender,s=void 0===i?function(e){var n,r,o=null!==(n=e.transfer)&&void 0!==n?n:[];delete e.transfer;var i=null===(r=e._extra)||void 0===r?void 0:r._port;if(i)delete e._extra._port,i.postMessage(e,o);else if("response"===e.type&&t.ports.has(e.requestId)){t.ports.get(e.requestId).postMessage(e,o)}else t.ports.forEach((function(n){try{n.postMessage(e,o)}catch(e){console.error(e)}})),t.tempPorts.forEach((function(n){try{n.postMessage(e,o)}catch(e){console.error(e)}}))}:i,a=g(n,["listener","sender"]);(t=e.call(this,w(w({},a),{listener:o,sender:s}))||this).ports=new Map,t.tempPorts=new Set,t._onConnectCallback=new Set,t._onDisconnectCallback=new Set;var c=W(t[d],V),u=W(t[d],K);return self.addEventListener("connect",(function(e){return C(t,void 0,void 0,(function(){var n,t,r,o=this;return k(this,(function(i){switch(i.label){case 0:(n=e.ports[0])._handler=function(e){var t,r=e.data;r.hasRespond&&(r._extra=null!==(t=r._extra)&&void 0!==t?t:{},r._extra._port=n),r.action===c&&o.ports.has(r.requestId)&&(o.ports.delete(r.requestId),o._onDisconnectCallback.forEach((function(e){e(r.requestId)}))),"request"===r.type&&r.action===u&&(o.emit({name:K,_extra:{_port:n},silent:!0,respond:!1}),o._handleConnect(r.requestId,n)),o[l](r)},n.addEventListener("message",n._handler),n.start(),this.tempPorts.add(n),i.label=1;case 1:return i.trys.push([1,3,,4]),[4,this.emit({name:K,_extra:{_port:n},silent:!0})];case 2:return t=i.sent(),this._handleConnect(t,n),[3,4];case 3:return r=i.sent(),this.tempPorts.delete(n),console.error(r),[3,4];case 4:return[2]}}))}))})),t}return _(n,e),n.prototype.onConnect=function(e){var n=this;return this._onConnectCallback.add(e),function(){n._onConnectCallback.delete(e)}},n.prototype._handleConnect=function(e,n){e&&!this.ports.has(e)&&(this.ports.set(e,n),this.tempPorts.delete(n),this._onConnectCallback.forEach((function(n){n(e)})))},n.prototype.onDisconnect=function(e){var n=this;return this._onDisconnectCallback.add(e),function(){n._onDisconnectCallback.delete(e)}},n}(A),X={Client:z,Worker:G},H=!0,Q=function(e){function n(n){var t=n.worker,r=n.useOnSafari,o=void 0===r?H:r,i=n.listener,s=void 0===i?function(e){var n=function(n){var t=n.data;e(function(e,n){try{return"string"==typeof e&&n&&P()?JSON.parse(e):e}catch(n){console.error("Failed to parse:",e)}return e}(t,o))};return navigator.serviceWorker.addEventListener("message",n),function(){navigator.serviceWorker.removeEventListener("message",n)}}:i,a=n.sender,c=void 0===a?function(e){var n,r=null!==(n=e.transfer)&&void 0!==n?n:[];delete e.transfer,t.postMessage(e,r)}:a,l=g(n,["worker","useOnSafari","listener","sender"]);return e.call(this,w(w({},l),{listener:s,sender:c}))||this}return _(n,e),n}(A),Y=function(e){function n(n){void 0===n&&(n={});var t=this,r=n.useOnSafari,o=void 0===r?H:r,i=n.listener,s=void 0===i?function(e){var n=function(n){var t=n.data,r=n.source;t._clientId=r.id,e(t)};return self.addEventListener("message",n),function(){return self.removeEventListener("message",n)}}:i,a=n.sender,c=void 0===a?function(e){return C(t,void 0,void 0,(function(){var n,t,r,i,s;return k(this,(function(a){switch(a.label){case 0:return n=e.transfer||[],delete e.transfer,t=function(e,n){try{return n&&P()?JSON.stringify(e):e}catch(n){throw console.error("Failed to stringify:",e),n}}(e,o),e._clientId?[4,self.clients.get(e._clientId)]:[3,2];case 1:return(r=a.sent())?(delete e._clientId,r.postMessage(t,n),[2]):(console.warn('The client "'.concat(e._clientId,'" is closed.')),[2]);case 2:return(i=null===(s=e._extra)||void 0===s?void 0:s._client)?(delete e._extra._client,i.postMessage(t,n),[2]):(self.clients.matchAll().then((function(e){return e.map((function(e){return e.postMessage(t,n)}))})),[2])}}))}))}:a,l=g(n,["useOnSafari","listener","sender"]);return t=e.call(this,w(w({},l),{listener:s,sender:c}))||this,self.addEventListener("activate",(function(e){e.waitUntil(self.clients.claim())})),t}return _(n,e),n}(A),Z={Client:Q,Service:Y},ee=function(e){function n(n){void 0===n&&(n={});var t=n.channel,r=void 0===t?"$$BroadcastChannel_Transport$$":t,o=n.broadcastChannel,i=void 0===o?new BroadcastChannel(r):o,s=n.listener,a=void 0===s?function(e){var n=function(n){var t=n.data;e(t)};return i.addEventListener("message",n),function(){i.removeEventListener("message",n)}}:s,c=n.sender,l=void 0===c?function(e){return i.postMessage(e)}:c,u=g(n,["channel","broadcastChannel","listener","sender"]);return e.call(this,w(w({},u),{listener:a,sender:l}))||this}return _(n,e),n}(A),ne="$$Electron_Transport$$",te=function(e){function n(n){var t=n.ipcMain,r=n.browserWindow,o=n.channel,i=void 0===o?ne:o,s=n.listener,a=void 0===s?function(e){var n=function(n,t){e(t)};return t.on(i,n),function(){t.off(i,n)}}:s,c=n.sender,l=void 0===c?function(e){return r.webContents.send(i,e)}:c,u=g(n,["ipcMain","browserWindow","channel","listener","sender"]);return e.call(this,w(w({},u),{listener:a,sender:l}))||this}return _(n,e),n}(A),re=function(e){function n(n){var t=n.ipcRenderer,r=n.channel,o=void 0===r?ne:r,i=n.listener,s=void 0===i?function(e){var n=function(n,t){e(t)};return t.on(o,n),function(){t.off(o,n)}}:i,a=n.sender,c=void 0===a?function(e){return t.send(o,e)}:a,l=g(n,["ipcRenderer","channel","listener","sender"]);return e.call(this,w(w({},l),{listener:s,sender:c}))||this}return _(n,e),n}(A),oe={Main:te,Renderer:re},ie="undefined"!=typeof window?window:void 0!==ie?ie:"undefined"!=typeof self?self:{},se="__DATA_TRANSPORT_BROWSER_EXTENSIONS__",ae=function(e){function n(n){var t;void 0===n&&(n={});var r=n.browser,o=void 0===r?null!==(t=ie.browser)&&void 0!==t?t:ie.chrome:r,i=n.listener,s=void 0===i?function(e){this[l]=e;var n=function(n,t,r){return n._sendResponse=r,e(n),!0};return o.runtime.onMessage.addListener(n),function(){o.runtime.onMessage.removeListener(n)}}:i,a=n.sender,c=void 0===a?function(e){if(e._sendResponse){var n=e._sendResponse;delete e._sendResponse,n(e)}else o.runtime.sendMessage(e,{},this[l])}:a,u=g(n,["browser","listener","sender"]);return e.call(this,w(w({},u),{listener:s,sender:c}))||this}return _(n,e),n}(A),ce="sharedworker-connect",le=function(e){function n(n){void 0===n&&(n={});var t,r=this,o=n.browser,i=void 0===o?null!==(t=ie.browser)&&void 0!==t?t:ie.chrome:o,s=n.listener,a=void 0===s?function(e){var n=this;return this[l]=e,function(){n.ports.forEach((function(e){e.disconnect()}))}}:s,c=n.sender,u=void 0===c?function(e){var n,t=null===(n=e._extra)||void 0===n?void 0:n._port;if(t)delete e._port,t.postMessage(e);else if("response"===e.type&&this.ports.has(e.requestId)){this.ports.get(e.requestId).postMessage(e)}else this.ports.forEach((function(n){try{n.postMessage(e)}catch(e){console.error(e)}}))}:c,d=g(n,["browser","listener","sender"]);return(r=e.call(this,w(w({},d),{listener:a,sender:u}))||this).ports=new Map,r._onConnectCallback=new Set,r._onDisconnectCallback=new Set,i.runtime.onConnect.addListener((function(e){return C(r,void 0,void 0,(function(){var n,t,r=this;return k(this,(function(o){switch(o.label){case 0:return e.name!==se?[3,2]:(n=function(n){var t;n._extra=null!==(t=n._extra)&&void 0!==t?t:{},n._extra._port=e,r[l](n)},e.onMessage.addListener(n),e.onDisconnect.addListener((function(){e.onMessage.removeListener(n),r.ports.forEach((function(n,t){n===e&&r.ports.delete(t)})),r._onDisconnectCallback.forEach((function(e){e(t)}))})),[4,this.emit({name:ce,_extra:{_port:e}})]);case 1:t=o.sent(),this.ports.set(t,e),this._onConnectCallback.forEach((function(e){e(t)})),o.label=2;case 2:return[2]}}))}))})),r}return _(n,e),n.prototype.onConnect=function(e){var n=this;return this._onConnectCallback.add(e),function(){n._onConnectCallback.delete(e)}},n.prototype.onDisconnect=function(e){var n=this;return this._onDisconnectCallback.add(e),function(){n._onDisconnectCallback.delete(e)}},n}(A),ue=function(e){function n(n){void 0===n&&(n={});var t,r=this,o=n.browser,i=void 0===o?null!==(t=ie.browser)&&void 0!==t?t:ie.chrome:o,s=n.port,a=void 0===s?i.runtime.connect({name:se}):s,c=n.listener,l=void 0===c?function(e){var n=function(n){e(n)};return a.onMessage.addListener(n),function(){a.onMessage.removeListener(n)}}:c,u=n.sender,d=void 0===u?function(e){a.postMessage(e)}:u,f=g(n,["browser","port","listener","sender"]);return(r=e.call(this,w(w({},f),{listener:l,sender:d}))||this)._onConnectCallback=new Set,r.listen(ce,(function(){return C(r,void 0,void 0,(function(){var e=this;return k(this,(function(n){return Promise.resolve().then((function(){e._onConnectCallback.forEach((function(e){e()}))})),[2,this.id]}))}))})),r}return _(n,e),n.prototype.onConnect=function(e){var n=this;return this._onConnectCallback.add(e),function(){n._onConnectCallback.delete(e)}},n}(A),de={Main:le,Client:ue},fe=61440,he=function(e){function n(n){var t=this,r=n.peer,o=n.listener,i=void 0===o?function(e){var n=function(n){var r,o,i,s=JSON.parse(n),a=Object.prototype.hasOwnProperty.call(s,"request")?"request":"response",c=null!==(i=t.receiveBuffer.get(s.__DATA_TRANSPORT_UUID__))&&void 0!==i?i:{data:[],timestamp:Date.now()};t.receiveBuffer.set(s.__DATA_TRANSPORT_UUID__,c),c.data[s.chunkId]=s[a],c.data.length=s.length,c.timestamp=Date.now();var l=c.data.filter((function(e){return e})).length===s.length;if(l){var u=JSON.parse(c.data.join(""));s[a]="request"===a?u:u[0],delete s.length,e(s),t.receiveBuffer.delete(s.__DATA_TRANSPORT_UUID__);try{for(var d=E(t.receiveBuffer),f=d.next();!f.done;f=d.next()){var h=S(f.value,2),p=h[0],v=h[1];Date.now()-v.timestamp>6e4&&t.receiveBuffer.delete(p)}}catch(e){r={error:e}}finally{try{f&&!f.done&&(o=d.return)&&o.call(d)}finally{if(r)throw r.error}}}};return r.on("data",n),function(){r.off("data",n)}}:o,s=n.sender,a=void 0===s?function(e){var n,t=Object.prototype.hasOwnProperty.call(e,"request")?"request":"response";e[t]=JSON.stringify("request"===t?e.request:void 0!==e.response?[e.response]:[]);for(var o=0,i=Math.ceil(e[t].length/fe);e[t].length>0;){var s=w(w({},e),((n={})[t]=e[t].slice(0,fe),n.chunkId=o,n.length=i,n));r.send(JSON.stringify(s)),e[t]=e[t].slice(fe),o+=1}}:s,c=g(n,["peer","listener","sender"]);if((t=e.call(this,w(w({},c),{listener:i,sender:a}))||this).receiveBuffer=new Map,r){var l=!1,u=[],d=r.send.bind(r),f=function(){l=!1;for(var e=u.shift(),n=function(){if(r._channel.bufferedAmount&&r._channel.bufferedAmount>65536){l=!0,u.unshift(e);var n=function(){r._channel.removeEventListener("bufferedamountlow",n),f()};return r._channel.addEventListener("bufferedamountlow",n),{value:void 0}}try{d(e),e=u.shift()}catch(e){throw new Error("Error send message to peer: ".concat(e.message))}};e;){var t=n();if("object"==typeof t)return t.value}};r.send=function(e){u.push(e),l||f()}}return t}return _(n,e),n}(A),pe=function(e){function n(n){var t=n.targetOrigin,r=void 0===t?"*":t,o=n.listener,i=void 0===o?function(e){var n=function(n){var t=n.data;return e(t)};return window.addEventListener("message",n),function(){window.removeEventListener("message",n)}}:o,s=n.sender,a=void 0===s?function(e){window.postMessage(e,r)}:s,c=g(n,["targetOrigin","listener","sender"]);return e.call(this,w(w({},c),{listener:i,sender:a}))||this}return _(n,e),n}(A),ve=function(e){function n(n){var t=n.child,r=n.listener,o=void 0===r?function(e){var n=function(n){e(n)};return t.on("message",n),function(){t.off("message",n)}}:r,i=n.sender,s=void 0===i?function(e){t.send(e)}:i,a=g(n,["child","listener","sender"]);return e.call(this,w(w({},a),{listener:o,sender:s}))||this}return _(n,e),n}(A),me=function(e){function n(n){void 0===n&&(n={});var t=n.listener,r=void 0===t?function(e){var n=function(n){e(n)};return process.on("message",n),function(){process.off("message",n)}}:t,o=n.sender,i=void 0===o?function(e){process.send(e)}:o,s=g(n,["listener","sender"]);return e.call(this,w(w({},s),{listener:r,sender:i}))||this}return _(n,e),n}(A),ye={Main:ve,Child:me},be={Base:A,MessageTransport:pe,IFrameMain:j.Main,IFrameInternal:j.IFrame,BrowserExtensions:ae,BrowserExtensionsMain:de.Main,BrowserExtensionsClient:de.Client,ElectronMain:oe.Main,ElectronRenderer:oe.Renderer,ServiceWorkerClient:Z.Client,ServiceWorkerService:Z.Service,WebWorkerClient:$.Main,WebWorkerInternal:$.Worker,WebRTC:he,Broadcast:ee,SharedWorkerClient:X.Client,SharedWorkerInternal:X.Worker,MainProcess:ye.Main,ChildProcess:ye.Child},_e=function(e,n){return new be[e](n)};exports.BroadcastTransport=ee,exports.BrowserExtensionsClientTransport=ue,exports.BrowserExtensionsGenericTransport=ae,exports.BrowserExtensionsMainTransport=le,exports.BrowserExtensionsTransport=de,exports.ChildProcessTransport=me,exports.ElectronMainTransport=te,exports.ElectronRendererTransport=re,exports.ElectronTransport=oe,exports.IFrameInternalTransport=U,exports.IFrameMainTransport=B,exports.IFrameTransport=j,exports.MainProcessTransport=ve,exports.MessageTransport=pe,exports.ProcessTransport=ye,exports.ServiceWorkerClientTransport=Q,exports.ServiceWorkerServiceTransport=Y,exports.ServiceWorkerTransport=Z,exports.SharedWorkerClientTransport=z,exports.SharedWorkerInternalTransport=G,exports.SharedWorkerTransport=X,exports.Transport=A,exports.TransportMap=be,exports.WebRTCTransport=he,exports.WorkerInternalTransport=J,exports.WorkerMainTransport=F,exports.WorkerTransport=$,exports.createTransport=_e,exports.getAction=W,exports.listen=function(e,n,t){var r,o=t.value;return e[c]=null!==(r=e[c])&&void 0!==r?r:new Map,e[c].set(n,o),w(w({},t),{value:function(){return Promise.resolve()}})},exports.listenKey=n,exports.listenerKey=e,exports.logKey=s,exports.merge=function(n,r){for(var o=[],i=2;i<arguments.length;i++)o[i-2]=arguments[i];var s=function(e,n,t){if(t||2===arguments.length)for(var r,o=0,i=n.length;o<i;o++)!r&&o in n||(r||(r=Array.prototype.slice.call(n,0,o)),r[o]=n[o]);return e.concat(r||Array.prototype.slice.call(n))}([n,r],S(o),!1);if(void 0===n||void 0===r)throw new Error("Only more than one transports can be merged.");return _e("Base",{listener:function(n){var t=function(e){n(e)},r=s.map((function(n){return n[e](t)}));return function(){r.forEach((function(e){return e&&e()}))}},sender:function(e){s.forEach((function(n){n[t](e)}))}})},exports.mockPorts=function(){var e=new Set,n=null;return{main:{listener:function(e){return n=e,function(){n=null}},sender:function(n){var t,r;try{for(var o=E(e),i=o.next();!i.done;i=o.next()){(0,i.value)(JSON.parse(JSON.stringify(n)))}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=o.return)&&r.call(o)}finally{if(t)throw t.error}}}},create:function(){return{listener:function(n){return e.add(n),function(){e.delete(n)}},sender:function(e){null==n||n(JSON.parse(JSON.stringify(e)))}}}}},exports.senderKey=t,exports.verboseKey=a; //# sourceMappingURL=index.cjs.production.min.js.map