@zintrust/workers 0.1.27

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 (178) hide show
  1. package/README.md +861 -0
  2. package/dist/AnomalyDetection.d.ts +102 -0
  3. package/dist/AnomalyDetection.js +321 -0
  4. package/dist/AutoScaler.d.ts +127 -0
  5. package/dist/AutoScaler.js +425 -0
  6. package/dist/BroadcastWorker.d.ts +21 -0
  7. package/dist/BroadcastWorker.js +24 -0
  8. package/dist/CanaryController.d.ts +103 -0
  9. package/dist/CanaryController.js +380 -0
  10. package/dist/ChaosEngineering.d.ts +79 -0
  11. package/dist/ChaosEngineering.js +216 -0
  12. package/dist/CircuitBreaker.d.ts +106 -0
  13. package/dist/CircuitBreaker.js +374 -0
  14. package/dist/ClusterLock.d.ts +90 -0
  15. package/dist/ClusterLock.js +385 -0
  16. package/dist/ComplianceManager.d.ts +177 -0
  17. package/dist/ComplianceManager.js +556 -0
  18. package/dist/DatacenterOrchestrator.d.ts +133 -0
  19. package/dist/DatacenterOrchestrator.js +404 -0
  20. package/dist/DeadLetterQueue.d.ts +122 -0
  21. package/dist/DeadLetterQueue.js +539 -0
  22. package/dist/HealthMonitor.d.ts +42 -0
  23. package/dist/HealthMonitor.js +301 -0
  24. package/dist/MultiQueueWorker.d.ts +89 -0
  25. package/dist/MultiQueueWorker.js +277 -0
  26. package/dist/NotificationWorker.d.ts +21 -0
  27. package/dist/NotificationWorker.js +23 -0
  28. package/dist/Observability.d.ts +153 -0
  29. package/dist/Observability.js +530 -0
  30. package/dist/PluginManager.d.ts +123 -0
  31. package/dist/PluginManager.js +392 -0
  32. package/dist/PriorityQueue.d.ts +117 -0
  33. package/dist/PriorityQueue.js +244 -0
  34. package/dist/ResourceMonitor.d.ts +164 -0
  35. package/dist/ResourceMonitor.js +605 -0
  36. package/dist/SLAMonitor.d.ts +110 -0
  37. package/dist/SLAMonitor.js +274 -0
  38. package/dist/WorkerFactory.d.ts +193 -0
  39. package/dist/WorkerFactory.js +1507 -0
  40. package/dist/WorkerInit.d.ts +85 -0
  41. package/dist/WorkerInit.js +223 -0
  42. package/dist/WorkerMetrics.d.ts +114 -0
  43. package/dist/WorkerMetrics.js +509 -0
  44. package/dist/WorkerRegistry.d.ts +145 -0
  45. package/dist/WorkerRegistry.js +319 -0
  46. package/dist/WorkerShutdown.d.ts +61 -0
  47. package/dist/WorkerShutdown.js +159 -0
  48. package/dist/WorkerVersioning.d.ts +107 -0
  49. package/dist/WorkerVersioning.js +300 -0
  50. package/dist/build-manifest.json +462 -0
  51. package/dist/config/workerConfig.d.ts +3 -0
  52. package/dist/config/workerConfig.js +19 -0
  53. package/dist/createQueueWorker.d.ts +23 -0
  54. package/dist/createQueueWorker.js +113 -0
  55. package/dist/dashboard/index.d.ts +1 -0
  56. package/dist/dashboard/index.js +1 -0
  57. package/dist/dashboard/types.d.ts +117 -0
  58. package/dist/dashboard/types.js +1 -0
  59. package/dist/dashboard/workers-api.d.ts +4 -0
  60. package/dist/dashboard/workers-api.js +638 -0
  61. package/dist/dashboard/workers-dashboard-ui.d.ts +3 -0
  62. package/dist/dashboard/workers-dashboard-ui.js +1026 -0
  63. package/dist/dashboard/workers-dashboard.d.ts +4 -0
  64. package/dist/dashboard/workers-dashboard.js +904 -0
  65. package/dist/helper/index.d.ts +5 -0
  66. package/dist/helper/index.js +10 -0
  67. package/dist/http/WorkerApiController.d.ts +38 -0
  68. package/dist/http/WorkerApiController.js +312 -0
  69. package/dist/http/WorkerController.d.ts +374 -0
  70. package/dist/http/WorkerController.js +1351 -0
  71. package/dist/http/middleware/CustomValidation.d.ts +92 -0
  72. package/dist/http/middleware/CustomValidation.js +270 -0
  73. package/dist/http/middleware/DatacenterValidator.d.ts +3 -0
  74. package/dist/http/middleware/DatacenterValidator.js +94 -0
  75. package/dist/http/middleware/EditWorkerValidation.d.ts +7 -0
  76. package/dist/http/middleware/EditWorkerValidation.js +55 -0
  77. package/dist/http/middleware/FeaturesValidator.d.ts +3 -0
  78. package/dist/http/middleware/FeaturesValidator.js +60 -0
  79. package/dist/http/middleware/InfrastructureValidator.d.ts +31 -0
  80. package/dist/http/middleware/InfrastructureValidator.js +226 -0
  81. package/dist/http/middleware/OptionsValidator.d.ts +3 -0
  82. package/dist/http/middleware/OptionsValidator.js +112 -0
  83. package/dist/http/middleware/PayloadSanitizer.d.ts +7 -0
  84. package/dist/http/middleware/PayloadSanitizer.js +42 -0
  85. package/dist/http/middleware/ProcessorPathSanitizer.d.ts +3 -0
  86. package/dist/http/middleware/ProcessorPathSanitizer.js +74 -0
  87. package/dist/http/middleware/QueueNameSanitizer.d.ts +3 -0
  88. package/dist/http/middleware/QueueNameSanitizer.js +45 -0
  89. package/dist/http/middleware/ValidateDriver.d.ts +7 -0
  90. package/dist/http/middleware/ValidateDriver.js +20 -0
  91. package/dist/http/middleware/VersionSanitizer.d.ts +3 -0
  92. package/dist/http/middleware/VersionSanitizer.js +25 -0
  93. package/dist/http/middleware/WorkerNameSanitizer.d.ts +3 -0
  94. package/dist/http/middleware/WorkerNameSanitizer.js +46 -0
  95. package/dist/http/middleware/WorkerValidationChain.d.ts +27 -0
  96. package/dist/http/middleware/WorkerValidationChain.js +185 -0
  97. package/dist/index.d.ts +46 -0
  98. package/dist/index.js +48 -0
  99. package/dist/routes/workers.d.ts +12 -0
  100. package/dist/routes/workers.js +81 -0
  101. package/dist/storage/WorkerStore.d.ts +45 -0
  102. package/dist/storage/WorkerStore.js +195 -0
  103. package/dist/type.d.ts +76 -0
  104. package/dist/type.js +1 -0
  105. package/dist/ui/router/ui.d.ts +3 -0
  106. package/dist/ui/router/ui.js +83 -0
  107. package/dist/ui/types/worker-ui.d.ts +229 -0
  108. package/dist/ui/types/worker-ui.js +5 -0
  109. package/package.json +53 -0
  110. package/src/AnomalyDetection.ts +434 -0
  111. package/src/AutoScaler.ts +654 -0
  112. package/src/BroadcastWorker.ts +34 -0
  113. package/src/CanaryController.ts +531 -0
  114. package/src/ChaosEngineering.ts +301 -0
  115. package/src/CircuitBreaker.ts +495 -0
  116. package/src/ClusterLock.ts +499 -0
  117. package/src/ComplianceManager.ts +815 -0
  118. package/src/DatacenterOrchestrator.ts +561 -0
  119. package/src/DeadLetterQueue.ts +733 -0
  120. package/src/HealthMonitor.ts +390 -0
  121. package/src/MultiQueueWorker.ts +431 -0
  122. package/src/NotificationWorker.ts +33 -0
  123. package/src/Observability.ts +696 -0
  124. package/src/PluginManager.ts +551 -0
  125. package/src/PriorityQueue.ts +351 -0
  126. package/src/ResourceMonitor.ts +769 -0
  127. package/src/SLAMonitor.ts +408 -0
  128. package/src/WorkerFactory.ts +2108 -0
  129. package/src/WorkerInit.ts +313 -0
  130. package/src/WorkerMetrics.ts +709 -0
  131. package/src/WorkerRegistry.ts +443 -0
  132. package/src/WorkerShutdown.ts +210 -0
  133. package/src/WorkerVersioning.ts +422 -0
  134. package/src/config/workerConfig.ts +25 -0
  135. package/src/createQueueWorker.ts +174 -0
  136. package/src/dashboard/index.ts +6 -0
  137. package/src/dashboard/types.ts +141 -0
  138. package/src/dashboard/workers-api.ts +785 -0
  139. package/src/dashboard/zintrust.svg +30 -0
  140. package/src/helper/index.ts +11 -0
  141. package/src/http/WorkerApiController.ts +369 -0
  142. package/src/http/WorkerController.ts +1512 -0
  143. package/src/http/middleware/CustomValidation.ts +360 -0
  144. package/src/http/middleware/DatacenterValidator.ts +124 -0
  145. package/src/http/middleware/EditWorkerValidation.ts +74 -0
  146. package/src/http/middleware/FeaturesValidator.ts +82 -0
  147. package/src/http/middleware/InfrastructureValidator.ts +295 -0
  148. package/src/http/middleware/OptionsValidator.ts +144 -0
  149. package/src/http/middleware/PayloadSanitizer.ts +52 -0
  150. package/src/http/middleware/ProcessorPathSanitizer.ts +86 -0
  151. package/src/http/middleware/QueueNameSanitizer.ts +55 -0
  152. package/src/http/middleware/ValidateDriver.ts +29 -0
  153. package/src/http/middleware/VersionSanitizer.ts +30 -0
  154. package/src/http/middleware/WorkerNameSanitizer.ts +56 -0
  155. package/src/http/middleware/WorkerValidationChain.ts +230 -0
  156. package/src/index.ts +98 -0
  157. package/src/routes/workers.ts +154 -0
  158. package/src/storage/WorkerStore.ts +240 -0
  159. package/src/type.ts +89 -0
  160. package/src/types/queue-monitor.d.ts +38 -0
  161. package/src/types/queue-redis.d.ts +38 -0
  162. package/src/ui/README.md +13 -0
  163. package/src/ui/components/JsonEditor.js +670 -0
  164. package/src/ui/components/JsonViewer.js +387 -0
  165. package/src/ui/components/WorkerCard.js +178 -0
  166. package/src/ui/components/WorkerExpandPanel.js +257 -0
  167. package/src/ui/components/fetcher.js +42 -0
  168. package/src/ui/components/sla-scorecard.js +32 -0
  169. package/src/ui/components/styles.css +30 -0
  170. package/src/ui/components/table-expander.js +34 -0
  171. package/src/ui/integration/worker-ui-integration.js +565 -0
  172. package/src/ui/router/ui.ts +99 -0
  173. package/src/ui/services/workerApi.js +240 -0
  174. package/src/ui/types/worker-ui.ts +283 -0
  175. package/src/ui/utils/jsonValidator.js +444 -0
  176. package/src/ui/workers/index.html +202 -0
  177. package/src/ui/workers/main.js +1781 -0
  178. package/src/ui/workers/styles.css +1350 -0
@@ -0,0 +1,360 @@
1
+ import { Logger, type IRequest, type IResponse } from '@zintrust/core';
2
+
3
+ export type RouteHandler = (req: IRequest, res: IResponse) => Promise<void> | void;
4
+
5
+ export interface ValidationSchema {
6
+ [key: string]: {
7
+ required?: boolean;
8
+ default?: unknown;
9
+ type?: 'string' | 'number' | 'boolean' | 'object' | 'array';
10
+ minLength?: number;
11
+ maxLength?: number;
12
+ min?: number;
13
+ max?: number;
14
+ pattern?: RegExp;
15
+ allowedValues?: unknown[];
16
+ custom?: (value: unknown) => string | null; // Returns error message or null
17
+ };
18
+ }
19
+
20
+ /**
21
+ * Custom validation middleware that accepts a validation schema
22
+ * Provides flexible validation for any request data structure
23
+ */
24
+ const validateRequiredField = (
25
+ fieldName: string,
26
+ value: unknown,
27
+ fieldSchema: ValidationSchema[string]
28
+ ): { field: string; message: string; code: string } | null => {
29
+ if (fieldSchema.required && (value === undefined || value === null)) {
30
+ return {
31
+ field: fieldName,
32
+ message: `${fieldName} is required`,
33
+ code: 'MISSING_REQUIRED_FIELD',
34
+ };
35
+ }
36
+ return null;
37
+ };
38
+
39
+ const validateFieldType = (
40
+ fieldName: string,
41
+ value: unknown,
42
+ fieldSchema: ValidationSchema[string]
43
+ ): { field: string; message: string; code: string } | null => {
44
+ if (!fieldSchema.type) return null;
45
+
46
+ const actualType = Array.isArray(value) ? 'array' : typeof value;
47
+ if (actualType !== fieldSchema.type) {
48
+ return {
49
+ field: fieldName,
50
+ message: `${fieldName} must be of type ${fieldSchema.type}`,
51
+ code: 'INVALID_TYPE',
52
+ };
53
+ }
54
+ return null;
55
+ };
56
+
57
+ const validateStringField = (
58
+ fieldName: string,
59
+ value: string,
60
+ fieldSchema: ValidationSchema[string]
61
+ ): { field: string; message: string; code: string }[] => {
62
+ const errors: { field: string; message: string; code: string }[] = [];
63
+
64
+ if (fieldSchema.minLength !== undefined && value.length < fieldSchema.minLength) {
65
+ errors.push({
66
+ field: fieldName,
67
+ message: `${fieldName} must be at least ${fieldSchema.minLength} characters long`,
68
+ code: 'STRING_TOO_SHORT',
69
+ });
70
+ }
71
+
72
+ if (fieldSchema.maxLength !== undefined && value.length > fieldSchema.maxLength) {
73
+ errors.push({
74
+ field: fieldName,
75
+ message: `${fieldName} must be at most ${fieldSchema.maxLength} characters long`,
76
+ code: 'STRING_TOO_LONG',
77
+ });
78
+ }
79
+
80
+ if (fieldSchema.pattern && !fieldSchema.pattern.test(value)) {
81
+ errors.push({
82
+ field: fieldName,
83
+ message: `${fieldName} format is invalid`,
84
+ code: 'INVALID_FORMAT',
85
+ });
86
+ }
87
+
88
+ return errors;
89
+ };
90
+
91
+ const validateNumberField = (
92
+ fieldName: string,
93
+ value: number,
94
+ fieldSchema: ValidationSchema[string]
95
+ ): { field: string; message: string; code: string }[] => {
96
+ const errors: { field: string; message: string; code: string }[] = [];
97
+
98
+ if (fieldSchema.min !== undefined && value < fieldSchema.min) {
99
+ errors.push({
100
+ field: fieldName,
101
+ message: `${fieldName} must be at least ${fieldSchema.min}`,
102
+ code: 'NUMBER_TOO_SMALL',
103
+ });
104
+ }
105
+
106
+ if (fieldSchema.max !== undefined && value > fieldSchema.max) {
107
+ errors.push({
108
+ field: fieldName,
109
+ message: `${fieldName} must be at most ${fieldSchema.max}`,
110
+ code: 'NUMBER_TOO_LARGE',
111
+ });
112
+ }
113
+
114
+ return errors;
115
+ };
116
+
117
+ const validateArrayField = (
118
+ fieldName: string,
119
+ value: unknown[],
120
+ fieldSchema: ValidationSchema[string]
121
+ ): { field: string; message: string; code: string }[] => {
122
+ const errors: { field: string; message: string; code: string }[] = [];
123
+
124
+ if (fieldSchema.minLength !== undefined && value.length < fieldSchema.minLength) {
125
+ errors.push({
126
+ field: fieldName,
127
+ message: `${fieldName} must contain at least ${fieldSchema.minLength} items`,
128
+ code: 'ARRAY_TOO_SHORT',
129
+ });
130
+ }
131
+
132
+ if (fieldSchema.maxLength !== undefined && value.length > fieldSchema.maxLength) {
133
+ errors.push({
134
+ field: fieldName,
135
+ message: `${fieldName} must contain at most ${fieldSchema.maxLength} items`,
136
+ code: 'ARRAY_TOO_LONG',
137
+ });
138
+ }
139
+
140
+ return errors;
141
+ };
142
+
143
+ const validateAllowedValues = (
144
+ fieldName: string,
145
+ value: unknown,
146
+ fieldSchema: ValidationSchema[string]
147
+ ): { field: string; message: string; code: string } | null => {
148
+ if (fieldSchema.allowedValues && !fieldSchema.allowedValues.includes(value)) {
149
+ return {
150
+ field: fieldName,
151
+ message: `${fieldName} must be one of: ${fieldSchema.allowedValues.join(', ')}`,
152
+ code: 'INVALID_VALUE',
153
+ };
154
+ }
155
+ return null;
156
+ };
157
+
158
+ const validateCustomRule = (
159
+ fieldName: string,
160
+ value: unknown,
161
+ fieldSchema: ValidationSchema[string]
162
+ ): { field: string; message: string; code: string } | null => {
163
+ if (fieldSchema.custom) {
164
+ const customError = fieldSchema.custom(value);
165
+ if (customError) {
166
+ return {
167
+ field: fieldName,
168
+ message: customError,
169
+ code: 'CUSTOM_VALIDATION_FAILED',
170
+ };
171
+ }
172
+ }
173
+ return null;
174
+ };
175
+
176
+ const validateField = (
177
+ fieldName: string,
178
+ value: unknown,
179
+ fieldSchema: ValidationSchema[string]
180
+ ): { field: string; message: string; code: string }[] => {
181
+ const errors: { field: string; message: string; code: string }[] = [];
182
+
183
+ // Check required field
184
+ const requiredError = validateRequiredField(fieldName, value, fieldSchema);
185
+ if (requiredError) {
186
+ errors.push(requiredError);
187
+ return errors; // Skip other validations for missing required field
188
+ }
189
+
190
+ // Skip validation if field is not provided and not required
191
+ if (value === undefined || value === null) {
192
+ return errors;
193
+ }
194
+
195
+ // Type validation
196
+ const typeError = validateFieldType(fieldName, value, fieldSchema);
197
+ if (typeError) {
198
+ errors.push(typeError);
199
+ return errors; // Skip other validations for wrong type
200
+ }
201
+
202
+ // String validations
203
+ if (typeof value === 'string') {
204
+ errors.push(...validateStringField(fieldName, value, fieldSchema));
205
+ }
206
+
207
+ // Number validations
208
+ if (typeof value === 'number') {
209
+ errors.push(...validateNumberField(fieldName, value, fieldSchema));
210
+ }
211
+
212
+ // Array validations
213
+ if (Array.isArray(value)) {
214
+ errors.push(...validateArrayField(fieldName, value, fieldSchema));
215
+ }
216
+
217
+ // Allowed values validation
218
+ const allowedValuesError = validateAllowedValues(fieldName, value, fieldSchema);
219
+ if (allowedValuesError) {
220
+ errors.push(allowedValuesError);
221
+ }
222
+
223
+ // Custom validation
224
+ const customError = validateCustomRule(fieldName, value, fieldSchema);
225
+ if (customError) {
226
+ errors.push(customError);
227
+ }
228
+
229
+ return errors;
230
+ };
231
+
232
+ export const withCustomValidation = (
233
+ schema: ValidationSchema,
234
+ handler: RouteHandler
235
+ ): RouteHandler => {
236
+ return async (req: IRequest, res: IResponse): Promise<void> => {
237
+ try {
238
+ const currentBody = req.getBody() as Record<string, unknown>;
239
+ const data = { ...req.data() } as Record<string, unknown>;
240
+
241
+ // Apply defaults from schema into request body when missing
242
+ for (const [fieldName, fieldSchema] of Object.entries(schema)) {
243
+ if (
244
+ (data[fieldName] === undefined || data[fieldName] === null) &&
245
+ fieldSchema.default !== undefined
246
+ ) {
247
+ data[fieldName] = fieldSchema.default;
248
+ }
249
+ }
250
+
251
+ // Persist defaults back into request body so downstream handlers see them
252
+ req.setBody({ ...currentBody, ...data });
253
+ const errors: Array<{ field: string; message: string; code: string }> = [];
254
+
255
+ // Validate each field in the schema
256
+ for (const [fieldName, fieldSchema] of Object.entries(schema)) {
257
+ const value = data[fieldName];
258
+ const fieldErrors = validateField(fieldName, value, fieldSchema);
259
+ errors.push(...fieldErrors);
260
+ }
261
+
262
+ // Return errors if any validation failed
263
+ if (errors.length > 0) {
264
+ return res.setStatus(400).json({
265
+ error: 'Validation failed',
266
+ message: 'Request data validation failed',
267
+ code: 'VALIDATION_FAILED',
268
+ details: errors,
269
+ });
270
+ }
271
+
272
+ return handler(req, res);
273
+ } catch (error) {
274
+ Logger.error('Custom validation failed', error);
275
+ return res.setStatus(500).json({
276
+ error: 'Internal validation error',
277
+ code: 'VALIDATION_ERROR',
278
+ });
279
+ }
280
+ };
281
+ };
282
+
283
+ /**
284
+ * Predefined validation schemas for common use cases
285
+ */
286
+ export const ValidationSchemas = {
287
+ /**
288
+ * Schema for pagination parameters
289
+ */
290
+ pagination: {
291
+ page: {
292
+ type: 'number' as const,
293
+ min: 1,
294
+ default: 1,
295
+ },
296
+ limit: {
297
+ type: 'number' as const,
298
+ min: 1,
299
+ max: 100,
300
+ default: 20,
301
+ },
302
+ },
303
+
304
+ /**
305
+ * Schema for date range filtering
306
+ */
307
+ dateRange: {
308
+ startDate: {
309
+ type: 'string' as const,
310
+ pattern: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/,
311
+ },
312
+ endDate: {
313
+ type: 'string' as const,
314
+ pattern: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/,
315
+ },
316
+ },
317
+
318
+ /**
319
+ * Schema for sorting parameters
320
+ */
321
+ sorting: {
322
+ sortBy: {
323
+ type: 'string' as const,
324
+ allowedValues: ['name', 'status', 'createdAt', 'updatedAt', 'queueName'],
325
+ default: 'createdAt',
326
+ },
327
+ sortOrder: {
328
+ type: 'string' as const,
329
+ allowedValues: ['asc', 'desc'],
330
+ default: 'desc',
331
+ },
332
+ },
333
+
334
+ /**
335
+ * Schema for worker filtering
336
+ */
337
+ workerFilter: {
338
+ status: {
339
+ type: 'string' as const,
340
+ allowedValues: ['running', 'stopped', 'failed', 'paused', ''],
341
+ optional: true,
342
+ },
343
+ queueName: {
344
+ type: 'string' as const,
345
+ minLength: 3,
346
+ maxLength: 50,
347
+ optional: true,
348
+ },
349
+ driver: {
350
+ type: 'string' as const,
351
+ allowedValues: ['db', 'redis', 'memory', ''],
352
+ optional: true,
353
+ },
354
+ search: {
355
+ type: 'string' as const,
356
+ maxLength: 100,
357
+ optional: true,
358
+ },
359
+ },
360
+ };
@@ -0,0 +1,124 @@
1
+ import { Logger, type IRequest, type IResponse } from '@zintrust/core';
2
+
3
+ export type RouteHandler = (req: IRequest, res: IResponse) => Promise<void> | void;
4
+
5
+ interface DatacenterConfig {
6
+ primaryRegion: string;
7
+ secondaryRegions: string[];
8
+ affinityRules: {
9
+ preferLocal: boolean;
10
+ maxLatency: number;
11
+ avoidRegions: string[];
12
+ };
13
+ }
14
+
15
+ const validateRegion = (region: string): boolean => {
16
+ // Allow custom region names but validate format
17
+ return /^[a-z0-9-]+$/.test(region) && region.length >= 3 && region.length <= 20;
18
+ };
19
+
20
+ const validatePrimaryRegion = (primaryRegion: string): string | null => {
21
+ if (!primaryRegion) {
22
+ return 'Primary region is required';
23
+ }
24
+
25
+ if (!validateRegion(primaryRegion)) {
26
+ return 'Primary region must be 3-20 characters, lowercase letters, numbers, and hyphens only';
27
+ }
28
+
29
+ return null;
30
+ };
31
+
32
+ const validateSecondaryRegions = (secondaryRegions: string[]): string | null => {
33
+ if (!Array.isArray(secondaryRegions)) {
34
+ return 'Secondary regions must be an array';
35
+ }
36
+
37
+ for (const region of secondaryRegions) {
38
+ if (!validateRegion(region)) {
39
+ return `Secondary region '${region}' must be 3-20 characters, lowercase letters, numbers, and hyphens only`;
40
+ }
41
+ }
42
+
43
+ return null;
44
+ };
45
+
46
+ const validateAffinityRules = (affinityRules: DatacenterConfig['affinityRules']): string | null => {
47
+ if (!affinityRules) {
48
+ return 'Affinity rules are required';
49
+ }
50
+
51
+ if (typeof affinityRules.preferLocal !== 'boolean') {
52
+ return 'Affinity rule preferLocal must be a boolean';
53
+ }
54
+
55
+ if (typeof affinityRules.maxLatency !== 'number' || affinityRules.maxLatency < 0) {
56
+ return 'Affinity rule maxLatency must be a non-negative number';
57
+ }
58
+
59
+ if (!Array.isArray(affinityRules.avoidRegions)) {
60
+ return 'Affinity rule avoidRegions must be an array';
61
+ }
62
+
63
+ for (const region of affinityRules.avoidRegions) {
64
+ if (!validateRegion(region)) {
65
+ return `Avoid region '${region}' must be 3-20 characters, lowercase letters, numbers, and hyphens only`;
66
+ }
67
+ }
68
+
69
+ return null;
70
+ };
71
+
72
+ export const withDatacenterValidation = (handler: RouteHandler): RouteHandler => {
73
+ return async (req: IRequest, res: IResponse): Promise<void> => {
74
+ try {
75
+ const data = req.data();
76
+ const datacenter = data['datacenter'] as DatacenterConfig;
77
+
78
+ if (!datacenter) {
79
+ return res.setStatus(400).json({
80
+ error: 'Datacenter configuration is required',
81
+ code: 'MISSING_DATACENTER',
82
+ });
83
+ }
84
+
85
+ // Validate primary region
86
+ const primaryRegionError = validatePrimaryRegion(datacenter.primaryRegion);
87
+ if (primaryRegionError) {
88
+ return res.setStatus(400).json({
89
+ error: 'Invalid primary region',
90
+ message: primaryRegionError,
91
+ code: 'INVALID_PRIMARY_REGION',
92
+ });
93
+ }
94
+
95
+ // Validate secondary regions
96
+ const secondaryRegionsError = validateSecondaryRegions(datacenter.secondaryRegions);
97
+ if (secondaryRegionsError) {
98
+ return res.setStatus(400).json({
99
+ error: 'Invalid secondary regions',
100
+ message: secondaryRegionsError,
101
+ code: 'INVALID_SECONDARY_REGIONS',
102
+ });
103
+ }
104
+
105
+ // Validate affinity rules
106
+ const affinityRulesError = validateAffinityRules(datacenter.affinityRules);
107
+ if (affinityRulesError) {
108
+ return res.setStatus(400).json({
109
+ error: 'Invalid affinity rules',
110
+ message: affinityRulesError,
111
+ code: 'INVALID_AFFINITY_RULES',
112
+ });
113
+ }
114
+
115
+ return handler(req, res);
116
+ } catch (error) {
117
+ Logger.error('Datacenter validation failed', error);
118
+ return res.setStatus(500).json({
119
+ error: 'Internal validation error',
120
+ code: 'VALIDATION_ERROR',
121
+ });
122
+ }
123
+ };
124
+ };
@@ -0,0 +1,74 @@
1
+ import { Logger, type IRequest, type IResponse } from '@zintrust/core';
2
+
3
+ export type RouteHandler = (req: IRequest, res: IResponse) => Promise<void> | void;
4
+
5
+ // Import individual validators
6
+ import { withDatacenterValidation } from './DatacenterValidator';
7
+ import { withFeaturesValidation } from './FeaturesValidator';
8
+ import { withInfrastructureValidation } from './InfrastructureValidator';
9
+ import { withStrictPayloadKeys } from './PayloadSanitizer';
10
+ import { withProcessorPathValidation } from './ProcessorPathSanitizer';
11
+ import { withQueueNameValidation } from './QueueNameSanitizer';
12
+ import { withVersionValidation } from './VersionSanitizer';
13
+ import { withWorkerNameValidation } from './WorkerNameSanitizer';
14
+
15
+ /**
16
+ * Composite middleware for worker edit validation
17
+ * Maps processorPath to processor for validation and validates all editable fields
18
+ */
19
+ export const withEditWorkerValidation = (handler: RouteHandler): RouteHandler => {
20
+ return async (req: IRequest, res: IResponse): Promise<void> => {
21
+ try {
22
+ const data = req.data();
23
+ const currentBody = req.getBody() as Record<string, unknown>;
24
+
25
+ // Map processorPath to processor for validation if processorPath exists
26
+ let mappedBody = { ...currentBody };
27
+ if (data['processorPath'] && !data['processor']) {
28
+ mappedBody = {
29
+ ...mappedBody,
30
+ processor: data['processorPath'], // Map for validation
31
+ };
32
+ }
33
+
34
+ // Update the request body with mapped fields
35
+ req.setBody(mappedBody);
36
+
37
+ // Apply validation with mapped fields, skipping options validation for editing
38
+ return withStrictPayloadKeys(
39
+ [
40
+ 'name',
41
+ 'queueName',
42
+ 'processor', // Validated field (mapped from processorPath)
43
+ 'processorPath', // Original field
44
+ 'version',
45
+ 'options', // Skip strict validation for editing
46
+ 'infrastructure',
47
+ 'features',
48
+ 'datacenter',
49
+ 'concurrency', // Original field
50
+ 'region',
51
+ 'autoStart',
52
+ 'status',
53
+ ],
54
+ withProcessorPathValidation(
55
+ withWorkerNameValidation(
56
+ withQueueNameValidation(
57
+ withVersionValidation(
58
+ withInfrastructureValidation(
59
+ withFeaturesValidation(withDatacenterValidation(handler))
60
+ )
61
+ )
62
+ )
63
+ )
64
+ )
65
+ )(req, res);
66
+ } catch (error) {
67
+ Logger.error('Edit worker validation failed', error);
68
+ return res.setStatus(500).json({
69
+ error: 'Internal validation error',
70
+ code: 'VALIDATION_ERROR',
71
+ });
72
+ }
73
+ };
74
+ };
@@ -0,0 +1,82 @@
1
+ import { Logger, type IRequest, type IResponse } from '@zintrust/core';
2
+
3
+ export type RouteHandler = (req: IRequest, res: IResponse) => Promise<void> | void;
4
+
5
+ interface FeaturesConfig {
6
+ clustering: boolean;
7
+ metrics: boolean;
8
+ autoScaling: boolean;
9
+ circuitBreaker: boolean;
10
+ deadLetterQueue: boolean;
11
+ resourceMonitoring: boolean;
12
+ compliance: boolean;
13
+ observability: boolean;
14
+ plugins: boolean;
15
+ versioning: boolean;
16
+ datacenterOrchestration: boolean;
17
+ }
18
+
19
+ const VALID_FEATURES = new Set([
20
+ 'clustering',
21
+ 'metrics',
22
+ 'autoScaling',
23
+ 'circuitBreaker',
24
+ 'deadLetterQueue',
25
+ 'resourceMonitoring',
26
+ 'compliance',
27
+ 'observability',
28
+ 'plugins',
29
+ 'versioning',
30
+ 'datacenterOrchestration',
31
+ ]);
32
+
33
+ export const withFeaturesValidation = (handler: RouteHandler): RouteHandler => {
34
+ return async (req: IRequest, res: IResponse): Promise<void> => {
35
+ try {
36
+ const data = req.data();
37
+ const features = data['features'] as FeaturesConfig;
38
+
39
+ if (!features) {
40
+ return handler(req, res); // Skip validation if features is not provided
41
+ }
42
+
43
+ // Check if features is an object
44
+ if (typeof features !== 'object' || features === null || Array.isArray(features)) {
45
+ return res.setStatus(400).json({
46
+ error: 'Invalid features configuration',
47
+ message: 'Features must be an object',
48
+ code: 'INVALID_FEATURES_TYPE',
49
+ });
50
+ }
51
+
52
+ // Validate each feature key and value
53
+ const featureKeys = Object.keys(features);
54
+ for (const key of featureKeys) {
55
+ if (!VALID_FEATURES.has(key)) {
56
+ return res.setStatus(400).json({
57
+ error: 'Invalid feature',
58
+ message: `Unknown feature: ${key}. Valid features are: ${Array.from(VALID_FEATURES).join(', ')}`,
59
+ code: 'INVALID_FEATURE',
60
+ });
61
+ }
62
+
63
+ const value = features[key as keyof FeaturesConfig];
64
+ if (typeof value !== 'boolean') {
65
+ return res.setStatus(400).json({
66
+ error: 'Invalid feature value',
67
+ message: `Feature ${key} must be a boolean (true or false)`,
68
+ code: 'INVALID_FEATURE_VALUE',
69
+ });
70
+ }
71
+ }
72
+
73
+ return handler(req, res);
74
+ } catch (error) {
75
+ Logger.error('Features validation failed', error);
76
+ return res.setStatus(500).json({
77
+ error: 'Internal validation error',
78
+ code: 'VALIDATION_ERROR',
79
+ });
80
+ }
81
+ };
82
+ };