redweb 0.6.2 → 0.6.4

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Arkam Mazrui
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Arkam Mazrui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/index.d.ts CHANGED
@@ -1,199 +1,205 @@
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
+ 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
+ * Method called when a WebSocket connection is closed. Can be overridden in subclasses.
101
+ * @param socket - The WebSocket connection being closed.
102
+ */
103
+ onClose(socket: WebSocket): void;
104
+
105
+ /**
106
+ * Handles an incoming message and routes it to the appropriate handler function.
107
+ * @param socket - The WebSocket connection that sent the message.
108
+ * @param message - The incoming message in JSON string format.
109
+ */
110
+ handleMessage(socket: WebSocket, message: string): void;
111
+
112
+ /**
113
+ * Broadcasts a message to all connections managed by this handler.
114
+ * @param message - The message to broadcast.
115
+ */
116
+ broadcast(message: object): void;
117
+ }
118
+
119
+ /**
120
+ * Base WebSocket server class.
121
+ */
122
+ export class BaseSocketServer {
123
+ /**
124
+ * Map of connected WebSocket clients by their IP address.
125
+ */
126
+ clients: Map<string, WebSocket>;
127
+
128
+ /**
129
+ * List of handler instances currently registered with the server.
130
+ */
131
+ handlers: BaseHandler[];
132
+
133
+ constructor(server: HTTPServer | HTTPSServer, options?: SocketServerOptions);
134
+
135
+ /**
136
+ * Adds a new handler to the WebSocket server.
137
+ * @param HandlerClass - A class extending `BaseHandler` to add to the server.
138
+ */
139
+ addHandler(HandlerClass: new () => BaseHandler): void;
140
+ }
141
+
142
+ /**
143
+ * HTTP server class.
144
+ */
145
+ export class HttpServer {
146
+ constructor(options?: RedWebOptions);
147
+ }
148
+
149
+ /**
150
+ * HTTPS server class.
151
+ */
152
+ export class HttpsServer {
153
+ constructor(options?: RedWebOptions);
154
+ }
155
+
156
+ /**
157
+ * WebSocket server class.
158
+ */
159
+ export class SocketServer extends BaseSocketServer {
160
+ constructor(options?: SocketServerOptions);
161
+ }
162
+
163
+ /**
164
+ * Secure WebSocket server class.
165
+ */
166
+ export class SecureSocketServer extends BaseSocketServer {
167
+ constructor(options?: SocketServerOptions);
168
+ }
169
+
170
+ /**
171
+ * SSL configuration loader.
172
+ */
173
+ export function loadSslConfig(sslOptions: { key: string; cert: string }): { key: string; cert: string };
174
+
175
+ /**
176
+ * Constants for encoding types.
177
+ */
178
+ export const ENCODINGS: {
179
+ json: 'json';
180
+ urlencoded: 'urlencoded';
181
+ };
182
+
183
+ /**
184
+ * Constants for HTTP methods.
185
+ */
186
+ export const METHODS: {
187
+ POST: 'post';
188
+ GET: 'get';
189
+ PUT: 'put';
190
+ DELETE: 'delete';
191
+ PATCH: 'patch';
192
+ OPTIONS: 'options';
193
+ HEAD: 'head';
194
+ };
195
+
196
+ /**
197
+ * Default HTTP options.
198
+ */
199
+ export const HTTP_OPTIONS: RedWebOptions;
200
+
201
+ /**
202
+ * Default socket options.
203
+ */
204
+ export const SOCKET_OPTIONS: SocketServerOptions;
205
+ }
package/index.js CHANGED
@@ -1,12 +1,12 @@
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 { 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
+ };
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.4",
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
+ }