vortez 5.0.0-dev.18 → 5.0.0-dev.19
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/.gitignore +9 -4
- package/README.md +681 -176
- package/build/Vortez.d.ts +1 -0
- package/build/Vortez.js +1 -0
- package/build/Vortez.js.map +1 -1
- package/build/server/Response.d.ts +1 -1
- package/build/server/Response.js +1 -1
- package/build/server/Response.js.map +1 -1
- package/build/server/Server.d.ts +4 -4
- package/build/server/Server.js +5 -5
- package/build/server/Server.js.map +1 -1
- package/build/server/ServerDebug.d.ts +10 -1
- package/build/server/ServerDebug.js +85 -17
- package/build/server/ServerDebug.js.map +1 -1
- package/build/server/config/Config.d.ts +274 -47
- package/build/server/config/Config.js +68 -47
- package/build/server/config/Config.js.map +1 -1
- package/build/server/config/{ConfigLoader.d.ts → Loader.d.ts} +4 -5
- package/build/server/config/{ConfigLoader.js → Loader.js} +7 -10
- package/build/server/config/Loader.js.map +1 -0
- package/build/server/router/Router.d.ts +87 -30
- package/build/server/router/Router.js +110 -48
- package/build/server/router/Router.js.map +1 -1
- package/build/server/router/algorithm/Algorithm.d.ts +39 -0
- package/build/server/router/algorithm/Algorithm.js +20 -0
- package/build/server/router/algorithm/Algorithm.js.map +1 -0
- package/build/server/router/algorithm/FIFO.d.ts +15 -0
- package/build/server/router/algorithm/FIFO.js +24 -0
- package/build/server/router/algorithm/FIFO.js.map +1 -0
- package/build/server/router/algorithm/Tree.d.ts +38 -0
- package/build/server/router/algorithm/Tree.js +126 -0
- package/build/server/router/algorithm/Tree.js.map +1 -0
- package/build/server/router/middleware/WsMiddleware.js +1 -1
- package/build/server/router/middleware/WsMiddleware.js.map +1 -1
- package/build/utilities/Flatten.d.ts +56 -0
- package/build/utilities/Flatten.js +59 -0
- package/build/utilities/Flatten.js.map +1 -0
- package/build/utilities/Utilities.d.ts +7 -58
- package/build/utilities/Utilities.js +8 -33
- package/build/utilities/Utilities.js.map +1 -1
- package/build/utilities/schema/Introspection.d.ts +24 -0
- package/build/utilities/schema/Introspection.js +87 -0
- package/build/utilities/schema/Introspection.js.map +1 -0
- package/build/utilities/schema/JSONSchema.d.ts +68 -0
- package/build/utilities/schema/JSONSchema.js +13 -0
- package/build/utilities/schema/JSONSchema.js.map +1 -0
- package/build/utilities/schema/Schema.d.ts +253 -0
- package/build/utilities/schema/Schema.js +241 -0
- package/build/utilities/schema/Schema.js.map +1 -0
- package/build/utilities/schema/SchemaError.d.ts +10 -0
- package/build/utilities/schema/SchemaError.js +13 -0
- package/build/utilities/schema/SchemaError.js.map +1 -0
- package/build/utilities/schema/Validator.d.ts +94 -0
- package/build/utilities/schema/Validator.js +246 -0
- package/build/utilities/schema/Validator.js.map +1 -0
- package/package.json +1 -1
- package/tests/config/config.js +233 -0
- package/tests/router.js +596 -0
- package/tests/schema/schema.js +368 -0
- package/tests/test.env +0 -0
- package/tests/test.js +3 -3
- package/build/server/config/ConfigLoader.js.map +0 -1
- package/build/server/config/ConfigValidator.d.ts +0 -71
- package/build/server/config/ConfigValidator.js +0 -131
- package/build/server/config/ConfigValidator.js.map +0 -1
- package/examples/in-docs.js +0 -96
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author NetFeez <netfeez.dev@gmail.com>
|
|
3
|
-
* @description
|
|
3
|
+
* @description Router v2 proposal: strategy-based routing (FIFO/Tree) with backward-compatible API.
|
|
4
4
|
* @license Apache-2.0
|
|
5
|
-
*/
|
|
5
|
+
*/
|
|
6
6
|
import HTTP from 'http';
|
|
7
7
|
import { Duplex } from 'stream';
|
|
8
8
|
import _Rule from './Rule.js';
|
|
@@ -11,6 +11,9 @@ import _HttpRule from './HttpRule.js';
|
|
|
11
11
|
import _WsMiddleware from './middleware/WsMiddleware.js';
|
|
12
12
|
import _HttpMiddleware from './middleware/HttpMiddleware.js';
|
|
13
13
|
import _Middleware from './middleware/Middleware.js';
|
|
14
|
+
import _Algorithm from './algorithm/Algorithm.js';
|
|
15
|
+
import _FIFO from './algorithm/FIFO.js';
|
|
16
|
+
import _Tree from './algorithm/Tree.js';
|
|
14
17
|
import Request from '../Request.js';
|
|
15
18
|
import Response from '../Response.js';
|
|
16
19
|
import Websocket from '../websocket/Websocket.js';
|
|
@@ -20,64 +23,92 @@ export { WsRule } from './WsRule.js';
|
|
|
20
23
|
export { HttpRule } from './HttpRule.js';
|
|
21
24
|
export declare class Router {
|
|
22
25
|
config: Config;
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
static AlgorithmMap: Router.AlgorithmMap;
|
|
27
|
+
readonly algorithm: Router.Algorithm;
|
|
25
28
|
readonly httpMiddleware: Router.HttpMiddleware;
|
|
26
29
|
readonly wsMiddleware: Router.WsMiddleware;
|
|
27
30
|
/**
|
|
28
31
|
* Creates a router for rule management.
|
|
29
32
|
* @param config - The server configuration.
|
|
33
|
+
* @param rules - An optional array of routing rules to initialize the router with.
|
|
34
|
+
* @param options - Additional options for configuring the router, including middleware and algorithm choice.
|
|
30
35
|
*/
|
|
31
|
-
constructor(config?: Config, rules?: Router.rules[],
|
|
36
|
+
constructor(config?: Config, rules?: Router.rules[], options?: Router.Options);
|
|
37
|
+
/** HTTP rules view (computed from algorithm). */
|
|
38
|
+
get httpRules(): Router.HttpRule[];
|
|
39
|
+
/** WebSocket rules view (computed from algorithm). */
|
|
40
|
+
get wsRules(): Router.WsRule[];
|
|
32
41
|
/**
|
|
33
42
|
* Adds middleware actions to the router.
|
|
34
43
|
* @param middleware - The middleware to be added.
|
|
44
|
+
* @remarks Applies to rules added after this call.
|
|
35
45
|
*/
|
|
36
46
|
use(middleware: Router.HttpMiddleware | Router.WsMiddleware): this;
|
|
37
47
|
/**
|
|
38
48
|
* Adds middleware error actions to the router.
|
|
39
49
|
* @param middleware - The middleware to be added.
|
|
50
|
+
* @remarks Applies to rules added after this call.
|
|
40
51
|
*/
|
|
41
52
|
useError(middleware: Router.HttpMiddleware | Router.WsMiddleware): this;
|
|
42
53
|
/**
|
|
43
54
|
* Triggered when the server receives an HTTP request.
|
|
44
|
-
* @param
|
|
45
|
-
* @param
|
|
55
|
+
* @param request - The received HTTP request.
|
|
56
|
+
* @param response - The server response handler.
|
|
57
|
+
* @returns True if the request was routed, false otherwise.
|
|
58
|
+
* @remarks This method attempts to route the incoming HTTP request using the routing algorithm.
|
|
59
|
+
* If a matching route is found, it processes the request and returns true; otherwise, it returns false, indicating that no suitable route was found for the request.
|
|
46
60
|
*/
|
|
47
61
|
requestManager(HttpRequest: HTTP.IncomingMessage, HttpResponse: HTTP.ServerResponse): void;
|
|
48
62
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @param
|
|
51
|
-
* @param
|
|
63
|
+
* Triggered when the server receives a WebSocket upgrade request.
|
|
64
|
+
* @param request - The received WebSocket upgrade request.
|
|
65
|
+
* @param webSocket - The WebSocket connection handler.
|
|
66
|
+
* @returns True if the request was routed, false otherwise.
|
|
67
|
+
* @remarks This method attempts to route the incoming WebSocket upgrade request using the routing algorithm.
|
|
68
|
+
* If a matching route is found, it processes the request and returns true; otherwise, it returns false, indicating that no suitable route was found for the request.
|
|
52
69
|
*/
|
|
53
70
|
upgradeManager(HttpRequest: HTTP.IncomingMessage, Socket: Duplex): void;
|
|
54
71
|
/**
|
|
55
72
|
* Routes incoming HTTP requests to be processed.
|
|
56
73
|
* @param request - The received HTTP request.
|
|
57
74
|
* @param response - The server response handler.
|
|
75
|
+
* @returns True if the request was routed, false otherwise.
|
|
76
|
+
* @remarks This method attempts to route the incoming HTTP request using the routing algorithm.
|
|
77
|
+
* If a matching route is found, it processes the request and returns true; otherwise, it returns false, indicating that no suitable route was found for the request.
|
|
58
78
|
*/
|
|
59
|
-
routeRequest(request: Request, response: Response):
|
|
79
|
+
routeRequest(request: Request, response: Response): boolean;
|
|
60
80
|
/**
|
|
61
|
-
* Routes WebSocket
|
|
62
|
-
* @param request - The received
|
|
63
|
-
* @param webSocket - The WebSocket connection
|
|
81
|
+
* Routes incoming WebSocket upgrade requests to be processed.
|
|
82
|
+
* @param request - The received WebSocket upgrade request.
|
|
83
|
+
* @param webSocket - The WebSocket connection handler.
|
|
84
|
+
* @returns True if the request was routed, false otherwise.
|
|
85
|
+
* @remarks This method attempts to route the incoming WebSocket upgrade request using the routing algorithm.
|
|
86
|
+
* If a matching route is found, it processes the request and returns true; otherwise, it returns false, indicating that no suitable route was found for the request.
|
|
64
87
|
*/
|
|
65
|
-
routeWebSocket(request: Request, webSocket: Websocket):
|
|
88
|
+
routeWebSocket(request: Request, webSocket: Websocket): boolean;
|
|
66
89
|
/**
|
|
67
|
-
* Adds
|
|
68
|
-
* @param rules -
|
|
90
|
+
* Adds multiple routing rules to the server.
|
|
91
|
+
* @param rules - An array of rules to be added.
|
|
92
|
+
* @returns The current router instance for chaining.
|
|
93
|
+
* @remarks This method is a convenience function that allows adding multiple rules at once by internally calling the addRule method for each rule in the array.
|
|
69
94
|
*/
|
|
70
95
|
addRules(...rules: Router.rules[]): this;
|
|
71
96
|
/**
|
|
72
|
-
* Adds
|
|
73
|
-
* @param rule - The rule
|
|
97
|
+
* Adds a routing rule to the server.
|
|
98
|
+
* @param rule - The rule to be added.
|
|
99
|
+
* @returns The current router instance for chaining.
|
|
100
|
+
* @remarks This method integrates the new rule into the routing algorithm and ensures that any associated middleware is properly merged.
|
|
101
|
+
* Middleware is snapshotted at registration time; later global middleware additions do not mutate this rule.
|
|
74
102
|
*/
|
|
75
103
|
addRule(rule: Router.rules): this;
|
|
76
104
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* @param
|
|
80
|
-
* @
|
|
105
|
+
* Mounts another router onto this router, optionally under a specific URL path.
|
|
106
|
+
* @param router - The router to be mounted.
|
|
107
|
+
* @param urlRule - An optional URL path to mount the router under. If not provided, the router's rules will be added at the root level.
|
|
108
|
+
* @returns The current router instance for chaining.
|
|
109
|
+
* @remarks This method allows you to compose routers together, enabling modular route management.
|
|
110
|
+
* When a URL path is specified, all routes from the mounted router will be prefixed with that path.
|
|
111
|
+
* Mounted rules keep their existing middleware order and then receive this router middleware at start through addRule().
|
|
81
112
|
*/
|
|
82
113
|
mount(router: Router, urlRule?: string): this;
|
|
83
114
|
/**
|
|
@@ -96,20 +127,37 @@ export declare class Router {
|
|
|
96
127
|
/**
|
|
97
128
|
* Adds a folder routing rule.
|
|
98
129
|
* @param urlRule - The URL path to listen on.
|
|
99
|
-
* @param source - The
|
|
130
|
+
* @param source - The path to the folder to be served.
|
|
100
131
|
*/
|
|
101
|
-
addFolder(urlRule: string, source: string):
|
|
132
|
+
addFolder(urlRule: string, source: string): this;
|
|
102
133
|
/**
|
|
103
134
|
* Adds a WebSocket routing rule.
|
|
104
135
|
* @param urlRule - The URL path to listen on.
|
|
105
|
-
* @param action - The action to be executed on
|
|
136
|
+
* @param action - The action to be executed when a WebSocket connection is established on the specified URL path.
|
|
137
|
+
* @returns The created WebSocket rule.
|
|
106
138
|
*/
|
|
107
|
-
|
|
139
|
+
addWebsocket(urlRule: string, action: Router.WsRule.action): Router.WsRule;
|
|
108
140
|
/**
|
|
109
|
-
* Creates a new router.
|
|
110
|
-
* @returns A new
|
|
141
|
+
* Creates a new router instance with the same configuration and middleware but without any rules.
|
|
142
|
+
* @returns A new RouterV2 instance.
|
|
143
|
+
* @remarks This method is useful for creating sub-routers that share the same configuration and middleware but have different routing rules.
|
|
111
144
|
*/
|
|
112
145
|
createRouter(): Router;
|
|
146
|
+
/**
|
|
147
|
+
* Gets an algorithm instance based on the provided input, which can be either a string key or an instance of the algorithm.
|
|
148
|
+
* @param algorithm - The algorithm to retrieve, either as a string key or an instance.
|
|
149
|
+
* @returns An instance of the requested algorithm.
|
|
150
|
+
* @throws Will throw an error if the algorithm key is not found in the AlgorithmMap.
|
|
151
|
+
* @remarks If the algorithm is provided as a string and is not found in the AlgorithmMap,it defaults to FIFO and logs a warning.
|
|
152
|
+
* @example
|
|
153
|
+
* // Using a string key to get an algorithm instance
|
|
154
|
+
* const fifoAlgorithm = RouterV2.getAlgorithm('FIFO');
|
|
155
|
+
*
|
|
156
|
+
* // Using an instance directly
|
|
157
|
+
* const treeAlgorithm = new RouterV2.Tree();
|
|
158
|
+
* const retrievedTreeAlgorithm = RouterV2.getAlgorithm(treeAlgorithm);
|
|
159
|
+
*/
|
|
160
|
+
static getAlgorithm(algorithm: keyof Router.AlgorithmMap | Router.Algorithm): Router.Algorithm;
|
|
113
161
|
}
|
|
114
162
|
export declare namespace Router {
|
|
115
163
|
export import Rule = _Rule;
|
|
@@ -118,9 +166,18 @@ export declare namespace Router {
|
|
|
118
166
|
export import WsMiddleware = _WsMiddleware;
|
|
119
167
|
export import HttpMiddleware = _HttpMiddleware;
|
|
120
168
|
export import Middleware = _Middleware;
|
|
121
|
-
|
|
169
|
+
export import Algorithm = _Algorithm;
|
|
170
|
+
export import FIFO = _FIFO;
|
|
171
|
+
export import Tree = _Tree;
|
|
172
|
+
type algorithmName = keyof Router.AlgorithmMap;
|
|
173
|
+
interface AlgorithmMap {
|
|
174
|
+
FIFO: typeof FIFO;
|
|
175
|
+
Tree: typeof Tree;
|
|
176
|
+
}
|
|
177
|
+
interface Options {
|
|
122
178
|
http?: HttpMiddleware;
|
|
123
179
|
ws?: WsMiddleware;
|
|
180
|
+
algorithm?: algorithmName | Algorithm;
|
|
124
181
|
}
|
|
125
182
|
type rules = WsRule | HttpRule;
|
|
126
183
|
}
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author NetFeez <netfeez.dev@gmail.com>
|
|
3
|
-
* @description
|
|
3
|
+
* @description Router v2 proposal: strategy-based routing (FIFO/Tree) with backward-compatible API.
|
|
4
4
|
* @license Apache-2.0
|
|
5
|
-
*/
|
|
5
|
+
*/
|
|
6
6
|
import _Rule from './Rule.js';
|
|
7
7
|
import _WsRule from './WsRule.js';
|
|
8
8
|
import _HttpRule from './HttpRule.js';
|
|
9
9
|
import _WsMiddleware from './middleware/WsMiddleware.js';
|
|
10
10
|
import _HttpMiddleware from './middleware/HttpMiddleware.js';
|
|
11
11
|
import _Middleware from './middleware/Middleware.js';
|
|
12
|
+
import _Algorithm from './algorithm/Algorithm.js';
|
|
13
|
+
import _FIFO from './algorithm/FIFO.js';
|
|
14
|
+
import _Tree from './algorithm/Tree.js';
|
|
12
15
|
import Request from '../Request.js';
|
|
13
16
|
import Response from '../Response.js';
|
|
14
17
|
import Websocket from '../websocket/Websocket.js';
|
|
@@ -20,24 +23,39 @@ export { HttpRule } from './HttpRule.js';
|
|
|
20
23
|
const logger = LoggerManager.getInstance();
|
|
21
24
|
export class Router {
|
|
22
25
|
config;
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
static AlgorithmMap = {
|
|
27
|
+
FIFO: _FIFO,
|
|
28
|
+
Tree: _Tree
|
|
29
|
+
};
|
|
30
|
+
algorithm;
|
|
25
31
|
httpMiddleware;
|
|
26
32
|
wsMiddleware;
|
|
27
33
|
/**
|
|
28
34
|
* Creates a router for rule management.
|
|
29
35
|
* @param config - The server configuration.
|
|
36
|
+
* @param rules - An optional array of routing rules to initialize the router with.
|
|
37
|
+
* @param options - Additional options for configuring the router, including middleware and algorithm choice.
|
|
30
38
|
*/
|
|
31
|
-
constructor(config = new Config(), rules = [],
|
|
39
|
+
constructor(config = new Config({}), rules = [], options = {}) {
|
|
32
40
|
this.config = config;
|
|
33
|
-
const { http: httpMiddleware = new Router.HttpMiddleware(), ws: wsMiddleware = new Router.WsMiddleware() } =
|
|
41
|
+
const { http: httpMiddleware = new Router.HttpMiddleware(), ws: wsMiddleware = new Router.WsMiddleware(), algorithm = 'FIFO', } = options;
|
|
34
42
|
this.httpMiddleware = httpMiddleware;
|
|
35
43
|
this.wsMiddleware = wsMiddleware;
|
|
44
|
+
this.algorithm = Router.getAlgorithm(algorithm);
|
|
36
45
|
this.addRules(...rules);
|
|
37
46
|
}
|
|
47
|
+
/** HTTP rules view (computed from algorithm). */
|
|
48
|
+
get httpRules() {
|
|
49
|
+
return this.algorithm.allRules.filter((rule) => rule instanceof Router.HttpRule);
|
|
50
|
+
}
|
|
51
|
+
/** WebSocket rules view (computed from algorithm). */
|
|
52
|
+
get wsRules() {
|
|
53
|
+
return this.algorithm.allRules.filter((rule) => rule instanceof Router.WsRule);
|
|
54
|
+
}
|
|
38
55
|
/**
|
|
39
56
|
* Adds middleware actions to the router.
|
|
40
57
|
* @param middleware - The middleware to be added.
|
|
58
|
+
* @remarks Applies to rules added after this call.
|
|
41
59
|
*/
|
|
42
60
|
use(middleware) {
|
|
43
61
|
if (middleware instanceof Router.HttpMiddleware)
|
|
@@ -49,6 +67,7 @@ export class Router {
|
|
|
49
67
|
/**
|
|
50
68
|
* Adds middleware error actions to the router.
|
|
51
69
|
* @param middleware - The middleware to be added.
|
|
70
|
+
* @remarks Applies to rules added after this call.
|
|
52
71
|
*/
|
|
53
72
|
useError(middleware) {
|
|
54
73
|
if (middleware instanceof Router.HttpMiddleware)
|
|
@@ -59,23 +78,28 @@ export class Router {
|
|
|
59
78
|
}
|
|
60
79
|
/**
|
|
61
80
|
* Triggered when the server receives an HTTP request.
|
|
62
|
-
* @param
|
|
63
|
-
* @param
|
|
81
|
+
* @param request - The received HTTP request.
|
|
82
|
+
* @param response - The server response handler.
|
|
83
|
+
* @returns True if the request was routed, false otherwise.
|
|
84
|
+
* @remarks This method attempts to route the incoming HTTP request using the routing algorithm.
|
|
85
|
+
* If a matching route is found, it processes the request and returns true; otherwise, it returns false, indicating that no suitable route was found for the request.
|
|
64
86
|
*/
|
|
65
87
|
requestManager(HttpRequest, HttpResponse) {
|
|
66
88
|
const request = new Request(HttpRequest);
|
|
67
|
-
const response = new Response(request, HttpResponse, this.config.templates);
|
|
89
|
+
const response = new Response(request, HttpResponse, this.config.data.templates);
|
|
68
90
|
const sessionID = request.cookies.get('Session');
|
|
69
91
|
logger.request.log(request.ip, request.method, request.url, sessionID);
|
|
70
92
|
const isRouted = this.routeRequest(request, response);
|
|
71
93
|
if (!isRouted)
|
|
72
94
|
response.sendError(400, `No router for: ${request.method} -> ${request.url}`);
|
|
73
95
|
}
|
|
74
|
-
;
|
|
75
96
|
/**
|
|
76
|
-
*
|
|
77
|
-
* @param
|
|
78
|
-
* @param
|
|
97
|
+
* Triggered when the server receives a WebSocket upgrade request.
|
|
98
|
+
* @param request - The received WebSocket upgrade request.
|
|
99
|
+
* @param webSocket - The WebSocket connection handler.
|
|
100
|
+
* @returns True if the request was routed, false otherwise.
|
|
101
|
+
* @remarks This method attempts to route the incoming WebSocket upgrade request using the routing algorithm.
|
|
102
|
+
* If a matching route is found, it processes the request and returns true; otherwise, it returns false, indicating that no suitable route was found for the request.
|
|
79
103
|
*/
|
|
80
104
|
upgradeManager(HttpRequest, Socket) {
|
|
81
105
|
const request = new Request(HttpRequest);
|
|
@@ -90,29 +114,29 @@ export class Router {
|
|
|
90
114
|
* Routes incoming HTTP requests to be processed.
|
|
91
115
|
* @param request - The received HTTP request.
|
|
92
116
|
* @param response - The server response handler.
|
|
117
|
+
* @returns True if the request was routed, false otherwise.
|
|
118
|
+
* @remarks This method attempts to route the incoming HTTP request using the routing algorithm.
|
|
119
|
+
* If a matching route is found, it processes the request and returns true; otherwise, it returns false, indicating that no suitable route was found for the request.
|
|
93
120
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (!rule)
|
|
97
|
-
return false;
|
|
98
|
-
rule.exec(request, response);
|
|
99
|
-
return true;
|
|
121
|
+
routeRequest(request, response) {
|
|
122
|
+
return this.algorithm.route(request, response);
|
|
100
123
|
}
|
|
101
124
|
/**
|
|
102
|
-
* Routes WebSocket
|
|
103
|
-
* @param request - The received
|
|
104
|
-
* @param webSocket - The WebSocket connection
|
|
125
|
+
* Routes incoming WebSocket upgrade requests to be processed.
|
|
126
|
+
* @param request - The received WebSocket upgrade request.
|
|
127
|
+
* @param webSocket - The WebSocket connection handler.
|
|
128
|
+
* @returns True if the request was routed, false otherwise.
|
|
129
|
+
* @remarks This method attempts to route the incoming WebSocket upgrade request using the routing algorithm.
|
|
130
|
+
* If a matching route is found, it processes the request and returns true; otherwise, it returns false, indicating that no suitable route was found for the request.
|
|
105
131
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (!rule)
|
|
109
|
-
return false;
|
|
110
|
-
rule.exec(request, webSocket);
|
|
111
|
-
return true;
|
|
132
|
+
routeWebSocket(request, webSocket) {
|
|
133
|
+
return this.algorithm.route(request, webSocket);
|
|
112
134
|
}
|
|
113
135
|
/**
|
|
114
|
-
* Adds
|
|
115
|
-
* @param rules -
|
|
136
|
+
* Adds multiple routing rules to the server.
|
|
137
|
+
* @param rules - An array of rules to be added.
|
|
138
|
+
* @returns The current router instance for chaining.
|
|
139
|
+
* @remarks This method is a convenience function that allows adding multiple rules at once by internally calling the addRule method for each rule in the array.
|
|
116
140
|
*/
|
|
117
141
|
addRules(...rules) {
|
|
118
142
|
for (const rule of rules)
|
|
@@ -120,25 +144,28 @@ export class Router {
|
|
|
120
144
|
return this;
|
|
121
145
|
}
|
|
122
146
|
/**
|
|
123
|
-
* Adds
|
|
124
|
-
* @param rule - The rule
|
|
147
|
+
* Adds a routing rule to the server.
|
|
148
|
+
* @param rule - The rule to be added.
|
|
149
|
+
* @returns The current router instance for chaining.
|
|
150
|
+
* @remarks This method integrates the new rule into the routing algorithm and ensures that any associated middleware is properly merged.
|
|
151
|
+
* Middleware is snapshotted at registration time; later global middleware additions do not mutate this rule.
|
|
125
152
|
*/
|
|
126
153
|
addRule(rule) {
|
|
127
|
-
if (rule instanceof Router.WsRule)
|
|
154
|
+
if (rule instanceof Router.WsRule)
|
|
128
155
|
rule.middleware.mergeAtStart(this.wsMiddleware);
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
156
|
+
else
|
|
132
157
|
rule.middleware.mergeAtStart(this.httpMiddleware);
|
|
133
|
-
|
|
134
|
-
}
|
|
158
|
+
this.algorithm.add(rule);
|
|
135
159
|
return this;
|
|
136
160
|
}
|
|
137
161
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* @param
|
|
141
|
-
* @
|
|
162
|
+
* Mounts another router onto this router, optionally under a specific URL path.
|
|
163
|
+
* @param router - The router to be mounted.
|
|
164
|
+
* @param urlRule - An optional URL path to mount the router under. If not provided, the router's rules will be added at the root level.
|
|
165
|
+
* @returns The current router instance for chaining.
|
|
166
|
+
* @remarks This method allows you to compose routers together, enabling modular route management.
|
|
167
|
+
* When a URL path is specified, all routes from the mounted router will be prefixed with that path.
|
|
168
|
+
* Mounted rules keep their existing middleware order and then receive this router middleware at start through addRule().
|
|
142
169
|
*/
|
|
143
170
|
mount(router, urlRule) {
|
|
144
171
|
const httpRules = router.httpRules.map((rule) => {
|
|
@@ -176,7 +203,7 @@ export class Router {
|
|
|
176
203
|
/**
|
|
177
204
|
* Adds a folder routing rule.
|
|
178
205
|
* @param urlRule - The URL path to listen on.
|
|
179
|
-
* @param source - The
|
|
206
|
+
* @param source - The path to the folder to be served.
|
|
180
207
|
*/
|
|
181
208
|
addFolder(urlRule, source) {
|
|
182
209
|
const rule = Router.HttpRule.folder(urlRule, source);
|
|
@@ -186,18 +213,50 @@ export class Router {
|
|
|
186
213
|
/**
|
|
187
214
|
* Adds a WebSocket routing rule.
|
|
188
215
|
* @param urlRule - The URL path to listen on.
|
|
189
|
-
* @param action - The action to be executed on
|
|
216
|
+
* @param action - The action to be executed when a WebSocket connection is established on the specified URL path.
|
|
217
|
+
* @returns The created WebSocket rule.
|
|
190
218
|
*/
|
|
191
|
-
|
|
219
|
+
addWebsocket(urlRule, action) {
|
|
192
220
|
const rule = new Router.WsRule(urlRule, action);
|
|
193
221
|
this.addRules(rule);
|
|
194
222
|
return rule;
|
|
195
223
|
}
|
|
196
224
|
/**
|
|
197
|
-
* Creates a new router.
|
|
198
|
-
* @returns A new
|
|
225
|
+
* Creates a new router instance with the same configuration and middleware but without any rules.
|
|
226
|
+
* @returns A new RouterV2 instance.
|
|
227
|
+
* @remarks This method is useful for creating sub-routers that share the same configuration and middleware but have different routing rules.
|
|
199
228
|
*/
|
|
200
|
-
createRouter() {
|
|
229
|
+
createRouter() {
|
|
230
|
+
const algorithm = this.algorithm instanceof Router.Tree ? new Router.Tree() : new Router.FIFO();
|
|
231
|
+
return new Router(this.config, [], {
|
|
232
|
+
http: this.httpMiddleware.clone(),
|
|
233
|
+
ws: this.wsMiddleware.clone(),
|
|
234
|
+
algorithm,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Gets an algorithm instance based on the provided input, which can be either a string key or an instance of the algorithm.
|
|
239
|
+
* @param algorithm - The algorithm to retrieve, either as a string key or an instance.
|
|
240
|
+
* @returns An instance of the requested algorithm.
|
|
241
|
+
* @throws Will throw an error if the algorithm key is not found in the AlgorithmMap.
|
|
242
|
+
* @remarks If the algorithm is provided as a string and is not found in the AlgorithmMap,it defaults to FIFO and logs a warning.
|
|
243
|
+
* @example
|
|
244
|
+
* // Using a string key to get an algorithm instance
|
|
245
|
+
* const fifoAlgorithm = RouterV2.getAlgorithm('FIFO');
|
|
246
|
+
*
|
|
247
|
+
* // Using an instance directly
|
|
248
|
+
* const treeAlgorithm = new RouterV2.Tree();
|
|
249
|
+
* const retrievedTreeAlgorithm = RouterV2.getAlgorithm(treeAlgorithm);
|
|
250
|
+
*/
|
|
251
|
+
static getAlgorithm(algorithm) {
|
|
252
|
+
if (algorithm instanceof Router.Algorithm)
|
|
253
|
+
return algorithm;
|
|
254
|
+
const AlgorithmClass = this.AlgorithmMap[algorithm];
|
|
255
|
+
if (AlgorithmClass)
|
|
256
|
+
return new AlgorithmClass();
|
|
257
|
+
logger.warn(`&C3Algorithm &C6${algorithm} &C3not found. Defaulting to &C6FIFO&C3.`);
|
|
258
|
+
return new Router.FIFO();
|
|
259
|
+
}
|
|
201
260
|
}
|
|
202
261
|
(function (Router) {
|
|
203
262
|
Router.Rule = _Rule;
|
|
@@ -206,6 +265,9 @@ export class Router {
|
|
|
206
265
|
Router.WsMiddleware = _WsMiddleware;
|
|
207
266
|
Router.HttpMiddleware = _HttpMiddleware;
|
|
208
267
|
Router.Middleware = _Middleware;
|
|
268
|
+
Router.Algorithm = _Algorithm;
|
|
269
|
+
Router.FIFO = _FIFO;
|
|
270
|
+
Router.Tree = _Tree;
|
|
209
271
|
})(Router || (Router = {}));
|
|
210
272
|
export default Router;
|
|
211
273
|
//# sourceMappingURL=Router.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.js","sourceRoot":"","sources":["../../../src/server/router/Router.ts"],"names":[],"mappings":"AAAA;;;;
|
|
1
|
+
{"version":3,"file":"Router.js","sourceRoot":"","sources":["../../../src/server/router/Router.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,OAAO,KAAK,MAAM,WAAW,CAAC;AAC9B,OAAO,OAAO,MAAM,aAAa,CAAC;AAClC,OAAO,SAAS,MAAM,eAAe,CAAC;AACtC,OAAO,aAAa,MAAM,8BAA8B,CAAC;AACzD,OAAO,eAAe,MAAM,gCAAgC,CAAC;AAC7D,OAAO,WAAW,MAAM,4BAA4B,CAAC;AAErD,OAAO,UAAU,MAAM,0BAA0B,CAAC;AAClD,OAAO,KAAK,MAAM,qBAAqB,CAAC;AACxC,OAAO,KAAK,MAAM,qBAAqB,CAAC;AAExC,OAAO,OAAO,MAAM,eAAe,CAAC;AACpC,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,SAAS,MAAM,2BAA2B,CAAC;AAClD,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAChD,OAAO,MAAM,MAAM,qBAAqB,CAAC;AAEzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,MAAM,MAAM,GAAG,aAAa,CAAC,WAAW,EAAE,CAAC;AAE3C,MAAM,OAAO,MAAM;IAiBV;IAhBD,MAAM,CAAC,YAAY,GAAwB;QACjD,IAAI,EAAE,KAA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AAC,IAAI,EAAE,CAAC;IAC1B,CAAC;;AAGF,WAAiB,MAAM;IACR,WAAI,GAAG,KAAK,CAAC;IACb,aAAM,GAAG,OAAO,CAAC;IACjB,eAAQ,GAAG,SAAS,CAAC;IACrB,mBAAY,GAAG,aAAa,CAAC;IAC7B,qBAAc,GAAG,eAAe,CAAC;IACjC,iBAAU,GAAG,WAAW,CAAC;IACzB,gBAAS,GAAG,UAAU,CAAC;IACvB,WAAI,GAAG,KAAK,CAAC;IACb,WAAI,GAAG,KAAK,CAAC;AAc5B,CAAC,EAvBgB,MAAM,KAAN,MAAM,QAuBtB;AAED,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type Request from '../../Request.js';
|
|
2
|
+
import Response from '../../Response.js';
|
|
3
|
+
import Websocket from '../../websocket/Websocket.js';
|
|
4
|
+
import HttpRule from '../HttpRule.js';
|
|
5
|
+
import WsRule from '../WsRule.js';
|
|
6
|
+
export declare abstract class Algorithm {
|
|
7
|
+
/** Get all rules in the routing algorithm. */
|
|
8
|
+
abstract get allRules(): Algorithm.ruleType[];
|
|
9
|
+
/**
|
|
10
|
+
* Add a rule to the routing algorithm.
|
|
11
|
+
* @param rule - The rule to add.
|
|
12
|
+
*/
|
|
13
|
+
abstract add(...rules: Algorithm.ruleType[]): Promise<void> | void;
|
|
14
|
+
/**
|
|
15
|
+
* Route a request to a rule.
|
|
16
|
+
* @param request - The request to route.
|
|
17
|
+
* @param client - The client to route the request to.
|
|
18
|
+
* @returns True if the request was routed, false otherwise.
|
|
19
|
+
*/
|
|
20
|
+
protected abstract routeHttp(request: Request, client: Response): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Route a request to a rule.
|
|
23
|
+
* @param request - The request to route.
|
|
24
|
+
* @param client - The client to route the request to.
|
|
25
|
+
* @returns True if the request was routed, false otherwise.
|
|
26
|
+
*/
|
|
27
|
+
protected abstract routeWebsocket(request: Request, client: Websocket): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Route a request to a rule.
|
|
30
|
+
* @param request - The request to route.
|
|
31
|
+
* @param client - The client to route the request to.
|
|
32
|
+
* @returns True if the request was routed, false otherwise.
|
|
33
|
+
*/
|
|
34
|
+
route(request: Request, client: Response | Websocket): boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare namespace Algorithm {
|
|
37
|
+
type ruleType = HttpRule | WsRule;
|
|
38
|
+
}
|
|
39
|
+
export default Algorithm;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import Response from '../../Response.js';
|
|
2
|
+
import Websocket from '../../websocket/Websocket.js';
|
|
3
|
+
export class Algorithm {
|
|
4
|
+
/**
|
|
5
|
+
* Route a request to a rule.
|
|
6
|
+
* @param request - The request to route.
|
|
7
|
+
* @param client - The client to route the request to.
|
|
8
|
+
* @returns True if the request was routed, false otherwise.
|
|
9
|
+
*/
|
|
10
|
+
route(request, client) {
|
|
11
|
+
if (client instanceof Response)
|
|
12
|
+
return this.routeHttp(request, client);
|
|
13
|
+
else if (client instanceof Websocket)
|
|
14
|
+
return this.routeWebsocket(request, client);
|
|
15
|
+
else
|
|
16
|
+
throw new Error('Invalid client type');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export default Algorithm;
|
|
20
|
+
//# sourceMappingURL=Algorithm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Algorithm.js","sourceRoot":"","sources":["../../../../src/server/router/algorithm/Algorithm.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,mBAAmB,CAAC;AACzC,OAAO,SAAS,MAAM,8BAA8B,CAAC;AAIrD,MAAM,OAAgB,SAAS;IAsB3B;;;;;OAKG;IACI,KAAK,CAAC,OAAgB,EAAE,MAA4B;QACvD,IAAI,MAAM,YAAY,QAAQ;YAAE,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;aAClE,IAAI,MAAM,YAAY,SAAS;YAAE,OAAO,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;;YAC7E,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAChD,CAAC;CACJ;AAID,eAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Algorithm from './Algorithm.js';
|
|
2
|
+
import type Request from '../../Request.js';
|
|
3
|
+
import Response from '../../Response.js';
|
|
4
|
+
import Websocket from '../../websocket/Websocket.js';
|
|
5
|
+
import HttpRule from '../HttpRule.js';
|
|
6
|
+
import WsRule from '../WsRule.js';
|
|
7
|
+
export declare class FIFO extends Algorithm {
|
|
8
|
+
protected rules: (HttpRule | WsRule)[];
|
|
9
|
+
get allRules(): (HttpRule | WsRule)[];
|
|
10
|
+
add(...rules: Algorithm.ruleType[]): Promise<void> | void;
|
|
11
|
+
protected routeHttp(request: Request, client: Response): boolean;
|
|
12
|
+
protected routeWebsocket(request: Request, client: Websocket): boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare namespace FIFO { }
|
|
15
|
+
export default FIFO;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import Algorithm from './Algorithm.js';
|
|
2
|
+
import HttpRule from '../HttpRule.js';
|
|
3
|
+
import WsRule from '../WsRule.js';
|
|
4
|
+
export class FIFO extends Algorithm {
|
|
5
|
+
rules = [];
|
|
6
|
+
get allRules() { return this.rules; }
|
|
7
|
+
add(...rules) { this.rules.push(...rules); }
|
|
8
|
+
routeHttp(request, client) {
|
|
9
|
+
const rule = this.rules.find((rule) => rule instanceof HttpRule && rule.test(request));
|
|
10
|
+
if (!rule)
|
|
11
|
+
return false;
|
|
12
|
+
rule.exec(request, client);
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
routeWebsocket(request, client) {
|
|
16
|
+
const rule = this.rules.find((rule) => rule instanceof WsRule && rule.test(request));
|
|
17
|
+
if (!rule)
|
|
18
|
+
return false;
|
|
19
|
+
rule.exec(request, client);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export default FIFO;
|
|
24
|
+
//# sourceMappingURL=FIFO.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FIFO.js","sourceRoot":"","sources":["../../../../src/server/router/algorithm/FIFO.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAKvC,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,MAAM,MAAM,cAAc,CAAC;AAElC,MAAM,OAAO,IAAK,SAAQ,SAAS;IACrB,KAAK,GAA0B,EAAE,CAAC;IAC5C,IAAW,QAAQ,KAAK,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAC5B,GAAG,CAAC,GAAG,KAA2B,IAA0B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IACrF,SAAS,CAAC,OAAgB,EAAE,MAAgB;QAC3D,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAoB,EAAE,CAAC,IAAI,YAAY,QAAQ,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QACzG,IAAI,CAAC,IAAI;YAAE,OAAO,KAAK,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC;IAChB,CAAC;IACkB,cAAc,CAAC,OAAgB,EAAE,MAAiB;QACjE,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAkB,EAAE,CAAC,IAAI,YAAY,MAAM,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;QACrG,IAAI,CAAC,IAAI;YAAE,OAAO,KAAK,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAED,eAAe,IAAI,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type Request from '../../Request.js';
|
|
2
|
+
import Algorithm from './Algorithm.js';
|
|
3
|
+
import Response from '../../Response.js';
|
|
4
|
+
import Websocket from '../../websocket/Websocket.js';
|
|
5
|
+
export declare class Tree extends Algorithm {
|
|
6
|
+
private root;
|
|
7
|
+
constructor();
|
|
8
|
+
get allRules(): Algorithm.ruleType[];
|
|
9
|
+
add(...rules: Algorithm.ruleType[]): void;
|
|
10
|
+
/**
|
|
11
|
+
* Route a request to a rule.
|
|
12
|
+
* @param request - The request to route.
|
|
13
|
+
* @param client - The client to route the request to.
|
|
14
|
+
* @returns True if the request was routed, false otherwise.
|
|
15
|
+
*/
|
|
16
|
+
protected routeHttp(request: Request, client: Response): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Route a websocket to a rule.
|
|
19
|
+
* @param request - The request to route.
|
|
20
|
+
* @param client - The client to route the request to.
|
|
21
|
+
* @returns True if the request was routed, false otherwise.
|
|
22
|
+
*/
|
|
23
|
+
protected routeWebsocket(request: Request, client: Websocket): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Navigate to a route node.
|
|
26
|
+
* @param request - The request to navigate to.
|
|
27
|
+
* @returns The route node or null if not found.
|
|
28
|
+
*/
|
|
29
|
+
private navigate;
|
|
30
|
+
/**
|
|
31
|
+
* Split a path into segments.
|
|
32
|
+
* @param path - The path to split.
|
|
33
|
+
* @returns An array of segments.
|
|
34
|
+
*/
|
|
35
|
+
private splitPath;
|
|
36
|
+
}
|
|
37
|
+
export declare namespace Tree { }
|
|
38
|
+
export default Tree;
|