@ignex/nova 0.1.3 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/README.md +4 -1
  2. package/docs/ai/TREE.md +69 -9
  3. package/docs/architecture.md +75 -27
  4. package/docs/events.md +83 -1
  5. package/docs/generic-bindings.md +10 -0
  6. package/docs/wire-format.md +65 -18
  7. package/package.json +2 -1
  8. package/prebuilds/linux-x64/libignex_ffi.so +0 -0
  9. package/public/generate.ts +97 -3
  10. package/public/server.ts +10 -0
  11. package/rust/src/generated/backend.rs +503 -0
  12. package/rust/src/transcode/generated.rs +376 -17
  13. package/src/bridge/nats/inbound.ts +46 -0
  14. package/src/bridge/nats/index.ts +131 -0
  15. package/src/bridge/nats/real-transport.ts +133 -0
  16. package/src/bridge/nats/types.ts +80 -0
  17. package/src/codegen/constants.ts +14 -4
  18. package/src/codegen/direct-gen.ts +20 -6
  19. package/src/codegen/registry-gen.ts +10 -6
  20. package/src/codegen/rust-glue-gen.ts +10 -3
  21. package/src/codegen/schema-model.ts +28 -3
  22. package/src/codegen/ts-ser-gen.ts +12 -3
  23. package/src/core/auth.ts +65 -4
  24. package/src/core/client-rpc.ts +75 -0
  25. package/src/core/client-state.ts +53 -0
  26. package/src/core/client-wire.ts +183 -8
  27. package/src/core/client.ts +84 -4
  28. package/src/core/groups.ts +5 -0
  29. package/src/core/metrics.ts +38 -21
  30. package/src/core/outbound.ts +50 -6
  31. package/src/core/rate-limit.ts +69 -0
  32. package/src/core/replay.ts +41 -1
  33. package/src/core/resume.ts +181 -0
  34. package/src/core/rooms.ts +10 -3
  35. package/src/core/routing.ts +128 -5
  36. package/src/core/server/client-info.ts +37 -0
  37. package/src/core/server/http-routes.ts +59 -0
  38. package/src/core/{server.ts → server/index.ts} +112 -120
  39. package/src/core/server/metrics-view.ts +53 -0
  40. package/src/core/server/socket-lifecycle.ts +57 -0
  41. package/src/core/state.ts +73 -1
  42. package/src/core/topic-log.ts +86 -0
  43. package/src/events/clients.ts +18 -0
  44. package/src/events/cluster/dedupe.ts +43 -0
  45. package/src/events/cluster/envelope.ts +149 -0
  46. package/src/events/cluster/index.ts +50 -0
  47. package/src/events/cluster/keys.ts +33 -0
  48. package/src/events/cluster/kinds.ts +32 -0
  49. package/src/events/cluster/presence-table.ts +99 -0
  50. package/src/events/cluster/presence.ts +53 -0
  51. package/src/events/cluster/redis-client.ts +50 -0
  52. package/src/events/cluster/store-memory.ts +67 -0
  53. package/src/events/cluster/store-redis.ts +44 -0
  54. package/src/events/cluster/subjects.ts +30 -0
  55. package/src/events/cluster/sync.ts +476 -0
  56. package/src/events/cluster/transport-nats.ts +24 -0
  57. package/src/events/cluster/transport-redis.ts +120 -0
  58. package/src/events/cluster-rpc.ts +196 -0
  59. package/src/events/delivery.ts +83 -0
  60. package/src/events/emit.ts +57 -11
  61. package/src/events/hub/context-factory.ts +79 -0
  62. package/src/events/hub/dispatch.ts +86 -0
  63. package/src/events/hub/index.ts +536 -0
  64. package/src/events/hub/internal.ts +31 -0
  65. package/src/events/hub/metrics-snapshot.ts +84 -0
  66. package/src/events/hub/resolve-cluster.ts +49 -0
  67. package/src/events/queue.ts +36 -9
  68. package/src/events/registry.ts +90 -54
  69. package/src/events/schedule.ts +73 -0
  70. package/src/events/trace.ts +283 -0
  71. package/src/events/types/client.ts +68 -0
  72. package/src/events/types/cluster.ts +40 -0
  73. package/src/events/types/context.ts +50 -0
  74. package/src/events/types/emit-target.ts +29 -0
  75. package/src/events/types/groups.ts +35 -0
  76. package/src/events/types/hub.ts +124 -0
  77. package/src/events/types/index.ts +30 -0
  78. package/src/events/types/metrics.ts +52 -0
  79. package/src/events/types/options.ts +62 -0
  80. package/src/generated/direct-ser.ts +146 -59
  81. package/src/generated/fbs/backend.fbs +23 -0
  82. package/src/generated/registry.ts +92 -33
  83. package/src/generated/rust/backend_generated.rs +503 -0
  84. package/src/generated/ts/backend.ts +4 -0
  85. package/src/generated/ts/resume.ts +74 -0
  86. package/src/generated/ts/resumed.ts +88 -0
  87. package/src/generated/ts/rpc-call.ts +112 -0
  88. package/src/generated/ts/rpc-result.ts +126 -0
  89. package/src/generated/ts/snapshot-request.ts +19 -5
  90. package/src/generated/ts-ser.ts +109 -16
  91. package/src/generated/wire-registry.json +7 -3
  92. package/src/schema/index.ts +45 -1
  93. package/src/transport/transport.ts +117 -77
  94. package/src/bridge/nats.ts +0 -309
  95. package/src/events/cluster.ts +0 -732
  96. package/src/events/hub.ts +0 -481
  97. package/src/events/types.ts +0 -378
  98. package/src/transport/stats.ts +0 -48
package/src/core/auth.ts CHANGED
@@ -7,9 +7,64 @@
7
7
  * `authenticate` may return a `ClientMeta` (`{id, groups, meta}`) to pin the
8
8
  * client's identity for targeted sends / grouping; otherwise a UUID is
9
9
  * auto-assigned. A duplicate explicit id rejects the new connection (409).
10
+ *
11
+ * Literal bearer tokens are compared in CONSTANT TIME (no length or prefix
12
+ * oracle for a brute-forcing caller). The same gate protects the HTTP admin
13
+ * surface (`GET /clients`) via `authorizeHttp` — introspection endpoints must
14
+ * never be wider than the WebSocket they introspect.
10
15
  */
16
+ import { timingSafeEqual } from "node:crypto";
11
17
  import type { ClientMeta, ServerState, WsData } from "./state";
12
18
 
19
+ /** Constant-time string equality (UTF-8 compared; length-safe). */
20
+ export function safeEqual(a: string, b: string): boolean {
21
+ const ab = Buffer.from(a, "utf8");
22
+ const bb = Buffer.from(b, "utf8");
23
+ if (ab.byteLength !== bb.byteLength) {
24
+ // burn comparable time so mismatched-length guesses aren't cheaper
25
+ timingSafeEqual(ab, ab);
26
+ return false;
27
+ }
28
+ return timingSafeEqual(ab, bb);
29
+ }
30
+
31
+ /** Evaluate the server's token gate against a raw Bearer value. */
32
+ export function tokenOk(state: ServerState, bearer: string): boolean {
33
+ if (state.token === undefined) return true;
34
+ return typeof state.token === "function" ? state.token(bearer) : safeEqual(bearer, state.token);
35
+ }
36
+
37
+ function bearerOf(req: Request): string {
38
+ const auth = req.headers.get("authorization") ?? "";
39
+ return auth.startsWith("Bearer ") ? auth.slice("Bearer ".length) : "";
40
+ }
41
+
42
+ /**
43
+ * HTTP admin gate (defense-in-depth for non-WebSocket routes): when a `token`
44
+ * is configured the request MUST carry a valid Bearer; otherwise, when an
45
+ * `authenticate` hook exists it must accept the request. Unauthenticated
46
+ * servers stay unauthenticated (documented dev behavior).
47
+ */
48
+ export async function authorizeHttp(
49
+ state: ServerState,
50
+ req: Request,
51
+ ): Promise<Response | undefined> {
52
+ if (state.token !== undefined || state.authenticate !== undefined) {
53
+ if (!tokenOk(state, bearerOf(req))) return new Response("unauthorized", { status: 401 });
54
+ if (state.token === undefined && state.authenticate !== undefined) {
55
+ // a failing auth backend must deny, not blow up the serve loop
56
+ let allowed: unknown;
57
+ try {
58
+ allowed = await state.authenticate(req);
59
+ } catch {
60
+ return new Response("unauthorized", { status: 401 });
61
+ }
62
+ if (!allowed) return new Response("unauthorized", { status: 401 });
63
+ }
64
+ }
65
+ return undefined;
66
+ }
67
+
13
68
  export async function checkUpgrade(
14
69
  state: ServerState,
15
70
  req: Request,
@@ -25,14 +80,19 @@ export async function checkUpgrade(
25
80
  }
26
81
  }
27
82
  if (state.token) {
28
- const auth = req.headers.get("authorization") ?? "";
29
- const bearer = auth.startsWith("Bearer ") ? auth.slice("Bearer ".length) : "";
30
- const ok = typeof state.token === "function" ? state.token(bearer) : bearer === state.token;
83
+ const ok = tokenOk(state, bearerOf(req));
31
84
  if (!ok) return new Response("unauthorized", { status: 401 });
32
85
  }
33
86
  let authMeta: ClientMeta | undefined;
34
87
  if (state.authenticate) {
35
- const res = await state.authenticate(req);
88
+ // a throwing / rejecting hook denies the upgrade cleanly (401) instead of
89
+ // surfacing an unhandled error through Bun.serve's fetch loop
90
+ let res: Awaited<ReturnType<typeof state.authenticate>>;
91
+ try {
92
+ res = await state.authenticate(req);
93
+ } catch {
94
+ return new Response("unauthorized", { status: 401 });
95
+ }
36
96
  if (!res) return new Response("unauthorized", { status: 401 });
37
97
  if (typeof res === "object") authMeta = res;
38
98
  }
@@ -44,6 +104,7 @@ export async function checkUpgrade(
44
104
  }
45
105
  const data: WsData = {
46
106
  lastSeq: 0,
107
+ sendSeq: 1,
47
108
  topics: new Set(),
48
109
  groups: new Set(authMeta?.groups ?? []),
49
110
  id,
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Client RPC — the request/response half of `client.request(name, payload)`.
3
+ *
4
+ * Extracted from the client composition root so the wire-level plumbing
5
+ * (base64 framing, pending-call registry, timeout bookkeeping) lives in one
6
+ * small module. The reply side (`rpcResult` handling) is in `client-wire.ts`.
7
+ */
8
+ import type { Bindings } from "../bindings/types";
9
+ import { sendControl } from "./client-wire";
10
+ import type { ClientState } from "./client-state";
11
+
12
+ /** Encode bytes as base64 without pulling in a Buffer dependency (browser-safe). */
13
+ export function bytesToB64(bytes: Uint8Array): string {
14
+ let s = "";
15
+ for (let i = 0; i < bytes.byteLength; i++) s += String.fromCharCode(bytes[i]!);
16
+ return btoa(s);
17
+ }
18
+
19
+ /** Reject every pending rpc call (client close / socket loss). */
20
+ export function failAllPending(
21
+ state: ClientState,
22
+ err: Error,
23
+ ): void {
24
+ for (const [id, call] of state.rpcPending) {
25
+ clearTimeout(call.timer);
26
+ call.reject(err);
27
+ state.rpcPending.delete(id);
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Run a request/response round-trip: encode `payload` with the event's own
33
+ * schema, send it inside an `rpcCall` control frame, and settle with
34
+ * `{ payload }` from the responder. Rejects on timeout, transport loss, or a
35
+ * server-side error — never leaves a pending entry behind.
36
+ */
37
+ export function createRpcRequest<B extends Bindings>(
38
+ state: ClientState,
39
+ name: string,
40
+ payload: unknown,
41
+ opts: { readonly timeoutMs?: number } | undefined,
42
+ bindings: B,
43
+ ): Promise<unknown> {
44
+ const ws = state.ws;
45
+ if (!ws || ws.readyState !== WebSocket.OPEN)
46
+ return Promise.reject(new Error("ignex: client is not connected"));
47
+
48
+ const id = crypto.randomUUID();
49
+ const inner = bindings.encodeFrame(name, payload);
50
+ const b64 = bytesToB64(inner);
51
+ const timeoutMs = opts?.timeoutMs ?? state.requestTimeoutMs;
52
+
53
+ return new Promise((resolve, reject) => {
54
+ const timer = setTimeout(() => {
55
+ state.rpcPending.delete(id);
56
+ reject(new Error(`ignex rpc "${name}" timed out after ${timeoutMs}ms`));
57
+ }, timeoutMs);
58
+
59
+ // registered BEFORE the send so a fast reply can never race the insert
60
+ state.rpcPending.set(id, {
61
+ name,
62
+ // the wire resolves with the bare payload — wrap it into RpcResult here
63
+ resolve: (out) => resolve({ payload: out }),
64
+ reject,
65
+ timer,
66
+ });
67
+ try {
68
+ sendControl(state, "rpcCall", { id, name, payloadB64: b64 });
69
+ } catch (err) {
70
+ clearTimeout(timer);
71
+ state.rpcPending.delete(id);
72
+ reject(err instanceof Error ? err : new Error(String(err)));
73
+ }
74
+ });
75
+ }
@@ -32,6 +32,24 @@ export interface IgnClientOptions<B extends Bindings = DefaultBindings> {
32
32
  heartbeatMs?: number;
33
33
  /** miss this many heartbeats before assuming the connection is dead, default 2 */
34
34
  heartbeatMisses?: number;
35
+ /**
36
+ * Buffer app-event `send()` calls made before the socket is open and flush
37
+ * them automatically on connect (default true). This lets new users write
38
+ * `client.send(...)` right after `createClient(...)` — even before
39
+ * `connect()` — without waiting for the `connected` status. Set `false` to
40
+ * keep the strict behaviour of throwing `"ignex: client is not connected"`.
41
+ */
42
+ queueSends?: boolean;
43
+ /**
44
+ * Gap-free delivery (requires the server started with `resume`): track the
45
+ * server's per-connection delivery seqs, detect gaps, and automatically
46
+ * request re-delivery via the `resume` control frame. Buffered out-of-order
47
+ * frames are bounded by `maxPending`. Default: true (no-op against servers
48
+ * that don't stamp seqs).
49
+ */
50
+ resume?: boolean | { maxPending?: number; timeoutMs?: number };
51
+ /** request/response default timeout (ms), default 10_000 */
52
+ requestTimeoutMs?: number;
35
53
  }
36
54
 
37
55
  export interface ClientState {
@@ -55,6 +73,26 @@ export interface ClientState {
55
73
  clientId: string;
56
74
  /** server-side groups this client belongs to (from `welcome`; [] until known) */
57
75
  groups: string[];
76
+
77
+ // ── delivery-seq tracking / gap recovery (envelope v2) ──────────────────
78
+ /** resolved resume options (enabled: false when off) */
79
+ resume: { enabled: boolean; maxPending: number; timeoutMs: number };
80
+ /** last CONTIGUOUS delivery seq processed (0 = none yet) */
81
+ rxSeq: number;
82
+ /** frames held out-of-order while a gap is being filled (seq → frame bytes) */
83
+ pending: Map<number, Uint8Array[]>;
84
+ /** seq the pending buffer is waiting to fill from */
85
+ pendingFrom: number;
86
+ /** in-flight resume request flag (throttles re-asks) */
87
+ resumeInFlight: boolean;
88
+ /** app frames queued while the socket was not open (flushed on open) */
89
+ pendingSends: Uint8Array[];
90
+ /** force-flush timer for an unfillable gap */
91
+ gapTimer: ReturnType<typeof setTimeout> | null;
92
+ /** request/response: correlation id → pending call */
93
+ rpcPending: Map<string, { resolve: (payload: unknown) => void; reject: (err: Error) => void; timer: ReturnType<typeof setTimeout>; name: string }>;
94
+ /** default request timeout (ms) */
95
+ requestTimeoutMs: number;
58
96
  }
59
97
 
60
98
  export function createClientState<B extends Bindings = DefaultBindings>(
@@ -79,6 +117,21 @@ export function createClientState<B extends Bindings = DefaultBindings>(
79
117
  lastPong: 0,
80
118
  clientId: "",
81
119
  groups: [],
120
+ resume: {
121
+ enabled: opts.resume !== false,
122
+ maxPending:
123
+ (typeof opts.resume === "object" ? opts.resume.maxPending : undefined) ?? 1024,
124
+ timeoutMs:
125
+ (typeof opts.resume === "object" ? opts.resume.timeoutMs : undefined) ?? 5_000,
126
+ },
127
+ rxSeq: 0,
128
+ pending: new Map(),
129
+ pendingFrom: 0,
130
+ resumeInFlight: false,
131
+ pendingSends: [],
132
+ gapTimer: null,
133
+ rpcPending: new Map(),
134
+ requestTimeoutMs: opts.requestTimeoutMs ?? 10_000,
82
135
  };
83
136
  }
84
137
 
@@ -3,6 +3,12 @@
3
3
  * `handleMessage` decodes the envelope, filters control frames, and fans app
4
4
  * events out to the registered handlers. All decode/encode goes through
5
5
  * `state.bindings`, so a client speaks whatever wire stack it was given.
6
+ *
7
+ * With envelope v2 (delivery seqs) and `resume` enabled, app frames pass
8
+ * through a small ordering gate: contiguous seqs deliver immediately; a GAP
9
+ * buffers out-of-order frames and asks the server to re-send the hole
10
+ * (`resume` control frame); replayed hole-fills keep their original seqs so
11
+ * delivery stays in-order and duplicate-free.
6
12
  */
7
13
  import type { ControlEventName, ControlEvents } from "../schema";
8
14
  import type { ClientState } from "./client-state";
@@ -23,10 +29,148 @@ export function sendControl<K extends ControlEventName>(
23
29
  sendFrame(state, state.bindings.encodeFrame(name, payload));
24
30
  }
25
31
 
32
+ /**
33
+ * Send an encoded APP frame, queueing it until the socket is open.
34
+ *
35
+ * New-user friendly: `client.send(...)` immediately after `createClient(...)`
36
+ * (even before `connect()`) buffers the frame and flushes it once the socket
37
+ * opens — callers don't have to wait for the `connected` status. When
38
+ * `opts.queueSends` is `false` this throws `"ignex: client is not connected"`
39
+ * (the previous strict behaviour); a closed client always throws.
40
+ */
41
+ export function sendFrameQueued(state: ClientState, frame: Uint8Array): void {
42
+ const ws = state.ws;
43
+ if (ws !== null && ws.readyState === WebSocket.OPEN) {
44
+ ws.send(frame as Uint8Array<ArrayBuffer>);
45
+ return;
46
+ }
47
+ if (state.closed) throw new Error("ignex: client is closed");
48
+ if (state.opts.queueSends === false) throw new Error("ignex: client is not connected");
49
+ state.pendingSends.push(frame);
50
+ }
51
+
52
+ /**
53
+ * Send every queued app frame in order. Called once the socket is open (after
54
+ * the `hello` + reconnect-subscribe control frames, which must precede app
55
+ * traffic). If the socket drops mid-flush the remainder stays queued.
56
+ */
57
+ export function flushPendingSends(state: ClientState): void {
58
+ if (state.pendingSends.length === 0) return;
59
+ const ws = state.ws;
60
+ if (ws === null || ws.readyState !== WebSocket.OPEN) return;
61
+ const q = state.pendingSends;
62
+ state.pendingSends = [];
63
+ for (let i = 0; i < q.length; i++) {
64
+ const w = state.ws;
65
+ if (w !== ws || w.readyState !== WebSocket.OPEN) {
66
+ state.pendingSends = q.slice(i); // connection changed — put the rest back
67
+ return;
68
+ }
69
+ w.send(q[i]! as Uint8Array<ArrayBuffer>);
70
+ }
71
+ }
72
+
26
73
  export function emitError(state: ClientState, err: Error): void {
27
74
  for (const cb of state.errorCbs) cb(err);
28
75
  }
29
76
 
77
+ /** Delivery header offsets derived from the bindings' header length. */
78
+ const flagsAt = (state: ClientState): number => state.bindings.wireHeaderLen - 9;
79
+ const seqAt = (state: ClientState): number => state.bindings.wireHeaderLen - 8;
80
+
81
+ function readDeliverySeq(state: ClientState, bytes: Uint8Array): number | null {
82
+ const len = state.bindings.wireHeaderLen;
83
+ if (len < 14 || bytes.byteLength < len) return null;
84
+ if ((bytes[flagsAt(state)]! & 1) === 0) return null;
85
+ return Number(new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getBigUint64(seqAt(state), true));
86
+ }
87
+
88
+ /** Ask the server to re-send everything after `state.rxSeq` (throttled). */
89
+ export function requestResume(state: ClientState): void {
90
+ if (!state.resume.enabled || state.resumeInFlight) return;
91
+ try {
92
+ sendControl(state, "resume", { lastSeq: state.rxSeq });
93
+ state.resumeInFlight = true;
94
+ } catch {
95
+ // not connected — reconnect flow will carry lastSeq in `hello`
96
+ }
97
+ }
98
+
99
+ /** Force-drain the pending buffer after an unfillable gap (accept loss). */
100
+ export function flushPending(state: ClientState): void {
101
+ if (state.pending.size === 0) return;
102
+ const seqs = [...state.pending.keys()].sort((a, b) => a - b);
103
+ for (const s of seqs) {
104
+ for (const f of state.pending.get(s)!) dispatchAppFrame(state, f);
105
+ state.pending.delete(s);
106
+ }
107
+ if (seqs.length > 0 && seqs[seqs.length - 1]! > state.rxSeq) {
108
+ state.rxSeq = seqs[seqs.length - 1]!;
109
+ }
110
+ state.pendingFrom = 0;
111
+ }
112
+
113
+ /** Dispatch one decoded APP frame to handlers (no ordering). */
114
+ function dispatchAppFrame(state: ClientState, bytes: Uint8Array): void {
115
+ const frame = state.bindings.decodeFrame(bytes);
116
+ if (!frame) return;
117
+ const set = state.handlers.get(frame.name);
118
+ if (set) for (const cb of set) cb(frame.payload);
119
+ for (const cb of state.anyHandlers) cb(frame.name, frame.payload);
120
+ }
121
+
122
+ /**
123
+ * Ordered delivery of one raw app frame. Returns true when the frame was
124
+ * consumed here (contiguous / buffered / replayed); false = not tracking.
125
+ */
126
+ function orderedDeliver(state: ClientState, bytes: Uint8Array, seq: number): boolean {
127
+ if (!state.resume.enabled) return false;
128
+ if (seq === state.rxSeq + 1) {
129
+ state.rxSeq = seq;
130
+ dispatchAppFrame(state, bytes);
131
+ // drain anything the gap-fill delivered contiguously behind us
132
+ while (state.pending.has(state.rxSeq + 1)) {
133
+ const next = state.rxSeq + 1;
134
+ for (const f of state.pending.get(next)!) dispatchAppFrame(state, f);
135
+ state.pending.delete(next);
136
+ state.rxSeq = next;
137
+ }
138
+ if (state.pending.size === 0) state.pendingFrom = 0;
139
+ return true;
140
+ }
141
+ if (seq <= state.rxSeq) {
142
+ // Duplicate of an already-dispatched frame. Every seq ≤ rxSeq has been
143
+ // delivered (contiguously or via the pending drain), so a late replay of
144
+ // it must NOT be dispatched again. This happens when a `resume`/`hello`
145
+ // replay range overlaps frames the client already received live — e.g.
146
+ // the server processed our `hello { lastSeq }` AFTER the frames were sent
147
+ // (reconnect race), or a gap-fill replay re-sends frames that were already
148
+ // drained out of `pending`. Redelivering here would break the duplicate-
149
+ // free contract, so we consume and drop.
150
+ return true;
151
+ }
152
+ // seq > rxSeq + 1 → GAP: buffer, then ask the server to fill it
153
+ let list = state.pending.get(seq);
154
+ if (!list) {
155
+ if (state.pending.size >= state.resume.maxPending) {
156
+ flushPending(state); // accept loss rather than grow unbounded
157
+ }
158
+ list = [];
159
+ state.pending.set(seq, list);
160
+ }
161
+ list.push(bytes);
162
+ if (state.pendingFrom === 0) state.pendingFrom = state.rxSeq;
163
+ requestResume(state);
164
+ if (state.gapTimer === null) {
165
+ state.gapTimer = setTimeout(() => {
166
+ state.gapTimer = null;
167
+ // server could not fill the hole (resume-miss / offline) — accept loss
168
+ flushPending(state);
169
+ }, state.resume.timeoutMs);
170
+ }
171
+ return true;
172
+ }
173
+
30
174
  export function handleControl(state: ClientState, name: ControlEventName, payload: unknown): void {
31
175
  switch (name) {
32
176
  case "hello": {
@@ -44,6 +188,37 @@ export function handleControl(state: ClientState, name: ControlEventName, payloa
44
188
  state.groups = [...p.groups];
45
189
  break;
46
190
  }
191
+ case "resumed": {
192
+ state.resumeInFlight = false;
193
+ const p = payload as ControlEvents["resumed"];
194
+ if (!p.ok && p.from === 0) {
195
+ // server has nothing after our seq (fresh process / grave evicted) —
196
+ // accept the loss and continue from the buffered frames
197
+ flushPending(state);
198
+ }
199
+ // ok=true: replayed frames follow with their original seqs and slot
200
+ // into the ordering gate automatically.
201
+ break;
202
+ }
203
+ case "rpcResult": {
204
+ const p = payload as ControlEvents["rpcResult"];
205
+ const call = state.rpcPending.get(p.id);
206
+ if (!call) break;
207
+ state.rpcPending.delete(p.id);
208
+ clearTimeout(call.timer);
209
+ if (!p.ok) {
210
+ call.reject(new Error(`ignex rpc "${call.name}" failed: ${p.err}`));
211
+ break;
212
+ }
213
+ try {
214
+ const bytes = Uint8Array.from(atob(p.payloadB64), (c) => c.charCodeAt(0));
215
+ const frame = state.bindings.decodeFrame(bytes);
216
+ call.resolve(frame?.payload);
217
+ } catch (err) {
218
+ call.reject(err instanceof Error ? err : new Error("ignex rpc: bad result"));
219
+ }
220
+ break;
221
+ }
47
222
  case "pong":
48
223
  state.lastPong = Date.now();
49
224
  break;
@@ -55,17 +230,17 @@ export function handleControl(state: ClientState, name: ControlEventName, payloa
55
230
  export function handleMessage(state: ClientState, data: ArrayBuffer | string): void {
56
231
  if (typeof data === "string") return; // ignore text frames
57
232
  const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : (data as Uint8Array);
58
- const frame = state.bindings.decodeFrame(bytes);
59
- if (!frame) {
233
+ // fast envelope check before any decode
234
+ const header = state.bindings.readFrameHeader(bytes);
235
+ if (!header) {
60
236
  emitError(state, new Error("ignex: undecodable / version-mismatched frame dropped"));
61
237
  return;
62
238
  }
63
- if (state.bindings.isControlId(frame.id)) {
64
- handleControl(state, frame.name as ControlEventName, frame.payload);
239
+ if (state.bindings.isControlId(header.id)) {
240
+ handleControl(state, header.name as ControlEventName, state.bindings.decodePayload(header.id, bytes));
65
241
  return;
66
242
  }
67
- const name = frame.name;
68
- const set = state.handlers.get(name);
69
- if (set) for (const cb of set) cb(frame.payload);
70
- for (const cb of state.anyHandlers) cb(name, frame.payload);
243
+ const seq = readDeliverySeq(state, bytes);
244
+ if (seq !== null && orderedDeliver(state, bytes, seq)) return;
245
+ dispatchAppFrame(state, bytes);
71
246
  }
@@ -12,9 +12,22 @@
12
12
  */
13
13
  import type { Bindings, DefaultBindings, EventNameOf, EventsOf } from "../bindings/types";
14
14
  import { createClientState, setStatus, type ClientState, type ClientStatus, type IgnClientOptions } from "./client-state";
15
- import { handleMessage, sendControl, sendFrame } from "./client-wire";
15
+ import {
16
+ handleMessage,
17
+ emitError,
18
+ sendControl,
19
+ sendFrameQueued,
20
+ flushPendingSends,
21
+ flushPending,
22
+ } from "./client-wire";
16
23
  import { startHeartbeat, stopHeartbeat } from "./client-heartbeat";
17
24
  import { scheduleReconnect } from "./client-reconnect";
25
+ import { failAllPending, createRpcRequest } from "./client-rpc";
26
+
27
+ /** Result of a request/response round-trip. */
28
+ export interface RpcResult<P> {
29
+ payload: P;
30
+ }
18
31
 
19
32
  /** The public client API (returned by `createClient`). */
20
33
  export interface IgnClient<B extends Bindings = DefaultBindings> {
@@ -40,9 +53,21 @@ export interface IgnClient<B extends Bindings = DefaultBindings> {
40
53
  close(): void;
41
54
  /** Send a typed app event to the server (server must allow it via `inbound`). */
42
55
  send<K extends EventNameOf<B>>(name: K, payload: EventsOf<B>[K]): void;
56
+ /**
57
+ * Request/response: send `name` and await the responder's payload (encoded
58
+ * with the SAME event schema). Rejects on timeout (`timeoutMs`, default
59
+ * `requestTimeoutMs`) or when no responder is registered server-side.
60
+ */
61
+ request<K extends EventNameOf<B>>(
62
+ name: K,
63
+ payload: EventsOf<B>[K],
64
+ opts?: { readonly timeoutMs?: number },
65
+ ): Promise<RpcResult<EventsOf<B>[K]>>;
43
66
  /** Ask the server to subscribe this socket to a topic (room membership + replay). */
44
67
  subscribe(topic: string): void;
45
68
  unsubscribe(topic: string): void;
69
+ /** Ask the server to re-send recorded topic history strictly after `fromSeq`. */
70
+ snapshotRequest(topic: string, fromSeq?: number): void;
46
71
  /** Ask the server to add this socket to a server-side group. */
47
72
  joinGroup(group: string): void;
48
73
  leaveGroup(group: string): void;
@@ -59,22 +84,57 @@ export function createClient<B extends Bindings = DefaultBindings>(
59
84
  const state: ClientState = createClientState(url, opts);
60
85
 
61
86
  function connect(): IgnClient<B> {
87
+ // idempotent: an already-open / opening socket is never leaked or replaced
88
+ if (state.ws !== null) {
89
+ const ready = state.ws.readyState;
90
+ if (ready === WebSocket.OPEN || ready === WebSocket.CONNECTING) return api;
91
+ }
62
92
  state.closed = false;
63
93
  setStatus(state, state.attempts === 0 ? "connecting" : "reconnecting");
64
94
  const ws = new WebSocket(url);
65
95
  ws.binaryType = "arraybuffer";
96
+ // ownership guard: once a NEWER socket occupies `state.ws`, callbacks from
97
+ // this (stale) socket must not touch shared state or reschedule reconnects
98
+ const ownsState = (): boolean => state.ws === null || state.ws === ws;
66
99
  ws.onopen = () => {
100
+ if (!ownsState()) return;
67
101
  state.attempts = 0;
68
102
  setStatus(state, "connected");
69
- sendControl(state, "hello", { version: state.bindings.wireVersion, caps: [], lastSeq: 0 });
103
+ state.resumeInFlight = false;
104
+ if (state.gapTimer !== null) {
105
+ clearTimeout(state.gapTimer);
106
+ state.gapTimer = null;
107
+ }
108
+ // carry the last contiguous delivery seq so a resume-capable server can
109
+ // re-send what this session missed (cross-session resume)
110
+ sendControl(state, "hello", {
111
+ version: state.bindings.wireVersion,
112
+ caps: [],
113
+ lastSeq: state.resume.enabled ? state.rxSeq : 0,
114
+ });
70
115
  // re-subscribe topics from before the disconnect (server cleared them)
71
116
  for (const t of state.subscribedTopics) sendControl(state, "subscribe", { topic: t });
117
+ // deliver app events that were sent before the socket was open (send()
118
+ // queues by default — no need to wait for the connected status)
119
+ flushPendingSends(state);
72
120
  startHeartbeat(state);
73
121
  };
74
- ws.onmessage = (ev) => handleMessage(state, ev.data as ArrayBuffer | string);
122
+ ws.onmessage = (ev) => {
123
+ if (!ownsState()) return;
124
+ handleMessage(state, ev.data as ArrayBuffer | string);
125
+ };
126
+ ws.onerror = () => {
127
+ // surface refused upgrades / transport failures (onclose follows and
128
+ // drives the reconnect state machine — no double scheduling here)
129
+ if (!ownsState()) return;
130
+ emitError(state, new Error("ignex: connection error"));
131
+ };
75
132
  ws.onclose = () => {
133
+ if (!ownsState()) return; // replaced by a newer socket — its lifecycle wins
76
134
  stopHeartbeat(state);
77
135
  state.ws = null;
136
+ state.resumeInFlight = false;
137
+ flushPending(state); // accept in-flight gap loss; hello carries rxSeq on reconnect
78
138
  if (state.closed) {
79
139
  setStatus(state, "closed");
80
140
  return;
@@ -148,12 +208,29 @@ export function createClient<B extends Bindings = DefaultBindings>(
148
208
  stopHeartbeat(state);
149
209
  if (state.reconnectTimer) clearTimeout(state.reconnectTimer);
150
210
  state.reconnectTimer = null;
211
+ if (state.gapTimer !== null) {
212
+ clearTimeout(state.gapTimer);
213
+ state.gapTimer = null;
214
+ }
215
+ state.pending.clear();
216
+ state.pendingFrom = 0;
217
+ state.pendingSends = []; // never delivered — drop the queued app frames
218
+ failAllPending(state, new Error("ignex: client closed"));
151
219
  state.ws?.close();
152
220
  state.ws = null;
153
221
  setStatus(state, "closed");
154
222
  },
155
223
  send(name, payload) {
156
- sendFrame(state, state.bindings.encodeFrame(name, payload));
224
+ sendFrameQueued(state, state.bindings.encodeFrame(name, payload));
225
+ },
226
+ request(name, payload, opts) {
227
+ return createRpcRequest(
228
+ state,
229
+ name,
230
+ payload,
231
+ opts,
232
+ state.bindings,
233
+ ) as Promise<RpcResult<EventsOf<B>[typeof name]>>;
157
234
  },
158
235
  subscribe(topic) {
159
236
  state.subscribedTopics.add(topic);
@@ -163,6 +240,9 @@ export function createClient<B extends Bindings = DefaultBindings>(
163
240
  state.subscribedTopics.delete(topic);
164
241
  sendControl(state, "unsubscribe", { topic });
165
242
  },
243
+ snapshotRequest(topic, fromSeq = 0) {
244
+ sendControl(state, "snapshotRequest", { topic, fromSeq });
245
+ },
166
246
  joinGroup(group) {
167
247
  sendControl(state, "joinGroup", { group });
168
248
  },
@@ -14,6 +14,11 @@ import type { ServerState, WsData } from "./state";
14
14
 
15
15
  /** Add `ws` to `group` (idempotent) and index it in `state.groups`. */
16
16
  export function joinGroup(state: ServerState, ws: ServerWebSocket<WsData>, group: string): void {
17
+ // every join path (control frames, programmatic, auth-seeded) is gated
18
+ if (state.authorizeGroup !== undefined && !state.authorizeGroup(group, ws)) {
19
+ state.metrics.rejectedJoins++;
20
+ return;
21
+ }
17
22
  ws.data.groups.add(group);
18
23
  let set = state.groups.get(group);
19
24
  if (!set) {