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.
- 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
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import { s as identity, S as SHARED_CHANNEL, w as withResolvers, e as toSymbol, g as isView, b as isArray$1, x as push, r as byteOffset } from './shared-CjWXp6-D.js';
|
|
2
|
+
import { i as i32 } from './i32-Cw-Rqr5y.js';
|
|
3
|
+
import { p as UNDEFINED, d as dv, b as BIGINT, u as u8a8, S as SYMBOL, T as TRUE, g as FALSE, q as encoder$1, l as STRING, B as BLOB, F as FILE, h as REGEXP, j as ImageData, i as IMAGE_DATA, E as ERROR, k as SET, M as MAP, D as DATE, n as BUFFER, V as VIEW, N as NULL, U as UI8, o as NUMBER, f as NAN, Z as ZERO, c as N_ZERO, e as N_INFINITY, I as INFINITY, R as RECURSION, a as BIGUINT, O as OBJECT, A as ARRAY } from './views-DBBYng9N.js';
|
|
4
|
+
import { a as toTag } from './global-DzpOeFX2.js';
|
|
5
|
+
|
|
6
|
+
const { isArray } = Array;
|
|
7
|
+
|
|
8
|
+
class Sender extends Worker {
|
|
9
|
+
#next;
|
|
10
|
+
#requests;
|
|
11
|
+
constructor(scriptURL, options) {
|
|
12
|
+
const onsend = options.onsend ?? identity;
|
|
13
|
+
super(scriptURL, options);
|
|
14
|
+
this.#next = i32();
|
|
15
|
+
this.#requests = new Map;
|
|
16
|
+
super.addEventListener('message', async event => {
|
|
17
|
+
const { data } = event;
|
|
18
|
+
if (isArray(data) && data.length === 2 && data[0] === SHARED_CHANNEL) {
|
|
19
|
+
event.stopImmediatePropagation();
|
|
20
|
+
event.preventDefault();
|
|
21
|
+
const [id, payload] = data[1];
|
|
22
|
+
const resolve = this.#requests.get(id);
|
|
23
|
+
this.#requests.delete(id);
|
|
24
|
+
resolve(await onsend(payload));
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
send(payload, ...rest) {
|
|
30
|
+
const id = this.#next();
|
|
31
|
+
const { promise, resolve } = withResolvers();
|
|
32
|
+
this.#requests.set(id, resolve);
|
|
33
|
+
super.postMessage([SHARED_CHANNEL, [id, payload]], ...rest);
|
|
34
|
+
return promise;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// This is an Array facade for the encoder.
|
|
39
|
+
|
|
40
|
+
class Stack {
|
|
41
|
+
/**
|
|
42
|
+
* @param {Stack} self
|
|
43
|
+
* @param {Uint8Array} value
|
|
44
|
+
*/
|
|
45
|
+
static push(self, value) {
|
|
46
|
+
self.sync(false);
|
|
47
|
+
self._(value, value.length);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {ArrayBufferLike} buffer
|
|
52
|
+
* @param {number} offset
|
|
53
|
+
*/
|
|
54
|
+
constructor(buffer, offset) {
|
|
55
|
+
/** @type {number[]} */
|
|
56
|
+
const output = [];
|
|
57
|
+
|
|
58
|
+
/** @private length */
|
|
59
|
+
this.l = 0;
|
|
60
|
+
|
|
61
|
+
/** @private output */
|
|
62
|
+
this.o = output;
|
|
63
|
+
|
|
64
|
+
/** @private view */
|
|
65
|
+
this.v = new Uint8Array(buffer, offset);
|
|
66
|
+
|
|
67
|
+
/** @type {typeof Array.prototype.push} */
|
|
68
|
+
this.push = output.push.bind(output);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @type {number}
|
|
73
|
+
*/
|
|
74
|
+
get length() {
|
|
75
|
+
return this.l + this.o.length;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Sync all entries in the output to the buffer.
|
|
80
|
+
* @param {boolean} last `true` if it's the last sync.
|
|
81
|
+
*/
|
|
82
|
+
sync(last) {
|
|
83
|
+
const output = this.o;
|
|
84
|
+
const length = output.length;
|
|
85
|
+
if (length) this._(last ? output : output.splice(0), length);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Set a value to the buffer
|
|
90
|
+
* @private
|
|
91
|
+
* @param {Uint8Array|number[]} value
|
|
92
|
+
* @param {number} byteLength
|
|
93
|
+
*/
|
|
94
|
+
_(value, byteLength) {
|
|
95
|
+
const { buffer, byteOffset } = this.v;
|
|
96
|
+
const offset = this.l;
|
|
97
|
+
this.l += byteLength;
|
|
98
|
+
byteLength += byteOffset + offset;
|
|
99
|
+
if (buffer.byteLength < byteLength)
|
|
100
|
+
/** @type {SharedArrayBuffer} */(buffer).grow(byteLength);
|
|
101
|
+
this.v.set(value, offset);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//@ts-check
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
/** @typedef {Map<number, number[]>} Cache */
|
|
109
|
+
|
|
110
|
+
const { isNaN, isFinite, isInteger } = Number;
|
|
111
|
+
const { ownKeys } = Reflect;
|
|
112
|
+
const { is } = Object;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {any} input
|
|
116
|
+
* @param {number[]|Stack} output
|
|
117
|
+
* @param {Cache} cache
|
|
118
|
+
* @returns {boolean}
|
|
119
|
+
*/
|
|
120
|
+
const process = (input, output, cache) => {
|
|
121
|
+
const value = cache.get(input);
|
|
122
|
+
const unknown = !value;
|
|
123
|
+
if (unknown) {
|
|
124
|
+
dv.setUint32(0, output.length, true);
|
|
125
|
+
cache.set(input, [u8a8[0], u8a8[1], u8a8[2], u8a8[3]]);
|
|
126
|
+
}
|
|
127
|
+
else
|
|
128
|
+
output.push(RECURSION, value[0], value[1], value[2], value[3]);
|
|
129
|
+
return unknown;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @param {number[]|Stack} output
|
|
134
|
+
* @param {number} type
|
|
135
|
+
* @param {number} length
|
|
136
|
+
*/
|
|
137
|
+
const set = (output, type, length) => {
|
|
138
|
+
dv.setUint32(0, length, true);
|
|
139
|
+
output.push(type, u8a8[0], u8a8[1], u8a8[2], u8a8[3]);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {any} input
|
|
144
|
+
* @param {number[]|Stack} output
|
|
145
|
+
* @param {Cache} cache
|
|
146
|
+
*/
|
|
147
|
+
const inflate = (input, output, cache) => {
|
|
148
|
+
switch (typeof input) {
|
|
149
|
+
case 'number': {
|
|
150
|
+
if (input && isFinite(input)) {
|
|
151
|
+
if (isInteger(input) && input < 256 && -1 < input)
|
|
152
|
+
output.push(UI8, input);
|
|
153
|
+
else {
|
|
154
|
+
dv.setFloat64(0, input, true);
|
|
155
|
+
output.push(NUMBER, u8a8[0], u8a8[1], u8a8[2], u8a8[3], u8a8[4], u8a8[5], u8a8[6], u8a8[7]);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else if (isNaN(input)) output.push(NAN);
|
|
159
|
+
else if (!input) output.push(is(input, 0) ? ZERO : N_ZERO);
|
|
160
|
+
else output.push(input < 0 ? N_INFINITY : INFINITY);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
case 'object': {
|
|
164
|
+
switch (true) {
|
|
165
|
+
case input === null:
|
|
166
|
+
output.push(NULL);
|
|
167
|
+
break;
|
|
168
|
+
case !process(input, output, cache): break;
|
|
169
|
+
case isArray$1(input): {
|
|
170
|
+
const length = input.length;
|
|
171
|
+
set(output, ARRAY, length);
|
|
172
|
+
for (let i = 0; i < length; i++)
|
|
173
|
+
inflate(input[i], output, cache);
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
case isView(input): {
|
|
177
|
+
output.push(VIEW);
|
|
178
|
+
inflate(toTag(input), output, cache);
|
|
179
|
+
input = input.buffer;
|
|
180
|
+
if (!process(input, output, cache)) break;
|
|
181
|
+
// fallthrough
|
|
182
|
+
}
|
|
183
|
+
case input instanceof ArrayBuffer: {
|
|
184
|
+
const ui8a = new Uint8Array(input);
|
|
185
|
+
set(output, BUFFER, ui8a.length);
|
|
186
|
+
//@ts-ignore
|
|
187
|
+
pushView(output, ui8a);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
case input instanceof Date:
|
|
191
|
+
output.push(DATE);
|
|
192
|
+
inflate(input.getTime(), output, cache);
|
|
193
|
+
break;
|
|
194
|
+
case input instanceof Map: {
|
|
195
|
+
set(output, MAP, input.size);
|
|
196
|
+
for (const [key, value] of input) {
|
|
197
|
+
inflate(key, output, cache);
|
|
198
|
+
inflate(value, output, cache);
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
case input instanceof Set: {
|
|
203
|
+
set(output, SET, input.size);
|
|
204
|
+
for (const value of input)
|
|
205
|
+
inflate(value, output, cache);
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
case input instanceof Error:
|
|
209
|
+
output.push(ERROR);
|
|
210
|
+
inflate(input.name, output, cache);
|
|
211
|
+
inflate(input.message, output, cache);
|
|
212
|
+
inflate(input.stack, output, cache);
|
|
213
|
+
break;
|
|
214
|
+
/* c8 ignore start */
|
|
215
|
+
case input instanceof ImageData:
|
|
216
|
+
output.push(IMAGE_DATA);
|
|
217
|
+
inflate(input.data, output, cache);
|
|
218
|
+
inflate(input.width, output, cache);
|
|
219
|
+
inflate(input.height, output, cache);
|
|
220
|
+
inflate(input.colorSpace, output, cache);
|
|
221
|
+
//@ts-ignore
|
|
222
|
+
inflate(input.pixelFormat, output, cache);
|
|
223
|
+
break;
|
|
224
|
+
/* c8 ignore stop */
|
|
225
|
+
case input instanceof RegExp:
|
|
226
|
+
output.push(REGEXP);
|
|
227
|
+
inflate(input.source, output, cache);
|
|
228
|
+
inflate(input.flags, output, cache);
|
|
229
|
+
break;
|
|
230
|
+
case input instanceof File: {
|
|
231
|
+
output.push(FILE);
|
|
232
|
+
inflate(input.name, output, cache);
|
|
233
|
+
inflate(input.lastModified, output, cache);
|
|
234
|
+
// fallthrough
|
|
235
|
+
}
|
|
236
|
+
case input instanceof Blob: {
|
|
237
|
+
const size = input.size;
|
|
238
|
+
output.push(BLOB);
|
|
239
|
+
inflate(input.type, output, cache);
|
|
240
|
+
inflate(size, output, cache);
|
|
241
|
+
const length = output.length;
|
|
242
|
+
//@ts-ignore
|
|
243
|
+
pushView(output, new Uint8Array(size));
|
|
244
|
+
blobs.push(input.arrayBuffer().then(buffer => [length, buffer]));
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
default: {
|
|
248
|
+
if ('toJSON' in input) {
|
|
249
|
+
const json = input.toJSON();
|
|
250
|
+
inflate(json === input ? null : json, output, cache);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
const keys = ownKeys(input);
|
|
254
|
+
const length = keys.length;
|
|
255
|
+
set(output, OBJECT, length);
|
|
256
|
+
for (let i = 0; i < length; i++) {
|
|
257
|
+
const key = keys[i];
|
|
258
|
+
inflate(key, output, cache);
|
|
259
|
+
inflate(input[key], output, cache);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case 'string': {
|
|
268
|
+
if (process(input, output, cache)) {
|
|
269
|
+
const encoded = encoder$1.encode(input);
|
|
270
|
+
set(output, STRING, encoded.length);
|
|
271
|
+
//@ts-ignore
|
|
272
|
+
pushView(output, encoded);
|
|
273
|
+
}
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case 'boolean': {
|
|
277
|
+
output.push(input ? TRUE : FALSE);
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
case 'symbol': {
|
|
281
|
+
output.push(SYMBOL);
|
|
282
|
+
inflate(toSymbol(input), output, cache);
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
case 'bigint': {
|
|
286
|
+
let type = BIGINT;
|
|
287
|
+
if (9223372036854775807n < input) {
|
|
288
|
+
dv.setBigUint64(0, input, true);
|
|
289
|
+
type = BIGUINT;
|
|
290
|
+
}
|
|
291
|
+
else dv.setBigInt64(0, input, true);
|
|
292
|
+
output.push(type, u8a8[0], u8a8[1], u8a8[2], u8a8[3], u8a8[4], u8a8[5], u8a8[6], u8a8[7]);
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
// this covers functions too
|
|
296
|
+
default: {
|
|
297
|
+
output.push(UNDEFINED);
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const blobs = [];
|
|
304
|
+
|
|
305
|
+
/** @type {typeof push|typeof Stack.push} */
|
|
306
|
+
let pushView = push;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* @param {{ byteOffset?: number, Array?: typeof Stack }} [options]
|
|
310
|
+
* @returns {(value: any, buffer: ArrayBufferLike) => number | Promise<number>}
|
|
311
|
+
*/
|
|
312
|
+
const encoder = ({ byteOffset = 0, Array = Stack } = {}) => (value, buffer) => {
|
|
313
|
+
const output = new Array(buffer, byteOffset);
|
|
314
|
+
pushView = Array.push;
|
|
315
|
+
inflate(value, output, new Map);
|
|
316
|
+
const length = output.length;
|
|
317
|
+
output.sync(true);
|
|
318
|
+
return blobs.length ?
|
|
319
|
+
Promise.all(blobs.splice(0)).then(entries => {
|
|
320
|
+
const ui8a = new Uint8Array(buffer, byteOffset);
|
|
321
|
+
for (const [len, buff] of entries)
|
|
322
|
+
ui8a.set(new Uint8Array(buff), len);
|
|
323
|
+
return length;
|
|
324
|
+
}) :
|
|
325
|
+
length;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const { notify, store } = Atomics;
|
|
329
|
+
|
|
330
|
+
const SAB = ({
|
|
331
|
+
initByteLength = 1024,
|
|
332
|
+
maxByteLength = (1024 * 8)
|
|
333
|
+
}) =>
|
|
334
|
+
new SharedArrayBuffer(
|
|
335
|
+
byteOffset + initByteLength,
|
|
336
|
+
{ maxByteLength: byteOffset + maxByteLength }
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* @typedef {Object} ServiceWorkerOptions see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#options
|
|
341
|
+
* @property {string} [url] will use the `serviceWorker` value if that is a `string`, otherwise it refers to where the service worker file is located.
|
|
342
|
+
* @property {'classic' | 'module'} [type]
|
|
343
|
+
* @property {'all' | 'imports' | 'none'} [updateViaCache]
|
|
344
|
+
*/
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* @typedef {Object} Options
|
|
348
|
+
* @property {(payload: unknown) => Int32Array | Promise<Int32Array>} [onsync] invoked when the worker expect a response as `Int32Array` to populate the SharedArrayBuffer with.
|
|
349
|
+
* @property {(payload: unknown) => unknown | Promise<unknown>} [onsend] invoked when the worker replies to a `worker.send(data)` call.
|
|
350
|
+
* @property {string} [ws] the WebSocket URL to use for worker <-> server communication.
|
|
351
|
+
* @property {number} [initByteLength=1024] defines the initial byte length of the SharedArrayBuffer.
|
|
352
|
+
* @property {number} [maxByteLength=8192] defines the maximum byte length (growth) of the SharedArrayBuffer.
|
|
353
|
+
* @property {string | ServiceWorkerOptions} [serviceWorker] defines the service worker to use as fallback if SharedArrayBuffer is not supported. If not defined, the `async` fallback will be used so that no `sync` operations from the worker will be possible.
|
|
354
|
+
* @property {import('reflected-ffi/encoder').encoder} [encoder] defines the encoder function to use to encode the result into the SharedArrayBuffer.
|
|
355
|
+
*/
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Initialize the worker channel communication and resolves with the worker instance.
|
|
359
|
+
* @template T
|
|
360
|
+
* @param {T} Worker
|
|
361
|
+
* @returns
|
|
362
|
+
*/
|
|
363
|
+
const bootstrap = Worker => {
|
|
364
|
+
/**
|
|
365
|
+
* @param {string} scriptURL
|
|
366
|
+
* @param {Options} options
|
|
367
|
+
* @returns
|
|
368
|
+
*/
|
|
369
|
+
return (scriptURL, options) => {
|
|
370
|
+
const { promise, resolve } = withResolvers();
|
|
371
|
+
// @ts-ignore
|
|
372
|
+
new Worker(scriptURL, options, resolve);
|
|
373
|
+
return /** @type {Promise<T>} */(promise);
|
|
374
|
+
};
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
const handler = (sab, options, useAtomics) => {
|
|
378
|
+
const i32a = new Int32Array(sab);
|
|
379
|
+
const encode = (options.encoder ?? encoder)({ byteOffset });
|
|
380
|
+
const onsync = options.onsync ?? identity;
|
|
381
|
+
|
|
382
|
+
const resolve = useAtomics ?
|
|
383
|
+
(length => {
|
|
384
|
+
store(i32a, 1, length);
|
|
385
|
+
store(i32a, 0, 1);
|
|
386
|
+
notify(i32a, 0);
|
|
387
|
+
}) :
|
|
388
|
+
(length => {
|
|
389
|
+
i32a[1] = length;
|
|
390
|
+
i32a[0] = 1;
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
const process = result => {
|
|
394
|
+
const length = encode(result, sab);
|
|
395
|
+
return typeof length === 'number' ? resolve(length) : length.then(resolve);
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
return async ({ data }) => process(await onsync(data));
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
const isOK = value => {
|
|
402
|
+
switch (typeof value) {
|
|
403
|
+
case 'symbol':
|
|
404
|
+
case 'function':
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
return true;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const post = (sab, options) => {
|
|
411
|
+
const opts = {};
|
|
412
|
+
for (const key in options) {
|
|
413
|
+
const value = options[key];
|
|
414
|
+
if (isOK(key) && isOK(value)) opts[key] = value;
|
|
415
|
+
}
|
|
416
|
+
return [sab, opts];
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
const url = (scriptURL, reflected, options) => {
|
|
420
|
+
const url = new URL(scriptURL, location.href);
|
|
421
|
+
url.searchParams.set('reflected', reflected);
|
|
422
|
+
return [url, { ...options, type: 'module' }];
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
export { Sender as S, SAB as a, bootstrap as b, handler as h, post as p, url as u };
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// shorter: 'fc260aad-4404-43b8-ae9d-2c06554bb294'.split('-').map(s => parseInt(s, 16)).reduce((p, c) => p + c).toString(16)
|
|
2
|
+
var SHARED_CHANNEL = 'fc260aad-4404-43b8-ae9d-2c06554bb294';
|
|
3
|
+
|
|
4
|
+
const DIRECT = 0;
|
|
5
|
+
const REMOTE = 1 << 0;
|
|
6
|
+
const OBJECT = 1 << 1;
|
|
7
|
+
const ARRAY = 1 << 2;
|
|
8
|
+
const FUNCTION = 1 << 3;
|
|
9
|
+
const SYMBOL = 1 << 4;
|
|
10
|
+
const BIGINT = 1 << 5;
|
|
11
|
+
const BUFFER = 1 << 6;
|
|
12
|
+
|
|
13
|
+
const VIEW = BUFFER | ARRAY;
|
|
14
|
+
const REMOTE_OBJECT = REMOTE | OBJECT;
|
|
15
|
+
const REMOTE_FUNCTION = REMOTE | FUNCTION;
|
|
16
|
+
|
|
17
|
+
/** @type {Map<symbol, string>} */
|
|
18
|
+
const symbols = new Map(
|
|
19
|
+
Reflect.ownKeys(Symbol).map(
|
|
20
|
+
key => [Symbol[key], `@${String(key)}`]
|
|
21
|
+
)
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {symbol} value
|
|
26
|
+
* @param {string} description
|
|
27
|
+
* @returns {string}
|
|
28
|
+
*/
|
|
29
|
+
const asSymbol = (value, description) => (
|
|
30
|
+
description === void 0 ? '?' :
|
|
31
|
+
(Symbol.keyFor(value) === void 0 ? `!${description}` : `#${description}`)
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Extract the value from a pair of type and value.
|
|
36
|
+
* @param {string} name
|
|
37
|
+
* @returns {symbol}
|
|
38
|
+
*/
|
|
39
|
+
const fromSymbol = name => {
|
|
40
|
+
switch (name[0]) {
|
|
41
|
+
case '@': return Symbol[name.slice(1)];
|
|
42
|
+
case '#': return Symbol.for(name.slice(1));
|
|
43
|
+
case '!': return Symbol(name.slice(1));
|
|
44
|
+
default: return Symbol();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create the name of a symbol.
|
|
50
|
+
* @param {symbol} value
|
|
51
|
+
* @returns {string}
|
|
52
|
+
*/
|
|
53
|
+
const toSymbol = value => symbols.get(value) || asSymbol(value, value.description);
|
|
54
|
+
|
|
55
|
+
const defineProperty = Object.defineProperty;
|
|
56
|
+
|
|
57
|
+
const assign = Object.assign;
|
|
58
|
+
|
|
59
|
+
const isArray = Array.isArray;
|
|
60
|
+
|
|
61
|
+
const isView = ArrayBuffer.isView;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A type/value pair.
|
|
65
|
+
* @typedef {[number, any]} TypeValue
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Create a type/value pair.
|
|
70
|
+
* @param {number} type
|
|
71
|
+
* @param {any} value
|
|
72
|
+
* @returns {TypeValue}
|
|
73
|
+
*/
|
|
74
|
+
const tv = (type, value) => [type, value];
|
|
75
|
+
|
|
76
|
+
const identity$1 = value => value;
|
|
77
|
+
|
|
78
|
+
const array = [];
|
|
79
|
+
const object = {};
|
|
80
|
+
|
|
81
|
+
/* c8 ignore start */
|
|
82
|
+
const callback = function () {};
|
|
83
|
+
/* c8 ignore stop */
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create a function that loops through an array and applies a function to each value.
|
|
87
|
+
* @param {(value:any, cache?:Map<any, any>) => any} asValue
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
const loopValues = asValue => (
|
|
91
|
+
/**
|
|
92
|
+
* Loop through an array and apply a function to each value.
|
|
93
|
+
* @param {any[]} arr
|
|
94
|
+
* @param {Map} [cache]
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
(arr, cache = new Map) => {
|
|
98
|
+
for (let i = 0, length = arr.length; i < length; i++)
|
|
99
|
+
arr[i] = asValue(arr[i], cache);
|
|
100
|
+
return arr;
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Extract the value from a pair of type and value.
|
|
106
|
+
* @param {TypeValue} pair
|
|
107
|
+
* @returns {string|symbol}
|
|
108
|
+
*/
|
|
109
|
+
const fromKey = ([type, value]) => type === DIRECT ? value : fromSymbol(value);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Associate a key with an optionally transformed value.
|
|
113
|
+
* @param {string|symbol} value
|
|
114
|
+
* @returns {TypeValue}
|
|
115
|
+
*/
|
|
116
|
+
const toKey = value => typeof value === 'string' ?
|
|
117
|
+
tv(DIRECT, value) : tv(SYMBOL, toSymbol(value))
|
|
118
|
+
;
|
|
119
|
+
|
|
120
|
+
const MAX_ARGS = 0x7FFF;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @param {number[]} output
|
|
124
|
+
* @param {Uint8Array} value
|
|
125
|
+
*/
|
|
126
|
+
const push = (output, value) => {
|
|
127
|
+
for (let $ = output.push, i = 0, length = value.length; i < length; i += MAX_ARGS)
|
|
128
|
+
$.apply(output, value.subarray(i, i + MAX_ARGS));
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
//@ts-check
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @template T
|
|
135
|
+
* @typedef {{promise: Promise<T>, resolve: (value: T) => void, reject: (reason?: any) => void}} Resolvers
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
// fallback for Android WebView
|
|
139
|
+
//@ts-ignore
|
|
140
|
+
const withResolvers = Promise.withResolvers || function withResolvers() {
|
|
141
|
+
var a, b, c = new this((resolve, reject) => {
|
|
142
|
+
a = resolve;
|
|
143
|
+
b = reject;
|
|
144
|
+
});
|
|
145
|
+
return {resolve: a, reject: b, promise: c};
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @template T
|
|
150
|
+
* @type {() => Resolvers<T>}
|
|
151
|
+
*/
|
|
152
|
+
var withResolvers$1 = withResolvers.bind(Promise);
|
|
153
|
+
|
|
154
|
+
const create = Object.create;
|
|
155
|
+
|
|
156
|
+
const byteOffset = Int32Array.BYTES_PER_ELEMENT * 2;
|
|
157
|
+
|
|
158
|
+
let hasRandomUUID = true;
|
|
159
|
+
try {
|
|
160
|
+
crypto.randomUUID();
|
|
161
|
+
} catch (_) {
|
|
162
|
+
hasRandomUUID = false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const identity = value => value;
|
|
166
|
+
|
|
167
|
+
const randomUUID = hasRandomUUID ?
|
|
168
|
+
(() => crypto.randomUUID() ):
|
|
169
|
+
(() => (Date.now() + Math.random()).toString(36));
|
|
170
|
+
|
|
171
|
+
export { ARRAY as A, BUFFER as B, DIRECT as D, FUNCTION as F, OBJECT as O, REMOTE as R, SHARED_CHANNEL as S, VIEW as V, assign as a, isArray as b, BIGINT as c, SYMBOL as d, toSymbol as e, fromSymbol as f, isView as g, REMOTE_FUNCTION as h, identity$1 as i, callback as j, array as k, loopValues as l, REMOTE_OBJECT as m, toKey as n, object as o, fromKey as p, create as q, byteOffset as r, identity as s, tv as t, defineProperty as u, randomUUID as v, withResolvers$1 as w, push as x };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { s as sender, d as decoder } from './sender-C0gu9RzR.js';
|
|
2
|
+
import { r as byteOffset, s as identity } from './shared-CjWXp6-D.js';
|
|
3
|
+
|
|
4
|
+
const { load, store, wait } = Atomics;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Object} Options
|
|
8
|
+
* @property {(payload: Int32Array) => unknown} [onsync] transforms the resulting `Int32Array` from *main* thread into a value usable within the worker.
|
|
9
|
+
* @property {(payload: unknown) => unknown |Promise<unknown>} onsend invoked to define what to return to the *main* thread when it calls `worker.send(payload)`.
|
|
10
|
+
* @property {import('reflected-ffi/decoder').decoder} [decoder] defines the decoder function to use to decode the result from the SharedArrayBuffer.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {MessageChannel | BroadcastChannel} channel
|
|
15
|
+
* @param {Int32Array} i32a
|
|
16
|
+
* @param {Options} options
|
|
17
|
+
* @returns {(payload: unknown, ...rest: unknown[]) => unknown}
|
|
18
|
+
*/
|
|
19
|
+
const handle = (channel, i32a, options) => {
|
|
20
|
+
const decode = (options.decoder ?? decoder)({ byteOffset });
|
|
21
|
+
const onsync = options.onsync ?? identity;
|
|
22
|
+
const { buffer } = i32a;
|
|
23
|
+
return (payload, ...rest) => {
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
channel.postMessage(payload, ...rest);
|
|
26
|
+
wait(i32a, 0, 0);
|
|
27
|
+
store(i32a, 0, 0);
|
|
28
|
+
const length = load(i32a, 1);
|
|
29
|
+
return onsync(length ? decode(length, buffer) : void 0);
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param {Promise<[SharedArrayBuffer, Options, MessageChannel | BroadcastChannel]>} promise
|
|
36
|
+
* @param {(event:MessageEvent) => void} listener
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
const handler = (promise, listener) => {
|
|
40
|
+
addEventListener('message', listener, { once: true });
|
|
41
|
+
/**
|
|
42
|
+
* @param {Options} options
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
return options => promise.then(
|
|
46
|
+
([sab, main, channel]) => {
|
|
47
|
+
postMessage(1);
|
|
48
|
+
return handle(channel, new Int32Array(sab), sender({ ...main, ...options }));
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { handler as h };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { s as sender, d as decoder } from './sender-B7Xmjz4q.js';
|
|
2
|
+
import { s as byteOffset, u as identity } from './shared-5Nhc4gvB.js';
|
|
3
|
+
|
|
4
|
+
const { load, store, wait } = Atomics;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Object} Options
|
|
8
|
+
* @property {(payload: Int32Array) => unknown} [onsync] transforms the resulting `Int32Array` from *main* thread into a value usable within the worker.
|
|
9
|
+
* @property {(payload: unknown) => unknown |Promise<unknown>} onsend invoked to define what to return to the *main* thread when it calls `worker.send(payload)`.
|
|
10
|
+
* @property {import('reflected-ffi/decoder').decoder} [decoder] defines the decoder function to use to decode the result from the SharedArrayBuffer.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {MessageChannel | BroadcastChannel} channel
|
|
15
|
+
* @param {Int32Array} i32a
|
|
16
|
+
* @param {Options} options
|
|
17
|
+
* @returns {(payload: unknown, ...rest: unknown[]) => unknown}
|
|
18
|
+
*/
|
|
19
|
+
const handle = (channel, i32a, options) => {
|
|
20
|
+
const decode = (options.decoder ?? decoder)({ byteOffset });
|
|
21
|
+
const onsync = options.onsync ?? identity;
|
|
22
|
+
const { buffer } = i32a;
|
|
23
|
+
return (payload, ...rest) => {
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
channel.postMessage(payload, ...rest);
|
|
26
|
+
wait(i32a, 0, 0);
|
|
27
|
+
store(i32a, 0, 0);
|
|
28
|
+
const length = load(i32a, 1);
|
|
29
|
+
return onsync(length ? decode(length, buffer) : void 0);
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param {Promise<[SharedArrayBuffer, Options, MessageChannel | BroadcastChannel]>} promise
|
|
36
|
+
* @param {(event:MessageEvent) => void} listener
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
const handler = (promise, listener) => {
|
|
40
|
+
addEventListener('message', listener, { once: true });
|
|
41
|
+
/**
|
|
42
|
+
* @param {Options} options
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
return options => promise.then(
|
|
46
|
+
([sab, main, channel]) => {
|
|
47
|
+
postMessage(1);
|
|
48
|
+
return handle(channel, new Int32Array(sab), sender({ ...main, ...options }));
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { handler as h };
|