reflected 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/dist/async-CM1iXZPW.js +44 -0
  2. package/dist/async-OLiknCrf.js +41 -0
  3. package/dist/async.js +856 -1
  4. package/dist/broadcast-B4sl9DBp.js +24 -0
  5. package/dist/broadcast-eR5_7TCY.js +15 -0
  6. package/dist/broadcast.js +850 -1
  7. package/dist/ffi/async-0Q0H4a_N.js +41 -0
  8. package/dist/ffi/async-CbwemiL2.js +41 -0
  9. package/dist/ffi/async-D6Zj6i_Q.js +44 -0
  10. package/dist/ffi/async-DLrN8K6M.js +44 -0
  11. package/dist/ffi/broadcast-CKNplfox.js +15 -0
  12. package/dist/ffi/broadcast-CbbtYobq.js +25 -0
  13. package/dist/ffi/broadcast-DIucqhL0.js +25 -0
  14. package/dist/ffi/broadcast-uydYnG6E.js +15 -0
  15. package/dist/ffi/global-DzpOeFX2.js +14 -0
  16. package/dist/ffi/global-LTfnrHcF.js +11 -0
  17. package/dist/ffi/i32-Cw-Rqr5y.js +13 -0
  18. package/dist/ffi/main.js +650 -1
  19. package/dist/ffi/message-2Dqj3uZ3.js +15 -0
  20. package/dist/ffi/message-Co0Oew7f.js +25 -0
  21. package/dist/ffi/message-Cxjrx_su.js +25 -0
  22. package/dist/ffi/message-mK0eTb54.js +15 -0
  23. package/dist/ffi/sender-B7Xmjz4q.js +194 -0
  24. package/dist/ffi/sender-C0gu9RzR.js +194 -0
  25. package/dist/ffi/shared-5Nhc4gvB.js +175 -0
  26. package/dist/ffi/shared-B3KmQkGh.js +425 -0
  27. package/dist/ffi/shared-CjWXp6-D.js +171 -0
  28. package/dist/ffi/shared-Czh3AlGt.js +53 -0
  29. package/dist/ffi/shared-DZdgWBc6.js +53 -0
  30. package/dist/ffi/shared-array-buffer-BQgQXQvC.js +28 -0
  31. package/dist/ffi/shared-ddawRQD0.js +425 -0
  32. package/dist/ffi/views-C3SJnvMr.js +48 -0
  33. package/dist/ffi/views-DBBYng9N.js +52 -0
  34. package/dist/ffi/worker.js +616 -1
  35. package/dist/ffi/xhr-Be-3qHQb.js +101 -0
  36. package/dist/ffi/xhr-CKhf21P8.js +41 -0
  37. package/dist/ffi/xhr-CRJGE1xP.js +41 -0
  38. package/dist/ffi/xhr-DSsSGsb-.js +101 -0
  39. package/dist/i32-UV5fM9Tw.js +12 -0
  40. package/dist/index.js +57 -1
  41. package/dist/message-D_SM4ST4.js +24 -0
  42. package/dist/message-vCWcUEmt.js +15 -0
  43. package/dist/message.js +843 -1
  44. package/dist/proxy/main/async-8J3q5T6q.js +41 -0
  45. package/dist/proxy/main/async-Di4MuQWe.js +44 -0
  46. package/dist/proxy/main/broadcast-CR8vlkKk.js +24 -0
  47. package/dist/proxy/main/broadcast-LNW6nFVD.js +15 -0
  48. package/dist/proxy/main/channel-NcnPEVL4.js +110 -0
  49. package/dist/proxy/main/i32-UV5fM9Tw.js +12 -0
  50. package/dist/proxy/main/message-j-4BYSZ4.js +24 -0
  51. package/dist/proxy/main/message-uirwoNUQ.js +15 -0
  52. package/dist/proxy/main/proxy.js +106 -1
  53. package/dist/proxy/main/sender-Dkjjbv2Y.js +194 -0
  54. package/dist/proxy/main/shared-C3U1MNPd.js +53 -0
  55. package/dist/proxy/main/shared-CV7CPYuP.js +43 -0
  56. package/dist/proxy/main/shared-DdPca9Ke.js +431 -0
  57. package/dist/proxy/main/shared-array-buffer-BQgQXQvC.js +28 -0
  58. package/dist/proxy/main/xhr-BUjEbkCh.js +100 -0
  59. package/dist/proxy/main/xhr-WnkM0dVo.js +41 -0
  60. package/dist/proxy/worker/async-CZJaURwO.js +41 -0
  61. package/dist/proxy/worker/async-DaSAye0a.js +44 -0
  62. package/dist/proxy/worker/broadcast-DRVfswix.js +24 -0
  63. package/dist/proxy/worker/broadcast-fWsZlH8v.js +15 -0
  64. package/dist/proxy/worker/channel-NcnPEVL4.js +110 -0
  65. package/dist/proxy/worker/i32-UV5fM9Tw.js +12 -0
  66. package/dist/proxy/worker/message-Bots8UXM.js +15 -0
  67. package/dist/proxy/worker/message-sej0J6k5.js +24 -0
  68. package/dist/proxy/worker/proxy.js +79 -1
  69. package/dist/proxy/worker/sender-D5Lm-DmC.js +194 -0
  70. package/dist/proxy/worker/shared-CgOGpfKJ.js +431 -0
  71. package/dist/proxy/worker/shared-Cn1K0k8G.js +53 -0
  72. package/dist/proxy/worker/shared-DR7YYduq.js +41 -0
  73. package/dist/proxy/worker/shared-array-buffer-BQgQXQvC.js +28 -0
  74. package/dist/proxy/worker/xhr-CQDzS28h.js +100 -0
  75. package/dist/proxy/worker/xhr-DAgmljtu.js +41 -0
  76. package/dist/sender-BSx3lj4w.js +193 -0
  77. package/dist/shared-Ccm1cPah.js +125 -0
  78. package/dist/shared-DVDPRDN_.js +53 -0
  79. package/dist/shared-L0wiAfqu.js +431 -0
  80. package/dist/shared-array-buffer-BQgQXQvC.js +28 -0
  81. package/dist/sw.js +155 -1
  82. package/dist/with-resolvers-DsO_nhbc.js +24 -0
  83. package/dist/xhr-BZOjL92j.js +100 -0
  84. package/dist/xhr-Crh-419R.js +41 -0
  85. package/dist/xhr.js +909 -1
  86. package/package.json +1 -1
  87. package/reflected.tar.gz +0 -0
  88. package/dist/async-C2eqvaGm.js +0 -1
  89. package/dist/async-s2VCmNNG.js +0 -1
  90. package/dist/broadcast-CArZGSOF.js +0 -1
  91. package/dist/broadcast-f7NecPmf.js +0 -1
  92. package/dist/ffi/async-B_TI4xaG.js +0 -1
  93. package/dist/ffi/async-BeKYhv9s.js +0 -1
  94. package/dist/ffi/async-Dl9wCRDA.js +0 -1
  95. package/dist/ffi/async-F0rxZjV9.js +0 -1
  96. package/dist/ffi/broadcast-CBUwPa0e.js +0 -1
  97. package/dist/ffi/broadcast-Ce_cOurt.js +0 -1
  98. package/dist/ffi/broadcast-D0jZEoUb.js +0 -1
  99. package/dist/ffi/broadcast-D7p6b88J.js +0 -1
  100. package/dist/ffi/global-B3nXXiRL.js +0 -1
  101. package/dist/ffi/global-Bs8RLRoX.js +0 -1
  102. package/dist/ffi/i32-BkNtpHwD.js +0 -1
  103. package/dist/ffi/message-BdbPt0y5.js +0 -1
  104. package/dist/ffi/message-DNbm1E-6.js +0 -1
  105. package/dist/ffi/message-DW2TgKPr.js +0 -1
  106. package/dist/ffi/message-TyU9xCIl.js +0 -1
  107. package/dist/ffi/sender-C5EHIpuL.js +0 -1
  108. package/dist/ffi/sender-DBFDKow4.js +0 -1
  109. package/dist/ffi/shared-BfrghvHB.js +0 -1
  110. package/dist/ffi/shared-C2XCOXcu.js +0 -1
  111. package/dist/ffi/shared-Cs1s8cy4.js +0 -1
  112. package/dist/ffi/shared-DsgUjYDJ.js +0 -1
  113. package/dist/ffi/shared-DwSbyGXy.js +0 -1
  114. package/dist/ffi/shared-DyOzpRO0.js +0 -1
  115. package/dist/ffi/shared-array-buffer-cwdMr2mc.js +0 -1
  116. package/dist/ffi/views-CdOIrBQR.js +0 -1
  117. package/dist/ffi/views-Cr2pQ4B-.js +0 -1
  118. package/dist/ffi/xhr-BkOoxiJZ.js +0 -1
  119. package/dist/ffi/xhr-C598zZcp.js +0 -1
  120. package/dist/ffi/xhr-CasCWs_z.js +0 -1
  121. package/dist/ffi/xhr-DHyu_Jz0.js +0 -1
  122. package/dist/i32-C78nBJH2.js +0 -1
  123. package/dist/message-RcKwy8Ib.js +0 -1
  124. package/dist/message-l7W12V0c.js +0 -1
  125. package/dist/proxy/main/async-Bc0QcTaC.js +0 -1
  126. package/dist/proxy/main/async-CyZdYOvJ.js +0 -1
  127. package/dist/proxy/main/broadcast-BX9FmxMK.js +0 -1
  128. package/dist/proxy/main/broadcast-DXhTouQy.js +0 -1
  129. package/dist/proxy/main/channel-DQBWE7_E.js +0 -1
  130. package/dist/proxy/main/i32-C78nBJH2.js +0 -1
  131. package/dist/proxy/main/message-CbPHeJAY.js +0 -1
  132. package/dist/proxy/main/message-DxRlOVN7.js +0 -1
  133. package/dist/proxy/main/sender-CNwZwCuI.js +0 -1
  134. package/dist/proxy/main/shared-BoatDt-V.js +0 -1
  135. package/dist/proxy/main/shared-FMegJ6yb.js +0 -1
  136. package/dist/proxy/main/shared-array-buffer-cwdMr2mc.js +0 -1
  137. package/dist/proxy/main/shared-z_jGYqUa.js +0 -1
  138. package/dist/proxy/main/xhr-BaBkekSn.js +0 -1
  139. package/dist/proxy/main/xhr-Fg-l74MV.js +0 -1
  140. package/dist/proxy/worker/async-Do59YjcF.js +0 -1
  141. package/dist/proxy/worker/async-fqI-s7gn.js +0 -1
  142. package/dist/proxy/worker/broadcast-Bdt9gmj_.js +0 -1
  143. package/dist/proxy/worker/broadcast-D7rjohJL.js +0 -1
  144. package/dist/proxy/worker/channel-DQBWE7_E.js +0 -1
  145. package/dist/proxy/worker/i32-C78nBJH2.js +0 -1
  146. package/dist/proxy/worker/message-BvU2dKHz.js +0 -1
  147. package/dist/proxy/worker/message-DhtuceT9.js +0 -1
  148. package/dist/proxy/worker/sender-CWhM7QKB.js +0 -1
  149. package/dist/proxy/worker/shared-DDIlHBz4.js +0 -1
  150. package/dist/proxy/worker/shared-NtUlwI0Z.js +0 -1
  151. package/dist/proxy/worker/shared-QrUiXPgc.js +0 -1
  152. package/dist/proxy/worker/shared-array-buffer-cwdMr2mc.js +0 -1
  153. package/dist/proxy/worker/xhr-CK-Z6L0B.js +0 -1
  154. package/dist/proxy/worker/xhr-JKtROQ3y.js +0 -1
  155. package/dist/sender-D7tfUn3T.js +0 -1
  156. package/dist/shared-BCEcO4-0.js +0 -1
  157. package/dist/shared-BWvJKuCK.js +0 -1
  158. package/dist/shared-BhhIigP4.js +0 -1
  159. package/dist/shared-array-buffer-cwdMr2mc.js +0 -1
  160. package/dist/with-resolvers-CHEvl4oe.js +0 -1
  161. package/dist/xhr-Bd0lMv0-.js +0 -1
  162. package/dist/xhr-DaixJn9d.js +0 -1
package/dist/broadcast.js CHANGED
@@ -1 +1,850 @@
1
- const e=Promise.withResolvers||function(){var e,t,s=new this((s,n)=>{e=s,t=n});return{resolve:e,reject:t,promise:s}};var t=e.bind(Promise),s="fc260aad-4404-43b8-ae9d-2c06554bb294";const n=2*Int32Array.BYTES_PER_ELEMENT;let r=!0;try{crypto.randomUUID()}catch(e){r=!1}const a=e=>e,o=r?()=>crypto.randomUUID():()=>(Date.now()+Math.random()).toString(36),{isArray:c}=Array;class i extends Worker{#e;#t;constructor(e,t){const n=t.onsend??a;super(e,t),this.#e=(()=>{const e=new Int32Array(1);return()=>e[0]++})(),this.#t=new Map,super.addEventListener("message",async e=>{const{data:t}=e;if(c(t)&&2===t.length&&t[0]===s){e.stopImmediatePropagation(),e.preventDefault();const[s,r]=t[1],a=this.#t.get(s);this.#t.delete(s),a(await n(r))}})}send(e,...n){const r=this.#e(),{promise:a,resolve:o}=t();return this.#t.set(r,o),super.postMessage([s,[r,e]],...n),a}}let u=0;const l=u++,h=u++,f=u++,p=u++,y=u++,g=u++,d=u++,b=u++,w=u++,m=u++,v=u++,A=u++,k=u++,S=u++,B=u++,U=u++,E=u++,M=u++,x=u++,I=u++,L=u++,O=u++,T=u++,D=u++,P=u++,F=u++,N=u++,R=u++;const j=globalThis.ImageData||class{};class q{static push(e,t){e.sync(!1),e._(t,t.length)}constructor(e,t){const s=[];this.l=0,this.o=s,this.v=new Uint8Array(e,t),this.push=s.push.bind(s)}get length(){return this.l+this.o.length}sync(e){const t=this.o,s=t.length;s&&this._(e?t:t.splice(0),s)}_(e,t){const{buffer:s,byteOffset:n}=this.v,r=this.l;this.l+=t,t+=n+r,s.byteLength<t&&s.grow(t),this.v.set(e,r)}}const _=new Map(Reflect.ownKeys(Symbol).map(e=>[Symbol[e],`@${String(e)}`])),z=e=>_.get(e)||((e,t)=>void 0===t?"?":void 0===Symbol.keyFor(e)?`!${t}`:`#${t}`)(e,e.description),$=Object.defineProperty,C=Array.isArray,J=ArrayBuffer.isView,K=new TextDecoder,V=new TextEncoder,W=new ArrayBuffer(8),Y=new DataView(W),G=new Uint8Array(W),{getPrototypeOf:H}=Object,{construct:Q}=Reflect,{toStringTag:X}=Symbol,Z=(e,t=e[X])=>t in globalThis?t:Z(Q(H(e.constructor),[0])),{isNaN:ee,isFinite:te,isInteger:se}=Number,{ownKeys:ne}=Reflect,{is:re}=Object,ae=(e,t,s)=>{const n=s.get(e),r=!n;return r?(Y.setUint32(0,t.length,!0),s.set(e,[G[0],G[1],G[2],G[3]])):t.push(R,n[0],n[1],n[2],n[3]),r},oe=(e,t,s)=>{Y.setUint32(0,s,!0),e.push(t,G[0],G[1],G[2],G[3])},ce=(e,t,s)=>{switch(typeof e){case"number":e&&te(e)?se(e)&&e<256&&-1<e?t.push(g,e):(Y.setFloat64(0,e,!0),t.push(y,G[0],G[1],G[2],G[3],G[4],G[5],G[6],G[7])):ee(e)?t.push(d):e?t.push(e<0?w:b):t.push(re(e,0)?m:v);break;case"object":switch(!0){case null===e:t.push(p);break;case!ae(e,t,s):break;case C(e):{const n=e.length;oe(t,U,n);for(let r=0;r<n;r++)ce(e[r],t,s);break}case J(e):if(t.push(D),ce(Z(e),t,s),e=e.buffer,!ae(e,t,s))break;case e instanceof ArrayBuffer:{const s=new Uint8Array(e);oe(t,E,s.length),ue(t,s);break}case e instanceof Date:t.push(M),ce(e.getTime(),t,s);break;case e instanceof Map:oe(t,I,e.size);for(const[n,r]of e)ce(n,t,s),ce(r,t,s);break;case e instanceof Set:oe(t,T,e.size);for(const n of e)ce(n,t,s);break;case e instanceof Error:t.push(x),ce(e.name,t,s),ce(e.message,t,s),ce(e.stack,t,s);break;case e instanceof j:t.push(P),ce(e.data,t,s),ce(e.width,t,s),ce(e.height,t,s),ce(e.colorSpace,t,s),ce(e.pixelFormat,t,s);break;case e instanceof RegExp:t.push(O),ce(e.source,t,s),ce(e.flags,t,s);break;case e instanceof File:t.push(N),ce(e.name,t,s),ce(e.lastModified,t,s);case e instanceof Blob:{const n=e.size;t.push(F),ce(e.type,t,s),ce(n,t,s);const r=t.length;ue(t,new Uint8Array(n)),ie.push(e.arrayBuffer().then(e=>[r,e]));break}default:if("toJSON"in e){const n=e.toJSON();ce(n===e?null:n,t,s)}else{const n=ne(e),r=n.length;oe(t,L,r);for(let a=0;a<r;a++){const r=n[a];ce(r,t,s),ce(e[r],t,s)}}}break;case"string":if(ae(e,t,s)){const s=V.encode(e);oe(t,S,s.length),ue(t,s)}break;case"boolean":t.push(e?h:l);break;case"symbol":t.push(B),ce(z(e),t,s);break;case"bigint":{let s=A;9223372036854775807n<e?(Y.setBigUint64(0,e,!0),s=k):Y.setBigInt64(0,e,!0),t.push(s,G[0],G[1],G[2],G[3],G[4],G[5],G[6],G[7]);break}default:t.push(f)}},ie=[];let ue=(e,t)=>{for(let s=e.push,n=0,r=t.length;n<r;n+=32767)s.apply(e,t.subarray(n,n+32767))};const le=({byteOffset:e=0,Array:t=q}={})=>(s,n)=>{const r=new t(n,e);ue=t.push,ce(s,r,new Map);const a=r.length;return r.sync(!0),ie.length?Promise.all(ie.splice(0)).then(t=>{const s=new Uint8Array(n,e);for(const[e,n]of t)s.set(new Uint8Array(n),e);return a}):a},{notify:he,store:fe}=Atomics,pe=(e,t,s)=>{const r=new Int32Array(e),o=(t.encoder??le)({byteOffset:n}),c=t.onsync??a,i=e=>{fe(r,1,e),fe(r,0,1),he(r,0)};return async({data:t})=>(t=>{const s=o(t,e);return"number"==typeof s?i(s):s.then(i)})(await c(t))},ye=e=>{switch(typeof e){case"symbol":case"function":return!1}return!0},ge="broadcast";var de=(e=>(s,n)=>{const{promise:r,resolve:a}=t();return new e(s,n,a),r})(class extends i{constructor(e,t,s){const r=o(),a=new BroadcastChannel(r),c=(({initByteLength:e=1024,maxByteLength:t=8192})=>new SharedArrayBuffer(n+e,{maxByteLength:n+t}))(t);a.addEventListener("message",pe(c,t)),super(...((e,t,s)=>{const n=new URL(e,location.href);return n.searchParams.set("reflected",t),[n,{...s,type:"module"}]})(e,ge,t)),super.addEventListener("message",()=>s(this),{once:!0}),super.postMessage(((e,t)=>{const s={};for(const e in t){const n=t[e];ye(e)&&ye(n)&&(s[e]=n)}return[e,s]})(c,t).concat(r))}get channel(){return ge}});const be=(e,t,s)=>(e.set(t,s),s),we=e=>{G[0]=e[Ae++],G[1]=e[Ae++],G[2]=e[Ae++],G[3]=e[Ae++],G[4]=e[Ae++],G[5]=e[Ae++],G[6]=e[Ae++],G[7]=e[Ae++]},me=e=>(G[0]=e[Ae++],G[1]=e[Ae++],G[2]=e[Ae++],G[3]=e[Ae++],Y.getUint32(0,!0)),ve=(e,t)=>{switch(e[Ae++]){case y:return we(e),Y.getFloat64(0,!0);case g:return e[Ae++];case L:{const s=be(t,Ae-1,{});for(let n=0,r=me(e);n<r;n++)s[ve(e,t)]=ve(e,t);return s}case U:{const s=be(t,Ae-1,[]);for(let n=0,r=me(e);n<r;n++)s.push(ve(e,t));return s}case D:{const s=Ae-1,n=ve(e,t);return be(t,s,new globalThis[n](ve(e,t)))}case E:{const s=Ae-1,n=me(e);return be(t,s,e.slice(Ae,Ae+=n).buffer)}case S:{const s=Ae-1,n=me(e);return be(t,s,K.decode(e.slice(Ae,Ae+=n)))}case M:return be(t,Ae-1,new Date(ve(e,t)));case I:{const s=be(t,Ae-1,new Map);for(let n=0,r=me(e);n<r;n++)s.set(ve(e,t),ve(e,t));return s}case T:{const s=be(t,Ae-1,new Set);for(let n=0,r=me(e);n<r;n++)s.add(ve(e,t));return s}case x:{const s=Ae-1,n=ve(e,t),r=ve(e,t),a=ve(e,t),o=new(globalThis[n]||Error)(r);return be(t,s,$(o,"stack",{value:a}))}case P:{const s=Ae-1,n=ve(e,t),r=ve(e,t),a=ve(e,t),o=ve(e,t),c=ve(e,t);return be(t,s,new j(n,r,a,{colorSpace:o,pixelFormat:c}))}case O:{const s=Ae-1,n=ve(e,t),r=ve(e,t);return be(t,s,new RegExp(n,r))}case l:return!1;case h:return!0;case d:return NaN;case b:return 1/0;case w:return-1/0;case m:return 0;case v:return-0;case p:return null;case A:return we(e),Y.getBigInt64(0,!0);case k:return we(e),Y.getBigUint64(0,!0);case B:return(e=>{switch(e[0]){case"@":return Symbol[e.slice(1)];case"#":return Symbol.for(e.slice(1));case"!":return Symbol(e.slice(1));default:return Symbol()}})(ve(e,t));case R:return t.get(me(e));case F:{const s=Ae-1,n=ve(e,t),r=ve(e,t);return be(t,s,new Blob([e.slice(Ae,Ae+=r)],{type:n}))}case N:{const s=Ae-1,n=ve(e,t),r=ve(e,t),a=ve(e,t);return be(t,s,new File([a],n,{type:a.type,lastModified:r}))}default:return}};let Ae=0;const ke=({byteOffset:e=0}={})=>(t,s)=>{return n=new Uint8Array(s,e,t),Ae=0,ve(n,new Map);var n},{isArray:Se}=Array;const{load:Be,store:Ue,wait:Ee}=Atomics,{promise:Me,resolve:xe}=t(),Ie="broadcast";var Le=((e,t)=>(addEventListener("message",t,{once:!0}),t=>e.then(([e,r,o])=>(postMessage(1),((e,t,s)=>{const r=(s.decoder??ke)({byteOffset:n}),o=s.onsync??a,{buffer:c}=t;return(s,...n)=>{e.postMessage(s,...n),Ee(t,0,0),Ue(t,0,0);const a=Be(t,1);return o(a?r(a,c):void 0)}})(o,new Int32Array(e),(e=>{const t=e.onsend??a;return addEventListener("message",async e=>{const{data:n}=e;if(Se(n)&&n[0]===s){e.stopImmediatePropagation(),e.preventDefault();const[r,a]=n[1];postMessage([s,[r,await t(a)]])}}),e})({...r,...t}))))))(Me,({data:[e,t,s]})=>xe([e,t,new BroadcastChannel(s)])),Oe="importScripts"in globalThis?Le:de;export{Ie as channel,Oe as default};
1
+ //@ts-check
2
+
3
+ /**
4
+ * @template T
5
+ * @typedef {{promise: Promise<T>, resolve: (value: T) => void, reject: (reason?: any) => void}} Resolvers
6
+ */
7
+
8
+ // fallback for Android WebView
9
+ //@ts-ignore
10
+ const withResolvers = Promise.withResolvers || function withResolvers() {
11
+ var a, b, c = new this((resolve, reject) => {
12
+ a = resolve;
13
+ b = reject;
14
+ });
15
+ return {resolve: a, reject: b, promise: c};
16
+ };
17
+
18
+ /**
19
+ * @template T
20
+ * @type {() => Resolvers<T>}
21
+ */
22
+ var withResolvers$1 = withResolvers.bind(Promise);
23
+
24
+ // @ts-check
25
+
26
+ /**
27
+ * @param {number} [start]
28
+ * @returns {() => number}
29
+ */
30
+ var i32 = start => {
31
+ const i32 = new Int32Array(1);
32
+ return () => i32[0]++;
33
+ };
34
+
35
+ // shorter: 'fc260aad-4404-43b8-ae9d-2c06554bb294'.split('-').map(s => parseInt(s, 16)).reduce((p, c) => p + c).toString(16)
36
+ var SHARED_CHANNEL = 'fc260aad-4404-43b8-ae9d-2c06554bb294';
37
+
38
+ const byteOffset = Int32Array.BYTES_PER_ELEMENT * 2;
39
+
40
+ let hasRandomUUID = true;
41
+ try {
42
+ crypto.randomUUID();
43
+ } catch (_) {
44
+ hasRandomUUID = false;
45
+ }
46
+
47
+ const identity = value => value;
48
+
49
+ const randomUUID = hasRandomUUID ?
50
+ (() => crypto.randomUUID() ):
51
+ (() => (Date.now() + Math.random()).toString(36));
52
+
53
+ const { isArray: isArray$2 } = Array;
54
+
55
+ class Sender extends Worker {
56
+ #next;
57
+ #requests;
58
+ constructor(scriptURL, options) {
59
+ const onsend = options.onsend ?? identity;
60
+ super(scriptURL, options);
61
+ this.#next = i32();
62
+ this.#requests = new Map;
63
+ super.addEventListener('message', async event => {
64
+ const { data } = event;
65
+ if (isArray$2(data) && data.length === 2 && data[0] === SHARED_CHANNEL) {
66
+ event.stopImmediatePropagation();
67
+ event.preventDefault();
68
+ const [id, payload] = data[1];
69
+ const resolve = this.#requests.get(id);
70
+ this.#requests.delete(id);
71
+ resolve(await onsend(payload));
72
+ }
73
+ });
74
+ }
75
+
76
+ send(payload, ...rest) {
77
+ const id = this.#next();
78
+ const { promise, resolve } = withResolvers$1();
79
+ this.#requests.set(id, resolve);
80
+ super.postMessage([SHARED_CHANNEL, [id, payload]], ...rest);
81
+ return promise;
82
+ }
83
+ }
84
+
85
+ let i$1 = 0;
86
+
87
+ const FALSE = i$1++;
88
+ const TRUE = i$1++;
89
+
90
+ const UNDEFINED = i$1++;
91
+ const NULL = i$1++;
92
+
93
+ const NUMBER = i$1++;
94
+ const UI8 = i$1++;
95
+ const NAN = i$1++;
96
+ const INFINITY = i$1++;
97
+ const N_INFINITY = i$1++;
98
+ const ZERO = i$1++;
99
+ const N_ZERO = i$1++;
100
+
101
+ const BIGINT = i$1++;
102
+ const BIGUINT = i$1++;
103
+
104
+ const STRING = i$1++;
105
+
106
+ const SYMBOL = i$1++;
107
+
108
+ const ARRAY = i$1++;
109
+ const BUFFER = i$1++;
110
+ const DATE = i$1++;
111
+ const ERROR = i$1++;
112
+ const MAP = i$1++;
113
+ const OBJECT = i$1++;
114
+ const REGEXP = i$1++;
115
+ const SET = i$1++;
116
+ const VIEW = i$1++;
117
+
118
+ const IMAGE_DATA = i$1++;
119
+ const BLOB = i$1++;
120
+ const FILE = i$1++;
121
+
122
+ const RECURSION = i$1++;
123
+
124
+ class Never {}
125
+
126
+ const ImageData = globalThis.ImageData || /** @type {typeof ImageData} */(Never);
127
+
128
+ // This is an Array facade for the encoder.
129
+
130
+ class Stack {
131
+ /**
132
+ * @param {Stack} self
133
+ * @param {Uint8Array} value
134
+ */
135
+ static push(self, value) {
136
+ self.sync(false);
137
+ self._(value, value.length);
138
+ }
139
+
140
+ /**
141
+ * @param {ArrayBufferLike} buffer
142
+ * @param {number} offset
143
+ */
144
+ constructor(buffer, offset) {
145
+ /** @type {number[]} */
146
+ const output = [];
147
+
148
+ /** @private length */
149
+ this.l = 0;
150
+
151
+ /** @private output */
152
+ this.o = output;
153
+
154
+ /** @private view */
155
+ this.v = new Uint8Array(buffer, offset);
156
+
157
+ /** @type {typeof Array.prototype.push} */
158
+ this.push = output.push.bind(output);
159
+ }
160
+
161
+ /**
162
+ * @type {number}
163
+ */
164
+ get length() {
165
+ return this.l + this.o.length;
166
+ }
167
+
168
+ /**
169
+ * Sync all entries in the output to the buffer.
170
+ * @param {boolean} last `true` if it's the last sync.
171
+ */
172
+ sync(last) {
173
+ const output = this.o;
174
+ const length = output.length;
175
+ if (length) this._(last ? output : output.splice(0), length);
176
+ }
177
+
178
+ /**
179
+ * Set a value to the buffer
180
+ * @private
181
+ * @param {Uint8Array|number[]} value
182
+ * @param {number} byteLength
183
+ */
184
+ _(value, byteLength) {
185
+ const { buffer, byteOffset } = this.v;
186
+ const offset = this.l;
187
+ this.l += byteLength;
188
+ byteLength += byteOffset + offset;
189
+ if (buffer.byteLength < byteLength)
190
+ /** @type {SharedArrayBuffer} */(buffer).grow(byteLength);
191
+ this.v.set(value, offset);
192
+ }
193
+ }
194
+
195
+ /** @type {Map<symbol, string>} */
196
+ const symbols = new Map(
197
+ Reflect.ownKeys(Symbol).map(
198
+ key => [Symbol[key], `@${String(key)}`]
199
+ )
200
+ );
201
+
202
+ /**
203
+ * @param {symbol} value
204
+ * @param {string} description
205
+ * @returns {string}
206
+ */
207
+ const asSymbol = (value, description) => (
208
+ description === void 0 ? '?' :
209
+ (Symbol.keyFor(value) === void 0 ? `!${description}` : `#${description}`)
210
+ );
211
+
212
+ /**
213
+ * Extract the value from a pair of type and value.
214
+ * @param {string} name
215
+ * @returns {symbol}
216
+ */
217
+ const fromSymbol = name => {
218
+ switch (name[0]) {
219
+ case '@': return Symbol[name.slice(1)];
220
+ case '#': return Symbol.for(name.slice(1));
221
+ case '!': return Symbol(name.slice(1));
222
+ default: return Symbol();
223
+ }
224
+ };
225
+
226
+ /**
227
+ * Create the name of a symbol.
228
+ * @param {symbol} value
229
+ * @returns {string}
230
+ */
231
+ const toSymbol = value => symbols.get(value) || asSymbol(value, value.description);
232
+
233
+ const defineProperty = Object.defineProperty;
234
+
235
+ const isArray$1 = Array.isArray;
236
+
237
+ const isView = ArrayBuffer.isView;
238
+
239
+ const MAX_ARGS = 0x7FFF;
240
+
241
+ /**
242
+ * @param {number[]} output
243
+ * @param {Uint8Array} value
244
+ */
245
+ const push = (output, value) => {
246
+ for (let $ = output.push, i = 0, length = value.length; i < length; i += MAX_ARGS)
247
+ $.apply(output, value.subarray(i, i + MAX_ARGS));
248
+ };
249
+
250
+ const decoder$1 = new TextDecoder;
251
+
252
+ const encoder$1 = new TextEncoder;
253
+
254
+ const buffer = new ArrayBuffer(8);
255
+ const dv = new DataView(buffer);
256
+ const u8a8 = new Uint8Array(buffer);
257
+
258
+ const { getPrototypeOf } = Object;
259
+ const { construct } = Reflect;
260
+ const { toStringTag } = Symbol;
261
+
262
+ const toTag = (ref, name = ref[toStringTag]) =>
263
+ name in globalThis ? name : toTag(construct(getPrototypeOf(ref.constructor),[0]));
264
+
265
+ //@ts-check
266
+
267
+
268
+ /** @typedef {Map<number, number[]>} Cache */
269
+
270
+ const { isNaN, isFinite, isInteger } = Number;
271
+ const { ownKeys } = Reflect;
272
+ const { is } = Object;
273
+
274
+ /**
275
+ * @param {any} input
276
+ * @param {number[]|Stack} output
277
+ * @param {Cache} cache
278
+ * @returns {boolean}
279
+ */
280
+ const process = (input, output, cache) => {
281
+ const value = cache.get(input);
282
+ const unknown = !value;
283
+ if (unknown) {
284
+ dv.setUint32(0, output.length, true);
285
+ cache.set(input, [u8a8[0], u8a8[1], u8a8[2], u8a8[3]]);
286
+ }
287
+ else
288
+ output.push(RECURSION, value[0], value[1], value[2], value[3]);
289
+ return unknown;
290
+ };
291
+
292
+ /**
293
+ * @param {number[]|Stack} output
294
+ * @param {number} type
295
+ * @param {number} length
296
+ */
297
+ const set = (output, type, length) => {
298
+ dv.setUint32(0, length, true);
299
+ output.push(type, u8a8[0], u8a8[1], u8a8[2], u8a8[3]);
300
+ };
301
+
302
+ /**
303
+ * @param {any} input
304
+ * @param {number[]|Stack} output
305
+ * @param {Cache} cache
306
+ */
307
+ const inflate = (input, output, cache) => {
308
+ switch (typeof input) {
309
+ case 'number': {
310
+ if (input && isFinite(input)) {
311
+ if (isInteger(input) && input < 256 && -1 < input)
312
+ output.push(UI8, input);
313
+ else {
314
+ dv.setFloat64(0, input, true);
315
+ output.push(NUMBER, u8a8[0], u8a8[1], u8a8[2], u8a8[3], u8a8[4], u8a8[5], u8a8[6], u8a8[7]);
316
+ }
317
+ }
318
+ else if (isNaN(input)) output.push(NAN);
319
+ else if (!input) output.push(is(input, 0) ? ZERO : N_ZERO);
320
+ else output.push(input < 0 ? N_INFINITY : INFINITY);
321
+ break;
322
+ }
323
+ case 'object': {
324
+ switch (true) {
325
+ case input === null:
326
+ output.push(NULL);
327
+ break;
328
+ case !process(input, output, cache): break;
329
+ case isArray$1(input): {
330
+ const length = input.length;
331
+ set(output, ARRAY, length);
332
+ for (let i = 0; i < length; i++)
333
+ inflate(input[i], output, cache);
334
+ break;
335
+ }
336
+ case isView(input): {
337
+ output.push(VIEW);
338
+ inflate(toTag(input), output, cache);
339
+ input = input.buffer;
340
+ if (!process(input, output, cache)) break;
341
+ // fallthrough
342
+ }
343
+ case input instanceof ArrayBuffer: {
344
+ const ui8a = new Uint8Array(input);
345
+ set(output, BUFFER, ui8a.length);
346
+ //@ts-ignore
347
+ pushView(output, ui8a);
348
+ break;
349
+ }
350
+ case input instanceof Date:
351
+ output.push(DATE);
352
+ inflate(input.getTime(), output, cache);
353
+ break;
354
+ case input instanceof Map: {
355
+ set(output, MAP, input.size);
356
+ for (const [key, value] of input) {
357
+ inflate(key, output, cache);
358
+ inflate(value, output, cache);
359
+ }
360
+ break;
361
+ }
362
+ case input instanceof Set: {
363
+ set(output, SET, input.size);
364
+ for (const value of input)
365
+ inflate(value, output, cache);
366
+ break;
367
+ }
368
+ case input instanceof Error:
369
+ output.push(ERROR);
370
+ inflate(input.name, output, cache);
371
+ inflate(input.message, output, cache);
372
+ inflate(input.stack, output, cache);
373
+ break;
374
+ /* c8 ignore start */
375
+ case input instanceof ImageData:
376
+ output.push(IMAGE_DATA);
377
+ inflate(input.data, output, cache);
378
+ inflate(input.width, output, cache);
379
+ inflate(input.height, output, cache);
380
+ inflate(input.colorSpace, output, cache);
381
+ //@ts-ignore
382
+ inflate(input.pixelFormat, output, cache);
383
+ break;
384
+ /* c8 ignore stop */
385
+ case input instanceof RegExp:
386
+ output.push(REGEXP);
387
+ inflate(input.source, output, cache);
388
+ inflate(input.flags, output, cache);
389
+ break;
390
+ case input instanceof File: {
391
+ output.push(FILE);
392
+ inflate(input.name, output, cache);
393
+ inflate(input.lastModified, output, cache);
394
+ // fallthrough
395
+ }
396
+ case input instanceof Blob: {
397
+ const size = input.size;
398
+ output.push(BLOB);
399
+ inflate(input.type, output, cache);
400
+ inflate(size, output, cache);
401
+ const length = output.length;
402
+ //@ts-ignore
403
+ pushView(output, new Uint8Array(size));
404
+ blobs.push(input.arrayBuffer().then(buffer => [length, buffer]));
405
+ break;
406
+ }
407
+ default: {
408
+ if ('toJSON' in input) {
409
+ const json = input.toJSON();
410
+ inflate(json === input ? null : json, output, cache);
411
+ }
412
+ else {
413
+ const keys = ownKeys(input);
414
+ const length = keys.length;
415
+ set(output, OBJECT, length);
416
+ for (let i = 0; i < length; i++) {
417
+ const key = keys[i];
418
+ inflate(key, output, cache);
419
+ inflate(input[key], output, cache);
420
+ }
421
+ }
422
+ break;
423
+ }
424
+ }
425
+ break;
426
+ }
427
+ case 'string': {
428
+ if (process(input, output, cache)) {
429
+ const encoded = encoder$1.encode(input);
430
+ set(output, STRING, encoded.length);
431
+ //@ts-ignore
432
+ pushView(output, encoded);
433
+ }
434
+ break;
435
+ }
436
+ case 'boolean': {
437
+ output.push(input ? TRUE : FALSE);
438
+ break;
439
+ }
440
+ case 'symbol': {
441
+ output.push(SYMBOL);
442
+ inflate(toSymbol(input), output, cache);
443
+ break;
444
+ }
445
+ case 'bigint': {
446
+ let type = BIGINT;
447
+ if (9223372036854775807n < input) {
448
+ dv.setBigUint64(0, input, true);
449
+ type = BIGUINT;
450
+ }
451
+ else dv.setBigInt64(0, input, true);
452
+ output.push(type, u8a8[0], u8a8[1], u8a8[2], u8a8[3], u8a8[4], u8a8[5], u8a8[6], u8a8[7]);
453
+ break;
454
+ }
455
+ // this covers functions too
456
+ default: {
457
+ output.push(UNDEFINED);
458
+ break;
459
+ }
460
+ }
461
+ };
462
+
463
+ const blobs = [];
464
+
465
+ /** @type {typeof push|typeof Stack.push} */
466
+ let pushView = push;
467
+
468
+ /**
469
+ * @param {{ byteOffset?: number, Array?: typeof Stack }} [options]
470
+ * @returns {(value: any, buffer: ArrayBufferLike) => number | Promise<number>}
471
+ */
472
+ const encoder = ({ byteOffset = 0, Array = Stack } = {}) => (value, buffer) => {
473
+ const output = new Array(buffer, byteOffset);
474
+ pushView = Array.push;
475
+ inflate(value, output, new Map);
476
+ const length = output.length;
477
+ output.sync(true);
478
+ return blobs.length ?
479
+ Promise.all(blobs.splice(0)).then(entries => {
480
+ const ui8a = new Uint8Array(buffer, byteOffset);
481
+ for (const [len, buff] of entries)
482
+ ui8a.set(new Uint8Array(buff), len);
483
+ return length;
484
+ }) :
485
+ length;
486
+ };
487
+
488
+ const { notify, store: store$1 } = Atomics;
489
+
490
+ const SAB = ({
491
+ initByteLength = 1024,
492
+ maxByteLength = (1024 * 8)
493
+ }) =>
494
+ new SharedArrayBuffer(
495
+ byteOffset + initByteLength,
496
+ { maxByteLength: byteOffset + maxByteLength }
497
+ );
498
+
499
+ /**
500
+ * @typedef {Object} ServiceWorkerOptions see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#options
501
+ * @property {string} [url] will use the `serviceWorker` value if that is a `string`, otherwise it refers to where the service worker file is located.
502
+ * @property {'classic' | 'module'} [type]
503
+ * @property {'all' | 'imports' | 'none'} [updateViaCache]
504
+ */
505
+
506
+ /**
507
+ * @typedef {Object} Options
508
+ * @property {(payload: unknown) => Int32Array | Promise<Int32Array>} [onsync] invoked when the worker expect a response as `Int32Array` to populate the SharedArrayBuffer with.
509
+ * @property {(payload: unknown) => unknown | Promise<unknown>} [onsend] invoked when the worker replies to a `worker.send(data)` call.
510
+ * @property {string} [ws] the WebSocket URL to use for worker <-> server communication.
511
+ * @property {number} [initByteLength=1024] defines the initial byte length of the SharedArrayBuffer.
512
+ * @property {number} [maxByteLength=8192] defines the maximum byte length (growth) of the SharedArrayBuffer.
513
+ * @property {string | ServiceWorkerOptions} [serviceWorker] defines the service worker to use as fallback if SharedArrayBuffer is not supported. If not defined, the `async` fallback will be used so that no `sync` operations from the worker will be possible.
514
+ * @property {import('reflected-ffi/encoder').encoder} [encoder] defines the encoder function to use to encode the result into the SharedArrayBuffer.
515
+ */
516
+
517
+ /**
518
+ * Initialize the worker channel communication and resolves with the worker instance.
519
+ * @template T
520
+ * @param {T} Worker
521
+ * @returns
522
+ */
523
+ const bootstrap = Worker => {
524
+ /**
525
+ * @param {string} scriptURL
526
+ * @param {Options} options
527
+ * @returns
528
+ */
529
+ return (scriptURL, options) => {
530
+ const { promise, resolve } = withResolvers$1();
531
+ // @ts-ignore
532
+ new Worker(scriptURL, options, resolve);
533
+ return /** @type {Promise<T>} */(promise);
534
+ };
535
+ };
536
+
537
+ const handler$1 = (sab, options, useAtomics) => {
538
+ const i32a = new Int32Array(sab);
539
+ const encode = (options.encoder ?? encoder)({ byteOffset });
540
+ const onsync = options.onsync ?? identity;
541
+
542
+ const resolve = (length => {
543
+ store$1(i32a, 1, length);
544
+ store$1(i32a, 0, 1);
545
+ notify(i32a, 0);
546
+ }) ;
547
+
548
+ const process = result => {
549
+ const length = encode(result, sab);
550
+ return typeof length === 'number' ? resolve(length) : length.then(resolve);
551
+ };
552
+
553
+ return async ({ data }) => process(await onsync(data));
554
+ };
555
+
556
+ const isOK = value => {
557
+ switch (typeof value) {
558
+ case 'symbol':
559
+ case 'function':
560
+ return false;
561
+ }
562
+ return true;
563
+ };
564
+
565
+ const post = (sab, options) => {
566
+ const opts = {};
567
+ for (const key in options) {
568
+ const value = options[key];
569
+ if (isOK(key) && isOK(value)) opts[key] = value;
570
+ }
571
+ return [sab, opts];
572
+ };
573
+
574
+ const url = (scriptURL, reflected, options) => {
575
+ const url = new URL(scriptURL, location.href);
576
+ url.searchParams.set('reflected', reflected);
577
+ return [url, { ...options, type: 'module' }];
578
+ };
579
+
580
+ const CHANNEL = 'broadcast';
581
+
582
+ var main = bootstrap(class Worker extends Sender {
583
+ constructor(scriptURL, options, resolve) {
584
+ const channel = randomUUID();
585
+ const bc = new BroadcastChannel(channel);
586
+ const sab = SAB(options);
587
+ bc.addEventListener('message', handler$1(sab, options));
588
+ super(...url(scriptURL, CHANNEL, options));
589
+ super.addEventListener('message', () => resolve(this), { once: true });
590
+ super.postMessage(post(sab, options).concat(channel));
591
+ }
592
+
593
+ get channel() {
594
+ return CHANNEL;
595
+ }
596
+ });
597
+
598
+ //@ts-check
599
+
600
+
601
+ /** @typedef {Map<number, any>} Cache */
602
+
603
+ /**
604
+ * @param {Cache} cache
605
+ * @param {number} index
606
+ * @param {any} value
607
+ * @returns {any}
608
+ */
609
+ const $ = (cache, index, value) => {
610
+ cache.set(index, value);
611
+ return value;
612
+ };
613
+
614
+ /**
615
+ * @param {Uint8Array} input
616
+ */
617
+ const number = input => {
618
+ u8a8[0] = input[i++];
619
+ u8a8[1] = input[i++];
620
+ u8a8[2] = input[i++];
621
+ u8a8[3] = input[i++];
622
+ u8a8[4] = input[i++];
623
+ u8a8[5] = input[i++];
624
+ u8a8[6] = input[i++];
625
+ u8a8[7] = input[i++];
626
+ };
627
+
628
+ /**
629
+ * @param {Uint8Array} input
630
+ * @returns {number}
631
+ */
632
+ const size = input => {
633
+ u8a8[0] = input[i++];
634
+ u8a8[1] = input[i++];
635
+ u8a8[2] = input[i++];
636
+ u8a8[3] = input[i++];
637
+ return dv.getUint32(0, true);
638
+ };
639
+
640
+ /**
641
+ * @param {Uint8Array} input
642
+ * @param {Cache} cache
643
+ * @returns {any}
644
+ */
645
+ const deflate = (input, cache) => {
646
+ switch (input[i++]) {
647
+ case NUMBER: {
648
+ number(input);
649
+ return dv.getFloat64(0, true);
650
+ }
651
+ case UI8: return input[i++];
652
+ case OBJECT: {
653
+ const object = $(cache, i - 1, {});
654
+ for (let j = 0, length = size(input); j < length; j++)
655
+ object[deflate(input, cache)] = deflate(input, cache);
656
+ return object;
657
+ }
658
+ case ARRAY: {
659
+ const array = $(cache, i - 1, []);
660
+ for (let j = 0, length = size(input); j < length; j++)
661
+ array.push(deflate(input, cache));
662
+ return array;
663
+ }
664
+ case VIEW: {
665
+ const index = i - 1;
666
+ const name = deflate(input, cache);
667
+ return $(cache, index, new globalThis[name](deflate(input, cache)));
668
+ }
669
+ case BUFFER: {
670
+ const index = i - 1;
671
+ const length = size(input);
672
+ return $(cache, index, input.slice(i, i += length).buffer);
673
+ }
674
+ case STRING: {
675
+ const index = i - 1;
676
+ const length = size(input);
677
+ // this could be a subarray but it's not supported on the Web and
678
+ // it wouldn't work with arrays instead of typed arrays.
679
+ return $(cache, index, decoder$1.decode(input.slice(i, i += length)));
680
+ }
681
+ case DATE: {
682
+ return $(cache, i - 1, new Date(deflate(input, cache)));
683
+ }
684
+ case MAP: {
685
+ const map = $(cache, i - 1, new Map);
686
+ for (let j = 0, length = size(input); j < length; j++)
687
+ map.set(deflate(input, cache), deflate(input, cache));
688
+ return map;
689
+ }
690
+ case SET: {
691
+ const set = $(cache, i - 1, new Set);
692
+ for (let j = 0, length = size(input); j < length; j++)
693
+ set.add(deflate(input, cache));
694
+ return set;
695
+ }
696
+ case ERROR: {
697
+ const index = i - 1;
698
+ const name = deflate(input, cache);
699
+ const message = deflate(input, cache);
700
+ const stack = deflate(input, cache);
701
+ const Class = globalThis[name] || Error;
702
+ const error = new Class(message);
703
+ return $(cache, index, defineProperty(error, 'stack', { value: stack }));
704
+ }
705
+ /* c8 ignore start */
706
+ case IMAGE_DATA: {
707
+ const index = i - 1;
708
+ const data = deflate(input, cache);
709
+ const width = deflate(input, cache);
710
+ const height = deflate(input, cache);
711
+ const colorSpace = deflate(input, cache);
712
+ const pixelFormat = deflate(input, cache);
713
+ const settings = { colorSpace, pixelFormat };
714
+ return $(cache, index, new ImageData(data, width, height, settings));
715
+ }
716
+ /* c8 ignore stop */
717
+ case REGEXP: {
718
+ const index = i - 1;
719
+ const source = deflate(input, cache);
720
+ const flags = deflate(input, cache);
721
+ return $(cache, index, new RegExp(source, flags));
722
+ }
723
+ case FALSE: return false;
724
+ case TRUE: return true;
725
+ case NAN: return NaN;
726
+ case INFINITY: return Infinity;
727
+ case N_INFINITY: return -Infinity;
728
+ case ZERO: return 0;
729
+ case N_ZERO: return -0;
730
+ case NULL: return null;
731
+ case BIGINT: return (number(input), dv.getBigInt64(0, true));
732
+ case BIGUINT: return (number(input), dv.getBigUint64(0, true));
733
+ case SYMBOL: return fromSymbol(deflate(input, cache));
734
+ case RECURSION: return cache.get(size(input));
735
+ case BLOB: {
736
+ const index = i - 1;
737
+ const type = deflate(input, cache);
738
+ const size = deflate(input, cache);
739
+ return $(cache, index, new Blob([input.slice(i, i += size)], { type }));
740
+ }
741
+ case FILE: {
742
+ const index = i - 1;
743
+ const name = deflate(input, cache);
744
+ const lastModified = deflate(input, cache);
745
+ const blob = deflate(input, cache);
746
+ return $(cache, index, new File([blob], name, { type: blob.type, lastModified }));
747
+ }
748
+ // this covers functions too
749
+ default: return undefined;
750
+ }
751
+ };
752
+
753
+ let i = 0;
754
+
755
+ /**
756
+ * @param {Uint8Array} value
757
+ * @returns {any}
758
+ */
759
+ const decode = value => {
760
+ i = 0;
761
+ return deflate(value, new Map);
762
+ };
763
+
764
+ /**
765
+ * @param {{ byteOffset?: number }} [options]
766
+ * @returns {(length: number, buffer: ArrayBufferLike) => any}
767
+ */
768
+ const decoder = ({ byteOffset = 0 } = {}) => (length, buffer) => decode(
769
+ new Uint8Array(buffer, byteOffset, length)
770
+ );
771
+
772
+ const { isArray } = Array;
773
+
774
+ var sender = options => {
775
+ const onsend = options.onsend ?? identity;
776
+ addEventListener('message', async event => {
777
+ const { data } = event;
778
+ if (isArray(data) && data[0] === SHARED_CHANNEL) {
779
+ event.stopImmediatePropagation();
780
+ event.preventDefault();
781
+ const [id, payload] = data[1];
782
+ postMessage([SHARED_CHANNEL, [id, await onsend(payload)]]);
783
+ }
784
+ });
785
+ return options;
786
+ };
787
+
788
+ const { load, store, wait } = Atomics;
789
+
790
+ /**
791
+ * @typedef {Object} Options
792
+ * @property {(payload: Int32Array) => unknown} [onsync] transforms the resulting `Int32Array` from *main* thread into a value usable within the worker.
793
+ * @property {(payload: unknown) => unknown |Promise<unknown>} onsend invoked to define what to return to the *main* thread when it calls `worker.send(payload)`.
794
+ * @property {import('reflected-ffi/decoder').decoder} [decoder] defines the decoder function to use to decode the result from the SharedArrayBuffer.
795
+ */
796
+
797
+ /**
798
+ * @param {MessageChannel | BroadcastChannel} channel
799
+ * @param {Int32Array} i32a
800
+ * @param {Options} options
801
+ * @returns {(payload: unknown, ...rest: unknown[]) => unknown}
802
+ */
803
+ const handle = (channel, i32a, options) => {
804
+ const decode = (options.decoder ?? decoder)({ byteOffset });
805
+ const onsync = options.onsync ?? identity;
806
+ const { buffer } = i32a;
807
+ return (payload, ...rest) => {
808
+ // @ts-ignore
809
+ channel.postMessage(payload, ...rest);
810
+ wait(i32a, 0, 0);
811
+ store(i32a, 0, 0);
812
+ const length = load(i32a, 1);
813
+ return onsync(length ? decode(length, buffer) : void 0);
814
+ };
815
+ };
816
+
817
+ /**
818
+ *
819
+ * @param {Promise<[SharedArrayBuffer, Options, MessageChannel | BroadcastChannel]>} promise
820
+ * @param {(event:MessageEvent) => void} listener
821
+ * @returns
822
+ */
823
+ const handler = (promise, listener) => {
824
+ addEventListener('message', listener, { once: true });
825
+ /**
826
+ * @param {Options} options
827
+ * @returns
828
+ */
829
+ return options => promise.then(
830
+ ([sab, main, channel]) => {
831
+ postMessage(1);
832
+ return handle(channel, new Int32Array(sab), sender({ ...main, ...options }));
833
+ }
834
+ );
835
+ };
836
+
837
+ const { promise, resolve } = withResolvers$1();
838
+
839
+ const channel = 'broadcast';
840
+
841
+ var worker = handler(
842
+ promise,
843
+ ({ data: [sab, main, channel] }) => resolve([sab, main, new BroadcastChannel(channel)]),
844
+ );
845
+
846
+ var broadcast = (
847
+ 'importScripts' in globalThis ? worker : main
848
+ );
849
+
850
+ export { channel, broadcast as default };