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/ffi/main.js CHANGED
@@ -1 +1,650 @@
1
- import{S as e,f as t,o as r,i as s,a as n,b as a,c as o,R as i,d as c,e as l,F as u,A as f,O as g,t as p,B as d,g as m,h as y,I as w,j as h,V as b,k as v,l as j,m as T,n as x,D as E,p as S,w as k,q as M,r as P}from"./shared-Cs1s8cy4.js";import{t as A}from"./global-B3nXXiRL.js";import{i as R}from"./i32-BkNtpHwD.js";import{n as B}from"./shared-array-buffer-cwdMr2mc.js";const I="M:"+e,L="W:"+e,O="S:"+e,U="D:"+e;let W=0;const $=W++,D=W++,F=W++,N=W++,q=W++,z=W++,V=W++,_=W++,K=W++,Y=W++,C=W++,G=W++,H=W++,J=W++,Q=W++;W++;const X=W++,Z=W++,ee=(e,r)=>[r?e:t(new Uint8Array(e)),e.resizable?e.maxByteLength:0],te=/\[('|")?(.+?)\1\]/g,re=(e,t)=>e?.[t];var se=(e,t)=>t.replace(te,".$2").split(".").reduce(re,e),ne=(e,...t)=>t.map(ae,e);function ae(e){return"string"==typeof e?se(this,e):this[e]}const oe=globalThis.Node||class{},{apply:ie,construct:ce,defineProperty:le,deleteProperty:ue,get:fe,getOwnPropertyDescriptor:ge,getPrototypeOf:pe,has:de,isExtensible:me,ownKeys:ye,set:we,setPrototypeOf:he}=Reflect;var be=({reflect:e=s,transform:t=s,remote:k=s,module:M=e=>import(e),buffer:P=!1,timeout:B=-1}=r)=>{const I=(t,r=new Map)=>{if(!o(t))return t;const[s,n]=t;switch(s){case g:{if(null===n)return globalThis;let e=r.get(t);if(!e){e=n,r.set(t,n);for(const e in n)n[e]=I(n[e],r)}return e}case f:return r.get(t)||(r.set(t,n),O(n,r));case u:{let t=Te.get(n),r=t?.deref();return r||(t&&Ee.unregister(t),r=function(...t){k.apply(this,t);for(let e=0,r=t.length;e<r;e++)t[e]=L(t[e]);return e(z,n,L(this),t).then(I)},t=new WeakRef(r),Te.set(n,t),Ee.register(r,n,t)),r}case c:return l(n);default:return s&i?ae(n):n}},L=e=>{switch(typeof e){case"object":{if(null===e)break;if(e===globalThis)return xe;const r=t(e);return ke&&Se.has(r)||r instanceof w?p(E,r):h(r)?p(b,((e,t)=>{const{BYTES_PER_ELEMENT:r,byteOffset:s,buffer:n,length:a}=e;return[A(e),ee(n,t),s,a!==(n.byteLength-s)/r?a:0]})(r,P)):r instanceof ArrayBuffer?p(v,ee(r,P)):p(o(r)?j:T,re(r))}case"function":return p(y,re(t(e)));case"symbol":return p(c,m(e));case"bigint":return p(d,e.toString())}return e},O=x(I),U=x(a),W=x(S),{clear:te,id:re,ref:ae,unref:be}=((e=0,t=new Map,r=new Map)=>{const s=R(e);return{clear:()=>{t.clear(),r.clear()},id:e=>{let n=r.get(e);if(void 0===n){for(;t.has(n=s()););t.set(n,e),r.set(e,n)}return n},ref:e=>t.get(e),unref:e=>(r.delete(t.get(e)),t.delete(e))}})(),ve=/^(?:[0-9]+|length)$/,je=-1<B,Te=new Map,xe=p(g,null),Ee=new FinalizationRegistry(t=>{Te.delete(t),e($,t)});let Se,ke=!1;return{assign:n,gather:ne,query:se,direct:e=>(ke||(ke=!0,Se=new WeakSet),Se.add(e),e),evaluate:(e,...t)=>ie(e,null,t),reflect(e,t,...r){const s=null===t,i=s?globalThis:ae(t);switch(e){case Y:{const e=a(r[0]),t=s&&"import"===e,n=t?M:fe(i,e),c=L(n);if(!je)return c;let l,u=t,f=i;if(!t&&!(f instanceof oe||o(f)&&"string"==typeof e&&ve.test(e)))if(e in i){for(;!(l=ge(f,e))&&(f=pe(f),f););u=!!l&&"value"in l}else u=!0;return[u,c]}case z:{const e=new Map;return L(ie(i,I(r[0],e),O(r[1],e)))}case X:return we(i,a(r[0]),I(r[1]));case H:return de(i,a(r[0]));case Q:return W(ye(i),Te);case V:return L(ce(i,O(r[0])));case C:{const e=ge(i,a(r[0]));if(e)for(const t in e)e[t]=L(e[t]);return e}case _:return le(i,a(r[0]),I(r[1]));case K:return ue(i,a(r[0]));case G:return L(pe(i));case Z:return he(i,I(r[0]));case D:return void n(i,I(r[0]));case F:{const e=I(r[0]),t=Function(`return(${e}).apply(null,arguments)`);return L(ie(t,null,O(r[1])))}case N:for(let e,t=0,s=(r=U(r[0],Te)).length;t<s;t++)e=r[t],r[t]=L("string"==typeof e?se(i,e):i[e]);return r;case q:return L(se(i,r[0]));case $:return be(t);case J:return me(i)}},terminate(){for(const e of Te.values())Ee.unregister(e);Te.clear(),te()}}};let ve;if("importScripts"in globalThis){let e;const{promise:t,resolve:r}=k(),s=new URL(location).searchParams.get("reflected");e="message"===s?import("./message-DW2TgKPr.js"):"broadcast"===s?import("./broadcast-CBUwPa0e.js"):"xhr"===s?import("./xhr-BkOoxiJZ.js"):import("./async-Dl9wCRDA.js"),ve=async r=>{const{data:s,ports:n}=await t,{default:a}=await e,o=new Event("message");return o.data=s,o.ports=n,dispatchEvent(o),a(r)},addEventListener("message",r,{once:!0})}else ve=B?"InstallTrigger"in globalThis?(await import("./broadcast-D7p6b88J.js")).default:(await import("./message-TyU9xCIl.js")).default:navigator.serviceWorker?(await import("./xhr-CasCWs_z.js")).default:(await import("./async-F0rxZjV9.js").then(function(e){return e.a})).default;var je=ve;var Te=async(e,t)=>{let r=0,s="";const n=t.debug??!1,a=P(null),o=await je(e,{...t,onsync:n?async([e,t])=>{s=e;try{return await(a[e]?.(...t))}finally{s=""}}:([e,t])=>a[e]?.(...t)});return M(o,{proxy:new Proxy(a,{get:n?(e,t)=>e[t]??(e[t]=async(...e)=>{s&&(r=setTimeout(console.warn,3e3,((e,t)=>`💀🔒 - main.${String(e)}() is invoking worker.${String(t)}()`)(s,t)));try{return await o.send([t,e])}finally{s="",clearTimeout(r)}}):(e,t)=>e[t]??(e[t]=(...e)=>o.send([t,e])),set:(e,t,r)=>{const s="then"!==t;return s&&(e[t]=r),s}})})};const xe=new TextEncoder,Ee=new Uint8Array(4),Se=new DataView(Ee.buffer),ke=(e,t,r)=>{const s=new Uint8Array(5+r.length);return s.set(e,0),s[4]=t,s.set(r,5),s};var Me=async(e,t)=>{const r=await Te(e,t),{proxy:s}=r,{terminate:n}=r,a=()=>{o?.close(),i.terminate(),n.call(r)};let o;if(t?.ws){const[e,r]=((e=e=>e)=>{const t=new Map,r=R();return[()=>{let s;do{s=e(r())}while(t.has(s));const n=k();return t.set(s,n),[s,n.promise]},(e,r,s)=>{const n=t.get(e);t.delete(e),s?n?.reject(s):n?.resolve(r)}]})(Number),[n,i]=e(),c=s[U];o=new WebSocket(t?.ws),o.onerror=console.error,o.onclose=a,o.onopen=async()=>{Se.setInt32(0,0,!0),o.send(ke(Ee,0,xe.encode(O))),r(n)},o.onmessage=async e=>{const t=new Uint8Array(await e.data.arrayBuffer()),s=t.slice(5),n=t[4];if(Ee.set(t,0),1===n)r(Se.getInt32(0,!0),s);else if(2===n){const e=await c(s);o.send(ke(t.subarray(0,4),2,e))}},s[O]=async t=>{await i;const[r,s]=e();return Se.setInt32(0,r,!0),o.send(ke(Ee,1,t)),s}}const i=be({timeout:0,buffer:!0,...t,reflect:s[L]});return s[I]=e=>i.reflect(...e),M(r,{ffi:i,terminate:a})};export{Me as default};
1
+ import { S as SHARED_CHANNEL, f as fromArray, o as object, i as identity, a as assign, b as fromKey, c as isArray, R as REMOTE, d as SYMBOL, e as fromSymbol, F as FUNCTION, A as ARRAY, O as OBJECT, t as tv, B as BIGINT, g as toSymbol, h as REMOTE_FUNCTION, I as ImageData, j as isView, V as VIEW, k as BUFFER, l as REMOTE_ARRAY, m as REMOTE_OBJECT, n as loopValues, D as DIRECT, p as toKey, w as withResolvers, q as assign$1, r as create } from './shared-5Nhc4gvB.js';
2
+ import { t as toTag } from './global-LTfnrHcF.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
+ /**
39
+ * @param {ArrayBufferLike} value
40
+ * @param {boolean} direct
41
+ * @returns {BufferDetails}
42
+ */
43
+ const toBuffer = (value, direct) => [
44
+ direct ? value : fromArray(new Uint8Array(value)),
45
+ //@ts-ignore
46
+ value.resizable ? value.maxByteLength : 0
47
+ ];
48
+
49
+ /**
50
+ * @param {ArrayBufferView} value
51
+ * @param {boolean} direct
52
+ * @returns {ViewDetails}
53
+ */
54
+ const toView = (value, direct) => {
55
+ //@ts-ignore
56
+ const { BYTES_PER_ELEMENT, byteOffset, buffer, length } = value;
57
+ return [
58
+ toTag(value),
59
+ toBuffer(buffer, direct),
60
+ byteOffset,
61
+ length !== ((buffer.byteLength - byteOffset) / BYTES_PER_ELEMENT) ? length : 0,
62
+ ];
63
+ };
64
+
65
+ const brackets = /\[('|")?(.+?)\1\]/g;
66
+
67
+ const keys = (target, key) => target?.[key];
68
+
69
+ /**
70
+ * Parses the given path and returns the value at the given target.
71
+ * @param {any} target
72
+ * @param {string} path
73
+ * @returns {any}
74
+ */
75
+ var query = (target, path) => path.replace(brackets, '.$2').split('.').reduce(keys, target);
76
+
77
+ /**
78
+ * Parses each given path and returns each value at the given target.
79
+ * @param {any} target
80
+ * @param {...(string|symbol)[]} keys
81
+ * @returns {any[]}
82
+ */
83
+ var gather = (target, ...keys) => keys.map(asResult, target);
84
+
85
+ function asResult(key) {
86
+ return typeof key === 'string' ? query(this, key) : this[key];
87
+ }
88
+
89
+ /**
90
+ * @template T
91
+ * @typedef {Object} Heap
92
+ * @property {() => void} clear
93
+ * @property {(ref:T) => number} id
94
+ * @property {(id:number) => T} ref
95
+ * @property {(id:number) => boolean} unref
96
+ */
97
+
98
+ /**
99
+ * Create a heap-like utility to hold references in memory.
100
+ * @param {number} [id=0] The initial `id` which is `0` by default.
101
+ * @param {Map<number, any>} [ids=new Map] The used map of ids to references.
102
+ * @param {Map<any, number>} [refs=new Map] The used map of references to ids.
103
+ * @returns {Heap<any>}
104
+ */
105
+ var heap = (id = 0, ids = new Map, refs = new Map) => {
106
+ const next = i32(id);
107
+ return {
108
+ clear: () => {
109
+ ids.clear();
110
+ refs.clear();
111
+ },
112
+ id: ref => {
113
+ let uid = refs.get(ref);
114
+ if (uid === void 0) {
115
+ /* c8 ignore next */
116
+ while (ids.has(uid = next()));
117
+ ids.set(uid, ref);
118
+ refs.set(ref, uid);
119
+ }
120
+ return uid;
121
+ },
122
+ ref: id => ids.get(id),
123
+ unref: id => {
124
+ refs.delete(ids.get(id));
125
+ return ids.delete(id);
126
+ },
127
+ };
128
+ };
129
+
130
+ // import DEBUG from './utils/debug.js';
131
+
132
+
133
+ const Node = globalThis.Node || class Node {};
134
+
135
+ const {
136
+ apply,
137
+ construct,
138
+ defineProperty,
139
+ deleteProperty,
140
+ get,
141
+ getOwnPropertyDescriptor,
142
+ getPrototypeOf,
143
+ has,
144
+ isExtensible,
145
+ ownKeys,
146
+ set,
147
+ setPrototypeOf,
148
+ } = Reflect;
149
+
150
+ /**
151
+ * @typedef {Object} LocalOptions Optional utilities used to orchestrate local <-> remote communication.
152
+ * @property {Function} [reflect=identity] The function used to reflect operations via the remote receiver. Currently only `apply` and `unref` are supported.
153
+ * @property {Function} [transform=identity] The function used to transform local values into simpler references that the remote side can understand.
154
+ * @property {Function} [remote=identity] The function used to intercept remote invokes *before* these happen. Usable to sync `events` or do other tasks.
155
+ * @property {Function} [module] The function used to import modules when remote asks to `import(...)` something.
156
+ * @property {boolean} [buffer=false] Optionally allows direct buffer serialization breaking JSON compatibility.
157
+ * @property {number} [timeout=-1] Optionally allows remote values to be cached when possible for a `timeout` milliseconds value. `-1` means no timeout.
158
+ */
159
+
160
+ /**
161
+ * @param {LocalOptions} options
162
+ * @returns
163
+ */
164
+ var local = ({
165
+ reflect = identity,
166
+ transform = identity,
167
+ remote = identity,
168
+ module = name => import(name),
169
+ buffer = false,
170
+ timeout = -1,
171
+ } = object) => {
172
+ // received values arrive via postMessage so are compatible
173
+ // with the structured clone algorithm
174
+ const fromValue = (value, cache = new Map) => {
175
+ if (!isArray(value)) return value;
176
+ const [t, v] = value;
177
+ switch (t) {
178
+ case OBJECT: {
179
+ if (v === null) return globalThis;
180
+ let cached = cache.get(value);
181
+ if (!cached) {
182
+ cached = v;
183
+ cache.set(value, v);
184
+ for (const k in v) v[k] = fromValue(v[k], cache);
185
+ }
186
+ return cached;
187
+ }
188
+ case ARRAY: {
189
+ return cache.get(value) || (
190
+ cache.set(value, v),
191
+ fromValues(v, cache)
192
+ );
193
+ }
194
+ case FUNCTION: {
195
+ let wr = weakRefs.get(v), fn = wr?.deref();
196
+ if (!fn) {
197
+ /* c8 ignore start */
198
+ if (wr) fr.unregister(wr);
199
+ /* c8 ignore stop */
200
+ fn = function (...args) {
201
+ remote.apply(this, args);
202
+
203
+ // values reflected asynchronously are not passed stringified
204
+ // because it makes no sense to use Atomics and SharedArrayBuffer
205
+ // to transfer these ... yet these must reflect the current state
206
+ // on this local side of affairs.
207
+ for (let i = 0, length = args.length; i < length; i++)
208
+ args[i] = toValue(args[i]);
209
+
210
+ const result = reflect(APPLY, v, toValue(this), args);
211
+ return result.then(fromValue);
212
+ };
213
+ wr = new WeakRef(fn);
214
+ weakRefs.set(v, wr);
215
+ fr.register(fn, v, wr);
216
+ }
217
+ return fn;
218
+ }
219
+ case SYMBOL: return fromSymbol(v);
220
+ default: return (t & REMOTE) ? ref(v) : v;
221
+ }
222
+ };
223
+
224
+ // OBJECT, DIRECT, VIEW, BUFFER, REMOTE_ARRAY, REMOTE_OBJECT, REMOTE_FUNCTION, SYMBOL, BIGINT
225
+ /**
226
+ * Converts values into TypeValue pairs when these
227
+ * are not JSON compatible (symbol, bigint) or
228
+ * local (functions, arrays, objects, globalThis).
229
+ * @param {any} value the current value
230
+ * @returns {any} the value as is or its TypeValue counterpart
231
+ */
232
+ const toValue = value => {
233
+ switch (typeof value) {
234
+ case 'object': {
235
+ if (value === null) break;
236
+ if (value === globalThis) return globalTarget;
237
+ const $ = transform(value);
238
+ return ((hasDirect && direct.has($)) || $ instanceof ImageData) ?
239
+ tv(DIRECT, $) : (
240
+ isView($) ?
241
+ tv(VIEW, toView($, buffer)) : (
242
+ $ instanceof ArrayBuffer ?
243
+ tv(BUFFER, toBuffer($, buffer)) :
244
+ tv(isArray($) ? REMOTE_ARRAY : REMOTE_OBJECT, id($))
245
+ )
246
+ );
247
+ }
248
+ case 'function': return tv(REMOTE_FUNCTION, id(transform(value)));
249
+ case 'symbol': return tv(SYMBOL, toSymbol(value));
250
+ case 'bigint': return tv(BIGINT, value.toString());
251
+ }
252
+ return value;
253
+ };
254
+
255
+ const fromValues = loopValues(fromValue);
256
+ const fromKeys = loopValues(fromKey);
257
+ const toKeys = loopValues(toKey);
258
+
259
+ const { clear, id, ref, unref } = heap();
260
+
261
+ const arrayKey = /^(?:[0-9]+|length)$/;
262
+ const memoize = -1 < timeout;
263
+ const weakRefs = new Map;
264
+ const globalTarget = tv(OBJECT, null);
265
+ const fr = new FinalizationRegistry(v => {
266
+ weakRefs.delete(v);
267
+ reflect(UNREF, v);
268
+ });
269
+
270
+ let hasDirect = false, direct;
271
+
272
+ return {
273
+ assign,
274
+ gather,
275
+ query,
276
+
277
+ /**
278
+ * Alows local references to be passed directly to the remote receiver,
279
+ * either as copy or serliazied values (it depends on the implementation).
280
+ * @template {WeakKey} T
281
+ * @param {T} value
282
+ * @returns {T}
283
+ */
284
+ direct(value) {
285
+ if (!hasDirect) {
286
+ // if (DEBUG) console.debug('DIRECT');
287
+ hasDirect = true;
288
+ direct = new WeakSet;
289
+ }
290
+ direct.add(value);
291
+ return value;
292
+ },
293
+
294
+ /**
295
+ * Provide a portable API that just invokes the given callback with the given arguments.
296
+ * @param {Function} callback
297
+ * @param {...any} args
298
+ * @returns {any}
299
+ */
300
+ evaluate: (callback, ...args) => apply(callback, null, args),
301
+
302
+ /**
303
+ * This callback reflects locally every remote call.
304
+ * It accepts TypeValue pairs but it always returns a string
305
+ * to make it possible to use Atomics and SharedArrayBuffer.
306
+ * @param {number} method
307
+ * @param {number?} uid
308
+ * @param {...any} args
309
+ * @returns
310
+ */
311
+ reflect(method, uid, ...args) {
312
+ // if (DEBUG) console.debug(method === UNREF ? 'GC' : 'ROUNDTRIP');
313
+ const isGlobal = uid === null;
314
+ const target = isGlobal ? globalThis : ref(uid);
315
+ // the order is by most common use cases
316
+ switch (method) {
317
+ case GET: {
318
+ const key = fromKey(args[0]);
319
+ const asModule = isGlobal && key === 'import';
320
+ const value = asModule ? module : get(target, key);
321
+ const result = toValue(value);
322
+ if (!memoize) return result;
323
+ let cache = asModule, t = target, d;
324
+ if (!asModule && !(
325
+ // avoid caching DOM related stuff (all accessors)
326
+ (t instanceof Node) ||
327
+ // avoid also caching Array length or index accessors
328
+ (isArray(t) && typeof key === 'string' && arrayKey.test(key))
329
+ )) {
330
+ // cache unknown properties but ...
331
+ if (key in target) {
332
+ // ... avoid caching accessors!
333
+ while (!(d = getOwnPropertyDescriptor(t, key))) {
334
+ t = getPrototypeOf(t);
335
+ /* c8 ignore start */
336
+ // this is an emergency case for "unknown" values
337
+ if (!t) break;
338
+ /* c8 ignore stop */
339
+ }
340
+ cache = !!d && 'value' in d;
341
+ }
342
+ // accessing non existent properties could be repeated
343
+ // for no reason whatsoever and it gets removed once
344
+ // the property is eventually set so ...
345
+ else cache = true;
346
+ }
347
+ return [cache, result];
348
+ }
349
+ case APPLY: {
350
+ const map = new Map;
351
+ return toValue(apply(target, fromValue(args[0], map), fromValues(args[1], map)));
352
+ }
353
+ case SET: return set(target, fromKey(args[0]), fromValue(args[1]));
354
+ case HAS: return has(target, fromKey(args[0]));
355
+ case OWN_KEYS: return toKeys(ownKeys(target), weakRefs);
356
+ case CONSTRUCT: return toValue(construct(target, fromValues(args[0])));
357
+ case GET_OWN_PROPERTY_DESCRIPTOR: {
358
+ const descriptor = getOwnPropertyDescriptor(target, fromKey(args[0]));
359
+ if (descriptor) {
360
+ for (const k in descriptor)
361
+ descriptor[k] = toValue(descriptor[k]);
362
+ }
363
+ return descriptor;
364
+ }
365
+ case DEFINE_PROPERTY: return defineProperty(target, fromKey(args[0]), fromValue(args[1]));
366
+ case DELETE_PROPERTY: return deleteProperty(target, fromKey(args[0]));
367
+ case GET_PROTOTYPE_OF: return toValue(getPrototypeOf(target));
368
+ case SET_PROTOTYPE_OF: return setPrototypeOf(target, fromValue(args[0]));
369
+ case ASSIGN: {
370
+ assign(target, fromValue(args[0]));
371
+ return;
372
+ }
373
+ case EVALUATE: {
374
+ const body = fromValue(args[0]);
375
+ const fn = Function(`return(${body}).apply(null,arguments)`);
376
+ return toValue(apply(fn, null, fromValues(args[1])));
377
+ }
378
+ case GATHER: {
379
+ args = fromKeys(args[0], weakRefs);
380
+ for (let k, i = 0, length = args.length; i < length; i++) {
381
+ k = args[i];
382
+ args[i] = toValue(typeof k === 'string' ? query(target, k) : target[k]);
383
+ }
384
+ return args;
385
+ }
386
+ case QUERY: return toValue(query(target, args[0]));
387
+ case UNREF: return unref(uid);
388
+ case IS_EXTENSIBLE: return isExtensible(target);
389
+ }
390
+ },
391
+
392
+ /**
393
+ * Terminates the local side of the communication,
394
+ * erasing and unregistering all the cached references.
395
+ */
396
+ terminate() {
397
+ for (const wr of weakRefs.values()) fr.unregister(wr);
398
+ weakRefs.clear();
399
+ clear();
400
+ },
401
+ };
402
+ };
403
+
404
+ //@ts-check
405
+
406
+
407
+ /**
408
+ * @template V
409
+ * @callback Resolve
410
+ * @param {V?} [value]
411
+ * @returns {void}
412
+ */
413
+
414
+ /**
415
+ * @callback Reject
416
+ * @param {any?} [error]
417
+ * @returns {void}
418
+ */
419
+
420
+ /**
421
+ * @template V
422
+ * @typedef {object} Resolvers
423
+ * @prop {Promise<V>} promise
424
+ * @prop {Resolve<V>} resolve
425
+ * @prop {Reject} reject
426
+ */
427
+
428
+ /**
429
+ * @template K,V
430
+ * @typedef {() => [K, Promise<V>]} Next
431
+ */
432
+
433
+ /**
434
+ * @template K,V
435
+ * @callback Resolver
436
+ * @param {K} uid
437
+ * @param {V?} [value]
438
+ * @param {any?} [error]
439
+ */
440
+
441
+ /**
442
+ * @template K,V
443
+ * @typedef {[Next<K,V>, Resolver<K,V>]} NextResolver
444
+ */
445
+
446
+ /**
447
+ * @template K,V
448
+ * @param {(id: number) => K} [as]
449
+ * @returns
450
+ */
451
+ var nextResolver = (as = (id => /** @type {K} */(id))) => {
452
+ /** @type {Map<K,Resolvers<V>>} */
453
+ const map = new Map;
454
+ const next = i32();
455
+ return /** @type {NextResolver<K,V>} */([
456
+ /** @type {Next<K,V>} */
457
+ () => {
458
+ let uid;
459
+ do { uid = as(next()); }
460
+ while (map.has(uid));
461
+ const wr = /** @type {Resolvers<V>} */(/** @type {unknown} */(withResolvers()));
462
+ map.set(uid, wr);
463
+ return [uid, wr.promise];
464
+ },
465
+ /** @type {Resolver<K,V>} */
466
+ (uid, value, error) => {
467
+ const wr = map.get(uid);
468
+ map.delete(uid);
469
+ if (error) wr?.reject(error);
470
+ else wr?.resolve(value);
471
+ },
472
+ ]);
473
+ };
474
+
475
+ /** @type {Function} */
476
+ let module$1;
477
+
478
+ if ('importScripts' in globalThis) {
479
+ let get;
480
+ const { promise, resolve } = withResolvers();
481
+ // @ts-ignore
482
+ const reflected = new URL(location).searchParams.get('reflected');
483
+ if (reflected === 'message') get = import(/* webpackIgnore: true */'./message-2Dqj3uZ3.js');
484
+ else if (reflected === 'broadcast') get = import(/* webpackIgnore: true */'./broadcast-uydYnG6E.js');
485
+ else if (reflected === 'xhr') get = import(/* webpackIgnore: true */'./xhr-CKhf21P8.js');
486
+ else get = import(/* webpackIgnore: true */'./async-CbwemiL2.js');
487
+ module$1 = async options => {
488
+ const { data, ports } = await promise;
489
+ const { default: reflect } = await get;
490
+ const event = new Event('message');
491
+ // @ts-ignore
492
+ event.data = data;
493
+ // @ts-ignore
494
+ event.ports = ports;
495
+ dispatchEvent(event);
496
+ return reflect(options);
497
+ };
498
+ addEventListener('message', resolve, { once: true });
499
+ }
500
+ else if (native) {
501
+ if ('InstallTrigger' in globalThis) {
502
+ module$1 = (await import(/* webpackIgnore: true */'./broadcast-DIucqhL0.js')).default;
503
+ }
504
+ else {
505
+ module$1 = (await import(/* webpackIgnore: true */'./message-Co0Oew7f.js')).default;
506
+ }
507
+ }
508
+ else if (navigator.serviceWorker) {
509
+ module$1 = (await import(/* webpackIgnore: true */'./xhr-Be-3qHQb.js')).default;
510
+ }
511
+ else {
512
+ module$1 = (await import(/* webpackIgnore: true */'./async-D6Zj6i_Q.js').then(function (n) { return n.a; })).default;
513
+ }
514
+
515
+ var reflected = module$1;
516
+
517
+ /**
518
+ * @param {string | symbol} syncing
519
+ * @param {string | symbol} name
520
+ * @returns
521
+ */
522
+ const deadlock = (syncing, name) => `💀🔒 - main.${String(syncing)}() is invoking worker.${String(name)}()`;
523
+
524
+ var main$1 = async (url, options) => {
525
+ let t = 0, syncing = '';
526
+ const debug = options.debug ?? false;
527
+ const target = create(null);
528
+ const worker = await reflected(url, {
529
+ ...options,
530
+ onsync: (
531
+ debug ?
532
+ (async ([name, args]) => {
533
+ syncing = name;
534
+ try { return await target[name]?.(...args) }
535
+ finally { syncing = ''; }
536
+ }) :
537
+ (([name, args]) => target[name]?.(...args))
538
+ )
539
+ });
540
+ return assign$1(worker, {
541
+ proxy: new Proxy(target, {
542
+ get: debug ?
543
+ (target, name) => (
544
+ target[name] ??
545
+ (target[name] = async (...args) => {
546
+ if (syncing) t = setTimeout(console.warn, 3e3, deadlock(syncing, name));
547
+ try { return await worker.send([name, args]) }
548
+ finally {
549
+ syncing = '';
550
+ clearTimeout(t);
551
+ }
552
+ })
553
+ ) :
554
+ ((target, name) => (
555
+ target[name] ?? (target[name] = (...args) => worker.send([name, args]))
556
+ )),
557
+ set: (target, name, value) => {
558
+ const ok = name !== 'then';
559
+ if (ok) target[name] = value;
560
+ return ok;
561
+ },
562
+ }),
563
+ });
564
+ };
565
+
566
+ const BYTES = 4; // Int32Array.BYTES_PER_ELEMENT;
567
+ const encoder = new TextEncoder;
568
+ const uid8View = new Uint8Array(BYTES);
569
+ const uid32Data = new DataView(uid8View.buffer);
570
+
571
+ /**
572
+ *
573
+ * @param {Uint8Array} prefix
574
+ * @param {0 | 1 | 2} type
575
+ * @param {Uint8Array} data
576
+ * @returns
577
+ */
578
+ const asUint8View = (prefix, type, data) => {
579
+ const ui8a = new Uint8Array(BYTES + 1 + data.length);
580
+ ui8a.set(prefix, 0);
581
+ ui8a[BYTES] = type;
582
+ ui8a.set(data, BYTES + 1);
583
+ return ui8a;
584
+ };
585
+
586
+ /**
587
+ * @param {string} url
588
+ * @param {import('../index.js').MainOptions & import('reflected-ffi/local').LocalOptions} options
589
+ */
590
+ var main = async (url, options) => {
591
+ const worker = await main$1(url, options);
592
+ const { proxy } = worker;
593
+
594
+ const { terminate: end } = worker;
595
+ const terminate = () => {
596
+ ws?.close();
597
+ ffi.terminate();
598
+ end.call(worker);
599
+ };
600
+
601
+ let ws;
602
+ if (options?.ws) {
603
+ const [next, resolve] = nextResolver(Number);
604
+ const [uid, opened] = next();
605
+ const worker = proxy[DRIVER];
606
+ ws = new WebSocket(options?.ws);
607
+ ws.onerror = console.error;
608
+ ws.onclose = terminate;
609
+ ws.onopen = async () => {
610
+ uid32Data.setInt32(0, 0, true);
611
+ ws.send(asUint8View(uid8View, 0, encoder.encode(SOCKET)));
612
+ resolve(uid);
613
+ };
614
+ ws.onmessage = async event => {
615
+ const view = new Uint8Array(await event.data.arrayBuffer());
616
+ // TODO: try subarray instead
617
+ const data = view.slice(BYTES + 1);
618
+ const type = view[BYTES];
619
+ uid8View.set(view.subarray(0, BYTES), 0);
620
+ if (type === 1)
621
+ resolve(uid32Data.getInt32(0, true), data);
622
+ else if (type === 2) {
623
+ const result = await worker(data);
624
+ ws.send(asUint8View(view.subarray(0, BYTES), 2, result));
625
+ }
626
+ };
627
+ proxy[SOCKET] = async data => {
628
+ await opened;
629
+ const [uid, promise] = next();
630
+ uid32Data.setInt32(0, uid, true);
631
+ ws.send(asUint8View(uid8View, 1, data));
632
+ return promise;
633
+ };
634
+ }
635
+
636
+ const ffi = local({
637
+ timeout: 0,
638
+ buffer: true,
639
+ ...options,
640
+ // @ts-ignore
641
+ reflect: proxy[WORKER],
642
+ });
643
+
644
+ // @ts-ignore
645
+ proxy[MAIN] = args => ffi.reflect(...args);
646
+
647
+ return assign$1(worker, { ffi, terminate });
648
+ };
649
+
650
+ export { main as default };
@@ -0,0 +1,15 @@
1
+ import { w as withResolvers } from './shared-5Nhc4gvB.js';
2
+ import { h as handler } from './shared-DZdgWBc6.js';
3
+ import './sender-B7Xmjz4q.js';
4
+ import './views-C3SJnvMr.js';
5
+
6
+ const { promise, resolve } = withResolvers();
7
+
8
+ const channel = 'message';
9
+
10
+ var message = handler(
11
+ promise,
12
+ ({ data: [sab, main], ports: [channel] }) => resolve([sab, main, channel])
13
+ );
14
+
15
+ export { channel, message as default };
@@ -0,0 +1,25 @@
1
+ import { b as bootstrap, S as Sender, a as SAB, h as handler, u as url, p as post } from './shared-ddawRQD0.js';
2
+ import './shared-5Nhc4gvB.js';
3
+ import './i32-Cw-Rqr5y.js';
4
+ import './views-C3SJnvMr.js';
5
+ import './global-LTfnrHcF.js';
6
+
7
+ const CHANNEL = 'message';
8
+
9
+ var message = bootstrap(class Worker extends Sender {
10
+ constructor(scriptURL, options, resolve) {
11
+ const { port1, port2 } = new MessageChannel;
12
+ const sab = SAB(options);
13
+ port1.addEventListener(CHANNEL, handler(sab, options, true));
14
+ port1.start();
15
+ super(...url(scriptURL, CHANNEL, options));
16
+ super.addEventListener(CHANNEL, () => resolve(this), { once: true });
17
+ super.postMessage(post(sab, options), [port2]);
18
+ }
19
+
20
+ get channel() {
21
+ return CHANNEL;
22
+ }
23
+ });
24
+
25
+ export { message as default };