vortez 4.1.1 → 5.0.0-dev.14
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 +4 -12
- package/README.md +235 -289
- package/build/Template.d.ts +16 -2
- package/build/Template.js +20 -8
- package/build/Template.js.map +1 -1
- package/build/Vortez.d.ts +8 -15
- package/build/Vortez.js +8 -16
- package/build/Vortez.js.map +1 -1
- package/build/beta/Beta.d.ts +8 -0
- package/build/beta/Beta.js +9 -0
- package/build/beta/Beta.js.map +1 -0
- package/build/{Beta → beta}/JwtManager.js +1 -1
- package/build/{Beta → beta}/JwtManager.js.map +1 -1
- package/build/{Beta → beta}/Mail.d.ts +1 -1
- package/build/{Beta → beta}/Mail.js +1 -1
- package/build/beta/Mail.js.map +1 -0
- package/build/logger/Debug.d.ts +174 -0
- package/build/logger/Debug.js +295 -0
- package/build/logger/Debug.js.map +1 -0
- package/build/logger/Logger.d.ts +38 -0
- package/build/logger/Logger.js +48 -0
- package/build/logger/Logger.js.map +1 -0
- package/build/{Server → server}/BodyParser.js.map +1 -1
- package/build/{Server → server}/Cookie.js.map +1 -1
- package/build/server/LoggerManager.d.ts +21 -0
- package/build/server/LoggerManager.js +30 -0
- package/build/server/LoggerManager.js.map +1 -0
- package/build/{Server → server}/Request.d.ts +4 -2
- package/build/{Server → server}/Request.js +2 -1
- package/build/server/Request.js.map +1 -0
- package/build/{Server → server}/Response.d.ts +4 -1
- package/build/{Server → server}/Response.js +24 -8
- package/build/server/Response.js.map +1 -0
- package/build/{Server → server}/Server.d.ts +20 -67
- package/build/server/Server.js +211 -0
- package/build/server/Server.js.map +1 -0
- package/build/server/ServerDebug.d.ts +17 -0
- package/build/server/ServerDebug.js +67 -0
- package/build/server/ServerDebug.js.map +1 -0
- package/build/server/ServerError.d.ts +18 -0
- package/build/server/ServerError.js +15 -0
- package/build/server/ServerError.js.map +1 -0
- package/build/{Server → server}/Session.d.ts +9 -13
- package/build/{Server → server}/Session.js +34 -35
- package/build/server/Session.js.map +1 -0
- package/build/{Config.d.ts → server/config/Config.d.ts} +19 -3
- package/build/server/config/Config.js +60 -0
- package/build/server/config/Config.js.map +1 -0
- package/build/server/config/ConfigLoader.d.ts +24 -0
- package/build/server/config/ConfigLoader.js +53 -0
- package/build/server/config/ConfigLoader.js.map +1 -0
- package/build/server/config/ConfigValidator.d.ts +71 -0
- package/build/server/config/ConfigValidator.js +131 -0
- package/build/server/config/ConfigValidator.js.map +1 -0
- package/build/server/router/HttpRule.d.ts +69 -0
- package/build/server/router/HttpRule.js +96 -0
- package/build/server/router/HttpRule.js.map +1 -0
- package/build/server/router/Router.d.ts +127 -0
- package/build/server/router/Router.js +211 -0
- package/build/server/router/Router.js.map +1 -0
- package/build/server/router/Rule.d.ts +71 -0
- package/build/server/router/Rule.js +113 -0
- package/build/server/router/Rule.js.map +1 -0
- package/build/server/router/WsRule.d.ts +25 -0
- package/build/server/router/WsRule.js +34 -0
- package/build/server/router/WsRule.js.map +1 -0
- package/build/server/router/middleware/HttpMiddleware.d.ts +35 -0
- package/build/server/router/middleware/HttpMiddleware.js +90 -0
- package/build/server/router/middleware/HttpMiddleware.js.map +1 -0
- package/build/server/router/middleware/Middleware.d.ts +77 -0
- package/build/server/router/middleware/Middleware.js +69 -0
- package/build/server/router/middleware/Middleware.js.map +1 -0
- package/build/server/router/middleware/WsMiddleware.d.ts +24 -0
- package/build/server/router/middleware/WsMiddleware.js +90 -0
- package/build/server/router/middleware/WsMiddleware.js.map +1 -0
- package/build/{Server/WebSocket → server/websocket}/Chunk.js.map +1 -1
- package/build/server/websocket/Websocket.d.ts +88 -0
- package/build/{Server/WebSocket/WebSocket.js → server/websocket/Websocket.js} +91 -53
- package/build/server/websocket/Websocket.js.map +1 -0
- package/build/utilities/ConsoleUI.d.ts +88 -0
- package/build/utilities/ConsoleUI.js +141 -0
- package/build/utilities/ConsoleUI.js.map +1 -0
- package/build/utilities/DebugUI.d.ts +66 -0
- package/build/utilities/DebugUI.js +98 -0
- package/build/utilities/DebugUI.js.map +1 -0
- package/build/utilities/Env.d.ts +81 -0
- package/build/{Utilities → utilities}/Env.js +47 -35
- package/build/utilities/Env.js.map +1 -0
- package/build/utilities/Path.d.ts +24 -0
- package/build/utilities/Path.js +36 -0
- package/build/{Utilities → utilities}/Path.js.map +1 -1
- package/build/{Utilities → utilities}/Utilities.d.ts +68 -55
- package/build/{Utilities → utilities}/Utilities.js +36 -19
- package/build/utilities/Utilities.js.map +1 -0
- package/changes.md +122 -88
- package/package.json +10 -13
- package/tests/debug.js +9 -3
- package/tests/test.js +50 -48
- package/tests/utilities.js +1 -1
- package/Notes.md +0 -12
- package/build/Beta/Mail.js.map +0 -1
- package/build/Config.js +0 -33
- package/build/Config.js.map +0 -1
- package/build/ConsoleUI.d.ts +0 -57
- package/build/ConsoleUI.js +0 -110
- package/build/ConsoleUI.js.map +0 -1
- package/build/Debug.d.ts +0 -154
- package/build/Debug.js +0 -256
- package/build/Debug.js.map +0 -1
- package/build/LoggerManager/Logger.d.ts +0 -23
- package/build/LoggerManager/Logger.js +0 -23
- package/build/LoggerManager/Logger.js.map +0 -1
- package/build/LoggerManager/LoggerManager.d.ts +0 -18
- package/build/LoggerManager/LoggerManager.js +0 -30
- package/build/LoggerManager/LoggerManager.js.map +0 -1
- package/build/Server/Request.js.map +0 -1
- package/build/Server/Response.js.map +0 -1
- package/build/Server/Rule.d.ts +0 -81
- package/build/Server/Rule.js +0 -146
- package/build/Server/Rule.js.map +0 -1
- package/build/Server/Server.js +0 -330
- package/build/Server/Server.js.map +0 -1
- package/build/Server/Session.js.map +0 -1
- package/build/Server/WebSocket/WebSocket.d.ts +0 -70
- package/build/Server/WebSocket/WebSocket.js.map +0 -1
- package/build/Utilities/Env.d.ts +0 -75
- package/build/Utilities/Env.js.map +0 -1
- package/build/Utilities/Path.d.ts +0 -18
- package/build/Utilities/Path.js +0 -27
- package/build/Utilities/Utilities.js.map +0 -1
- /package/build/{Beta → beta}/JwtManager.d.ts +0 -0
- /package/build/{Server → server}/BodyParser.d.ts +0 -0
- /package/build/{Server → server}/BodyParser.js +0 -0
- /package/build/{Server → server}/Cookie.d.ts +0 -0
- /package/build/{Server → server}/Cookie.js +0 -0
- /package/build/{Server/WebSocket → server/websocket}/Chunk.d.ts +0 -0
- /package/build/{Server/WebSocket → server/websocket}/Chunk.js +0 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description Manages the middleware pipeline for HttpRules.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import ServerError from '../../ServerError.js';
|
|
7
|
+
import LoggerManager from '../../LoggerManager.js';
|
|
8
|
+
import Middleware from './Middleware.js';
|
|
9
|
+
const logger = LoggerManager.getInstance();
|
|
10
|
+
export class HttpMiddleware extends Middleware {
|
|
11
|
+
clone() { return new HttpMiddleware(this.pipeline, this.errorPipeline); }
|
|
12
|
+
/**
|
|
13
|
+
* Runs the middleware pipeline.
|
|
14
|
+
* @param request - The request received by the server.
|
|
15
|
+
* @param response - The response to be sent by the server.
|
|
16
|
+
* @param action - The action to run.
|
|
17
|
+
* @param state - The state to pass to the action.
|
|
18
|
+
*/
|
|
19
|
+
async run(request, response, action, state = {}) {
|
|
20
|
+
try {
|
|
21
|
+
let index = 0;
|
|
22
|
+
const next = async (error) => {
|
|
23
|
+
if (error)
|
|
24
|
+
throw error;
|
|
25
|
+
if (response.isSended)
|
|
26
|
+
return void logger.warn('response was already sent when calling next()');
|
|
27
|
+
if (index >= this.pipeline.length)
|
|
28
|
+
return await action(request, response, state);
|
|
29
|
+
const current = this.pipeline[index++];
|
|
30
|
+
return await current(request, response, next, state);
|
|
31
|
+
};
|
|
32
|
+
await next();
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (this.errorPipeline.length === 0)
|
|
36
|
+
return this.errorHandler(error, request, response);
|
|
37
|
+
else
|
|
38
|
+
return this.runError(error, request, response, state);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Runs the error pipeline.
|
|
43
|
+
* @param error - The error to handle.
|
|
44
|
+
* @param request - The request received by the server.
|
|
45
|
+
* @param response - The response to be sent by the server.
|
|
46
|
+
* @param state - The state to pass to the action.
|
|
47
|
+
*/
|
|
48
|
+
async runError(error, request, response, state = {}) {
|
|
49
|
+
try {
|
|
50
|
+
let index = 0;
|
|
51
|
+
const next = async (caughtError) => {
|
|
52
|
+
if (caughtError)
|
|
53
|
+
throw caughtError;
|
|
54
|
+
if (index >= this.errorPipeline.length)
|
|
55
|
+
return await this.errorHandler(error, request, response);
|
|
56
|
+
const current = this.errorPipeline[index++];
|
|
57
|
+
return await current(error, request, response, next, state);
|
|
58
|
+
};
|
|
59
|
+
await next();
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
return this.errorHandler(error, request, response);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Handles errors.
|
|
67
|
+
* @param error - The error to handle.
|
|
68
|
+
*/
|
|
69
|
+
async errorHandler(error, request, response) {
|
|
70
|
+
if (error instanceof ServerError) {
|
|
71
|
+
if (response.isSended)
|
|
72
|
+
return void logger.warn('throw ApiError used when response was already sent');
|
|
73
|
+
return response.sendError(error.status, error.message);
|
|
74
|
+
}
|
|
75
|
+
else if (error instanceof Error) {
|
|
76
|
+
logger.error(error);
|
|
77
|
+
if (response.isSended)
|
|
78
|
+
return;
|
|
79
|
+
return response.sendError(500, error.message);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
logger.error(error);
|
|
83
|
+
if (response.isSended)
|
|
84
|
+
return;
|
|
85
|
+
return response.sendError(500, 'Internal Server Error');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export default HttpMiddleware;
|
|
90
|
+
//# sourceMappingURL=HttpMiddleware.js.map
|
|
@@ -0,0 +1 @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E,KAAK,CAAC,OAAO,CAAC,CAAC;QAC3D,CAAC;aAAM,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAChC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACpB,IAAI,QAAQ,CAAC,QAAQ;gBAAE,OAAO;YAC9B,OAAO,QAAQ,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;QAClD,CAAC;aAAM,CAAC;YACJ,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACpB,IAAI,QAAQ,CAAC,QAAQ;gBAAE,OAAO;YAC9B,OAAO,QAAQ,CAAC,SAAS,CAAC,GAAG,EAAE,uBAAuB,CAAC,CAAC;QAC5D,CAAC;IACL,CAAC;CACJ;AAED,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description Manages the middleware pipeline.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import Request from '../../Request.js';
|
|
7
|
+
import Response from '../../Response.js';
|
|
8
|
+
import Websocket from '../../websocket/Websocket.js';
|
|
9
|
+
import HttpRule from '../HttpRule.js';
|
|
10
|
+
import WsRule from '../WsRule.js';
|
|
11
|
+
export declare abstract class Middleware<Rule extends HttpRule | WsRule> {
|
|
12
|
+
protected readonly pipeline: Middleware.action<Rule>[];
|
|
13
|
+
protected readonly errorPipeline: Middleware.errorAction<Rule>[];
|
|
14
|
+
constructor(pipeline?: Middleware.action<Rule>[], errorPipeline?: Middleware.errorAction<Rule>[]);
|
|
15
|
+
/**
|
|
16
|
+
* Adds a new action to the middleware pipeline.
|
|
17
|
+
* @param action - The action to add.
|
|
18
|
+
*/
|
|
19
|
+
use(action: Middleware.action<Rule> | Middleware<Rule>): this;
|
|
20
|
+
/**
|
|
21
|
+
* Adds a action to the middleware error pipeline.
|
|
22
|
+
* @param action - The action to add.
|
|
23
|
+
*/
|
|
24
|
+
useError(action: Middleware.errorAction<Rule> | Middleware<Rule>): this;
|
|
25
|
+
/**
|
|
26
|
+
* Merges another middleware pipeline.
|
|
27
|
+
* @param middleware - The middleware to merge.
|
|
28
|
+
*/
|
|
29
|
+
merge(middleware: Middleware<Rule>): this;
|
|
30
|
+
/**
|
|
31
|
+
* Merges another middleware pipeline at the start.
|
|
32
|
+
* @param middleware - The middleware to merge.
|
|
33
|
+
*/
|
|
34
|
+
mergeAtStart(middleware: Middleware<Rule>): this;
|
|
35
|
+
/**
|
|
36
|
+
* Clones the middleware pipeline.
|
|
37
|
+
* @returns The cloned middleware pipeline.
|
|
38
|
+
*/
|
|
39
|
+
abstract clone(): Middleware<Rule>;
|
|
40
|
+
/**
|
|
41
|
+
* Runs the middleware pipeline.
|
|
42
|
+
* @param args - The arguments to pass to the action.
|
|
43
|
+
*/
|
|
44
|
+
abstract run(...args: any[]): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Runs the middleware error pipeline.
|
|
47
|
+
* @param args - The arguments to pass to the action.
|
|
48
|
+
*/
|
|
49
|
+
abstract runError(...args: any[]): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Gets the middleware names.
|
|
52
|
+
* @returns The middleware names.
|
|
53
|
+
*/
|
|
54
|
+
get middlewareNames(): string[];
|
|
55
|
+
/**
|
|
56
|
+
* Gets the error middleware names.
|
|
57
|
+
* @returns The error middleware names.
|
|
58
|
+
*/
|
|
59
|
+
get errorMiddlewareNames(): string[];
|
|
60
|
+
}
|
|
61
|
+
export declare namespace Middleware {
|
|
62
|
+
interface State {
|
|
63
|
+
[key: string]: any;
|
|
64
|
+
}
|
|
65
|
+
type next = (error?: unknown) => void;
|
|
66
|
+
namespace action {
|
|
67
|
+
type http = (request: Request, response: Response, next: next, state: State) => void | Promise<void>;
|
|
68
|
+
type ws = (request: Request, client: Websocket, next: next, state: State) => void | Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
namespace errorAction {
|
|
71
|
+
type http = (error: unknown, request: Request, response: Response, next: next, state: State) => void | Promise<void>;
|
|
72
|
+
type ws = (error: unknown, request: Request, client: Websocket, next: next, state: State) => void | Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
type action<Rule extends HttpRule | WsRule> = Rule extends WsRule ? action.ws : action.http;
|
|
75
|
+
type errorAction<Rule extends HttpRule | WsRule> = Rule extends WsRule ? errorAction.ws : errorAction.http;
|
|
76
|
+
}
|
|
77
|
+
export default Middleware;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description Manages the middleware pipeline.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export class Middleware {
|
|
7
|
+
pipeline;
|
|
8
|
+
errorPipeline;
|
|
9
|
+
constructor(pipeline = [], errorPipeline = []) {
|
|
10
|
+
this.pipeline = pipeline;
|
|
11
|
+
this.errorPipeline = errorPipeline;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Adds a new action to the middleware pipeline.
|
|
15
|
+
* @param action - The action to add.
|
|
16
|
+
*/
|
|
17
|
+
use(action) {
|
|
18
|
+
if (action instanceof Middleware)
|
|
19
|
+
this.pipeline.push(...action.pipeline);
|
|
20
|
+
else
|
|
21
|
+
this.pipeline.push(action);
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Adds a action to the middleware error pipeline.
|
|
26
|
+
* @param action - The action to add.
|
|
27
|
+
*/
|
|
28
|
+
useError(action) {
|
|
29
|
+
if (action instanceof Middleware)
|
|
30
|
+
this.errorPipeline.push(...action.errorPipeline);
|
|
31
|
+
else
|
|
32
|
+
this.errorPipeline.push(action);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Merges another middleware pipeline.
|
|
37
|
+
* @param middleware - The middleware to merge.
|
|
38
|
+
*/
|
|
39
|
+
merge(middleware) {
|
|
40
|
+
this.pipeline.push(...middleware.pipeline);
|
|
41
|
+
this.errorPipeline.push(...middleware.errorPipeline);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Merges another middleware pipeline at the start.
|
|
46
|
+
* @param middleware - The middleware to merge.
|
|
47
|
+
*/
|
|
48
|
+
mergeAtStart(middleware) {
|
|
49
|
+
this.pipeline.unshift(...middleware.pipeline);
|
|
50
|
+
this.errorPipeline.unshift(...middleware.errorPipeline);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets the middleware names.
|
|
55
|
+
* @returns The middleware names.
|
|
56
|
+
*/
|
|
57
|
+
get middlewareNames() {
|
|
58
|
+
return this.pipeline.map(action => action.name);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Gets the error middleware names.
|
|
62
|
+
* @returns The error middleware names.
|
|
63
|
+
*/
|
|
64
|
+
get errorMiddlewareNames() {
|
|
65
|
+
return this.errorPipeline.map(action => action.name);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export default Middleware;
|
|
69
|
+
//# sourceMappingURL=Middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Middleware.js","sourceRoot":"","sources":["../../../../src/server/router/middleware/Middleware.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAQH,MAAM,OAAgB,UAAU;IAEL;IACA;IAFvB,YACuB,WAAsC,EAAE,EACxC,gBAAgD,EAAE;QADlD,aAAQ,GAAR,QAAQ,CAAgC;QACxC,kBAAa,GAAb,aAAa,CAAqC;IACtE,CAAC;IACJ;;;OAGG;IACI,GAAG,CAAC,MAAkD;QACzD,IAAI,MAAM,YAAY,UAAU;YAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;;YACpE,IAAI,CAAC,QAAQ,CAAC,IAAI,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|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import Request from '../../Request.js';
|
|
7
|
+
import WsRule from '../WsRule.js';
|
|
8
|
+
import Middleware from './Middleware.js';
|
|
9
|
+
import Websocket from '../../websocket/Websocket.js';
|
|
10
|
+
export declare class WsMiddleware extends Middleware<WsRule> {
|
|
11
|
+
clone(): WsMiddleware;
|
|
12
|
+
/**
|
|
13
|
+
* Runs the middleware pipeline.
|
|
14
|
+
* @param request The request received by the server.
|
|
15
|
+
* @param client The WebSocket connection.
|
|
16
|
+
* @param action The action to execute after the middleware pipeline.
|
|
17
|
+
* @param state The state to pass to the middleware and action.
|
|
18
|
+
*/
|
|
19
|
+
run(request: Request, client: Websocket, action: WsRule.action, state?: Middleware.State): Promise<void>;
|
|
20
|
+
runError(error: unknown, request: Request, client: Websocket, state?: Middleware.State): Promise<void>;
|
|
21
|
+
protected errorHandler(error: unknown, request: Request, client: Websocket): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export declare namespace WsMiddleware { }
|
|
24
|
+
export default WsMiddleware;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description Manages the middleware pipeline for WsRules.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import ServerError from '../../ServerError.js';
|
|
7
|
+
import LoggerManager from '../../LoggerManager.js';
|
|
8
|
+
import Middleware from './Middleware.js';
|
|
9
|
+
const logger = LoggerManager.getInstance();
|
|
10
|
+
export class WsMiddleware extends Middleware {
|
|
11
|
+
clone() { return new WsMiddleware(this.pipeline); }
|
|
12
|
+
/**
|
|
13
|
+
* Runs the middleware pipeline.
|
|
14
|
+
* @param request The request received by the server.
|
|
15
|
+
* @param client The WebSocket connection.
|
|
16
|
+
* @param action The action to execute after the middleware pipeline.
|
|
17
|
+
* @param state The state to pass to the middleware and action.
|
|
18
|
+
*/
|
|
19
|
+
async run(request, client, action, state = {}) {
|
|
20
|
+
try {
|
|
21
|
+
let index = 0;
|
|
22
|
+
const next = async (error) => {
|
|
23
|
+
if (error)
|
|
24
|
+
throw error;
|
|
25
|
+
if (client.isClosed)
|
|
26
|
+
return void logger.warn('websocket was closed when calling next()');
|
|
27
|
+
if (client.status === 'rejected')
|
|
28
|
+
return void logger.warn('websocket was rejected when calling next()');
|
|
29
|
+
if (index >= this.pipeline.length) {
|
|
30
|
+
if (client.status === 'pending')
|
|
31
|
+
client.accept();
|
|
32
|
+
return await action(request, client, state);
|
|
33
|
+
}
|
|
34
|
+
const current = this.pipeline[index++];
|
|
35
|
+
return await current(request, client, next, state);
|
|
36
|
+
};
|
|
37
|
+
await next();
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (this.errorPipeline.length === 0)
|
|
41
|
+
return this.errorHandler(error, request, client);
|
|
42
|
+
else
|
|
43
|
+
return this.runError(error, request, client, state);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async runError(error, request, client, state = {}) {
|
|
47
|
+
try {
|
|
48
|
+
let index = 0;
|
|
49
|
+
const next = async (caughtError) => {
|
|
50
|
+
if (caughtError)
|
|
51
|
+
throw caughtError;
|
|
52
|
+
if (index >= this.errorPipeline.length)
|
|
53
|
+
return await this.errorHandler(error, request, client);
|
|
54
|
+
const current = this.errorPipeline[index++];
|
|
55
|
+
return await current(error, request, client, next, state);
|
|
56
|
+
};
|
|
57
|
+
await next();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
return this.errorHandler(error, request, client);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async errorHandler(error, request, client) {
|
|
64
|
+
if (error instanceof ServerError) {
|
|
65
|
+
if (error.isSended)
|
|
66
|
+
return;
|
|
67
|
+
if (client.isClosed)
|
|
68
|
+
return void logger.error(error);
|
|
69
|
+
if (client.status !== 'pending')
|
|
70
|
+
return;
|
|
71
|
+
client.reject(error.status, error.message);
|
|
72
|
+
}
|
|
73
|
+
else if (error instanceof Error) {
|
|
74
|
+
logger.error(error);
|
|
75
|
+
if (client.isClosed || client.status !== 'pending')
|
|
76
|
+
return;
|
|
77
|
+
client.reject(500, error.message);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
logger.error(error);
|
|
81
|
+
if (client.isClosed || client.status !== 'pending')
|
|
82
|
+
return;
|
|
83
|
+
if (client.isClosed)
|
|
84
|
+
return;
|
|
85
|
+
client.reject(500, 'Internal Server Error');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export default WsMiddleware;
|
|
90
|
+
//# sourceMappingURL=WsMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WsMiddleware.js","sourceRoot":"","sources":["../../../../src/server/router/middleware/WsMiddleware.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,WAAW,MAAM,sBAAsB,CAAC;AAC/C,OAAO,aAAa,MAAM,wBAAwB,CAAC;AAEnD,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAGzC,MAAM,MAAM,GAAG,aAAa,CAAC,WAAW,EAAE,CAAC;AAE3C,MAAM,OAAO,YAAa,SAAQ,UAAkB;IACzC,KAAK,KAAmB,OAAO,IAAI,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACxE;;;;;;OAMG;IACI,KAAK,CAAC,GAAG,CAAC,OAAgB,EAAE,MAAiB,EAAE,MAAqB,EAAE,QAA0B,EAAE;QACrG,IAAI,CAAC;YACD,IAAI,KAAK,GAAG,CAAC,CAAC;YACd,MAAM,IAAI,GAAoB,KAAK,EAAE,KAAe,EAAE,EAAE;gBACpD,IAAI,KAAK;oBAAE,MAAM,KAAK,CAAC;gBACvB,IAAI,MAAM,CAAC,QAAQ;oBAAE,OAAO,KAAK,MAAM,CAAC,IAAI,CAAC,0CAA0C,CAAC,CAAC;gBACzF,IAAI,MAAM,CAAC,MAAM,KA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C;IACL,CAAC;CACJ;AAED,eAAe,YAAY,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Chunk.js","sourceRoot":"","sources":["../../../src/
|
|
1
|
+
{"version":3,"file":"Chunk.js","sourceRoot":"","sources":["../../../src/server/websocket/Chunk.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,OAAO,KAAK;IACP,GAAG,CAAU;IACb,GAAG,CAAS;IACZ,MAAM,CAAS;IACf,IAAI,CAAU;IACd,IAAI,CAAkB;IACtB,QAAQ,CAAgB;IACxB,QAAQ,CAAS;IACjB,SAAS,CAAS;IAClB,KAAK,CAAS;IACd,OAAO,CAAS;IACvB,YAAmB,KAAa;QAC5B,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC5F,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;QACzC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC;YACnD,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAC9C,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,QAA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G,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;IAC3E,CAAC;IACM,MAAM,CAAC,MAAM,CAAC,KAAa,EAAE,SAAiB,EAAE,QAA4B;QAC/E,MAAM,OAAO,GAAG,EAAE,CAAC;QACnB,KAAI,IAAI,KAAK,GAAG,SAAS,EAAE,SAAS,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,EAAE,CAAC;YACnF,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,SAAS,GAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACxE,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;CACJ;AAcD,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description adds websocket functionality to Vortez
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import EVENTS from 'events';
|
|
7
|
+
import { Duplex } from 'stream';
|
|
8
|
+
import Request from '../Request.js';
|
|
9
|
+
export { Chunk } from './Chunk.js';
|
|
10
|
+
export declare class Websocket extends EVENTS {
|
|
11
|
+
readonly request: Request;
|
|
12
|
+
readonly connection: Duplex;
|
|
13
|
+
static readonly WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
14
|
+
private _status;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new websocket.
|
|
17
|
+
* @param client The connection with the client.
|
|
18
|
+
*/
|
|
19
|
+
constructor(request: Request, connection: Duplex);
|
|
20
|
+
/** Whether the connection is closed. */
|
|
21
|
+
get isClosed(): boolean;
|
|
22
|
+
get status(): Websocket.handshakeStatus;
|
|
23
|
+
/**
|
|
24
|
+
* Accepts the connection with the client.
|
|
25
|
+
*/
|
|
26
|
+
accept(): void;
|
|
27
|
+
/**
|
|
28
|
+
* Rejects the connection with the client.
|
|
29
|
+
* @param code The code that will be sent to the client.
|
|
30
|
+
* @param reason The reason that will be sent to the client.
|
|
31
|
+
*/
|
|
32
|
+
reject(code: number, reason: string): void;
|
|
33
|
+
/** Finishes the connection. */
|
|
34
|
+
end(): void;
|
|
35
|
+
/** Destroys the connection. */
|
|
36
|
+
destroy(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Send data to the client.
|
|
39
|
+
* @param data The data that will be sent.
|
|
40
|
+
*/
|
|
41
|
+
send(data: string | Buffer): void;
|
|
42
|
+
/**
|
|
43
|
+
* Send data to the client in JSON format.
|
|
44
|
+
* @param data The data that will be sent.
|
|
45
|
+
*/
|
|
46
|
+
sendJson(data: any): void;
|
|
47
|
+
/**
|
|
48
|
+
* Writes data to the client socket.
|
|
49
|
+
* @param data The data that will be sent.
|
|
50
|
+
*/
|
|
51
|
+
private write;
|
|
52
|
+
/**
|
|
53
|
+
* Encodes the data to be sent to the client.
|
|
54
|
+
* @param data The data that will be encoded.
|
|
55
|
+
* @param isText Whether the data is a text or a binary.
|
|
56
|
+
*/
|
|
57
|
+
private encode;
|
|
58
|
+
/**
|
|
59
|
+
* Converts a string to a buffer.
|
|
60
|
+
* @param message The string that will be converted.
|
|
61
|
+
*/
|
|
62
|
+
private stringToBuffer;
|
|
63
|
+
/** Initializes the events. */
|
|
64
|
+
private initEvents;
|
|
65
|
+
on(event: 'close', listener: Websocket.listener.close): this;
|
|
66
|
+
on(event: 'error', listener: Websocket.listener.error): this;
|
|
67
|
+
on(event: 'finish', listener: Websocket.listener.finish): this;
|
|
68
|
+
on(event: 'message', listener: Websocket.listener.message): this;
|
|
69
|
+
off(event: 'close', listener: Websocket.listener.close): this;
|
|
70
|
+
off(event: 'error', listener: Websocket.listener.error): this;
|
|
71
|
+
off(event: 'finish', listener: Websocket.listener.finish): this;
|
|
72
|
+
off(event: 'message', listener: Websocket.listener.message): this;
|
|
73
|
+
private buildHeaders;
|
|
74
|
+
}
|
|
75
|
+
export declare namespace Websocket {
|
|
76
|
+
namespace listener {
|
|
77
|
+
type close = () => void;
|
|
78
|
+
type error = (error: Error) => void;
|
|
79
|
+
type finish = () => void;
|
|
80
|
+
type message = (data: Buffer, info: dataInfo) => void;
|
|
81
|
+
}
|
|
82
|
+
interface dataInfo {
|
|
83
|
+
opCode: number;
|
|
84
|
+
size: number | bigint;
|
|
85
|
+
}
|
|
86
|
+
type handshakeStatus = 'accepted' | 'rejected' | 'pending';
|
|
87
|
+
}
|
|
88
|
+
export default Websocket;
|