@ziex/cli 0.1.0-dev.866 → 0.1.0-test.1
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/package.json +9 -28
- package/README.md +0 -195
- package/app.d.ts +0 -112
- package/aws-lambda/index.d.ts +0 -96
- package/aws-lambda/index.js +0 -126
- package/build.zig +0 -5
- package/build.zig.zon +0 -7
- package/cloudflare/app.d.ts +0 -2
- package/cloudflare/do.d.ts +0 -48
- package/cloudflare/index.d.ts +0 -4
- package/cloudflare/index.js +0 -1707
- package/cloudflare/kv.d.ts +0 -2
- package/cloudflare/worker.d.ts +0 -3
- package/index.d.ts +0 -2
- package/index.js +0 -739
- package/kv.d.ts +0 -27
- package/react/dom.d.ts +0 -21
- package/react/index.d.ts +0 -2
- package/react/index.js +0 -144
- package/react/types.d.ts +0 -190
- package/runtime.d.ts +0 -70
- package/vercel/index.d.ts +0 -26
- package/vercel/index.js +0 -18
- package/wasi.d.ts +0 -61
- package/wasm/core.d.ts +0 -78
- package/wasm/dom.d.ts +0 -4
- package/wasm/index.d.ts +0 -46
- package/wasm/index.js +0 -895
- package/wasm/init.d.ts +0 -1
- package/wasm/init.js +0 -873
- package/wasm/types.d.ts +0 -1
- package/wasm/wasi.d.ts +0 -28
- package/zx.d.ts +0 -1
package/wasm/index.js
DELETED
|
@@ -1,895 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, {
|
|
5
|
-
get: all[name],
|
|
6
|
-
enumerable: true,
|
|
7
|
-
configurable: true,
|
|
8
|
-
set: (newValue) => all[name] = () => newValue
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// ../../vendor/jsz/js/src/zigjs.ts
|
|
13
|
-
var NAN_PREFIX = 2146959360;
|
|
14
|
-
var predefined = {
|
|
15
|
-
nan: 0,
|
|
16
|
-
null: 1,
|
|
17
|
-
true: 2,
|
|
18
|
-
false: 3,
|
|
19
|
-
undefined: 4,
|
|
20
|
-
globalThis: 5,
|
|
21
|
-
runtime: 6
|
|
22
|
-
};
|
|
23
|
-
var PREDEFINED_ID_MAX = 6;
|
|
24
|
-
var encoder = new TextEncoder;
|
|
25
|
-
var decoder = new TextDecoder("utf-8");
|
|
26
|
-
|
|
27
|
-
class ZigJS {
|
|
28
|
-
memory;
|
|
29
|
-
values = [NaN, null, true, false, undefined, globalThis, this];
|
|
30
|
-
idPool = [];
|
|
31
|
-
importObject() {
|
|
32
|
-
return {
|
|
33
|
-
"zig-js": {
|
|
34
|
-
valueGet: this.valueGet.bind(this),
|
|
35
|
-
valueSet: this.valueSet.bind(this),
|
|
36
|
-
valueDeinit: this.valueDeinit.bind(this),
|
|
37
|
-
valueObjectCreate: this.valueObjectCreate.bind(this),
|
|
38
|
-
valueStringCreate: this.valueStringCreate.bind(this),
|
|
39
|
-
valueStringLen: this.valueStringLen.bind(this),
|
|
40
|
-
valueStringCopy: this.valueStringCopy.bind(this),
|
|
41
|
-
valueNew: this.valueNew.bind(this),
|
|
42
|
-
funcApply: this.funcApply.bind(this)
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
valueGet(out, id, ptr, len) {
|
|
47
|
-
const val = this.loadValue(id);
|
|
48
|
-
const str = this.loadString(ptr, len);
|
|
49
|
-
const result = Reflect.get(val, str);
|
|
50
|
-
this.storeValue(out, result);
|
|
51
|
-
}
|
|
52
|
-
valueSet(id, ptr, len, refAddr) {
|
|
53
|
-
const obj = this.loadValue(id);
|
|
54
|
-
const str = this.loadString(ptr, len);
|
|
55
|
-
const val = this.loadRef(refAddr);
|
|
56
|
-
Reflect.set(obj, str, val);
|
|
57
|
-
}
|
|
58
|
-
valueDeinit(id) {
|
|
59
|
-
if (id > PREDEFINED_ID_MAX) {
|
|
60
|
-
this.values[id] = null;
|
|
61
|
-
this.idPool.push(id);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
valueObjectCreate(out) {
|
|
65
|
-
this.storeValue(out, new Object);
|
|
66
|
-
}
|
|
67
|
-
valueStringCreate(out, ptr, len) {
|
|
68
|
-
const str = this.loadString(ptr, len);
|
|
69
|
-
this.storeValue(out, str);
|
|
70
|
-
}
|
|
71
|
-
valueStringLen(id) {
|
|
72
|
-
const val = this.loadValue(id);
|
|
73
|
-
const buf = encoder.encode(val);
|
|
74
|
-
return buf.byteLength;
|
|
75
|
-
}
|
|
76
|
-
valueStringCopy(id, ptr, max) {
|
|
77
|
-
if (this.memory == null)
|
|
78
|
-
return;
|
|
79
|
-
const val = this.loadValue(id);
|
|
80
|
-
const bytes = encoder.encode(val);
|
|
81
|
-
if (bytes.byteLength > max)
|
|
82
|
-
return;
|
|
83
|
-
new Uint8Array(this.memory.buffer, ptr, bytes.length).set(bytes);
|
|
84
|
-
}
|
|
85
|
-
valueNew(out, id, argsAddr, argsLen) {
|
|
86
|
-
const fn = this.loadValue(id);
|
|
87
|
-
const args = [];
|
|
88
|
-
for (let i = 0;i < argsLen; i++) {
|
|
89
|
-
args.push(this.loadRef(argsAddr + i * 8));
|
|
90
|
-
}
|
|
91
|
-
const result = Reflect.construct(fn, args);
|
|
92
|
-
this.storeValue(out, result);
|
|
93
|
-
}
|
|
94
|
-
funcApply(out, id, thisRefAddr, argsAddr, argsLen) {
|
|
95
|
-
const fn = this.loadValue(id);
|
|
96
|
-
const thisVal = this.loadRef(thisRefAddr);
|
|
97
|
-
const args = [];
|
|
98
|
-
for (let i = 0;i < argsLen; i++) {
|
|
99
|
-
args.push(this.loadRef(argsAddr + i * 8));
|
|
100
|
-
}
|
|
101
|
-
const result = Reflect.apply(fn, thisVal, args);
|
|
102
|
-
this.storeValue(out, result);
|
|
103
|
-
}
|
|
104
|
-
loadValue(id) {
|
|
105
|
-
return this.values[id];
|
|
106
|
-
}
|
|
107
|
-
deleteValue(id) {
|
|
108
|
-
const val = this.values[id];
|
|
109
|
-
this.valueDeinit(id);
|
|
110
|
-
return val;
|
|
111
|
-
}
|
|
112
|
-
loadRef(refAddr) {
|
|
113
|
-
if (this.memory == null)
|
|
114
|
-
return;
|
|
115
|
-
const view = new DataView(this.memory.buffer);
|
|
116
|
-
const floatVal = view.getFloat64(refAddr, true);
|
|
117
|
-
if (!isNaN(floatVal))
|
|
118
|
-
return floatVal;
|
|
119
|
-
const id = this.loadRefId(refAddr);
|
|
120
|
-
return this.values[id];
|
|
121
|
-
}
|
|
122
|
-
loadRefId(refAddr) {
|
|
123
|
-
if (this.memory == null)
|
|
124
|
-
return 0;
|
|
125
|
-
return new DataView(this.memory.buffer).getUint32(refAddr, true);
|
|
126
|
-
}
|
|
127
|
-
storeValue(out, val) {
|
|
128
|
-
if (this.memory == null)
|
|
129
|
-
return;
|
|
130
|
-
const view = new DataView(this.memory.buffer);
|
|
131
|
-
if (typeof val === "number") {
|
|
132
|
-
if (isNaN(val)) {
|
|
133
|
-
view.setUint32(out, predefined.nan, true);
|
|
134
|
-
view.setUint32(out + 4, NAN_PREFIX, true);
|
|
135
|
-
} else {
|
|
136
|
-
view.setFloat64(out, val, true);
|
|
137
|
-
}
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
if (val === null) {
|
|
141
|
-
view.setUint32(out, predefined.null, true);
|
|
142
|
-
view.setUint32(out + 4, NAN_PREFIX, true);
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
if (val === undefined) {
|
|
146
|
-
view.setUint32(out, predefined.undefined, true);
|
|
147
|
-
view.setUint32(out + 4, NAN_PREFIX, true);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
let id = this.idPool.pop();
|
|
151
|
-
if (id === undefined) {
|
|
152
|
-
id = this.values.length;
|
|
153
|
-
}
|
|
154
|
-
this.values[id] = val;
|
|
155
|
-
let typeId = 0;
|
|
156
|
-
switch (typeof val) {
|
|
157
|
-
case "object":
|
|
158
|
-
typeId = 1;
|
|
159
|
-
break;
|
|
160
|
-
case "string":
|
|
161
|
-
typeId = 2;
|
|
162
|
-
break;
|
|
163
|
-
case "symbol":
|
|
164
|
-
typeId = 3;
|
|
165
|
-
break;
|
|
166
|
-
case "function":
|
|
167
|
-
typeId = 4;
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
view.setUint32(out, Number(id), true);
|
|
171
|
-
view.setUint32(out + 4, NAN_PREFIX | typeId, true);
|
|
172
|
-
}
|
|
173
|
-
loadString(ptr, len) {
|
|
174
|
-
if (this.memory == null)
|
|
175
|
-
return "";
|
|
176
|
-
const arr = new Uint8ClampedArray(this.memory.buffer, ptr, Number(len));
|
|
177
|
-
const data = arr.slice();
|
|
178
|
-
return decoder.decode(data);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
// src/wasm/core.ts
|
|
182
|
-
var CallbackType = {
|
|
183
|
-
Event: 0,
|
|
184
|
-
FetchSuccess: 1,
|
|
185
|
-
FetchError: 2,
|
|
186
|
-
Timeout: 3,
|
|
187
|
-
Interval: 4,
|
|
188
|
-
WebSocketOpen: 5,
|
|
189
|
-
WebSocketMessage: 6,
|
|
190
|
-
WebSocketError: 7,
|
|
191
|
-
WebSocketClose: 8
|
|
192
|
-
};
|
|
193
|
-
var jsz = new ZigJS;
|
|
194
|
-
var tempRefBuffer = new ArrayBuffer(8);
|
|
195
|
-
var tempRefView = new DataView(tempRefBuffer);
|
|
196
|
-
function storeValueGetRef(val) {
|
|
197
|
-
const originalMemory = jsz.memory;
|
|
198
|
-
jsz.memory = { buffer: tempRefBuffer };
|
|
199
|
-
jsz.storeValue(0, val);
|
|
200
|
-
jsz.memory = originalMemory;
|
|
201
|
-
return tempRefView.getBigUint64(0, true);
|
|
202
|
-
}
|
|
203
|
-
var textDecoder = new TextDecoder;
|
|
204
|
-
var textEncoder = new TextEncoder;
|
|
205
|
-
var memoryView = null;
|
|
206
|
-
var memoryBuffer = null;
|
|
207
|
-
function getMemoryView() {
|
|
208
|
-
const buf = jsz.memory.buffer;
|
|
209
|
-
if (buf !== memoryBuffer) {
|
|
210
|
-
memoryBuffer = buf;
|
|
211
|
-
memoryView = new Uint8Array(buf);
|
|
212
|
-
}
|
|
213
|
-
return memoryView;
|
|
214
|
-
}
|
|
215
|
-
var stringCache = new Map;
|
|
216
|
-
function stringCacheKey(ptr, len) {
|
|
217
|
-
return ptr * 65536 + len;
|
|
218
|
-
}
|
|
219
|
-
function readString(ptr, len) {
|
|
220
|
-
const key = stringCacheKey(ptr, len);
|
|
221
|
-
const cached = stringCache.get(key);
|
|
222
|
-
if (cached !== undefined)
|
|
223
|
-
return cached;
|
|
224
|
-
const str = textDecoder.decode(getMemoryView().subarray(ptr, ptr + len));
|
|
225
|
-
stringCache.set(key, str);
|
|
226
|
-
return str;
|
|
227
|
-
}
|
|
228
|
-
function writeBytes(ptr, data) {
|
|
229
|
-
getMemoryView().set(data, ptr);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
class ZxBridgeCore {
|
|
233
|
-
#intervals = new Map;
|
|
234
|
-
_alloc;
|
|
235
|
-
#handler;
|
|
236
|
-
#fetchCompleteHandler;
|
|
237
|
-
constructor(exports) {
|
|
238
|
-
this._alloc = exports.__zx_alloc;
|
|
239
|
-
this.#handler = exports.__zx_cb;
|
|
240
|
-
this.#fetchCompleteHandler = exports.__zx_fetch_complete;
|
|
241
|
-
if (exports.memory)
|
|
242
|
-
jsz.memory = exports.memory;
|
|
243
|
-
}
|
|
244
|
-
#invoke(type, id, data) {
|
|
245
|
-
const handler = this.#handler;
|
|
246
|
-
if (!handler) {
|
|
247
|
-
console.warn("__zx_cb not exported from WASM");
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
const dataRef = storeValueGetRef(data);
|
|
251
|
-
handler(type, id, dataRef);
|
|
252
|
-
}
|
|
253
|
-
fetchAsync(urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) {
|
|
254
|
-
const url = readString(urlPtr, urlLen);
|
|
255
|
-
const method = methodLen > 0 ? readString(methodPtr, methodLen) : "GET";
|
|
256
|
-
const headersJson = headersLen > 0 ? readString(headersPtr, headersLen) : "{}";
|
|
257
|
-
const body = bodyLen > 0 ? readString(bodyPtr, bodyLen) : undefined;
|
|
258
|
-
let headers = {};
|
|
259
|
-
try {
|
|
260
|
-
headers = JSON.parse(headersJson);
|
|
261
|
-
} catch {
|
|
262
|
-
for (const line of headersJson.split(`
|
|
263
|
-
`)) {
|
|
264
|
-
const colonIdx = line.indexOf(":");
|
|
265
|
-
if (colonIdx > 0) {
|
|
266
|
-
headers[line.slice(0, colonIdx)] = line.slice(colonIdx + 1);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
const controller = new AbortController;
|
|
271
|
-
const timeout = timeoutMs > 0 ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
272
|
-
const fetchOptions = {
|
|
273
|
-
method,
|
|
274
|
-
headers: Object.keys(headers).length > 0 ? headers : undefined,
|
|
275
|
-
body: method !== "GET" && method !== "HEAD" ? body : undefined,
|
|
276
|
-
signal: controller.signal
|
|
277
|
-
};
|
|
278
|
-
fetch(url, fetchOptions).then(async (response) => {
|
|
279
|
-
if (timeout)
|
|
280
|
-
clearTimeout(timeout);
|
|
281
|
-
const text = await response.text();
|
|
282
|
-
this._notifyFetchComplete(fetchId, response.status, text, false);
|
|
283
|
-
}).catch((error) => {
|
|
284
|
-
if (timeout)
|
|
285
|
-
clearTimeout(timeout);
|
|
286
|
-
const isAbort = error.name === "AbortError";
|
|
287
|
-
const errorMsg = isAbort ? "Request timeout" : error.message ?? "Fetch failed";
|
|
288
|
-
this._notifyFetchComplete(fetchId, 0, errorMsg, true);
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
_notifyFetchComplete(fetchId, statusCode, body, isError) {
|
|
292
|
-
const handler = this.#fetchCompleteHandler;
|
|
293
|
-
const encoded = textEncoder.encode(body);
|
|
294
|
-
const ptr = this._alloc(encoded.length);
|
|
295
|
-
writeBytes(ptr, encoded);
|
|
296
|
-
handler(fetchId, statusCode, ptr, encoded.length, isError ? 1 : 0);
|
|
297
|
-
}
|
|
298
|
-
setTimeout(callbackId, delayMs) {
|
|
299
|
-
setTimeout(() => {
|
|
300
|
-
this.#invoke(CallbackType.Timeout, callbackId, null);
|
|
301
|
-
}, delayMs);
|
|
302
|
-
}
|
|
303
|
-
setInterval(callbackId, intervalMs) {
|
|
304
|
-
const handle = setInterval(() => {
|
|
305
|
-
this.#invoke(CallbackType.Interval, callbackId, null);
|
|
306
|
-
}, intervalMs);
|
|
307
|
-
this.#intervals.set(callbackId, handle);
|
|
308
|
-
}
|
|
309
|
-
clearInterval(callbackId) {
|
|
310
|
-
const handle = this.#intervals.get(callbackId);
|
|
311
|
-
if (handle !== undefined) {
|
|
312
|
-
clearInterval(handle);
|
|
313
|
-
this.#intervals.delete(callbackId);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
_writeStringToWasm(str) {
|
|
317
|
-
return this._writeBytesToWasm(textEncoder.encode(str));
|
|
318
|
-
}
|
|
319
|
-
_writeBytesToWasm(data) {
|
|
320
|
-
const ptr = this._alloc(data.length);
|
|
321
|
-
writeBytes(ptr, data);
|
|
322
|
-
return { ptr, len: data.length };
|
|
323
|
-
}
|
|
324
|
-
static log(level, ptr, len) {
|
|
325
|
-
const msg = textDecoder.decode(getMemoryView().subarray(ptr, ptr + len));
|
|
326
|
-
switch (level) {
|
|
327
|
-
case 0:
|
|
328
|
-
console.error(msg);
|
|
329
|
-
break;
|
|
330
|
-
case 1:
|
|
331
|
-
console.warn(msg);
|
|
332
|
-
break;
|
|
333
|
-
case 3:
|
|
334
|
-
console.debug(msg);
|
|
335
|
-
break;
|
|
336
|
-
default:
|
|
337
|
-
console.log(msg);
|
|
338
|
-
break;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
static createImportObject(bridgeRef) {
|
|
342
|
-
return {
|
|
343
|
-
...jsz.importObject(),
|
|
344
|
-
__zx: {
|
|
345
|
-
_log: (level, ptr, len) => ZxBridgeCore.log(level, ptr, len),
|
|
346
|
-
_fetchAsync: (urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) => {
|
|
347
|
-
bridgeRef.current?.fetchAsync(urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId);
|
|
348
|
-
},
|
|
349
|
-
_setTimeout: (callbackId, delayMs) => {
|
|
350
|
-
bridgeRef.current?.setTimeout(callbackId, delayMs);
|
|
351
|
-
},
|
|
352
|
-
_setInterval: (callbackId, intervalMs) => {
|
|
353
|
-
bridgeRef.current?.setInterval(callbackId, intervalMs);
|
|
354
|
-
},
|
|
355
|
-
_clearInterval: (callbackId) => {
|
|
356
|
-
bridgeRef.current?.clearInterval(callbackId);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
// src/wasm/index.ts
|
|
363
|
-
class ZxBridge extends ZxBridgeCore {
|
|
364
|
-
#websockets = new Map;
|
|
365
|
-
#wsOnOpenHandler;
|
|
366
|
-
#wsOnMessageHandler;
|
|
367
|
-
#wsOnErrorHandler;
|
|
368
|
-
#wsOnCloseHandler;
|
|
369
|
-
#eventbridge;
|
|
370
|
-
constructor(exports) {
|
|
371
|
-
super(exports);
|
|
372
|
-
this.#wsOnOpenHandler = exports.__zx_ws_onopen;
|
|
373
|
-
this.#wsOnMessageHandler = exports.__zx_ws_onmessage;
|
|
374
|
-
this.#wsOnErrorHandler = exports.__zx_ws_onerror;
|
|
375
|
-
this.#wsOnCloseHandler = exports.__zx_ws_onclose;
|
|
376
|
-
this.#eventbridge = exports.__zx_eventbridge;
|
|
377
|
-
}
|
|
378
|
-
submitFormActionAsync(form, statesJson, fetchId) {
|
|
379
|
-
const formData = new FormData(form);
|
|
380
|
-
formData.append("__$states", statesJson);
|
|
381
|
-
fetch(window.location.href, {
|
|
382
|
-
method: "POST",
|
|
383
|
-
headers: { "X-ZX-Action": "1" },
|
|
384
|
-
body: formData
|
|
385
|
-
}).then(async (response) => {
|
|
386
|
-
const text = await response.text();
|
|
387
|
-
this._notifyFetchComplete(fetchId, response.status, text, false);
|
|
388
|
-
}).catch((error) => {
|
|
389
|
-
const msg = error instanceof Error ? error.message : "Fetch failed";
|
|
390
|
-
this._notifyFetchComplete(fetchId, 0, msg, true);
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
wsConnect(wsId, urlPtr, urlLen, protocolsPtr, protocolsLen) {
|
|
394
|
-
const url = readString(urlPtr, urlLen);
|
|
395
|
-
const protocolsStr = protocolsLen > 0 ? readString(protocolsPtr, protocolsLen) : "";
|
|
396
|
-
const protocols = protocolsStr ? protocolsStr.split(",").map((p) => p.trim()).filter(Boolean) : undefined;
|
|
397
|
-
try {
|
|
398
|
-
const ws = protocols && protocols.length > 0 ? new WebSocket(url, protocols) : new WebSocket(url);
|
|
399
|
-
ws.binaryType = "arraybuffer";
|
|
400
|
-
ws.onopen = () => {
|
|
401
|
-
const handler = this.#wsOnOpenHandler;
|
|
402
|
-
if (!handler)
|
|
403
|
-
return;
|
|
404
|
-
const protocol = ws.protocol || "";
|
|
405
|
-
const { ptr, len } = this._writeStringToWasm(protocol);
|
|
406
|
-
handler(wsId, ptr, len);
|
|
407
|
-
};
|
|
408
|
-
ws.onmessage = (event) => {
|
|
409
|
-
const handler = this.#wsOnMessageHandler;
|
|
410
|
-
if (!handler)
|
|
411
|
-
return;
|
|
412
|
-
const isBinary = event.data instanceof ArrayBuffer;
|
|
413
|
-
const data = isBinary ? new Uint8Array(event.data) : textEncoder.encode(event.data);
|
|
414
|
-
const { ptr, len } = this._writeBytesToWasm(data);
|
|
415
|
-
handler(wsId, ptr, len, isBinary ? 1 : 0);
|
|
416
|
-
};
|
|
417
|
-
ws.onerror = (_event) => {
|
|
418
|
-
const handler = this.#wsOnErrorHandler;
|
|
419
|
-
if (!handler)
|
|
420
|
-
return;
|
|
421
|
-
const { ptr, len } = this._writeStringToWasm("WebSocket error");
|
|
422
|
-
handler(wsId, ptr, len);
|
|
423
|
-
};
|
|
424
|
-
ws.onclose = (event) => {
|
|
425
|
-
const handler = this.#wsOnCloseHandler;
|
|
426
|
-
if (!handler)
|
|
427
|
-
return;
|
|
428
|
-
const reason = event.reason || "";
|
|
429
|
-
const { ptr, len } = this._writeStringToWasm(reason);
|
|
430
|
-
handler(wsId, event.code, ptr, len, event.wasClean ? 1 : 0);
|
|
431
|
-
this.#websockets.delete(wsId);
|
|
432
|
-
};
|
|
433
|
-
this.#websockets.set(wsId, ws);
|
|
434
|
-
} catch (error) {
|
|
435
|
-
const handler = this.#wsOnErrorHandler;
|
|
436
|
-
if (handler) {
|
|
437
|
-
const msg = error instanceof Error ? error.message : "WebSocket connection failed";
|
|
438
|
-
const { ptr, len } = this._writeStringToWasm(msg);
|
|
439
|
-
handler(wsId, ptr, len);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
wsSend(wsId, dataPtr, dataLen, isBinary) {
|
|
444
|
-
const ws = this.#websockets.get(wsId);
|
|
445
|
-
if (!ws || ws.readyState !== WebSocket.OPEN)
|
|
446
|
-
return;
|
|
447
|
-
const memory = getMemoryView();
|
|
448
|
-
if (isBinary) {
|
|
449
|
-
ws.send(memory.slice(dataPtr, dataPtr + dataLen));
|
|
450
|
-
} else {
|
|
451
|
-
ws.send(textDecoder.decode(memory.subarray(dataPtr, dataPtr + dataLen)));
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
wsClose(wsId, code, reasonPtr, reasonLen) {
|
|
455
|
-
const ws = this.#websockets.get(wsId);
|
|
456
|
-
if (!ws)
|
|
457
|
-
return;
|
|
458
|
-
const reason = reasonLen > 0 ? readString(reasonPtr, reasonLen) : undefined;
|
|
459
|
-
try {
|
|
460
|
-
if (reason)
|
|
461
|
-
ws.close(code, reason);
|
|
462
|
-
else
|
|
463
|
-
ws.close(code);
|
|
464
|
-
} catch {
|
|
465
|
-
ws.close();
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
eventbridge(velementId, eventTypeId, event) {
|
|
469
|
-
if (!this.#eventbridge)
|
|
470
|
-
return;
|
|
471
|
-
const eventRef = storeValueGetRef(event);
|
|
472
|
-
this.#eventbridge(velementId, eventTypeId, eventRef);
|
|
473
|
-
}
|
|
474
|
-
static createImportObject(bridgeRef) {
|
|
475
|
-
return {
|
|
476
|
-
...jsz.importObject(),
|
|
477
|
-
__zx: {
|
|
478
|
-
_log: (level, ptr, len) => ZxBridgeCore.log(level, ptr, len),
|
|
479
|
-
_fetchAsync: (urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) => {
|
|
480
|
-
bridgeRef.current?.fetchAsync(urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId);
|
|
481
|
-
},
|
|
482
|
-
_setTimeout: (callbackId, delayMs) => {
|
|
483
|
-
bridgeRef.current?.setTimeout(callbackId, delayMs);
|
|
484
|
-
},
|
|
485
|
-
_setInterval: (callbackId, intervalMs) => {
|
|
486
|
-
bridgeRef.current?.setInterval(callbackId, intervalMs);
|
|
487
|
-
},
|
|
488
|
-
_clearInterval: (callbackId) => {
|
|
489
|
-
bridgeRef.current?.clearInterval(callbackId);
|
|
490
|
-
},
|
|
491
|
-
_wsConnect: (wsId, urlPtr, urlLen, protocolsPtr, protocolsLen) => {
|
|
492
|
-
bridgeRef.current?.wsConnect(wsId, urlPtr, urlLen, protocolsPtr, protocolsLen);
|
|
493
|
-
},
|
|
494
|
-
_wsSend: (wsId, dataPtr, dataLen, isBinary) => {
|
|
495
|
-
bridgeRef.current?.wsSend(wsId, dataPtr, dataLen, isBinary);
|
|
496
|
-
},
|
|
497
|
-
_wsClose: (wsId, code, reasonPtr, reasonLen) => {
|
|
498
|
-
bridgeRef.current?.wsClose(wsId, code, reasonPtr, reasonLen);
|
|
499
|
-
},
|
|
500
|
-
_ce: (id, vnodeId) => {
|
|
501
|
-
const tagName = TAG_NAMES[id];
|
|
502
|
-
const el = id >= SVG_TAG_START_INDEX ? document.createElementNS("http://www.w3.org/2000/svg", tagName) : document.createElement(tagName);
|
|
503
|
-
el.__zx_ref = Number(vnodeId);
|
|
504
|
-
domNodes.set(vnodeId, el);
|
|
505
|
-
return storeValueGetRef(el);
|
|
506
|
-
},
|
|
507
|
-
_ct: (ptr, len, vnodeId) => {
|
|
508
|
-
const text = readString(ptr, len);
|
|
509
|
-
const node = document.createTextNode(text);
|
|
510
|
-
node.__zx_ref = Number(vnodeId);
|
|
511
|
-
domNodes.set(vnodeId, node);
|
|
512
|
-
return storeValueGetRef(node);
|
|
513
|
-
},
|
|
514
|
-
_sa: (vnodeId, namePtr, nameLen, valPtr, valLen) => {
|
|
515
|
-
domNodes.get(vnodeId)?.setAttribute(readString(namePtr, nameLen), readString(valPtr, valLen));
|
|
516
|
-
},
|
|
517
|
-
_ra: (vnodeId, namePtr, nameLen) => {
|
|
518
|
-
domNodes.get(vnodeId)?.removeAttribute(readString(namePtr, nameLen));
|
|
519
|
-
},
|
|
520
|
-
_snv: (vnodeId, ptr, len) => {
|
|
521
|
-
const node = domNodes.get(vnodeId);
|
|
522
|
-
if (node)
|
|
523
|
-
node.nodeValue = readString(ptr, len);
|
|
524
|
-
},
|
|
525
|
-
_ac: (parentId, childId) => {
|
|
526
|
-
const parent = domNodes.get(parentId);
|
|
527
|
-
const child = domNodes.get(childId);
|
|
528
|
-
if (parent && child)
|
|
529
|
-
parent.appendChild(child);
|
|
530
|
-
},
|
|
531
|
-
_ib: (parentId, childId, refId) => {
|
|
532
|
-
const parent = domNodes.get(parentId);
|
|
533
|
-
const child = domNodes.get(childId);
|
|
534
|
-
const ref = domNodes.get(refId) ?? null;
|
|
535
|
-
if (parent && child)
|
|
536
|
-
parent.insertBefore(child, ref);
|
|
537
|
-
},
|
|
538
|
-
_rc: (parentId, childId) => {
|
|
539
|
-
const parent = domNodes.get(parentId);
|
|
540
|
-
const child = domNodes.get(childId);
|
|
541
|
-
if (parent && child) {
|
|
542
|
-
parent.removeChild(child);
|
|
543
|
-
cleanupDomNodes(child);
|
|
544
|
-
}
|
|
545
|
-
},
|
|
546
|
-
_rpc: (parentId, newId, oldId) => {
|
|
547
|
-
const parent = domNodes.get(parentId);
|
|
548
|
-
const newChild = domNodes.get(newId);
|
|
549
|
-
const oldChild = domNodes.get(oldId);
|
|
550
|
-
if (parent && newChild && oldChild) {
|
|
551
|
-
parent.replaceChild(newChild, oldChild);
|
|
552
|
-
cleanupDomNodes(oldChild);
|
|
553
|
-
}
|
|
554
|
-
},
|
|
555
|
-
_getLocationHref: (bufPtr, bufLen) => {
|
|
556
|
-
const bytes = textEncoder.encode(window.location.href);
|
|
557
|
-
const len = Math.min(bytes.length, bufLen);
|
|
558
|
-
writeBytes(bufPtr, bytes.subarray(0, len));
|
|
559
|
-
return len;
|
|
560
|
-
},
|
|
561
|
-
_getFormData: (vnodeId, bufPtr, bufLen) => {
|
|
562
|
-
const form = domNodes.get(vnodeId);
|
|
563
|
-
if (!form || !(form instanceof HTMLFormElement))
|
|
564
|
-
return 0;
|
|
565
|
-
const formData = new FormData(form);
|
|
566
|
-
const urlEncoded = new URLSearchParams(formData).toString();
|
|
567
|
-
const bytes = textEncoder.encode(urlEncoded);
|
|
568
|
-
const len = Math.min(bytes.length, bufLen);
|
|
569
|
-
writeBytes(bufPtr, bytes.subarray(0, len));
|
|
570
|
-
return len;
|
|
571
|
-
},
|
|
572
|
-
_submitFormAction: (vnodeId) => {
|
|
573
|
-
const form = domNodes.get(vnodeId);
|
|
574
|
-
if (!form || !(form instanceof HTMLFormElement))
|
|
575
|
-
return;
|
|
576
|
-
const formData = new FormData(form);
|
|
577
|
-
fetch(window.location.href, {
|
|
578
|
-
method: "POST",
|
|
579
|
-
headers: { "X-ZX-Action": "1" },
|
|
580
|
-
body: formData
|
|
581
|
-
}).catch(() => {});
|
|
582
|
-
},
|
|
583
|
-
_submitFormActionAsync: (vnodeId, statesPtr, statesLen, fetchId) => {
|
|
584
|
-
const form = domNodes.get(vnodeId);
|
|
585
|
-
if (!form || !(form instanceof HTMLFormElement))
|
|
586
|
-
return;
|
|
587
|
-
const statesJson = statesLen > 0 ? readString(statesPtr, statesLen) : "[]";
|
|
588
|
-
bridgeRef.current?.submitFormActionAsync(form, statesJson, fetchId);
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
};
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
var domNodes = new Map;
|
|
595
|
-
function cleanupDomNodes(node) {
|
|
596
|
-
const ref = node.__zx_ref;
|
|
597
|
-
if (ref !== undefined)
|
|
598
|
-
domNodes.delete(BigInt(ref));
|
|
599
|
-
const children = node.childNodes;
|
|
600
|
-
for (let i = 0;i < children.length; i++)
|
|
601
|
-
cleanupDomNodes(children[i]);
|
|
602
|
-
}
|
|
603
|
-
var SVG_TAG_START_INDEX = 140;
|
|
604
|
-
var TAG_NAMES = [
|
|
605
|
-
"aside",
|
|
606
|
-
"fragment",
|
|
607
|
-
"iframe",
|
|
608
|
-
"slot",
|
|
609
|
-
"img",
|
|
610
|
-
"html",
|
|
611
|
-
"base",
|
|
612
|
-
"head",
|
|
613
|
-
"link",
|
|
614
|
-
"meta",
|
|
615
|
-
"script",
|
|
616
|
-
"style",
|
|
617
|
-
"title",
|
|
618
|
-
"address",
|
|
619
|
-
"article",
|
|
620
|
-
"body",
|
|
621
|
-
"h1",
|
|
622
|
-
"h6",
|
|
623
|
-
"footer",
|
|
624
|
-
"header",
|
|
625
|
-
"h2",
|
|
626
|
-
"h3",
|
|
627
|
-
"h4",
|
|
628
|
-
"h5",
|
|
629
|
-
"hgroup",
|
|
630
|
-
"nav",
|
|
631
|
-
"section",
|
|
632
|
-
"dd",
|
|
633
|
-
"dl",
|
|
634
|
-
"dt",
|
|
635
|
-
"div",
|
|
636
|
-
"figcaption",
|
|
637
|
-
"figure",
|
|
638
|
-
"hr",
|
|
639
|
-
"li",
|
|
640
|
-
"ol",
|
|
641
|
-
"ul",
|
|
642
|
-
"menu",
|
|
643
|
-
"main",
|
|
644
|
-
"p",
|
|
645
|
-
"picture",
|
|
646
|
-
"pre",
|
|
647
|
-
"a",
|
|
648
|
-
"abbr",
|
|
649
|
-
"b",
|
|
650
|
-
"bdi",
|
|
651
|
-
"bdo",
|
|
652
|
-
"br",
|
|
653
|
-
"cite",
|
|
654
|
-
"code",
|
|
655
|
-
"data",
|
|
656
|
-
"time",
|
|
657
|
-
"dfn",
|
|
658
|
-
"em",
|
|
659
|
-
"i",
|
|
660
|
-
"kbd",
|
|
661
|
-
"mark",
|
|
662
|
-
"q",
|
|
663
|
-
"blockquote",
|
|
664
|
-
"rp",
|
|
665
|
-
"ruby",
|
|
666
|
-
"rt",
|
|
667
|
-
"rtc",
|
|
668
|
-
"rb",
|
|
669
|
-
"s",
|
|
670
|
-
"del",
|
|
671
|
-
"ins",
|
|
672
|
-
"samp",
|
|
673
|
-
"small",
|
|
674
|
-
"span",
|
|
675
|
-
"strong",
|
|
676
|
-
"sub",
|
|
677
|
-
"sup",
|
|
678
|
-
"u",
|
|
679
|
-
"var",
|
|
680
|
-
"wbr",
|
|
681
|
-
"area",
|
|
682
|
-
"map",
|
|
683
|
-
"audio",
|
|
684
|
-
"source",
|
|
685
|
-
"track",
|
|
686
|
-
"video",
|
|
687
|
-
"embed",
|
|
688
|
-
"object",
|
|
689
|
-
"param",
|
|
690
|
-
"canvas",
|
|
691
|
-
"noscript",
|
|
692
|
-
"caption",
|
|
693
|
-
"table",
|
|
694
|
-
"col",
|
|
695
|
-
"colgroup",
|
|
696
|
-
"tbody",
|
|
697
|
-
"tr",
|
|
698
|
-
"thead",
|
|
699
|
-
"tfoot",
|
|
700
|
-
"td",
|
|
701
|
-
"th",
|
|
702
|
-
"button",
|
|
703
|
-
"datalist",
|
|
704
|
-
"option",
|
|
705
|
-
"fieldset",
|
|
706
|
-
"label",
|
|
707
|
-
"form",
|
|
708
|
-
"input",
|
|
709
|
-
"keygen",
|
|
710
|
-
"legend",
|
|
711
|
-
"meter",
|
|
712
|
-
"optgroup",
|
|
713
|
-
"select",
|
|
714
|
-
"output",
|
|
715
|
-
"progress",
|
|
716
|
-
"textarea",
|
|
717
|
-
"details",
|
|
718
|
-
"dialog",
|
|
719
|
-
"menuitem",
|
|
720
|
-
"summary",
|
|
721
|
-
"content",
|
|
722
|
-
"element",
|
|
723
|
-
"shadow",
|
|
724
|
-
"template",
|
|
725
|
-
"acronym",
|
|
726
|
-
"applet",
|
|
727
|
-
"basefont",
|
|
728
|
-
"font",
|
|
729
|
-
"big",
|
|
730
|
-
"blink",
|
|
731
|
-
"center",
|
|
732
|
-
"command",
|
|
733
|
-
"dir",
|
|
734
|
-
"frame",
|
|
735
|
-
"frameset",
|
|
736
|
-
"isindex",
|
|
737
|
-
"listing",
|
|
738
|
-
"marquee",
|
|
739
|
-
"noembed",
|
|
740
|
-
"plaintext",
|
|
741
|
-
"spacer",
|
|
742
|
-
"strike",
|
|
743
|
-
"tt",
|
|
744
|
-
"xmp",
|
|
745
|
-
"animate",
|
|
746
|
-
"animateMotion",
|
|
747
|
-
"animateTransform",
|
|
748
|
-
"circle",
|
|
749
|
-
"clipPath",
|
|
750
|
-
"defs",
|
|
751
|
-
"desc",
|
|
752
|
-
"ellipse",
|
|
753
|
-
"feBlend",
|
|
754
|
-
"feColorMatrix",
|
|
755
|
-
"feComponentTransfer",
|
|
756
|
-
"feComposite",
|
|
757
|
-
"feConvolveMatrix",
|
|
758
|
-
"feDiffuseLighting",
|
|
759
|
-
"feDisplacementMap",
|
|
760
|
-
"feDistantLight",
|
|
761
|
-
"feDropShadow",
|
|
762
|
-
"feFlood",
|
|
763
|
-
"feFuncA",
|
|
764
|
-
"feFuncB",
|
|
765
|
-
"feFuncG",
|
|
766
|
-
"feFuncR",
|
|
767
|
-
"feGaussianBlur",
|
|
768
|
-
"feImage",
|
|
769
|
-
"feMerge",
|
|
770
|
-
"feMergeNode",
|
|
771
|
-
"feMorphology",
|
|
772
|
-
"feOffset",
|
|
773
|
-
"fePointLight",
|
|
774
|
-
"feSpecularLighting",
|
|
775
|
-
"feSpotLight",
|
|
776
|
-
"feTile",
|
|
777
|
-
"feTurbulence",
|
|
778
|
-
"filter",
|
|
779
|
-
"foreignObject",
|
|
780
|
-
"g",
|
|
781
|
-
"image",
|
|
782
|
-
"line",
|
|
783
|
-
"linearGradient",
|
|
784
|
-
"marker",
|
|
785
|
-
"mask",
|
|
786
|
-
"metadata",
|
|
787
|
-
"mpath",
|
|
788
|
-
"path",
|
|
789
|
-
"pattern",
|
|
790
|
-
"polygon",
|
|
791
|
-
"polyline",
|
|
792
|
-
"radialGradient",
|
|
793
|
-
"rect",
|
|
794
|
-
"set",
|
|
795
|
-
"stop",
|
|
796
|
-
"svg",
|
|
797
|
-
"switch",
|
|
798
|
-
"symbol",
|
|
799
|
-
"text",
|
|
800
|
-
"textPath",
|
|
801
|
-
"tspan",
|
|
802
|
-
"use",
|
|
803
|
-
"view"
|
|
804
|
-
];
|
|
805
|
-
var DELEGATED_EVENTS = [
|
|
806
|
-
"click",
|
|
807
|
-
"dblclick",
|
|
808
|
-
"input",
|
|
809
|
-
"change",
|
|
810
|
-
"submit",
|
|
811
|
-
"focus",
|
|
812
|
-
"blur",
|
|
813
|
-
"keydown",
|
|
814
|
-
"keyup",
|
|
815
|
-
"keypress",
|
|
816
|
-
"mouseenter",
|
|
817
|
-
"mouseleave",
|
|
818
|
-
"mousedown",
|
|
819
|
-
"mouseup",
|
|
820
|
-
"mousemove",
|
|
821
|
-
"touchstart",
|
|
822
|
-
"touchend",
|
|
823
|
-
"touchmove",
|
|
824
|
-
"scroll"
|
|
825
|
-
];
|
|
826
|
-
var EVENT_TYPE_MAP = {
|
|
827
|
-
click: 0,
|
|
828
|
-
dblclick: 1,
|
|
829
|
-
input: 2,
|
|
830
|
-
change: 3,
|
|
831
|
-
submit: 4,
|
|
832
|
-
focus: 5,
|
|
833
|
-
blur: 6,
|
|
834
|
-
keydown: 7,
|
|
835
|
-
keyup: 8,
|
|
836
|
-
keypress: 9,
|
|
837
|
-
mouseenter: 10,
|
|
838
|
-
mouseleave: 11,
|
|
839
|
-
mousedown: 12,
|
|
840
|
-
mouseup: 13,
|
|
841
|
-
mousemove: 14,
|
|
842
|
-
touchstart: 15,
|
|
843
|
-
touchend: 16,
|
|
844
|
-
touchmove: 17,
|
|
845
|
-
scroll: 18
|
|
846
|
-
};
|
|
847
|
-
function initEventDelegation(bridge, rootSelector = "body") {
|
|
848
|
-
const root = document.querySelector(rootSelector);
|
|
849
|
-
if (!root)
|
|
850
|
-
return;
|
|
851
|
-
for (const eventType of DELEGATED_EVENTS) {
|
|
852
|
-
root.addEventListener(eventType, (event) => {
|
|
853
|
-
let target = event.target;
|
|
854
|
-
while (target && target !== document.body) {
|
|
855
|
-
const zxRef = target.__zx_ref;
|
|
856
|
-
if (zxRef !== undefined) {
|
|
857
|
-
bridge.eventbridge(BigInt(zxRef), EVENT_TYPE_MAP[eventType] ?? 0, event);
|
|
858
|
-
if (event.cancelBubble)
|
|
859
|
-
break;
|
|
860
|
-
}
|
|
861
|
-
target = target.parentElement;
|
|
862
|
-
}
|
|
863
|
-
}, { passive: eventType.startsWith("touch") || eventType === "scroll" });
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
var DEFAULT_URL = "/assets/_/main.wasm";
|
|
867
|
-
async function init(options = {}) {
|
|
868
|
-
const url = options.url ?? document.getElementById("__$wasmlink")?.href ?? DEFAULT_URL;
|
|
869
|
-
const bridgeRef = { current: null };
|
|
870
|
-
const importObject = Object.assign({}, ZxBridge.createImportObject(bridgeRef), options.importObject);
|
|
871
|
-
const source = await WebAssembly.instantiateStreaming(fetch(url), importObject);
|
|
872
|
-
const { instance } = source;
|
|
873
|
-
jsz.memory = instance.exports.memory;
|
|
874
|
-
const bridge = new ZxBridge(instance.exports);
|
|
875
|
-
bridgeRef.current = bridge;
|
|
876
|
-
initEventDelegation(bridge, options.eventDelegationRoot ?? "body");
|
|
877
|
-
const main = instance.exports.mainClient;
|
|
878
|
-
if (typeof main === "function")
|
|
879
|
-
main();
|
|
880
|
-
return { source, bridge };
|
|
881
|
-
}
|
|
882
|
-
export {
|
|
883
|
-
writeBytes,
|
|
884
|
-
textEncoder,
|
|
885
|
-
textDecoder,
|
|
886
|
-
storeValueGetRef,
|
|
887
|
-
readString,
|
|
888
|
-
jsz,
|
|
889
|
-
initEventDelegation,
|
|
890
|
-
init,
|
|
891
|
-
getMemoryView,
|
|
892
|
-
ZxBridgeCore,
|
|
893
|
-
ZxBridge,
|
|
894
|
-
CallbackType
|
|
895
|
-
};
|