wnodex 0.1.0 → 0.2.2

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 (207) hide show
  1. package/.turbo/turbo-build.log +7 -0
  2. package/LICENSE +1 -1
  3. package/README.md +73 -67
  4. package/dist/{schemas/wnodex-config.d.ts → config.d.ts} +14 -9
  5. package/dist/config.d.ts.map +1 -0
  6. package/dist/config.js +22 -0
  7. package/dist/index.d.ts +68 -38
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +126 -41
  10. package/dist/setup-config.d.ts +3 -0
  11. package/dist/setup-config.d.ts.map +1 -0
  12. package/dist/setup-config.js +9 -0
  13. package/dist/setup-middlewares.d.ts +4 -0
  14. package/dist/setup-middlewares.d.ts.map +1 -0
  15. package/dist/setup-middlewares.js +32 -0
  16. package/package.json +51 -15
  17. package/rolldown.config.ts +20 -0
  18. package/src/config.ts +27 -0
  19. package/src/index.ts +149 -38
  20. package/src/setup-config.ts +17 -0
  21. package/src/setup-middlewares.ts +49 -0
  22. package/tests/body-parser.schema.test.ts +57 -0
  23. package/tests/compression.schema.test.ts +27 -0
  24. package/tests/cookie-parser.schema.test.ts +56 -0
  25. package/tests/cors.schema.test.ts +35 -0
  26. package/tests/helmet.schema.test.ts +33 -0
  27. package/tests/hpp.schema.test.ts +31 -0
  28. package/tests/index.test.ts +105 -0
  29. package/tests/passport.schema.test.ts +33 -0
  30. package/tests/rate-limit.schema.test.ts +56 -0
  31. package/tests/session.schema.test.ts +80 -0
  32. package/tsconfig.json +8 -8
  33. package/vitest.config.ts +13 -0
  34. package/dist/consts/cors.d.ts +0 -15
  35. package/dist/consts/cors.d.ts.map +0 -1
  36. package/dist/consts/cors.js +0 -29
  37. package/dist/consts/error-codes.d.ts +0 -10
  38. package/dist/consts/error-codes.d.ts.map +0 -1
  39. package/dist/consts/error-codes.js +0 -12
  40. package/dist/consts/index.d.ts +0 -8
  41. package/dist/consts/index.d.ts.map +0 -1
  42. package/dist/consts/index.js +0 -10
  43. package/dist/consts/log-levels.d.ts +0 -8
  44. package/dist/consts/log-levels.d.ts.map +0 -1
  45. package/dist/consts/log-levels.js +0 -10
  46. package/dist/consts/node-env.d.ts +0 -7
  47. package/dist/consts/node-env.d.ts.map +0 -1
  48. package/dist/consts/node-env.js +0 -9
  49. package/dist/consts/port.d.ts +0 -6
  50. package/dist/consts/port.d.ts.map +0 -1
  51. package/dist/consts/port.js +0 -26
  52. package/dist/consts/rate-limit.d.ts +0 -9
  53. package/dist/consts/rate-limit.d.ts.map +0 -1
  54. package/dist/consts/rate-limit.js +0 -11
  55. package/dist/consts/session.d.ts +0 -6
  56. package/dist/consts/session.d.ts.map +0 -1
  57. package/dist/consts/session.js +0 -8
  58. package/dist/errors/base-error.d.ts +0 -57
  59. package/dist/errors/base-error.d.ts.map +0 -1
  60. package/dist/errors/base-error.js +0 -93
  61. package/dist/errors/config-error.d.ts +0 -20
  62. package/dist/errors/config-error.d.ts.map +0 -1
  63. package/dist/errors/config-error.js +0 -30
  64. package/dist/errors/http-error.d.ts +0 -17
  65. package/dist/errors/http-error.d.ts.map +0 -1
  66. package/dist/errors/http-error.js +0 -24
  67. package/dist/errors/index.d.ts +0 -5
  68. package/dist/errors/index.d.ts.map +0 -1
  69. package/dist/errors/index.js +0 -7
  70. package/dist/errors/validation-error.d.ts +0 -19
  71. package/dist/errors/validation-error.d.ts.map +0 -1
  72. package/dist/errors/validation-error.js +0 -29
  73. package/dist/helpers/get-node-env.d.ts +0 -13
  74. package/dist/helpers/get-node-env.d.ts.map +0 -1
  75. package/dist/helpers/get-node-env.js +0 -27
  76. package/dist/helpers/index.d.ts +0 -5
  77. package/dist/helpers/index.d.ts.map +0 -1
  78. package/dist/helpers/index.js +0 -7
  79. package/dist/helpers/is-development.d.ts +0 -9
  80. package/dist/helpers/is-development.d.ts.map +0 -1
  81. package/dist/helpers/is-development.js +0 -16
  82. package/dist/helpers/setup-config.d.ts +0 -12
  83. package/dist/helpers/setup-config.d.ts.map +0 -1
  84. package/dist/helpers/setup-config.js +0 -21
  85. package/dist/helpers/setup-middlewares.d.ts +0 -12
  86. package/dist/helpers/setup-middlewares.d.ts.map +0 -1
  87. package/dist/helpers/setup-middlewares.js +0 -45
  88. package/dist/logger.d.ts +0 -3
  89. package/dist/logger.d.ts.map +0 -1
  90. package/dist/logger.js +0 -21
  91. package/dist/middlewares/compression.d.ts +0 -18
  92. package/dist/middlewares/compression.d.ts.map +0 -1
  93. package/dist/middlewares/compression.js +0 -26
  94. package/dist/middlewares/cookie-parser.ts.d.ts +0 -20
  95. package/dist/middlewares/cookie-parser.ts.d.ts.map +0 -1
  96. package/dist/middlewares/cookie-parser.ts.js +0 -33
  97. package/dist/middlewares/cors.d.ts +0 -22
  98. package/dist/middlewares/cors.d.ts.map +0 -1
  99. package/dist/middlewares/cors.js +0 -34
  100. package/dist/middlewares/error-handler.d.ts +0 -21
  101. package/dist/middlewares/error-handler.d.ts.map +0 -1
  102. package/dist/middlewares/error-handler.js +0 -43
  103. package/dist/middlewares/helmet.d.ts +0 -16
  104. package/dist/middlewares/helmet.d.ts.map +0 -1
  105. package/dist/middlewares/helmet.js +0 -28
  106. package/dist/middlewares/hpp.d.ts +0 -18
  107. package/dist/middlewares/hpp.d.ts.map +0 -1
  108. package/dist/middlewares/hpp.js +0 -30
  109. package/dist/middlewares/index.d.ts +0 -10
  110. package/dist/middlewares/index.d.ts.map +0 -1
  111. package/dist/middlewares/index.js +0 -12
  112. package/dist/middlewares/passport.d.ts +0 -16
  113. package/dist/middlewares/passport.d.ts.map +0 -1
  114. package/dist/middlewares/passport.js +0 -27
  115. package/dist/middlewares/rate-limit.d.ts +0 -23
  116. package/dist/middlewares/rate-limit.d.ts.map +0 -1
  117. package/dist/middlewares/rate-limit.js +0 -35
  118. package/dist/middlewares/session.d.ts +0 -16
  119. package/dist/middlewares/session.d.ts.map +0 -1
  120. package/dist/middlewares/session.js +0 -23
  121. package/dist/schemas/compression.d.ts +0 -5
  122. package/dist/schemas/compression.d.ts.map +0 -1
  123. package/dist/schemas/compression.js +0 -5
  124. package/dist/schemas/cookie-parser.d.ts +0 -9
  125. package/dist/schemas/cookie-parser.d.ts.map +0 -1
  126. package/dist/schemas/cookie-parser.js +0 -14
  127. package/dist/schemas/cors.d.ts +0 -6
  128. package/dist/schemas/cors.d.ts.map +0 -1
  129. package/dist/schemas/cors.js +0 -9
  130. package/dist/schemas/error-metadata.d.ts +0 -18
  131. package/dist/schemas/error-metadata.d.ts.map +0 -1
  132. package/dist/schemas/error-metadata.js +0 -12
  133. package/dist/schemas/helmet.d.ts +0 -6
  134. package/dist/schemas/helmet.d.ts.map +0 -1
  135. package/dist/schemas/helmet.js +0 -8
  136. package/dist/schemas/hpp.d.ts +0 -5
  137. package/dist/schemas/hpp.d.ts.map +0 -1
  138. package/dist/schemas/hpp.js +0 -8
  139. package/dist/schemas/index.d.ts +0 -13
  140. package/dist/schemas/index.d.ts.map +0 -1
  141. package/dist/schemas/index.js +0 -15
  142. package/dist/schemas/node-env.d.ts +0 -16
  143. package/dist/schemas/node-env.d.ts.map +0 -1
  144. package/dist/schemas/node-env.js +0 -8
  145. package/dist/schemas/passport.d.ts +0 -6
  146. package/dist/schemas/passport.d.ts.map +0 -1
  147. package/dist/schemas/passport.js +0 -8
  148. package/dist/schemas/port.d.ts +0 -4
  149. package/dist/schemas/port.d.ts.map +0 -1
  150. package/dist/schemas/port.js +0 -16
  151. package/dist/schemas/rate-limit.d.ts +0 -9
  152. package/dist/schemas/rate-limit.d.ts.map +0 -1
  153. package/dist/schemas/rate-limit.js +0 -26
  154. package/dist/schemas/session.d.ts +0 -18
  155. package/dist/schemas/session.d.ts.map +0 -1
  156. package/dist/schemas/session.js +0 -32
  157. package/dist/schemas/wnodex-config.d.ts.map +0 -1
  158. package/dist/schemas/wnodex-config.js +0 -24
  159. package/dist/tsconfig.lib.tsbuildinfo +0 -1
  160. package/dist/wnodex.d.ts +0 -69
  161. package/dist/wnodex.d.ts.map +0 -1
  162. package/dist/wnodex.js +0 -131
  163. package/src/consts/cors.ts +0 -27
  164. package/src/consts/error-codes.ts +0 -9
  165. package/src/consts/index.ts +0 -7
  166. package/src/consts/log-levels.ts +0 -7
  167. package/src/consts/node-env.ts +0 -6
  168. package/src/consts/port.ts +0 -23
  169. package/src/consts/rate-limit.ts +0 -9
  170. package/src/consts/session.ts +0 -6
  171. package/src/errors/base-error.ts +0 -111
  172. package/src/errors/config-error.ts +0 -31
  173. package/src/errors/http-error.ts +0 -21
  174. package/src/errors/index.ts +0 -4
  175. package/src/errors/validation-error.ts +0 -30
  176. package/src/helpers/get-node-env.ts +0 -27
  177. package/src/helpers/index.ts +0 -4
  178. package/src/helpers/is-development.ts +0 -15
  179. package/src/helpers/setup-config.ts +0 -25
  180. package/src/helpers/setup-middlewares.ts +0 -56
  181. package/src/logger.ts +0 -22
  182. package/src/middlewares/compression.ts +0 -32
  183. package/src/middlewares/cookie-parser.ts.ts +0 -37
  184. package/src/middlewares/cors.ts +0 -35
  185. package/src/middlewares/error-handler.ts +0 -54
  186. package/src/middlewares/helmet.ts +0 -29
  187. package/src/middlewares/hpp.ts +0 -31
  188. package/src/middlewares/index.ts +0 -9
  189. package/src/middlewares/passport.ts +0 -30
  190. package/src/middlewares/rate-limit.ts +0 -39
  191. package/src/middlewares/session.ts +0 -25
  192. package/src/schemas/compression.ts +0 -8
  193. package/src/schemas/cookie-parser.ts +0 -21
  194. package/src/schemas/cors.ts +0 -13
  195. package/src/schemas/error-metadata.ts +0 -15
  196. package/src/schemas/helmet.ts +0 -11
  197. package/src/schemas/hpp.ts +0 -9
  198. package/src/schemas/index.ts +0 -12
  199. package/src/schemas/node-env.ts +0 -11
  200. package/src/schemas/passport.ts +0 -11
  201. package/src/schemas/port.ts +0 -22
  202. package/src/schemas/rate-limit.ts +0 -33
  203. package/src/schemas/session.ts +0 -40
  204. package/src/schemas/wnodex-config.ts +0 -26
  205. package/src/wnodex.ts +0 -152
  206. package/tsconfig.lib.json +0 -13
  207. package/tsconfig.tsbuildinfo +0 -1
@@ -1,39 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import rateLimit from 'express-rate-limit';
4
-
5
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
6
-
7
- /**
8
- * Configures the rate limiting middleware to protect the application
9
- * from excessive requests, mitigating abuse and denial of service attacks.
10
- *
11
- * It reads the `rateLimit` option from the provided config.
12
- * If it's a boolean:
13
- * - `false` disables rate limiting.
14
- * - `true` enables rate limiting with default settings.
15
- * If it's an object, it spreads the properties into the rateLimit options.
16
- * @param app Express Application instance to apply the rate limiter to.
17
- * @param config Typed application configuration object containing rateLimit option.
18
- *
19
- * @returns The result of `app.use(rateLimit())` if rate limiting is enabled, otherwise `undefined`.
20
- *
21
- * @example
22
- * private setupMiddlewares() {
23
- * configureHelmet(this.app, this.config);
24
- * }
25
- */
26
- export function configureRateLimit(
27
- app: Application,
28
- config: WnodexConfigOutput
29
- ) {
30
- const { rateLimit: options } = config;
31
-
32
- if (typeof options === 'boolean') {
33
- if (options === false || !options) return;
34
-
35
- return app.use(rateLimit());
36
- } else {
37
- return app.use(rateLimit({ ...options }));
38
- }
39
- }
@@ -1,25 +0,0 @@
1
- import type { Application } from 'express';
2
-
3
- import session from 'express-session';
4
-
5
- import type { WnodexConfigOutput } from '../schemas/wnodex-config.js';
6
-
7
- /**
8
- * Configures the Express session middleware with the provided options.
9
- * Applies session management to the Express application, enabling
10
- * stateful sessions with cookies.
11
- * @param app The Express application instance.
12
- * @param config The validated configuration object containing session options.
13
- * @returns The Express Application instance with Session middleware applied,
14
- * or undefined if ession is disabled.
15
- *
16
- * @example
17
- * configureSession(app, { session: { secret: 'your-secret', resave: false, saveUninitialized: false } });
18
- */
19
- export function configureSession(app: Application, config: WnodexConfigOutput) {
20
- const { session: options } = config;
21
-
22
- if (!options) return;
23
-
24
- return app.use(session({ ...options }));
25
- }
@@ -1,8 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- export const CompressionOptionsSchema = z.boolean().optional().default(true);
4
-
5
- export type CompressionOptionsInput = z.input<typeof CompressionOptionsSchema>;
6
- export type CompressionOptionsOutput = z.output<
7
- typeof CompressionOptionsSchema
8
- >;
@@ -1,21 +0,0 @@
1
- import type { CookieParseOptions } from 'cookie-parser';
2
-
3
- import { z } from 'zod';
4
-
5
- export const CookieParserOptionsSchema = z
6
- .union([
7
- z.boolean(),
8
- z.object({
9
- secret: z.union([z.string(), z.array(z.string())]),
10
- options: z.custom<CookieParseOptions>().optional(),
11
- }),
12
- ])
13
- .optional()
14
- .default(true);
15
-
16
- export type CookieParserOptionsInput = z.input<
17
- typeof CookieParserOptionsSchema
18
- >;
19
- export type CookieParserOptionsOutput = z.output<
20
- typeof CookieParserOptionsSchema
21
- >;
@@ -1,13 +0,0 @@
1
- import type { CorsOptions } from 'cors';
2
-
3
- import { z } from 'zod';
4
-
5
- import { DEFAULT_CORS_OPTIONS } from '../consts/cors.js';
6
-
7
- export const CorsOptionsSchema = z
8
- .union([z.boolean(), z.custom<CorsOptions>()])
9
- .optional()
10
- .default(DEFAULT_CORS_OPTIONS);
11
-
12
- export type CorsOptionsInput = z.input<typeof CorsOptionsSchema>;
13
- export type CorsOptionsOutput = z.output<typeof CorsOptionsSchema>;
@@ -1,15 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- import { ERROR_CODES } from '../consts/error-codes.js';
4
-
5
- export const ErrorMetadataSchema = z
6
- .object({
7
- code: z.enum(Object.values(ERROR_CODES)),
8
- cause: z.unknown().optional(),
9
- context: z.record(z.string(), z.unknown()).optional(),
10
- })
11
- .strict();
12
-
13
- export type ErrorMetadata = z.infer<typeof ErrorMetadataSchema>;
14
- export type ErrorMetadataInput = z.input<typeof ErrorMetadataSchema>;
15
- export type ErrorMetadataOutput = z.output<typeof ErrorMetadataSchema>;
@@ -1,11 +0,0 @@
1
- import type { HelmetOptions } from 'helmet';
2
-
3
- import { z } from 'zod';
4
-
5
- export const HelmetOptionsSchema = z
6
- .union([z.boolean(), z.custom<HelmetOptions>()])
7
- .optional()
8
- .default(false);
9
-
10
- export type HelmetOptionsInput = z.input<typeof HelmetOptionsSchema>;
11
- export type HelmetOptionsOutput = z.output<typeof HelmetOptionsSchema>;
@@ -1,9 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- export const HppOptionsSchema = z
4
- .union([z.boolean(), z.array(z.string())])
5
- .optional()
6
- .default(true);
7
-
8
- export type HppOptionsInput = z.input<typeof HppOptionsSchema>;
9
- export type HppOptionsoutput = z.output<typeof HppOptionsSchema>;
@@ -1,12 +0,0 @@
1
- export * from './compression.js';
2
- export * from './cookie-parser.js';
3
- export * from './cors.js';
4
- export * from './error-metadata.js';
5
- export * from './helmet.js';
6
- export * from './hpp.js';
7
- export * from './node-env.js';
8
- export * from './passport.js';
9
- export * from './port.js';
10
- export * from './rate-limit.js';
11
- export * from './session.js';
12
- export * from './wnodex-config.js';
@@ -1,11 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- import { NODE_ENV_VALUES } from '../consts/node-env.js';
4
-
5
- export const NODE_ENVS = Object.values(NODE_ENV_VALUES);
6
-
7
- export const NodeEnvSchema = z.enum([...NODE_ENVS]);
8
-
9
- export const NodeEnvEnum = NodeEnvSchema.enum;
10
-
11
- export type NodeEnv = z.infer<typeof NodeEnvSchema>;
@@ -1,11 +0,0 @@
1
- import passport from 'passport';
2
-
3
- import { z } from 'zod';
4
-
5
- export const PassportSchema = z
6
- .union([z.literal(false), z.custom<typeof passport>()])
7
- .optional()
8
- .default(false);
9
-
10
- export type PassportInput = z.input<typeof PassportSchema>;
11
- export type PassportOutput = z.output<typeof PassportSchema>;
@@ -1,22 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- import {
4
- DEFAULT_PORT,
5
- MAX_PORT,
6
- MIN_PORT,
7
- RESERVED_PORTS,
8
- } from '../consts/port.js';
9
-
10
- export const PortSchema = z
11
- .union([
12
- z.string().regex(/^\d{4,5}$/),
13
- z.number().int().min(MIN_PORT).max(MAX_PORT),
14
- ])
15
- .transform((val) => (typeof val === 'string' ? Number(val) : val))
16
- .refine((port: number) => !RESERVED_PORTS[port], {
17
- message: 'The selected port is reserved for another application',
18
- })
19
- .optional()
20
- .default(DEFAULT_PORT);
21
-
22
- export type PortInput = z.input<typeof PortSchema>;
@@ -1,33 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- import {
4
- DEFAULT_RATE_LIMIT_MAX,
5
- DEFAULT_RATE_LIMIT_MESSAGE,
6
- DEFAULT_RATE_LIMIT_OPTIONS,
7
- DEFAULT_RATE_LIMIT_WINDOW_MS,
8
- } from '../consts/rate-limit.js';
9
-
10
- export const RateLimitOptionsSchema = z
11
- .union([
12
- z.boolean(),
13
- z.object({
14
- windowMs: z
15
- .number()
16
- .int()
17
- .positive()
18
- .optional()
19
- .default(DEFAULT_RATE_LIMIT_WINDOW_MS),
20
- max: z
21
- .number()
22
- .int()
23
- .positive()
24
- .optional()
25
- .default(DEFAULT_RATE_LIMIT_MAX),
26
- message: z.string().optional().default(DEFAULT_RATE_LIMIT_MESSAGE),
27
- }),
28
- ])
29
- .optional()
30
- .default({ ...DEFAULT_RATE_LIMIT_OPTIONS });
31
-
32
- export type RateLimitOptionsInput = z.input<typeof RateLimitOptionsSchema>;
33
- export type RateLimitOptionsOutput = z.output<typeof RateLimitOptionsSchema>;
@@ -1,40 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- import {
4
- DEFAULT_SESSION_COOKIE_HTTP_ONLY,
5
- DEFAULT_SESSION_COOKIE_MAX_AGE,
6
- DEFAULT_SESSION_COOKIE_SECURE,
7
- DEFAULT_SESSION_RESAVE,
8
- DEFAULT_SESSION_SAVE_UNINITIALIZED,
9
- } from '../consts/session.js';
10
-
11
- export const SessionCookieSchema = z
12
- .object({
13
- secure: z.boolean().optional().default(DEFAULT_SESSION_COOKIE_SECURE),
14
- maxAge: z.number().optional().default(DEFAULT_SESSION_COOKIE_MAX_AGE),
15
- httpOnly: z.boolean().optional().default(DEFAULT_SESSION_COOKIE_HTTP_ONLY),
16
- })
17
- .optional()
18
- .default({
19
- secure: DEFAULT_SESSION_COOKIE_SECURE,
20
- maxAge: DEFAULT_SESSION_COOKIE_MAX_AGE,
21
- httpOnly: DEFAULT_SESSION_COOKIE_HTTP_ONLY,
22
- });
23
-
24
- export const SessionOptionsSchema = z
25
- .union([
26
- z.literal(false),
27
- z.object({
28
- secret: z.string(),
29
- resave: z.boolean().optional().default(DEFAULT_SESSION_RESAVE),
30
- saveUninitialized: z
31
- .boolean()
32
- .optional()
33
- .default(DEFAULT_SESSION_SAVE_UNINITIALIZED),
34
- cookie: SessionCookieSchema,
35
- }),
36
- ])
37
- .optional()
38
- .default(false);
39
-
40
- export type SessionOptions = z.infer<typeof SessionOptionsSchema>;
@@ -1,26 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- import { CompressionOptionsSchema } from './compression.js';
4
- import { CookieParserOptionsSchema } from './cookie-parser.js';
5
- import { CorsOptionsSchema } from './cors.js';
6
- import { HelmetOptionsSchema } from './helmet.js';
7
- import { HppOptionsSchema } from './hpp.js';
8
- import { PassportSchema } from './passport.js';
9
- import { PortSchema } from './port.js';
10
- import { RateLimitOptionsSchema } from './rate-limit.js';
11
- import { SessionOptionsSchema } from './session.js';
12
-
13
- export const WnodexConfigSchema = z.object({
14
- port: PortSchema,
15
- helmet: HelmetOptionsSchema,
16
- cors: CorsOptionsSchema,
17
- compression: CompressionOptionsSchema,
18
- rateLimit: RateLimitOptionsSchema,
19
- cookieParser: CookieParserOptionsSchema,
20
- hpp: HppOptionsSchema,
21
- session: SessionOptionsSchema,
22
- passport: PassportSchema,
23
- });
24
-
25
- export type WnodexConfigInput = z.input<typeof WnodexConfigSchema>;
26
- export type WnodexConfigOutput = z.output<typeof WnodexConfigSchema>;
package/src/wnodex.ts DELETED
@@ -1,152 +0,0 @@
1
- import type { Server } from 'node:http';
2
-
3
- import express, { type Application } from 'express';
4
-
5
- import type { Logger } from 'pino';
6
-
7
- import { setupConfig } from './helpers/setup-config.js';
8
- import { setupMiddlewares } from './helpers/setup-middlewares.js';
9
- import { logger } from './logger.js';
10
- import {
11
- type WnodexConfigInput,
12
- type WnodexConfigOutput,
13
- } from './schemas/wnodex-config.js';
14
-
15
- /**
16
- * Main application class for the Wnodex server.
17
- *
18
- * Handles Express app instantiation, configuration validation,
19
- * middleware setup, logging, error handling, server startup, and graceful shutdown.
20
- */
21
- export class Wnodex {
22
- public app: Application;
23
- private server?: Server;
24
- private readonly config: WnodexConfigOutput;
25
- private readonly logger: Logger;
26
-
27
- /**
28
- * Constructs a new Wnodex server instance.
29
- * Initializes the Express app, logger, validates and stores configuration, and applies middlewares.
30
- * @param config Configuration input for Wnodex, validated by WnodexConfigSchema.
31
- * @example
32
- * const wnodex = new Wnodex({ port: 3000 });
33
- */
34
- constructor(config: WnodexConfigInput) {
35
- this.app = express();
36
-
37
- // Logger
38
- this.logger = logger;
39
- this.app.set('logger', logger);
40
-
41
- // Config
42
- this.config = setupConfig(config);
43
-
44
- // Global Middlewares
45
- setupMiddlewares(this.app, this.config);
46
- }
47
-
48
- /**
49
- * Provides access to the application's logger instance.
50
- * @returns Logger instance.
51
- * @example
52
- * const log = wnodex.getLogger();
53
- */
54
- public getLogger(): Logger {
55
- return this.logger;
56
- }
57
-
58
- /**
59
- * Returns the validated Wnodex configuration.
60
- * @returns WnodexConfigOutput object.
61
- * @example
62
- * const config = wnodex.getConfig();
63
- */
64
- public getConfig(): WnodexConfigOutput {
65
- return this.config;
66
- }
67
-
68
- /**
69
- * Public getter for the express app instance.
70
- * @returns The express application instance.
71
- * @example
72
- * const app = Wnodex.getApp();
73
- */
74
- public getApp(): Application {
75
- return this.app;
76
- }
77
-
78
- // --- Wnodex Start Helper ---
79
-
80
- /**
81
- * Starts the Wnodex server and listens on the specified port.
82
- * Resolves when the server is ready.
83
- * @returns Promise that resolves once server is running.
84
- * @example
85
- * await wnodex.start();
86
- */
87
- public start(): Promise<void> {
88
- const logger = this.getLogger();
89
- const { port } = this.getConfig();
90
-
91
- return new Promise((resolve) => {
92
- this.server = this.app.listen(port, () => {
93
- logger.info(`Server running @ http://localhost:${port}`);
94
- resolve();
95
- });
96
-
97
- // Attach error handler immediately after server creation
98
- this.server?.on('error', (err) => {
99
- logger.error(err, 'Server error:');
100
- });
101
- });
102
- }
103
-
104
- // --- Wnodex Graceful Shutdown ---
105
-
106
- /**
107
- * Gracefully shuts down the Wnodex server.
108
- * Stops accepting new connections, allows active connections to finish,
109
- * runs optional asynchronous cleanup chores, and closes the server.
110
- * @param chores An optional async callback function to perform cleanup tasks
111
- * before shutdown (e.g., closing DB connections, clearing cache). This function is awaited before closing the server.
112
- *
113
- * @returns Promise that resolves when the server has stopped gracefully.
114
- *
115
- * @example
116
- * await wnodex.shutdown(async () => {
117
- * await db.close();
118
- * await cache.clear();
119
- * });
120
- */
121
- public async shutdown(chores?: () => Promise<void> | void): Promise<void> {
122
- const logger = this.getLogger();
123
-
124
- if (!this.server) {
125
- logger.warn('Server not running.');
126
- return;
127
- }
128
-
129
- logger.info('Shutdown initiated...');
130
-
131
- if (chores) {
132
- try {
133
- await chores();
134
- } catch (error) {
135
- logger.error(error, 'Error during cleanup chores');
136
- // Proceed with shutdown even if chores fail
137
- }
138
- }
139
-
140
- return new Promise((resolve, reject) => {
141
- this.server!.close((err) => {
142
- if (err) {
143
- logger.error(err, 'Error during server shutdown');
144
- reject(err);
145
- return;
146
- }
147
- logger.info('Server stopped gracefully.');
148
- resolve();
149
- });
150
- });
151
- }
152
- }
package/tsconfig.lib.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "baseUrl": ".",
5
- "rootDir": "src",
6
- "outDir": "dist",
7
- "tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo",
8
- "emitDeclarationOnly": false,
9
- "types": ["node"]
10
- },
11
- "include": ["src/**/*.ts"],
12
- "references": []
13
- }
@@ -1 +0,0 @@
1
- {"fileNames":[],"fileInfos":[],"root":[],"options":{"composite":true,"declarationMap":true,"emitDeclarationOnly":true,"importHelpers":true,"module":199,"noEmitOnError":true,"noFallthroughCasesInSwitch":true,"noImplicitOverride":true,"noImplicitReturns":true,"noUnusedLocals":true,"skipLibCheck":true,"strict":true,"target":9},"version":"5.9.3"}