@aegis-fluxion/core 0.7.0 → 0.7.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/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { IncomingMessage } from 'node:http';
2
2
  import WebSocket, { ClientOptions, ServerOptions } from 'ws';
3
3
 
4
+ declare const SECURE_SERVER_ADAPTER_MESSAGE_VERSION = 1;
4
5
  interface SecureEnvelope<TData = unknown> {
5
6
  event: string;
6
7
  data: TData;
@@ -15,8 +16,38 @@ interface SecureServerHeartbeatOptions {
15
16
  intervalMs?: number;
16
17
  timeoutMs?: number;
17
18
  }
19
+ type SecureServerRateLimitAction = "throttle" | "disconnect";
20
+ interface SecureServerRateLimitOptions {
21
+ enabled?: boolean;
22
+ windowMs?: number;
23
+ maxEventsPerConnection?: number;
24
+ maxEventsPerIp?: number;
25
+ action?: SecureServerRateLimitAction;
26
+ throttleMs?: number;
27
+ maxThrottleMs?: number;
28
+ disconnectAfterViolations?: number;
29
+ disconnectCode?: number;
30
+ disconnectReason?: string;
31
+ }
32
+ type SecureServerAdapterMessageScope = "broadcast" | "room";
33
+ interface SecureServerAdapterMessage {
34
+ version: typeof SECURE_SERVER_ADAPTER_MESSAGE_VERSION;
35
+ originServerId: string;
36
+ scope: SecureServerAdapterMessageScope;
37
+ event: string;
38
+ data: unknown;
39
+ emittedAt: number;
40
+ room?: string;
41
+ }
42
+ interface SecureServerAdapter {
43
+ attach: (server: SecureServer) => void | Promise<void>;
44
+ publish: (message: SecureServerAdapterMessage) => void | Promise<void>;
45
+ detach?: (server: SecureServer) => void | Promise<void>;
46
+ }
18
47
  interface SecureServerOptions extends ServerOptions {
19
48
  heartbeat?: SecureServerHeartbeatOptions;
49
+ rateLimit?: SecureServerRateLimitOptions;
50
+ adapter?: SecureServerAdapter;
20
51
  }
21
52
  interface SecureClientReconnectOptions {
22
53
  enabled?: boolean;
@@ -84,9 +115,13 @@ interface SecureServerMessageMiddlewareContext {
84
115
  type SecureServerMiddlewareContext = SecureServerConnectionMiddlewareContext | SecureServerMessageMiddlewareContext;
85
116
  type SecureServerMiddlewareNext = () => Promise<void>;
86
117
  type SecureServerMiddleware = (context: SecureServerMiddlewareContext, next: SecureServerMiddlewareNext) => void | Promise<void>;
118
+ declare function normalizeSecureServerAdapterMessage(value: unknown): SecureServerAdapterMessage;
87
119
  declare class SecureServer {
120
+ private readonly instanceId;
88
121
  private readonly socketServer;
122
+ private adapter;
89
123
  private readonly heartbeatConfig;
124
+ private readonly rateLimitConfig;
90
125
  private heartbeatIntervalHandle;
91
126
  private readonly clientsById;
92
127
  private readonly clientIdBySocket;
@@ -105,9 +140,15 @@ declare class SecureServer {
105
140
  private readonly heartbeatStateBySocket;
106
141
  private readonly roomMembersByName;
107
142
  private readonly roomNamesByClientId;
143
+ private readonly clientIpByClientId;
144
+ private readonly rateLimitBucketsByClientId;
145
+ private readonly rateLimitBucketsByIp;
108
146
  constructor(options: SecureServerOptions);
109
147
  get clientCount(): number;
148
+ get serverId(): string;
110
149
  get clients(): ReadonlyMap<string, SecureServerClient>;
150
+ setAdapter(adapter: SecureServerAdapter | null): Promise<void>;
151
+ handleAdapterMessage(message: unknown): Promise<void>;
111
152
  on(event: "connection", handler: SecureServerConnectionHandler): this;
112
153
  on(event: "disconnect", handler: SecureServerDisconnectHandler): this;
113
154
  on(event: "ready", handler: SecureServerReadyHandler): this;
@@ -127,6 +168,16 @@ declare class SecureServer {
127
168
  to(room: string): SecureServerRoomOperator;
128
169
  close(code?: number, reason?: string): void;
129
170
  private resolveHeartbeatConfig;
171
+ private resolveRateLimitConfig;
172
+ private createRateLimitBucket;
173
+ private getOrCreateRateLimitBucket;
174
+ private updateRateLimitBucket;
175
+ private pruneRateLimitBucketMap;
176
+ private pruneRateLimitBuckets;
177
+ private normalizeIpAddress;
178
+ private resolveClientIp;
179
+ private isIpStillConnected;
180
+ private evaluateIncomingRateLimit;
130
181
  private startHeartbeatLoop;
131
182
  private stopHeartbeatLoop;
132
183
  private performHeartbeatSweep;
@@ -157,6 +208,8 @@ declare class SecureServer {
157
208
  private queuePayload;
158
209
  private flushQueuedPayloads;
159
210
  private createSecureServerClient;
211
+ private emitLocally;
212
+ private publishAdapterMessage;
160
213
  private normalizeRoomName;
161
214
  private joinClientToRoom;
162
215
  private leaveClientFromRoom;
@@ -223,4 +276,4 @@ declare class SecureClient {
223
276
  private flushPendingPayloadQueue;
224
277
  }
225
278
 
226
- export { type SecureAckCallback, type SecureAckOptions, type SecureBinaryPayload, SecureClient, type SecureClientConnectHandler, type SecureClientDisconnectHandler, type SecureClientEventHandler, type SecureClientEventMap, type SecureClientLifecycleEvent, type SecureClientOptions, type SecureClientReadyHandler, type SecureClientReconnectOptions, type SecureEnvelope, type SecureErrorHandler, SecureServer, type SecureServerClient, type SecureServerConnectionHandler, type SecureServerConnectionMiddlewareContext, type SecureServerDisconnectHandler, type SecureServerEventHandler, type SecureServerEventMap, type SecureServerHeartbeatOptions, type SecureServerLifecycleEvent, type SecureServerMessageMiddlewareContext, type SecureServerMiddleware, type SecureServerMiddlewareContext, type SecureServerMiddlewareNext, type SecureServerOptions, type SecureServerReadyHandler, type SecureServerRoomOperator };
279
+ export { type SecureAckCallback, type SecureAckOptions, type SecureBinaryPayload, SecureClient, type SecureClientConnectHandler, type SecureClientDisconnectHandler, type SecureClientEventHandler, type SecureClientEventMap, type SecureClientLifecycleEvent, type SecureClientOptions, type SecureClientReadyHandler, type SecureClientReconnectOptions, type SecureEnvelope, type SecureErrorHandler, SecureServer, type SecureServerAdapter, type SecureServerAdapterMessage, type SecureServerAdapterMessageScope, type SecureServerClient, type SecureServerConnectionHandler, type SecureServerConnectionMiddlewareContext, type SecureServerDisconnectHandler, type SecureServerEventHandler, type SecureServerEventMap, type SecureServerHeartbeatOptions, type SecureServerLifecycleEvent, type SecureServerMessageMiddlewareContext, type SecureServerMiddleware, type SecureServerMiddlewareContext, type SecureServerMiddlewareNext, type SecureServerOptions, type SecureServerRateLimitAction, type SecureServerRateLimitOptions, type SecureServerReadyHandler, type SecureServerRoomOperator, normalizeSecureServerAdapterMessage };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { IncomingMessage } from 'node:http';
2
2
  import WebSocket, { ClientOptions, ServerOptions } from 'ws';
3
3
 
4
+ declare const SECURE_SERVER_ADAPTER_MESSAGE_VERSION = 1;
4
5
  interface SecureEnvelope<TData = unknown> {
5
6
  event: string;
6
7
  data: TData;
@@ -15,8 +16,38 @@ interface SecureServerHeartbeatOptions {
15
16
  intervalMs?: number;
16
17
  timeoutMs?: number;
17
18
  }
19
+ type SecureServerRateLimitAction = "throttle" | "disconnect";
20
+ interface SecureServerRateLimitOptions {
21
+ enabled?: boolean;
22
+ windowMs?: number;
23
+ maxEventsPerConnection?: number;
24
+ maxEventsPerIp?: number;
25
+ action?: SecureServerRateLimitAction;
26
+ throttleMs?: number;
27
+ maxThrottleMs?: number;
28
+ disconnectAfterViolations?: number;
29
+ disconnectCode?: number;
30
+ disconnectReason?: string;
31
+ }
32
+ type SecureServerAdapterMessageScope = "broadcast" | "room";
33
+ interface SecureServerAdapterMessage {
34
+ version: typeof SECURE_SERVER_ADAPTER_MESSAGE_VERSION;
35
+ originServerId: string;
36
+ scope: SecureServerAdapterMessageScope;
37
+ event: string;
38
+ data: unknown;
39
+ emittedAt: number;
40
+ room?: string;
41
+ }
42
+ interface SecureServerAdapter {
43
+ attach: (server: SecureServer) => void | Promise<void>;
44
+ publish: (message: SecureServerAdapterMessage) => void | Promise<void>;
45
+ detach?: (server: SecureServer) => void | Promise<void>;
46
+ }
18
47
  interface SecureServerOptions extends ServerOptions {
19
48
  heartbeat?: SecureServerHeartbeatOptions;
49
+ rateLimit?: SecureServerRateLimitOptions;
50
+ adapter?: SecureServerAdapter;
20
51
  }
21
52
  interface SecureClientReconnectOptions {
22
53
  enabled?: boolean;
@@ -84,9 +115,13 @@ interface SecureServerMessageMiddlewareContext {
84
115
  type SecureServerMiddlewareContext = SecureServerConnectionMiddlewareContext | SecureServerMessageMiddlewareContext;
85
116
  type SecureServerMiddlewareNext = () => Promise<void>;
86
117
  type SecureServerMiddleware = (context: SecureServerMiddlewareContext, next: SecureServerMiddlewareNext) => void | Promise<void>;
118
+ declare function normalizeSecureServerAdapterMessage(value: unknown): SecureServerAdapterMessage;
87
119
  declare class SecureServer {
120
+ private readonly instanceId;
88
121
  private readonly socketServer;
122
+ private adapter;
89
123
  private readonly heartbeatConfig;
124
+ private readonly rateLimitConfig;
90
125
  private heartbeatIntervalHandle;
91
126
  private readonly clientsById;
92
127
  private readonly clientIdBySocket;
@@ -105,9 +140,15 @@ declare class SecureServer {
105
140
  private readonly heartbeatStateBySocket;
106
141
  private readonly roomMembersByName;
107
142
  private readonly roomNamesByClientId;
143
+ private readonly clientIpByClientId;
144
+ private readonly rateLimitBucketsByClientId;
145
+ private readonly rateLimitBucketsByIp;
108
146
  constructor(options: SecureServerOptions);
109
147
  get clientCount(): number;
148
+ get serverId(): string;
110
149
  get clients(): ReadonlyMap<string, SecureServerClient>;
150
+ setAdapter(adapter: SecureServerAdapter | null): Promise<void>;
151
+ handleAdapterMessage(message: unknown): Promise<void>;
111
152
  on(event: "connection", handler: SecureServerConnectionHandler): this;
112
153
  on(event: "disconnect", handler: SecureServerDisconnectHandler): this;
113
154
  on(event: "ready", handler: SecureServerReadyHandler): this;
@@ -127,6 +168,16 @@ declare class SecureServer {
127
168
  to(room: string): SecureServerRoomOperator;
128
169
  close(code?: number, reason?: string): void;
129
170
  private resolveHeartbeatConfig;
171
+ private resolveRateLimitConfig;
172
+ private createRateLimitBucket;
173
+ private getOrCreateRateLimitBucket;
174
+ private updateRateLimitBucket;
175
+ private pruneRateLimitBucketMap;
176
+ private pruneRateLimitBuckets;
177
+ private normalizeIpAddress;
178
+ private resolveClientIp;
179
+ private isIpStillConnected;
180
+ private evaluateIncomingRateLimit;
130
181
  private startHeartbeatLoop;
131
182
  private stopHeartbeatLoop;
132
183
  private performHeartbeatSweep;
@@ -157,6 +208,8 @@ declare class SecureServer {
157
208
  private queuePayload;
158
209
  private flushQueuedPayloads;
159
210
  private createSecureServerClient;
211
+ private emitLocally;
212
+ private publishAdapterMessage;
160
213
  private normalizeRoomName;
161
214
  private joinClientToRoom;
162
215
  private leaveClientFromRoom;
@@ -223,4 +276,4 @@ declare class SecureClient {
223
276
  private flushPendingPayloadQueue;
224
277
  }
225
278
 
226
- export { type SecureAckCallback, type SecureAckOptions, type SecureBinaryPayload, SecureClient, type SecureClientConnectHandler, type SecureClientDisconnectHandler, type SecureClientEventHandler, type SecureClientEventMap, type SecureClientLifecycleEvent, type SecureClientOptions, type SecureClientReadyHandler, type SecureClientReconnectOptions, type SecureEnvelope, type SecureErrorHandler, SecureServer, type SecureServerClient, type SecureServerConnectionHandler, type SecureServerConnectionMiddlewareContext, type SecureServerDisconnectHandler, type SecureServerEventHandler, type SecureServerEventMap, type SecureServerHeartbeatOptions, type SecureServerLifecycleEvent, type SecureServerMessageMiddlewareContext, type SecureServerMiddleware, type SecureServerMiddlewareContext, type SecureServerMiddlewareNext, type SecureServerOptions, type SecureServerReadyHandler, type SecureServerRoomOperator };
279
+ export { type SecureAckCallback, type SecureAckOptions, type SecureBinaryPayload, SecureClient, type SecureClientConnectHandler, type SecureClientDisconnectHandler, type SecureClientEventHandler, type SecureClientEventMap, type SecureClientLifecycleEvent, type SecureClientOptions, type SecureClientReadyHandler, type SecureClientReconnectOptions, type SecureEnvelope, type SecureErrorHandler, SecureServer, type SecureServerAdapter, type SecureServerAdapterMessage, type SecureServerAdapterMessageScope, type SecureServerClient, type SecureServerConnectionHandler, type SecureServerConnectionMiddlewareContext, type SecureServerDisconnectHandler, type SecureServerEventHandler, type SecureServerEventMap, type SecureServerHeartbeatOptions, type SecureServerLifecycleEvent, type SecureServerMessageMiddlewareContext, type SecureServerMiddleware, type SecureServerMiddlewareContext, type SecureServerMiddlewareNext, type SecureServerOptions, type SecureServerRateLimitAction, type SecureServerRateLimitOptions, type SecureServerReadyHandler, type SecureServerRoomOperator, normalizeSecureServerAdapterMessage };