@iadev93/zuno-express 0.0.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/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # @iadev93/zuno-express
2
+
3
+ Express adapter for **Zuno**.
4
+
5
+ Provides server‑side synchronization endpoints using Server‑Sent Events (SSE).
6
+
7
+ ---
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @iadev93/zuno-express
13
+ ```
14
+
15
+ Peer dependency:
16
+
17
+ * `express >= 4`
18
+
19
+ ---
20
+
21
+ ## Usage
22
+
23
+ ```ts
24
+ import express from "express";
25
+ import { createZunoExpress } from "@iadev93/zuno-express";
26
+ import { createZuno } from "@iadev93/zuno";
27
+
28
+ const app = express();
29
+ const zuno = createZuno();
30
+
31
+ createZunoExpress(app, { zuno });
32
+
33
+ app.listen(3000);
34
+ ```
35
+
36
+ ---
37
+
38
+ ## What It Provides
39
+
40
+ * SSE endpoint for state sync
41
+ * Snapshot delivery
42
+ * Version‑safe event ingestion
43
+
44
+ ---
45
+
46
+ ## What It Does NOT Do
47
+
48
+ * No WebSockets
49
+ * No framework‑specific state
50
+ * No persistence layer
51
+
52
+ ---
53
+
54
+ ## License
55
+
56
+ MIT
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@iadev93/zuno-express",
3
+ "version": "0.0.1",
4
+ "main": "./dist/index.js",
5
+ "types": "./dist/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ }
11
+ },
12
+ "scripts": {
13
+ "build": "tsc -p tsconfig.json"
14
+ },
15
+ "peerDependencies": {
16
+ "@iadev93/zuno": "workspace:*",
17
+ "express": "^5.0.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/express": "^5.0.0",
21
+ "@types/node": "^20.0.0",
22
+ "typescript": "^5.9.3",
23
+ "@iadev93/zuno": "workspace:*"
24
+ }
25
+ }
@@ -0,0 +1,36 @@
1
+ import type { IncomingHttpHeaders } from "http"
2
+ import { createExpressSSEHandler } from "./express-sse-handler"
3
+ import { createExpressSyncHandler } from "./express-sync-handler"
4
+ import { createExpressSnapshotHandler } from "./express-snapshot-handler"
5
+
6
+ /**
7
+ * Options for creating an Express router for Zuno.
8
+ */
9
+ type CreateZunoExpressOptions = {
10
+ headers?: IncomingHttpHeaders
11
+ }
12
+
13
+ /**
14
+ * Creates an Express router for Zuno.
15
+ *
16
+ * @param opts - Options for creating the Express router.
17
+ * @returns An object containing the SSE, sync, and snapshot handlers
18
+ */
19
+ export function createZunoExpress(opts?: CreateZunoExpressOptions) {
20
+ const { headers } = opts ?? {}
21
+
22
+ return {
23
+ /**
24
+ * Handles SSE connections.
25
+ */
26
+ sse: createExpressSSEHandler(headers),
27
+ /**
28
+ * Handles sync connections.
29
+ */
30
+ sync: createExpressSyncHandler(),
31
+ /**
32
+ * Handles snapshot connections.
33
+ */
34
+ snapshot: createExpressSnapshotHandler(),
35
+ }
36
+ }
@@ -0,0 +1,10 @@
1
+ import type { Request, Response } from "express"
2
+ import { sendSnapshot } from "@iadev93/zuno/server"
3
+
4
+ /**
5
+ * Creates an Express handler for handling Zuno state events.
6
+ * @returns An Express handler function.
7
+ */
8
+ export function createExpressSnapshotHandler() {
9
+ return (req: Request, res: Response) => sendSnapshot(req, res)
10
+ }
@@ -0,0 +1,15 @@
1
+ import type { Request, Response } from "express"
2
+ import type { IncomingHttpHeaders } from "http";
3
+
4
+ import { createSSEConnection } from "@iadev93/zuno/server";
5
+
6
+ /**
7
+ * Creates an SSE handler for express.
8
+ * @returns An Express handler function.
9
+ */
10
+ export function createExpressSSEHandler(headers?: IncomingHttpHeaders) {
11
+ /**
12
+ * Creates an SSE connection for the client.
13
+ */
14
+ return (req: Request, res: Response) => createSSEConnection(req, res, headers ?? {})
15
+ }
@@ -0,0 +1,30 @@
1
+ import type { Request, Response } from "express"
2
+ import type { ZunoStateEvent } from "@iadev93/zuno"
3
+
4
+ import { applyStateEvent } from "@iadev93/zuno/server"
5
+
6
+ /**
7
+ * Creates an Express handler for handling Zuno state events.
8
+ * @returns An Express handler function.
9
+ */
10
+ export function createExpressSyncHandler() {
11
+ return (req: Request, res: Response) => {
12
+ /** The incoming state event to apply. */
13
+ const incoming = req.body as ZunoStateEvent
14
+
15
+ /** Applies the incoming state event to the target Zuno universe or store. */
16
+ const result = applyStateEvent(incoming)
17
+
18
+ /** If the state event is not valid, returns a 409 status code with the reason and current state. */
19
+ if (!result.ok) {
20
+ res.status(409).json({
21
+ reason: result.reason,
22
+ current: result.current,
23
+ })
24
+ return
25
+ }
26
+
27
+ /** Returns a 200 status code with the result. */
28
+ res.status(200).json({ ok: true, event: result.event })
29
+ }
30
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './createZunoExpress';
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist",
6
+ "declaration": true,
7
+ "declarationMap": true,
8
+ "emitDeclarationOnly": false,
9
+ "module": "ESNext",
10
+ "moduleResolution": "Bundler"
11
+ },
12
+ "include": [
13
+ "./src/**/*.ts"
14
+ ]
15
+ }