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