UNPKG

@interopio/browser-worker

Version:

IO Connect Browser service worker module

2 lines (1 loc) 17.6 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(((e="undefined"!=typeof globalThis?globalThis:e||self).web=e.web||{},e.web.worker=e.web.worker||{},e.web.worker.min={}))}(this,(function(e){"use strict";function n(e,n,t,r){return new(t||(t=Promise))((function(o,i){function a(e){try{u(r.next(e))}catch(e){i(e)}}function c(e){try{u(r.throw(e))}catch(e){i(e)}}function u(e){var n;e.done?o(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(a,c)}u((r=r.apply(e,n||[])).next())}))}function t(e,n){var t,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(u){return function(c){if(t)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(a=0)),a;)try{if(t=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return a.label++,{value:c[1],done:!1};case 5:a.label++,r=c[1],c=[0];continue;case 7:c=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){a=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){a.label=c[1];break}if(6===c[0]&&a.label<o[1]){a.label=o[1],o=c;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(c);break}o[2]&&a.ops.pop(),a.trys.pop();continue}c=n.call(e,a)}catch(e){c=[6,e],r=0}finally{t=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}"function"==typeof SuppressedError&&SuppressedError;var r="glue42-core-worker",o=function(e){return{ok:!0,result:e}},i=function(e){return{ok:!1,error:e}},a=function(e,n,t){return!1===n.ok?n:!1===t.ok?t:o(e(n.result,t.result))},c=function(e,n){return!0===n.ok?n:i(e(n.error))},u=function(){return u=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},u.apply(this,arguments)};function s(e,n){if(e===n)return!0;if(null===e&&null===n)return!0;if(typeof e!=typeof n)return!1;if("object"==typeof e){if(Array.isArray(e)){if(!Array.isArray(n))return!1;if(e.length!==n.length)return!1;for(var t=0;t<e.length;t++)if(!s(e[t],n[t]))return!1;return!0}var r=Object.keys(e);if(r.length!==Object.keys(n).length)return!1;for(t=0;t<r.length;t++){if(!n.hasOwnProperty(r[t]))return!1;if(!s(e[r[t]],n[r[t]]))return!1}return!0}}var f=function(e){return Array.isArray(e)},l=function(e){return"object"==typeof e&&null!==e&&!f(e)},d=function(e,n){return"expected "+e+", got "+function(e){switch(typeof e){case"string":return"a string";case"number":return"a number";case"boolean":return"a boolean";case"undefined":return"undefined";case"object":return e instanceof Array?"an array":null===e?"null":"an object";default:return JSON.stringify(e)}}(n)},p=function(e){return e.map((function(e){return"string"==typeof e?"."+e:"["+e+"]"})).join("")},g=function(e,n){var t=n.at,r=function(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}(n,["at"]);return u({at:e+(t||"")},r)},v=function(){function e(n){var t=this;this.decode=n,this.run=function(e){return c((function(n){return{kind:"DecoderError",input:e,at:"input"+(n.at||""),message:n.message||""}}),t.decode(e))},this.runPromise=function(e){return!0===(n=t.run(e)).ok?Promise.resolve(n.result):Promise.reject(n.error);var n},this.runWithException=function(e){return function(e){if(!0===e.ok)return e.result;throw e.error}(t.run(e))},this.map=function(n){return new e((function(e){return function(e,n){return!0===n.ok?o(e(n.result)):n}(n,t.decode(e))}))},this.andThen=function(n){return new e((function(e){return function(e,n){return!0===n.ok?e(n.result):n}((function(t){return n(t).decode(e)}),t.decode(e))}))},this.where=function(n,r){return t.andThen((function(t){return n(t)?e.succeed(t):e.fail(r)}))}}return e.string=function(){return new e((function(e){return"string"==typeof e?o(e):i({message:d("a string",e)})}))},e.number=function(){return new e((function(e){return"number"==typeof e?o(e):i({message:d("a number",e)})}))},e.boolean=function(){return new e((function(e){return"boolean"==typeof e?o(e):i({message:d("a boolean",e)})}))},e.constant=function(n){return new e((function(e){return s(e,n)?o(n):i({message:"expected "+JSON.stringify(n)+", got "+JSON.stringify(e)})}))},e.object=function(n){return new e((function(e){if(l(e)&&n){var t={};for(var r in n)if(n.hasOwnProperty(r)){var a=n[r].decode(e[r]);if(!0!==a.ok)return void 0===e[r]?i({message:"the key '"+r+"' is required but was not present"}):i(g("."+r,a.error));void 0!==a.result&&(t[r]=a.result)}return o(t)}return l(e)?o(e):i({message:d("an object",e)})}))},e.array=function(n){return new e((function(e){if(f(e)&&n){return e.reduce((function(e,t,r){return a((function(e,n){return e.concat([n])}),e,function(e,t){return c((function(e){return g("["+t+"]",e)}),n.decode(e))}(t,r))}),o([]))}return f(e)?o(e):i({message:d("an array",e)})}))},e.tuple=function(n){return new e((function(e){if(f(e)){if(e.length!==n.length)return i({message:"expected a tuple of length "+n.length+", got one of length "+e.length});for(var t=[],r=0;r<n.length;r++){var a=n[r].decode(e[r]);if(!a.ok)return i(g("["+r+"]",a.error));t[r]=a.result}return o(t)}return i({message:d("a tuple of length "+n.length,e)})}))},e.union=function(n,t){for(var r=[],o=2;o<arguments.length;o++)r[o-2]=arguments[o];return e.oneOf.apply(e,[n,t].concat(r))},e.intersection=function(n,t){for(var r=[],i=2;i<arguments.length;i++)r[i-2]=arguments[i];return new e((function(e){return[n,t].concat(r).reduce((function(n,t){return a(Object.assign,n,t.decode(e))}),o({}))}))},e.anyJson=function(){return new e((function(e){return o(e)}))},e.unknownJson=function(){return new e((function(e){return o(e)}))},e.dict=function(n){return new e((function(e){if(l(e)){var t={};for(var r in e)if(e.hasOwnProperty(r)){var a=n.decode(e[r]);if(!0!==a.ok)return i(g("."+r,a.error));t[r]=a.result}return o(t)}return i({message:d("an object",e)})}))},e.optional=function(n){return new e((function(e){return null==e?o(void 0):n.decode(e)}))},e.oneOf=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return new e((function(e){for(var t=[],r=0;r<n.length;r++){var o=n[r].decode(e);if(!0===o.ok)return o;t[r]=o.error}var a=t.map((function(e){return"at error"+(e.at||"")+": "+e.message})).join('", "');return i({message:'expected a value matching one of the decoders, got the errors ["'+a+'"]'})}))},e.withDefault=function(n,t){return new e((function(e){return o(function(e,n){return!0===n.ok?n.result:e}(n,t.decode(e)))}))},e.valueAt=function(n,t){return new e((function(e){for(var r=e,o=0;o<n.length;o++){if(void 0===r)return i({at:p(n.slice(0,o+1)),message:"path does not exist"});if("string"==typeof n[o]&&!l(r))return i({at:p(n.slice(0,o+1)),message:d("an object",r)});if("number"==typeof n[o]&&!f(r))return i({at:p(n.slice(0,o+1)),message:d("an array",r)});r=r[n[o]]}return c((function(e){return void 0===r?{at:p(n),message:"path does not exist"}:g(p(n),e)}),t.decode(r))}))},e.succeed=function(n){return new e((function(e){return o(n)}))},e.fail=function(n){return new e((function(e){return i({message:n})}))},e.lazy=function(n){return new e((function(e){return n().decode(e)}))},e}(),h=v.string;v.number;var m=v.boolean,b=v.anyJson;v.unknownJson,v.constant;var y=v.object,w=v.array;v.tuple,v.dict;var k=v.optional;v.oneOf,v.union,v.intersection,v.withDefault,v.valueAt,v.succeed;var j=v.fail;v.lazy;var O=h().where((function(e){return e.length>0}),"Expected a non-empty string"),D=function(e,n){var t=typeof e;return"function"===t?b():j("The provided argument as ".concat(n," should be of type function, provided: ").concat(typeof t))},I=y({action:O,handler:b().andThen((function(e){return D(e,"handler")}))}),E=y({platform:k(y({url:k(O),openIfMissing:k(m())})),notifications:k(y({defaultClick:k(b().andThen((function(e){return D(e,"defaultClick")}))),actionClicks:k(w(I))}))});let S=(e=21)=>{let n="",t=e;for(;t--;)n+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[64*Math.random()|0];return n};const x=(e,n)=>n.some((n=>e instanceof n));let B,L;const P=new WeakMap,T=new WeakMap,C=new WeakMap,N=new WeakMap,M=new WeakMap;let A={get(e,n,t){if(e instanceof IDBTransaction){if("done"===n)return T.get(e);if("objectStoreNames"===n)return e.objectStoreNames||C.get(e);if("store"===n)return t.objectStoreNames[1]?void 0:t.objectStore(t.objectStoreNames[0])}return q(e[n])},set:(e,n,t)=>(e[n]=t,!0),has:(e,n)=>e instanceof IDBTransaction&&("done"===n||"store"===n)||n in e};function W(e){return e!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(L||(L=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...n){return e.apply(V(this),n),q(P.get(this))}:function(...n){return q(e.apply(V(this),n))}:function(n,...t){const r=e.call(V(this),n,...t);return C.set(r,n.sort?n.sort():[n]),q(r)}}function J(e){return"function"==typeof e?W(e):(e instanceof IDBTransaction&&function(e){if(T.has(e))return;const n=new Promise(((n,t)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",i),e.removeEventListener("abort",i)},o=()=>{n(),r()},i=()=>{t(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",i),e.addEventListener("abort",i)}));T.set(e,n)}(e),x(e,B||(B=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,A):e)}function q(e){if(e instanceof IDBRequest)return function(e){const n=new Promise(((n,t)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",i)},o=()=>{n(q(e.result)),r()},i=()=>{t(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",i)}));return n.then((n=>{n instanceof IDBCursor&&P.set(n,e)})).catch((()=>{})),M.set(n,e),n}(e);if(N.has(e))return N.get(e);const n=J(e);return n!==e&&(N.set(e,n),M.set(n,e)),n}const V=e=>M.get(e);const F=["get","getKey","getAll","getAllKeys","count"],K=["put","add","delete","clear"],R=new Map;function U(e,n){if(!(e instanceof IDBDatabase)||n in e||"string"!=typeof n)return;if(R.get(n))return R.get(n);const t=n.replace(/FromIndex$/,""),r=n!==t,o=K.includes(t);if(!(t in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!F.includes(t))return;const i=async function(e,...n){const i=this.transaction(e,o?"readwrite":"readonly");let a=i.store;return r&&(a=a.index(n.shift())),(await Promise.all([a[t](...n),o&&i.done]))[0]};return R.set(n,i),i}var $;A=(e=>({...e,get:(n,t,r)=>U(n,t)||e.get(n,t,r),has:(n,t)=>!!U(n,t)||e.has(n,t)}))(A);var z=function(e){return e.split("?")[0].split("#")[0].replace(/\/$/,"")},G=function(){return void 0!==$?$:$=function(e,n,{blocked:t,upgrade:r,blocking:o,terminated:i}={}){const a=indexedDB.open(e,n),c=q(a);return r&&a.addEventListener("upgradeneeded",(e=>{r(q(a.result),e.oldVersion,e.newVersion,q(a.transaction),e)})),t&&a.addEventListener("blocked",(e=>t(e.oldVersion,e.newVersion,e))),c.then((e=>{i&&e.addEventListener("close",(()=>i())),o&&e.addEventListener("versionchange",(e=>o(e.oldVersion,e.newVersion,e)))})).catch((()=>{})),c}("glue42core",3,{upgrade:function(e){e.objectStoreNames.contains("workspaceLayouts")||e.createObjectStore("workspaceLayouts"),e.objectStoreNames.contains("globalLayouts")||e.createObjectStore("globalLayouts"),e.objectStoreNames.contains("serviceWorker")||e.createObjectStore("serviceWorker"),e.objectStoreNames.contains("prefs")||e.createObjectStore("prefs")}})},_=function(){var e=new Promise((function(e){var n=new BroadcastChannel(r),t=function(r){r.data.pong&&(n.removeEventListener("message",t),e(!0))};n.addEventListener("message",t),n.postMessage({messageType:"ping"})})),n=new Promise((function(e){return setTimeout((function(){return e(!1)}),3e3)}));return Promise.race([e,n])},H=function(e){return n(void 0,void 0,void 0,(function(){var n,r,o;return t(this,(function(t){return(n=Object.assign({},e,{title:void 0,clickInterop:void 0,actions:void 0})).actions=null===(o=e.actions)||void 0===o?void 0:o.map((function(e){return{action:e.action,title:e.title,icon:e.icon}})),r={clickInterop:e.clickInterop,actions:e.actions,id:S()},n.data?n.data.glueData=r:n.data={glueData:r},[2,self.registration.showNotification(e.title,n)]}))}))},Q=function(e){return new Promise((function(n,t){if(!e)return t("Cannot open the platform, because a url was not provided");var o=new BroadcastChannel(r),i=function(e){e.data.platformStarted&&(o.removeEventListener("message",i),n())};o.addEventListener("message",i),self.clients.openWindow(e).catch(t),setTimeout((function(){return t("Timed out waiting for the platform to open and send a ready signal: ".concat(6e4," MS"))}),6e4)}))},X=function(e){var o=function(e){var n;void 0===e&&(e={});var t=E.runWithException(e);return(null===(n=t.platform)||void 0===n?void 0:n.url)&&(t.platform.url=t.platform.url.replace(/\/$/,"")),t}(e);self.addEventListener("notificationclick",(function(e){var i,a=new BroadcastChannel(r);console.debug("Received a notification, checking if the platform is open");var c=_().then((function(n){var t,r,a;i=n,console.debug("The platform is: ".concat(i?"open":"not open"));var c=e.action;if(!c&&(null===(t=o.notifications)||void 0===t?void 0:t.defaultClick))return console.debug("Calling a defined default click handler"),o.notifications.defaultClick(e,i);if(c&&(null===(a=null===(r=o.notifications)||void 0===r?void 0:r.actionClicks)||void 0===a?void 0:a.some((function(e){return e.action===c})))){var u=o.notifications.actionClicks.find((function(e){return e.action===c})).handler;return console.debug("Calling a defined action click handler for action: ".concat(c)),u(e,i)}})).then((function(){return console.debug("Getting the platform url"),function(e){return n(void 0,void 0,void 0,(function(){var n,r,o,i,a,c;return t(this,(function(t){switch(t.label){case 0:return(null===(a=e.platform)||void 0===a?void 0:a.url)?(n=e.platform.url.split("?")[0].split("#")[0],console.debug("getting url from config: ".concat(n)),[2,z(n)]):(console.debug("starting the db"),[4,G()]);case 1:return(r=t.sent()).objectStoreNames.contains("serviceWorker")?[4,r.get("serviceWorker","workerData")]:(console.warn("there is no service worker store"),[2]);case 2:return o=t.sent(),i=null===(c=null==o?void 0:o.platformUrl)||void 0===c?void 0:c.split("?")[0].split("#")[0],[2,z(i)]}}))}))}(o)})).then((function(r){var a,c,u;if(console.debug("Found platform url: ".concat(r)),!i&&(null===(a=o.platform)||void 0===a?void 0:a.openIfMissing))return console.debug("Opening the platform"),Q(r);var s=null===(u=null===(c=e.notification.data)||void 0===c?void 0:c.glueData)||void 0===u?void 0:u.focusPlatformOnDefaultClick;return i&&s?(console.debug("Focusing the platform"),function(e){return n(void 0,void 0,void 0,(function(){var n,r,o,i;return t(this,(function(t){switch(t.label){case 0:return e?[4,self.clients.matchAll({type:"window"})]:(console.warn("Cannot open the platform, because a url was not provided"),[2]);case 1:n=t.sent(),r=0,o=n,t.label=2;case 2:return r<o.length?(i=o[r],i.url.split("?")[0].split("#")[0].replace(/\/$/,"")!==e?[3,4]:[4,i.focus()]):[3,5];case 3:return t.sent(),[2];case 4:return r++,[3,2];case 5:return[2]}}))}))}(r)):void 0})).then((function(){var n=e.action,t=e.notification.data.glueData,r={badge:e.notification.badge,body:e.notification.body,data:e.notification.data,dir:e.notification.dir,icon:e.notification.icon,image:e.notification.image,lang:e.notification.lang,renotify:e.notification.renotify,requireInteraction:e.notification.requireInteraction,silent:e.notification.silent,tag:e.notification.tag,timestamp:e.notification.timestamp,vibrate:e.notification.vibrate};a.postMessage({messageType:"notificationClick",action:n,glueData:t,definition:r})})).catch((function(e){var n="string"==typeof e?e:JSON.stringify(e.message);a.postMessage({messageType:"notificationError",error:n})}));e.waitUntil(c)})),self.addEventListener("notificationclose",(function(e){var n=new BroadcastChannel(r);console.debug("Received a notification close event, checking if the platform is open");var t=_().then((function(t){if(console.debug("The platform is: ".concat(t?"open":"not open")),t){var r=e.action,o=e.notification.data.glueData,i={badge:e.notification.badge,body:e.notification.body,data:e.notification.data,dir:e.notification.dir,icon:e.notification.icon,image:e.notification.image,lang:e.notification.lang,renotify:e.notification.renotify,requireInteraction:e.notification.requireInteraction,silent:e.notification.silent,tag:e.notification.tag,timestamp:e.notification.timestamp,vibrate:e.notification.vibrate};n.postMessage({messageType:"notificationClose",action:r,glueData:o,definition:i})}else console.log("The platform is not opened and will not open it for a notification close event")})).catch((function(e){var t="string"==typeof e?e:JSON.stringify(e.message);n.postMessage({messageType:"notificationError",error:t})}));e.waitUntil(t)}))};"undefined"!=typeof self&&(self.IOWorker=X,self.openBrowserPlatform=Q,self.raiseIONotification=H),e.default=X,e.openBrowserPlatform=Q,e.raiseIONotification=H,Object.defineProperty(e,"__esModule",{value:!0})}));