reflected 0.2.4 → 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
@@ -1 +1,616 @@
1
- import{S as t,o as e,O as r,i as s,a as n,l as a,b as i,R as o,B as c,V as u,c as l,d as f,f as h,D as p,t as g,e as y,F as d,g as w,h as m,j as b,k as v,m as $,n as j,p as x,A as P,w as k,q as M}from"./shared-DwSbyGXy.js";import{t as O}from"./global-Bs8RLRoX.js";import{i as S}from"./i32-BkNtpHwD.js";import{n as A}from"./shared-array-buffer-cwdMr2mc.js";const B="M:"+t,T="W:"+t,_="S:"+t,E="D:"+t;let R=0;const K=R++,L=R++,U=R++,W=R++,D=R++,q=R++,F=R++,I=R++,z=R++,V=R++,C=R++,G=R++,H=R++,J=R++,N=R++;R++;const Q=R++,X=R++,Y=(t,e)=>new ArrayBuffer(t,{maxByteLength:e}),Z=([t,e],r)=>{const s=r?t.byteLength:t.length;if(r){if(e){const r=Y(s,e);new Uint8Array(r).set(new Uint8Array(t)),t=r}}else{const r=e?Y(s,e):new ArrayBuffer(s);new Uint8Array(r).set(t),t=r}return t};const tt=/\[('|")?(.+?)\1\]/g,et=(t,e)=>t?.[e];var rt=t=>{const e=[],r=t=>{const r=e.splice(t);for(;t<r.length;)r[t++].delete(r[t++])};return class s extends Map{static keys=Symbol();static proto=Symbol();drop(t,e){return t!==s.proto&&super.delete(s.keys),super.delete(t),e}set(s,n){return((s,n)=>{e.push(s,n)<3&&setTimeout(r,t,0)})(super.set(s,n),s),n}}},st=({reflect:t=s,transform:k=s,released:M=s,buffer:A=!1,timeout:B=-1}=e)=>{const T=a(x),_=a(j),E=t=>{if(!i(t))return t;const[e,s]=t;if(e&o)return st(e,s);switch(e){case r:return bt;case p:return s;case f:return h(s);case l:return BigInt(s);case u:return(([t,e,r,s],n)=>{const a=Z(e,n),i=globalThis[t];return s?new i(a,r,s):new i(a,r)})(s,A);case c:return Z(s,A)}},R=(t,e=new Map)=>{switch(typeof t){case"object":{if(null===t)break;if(t===globalThis)return mt;if(wt in t)return lt;let s=e.get(t);if(!s){const n=k(t);if(ft||!ut.has(n)){if(i(n)){const r=[];s=g(P,r),e.set(t,s);for(let t=0,s=n.length;t<s;t++)r[t]=R(n[t],e);return s}if(!(w(n)||n instanceof ArrayBuffer||"Object"!==O(n))){const a={};s=g(r,a),e.set(t,s);for(const t in n)a[t]=R(n[t],e);return s}}s=g(p,n),e.set(t,s)}return s}case"function":{if(wt in t)return lt;let r=e.get(t);if(!r){const s=k(t);r=g(d,pt(s)),e.set(t,r)}return r}case"symbol":return g(f,y(t))}return t},Y=a(R),st=(t,r)=>{let s=dt.get(r),n=s?.deref();return n||(s&&vt.unregister(s),n=t===m?new Proxy(b,new ct(t,r)):new Proxy(t===$?e:v,new ot(t,r)),s=new WeakRef(n),dt.set(r,s),vt.register(n,r,s)),n},nt=t=>{switch(typeof t){case"object":if(null===t)break;case"function":return wt in t}return!1},at=-1<B,it=at?rt(B):Map;class ot{constructor(t,e){this.t=t,this.v=e,at&&(this.$=new it)}get(e,r){if(at&&this.$.has(r))return this.$.get(r);const s=t(V,this.v,j(r));return at?s[0]?this.$.set(r,E(s[1])):E(s[1]):E(s)}set(e,r,s){const n=t(Q,this.v,j(r),R(s));return at?this.$.drop(r,n):n}has(e,r){return r===wt?(lt=[this.t,this.v],!0):t(H,this.v,j(r))}_oK(){return T(t(N,this.v),dt)}ownKeys(t){return at?this.$.has(it.keys)?this.$.get(it.keys):this.$.set(it.keys,this._oK()):this._oK()}getOwnPropertyDescriptor(e,r){const s=E(t(C,this.v,j(r)));if(s)for(const t in s)s[t]=E(s[t]);return s}defineProperty(e,r,s){const n=t(I,this.v,j(r),R(s));return at?this.$.drop(r,n):n}deleteProperty(e,r){const s=t(z,this.v,j(r));return at?this.$.drop(r,s):s}_gPO(){return E(t(G,this.v))}getPrototypeOf(t){return at?this.$.has(it.proto)?this.$.get(it.proto):this.$.set(it.proto,this._gPO()):this._gPO()}setPrototypeOf(e,r){const s=t(X,this.v,R(r));return at?this.$.drop(it.proto,s):s}isExtensible(e){return t(J,this.v)}preventExtensions(t){return!1}}class ct extends ot{construct(e,r){return E(t(F,this.v,Y(r)))}apply(e,r,s){const n=new Map;return E(t(q,this.v,R(r,n),Y(s,n)))}get(t,e){switch(e){case"apply":return(e,r)=>this.apply(t,e,r);case"call":return(e,...r)=>this.apply(t,e,r);default:return super.get(t,e)}}}let ut,lt,ft=!0;const{apply:ht}=Reflect,{id:pt,ref:gt,unref:yt}=((t=0,e=new Map,r=new Map)=>{const s=S(t);return{clear:()=>{e.clear(),r.clear()},id:t=>{let n=r.get(t);if(void 0===n){for(;e.has(n=s()););e.set(n,t),r.set(t,n)}return n},ref:t=>e.get(t),unref:t=>(r.delete(e.get(t)),e.delete(t))}})(),dt=new Map,wt=Symbol("reflected-ffi"),mt=g(r,null),bt=new Proxy(e,new ot(r,null)),vt=new FinalizationRegistry(e=>{dt.delete(e),t(K,e)});return{global:bt,isProxy:nt,assign(e,...r){const s=nt(e),a=n(s?{}:e,...r);return s&&t(L,lt[1],R(a)),e},direct:t=>(ft&&(ft=!1,ut=new WeakSet),ut.add(t),t),evaluate:(e,...r)=>E(t(U,null,function(t=this){return String(t).replace(/^(async\s*)?(\bfunction\b)?(.*?)\(/,(t,e,r,s)=>s&&!r?`${e||""}function ${s}(`:t)}(e),Y(r))),gather(e,...r){const s=nt(e),n=s?E:t=>e[t];s&&(r=t(W,lt[1],_(r,dt)));for(let t=0;t<r.length;t++)r[t]=n(r[t]);return r},query:(e,r)=>nt(e)?E(t(D,lt[1],r)):((t,e)=>e.replace(tt,".$2").split(".").reduce(et,t))(e,r),reflect:async(t,e,...r)=>{switch(t){case q:{const[t,s]=r;for(let t=0,e=s.length;t<e;t++)s[t]=E(s[t]);return R(await ht(gt(e),E(t),s))}case K:return M(gt(e)),yt(e)}}}};let nt;if("importScripts"in globalThis){let t;const{promise:e,resolve:r}=k(),s=new URL(location).searchParams.get("reflected");t="message"===s?import("./message-BdbPt0y5.js"):"broadcast"===s?import("./broadcast-Ce_cOurt.js"):"xhr"===s?import("./xhr-DHyu_Jz0.js"):import("./async-B_TI4xaG.js"),nt=async r=>{const{data:s,ports:n}=await e,{default:a}=await t,i=new Event("message");return i.data=s,i.ports=n,dispatchEvent(i),a(r)},addEventListener("message",r,{once:!0})}else nt=A?"InstallTrigger"in globalThis?(await import("./broadcast-D0jZEoUb.js")).default:(await import("./message-DNbm1E-6.js")).default:navigator.serviceWorker?(await import("./xhr-C598zZcp.js")).default:(await import("./async-BeKYhv9s.js").then(function(t){return t.a})).default;var at=nt,it=async t=>{const e=await(async t=>{const e=M(null),r=await at({...t,onsend:([t,r])=>e[t](...r)});return new Proxy(e,{get(t,e){if("then"!==e)return t[e]??(t[e]=(...t)=>r([e,t]))},set(t,e,r){const s="then"!==e;return s&&(t[e]=r),s}})})(t),r=st({timeout:0,buffer:!0,...t,reflect:(...t)=>e[B](t)});return e[T]=r.reflect,r.server=t?.ws?.(t=>{e[E]=t},e[_]),{ffi:r,proxy:e}};export{it as default};
1
+ import { S as SHARED_CHANNEL, o as object, O as OBJECT, i as identity, a as assign, l as loopValues, b as isArray, R as REMOTE, B as BUFFER, V as VIEW, c as BIGINT, d as SYMBOL, f as fromSymbol, D as DIRECT, t as tv, e as toSymbol, F as FUNCTION, g as isView, h as REMOTE_FUNCTION, j as callback, k as array, m as REMOTE_OBJECT, n as toKey, p as fromKey, A as ARRAY, w as withResolvers, q as create } from './shared-CjWXp6-D.js';
2
+ import { t as toName } from './global-DzpOeFX2.js';
3
+ import { i as i32 } from './i32-Cw-Rqr5y.js';
4
+ import { n as native } from './shared-array-buffer-BQgQXQvC.js';
5
+
6
+ // Main thread <-> Worker thread
7
+ const MAIN = 'M:' + SHARED_CHANNEL;
8
+ const WORKER = 'W:' + SHARED_CHANNEL;
9
+
10
+ // Worker thread <-> Server thread
11
+ const SOCKET = 'S:' + SHARED_CHANNEL;
12
+ const DRIVER = 'D:' + SHARED_CHANNEL;
13
+
14
+ let i = 0;
15
+
16
+ // extras
17
+ const UNREF = i++;
18
+ const ASSIGN = i++;
19
+ const EVALUATE = i++;
20
+ const GATHER = i++;
21
+ const QUERY = i++;
22
+
23
+ // traps
24
+ const APPLY = i++;
25
+ const CONSTRUCT = i++;
26
+ const DEFINE_PROPERTY = i++;
27
+ const DELETE_PROPERTY = i++;
28
+ const GET = i++;
29
+ const GET_OWN_PROPERTY_DESCRIPTOR = i++;
30
+ const GET_PROTOTYPE_OF = i++;
31
+ const HAS = i++;
32
+ const IS_EXTENSIBLE = i++;
33
+ const OWN_KEYS = i++;
34
+ i++;
35
+ const SET = i++;
36
+ const SET_PROTOTYPE_OF = i++;
37
+
38
+ /** @typedef {[ArrayBufferLike|number[], number]} BufferDetails */
39
+ /** @typedef {[string, BufferDetails, number, number]} ViewDetails */
40
+
41
+ /**
42
+ * @param {number} length
43
+ * @param {number} maxByteLength
44
+ * @returns {ArrayBufferLike}
45
+ */
46
+ const resizable = (length, maxByteLength) => new ArrayBuffer(length, { maxByteLength });
47
+
48
+ /**
49
+ * @param {BufferDetails} details
50
+ * @param {boolean} direct
51
+ * @returns {ArrayBufferLike}
52
+ */
53
+ const fromBuffer = ([value, maxByteLength], direct) => {
54
+ const length = direct ? /** @type {ArrayBufferLike} */ (value).byteLength : /** @type {number[]} */ (value).length;
55
+ if (direct) {
56
+ if (maxByteLength) {
57
+ const buffer = resizable(length, maxByteLength);
58
+ new Uint8Array(buffer).set(new Uint8Array(/** @type {ArrayBufferLike} */ (value)));
59
+ value = buffer;
60
+ }
61
+ }
62
+ else {
63
+ const buffer = maxByteLength ? resizable(length, maxByteLength) : new ArrayBuffer(length);
64
+ new Uint8Array(buffer).set(/** @type {number[]} */ (value));
65
+ value = buffer;
66
+ }
67
+ return /** @type {ArrayBufferLike} */ (value);
68
+ };
69
+
70
+ /**
71
+ * @param {ViewDetails} details
72
+ * @param {boolean} direct
73
+ */
74
+ const fromView = ([name, args, byteOffset, length], direct) => {
75
+ const buffer = fromBuffer(args, direct);
76
+ const Class = globalThis[name];
77
+ return length ? new Class(buffer, byteOffset, length) : new Class(buffer, byteOffset);
78
+ };
79
+
80
+ // (c) https://github.com/WebReflection/to-json-callback
81
+ // brought in here to avoid a dependency for quick testing
82
+
83
+ /**
84
+ * @param {Function} [callback=this]
85
+ * @returns {string}
86
+ */
87
+ function toJSONCallback (callback = this) {
88
+ return String(callback).replace(
89
+ /^(async\s*)?(\bfunction\b)?(.*?)\(/,
90
+ (_, isAsync, fn, name) => (
91
+ name && !fn ?
92
+ `${isAsync || ""}function ${name}(` :
93
+ _
94
+ ),
95
+ );
96
+ }
97
+
98
+ const brackets = /\[('|")?(.+?)\1\]/g;
99
+
100
+ const keys = (target, key) => target?.[key];
101
+
102
+ /**
103
+ * Parses the given path and returns the value at the given target.
104
+ * @param {any} target
105
+ * @param {string} path
106
+ * @returns {any}
107
+ */
108
+ var query = (target, path) => path.replace(brackets, '.$2').split('.').reduce(keys, target);
109
+
110
+ /**
111
+ * @template T
112
+ * @typedef {Object} Heap
113
+ * @property {() => void} clear
114
+ * @property {(ref:T) => number} id
115
+ * @property {(id:number) => T} ref
116
+ * @property {(id:number) => boolean} unref
117
+ */
118
+
119
+ /**
120
+ * Create a heap-like utility to hold references in memory.
121
+ * @param {number} [id=0] The initial `id` which is `0` by default.
122
+ * @param {Map<number, any>} [ids=new Map] The used map of ids to references.
123
+ * @param {Map<any, number>} [refs=new Map] The used map of references to ids.
124
+ * @returns {Heap<any>}
125
+ */
126
+ var heap = (id = 0, ids = new Map, refs = new Map) => {
127
+ const next = i32(id);
128
+ return {
129
+ clear: () => {
130
+ ids.clear();
131
+ refs.clear();
132
+ },
133
+ id: ref => {
134
+ let uid = refs.get(ref);
135
+ if (uid === void 0) {
136
+ /* c8 ignore next */
137
+ while (ids.has(uid = next()));
138
+ ids.set(uid, ref);
139
+ refs.set(ref, uid);
140
+ }
141
+ return uid;
142
+ },
143
+ ref: id => ids.get(id),
144
+ unref: id => {
145
+ refs.delete(ids.get(id));
146
+ return ids.delete(id);
147
+ },
148
+ };
149
+ };
150
+
151
+ /**
152
+ * @param {number} timeout
153
+ * @returns {typeof import('../ts/memo.js').Memo}
154
+ */
155
+ var memo = timeout => {
156
+ const entries = [];
157
+
158
+ const drop = i => {
159
+ const cached = entries.splice(i);
160
+ while (i < cached.length)
161
+ cached[i++].delete(cached[i++]);
162
+ };
163
+
164
+ const set = (self, key) => {
165
+ if (entries.push(self, key) < 3)
166
+ setTimeout(drop, timeout, 0);
167
+ };
168
+
169
+ return class Memo extends Map {
170
+ static keys = Symbol();
171
+ static proto = Symbol();
172
+
173
+ drop(key, value) {
174
+ if (key !== Memo.proto) super.delete(Memo.keys);
175
+ super.delete(key);
176
+ return value;
177
+ }
178
+
179
+ set(key, value) {
180
+ set(super.set(key, value), key);
181
+ return value;
182
+ }
183
+ }
184
+ };
185
+
186
+ /**
187
+ * @typedef {Object} RemoteOptions Optional utilities used to orchestrate local <-> remote communication.
188
+ * @property {Function} [reflect=identity] The function used to reflect operations via the remote receiver. All `Reflect` methods + `unref` are supported.
189
+ * @property {Function} [transform=identity] The function used to transform local values into simpler references that the remote side can understand.
190
+ * @property {Function} [released=identity] The function invoked when a reference is released.
191
+ * @property {boolean} [buffer=false] Optionally allows direct buffer deserialization breaking JSON compatibility.
192
+ * @property {number} [timeout=-1] Optionally allows remote values to be cached when possible for a `timeout` milliseconds value. `-1` means no timeout.
193
+ */
194
+
195
+ /**
196
+ * @param {RemoteOptions} options
197
+ * @returns
198
+ */
199
+ var remote = ({
200
+ reflect = identity,
201
+ transform = identity,
202
+ released = identity,
203
+ buffer = false,
204
+ timeout = -1,
205
+ } = object) => {
206
+ const fromKeys = loopValues(fromKey);
207
+ const toKeys = loopValues(toKey);
208
+
209
+ // OBJECT, DIRECT, VIEW, REMOTE_ARRAY, REMOTE_OBJECT, REMOTE_FUNCTION, SYMBOL, BIGINT
210
+ const fromValue = value => {
211
+ if (!isArray(value)) return value;
212
+ const [t, v] = value;
213
+ if (t & REMOTE) return asProxy(t, v);
214
+ switch (t) {
215
+ case OBJECT: return global;
216
+ case DIRECT: return v;
217
+ case SYMBOL: return fromSymbol(v);
218
+ case BIGINT: return BigInt(v);
219
+ case VIEW: return fromView(v, buffer);
220
+ case BUFFER: return fromBuffer(v, buffer);
221
+ // there is no other case
222
+ }
223
+ };
224
+
225
+ const toValue = (value, cache = new Map) => {
226
+ switch (typeof value) {
227
+ case 'object': {
228
+ if (value === null) break;
229
+ if (value === globalThis) return globalTarget;
230
+ if (reflected in value) return reference;
231
+ let cached = cache.get(value);
232
+ if (!cached) {
233
+ const $ = transform(value);
234
+ if (indirect || !direct.has($)) {
235
+ if (isArray($)) {
236
+ const a = [];
237
+ cached = tv(ARRAY, a);
238
+ cache.set(value, cached);
239
+ for (let i = 0, length = $.length; i < length; i++)
240
+ a[i] = toValue($[i], cache);
241
+ return cached;
242
+ }
243
+ if (!isView($) && !($ instanceof ArrayBuffer) && toName($) === 'Object') {
244
+ const o = {};
245
+ cached = tv(OBJECT, o);
246
+ cache.set(value, cached);
247
+ // // the try/catch guards against WebAssembly opaque objects:
248
+ // // https://github.com/pyodide/pyodide/issues/5929#issuecomment-3377201964
249
+ // try { for (const _ in $) break; } catch { return cached }
250
+ for (const k in $) o[k] = toValue($[k], cache);
251
+ return cached;
252
+ }
253
+ }
254
+ cached = tv(DIRECT, $);
255
+ cache.set(value, cached);
256
+ }
257
+ return cached;
258
+ }
259
+ case 'function': {
260
+ if (reflected in value) return reference;
261
+ let cached = cache.get(value);
262
+ if (!cached) {
263
+ const $ = transform(value);
264
+ cached = tv(FUNCTION, id($));
265
+ cache.set(value, cached);
266
+ }
267
+ return cached;
268
+ }
269
+ case 'symbol': return tv(SYMBOL, toSymbol(value));
270
+ }
271
+ return value;
272
+ };
273
+
274
+ const toValues = loopValues(toValue);
275
+
276
+ const asProxy = (t, v) => {
277
+ let wr = weakRefs.get(v), proxy = wr?.deref();
278
+ if (!proxy) {
279
+ /* c8 ignore start */
280
+ if (wr) fr.unregister(wr);
281
+ /* c8 ignore stop */
282
+ if (t === REMOTE_FUNCTION)
283
+ proxy = new Proxy(callback, new FunctionHandler(t, v));
284
+ else
285
+ proxy = new Proxy(t === REMOTE_OBJECT ? object : array, new Handler(t, v));
286
+ wr = new WeakRef(proxy);
287
+ weakRefs.set(v, wr);
288
+ fr.register(proxy, v, wr);
289
+ }
290
+ return proxy;
291
+ };
292
+
293
+ /**
294
+ * Checks if the given value is a proxy created in the remote side.
295
+ * @param {any} value
296
+ * @returns {boolean}
297
+ */
298
+ const isProxy = value => {
299
+ switch (typeof value) {
300
+ case 'object': if (value === null) break;
301
+ case 'function': return reflected in value;
302
+ }
303
+ return false;
304
+ };
305
+
306
+ const memoize = -1 < timeout;
307
+ const Memo = /** @type {typeof import('./ts/memo.js').Memo} */(
308
+ memoize ? memo(timeout) : Map
309
+ );
310
+
311
+ class Handler {
312
+ constructor(t, v) {
313
+ this.t = t;
314
+ this.v = v;
315
+ if (memoize) this.$ = new Memo;
316
+ }
317
+
318
+ get(_, key) {
319
+ if (memoize && this.$.has(key)) return this.$.get(key);
320
+ const value = reflect(GET, this.v, toKey(key));
321
+ return memoize ?
322
+ (value[0] ?
323
+ this.$.set(key, fromValue(value[1])) :
324
+ fromValue(value[1])) :
325
+ fromValue(value)
326
+ ;
327
+ }
328
+
329
+ set(_, key, value) {
330
+ const result = reflect(SET, this.v, toKey(key), toValue(value));
331
+ return memoize ? this.$.drop(key, result) : result;
332
+ }
333
+
334
+ // TODO: should `in` operations be cached too?
335
+ has(_, prop) {
336
+ if (prop === reflected) {
337
+ reference = [this.t, this.v];
338
+ return true;
339
+ }
340
+ return reflect(HAS, this.v, toKey(prop));
341
+ }
342
+
343
+ _oK() { return fromKeys(reflect(OWN_KEYS, this.v), weakRefs) }
344
+ ownKeys(_) {
345
+ return memoize ?
346
+ (this.$.has(Memo.keys) ?
347
+ this.$.get(Memo.keys) :
348
+ this.$.set(Memo.keys, this._oK())) :
349
+ this._oK()
350
+ ;
351
+ }
352
+
353
+ // this would require a cache a part per each key or make
354
+ // the Cache code more complex for probably little gain
355
+ getOwnPropertyDescriptor(_, key) {
356
+ const descriptor = fromValue(reflect(GET_OWN_PROPERTY_DESCRIPTOR, this.v, toKey(key)));
357
+ if (descriptor) {
358
+ for (const k in descriptor)
359
+ descriptor[k] = fromValue(descriptor[k]);
360
+ }
361
+ return descriptor;
362
+ }
363
+
364
+ defineProperty(_, key, descriptor) {
365
+ const result = reflect(DEFINE_PROPERTY, this.v, toKey(key), toValue(descriptor));
366
+ return memoize ? this.$.drop(key, result) : result;
367
+ }
368
+
369
+ deleteProperty(_, key) {
370
+ const result = reflect(DELETE_PROPERTY, this.v, toKey(key));
371
+ return memoize ? this.$.drop(key, result) : result;
372
+ }
373
+
374
+ _gPO() { return fromValue(reflect(GET_PROTOTYPE_OF, this.v)) }
375
+ getPrototypeOf(_) {
376
+ /* c8 ignore start */
377
+ return memoize ?
378
+ (this.$.has(Memo.proto) ?
379
+ this.$.get(Memo.proto) :
380
+ this.$.set(Memo.proto, this._gPO())) :
381
+ this._gPO()
382
+ ;
383
+ /* c8 ignore stop */
384
+ }
385
+
386
+ setPrototypeOf(_, value) {
387
+ const result = reflect(SET_PROTOTYPE_OF, this.v, toValue(value));
388
+ return memoize ? this.$.drop(Memo.proto, result) : result;
389
+ }
390
+ // way less common than others to be cached
391
+ isExtensible(_) { return reflect(IS_EXTENSIBLE, this.v) }
392
+
393
+ // ⚠️ due shared proxies' targets this cannot be reflected
394
+ preventExtensions(_) { return false }
395
+ }
396
+
397
+ class FunctionHandler extends Handler {
398
+ construct(_, args) { return fromValue(reflect(CONSTRUCT, this.v, toValues(args))) }
399
+
400
+ apply(_, self, args) {
401
+ const map = new Map;
402
+ return fromValue(reflect(APPLY, this.v, toValue(self, map), toValues(args, map)));
403
+ }
404
+
405
+ get(_, key) {
406
+ switch (key) {
407
+ // skip obvious roundtrip cases
408
+ case 'apply': return (self, args) => this.apply(_, self, args);
409
+ case 'call': return (self, ...args) => this.apply(_, self, args);
410
+ default: return super.get(_, key);
411
+ }
412
+ }
413
+ }
414
+
415
+ let indirect = true, direct, reference;
416
+
417
+ const { apply } = Reflect;
418
+ const { id, ref, unref } = heap();
419
+ const weakRefs = new Map;
420
+ const reflected = Symbol('reflected-ffi');
421
+ const globalTarget = tv(OBJECT, null);
422
+ const global = new Proxy(object, new Handler(OBJECT, null));
423
+ const fr = new FinalizationRegistry(v => {
424
+ weakRefs.delete(v);
425
+ reflect(UNREF, v);
426
+ });
427
+
428
+ return {
429
+ /**
430
+ * The local global proxy reference.
431
+ * @type {unknown}
432
+ */
433
+ global,
434
+
435
+ isProxy,
436
+
437
+ /** @type {typeof assign} */
438
+ assign(target, ...sources) {
439
+ const asProxy = isProxy(target);
440
+ const assignment = assign(asProxy ? {} : target, ...sources);
441
+ if (asProxy) reflect(ASSIGN, reference[1], toValue(assignment));
442
+ return target;
443
+ },
444
+
445
+ /**
446
+ * Alows local references to be passed directly to the remote receiver,
447
+ * either as copy or serliazied values (it depends on the implementation).
448
+ * @template {WeakKey} T
449
+ * @param {T} value
450
+ * @returns {T}
451
+ */
452
+ direct(value) {
453
+ if (indirect) {
454
+ indirect = false;
455
+ direct = new WeakSet;
456
+ }
457
+ direct.add(value);
458
+ return value;
459
+ },
460
+
461
+ /**
462
+ * Evaluates elsewhere the given callback with the given arguments.
463
+ * This utility is similar to puppeteer's `page.evaluate` where the function
464
+ * content is evaluated in the local side and its result is returned.
465
+ * @param {Function} callback
466
+ * @param {...any} args
467
+ * @returns {any}
468
+ */
469
+ evaluate: (callback, ...args) => fromValue(
470
+ reflect(EVALUATE, null, toJSONCallback(callback), toValues(args))
471
+ ),
472
+
473
+ /**
474
+ * @param {object} target
475
+ * @param {...(string|symbol)} keys
476
+ * @returns {any[]}
477
+ */
478
+ gather(target, ...keys) {
479
+ const asProxy = isProxy(target);
480
+ const asValue = asProxy ? fromValue : (key => target[key]);
481
+ if (asProxy) keys = reflect(GATHER, reference[1], toKeys(keys, weakRefs));
482
+ for (let i = 0; i < keys.length; i++) keys[i] = asValue(keys[i]);
483
+ return keys;
484
+ },
485
+
486
+ /**
487
+ * Queries the given target for the given path.
488
+ * @param {any} target
489
+ * @param {string} path
490
+ * @returns {any}
491
+ */
492
+ query: (target, path) => (
493
+ isProxy(target) ?
494
+ fromValue(reflect(QUERY, reference[1], path)) :
495
+ query(target, path)
496
+ ),
497
+
498
+ /**
499
+ * The callback needed to resolve any local call. Currently only `apply` and `unref` are supported.
500
+ * Its returned value will be understood by the remote implementation
501
+ * and it is compatible with the structured clone algorithm.
502
+ * @param {number} method
503
+ * @param {number?} uid
504
+ * @param {...any} args
505
+ * @returns
506
+ */
507
+ reflect: async (method, uid, ...args) => {
508
+ switch (method) {
509
+ case APPLY: {
510
+ const [context, params] = args;
511
+ for (let i = 0, length = params.length; i < length; i++)
512
+ params[i] = fromValue(params[i]);
513
+ return toValue(await apply(ref(uid), fromValue(context), params));
514
+ }
515
+ case UNREF: {
516
+ released(ref(uid));
517
+ return unref(uid);
518
+ }
519
+ }
520
+ },
521
+ };
522
+ };
523
+
524
+ /** @type {Function} */
525
+ let module$1;
526
+
527
+ if ('importScripts' in globalThis) {
528
+ let get;
529
+ const { promise, resolve } = withResolvers();
530
+ // @ts-ignore
531
+ const reflected = new URL(location).searchParams.get('reflected');
532
+ if (reflected === 'message') get = import(/* webpackIgnore: true */'./message-mK0eTb54.js');
533
+ else if (reflected === 'broadcast') get = import(/* webpackIgnore: true */'./broadcast-CKNplfox.js');
534
+ else if (reflected === 'xhr') get = import(/* webpackIgnore: true */'./xhr-CRJGE1xP.js');
535
+ else get = import(/* webpackIgnore: true */'./async-0Q0H4a_N.js');
536
+ module$1 = async options => {
537
+ const { data, ports } = await promise;
538
+ const { default: reflect } = await get;
539
+ const event = new Event('message');
540
+ // @ts-ignore
541
+ event.data = data;
542
+ // @ts-ignore
543
+ event.ports = ports;
544
+ dispatchEvent(event);
545
+ return reflect(options);
546
+ };
547
+ addEventListener('message', resolve, { once: true });
548
+ }
549
+ else if (native) {
550
+ if ('InstallTrigger' in globalThis) {
551
+ module$1 = (await import(/* webpackIgnore: true */'./broadcast-CbbtYobq.js')).default;
552
+ }
553
+ else {
554
+ module$1 = (await import(/* webpackIgnore: true */'./message-Cxjrx_su.js')).default;
555
+ }
556
+ }
557
+ else if (navigator.serviceWorker) {
558
+ module$1 = (await import(/* webpackIgnore: true */'./xhr-DSsSGsb-.js')).default;
559
+ }
560
+ else {
561
+ module$1 = (await import(/* webpackIgnore: true */'./async-DLrN8K6M.js').then(function (n) { return n.a; })).default;
562
+ }
563
+
564
+ var reflected = module$1;
565
+
566
+ var worker$1 = async options => {
567
+ const target = create(null);
568
+ const send = await reflected({
569
+ ...options,
570
+ onsend: ([name, args]) => target[name](...args),
571
+ });
572
+ return new Proxy(target, {
573
+ get(target, name) {
574
+ // the curse of potentially awaiting proxies in the wild
575
+ // requires this ugly guard around `then`
576
+ if (name === 'then') return;
577
+ return target[name] ?? (target[name] = (...args) => send([name, args]));
578
+ },
579
+ // @ts-ignore
580
+ set(target, name, value) {
581
+ const ok = name !== 'then';
582
+ if (ok) target[name] = value;
583
+ return ok;
584
+ },
585
+ });
586
+ };
587
+
588
+ /**
589
+ * @param {import('../index.js').WorkerOptions & import('reflected-ffi/remote').RemoteOptions} options
590
+ */
591
+ var worker = async options => {
592
+ const proxy = await worker$1(options);
593
+
594
+ const ffi = remote({
595
+ timeout: 0,
596
+ buffer: true,
597
+ ...options,
598
+ reflect: (...args) => proxy[MAIN](args),
599
+ });
600
+
601
+ proxy[WORKER] = ffi.reflect;
602
+
603
+ // @ts-ignore
604
+ ffi.ws = options?.ws?.(
605
+ // ondata
606
+ fn => {
607
+ proxy[DRIVER] = fn;
608
+ },
609
+ // send data
610
+ proxy[SOCKET]
611
+ );
612
+
613
+ return { ffi, proxy };
614
+ };
615
+
616
+ export { worker as default };