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/xhr.js CHANGED
@@ -1 +1,909 @@
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=e=>{const t=new Int32Array(1);return()=>t[0]++},n="fc260aad-4404-43b8-ae9d-2c06554bb294";const r=2*Int32Array.BYTES_PER_ELEMENT;let a=!0;try{crypto.randomUUID()}catch(e){a=!1}const o=e=>e,c=a?()=>crypto.randomUUID():()=>(Date.now()+Math.random()).toString(36),{isArray:i}=Array;class l extends Worker{#e;#t;constructor(e,t){const r=t.onsend??o;super(e,t),this.#e=s(),this.#t=new Map,super.addEventListener("message",async e=>{const{data:t}=e;if(i(t)&&2===t.length&&t[0]===n){e.stopImmediatePropagation(),e.preventDefault();const[s,n]=t[1],a=this.#t.get(s);this.#t.delete(s),a(await r(n))}})}send(e,...s){const r=this.#e(),{promise:a,resolve:o}=t();return this.#t.set(r,o),super.postMessage([n,[r,e]],...s),a}}let{SharedArrayBuffer:u}=globalThis;try{new u(4,{maxByteLength:8})}catch(e){u=class extends ArrayBuffer{get growable(){return super.resizable}grow(e){super.resize(e)}}}var h=({initByteLength:e=1024,maxByteLength:t=8192})=>new u(r+e,{maxByteLength:r+t});let p=0;const f=p++,g=p++,d=p++,y=p++,w=p++,b=p++,m=p++,v=p++,A=p++,k=p++,S=p++,U=p++,x=p++,B=p++,M=p++,E=p++,L=p++,T=p++,I=p++,O=p++,R=p++,D=p++,P=p++,N=p++,q=p++,F=p++,j=p++,z=p++;const C=globalThis.ImageData||class{};class _{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 W=new Map(Reflect.ownKeys(Symbol).map(e=>[Symbol[e],`@${String(e)}`])),J=e=>W.get(e)||((e,t)=>void 0===t?"?":void 0===Symbol.keyFor(e)?`!${t}`:`#${t}`)(e,e.description),$=Object.defineProperty,H=Array.isArray,K=ArrayBuffer.isView,V=new TextDecoder,X=new TextEncoder,Y=new ArrayBuffer(8),G=new DataView(Y),Q=new Uint8Array(Y),{getPrototypeOf:Z}=Object,{construct:ee}=Reflect,{toStringTag:te}=Symbol,se=(e,t=e[te])=>t in globalThis?t:se(ee(Z(e.constructor),[0])),{isNaN:ne,isFinite:re,isInteger:ae}=Number,{ownKeys:oe}=Reflect,{is:ce}=Object,ie=(e,t,s)=>{const n=s.get(e),r=!n;return r?(G.setUint32(0,t.length,!0),s.set(e,[Q[0],Q[1],Q[2],Q[3]])):t.push(z,n[0],n[1],n[2],n[3]),r},le=(e,t,s)=>{G.setUint32(0,s,!0),e.push(t,Q[0],Q[1],Q[2],Q[3])},ue=(e,t,s)=>{switch(typeof e){case"number":e&&re(e)?ae(e)&&e<256&&-1<e?t.push(b,e):(G.setFloat64(0,e,!0),t.push(w,Q[0],Q[1],Q[2],Q[3],Q[4],Q[5],Q[6],Q[7])):ne(e)?t.push(m):e?t.push(e<0?A:v):t.push(ce(e,0)?k:S);break;case"object":switch(!0){case null===e:t.push(y);break;case!ie(e,t,s):break;case H(e):{const n=e.length;le(t,E,n);for(let r=0;r<n;r++)ue(e[r],t,s);break}case K(e):if(t.push(N),ue(se(e),t,s),e=e.buffer,!ie(e,t,s))break;case e instanceof ArrayBuffer:{const s=new Uint8Array(e);le(t,L,s.length),pe(t,s);break}case e instanceof Date:t.push(T),ue(e.getTime(),t,s);break;case e instanceof Map:le(t,O,e.size);for(const[n,r]of e)ue(n,t,s),ue(r,t,s);break;case e instanceof Set:le(t,P,e.size);for(const n of e)ue(n,t,s);break;case e instanceof Error:t.push(I),ue(e.name,t,s),ue(e.message,t,s),ue(e.stack,t,s);break;case e instanceof C:t.push(q),ue(e.data,t,s),ue(e.width,t,s),ue(e.height,t,s),ue(e.colorSpace,t,s),ue(e.pixelFormat,t,s);break;case e instanceof RegExp:t.push(D),ue(e.source,t,s),ue(e.flags,t,s);break;case e instanceof File:t.push(j),ue(e.name,t,s),ue(e.lastModified,t,s);case e instanceof Blob:{const n=e.size;t.push(F),ue(e.type,t,s),ue(n,t,s);const r=t.length;pe(t,new Uint8Array(n)),he.push(e.arrayBuffer().then(e=>[r,e]));break}default:if("toJSON"in e){const n=e.toJSON();ue(n===e?null:n,t,s)}else{const n=oe(e),r=n.length;le(t,R,r);for(let a=0;a<r;a++){const r=n[a];ue(r,t,s),ue(e[r],t,s)}}}break;case"string":if(ie(e,t,s)){const s=X.encode(e);le(t,B,s.length),pe(t,s)}break;case"boolean":t.push(e?g:f);break;case"symbol":t.push(M),ue(J(e),t,s);break;case"bigint":{let s=U;9223372036854775807n<e?(G.setBigUint64(0,e,!0),s=x):G.setBigInt64(0,e,!0),t.push(s,Q[0],Q[1],Q[2],Q[3],Q[4],Q[5],Q[6],Q[7]);break}default:t.push(d)}},he=[];let pe=(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 fe=({byteOffset:e=0,Array:t=_}={})=>(s,n)=>{const r=new t(n,e);pe=t.push,ue(s,r,new Map);const a=r.length;return r.sync(!0),he.length?Promise.all(he.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},ge=(e,t,s)=>{const n=new Int32Array(e),a=(t.encoder??fe)({byteOffset:r}),c=t.onsync??o,i=e=>{n[1]=e,n[0]=1};return async({data:t})=>(t=>{const s=a(t,e);return"number"==typeof s?i(s):s.then(i)})(await c(t))},de=e=>{switch(typeof e){case"symbol":case"function":return!1}return!0},ye=(e,t)=>{const s={};for(const e in t){const n=t[e];de(e)&&de(n)&&(s[e]=n)}return[e,s]},we=(e,t,s)=>{const n=new URL(e,location.href);return n.searchParams.set("reflected",t),[n,{...s,type:"module"}]},be="async";let me=class extends l{constructor(e,t,s){const n=c(),a=new BroadcastChannel(n),o=h(t),i=new Int32Array(o),l=ge(o,t);a.addEventListener("message",async({data:[e,t]})=>{await l({data:t}),a.postMessage([e,new Uint8Array(o,r,i[1])])}),super(...we(e,be,t)),super.addEventListener("message",()=>s(this),{once:!0}),super.postMessage(ye(o,t).concat(n))}get channel(){return be}};const ve=new Map,Ae=new BroadcastChannel(n);Ae.addEventListener("message",async({data:[e,t]})=>{if("request"===e){const[e,[s,n]]=t,r=ve.get(n);if(r){const t=r.get(s);r.delete(s),Ae.postMessage(["response",[e,await t]])}}});const{promise:ke,resolve:Se}=t();let Ue=!0;const xe=(e,t)=>{let s,n=!0,{url:r}=t;e.getRegistration(r).then(s=>s??e.register(r,t)).then(function a(o){const{controller:c}=e;if(n=n&&!!c,s=o.installing||o.waiting||o.active,!s)return xe(e,t);if("activated"===s.state){if(n){if(c.scriptURL===r)return Se();o.unregister()}location.reload()}else s.addEventListener("statechange",()=>a(o),{once:!0})})};let Be=class extends l{#s;constructor(e,s,n){if(Ue){Ue=!1;let{serviceWorker:t}=s;if(!t)return new me(e,s,n);"string"==typeof t&&(t={url:t}),t.url=new URL(t.url,location.href).href,xe(navigator.serviceWorker,t)}const a=c(),o=new BroadcastChannel(a),i=h(s),l=new Map,u=new Int32Array(i),p=ge(i,s);ve.set(a,l),o.addEventListener("message",async({data:[e,s]})=>{const{promise:n,resolve:a}=t();l.set(e,n),await p({data:s}),a(new Uint8Array(i,r,u[1]))}),super(...we(e,"xhr",s)),super.addEventListener("message",()=>n(this),{once:!0}),super.postMessage(ye(i,s).concat(a)),this.#s=a}terminate(){ve.delete(this.#s),super.terminate()}get channel(){return"xhr"}};const Me=(e,t,s)=>(e.set(t,s),s),Ee=e=>{Q[0]=e[Ie++],Q[1]=e[Ie++],Q[2]=e[Ie++],Q[3]=e[Ie++],Q[4]=e[Ie++],Q[5]=e[Ie++],Q[6]=e[Ie++],Q[7]=e[Ie++]},Le=e=>(Q[0]=e[Ie++],Q[1]=e[Ie++],Q[2]=e[Ie++],Q[3]=e[Ie++],G.getUint32(0,!0)),Te=(e,t)=>{switch(e[Ie++]){case w:return Ee(e),G.getFloat64(0,!0);case b:return e[Ie++];case R:{const s=Me(t,Ie-1,{});for(let n=0,r=Le(e);n<r;n++)s[Te(e,t)]=Te(e,t);return s}case E:{const s=Me(t,Ie-1,[]);for(let n=0,r=Le(e);n<r;n++)s.push(Te(e,t));return s}case N:{const s=Ie-1,n=Te(e,t);return Me(t,s,new globalThis[n](Te(e,t)))}case L:{const s=Ie-1,n=Le(e);return Me(t,s,e.slice(Ie,Ie+=n).buffer)}case B:{const s=Ie-1,n=Le(e);return Me(t,s,V.decode(e.slice(Ie,Ie+=n)))}case T:return Me(t,Ie-1,new Date(Te(e,t)));case O:{const s=Me(t,Ie-1,new Map);for(let n=0,r=Le(e);n<r;n++)s.set(Te(e,t),Te(e,t));return s}case P:{const s=Me(t,Ie-1,new Set);for(let n=0,r=Le(e);n<r;n++)s.add(Te(e,t));return s}case I:{const s=Ie-1,n=Te(e,t),r=Te(e,t),a=Te(e,t),o=new(globalThis[n]||Error)(r);return Me(t,s,$(o,"stack",{value:a}))}case q:{const s=Ie-1,n=Te(e,t),r=Te(e,t),a=Te(e,t),o=Te(e,t),c=Te(e,t);return Me(t,s,new C(n,r,a,{colorSpace:o,pixelFormat:c}))}case D:{const s=Ie-1,n=Te(e,t),r=Te(e,t);return Me(t,s,new RegExp(n,r))}case f:return!1;case g:return!0;case m:return NaN;case v:return 1/0;case A:return-1/0;case k:return 0;case S:return-0;case y:return null;case U:return Ee(e),G.getBigInt64(0,!0);case x:return Ee(e),G.getBigUint64(0,!0);case M: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()}})(Te(e,t));case z:return t.get(Le(e));case F:{const s=Ie-1,n=Te(e,t),r=Te(e,t);return Me(t,s,new Blob([e.slice(Ie,Ie+=r)],{type:n}))}case j:{const s=Ie-1,n=Te(e,t),r=Te(e,t),a=Te(e,t);return Me(t,s,new File([a],n,{type:a.type,lastModified:r}))}default:return}};let Ie=0;const Oe=({byteOffset:e=0}={})=>(t,s)=>{return n=new Uint8Array(s,e,t),Ie=0,Te(n,new Map);var n},{isArray:Re}=Array;const{parse:De,stringify:Pe}=JSON,{promise:Ne,resolve:qe}=t();addEventListener("message",({data:[e,t,s]})=>qe([t,s]),{once:!0});const Fe="xhr";var je="importScripts"in globalThis?e=>Ne.then(([t,r])=>(postMessage(1),((e,t)=>{const n=new BroadcastChannel(e),r=s(),a=(t.decoder??Oe)({byteOffset:0}),{serviceWorker:o}=t;return(s,...c)=>{const i=r();n.postMessage([i,s],...c);const l=new XMLHttpRequest;l.open("POST",o,!1),l.setRequestHeader("Content-Type","application/json"),l.send(Pe([i,e]));const{length:u,buffer:h}=new Uint8Array(De(l.responseText));return t.onsync(u?a(u,h):void 0)}})(r,(e=>{const t=e.onsend??o;return addEventListener("message",async e=>{const{data:s}=e;if(Re(s)&&s[0]===n){e.stopImmediatePropagation(),e.preventDefault();const[r,a]=s[1];postMessage([n,[r,await t(a)]])}}),e})({...t,...e})))):(e,s)=>{const{promise:n,resolve:r}=t();return new Be(e,s,r)instanceof me?n:ke.then(()=>n)};export{Fe as channel,je 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
+ //@ts-check
86
+
87
+ let { SharedArrayBuffer: SAB$1 } = globalThis;
88
+
89
+ try {
90
+ //@ts-ignore due valid options not recognized
91
+ new SAB$1(4, { maxByteLength: 8 });
92
+ }
93
+ catch (_) {
94
+ SAB$1 = /** @type {SharedArrayBufferConstructor} */(
95
+ /** @type {unknown} */(
96
+ class SharedArrayBuffer extends ArrayBuffer {
97
+ get growable() {
98
+ //@ts-ignore due valid property not recognized
99
+ return super.resizable;
100
+ }
101
+ /** @param {number} newLength */
102
+ grow(newLength) {
103
+ //@ts-ignore due valid method not recognized
104
+ super.resize(newLength);
105
+ }
106
+ }
107
+ )
108
+ );
109
+ }
110
+
111
+ var SAB = ({
112
+ initByteLength = 1024,
113
+ maxByteLength = (1024 * 8)
114
+ }) =>
115
+ new SAB$1(
116
+ byteOffset + initByteLength,
117
+ { maxByteLength: byteOffset + maxByteLength }
118
+ );
119
+
120
+ let i$1 = 0;
121
+
122
+ const FALSE = i$1++;
123
+ const TRUE = i$1++;
124
+
125
+ const UNDEFINED = i$1++;
126
+ const NULL = i$1++;
127
+
128
+ const NUMBER = i$1++;
129
+ const UI8 = i$1++;
130
+ const NAN = i$1++;
131
+ const INFINITY = i$1++;
132
+ const N_INFINITY = i$1++;
133
+ const ZERO = i$1++;
134
+ const N_ZERO = i$1++;
135
+
136
+ const BIGINT = i$1++;
137
+ const BIGUINT = i$1++;
138
+
139
+ const STRING = i$1++;
140
+
141
+ const SYMBOL = i$1++;
142
+
143
+ const ARRAY = i$1++;
144
+ const BUFFER = i$1++;
145
+ const DATE = i$1++;
146
+ const ERROR = i$1++;
147
+ const MAP = i$1++;
148
+ const OBJECT = i$1++;
149
+ const REGEXP = i$1++;
150
+ const SET = i$1++;
151
+ const VIEW = i$1++;
152
+
153
+ const IMAGE_DATA = i$1++;
154
+ const BLOB = i$1++;
155
+ const FILE = i$1++;
156
+
157
+ const RECURSION = i$1++;
158
+
159
+ class Never {}
160
+
161
+ const ImageData = globalThis.ImageData || /** @type {typeof ImageData} */(Never);
162
+
163
+ // This is an Array facade for the encoder.
164
+
165
+ class Stack {
166
+ /**
167
+ * @param {Stack} self
168
+ * @param {Uint8Array} value
169
+ */
170
+ static push(self, value) {
171
+ self.sync(false);
172
+ self._(value, value.length);
173
+ }
174
+
175
+ /**
176
+ * @param {ArrayBufferLike} buffer
177
+ * @param {number} offset
178
+ */
179
+ constructor(buffer, offset) {
180
+ /** @type {number[]} */
181
+ const output = [];
182
+
183
+ /** @private length */
184
+ this.l = 0;
185
+
186
+ /** @private output */
187
+ this.o = output;
188
+
189
+ /** @private view */
190
+ this.v = new Uint8Array(buffer, offset);
191
+
192
+ /** @type {typeof Array.prototype.push} */
193
+ this.push = output.push.bind(output);
194
+ }
195
+
196
+ /**
197
+ * @type {number}
198
+ */
199
+ get length() {
200
+ return this.l + this.o.length;
201
+ }
202
+
203
+ /**
204
+ * Sync all entries in the output to the buffer.
205
+ * @param {boolean} last `true` if it's the last sync.
206
+ */
207
+ sync(last) {
208
+ const output = this.o;
209
+ const length = output.length;
210
+ if (length) this._(last ? output : output.splice(0), length);
211
+ }
212
+
213
+ /**
214
+ * Set a value to the buffer
215
+ * @private
216
+ * @param {Uint8Array|number[]} value
217
+ * @param {number} byteLength
218
+ */
219
+ _(value, byteLength) {
220
+ const { buffer, byteOffset } = this.v;
221
+ const offset = this.l;
222
+ this.l += byteLength;
223
+ byteLength += byteOffset + offset;
224
+ if (buffer.byteLength < byteLength)
225
+ /** @type {SharedArrayBuffer} */(buffer).grow(byteLength);
226
+ this.v.set(value, offset);
227
+ }
228
+ }
229
+
230
+ /** @type {Map<symbol, string>} */
231
+ const symbols = new Map(
232
+ Reflect.ownKeys(Symbol).map(
233
+ key => [Symbol[key], `@${String(key)}`]
234
+ )
235
+ );
236
+
237
+ /**
238
+ * @param {symbol} value
239
+ * @param {string} description
240
+ * @returns {string}
241
+ */
242
+ const asSymbol = (value, description) => (
243
+ description === void 0 ? '?' :
244
+ (Symbol.keyFor(value) === void 0 ? `!${description}` : `#${description}`)
245
+ );
246
+
247
+ /**
248
+ * Extract the value from a pair of type and value.
249
+ * @param {string} name
250
+ * @returns {symbol}
251
+ */
252
+ const fromSymbol = name => {
253
+ switch (name[0]) {
254
+ case '@': return Symbol[name.slice(1)];
255
+ case '#': return Symbol.for(name.slice(1));
256
+ case '!': return Symbol(name.slice(1));
257
+ default: return Symbol();
258
+ }
259
+ };
260
+
261
+ /**
262
+ * Create the name of a symbol.
263
+ * @param {symbol} value
264
+ * @returns {string}
265
+ */
266
+ const toSymbol = value => symbols.get(value) || asSymbol(value, value.description);
267
+
268
+ const defineProperty = Object.defineProperty;
269
+
270
+ const isArray$1 = Array.isArray;
271
+
272
+ const isView = ArrayBuffer.isView;
273
+
274
+ const MAX_ARGS = 0x7FFF;
275
+
276
+ /**
277
+ * @param {number[]} output
278
+ * @param {Uint8Array} value
279
+ */
280
+ const push = (output, value) => {
281
+ for (let $ = output.push, i = 0, length = value.length; i < length; i += MAX_ARGS)
282
+ $.apply(output, value.subarray(i, i + MAX_ARGS));
283
+ };
284
+
285
+ const decoder$1 = new TextDecoder;
286
+
287
+ const encoder$1 = new TextEncoder;
288
+
289
+ const buffer = new ArrayBuffer(8);
290
+ const dv = new DataView(buffer);
291
+ const u8a8 = new Uint8Array(buffer);
292
+
293
+ const { getPrototypeOf } = Object;
294
+ const { construct } = Reflect;
295
+ const { toStringTag } = Symbol;
296
+
297
+ const toTag = (ref, name = ref[toStringTag]) =>
298
+ name in globalThis ? name : toTag(construct(getPrototypeOf(ref.constructor),[0]));
299
+
300
+ //@ts-check
301
+
302
+
303
+ /** @typedef {Map<number, number[]>} Cache */
304
+
305
+ const { isNaN, isFinite, isInteger } = Number;
306
+ const { ownKeys } = Reflect;
307
+ const { is } = Object;
308
+
309
+ /**
310
+ * @param {any} input
311
+ * @param {number[]|Stack} output
312
+ * @param {Cache} cache
313
+ * @returns {boolean}
314
+ */
315
+ const process = (input, output, cache) => {
316
+ const value = cache.get(input);
317
+ const unknown = !value;
318
+ if (unknown) {
319
+ dv.setUint32(0, output.length, true);
320
+ cache.set(input, [u8a8[0], u8a8[1], u8a8[2], u8a8[3]]);
321
+ }
322
+ else
323
+ output.push(RECURSION, value[0], value[1], value[2], value[3]);
324
+ return unknown;
325
+ };
326
+
327
+ /**
328
+ * @param {number[]|Stack} output
329
+ * @param {number} type
330
+ * @param {number} length
331
+ */
332
+ const set = (output, type, length) => {
333
+ dv.setUint32(0, length, true);
334
+ output.push(type, u8a8[0], u8a8[1], u8a8[2], u8a8[3]);
335
+ };
336
+
337
+ /**
338
+ * @param {any} input
339
+ * @param {number[]|Stack} output
340
+ * @param {Cache} cache
341
+ */
342
+ const inflate = (input, output, cache) => {
343
+ switch (typeof input) {
344
+ case 'number': {
345
+ if (input && isFinite(input)) {
346
+ if (isInteger(input) && input < 256 && -1 < input)
347
+ output.push(UI8, input);
348
+ else {
349
+ dv.setFloat64(0, input, true);
350
+ output.push(NUMBER, u8a8[0], u8a8[1], u8a8[2], u8a8[3], u8a8[4], u8a8[5], u8a8[6], u8a8[7]);
351
+ }
352
+ }
353
+ else if (isNaN(input)) output.push(NAN);
354
+ else if (!input) output.push(is(input, 0) ? ZERO : N_ZERO);
355
+ else output.push(input < 0 ? N_INFINITY : INFINITY);
356
+ break;
357
+ }
358
+ case 'object': {
359
+ switch (true) {
360
+ case input === null:
361
+ output.push(NULL);
362
+ break;
363
+ case !process(input, output, cache): break;
364
+ case isArray$1(input): {
365
+ const length = input.length;
366
+ set(output, ARRAY, length);
367
+ for (let i = 0; i < length; i++)
368
+ inflate(input[i], output, cache);
369
+ break;
370
+ }
371
+ case isView(input): {
372
+ output.push(VIEW);
373
+ inflate(toTag(input), output, cache);
374
+ input = input.buffer;
375
+ if (!process(input, output, cache)) break;
376
+ // fallthrough
377
+ }
378
+ case input instanceof ArrayBuffer: {
379
+ const ui8a = new Uint8Array(input);
380
+ set(output, BUFFER, ui8a.length);
381
+ //@ts-ignore
382
+ pushView(output, ui8a);
383
+ break;
384
+ }
385
+ case input instanceof Date:
386
+ output.push(DATE);
387
+ inflate(input.getTime(), output, cache);
388
+ break;
389
+ case input instanceof Map: {
390
+ set(output, MAP, input.size);
391
+ for (const [key, value] of input) {
392
+ inflate(key, output, cache);
393
+ inflate(value, output, cache);
394
+ }
395
+ break;
396
+ }
397
+ case input instanceof Set: {
398
+ set(output, SET, input.size);
399
+ for (const value of input)
400
+ inflate(value, output, cache);
401
+ break;
402
+ }
403
+ case input instanceof Error:
404
+ output.push(ERROR);
405
+ inflate(input.name, output, cache);
406
+ inflate(input.message, output, cache);
407
+ inflate(input.stack, output, cache);
408
+ break;
409
+ /* c8 ignore start */
410
+ case input instanceof ImageData:
411
+ output.push(IMAGE_DATA);
412
+ inflate(input.data, output, cache);
413
+ inflate(input.width, output, cache);
414
+ inflate(input.height, output, cache);
415
+ inflate(input.colorSpace, output, cache);
416
+ //@ts-ignore
417
+ inflate(input.pixelFormat, output, cache);
418
+ break;
419
+ /* c8 ignore stop */
420
+ case input instanceof RegExp:
421
+ output.push(REGEXP);
422
+ inflate(input.source, output, cache);
423
+ inflate(input.flags, output, cache);
424
+ break;
425
+ case input instanceof File: {
426
+ output.push(FILE);
427
+ inflate(input.name, output, cache);
428
+ inflate(input.lastModified, output, cache);
429
+ // fallthrough
430
+ }
431
+ case input instanceof Blob: {
432
+ const size = input.size;
433
+ output.push(BLOB);
434
+ inflate(input.type, output, cache);
435
+ inflate(size, output, cache);
436
+ const length = output.length;
437
+ //@ts-ignore
438
+ pushView(output, new Uint8Array(size));
439
+ blobs.push(input.arrayBuffer().then(buffer => [length, buffer]));
440
+ break;
441
+ }
442
+ default: {
443
+ if ('toJSON' in input) {
444
+ const json = input.toJSON();
445
+ inflate(json === input ? null : json, output, cache);
446
+ }
447
+ else {
448
+ const keys = ownKeys(input);
449
+ const length = keys.length;
450
+ set(output, OBJECT, length);
451
+ for (let i = 0; i < length; i++) {
452
+ const key = keys[i];
453
+ inflate(key, output, cache);
454
+ inflate(input[key], output, cache);
455
+ }
456
+ }
457
+ break;
458
+ }
459
+ }
460
+ break;
461
+ }
462
+ case 'string': {
463
+ if (process(input, output, cache)) {
464
+ const encoded = encoder$1.encode(input);
465
+ set(output, STRING, encoded.length);
466
+ //@ts-ignore
467
+ pushView(output, encoded);
468
+ }
469
+ break;
470
+ }
471
+ case 'boolean': {
472
+ output.push(input ? TRUE : FALSE);
473
+ break;
474
+ }
475
+ case 'symbol': {
476
+ output.push(SYMBOL);
477
+ inflate(toSymbol(input), output, cache);
478
+ break;
479
+ }
480
+ case 'bigint': {
481
+ let type = BIGINT;
482
+ if (9223372036854775807n < input) {
483
+ dv.setBigUint64(0, input, true);
484
+ type = BIGUINT;
485
+ }
486
+ else dv.setBigInt64(0, input, true);
487
+ output.push(type, u8a8[0], u8a8[1], u8a8[2], u8a8[3], u8a8[4], u8a8[5], u8a8[6], u8a8[7]);
488
+ break;
489
+ }
490
+ // this covers functions too
491
+ default: {
492
+ output.push(UNDEFINED);
493
+ break;
494
+ }
495
+ }
496
+ };
497
+
498
+ const blobs = [];
499
+
500
+ /** @type {typeof push|typeof Stack.push} */
501
+ let pushView = push;
502
+
503
+ /**
504
+ * @param {{ byteOffset?: number, Array?: typeof Stack }} [options]
505
+ * @returns {(value: any, buffer: ArrayBufferLike) => number | Promise<number>}
506
+ */
507
+ const encoder = ({ byteOffset = 0, Array = Stack } = {}) => (value, buffer) => {
508
+ const output = new Array(buffer, byteOffset);
509
+ pushView = Array.push;
510
+ inflate(value, output, new Map);
511
+ const length = output.length;
512
+ output.sync(true);
513
+ return blobs.length ?
514
+ Promise.all(blobs.splice(0)).then(entries => {
515
+ const ui8a = new Uint8Array(buffer, byteOffset);
516
+ for (const [len, buff] of entries)
517
+ ui8a.set(new Uint8Array(buff), len);
518
+ return length;
519
+ }) :
520
+ length;
521
+ };
522
+
523
+ const handler = (sab, options, useAtomics) => {
524
+ const i32a = new Int32Array(sab);
525
+ const encode = (options.encoder ?? encoder)({ byteOffset });
526
+ const onsync = options.onsync ?? identity;
527
+
528
+ const resolve = (length => {
529
+ i32a[1] = length;
530
+ i32a[0] = 1;
531
+ });
532
+
533
+ const process = result => {
534
+ const length = encode(result, sab);
535
+ return typeof length === 'number' ? resolve(length) : length.then(resolve);
536
+ };
537
+
538
+ return async ({ data }) => process(await onsync(data));
539
+ };
540
+
541
+ const isOK = value => {
542
+ switch (typeof value) {
543
+ case 'symbol':
544
+ case 'function':
545
+ return false;
546
+ }
547
+ return true;
548
+ };
549
+
550
+ const post = (sab, options) => {
551
+ const opts = {};
552
+ for (const key in options) {
553
+ const value = options[key];
554
+ if (isOK(key) && isOK(value)) opts[key] = value;
555
+ }
556
+ return [sab, opts];
557
+ };
558
+
559
+ const url = (scriptURL, reflected, options) => {
560
+ const url = new URL(scriptURL, location.href);
561
+ url.searchParams.set('reflected', reflected);
562
+ return [url, { ...options, type: 'module' }];
563
+ };
564
+
565
+ const CHANNEL$1 = 'async';
566
+
567
+ let Worker$2 = class Worker extends Sender {
568
+ constructor(scriptURL, options, resolve) {
569
+ const channel = randomUUID();
570
+ const bc = new BroadcastChannel(channel);
571
+ const sab = SAB(options);
572
+ const i32a = new Int32Array(sab);
573
+ const handle = handler(sab, options);
574
+ bc.addEventListener('message', async ({ data: [id, payload] }) => {
575
+ await handle({ data: payload });
576
+ bc.postMessage([id, new Uint8Array(sab, byteOffset, i32a[1])]);
577
+ });
578
+ super(...url(scriptURL, CHANNEL$1, options));
579
+ super.addEventListener('message', () => resolve(this), { once: true });
580
+ super.postMessage(post(sab, options).concat(channel));
581
+ }
582
+
583
+ get channel() {
584
+ return CHANNEL$1;
585
+ }
586
+ };
587
+
588
+ const CHANNEL = 'xhr';
589
+
590
+ const channels = new Map;
591
+
592
+ const sharedBC = new BroadcastChannel(SHARED_CHANNEL);
593
+ sharedBC.addEventListener('message', async ({ data: [op, details] }) => {
594
+ if (op === 'request') {
595
+ const [uid, [id, channel]] = details;
596
+ const responses = channels.get(channel);
597
+ if (responses) {
598
+ const promise = responses.get(id);
599
+ responses.delete(id);
600
+ sharedBC.postMessage(['response', [uid, await promise]]);
601
+ }
602
+ }
603
+ });
604
+
605
+ const { promise: sw, resolve: resolve$1 } = withResolvers$1();
606
+ let init = true;
607
+
608
+ const activate = (swc, options) => {
609
+ let w, c = true, { url } = options;
610
+ swc.getRegistration(url)
611
+ .then(r => (r ?? swc.register(url, options)))
612
+ .then(function ready(r) {
613
+ const { controller } = swc;
614
+ c = c && !!controller;
615
+ w = (r.installing || r.waiting || r.active);
616
+ if (!w) return activate(swc, options);
617
+ if (w.state === 'activated') {
618
+ if (c) {
619
+ // allow ServiceWorker swap on different URL
620
+ if (controller.scriptURL === url)
621
+ return resolve$1();
622
+ r.unregister();
623
+ }
624
+ location.reload();
625
+ }
626
+ else {
627
+ w.addEventListener('statechange', () => ready(r), { once: true });
628
+ }
629
+ });
630
+ };
631
+
632
+ let Worker$1 = class Worker extends Sender {
633
+ #channel;
634
+ constructor(scriptURL, options, resolve) {
635
+ if (init) {
636
+ init = false;
637
+ let { serviceWorker } = options;
638
+ if (!serviceWorker) {
639
+ // @ts-ignore
640
+ return new Worker$2(scriptURL, options, resolve);
641
+ }
642
+ if (typeof serviceWorker === 'string') serviceWorker = { url: serviceWorker };
643
+ serviceWorker.url = new URL(serviceWorker.url, location.href).href;
644
+ activate(navigator.serviceWorker, serviceWorker);
645
+ }
646
+ const channel = randomUUID();
647
+ const bc = new BroadcastChannel(channel);
648
+ const sab = SAB(options);
649
+ const responses = new Map;
650
+ const i32a = new Int32Array(sab);
651
+ const handle = handler(sab, options);
652
+ channels.set(channel, responses);
653
+ bc.addEventListener('message', async ({ data: [id, payload] }) => {
654
+ const { promise, resolve } = withResolvers$1();
655
+ responses.set(id, promise);
656
+ await handle({ data: payload });
657
+ resolve(new Uint8Array(sab, byteOffset, i32a[1]));
658
+ });
659
+ super(...url(scriptURL, CHANNEL, options));
660
+ super.addEventListener('message', () => resolve(this), { once: true });
661
+ super.postMessage(post(sab, options).concat(channel));
662
+ this.#channel = channel;
663
+ }
664
+
665
+ terminate() {
666
+ channels.delete(this.#channel);
667
+ super.terminate();
668
+ }
669
+
670
+ get channel() {
671
+ return CHANNEL;
672
+ }
673
+ };
674
+ var main = (scriptURL, options) => {
675
+ const { promise, resolve } = withResolvers$1();
676
+ const worker = new Worker$1(scriptURL, options, resolve);
677
+ return worker instanceof Worker$2 ? promise : sw.then(() => promise);
678
+ };
679
+
680
+ //@ts-check
681
+
682
+
683
+ /** @typedef {Map<number, any>} Cache */
684
+
685
+ /**
686
+ * @param {Cache} cache
687
+ * @param {number} index
688
+ * @param {any} value
689
+ * @returns {any}
690
+ */
691
+ const $ = (cache, index, value) => {
692
+ cache.set(index, value);
693
+ return value;
694
+ };
695
+
696
+ /**
697
+ * @param {Uint8Array} input
698
+ */
699
+ const number = input => {
700
+ u8a8[0] = input[i++];
701
+ u8a8[1] = input[i++];
702
+ u8a8[2] = input[i++];
703
+ u8a8[3] = input[i++];
704
+ u8a8[4] = input[i++];
705
+ u8a8[5] = input[i++];
706
+ u8a8[6] = input[i++];
707
+ u8a8[7] = input[i++];
708
+ };
709
+
710
+ /**
711
+ * @param {Uint8Array} input
712
+ * @returns {number}
713
+ */
714
+ const size = input => {
715
+ u8a8[0] = input[i++];
716
+ u8a8[1] = input[i++];
717
+ u8a8[2] = input[i++];
718
+ u8a8[3] = input[i++];
719
+ return dv.getUint32(0, true);
720
+ };
721
+
722
+ /**
723
+ * @param {Uint8Array} input
724
+ * @param {Cache} cache
725
+ * @returns {any}
726
+ */
727
+ const deflate = (input, cache) => {
728
+ switch (input[i++]) {
729
+ case NUMBER: {
730
+ number(input);
731
+ return dv.getFloat64(0, true);
732
+ }
733
+ case UI8: return input[i++];
734
+ case OBJECT: {
735
+ const object = $(cache, i - 1, {});
736
+ for (let j = 0, length = size(input); j < length; j++)
737
+ object[deflate(input, cache)] = deflate(input, cache);
738
+ return object;
739
+ }
740
+ case ARRAY: {
741
+ const array = $(cache, i - 1, []);
742
+ for (let j = 0, length = size(input); j < length; j++)
743
+ array.push(deflate(input, cache));
744
+ return array;
745
+ }
746
+ case VIEW: {
747
+ const index = i - 1;
748
+ const name = deflate(input, cache);
749
+ return $(cache, index, new globalThis[name](deflate(input, cache)));
750
+ }
751
+ case BUFFER: {
752
+ const index = i - 1;
753
+ const length = size(input);
754
+ return $(cache, index, input.slice(i, i += length).buffer);
755
+ }
756
+ case STRING: {
757
+ const index = i - 1;
758
+ const length = size(input);
759
+ // this could be a subarray but it's not supported on the Web and
760
+ // it wouldn't work with arrays instead of typed arrays.
761
+ return $(cache, index, decoder$1.decode(input.slice(i, i += length)));
762
+ }
763
+ case DATE: {
764
+ return $(cache, i - 1, new Date(deflate(input, cache)));
765
+ }
766
+ case MAP: {
767
+ const map = $(cache, i - 1, new Map);
768
+ for (let j = 0, length = size(input); j < length; j++)
769
+ map.set(deflate(input, cache), deflate(input, cache));
770
+ return map;
771
+ }
772
+ case SET: {
773
+ const set = $(cache, i - 1, new Set);
774
+ for (let j = 0, length = size(input); j < length; j++)
775
+ set.add(deflate(input, cache));
776
+ return set;
777
+ }
778
+ case ERROR: {
779
+ const index = i - 1;
780
+ const name = deflate(input, cache);
781
+ const message = deflate(input, cache);
782
+ const stack = deflate(input, cache);
783
+ const Class = globalThis[name] || Error;
784
+ const error = new Class(message);
785
+ return $(cache, index, defineProperty(error, 'stack', { value: stack }));
786
+ }
787
+ /* c8 ignore start */
788
+ case IMAGE_DATA: {
789
+ const index = i - 1;
790
+ const data = deflate(input, cache);
791
+ const width = deflate(input, cache);
792
+ const height = deflate(input, cache);
793
+ const colorSpace = deflate(input, cache);
794
+ const pixelFormat = deflate(input, cache);
795
+ const settings = { colorSpace, pixelFormat };
796
+ return $(cache, index, new ImageData(data, width, height, settings));
797
+ }
798
+ /* c8 ignore stop */
799
+ case REGEXP: {
800
+ const index = i - 1;
801
+ const source = deflate(input, cache);
802
+ const flags = deflate(input, cache);
803
+ return $(cache, index, new RegExp(source, flags));
804
+ }
805
+ case FALSE: return false;
806
+ case TRUE: return true;
807
+ case NAN: return NaN;
808
+ case INFINITY: return Infinity;
809
+ case N_INFINITY: return -Infinity;
810
+ case ZERO: return 0;
811
+ case N_ZERO: return -0;
812
+ case NULL: return null;
813
+ case BIGINT: return (number(input), dv.getBigInt64(0, true));
814
+ case BIGUINT: return (number(input), dv.getBigUint64(0, true));
815
+ case SYMBOL: return fromSymbol(deflate(input, cache));
816
+ case RECURSION: return cache.get(size(input));
817
+ case BLOB: {
818
+ const index = i - 1;
819
+ const type = deflate(input, cache);
820
+ const size = deflate(input, cache);
821
+ return $(cache, index, new Blob([input.slice(i, i += size)], { type }));
822
+ }
823
+ case FILE: {
824
+ const index = i - 1;
825
+ const name = deflate(input, cache);
826
+ const lastModified = deflate(input, cache);
827
+ const blob = deflate(input, cache);
828
+ return $(cache, index, new File([blob], name, { type: blob.type, lastModified }));
829
+ }
830
+ // this covers functions too
831
+ default: return undefined;
832
+ }
833
+ };
834
+
835
+ let i = 0;
836
+
837
+ /**
838
+ * @param {Uint8Array} value
839
+ * @returns {any}
840
+ */
841
+ const decode = value => {
842
+ i = 0;
843
+ return deflate(value, new Map);
844
+ };
845
+
846
+ /**
847
+ * @param {{ byteOffset?: number }} [options]
848
+ * @returns {(length: number, buffer: ArrayBufferLike) => any}
849
+ */
850
+ const decoder = ({ byteOffset = 0 } = {}) => (length, buffer) => decode(
851
+ new Uint8Array(buffer, byteOffset, length)
852
+ );
853
+
854
+ const { isArray } = Array;
855
+
856
+ var sender = options => {
857
+ const onsend = options.onsend ?? identity;
858
+ addEventListener('message', async event => {
859
+ const { data } = event;
860
+ if (isArray(data) && data[0] === SHARED_CHANNEL) {
861
+ event.stopImmediatePropagation();
862
+ event.preventDefault();
863
+ const [id, payload] = data[1];
864
+ postMessage([SHARED_CHANNEL, [id, await onsend(payload)]]);
865
+ }
866
+ });
867
+ return options;
868
+ };
869
+
870
+ const { parse, stringify } = JSON;
871
+
872
+ const { promise, resolve } = withResolvers$1();
873
+
874
+ addEventListener(
875
+ 'message',
876
+ ({ data: [_, main, channel] }) => resolve([main, channel]),
877
+ { once: true }
878
+ );
879
+
880
+ const channel = 'xhr';
881
+
882
+ const handle = (channel, options) => {
883
+ const bc = new BroadcastChannel(channel);
884
+ const next = i32();
885
+ const decode = (options.decoder ?? decoder)({ byteOffset: 0 });
886
+ const { serviceWorker } = options;
887
+ return (payload, ...rest) => {
888
+ const id = next();
889
+ // @ts-ignore
890
+ bc.postMessage([id, payload], ...rest);
891
+ const xhr = new XMLHttpRequest;
892
+ xhr.open('POST', serviceWorker, false);
893
+ xhr.setRequestHeader('Content-Type', 'application/json');
894
+ xhr.send(stringify([id, channel]));
895
+ const { length, buffer } = new Uint8Array(parse(xhr.responseText));
896
+ return options.onsync(length ? decode(length, buffer) : void 0);
897
+ };
898
+ };
899
+
900
+ var worker = options => promise.then(([main, channel]) => {
901
+ postMessage(1);
902
+ return handle(channel, sender({ ...main, ...options }));
903
+ });
904
+
905
+ var xhr = (
906
+ 'importScripts' in globalThis ? worker : main
907
+ );
908
+
909
+ export { channel, xhr as default };