1 line
9.3 KiB
JavaScript
1 line
9.3 KiB
JavaScript
import{c as e,l as t,u as n}from"./vue-B11_pI9F.js";import{o as r}from"./common-utils-G6HD4Ji1.js";var i,a,o,s,c,l,u,d,f,p,m,h,g,_,v,y=class extends Error{constructor(e,t){super(t),r(this,`code`,void 0),this.name=`PenpalError`,this.code=e}},b=e=>({name:e.name,message:e.message,stack:e.stack,penpalCode:e instanceof y?e.code:void 0}),x=({name:e,message:t,stack:n,penpalCode:r})=>{let i=r?new y(r,t):Error(t);return i.name=e,i.stack=n,i},S=Symbol(`Reply`),C=(i=new WeakMap,class{constructor(e,t){r(this,`value`,void 0),r(this,`transferables`,void 0),n(this,i,S),this.value=e,this.transferables=t?.transferables}}),w=`penpal`,T=e=>typeof e==`object`&&!!e,E=e=>typeof e==`function`,D=e=>T(e)&&e.namespace===w,O=e=>e.type===`SYN`,k=e=>e.type===`ACK1`,A=e=>e.type===`ACK2`,j=e=>e.type===`CALL`,M=e=>e.type===`REPLY`,N=e=>e.type===`DESTROY`,P=(e,t=[])=>{let n=[];for(let r of Object.keys(e)){let i=e[r];E(i)?n.push([...t,r]):T(i)&&n.push(...P(i,[...t,r]))}return n},F=(e,t)=>{let n=e.reduce((e,t)=>T(e)?e[t]:void 0,t);return E(n)?n:void 0},I=e=>e.join(`.`),L=(e,t,n)=>({namespace:w,channel:e,type:`REPLY`,callId:t,isError:!0,...n instanceof Error?{value:b(n),isSerializedErrorInstance:!0}:{value:n}}),R=(e,t,n,r)=>{let i=!1,a=async a=>{if(i||!j(a))return;r?.(`Received ${I(a.methodPath)}() call`,a);let{methodPath:o,args:s,id:c}=a,l,u;try{let e=F(o,t);if(!e)throw new y(`METHOD_NOT_FOUND`,`Method \`${I(o)}\` is not found.`);let r=await e(...s);r instanceof C&&(u=r.transferables,r=await r.value),l={namespace:w,channel:n,type:`REPLY`,callId:c,value:r}}catch(e){l=L(n,c,e)}if(!i)try{r?.(`Sending ${I(o)}() reply`,l),e.sendMessage(l,u)}catch(t){throw t.name===`DataCloneError`&&(l=L(n,c,t),r?.(`Sending ${I(o)}() reply`,l),e.sendMessage(l)),t}};return e.addMessageHandler(a),()=>{i=!0,e.removeMessageHandler(a)}},z=crypto.randomUUID?.bind(crypto)??(()=>[,,,,].fill(0).map(()=>Math.floor(Math.random()*(2**53-1)).toString(16)).join(`-`)),B=Symbol(`CallOptions`),ee=(a=new WeakMap,class{constructor(e){r(this,`transferables`,void 0),r(this,`timeout`,void 0),n(this,a,B),this.transferables=e?.transferables,this.timeout=e?.timeout}}),V=new Set([`apply`,`call`,`bind`]),H=(e,t,n=[])=>new Proxy(n.length?()=>{}:Object.create(null),{get(r,i){if(i!==`then`)return n.length&&V.has(i)?Reflect.get(r,i):H(e,t,[...n,i])},apply(t,r,i){return e(n,i)}}),U=e=>new y(`CONNECTION_DESTROYED`,`Method call ${I(e)}() failed due to destroyed connection`),W=(e,t,n)=>{let r=!1,i=new Map,a=e=>{if(!M(e))return;let{callId:t,value:r,isError:a,isSerializedErrorInstance:o}=e,s=i.get(t);s&&(i.delete(t),n?.(`Received ${I(s.methodPath)}() call`,e),a?s.reject(o?x(r):r):s.resolve(r))};return e.addMessageHandler(a),{remoteProxy:H((a,o)=>{if(r)throw U(a);let s=z(),c=o[o.length-1],l=c instanceof ee,{timeout:u,transferables:d}=l?c:{},f=l?o.slice(0,-1):o;return new Promise((r,o)=>{let c=u===void 0?void 0:window.setTimeout(()=>{i.delete(s),o(new y(`METHOD_CALL_TIMEOUT`,`Method call ${I(a)}() timed out after ${u}ms`))},u);i.set(s,{methodPath:a,resolve:r,reject:o,timeoutId:c});try{let r={namespace:w,channel:t,type:`CALL`,id:s,methodPath:a,args:f};n?.(`Sending ${I(a)}() call`,r),e.sendMessage(r,d)}catch(e){o(new y(`TRANSMISSION_FAILED`,e.message))}})},n),destroy:()=>{r=!0,e.removeMessageHandler(a);for(let{methodPath:e,reject:t,timeoutId:n}of i.values())clearTimeout(n),t(U(e));i.clear()}}},G=()=>{let e,t;return{promise:new Promise((n,r)=>{e=n,t=r}),resolve:e,reject:t}},K=`deprecated-penpal`,q=e=>T(e)&&`penpal`in e,J=e=>e.split(`.`),Y=e=>e.join(`.`),X=e=>{try{return JSON.stringify(e)}catch{return String(e)}},Z=e=>new y(`TRANSMISSION_FAILED`,`Unexpected message to translate: ${X(e)}`),Q=e=>{if(e.penpal===`syn`)return{namespace:w,channel:void 0,type:`SYN`,participantId:K};if(e.penpal===`ack`)return{namespace:w,channel:void 0,type:`ACK2`};if(e.penpal===`call`)return{namespace:w,channel:void 0,type:`CALL`,id:e.id,methodPath:J(e.methodName),args:e.args};if(e.penpal===`reply`)return e.resolution===`fulfilled`?{namespace:w,channel:void 0,type:`REPLY`,callId:e.id,value:e.returnValue}:{namespace:w,channel:void 0,type:`REPLY`,callId:e.id,isError:!0,...e.returnValueIsError?{value:e.returnValue,isSerializedErrorInstance:!0}:{value:e.returnValue}};throw Z(e)},te=e=>{if(k(e))return{penpal:`synAck`,methodNames:e.methodPaths.map(Y)};if(j(e))return{penpal:`call`,id:e.id,methodName:Y(e.methodPath),args:e.args};if(M(e))return e.isError?{penpal:`reply`,id:e.callId,resolution:`rejected`,...e.isSerializedErrorInstance?{returnValue:e.value,returnValueIsError:!0}:{returnValue:e.value}}:{penpal:`reply`,id:e.callId,resolution:`fulfilled`,returnValue:e.value};throw Z(e)},ne=({messenger:e,methods:t,timeout:n,channel:r,log:i})=>{let a=z(),o,s=[],c=!1,l=P(t),{promise:u,resolve:d,reject:f}=G(),p=n===void 0?void 0:setTimeout(()=>{f(new y(`CONNECTION_TIMEOUT`,`Connection timed out after ${n}ms`))},n),m=()=>{for(let e of s)e()},h=()=>{if(c)return;s.push(R(e,t,r,i));let{remoteProxy:n,destroy:a}=W(e,r,i);s.push(a),clearTimeout(p),c=!0,d({remoteProxy:n,destroy:m})},g=()=>{let t={namespace:w,type:`SYN`,channel:r,participantId:a};i?.(`Sending handshake SYN`,t);try{e.sendMessage(t)}catch(e){f(new y(`TRANSMISSION_FAILED`,e.message))}},_=t=>{if(i?.(`Received handshake SYN`,t),t.participantId===o&&o!==K||(o=t.participantId,g(),!(a>o||o===K)))return;let n={namespace:w,channel:r,type:`ACK1`,methodPaths:l};i?.(`Sending handshake ACK1`,n);try{e.sendMessage(n)}catch(e){f(new y(`TRANSMISSION_FAILED`,e.message));return}},v=t=>{i?.(`Received handshake ACK1`,t);let n={namespace:w,channel:r,type:`ACK2`};i?.(`Sending handshake ACK2`,n);try{e.sendMessage(n)}catch(e){f(new y(`TRANSMISSION_FAILED`,e.message));return}h()},b=e=>{i?.(`Received handshake ACK2`,e),h()},x=e=>{O(e)&&_(e),k(e)&&v(e),A(e)&&b(e)};return e.addMessageHandler(x),s.push(()=>e.removeMessageHandler(x)),g(),u},re=e=>{let t=!1,n;return(...r)=>(t||(t=!0,n=e(...r)),n)},$=new WeakSet,ie=({messenger:e,methods:t={},timeout:n,channel:r,log:i})=>{if(!e)throw new y(`INVALID_ARGUMENT`,`messenger must be defined`);if($.has(e))throw new y(`INVALID_ARGUMENT`,`A messenger can only be used for a single connection`);$.add(e);let a=[e.destroy],o=re(t=>{if(t){let t={namespace:w,channel:r,type:`DESTROY`};try{e.sendMessage(t)}catch{}}for(let e of a)e();i?.(`Connection destroyed`)}),s=e=>D(e)&&e.channel===r;return{promise:(async()=>{try{e.initialize({log:i,validateReceivedMessage:s}),e.addMessageHandler(e=>{N(e)&&o(!1)});let{remoteProxy:c,destroy:l}=await ne({messenger:e,methods:t,timeout:n,channel:r,log:i});return a.push(l),c}catch(e){throw o(!0),e}})(),destroy:()=>{o(!0)}}},ae=(o=new WeakMap,s=new WeakMap,c=new WeakMap,l=new WeakMap,u=new WeakMap,d=new WeakMap,f=new WeakMap,p=new WeakMap,m=new WeakMap,h=new WeakMap,g=new WeakMap,_=new WeakMap,v=new WeakMap,class{constructor({remoteWindow:i,allowedOrigins:a}){if(n(this,o,void 0),n(this,s,void 0),n(this,c,void 0),n(this,l,void 0),n(this,u,void 0),n(this,d,new Set),n(this,f,void 0),n(this,p,!1),r(this,`initialize`,({log:n,validateReceivedMessage:r})=>{e(c,this,n),e(l,this,r),window.addEventListener(`message`,t(_,this))}),r(this,`sendMessage`,(n,r)=>{if(O(n)){let e=t(h,this).call(this,n);t(o,this).postMessage(n,{targetOrigin:e,transfer:r});return}if(k(n)||t(p,this)){let e=t(p,this)?te(n):n,i=t(h,this).call(this,n);t(o,this).postMessage(e,{targetOrigin:i,transfer:r});return}if(A(n)){let{port1:i,port2:a}=new MessageChannel;e(f,this,i),i.addEventListener(`message`,t(v,this)),i.start();let s=[a,...r||[]],c=t(h,this).call(this,n);t(o,this).postMessage(n,{targetOrigin:c,transfer:s});return}if(t(f,this)){t(f,this).postMessage(n,{transfer:r});return}throw new y(`TRANSMISSION_FAILED`,`Cannot send message because the MessagePort is not connected`)}),r(this,`addMessageHandler`,e=>{t(d,this).add(e)}),r(this,`removeMessageHandler`,e=>{t(d,this).delete(e)}),r(this,`destroy`,()=>{window.removeEventListener(`message`,t(_,this)),t(g,this).call(this),t(d,this).clear()}),n(this,m,e=>t(s,this).some(t=>t instanceof RegExp?t.test(e):t===e||t===`*`)),n(this,h,e=>{if(O(e))return`*`;if(!t(u,this))throw new y(`TRANSMISSION_FAILED`,`Cannot send message because the remote origin is not established`);return t(u,this)===`null`&&t(s,this).includes(`*`)?`*`:t(u,this)}),n(this,g,()=>{t(f,this)?.removeEventListener(`message`,t(v,this)),t(f,this)?.close(),e(f,this,void 0)}),n(this,_,({source:n,origin:r,ports:i,data:a})=>{if(n===t(o,this)){if(q(a)){t(c,this)?.call(this,`Please upgrade the child window to the latest version of Penpal.`),e(p,this,!0);try{a=Q(a)}catch(e){t(c,this)?.call(this,`Failed to translate deprecated message: ${e.message}`);return}}if(t(l,this)?.call(this,a)){if(!t(m,this).call(this,r)){t(c,this)?.call(this,`Received a message from origin \`${r}\` which did not match allowed origins \`[${t(s,this).join(`, `)}]\``);return}if(O(a)&&(t(g,this).call(this),e(u,this,r)),A(a)&&!t(p,this)){if(e(f,this,i[0]),!t(f,this)){t(c,this)?.call(this,`Ignoring ACK2 because it did not include a MessagePort`);return}t(f,this).addEventListener(`message`,t(v,this)),t(f,this).start()}for(let e of t(d,this))e(a)}}}),n(this,v,({data:e})=>{if(t(l,this)?.call(this,e))for(let n of t(d,this))n(e)}),!i)throw new y(`INVALID_ARGUMENT`,`remoteWindow must be defined`);e(o,this,i),e(s,this,a?.length?a:[window.origin])}}),oe={ConnectionDestroyed:`CONNECTION_DESTROYED`,ConnectionTimeout:`CONNECTION_TIMEOUT`,InvalidArgument:`INVALID_ARGUMENT`,MethodCallTimeout:`METHOD_CALL_TIMEOUT`,MethodNotFound:`METHOD_NOT_FOUND`,TransmissionFailed:`TRANSMISSION_FAILED`};export{ie as i,y as n,ae as r,oe as t}; |