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.
- package/dist/async-CM1iXZPW.js +44 -0
- package/dist/async-OLiknCrf.js +41 -0
- package/dist/async.js +856 -1
- package/dist/broadcast-B4sl9DBp.js +24 -0
- package/dist/broadcast-eR5_7TCY.js +15 -0
- package/dist/broadcast.js +850 -1
- package/dist/ffi/async-0Q0H4a_N.js +41 -0
- package/dist/ffi/async-CbwemiL2.js +41 -0
- package/dist/ffi/async-D6Zj6i_Q.js +44 -0
- package/dist/ffi/async-DLrN8K6M.js +44 -0
- package/dist/ffi/broadcast-CKNplfox.js +15 -0
- package/dist/ffi/broadcast-CbbtYobq.js +25 -0
- package/dist/ffi/broadcast-DIucqhL0.js +25 -0
- package/dist/ffi/broadcast-uydYnG6E.js +15 -0
- package/dist/ffi/global-DzpOeFX2.js +14 -0
- package/dist/ffi/global-LTfnrHcF.js +11 -0
- package/dist/ffi/i32-Cw-Rqr5y.js +13 -0
- package/dist/ffi/main.js +650 -1
- package/dist/ffi/message-2Dqj3uZ3.js +15 -0
- package/dist/ffi/message-Co0Oew7f.js +25 -0
- package/dist/ffi/message-Cxjrx_su.js +25 -0
- package/dist/ffi/message-mK0eTb54.js +15 -0
- package/dist/ffi/sender-B7Xmjz4q.js +194 -0
- package/dist/ffi/sender-C0gu9RzR.js +194 -0
- package/dist/ffi/shared-5Nhc4gvB.js +175 -0
- package/dist/ffi/shared-B3KmQkGh.js +425 -0
- package/dist/ffi/shared-CjWXp6-D.js +171 -0
- package/dist/ffi/shared-Czh3AlGt.js +53 -0
- package/dist/ffi/shared-DZdgWBc6.js +53 -0
- package/dist/ffi/shared-array-buffer-BQgQXQvC.js +28 -0
- package/dist/ffi/shared-ddawRQD0.js +425 -0
- package/dist/ffi/views-C3SJnvMr.js +48 -0
- package/dist/ffi/views-DBBYng9N.js +52 -0
- package/dist/ffi/worker.js +616 -1
- package/dist/ffi/xhr-Be-3qHQb.js +101 -0
- package/dist/ffi/xhr-CKhf21P8.js +41 -0
- package/dist/ffi/xhr-CRJGE1xP.js +41 -0
- package/dist/ffi/xhr-DSsSGsb-.js +101 -0
- package/dist/i32-UV5fM9Tw.js +12 -0
- package/dist/index.js +57 -1
- package/dist/message-D_SM4ST4.js +24 -0
- package/dist/message-vCWcUEmt.js +15 -0
- package/dist/message.js +843 -1
- package/dist/proxy/main/async-8J3q5T6q.js +41 -0
- package/dist/proxy/main/async-Di4MuQWe.js +44 -0
- package/dist/proxy/main/broadcast-CR8vlkKk.js +24 -0
- package/dist/proxy/main/broadcast-LNW6nFVD.js +15 -0
- package/dist/proxy/main/channel-NcnPEVL4.js +110 -0
- package/dist/proxy/main/i32-UV5fM9Tw.js +12 -0
- package/dist/proxy/main/message-j-4BYSZ4.js +24 -0
- package/dist/proxy/main/message-uirwoNUQ.js +15 -0
- package/dist/proxy/main/proxy.js +106 -1
- package/dist/proxy/main/sender-Dkjjbv2Y.js +194 -0
- package/dist/proxy/main/shared-C3U1MNPd.js +53 -0
- package/dist/proxy/main/shared-CV7CPYuP.js +43 -0
- package/dist/proxy/main/shared-DdPca9Ke.js +431 -0
- package/dist/proxy/main/shared-array-buffer-BQgQXQvC.js +28 -0
- package/dist/proxy/main/xhr-BUjEbkCh.js +100 -0
- package/dist/proxy/main/xhr-WnkM0dVo.js +41 -0
- package/dist/proxy/worker/async-CZJaURwO.js +41 -0
- package/dist/proxy/worker/async-DaSAye0a.js +44 -0
- package/dist/proxy/worker/broadcast-DRVfswix.js +24 -0
- package/dist/proxy/worker/broadcast-fWsZlH8v.js +15 -0
- package/dist/proxy/worker/channel-NcnPEVL4.js +110 -0
- package/dist/proxy/worker/i32-UV5fM9Tw.js +12 -0
- package/dist/proxy/worker/message-Bots8UXM.js +15 -0
- package/dist/proxy/worker/message-sej0J6k5.js +24 -0
- package/dist/proxy/worker/proxy.js +79 -1
- package/dist/proxy/worker/sender-D5Lm-DmC.js +194 -0
- package/dist/proxy/worker/shared-CgOGpfKJ.js +431 -0
- package/dist/proxy/worker/shared-Cn1K0k8G.js +53 -0
- package/dist/proxy/worker/shared-DR7YYduq.js +41 -0
- package/dist/proxy/worker/shared-array-buffer-BQgQXQvC.js +28 -0
- package/dist/proxy/worker/xhr-CQDzS28h.js +100 -0
- package/dist/proxy/worker/xhr-DAgmljtu.js +41 -0
- package/dist/sender-BSx3lj4w.js +193 -0
- package/dist/shared-Ccm1cPah.js +125 -0
- package/dist/shared-DVDPRDN_.js +53 -0
- package/dist/shared-L0wiAfqu.js +431 -0
- package/dist/shared-array-buffer-BQgQXQvC.js +28 -0
- package/dist/sw.js +155 -1
- package/dist/with-resolvers-DsO_nhbc.js +24 -0
- package/dist/xhr-BZOjL92j.js +100 -0
- package/dist/xhr-Crh-419R.js +41 -0
- package/dist/xhr.js +909 -1
- package/package.json +1 -1
- package/reflected.tar.gz +0 -0
- package/dist/async-C2eqvaGm.js +0 -1
- package/dist/async-s2VCmNNG.js +0 -1
- package/dist/broadcast-CArZGSOF.js +0 -1
- package/dist/broadcast-f7NecPmf.js +0 -1
- package/dist/ffi/async-B_TI4xaG.js +0 -1
- package/dist/ffi/async-BeKYhv9s.js +0 -1
- package/dist/ffi/async-Dl9wCRDA.js +0 -1
- package/dist/ffi/async-F0rxZjV9.js +0 -1
- package/dist/ffi/broadcast-CBUwPa0e.js +0 -1
- package/dist/ffi/broadcast-Ce_cOurt.js +0 -1
- package/dist/ffi/broadcast-D0jZEoUb.js +0 -1
- package/dist/ffi/broadcast-D7p6b88J.js +0 -1
- package/dist/ffi/global-B3nXXiRL.js +0 -1
- package/dist/ffi/global-Bs8RLRoX.js +0 -1
- package/dist/ffi/i32-BkNtpHwD.js +0 -1
- package/dist/ffi/message-BdbPt0y5.js +0 -1
- package/dist/ffi/message-DNbm1E-6.js +0 -1
- package/dist/ffi/message-DW2TgKPr.js +0 -1
- package/dist/ffi/message-TyU9xCIl.js +0 -1
- package/dist/ffi/sender-C5EHIpuL.js +0 -1
- package/dist/ffi/sender-DBFDKow4.js +0 -1
- package/dist/ffi/shared-BfrghvHB.js +0 -1
- package/dist/ffi/shared-C2XCOXcu.js +0 -1
- package/dist/ffi/shared-Cs1s8cy4.js +0 -1
- package/dist/ffi/shared-DsgUjYDJ.js +0 -1
- package/dist/ffi/shared-DwSbyGXy.js +0 -1
- package/dist/ffi/shared-DyOzpRO0.js +0 -1
- package/dist/ffi/shared-array-buffer-cwdMr2mc.js +0 -1
- package/dist/ffi/views-CdOIrBQR.js +0 -1
- package/dist/ffi/views-Cr2pQ4B-.js +0 -1
- package/dist/ffi/xhr-BkOoxiJZ.js +0 -1
- package/dist/ffi/xhr-C598zZcp.js +0 -1
- package/dist/ffi/xhr-CasCWs_z.js +0 -1
- package/dist/ffi/xhr-DHyu_Jz0.js +0 -1
- package/dist/i32-C78nBJH2.js +0 -1
- package/dist/message-RcKwy8Ib.js +0 -1
- package/dist/message-l7W12V0c.js +0 -1
- package/dist/proxy/main/async-Bc0QcTaC.js +0 -1
- package/dist/proxy/main/async-CyZdYOvJ.js +0 -1
- package/dist/proxy/main/broadcast-BX9FmxMK.js +0 -1
- package/dist/proxy/main/broadcast-DXhTouQy.js +0 -1
- package/dist/proxy/main/channel-DQBWE7_E.js +0 -1
- package/dist/proxy/main/i32-C78nBJH2.js +0 -1
- package/dist/proxy/main/message-CbPHeJAY.js +0 -1
- package/dist/proxy/main/message-DxRlOVN7.js +0 -1
- package/dist/proxy/main/sender-CNwZwCuI.js +0 -1
- package/dist/proxy/main/shared-BoatDt-V.js +0 -1
- package/dist/proxy/main/shared-FMegJ6yb.js +0 -1
- package/dist/proxy/main/shared-array-buffer-cwdMr2mc.js +0 -1
- package/dist/proxy/main/shared-z_jGYqUa.js +0 -1
- package/dist/proxy/main/xhr-BaBkekSn.js +0 -1
- package/dist/proxy/main/xhr-Fg-l74MV.js +0 -1
- package/dist/proxy/worker/async-Do59YjcF.js +0 -1
- package/dist/proxy/worker/async-fqI-s7gn.js +0 -1
- package/dist/proxy/worker/broadcast-Bdt9gmj_.js +0 -1
- package/dist/proxy/worker/broadcast-D7rjohJL.js +0 -1
- package/dist/proxy/worker/channel-DQBWE7_E.js +0 -1
- package/dist/proxy/worker/i32-C78nBJH2.js +0 -1
- package/dist/proxy/worker/message-BvU2dKHz.js +0 -1
- package/dist/proxy/worker/message-DhtuceT9.js +0 -1
- package/dist/proxy/worker/sender-CWhM7QKB.js +0 -1
- package/dist/proxy/worker/shared-DDIlHBz4.js +0 -1
- package/dist/proxy/worker/shared-NtUlwI0Z.js +0 -1
- package/dist/proxy/worker/shared-QrUiXPgc.js +0 -1
- package/dist/proxy/worker/shared-array-buffer-cwdMr2mc.js +0 -1
- package/dist/proxy/worker/xhr-CK-Z6L0B.js +0 -1
- package/dist/proxy/worker/xhr-JKtROQ3y.js +0 -1
- package/dist/sender-D7tfUn3T.js +0 -1
- package/dist/shared-BCEcO4-0.js +0 -1
- package/dist/shared-BWvJKuCK.js +0 -1
- package/dist/shared-BhhIigP4.js +0 -1
- package/dist/shared-array-buffer-cwdMr2mc.js +0 -1
- package/dist/with-resolvers-CHEvl4oe.js +0 -1
- package/dist/xhr-Bd0lMv0-.js +0 -1
- package/dist/xhr-DaixJn9d.js +0 -1
package/dist/ffi/worker.js
CHANGED
|
@@ -1 +1,616 @@
|
|
|
1
|
-
import{S as
|
|
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 };
|