@veloxts/events 0.6.51

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.
@@ -0,0 +1,265 @@
1
+ /**
2
+ * Events Types
3
+ *
4
+ * Type definitions for real-time event broadcasting.
5
+ * Uses discriminated unions for type-safe driver configuration.
6
+ */
7
+ import type { FastifyRequest } from 'fastify';
8
+ /**
9
+ * Channel type determines access control.
10
+ * - public: Anyone can subscribe
11
+ * - private: Requires authentication
12
+ * - presence: Requires authentication + tracks who's online
13
+ */
14
+ export type ChannelType = 'public' | 'private' | 'presence';
15
+ /**
16
+ * Channel information.
17
+ */
18
+ export interface Channel {
19
+ /** Channel name (e.g., "orders.123", "chat.room-1") */
20
+ name: string;
21
+ /** Channel type */
22
+ type: ChannelType;
23
+ }
24
+ /**
25
+ * Presence channel member information.
26
+ */
27
+ export interface PresenceMember {
28
+ /** Unique user identifier */
29
+ id: string;
30
+ /** Optional user info (name, avatar, etc.) */
31
+ info?: Record<string, unknown>;
32
+ }
33
+ /**
34
+ * Channel authorization result.
35
+ */
36
+ export interface ChannelAuthResult {
37
+ /** Whether access is allowed */
38
+ authorized: boolean;
39
+ /** For presence channels, the member info to broadcast */
40
+ member?: PresenceMember;
41
+ /** Optional error message if not authorized */
42
+ error?: string;
43
+ }
44
+ /**
45
+ * Channel authorization function.
46
+ */
47
+ export type ChannelAuthorizer = (channel: Channel, request: FastifyRequest) => Promise<ChannelAuthResult> | ChannelAuthResult;
48
+ /**
49
+ * Event payload structure.
50
+ */
51
+ export interface BroadcastEvent<T = unknown> {
52
+ /** Event name (e.g., "order.shipped", "message.created") */
53
+ event: string;
54
+ /** Channel to broadcast to */
55
+ channel: string;
56
+ /** Event data payload */
57
+ data: T;
58
+ /** Optional sender socket ID to exclude from broadcast */
59
+ except?: string;
60
+ }
61
+ /**
62
+ * Subscription to a channel.
63
+ */
64
+ export interface Subscription {
65
+ /** Channel being subscribed to */
66
+ channel: string;
67
+ /** Unique socket/connection ID */
68
+ socketId: string;
69
+ /** For presence channels, the member info */
70
+ member?: PresenceMember;
71
+ }
72
+ /**
73
+ * Client connection information.
74
+ */
75
+ export interface ClientConnection {
76
+ /** Unique socket identifier */
77
+ id: string;
78
+ /** Channels this client is subscribed to */
79
+ channels: Set<string>;
80
+ /** Optional user info if authenticated */
81
+ user?: Record<string, unknown>;
82
+ /** Connection timestamp */
83
+ connectedAt: Date;
84
+ /** Last activity timestamp */
85
+ lastActivity: Date;
86
+ }
87
+ /**
88
+ * Message sent from client.
89
+ */
90
+ export interface ClientMessage {
91
+ /** Message type */
92
+ type: 'subscribe' | 'unsubscribe' | 'ping' | 'message';
93
+ /** Channel name (for subscribe/unsubscribe) */
94
+ channel?: string;
95
+ /** Event name (for message) */
96
+ event?: string;
97
+ /** Message data */
98
+ data?: unknown;
99
+ }
100
+ /**
101
+ * Message sent to client.
102
+ */
103
+ export interface ServerMessage {
104
+ /** Message type */
105
+ type: 'event' | 'subscription_succeeded' | 'subscription_error' | 'pong' | 'error';
106
+ /** Channel name */
107
+ channel?: string;
108
+ /** Event name */
109
+ event?: string;
110
+ /** Message data */
111
+ data?: unknown;
112
+ /** Error message */
113
+ error?: string;
114
+ }
115
+ /**
116
+ * Broadcast driver interface.
117
+ * All drivers must implement this interface.
118
+ */
119
+ export interface BroadcastDriver {
120
+ /**
121
+ * Broadcast an event to a channel.
122
+ */
123
+ broadcast<T>(event: BroadcastEvent<T>): Promise<void>;
124
+ /**
125
+ * Get all subscribers for a channel.
126
+ */
127
+ getSubscribers(channel: string): Promise<string[]>;
128
+ /**
129
+ * Get presence members for a channel.
130
+ */
131
+ getPresenceMembers(channel: string): Promise<PresenceMember[]>;
132
+ /**
133
+ * Get connection count for a channel.
134
+ */
135
+ getConnectionCount(channel: string): Promise<number>;
136
+ /**
137
+ * Get all active channels.
138
+ */
139
+ getChannels(): Promise<string[]>;
140
+ /**
141
+ * Close the driver and clean up resources.
142
+ */
143
+ close(): Promise<void>;
144
+ }
145
+ /**
146
+ * Base configuration shared by all drivers.
147
+ */
148
+ export interface EventsBaseOptions {
149
+ /** Default channel authorizer */
150
+ authorizer?: ChannelAuthorizer;
151
+ /**
152
+ * Secret key for signing auth tokens (required for private/presence channels).
153
+ * Should be at least 16 characters of high entropy.
154
+ */
155
+ authSecret?: string;
156
+ /** Ping interval in milliseconds (default: 30000) */
157
+ pingInterval?: number;
158
+ /** Connection timeout in milliseconds (default: 60000) */
159
+ connectionTimeout?: number;
160
+ }
161
+ /**
162
+ * WebSocket driver configuration.
163
+ */
164
+ export interface EventsWsOptions extends EventsBaseOptions {
165
+ driver: 'ws';
166
+ /** WebSocket path (default: "/ws") */
167
+ path?: string;
168
+ /** Redis URL for horizontal scaling (optional) */
169
+ redis?: string;
170
+ /** Maximum payload size in bytes (default: 1MB) */
171
+ maxPayloadSize?: number;
172
+ }
173
+ /**
174
+ * Server-Sent Events driver configuration.
175
+ */
176
+ export interface EventsSseOptions extends EventsBaseOptions {
177
+ driver: 'sse';
178
+ /** SSE path (default: "/events") */
179
+ path?: string;
180
+ /** Heartbeat interval in milliseconds (default: 15000) */
181
+ heartbeatInterval?: number;
182
+ /** Retry interval sent to client in milliseconds (default: 3000) */
183
+ retryInterval?: number;
184
+ }
185
+ /**
186
+ * Default configuration (WebSocket).
187
+ */
188
+ export interface EventsDefaultOptions extends EventsBaseOptions {
189
+ driver?: undefined;
190
+ /** WebSocket path (default: "/ws") */
191
+ path?: string;
192
+ /** Redis URL for horizontal scaling (optional) */
193
+ redis?: string;
194
+ }
195
+ /**
196
+ * Union type for all events plugin options.
197
+ */
198
+ export type EventsPluginOptions = EventsWsOptions | EventsSseOptions | EventsDefaultOptions;
199
+ /**
200
+ * Events manager options.
201
+ */
202
+ export interface EventsManagerOptions extends EventsBaseOptions {
203
+ /** The broadcast driver to use */
204
+ driver: BroadcastDriver;
205
+ }
206
+ /**
207
+ * Events manager interface.
208
+ * High-level API for broadcasting events.
209
+ */
210
+ export interface EventsManager {
211
+ /**
212
+ * Broadcast an event to a channel.
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * await events.broadcast('orders.123', 'order.shipped', {
217
+ * orderId: '123',
218
+ * trackingNumber: 'TRACK123',
219
+ * });
220
+ * ```
221
+ */
222
+ broadcast<T>(channel: string, event: string, data: T, except?: string): Promise<void>;
223
+ /**
224
+ * Broadcast an event to multiple channels.
225
+ *
226
+ * @example
227
+ * ```typescript
228
+ * await events.broadcastToMany(['users.1', 'users.2'], 'notification', {
229
+ * message: 'System maintenance scheduled',
230
+ * });
231
+ * ```
232
+ */
233
+ broadcastToMany<T>(channels: string[], event: string, data: T): Promise<void>;
234
+ /**
235
+ * Broadcast to all subscribers except the sender.
236
+ *
237
+ * @example
238
+ * ```typescript
239
+ * await events.toOthers('chat.room-1', 'message.sent', {
240
+ * text: 'Hello!',
241
+ * }, senderSocketId);
242
+ * ```
243
+ */
244
+ toOthers<T>(channel: string, event: string, data: T, except: string): Promise<void>;
245
+ /**
246
+ * Get subscriber count for a channel.
247
+ */
248
+ subscriberCount(channel: string): Promise<number>;
249
+ /**
250
+ * Get presence members for a channel.
251
+ */
252
+ presenceMembers(channel: string): Promise<PresenceMember[]>;
253
+ /**
254
+ * Check if a channel has subscribers.
255
+ */
256
+ hasSubscribers(channel: string): Promise<boolean>;
257
+ /**
258
+ * Get all active channels.
259
+ */
260
+ channels(): Promise<string[]>;
261
+ /**
262
+ * Close the manager and clean up resources.
263
+ */
264
+ close(): Promise<void>;
265
+ }
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Events Types
3
+ *
4
+ * Type definitions for real-time event broadcasting.
5
+ * Uses discriminated unions for type-safe driver configuration.
6
+ */
7
+ export {};
package/package.json ADDED
@@ -0,0 +1,83 @@
1
+ {
2
+ "name": "@veloxts/events",
3
+ "version": "0.6.51",
4
+ "description": "Real-time event broadcasting for VeloxTS framework",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ },
14
+ "./drivers/ws": {
15
+ "types": "./dist/drivers/ws.d.ts",
16
+ "import": "./dist/drivers/ws.js"
17
+ },
18
+ "./drivers/sse": {
19
+ "types": "./dist/drivers/sse.d.ts",
20
+ "import": "./dist/drivers/sse.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "README.md",
26
+ "GUIDE.md"
27
+ ],
28
+ "dependencies": {
29
+ "fastify-plugin": "5.1.0",
30
+ "superjson": "2.2.2",
31
+ "ws": "8.18.2",
32
+ "zod": "3.24.4",
33
+ "@veloxts/core": "0.6.51"
34
+ },
35
+ "peerDependencies": {
36
+ "fastify": "^5.0.0",
37
+ "ioredis": ">=5.0.0"
38
+ },
39
+ "peerDependenciesMeta": {
40
+ "ioredis": {
41
+ "optional": true
42
+ }
43
+ },
44
+ "devDependencies": {
45
+ "@types/node": "22.15.29",
46
+ "@types/ws": "8.18.1",
47
+ "@vitest/coverage-v8": "4.0.16",
48
+ "fastify": "5.6.2",
49
+ "ioredis": "5.6.1",
50
+ "typescript": "5.8.3",
51
+ "vitest": "4.0.16",
52
+ "@veloxts/testing": "0.6.51"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public"
56
+ },
57
+ "repository": {
58
+ "type": "git",
59
+ "url": "https://github.com/veloxts/veloxts.git",
60
+ "directory": "packages/events"
61
+ },
62
+ "keywords": [
63
+ "veloxts",
64
+ "events",
65
+ "websocket",
66
+ "sse",
67
+ "realtime",
68
+ "broadcasting",
69
+ "pubsub"
70
+ ],
71
+ "author": "VeloxTS Team",
72
+ "license": "MIT",
73
+ "engines": {
74
+ "node": ">=20.0.0"
75
+ },
76
+ "scripts": {
77
+ "build": "tsc",
78
+ "type-check": "tsc --noEmit",
79
+ "test": "vitest run",
80
+ "test:watch": "vitest",
81
+ "test:coverage": "vitest run --coverage"
82
+ }
83
+ }