xypriss 1.2.4 → 1.3.0

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 (103) hide show
  1. package/README.md +51 -3
  2. package/dist/cjs/mods/security/src/components/cache/index.js +1 -1
  3. package/dist/cjs/shared/logger/Logger.js +1 -0
  4. package/dist/cjs/shared/logger/Logger.js.map +1 -1
  5. package/dist/cjs/src/cluster/bun-cluster-manager.js +1567 -0
  6. package/dist/cjs/src/cluster/bun-cluster-manager.js.map +1 -0
  7. package/dist/cjs/src/cluster/cluster-manager.js +1 -1
  8. package/dist/cjs/src/cluster/cluster-manager.js.map +1 -1
  9. package/dist/cjs/src/cluster/index.js +25 -6
  10. package/dist/cjs/src/cluster/index.js.map +1 -1
  11. package/dist/cjs/src/cluster/memory-manager.js +463 -0
  12. package/dist/cjs/src/cluster/memory-manager.js.map +1 -0
  13. package/dist/cjs/src/cluster/modules/BunIPCManager.js +603 -0
  14. package/dist/cjs/src/cluster/modules/BunIPCManager.js.map +1 -0
  15. package/dist/cjs/src/cluster/modules/ClusterFactory.js +22 -1
  16. package/dist/cjs/src/cluster/modules/ClusterFactory.js.map +1 -1
  17. package/dist/cjs/src/cluster/modules/CpuMonitor.js +658 -0
  18. package/dist/cjs/src/cluster/modules/CpuMonitor.js.map +1 -0
  19. package/dist/cjs/src/cluster/modules/ProcessMonitor.js +513 -0
  20. package/dist/cjs/src/cluster/modules/ProcessMonitor.js.map +1 -0
  21. package/dist/cjs/src/plugins/server-maintenance-plugin.js +1 -1
  22. package/dist/cjs/src/server/FastServer.js +64 -43
  23. package/dist/cjs/src/server/FastServer.js.map +1 -1
  24. package/dist/cjs/src/server/components/fastapi/ClusterManagerComponent.js +226 -10
  25. package/dist/cjs/src/server/components/fastapi/ClusterManagerComponent.js.map +1 -1
  26. package/dist/cjs/src/server/const/Cluster.config.js +174 -31
  27. package/dist/cjs/src/server/const/Cluster.config.js.map +1 -1
  28. package/dist/cjs/src/server/const/default.js +11 -2
  29. package/dist/cjs/src/server/const/default.js.map +1 -1
  30. package/dist/cjs/src/server/utils/PortManager.js +26 -15
  31. package/dist/cjs/src/server/utils/PortManager.js.map +1 -1
  32. package/dist/esm/mods/security/src/components/cache/index.js +1 -1
  33. package/dist/esm/shared/logger/Logger.js +1 -0
  34. package/dist/esm/shared/logger/Logger.js.map +1 -1
  35. package/dist/esm/src/cluster/bun-cluster-manager.js +1565 -0
  36. package/dist/esm/src/cluster/bun-cluster-manager.js.map +1 -0
  37. package/dist/esm/src/cluster/cluster-manager.js +1 -1
  38. package/dist/esm/src/cluster/cluster-manager.js.map +1 -1
  39. package/dist/esm/src/cluster/index.js +25 -6
  40. package/dist/esm/src/cluster/index.js.map +1 -1
  41. package/dist/esm/src/cluster/memory-manager.js +461 -0
  42. package/dist/esm/src/cluster/memory-manager.js.map +1 -0
  43. package/dist/esm/src/cluster/modules/BunIPCManager.js +601 -0
  44. package/dist/esm/src/cluster/modules/BunIPCManager.js.map +1 -0
  45. package/dist/esm/src/cluster/modules/ClusterFactory.js +22 -1
  46. package/dist/esm/src/cluster/modules/ClusterFactory.js.map +1 -1
  47. package/dist/esm/src/cluster/modules/CpuMonitor.js +656 -0
  48. package/dist/esm/src/cluster/modules/CpuMonitor.js.map +1 -0
  49. package/dist/esm/src/cluster/modules/ProcessMonitor.js +511 -0
  50. package/dist/esm/src/cluster/modules/ProcessMonitor.js.map +1 -0
  51. package/dist/esm/src/plugins/server-maintenance-plugin.js +1 -1
  52. package/dist/esm/src/server/FastServer.js +64 -43
  53. package/dist/esm/src/server/FastServer.js.map +1 -1
  54. package/dist/esm/src/server/components/fastapi/ClusterManagerComponent.js +226 -10
  55. package/dist/esm/src/server/components/fastapi/ClusterManagerComponent.js.map +1 -1
  56. package/dist/esm/src/server/const/Cluster.config.js +174 -31
  57. package/dist/esm/src/server/const/Cluster.config.js.map +1 -1
  58. package/dist/esm/src/server/const/default.js +11 -2
  59. package/dist/esm/src/server/const/default.js.map +1 -1
  60. package/dist/esm/src/server/utils/PortManager.js +26 -15
  61. package/dist/esm/src/server/utils/PortManager.js.map +1 -1
  62. package/dist/index.d.ts +78 -1
  63. package/package.json +3 -1
  64. package/dist/cjs/src/plugins/modules/network/index.js +0 -120
  65. package/dist/cjs/src/plugins/modules/network/index.js.map +0 -1
  66. package/dist/cjs/src/server/plugins/PluginEngine.js +0 -378
  67. package/dist/cjs/src/server/plugins/PluginEngine.js.map +0 -1
  68. package/dist/cjs/src/server/plugins/PluginRegistry.js +0 -339
  69. package/dist/cjs/src/server/plugins/PluginRegistry.js.map +0 -1
  70. package/dist/cjs/src/server/plugins/builtin/JWTAuthPlugin.js +0 -591
  71. package/dist/cjs/src/server/plugins/builtin/JWTAuthPlugin.js.map +0 -1
  72. package/dist/cjs/src/server/plugins/builtin/ResponseTimePlugin.js +0 -413
  73. package/dist/cjs/src/server/plugins/builtin/ResponseTimePlugin.js.map +0 -1
  74. package/dist/cjs/src/server/plugins/builtin/SmartCachePlugin.js +0 -843
  75. package/dist/cjs/src/server/plugins/builtin/SmartCachePlugin.js.map +0 -1
  76. package/dist/cjs/src/server/plugins/core/CachePlugin.js +0 -1975
  77. package/dist/cjs/src/server/plugins/core/CachePlugin.js.map +0 -1
  78. package/dist/cjs/src/server/plugins/core/PerformancePlugin.js +0 -894
  79. package/dist/cjs/src/server/plugins/core/PerformancePlugin.js.map +0 -1
  80. package/dist/cjs/src/server/plugins/core/SecurityPlugin.js +0 -799
  81. package/dist/cjs/src/server/plugins/core/SecurityPlugin.js.map +0 -1
  82. package/dist/cjs/src/server/plugins/types/PluginTypes.js +0 -47
  83. package/dist/cjs/src/server/plugins/types/PluginTypes.js.map +0 -1
  84. package/dist/esm/src/plugins/modules/network/index.js +0 -109
  85. package/dist/esm/src/plugins/modules/network/index.js.map +0 -1
  86. package/dist/esm/src/server/plugins/PluginEngine.js +0 -376
  87. package/dist/esm/src/server/plugins/PluginEngine.js.map +0 -1
  88. package/dist/esm/src/server/plugins/PluginRegistry.js +0 -337
  89. package/dist/esm/src/server/plugins/PluginRegistry.js.map +0 -1
  90. package/dist/esm/src/server/plugins/builtin/JWTAuthPlugin.js +0 -589
  91. package/dist/esm/src/server/plugins/builtin/JWTAuthPlugin.js.map +0 -1
  92. package/dist/esm/src/server/plugins/builtin/ResponseTimePlugin.js +0 -411
  93. package/dist/esm/src/server/plugins/builtin/ResponseTimePlugin.js.map +0 -1
  94. package/dist/esm/src/server/plugins/builtin/SmartCachePlugin.js +0 -841
  95. package/dist/esm/src/server/plugins/builtin/SmartCachePlugin.js.map +0 -1
  96. package/dist/esm/src/server/plugins/core/CachePlugin.js +0 -1973
  97. package/dist/esm/src/server/plugins/core/CachePlugin.js.map +0 -1
  98. package/dist/esm/src/server/plugins/core/PerformancePlugin.js +0 -872
  99. package/dist/esm/src/server/plugins/core/PerformancePlugin.js.map +0 -1
  100. package/dist/esm/src/server/plugins/core/SecurityPlugin.js +0 -797
  101. package/dist/esm/src/server/plugins/core/SecurityPlugin.js.map +0 -1
  102. package/dist/esm/src/server/plugins/types/PluginTypes.js +0 -47
  103. package/dist/esm/src/server/plugins/types/PluginTypes.js.map +0 -1
@@ -1,894 +0,0 @@
1
- 'use strict';
2
-
3
- var index = require('../../../../mods/security/src/components/fortified-function/index.js');
4
- var PluginTypes = require('../types/PluginTypes.js');
5
- var si = require('systeminformation');
6
- var pidusage = require('pidusage');
7
- var osUtils = require('node-os-utils');
8
- var performanceMonitor = require('../../../../mods/security/src/utils/performanceMonitor.js');
9
-
10
- function _interopNamespaceDefault(e) {
11
- var n = Object.create(null);
12
- if (e) {
13
- Object.keys(e).forEach(function (k) {
14
- if (k !== 'default') {
15
- var d = Object.getOwnPropertyDescriptor(e, k);
16
- Object.defineProperty(n, k, d.get ? d : {
17
- enumerable: true,
18
- get: function () { return e[k]; }
19
- });
20
- }
21
- });
22
- }
23
- n.default = e;
24
- return Object.freeze(n);
25
- }
26
-
27
- var si__namespace = /*#__PURE__*/_interopNamespaceDefault(si);
28
- var osUtils__namespace = /*#__PURE__*/_interopNamespaceDefault(osUtils);
29
-
30
- /**
31
- * Performance Plugin Base Class
32
- *
33
- * Foundation for performance monitoring plugins with <0.3ms execution overhead
34
- * and comprehensive metrics collection using XyPrissJS performance utilities.
35
- */
36
- /**
37
- * Abstract base class for performance monitoring plugins
38
- */
39
- class PerformancePlugin {
40
- constructor() {
41
- this.type = PluginTypes.PluginType.PERFORMANCE;
42
- this.priority = PluginTypes.PluginPriority.NORMAL;
43
- this.isAsync = false; // Performance plugins should be synchronous for minimal overhead
44
- this.isCacheable = true;
45
- this.maxExecutionTime = 300; // 0.3ms max for performance operations
46
- // Performance thresholds
47
- this.performanceThresholds = {
48
- responseTime: 100, // 100ms
49
- memoryUsage: 50 * 1024 * 1024, // 50MB
50
- cpuUsage: 80, // 80%
51
- };
52
- // Performance tracking
53
- this.timers = new Map();
54
- this.metrics = new Map();
55
- this.lastCpuCheck = 0;
56
- this.cachedCpuUsage = 0;
57
- this.performanceData = {
58
- totalExecutions: 0,
59
- averageExecutionTime: 0,
60
- minExecutionTime: Infinity,
61
- maxExecutionTime: 0,
62
- errorCount: 0,
63
- successRate: 100,
64
- memoryUsage: 0,
65
- cpuUsage: 0,
66
- lastExecuted: new Date(),
67
- };
68
- }
69
- /**
70
- * Initialize performance plugin with XyPrissJS utilities
71
- */
72
- async initialize(context) {
73
- // Create fortified metrics collector for ultra-fast performance tracking
74
- this.fortifiedMetrics = index.func((metricName, value) => {
75
- this.recordMetricInternal(metricName, value);
76
- }, {
77
- ultraFast: "maximum",
78
- auditLog: false, // Disable audit logging for performance metrics
79
- timeout: 100, // Very short timeout for metrics
80
- errorHandling: "graceful",
81
- });
82
- // Initialize system monitoring
83
- await this.initializeSystemMonitoring();
84
- // Initialize performance monitoring
85
- await this.initializePerformanceMonitoring(context);
86
- }
87
- /**
88
- * Execute performance plugin with minimal overhead
89
- */
90
- execute(context) {
91
- const startTime = performance.now();
92
- try {
93
- // Start request timing if not already started
94
- if (!context.metrics.requestStartTime) {
95
- context.metrics.requestStartTime = startTime;
96
- }
97
- // Collect performance metrics
98
- const metrics = this.collectPerformanceMetrics(context);
99
- // Execute plugin-specific performance logic
100
- const result = this.executePerformanceLogic(context, metrics);
101
- const executionTime = performance.now() - startTime;
102
- // Update performance statistics
103
- this.updatePerformanceStats(executionTime, true);
104
- // Store metrics in context
105
- context.metrics.pluginExecutionTimes.set(this.id, executionTime);
106
- return {
107
- success: true,
108
- executionTime,
109
- data: {
110
- metrics,
111
- result,
112
- },
113
- shouldContinue: true,
114
- };
115
- }
116
- catch (error) {
117
- const executionTime = performance.now() - startTime;
118
- // Update error statistics
119
- this.updatePerformanceStats(executionTime, false);
120
- return {
121
- success: false,
122
- executionTime,
123
- error,
124
- shouldContinue: true, // Performance errors shouldn't stop execution
125
- };
126
- }
127
- }
128
- /**
129
- * Start a named timer for performance measurement
130
- */
131
- startTimer(name) {
132
- this.timers.set(name, performance.now());
133
- }
134
- /**
135
- * End a named timer and return elapsed time
136
- */
137
- endTimer(name) {
138
- const startTime = this.timers.get(name);
139
- if (!startTime) {
140
- console.warn(`Timer '${name}' was not started`);
141
- return 0;
142
- }
143
- const elapsed = performance.now() - startTime;
144
- this.timers.delete(name);
145
- // Record the timing metric
146
- this.recordMetric(`timer.${name}`, elapsed);
147
- return elapsed;
148
- }
149
- /**
150
- * Record a performance metric
151
- */
152
- recordMetric(name, value) {
153
- if (this.fortifiedMetrics) {
154
- this.fortifiedMetrics(name, value);
155
- }
156
- else {
157
- this.recordMetricInternal(name, value);
158
- }
159
- }
160
- /**
161
- * Get current performance metrics
162
- */
163
- getMetrics() {
164
- return { ...this.performanceData };
165
- }
166
- /**
167
- * Precompile performance monitoring for optimal execution
168
- */
169
- async precompile() {
170
- // Pre-warm performance monitoring systems
171
- await this.precompilePerformanceMonitoring();
172
- }
173
- /**
174
- * Warm up performance plugin
175
- */
176
- async warmup(context) {
177
- // Perform initial metrics collection to warm up systems
178
- this.collectPerformanceMetrics(context);
179
- }
180
- // ===== PERFORMANCE IMPLEMENTATIONS =====
181
- /**
182
- * Initialize plugin-specific performance monitoring
183
- * implementation with comprehensive monitoring setup
184
- */
185
- async initializePerformanceMonitoring(context) {
186
- try {
187
- // Initialize performance thresholds from configuration
188
- if (context.config.customSettings.performanceThresholds) {
189
- this.updatePerformanceThresholds(context.config.customSettings.performanceThresholds);
190
- }
191
- // Setup metric collection intervals
192
- if (context.config.customSettings.metricsCollection) {
193
- this.setupMetricsCollection(context.config.customSettings.metricsCollection);
194
- }
195
- // Initialize performance alerting
196
- if (context.config.customSettings.performanceAlerting) {
197
- this.setupPerformanceAlerting(context.config.customSettings.performanceAlerting);
198
- }
199
- // Setup performance profiling
200
- if (context.config.enableProfiling) {
201
- await this.setupPerformanceProfiling(context);
202
- }
203
- context.logger.info(`Performance plugin ${this.constructor.name} initialized successfully`);
204
- }
205
- catch (error) {
206
- context.logger.error(`Error initializing performance plugin: ${error.message}`, error);
207
- throw error;
208
- }
209
- }
210
- /**
211
- * Execute plugin-specific performance logic
212
- * implementation with comprehensive performance analysis
213
- */
214
- executePerformanceLogic(context, metrics) {
215
- try {
216
- const performanceResults = {
217
- metrics,
218
- thresholdViolations: [],
219
- performanceScore: 0,
220
- recommendations: [],
221
- alerts: [],
222
- optimizations: [],
223
- };
224
- // Check performance thresholds
225
- performanceResults.thresholdViolations =
226
- this.checkPerformanceThresholds(metrics);
227
- // Calculate performance score
228
- performanceResults.performanceScore =
229
- this.calculatePerformanceScore(metrics, performanceResults.thresholdViolations);
230
- // Generate performance recommendations
231
- performanceResults.recommendations =
232
- this.generatePerformanceRecommendations(metrics);
233
- // Check for performance alerts
234
- performanceResults.alerts = this.checkPerformanceAlerts(metrics);
235
- // Suggest optimizations
236
- performanceResults.optimizations =
237
- this.suggestOptimizations(metrics);
238
- // Record performance metrics
239
- this.recordPerformanceMetrics(metrics);
240
- return performanceResults;
241
- }
242
- catch (error) {
243
- console.error(`Error executing performance logic: ${error.message}`);
244
- return {
245
- error: error.message,
246
- performanceScore: 0,
247
- alerts: ["Performance monitoring failed"],
248
- };
249
- }
250
- }
251
- /**
252
- * Precompile performance monitoring systems
253
- * implementation with comprehensive pre-warming using real system metrics
254
- */
255
- async precompilePerformanceMonitoring() {
256
- try {
257
- // Pre-warm CPU usage calculation
258
- const cpuUsage = this.getCpuUsage();
259
- // Pre-warm memory usage calculation
260
- const memoryUsage = this.getMemoryUsage();
261
- // Pre-warm advanced CPU metrics
262
- const advancedCpuMetrics = await this.getAdvancedCpuMetrics();
263
- // Pre-warm process CPU usage
264
- const processCpuUsage = await this.getProcessCpuUsage();
265
- // Pre-warm metric recording with real system data
266
- this.recordMetric("precompile.cpu", cpuUsage);
267
- this.recordMetric("precompile.memory", memoryUsage);
268
- this.recordMetric("precompile.advanced_cpu", advancedCpuMetrics.usage);
269
- // Pre-warm timer functionality
270
- this.startTimer("precompile.timer");
271
- await new Promise((resolve) => setTimeout(resolve, 1)); // Minimal delay for realistic timing
272
- this.endTimer("precompile.timer");
273
- // Pre-warm performance threshold checking with real system metrics
274
- const realSystemMetrics = {
275
- requestDuration: 1, // Minimal request duration for precompile
276
- memoryUsage: memoryUsage,
277
- cpuUsage: cpuUsage,
278
- advancedCpu: advancedCpuMetrics,
279
- processCpu: processCpuUsage,
280
- timestamp: Date.now(),
281
- };
282
- this.checkPerformanceThresholds(realSystemMetrics);
283
- // Pre-warm global performance monitor integration
284
- const operationId = `precompile_${Date.now()}`;
285
- performanceMonitor.globalPerformanceMonitor.startOperation(operationId, "precompile_warmup", {
286
- algorithm: "performance_monitoring",
287
- dataSize: memoryUsage,
288
- });
289
- performanceMonitor.globalPerformanceMonitor.endOperation(operationId, true);
290
- // Pre-warm system resource monitoring
291
- const systemResources = performanceMonitor.globalPerformanceMonitor.getSystemResourceUsage();
292
- this.recordMetric("precompile.system_memory", systemResources.memory.heapUsed);
293
- this.recordMetric("precompile.system_uptime", systemResources.uptime);
294
- console.debug("Performance monitoring systems precompiled successfully with real metrics", {
295
- cpuUsage,
296
- memoryUsage,
297
- advancedCpuCores: advancedCpuMetrics.cores,
298
- processPid: processCpuUsage.pid,
299
- systemUptime: systemResources.uptime,
300
- });
301
- }
302
- catch (error) {
303
- console.error("Error precompiling performance monitoring:", error);
304
- }
305
- }
306
- // ===== PROTECTED HELPER METHODS =====
307
- /**
308
- * Collect comprehensive performance metrics
309
- */
310
- collectPerformanceMetrics(context) {
311
- const now = performance.now();
312
- return {
313
- // Timing metrics
314
- requestDuration: now - context.metrics.requestStartTime,
315
- pluginExecutionTime: now - context.startTime,
316
- // Memory metrics
317
- memoryUsage: this.getMemoryUsage(),
318
- // CPU metrics (if available)
319
- cpuUsage: this.getCpuUsage(),
320
- // Request metrics
321
- requestSize: this.getRequestSize(context.req),
322
- responseSize: this.getResponseSize(context.res),
323
- // Cache metrics
324
- cacheHits: context.metrics.cacheHits,
325
- cacheMisses: context.metrics.cacheMisses,
326
- // Custom metrics
327
- customMetrics: this.collectCustomMetrics(context),
328
- };
329
- }
330
- /**
331
- * Get current memory usage
332
- */
333
- getMemoryUsage() {
334
- try {
335
- const memUsage = process.memoryUsage();
336
- return memUsage.heapUsed;
337
- }
338
- catch (error) {
339
- return 0;
340
- }
341
- }
342
- /**
343
- * CPU usage monitoring with multi-core support
344
- */
345
- getCpuUsage() {
346
- try {
347
- const now = Date.now();
348
- // Use cached value if checked recently (within 1 second)
349
- if (now - this.lastCpuCheck < 1000) {
350
- return this.cachedCpuUsage;
351
- }
352
- // Get current CPU usage
353
- const currentUsage = process.cpuUsage();
354
- if (this.lastCpuUsage) {
355
- // Calculate CPU usage percentage
356
- const userDiff = currentUsage.user - this.lastCpuUsage.user;
357
- const systemDiff = currentUsage.system - this.lastCpuUsage.system;
358
- const totalDiff = userDiff + systemDiff;
359
- // Convert to percentage (microseconds to percentage)
360
- const timeDiff = now - this.lastCpuCheck;
361
- const cpuPercent = (totalDiff / (timeDiff * 1000)) * 100;
362
- this.cachedCpuUsage = Math.min(Math.max(cpuPercent, 0), 100);
363
- }
364
- this.lastCpuUsage = currentUsage;
365
- this.lastCpuCheck = now;
366
- return this.cachedCpuUsage;
367
- }
368
- catch (error) {
369
- console.error("Error getting CPU usage:", error);
370
- return 0;
371
- }
372
- }
373
- /**
374
- * Get advanced CPU metrics using systeminformation
375
- */
376
- async getAdvancedCpuMetrics() {
377
- try {
378
- const [cpuLoad, cpuInfo] = await Promise.all([
379
- si__namespace.currentLoad(),
380
- si__namespace.cpu(),
381
- ]);
382
- return {
383
- usage: cpuLoad.currentLoad,
384
- cores: cpuInfo.cores,
385
- speed: cpuInfo.speed,
386
- temperature: undefined, // Temperature not available in current load data,
387
- };
388
- }
389
- catch (error) {
390
- console.error("Error getting advanced CPU metrics:", error);
391
- return {
392
- usage: this.getCpuUsage(),
393
- cores: require("os").cpus().length,
394
- speed: 0,
395
- };
396
- }
397
- }
398
- /**
399
- * Get process-specific CPU usage using pidusage
400
- */
401
- async getProcessCpuUsage(pid) {
402
- try {
403
- const targetPid = pid || process.pid;
404
- const stats = await pidusage(targetPid);
405
- return stats;
406
- }
407
- catch (error) {
408
- console.error("Error getting process CPU usage:", error);
409
- return {
410
- cpu: 0,
411
- memory: 0,
412
- ppid: 0,
413
- pid: process.pid,
414
- ctime: 0,
415
- elapsed: 0,
416
- timestamp: Date.now(),
417
- };
418
- }
419
- }
420
- /**
421
- * Get request size in bytes
422
- */
423
- getRequestSize(req) {
424
- try {
425
- let size = 0;
426
- // Add headers size
427
- if (req.headers) {
428
- size += JSON.stringify(req.headers).length;
429
- }
430
- // Add body size
431
- if (req.body) {
432
- size += JSON.stringify(req.body).length;
433
- }
434
- // Add query size
435
- if (req.query) {
436
- size += JSON.stringify(req.query).length;
437
- }
438
- return size;
439
- }
440
- catch (error) {
441
- return 0;
442
- }
443
- }
444
- /**
445
- * Get response size in bytes with actual tracking
446
- */
447
- getResponseSize(res) {
448
- try {
449
- // Check for Content-Length header first
450
- const contentLength = res.getHeader("content-length");
451
- if (contentLength) {
452
- return parseInt(contentLength, 10) || 0;
453
- }
454
- // Check for Transfer-Encoding: chunked
455
- const transferEncoding = res.getHeader("transfer-encoding");
456
- if (transferEncoding === "chunked") {
457
- // For chunked responses, we need to track the actual size
458
- return this.getChunkedResponseSize(res);
459
- }
460
- // Fallback: estimate from headers and any available data
461
- let size = 0;
462
- // Add headers size
463
- const headers = res.getHeaders();
464
- if (headers) {
465
- for (const [key, value] of Object.entries(headers)) {
466
- size += key.length + String(value).length + 4; // +4 for ": " and "\r\n"
467
- }
468
- }
469
- // Add status line size (approximate)
470
- size += 15; // "HTTP/1.1 200 OK\r\n"
471
- return size;
472
- }
473
- catch (error) {
474
- console.error("Error calculating response size:", error);
475
- return 0;
476
- }
477
- }
478
- /**
479
- * Track chunked response size using response interceptor
480
- */
481
- getChunkedResponseSize(res) {
482
- try {
483
- // Check if we've already set up size tracking for this response
484
- if (res._XyPrissResponseSize !== undefined) {
485
- return res._XyPrissResponseSize;
486
- }
487
- // Initialize size tracking
488
- res._XyPrissResponseSize = 0;
489
- // Intercept the write method to track chunk sizes
490
- const originalWrite = res.write;
491
- res.write = function (chunk, encoding, callback) {
492
- if (chunk) {
493
- const chunkSize = Buffer.isBuffer(chunk)
494
- ? chunk.length
495
- : Buffer.byteLength(chunk, encoding || "utf8");
496
- res._XyPrissResponseSize += chunkSize;
497
- }
498
- return originalWrite.call(this, chunk, encoding, callback);
499
- };
500
- // Intercept the end method to track final chunk
501
- const originalEnd = res.end;
502
- res.end = function (chunk, encoding, callback) {
503
- if (chunk) {
504
- const chunkSize = Buffer.isBuffer(chunk)
505
- ? chunk.length
506
- : Buffer.byteLength(chunk, encoding || "utf8");
507
- res._XyPrissResponseSize += chunkSize;
508
- }
509
- return originalEnd.call(this, chunk, encoding, callback);
510
- };
511
- return res._XyPrissResponseSize;
512
- }
513
- catch (error) {
514
- console.error("Error setting up chunked response size tracking:", error);
515
- return 0;
516
- }
517
- }
518
- /**
519
- * Setup response size tracking middleware
520
- */
521
- setupResponseSizeTracking(_req, res) {
522
- try {
523
- // Initialize response size tracking
524
- res._XyPrissResponseSize = 0;
525
- res._XyPrissStartTime = Date.now();
526
- // Track response headers size
527
- const originalSetHeader = res.setHeader;
528
- res.setHeader = function (name, value) {
529
- const headerSize = name.length + String(value).length + 4;
530
- res._XyPrissResponseSize += headerSize;
531
- return originalSetHeader.call(this, name, value);
532
- };
533
- // Track response body size
534
- const originalWrite = res.write;
535
- res.write = function (chunk, encoding, callback) {
536
- if (chunk) {
537
- const chunkSize = Buffer.isBuffer(chunk)
538
- ? chunk.length
539
- : Buffer.byteLength(chunk, encoding || "utf8");
540
- res._XyPrissResponseSize += chunkSize;
541
- }
542
- return originalWrite.call(this, chunk, encoding, callback);
543
- };
544
- const originalEnd = res.end;
545
- res.end = function (chunk, encoding, callback) {
546
- if (chunk) {
547
- const chunkSize = Buffer.isBuffer(chunk)
548
- ? chunk.length
549
- : Buffer.byteLength(chunk, encoding || "utf8");
550
- res._XyPrissResponseSize += chunkSize;
551
- }
552
- // Record final response metrics
553
- const responseTime = Date.now() - res._XyPrissStartTime;
554
- res.setHeader("X-Response-Size", res._XyPrissResponseSize);
555
- res.setHeader("X-Response-Time", responseTime);
556
- return originalEnd.call(this, chunk, encoding, callback);
557
- };
558
- }
559
- catch (error) {
560
- console.error("Error setting up response size tracking:", error);
561
- }
562
- }
563
- /**
564
- * Collect custom performance metrics (to be overridden by subclasses)
565
- */
566
- collectCustomMetrics(_context) {
567
- return {};
568
- }
569
- /**
570
- * Initialize system monitoring with libraries
571
- */
572
- async initializeSystemMonitoring() {
573
- try {
574
- // Initialize CPU monitoring with osUtils
575
- this.cpuMonitor = osUtils__namespace.cpu;
576
- // Initialize CPU usage tracking
577
- this.lastCpuUsage = process.cpuUsage();
578
- this.lastCpuCheck = Date.now();
579
- this.cachedCpuUsage = 0;
580
- // Pre-warm system information calls
581
- await this.preWarmSystemInfo();
582
- }
583
- catch (error) {
584
- console.error("Error initializing system monitoring:", error);
585
- // Fallback to basic monitoring
586
- this.lastCpuUsage = process.cpuUsage();
587
- this.lastCpuCheck = Date.now();
588
- this.cachedCpuUsage = 0;
589
- }
590
- }
591
- /**
592
- * Pre-warm system information calls for better performance
593
- */
594
- async preWarmSystemInfo() {
595
- try {
596
- // Pre-warm systeminformation calls
597
- await Promise.allSettled([si__namespace.cpu(), si__namespace.currentLoad(), si__namespace.mem()]);
598
- // Pre-warm pidusage
599
- await pidusage(process.pid);
600
- }
601
- catch (error) {
602
- // Ignore pre-warming errors
603
- console.debug("System info pre-warming completed with some errors:", error);
604
- }
605
- }
606
- /**
607
- * Record metric internally
608
- */
609
- recordMetricInternal(name, value) {
610
- const values = this.metrics.get(name) || [];
611
- values.push(value);
612
- // Keep only last 100 values for memory efficiency
613
- if (values.length > 100) {
614
- values.shift();
615
- }
616
- this.metrics.set(name, values);
617
- }
618
- /**
619
- * Update performance statistics
620
- */
621
- updatePerformanceStats(executionTime, success) {
622
- this.performanceData.totalExecutions++;
623
- this.performanceData.lastExecuted = new Date();
624
- if (success) {
625
- // Update timing statistics
626
- this.performanceData.minExecutionTime = Math.min(this.performanceData.minExecutionTime, executionTime);
627
- this.performanceData.maxExecutionTime = Math.max(this.performanceData.maxExecutionTime, executionTime);
628
- // Update average execution time
629
- const totalTime = this.performanceData.averageExecutionTime *
630
- (this.performanceData.totalExecutions - 1) +
631
- executionTime;
632
- this.performanceData.averageExecutionTime =
633
- totalTime / this.performanceData.totalExecutions;
634
- }
635
- else {
636
- this.performanceData.errorCount++;
637
- }
638
- // Update success rate
639
- this.performanceData.successRate =
640
- ((this.performanceData.totalExecutions -
641
- this.performanceData.errorCount) /
642
- this.performanceData.totalExecutions) *
643
- 100;
644
- // Update resource usage
645
- this.performanceData.memoryUsage = this.getMemoryUsage();
646
- this.performanceData.cpuUsage = this.getCpuUsage();
647
- }
648
- /**
649
- * Check if performance thresholds are exceeded
650
- */
651
- checkPerformanceThresholds(metrics) {
652
- const violations = [];
653
- if (metrics.requestDuration > this.performanceThresholds.responseTime) {
654
- violations.push(`Response time exceeded: ${metrics.requestDuration}ms > ${this.performanceThresholds.responseTime}ms`);
655
- }
656
- if (metrics.memoryUsage > this.performanceThresholds.memoryUsage) {
657
- violations.push(`Memory usage exceeded: ${metrics.memoryUsage} > ${this.performanceThresholds.memoryUsage}`);
658
- }
659
- if (metrics.cpuUsage > this.performanceThresholds.cpuUsage) {
660
- violations.push(`CPU usage exceeded: ${metrics.cpuUsage}% > ${this.performanceThresholds.cpuUsage}%`);
661
- }
662
- return violations;
663
- }
664
- /**
665
- * Get metric statistics
666
- */
667
- getMetricStats(metricName) {
668
- const values = this.metrics.get(metricName) || [];
669
- if (values.length === 0) {
670
- return { count: 0, average: 0, min: 0, max: 0 };
671
- }
672
- const sum = values.reduce((a, b) => a + b, 0);
673
- return {
674
- count: values.length,
675
- average: sum / values.length,
676
- min: Math.min(...values),
677
- max: Math.max(...values),
678
- };
679
- }
680
- // ===== PERFORMANCE HELPER METHODS =====
681
- /**
682
- * Update performance thresholds from configuration
683
- */
684
- updatePerformanceThresholds(thresholds) {
685
- try {
686
- if (thresholds.responseTime) {
687
- this.performanceThresholds.responseTime =
688
- thresholds.responseTime;
689
- }
690
- if (thresholds.memoryUsage) {
691
- this.performanceThresholds.memoryUsage =
692
- thresholds.memoryUsage;
693
- }
694
- if (thresholds.cpuUsage) {
695
- this.performanceThresholds.cpuUsage =
696
- thresholds.cpuUsage;
697
- }
698
- console.debug("Performance thresholds updated successfully");
699
- }
700
- catch (error) {
701
- console.error("Error updating performance thresholds:", error);
702
- }
703
- }
704
- /**
705
- * Setup metrics collection intervals
706
- */
707
- setupMetricsCollection(config) {
708
- try {
709
- const interval = config.interval || 60000; // 1 minute default
710
- setInterval(() => {
711
- this.collectSystemMetrics();
712
- }, interval);
713
- console.debug("Metrics collection setup completed");
714
- }
715
- catch (error) {
716
- console.error("Error setting up metrics collection:", error);
717
- }
718
- }
719
- /**
720
- * Setup performance alerting
721
- */
722
- setupPerformanceAlerting(config) {
723
- try {
724
- // Setup alerting thresholds and handlers
725
- console.debug("Performance alerting setup completed");
726
- }
727
- catch (error) {
728
- console.error("Error setting up performance alerting:", error);
729
- }
730
- }
731
- /**
732
- * Setup performance profiling
733
- */
734
- async setupPerformanceProfiling(context) {
735
- try {
736
- // Setup profiling tools and configurations
737
- console.debug("Performance profiling setup completed");
738
- }
739
- catch (error) {
740
- console.error("Error setting up performance profiling:", error);
741
- }
742
- }
743
- /**
744
- * Calculate performance score based on metrics and violations
745
- */
746
- calculatePerformanceScore(metrics, violations) {
747
- try {
748
- let score = 100;
749
- // Deduct points for threshold violations
750
- score -= violations.length * 15;
751
- // Deduct points based on metric values
752
- if (metrics.requestDuration >
753
- this.performanceThresholds.responseTime * 0.8) {
754
- score -= 10;
755
- }
756
- if (metrics.memoryUsage >
757
- this.performanceThresholds.memoryUsage * 0.8) {
758
- score -= 10;
759
- }
760
- if (metrics.cpuUsage > this.performanceThresholds.cpuUsage * 0.8) {
761
- score -= 10;
762
- }
763
- // Bonus points for good performance
764
- if (metrics.requestDuration <
765
- this.performanceThresholds.responseTime * 0.5) {
766
- score += 5;
767
- }
768
- return Math.max(0, Math.min(100, score));
769
- }
770
- catch (error) {
771
- console.error("Error calculating performance score:", error);
772
- return 0;
773
- }
774
- }
775
- /**
776
- * Generate performance recommendations
777
- */
778
- generatePerformanceRecommendations(metrics) {
779
- const recommendations = [];
780
- try {
781
- if (metrics.requestDuration >
782
- this.performanceThresholds.responseTime) {
783
- recommendations.push("Consider optimizing request processing time");
784
- recommendations.push("Review database queries and API calls");
785
- }
786
- if (metrics.memoryUsage > this.performanceThresholds.memoryUsage) {
787
- recommendations.push("Consider optimizing memory usage");
788
- recommendations.push("Review object creation and garbage collection");
789
- }
790
- if (metrics.cpuUsage > this.performanceThresholds.cpuUsage) {
791
- recommendations.push("Consider optimizing CPU-intensive operations");
792
- recommendations.push("Review algorithmic complexity");
793
- }
794
- if (metrics.cacheHits / (metrics.cacheHits + metrics.cacheMisses) <
795
- 0.8) {
796
- recommendations.push("Consider improving cache hit rate");
797
- }
798
- return recommendations;
799
- }
800
- catch (error) {
801
- console.error("Error generating performance recommendations:", error);
802
- return ["Error generating recommendations"];
803
- }
804
- }
805
- /**
806
- * Check for performance alerts
807
- */
808
- checkPerformanceAlerts(metrics) {
809
- const alerts = [];
810
- try {
811
- // Critical performance alerts
812
- if (metrics.requestDuration >
813
- this.performanceThresholds.responseTime * 2) {
814
- alerts.push("CRITICAL: Response time severely degraded");
815
- }
816
- if (metrics.memoryUsage >
817
- this.performanceThresholds.memoryUsage * 1.5) {
818
- alerts.push("CRITICAL: Memory usage critically high");
819
- }
820
- if (metrics.cpuUsage > this.performanceThresholds.cpuUsage * 1.2) {
821
- alerts.push("WARNING: CPU usage elevated");
822
- }
823
- return alerts;
824
- }
825
- catch (error) {
826
- console.error("Error checking performance alerts:", error);
827
- return ["Error checking alerts"];
828
- }
829
- }
830
- /**
831
- * Suggest optimizations based on metrics
832
- */
833
- suggestOptimizations(metrics) {
834
- const optimizations = [];
835
- try {
836
- if (metrics.requestDuration > 100) {
837
- optimizations.push("Enable response compression");
838
- optimizations.push("Implement request caching");
839
- }
840
- if (metrics.memoryUsage > 50 * 1024 * 1024) {
841
- optimizations.push("Implement object pooling");
842
- optimizations.push("Optimize data structures");
843
- }
844
- if (metrics.cpuUsage > 70) {
845
- optimizations.push("Implement CPU-intensive task queuing");
846
- optimizations.push("Consider worker threads for heavy operations");
847
- }
848
- return optimizations;
849
- }
850
- catch (error) {
851
- console.error("Error suggesting optimizations:", error);
852
- return ["Error suggesting optimizations"];
853
- }
854
- }
855
- /**
856
- * Record performance metrics for analysis
857
- */
858
- recordPerformanceMetrics(metrics) {
859
- try {
860
- // Record key metrics
861
- this.recordMetric("request.duration", metrics.requestDuration);
862
- this.recordMetric("memory.usage", metrics.memoryUsage);
863
- this.recordMetric("cpu.usage", metrics.cpuUsage);
864
- this.recordMetric("cache.hits", metrics.cacheHits);
865
- this.recordMetric("cache.misses", metrics.cacheMisses);
866
- // Update performance data
867
- this.performanceData.memoryUsage = metrics.memoryUsage;
868
- this.performanceData.cpuUsage = metrics.cpuUsage;
869
- }
870
- catch (error) {
871
- console.error("Error recording performance metrics:", error);
872
- }
873
- }
874
- /**
875
- * Collect system metrics periodically
876
- */
877
- collectSystemMetrics() {
878
- try {
879
- const metrics = {
880
- timestamp: Date.now(),
881
- memoryUsage: this.getMemoryUsage(),
882
- cpuUsage: this.getCpuUsage(),
883
- };
884
- this.recordMetric("system.memory", metrics.memoryUsage);
885
- this.recordMetric("system.cpu", metrics.cpuUsage);
886
- }
887
- catch (error) {
888
- console.error("Error collecting system metrics:", error);
889
- }
890
- }
891
- }
892
-
893
- exports.PerformancePlugin = PerformancePlugin;
894
- //# sourceMappingURL=PerformancePlugin.js.map