securityos/node_modules/comlink/dist/esm/comlink.min.js

8 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2024-09-06 15:32:35 +00:00
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
const e=Symbol("Comlink.proxy"),t=Symbol("Comlink.endpoint"),n=Symbol("Comlink.releaseProxy"),r=Symbol("Comlink.finalizer"),a=Symbol("Comlink.thrown"),s=e=>"object"==typeof e&&null!==e||"function"==typeof e,o=new Map([["proxy",{canHandle:t=>s(t)&&t[e],serialize(e){const{port1:t,port2:n}=new MessageChannel;return i(e,t),[n,[n]]},deserialize:e=>(e.start(),u(e))}],["throw",{canHandle:e=>s(e)&&a in e,serialize({value:e}){let t;return t=e instanceof Error?{isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:{isError:!1,value:e},[t,[]]},deserialize(e){if(e.isError)throw Object.assign(new Error(e.value.message),e.value);throw e.value}}]]);function i(e,t=globalThis,n=["*"]){t.addEventListener("message",(function s(o){if(!o||!o.data)return;if(!function(e,t){for(const n of e){if(t===n||"*"===n)return!0;if(n instanceof RegExp&&n.test(t))return!0}return!1}(n,o.origin))return void console.warn(`Invalid origin '${o.origin}' for comlink proxy`);const{id:u,type:l,path:p}=Object.assign({path:[]},o.data),g=(o.data.argumentList||[]).map(w);let f;try{const t=p.slice(0,-1).reduce(((e,t)=>e[t]),e),n=p.reduce(((e,t)=>e[t]),e);switch(l){case"GET":f=n;break;case"SET":t[p.slice(-1)[0]]=w(o.data.value),f=!0;break;case"APPLY":f=n.apply(t,g);break;case"CONSTRUCT":f=y(new n(...g));break;case"ENDPOINT":{const{port1:t,port2:n}=new MessageChannel;i(e,n),f=E(t,[t])}break;case"RELEASE":f=void 0;break;default:return}}catch(e){f={value:e,[a]:0}}Promise.resolve(f).catch((e=>({value:e,[a]:0}))).then((n=>{const[a,o]=b(n);t.postMessage(Object.assign(Object.assign({},a),{id:u}),o),"RELEASE"===l&&(t.removeEventListener("message",s),c(t),r in e&&"function"==typeof e[r]&&e[r]())})).catch((e=>{const[n,r]=b({value:new TypeError("Unserializable return value"),[a]:0});t.postMessage(Object.assign(Object.assign({},n),{id:u}),r)}))})),t.start&&t.start()}function c(e){(function(e){return"MessagePort"===e.constructor.name})(e)&&e.close()}function u(e,t){return m(e,[],t)}function l(e){if(e)throw new Error("Proxy has been released and is not useable")}function p(e){return L(e,{type:"RELEASE"}).then((()=>{c(e)}))}const g=new WeakMap,f="FinalizationRegistry"in globalThis&&new FinalizationRegistry((e=>{const t=(g.get(e)||0)-1;g.set(e,t),0===t&&p(e)}));function m(e,r=[],a=function(){}){let s=!1;const o=new Proxy(a,{get(t,a){if(l(s),a===n)return()=>{!function(e){f&&f.unregister(e)}(o),p(e),s=!0};if("then"===a){if(0===r.length)return{then:()=>o};const t=L(e,{type:"GET",path:r.map((e=>e.toString()))}).then(w);return t.then.bind(t)}return m(e,[...r,a])},set(t,n,a){l(s);const[o,i]=b(a);return L(e,{type:"SET",path:[...r,n].map((e=>e.toString())),value:o},i).then(w)},apply(n,a,o){l(s);const i=r[r.length-1];if(i===t)return L(e,{type:"ENDPOINT"}).then(w);if("bind"===i)return m(e,r.slice(0,-1));const[c,u]=d(o);return L(e,{type:"APPLY",path:r.map((e=>e.toString())),argumentList:c},u).then(w)},construct(t,n){l(s);const[a,o]=d(n);return L(e,{type:"CONSTRUCT",path:r.map((e=>e.toString())),argumentList:a},o).then(w)}});return function(e,t){const n=(g.get(t)||0)+1;g.set(t,n),f&&f.register(e,t,e)}(o,e),o}function d(e){const t=e.map(b);return[t.map((e=>e[0])),(n=t.map((e=>e[1])),Array.prototype.concat.apply([],n))];var n}const h=new WeakMap;function E(e,t){return h.set(e,t),e}function y(t){return Object.assign(t,{[e]:!0})}function v(e,t=globalThis,n="*"){return{postMessage:(t,r)=>e.postMessage(t,n,r),addEventListener:t.addEventListener.bind(t),removeEventListener:t.removeEventListener.bind(t)}}function b(e){for(const[t,n]of o)if(n.canHandle(e)){const[r,a]=n.serialize(e);return[{type:"HANDLER",name:t,value:r},a]}return[{type:"RAW",value:e},h.get(e)||[]]}function w(e){switch(e.type){case"HANDLER":return o.get(e.name).deserialize(e.value);case"RAW":return e.value}}function L(e,t,n){return new Promise((r=>{const a=new Array(4).fill(0).map((()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16))).join("-");e.addEventListener("message",(function t(n){n.data&&n.data.id&&n.data.id===a&&(e.removeEventListener("message",t),r(n.data))})),e.start&&e.start(),e.postMessage(Object.assign
//# sourceMappingURL=comlink.min.js.map