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
package/dist/message.js
CHANGED
|
@@ -1 +1,843 @@
|
|
|
1
|
-
|
|
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
|
+
// @ts-check
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {number} [start]
|
|
28
|
+
* @returns {() => number}
|
|
29
|
+
*/
|
|
30
|
+
var i32 = start => {
|
|
31
|
+
const i32 = new Int32Array(1);
|
|
32
|
+
return () => i32[0]++;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// shorter: 'fc260aad-4404-43b8-ae9d-2c06554bb294'.split('-').map(s => parseInt(s, 16)).reduce((p, c) => p + c).toString(16)
|
|
36
|
+
var SHARED_CHANNEL = 'fc260aad-4404-43b8-ae9d-2c06554bb294';
|
|
37
|
+
|
|
38
|
+
const byteOffset = Int32Array.BYTES_PER_ELEMENT * 2;
|
|
39
|
+
try {
|
|
40
|
+
crypto.randomUUID();
|
|
41
|
+
} catch (_) {
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const identity = value => value;
|
|
45
|
+
|
|
46
|
+
const { isArray: isArray$2 } = Array;
|
|
47
|
+
|
|
48
|
+
class Sender extends Worker {
|
|
49
|
+
#next;
|
|
50
|
+
#requests;
|
|
51
|
+
constructor(scriptURL, options) {
|
|
52
|
+
const onsend = options.onsend ?? identity;
|
|
53
|
+
super(scriptURL, options);
|
|
54
|
+
this.#next = i32();
|
|
55
|
+
this.#requests = new Map;
|
|
56
|
+
super.addEventListener('message', async event => {
|
|
57
|
+
const { data } = event;
|
|
58
|
+
if (isArray$2(data) && data.length === 2 && data[0] === SHARED_CHANNEL) {
|
|
59
|
+
event.stopImmediatePropagation();
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
const [id, payload] = data[1];
|
|
62
|
+
const resolve = this.#requests.get(id);
|
|
63
|
+
this.#requests.delete(id);
|
|
64
|
+
resolve(await onsend(payload));
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
send(payload, ...rest) {
|
|
70
|
+
const id = this.#next();
|
|
71
|
+
const { promise, resolve } = withResolvers$1();
|
|
72
|
+
this.#requests.set(id, resolve);
|
|
73
|
+
super.postMessage([SHARED_CHANNEL, [id, payload]], ...rest);
|
|
74
|
+
return promise;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let i$1 = 0;
|
|
79
|
+
|
|
80
|
+
const FALSE = i$1++;
|
|
81
|
+
const TRUE = i$1++;
|
|
82
|
+
|
|
83
|
+
const UNDEFINED = i$1++;
|
|
84
|
+
const NULL = i$1++;
|
|
85
|
+
|
|
86
|
+
const NUMBER = i$1++;
|
|
87
|
+
const UI8 = i$1++;
|
|
88
|
+
const NAN = i$1++;
|
|
89
|
+
const INFINITY = i$1++;
|
|
90
|
+
const N_INFINITY = i$1++;
|
|
91
|
+
const ZERO = i$1++;
|
|
92
|
+
const N_ZERO = i$1++;
|
|
93
|
+
|
|
94
|
+
const BIGINT = i$1++;
|
|
95
|
+
const BIGUINT = i$1++;
|
|
96
|
+
|
|
97
|
+
const STRING = i$1++;
|
|
98
|
+
|
|
99
|
+
const SYMBOL = i$1++;
|
|
100
|
+
|
|
101
|
+
const ARRAY = i$1++;
|
|
102
|
+
const BUFFER = i$1++;
|
|
103
|
+
const DATE = i$1++;
|
|
104
|
+
const ERROR = i$1++;
|
|
105
|
+
const MAP = i$1++;
|
|
106
|
+
const OBJECT = i$1++;
|
|
107
|
+
const REGEXP = i$1++;
|
|
108
|
+
const SET = i$1++;
|
|
109
|
+
const VIEW = i$1++;
|
|
110
|
+
|
|
111
|
+
const IMAGE_DATA = i$1++;
|
|
112
|
+
const BLOB = i$1++;
|
|
113
|
+
const FILE = i$1++;
|
|
114
|
+
|
|
115
|
+
const RECURSION = i$1++;
|
|
116
|
+
|
|
117
|
+
class Never {}
|
|
118
|
+
|
|
119
|
+
const ImageData = globalThis.ImageData || /** @type {typeof ImageData} */(Never);
|
|
120
|
+
|
|
121
|
+
// This is an Array facade for the encoder.
|
|
122
|
+
|
|
123
|
+
class Stack {
|
|
124
|
+
/**
|
|
125
|
+
* @param {Stack} self
|
|
126
|
+
* @param {Uint8Array} value
|
|
127
|
+
*/
|
|
128
|
+
static push(self, value) {
|
|
129
|
+
self.sync(false);
|
|
130
|
+
self._(value, value.length);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @param {ArrayBufferLike} buffer
|
|
135
|
+
* @param {number} offset
|
|
136
|
+
*/
|
|
137
|
+
constructor(buffer, offset) {
|
|
138
|
+
/** @type {number[]} */
|
|
139
|
+
const output = [];
|
|
140
|
+
|
|
141
|
+
/** @private length */
|
|
142
|
+
this.l = 0;
|
|
143
|
+
|
|
144
|
+
/** @private output */
|
|
145
|
+
this.o = output;
|
|
146
|
+
|
|
147
|
+
/** @private view */
|
|
148
|
+
this.v = new Uint8Array(buffer, offset);
|
|
149
|
+
|
|
150
|
+
/** @type {typeof Array.prototype.push} */
|
|
151
|
+
this.push = output.push.bind(output);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @type {number}
|
|
156
|
+
*/
|
|
157
|
+
get length() {
|
|
158
|
+
return this.l + this.o.length;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Sync all entries in the output to the buffer.
|
|
163
|
+
* @param {boolean} last `true` if it's the last sync.
|
|
164
|
+
*/
|
|
165
|
+
sync(last) {
|
|
166
|
+
const output = this.o;
|
|
167
|
+
const length = output.length;
|
|
168
|
+
if (length) this._(last ? output : output.splice(0), length);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Set a value to the buffer
|
|
173
|
+
* @private
|
|
174
|
+
* @param {Uint8Array|number[]} value
|
|
175
|
+
* @param {number} byteLength
|
|
176
|
+
*/
|
|
177
|
+
_(value, byteLength) {
|
|
178
|
+
const { buffer, byteOffset } = this.v;
|
|
179
|
+
const offset = this.l;
|
|
180
|
+
this.l += byteLength;
|
|
181
|
+
byteLength += byteOffset + offset;
|
|
182
|
+
if (buffer.byteLength < byteLength)
|
|
183
|
+
/** @type {SharedArrayBuffer} */(buffer).grow(byteLength);
|
|
184
|
+
this.v.set(value, offset);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** @type {Map<symbol, string>} */
|
|
189
|
+
const symbols = new Map(
|
|
190
|
+
Reflect.ownKeys(Symbol).map(
|
|
191
|
+
key => [Symbol[key], `@${String(key)}`]
|
|
192
|
+
)
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @param {symbol} value
|
|
197
|
+
* @param {string} description
|
|
198
|
+
* @returns {string}
|
|
199
|
+
*/
|
|
200
|
+
const asSymbol = (value, description) => (
|
|
201
|
+
description === void 0 ? '?' :
|
|
202
|
+
(Symbol.keyFor(value) === void 0 ? `!${description}` : `#${description}`)
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Extract the value from a pair of type and value.
|
|
207
|
+
* @param {string} name
|
|
208
|
+
* @returns {symbol}
|
|
209
|
+
*/
|
|
210
|
+
const fromSymbol = name => {
|
|
211
|
+
switch (name[0]) {
|
|
212
|
+
case '@': return Symbol[name.slice(1)];
|
|
213
|
+
case '#': return Symbol.for(name.slice(1));
|
|
214
|
+
case '!': return Symbol(name.slice(1));
|
|
215
|
+
default: return Symbol();
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Create the name of a symbol.
|
|
221
|
+
* @param {symbol} value
|
|
222
|
+
* @returns {string}
|
|
223
|
+
*/
|
|
224
|
+
const toSymbol = value => symbols.get(value) || asSymbol(value, value.description);
|
|
225
|
+
|
|
226
|
+
const defineProperty = Object.defineProperty;
|
|
227
|
+
|
|
228
|
+
const isArray$1 = Array.isArray;
|
|
229
|
+
|
|
230
|
+
const isView = ArrayBuffer.isView;
|
|
231
|
+
|
|
232
|
+
const MAX_ARGS = 0x7FFF;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* @param {number[]} output
|
|
236
|
+
* @param {Uint8Array} value
|
|
237
|
+
*/
|
|
238
|
+
const push = (output, value) => {
|
|
239
|
+
for (let $ = output.push, i = 0, length = value.length; i < length; i += MAX_ARGS)
|
|
240
|
+
$.apply(output, value.subarray(i, i + MAX_ARGS));
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const decoder$1 = new TextDecoder;
|
|
244
|
+
|
|
245
|
+
const encoder$1 = new TextEncoder;
|
|
246
|
+
|
|
247
|
+
const buffer = new ArrayBuffer(8);
|
|
248
|
+
const dv = new DataView(buffer);
|
|
249
|
+
const u8a8 = new Uint8Array(buffer);
|
|
250
|
+
|
|
251
|
+
const { getPrototypeOf } = Object;
|
|
252
|
+
const { construct } = Reflect;
|
|
253
|
+
const { toStringTag } = Symbol;
|
|
254
|
+
|
|
255
|
+
const toTag = (ref, name = ref[toStringTag]) =>
|
|
256
|
+
name in globalThis ? name : toTag(construct(getPrototypeOf(ref.constructor),[0]));
|
|
257
|
+
|
|
258
|
+
//@ts-check
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
/** @typedef {Map<number, number[]>} Cache */
|
|
262
|
+
|
|
263
|
+
const { isNaN, isFinite, isInteger } = Number;
|
|
264
|
+
const { ownKeys } = Reflect;
|
|
265
|
+
const { is } = Object;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @param {any} input
|
|
269
|
+
* @param {number[]|Stack} output
|
|
270
|
+
* @param {Cache} cache
|
|
271
|
+
* @returns {boolean}
|
|
272
|
+
*/
|
|
273
|
+
const process = (input, output, cache) => {
|
|
274
|
+
const value = cache.get(input);
|
|
275
|
+
const unknown = !value;
|
|
276
|
+
if (unknown) {
|
|
277
|
+
dv.setUint32(0, output.length, true);
|
|
278
|
+
cache.set(input, [u8a8[0], u8a8[1], u8a8[2], u8a8[3]]);
|
|
279
|
+
}
|
|
280
|
+
else
|
|
281
|
+
output.push(RECURSION, value[0], value[1], value[2], value[3]);
|
|
282
|
+
return unknown;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @param {number[]|Stack} output
|
|
287
|
+
* @param {number} type
|
|
288
|
+
* @param {number} length
|
|
289
|
+
*/
|
|
290
|
+
const set = (output, type, length) => {
|
|
291
|
+
dv.setUint32(0, length, true);
|
|
292
|
+
output.push(type, u8a8[0], u8a8[1], u8a8[2], u8a8[3]);
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @param {any} input
|
|
297
|
+
* @param {number[]|Stack} output
|
|
298
|
+
* @param {Cache} cache
|
|
299
|
+
*/
|
|
300
|
+
const inflate = (input, output, cache) => {
|
|
301
|
+
switch (typeof input) {
|
|
302
|
+
case 'number': {
|
|
303
|
+
if (input && isFinite(input)) {
|
|
304
|
+
if (isInteger(input) && input < 256 && -1 < input)
|
|
305
|
+
output.push(UI8, input);
|
|
306
|
+
else {
|
|
307
|
+
dv.setFloat64(0, input, true);
|
|
308
|
+
output.push(NUMBER, u8a8[0], u8a8[1], u8a8[2], u8a8[3], u8a8[4], u8a8[5], u8a8[6], u8a8[7]);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
else if (isNaN(input)) output.push(NAN);
|
|
312
|
+
else if (!input) output.push(is(input, 0) ? ZERO : N_ZERO);
|
|
313
|
+
else output.push(input < 0 ? N_INFINITY : INFINITY);
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
case 'object': {
|
|
317
|
+
switch (true) {
|
|
318
|
+
case input === null:
|
|
319
|
+
output.push(NULL);
|
|
320
|
+
break;
|
|
321
|
+
case !process(input, output, cache): break;
|
|
322
|
+
case isArray$1(input): {
|
|
323
|
+
const length = input.length;
|
|
324
|
+
set(output, ARRAY, length);
|
|
325
|
+
for (let i = 0; i < length; i++)
|
|
326
|
+
inflate(input[i], output, cache);
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
case isView(input): {
|
|
330
|
+
output.push(VIEW);
|
|
331
|
+
inflate(toTag(input), output, cache);
|
|
332
|
+
input = input.buffer;
|
|
333
|
+
if (!process(input, output, cache)) break;
|
|
334
|
+
// fallthrough
|
|
335
|
+
}
|
|
336
|
+
case input instanceof ArrayBuffer: {
|
|
337
|
+
const ui8a = new Uint8Array(input);
|
|
338
|
+
set(output, BUFFER, ui8a.length);
|
|
339
|
+
//@ts-ignore
|
|
340
|
+
pushView(output, ui8a);
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case input instanceof Date:
|
|
344
|
+
output.push(DATE);
|
|
345
|
+
inflate(input.getTime(), output, cache);
|
|
346
|
+
break;
|
|
347
|
+
case input instanceof Map: {
|
|
348
|
+
set(output, MAP, input.size);
|
|
349
|
+
for (const [key, value] of input) {
|
|
350
|
+
inflate(key, output, cache);
|
|
351
|
+
inflate(value, output, cache);
|
|
352
|
+
}
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
case input instanceof Set: {
|
|
356
|
+
set(output, SET, input.size);
|
|
357
|
+
for (const value of input)
|
|
358
|
+
inflate(value, output, cache);
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case input instanceof Error:
|
|
362
|
+
output.push(ERROR);
|
|
363
|
+
inflate(input.name, output, cache);
|
|
364
|
+
inflate(input.message, output, cache);
|
|
365
|
+
inflate(input.stack, output, cache);
|
|
366
|
+
break;
|
|
367
|
+
/* c8 ignore start */
|
|
368
|
+
case input instanceof ImageData:
|
|
369
|
+
output.push(IMAGE_DATA);
|
|
370
|
+
inflate(input.data, output, cache);
|
|
371
|
+
inflate(input.width, output, cache);
|
|
372
|
+
inflate(input.height, output, cache);
|
|
373
|
+
inflate(input.colorSpace, output, cache);
|
|
374
|
+
//@ts-ignore
|
|
375
|
+
inflate(input.pixelFormat, output, cache);
|
|
376
|
+
break;
|
|
377
|
+
/* c8 ignore stop */
|
|
378
|
+
case input instanceof RegExp:
|
|
379
|
+
output.push(REGEXP);
|
|
380
|
+
inflate(input.source, output, cache);
|
|
381
|
+
inflate(input.flags, output, cache);
|
|
382
|
+
break;
|
|
383
|
+
case input instanceof File: {
|
|
384
|
+
output.push(FILE);
|
|
385
|
+
inflate(input.name, output, cache);
|
|
386
|
+
inflate(input.lastModified, output, cache);
|
|
387
|
+
// fallthrough
|
|
388
|
+
}
|
|
389
|
+
case input instanceof Blob: {
|
|
390
|
+
const size = input.size;
|
|
391
|
+
output.push(BLOB);
|
|
392
|
+
inflate(input.type, output, cache);
|
|
393
|
+
inflate(size, output, cache);
|
|
394
|
+
const length = output.length;
|
|
395
|
+
//@ts-ignore
|
|
396
|
+
pushView(output, new Uint8Array(size));
|
|
397
|
+
blobs.push(input.arrayBuffer().then(buffer => [length, buffer]));
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
default: {
|
|
401
|
+
if ('toJSON' in input) {
|
|
402
|
+
const json = input.toJSON();
|
|
403
|
+
inflate(json === input ? null : json, output, cache);
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
const keys = ownKeys(input);
|
|
407
|
+
const length = keys.length;
|
|
408
|
+
set(output, OBJECT, length);
|
|
409
|
+
for (let i = 0; i < length; i++) {
|
|
410
|
+
const key = keys[i];
|
|
411
|
+
inflate(key, output, cache);
|
|
412
|
+
inflate(input[key], output, cache);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
case 'string': {
|
|
421
|
+
if (process(input, output, cache)) {
|
|
422
|
+
const encoded = encoder$1.encode(input);
|
|
423
|
+
set(output, STRING, encoded.length);
|
|
424
|
+
//@ts-ignore
|
|
425
|
+
pushView(output, encoded);
|
|
426
|
+
}
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
case 'boolean': {
|
|
430
|
+
output.push(input ? TRUE : FALSE);
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
case 'symbol': {
|
|
434
|
+
output.push(SYMBOL);
|
|
435
|
+
inflate(toSymbol(input), output, cache);
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
case 'bigint': {
|
|
439
|
+
let type = BIGINT;
|
|
440
|
+
if (9223372036854775807n < input) {
|
|
441
|
+
dv.setBigUint64(0, input, true);
|
|
442
|
+
type = BIGUINT;
|
|
443
|
+
}
|
|
444
|
+
else dv.setBigInt64(0, input, true);
|
|
445
|
+
output.push(type, u8a8[0], u8a8[1], u8a8[2], u8a8[3], u8a8[4], u8a8[5], u8a8[6], u8a8[7]);
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
// this covers functions too
|
|
449
|
+
default: {
|
|
450
|
+
output.push(UNDEFINED);
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const blobs = [];
|
|
457
|
+
|
|
458
|
+
/** @type {typeof push|typeof Stack.push} */
|
|
459
|
+
let pushView = push;
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* @param {{ byteOffset?: number, Array?: typeof Stack }} [options]
|
|
463
|
+
* @returns {(value: any, buffer: ArrayBufferLike) => number | Promise<number>}
|
|
464
|
+
*/
|
|
465
|
+
const encoder = ({ byteOffset = 0, Array = Stack } = {}) => (value, buffer) => {
|
|
466
|
+
const output = new Array(buffer, byteOffset);
|
|
467
|
+
pushView = Array.push;
|
|
468
|
+
inflate(value, output, new Map);
|
|
469
|
+
const length = output.length;
|
|
470
|
+
output.sync(true);
|
|
471
|
+
return blobs.length ?
|
|
472
|
+
Promise.all(blobs.splice(0)).then(entries => {
|
|
473
|
+
const ui8a = new Uint8Array(buffer, byteOffset);
|
|
474
|
+
for (const [len, buff] of entries)
|
|
475
|
+
ui8a.set(new Uint8Array(buff), len);
|
|
476
|
+
return length;
|
|
477
|
+
}) :
|
|
478
|
+
length;
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
const { notify, store: store$1 } = Atomics;
|
|
482
|
+
|
|
483
|
+
const SAB = ({
|
|
484
|
+
initByteLength = 1024,
|
|
485
|
+
maxByteLength = (1024 * 8)
|
|
486
|
+
}) =>
|
|
487
|
+
new SharedArrayBuffer(
|
|
488
|
+
byteOffset + initByteLength,
|
|
489
|
+
{ maxByteLength: byteOffset + maxByteLength }
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* @typedef {Object} ServiceWorkerOptions see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#options
|
|
494
|
+
* @property {string} [url] will use the `serviceWorker` value if that is a `string`, otherwise it refers to where the service worker file is located.
|
|
495
|
+
* @property {'classic' | 'module'} [type]
|
|
496
|
+
* @property {'all' | 'imports' | 'none'} [updateViaCache]
|
|
497
|
+
*/
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* @typedef {Object} Options
|
|
501
|
+
* @property {(payload: unknown) => Int32Array | Promise<Int32Array>} [onsync] invoked when the worker expect a response as `Int32Array` to populate the SharedArrayBuffer with.
|
|
502
|
+
* @property {(payload: unknown) => unknown | Promise<unknown>} [onsend] invoked when the worker replies to a `worker.send(data)` call.
|
|
503
|
+
* @property {string} [ws] the WebSocket URL to use for worker <-> server communication.
|
|
504
|
+
* @property {number} [initByteLength=1024] defines the initial byte length of the SharedArrayBuffer.
|
|
505
|
+
* @property {number} [maxByteLength=8192] defines the maximum byte length (growth) of the SharedArrayBuffer.
|
|
506
|
+
* @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.
|
|
507
|
+
* @property {import('reflected-ffi/encoder').encoder} [encoder] defines the encoder function to use to encode the result into the SharedArrayBuffer.
|
|
508
|
+
*/
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Initialize the worker channel communication and resolves with the worker instance.
|
|
512
|
+
* @template T
|
|
513
|
+
* @param {T} Worker
|
|
514
|
+
* @returns
|
|
515
|
+
*/
|
|
516
|
+
const bootstrap = Worker => {
|
|
517
|
+
/**
|
|
518
|
+
* @param {string} scriptURL
|
|
519
|
+
* @param {Options} options
|
|
520
|
+
* @returns
|
|
521
|
+
*/
|
|
522
|
+
return (scriptURL, options) => {
|
|
523
|
+
const { promise, resolve } = withResolvers$1();
|
|
524
|
+
// @ts-ignore
|
|
525
|
+
new Worker(scriptURL, options, resolve);
|
|
526
|
+
return /** @type {Promise<T>} */(promise);
|
|
527
|
+
};
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
const handler$1 = (sab, options, useAtomics) => {
|
|
531
|
+
const i32a = new Int32Array(sab);
|
|
532
|
+
const encode = (options.encoder ?? encoder)({ byteOffset });
|
|
533
|
+
const onsync = options.onsync ?? identity;
|
|
534
|
+
|
|
535
|
+
const resolve = (length => {
|
|
536
|
+
store$1(i32a, 1, length);
|
|
537
|
+
store$1(i32a, 0, 1);
|
|
538
|
+
notify(i32a, 0);
|
|
539
|
+
}) ;
|
|
540
|
+
|
|
541
|
+
const process = result => {
|
|
542
|
+
const length = encode(result, sab);
|
|
543
|
+
return typeof length === 'number' ? resolve(length) : length.then(resolve);
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
return async ({ data }) => process(await onsync(data));
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const isOK = value => {
|
|
550
|
+
switch (typeof value) {
|
|
551
|
+
case 'symbol':
|
|
552
|
+
case 'function':
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
return true;
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
const post = (sab, options) => {
|
|
559
|
+
const opts = {};
|
|
560
|
+
for (const key in options) {
|
|
561
|
+
const value = options[key];
|
|
562
|
+
if (isOK(key) && isOK(value)) opts[key] = value;
|
|
563
|
+
}
|
|
564
|
+
return [sab, opts];
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
const url = (scriptURL, reflected, options) => {
|
|
568
|
+
const url = new URL(scriptURL, location.href);
|
|
569
|
+
url.searchParams.set('reflected', reflected);
|
|
570
|
+
return [url, { ...options, type: 'module' }];
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const CHANNEL = 'message';
|
|
574
|
+
|
|
575
|
+
var main = bootstrap(class Worker extends Sender {
|
|
576
|
+
constructor(scriptURL, options, resolve) {
|
|
577
|
+
const { port1, port2 } = new MessageChannel;
|
|
578
|
+
const sab = SAB(options);
|
|
579
|
+
port1.addEventListener(CHANNEL, handler$1(sab, options));
|
|
580
|
+
port1.start();
|
|
581
|
+
super(...url(scriptURL, CHANNEL, options));
|
|
582
|
+
super.addEventListener(CHANNEL, () => resolve(this), { once: true });
|
|
583
|
+
super.postMessage(post(sab, options), [port2]);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
get channel() {
|
|
587
|
+
return CHANNEL;
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
//@ts-check
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
/** @typedef {Map<number, any>} Cache */
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* @param {Cache} cache
|
|
598
|
+
* @param {number} index
|
|
599
|
+
* @param {any} value
|
|
600
|
+
* @returns {any}
|
|
601
|
+
*/
|
|
602
|
+
const $ = (cache, index, value) => {
|
|
603
|
+
cache.set(index, value);
|
|
604
|
+
return value;
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* @param {Uint8Array} input
|
|
609
|
+
*/
|
|
610
|
+
const number = input => {
|
|
611
|
+
u8a8[0] = input[i++];
|
|
612
|
+
u8a8[1] = input[i++];
|
|
613
|
+
u8a8[2] = input[i++];
|
|
614
|
+
u8a8[3] = input[i++];
|
|
615
|
+
u8a8[4] = input[i++];
|
|
616
|
+
u8a8[5] = input[i++];
|
|
617
|
+
u8a8[6] = input[i++];
|
|
618
|
+
u8a8[7] = input[i++];
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* @param {Uint8Array} input
|
|
623
|
+
* @returns {number}
|
|
624
|
+
*/
|
|
625
|
+
const size = input => {
|
|
626
|
+
u8a8[0] = input[i++];
|
|
627
|
+
u8a8[1] = input[i++];
|
|
628
|
+
u8a8[2] = input[i++];
|
|
629
|
+
u8a8[3] = input[i++];
|
|
630
|
+
return dv.getUint32(0, true);
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* @param {Uint8Array} input
|
|
635
|
+
* @param {Cache} cache
|
|
636
|
+
* @returns {any}
|
|
637
|
+
*/
|
|
638
|
+
const deflate = (input, cache) => {
|
|
639
|
+
switch (input[i++]) {
|
|
640
|
+
case NUMBER: {
|
|
641
|
+
number(input);
|
|
642
|
+
return dv.getFloat64(0, true);
|
|
643
|
+
}
|
|
644
|
+
case UI8: return input[i++];
|
|
645
|
+
case OBJECT: {
|
|
646
|
+
const object = $(cache, i - 1, {});
|
|
647
|
+
for (let j = 0, length = size(input); j < length; j++)
|
|
648
|
+
object[deflate(input, cache)] = deflate(input, cache);
|
|
649
|
+
return object;
|
|
650
|
+
}
|
|
651
|
+
case ARRAY: {
|
|
652
|
+
const array = $(cache, i - 1, []);
|
|
653
|
+
for (let j = 0, length = size(input); j < length; j++)
|
|
654
|
+
array.push(deflate(input, cache));
|
|
655
|
+
return array;
|
|
656
|
+
}
|
|
657
|
+
case VIEW: {
|
|
658
|
+
const index = i - 1;
|
|
659
|
+
const name = deflate(input, cache);
|
|
660
|
+
return $(cache, index, new globalThis[name](deflate(input, cache)));
|
|
661
|
+
}
|
|
662
|
+
case BUFFER: {
|
|
663
|
+
const index = i - 1;
|
|
664
|
+
const length = size(input);
|
|
665
|
+
return $(cache, index, input.slice(i, i += length).buffer);
|
|
666
|
+
}
|
|
667
|
+
case STRING: {
|
|
668
|
+
const index = i - 1;
|
|
669
|
+
const length = size(input);
|
|
670
|
+
// this could be a subarray but it's not supported on the Web and
|
|
671
|
+
// it wouldn't work with arrays instead of typed arrays.
|
|
672
|
+
return $(cache, index, decoder$1.decode(input.slice(i, i += length)));
|
|
673
|
+
}
|
|
674
|
+
case DATE: {
|
|
675
|
+
return $(cache, i - 1, new Date(deflate(input, cache)));
|
|
676
|
+
}
|
|
677
|
+
case MAP: {
|
|
678
|
+
const map = $(cache, i - 1, new Map);
|
|
679
|
+
for (let j = 0, length = size(input); j < length; j++)
|
|
680
|
+
map.set(deflate(input, cache), deflate(input, cache));
|
|
681
|
+
return map;
|
|
682
|
+
}
|
|
683
|
+
case SET: {
|
|
684
|
+
const set = $(cache, i - 1, new Set);
|
|
685
|
+
for (let j = 0, length = size(input); j < length; j++)
|
|
686
|
+
set.add(deflate(input, cache));
|
|
687
|
+
return set;
|
|
688
|
+
}
|
|
689
|
+
case ERROR: {
|
|
690
|
+
const index = i - 1;
|
|
691
|
+
const name = deflate(input, cache);
|
|
692
|
+
const message = deflate(input, cache);
|
|
693
|
+
const stack = deflate(input, cache);
|
|
694
|
+
const Class = globalThis[name] || Error;
|
|
695
|
+
const error = new Class(message);
|
|
696
|
+
return $(cache, index, defineProperty(error, 'stack', { value: stack }));
|
|
697
|
+
}
|
|
698
|
+
/* c8 ignore start */
|
|
699
|
+
case IMAGE_DATA: {
|
|
700
|
+
const index = i - 1;
|
|
701
|
+
const data = deflate(input, cache);
|
|
702
|
+
const width = deflate(input, cache);
|
|
703
|
+
const height = deflate(input, cache);
|
|
704
|
+
const colorSpace = deflate(input, cache);
|
|
705
|
+
const pixelFormat = deflate(input, cache);
|
|
706
|
+
const settings = { colorSpace, pixelFormat };
|
|
707
|
+
return $(cache, index, new ImageData(data, width, height, settings));
|
|
708
|
+
}
|
|
709
|
+
/* c8 ignore stop */
|
|
710
|
+
case REGEXP: {
|
|
711
|
+
const index = i - 1;
|
|
712
|
+
const source = deflate(input, cache);
|
|
713
|
+
const flags = deflate(input, cache);
|
|
714
|
+
return $(cache, index, new RegExp(source, flags));
|
|
715
|
+
}
|
|
716
|
+
case FALSE: return false;
|
|
717
|
+
case TRUE: return true;
|
|
718
|
+
case NAN: return NaN;
|
|
719
|
+
case INFINITY: return Infinity;
|
|
720
|
+
case N_INFINITY: return -Infinity;
|
|
721
|
+
case ZERO: return 0;
|
|
722
|
+
case N_ZERO: return -0;
|
|
723
|
+
case NULL: return null;
|
|
724
|
+
case BIGINT: return (number(input), dv.getBigInt64(0, true));
|
|
725
|
+
case BIGUINT: return (number(input), dv.getBigUint64(0, true));
|
|
726
|
+
case SYMBOL: return fromSymbol(deflate(input, cache));
|
|
727
|
+
case RECURSION: return cache.get(size(input));
|
|
728
|
+
case BLOB: {
|
|
729
|
+
const index = i - 1;
|
|
730
|
+
const type = deflate(input, cache);
|
|
731
|
+
const size = deflate(input, cache);
|
|
732
|
+
return $(cache, index, new Blob([input.slice(i, i += size)], { type }));
|
|
733
|
+
}
|
|
734
|
+
case FILE: {
|
|
735
|
+
const index = i - 1;
|
|
736
|
+
const name = deflate(input, cache);
|
|
737
|
+
const lastModified = deflate(input, cache);
|
|
738
|
+
const blob = deflate(input, cache);
|
|
739
|
+
return $(cache, index, new File([blob], name, { type: blob.type, lastModified }));
|
|
740
|
+
}
|
|
741
|
+
// this covers functions too
|
|
742
|
+
default: return undefined;
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
let i = 0;
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* @param {Uint8Array} value
|
|
750
|
+
* @returns {any}
|
|
751
|
+
*/
|
|
752
|
+
const decode = value => {
|
|
753
|
+
i = 0;
|
|
754
|
+
return deflate(value, new Map);
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* @param {{ byteOffset?: number }} [options]
|
|
759
|
+
* @returns {(length: number, buffer: ArrayBufferLike) => any}
|
|
760
|
+
*/
|
|
761
|
+
const decoder = ({ byteOffset = 0 } = {}) => (length, buffer) => decode(
|
|
762
|
+
new Uint8Array(buffer, byteOffset, length)
|
|
763
|
+
);
|
|
764
|
+
|
|
765
|
+
const { isArray } = Array;
|
|
766
|
+
|
|
767
|
+
var sender = options => {
|
|
768
|
+
const onsend = options.onsend ?? identity;
|
|
769
|
+
addEventListener('message', async event => {
|
|
770
|
+
const { data } = event;
|
|
771
|
+
if (isArray(data) && data[0] === SHARED_CHANNEL) {
|
|
772
|
+
event.stopImmediatePropagation();
|
|
773
|
+
event.preventDefault();
|
|
774
|
+
const [id, payload] = data[1];
|
|
775
|
+
postMessage([SHARED_CHANNEL, [id, await onsend(payload)]]);
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
return options;
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
const { load, store, wait } = Atomics;
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* @typedef {Object} Options
|
|
785
|
+
* @property {(payload: Int32Array) => unknown} [onsync] transforms the resulting `Int32Array` from *main* thread into a value usable within the worker.
|
|
786
|
+
* @property {(payload: unknown) => unknown |Promise<unknown>} onsend invoked to define what to return to the *main* thread when it calls `worker.send(payload)`.
|
|
787
|
+
* @property {import('reflected-ffi/decoder').decoder} [decoder] defines the decoder function to use to decode the result from the SharedArrayBuffer.
|
|
788
|
+
*/
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* @param {MessageChannel | BroadcastChannel} channel
|
|
792
|
+
* @param {Int32Array} i32a
|
|
793
|
+
* @param {Options} options
|
|
794
|
+
* @returns {(payload: unknown, ...rest: unknown[]) => unknown}
|
|
795
|
+
*/
|
|
796
|
+
const handle = (channel, i32a, options) => {
|
|
797
|
+
const decode = (options.decoder ?? decoder)({ byteOffset });
|
|
798
|
+
const onsync = options.onsync ?? identity;
|
|
799
|
+
const { buffer } = i32a;
|
|
800
|
+
return (payload, ...rest) => {
|
|
801
|
+
// @ts-ignore
|
|
802
|
+
channel.postMessage(payload, ...rest);
|
|
803
|
+
wait(i32a, 0, 0);
|
|
804
|
+
store(i32a, 0, 0);
|
|
805
|
+
const length = load(i32a, 1);
|
|
806
|
+
return onsync(length ? decode(length, buffer) : void 0);
|
|
807
|
+
};
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
*
|
|
812
|
+
* @param {Promise<[SharedArrayBuffer, Options, MessageChannel | BroadcastChannel]>} promise
|
|
813
|
+
* @param {(event:MessageEvent) => void} listener
|
|
814
|
+
* @returns
|
|
815
|
+
*/
|
|
816
|
+
const handler = (promise, listener) => {
|
|
817
|
+
addEventListener('message', listener, { once: true });
|
|
818
|
+
/**
|
|
819
|
+
* @param {Options} options
|
|
820
|
+
* @returns
|
|
821
|
+
*/
|
|
822
|
+
return options => promise.then(
|
|
823
|
+
([sab, main, channel]) => {
|
|
824
|
+
postMessage(1);
|
|
825
|
+
return handle(channel, new Int32Array(sab), sender({ ...main, ...options }));
|
|
826
|
+
}
|
|
827
|
+
);
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
const { promise, resolve } = withResolvers$1();
|
|
831
|
+
|
|
832
|
+
const channel = 'message';
|
|
833
|
+
|
|
834
|
+
var worker = handler(
|
|
835
|
+
promise,
|
|
836
|
+
({ data: [sab, main], ports: [channel] }) => resolve([sab, main, channel])
|
|
837
|
+
);
|
|
838
|
+
|
|
839
|
+
var message = (
|
|
840
|
+
'importScripts' in globalThis ? worker : main
|
|
841
|
+
);
|
|
842
|
+
|
|
843
|
+
export { channel, message as default };
|