ioserver 1.5.3 → 2.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.
@@ -0,0 +1,260 @@
1
+ /**
2
+ * @fileoverview IOServer - A TypeScript framework for building real-time applications
3
+ * with Socket.IO and Fastify integration. Provides modular architecture with
4
+ * Services, Controllers, Managers, and Watchers.
5
+ *
6
+ * @author Ben Mz <0x42en@users.noreply.github.com>
7
+ * @version 2.0.0
8
+ * @since 1.0.0
9
+ */
10
+ import { FastifyInstance } from 'fastify';
11
+ import { Server as SocketIOServer } from 'socket.io';
12
+ declare module 'fastify' {
13
+ interface FastifyInstance {
14
+ io: SocketIOServer;
15
+ }
16
+ }
17
+ /**
18
+ * Configuration options for IOServer instance
19
+ * @interface IOServerOptions
20
+ */
21
+ export interface IOServerOptions {
22
+ /** Logging verbosity level - defaults to 'ERROR' */
23
+ verbose?: LogLevel;
24
+ /** Server hostname - defaults to 'localhost' */
25
+ host?: string;
26
+ /** Server port number - defaults to 8080 */
27
+ port?: number;
28
+ /** Enable Socket.IO cookies - defaults to false */
29
+ cookie?: boolean;
30
+ /** Transport modes for Socket.IO - defaults to ['websocket', 'polling'] */
31
+ mode?: TransportMode | TransportMode[];
32
+ /** CORS configuration options */
33
+ cors?: any;
34
+ /** Path to routes directory - defaults to './routes' */
35
+ routes?: string;
36
+ }
37
+ /**
38
+ * Configuration options for registering a Service
39
+ * @interface ServiceOptions
40
+ */
41
+ export interface ServiceOptions {
42
+ /** Service namespace (optional, defaults to '/') */
43
+ name?: string;
44
+ /** Service class constructor that extends BaseService */
45
+ service: new (appHandle: AppHandle) => any;
46
+ /** Array of middleware classes to apply to this service */
47
+ middlewares?: (new () => any)[];
48
+ }
49
+ /**
50
+ * Configuration options for registering a Controller
51
+ * @interface ControllerOptions
52
+ */
53
+ export interface ControllerOptions {
54
+ /** Controller name that matches the route file name */
55
+ name: string;
56
+ /** Controller class constructor that extends BaseController */
57
+ controller: new (appHandle: AppHandle) => any;
58
+ /** Array of middleware classes to apply to this controller */
59
+ middlewares?: (new () => any)[];
60
+ /** URL prefix for all routes in this controller */
61
+ prefix?: string;
62
+ }
63
+ /**
64
+ * Configuration options for registering a Manager
65
+ * @interface ManagerOptions
66
+ */
67
+ export interface ManagerOptions {
68
+ /** Manager name (used as property name in appHandle) */
69
+ name: string;
70
+ /** Manager class constructor that extends BaseManager */
71
+ manager: new (appHandle: AppHandle) => any;
72
+ }
73
+ /**
74
+ * Configuration options for registering a Watcher
75
+ * @interface WatcherOptions
76
+ */
77
+ export interface WatcherOptions {
78
+ /** Watcher name for identification */
79
+ name: string;
80
+ /** Watcher class constructor that extends BaseWatcher */
81
+ watcher: new (appHandle: AppHandle) => any;
82
+ }
83
+ /**
84
+ * Options for sending real-time messages to clients
85
+ * @interface SendToOptions
86
+ */
87
+ export interface SendToOptions {
88
+ /** Target namespace (optional, defaults to '/') */
89
+ namespace?: string;
90
+ /** Event name to emit */
91
+ event: string;
92
+ /** Data payload to send */
93
+ data: any;
94
+ /** Target specific room (optional) */
95
+ room?: string;
96
+ /** Target specific socket ID (optional) */
97
+ sid?: string;
98
+ }
99
+ /**
100
+ * Application handle providing shared functionality across all components
101
+ * @interface AppHandle
102
+ */
103
+ export interface AppHandle {
104
+ /** Function to send real-time messages to clients */
105
+ send: (options: SendToOptions) => boolean;
106
+ /** Logging function with level-based filtering */
107
+ log: (level: number, text: string) => void;
108
+ /** Current logging verbosity level */
109
+ verbose: LogLevel;
110
+ /** Dynamic properties for registered managers */
111
+ [key: string]: any;
112
+ }
113
+ /**
114
+ * Log levels for controlling verbosity of output
115
+ */
116
+ export type LogLevel = 'EMERGENCY' | 'ALERT' | 'CRITICAL' | 'ERROR' | 'WARNING' | 'NOTIFICATION' | 'INFORMATION' | 'DEBUG';
117
+ /**
118
+ * Transport modes supported by Socket.IO
119
+ */
120
+ export type TransportMode = 'websocket' | 'polling';
121
+ /**
122
+ * Main IOServer class - A framework for building real-time applications
123
+ *
124
+ * Combines Fastify HTTP server with Socket.IO for WebSocket support,
125
+ * providing a modular architecture with Services, Controllers, Managers, and Watchers.
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * const server = new IOServer({
130
+ * host: 'localhost',
131
+ * port: 3000,
132
+ * verbose: 'INFO'
133
+ * });
134
+ *
135
+ * server.addService({ service: ChatService });
136
+ * server.addController({ name: 'api', controller: ApiController });
137
+ * await server.start();
138
+ * ```
139
+ */
140
+ export declare class IOServer {
141
+ private static readonly VERSION;
142
+ private static readonly DEFAULT_PORT;
143
+ private static readonly DEFAULT_HOST;
144
+ private static readonly LOG_LEVELS;
145
+ private static readonly TRANSPORTS;
146
+ private static readonly RESERVED_NAMES;
147
+ private readonly host;
148
+ private readonly port;
149
+ private readonly verbose;
150
+ private readonly routesPath;
151
+ private readonly webapp;
152
+ private socketio;
153
+ private readonly appHandle;
154
+ private readonly serviceLists;
155
+ private readonly managerLists;
156
+ private readonly methodLists;
157
+ private readonly watcherLists;
158
+ private readonly controllerLists;
159
+ private readonly middlewareLists;
160
+ /**
161
+ * Creates a new IOServer instance
162
+ * @param {IOServerOptions} options - Configuration options for the server
163
+ */
164
+ constructor(options?: IOServerOptions);
165
+ private validatePort;
166
+ private validateLogLevel;
167
+ private processTransportModes;
168
+ private processCorsOptions;
169
+ private initializeFastify;
170
+ private setupPlugins;
171
+ private setupSocketIO;
172
+ private log;
173
+ private unique;
174
+ private registerInternalClass;
175
+ private getListMap;
176
+ private dumpMethods;
177
+ /**
178
+ * Registers a watcher component for background tasks
179
+ * @param {WatcherOptions} options - Watcher configuration options
180
+ * @throws {IOServerError} When watcher instantiation fails
181
+ */
182
+ addWatcher(options: WatcherOptions): void;
183
+ /**
184
+ * Registers a manager component for shared functionality
185
+ * @param {ManagerOptions} options - Manager configuration options
186
+ * @throws {IOServerError} When manager instantiation fails
187
+ */
188
+ addManager(options: ManagerOptions): void;
189
+ /**
190
+ * Registers a service component for real-time WebSocket handling
191
+ * @param {ServiceOptions} options - Service configuration options
192
+ * @throws {IOServerError} When service instantiation fails
193
+ */
194
+ addService(options: ServiceOptions): void;
195
+ /**
196
+ * Registers a controller component for HTTP route handling
197
+ * @param {ControllerOptions} options - Controller configuration options
198
+ * @throws {IOServerError} When controller instantiation or route loading fails
199
+ */
200
+ addController(options: ControllerOptions): void;
201
+ private registerControllerRoutes;
202
+ /**
203
+ * Retrieves a registered service by name
204
+ * @param {string} name - The service name
205
+ * @returns {any} The service instance or undefined if not found
206
+ */
207
+ getService(name: string): any;
208
+ /**
209
+ * Starts the IOServer instance
210
+ * Initializes Socket.IO, starts watchers, and begins listening for connections
211
+ * @returns {Promise<void>} Promise that resolves when server is started
212
+ * @throws {IOServerError} When server startup fails
213
+ */
214
+ start(): Promise<void>;
215
+ /**
216
+ * Gets the server hostname
217
+ * @returns {string} The server hostname
218
+ */
219
+ getHost(): string;
220
+ /**
221
+ * Gets the server port number
222
+ * @returns {number} The server port
223
+ */
224
+ getPort(): number;
225
+ /**
226
+ * Gets the Fastify instance
227
+ * @returns {FastifyInstance} The Fastify application instance
228
+ */
229
+ getApp(): FastifyInstance;
230
+ /**
231
+ * Stops the IOServer instance
232
+ * @returns {Promise<void>} Promise that resolves when server is stopped
233
+ * @throws {IOServerError} When server shutdown fails
234
+ */
235
+ stop(): Promise<void>;
236
+ /**
237
+ * Sends real-time messages to connected clients
238
+ * @param {SendToOptions} options - Options for message delivery
239
+ * @returns {boolean} True if message was sent successfully, false otherwise
240
+ */
241
+ sendTo(options: SendToOptions): boolean;
242
+ /**
243
+ * Handles new WebSocket connections for a specific service
244
+ * @private
245
+ * @param {string} serviceName - The name of the service
246
+ * @returns {Function} Connection handler function
247
+ */
248
+ private handleConnection;
249
+ /**
250
+ * Creates a callback handler for WebSocket method calls
251
+ * @private
252
+ * @param {string} serviceName - The name of the service
253
+ * @param {string} methodName - The name of the method
254
+ * @param {any} socket - The Socket.IO socket instance
255
+ * @returns {Function} Callback handler function
256
+ */
257
+ private handleCallback;
258
+ }
259
+ export default IOServer;
260
+ //# sourceMappingURL=IOServer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IOServer.d.ts","sourceRoot":"","sources":["../src/IOServer.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAgB,EACd,eAAe,EAIhB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,MAAM,IAAI,cAAc,EAAE,MAAM,WAAW,CAAC;AAMrD,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,eAAe;QACvB,EAAE,EAAE,cAAc,CAAC;KACpB;CACF;AAED;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,oDAAoD;IACpD,OAAO,CAAC,EAAE,QAAQ,CAAC;IACnB,gDAAgD;IAChD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4CAA4C;IAC5C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,mDAAmD;IACnD,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,2EAA2E;IAC3E,IAAI,CAAC,EAAE,aAAa,GAAG,aAAa,EAAE,CAAC;IACvC,iCAAiC;IACjC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,wDAAwD;IACxD,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,oDAAoD;IACpD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,yDAAyD;IACzD,OAAO,EAAE,KAAK,SAAS,EAAE,SAAS,KAAK,GAAG,CAAC;IAC3C,2DAA2D;IAC3D,WAAW,CAAC,EAAE,CAAC,UAAU,GAAG,CAAC,EAAE,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC,uDAAuD;IACvD,IAAI,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,UAAU,EAAE,KAAK,SAAS,EAAE,SAAS,KAAK,GAAG,CAAC;IAC9C,8DAA8D;IAC9D,WAAW,CAAC,EAAE,CAAC,UAAU,GAAG,CAAC,EA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