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.
Files changed (162) hide show
  1. package/dist/async-CM1iXZPW.js +44 -0
  2. package/dist/async-OLiknCrf.js +41 -0
  3. package/dist/async.js +856 -1
  4. package/dist/broadcast-B4sl9DBp.js +24 -0
  5. package/dist/broadcast-eR5_7TCY.js +15 -0
  6. package/dist/broadcast.js +850 -1
  7. package/dist/ffi/async-0Q0H4a_N.js +41 -0
  8. package/dist/ffi/async-CbwemiL2.js +41 -0
  9. package/dist/ffi/async-D6Zj6i_Q.js +44 -0
  10. package/dist/ffi/async-DLrN8K6M.js +44 -0
  11. package/dist/ffi/broadcast-CKNplfox.js +15 -0
  12. package/dist/ffi/broadcast-CbbtYobq.js +25 -0
  13. package/dist/ffi/broadcast-DIucqhL0.js +25 -0
  14. package/dist/ffi/broadcast-uydYnG6E.js +15 -0
  15. package/dist/ffi/global-DzpOeFX2.js +14 -0
  16. package/dist/ffi/global-LTfnrHcF.js +11 -0
  17. package/dist/ffi/i32-Cw-Rqr5y.js +13 -0
  18. package/dist/ffi/main.js +650 -1
  19. package/dist/ffi/message-2Dqj3uZ3.js +15 -0
  20. package/dist/ffi/message-Co0Oew7f.js +25 -0
  21. package/dist/ffi/message-Cxjrx_su.js +25 -0
  22. package/dist/ffi/message-mK0eTb54.js +15 -0
  23. package/dist/ffi/sender-B7Xmjz4q.js +194 -0
  24. package/dist/ffi/sender-C0gu9RzR.js +194 -0
  25. package/dist/ffi/shared-5Nhc4gvB.js +175 -0
  26. package/dist/ffi/shared-B3KmQkGh.js +425 -0
  27. package/dist/ffi/shared-CjWXp6-D.js +171 -0
  28. package/dist/ffi/shared-Czh3AlGt.js +53 -0
  29. package/dist/ffi/shared-DZdgWBc6.js +53 -0
  30. package/dist/ffi/shared-array-buffer-BQgQXQvC.js +28 -0
  31. package/dist/ffi/shared-ddawRQD0.js +425 -0
  32. package/dist/ffi/views-C3SJnvMr.js +48 -0
  33. package/dist/ffi/views-DBBYng9N.js +52 -0
  34. package/dist/ffi/worker.js +616 -1
  35. package/dist/ffi/xhr-Be-3qHQb.js +101 -0
  36. package/dist/ffi/xhr-CKhf21P8.js +41 -0
  37. package/dist/ffi/xhr-CRJGE1xP.js +41 -0
  38. package/dist/ffi/xhr-DSsSGsb-.js +101 -0
  39. package/dist/i32-UV5fM9Tw.js +12 -0
  40. package/dist/index.js +57 -1
  41. package/dist/message-D_SM4ST4.js +24 -0
  42. package/dist/message-vCWcUEmt.js +15 -0
  43. package/dist/message.js +843 -1
  44. package/dist/proxy/main/async-8J3q5T6q.js +41 -0
  45. package/dist/proxy/main/async-Di4MuQWe.js +44 -0
  46. package/dist/proxy/main/broadcast-CR8vlkKk.js +24 -0
  47. package/dist/proxy/main/broadcast-LNW6nFVD.js +15 -0
  48. package/dist/proxy/main/channel-NcnPEVL4.js +110 -0
  49. package/dist/proxy/main/i32-UV5fM9Tw.js +12 -0
  50. package/dist/proxy/main/message-j-4BYSZ4.js +24 -0
  51. package/dist/proxy/main/message-uirwoNUQ.js +15 -0
  52. package/dist/proxy/main/proxy.js +106 -1
  53. package/dist/proxy/main/sender-Dkjjbv2Y.js +194 -0
  54. package/dist/proxy/main/shared-C3U1MNPd.js +53 -0
  55. package/dist/proxy/main/shared-CV7CPYuP.js +43 -0
  56. package/dist/proxy/main/shared-DdPca9Ke.js +431 -0
  57. package/dist/proxy/main/shared-array-buffer-BQgQXQvC.js +28 -0
  58. package/dist/proxy/main/xhr-BUjEbkCh.js +100 -0
  59. package/dist/proxy/main/xhr-WnkM0dVo.js +41 -0
  60. package/dist/proxy/worker/async-CZJaURwO.js +41 -0
  61. package/dist/proxy/worker/async-DaSAye0a.js +44 -0
  62. package/dist/proxy/worker/broadcast-DRVfswix.js +24 -0
  63. package/dist/proxy/worker/broadcast-fWsZlH8v.js +15 -0
  64. package/dist/proxy/worker/channel-NcnPEVL4.js +110 -0
  65. package/dist/proxy/worker/i32-UV5fM9Tw.js +12 -0
  66. package/dist/proxy/worker/message-Bots8UXM.js +15 -0
  67. package/dist/proxy/worker/message-sej0J6k5.js +24 -0
  68. package/dist/proxy/worker/proxy.js +79 -1
  69. package/dist/proxy/worker/sender-D5Lm-DmC.js +194 -0
  70. package/dist/proxy/worker/shared-CgOGpfKJ.js +431 -0
  71. package/dist/proxy/worker/shared-Cn1K0k8G.js +53 -0
  72. package/dist/proxy/worker/shared-DR7YYduq.js +41 -0
  73. package/dist/proxy/worker/shared-array-buffer-BQgQXQvC.js +28 -0
  74. package/dist/proxy/worker/xhr-CQDzS28h.js +100 -0
  75. package/dist/proxy/worker/xhr-DAgmljtu.js +41 -0
  76. package/dist/sender-BSx3lj4w.js +193 -0
  77. package/dist/shared-Ccm1cPah.js +125 -0
  78. package/dist/shared-DVDPRDN_.js +53 -0
  79. package/dist/shared-L0wiAfqu.js +431 -0
  80. package/dist/shared-array-buffer-BQgQXQvC.js +28 -0
  81. package/dist/sw.js +155 -1
  82. package/dist/with-resolvers-DsO_nhbc.js +24 -0
  83. package/dist/xhr-BZOjL92j.js +100 -0
  84. package/dist/xhr-Crh-419R.js +41 -0
  85. package/dist/xhr.js +909 -1
  86. package/package.json +1 -1
  87. package/reflected.tar.gz +0 -0
  88. package/dist/async-C2eqvaGm.js +0 -1
  89. package/dist/async-s2VCmNNG.js +0 -1
  90. package/dist/broadcast-CArZGSOF.js +0 -1
  91. package/dist/broadcast-f7NecPmf.js +0 -1
  92. package/dist/ffi/async-B_TI4xaG.js +0 -1
  93. package/dist/ffi/async-BeKYhv9s.js +0 -1
  94. package/dist/ffi/async-Dl9wCRDA.js +0 -1
  95. package/dist/ffi/async-F0rxZjV9.js +0 -1
  96. package/dist/ffi/broadcast-CBUwPa0e.js +0 -1
  97. package/dist/ffi/broadcast-Ce_cOurt.js +0 -1
  98. package/dist/ffi/broadcast-D0jZEoUb.js +0 -1
  99. package/dist/ffi/broadcast-D7p6b88J.js +0 -1
  100. package/dist/ffi/global-B3nXXiRL.js +0 -1
  101. package/dist/ffi/global-Bs8RLRoX.js +0 -1
  102. package/dist/ffi/i32-BkNtpHwD.js +0 -1
  103. package/dist/ffi/message-BdbPt0y5.js +0 -1
  104. package/dist/ffi/message-DNbm1E-6.js +0 -1
  105. package/dist/ffi/message-DW2TgKPr.js +0 -1
  106. package/dist/ffi/message-TyU9xCIl.js +0 -1
  107. package/dist/ffi/sender-C5EHIpuL.js +0 -1
  108. package/dist/ffi/sender-DBFDKow4.js +0 -1
  109. package/dist/ffi/shared-BfrghvHB.js +0 -1
  110. package/dist/ffi/shared-C2XCOXcu.js +0 -1
  111. package/dist/ffi/shared-Cs1s8cy4.js +0 -1
  112. package/dist/ffi/shared-DsgUjYDJ.js +0 -1
  113. package/dist/ffi/shared-DwSbyGXy.js +0 -1
  114. package/dist/ffi/shared-DyOzpRO0.js +0 -1
  115. package/dist/ffi/shared-array-buffer-cwdMr2mc.js +0 -1
  116. package/dist/ffi/views-CdOIrBQR.js +0 -1
  117. package/dist/ffi/views-Cr2pQ4B-.js +0 -1
  118. package/dist/ffi/xhr-BkOoxiJZ.js +0 -1
  119. package/dist/ffi/xhr-C598zZcp.js +0 -1
  120. package/dist/ffi/xhr-CasCWs_z.js +0 -1
  121. package/dist/ffi/xhr-DHyu_Jz0.js +0 -1
  122. package/dist/i32-C78nBJH2.js +0 -1
  123. package/dist/message-RcKwy8Ib.js +0 -1
  124. package/dist/message-l7W12V0c.js +0 -1
  125. package/dist/proxy/main/async-Bc0QcTaC.js +0 -1
  126. package/dist/proxy/main/async-CyZdYOvJ.js +0 -1
  127. package/dist/proxy/main/broadcast-BX9FmxMK.js +0 -1
  128. package/dist/proxy/main/broadcast-DXhTouQy.js +0 -1
  129. package/dist/proxy/main/channel-DQBWE7_E.js +0 -1
  130. package/dist/proxy/main/i32-C78nBJH2.js +0 -1
  131. package/dist/proxy/main/message-CbPHeJAY.js +0 -1
  132. package/dist/proxy/main/message-DxRlOVN7.js +0 -1
  133. package/dist/proxy/main/sender-CNwZwCuI.js +0 -1
  134. package/dist/proxy/main/shared-BoatDt-V.js +0 -1
  135. package/dist/proxy/main/shared-FMegJ6yb.js +0 -1
  136. package/dist/proxy/main/shared-array-buffer-cwdMr2mc.js +0 -1
  137. package/dist/proxy/main/shared-z_jGYqUa.js +0 -1
  138. package/dist/proxy/main/xhr-BaBkekSn.js +0 -1
  139. package/dist/proxy/main/xhr-Fg-l74MV.js +0 -1
  140. package/dist/proxy/worker/async-Do59YjcF.js +0 -1
  141. package/dist/proxy/worker/async-fqI-s7gn.js +0 -1
  142. package/dist/proxy/worker/broadcast-Bdt9gmj_.js +0 -1
  143. package/dist/proxy/worker/broadcast-D7rjohJL.js +0 -1
  144. package/dist/proxy/worker/channel-DQBWE7_E.js +0 -1
  145. package/dist/proxy/worker/i32-C78nBJH2.js +0 -1
  146. package/dist/proxy/worker/message-BvU2dKHz.js +0 -1
  147. package/dist/proxy/worker/message-DhtuceT9.js +0 -1
  148. package/dist/proxy/worker/sender-CWhM7QKB.js +0 -1
  149. package/dist/proxy/worker/shared-DDIlHBz4.js +0 -1
  150. package/dist/proxy/worker/shared-NtUlwI0Z.js +0 -1
  151. package/dist/proxy/worker/shared-QrUiXPgc.js +0 -1
  152. package/dist/proxy/worker/shared-array-buffer-cwdMr2mc.js +0 -1
  153. package/dist/proxy/worker/xhr-CK-Z6L0B.js +0 -1
  154. package/dist/proxy/worker/xhr-JKtROQ3y.js +0 -1
  155. package/dist/sender-D7tfUn3T.js +0 -1
  156. package/dist/shared-BCEcO4-0.js +0 -1
  157. package/dist/shared-BWvJKuCK.js +0 -1
  158. package/dist/shared-BhhIigP4.js +0 -1
  159. package/dist/shared-array-buffer-cwdMr2mc.js +0 -1
  160. package/dist/with-resolvers-CHEvl4oe.js +0 -1
  161. package/dist/xhr-Bd0lMv0-.js +0 -1
  162. package/dist/xhr-DaixJn9d.js +0 -1
@@ -0,0 +1,431 @@
1
+ import { w as withResolvers } from './with-resolvers-DsO_nhbc.js';
2
+ import { i as i32 } from './i32-UV5fM9Tw.js';
3
+ import { i as identity, t as SHARED_CHANNEL, w as UNDEFINED, d as dv, c as BIGINT, u as u8a8, S as SYMBOL, x as toSymbol, T as TRUE, j as FALSE, y as encoder$1, p as STRING, B as BLOB, F as FILE, k as REGEXP, m as ImageData, l as IMAGE_DATA, E as ERROR, o as SET, M as MAP, D as DATE, r as BUFFER, z as isView, V as VIEW, C as isArray$1, N as NULL, U as UI8, s as NUMBER, h as NAN, Z as ZERO, e as N_ZERO, g as N_INFINITY, I as INFINITY, R as RECURSION, G as push, a as BIGUINT, O as OBJECT, A as ARRAY, b as byteOffset } from './shared-Ccm1cPah.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,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 };
package/dist/sw.js CHANGED
@@ -1 +1,155 @@
1
- const e=Promise.withResolvers||function(){var e,t,n=new this((n,s)=>{e=n,t=s});return{resolve:e,reject:t,promise:n}};var t=e.bind(Promise);const[n,s]=((e=e=>e)=>{const n=new Map,s=(()=>{const e=new Int32Array(1);return()=>e[0]++})();return[()=>{let a;do{a=e(s())}while(n.has(a));const o=t();return n.set(a,o),[a,o.promise]},(e,t,s)=>{const a=n.get(e);n.delete(e),s?a?.reject(s):a?.resolve(t)}]})(),{protocol:a,host:o,pathname:r}=location,i=`${a}//${o}${r}`,c=new BroadcastChannel("fc260aad-4404-43b8-ae9d-2c06554bb294");c.addEventListener("message",({data:[e,t]})=>{if("response"===e){const[e,n]=t;s(e,`[${n.join(",")}]`)}});const d={status:200,statusText:"OK",headers:new Headers({"Cache-Control":"no-cache, must-revalidate",Expires:"Mon, 26 Jul 1997 05:00:00 GMT","Content-type":"application/json"})},l=async e=>{const[t,s]=n();return c.postMessage(["request",[t,e]]),new Response(await s,d)};addEventListener("activate",e=>e.waitUntil(clients.claim())),addEventListener("fetch",async e=>{const{request:t}=e;"POST"===t.method&&t.url.startsWith(i)&&(e.stopImmediatePropagation(),e.respondWith(t.json().then(l)),e.preventDefault())}),addEventListener("install",()=>skipWaiting());
1
+ // shorter: 'fc260aad-4404-43b8-ae9d-2c06554bb294'.split('-').map(s => parseInt(s, 16)).reduce((p, c) => p + c).toString(16)
2
+ var CHANNEL = 'fc260aad-4404-43b8-ae9d-2c06554bb294';
3
+
4
+ //@ts-check
5
+
6
+ /**
7
+ * @template T
8
+ * @typedef {{promise: Promise<T>, resolve: (value: T) => void, reject: (reason?: any) => void}} Resolvers
9
+ */
10
+
11
+ // fallback for Android WebView
12
+ //@ts-ignore
13
+ const withResolvers = Promise.withResolvers || function withResolvers() {
14
+ var a, b, c = new this((resolve, reject) => {
15
+ a = resolve;
16
+ b = reject;
17
+ });
18
+ return {resolve: a, reject: b, promise: c};
19
+ };
20
+
21
+ /**
22
+ * @template T
23
+ * @type {() => Resolvers<T>}
24
+ */
25
+ var withResolvers$1 = withResolvers.bind(Promise);
26
+
27
+ // @ts-check
28
+
29
+ /**
30
+ * @param {number} [start]
31
+ * @returns {() => number}
32
+ */
33
+ var i32 = start => {
34
+ const i32 = new Int32Array(1);
35
+ return () => i32[0]++;
36
+ };
37
+
38
+ //@ts-check
39
+
40
+
41
+ /**
42
+ * @template V
43
+ * @callback Resolve
44
+ * @param {V?} [value]
45
+ * @returns {void}
46
+ */
47
+
48
+ /**
49
+ * @callback Reject
50
+ * @param {any?} [error]
51
+ * @returns {void}
52
+ */
53
+
54
+ /**
55
+ * @template V
56
+ * @typedef {object} Resolvers
57
+ * @prop {Promise<V>} promise
58
+ * @prop {Resolve<V>} resolve
59
+ * @prop {Reject} reject
60
+ */
61
+
62
+ /**
63
+ * @template K,V
64
+ * @typedef {() => [K, Promise<V>]} Next
65
+ */
66
+
67
+ /**
68
+ * @template K,V
69
+ * @callback Resolver
70
+ * @param {K} uid
71
+ * @param {V?} [value]
72
+ * @param {any?} [error]
73
+ */
74
+
75
+ /**
76
+ * @template K,V
77
+ * @typedef {[Next<K,V>, Resolver<K,V>]} NextResolver
78
+ */
79
+
80
+ /**
81
+ * @template K,V
82
+ * @param {(id: number) => K} [as]
83
+ * @returns
84
+ */
85
+ var nextResolver = (as = (id => /** @type {K} */(id))) => {
86
+ /** @type {Map<K,Resolvers<V>>} */
87
+ const map = new Map;
88
+ const next = i32();
89
+ return /** @type {NextResolver<K,V>} */([
90
+ /** @type {Next<K,V>} */
91
+ () => {
92
+ let uid;
93
+ do { uid = as(next()); }
94
+ while (map.has(uid));
95
+ const wr = /** @type {Resolvers<V>} */(/** @type {unknown} */(withResolvers$1()));
96
+ map.set(uid, wr);
97
+ return [uid, wr.promise];
98
+ },
99
+ /** @type {Resolver<K,V>} */
100
+ (uid, value, error) => {
101
+ const wr = map.get(uid);
102
+ map.delete(uid);
103
+ if (error) wr?.reject(error);
104
+ else wr?.resolve(value);
105
+ },
106
+ ]);
107
+ };
108
+
109
+ const [next, resolve] = nextResolver();
110
+
111
+ const { protocol, host, pathname } = location;
112
+ const url = `${protocol}//${host}${pathname}`;
113
+
114
+ const bc = new BroadcastChannel(CHANNEL);
115
+ bc.addEventListener('message', ({ data: [op, details] }) => {
116
+ if (op === 'response') {
117
+ const [uid, payload] = details;
118
+ resolve(uid, `[${payload.join(',')}]`);
119
+ }
120
+ });
121
+
122
+ const response = {
123
+ status: 200,
124
+ statusText: 'OK',
125
+ headers: new Headers({
126
+ 'Cache-Control': 'no-cache, must-revalidate',
127
+ 'Expires': 'Mon, 26 Jul 1997 05:00:00 GMT',
128
+ 'Content-type': 'application/json',
129
+ })
130
+ };
131
+
132
+ const respond = async details => {
133
+ const [uid, promise] = next();
134
+ bc.postMessage(['request', [uid, details]]);
135
+ return new Response(await promise, response);
136
+ };
137
+
138
+ // @ts-ignore
139
+ const activate = event => event.waitUntil(clients.claim());
140
+
141
+ const fetch = async event => {
142
+ const { request: r } = event;
143
+ if (r.method === 'POST' && r.url.startsWith(url)) {
144
+ event.stopImmediatePropagation();
145
+ event.respondWith(r.json().then(respond));
146
+ event.preventDefault();
147
+ }
148
+ };
149
+
150
+ // @ts-ignore
151
+ const install = () => skipWaiting();
152
+
153
+ addEventListener('activate', activate);
154
+ addEventListener('fetch', fetch);
155
+ addEventListener('install', install);
@@ -0,0 +1,24 @@
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
+ export { withResolvers$1 as w };