ziex 0.1.0-dev.786 → 0.1.0-dev.787

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.
@@ -13,7 +13,15 @@ export interface KVNamespace {
13
13
  }[];
14
14
  }>;
15
15
  }
16
+ /**
17
+ * In-memory KV namespace. Used as the default shim on platforms that don't
18
+ * provide a real KV binding (e.g. Vercel). Data lives only for the lifetime
19
+ * of the isolate instance.
20
+ */
21
+ export declare function createMemoryKV(): KVNamespace;
16
22
  /**
17
23
  * Create a `__zx_kv` import object for use with `worker.run({ kv: ... })`.
24
+ * Always returns a valid import object. When JSPI is unavailable all KV
25
+ * operations are stubbed (get → not-found, put/delete → success, list → []).
18
26
  */
19
27
  export declare function createKVImports(bindings: Record<string, KVNamespace>, getMemory: () => WebAssembly.Memory): Record<string, unknown>;
@@ -13,15 +13,98 @@
13
13
  * });
14
14
  * ```
15
15
  */
16
- export declare function run({ request, env, ctx, module, kv: kvBindings, imports, }: {
16
+ /** Minimal Durable Object namespace shape needed for WebSocket routing. */
17
+ type DurableObjectNamespace = {
18
+ idFromName(name: string): unknown;
19
+ get(id: unknown): {
20
+ fetch(req: Request): Promise<Response>;
21
+ };
22
+ };
23
+ export declare function run({ request, env, ctx, module, kv: kvBindings, imports, wasi, websocket: doNamespace, }: {
17
24
  request: Request;
18
25
  env?: unknown;
19
- ctx?: unknown;
26
+ ctx?: {
27
+ waitUntil(promise: Promise<unknown>): void;
28
+ };
20
29
  module: WebAssembly.Module;
21
30
  /** KV namespace bindings — `{ default: env.KV, otherName: env.OTHER_KV }` */
22
31
  kv?: Record<string, import("./kv").KVNamespace>;
23
32
  imports?: (mem: () => WebAssembly.Memory) => Record<string, Record<string, unknown>>;
33
+ wasi?: WASI;
34
+ /**
35
+ * Durable Object namespace to use for WebSocket connections.
36
+ * When provided, WebSocket upgrade requests are automatically forwarded to
37
+ * the DO so that pub/sub works across multiple connected clients.
38
+ *
39
+ * The DO class must be created with `createWebSocketDO` and exported from
40
+ * your worker:
41
+ *
42
+ * ```ts
43
+ * export const MyDO = createWebSocketDO(module);
44
+ * export default {
45
+ * fetch: (req, env, ctx) => run({ request: req, ctx, module, websocket: env.MyDO }),
46
+ * };
47
+ * ```
48
+ */
49
+ websocket?: DurableObjectNamespace;
24
50
  }): Promise<Response>;
51
+ type WsState = {
52
+ upgraded: boolean;
53
+ server: WebSocket | null;
54
+ pendingWrites: Uint8Array[];
55
+ messageQueue: Uint8Array[];
56
+ recvResolve: ((bytes: Uint8Array | null) => void) | null;
57
+ /** Resolved when ws_recv is called for the first time (WASM has entered the message loop). */
58
+ _resolveFirstSuspend?: () => void;
59
+ subscribe?: (topic: string) => void;
60
+ unsubscribe?: (topic: string) => void;
61
+ publish?: (topic: string, data: Uint8Array) => number;
62
+ isSubscribed?: (topic: string) => boolean;
63
+ };
64
+ type ConnState = WsState & {
65
+ topics: Set<string>;
66
+ };
67
+ /**
68
+ * Create a Durable Object class that handles WebSocket connections for a ZX app.
69
+ *
70
+ * Each DO instance corresponds to one "room" (keyed by pathname). All clients
71
+ * connecting to the same route share a DO instance, enabling pub/sub via
72
+ * `ctx.socket.subscribe()` / `ctx.socket.publish()`.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * // worker.ts
77
+ * import { run, createWebSocketDO } from "ziex/cloudflare";
78
+ * import module from "./app.wasm";
79
+ *
80
+ * export const ZxWS = createWebSocketDO(module);
81
+ *
82
+ * export default {
83
+ * fetch: (req: Request, env: any, ctx: any) =>
84
+ * run({ request: req, ctx, module, websocket: env.ZxWS }),
85
+ * };
86
+ * ```
87
+ */
88
+ export declare function createWebSocketDO(module: WebAssembly.Module, options?: {
89
+ /**
90
+ * KV namespace bindings for the DO. Pass a factory that receives the DO's
91
+ * `env` so bindings are resolved at runtime:
92
+ *
93
+ * ```ts
94
+ * createWebSocketDO(module, { kv: (env) => ({ default: env.KV }) })
95
+ * ```
96
+ */
97
+ kv?: (env: any) => Record<string, import("./kv").KVNamespace>;
98
+ imports?: (mem: () => WebAssembly.Memory) => Record<string, Record<string, unknown>>;
99
+ }): {
100
+ new (state: any, env: any): {
101
+ readonly doState: any;
102
+ readonly env: any;
103
+ /** All active connections in this room, keyed by their server-side WebSocket. */
104
+ readonly connections: Map<WebSocket, ConnState>;
105
+ fetch(request: Request): Promise<Response>;
106
+ };
107
+ };
25
108
  export declare function prepare({ request, env, ctx, }: {
26
109
  request: Request;
27
110
  env: unknown;
@@ -37,3 +120,29 @@ export declare function respond({ exec, stdout, stderr, }: {
37
120
  stdout: TransformStream;
38
121
  stderr: TransformStream | ReadableStream<Uint8Array>;
39
122
  }): Promise<Response>;
123
+ type WASI = {
124
+ args?: Array<string>;
125
+ env?: Array<string>;
126
+ fds?: Array<unknown>;
127
+ inst?: {
128
+ exports: {
129
+ memory: WebAssembly.Memory;
130
+ };
131
+ };
132
+ wasiImport: Record<string, Function> | {
133
+ [key: string]: (...args: Array<any>) => unknown;
134
+ };
135
+ start(instance: {
136
+ exports: {
137
+ memory: WebAssembly.Memory;
138
+ _start: () => unknown;
139
+ };
140
+ }): number;
141
+ initialize?(instance: {
142
+ exports: {
143
+ memory: WebAssembly.Memory;
144
+ _initialize?: () => unknown;
145
+ };
146
+ }): void;
147
+ };
148
+ export {};
@@ -0,0 +1,92 @@
1
+ import { Hono } from "hono";
2
+ import type { KVNamespace } from "../cloudflare/kv";
3
+ type DurableObjectNamespace = {
4
+ idFromName(name: string): unknown;
5
+ get(id: unknown): {
6
+ fetch(req: Request): Promise<Response>;
7
+ };
8
+ };
9
+ type WASI = {
10
+ wasiImport: Record<string, (...args: any[]) => unknown>;
11
+ start(instance: {
12
+ exports: {
13
+ memory: WebAssembly.Memory;
14
+ _start: () => unknown;
15
+ };
16
+ }): number;
17
+ initialize?(instance: {
18
+ exports: {
19
+ memory: WebAssembly.Memory;
20
+ _initialize?: () => unknown;
21
+ };
22
+ }): void;
23
+ };
24
+ /**
25
+ * Create a Hono app that runs a Ziex WASM module.
26
+ *
27
+ * The returned app handles all routes (`*`) and forwards each request to
28
+ * the WASM runtime. Deploy it on any platform Hono supports:
29
+ *
30
+ * @example Cloudflare Workers
31
+ * ```ts
32
+ * import { createHonoApp } from "ziex/hono";
33
+ * import module from "./app.wasm";
34
+ *
35
+ * const app = createHonoApp<{ KV: KVNamespace }>({
36
+ * module,
37
+ * kv: (env) => ({ default: env.KV }),
38
+ * });
39
+ *
40
+ * export default app;
41
+ * ```
42
+ *
43
+ * @example Node / Vercel (requires JSPI support)
44
+ * ```ts
45
+ * import { createHonoApp } from "ziex/hono";
46
+ * import { serve } from "@hono/node-server";
47
+ * import module from "./app.wasm";
48
+ *
49
+ * const app = createHonoApp({ module });
50
+ * serve(app);
51
+ * ```
52
+ *
53
+ * **Note:** The WASM async model relies on JSPI (`WebAssembly.Suspending` /
54
+ * `WebAssembly.promising`). Verify that your target runtime supports it.
55
+ */
56
+ export declare function createHonoApp<Bindings extends Record<string, unknown> = Record<string, unknown>>(options: {
57
+ /** Compiled WASM module for the Ziex app. */
58
+ module: WebAssembly.Module;
59
+ /**
60
+ * Optional pre-configured WASI instance. When omitted the built-in WASI
61
+ * shim (from `ziex/cloudflare`) is used automatically.
62
+ */
63
+ wasi?: WASI;
64
+ /**
65
+ * Extra WASM import objects, keyed by module namespace.
66
+ * Receives a `mem` accessor so you can read WASM memory lazily.
67
+ */
68
+ imports?: (mem: () => WebAssembly.Memory) => Record<string, Record<string, unknown>>;
69
+ /**
70
+ * Factory that maps platform env bindings to Ziex KV namespaces.
71
+ * Called once per request so bindings are resolved at runtime.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * kv: (env) => ({ default: env.MY_KV })
76
+ * ```
77
+ */
78
+ kv?: (env: Bindings) => Record<string, KVNamespace>;
79
+ /**
80
+ * Factory that maps platform env bindings to a Durable Object namespace
81
+ * for WebSocket pub/sub support.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * websocket: (env) => env.ZxWS
86
+ * ```
87
+ */
88
+ websocket?: (env: Bindings) => DurableObjectNamespace;
89
+ }): Hono<{
90
+ Bindings: Bindings;
91
+ }>;
92
+ export {};