@mandujs/core 0.13.1 → 0.13.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mandujs/core",
3
- "version": "0.13.1",
3
+ "version": "0.13.2",
4
4
  "description": "Mandu Framework Core - Spec, Generator, Guard, Runtime",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -10,6 +10,8 @@ export { ManduFilling, ManduFillingFactory, LoaderTimeoutError } from "./filling
10
10
  export type { Handler, Guard, HttpMethod, Loader, LoaderOptions } from "./filling";
11
11
  export { SSEConnection, createSSEConnection } from "./sse";
12
12
  export type { SSEOptions, SSESendOptions, SSECleanup } from "./sse";
13
+ export { resolveResumeCursor, catchupFromCursor, mergeUniqueById } from "./sse-catchup";
14
+ export type { SSECursor, CatchupResult, CatchupOptions } from "./sse-catchup";
13
15
 
14
16
  // Auth Guards
15
17
  export {
@@ -0,0 +1,56 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { catchupFromCursor, mergeUniqueById, resolveResumeCursor } from "./sse-catchup";
3
+
4
+ describe("sse catch-up primitives", () => {
5
+ const snapshot = [
6
+ { id: "m1", text: "first" },
7
+ { id: "m2", text: "second" },
8
+ { id: "m3", text: "third" },
9
+ ];
10
+
11
+ it("resolves Last-Event-ID cursor", () => {
12
+ const req = new Request("http://localhost/stream", {
13
+ headers: { "Last-Event-ID": "m2" },
14
+ });
15
+
16
+ expect(resolveResumeCursor(req)).toBe("m2");
17
+ });
18
+
19
+ it("returns delta list when cursor exists", () => {
20
+ const result = catchupFromCursor({ cursorId: "m1", snapshot });
21
+
22
+ expect(result.mode).toBe("delta");
23
+ expect(result.items.map((m) => m.id)).toEqual(["m2", "m3"]);
24
+ });
25
+
26
+ it("falls back to snapshot when cursor is missing", () => {
27
+ const result = catchupFromCursor({ snapshot });
28
+
29
+ expect(result.mode).toBe("snapshot");
30
+ expect(result.reason).toBe("missing-cursor");
31
+ expect(result.items.map((m) => m.id)).toEqual(["m1", "m2", "m3"]);
32
+ });
33
+
34
+ it("falls back to snapshot when cursor is unknown", () => {
35
+ const result = catchupFromCursor({ cursorId: "missing", snapshot });
36
+
37
+ expect(result.mode).toBe("snapshot");
38
+ expect(result.reason).toBe("unknown-cursor");
39
+ expect(result.items.map((m) => m.id)).toEqual(["m1", "m2", "m3"]);
40
+ });
41
+
42
+ it("merges incoming messages idempotently", () => {
43
+ const base = [
44
+ { id: "m1", text: "first" },
45
+ { id: "m2", text: "second" },
46
+ ];
47
+ const incoming = [
48
+ { id: "m2", text: "second-dup" },
49
+ { id: "m3", text: "third" },
50
+ ];
51
+
52
+ const merged = mergeUniqueById(base, incoming);
53
+
54
+ expect(merged.map((m) => m.id)).toEqual(["m1", "m2", "m3"]);
55
+ });
56
+ });
@@ -0,0 +1,67 @@
1
+ export interface SSECursor {
2
+ id: string;
3
+ }
4
+
5
+ export interface CatchupResult<T> {
6
+ mode: "delta" | "snapshot";
7
+ items: T[];
8
+ reason?: "missing-cursor" | "unknown-cursor";
9
+ }
10
+
11
+ export interface CatchupOptions<T extends SSECursor> {
12
+ cursorId?: string | null;
13
+ snapshot: readonly T[];
14
+ }
15
+
16
+ /**
17
+ * Resolve resume cursor from Last-Event-ID header.
18
+ */
19
+ export function resolveResumeCursor(request: Request): string | undefined {
20
+ const raw = request.headers.get("last-event-id")?.trim();
21
+ return raw ? raw : undefined;
22
+ }
23
+
24
+ /**
25
+ * Return reconnect catch-up list if cursor exists, otherwise full snapshot fallback.
26
+ */
27
+ export function catchupFromCursor<T extends SSECursor>(options: CatchupOptions<T>): CatchupResult<T> {
28
+ const { cursorId, snapshot } = options;
29
+
30
+ if (!cursorId) {
31
+ return {
32
+ mode: "snapshot",
33
+ reason: "missing-cursor",
34
+ items: [...snapshot],
35
+ };
36
+ }
37
+
38
+ const index = snapshot.findIndex((item) => item.id === cursorId);
39
+ if (index < 0) {
40
+ return {
41
+ mode: "snapshot",
42
+ reason: "unknown-cursor",
43
+ items: [...snapshot],
44
+ };
45
+ }
46
+
47
+ return {
48
+ mode: "delta",
49
+ items: snapshot.slice(index + 1),
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Idempotent merge utility for reconnect + snapshot mix.
55
+ */
56
+ export function mergeUniqueById<T extends SSECursor>(base: readonly T[], incoming: readonly T[]): T[] {
57
+ const seen = new Set(base.map((item) => item.id));
58
+ const merged = [...base];
59
+
60
+ for (const item of incoming) {
61
+ if (seen.has(item.id)) continue;
62
+ seen.add(item.id);
63
+ merged.push(item);
64
+ }
65
+
66
+ return merged;
67
+ }