@morojs/moro 1.5.17 → 1.6.1

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 (138) hide show
  1. package/README.md +48 -65
  2. package/dist/core/auth/morojs-adapter.js +12 -16
  3. package/dist/core/auth/morojs-adapter.js.map +1 -1
  4. package/dist/core/config/file-loader.d.ts +5 -0
  5. package/dist/core/config/file-loader.js +171 -0
  6. package/dist/core/config/file-loader.js.map +1 -1
  7. package/dist/core/config/index.d.ts +10 -39
  8. package/dist/core/config/index.js +29 -66
  9. package/dist/core/config/index.js.map +1 -1
  10. package/dist/core/config/loader.d.ts +7 -0
  11. package/dist/core/config/loader.js +269 -0
  12. package/dist/core/config/loader.js.map +1 -0
  13. package/dist/core/config/schema.js +31 -41
  14. package/dist/core/config/schema.js.map +1 -1
  15. package/dist/core/config/utils.d.ts +2 -9
  16. package/dist/core/config/utils.js +32 -19
  17. package/dist/core/config/utils.js.map +1 -1
  18. package/dist/core/config/validation.d.ts +17 -0
  19. package/dist/core/config/validation.js +131 -0
  20. package/dist/core/config/validation.js.map +1 -0
  21. package/dist/core/database/adapters/mongodb.d.ts +0 -10
  22. package/dist/core/database/adapters/mongodb.js +2 -23
  23. package/dist/core/database/adapters/mongodb.js.map +1 -1
  24. package/dist/core/database/adapters/mysql.d.ts +0 -11
  25. package/dist/core/database/adapters/mysql.js +0 -1
  26. package/dist/core/database/adapters/mysql.js.map +1 -1
  27. package/dist/core/database/adapters/postgresql.d.ts +1 -9
  28. package/dist/core/database/adapters/postgresql.js +1 -1
  29. package/dist/core/database/adapters/postgresql.js.map +1 -1
  30. package/dist/core/database/adapters/redis.d.ts +0 -9
  31. package/dist/core/database/adapters/redis.js +4 -14
  32. package/dist/core/database/adapters/redis.js.map +1 -1
  33. package/dist/core/framework.d.ts +7 -6
  34. package/dist/core/framework.js +16 -131
  35. package/dist/core/framework.js.map +1 -1
  36. package/dist/core/http/http-server.d.ts +0 -12
  37. package/dist/core/http/http-server.js +23 -151
  38. package/dist/core/http/http-server.js.map +1 -1
  39. package/dist/core/http/router.d.ts +0 -12
  40. package/dist/core/http/router.js +36 -114
  41. package/dist/core/http/router.js.map +1 -1
  42. package/dist/core/logger/filters.js +4 -12
  43. package/dist/core/logger/filters.js.map +1 -1
  44. package/dist/core/logger/index.d.ts +1 -1
  45. package/dist/core/logger/index.js +1 -2
  46. package/dist/core/logger/index.js.map +1 -1
  47. package/dist/core/logger/logger.d.ts +13 -29
  48. package/dist/core/logger/logger.js +203 -380
  49. package/dist/core/logger/logger.js.map +1 -1
  50. package/dist/core/logger/outputs.js +2 -0
  51. package/dist/core/logger/outputs.js.map +1 -1
  52. package/dist/core/middleware/built-in/auth.js +17 -88
  53. package/dist/core/middleware/built-in/auth.js.map +1 -1
  54. package/dist/core/middleware/built-in/cache.js +1 -3
  55. package/dist/core/middleware/built-in/cache.js.map +1 -1
  56. package/dist/core/middleware/built-in/index.d.ts +0 -1
  57. package/dist/core/middleware/built-in/index.js +1 -6
  58. package/dist/core/middleware/built-in/index.js.map +1 -1
  59. package/dist/core/middleware/built-in/request-logger.js +2 -3
  60. package/dist/core/middleware/built-in/request-logger.js.map +1 -1
  61. package/dist/core/middleware/built-in/sse.js +7 -9
  62. package/dist/core/middleware/built-in/sse.js.map +1 -1
  63. package/dist/core/modules/auto-discovery.d.ts +0 -17
  64. package/dist/core/modules/auto-discovery.js +12 -367
  65. package/dist/core/modules/auto-discovery.js.map +1 -1
  66. package/dist/core/modules/modules.js +2 -12
  67. package/dist/core/modules/modules.js.map +1 -1
  68. package/dist/core/networking/adapters/ws-adapter.d.ts +1 -1
  69. package/dist/core/networking/adapters/ws-adapter.js +2 -2
  70. package/dist/core/networking/adapters/ws-adapter.js.map +1 -1
  71. package/dist/core/networking/service-discovery.js +7 -7
  72. package/dist/core/networking/service-discovery.js.map +1 -1
  73. package/dist/core/routing/index.d.ts +0 -20
  74. package/dist/core/routing/index.js +13 -178
  75. package/dist/core/routing/index.js.map +1 -1
  76. package/dist/core/runtime/node-adapter.js +6 -12
  77. package/dist/core/runtime/node-adapter.js.map +1 -1
  78. package/dist/moro.d.ts +0 -48
  79. package/dist/moro.js +148 -456
  80. package/dist/moro.js.map +1 -1
  81. package/dist/types/config.d.ts +2 -58
  82. package/dist/types/core.d.ts +40 -34
  83. package/dist/types/http.d.ts +1 -16
  84. package/dist/types/logger.d.ts +0 -7
  85. package/dist/types/module.d.ts +0 -11
  86. package/package.json +2 -2
  87. package/src/core/auth/morojs-adapter.ts +13 -18
  88. package/src/core/config/file-loader.ts +233 -0
  89. package/src/core/config/index.ts +32 -77
  90. package/src/core/config/loader.ts +633 -0
  91. package/src/core/config/schema.ts +31 -41
  92. package/src/core/config/utils.ts +29 -22
  93. package/src/core/config/validation.ts +140 -0
  94. package/src/core/database/README.md +16 -26
  95. package/src/core/database/adapters/mongodb.ts +2 -30
  96. package/src/core/database/adapters/mysql.ts +0 -14
  97. package/src/core/database/adapters/postgresql.ts +2 -12
  98. package/src/core/database/adapters/redis.ts +4 -27
  99. package/src/core/framework.ts +23 -163
  100. package/src/core/http/http-server.ts +36 -176
  101. package/src/core/http/router.ts +38 -127
  102. package/src/core/logger/filters.ts +4 -12
  103. package/src/core/logger/index.ts +0 -1
  104. package/src/core/logger/logger.ts +216 -427
  105. package/src/core/logger/outputs.ts +2 -0
  106. package/src/core/middleware/built-in/auth.ts +17 -98
  107. package/src/core/middleware/built-in/cache.ts +1 -3
  108. package/src/core/middleware/built-in/index.ts +0 -8
  109. package/src/core/middleware/built-in/request-logger.ts +1 -3
  110. package/src/core/middleware/built-in/sse.ts +7 -9
  111. package/src/core/modules/auto-discovery.ts +13 -476
  112. package/src/core/modules/modules.ts +9 -20
  113. package/src/core/networking/adapters/ws-adapter.ts +5 -2
  114. package/src/core/networking/service-discovery.ts +7 -6
  115. package/src/core/routing/index.ts +14 -198
  116. package/src/core/runtime/node-adapter.ts +6 -12
  117. package/src/moro.ts +166 -554
  118. package/src/types/config.ts +2 -59
  119. package/src/types/core.ts +45 -47
  120. package/src/types/http.ts +1 -23
  121. package/src/types/logger.ts +0 -9
  122. package/src/types/module.ts +0 -12
  123. package/dist/core/config/config-manager.d.ts +0 -44
  124. package/dist/core/config/config-manager.js +0 -114
  125. package/dist/core/config/config-manager.js.map +0 -1
  126. package/dist/core/config/config-sources.d.ts +0 -21
  127. package/dist/core/config/config-sources.js +0 -502
  128. package/dist/core/config/config-sources.js.map +0 -1
  129. package/dist/core/config/config-validator.d.ts +0 -21
  130. package/dist/core/config/config-validator.js +0 -765
  131. package/dist/core/config/config-validator.js.map +0 -1
  132. package/dist/core/middleware/built-in/jwt-helpers.d.ts +0 -118
  133. package/dist/core/middleware/built-in/jwt-helpers.js +0 -221
  134. package/dist/core/middleware/built-in/jwt-helpers.js.map +0 -1
  135. package/src/core/config/config-manager.ts +0 -133
  136. package/src/core/config/config-sources.ts +0 -596
  137. package/src/core/config/config-validator.ts +0 -1078
  138. package/src/core/middleware/built-in/jwt-helpers.ts +0 -240
@@ -1,765 +0,0 @@
1
- "use strict";
2
- /**
3
- * Configuration Validator - Type-Safe Schema Validation
4
- *
5
- * This module provides runtime validation for configuration objects using
6
- * simple TypeScript functions that match the type definitions exactly.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.ConfigValidationError = void 0;
10
- exports.validateConfig = validateConfig;
11
- const logger_1 = require("../logger");
12
- const logger = (0, logger_1.createFrameworkLogger)('ConfigValidator');
13
- /**
14
- * Configuration validation error with detailed context
15
- */
16
- class ConfigValidationError extends Error {
17
- field;
18
- value;
19
- expectedType;
20
- constructor(field, value, expectedType, message) {
21
- super(`Configuration validation failed for '${field}': ${message}`);
22
- this.field = field;
23
- this.value = value;
24
- this.expectedType = expectedType;
25
- this.name = 'ConfigValidationError';
26
- }
27
- }
28
- exports.ConfigValidationError = ConfigValidationError;
29
- /**
30
- * Validate and normalize a complete configuration object
31
- * This ensures type safety and provides helpful error messages
32
- */
33
- function validateConfig(config) {
34
- logger.debug('Validating configuration');
35
- try {
36
- const validatedConfig = {
37
- server: validateServerConfig(config.server, 'server'),
38
- serviceDiscovery: validateServiceDiscoveryConfig(config.serviceDiscovery, 'serviceDiscovery'),
39
- database: validateDatabaseConfig(config.database, 'database'),
40
- modules: validateModuleDefaultsConfig(config.modules, 'modules'),
41
- logging: validateLoggingConfig(config.logging, 'logging'),
42
- security: validateSecurityConfig(config.security, 'security'),
43
- external: validateExternalServicesConfig(config.external, 'external'),
44
- performance: validatePerformanceConfig(config.performance, 'performance'),
45
- websocket: validateWebSocketConfig(config.websocket, 'websocket'),
46
- };
47
- logger.debug('Configuration validation successful');
48
- return validatedConfig;
49
- }
50
- catch (error) {
51
- if (error instanceof ConfigValidationError) {
52
- logger.error(`❌ Configuration validation failed for '${error.field}':`, error.message);
53
- // Provide helpful hints
54
- provideValidationHints(error);
55
- throw error;
56
- }
57
- logger.error('❌ Unexpected configuration validation error:', String(error));
58
- throw new Error(`Configuration validation failed: ${String(error)}`);
59
- }
60
- }
61
- /**
62
- * Validate server configuration
63
- */
64
- function validateServerConfig(config, path) {
65
- if (!config || typeof config !== 'object') {
66
- throw new ConfigValidationError(path, config, 'object', 'Server configuration must be an object');
67
- }
68
- return {
69
- port: validatePort(config.port, `${path}.port`),
70
- host: validateString(config.host, `${path}.host`),
71
- maxConnections: validateNumber(config.maxConnections, `${path}.maxConnections`, { min: 1 }),
72
- timeout: validateNumber(config.timeout, `${path}.timeout`, { min: 1000 }),
73
- bodySizeLimit: validateString(config.bodySizeLimit, `${path}.bodySizeLimit`),
74
- requestTracking: {
75
- enabled: validateBoolean(config.requestTracking?.enabled, `${path}.requestTracking.enabled`),
76
- },
77
- errorBoundary: {
78
- enabled: validateBoolean(config.errorBoundary?.enabled, `${path}.errorBoundary.enabled`),
79
- },
80
- };
81
- }
82
- /**
83
- * Validate service discovery configuration
84
- */
85
- function validateServiceDiscoveryConfig(config, path) {
86
- if (!config || typeof config !== 'object') {
87
- throw new ConfigValidationError(path, config, 'object', 'Service discovery configuration must be an object');
88
- }
89
- return {
90
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
91
- type: validateEnum(config.type, ['memory', 'consul', 'kubernetes'], `${path}.type`),
92
- consulUrl: validateString(config.consulUrl, `${path}.consulUrl`),
93
- kubernetesNamespace: validateString(config.kubernetesNamespace, `${path}.kubernetesNamespace`),
94
- healthCheckInterval: validateNumber(config.healthCheckInterval, `${path}.healthCheckInterval`, {
95
- min: 1000,
96
- }),
97
- retryAttempts: validateNumber(config.retryAttempts, `${path}.retryAttempts`, { min: 0 }),
98
- };
99
- }
100
- /**
101
- * Validate database configuration
102
- */
103
- function validateDatabaseConfig(config, path) {
104
- if (!config || typeof config !== 'object') {
105
- throw new ConfigValidationError(path, config, 'object', 'Database configuration must be an object');
106
- }
107
- const result = {};
108
- // Optional URL
109
- if (config.url !== undefined) {
110
- result.url = validateString(config.url, `${path}.url`);
111
- }
112
- // Optional Redis - only validate if present
113
- if (config.redis !== undefined) {
114
- result.redis = validateRedisConfig(config.redis, `${path}.redis`);
115
- }
116
- // Optional MySQL - only validate if present
117
- if (config.mysql !== undefined) {
118
- result.mysql = validateMySQLConfig(config.mysql, `${path}.mysql`);
119
- }
120
- // Optional PostgreSQL - only validate if present
121
- if (config.postgresql !== undefined) {
122
- result.postgresql = validatePostgreSQLConfig(config.postgresql, `${path}.postgresql`);
123
- }
124
- // Optional SQLite - only validate if present
125
- if (config.sqlite !== undefined) {
126
- result.sqlite = validateSQLiteConfig(config.sqlite, `${path}.sqlite`);
127
- }
128
- // Optional MongoDB - only validate if present
129
- if (config.mongodb !== undefined) {
130
- result.mongodb = validateMongoDBConfig(config.mongodb, `${path}.mongodb`);
131
- }
132
- return result;
133
- }
134
- /**
135
- * Validate Redis configuration
136
- */
137
- function validateRedisConfig(config, path) {
138
- if (!config || typeof config !== 'object') {
139
- throw new ConfigValidationError(path, config, 'object', 'Redis configuration must be an object');
140
- }
141
- return {
142
- url: validateString(config.url, `${path}.url`),
143
- maxRetries: validateNumber(config.maxRetries, `${path}.maxRetries`, { min: 0 }),
144
- retryDelay: validateNumber(config.retryDelay, `${path}.retryDelay`, { min: 0 }),
145
- keyPrefix: validateString(config.keyPrefix, `${path}.keyPrefix`),
146
- };
147
- }
148
- /**
149
- * Validate MySQL configuration
150
- */
151
- function validateMySQLConfig(config, path) {
152
- if (!config || typeof config !== 'object') {
153
- throw new ConfigValidationError(path, config, 'object', 'MySQL configuration must be an object');
154
- }
155
- const result = {
156
- host: validateString(config.host, `${path}.host`),
157
- port: validatePort(config.port, `${path}.port`),
158
- connectionLimit: validateNumber(config.connectionLimit, `${path}.connectionLimit`, { min: 1 }),
159
- acquireTimeout: validateNumber(config.acquireTimeout, `${path}.acquireTimeout`, { min: 1000 }),
160
- timeout: validateNumber(config.timeout, `${path}.timeout`, { min: 1000 }),
161
- };
162
- // Optional fields
163
- if (config.database !== undefined) {
164
- result.database = validateString(config.database, `${path}.database`);
165
- }
166
- if (config.username !== undefined) {
167
- result.username = validateString(config.username, `${path}.username`);
168
- }
169
- if (config.password !== undefined) {
170
- result.password = validateString(config.password, `${path}.password`);
171
- }
172
- return result;
173
- }
174
- /**
175
- * Validate PostgreSQL configuration
176
- */
177
- function validatePostgreSQLConfig(config, path) {
178
- if (!config || typeof config !== 'object') {
179
- throw new ConfigValidationError(path, config, 'object', 'PostgreSQL configuration must be an object');
180
- }
181
- const result = {
182
- host: validateString(config.host, `${path}.host`),
183
- port: validatePort(config.port, `${path}.port`),
184
- connectionLimit: validateNumber(config.connectionLimit, `${path}.connectionLimit`, { min: 1 }),
185
- };
186
- // Optional fields
187
- if (config.database !== undefined) {
188
- result.database = validateString(config.database, `${path}.database`);
189
- }
190
- if (config.user !== undefined) {
191
- result.user = validateString(config.user, `${path}.user`);
192
- }
193
- if (config.password !== undefined) {
194
- result.password = validateString(config.password, `${path}.password`);
195
- }
196
- if (config.ssl !== undefined) {
197
- result.ssl = validateBoolean(config.ssl, `${path}.ssl`);
198
- }
199
- return result;
200
- }
201
- /**
202
- * Validate SQLite configuration
203
- */
204
- function validateSQLiteConfig(config, path) {
205
- if (!config || typeof config !== 'object') {
206
- throw new ConfigValidationError(path, config, 'object', 'SQLite configuration must be an object');
207
- }
208
- const result = {
209
- filename: validateString(config.filename, `${path}.filename`),
210
- };
211
- // Optional fields
212
- if (config.memory !== undefined) {
213
- result.memory = validateBoolean(config.memory, `${path}.memory`);
214
- }
215
- if (config.verbose !== undefined) {
216
- result.verbose = validateBoolean(config.verbose, `${path}.verbose`);
217
- }
218
- return result;
219
- }
220
- /**
221
- * Validate MongoDB configuration
222
- */
223
- function validateMongoDBConfig(config, path) {
224
- if (!config || typeof config !== 'object') {
225
- throw new ConfigValidationError(path, config, 'object', 'MongoDB configuration must be an object');
226
- }
227
- const result = {};
228
- // Either url or host+port
229
- if (config.url !== undefined) {
230
- result.url = validateString(config.url, `${path}.url`);
231
- }
232
- if (config.host !== undefined) {
233
- result.host = validateString(config.host, `${path}.host`);
234
- }
235
- if (config.port !== undefined) {
236
- result.port = validatePort(config.port, `${path}.port`);
237
- }
238
- if (config.database !== undefined) {
239
- result.database = validateString(config.database, `${path}.database`);
240
- }
241
- if (config.username !== undefined) {
242
- result.username = validateString(config.username, `${path}.username`);
243
- }
244
- if (config.password !== undefined) {
245
- result.password = validateString(config.password, `${path}.password`);
246
- }
247
- return result;
248
- }
249
- /**
250
- * Validate module defaults configuration
251
- */
252
- function validateModuleDefaultsConfig(config, path) {
253
- if (!config || typeof config !== 'object') {
254
- throw new ConfigValidationError(path, config, 'object', 'Module defaults configuration must be an object');
255
- }
256
- return {
257
- cache: validateCacheConfig(config.cache, `${path}.cache`),
258
- rateLimit: validateRateLimitConfig(config.rateLimit, `${path}.rateLimit`),
259
- validation: validateValidationConfig(config.validation, `${path}.validation`),
260
- autoDiscovery: validateAutoDiscoveryConfig(config.autoDiscovery, `${path}.autoDiscovery`),
261
- };
262
- }
263
- /**
264
- * Validate auto-discovery configuration
265
- */
266
- function validateAutoDiscoveryConfig(config, path) {
267
- if (!config || typeof config !== 'object') {
268
- throw new ConfigValidationError(path, config, 'object', 'Auto-discovery configuration must be an object');
269
- }
270
- return {
271
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
272
- paths: validateStringArray(config.paths, `${path}.paths`),
273
- patterns: validateStringArray(config.patterns, `${path}.patterns`),
274
- recursive: validateBoolean(config.recursive, `${path}.recursive`),
275
- loadingStrategy: validateEnum(config.loadingStrategy, ['eager', 'lazy', 'conditional'], `${path}.loadingStrategy`),
276
- watchForChanges: validateBoolean(config.watchForChanges, `${path}.watchForChanges`),
277
- ignorePatterns: validateStringArray(config.ignorePatterns, `${path}.ignorePatterns`),
278
- loadOrder: validateEnum(config.loadOrder, ['alphabetical', 'dependency', 'custom'], `${path}.loadOrder`),
279
- failOnError: validateBoolean(config.failOnError, `${path}.failOnError`),
280
- maxDepth: validateNumber(config.maxDepth, `${path}.maxDepth`),
281
- };
282
- }
283
- /**
284
- * Validate cache configuration
285
- */
286
- function validateCacheConfig(config, path) {
287
- if (!config || typeof config !== 'object') {
288
- throw new ConfigValidationError(path, config, 'object', 'Cache configuration must be an object');
289
- }
290
- return {
291
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
292
- defaultTtl: validateNumber(config.defaultTtl, `${path}.defaultTtl`, { min: 0 }),
293
- maxSize: validateNumber(config.maxSize, `${path}.maxSize`, { min: 1 }),
294
- strategy: validateEnum(config.strategy, ['lru', 'lfu', 'fifo'], `${path}.strategy`),
295
- };
296
- }
297
- /**
298
- * Validate rate limit configuration
299
- */
300
- function validateRateLimitConfig(config, path) {
301
- if (!config || typeof config !== 'object') {
302
- throw new ConfigValidationError(path, config, 'object', 'Rate limit configuration must be an object');
303
- }
304
- return {
305
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
306
- defaultRequests: validateNumber(config.defaultRequests, `${path}.defaultRequests`, { min: 1 }),
307
- defaultWindow: validateNumber(config.defaultWindow, `${path}.defaultWindow`, { min: 1000 }),
308
- skipSuccessfulRequests: validateBoolean(config.skipSuccessfulRequests, `${path}.skipSuccessfulRequests`),
309
- skipFailedRequests: validateBoolean(config.skipFailedRequests, `${path}.skipFailedRequests`),
310
- };
311
- }
312
- /**
313
- * Validate validation configuration
314
- */
315
- function validateValidationConfig(config, path) {
316
- if (!config || typeof config !== 'object') {
317
- throw new ConfigValidationError(path, config, 'object', 'Validation configuration must be an object');
318
- }
319
- return {
320
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
321
- stripUnknown: validateBoolean(config.stripUnknown, `${path}.stripUnknown`),
322
- abortEarly: validateBoolean(config.abortEarly, `${path}.abortEarly`),
323
- };
324
- }
325
- /**
326
- * Validate logging configuration
327
- */
328
- function validateLoggingConfig(config, path) {
329
- if (!config || typeof config !== 'object') {
330
- throw new ConfigValidationError(path, config, 'object', 'Logging configuration must be an object');
331
- }
332
- return {
333
- level: validateEnum(config.level, ['debug', 'info', 'warn', 'error', 'fatal'], `${path}.level`),
334
- format: validateEnum(config.format, ['pretty', 'json', 'compact'], `${path}.format`),
335
- enableColors: validateBoolean(config.enableColors, `${path}.enableColors`),
336
- enableTimestamp: validateBoolean(config.enableTimestamp, `${path}.enableTimestamp`),
337
- enableContext: validateBoolean(config.enableContext, `${path}.enableContext`),
338
- outputs: validateLoggingOutputsConfig(config.outputs, `${path}.outputs`),
339
- };
340
- }
341
- /**
342
- * Validate logging outputs configuration
343
- */
344
- function validateLoggingOutputsConfig(config, path) {
345
- if (!config || typeof config !== 'object') {
346
- throw new ConfigValidationError(path, config, 'object', 'Logging outputs configuration must be an object');
347
- }
348
- return {
349
- console: validateBoolean(config.console, `${path}.console`),
350
- file: validateLoggingFileConfig(config.file, `${path}.file`),
351
- webhook: validateLoggingWebhookConfig(config.webhook, `${path}.webhook`),
352
- };
353
- }
354
- /**
355
- * Validate logging file configuration
356
- */
357
- function validateLoggingFileConfig(config, path) {
358
- if (!config || typeof config !== 'object') {
359
- throw new ConfigValidationError(path, config, 'object', 'Logging file configuration must be an object');
360
- }
361
- return {
362
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
363
- path: validateString(config.path, `${path}.path`),
364
- maxSize: validateString(config.maxSize, `${path}.maxSize`),
365
- maxFiles: validateNumber(config.maxFiles, `${path}.maxFiles`, { min: 1 }),
366
- };
367
- }
368
- /**
369
- * Validate logging webhook configuration
370
- */
371
- function validateLoggingWebhookConfig(config, path) {
372
- if (!config || typeof config !== 'object') {
373
- throw new ConfigValidationError(path, config, 'object', 'Logging webhook configuration must be an object');
374
- }
375
- const result = {
376
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
377
- headers: validateObject(config.headers, `${path}.headers`),
378
- };
379
- // Optional URL
380
- if (config.url !== undefined) {
381
- result.url = validateString(config.url, `${path}.url`);
382
- }
383
- return result;
384
- }
385
- /**
386
- * Validate security configuration
387
- */
388
- function validateSecurityConfig(config, path) {
389
- if (!config || typeof config !== 'object') {
390
- throw new ConfigValidationError(path, config, 'object', 'Security configuration must be an object');
391
- }
392
- return {
393
- cors: validateCorsConfig(config.cors, `${path}.cors`),
394
- helmet: validateHelmetConfig(config.helmet, `${path}.helmet`),
395
- rateLimit: validateSecurityRateLimitConfig(config.rateLimit, `${path}.rateLimit`),
396
- };
397
- }
398
- /**
399
- * Validate CORS configuration
400
- */
401
- function validateCorsConfig(config, path) {
402
- if (!config || typeof config !== 'object') {
403
- throw new ConfigValidationError(path, config, 'object', 'CORS configuration must be an object');
404
- }
405
- return {
406
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
407
- origin: validateCorsOrigin(config.origin, `${path}.origin`),
408
- methods: validateStringArray(config.methods, `${path}.methods`),
409
- allowedHeaders: validateStringArray(config.allowedHeaders, `${path}.allowedHeaders`),
410
- credentials: validateBoolean(config.credentials, `${path}.credentials`),
411
- };
412
- }
413
- /**
414
- * Validate CORS origin (can be string, array, or boolean)
415
- */
416
- function validateCorsOrigin(value, path) {
417
- if (typeof value === 'boolean' || typeof value === 'string') {
418
- return value;
419
- }
420
- if (Array.isArray(value)) {
421
- return validateStringArray(value, path);
422
- }
423
- throw new ConfigValidationError(path, value, 'string | string[] | boolean', 'Must be a string, array of strings, or boolean');
424
- }
425
- /**
426
- * Validate helmet configuration
427
- */
428
- function validateHelmetConfig(config, path) {
429
- if (!config || typeof config !== 'object') {
430
- throw new ConfigValidationError(path, config, 'object', 'Helmet configuration must be an object');
431
- }
432
- return {
433
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
434
- contentSecurityPolicy: validateBoolean(config.contentSecurityPolicy, `${path}.contentSecurityPolicy`),
435
- hsts: validateBoolean(config.hsts, `${path}.hsts`),
436
- noSniff: validateBoolean(config.noSniff, `${path}.noSniff`),
437
- frameguard: validateBoolean(config.frameguard, `${path}.frameguard`),
438
- };
439
- }
440
- /**
441
- * Validate security rate limit configuration
442
- */
443
- function validateSecurityRateLimitConfig(config, path) {
444
- if (!config || typeof config !== 'object') {
445
- throw new ConfigValidationError(path, config, 'object', 'Security rate limit configuration must be an object');
446
- }
447
- return {
448
- global: validateGlobalRateLimitConfig(config.global, `${path}.global`),
449
- };
450
- }
451
- /**
452
- * Validate global rate limit configuration
453
- */
454
- function validateGlobalRateLimitConfig(config, path) {
455
- if (!config || typeof config !== 'object') {
456
- throw new ConfigValidationError(path, config, 'object', 'Global rate limit configuration must be an object');
457
- }
458
- return {
459
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
460
- requests: validateNumber(config.requests, `${path}.requests`, { min: 1 }),
461
- window: validateNumber(config.window, `${path}.window`, { min: 1000 }),
462
- };
463
- }
464
- /**
465
- * Validate external services configuration
466
- */
467
- function validateExternalServicesConfig(config, path) {
468
- if (!config || typeof config !== 'object') {
469
- throw new ConfigValidationError(path, config, 'object', 'External services configuration must be an object');
470
- }
471
- const result = {};
472
- // Optional services - only validate if present
473
- if (config.stripe !== undefined) {
474
- result.stripe = validateStripeConfig(config.stripe, `${path}.stripe`);
475
- }
476
- if (config.paypal !== undefined) {
477
- result.paypal = validatePayPalConfig(config.paypal, `${path}.paypal`);
478
- }
479
- if (config.smtp !== undefined) {
480
- result.smtp = validateSMTPConfig(config.smtp, `${path}.smtp`);
481
- }
482
- return result;
483
- }
484
- /**
485
- * Validate Stripe configuration
486
- */
487
- function validateStripeConfig(config, path) {
488
- if (!config || typeof config !== 'object') {
489
- throw new ConfigValidationError(path, config, 'object', 'Stripe configuration must be an object');
490
- }
491
- const result = {};
492
- // Optional fields
493
- if (config.secretKey !== undefined) {
494
- result.secretKey = validateString(config.secretKey, `${path}.secretKey`);
495
- }
496
- if (config.publishableKey !== undefined) {
497
- result.publishableKey = validateString(config.publishableKey, `${path}.publishableKey`);
498
- }
499
- if (config.webhookSecret !== undefined) {
500
- result.webhookSecret = validateString(config.webhookSecret, `${path}.webhookSecret`);
501
- }
502
- if (config.apiVersion !== undefined) {
503
- result.apiVersion = validateString(config.apiVersion, `${path}.apiVersion`);
504
- }
505
- else {
506
- result.apiVersion = '2023-10-16'; // Default API version
507
- }
508
- return result;
509
- }
510
- /**
511
- * Validate PayPal configuration
512
- */
513
- function validatePayPalConfig(config, path) {
514
- if (!config || typeof config !== 'object') {
515
- throw new ConfigValidationError(path, config, 'object', 'PayPal configuration must be an object');
516
- }
517
- const result = {
518
- environment: validateEnum(config.environment, ['sandbox', 'production'], `${path}.environment`),
519
- };
520
- // Optional fields
521
- if (config.clientId !== undefined) {
522
- result.clientId = validateString(config.clientId, `${path}.clientId`);
523
- }
524
- if (config.clientSecret !== undefined) {
525
- result.clientSecret = validateString(config.clientSecret, `${path}.clientSecret`);
526
- }
527
- if (config.webhookId !== undefined) {
528
- result.webhookId = validateString(config.webhookId, `${path}.webhookId`);
529
- }
530
- return result;
531
- }
532
- /**
533
- * Validate SMTP configuration
534
- */
535
- function validateSMTPConfig(config, path) {
536
- if (!config || typeof config !== 'object') {
537
- throw new ConfigValidationError(path, config, 'object', 'SMTP configuration must be an object');
538
- }
539
- const result = {
540
- port: validatePort(config.port, `${path}.port`),
541
- secure: validateBoolean(config.secure, `${path}.secure`),
542
- };
543
- // Optional fields
544
- if (config.host !== undefined) {
545
- result.host = validateString(config.host, `${path}.host`);
546
- }
547
- if (config.username !== undefined) {
548
- result.username = validateString(config.username, `${path}.username`);
549
- }
550
- if (config.password !== undefined) {
551
- result.password = validateString(config.password, `${path}.password`);
552
- }
553
- return result;
554
- }
555
- /**
556
- * Validate performance configuration
557
- */
558
- function validatePerformanceConfig(config, path) {
559
- if (!config || typeof config !== 'object') {
560
- throw new ConfigValidationError(path, config, 'object', 'Performance configuration must be an object');
561
- }
562
- return {
563
- compression: validateCompressionConfig(config.compression, `${path}.compression`),
564
- circuitBreaker: validateCircuitBreakerConfig(config.circuitBreaker, `${path}.circuitBreaker`),
565
- clustering: validateClusteringConfig(config.clustering, `${path}.clustering`),
566
- };
567
- }
568
- /**
569
- * Validate compression configuration
570
- */
571
- function validateCompressionConfig(config, path) {
572
- if (!config || typeof config !== 'object') {
573
- throw new ConfigValidationError(path, config, 'object', 'Compression configuration must be an object');
574
- }
575
- return {
576
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
577
- level: validateNumber(config.level, `${path}.level`, { min: 1, max: 9 }),
578
- threshold: validateNumber(config.threshold, `${path}.threshold`, { min: 0 }),
579
- };
580
- }
581
- /**
582
- * Validate circuit breaker configuration
583
- */
584
- function validateCircuitBreakerConfig(config, path) {
585
- if (!config || typeof config !== 'object') {
586
- throw new ConfigValidationError(path, config, 'object', 'Circuit breaker configuration must be an object');
587
- }
588
- return {
589
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
590
- failureThreshold: validateNumber(config.failureThreshold, `${path}.failureThreshold`, {
591
- min: 1,
592
- }),
593
- resetTimeout: validateNumber(config.resetTimeout, `${path}.resetTimeout`, { min: 1000 }),
594
- monitoringPeriod: validateNumber(config.monitoringPeriod, `${path}.monitoringPeriod`, {
595
- min: 1000,
596
- }),
597
- };
598
- }
599
- /**
600
- * Validate clustering configuration
601
- */
602
- function validateClusteringConfig(config, path) {
603
- if (!config || typeof config !== 'object') {
604
- throw new ConfigValidationError(path, config, 'object', 'Clustering configuration must be an object');
605
- }
606
- const result = {
607
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
608
- };
609
- // Workers can be number or 'auto'
610
- if (typeof config.workers === 'string' && config.workers === 'auto') {
611
- result.workers = 'auto';
612
- }
613
- else {
614
- result.workers = validateNumber(config.workers, `${path}.workers`, { min: 1 });
615
- }
616
- // Optional memoryPerWorkerGB
617
- if (config.memoryPerWorkerGB !== undefined) {
618
- result.memoryPerWorkerGB = validateNumber(config.memoryPerWorkerGB, `${path}.memoryPerWorkerGB`, { min: 0.1 });
619
- }
620
- return result;
621
- }
622
- /**
623
- * Validate WebSocket configuration
624
- */
625
- function validateWebSocketConfig(config, path) {
626
- if (!config || typeof config !== 'object') {
627
- throw new ConfigValidationError(path, config, 'object', 'WebSocket configuration must be an object');
628
- }
629
- const result = {
630
- enabled: validateBoolean(config.enabled, `${path}.enabled`),
631
- };
632
- // Optional fields
633
- if (config.adapter !== undefined) {
634
- result.adapter = validateString(config.adapter, `${path}.adapter`);
635
- }
636
- if (config.compression !== undefined) {
637
- result.compression = validateBoolean(config.compression, `${path}.compression`);
638
- }
639
- if (config.customIdGenerator !== undefined) {
640
- result.customIdGenerator = config.customIdGenerator; // Function - no validation needed
641
- }
642
- if (config.options !== undefined) {
643
- result.options = validateWebSocketOptions(config.options, `${path}.options`);
644
- }
645
- return result;
646
- }
647
- /**
648
- * Validate WebSocket options
649
- */
650
- function validateWebSocketOptions(config, path) {
651
- if (!config || typeof config !== 'object') {
652
- throw new ConfigValidationError(path, config, 'object', 'WebSocket options must be an object');
653
- }
654
- const result = {};
655
- if (config.cors !== undefined) {
656
- result.cors = validateWebSocketCorsOptions(config.cors, `${path}.cors`);
657
- }
658
- if (config.path !== undefined) {
659
- result.path = validateString(config.path, `${path}.path`);
660
- }
661
- if (config.maxPayloadLength !== undefined) {
662
- result.maxPayloadLength = validateNumber(config.maxPayloadLength, `${path}.maxPayloadLength`, {
663
- min: 1024,
664
- });
665
- }
666
- return result;
667
- }
668
- /**
669
- * Validate WebSocket CORS options
670
- */
671
- function validateWebSocketCorsOptions(config, path) {
672
- if (!config || typeof config !== 'object') {
673
- throw new ConfigValidationError(path, config, 'object', 'WebSocket CORS options must be an object');
674
- }
675
- const result = {};
676
- if (config.origin !== undefined) {
677
- result.origin = validateCorsOrigin(config.origin, `${path}.origin`);
678
- }
679
- if (config.credentials !== undefined) {
680
- result.credentials = validateBoolean(config.credentials, `${path}.credentials`);
681
- }
682
- return result;
683
- }
684
- // Basic validation functions
685
- function validatePort(value, path) {
686
- const num = Number(value);
687
- if (isNaN(num) || num < 1 || num > 65535) {
688
- throw new ConfigValidationError(path, value, 'number (1-65535)', 'Must be a number between 1 and 65535');
689
- }
690
- return num;
691
- }
692
- function validateBoolean(value, path) {
693
- if (value === 'true' || value === true)
694
- return true;
695
- if (value === 'false' || value === false)
696
- return false;
697
- if (value === '1' || value === 1)
698
- return true;
699
- if (value === '0' || value === 0)
700
- return false;
701
- throw new ConfigValidationError(path, value, 'boolean', 'Must be a boolean (true/false) or numeric (1/0)');
702
- }
703
- function validateNumber(value, path, options = {}) {
704
- const num = Number(value);
705
- if (isNaN(num)) {
706
- throw new ConfigValidationError(path, value, 'number', 'Must be a valid number');
707
- }
708
- if (options.min !== undefined && num < options.min) {
709
- throw new ConfigValidationError(path, value, `number >= ${options.min}`, `Must be at least ${options.min}`);
710
- }
711
- if (options.max !== undefined && num > options.max) {
712
- throw new ConfigValidationError(path, value, `number <= ${options.max}`, `Must be at most ${options.max}`);
713
- }
714
- return num;
715
- }
716
- function validateString(value, path) {
717
- if (typeof value !== 'string') {
718
- throw new ConfigValidationError(path, value, 'string', 'Must be a string');
719
- }
720
- return value;
721
- }
722
- function validateEnum(value, validValues, path) {
723
- const str = validateString(value, path);
724
- if (!validValues.includes(str)) {
725
- throw new ConfigValidationError(path, value, `one of: ${validValues.join(', ')}`, `Must be one of: ${validValues.join(', ')}`);
726
- }
727
- return str;
728
- }
729
- function validateStringArray(value, path) {
730
- if (!Array.isArray(value)) {
731
- // Try to parse comma-separated string
732
- if (typeof value === 'string') {
733
- return value
734
- .split(',')
735
- .map(s => s.trim())
736
- .filter(s => s.length > 0);
737
- }
738
- throw new ConfigValidationError(path, value, 'string[]', 'Must be an array or comma-separated string');
739
- }
740
- return value.map((item, index) => validateString(item, `${path}[${index}]`));
741
- }
742
- function validateObject(value, path) {
743
- if (!value || typeof value !== 'object' || Array.isArray(value)) {
744
- throw new ConfigValidationError(path, value, 'object', 'Must be an object');
745
- }
746
- return value;
747
- }
748
- /**
749
- * Provide helpful validation hints based on the error
750
- */
751
- function provideValidationHints(error) {
752
- if (error.field.includes('port')) {
753
- logger.error(' 💡 Hint: Ports must be numbers between 1 and 65535');
754
- }
755
- if (error.field.includes('url')) {
756
- logger.error(' 💡 Hint: URLs must include protocol (http:// or https://)');
757
- }
758
- if (error.field.includes('environment')) {
759
- logger.error(' 💡 Hint: NODE_ENV must be one of: development, staging, production');
760
- }
761
- if (error.field.includes('level')) {
762
- logger.error(' 💡 Hint: Log level must be one of: debug, info, warn, error, fatal');
763
- }
764
- }
765
- //# sourceMappingURL=config-validator.js.map