ziex 0.1.0-dev.1367 → 0.1.0-dev.1381
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/aws-lambda/index.js +20 -29
- package/build.zig.zon +10 -3
- package/cloudflare/index.js +1635 -1854
- package/index.js +155 -216
- package/{pkg/ziex/src/react → jsx}/dom.d.ts +5 -9
- package/{pkg/ziex/src/react → jsx}/index.d.ts +1 -1
- package/{react → jsx}/index.js +19 -25
- package/{pkg/ziex/src/react → jsx}/types.d.ts +14 -9
- package/package.json +6 -14
- package/vercel/index.js +0 -15
- package/wasm/index.js +253 -261
- package/wasm/init.dev.js +242 -189
- package/wasm/init.js +3 -3
- package/pkg/ziex/src/zx.d.ts +0 -1
- package/vendor/jsz/js/src/index.d.ts +0 -1
- package/vendor/jsz/js/src/zigjs.d.ts +0 -107
- /package/{pkg/ziex/src/app.d.ts → app.d.ts} +0 -0
- /package/{pkg/ziex/src/aws-lambda → aws-lambda}/index.d.ts +0 -0
- /package/{pkg/ziex/src/browser → browser}/kv.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/app.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/db.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/do.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/index.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/kv.d.ts +0 -0
- /package/{pkg/ziex/src/cloudflare → cloudflare}/worker.d.ts +0 -0
- /package/{pkg/ziex/src/db.d.ts → db.d.ts} +0 -0
- /package/{pkg/ziex/src/fetch.d.ts → fetch.d.ts} +0 -0
- /package/{pkg/ziex/src/index.d.ts → index.d.ts} +0 -0
- /package/{pkg/ziex/src/kv.d.ts → kv.d.ts} +0 -0
- /package/{pkg/ziex/src/runtime.d.ts → runtime.d.ts} +0 -0
- /package/{pkg/ziex/src/vercel → vercel}/index.d.ts +0 -0
- /package/{pkg/ziex/src/wasi.d.ts → wasi.d.ts} +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/core.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/dom.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/index.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/init.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/types.d.ts +0 -0
- /package/{pkg/ziex/src/wasm → wasm}/wasi.d.ts +0 -0
package/wasm/init.dev.js
CHANGED
|
@@ -1,22 +1,36 @@
|
|
|
1
1
|
// ../../vendor/jsz/js/src/zigjs.ts
|
|
2
2
|
var NAN_PREFIX = 2146959360;
|
|
3
3
|
var predefined = {
|
|
4
|
-
nan: 0,
|
|
5
|
-
null: 1,
|
|
6
|
-
true: 2,
|
|
7
|
-
false: 3,
|
|
8
|
-
undefined: 4,
|
|
9
|
-
globalThis: 5,
|
|
10
|
-
runtime: 6
|
|
4
|
+
"nan": 0,
|
|
5
|
+
"null": 1,
|
|
6
|
+
"true": 2,
|
|
7
|
+
"false": 3,
|
|
8
|
+
"undefined": 4,
|
|
9
|
+
"globalThis": 5,
|
|
10
|
+
"runtime": 6
|
|
11
11
|
};
|
|
12
12
|
var PREDEFINED_ID_MAX = 6;
|
|
13
|
-
var encoder = new TextEncoder;
|
|
13
|
+
var encoder = new TextEncoder();
|
|
14
14
|
var decoder = new TextDecoder("utf-8");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
var ZigJS = class {
|
|
16
|
+
constructor() {
|
|
17
|
+
/**
|
|
18
|
+
* The values, indexed by ID (number). Duplicate values can be in this
|
|
19
|
+
* if they are loaded multiple times. That just acts as duplicate references.
|
|
20
|
+
* */
|
|
21
|
+
this.values = [NaN, null, true, false, void 0, globalThis, this];
|
|
22
|
+
/**
|
|
23
|
+
* When a value is removed from the values array, its ID is put
|
|
24
|
+
* in the pool so that we can use it next. This avoids fragmentation in
|
|
25
|
+
* our array keys.
|
|
26
|
+
* */
|
|
27
|
+
this.idPool = [];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns the import object that should be merged with your import
|
|
31
|
+
* object when instantiating your wasm instance. This injects the required
|
|
32
|
+
* functions into the wasm environment.
|
|
33
|
+
* */
|
|
20
34
|
importObject() {
|
|
21
35
|
return {
|
|
22
36
|
"zig-js": {
|
|
@@ -32,92 +46,121 @@ class ZigJS {
|
|
|
32
46
|
}
|
|
33
47
|
};
|
|
34
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Get a value from the JS environment.
|
|
51
|
+
* */
|
|
35
52
|
valueGet(out, id, ptr, len) {
|
|
36
53
|
const val = this.loadValue(id);
|
|
37
54
|
const str = this.loadString(ptr, len);
|
|
38
55
|
const result = Reflect.get(val, str);
|
|
39
56
|
this.storeValue(out, result);
|
|
40
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Set a value on an object.
|
|
60
|
+
* */
|
|
41
61
|
valueSet(id, ptr, len, refAddr) {
|
|
42
62
|
const obj = this.loadValue(id);
|
|
43
63
|
const str = this.loadString(ptr, len);
|
|
44
64
|
const val = this.loadRef(refAddr);
|
|
45
65
|
Reflect.set(obj, str, val);
|
|
46
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Dereference a value, allowing the JS environment to potentially GC it.
|
|
69
|
+
* */
|
|
47
70
|
valueDeinit(id) {
|
|
48
71
|
if (id > PREDEFINED_ID_MAX) {
|
|
49
72
|
this.values[id] = null;
|
|
50
73
|
this.idPool.push(id);
|
|
51
74
|
}
|
|
52
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Create an empty object.
|
|
78
|
+
* */
|
|
53
79
|
valueObjectCreate(out) {
|
|
54
|
-
this.storeValue(out, new Object);
|
|
80
|
+
this.storeValue(out, new Object());
|
|
55
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Creates a string on the JS side from a UTF-8 encoded string in wasm memory.
|
|
84
|
+
* */
|
|
56
85
|
valueStringCreate(out, ptr, len) {
|
|
57
86
|
const str = this.loadString(ptr, len);
|
|
58
87
|
this.storeValue(out, str);
|
|
59
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Returns the length of the string given by id.
|
|
91
|
+
* */
|
|
60
92
|
valueStringLen(id) {
|
|
61
93
|
const val = this.loadValue(id);
|
|
62
94
|
const buf = encoder.encode(val);
|
|
63
95
|
return buf.byteLength;
|
|
64
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Copy the string at id "id" into the shared memory at ptr.
|
|
99
|
+
* */
|
|
65
100
|
valueStringCopy(id, ptr, max) {
|
|
66
|
-
if (this.memory == null)
|
|
67
|
-
return;
|
|
101
|
+
if (this.memory == null) return;
|
|
68
102
|
const val = this.loadValue(id);
|
|
69
103
|
const bytes = encoder.encode(val);
|
|
70
|
-
if (bytes.byteLength > max)
|
|
71
|
-
|
|
72
|
-
if (bytes.length === 0)
|
|
73
|
-
return;
|
|
104
|
+
if (bytes.byteLength > max) return;
|
|
105
|
+
if (bytes.length === 0) return;
|
|
74
106
|
new Uint8Array(this.memory.buffer, ptr, bytes.length).set(bytes);
|
|
75
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Call a constructor given by id.
|
|
110
|
+
* */
|
|
76
111
|
valueNew(out, id, argsAddr, argsLen) {
|
|
77
112
|
const fn = this.loadValue(id);
|
|
78
113
|
const args = [];
|
|
79
|
-
for (let i = 0;i < argsLen; i++) {
|
|
114
|
+
for (let i = 0; i < argsLen; i++) {
|
|
80
115
|
args.push(this.loadRef(argsAddr + i * 8));
|
|
81
116
|
}
|
|
82
117
|
const result = Reflect.construct(fn, args);
|
|
83
118
|
this.storeValue(out, result);
|
|
84
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Call a function given by id.
|
|
122
|
+
* */
|
|
85
123
|
funcApply(out, id, thisRefAddr, argsAddr, argsLen) {
|
|
86
124
|
const fn = this.loadValue(id);
|
|
87
125
|
const thisVal = this.loadRef(thisRefAddr);
|
|
88
126
|
const args = [];
|
|
89
|
-
for (let i = 0;i < argsLen; i++) {
|
|
127
|
+
for (let i = 0; i < argsLen; i++) {
|
|
90
128
|
args.push(this.loadRef(argsAddr + i * 8));
|
|
91
129
|
}
|
|
92
130
|
const result = Reflect.apply(fn, thisVal, args);
|
|
93
131
|
this.storeValue(out, result);
|
|
94
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* This can be used to load a value given by an ID. A WASM function
|
|
135
|
+
* might return a value as an ID. This can be used to retrieve it.
|
|
136
|
+
* */
|
|
95
137
|
loadValue(id) {
|
|
96
138
|
return this.values[id];
|
|
97
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* This removes the value from the values list and returns it. This
|
|
142
|
+
* will not remove predefined values but can still be used to retrieve
|
|
143
|
+
* them.
|
|
144
|
+
* */
|
|
98
145
|
deleteValue(id) {
|
|
99
146
|
const val = this.values[id];
|
|
100
147
|
this.valueDeinit(id);
|
|
101
148
|
return val;
|
|
102
149
|
}
|
|
103
150
|
loadRef(refAddr) {
|
|
104
|
-
if (this.memory == null)
|
|
105
|
-
return;
|
|
151
|
+
if (this.memory == null) return;
|
|
106
152
|
const view = new DataView(this.memory.buffer);
|
|
107
153
|
const floatVal = view.getFloat64(refAddr, true);
|
|
108
|
-
if (!isNaN(floatVal))
|
|
109
|
-
return floatVal;
|
|
154
|
+
if (!isNaN(floatVal)) return floatVal;
|
|
110
155
|
const id = this.loadRefId(refAddr);
|
|
111
156
|
return this.values[id];
|
|
112
157
|
}
|
|
113
158
|
loadRefId(refAddr) {
|
|
114
|
-
if (this.memory == null)
|
|
115
|
-
return 0;
|
|
159
|
+
if (this.memory == null) return 0;
|
|
116
160
|
return new DataView(this.memory.buffer).getUint32(refAddr, true);
|
|
117
161
|
}
|
|
118
162
|
storeValue(out, val) {
|
|
119
|
-
if (this.memory == null)
|
|
120
|
-
return;
|
|
163
|
+
if (this.memory == null) return;
|
|
121
164
|
const view = new DataView(this.memory.buffer);
|
|
122
165
|
if (typeof val === "number") {
|
|
123
166
|
if (isNaN(val)) {
|
|
@@ -133,7 +176,7 @@ class ZigJS {
|
|
|
133
176
|
view.setUint32(out + 4, NAN_PREFIX, true);
|
|
134
177
|
return;
|
|
135
178
|
}
|
|
136
|
-
if (val ===
|
|
179
|
+
if (val === void 0) {
|
|
137
180
|
view.setUint32(out, predefined.undefined, true);
|
|
138
181
|
view.setUint32(out + 4, NAN_PREFIX, true);
|
|
139
182
|
return;
|
|
@@ -148,7 +191,7 @@ class ZigJS {
|
|
|
148
191
|
return;
|
|
149
192
|
}
|
|
150
193
|
let id = this.idPool.pop();
|
|
151
|
-
if (id ===
|
|
194
|
+
if (id === void 0) {
|
|
152
195
|
id = this.values.length;
|
|
153
196
|
}
|
|
154
197
|
this.values[id] = val;
|
|
@@ -171,23 +214,22 @@ class ZigJS {
|
|
|
171
214
|
view.setUint32(out + 4, NAN_PREFIX | typeId, true);
|
|
172
215
|
}
|
|
173
216
|
loadString(ptr, len) {
|
|
174
|
-
if (this.memory == null)
|
|
175
|
-
return "";
|
|
217
|
+
if (this.memory == null) return "";
|
|
176
218
|
const arr = new Uint8ClampedArray(this.memory.buffer, ptr, Number(len));
|
|
177
219
|
const data = arr.slice();
|
|
178
220
|
return decoder.decode(data);
|
|
179
221
|
}
|
|
180
|
-
}
|
|
222
|
+
};
|
|
223
|
+
|
|
181
224
|
// src/fetch.ts
|
|
182
225
|
function createFetchImports(getMemory) {
|
|
183
|
-
const encoder2 = new TextEncoder;
|
|
184
|
-
const decoder2 = new TextDecoder;
|
|
226
|
+
const encoder2 = new TextEncoder();
|
|
227
|
+
const decoder2 = new TextDecoder();
|
|
185
228
|
function readStr(ptr, len) {
|
|
186
229
|
return decoder2.decode(new Uint8Array(getMemory().buffer, ptr, len));
|
|
187
230
|
}
|
|
188
|
-
function
|
|
189
|
-
if (data.length > buf_max)
|
|
190
|
-
return -2;
|
|
231
|
+
function writeBytes2(buf_ptr, buf_max, data) {
|
|
232
|
+
if (data.length > buf_max) return -2;
|
|
191
233
|
new Uint8Array(getMemory().buffer, buf_ptr, data.length).set(data);
|
|
192
234
|
return data.length;
|
|
193
235
|
}
|
|
@@ -199,8 +241,7 @@ function createFetchImports(getMemory) {
|
|
|
199
241
|
try {
|
|
200
242
|
headers = JSON.parse(headersJson);
|
|
201
243
|
} catch {
|
|
202
|
-
for (const line of headersJson.split(
|
|
203
|
-
`)) {
|
|
244
|
+
for (const line of headersJson.split("\n")) {
|
|
204
245
|
const colonIdx = line.indexOf(":");
|
|
205
246
|
if (colonIdx > 0) {
|
|
206
247
|
headers[line.slice(0, colonIdx)] = line.slice(colonIdx + 1);
|
|
@@ -213,25 +254,23 @@ function createFetchImports(getMemory) {
|
|
|
213
254
|
const url = readStr(url_ptr, url_len);
|
|
214
255
|
const method = method_len > 0 ? readStr(method_ptr, method_len) : "GET";
|
|
215
256
|
const headersJson = headers_len > 0 ? readStr(headers_ptr, headers_len) : "{}";
|
|
216
|
-
const body = body_len > 0 ? readBytes(body_ptr, body_len) :
|
|
257
|
+
const body = body_len > 0 ? readBytes(body_ptr, body_len) : void 0;
|
|
217
258
|
const headers = parseHeaders(headersJson);
|
|
218
|
-
const controller = new AbortController;
|
|
259
|
+
const controller = new AbortController();
|
|
219
260
|
const timeout = timeout_ms > 0 ? setTimeout(() => controller.abort(), timeout_ms) : null;
|
|
220
261
|
try {
|
|
221
262
|
const response = await fetch(url, {
|
|
222
263
|
method,
|
|
223
|
-
headers: Object.keys(headers).length > 0 ? headers :
|
|
224
|
-
body: method !== "GET" && method !== "HEAD" ? body :
|
|
264
|
+
headers: Object.keys(headers).length > 0 ? headers : void 0,
|
|
265
|
+
body: method !== "GET" && method !== "HEAD" ? body : void 0,
|
|
225
266
|
signal: controller.signal
|
|
226
267
|
});
|
|
227
|
-
if (timeout)
|
|
228
|
-
clearTimeout(timeout);
|
|
268
|
+
if (timeout) clearTimeout(timeout);
|
|
229
269
|
new DataView(getMemory().buffer).setUint16(status_out, response.status, true);
|
|
230
270
|
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
231
|
-
return
|
|
271
|
+
return writeBytes2(buf_ptr, buf_max, bytes);
|
|
232
272
|
} catch (err) {
|
|
233
|
-
if (timeout)
|
|
234
|
-
clearTimeout(timeout);
|
|
273
|
+
if (timeout) clearTimeout(timeout);
|
|
235
274
|
const msg = err instanceof Error ? err.message : String(err);
|
|
236
275
|
console.error(`wasm fetch failed: ${url} (${msg})`);
|
|
237
276
|
return -1;
|
|
@@ -260,7 +299,7 @@ var CallbackType = {
|
|
|
260
299
|
WebSocketError: 7,
|
|
261
300
|
WebSocketClose: 8
|
|
262
301
|
};
|
|
263
|
-
var jsz = new ZigJS;
|
|
302
|
+
var jsz = new ZigJS();
|
|
264
303
|
var tempRefBuffer = new ArrayBuffer(8);
|
|
265
304
|
var tempRefView = new DataView(tempRefBuffer);
|
|
266
305
|
function storeValueGetRef(val) {
|
|
@@ -270,8 +309,8 @@ function storeValueGetRef(val) {
|
|
|
270
309
|
jsz.memory = originalMemory;
|
|
271
310
|
return tempRefView.getBigUint64(0, true);
|
|
272
311
|
}
|
|
273
|
-
var textDecoder = new TextDecoder;
|
|
274
|
-
var textEncoder = new TextEncoder;
|
|
312
|
+
var textDecoder = new TextDecoder();
|
|
313
|
+
var textEncoder = new TextEncoder();
|
|
275
314
|
var memoryView = null;
|
|
276
315
|
var memoryBuffer = null;
|
|
277
316
|
function getMemoryView() {
|
|
@@ -282,15 +321,14 @@ function getMemoryView() {
|
|
|
282
321
|
}
|
|
283
322
|
return memoryView;
|
|
284
323
|
}
|
|
285
|
-
var stringCache = new Map;
|
|
324
|
+
var stringCache = /* @__PURE__ */ new Map();
|
|
286
325
|
function stringCacheKey(ptr, len) {
|
|
287
326
|
return ptr * 65536 + len;
|
|
288
327
|
}
|
|
289
328
|
function readString(ptr, len) {
|
|
290
329
|
const key = stringCacheKey(ptr, len);
|
|
291
330
|
const cached = stringCache.get(key);
|
|
292
|
-
if (cached !==
|
|
293
|
-
return cached;
|
|
331
|
+
if (cached !== void 0) return cached;
|
|
294
332
|
const str = textDecoder.decode(getMemoryView().subarray(ptr, ptr + len));
|
|
295
333
|
stringCache.set(key, str);
|
|
296
334
|
return str;
|
|
@@ -299,27 +337,22 @@ function writeBytes(ptr, data) {
|
|
|
299
337
|
getMemoryView().set(data, ptr);
|
|
300
338
|
}
|
|
301
339
|
function wrapPromisingExport(fn) {
|
|
302
|
-
if (!fn)
|
|
303
|
-
return;
|
|
340
|
+
if (!fn) return void 0;
|
|
304
341
|
const promising = WebAssembly.promising;
|
|
305
|
-
if (typeof promising !== "function")
|
|
306
|
-
return fn;
|
|
342
|
+
if (typeof promising !== "function") return fn;
|
|
307
343
|
return promising(fn);
|
|
308
344
|
}
|
|
309
345
|
function invokeWasmExport(fn, ...args) {
|
|
310
|
-
if (!fn)
|
|
311
|
-
return;
|
|
346
|
+
if (!fn) return;
|
|
312
347
|
const result = fn(...args);
|
|
313
348
|
if (result && typeof result.then === "function") {
|
|
314
|
-
result.then(
|
|
349
|
+
void result.then(void 0, (error) => {
|
|
315
350
|
console.error(error);
|
|
316
351
|
});
|
|
317
352
|
}
|
|
318
353
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
#intervals = new Map;
|
|
322
|
-
_alloc;
|
|
354
|
+
var ZxBridgeCore = class _ZxBridgeCore {
|
|
355
|
+
#intervals = /* @__PURE__ */ new Map();
|
|
323
356
|
#handler;
|
|
324
357
|
#fetchCompleteHandler;
|
|
325
358
|
constructor(exports) {
|
|
@@ -330,9 +363,9 @@ class ZxBridgeCore {
|
|
|
330
363
|
throw new Error("__zx_fetch_complete not exported from WASM");
|
|
331
364
|
}
|
|
332
365
|
this.#fetchCompleteHandler = fetchCompleteHandler;
|
|
333
|
-
if (exports.memory)
|
|
334
|
-
jsz.memory = exports.memory;
|
|
366
|
+
if (exports.memory) jsz.memory = exports.memory;
|
|
335
367
|
}
|
|
368
|
+
/** Invoke the unified callback handler */
|
|
336
369
|
#invoke(type, id, data) {
|
|
337
370
|
const handler = this.#handler;
|
|
338
371
|
if (!handler) {
|
|
@@ -342,44 +375,46 @@ class ZxBridgeCore {
|
|
|
342
375
|
const dataRef = storeValueGetRef(data);
|
|
343
376
|
invokeWasmExport(handler, type, id, dataRef);
|
|
344
377
|
}
|
|
378
|
+
/**
|
|
379
|
+
* Async fetch with full options support.
|
|
380
|
+
* Calls __zx_fetch_complete when done.
|
|
381
|
+
*/
|
|
345
382
|
fetchAsync(urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) {
|
|
346
383
|
const url = readString(urlPtr, urlLen);
|
|
347
384
|
const method = methodLen > 0 ? readString(methodPtr, methodLen) : "GET";
|
|
348
385
|
const headersJson = headersLen > 0 ? readString(headersPtr, headersLen) : "{}";
|
|
349
|
-
const body = bodyLen > 0 ? getMemoryView().subarray(bodyPtr, bodyPtr + bodyLen) :
|
|
386
|
+
const body = bodyLen > 0 ? getMemoryView().subarray(bodyPtr, bodyPtr + bodyLen) : void 0;
|
|
350
387
|
let headers = {};
|
|
351
388
|
try {
|
|
352
389
|
headers = JSON.parse(headersJson);
|
|
353
390
|
} catch {
|
|
354
|
-
for (const line of headersJson.split(
|
|
355
|
-
`)) {
|
|
391
|
+
for (const line of headersJson.split("\n")) {
|
|
356
392
|
const colonIdx = line.indexOf(":");
|
|
357
393
|
if (colonIdx > 0) {
|
|
358
394
|
headers[line.slice(0, colonIdx)] = line.slice(colonIdx + 1);
|
|
359
395
|
}
|
|
360
396
|
}
|
|
361
397
|
}
|
|
362
|
-
const controller = new AbortController;
|
|
398
|
+
const controller = new AbortController();
|
|
363
399
|
const timeout = timeoutMs > 0 ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
364
400
|
const fetchOptions = {
|
|
365
401
|
method,
|
|
366
|
-
headers: Object.keys(headers).length > 0 ? headers :
|
|
367
|
-
body: method !== "GET" && method !== "HEAD" ? body :
|
|
402
|
+
headers: Object.keys(headers).length > 0 ? headers : void 0,
|
|
403
|
+
body: method !== "GET" && method !== "HEAD" ? body : void 0,
|
|
368
404
|
signal: controller.signal
|
|
369
405
|
};
|
|
370
406
|
fetch(url, fetchOptions).then(async (response) => {
|
|
371
|
-
if (timeout)
|
|
372
|
-
clearTimeout(timeout);
|
|
407
|
+
if (timeout) clearTimeout(timeout);
|
|
373
408
|
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
374
409
|
this._notifyFetchComplete(fetchId, response.status, bytes, false);
|
|
375
410
|
}).catch((error) => {
|
|
376
|
-
if (timeout)
|
|
377
|
-
clearTimeout(timeout);
|
|
411
|
+
if (timeout) clearTimeout(timeout);
|
|
378
412
|
const isAbort = error.name === "AbortError";
|
|
379
413
|
const errorMsg = isAbort ? "Request timeout" : error.message ?? "Fetch failed";
|
|
380
414
|
this._notifyFetchComplete(fetchId, 0, errorMsg, true);
|
|
381
415
|
});
|
|
382
416
|
}
|
|
417
|
+
/** Notify WASM that a fetch completed. `body` is raw bytes on success, text on error. */
|
|
383
418
|
_notifyFetchComplete(fetchId, statusCode, body, isError) {
|
|
384
419
|
const handler = this.#fetchCompleteHandler;
|
|
385
420
|
const encoded = typeof body === "string" ? textEncoder.encode(body) : body;
|
|
@@ -387,20 +422,23 @@ class ZxBridgeCore {
|
|
|
387
422
|
writeBytes(ptr, encoded);
|
|
388
423
|
invokeWasmExport(handler, fetchId, statusCode, ptr, encoded.length, isError ? 1 : 0);
|
|
389
424
|
}
|
|
425
|
+
/** Set a timeout and callback when it fires */
|
|
390
426
|
setTimeout(callbackId, delayMs) {
|
|
391
427
|
setTimeout(() => {
|
|
392
428
|
this.#invoke(CallbackType.Timeout, callbackId, null);
|
|
393
429
|
}, delayMs);
|
|
394
430
|
}
|
|
431
|
+
/** Set an interval and callback each time it fires */
|
|
395
432
|
setInterval(callbackId, intervalMs) {
|
|
396
433
|
const handle = setInterval(() => {
|
|
397
434
|
this.#invoke(CallbackType.Interval, callbackId, null);
|
|
398
435
|
}, intervalMs);
|
|
399
436
|
this.#intervals.set(callbackId, handle);
|
|
400
437
|
}
|
|
438
|
+
/** Clear an interval */
|
|
401
439
|
clearInterval(callbackId) {
|
|
402
440
|
const handle = this.#intervals.get(callbackId);
|
|
403
|
-
if (handle !==
|
|
441
|
+
if (handle !== void 0) {
|
|
404
442
|
clearInterval(handle);
|
|
405
443
|
this.#intervals.delete(callbackId);
|
|
406
444
|
}
|
|
@@ -411,6 +449,7 @@ class ZxBridgeCore {
|
|
|
411
449
|
}
|
|
412
450
|
this.#intervals.clear();
|
|
413
451
|
}
|
|
452
|
+
/** Write a string to WASM memory, returning pointer and length */
|
|
414
453
|
_writeStringToWasm(str) {
|
|
415
454
|
return this._writeBytesToWasm(textEncoder.encode(str));
|
|
416
455
|
}
|
|
@@ -419,6 +458,7 @@ class ZxBridgeCore {
|
|
|
419
458
|
writeBytes(ptr, data);
|
|
420
459
|
return { ptr, len: data.length };
|
|
421
460
|
}
|
|
461
|
+
/** Log a message from WASM at the given level (0=error, 1=warn, 2=info, 3=debug) */
|
|
422
462
|
static log(level, ptr, len) {
|
|
423
463
|
const msg = textDecoder.decode(getMemoryView().subarray(ptr, ptr + len));
|
|
424
464
|
switch (level) {
|
|
@@ -436,18 +476,34 @@ class ZxBridgeCore {
|
|
|
436
476
|
break;
|
|
437
477
|
}
|
|
438
478
|
}
|
|
479
|
+
/**
|
|
480
|
+
* Create the core import object for WASM instantiation.
|
|
481
|
+
* Includes only environment-agnostic bindings: log, fetch, and timers.
|
|
482
|
+
* Use ZxBridge.createImportObject (from wasm/index.ts) in browser contexts
|
|
483
|
+
* to additionally include DOM and WebSocket bindings.
|
|
484
|
+
*/
|
|
439
485
|
static createImportObject(bridgeRef) {
|
|
440
486
|
return {
|
|
441
487
|
...jsz.importObject(),
|
|
442
488
|
__zx_net: createFetchImports(() => {
|
|
443
|
-
if (!jsz.memory)
|
|
444
|
-
throw new Error("WASM memory is not ready");
|
|
489
|
+
if (!jsz.memory) throw new Error("WASM memory is not ready");
|
|
445
490
|
return jsz.memory;
|
|
446
491
|
}),
|
|
447
492
|
__zx: {
|
|
448
|
-
_log: (level, ptr, len) =>
|
|
493
|
+
_log: (level, ptr, len) => _ZxBridgeCore.log(level, ptr, len),
|
|
449
494
|
_fetchAsync: (urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) => {
|
|
450
|
-
bridgeRef.current?.fetchAsync(
|
|
495
|
+
bridgeRef.current?.fetchAsync(
|
|
496
|
+
urlPtr,
|
|
497
|
+
urlLen,
|
|
498
|
+
methodPtr,
|
|
499
|
+
methodLen,
|
|
500
|
+
headersPtr,
|
|
501
|
+
headersLen,
|
|
502
|
+
bodyPtr,
|
|
503
|
+
bodyLen,
|
|
504
|
+
timeoutMs,
|
|
505
|
+
fetchId
|
|
506
|
+
);
|
|
451
507
|
},
|
|
452
508
|
_setTimeout: (callbackId, delayMs) => {
|
|
453
509
|
bridgeRef.current?.setTimeout(callbackId, delayMs);
|
|
@@ -461,11 +517,11 @@ class ZxBridgeCore {
|
|
|
461
517
|
}
|
|
462
518
|
};
|
|
463
519
|
}
|
|
464
|
-
}
|
|
520
|
+
};
|
|
521
|
+
|
|
465
522
|
// src/kv.ts
|
|
466
523
|
function putOptions(ttlSeconds) {
|
|
467
|
-
if (ttlSeconds < 60)
|
|
468
|
-
return;
|
|
524
|
+
if (ttlSeconds < 60) return void 0;
|
|
469
525
|
return { expirationTtl: ttlSeconds };
|
|
470
526
|
}
|
|
471
527
|
function isSyncKVNamespace(binding) {
|
|
@@ -473,14 +529,13 @@ function isSyncKVNamespace(binding) {
|
|
|
473
529
|
return typeof candidate.getSync === "function" && typeof candidate.putSync === "function" && typeof candidate.deleteSync === "function" && typeof candidate.listSync === "function";
|
|
474
530
|
}
|
|
475
531
|
function createKVImports(bindings, getMemory) {
|
|
476
|
-
const encoder2 = new TextEncoder;
|
|
477
|
-
const decoder2 = new TextDecoder;
|
|
532
|
+
const encoder2 = new TextEncoder();
|
|
533
|
+
const decoder2 = new TextDecoder();
|
|
478
534
|
function readStr(ptr, len) {
|
|
479
535
|
return decoder2.decode(new Uint8Array(getMemory().buffer, ptr, len));
|
|
480
536
|
}
|
|
481
537
|
function writeBytes2(buf_ptr, buf_max, data) {
|
|
482
|
-
if (data.length > buf_max)
|
|
483
|
-
return -2;
|
|
538
|
+
if (data.length > buf_max) return -2;
|
|
484
539
|
new Uint8Array(getMemory().buffer, buf_ptr, data.length).set(data);
|
|
485
540
|
return data.length;
|
|
486
541
|
}
|
|
@@ -489,40 +544,36 @@ function createKVImports(bindings, getMemory) {
|
|
|
489
544
|
}
|
|
490
545
|
const Suspending = WebAssembly.Suspending;
|
|
491
546
|
if (typeof Suspending !== "function") {
|
|
492
|
-
let
|
|
547
|
+
let syncBinding2 = function(ns) {
|
|
493
548
|
const candidate = binding(ns);
|
|
494
549
|
return candidate && isSyncKVNamespace(candidate) ? candidate : null;
|
|
495
550
|
};
|
|
551
|
+
var syncBinding = syncBinding2;
|
|
496
552
|
return {
|
|
497
553
|
kv_get: (ns_ptr, ns_len, key_ptr, key_len, buf_ptr, buf_max) => {
|
|
498
|
-
const b =
|
|
499
|
-
if (!b)
|
|
500
|
-
return -1;
|
|
554
|
+
const b = syncBinding2(readStr(ns_ptr, ns_len));
|
|
555
|
+
if (!b) return -1;
|
|
501
556
|
const value = b.getSync(readStr(key_ptr, key_len));
|
|
502
|
-
if (value === null)
|
|
503
|
-
return -1;
|
|
557
|
+
if (value === null) return -1;
|
|
504
558
|
return writeBytes2(buf_ptr, buf_max, encoder2.encode(value));
|
|
505
559
|
},
|
|
506
560
|
kv_put: (ns_ptr, ns_len, key_ptr, key_len, val_ptr, val_len, ttl_seconds) => {
|
|
507
|
-
const b =
|
|
508
|
-
if (!b)
|
|
509
|
-
return 0;
|
|
561
|
+
const b = syncBinding2(readStr(ns_ptr, ns_len));
|
|
562
|
+
if (!b) return 0;
|
|
510
563
|
b.putSync(readStr(key_ptr, key_len), readStr(val_ptr, val_len), putOptions(ttl_seconds));
|
|
511
564
|
return 0;
|
|
512
565
|
},
|
|
513
566
|
kv_delete: (ns_ptr, ns_len, key_ptr, key_len) => {
|
|
514
|
-
const b =
|
|
515
|
-
if (!b)
|
|
516
|
-
return 0;
|
|
567
|
+
const b = syncBinding2(readStr(ns_ptr, ns_len));
|
|
568
|
+
if (!b) return 0;
|
|
517
569
|
b.deleteSync(readStr(key_ptr, key_len));
|
|
518
570
|
return 0;
|
|
519
571
|
},
|
|
520
572
|
kv_list: (ns_ptr, ns_len, pfx_ptr, pfx_len, buf_ptr, buf_max) => {
|
|
521
|
-
const b =
|
|
522
|
-
if (!b)
|
|
523
|
-
return writeBytes2(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
573
|
+
const b = syncBinding2(readStr(ns_ptr, ns_len));
|
|
574
|
+
if (!b) return writeBytes2(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
524
575
|
const prefix = readStr(pfx_ptr, pfx_len);
|
|
525
|
-
const result = b.listSync(prefix.length > 0 ? { prefix } :
|
|
576
|
+
const result = b.listSync(prefix.length > 0 ? { prefix } : void 0);
|
|
526
577
|
return writeBytes2(buf_ptr, buf_max, encoder2.encode(JSON.stringify(result.keys.map((k) => k.name))));
|
|
527
578
|
}
|
|
528
579
|
};
|
|
@@ -530,33 +581,28 @@ function createKVImports(bindings, getMemory) {
|
|
|
530
581
|
return {
|
|
531
582
|
kv_get: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len, buf_ptr, buf_max) => {
|
|
532
583
|
const b = binding(readStr(ns_ptr, ns_len));
|
|
533
|
-
if (!b)
|
|
534
|
-
return -1;
|
|
584
|
+
if (!b) return -1;
|
|
535
585
|
const value = await b.get(readStr(key_ptr, key_len));
|
|
536
|
-
if (value === null)
|
|
537
|
-
return -1;
|
|
586
|
+
if (value === null) return -1;
|
|
538
587
|
return writeBytes2(buf_ptr, buf_max, encoder2.encode(value));
|
|
539
588
|
}),
|
|
540
589
|
kv_put: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len, val_ptr, val_len, ttl_seconds) => {
|
|
541
590
|
const b = binding(readStr(ns_ptr, ns_len));
|
|
542
|
-
if (!b)
|
|
543
|
-
return -1;
|
|
591
|
+
if (!b) return -1;
|
|
544
592
|
await b.put(readStr(key_ptr, key_len), readStr(val_ptr, val_len), putOptions(ttl_seconds));
|
|
545
593
|
return 0;
|
|
546
594
|
}),
|
|
547
595
|
kv_delete: new Suspending(async (ns_ptr, ns_len, key_ptr, key_len) => {
|
|
548
596
|
const b = binding(readStr(ns_ptr, ns_len));
|
|
549
|
-
if (!b)
|
|
550
|
-
return -1;
|
|
597
|
+
if (!b) return -1;
|
|
551
598
|
await b.delete(readStr(key_ptr, key_len));
|
|
552
599
|
return 0;
|
|
553
600
|
}),
|
|
554
601
|
kv_list: new Suspending(async (ns_ptr, ns_len, prefix_ptr, prefix_len, buf_ptr, buf_max) => {
|
|
555
602
|
const b = binding(readStr(ns_ptr, ns_len));
|
|
556
|
-
if (!b)
|
|
557
|
-
return writeBytes2(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
603
|
+
if (!b) return writeBytes2(buf_ptr, buf_max, encoder2.encode("[]"));
|
|
558
604
|
const prefix = readStr(prefix_ptr, prefix_len);
|
|
559
|
-
const result = await b.list(prefix.length > 0 ? { prefix } :
|
|
605
|
+
const result = await b.list(prefix.length > 0 ? { prefix } : void 0);
|
|
560
606
|
return writeBytes2(buf_ptr, buf_max, encoder2.encode(JSON.stringify(result.keys.map((k) => k.name))));
|
|
561
607
|
})
|
|
562
608
|
};
|
|
@@ -668,10 +714,9 @@ function createLocalStorageKV(options = {}) {
|
|
|
668
714
|
listSync(options2) {
|
|
669
715
|
const prefix = namespacePrefix + (options2?.prefix ?? "");
|
|
670
716
|
const keys = [];
|
|
671
|
-
for (let i = 0;i < storage.length; i += 1) {
|
|
717
|
+
for (let i = 0; i < storage.length; i += 1) {
|
|
672
718
|
const key = storage.key(i);
|
|
673
|
-
if (!key || !key.startsWith(prefix))
|
|
674
|
-
continue;
|
|
719
|
+
if (!key || !key.startsWith(prefix)) continue;
|
|
675
720
|
keys.push({ name: key.slice(namespacePrefix.length) });
|
|
676
721
|
}
|
|
677
722
|
return { keys };
|
|
@@ -692,8 +737,8 @@ function createBrowserKVBindings(options = {}) {
|
|
|
692
737
|
}
|
|
693
738
|
|
|
694
739
|
// src/wasm/index.ts
|
|
695
|
-
|
|
696
|
-
#websockets = new Map;
|
|
740
|
+
var ZxBridge = class extends ZxBridgeCore {
|
|
741
|
+
#websockets = /* @__PURE__ */ new Map();
|
|
697
742
|
#wsOnOpenHandler;
|
|
698
743
|
#wsOnMessageHandler;
|
|
699
744
|
#wsOnErrorHandler;
|
|
@@ -707,7 +752,9 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
707
752
|
this.#wsOnErrorHandler = wrapPromisingExport(exports.__zx_ws_onerror);
|
|
708
753
|
this.#wsOnCloseHandler = wrapPromisingExport(exports.__zx_ws_onclose);
|
|
709
754
|
this.#eventbridge = exports.__zx_eventbridge;
|
|
710
|
-
this.#eventbridgeAsync = wrapPromisingExport(
|
|
755
|
+
this.#eventbridgeAsync = wrapPromisingExport(
|
|
756
|
+
exports.__zx_eventbridge_async ?? exports.__zx_eventbridge
|
|
757
|
+
);
|
|
711
758
|
}
|
|
712
759
|
eventMaySuspend(velementId, eventTypeId) {
|
|
713
760
|
return !!((eventHandlerModes.get(velementId) ?? 0) & 1 << eventTypeId);
|
|
@@ -716,19 +763,19 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
716
763
|
const bit = 1 << eventTypeId;
|
|
717
764
|
const current = eventHandlerModes.get(velementId) ?? 0;
|
|
718
765
|
const next = maySuspend ? current | bit : current & ~bit;
|
|
719
|
-
if (next === 0)
|
|
720
|
-
|
|
721
|
-
else
|
|
722
|
-
eventHandlerModes.set(velementId, next);
|
|
766
|
+
if (next === 0) eventHandlerModes.delete(velementId);
|
|
767
|
+
else eventHandlerModes.set(velementId, next);
|
|
723
768
|
}
|
|
724
769
|
clearEventHandlerModes(velementId) {
|
|
725
770
|
eventHandlerModes.delete(velementId);
|
|
726
771
|
}
|
|
772
|
+
/** Submit a form action with bound-state round-trip. */
|
|
727
773
|
submitFormActionAsync(form, actionId, statesJson, fetchId) {
|
|
728
774
|
const formData = new FormData(form);
|
|
729
775
|
formData.append("__$states", statesJson);
|
|
730
776
|
fetch(window.location.href, {
|
|
731
777
|
method: "POST",
|
|
778
|
+
// `>>> 0` reinterprets the i32 from wasm as u32 (ids can exceed 2^31).
|
|
732
779
|
headers: { "x-action": String(actionId >>> 0) },
|
|
733
780
|
body: formData
|
|
734
781
|
}).then(async (response) => {
|
|
@@ -739,25 +786,27 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
739
786
|
this._notifyFetchComplete(fetchId, 0, msg, true);
|
|
740
787
|
});
|
|
741
788
|
}
|
|
789
|
+
/**
|
|
790
|
+
* Create and connect a WebSocket.
|
|
791
|
+
* Calls __zx_ws_onopen, __zx_ws_onmessage, __zx_ws_onerror, __zx_ws_onclose.
|
|
792
|
+
*/
|
|
742
793
|
wsConnect(wsId, urlPtr, urlLen, protocolsPtr, protocolsLen) {
|
|
743
794
|
const url = readString(urlPtr, urlLen);
|
|
744
795
|
const protocolsStr = protocolsLen > 0 ? readString(protocolsPtr, protocolsLen) : "";
|
|
745
|
-
const protocols = protocolsStr ? protocolsStr.split(",").map((p) => p.trim()).filter(Boolean) :
|
|
796
|
+
const protocols = protocolsStr ? protocolsStr.split(",").map((p) => p.trim()).filter(Boolean) : void 0;
|
|
746
797
|
try {
|
|
747
798
|
const ws = protocols && protocols.length > 0 ? new WebSocket(url, protocols) : new WebSocket(url);
|
|
748
799
|
ws.binaryType = "arraybuffer";
|
|
749
800
|
ws.onopen = () => {
|
|
750
801
|
const handler = this.#wsOnOpenHandler;
|
|
751
|
-
if (!handler)
|
|
752
|
-
return;
|
|
802
|
+
if (!handler) return;
|
|
753
803
|
const protocol = ws.protocol || "";
|
|
754
804
|
const { ptr, len } = this._writeStringToWasm(protocol);
|
|
755
805
|
invokeWasmExport(handler, wsId, ptr, len);
|
|
756
806
|
};
|
|
757
807
|
ws.onmessage = (event) => {
|
|
758
808
|
const handler = this.#wsOnMessageHandler;
|
|
759
|
-
if (!handler)
|
|
760
|
-
return;
|
|
809
|
+
if (!handler) return;
|
|
761
810
|
const isBinary = event.data instanceof ArrayBuffer;
|
|
762
811
|
const data = isBinary ? new Uint8Array(event.data) : textEncoder.encode(event.data);
|
|
763
812
|
const { ptr, len } = this._writeBytesToWasm(data);
|
|
@@ -765,15 +814,13 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
765
814
|
};
|
|
766
815
|
ws.onerror = (_event) => {
|
|
767
816
|
const handler = this.#wsOnErrorHandler;
|
|
768
|
-
if (!handler)
|
|
769
|
-
return;
|
|
817
|
+
if (!handler) return;
|
|
770
818
|
const { ptr, len } = this._writeStringToWasm("WebSocket error");
|
|
771
819
|
invokeWasmExport(handler, wsId, ptr, len);
|
|
772
820
|
};
|
|
773
821
|
ws.onclose = (event) => {
|
|
774
822
|
const handler = this.#wsOnCloseHandler;
|
|
775
|
-
if (!handler)
|
|
776
|
-
return;
|
|
823
|
+
if (!handler) return;
|
|
777
824
|
const reason = event.reason || "";
|
|
778
825
|
const { ptr, len } = this._writeStringToWasm(reason);
|
|
779
826
|
invokeWasmExport(handler, wsId, event.code, ptr, len, event.wasClean ? 1 : 0);
|
|
@@ -789,10 +836,10 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
789
836
|
}
|
|
790
837
|
}
|
|
791
838
|
}
|
|
839
|
+
/** Send data over WebSocket */
|
|
792
840
|
wsSend(wsId, dataPtr, dataLen, isBinary) {
|
|
793
841
|
const ws = this.#websockets.get(wsId);
|
|
794
|
-
if (!ws || ws.readyState !== WebSocket.OPEN)
|
|
795
|
-
return;
|
|
842
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) return;
|
|
796
843
|
const memory = getMemoryView();
|
|
797
844
|
if (isBinary) {
|
|
798
845
|
ws.send(memory.slice(dataPtr, dataPtr + dataLen));
|
|
@@ -800,16 +847,14 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
800
847
|
ws.send(textDecoder.decode(memory.subarray(dataPtr, dataPtr + dataLen)));
|
|
801
848
|
}
|
|
802
849
|
}
|
|
850
|
+
/** Close WebSocket connection */
|
|
803
851
|
wsClose(wsId, code, reasonPtr, reasonLen) {
|
|
804
852
|
const ws = this.#websockets.get(wsId);
|
|
805
|
-
if (!ws)
|
|
806
|
-
|
|
807
|
-
const reason = reasonLen > 0 ? readString(reasonPtr, reasonLen) : undefined;
|
|
853
|
+
if (!ws) return;
|
|
854
|
+
const reason = reasonLen > 0 ? readString(reasonPtr, reasonLen) : void 0;
|
|
808
855
|
try {
|
|
809
|
-
if (reason)
|
|
810
|
-
|
|
811
|
-
else
|
|
812
|
-
ws.close(code);
|
|
856
|
+
if (reason) ws.close(code, reason);
|
|
857
|
+
else ws.close(code);
|
|
813
858
|
} catch {
|
|
814
859
|
ws.close();
|
|
815
860
|
}
|
|
@@ -820,10 +865,12 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
820
865
|
for (const ws of this.#websockets.values()) {
|
|
821
866
|
try {
|
|
822
867
|
ws.close();
|
|
823
|
-
} catch {
|
|
868
|
+
} catch {
|
|
869
|
+
}
|
|
824
870
|
}
|
|
825
871
|
this.#websockets.clear();
|
|
826
872
|
}
|
|
873
|
+
/** Handle a DOM event (called by event delegation) */
|
|
827
874
|
eventbridge(velementId, eventTypeId, event) {
|
|
828
875
|
const eventRef = storeValueGetRef(event);
|
|
829
876
|
if (this.eventMaySuspend(velementId, eventTypeId)) {
|
|
@@ -832,12 +879,12 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
832
879
|
}
|
|
833
880
|
invokeWasmExport(this.#eventbridge, velementId, eventTypeId, eventRef);
|
|
834
881
|
}
|
|
882
|
+
/** Create the full browser import object for WASM instantiation (includes DOM + WebSocket). */
|
|
835
883
|
static createImportObject(bridgeRef) {
|
|
836
884
|
return {
|
|
837
885
|
...jsz.importObject(),
|
|
838
886
|
__zx_net: createFetchImports(() => {
|
|
839
|
-
if (!jsz.memory)
|
|
840
|
-
throw new Error("WASM memory is not ready");
|
|
887
|
+
if (!jsz.memory) throw new Error("WASM memory is not ready");
|
|
841
888
|
return jsz.memory;
|
|
842
889
|
}),
|
|
843
890
|
__zx: {
|
|
@@ -849,7 +896,18 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
849
896
|
bridgeRef.current?.clearEventHandlerModes(vnodeId);
|
|
850
897
|
},
|
|
851
898
|
_fetchAsync: (urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) => {
|
|
852
|
-
bridgeRef.current?.fetchAsync(
|
|
899
|
+
bridgeRef.current?.fetchAsync(
|
|
900
|
+
urlPtr,
|
|
901
|
+
urlLen,
|
|
902
|
+
methodPtr,
|
|
903
|
+
methodLen,
|
|
904
|
+
headersPtr,
|
|
905
|
+
headersLen,
|
|
906
|
+
bodyPtr,
|
|
907
|
+
bodyLen,
|
|
908
|
+
timeoutMs,
|
|
909
|
+
fetchId
|
|
910
|
+
);
|
|
853
911
|
},
|
|
854
912
|
_setTimeout: (callbackId, delayMs) => {
|
|
855
913
|
bridgeRef.current?.setTimeout(callbackId, delayMs);
|
|
@@ -860,6 +918,7 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
860
918
|
_clearInterval: (callbackId) => {
|
|
861
919
|
bridgeRef.current?.clearInterval(callbackId);
|
|
862
920
|
},
|
|
921
|
+
// WebSocket API
|
|
863
922
|
_wsConnect: (wsId, urlPtr, urlLen, protocolsPtr, protocolsLen) => {
|
|
864
923
|
bridgeRef.current?.wsConnect(wsId, urlPtr, urlLen, protocolsPtr, protocolsLen);
|
|
865
924
|
},
|
|
@@ -903,26 +962,22 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
903
962
|
},
|
|
904
963
|
_snv: (vnodeId, ptr, len) => {
|
|
905
964
|
const node = domNodes.get(vnodeId);
|
|
906
|
-
if (node)
|
|
907
|
-
node.nodeValue = readString(ptr, len);
|
|
965
|
+
if (node) node.nodeValue = readString(ptr, len);
|
|
908
966
|
},
|
|
909
967
|
_srh: (vnodeId, ptr, len) => {
|
|
910
968
|
const el = domNodes.get(vnodeId);
|
|
911
|
-
if (el)
|
|
912
|
-
el.innerHTML = readString(ptr, len);
|
|
969
|
+
if (el) el.innerHTML = readString(ptr, len);
|
|
913
970
|
},
|
|
914
971
|
_ac: (parentId, childId) => {
|
|
915
972
|
const parent = domNodes.get(parentId);
|
|
916
973
|
const child = domNodes.get(childId);
|
|
917
|
-
if (parent && child)
|
|
918
|
-
parent.appendChild(child);
|
|
974
|
+
if (parent && child) parent.appendChild(child);
|
|
919
975
|
},
|
|
920
976
|
_ib: (parentId, childId, refId) => {
|
|
921
977
|
const parent = domNodes.get(parentId);
|
|
922
978
|
const child = domNodes.get(childId);
|
|
923
979
|
const ref = domNodes.get(refId) ?? null;
|
|
924
|
-
if (parent && child)
|
|
925
|
-
parent.insertBefore(child, ref);
|
|
980
|
+
if (parent && child) parent.insertBefore(child, ref);
|
|
926
981
|
},
|
|
927
982
|
_rc: (parentId, childId) => {
|
|
928
983
|
const parent = domNodes.get(parentId);
|
|
@@ -949,8 +1004,7 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
949
1004
|
},
|
|
950
1005
|
_getFormData: (vnodeId, bufPtr, bufLen) => {
|
|
951
1006
|
const form = domNodes.get(vnodeId);
|
|
952
|
-
if (!form || !(form instanceof HTMLFormElement))
|
|
953
|
-
return 0;
|
|
1007
|
+
if (!form || !(form instanceof HTMLFormElement)) return 0;
|
|
954
1008
|
const formData = new FormData(form);
|
|
955
1009
|
const urlEncoded = new URLSearchParams(formData).toString();
|
|
956
1010
|
const bytes = textEncoder.encode(urlEncoded);
|
|
@@ -960,34 +1014,32 @@ class ZxBridge extends ZxBridgeCore {
|
|
|
960
1014
|
},
|
|
961
1015
|
_submitFormAction: (vnodeId, actionId) => {
|
|
962
1016
|
const form = domNodes.get(vnodeId);
|
|
963
|
-
if (!form || !(form instanceof HTMLFormElement))
|
|
964
|
-
return;
|
|
1017
|
+
if (!form || !(form instanceof HTMLFormElement)) return;
|
|
965
1018
|
const formData = new FormData(form);
|
|
966
1019
|
fetch(window.location.href, {
|
|
967
1020
|
method: "POST",
|
|
1021
|
+
// `>>> 0` reinterprets the i32 from wasm as u32 (ids can exceed 2^31).
|
|
968
1022
|
headers: { "x-action": String(actionId >>> 0) },
|
|
969
1023
|
body: formData
|
|
970
|
-
}).catch(() => {
|
|
1024
|
+
}).catch(() => {
|
|
1025
|
+
});
|
|
971
1026
|
},
|
|
972
1027
|
_submitFormActionAsync: (vnodeId, actionId, statesPtr, statesLen, fetchId) => {
|
|
973
1028
|
const form = domNodes.get(vnodeId);
|
|
974
|
-
if (!form || !(form instanceof HTMLFormElement))
|
|
975
|
-
return;
|
|
1029
|
+
if (!form || !(form instanceof HTMLFormElement)) return;
|
|
976
1030
|
const statesJson = statesLen > 0 ? readString(statesPtr, statesLen) : "[]";
|
|
977
1031
|
bridgeRef.current?.submitFormActionAsync(form, actionId, statesJson, fetchId);
|
|
978
1032
|
}
|
|
979
1033
|
}
|
|
980
1034
|
};
|
|
981
1035
|
}
|
|
982
|
-
}
|
|
983
|
-
var domNodes = new Map;
|
|
1036
|
+
};
|
|
1037
|
+
var domNodes = /* @__PURE__ */ new Map();
|
|
984
1038
|
function cleanupDomNodes(node) {
|
|
985
1039
|
const ref = node.__zx_ref;
|
|
986
|
-
if (ref !==
|
|
987
|
-
domNodes.delete(BigInt(ref));
|
|
1040
|
+
if (ref !== void 0) domNodes.delete(BigInt(ref));
|
|
988
1041
|
const children = node.childNodes;
|
|
989
|
-
for (let i = 0;i < children.length; i++)
|
|
990
|
-
cleanupDomNodes(children[i]);
|
|
1042
|
+
for (let i = 0; i < children.length; i++) cleanupDomNodes(children[i]);
|
|
991
1043
|
}
|
|
992
1044
|
var SVG_TAG_START_INDEX = 140;
|
|
993
1045
|
var TAG_NAMES = [
|
|
@@ -1131,6 +1183,7 @@ var TAG_NAMES = [
|
|
|
1131
1183
|
"strike",
|
|
1132
1184
|
"tt",
|
|
1133
1185
|
"xmp",
|
|
1186
|
+
// SVG Tags
|
|
1134
1187
|
"animate",
|
|
1135
1188
|
"animateMotion",
|
|
1136
1189
|
"animateTransform",
|
|
@@ -1197,6 +1250,7 @@ var DELEGATED_EVENTS = [
|
|
|
1197
1250
|
{ domType: "input", eventTypeId: 2 },
|
|
1198
1251
|
{ domType: "change", eventTypeId: 3 },
|
|
1199
1252
|
{ domType: "submit", eventTypeId: 4 },
|
|
1253
|
+
// focus/blur do not bubble, so delegated handlers must listen to focusin/focusout.
|
|
1200
1254
|
{ domType: "focusin", eventTypeId: 5 },
|
|
1201
1255
|
{ domType: "focusout", eventTypeId: 6 },
|
|
1202
1256
|
{ domType: "keydown", eventTypeId: 7 },
|
|
@@ -1212,21 +1266,20 @@ var DELEGATED_EVENTS = [
|
|
|
1212
1266
|
{ domType: "touchmove", eventTypeId: 17 },
|
|
1213
1267
|
{ domType: "scroll", eventTypeId: 18 }
|
|
1214
1268
|
];
|
|
1215
|
-
var eventHandlerModes = new Map;
|
|
1269
|
+
var eventHandlerModes = /* @__PURE__ */ new Map();
|
|
1216
1270
|
function initEventDelegation(bridge, rootSelector = "body") {
|
|
1217
1271
|
const root = document.querySelector(rootSelector);
|
|
1218
|
-
if (!root)
|
|
1219
|
-
|
|
1272
|
+
if (!root) return () => {
|
|
1273
|
+
};
|
|
1220
1274
|
const removers = [];
|
|
1221
1275
|
for (const delegatedEvent of DELEGATED_EVENTS) {
|
|
1222
1276
|
const listener = (event) => {
|
|
1223
1277
|
let target = event.target;
|
|
1224
1278
|
while (target && target !== document.body) {
|
|
1225
1279
|
const zxRef = target.__zx_ref;
|
|
1226
|
-
if (zxRef !==
|
|
1280
|
+
if (zxRef !== void 0) {
|
|
1227
1281
|
bridge.eventbridge(BigInt(zxRef), delegatedEvent.eventTypeId, event);
|
|
1228
|
-
if (event.cancelBubble)
|
|
1229
|
-
break;
|
|
1282
|
+
if (event.cancelBubble) break;
|
|
1230
1283
|
}
|
|
1231
1284
|
target = target.parentElement;
|
|
1232
1285
|
}
|
|
@@ -1236,8 +1289,7 @@ function initEventDelegation(bridge, rootSelector = "body") {
|
|
|
1236
1289
|
removers.push(() => root.removeEventListener(delegatedEvent.domType, listener, options));
|
|
1237
1290
|
}
|
|
1238
1291
|
return () => {
|
|
1239
|
-
for (const remove of removers)
|
|
1240
|
-
remove();
|
|
1292
|
+
for (const remove of removers) remove();
|
|
1241
1293
|
};
|
|
1242
1294
|
}
|
|
1243
1295
|
var DEFAULT_URL = "/assets/_/main.wasm";
|
|
@@ -1259,8 +1311,7 @@ function normalizeOptions(options = {}) {
|
|
|
1259
1311
|
};
|
|
1260
1312
|
}
|
|
1261
1313
|
function registerDevReinit(options) {
|
|
1262
|
-
if (typeof window === "undefined")
|
|
1263
|
-
return;
|
|
1314
|
+
if (typeof window === "undefined") return;
|
|
1264
1315
|
const reinit = () => init(options);
|
|
1265
1316
|
window.__zx_dev_reinit = reinit;
|
|
1266
1317
|
window.__ZIEX_DEVTOOLS_GLOBAL_HOOK__ = {
|
|
@@ -1280,14 +1331,17 @@ async function init(options = {}) {
|
|
|
1280
1331
|
const kvBindings = options.kv ?? createBrowserKVBindings();
|
|
1281
1332
|
const kvImportObject = {
|
|
1282
1333
|
__zx_kv: createKVImports(kvBindings, () => {
|
|
1283
|
-
if (wasmMemory)
|
|
1284
|
-
|
|
1285
|
-
if (jsz.memory)
|
|
1286
|
-
return jsz.memory;
|
|
1334
|
+
if (wasmMemory) return wasmMemory;
|
|
1335
|
+
if (jsz.memory) return jsz.memory;
|
|
1287
1336
|
throw new Error("WASM memory is not ready");
|
|
1288
1337
|
})
|
|
1289
1338
|
};
|
|
1290
|
-
const importObject = Object.assign(
|
|
1339
|
+
const importObject = Object.assign(
|
|
1340
|
+
{},
|
|
1341
|
+
ZxBridge.createImportObject(bridgeRef),
|
|
1342
|
+
kvImportObject,
|
|
1343
|
+
options.importObject
|
|
1344
|
+
);
|
|
1291
1345
|
const source = await WebAssembly.instantiateStreaming(fetch(url), importObject);
|
|
1292
1346
|
const { instance } = source;
|
|
1293
1347
|
wasmMemory = instance.exports.memory;
|
|
@@ -1306,8 +1360,7 @@ async function init(options = {}) {
|
|
|
1306
1360
|
domNodes.clear();
|
|
1307
1361
|
}
|
|
1308
1362
|
};
|
|
1309
|
-
if (true)
|
|
1310
|
-
registerDevReinit(normalizedOptions);
|
|
1363
|
+
if (true) registerDevReinit(normalizedOptions);
|
|
1311
1364
|
return { source, bridge };
|
|
1312
1365
|
}
|
|
1313
1366
|
|