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/LICENSE +20 -20
- package/README.md +63 -85
- package/index.d.ts +255 -199
- package/index.js +13 -12
- package/package.json +27 -27
- package/src/http/BaseHttpServer.js +69 -69
- package/src/http/HttpServer.js +14 -14
- package/src/http/HttpsServer.js +17 -17
- package/src/http/index.js +1 -1
- package/src/sslConfig.js +20 -20
- package/src/ws/BaseHandler.js +39 -93
- package/src/ws/BaseSocketServer.js +57 -203
- package/src/ws/DefaultHandler.js +14 -0
- package/src/ws/DefaultRoute.js +14 -0
- package/src/ws/SecureSocketServer.js +20 -20
- package/src/ws/SocketRoute.js +114 -0
- package/src/ws/SocketServer.js +18 -18
- package/src/ws/index.js +6 -5
- package/src/ws/util.js +9 -0
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
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
*
|
|
67
|
-
*/
|
|
68
|
-
export
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
*
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
*
|
|
95
|
-
* @param
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Handles
|
|
101
|
-
* @param socket - The WebSocket connection
|
|
102
|
-
* @param
|
|
103
|
-
*/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
*
|
|
108
|
-
* @param
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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 {
|
|
2
|
-
const {
|
|
3
|
-
const {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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.
|
|
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
|
+
}
|