@aegis-fluxion/core 0.3.0 → 0.4.0

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,179 @@
1
+ # @aegis-fluxion/core
2
+
3
+ Core E2E-encrypted WebSocket primitives for `aegis-fluxion`.
4
+
5
+ Version: **0.4.0**
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - ECDH handshake (`prime256v1`) with ephemeral key exchange
12
+ - AES-256-GCM encrypted message envelopes
13
+ - Server/client lifecycle events (`connect`, `ready`, `disconnect`, `error`)
14
+ - Secure room routing (`join`, `leave`, `to(room).emit`)
15
+ - Heartbeat-based zombie cleanup
16
+ - Auto-reconnect with exponential backoff
17
+ - **RPC-style ACK request/response with timeout support**
18
+
19
+ ---
20
+
21
+ ## Install
22
+
23
+ ```bash
24
+ npm install @aegis-fluxion/core ws
25
+ ```
26
+
27
+ ---
28
+
29
+ ## API at a Glance
30
+
31
+ ### `SecureServer`
32
+
33
+ - `on("connection" | "ready" | "disconnect" | "error", handler)`
34
+ - `on("custom:event", (data, client) => unknown | Promise<unknown>)`
35
+ - `emit(event, data): SecureServer`
36
+ - `emitTo(clientId, event, data): boolean`
37
+ - `emitTo(clientId, event, data, callback): boolean`
38
+ - `emitTo(clientId, event, data, options): Promise<unknown>`
39
+ - `emitTo(clientId, event, data, options, callback): boolean`
40
+ - `to(room).emit(event, data): SecureServer`
41
+ - `close(code?, reason?): void`
42
+
43
+ ### `SecureServerClient`
44
+
45
+ - `id: string`
46
+ - `socket: WebSocket`
47
+ - `join(room): boolean`
48
+ - `leave(room): boolean`
49
+ - `leaveAll(): number`
50
+ - `emit(event, data, ...ackArgs): boolean | Promise<unknown>`
51
+
52
+ ### `SecureClient`
53
+
54
+ - `connect(): void`
55
+ - `disconnect(code?, reason?): void`
56
+ - `isConnected(): boolean`
57
+ - `readyState: number | null`
58
+ - `emit(event, data): boolean`
59
+ - `emit(event, data, callback): boolean`
60
+ - `emit(event, data, options): Promise<unknown>`
61
+ - `emit(event, data, options, callback): boolean`
62
+ - `on("connect" | "ready" | "disconnect" | "error", handler)`
63
+ - `on("custom:event", handler)`
64
+
65
+ ---
66
+
67
+ ## ACK (Request-Response) Usage
68
+
69
+ ### 1) Client -> Server (Promise ACK)
70
+
71
+ ```ts
72
+ import { SecureClient, SecureServer } from "@aegis-fluxion/core";
73
+
74
+ const server = new SecureServer({ port: 8080, host: "127.0.0.1" });
75
+
76
+ server.on("math:add", ({ a, b }) => {
77
+ return { total: Number(a) + Number(b) };
78
+ });
79
+
80
+ const client = new SecureClient("ws://127.0.0.1:8080");
81
+
82
+ client.on("ready", async () => {
83
+ const response = await client.emit(
84
+ "math:add",
85
+ { a: 2, b: 3 },
86
+ { timeoutMs: 1000 }
87
+ );
88
+
89
+ console.log(response); // { total: 5 }
90
+ });
91
+ ```
92
+
93
+ ### 2) Client -> Server (Callback ACK)
94
+
95
+ ```ts
96
+ client.emit(
97
+ "math:add",
98
+ { a: 4, b: 6 },
99
+ { timeoutMs: 1000 },
100
+ (error, response) => {
101
+ if (error) {
102
+ console.error("ACK error:", error.message);
103
+ return;
104
+ }
105
+
106
+ console.log(response); // { total: 10 }
107
+ }
108
+ );
109
+ ```
110
+
111
+ ### 3) Server -> Client (Promise ACK)
112
+
113
+ ```ts
114
+ server.on("ready", async (clientSocket) => {
115
+ const response = await clientSocket.emit(
116
+ "agent:health",
117
+ { verbose: true },
118
+ { timeoutMs: 1200 }
119
+ );
120
+
121
+ console.log(response);
122
+ });
123
+
124
+ client.on("agent:health", () => {
125
+ return { ok: true, uptime: process.uptime() };
126
+ });
127
+ ```
128
+
129
+ ### 4) ACK Timeout Behavior
130
+
131
+ When no response arrives before `timeoutMs`, ACK request fails:
132
+
133
+ - Promise form -> rejects with timeout error
134
+ - Callback form -> callback receives `Error`
135
+
136
+ ```ts
137
+ try {
138
+ await client.emit("never:respond", { ping: true }, { timeoutMs: 300 });
139
+ } catch (error) {
140
+ console.error((error as Error).message);
141
+ // ACK response timed out after 300ms for event "never:respond".
142
+ }
143
+ ```
144
+
145
+ ---
146
+
147
+ ## Security Notes
148
+
149
+ - ACK request and ACK response frames are encrypted with the same AES-GCM tunnel as normal events.
150
+ - Internal handshake/RPC transport events are reserved and cannot be emitted manually.
151
+ - On disconnect/heartbeat timeout, pending ACK promises are rejected and memory state is cleaned.
152
+
153
+ ---
154
+
155
+ ## Development
156
+
157
+ From monorepo root:
158
+
159
+ ```bash
160
+ npm run typecheck -w @aegis-fluxion/core
161
+ npm run test -w @aegis-fluxion/core
162
+ npm run build -w @aegis-fluxion/core
163
+ ```
164
+
165
+ ---
166
+
167
+ ## Publish
168
+
169
+ From monorepo root:
170
+
171
+ ```bash
172
+ npm publish -w @aegis-fluxion/core --access public
173
+ ```
174
+
175
+ ---
176
+
177
+ ## License
178
+
179
+ MIT