redweb 0.6.2 → 0.6.5

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/index.d.ts CHANGED
@@ -1,199 +1,255 @@
1
- declare module 'redweb' {
2
- import { Request, Response } from 'express';
3
- import { Server as HTTPServer } from 'http';
4
- import { Server as HTTPSServer } from 'https';
5
- import WebSocket from 'ws';
6
-
7
- /**
8
- * RedWeb encoding types.
9
- */
10
- export type RedWebEncoding = 'json' | 'urlencoded';
11
-
12
- /**
13
- * Service configuration for RedWeb.
14
- */
15
- export interface Service {
16
- serviceName: string;
17
- method: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
18
- function: (req: Request, res: Response) => void;
19
- }
20
-
21
- /**
22
- * Options for configuring a RedWeb server.
23
- */
24
- export interface RedWebOptions {
25
- port?: number;
26
- bind?: string;
27
- publicPaths?: string[];
28
- services?: Service[];
29
- listenCallback?: () => void;
30
- encoding?: RedWebEncoding;
31
- ssl?: {
32
- key: string;
33
- cert: string;
34
- };
35
- }
36
-
37
- /**
38
- * Configuration for a WebSocket handler.
39
- */
40
- export interface HandlerConfig {
41
- name: string;
42
- handlers: {
43
- [type: string]: (socket: WebSocket, data: any) => void;
44
- };
45
- }
46
-
47
- /**
48
- * Options for configuring a WebSocket server.
49
- */
50
- export interface SocketServerOptions {
51
- port?: number;
52
- connectionOpenCallback?: (socket: WebSocket) => void;
53
- connectionCloseCallback?: (socket: WebSocket) => void;
54
- messageCallback?: (socket: WebSocket, message: string) => void;
55
- messageHandlers?: {
56
- [type: string]: (socket: WebSocket, data: any) => void;
57
- };
58
- ssl?: {
59
- key: string;
60
- cert: string;
61
- };
62
- handlerConfig?: Array<new () => BaseHandler>;
63
- }
64
-
65
- /**
66
- * Base class for WebSocket handlers.
67
- */
68
- export class BaseHandler {
69
- /**
70
- * The name of the handler (used to identify it in the server).
71
- */
72
- name: string;
73
-
74
- /**
75
- * Dictionary of message handlers for this handler.
76
- */
77
- messageHandlers: {
78
- [type: string]: (socket: WebSocket, data: any) => void;
79
- };
80
-
81
- /**
82
- * List of active WebSocket connections managed by this handler.
83
- */
84
- connections: WebSocket[];
85
-
86
- /**
87
- * Creates a new handler instance.
88
- * @param config - Configuration for the handler.
89
- */
90
- constructor(config: HandlerConfig);
91
-
92
- /**
93
- * Adds a new WebSocket connection, processes initial data, and sets up message handling for this handler.
94
- * @param socket - The WebSocket connection to add.
95
- * @param data - Optional initial data sent during the connection handshake.
96
- */
97
- newConnection(socket: WebSocket, data?: any): void;
98
-
99
- /**
100
- * Handles an incoming message and routes it to the appropriate handler function.
101
- * @param socket - The WebSocket connection that sent the message.
102
- * @param message - The incoming message in JSON string format.
103
- */
104
- handleMessage(socket: WebSocket, message: string): void;
105
-
106
- /**
107
- * Broadcasts a message to all connections managed by this handler.
108
- * @param message - The message to broadcast.
109
- */
110
- broadcast(message: object): void;
111
- }
112
-
113
- /**
114
- * Base WebSocket server class.
115
- */
116
- export class BaseSocketServer {
117
- /**
118
- * Map of connected WebSocket clients by their IP address.
119
- */
120
- clients: Map<string, WebSocket>;
121
-
122
- /**
123
- * List of handler instances currently registered with the server.
124
- */
125
- handlers: BaseHandler[];
126
-
127
- constructor(server: HTTPServer | HTTPSServer, options?: SocketServerOptions);
128
-
129
- /**
130
- * Adds a new handler to the WebSocket server.
131
- * @param HandlerClass - A class extending `BaseHandler` to add to the server.
132
- */
133
- addHandler(HandlerClass: new () => BaseHandler): void;
134
- }
135
-
136
- /**
137
- * HTTP server class.
138
- */
139
- export class HttpServer {
140
- constructor(options?: RedWebOptions);
141
- }
142
-
143
- /**
144
- * HTTPS server class.
145
- */
146
- export class HttpsServer {
147
- constructor(options?: RedWebOptions);
148
- }
149
-
150
- /**
151
- * WebSocket server class.
152
- */
153
- export class SocketServer extends BaseSocketServer {
154
- constructor(options?: SocketServerOptions);
155
- }
156
-
157
- /**
158
- * Secure WebSocket server class.
159
- */
160
- export class SecureSocketServer extends BaseSocketServer {
161
- constructor(options?: SocketServerOptions);
162
- }
163
-
164
- /**
165
- * SSL configuration loader.
166
- */
167
- export function loadSslConfig(sslOptions: { key: string; cert: string }): { key: string; cert: string };
168
-
169
- /**
170
- * Constants for encoding types.
171
- */
172
- export const ENCODINGS: {
173
- json: 'json';
174
- urlencoded: 'urlencoded';
175
- };
176
-
177
- /**
178
- * Constants for HTTP methods.
179
- */
180
- export const METHODS: {
181
- POST: 'post';
182
- GET: 'get';
183
- PUT: 'put';
184
- DELETE: 'delete';
185
- PATCH: 'patch';
186
- OPTIONS: 'options';
187
- HEAD: 'head';
188
- };
189
-
190
- /**
191
- * Default HTTP options.
192
- */
193
- export const HTTP_OPTIONS: RedWebOptions;
194
-
195
- /**
196
- * Default socket options.
197
- */
198
- export const SOCKET_OPTIONS: SocketServerOptions;
199
- }
1
+ declare module 'redweb' {
2
+ import { Request, Response } from 'express';
3
+ import { Server as HTTPServer } from 'http';
4
+ import { Server as HTTPSServer } from 'https';
5
+ import WebSocket from 'ws';
6
+
7
+ /**
8
+ * RedWeb encoding types.
9
+ */
10
+ export type RedWebEncoding = 'json' | 'urlencoded';
11
+
12
+ /**
13
+ * Service configuration for RedWeb.
14
+ */
15
+ export interface Service {
16
+ serviceName: string;
17
+ method: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
18
+ function: (req: Request, res: Response) => void;
19
+ }
20
+
21
+ /**
22
+ * Options for configuring a RedWeb server.
23
+ */
24
+ export interface RedWebOptions {
25
+ port?: number;
26
+ bind?: string;
27
+ publicPaths?: string[];
28
+ services?: Service[];
29
+ listenCallback?: () => void;
30
+ encoding?: RedWebEncoding;
31
+ ssl?: {
32
+ key: string;
33
+ cert: string;
34
+ };
35
+ }
36
+
37
+ /**
38
+ * Configuration for a WebSocket handler.
39
+ */
40
+ export interface HandlerConfig {
41
+ name: string;
42
+ handlers: {
43
+ [type: string]: (socket: WebSocket, data: any) => void;
44
+ };
45
+ }
46
+
47
+ /**
48
+ * Options for configuring a WebSocket server.
49
+ */
50
+ export interface SocketServerOptions {
51
+ port?: number;
52
+ connectionOpenCallback?: (socket: WebSocket) => void;
53
+ connectionCloseCallback?: (socket: WebSocket) => void;
54
+ messageCallback?: (socket: WebSocket, message: string) => void;
55
+ messageHandlers?: {
56
+ [type: string]: (socket: WebSocket, data: any) => void;
57
+ };
58
+ ssl?: {
59
+ key: string;
60
+ cert: string;
61
+ };
62
+ routes?: Array<new () => SocketRoute>;
63
+ }
64
+
65
+ /**
66
+ * WebSocket route configuration.
67
+ */
68
+ export interface SocketRouteConfig {
69
+ path: string; // The WebSocket route path (e.g., "/chat").
70
+ handlers: Array<new () => BaseHandler>; // Array of handler classes for the route.
71
+ }
72
+
73
+ /**
74
+ * Represents a WebSocket route.
75
+ */
76
+ export class SocketRoute {
77
+ /**
78
+ * The path for the WebSocket route.
79
+ */
80
+ path: string;
81
+
82
+ /**
83
+ * Handlers associated with the route.
84
+ */
85
+ handlers: BaseHandler[];
86
+
87
+ /**
88
+ * Creates a new `SocketRoute` instance.
89
+ * @param config - Configuration options for the route.
90
+ */
91
+ constructor(config: SocketRouteConfig);
92
+
93
+ /**
94
+ * Adds a new handler dynamically.
95
+ * @param HandlerClass - A class extending `BaseHandler`.
96
+ */
97
+ addHandler(HandlerClass: new () => BaseHandler): void;
98
+
99
+ /**
100
+ * Handles a new WebSocket connection.
101
+ * @param socket - The WebSocket connection instance.
102
+ * @param req - The HTTP request associated with the connection.
103
+ */
104
+ handleConnection(socket: WebSocket, req: import('http').IncomingMessage): void;
105
+
106
+ /**
107
+ * Handles incoming WebSocket messages.
108
+ * @param socket - The WebSocket connection instance.
109
+ * @param data - The message data.
110
+ */
111
+ handleMessage(socket: WebSocket, data: any): void;
112
+
113
+ /**
114
+ * Handles WebSocket disconnections.
115
+ * @param socket - The WebSocket connection instance.
116
+ */
117
+ handleClose(socket: WebSocket): void;
118
+
119
+ /**
120
+ * Handles WebSocket errors.
121
+ * @param socket - The WebSocket connection instance.
122
+ * @param error - The error object.
123
+ */
124
+ handleError(socket: WebSocket, error: Error): void;
125
+ }
126
+
127
+ /**
128
+ * Base class for WebSocket handlers.
129
+ */
130
+ export class BaseHandler {
131
+ /**
132
+ * The name of the handler (used to identify it in the server).
133
+ */
134
+ name: string;
135
+ /**
136
+ * Creates a new handler instance.
137
+ * @param name - The name of the handler.
138
+ */
139
+ constructor(name: string);
140
+
141
+ /**
142
+ * Handles an incoming message.
143
+ * @param socket - The WebSocket connection that sent the message.
144
+ * @param message - The message data.
145
+ */
146
+ onMessage(socket: WebSocket, message: Object): void;
147
+
148
+ /**
149
+ * Called during the first contact with a new WebSocket connection.
150
+ * @param socket - The WebSocket connection instance.
151
+ */
152
+ onInitialContact(socket: WebSocket): void;
153
+
154
+ /**
155
+ * Called when a WebSocket connection closes.
156
+ * @param socket - The WebSocket connection instance.
157
+ */
158
+ onClose(socket: WebSocket): void;
159
+ }
160
+
161
+ /**
162
+ * Base WebSocket server class.
163
+ */
164
+ export class BaseSocketServer {
165
+ /**
166
+ * List of WebSocket routes.
167
+ */
168
+ routes: SocketRoute[];
169
+
170
+ /**
171
+ * Creates a new `BaseSocketServer`.
172
+ * @param server - The HTTP server instance.
173
+ * @param options - Configuration options.
174
+ */
175
+ constructor(server: HTTPServer | HTTPSServer, options?: SocketServerOptions);
176
+
177
+ /**
178
+ * Handles WebSocket upgrade requests.
179
+ * @param req - The incoming HTTP upgrade request.
180
+ * @param socket - The raw network socket.
181
+ * @param head - The initial data chunk.
182
+ */
183
+ handleUpgrade(req: import('http').IncomingMessage, socket: import('net').Socket, head: Buffer): void;
184
+
185
+ /**
186
+ *
187
+ * @param route
188
+ */
189
+ addRoute(route: new () => SocketRoute);
190
+ }
191
+
192
+ /**
193
+ * HTTP server class.
194
+ */
195
+ export class HttpServer {
196
+ constructor(options?: RedWebOptions);
197
+ }
198
+
199
+ /**
200
+ * HTTPS server class.
201
+ */
202
+ export class HttpsServer {
203
+ constructor(options?: RedWebOptions);
204
+ }
205
+
206
+ /**
207
+ * WebSocket server class.
208
+ */
209
+ export class SocketServer extends BaseSocketServer {
210
+ constructor(options?: SocketServerOptions);
211
+ }
212
+
213
+ /**
214
+ * Secure WebSocket server class.
215
+ */
216
+ export class SecureSocketServer extends BaseSocketServer {
217
+ constructor(options?: SocketServerOptions);
218
+ }
219
+
220
+ /**
221
+ * SSL configuration loader.
222
+ */
223
+ export function loadSslConfig(sslOptions: { key: string; cert: string }): { key: string; cert: string };
224
+
225
+ /**
226
+ * Constants for encoding types.
227
+ */
228
+ export const ENCODINGS: {
229
+ json: 'json';
230
+ urlencoded: 'urlencoded';
231
+ };
232
+
233
+ /**
234
+ * Constants for HTTP methods.
235
+ */
236
+ export const METHODS: {
237
+ POST: 'post';
238
+ GET: 'get';
239
+ PUT: 'put';
240
+ DELETE: 'delete';
241
+ PATCH: 'patch';
242
+ OPTIONS: 'options';
243
+ HEAD: 'head';
244
+ };
245
+
246
+ /**
247
+ * Default HTTP options.
248
+ */
249
+ export const HTTP_OPTIONS: RedWebOptions;
250
+
251
+ /**
252
+ * Default socket options.
253
+ */
254
+ export const SOCKET_OPTIONS: SocketServerOptions;
255
+ }
package/index.js CHANGED
@@ -1,12 +1,13 @@
1
- const { HttpServer, HttpsServer, METHODS } = require('./src/http');
2
- const { SocketServer, SecureSocketServer, SOCKET_OPTIONS } = require('./src/ws');
3
- const { BaseHandler } = require('./src/ws/BaseHandler');
4
- module.exports = {
5
- SocketServer,
6
- SecureSocketServer,
7
- BaseHandler,
8
- HttpServer,
9
- HttpsServer,
10
- SOCKET_OPTIONS,
11
- METHODS
12
- };
1
+ const { METHODS } = require('./src/http');
2
+ const { sendJson } = require('./src/ws/util');
3
+ const { SocketServer, SecureSocketServer, SOCKET_OPTIONS, SocketRoute } = require('./src/ws');
4
+ const { BaseHandler } = require('./src/ws/BaseHandler');
5
+ module.exports = {
6
+ SocketServer,
7
+ SecureSocketServer,
8
+ BaseHandler,
9
+ SocketRoute,
10
+ sendJson,
11
+ SOCKET_OPTIONS,
12
+ METHODS
13
+ };
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
- {
2
- "name": "redweb",
3
- "version": "0.6.2",
4
- "description": "A way to quickly set up an express server",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node tester.js"
8
- },
9
- "files": [
10
- "src/*",
11
- "index.d.ts"
12
- ],
13
- "keywords": [],
14
- "author": "",
15
- "license": "ISC",
16
- "dependencies": {
17
- "cors": "^2.8.5",
18
- "express": "^4.19.2",
19
- "ws": "^8.17.0"
20
- },
21
- "devDependencies": {
22
- "@types/express": "^4.17.21",
23
- "@types/jest": "^29.5.12",
24
- "jest": "^29.7.0",
25
- "supertest": "^7.0.0"
26
- }
27
- }
1
+ {
2
+ "name": "redweb",
3
+ "version": "0.6.5",
4
+ "description": "A way to quickly set up an express server",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node tester.js"
8
+ },
9
+ "files": [
10
+ "src/*",
11
+ "index.d.ts"
12
+ ],
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "dependencies": {
17
+ "cors": "^2.8.5",
18
+ "express": "^4.19.2",
19
+ "ws": "^8.17.0"
20
+ },
21
+ "devDependencies": {
22
+ "@types/express": "^4.17.21",
23
+ "@types/jest": "^29.5.12",
24
+ "jest": "^29.7.0",
25
+ "supertest": "^7.0.0"
26
+ }
27
+ }