ziex 0.1.0-dev.1259 → 0.1.0-dev.1360

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 (35) hide show
  1. package/README.md +20 -24
  2. package/cloudflare/index.js +101 -15
  3. package/index.js +78 -4
  4. package/package.json +2 -2
  5. package/pkg/ziex/src/fetch.d.ts +5 -0
  6. package/{wasm → pkg/ziex/src/wasm}/core.d.ts +2 -2
  7. package/{wasm → pkg/ziex/src/wasm}/index.d.ts +2 -1
  8. package/vendor/jsz/js/src/index.d.ts +1 -0
  9. package/vendor/jsz/js/src/zigjs.d.ts +107 -0
  10. package/wasm/index.js +92 -11
  11. package/wasm/init.dev.js +1310 -0
  12. package/wasm/init.js +3 -1229
  13. /package/{app.d.ts → pkg/ziex/src/app.d.ts} +0 -0
  14. /package/{aws-lambda → pkg/ziex/src/aws-lambda}/index.d.ts +0 -0
  15. /package/{browser → pkg/ziex/src/browser}/kv.d.ts +0 -0
  16. /package/{cloudflare → pkg/ziex/src/cloudflare}/app.d.ts +0 -0
  17. /package/{cloudflare → pkg/ziex/src/cloudflare}/db.d.ts +0 -0
  18. /package/{cloudflare → pkg/ziex/src/cloudflare}/do.d.ts +0 -0
  19. /package/{cloudflare → pkg/ziex/src/cloudflare}/index.d.ts +0 -0
  20. /package/{cloudflare → pkg/ziex/src/cloudflare}/kv.d.ts +0 -0
  21. /package/{cloudflare → pkg/ziex/src/cloudflare}/worker.d.ts +0 -0
  22. /package/{db.d.ts → pkg/ziex/src/db.d.ts} +0 -0
  23. /package/{index.d.ts → pkg/ziex/src/index.d.ts} +0 -0
  24. /package/{kv.d.ts → pkg/ziex/src/kv.d.ts} +0 -0
  25. /package/{react → pkg/ziex/src/react}/dom.d.ts +0 -0
  26. /package/{react → pkg/ziex/src/react}/index.d.ts +0 -0
  27. /package/{react → pkg/ziex/src/react}/types.d.ts +0 -0
  28. /package/{runtime.d.ts → pkg/ziex/src/runtime.d.ts} +0 -0
  29. /package/{vercel → pkg/ziex/src/vercel}/index.d.ts +0 -0
  30. /package/{wasi.d.ts → pkg/ziex/src/wasi.d.ts} +0 -0
  31. /package/{wasm → pkg/ziex/src/wasm}/dom.d.ts +0 -0
  32. /package/{wasm → pkg/ziex/src/wasm}/init.d.ts +0 -0
  33. /package/{wasm → pkg/ziex/src/wasm}/types.d.ts +0 -0
  34. /package/{wasm → pkg/ziex/src/wasm}/wasi.d.ts +0 -0
  35. /package/{zx.d.ts → pkg/ziex/src/zx.d.ts} +0 -0
package/wasm/index.js CHANGED
@@ -193,6 +193,76 @@ class ZigJS {
193
193
  return decoder.decode(data);
194
194
  }
195
195
  }
196
+ // src/fetch.ts
197
+ function createFetchImports(getMemory) {
198
+ const encoder2 = new TextEncoder;
199
+ const decoder2 = new TextDecoder;
200
+ function readStr(ptr, len) {
201
+ return decoder2.decode(new Uint8Array(getMemory().buffer, ptr, len));
202
+ }
203
+ function writeBytes(buf_ptr, buf_max, data) {
204
+ if (data.length > buf_max)
205
+ return -2;
206
+ new Uint8Array(getMemory().buffer, buf_ptr, data.length).set(data);
207
+ return data.length;
208
+ }
209
+ function readBytes(ptr, len) {
210
+ return new Uint8Array(getMemory().buffer, ptr, len);
211
+ }
212
+ function parseHeaders(headersJson) {
213
+ let headers = {};
214
+ try {
215
+ headers = JSON.parse(headersJson);
216
+ } catch {
217
+ for (const line of headersJson.split(`
218
+ `)) {
219
+ const colonIdx = line.indexOf(":");
220
+ if (colonIdx > 0) {
221
+ headers[line.slice(0, colonIdx)] = line.slice(colonIdx + 1);
222
+ }
223
+ }
224
+ }
225
+ return headers;
226
+ }
227
+ async function doFetch(url_ptr, url_len, method_ptr, method_len, headers_ptr, headers_len, body_ptr, body_len, timeout_ms, status_out, buf_ptr, buf_max) {
228
+ const url = readStr(url_ptr, url_len);
229
+ const method = method_len > 0 ? readStr(method_ptr, method_len) : "GET";
230
+ const headersJson = headers_len > 0 ? readStr(headers_ptr, headers_len) : "{}";
231
+ const body = body_len > 0 ? readBytes(body_ptr, body_len) : undefined;
232
+ const headers = parseHeaders(headersJson);
233
+ const controller = new AbortController;
234
+ const timeout = timeout_ms > 0 ? setTimeout(() => controller.abort(), timeout_ms) : null;
235
+ try {
236
+ const response = await fetch(url, {
237
+ method,
238
+ headers: Object.keys(headers).length > 0 ? headers : undefined,
239
+ body: method !== "GET" && method !== "HEAD" ? body : undefined,
240
+ signal: controller.signal
241
+ });
242
+ if (timeout)
243
+ clearTimeout(timeout);
244
+ new DataView(getMemory().buffer).setUint16(status_out, response.status, true);
245
+ const bytes = new Uint8Array(await response.arrayBuffer());
246
+ return writeBytes(buf_ptr, buf_max, bytes);
247
+ } catch (err) {
248
+ if (timeout)
249
+ clearTimeout(timeout);
250
+ const msg = err instanceof Error ? err.message : String(err);
251
+ console.error(`wasm fetch failed: ${url} (${msg})`);
252
+ return -1;
253
+ }
254
+ }
255
+ const Suspending = WebAssembly.Suspending;
256
+ if (typeof Suspending !== "function") {
257
+ return {
258
+ fetch: (_url_ptr, _url_len, _method_ptr, _method_len, _headers_ptr, _headers_len, _body_ptr, _body_len, _timeout_ms, _status_out, _buf_ptr, _buf_max) => -1
259
+ };
260
+ }
261
+ return {
262
+ fetch: new Suspending(doFetch)
263
+ };
264
+ }
265
+
196
266
  // src/wasm/core.ts
197
267
  var CallbackType = {
198
268
  Event: 0,
@@ -291,7 +361,7 @@ class ZxBridgeCore {
291
361
  const url = readString(urlPtr, urlLen);
292
362
  const method = methodLen > 0 ? readString(methodPtr, methodLen) : "GET";
293
363
  const headersJson = headersLen > 0 ? readString(headersPtr, headersLen) : "{}";
294
- const body = bodyLen > 0 ? readString(bodyPtr, bodyLen) : undefined;
364
+ const body = bodyLen > 0 ? getMemoryView().subarray(bodyPtr, bodyPtr + bodyLen) : undefined;
295
365
  let headers = {};
296
366
  try {
297
367
  headers = JSON.parse(headersJson);
@@ -315,8 +385,8 @@ class ZxBridgeCore {
315
385
  fetch(url, fetchOptions).then(async (response) => {
316
386
  if (timeout)
317
387
  clearTimeout(timeout);
318
- const text = await response.text();
319
- this._notifyFetchComplete(fetchId, response.status, text, false);
388
+ const bytes = new Uint8Array(await response.arrayBuffer());
389
+ this._notifyFetchComplete(fetchId, response.status, bytes, false);
320
390
  }).catch((error) => {
321
391
  if (timeout)
322
392
  clearTimeout(timeout);
@@ -327,7 +397,7 @@ class ZxBridgeCore {
327
397
  }
328
398
  _notifyFetchComplete(fetchId, statusCode, body, isError) {
329
399
  const handler = this.#fetchCompleteHandler;
330
- const encoded = textEncoder.encode(body);
400
+ const encoded = typeof body === "string" ? textEncoder.encode(body) : body;
331
401
  const ptr = this._alloc(encoded.length);
332
402
  writeBytes(ptr, encoded);
333
403
  invokeWasmExport(handler, fetchId, statusCode, ptr, encoded.length, isError ? 1 : 0);
@@ -384,6 +454,11 @@ class ZxBridgeCore {
384
454
  static createImportObject(bridgeRef) {
385
455
  return {
386
456
  ...jsz.importObject(),
457
+ __zx_net: createFetchImports(() => {
458
+ if (!jsz.memory)
459
+ throw new Error("WASM memory is not ready");
460
+ return jsz.memory;
461
+ }),
387
462
  __zx: {
388
463
  _log: (level, ptr, len) => ZxBridgeCore.log(level, ptr, len),
389
464
  _fetchAsync: (urlPtr, urlLen, methodPtr, methodLen, headersPtr, headersLen, bodyPtr, bodyLen, timeoutMs, fetchId) => {
@@ -682,12 +757,12 @@ class ZxBridge extends ZxBridgeCore {
682
757
  clearEventHandlerModes(velementId) {
683
758
  eventHandlerModes.delete(velementId);
684
759
  }
685
- submitFormActionAsync(form, statesJson, fetchId) {
760
+ submitFormActionAsync(form, actionId, statesJson, fetchId) {
686
761
  const formData = new FormData(form);
687
762
  formData.append("__$states", statesJson);
688
763
  fetch(window.location.href, {
689
764
  method: "POST",
690
- headers: { "X-ZX-Action": "1" },
765
+ headers: { "x-action": String(actionId >>> 0) },
691
766
  body: formData
692
767
  }).then(async (response) => {
693
768
  const text = await response.text();
@@ -793,6 +868,11 @@ class ZxBridge extends ZxBridgeCore {
793
868
  static createImportObject(bridgeRef) {
794
869
  return {
795
870
  ...jsz.importObject(),
871
+ __zx_net: createFetchImports(() => {
872
+ if (!jsz.memory)
873
+ throw new Error("WASM memory is not ready");
874
+ return jsz.memory;
875
+ }),
796
876
  __zx: {
797
877
  _log: (level, ptr, len) => ZxBridgeCore.log(level, ptr, len),
798
878
  _setEventHandlerMode: (vnodeId, eventTypeId, maySuspend) => {
@@ -911,23 +991,23 @@ class ZxBridge extends ZxBridgeCore {
911
991
  writeBytes(bufPtr, bytes.subarray(0, len));
912
992
  return len;
913
993
  },
914
- _submitFormAction: (vnodeId) => {
994
+ _submitFormAction: (vnodeId, actionId) => {
915
995
  const form = domNodes.get(vnodeId);
916
996
  if (!form || !(form instanceof HTMLFormElement))
917
997
  return;
918
998
  const formData = new FormData(form);
919
999
  fetch(window.location.href, {
920
1000
  method: "POST",
921
- headers: { "X-ZX-Action": "1" },
1001
+ headers: { "x-action": String(actionId >>> 0) },
922
1002
  body: formData
923
1003
  }).catch(() => {});
924
1004
  },
925
- _submitFormActionAsync: (vnodeId, statesPtr, statesLen, fetchId) => {
1005
+ _submitFormActionAsync: (vnodeId, actionId, statesPtr, statesLen, fetchId) => {
926
1006
  const form = domNodes.get(vnodeId);
927
1007
  if (!form || !(form instanceof HTMLFormElement))
928
1008
  return;
929
1009
  const statesJson = statesLen > 0 ? readString(statesPtr, statesLen) : "[]";
930
- bridgeRef.current?.submitFormActionAsync(form, statesJson, fetchId);
1010
+ bridgeRef.current?.submitFormActionAsync(form, actionId, statesJson, fetchId);
931
1011
  }
932
1012
  }
933
1013
  };
@@ -1259,7 +1339,8 @@ async function init(options = {}) {
1259
1339
  domNodes.clear();
1260
1340
  }
1261
1341
  };
1262
- registerDevReinit(normalizedOptions);
1342
+ if (true)
1343
+ registerDevReinit(normalizedOptions);
1263
1344
  return { source, bridge };
1264
1345
  }
1265
1346
  export {