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.
Files changed (137) hide show
  1. package/.gitignore +4 -12
  2. package/README.md +235 -289
  3. package/build/Template.d.ts +16 -2
  4. package/build/Template.js +20 -8
  5. package/build/Template.js.map +1 -1
  6. package/build/Vortez.d.ts +8 -15
  7. package/build/Vortez.js +8 -16
  8. package/build/Vortez.js.map +1 -1
  9. package/build/beta/Beta.d.ts +8 -0
  10. package/build/beta/Beta.js +9 -0
  11. package/build/beta/Beta.js.map +1 -0
  12. package/build/{Beta → beta}/JwtManager.js +1 -1
  13. package/build/{Beta → beta}/JwtManager.js.map +1 -1
  14. package/build/{Beta → beta}/Mail.d.ts +1 -1
  15. package/build/{Beta → beta}/Mail.js +1 -1
  16. package/build/beta/Mail.js.map +1 -0
  17. package/build/logger/Debug.d.ts +174 -0
  18. package/build/logger/Debug.js +295 -0
  19. package/build/logger/Debug.js.map +1 -0
  20. package/build/logger/Logger.d.ts +38 -0
  21. package/build/logger/Logger.js +48 -0
  22. package/build/logger/Logger.js.map +1 -0
  23. package/build/{Server → server}/BodyParser.js.map +1 -1
  24. package/build/{Server → server}/Cookie.js.map +1 -1
  25. package/build/server/LoggerManager.d.ts +21 -0
  26. package/build/server/LoggerManager.js +30 -0
  27. package/build/server/LoggerManager.js.map +1 -0
  28. package/build/{Server → server}/Request.d.ts +4 -2
  29. package/build/{Server → server}/Request.js +2 -1
  30. package/build/server/Request.js.map +1 -0
  31. package/build/{Server → server}/Response.d.ts +4 -1
  32. package/build/{Server → server}/Response.js +24 -8
  33. package/build/server/Response.js.map +1 -0
  34. package/build/{Server → server}/Server.d.ts +20 -67
  35. package/build/server/Server.js +211 -0
  36. package/build/server/Server.js.map +1 -0
  37. package/build/server/ServerDebug.d.ts +17 -0
  38. package/build/server/ServerDebug.js +67 -0
  39. package/build/server/ServerDebug.js.map +1 -0
  40. package/build/server/ServerError.d.ts +18 -0
  41. package/build/server/ServerError.js +15 -0
  42. package/build/server/ServerError.js.map +1 -0
  43. package/build/{Server → server}/Session.d.ts +9 -13
  44. package/build/{Server → server}/Session.js +34 -35
  45. package/build/server/Session.js.map +1 -0
  46. package/build/{Config.d.ts → server/config/Config.d.ts} +19 -3
  47. package/build/server/config/Config.js +60 -0
  48. package/build/server/config/Config.js.map +1 -0
  49. package/build/server/config/ConfigLoader.d.ts +24 -0
  50. package/build/server/config/ConfigLoader.js +53 -0
  51. package/build/server/config/ConfigLoader.js.map +1 -0
  52. package/build/server/config/ConfigValidator.d.ts +71 -0
  53. package/build/server/config/ConfigValidator.js +131 -0
  54. package/build/server/config/ConfigValidator.js.map +1 -0
  55. package/build/server/router/HttpRule.d.ts +69 -0
  56. package/build/server/router/HttpRule.js +96 -0
  57. package/build/server/router/HttpRule.js.map +1 -0
  58. package/build/server/router/Router.d.ts +127 -0
  59. package/build/server/router/Router.js +211 -0
  60. package/build/server/router/Router.js.map +1 -0
  61. package/build/server/router/Rule.d.ts +71 -0
  62. package/build/server/router/Rule.js +113 -0
  63. package/build/server/router/Rule.js.map +1 -0
  64. package/build/server/router/WsRule.d.ts +25 -0
  65. package/build/server/router/WsRule.js +34 -0
  66. package/build/server/router/WsRule.js.map +1 -0
  67. package/build/server/router/middleware/HttpMiddleware.d.ts +35 -0
  68. package/build/server/router/middleware/HttpMiddleware.js +90 -0
  69. package/build/server/router/middleware/HttpMiddleware.js.map +1 -0
  70. package/build/server/router/middleware/Middleware.d.ts +77 -0
  71. package/build/server/router/middleware/Middleware.js +69 -0
  72. package/build/server/router/middleware/Middleware.js.map +1 -0
  73. package/build/server/router/middleware/WsMiddleware.d.ts +24 -0
  74. package/build/server/router/middleware/WsMiddleware.js +90 -0
  75. package/build/server/router/middleware/WsMiddleware.js.map +1 -0
  76. package/build/{Server/WebSocket → server/websocket}/Chunk.js.map +1 -1
  77. package/build/server/websocket/Websocket.d.ts +88 -0
  78. package/build/{Server/WebSocket/WebSocket.js → server/websocket/Websocket.js} +91 -53
  79. package/build/server/websocket/Websocket.js.map +1 -0
  80. package/build/utilities/ConsoleUI.d.ts +88 -0
  81. package/build/utilities/ConsoleUI.js +141 -0
  82. package/build/utilities/ConsoleUI.js.map +1 -0
  83. package/build/utilities/DebugUI.d.ts +66 -0
  84. package/build/utilities/DebugUI.js +98 -0
  85. package/build/utilities/DebugUI.js.map +1 -0
  86. package/build/utilities/Env.d.ts +81 -0
  87. package/build/{Utilities → utilities}/Env.js +47 -35
  88. package/build/utilities/Env.js.map +1 -0
  89. package/build/utilities/Path.d.ts +24 -0
  90. package/build/utilities/Path.js +36 -0
  91. package/build/{Utilities → utilities}/Path.js.map +1 -1
  92. package/build/{Utilities → utilities}/Utilities.d.ts +68 -55
  93. package/build/{Utilities → utilities}/Utilities.js +36 -19
  94. package/build/utilities/Utilities.js.map +1 -0
  95. package/changes.md +122 -88
  96. package/package.json +10 -13
  97. package/tests/debug.js +9 -3
  98. package/tests/test.js +50 -48
  99. package/tests/utilities.js +1 -1
  100. package/Notes.md +0 -12
  101. package/build/Beta/Mail.js.map +0 -1
  102. package/build/Config.js +0 -33
  103. package/build/Config.js.map +0 -1
  104. package/build/ConsoleUI.d.ts +0 -57
  105. package/build/ConsoleUI.js +0 -110
  106. package/build/ConsoleUI.js.map +0 -1
  107. package/build/Debug.d.ts +0 -154
  108. package/build/Debug.js +0 -256
  109. package/build/Debug.js.map +0 -1
  110. package/build/LoggerManager/Logger.d.ts +0 -23
  111. package/build/LoggerManager/Logger.js +0 -23
  112. package/build/LoggerManager/Logger.js.map +0 -1
  113. package/build/LoggerManager/LoggerManager.d.ts +0 -18
  114. package/build/LoggerManager/LoggerManager.js +0 -30
  115. package/build/LoggerManager/LoggerManager.js.map +0 -1
  116. package/build/Server/Request.js.map +0 -1
  117. package/build/Server/Response.js.map +0 -1
  118. package/build/Server/Rule.d.ts +0 -81
  119. package/build/Server/Rule.js +0 -146
  120. package/build/Server/Rule.js.map +0 -1
  121. package/build/Server/Server.js +0 -330
  122. package/build/Server/Server.js.map +0 -1
  123. package/build/Server/Session.js.map +0 -1
  124. package/build/Server/WebSocket/WebSocket.d.ts +0 -70
  125. package/build/Server/WebSocket/WebSocket.js.map +0 -1
  126. package/build/Utilities/Env.d.ts +0 -75
  127. package/build/Utilities/Env.js.map +0 -1
  128. package/build/Utilities/Path.d.ts +0 -18
  129. package/build/Utilities/Path.js +0 -27
  130. package/build/Utilities/Utilities.js.map +0 -1
  131. /package/build/{Beta → beta}/JwtManager.d.ts +0 -0
  132. /package/build/{Server → server}/BodyParser.d.ts +0 -0
  133. /package/build/{Server → server}/BodyParser.js +0 -0
  134. /package/build/{Server → server}/Cookie.d.ts +0 -0
  135. /package/build/{Server → server}/Cookie.js +0 -0
  136. /package/build/{Server/WebSocket → server/websocket}/Chunk.d.ts +0 -0
  137. /package/build/{Server/WebSocket → server/websocket}/Chunk.js +0 -0
@@ -10,55 +10,54 @@
10
10
  // Note: a public/private key system should be implemented to prevent
11
11
  // users from using someone else's ss_uuid to access their data.
12
12
  import CRYPTO from 'crypto';
13
- import EVENTS from 'events';
14
- export class Session extends EVENTS {
13
+ export class Session {
15
14
  /** Stores session instances. */
16
- static sessions = new Map;
17
- /** Stores session data. */
18
- data;
19
- /** Stores the session's SS_UUID. */
20
- sessionID;
15
+ static instances = new Map;
21
16
  /**
22
17
  * Creates or retrieves the current session instance from cookies.
23
18
  * @param cookies - The Cookie instance from the request.
24
19
  * @returns The current session instance.
25
20
  */
26
- static getInstance(cookies) {
27
- let sessionID = cookies.get('Session');
28
- if (!sessionID) {
29
- sessionID = CRYPTO.randomUUID();
30
- cookies.set('Session', sessionID, {
31
- secure: true,
32
- httpOnly: true,
33
- path: '/',
34
- expires: (() => {
35
- const date = new Date();
36
- date.setFullYear(date.getFullYear() + 1);
37
- return date;
38
- })()
39
- });
40
- }
41
- let session = Session.sessions.get(sessionID);
42
- if (!session) {
43
- session = new Session(sessionID);
44
- Session.sessions.set(sessionID, session);
45
- }
46
- return session;
21
+ static get(cookies) {
22
+ const id = this.extractID(cookies);
23
+ const session = this.instances.get(id);
24
+ if (session)
25
+ return session;
26
+ const newSession = new Session(id);
27
+ this.instances.set(id, newSession);
28
+ return newSession;
29
+ }
30
+ static extractID(cookies) {
31
+ let id = cookies.get('Session');
32
+ if (id)
33
+ return id;
34
+ do
35
+ id = CRYPTO.randomUUID();
36
+ while (Session.instances.has(id));
37
+ cookies.set('Session', id, {
38
+ secure: true, httpOnly: true,
39
+ path: '/',
40
+ expires: this.getExpiration()
41
+ });
42
+ return id;
43
+ }
44
+ static getExpiration() {
45
+ const date = new Date();
46
+ date.setFullYear(date.getFullYear() + 1);
47
+ return date;
47
48
  }
49
+ /** Stores the session's SS_UUID. */
50
+ id;
51
+ /** Stores session data. */
52
+ data;
48
53
  /**
49
54
  * Constructs a session instance with the given ID.
50
55
  * @param sessionID - The session's unique ID.
51
56
  */
52
57
  constructor(sessionID) {
53
- super();
54
58
  this.data = new Map();
55
- this.sessionID = sessionID;
59
+ this.id = sessionID;
56
60
  }
57
- /**
58
- * Returns the session's SS_UUID.
59
- * @returns The session ID string.
60
- */
61
- getID() { return this.sessionID; }
62
61
  /**
63
62
  * Checks whether a given key exists in the session.
64
63
  * @param name - The key to check in the session data.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Session.js","sourceRoot":"","sources":["../../src/server/Session.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,6DAA6D;AAC7D,mEAAmE;AACnE,uEAAuE;AACvE,0DAA0D;AAC1D,qEAAqE;AACrE,kEAAkE;AAElE,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,MAAM,OAAO,OAAO;IAChB,gCAAgC;IACxB,MAAM,CAAC,SAAS,GAAyB,IAAI,GAAG,CAAC;IACzD;;;;OAIG;IACI,MAAM,CAAC,GAAG,CAAC,OAAe;QAC7B,MAAM,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QACnC,MAAM,OA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C,IAAI,CAAC,CAAC,CAAC,CAAC;IACjE;;;;OAIG;IACI,GAAG,CAAC,IAAY,IAAqB,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACzE;;;;OAIG;IACI,MAAM;QACT,MAAM,IAAI,GAA0B,EAAE,CAAC;QACvC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;QACrD,OAAO,IAAI,CAAC;IAChB,CAAC;IACD;;;;OAIG;IACI,GAAG,CAAC,IAAY,EAAE,KAAU,IAAU,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;IAC1E;;;OAGG;IACI,MAAM,CAAC,IAAY,IAAU,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;;AASjE,eAAe,OAAO,CAAC"}
@@ -3,17 +3,30 @@
3
3
  * @description add the config manager to the Vortez.
4
4
  * @license Apache-2.0
5
5
  */
6
- import LoggerManager from "./LoggerManager/LoggerManager.js";
6
+ import LoggerManager from "../LoggerManager.js";
7
+ import _ConfigLoader from "./ConfigLoader.js";
8
+ import _ConfigValidator from "./ConfigValidator.js";
9
+ export { ConfigLoader } from "./ConfigLoader.js";
10
+ export { ConfigValidator } from "./ConfigValidator.js";
7
11
  export declare class Config implements Config.Main {
12
+ static readonly DEFAULT: Config.Main;
8
13
  templates: Config.Templates;
9
14
  host: string;
10
15
  port: number;
11
16
  logger: LoggerManager;
12
17
  ssl: Config.SSLOptions | null;
13
- static instance: Config;
14
18
  constructor(options?: Config.options);
15
19
  get showAll(): boolean;
16
20
  set showAll(value: boolean);
21
+ /**
22
+ * Converts the config to a JSON string.
23
+ * @returns The JSON string.
24
+ */
25
+ toJson(): string;
26
+ /**
27
+ * Get the default templates.
28
+ * @returns The default templates.
29
+ */
17
30
  static defaultTemplates(): Config.Templates;
18
31
  }
19
32
  export declare namespace Config {
@@ -31,9 +44,12 @@ export declare namespace Config {
31
44
  host: string;
32
45
  port: number;
33
46
  ssl: SSLOptions | null;
34
- logger: LoggerManager;
35
47
  templates: Templates;
36
48
  }
37
49
  type options = Partial<Main>;
38
50
  }
51
+ export declare namespace Config {
52
+ export import Loader = _ConfigLoader;
53
+ export import Validator = _ConfigValidator;
54
+ }
39
55
  export default Config;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @author NetFeez <codefeez.dev@gmail.com>
3
+ * @description add the config manager to the Vortez.
4
+ * @license Apache-2.0
5
+ */
6
+ import Debug from "../../logger/Debug.js";
7
+ import LoggerManager from "../LoggerManager.js";
8
+ import Utilities from "../../utilities/Utilities.js";
9
+ import _ConfigLoader from "./ConfigLoader.js";
10
+ import _ConfigValidator from "./ConfigValidator.js";
11
+ export { ConfigLoader } from "./ConfigLoader.js";
12
+ export { ConfigValidator } from "./ConfigValidator.js";
13
+ export class Config {
14
+ static DEFAULT = {
15
+ host: 'localhost',
16
+ port: 80,
17
+ ssl: null,
18
+ templates: Config.defaultTemplates()
19
+ };
20
+ templates;
21
+ host;
22
+ port;
23
+ logger;
24
+ ssl;
25
+ constructor(options = {}) {
26
+ this.host = options.host ?? Config.DEFAULT.host;
27
+ this.port = options.port ?? Config.DEFAULT.port;
28
+ this.ssl = options.ssl ?? Config.DEFAULT.ssl;
29
+ this.logger = LoggerManager.getInstance();
30
+ this.templates = options.templates ?? Config.defaultTemplates();
31
+ }
32
+ get showAll() { return Debug.showAll; }
33
+ set showAll(value) { Debug.showAll = value; }
34
+ /**
35
+ * Converts the config to a JSON string.
36
+ * @returns The JSON string.
37
+ */
38
+ toJson() {
39
+ const { host, port, ssl, templates } = this;
40
+ const config = { host, port, ssl, templates };
41
+ const content = JSON.stringify(config, null, 4);
42
+ return content;
43
+ }
44
+ /**
45
+ * Get the default templates.
46
+ * @returns The default templates.
47
+ */
48
+ static defaultTemplates() {
49
+ return {
50
+ folder: Utilities.Path.relative('./global/Template/Folder.vhtml'),
51
+ error: Utilities.Path.relative('./global/Template/Error.vhtml')
52
+ };
53
+ }
54
+ }
55
+ (function (Config) {
56
+ Config.Loader = _ConfigLoader;
57
+ Config.Validator = _ConfigValidator;
58
+ })(Config || (Config = {}));
59
+ export default Config;
60
+ //# sourceMappingURL=Config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Config.js","sourceRoot":"","sources":["../../../src/server/config/Config.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,MAAM,uBAAuB,CAAC;AAC1C,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAChD,OAAO,SAAS,MAAM,8BAA8B,CAAC;AACrD,OAAO,aAAa,MAAM,mBAAmB,CAAC;AAC9C,OAAO,gBAAgB,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,MAAM,OAAO,MAAM;IACR,MAAM,CAAU,OAAO,GAAgB;QAC1C,IAAI,EAAE,WAAW;QACjB,IAAI,EAAE,EAAE;QACR,GAAG,EAAE,IAAI;QACT,SAAS,EAAE,MAAM,CAAC,gBAAgB,EAAE;KACvC,CAAC;IACK,SAAS,CAAmB;IAC5B,IAAI,CAAS;IACb,IAAI,CAAS;IACb,MAAM,CAAgB;IACtB,GAAG,CAA2B;IACrC,YAAmB,UAA0B,EAAE;QAC3C,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC;QAChD,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC;QAChD,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC;QAC7C,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,WAAW,EAAE,CAAC;QAC1C,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,MAAM,CAAC,gBAAgB,EAAE,CAAC;IACpE,CAAC;IACD,IAAW,OAAO,KAAc,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IACvD,IAAW,OAAO,CAAC,KAAc,IAAI,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC;IAC7D;;;OAGG;IACI,MAAM;QACT,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;QAC5C,MAAM,MAAM,GAAgB,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC;QAC3D,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QAChD,OAAO,OAAO,CAAC;IACnB,CAAC;IACD;;;OAGG;IACI,MAAM,CAAC,gBAAgB;QAC1B,OAAO;YACH,MAAM,EAAE,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,gCAAgC,CAAC;YACjE,KAAK,EAAE,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,+BAA+B,CAAC;SAClE,CAAC;IACN,CAAC;;AAsBL,WAAiB,MAAM;IACL,aAAM,GAAG,aAAa,CAAC;IACvB,gBAAS,GAAG,gBAAgB,CAAC;AAC/C,CAAC,EAHgB,MAAM,KAAN,MAAM,QAGtB;AACD,eAAe,MAAM,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @author NetFeez <codefeez.dev@gmail.com>
3
+ * @description add the config loader to the Vortez.
4
+ * @license Apache-2.0
5
+ */
6
+ import Config from "./Config.js";
7
+ export declare class ConfigLoader {
8
+ /**
9
+ * Loads the config from the given path.
10
+ * @param path - The path to the config file.
11
+ * @param defaultConfig - The default config to use if the config file does not exist.
12
+ * @returns A promise that resolves with the loaded config.
13
+ */
14
+ static load(path: string, defaultConfig?: Config | Config.options): Promise<Config>;
15
+ /**
16
+ * Saves the config to the given path.
17
+ * @param path - The path to the config file.
18
+ * @param config - The config to save.
19
+ * @returns A promise that resolves when the config is saved.
20
+ */
21
+ static save(path: string, config: Config): Promise<void>;
22
+ }
23
+ export declare namespace ConfigLoader { }
24
+ export default ConfigLoader;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @author NetFeez <codefeez.dev@gmail.com>
3
+ * @description add the config loader to the Vortez.
4
+ * @license Apache-2.0
5
+ */
6
+ import { promises as FSP } from "fs";
7
+ import Logger from "../../logger/Logger.js";
8
+ import Config from "./Config.js";
9
+ import Utilities from "../../utilities/Utilities.js";
10
+ import { ConfigValidator } from "./ConfigValidator.js";
11
+ const logger = new Logger({ prefix: 'Config' });
12
+ export class ConfigLoader {
13
+ /**
14
+ * Loads the config from the given path.
15
+ * @param path - The path to the config file.
16
+ * @param defaultConfig - The default config to use if the config file does not exist.
17
+ * @returns A promise that resolves with the loaded config.
18
+ */
19
+ static async load(path, defaultConfig = {}) {
20
+ logger.log(`loading config from &C6[${path}]`);
21
+ if (!await Utilities.fileExists(path)) {
22
+ const config = new Config(defaultConfig);
23
+ logger.log(`config file &C6[${path}]&R does not exist, creating it`);
24
+ await ConfigLoader.save(path, config);
25
+ logger.log(`config file &C6[${path}]&R &C2was created successfully`);
26
+ return config;
27
+ }
28
+ try {
29
+ const content = await FSP.readFile(path, 'utf8');
30
+ const data = JSON.parse(content);
31
+ const validatedConfig = ConfigValidator.validate(data);
32
+ return new Config(validatedConfig);
33
+ }
34
+ catch (error) {
35
+ logger.error(`config file &C6[${path}]&R &C1could not be loaded`);
36
+ throw error;
37
+ }
38
+ }
39
+ /**
40
+ * Saves the config to the given path.
41
+ * @param path - The path to the config file.
42
+ * @param config - The config to save.
43
+ * @returns A promise that resolves when the config is saved.
44
+ */
45
+ static async save(path, config) {
46
+ const dir = Utilities.Path.dirname(path);
47
+ await FSP.mkdir(dir, { recursive: true });
48
+ const content = config.toJson();
49
+ await FSP.writeFile(path, content, 'utf8');
50
+ }
51
+ }
52
+ export default ConfigLoader;
53
+ //# sourceMappingURL=ConfigLoader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConfigLoader.js","sourceRoot":"","sources":["../../../src/server/config/ConfigLoader.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,IAAI,GAAG,EAAE,MAAM,IAAI,CAAC;AACrC,OAAO,MAAM,MAAM,wBAAwB,CAAC;AAC5C,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,SAAS,MAAM,8BAA8B,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AAEhD,MAAM,OAAO,YAAY;IACrB;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAY,EAAE,gBAAyC,EAAE;QAC9E,MAAM,CAAC,GAAG,CAAC,2BAA2B,IAAI,GAAG,CAAC,CAAC;QAC/C,IAAI,CAAC,MAAM,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YACpC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,aAAa,CAAC,CAAC;YACzC,MAAM,CAAC,GAAG,CAAC,mBAAmB,IAAI,iCAAiC,CAAC,CAAC;YACrE,MAAM,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YACtC,MAAM,CAAC,GAAG,CAAC,mBAAmB,IAAI,iCAAiC,CAAC,CAAC;YACrE,OAAO,MAAM,CAAC;QAClB,CAAC;QACD,IAAI,CAAC;YACD,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YACjD,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACjC,MAAM,eAAe,GAAG,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YACvD,OAAO,IAAI,MAAM,CAAC,eAAe,CAAC,CAAC;QACvC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,CAAC,KAAK,CAAC,mBAAmB,IAAI,4BAA4B,CAAC,CAAC;YAClE,MAAM,KAAK,CAAC;QAChB,CAAC;IACL,CAAC;IACD;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAY,EAAE,MAAc;QACjD,MAAM,GAAG,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACzC,MAAM,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1C,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QAChC,MAAM,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAC/C,CAAC;CACJ;AAED,eAAe,YAAY,CAAC"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @author NetFeez <codefeez.dev@gmail.com>
3
+ * @description add the config validator to use in the config loader.
4
+ * @license Apache-2.0
5
+ */
6
+ import Config from "./Config.js";
7
+ export declare class ConfigValidator {
8
+ /**
9
+ * Validates the config.
10
+ * @param config - The config to validate.
11
+ * @returns The validated config.
12
+ */
13
+ static validate(config: Config.options): Config.Main;
14
+ /**
15
+ * Validates the host.
16
+ * @param host - The host to validate.
17
+ * @returns The validated host.
18
+ */
19
+ static validateHost(host: unknown): string;
20
+ /**
21
+ * Validates the port.
22
+ * @param port - The port to validate.
23
+ * @returns The validated port.
24
+ */
25
+ static validatePort(port: unknown): number;
26
+ /**
27
+ * Validates the templates.
28
+ * @param templates - The templates to validate.
29
+ * @returns The validated templates.
30
+ */
31
+ static validateTemplates(templates: unknown): Config.Templates;
32
+ /**
33
+ * Validates the ssl.
34
+ * @param ssl - The ssl to validate.
35
+ * @returns The validated ssl.
36
+ */
37
+ static validateSSL(ssl: unknown): Config.SSLOptions | null;
38
+ /**
39
+ * Validates a string value.
40
+ * @param value - The value to validate.
41
+ * @param defaultValue - The default value to use if the value is not a string.
42
+ * @param fieldName - The name of the field being validated.
43
+ * @returns The validated string value.
44
+ */
45
+ private static validateString;
46
+ /**
47
+ * Validates a number value.
48
+ * @param value - The value to validate.
49
+ * @param defaultValue - The default value to use if the value is not a number.
50
+ * @param fieldName - The name of the field being validated.
51
+ * @returns The validated number value.
52
+ */
53
+ private static validateNumber;
54
+ /**
55
+ * Validates a number value within a given range.
56
+ * @param value - The value to validate.
57
+ * @param min - The minimum value of the range.
58
+ * @param max - The maximum value of the range.
59
+ */
60
+ private static validateRange;
61
+ /**
62
+ * Validates a number value within a given range.
63
+ * @param value - The value to validate.
64
+ * @param min - The minimum value of the range.
65
+ * @param max - The maximum value of the range.
66
+ * @param defaultValue - The default value to use if the value is not a number or is out of range.
67
+ */
68
+ private static validateNumberInRange;
69
+ }
70
+ export declare namespace ConfigValidator { }
71
+ export default ConfigValidator;
@@ -0,0 +1,131 @@
1
+ /**
2
+ * @author NetFeez <codefeez.dev@gmail.com>
3
+ * @description add the config validator to use in the config loader.
4
+ * @license Apache-2.0
5
+ */
6
+ import Config from "./Config.js";
7
+ import Logger from "../../logger/Logger.js";
8
+ const logger = new Logger({ prefix: 'Config' });
9
+ export class ConfigValidator {
10
+ /**
11
+ * Validates the config.
12
+ * @param config - The config to validate.
13
+ * @returns The validated config.
14
+ */
15
+ static validate(config) {
16
+ const { host, port, ssl, templates } = config;
17
+ return {
18
+ host: this.validateHost(host),
19
+ port: this.validatePort(port),
20
+ ssl: this.validateSSL(ssl),
21
+ templates: this.validateTemplates(templates)
22
+ };
23
+ }
24
+ /**
25
+ * Validates the host.
26
+ * @param host - The host to validate.
27
+ * @returns The validated host.
28
+ */
29
+ static validateHost(host) {
30
+ return this.validateString(host, 'localhost', 'host');
31
+ }
32
+ /**
33
+ * Validates the port.
34
+ * @param port - The port to validate.
35
+ * @returns The validated port.
36
+ */
37
+ static validatePort(port) {
38
+ return this.validateNumberInRange(port, 0, 65535, Config.DEFAULT.port, 'port');
39
+ }
40
+ /**
41
+ * Validates the templates.
42
+ * @param templates - The templates to validate.
43
+ * @returns The validated templates.
44
+ */
45
+ static validateTemplates(templates) {
46
+ if (!templates || typeof templates !== 'object') {
47
+ logger.warn('templates must be an object, using defaults');
48
+ return Config.DEFAULT.templates;
49
+ }
50
+ const defTemplates = Config.DEFAULT.templates;
51
+ const templatesP = templates;
52
+ const result = {};
53
+ for (const key in defTemplates) {
54
+ result[key] = templatesP[key] != null ? templatesP[key] : defTemplates[key];
55
+ }
56
+ return result;
57
+ }
58
+ /**
59
+ * Validates the ssl.
60
+ * @param ssl - The ssl to validate.
61
+ * @returns The validated ssl.
62
+ */
63
+ static validateSSL(ssl) {
64
+ if (!ssl || typeof ssl !== 'object') {
65
+ return null;
66
+ }
67
+ const sslP = ssl;
68
+ const { pubKey, privKey, port = 443 } = sslP;
69
+ if (typeof pubKey !== 'string') {
70
+ logger.warn('ssl.pubKey must be a string, skipping ssl');
71
+ return Config.DEFAULT.ssl;
72
+ }
73
+ if (typeof privKey !== 'string') {
74
+ logger.warn('ssl.privKey must be a string, skipping ssl');
75
+ return Config.DEFAULT.ssl;
76
+ }
77
+ const validPort = this.validateNumberInRange(port, 0, 65535, 443, 'ssl.port');
78
+ return { pubKey, privKey, port: validPort };
79
+ }
80
+ /**
81
+ * Validates a string value.
82
+ * @param value - The value to validate.
83
+ * @param defaultValue - The default value to use if the value is not a string.
84
+ * @param fieldName - The name of the field being validated.
85
+ * @returns The validated string value.
86
+ */
87
+ static validateString(value, defaultValue, fieldName) {
88
+ if (typeof value === 'string')
89
+ return value;
90
+ logger.warn(`${fieldName} must be a string, using default: ${defaultValue}`);
91
+ return defaultValue;
92
+ }
93
+ /**
94
+ * Validates a number value.
95
+ * @param value - The value to validate.
96
+ * @param defaultValue - The default value to use if the value is not a number.
97
+ * @param fieldName - The name of the field being validated.
98
+ * @returns The validated number value.
99
+ */
100
+ static validateNumber(value, defaultValue, fieldName) {
101
+ if (typeof value === 'number')
102
+ return value;
103
+ logger.warn(`${fieldName} must be a number, using default: ${defaultValue}`);
104
+ return defaultValue;
105
+ }
106
+ /**
107
+ * Validates a number value within a given range.
108
+ * @param value - The value to validate.
109
+ * @param min - The minimum value of the range.
110
+ * @param max - The maximum value of the range.
111
+ */
112
+ static validateRange(value, min, max, defaultValue, fieldName) {
113
+ if (value >= min && value <= max)
114
+ return value;
115
+ logger.warn(`${fieldName} must be between ${min} and ${max}, using default: ${defaultValue}`);
116
+ return defaultValue;
117
+ }
118
+ /**
119
+ * Validates a number value within a given range.
120
+ * @param value - The value to validate.
121
+ * @param min - The minimum value of the range.
122
+ * @param max - The maximum value of the range.
123
+ * @param defaultValue - The default value to use if the value is not a number or is out of range.
124
+ */
125
+ static validateNumberInRange(value, min, max, defaultValue, fieldName) {
126
+ const num = this.validateNumber(value, defaultValue, fieldName);
127
+ return this.validateRange(num, min, max, defaultValue, fieldName);
128
+ }
129
+ }
130
+ export default ConfigValidator;
131
+ //# sourceMappingURL=ConfigValidator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConfigValidator.js","sourceRoot":"","sources":["../../../src/server/config/ConfigValidator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,MAAM,wBAAwB,CAAC;AAE5C,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AAEhD,MAAM,OAAO,eAAe;IACxB;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,MAAsB;QACzC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE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AAI,CAAC,GAAG,SAAS,qCAAqC,YAAY,EAAE,CAAC,CAAC;QAC7E,OAAO,YAAY,CAAC;IACxB,CAAC;IACD;;;;;OAKG;IACK,MAAM,CAAC,aAAa,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW,EAAE,YAAoB,EAAE,SAAiB;QACzG,IAAI,KAAK,IAAI,GAAG,IAAI,KAAK,IAAI,GAAG;YAAE,OAAO,KAAK,CAAC;QAC/C,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,oBAAoB,GAAG,QAAQ,GAAG,oBAAoB,YAAY,EAAE,CAAC,CAAC;QAC9F,OAAO,YAAY,CAAC;IACxB,CAAC;IACD;;;;;;OAMG;IACK,MAAM,CAAC,qBAAqB,CAAC,KAAc,EAAE,GAAW,EAAE,GAAW,EAAE,YAAoB,EAAE,SAAiB;QAClH,MAAM,GAAG,GAAG,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;QAChE,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;IACtE,CAAC;CACJ;AAED,eAAe,eAAe,CAAC"}
@@ -0,0 +1,69 @@
1
+ import Request from '../Request.js';
2
+ import Response from '../Response.js';
3
+ import BaseRule from './Rule.js';
4
+ import HttpMiddleware from './middleware/HttpMiddleware.js';
5
+ import Middleware from './middleware/Middleware.js';
6
+ export declare class HttpRule extends BaseRule<HttpRule.action> {
7
+ method: Request.Method;
8
+ readonly middleware: HttpMiddleware;
9
+ /**
10
+ * Creates a routing rule for Vortez.
11
+ * @param method - The HTTP method of the rule.
12
+ * @param urlRule - The URL rule adopted by this Rule instance.
13
+ * @param action - The executable content of the rule.
14
+ * @param middleware - The middleware to clone.
15
+ */
16
+ constructor(method: Request.Method, urlRule: string, action: HttpRule.action, middleware?: HttpMiddleware);
17
+ exec(request: Request, response: Response, state?: Middleware.State): void;
18
+ test(request: Request): boolean;
19
+ /**
20
+ * Adds a middleware to this specific rule.
21
+ * @param action The middleware action.
22
+ */
23
+ use(action: Middleware.action.http): this;
24
+ /**
25
+ * Adds a error middleware to this specific rule.
26
+ * @param action The middleware action.
27
+ */
28
+ useError(action: Middleware.errorAction.http): this;
29
+ /**
30
+ * Creates a routing rule to send a folder to the client.
31
+ * @param urlRule - The URL rule adopted by this Rule instance.
32
+ * @param path - The path of the folder to send.
33
+ * @param middleware - The middleware to clone.
34
+ */
35
+ static folder(urlRule: string, path: string, middleware?: HttpMiddleware): HttpRule;
36
+ /**
37
+ * Creates a routing rule to send a file to the client.
38
+ * @param urlRule - The URL rule adopted by this Rule instance.
39
+ * @param path - The path of the file to send.
40
+ * @param middleware - The middleware to clone.
41
+ */
42
+ static file(urlRule: string, path: string, middleware?: HttpMiddleware): HttpRule;
43
+ /**
44
+ * Creates a routing rule.
45
+ * @param method - The HTTP method of the rule.
46
+ * @param urlRule - The URL rule adopted by this Rule instance.
47
+ * @param action - The executable content of the rule.
48
+ * @param middleware - The middleware to clone.
49
+ */
50
+ static action(method: Request.Method, urlRule: string, action: HttpRule.action, middleware?: HttpMiddleware): HttpRule;
51
+ /**
52
+ * Sends a folder to the client.
53
+ * @param path - The path of the folder to send.
54
+ * @param request - The incoming request.
55
+ * @param client - The client that made the request.
56
+ */
57
+ private static sendFolder;
58
+ /**
59
+ * Sends a file to the client.
60
+ * @param path - The path of the file to send.
61
+ * @param request - The incoming request.
62
+ * @param client - The client that made the request.
63
+ */
64
+ private static sendFile;
65
+ }
66
+ export declare namespace HttpRule {
67
+ type action = (request: Request, response: Response, state: Middleware.State) => void | Promise<void>;
68
+ }
69
+ export default HttpRule;
@@ -0,0 +1,96 @@
1
+ import BaseRule from './Rule.js';
2
+ import HttpMiddleware from './middleware/HttpMiddleware.js';
3
+ export class HttpRule extends BaseRule {
4
+ method;
5
+ middleware;
6
+ /**
7
+ * Creates a routing rule for Vortez.
8
+ * @param method - The HTTP method of the rule.
9
+ * @param urlRule - The URL rule adopted by this Rule instance.
10
+ * @param action - The executable content of the rule.
11
+ * @param middleware - The middleware to clone.
12
+ */
13
+ constructor(method, urlRule, action, middleware = new HttpMiddleware()) {
14
+ super(urlRule, action);
15
+ this.method = method;
16
+ this.middleware = middleware;
17
+ }
18
+ exec(request, response, state) {
19
+ request.ruleParams = this.getParams(request.url);
20
+ this.middleware.run(request, response, this.action, state);
21
+ }
22
+ test(request) {
23
+ return (this.method == request.method || this.method == 'ALL') && super.test(request);
24
+ }
25
+ /**
26
+ * Adds a middleware to this specific rule.
27
+ * @param action The middleware action.
28
+ */
29
+ use(action) {
30
+ this.middleware.use(action);
31
+ return this;
32
+ }
33
+ /**
34
+ * Adds a error middleware to this specific rule.
35
+ * @param action The middleware action.
36
+ */
37
+ useError(action) {
38
+ this.middleware.useError(action);
39
+ return this;
40
+ }
41
+ /**
42
+ * Creates a routing rule to send a folder to the client.
43
+ * @param urlRule - The URL rule adopted by this Rule instance.
44
+ * @param path - The path of the folder to send.
45
+ * @param middleware - The middleware to clone.
46
+ */
47
+ static folder(urlRule, path, middleware) {
48
+ if (urlRule.endsWith('/'))
49
+ urlRule += '*';
50
+ if (!urlRule.endsWith('/*'))
51
+ urlRule += '/*';
52
+ const action = this.sendFolder.bind(this, path);
53
+ return new HttpRule('GET', urlRule, action, middleware);
54
+ }
55
+ /**
56
+ * Creates a routing rule to send a file to the client.
57
+ * @param urlRule - The URL rule adopted by this Rule instance.
58
+ * @param path - The path of the file to send.
59
+ * @param middleware - The middleware to clone.
60
+ */
61
+ static file(urlRule, path, middleware) {
62
+ const action = this.sendFile.bind(this, path);
63
+ return new HttpRule('GET', urlRule, action, middleware);
64
+ }
65
+ /**
66
+ * Creates a routing rule.
67
+ * @param method - The HTTP method of the rule.
68
+ * @param urlRule - The URL rule adopted by this Rule instance.
69
+ * @param action - The executable content of the rule.
70
+ * @param middleware - The middleware to clone.
71
+ */
72
+ static action(method, urlRule, action, middleware) {
73
+ return new HttpRule(method, urlRule, action, middleware);
74
+ }
75
+ /**
76
+ * Sends a folder to the client.
77
+ * @param path - The path of the folder to send.
78
+ * @param request - The incoming request.
79
+ * @param client - The client that made the request.
80
+ */
81
+ static async sendFolder(path, request, client) {
82
+ const { $surplus = '' } = request.ruleParams;
83
+ await client.sendFolder(path, $surplus);
84
+ }
85
+ /**
86
+ * Sends a file to the client.
87
+ * @param path - The path of the file to send.
88
+ * @param request - The incoming request.
89
+ * @param client - The client that made the request.
90
+ */
91
+ static async sendFile(path, request, client) {
92
+ await client.sendFile(path);
93
+ }
94
+ }
95
+ export default HttpRule;
96
+ //# sourceMappingURL=HttpRule.js.map