xypriss 1.1.4 → 1.2.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.
- package/dist/cjs/src/index.js +56 -0
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/plugins/modules/PluginRegistry.js +6 -38
- package/dist/cjs/src/plugins/modules/PluginRegistry.js.map +1 -1
- package/dist/cjs/src/plugins/modules/builtin/JWTAuthPlugin.js +1 -1
- package/dist/cjs/src/plugins/modules/builtin/JWTAuthPlugin.js.map +1 -1
- package/dist/cjs/src/plugins/modules/builtin/SmartCachePlugin.js +1 -1
- package/dist/cjs/src/plugins/modules/builtin/SmartCachePlugin.js.map +1 -1
- package/dist/cjs/src/plugins/modules/core/CachePlugin.js +4 -4
- package/dist/cjs/src/plugins/modules/core/CachePlugin.js.map +1 -1
- package/dist/cjs/src/plugins/modules/core/PerformancePlugin.js +2 -2
- package/dist/cjs/src/plugins/modules/core/PerformancePlugin.js.map +1 -1
- package/dist/cjs/src/plugins/modules/core/SecurityPlugin.js +2 -2
- package/dist/cjs/src/plugins/modules/core/SecurityPlugin.js.map +1 -1
- package/dist/cjs/src/plugins/modules/index.js +304 -0
- package/dist/cjs/src/plugins/modules/index.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/builtin/CompressionPlugin.js +410 -0
- package/dist/cjs/src/plugins/modules/network/builtin/CompressionPlugin.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/builtin/ConnectionPlugin.js +797 -0
- package/dist/cjs/src/plugins/modules/network/builtin/ConnectionPlugin.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/builtin/ProxyPlugin.js +409 -0
- package/dist/cjs/src/plugins/modules/network/builtin/ProxyPlugin.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/builtin/RateLimitPlugin.js +606 -0
- package/dist/cjs/src/plugins/modules/network/builtin/RateLimitPlugin.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/core/NetworkPlugin.js +225 -0
- package/dist/cjs/src/plugins/modules/network/core/NetworkPlugin.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/core/NetworkPluginFactory.js +40 -0
- package/dist/cjs/src/plugins/modules/network/core/NetworkPluginFactory.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/index.js +120 -0
- package/dist/cjs/src/plugins/modules/network/index.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/types/NetworkTypes.js +24 -0
- package/dist/cjs/src/plugins/modules/network/types/NetworkTypes.js.map +1 -0
- package/dist/cjs/src/plugins/modules/network/utils/NetworkPluginUtils.js +63 -0
- package/dist/cjs/src/plugins/modules/network/utils/NetworkPluginUtils.js.map +1 -0
- package/dist/cjs/src/plugins/modules/types/PluginTypes.js +1 -0
- package/dist/cjs/src/plugins/modules/types/PluginTypes.js.map +1 -1
- package/dist/cjs/src/server/FastServer.js +111 -2
- package/dist/cjs/src/server/FastServer.js.map +1 -1
- package/dist/cjs/src/server/conf/networkConnectionConf.js +25 -0
- package/dist/cjs/src/server/conf/networkConnectionConf.js.map +1 -0
- package/dist/cjs/src/server/conf/proxyConfig.js +23 -0
- package/dist/cjs/src/server/conf/proxyConfig.js.map +1 -0
- package/dist/cjs/src/server/conf/rateLimitConfig.js +35 -0
- package/dist/cjs/src/server/conf/rateLimitConfig.js.map +1 -0
- package/dist/cjs/src/server/const/default.js +6 -0
- package/dist/cjs/src/server/const/default.js.map +1 -1
- package/dist/cjs/src/server/handlers/NotFoundHandler.js +217 -0
- package/dist/cjs/src/server/handlers/NotFoundHandler.js.map +1 -0
- package/dist/cjs/src/server/handlers/templates/notFoundTemp.js +163 -0
- package/dist/cjs/src/server/handlers/templates/notFoundTemp.js.map +1 -0
- package/dist/esm/mods/security/src/components/cache/UFSIMC.js +5 -5
- package/dist/esm/mods/security/src/components/cache/UFSIMC.js.map +1 -1
- package/dist/esm/mods/security/src/components/cache/cacheSys.utils.js +3 -3
- package/dist/esm/mods/security/src/components/cache/cacheSys.utils.js.map +1 -1
- package/dist/esm/src/index.js +18 -0
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/plugins/modules/PluginRegistry.js +6 -38
- package/dist/esm/src/plugins/modules/PluginRegistry.js.map +1 -1
- package/dist/esm/src/plugins/modules/builtin/JWTAuthPlugin.js +1 -1
- package/dist/esm/src/plugins/modules/builtin/JWTAuthPlugin.js.map +1 -1
- package/dist/esm/src/plugins/modules/builtin/SmartCachePlugin.js +1 -1
- package/dist/esm/src/plugins/modules/builtin/SmartCachePlugin.js.map +1 -1
- package/dist/esm/src/plugins/modules/core/CachePlugin.js +4 -4
- package/dist/esm/src/plugins/modules/core/CachePlugin.js.map +1 -1
- package/dist/esm/src/plugins/modules/core/PerformancePlugin.js +2 -2
- package/dist/esm/src/plugins/modules/core/PerformancePlugin.js.map +1 -1
- package/dist/esm/src/plugins/modules/core/SecurityPlugin.js +2 -2
- package/dist/esm/src/plugins/modules/core/SecurityPlugin.js.map +1 -1
- package/dist/esm/src/plugins/modules/index.js +275 -0
- package/dist/esm/src/plugins/modules/index.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/builtin/CompressionPlugin.js +389 -0
- package/dist/esm/src/plugins/modules/network/builtin/CompressionPlugin.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/builtin/ConnectionPlugin.js +776 -0
- package/dist/esm/src/plugins/modules/network/builtin/ConnectionPlugin.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/builtin/ProxyPlugin.js +407 -0
- package/dist/esm/src/plugins/modules/network/builtin/ProxyPlugin.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/builtin/RateLimitPlugin.js +585 -0
- package/dist/esm/src/plugins/modules/network/builtin/RateLimitPlugin.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/core/NetworkPlugin.js +223 -0
- package/dist/esm/src/plugins/modules/network/core/NetworkPlugin.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/core/NetworkPluginFactory.js +38 -0
- package/dist/esm/src/plugins/modules/network/core/NetworkPluginFactory.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/index.js +109 -0
- package/dist/esm/src/plugins/modules/network/index.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/types/NetworkTypes.js +24 -0
- package/dist/esm/src/plugins/modules/network/types/NetworkTypes.js.map +1 -0
- package/dist/esm/src/plugins/modules/network/utils/NetworkPluginUtils.js +61 -0
- package/dist/esm/src/plugins/modules/network/utils/NetworkPluginUtils.js.map +1 -0
- package/dist/esm/src/plugins/modules/types/PluginTypes.js +1 -0
- package/dist/esm/src/plugins/modules/types/PluginTypes.js.map +1 -1
- package/dist/esm/src/server/FastServer.js +111 -2
- package/dist/esm/src/server/FastServer.js.map +1 -1
- package/dist/esm/src/server/conf/networkConnectionConf.js +23 -0
- package/dist/esm/src/server/conf/networkConnectionConf.js.map +1 -0
- package/dist/esm/src/server/conf/proxyConfig.js +21 -0
- package/dist/esm/src/server/conf/proxyConfig.js.map +1 -0
- package/dist/esm/src/server/conf/rateLimitConfig.js +33 -0
- package/dist/esm/src/server/conf/rateLimitConfig.js.map +1 -0
- package/dist/esm/src/server/const/default.js +6 -0
- package/dist/esm/src/server/const/default.js.map +1 -1
- package/dist/esm/src/server/handlers/NotFoundHandler.js +194 -0
- package/dist/esm/src/server/handlers/NotFoundHandler.js.map +1 -0
- package/dist/esm/src/server/handlers/templates/notFoundTemp.js +161 -0
- package/dist/esm/src/server/handlers/templates/notFoundTemp.js.map +1 -0
- package/dist/index.d.ts +5085 -14
- package/package.json +8 -7
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { JWTAuthPlugin } from './builtin/JWTAuthPlugin.js';
|
|
2
|
+
import { ResponseTimePlugin } from './builtin/ResponseTimePlugin.js';
|
|
3
|
+
import { SmartCachePlugin } from './builtin/SmartCachePlugin.js';
|
|
4
|
+
import { PluginEngine } from './PluginEngine.js';
|
|
5
|
+
import { PluginRegistry } from './PluginRegistry.js';
|
|
6
|
+
import { PluginPriority } from './types/PluginTypes.js';
|
|
7
|
+
export { PluginEventType, PluginType } from './types/PluginTypes.js';
|
|
8
|
+
export { SecurityPlugin as SecurityPluginBase } from './core/SecurityPlugin.js';
|
|
9
|
+
import '../../../mods/security/src/components/fortified-function/core/fortified-function-core.js';
|
|
10
|
+
import '../../../mods/security/src/components/fortified-function/core/fortified-config.js';
|
|
11
|
+
import '../../../mods/security/src/components/fortified-function/core/fortified-logger.js';
|
|
12
|
+
import 'events';
|
|
13
|
+
import '../../../mods/security/src/components/fortified-function/serializer/safe-serializer.js';
|
|
14
|
+
import 'nehoid';
|
|
15
|
+
import '../../../mods/security/src/utils/memory/index.js';
|
|
16
|
+
import 'crypto';
|
|
17
|
+
import '../../../mods/security/src/types/secure-memory.js';
|
|
18
|
+
import '../../../mods/security/src/components/secure-string/advanced/entropy-analyzer.js';
|
|
19
|
+
import '../../../mods/security/src/components/secure-string/advanced/quantum-safe.js';
|
|
20
|
+
import '../../../mods/security/src/components/secure-string/advanced/performance-monitor.js';
|
|
21
|
+
import '../../../mods/security/src/core/hash/hash-core.js';
|
|
22
|
+
import '../../../mods/security/src/core/hash/hash-types.js';
|
|
23
|
+
import '../../../mods/security/src/core/hash/hash-security.js';
|
|
24
|
+
import '../../../mods/security/src/core/hash/hash-advanced.js';
|
|
25
|
+
import '../../../mods/security/src/algorithms/hash-algorithms.js';
|
|
26
|
+
import '../../../mods/security/src/core/random/random-types.js';
|
|
27
|
+
import '../../../mods/security/src/core/random/random-sources.js';
|
|
28
|
+
import 'nehonix-uri-processor';
|
|
29
|
+
import '../../../mods/security/src/types.js';
|
|
30
|
+
import '../../../mods/security/src/components/secure-array/utils/id-generator.js';
|
|
31
|
+
import '../../../mods/security/src/index.js';
|
|
32
|
+
import '../../../shared/logger/Logger.js';
|
|
33
|
+
import '../../../mods/security/src/components/fortified-function/UFA/ultra-fast-allocator.js';
|
|
34
|
+
import 'argon2';
|
|
35
|
+
import 'child_process';
|
|
36
|
+
import 'https';
|
|
37
|
+
import '../../../mods/security/src/components/runtime-verification.js';
|
|
38
|
+
import '../../../mods/security/src/components/tamper-evident-logging.js';
|
|
39
|
+
import '../../../mods/security/src/core/keys/keys-types.js';
|
|
40
|
+
import '../../../mods/security/src/core/keys/keys-logger.js';
|
|
41
|
+
import '../../../mods/security/src/core/keys/keys-utils.js';
|
|
42
|
+
import '../../../mods/security/src/core/keys/algorithms/mods/PBKDF2Algo.js';
|
|
43
|
+
import '../../../mods/security/src/core/password/index.js';
|
|
44
|
+
import 'systeminformation';
|
|
45
|
+
import 'pidusage';
|
|
46
|
+
import 'node-os-utils';
|
|
47
|
+
import '../../../mods/security/src/components/cache/index.js';
|
|
48
|
+
import 'perf_hooks';
|
|
49
|
+
export { NetworkCategory } from './network/types/NetworkTypes.js';
|
|
50
|
+
import 'fs';
|
|
51
|
+
import 'path';
|
|
52
|
+
import 'xypriss-security';
|
|
53
|
+
import 'zlib';
|
|
54
|
+
import 'compression';
|
|
55
|
+
import 'http-proxy-middleware';
|
|
56
|
+
import 'express-rate-limit';
|
|
57
|
+
import 'ioredis';
|
|
58
|
+
import 'util';
|
|
59
|
+
import '../../encryption/EncryptionService.js';
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* XyPrissJS Ultra-Fast Plugin System
|
|
63
|
+
*
|
|
64
|
+
* Comprehensive plugin system exports for ultra-fast performance
|
|
65
|
+
* with <1ms execution overhead and enterprise-grade security.
|
|
66
|
+
*/
|
|
67
|
+
/**
|
|
68
|
+
* Plugin system version and metadata
|
|
69
|
+
*/
|
|
70
|
+
const PLUGIN_SYSTEM_VERSION = "1.0.0";
|
|
71
|
+
const PLUGIN_SYSTEM_NAME = "XyPrissJS Ultra-Fast Plugin System";
|
|
72
|
+
/**
|
|
73
|
+
* Performance targets achieved by the plugin system
|
|
74
|
+
*/
|
|
75
|
+
const PERFORMANCE_TARGETS = {
|
|
76
|
+
PLUGIN_EXECUTION_OVERHEAD: "<1ms",
|
|
77
|
+
SECURITY_PLUGIN_EXECUTION: "<2ms",
|
|
78
|
+
CACHE_PLUGIN_EXECUTION: "<0.5ms",
|
|
79
|
+
PERFORMANCE_PLUGIN_EXECUTION: "<0.3ms",
|
|
80
|
+
PLUGIN_REGISTRATION: "<0.1ms",
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Default plugin configurations
|
|
84
|
+
*/
|
|
85
|
+
const DEFAULT_PLUGIN_CONFIG = {
|
|
86
|
+
maxExecutionTime: 1000, // 1 second default timeout
|
|
87
|
+
enableProfiling: true,
|
|
88
|
+
enableCaching: true,
|
|
89
|
+
enableEncryption: false,
|
|
90
|
+
enableAuditLogging: false,
|
|
91
|
+
securityLevel: "basic",
|
|
92
|
+
customSettings: {},
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Plugin system utilities
|
|
96
|
+
*/
|
|
97
|
+
class PluginSystemUtils {
|
|
98
|
+
/**
|
|
99
|
+
* Validate plugin configuration
|
|
100
|
+
*/
|
|
101
|
+
static validatePluginConfig(config) {
|
|
102
|
+
if (!config.id || typeof config.id !== "string") {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (!config.name || typeof config.name !== "string") {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
if (!config.version || typeof config.version !== "string") {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (typeof config.execute !== "function") {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Create plugin performance summary
|
|
118
|
+
*/
|
|
119
|
+
static createPerformanceSummary(stats) {
|
|
120
|
+
if (!stats || stats.length === 0) {
|
|
121
|
+
return {
|
|
122
|
+
totalPlugins: 0,
|
|
123
|
+
averageExecutionTime: 0,
|
|
124
|
+
totalExecutions: 0,
|
|
125
|
+
successRate: 100,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const totalExecutions = stats.reduce((sum, stat) => sum + stat.executionCount, 0);
|
|
129
|
+
const totalExecutionTime = stats.reduce((sum, stat) => sum + stat.totalExecutionTime, 0);
|
|
130
|
+
const totalErrors = stats.reduce((sum, stat) => sum + stat.errorCount, 0);
|
|
131
|
+
return {
|
|
132
|
+
totalPlugins: stats.length,
|
|
133
|
+
averageExecutionTime: totalExecutions > 0 ? totalExecutionTime / totalExecutions : 0,
|
|
134
|
+
totalExecutions,
|
|
135
|
+
successRate: totalExecutions > 0
|
|
136
|
+
? ((totalExecutions - totalErrors) / totalExecutions) * 100
|
|
137
|
+
: 100,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Generate plugin health report
|
|
142
|
+
*/
|
|
143
|
+
static generateHealthReport(registryStats, engineStats) {
|
|
144
|
+
const isHealthy = registryStats.totalPlugins > 0 &&
|
|
145
|
+
registryStats.averageExecutionTime < 10 && // 10ms threshold
|
|
146
|
+
engineStats.circuitBreakersOpen === 0;
|
|
147
|
+
return {
|
|
148
|
+
status: isHealthy ? "healthy" : "degraded",
|
|
149
|
+
timestamp: new Date().toISOString(),
|
|
150
|
+
metrics: {
|
|
151
|
+
totalPlugins: registryStats.totalPlugins,
|
|
152
|
+
averageExecutionTime: registryStats.averageExecutionTime,
|
|
153
|
+
circuitBreakersOpen: engineStats.circuitBreakersOpen,
|
|
154
|
+
contextPoolSize: engineStats.contextPoolSize,
|
|
155
|
+
warmedUpPlugins: engineStats.warmedUpPlugins,
|
|
156
|
+
},
|
|
157
|
+
recommendations: this.generateRecommendations(registryStats, engineStats),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Generate performance recommendations
|
|
162
|
+
*/
|
|
163
|
+
static generateRecommendations(registryStats, engineStats) {
|
|
164
|
+
const recommendations = [];
|
|
165
|
+
if (registryStats.averageExecutionTime > 5) {
|
|
166
|
+
recommendations.push("Consider optimizing slow plugins or increasing timeout values");
|
|
167
|
+
}
|
|
168
|
+
if (engineStats.circuitBreakersOpen > 0) {
|
|
169
|
+
recommendations.push("Review and fix failing plugins to restore full functionality");
|
|
170
|
+
}
|
|
171
|
+
if (engineStats.contextPoolSize < 10) {
|
|
172
|
+
recommendations.push("Consider increasing context pool size for better performance");
|
|
173
|
+
}
|
|
174
|
+
if (registryStats.totalPlugins === 0) {
|
|
175
|
+
recommendations.push("Initialize built-in plugins to enable enhanced functionality");
|
|
176
|
+
}
|
|
177
|
+
if (recommendations.length === 0) {
|
|
178
|
+
recommendations.push("Plugin system is performing optimally");
|
|
179
|
+
}
|
|
180
|
+
return recommendations;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Plugin system factory for easy initialization
|
|
185
|
+
*/
|
|
186
|
+
class PluginSystemFactory {
|
|
187
|
+
/**
|
|
188
|
+
* Create a pre-configured plugin registry
|
|
189
|
+
*/
|
|
190
|
+
static createRegistry(cache, cluster, config) {
|
|
191
|
+
return new PluginRegistry(cache, cluster, config);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Create a pre-configured plugin engine
|
|
195
|
+
*/
|
|
196
|
+
static createEngine(registry, cache, cluster) {
|
|
197
|
+
return new PluginEngine(registry, cache, cluster);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Create all built-in plugins
|
|
201
|
+
*/
|
|
202
|
+
static createBuiltinPlugins() {
|
|
203
|
+
return [
|
|
204
|
+
new JWTAuthPlugin(),
|
|
205
|
+
new ResponseTimePlugin(),
|
|
206
|
+
new SmartCachePlugin(),
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Initialize complete plugin system
|
|
211
|
+
*/
|
|
212
|
+
static async initializeSystem(cache, cluster, config) {
|
|
213
|
+
const registry = this.createRegistry(cache, cluster, config);
|
|
214
|
+
const engine = this.createEngine(registry, cache, cluster);
|
|
215
|
+
const plugins = this.createBuiltinPlugins();
|
|
216
|
+
// Register all built-in plugins
|
|
217
|
+
for (const plugin of plugins) {
|
|
218
|
+
await registry.register(plugin);
|
|
219
|
+
}
|
|
220
|
+
return { registry, engine, plugins };
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Plugin development helpers
|
|
225
|
+
*/
|
|
226
|
+
class PluginDevelopmentHelpers {
|
|
227
|
+
/**
|
|
228
|
+
* Create a basic plugin template
|
|
229
|
+
*/
|
|
230
|
+
static createPluginTemplate(id, name, type, priority = PluginPriority.NORMAL) {
|
|
231
|
+
return {
|
|
232
|
+
id,
|
|
233
|
+
name,
|
|
234
|
+
version: "1.0.0",
|
|
235
|
+
type,
|
|
236
|
+
priority,
|
|
237
|
+
isAsync: true,
|
|
238
|
+
isCacheable: false,
|
|
239
|
+
maxExecutionTime: 1000,
|
|
240
|
+
execute: async (context) => {
|
|
241
|
+
// Plugin implementation goes here
|
|
242
|
+
return {
|
|
243
|
+
success: true,
|
|
244
|
+
executionTime: 0,
|
|
245
|
+
shouldContinue: true,
|
|
246
|
+
};
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Validate plugin implementation
|
|
252
|
+
*/
|
|
253
|
+
static validatePlugin(plugin) {
|
|
254
|
+
const errors = [];
|
|
255
|
+
if (!plugin.id)
|
|
256
|
+
errors.push("Plugin must have an id");
|
|
257
|
+
if (!plugin.name)
|
|
258
|
+
errors.push("Plugin must have a name");
|
|
259
|
+
if (!plugin.version)
|
|
260
|
+
errors.push("Plugin must have a version");
|
|
261
|
+
if (!plugin.type)
|
|
262
|
+
errors.push("Plugin must have a type");
|
|
263
|
+
if (!plugin.execute)
|
|
264
|
+
errors.push("Plugin must have an execute method");
|
|
265
|
+
if (typeof plugin.execute !== "function")
|
|
266
|
+
errors.push("Plugin execute must be a function");
|
|
267
|
+
return {
|
|
268
|
+
valid: errors.length === 0,
|
|
269
|
+
errors,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export { DEFAULT_PLUGIN_CONFIG, JWTAuthPlugin, PERFORMANCE_TARGETS, PLUGIN_SYSTEM_NAME, PLUGIN_SYSTEM_VERSION, PluginDevelopmentHelpers, PluginEngine, PluginPriority, PluginRegistry, PluginSystemFactory, PluginSystemUtils, ResponseTimePlugin, SmartCachePlugin };
|
|
275
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../../src/plugins/modules/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;AAKG;AAmEH;;AAEG;AACI,MAAM,qBAAqB,GAAG,QAAQ;AACtC,MAAM,kBAAkB,GAAG,qCAAqC;AAEvE;;AAEG;AACU,MAAA,mBAAmB,GAAG;AAC/B,IAAA,yBAAyB,EAAE,MAAM;AACjC,IAAA,yBAAyB,EAAE,MAAM;AACjC,IAAA,sBAAsB,EAAE,QAAQ;AAChC,IAAA,4BAA4B,EAAE,QAAQ;AACtC,IAAA,mBAAmB,EAAE,QAAQ;EACtB;AAEX;;AAEG;AACU,MAAA,qBAAqB,GAAG;IACjC,gBAAgB,EAAE,IAAI;AACtB,IAAA,eAAe,EAAE,IAAI;AACrB,IAAA,aAAa,EAAE,IAAI;AACnB,IAAA,gBAAgB,EAAE,KAAK;AACvB,IAAA,kBAAkB,EAAE,KAAK;AACzB,IAAA,aAAa,EAAE,OAAgB;AAC/B,IAAA,cAAc,EAAE,EAAE;EACpB;AAEF;;AAEG;MACU,iBAAiB,CAAA;AAC1B;;AAEG;IACH,OAAO,oBAAoB,CAAC,MAAW,EAAA;AACnC,QAAA,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,OAAO,MAAM,CAAC,EAAE,KAAK,QAAQ,EAAE;AAC7C,YAAA,OAAO,KAAK,CAAC;SAChB;AAED,QAAA,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,OAAO,MAAM,CAAC,IA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AM,KAAK,CAAC;YAC1B,MAAM;SACT,CAAC;KACL;AACJ;;;;"}
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { performance } from 'perf_hooks';
|
|
2
|
+
import * as zlib from 'zlib';
|
|
3
|
+
import compression from 'compression';
|
|
4
|
+
import { NetworkPlugin } from '../core/NetworkPlugin.js';
|
|
5
|
+
import { NetworkCategory } from '../types/NetworkTypes.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Compression Plugin
|
|
9
|
+
*
|
|
10
|
+
* Handles response compression with multiple algorithms (Gzip, Brotli, Deflate)
|
|
11
|
+
* Provides smart compression based on content type and size optimization
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Response compression plugin for optimizing bandwidth usage
|
|
15
|
+
*/
|
|
16
|
+
class CompressionPlugin extends NetworkPlugin {
|
|
17
|
+
constructor(config = {
|
|
18
|
+
enabled: true,
|
|
19
|
+
algorithms: ["gzip", "deflate"],
|
|
20
|
+
level: 6,
|
|
21
|
+
threshold: 1024,
|
|
22
|
+
contentTypes: ["text/*", "application/json"],
|
|
23
|
+
}) {
|
|
24
|
+
super(config);
|
|
25
|
+
this.id = "xypriss.network.compression";
|
|
26
|
+
this.name = "Response Compression Plugin";
|
|
27
|
+
this.version = "1.0.0";
|
|
28
|
+
this.networkCategory = NetworkCategory.COMPRESSION;
|
|
29
|
+
// Compression-specific state
|
|
30
|
+
this.compressionStats = {
|
|
31
|
+
totalRequests: 0,
|
|
32
|
+
compressedRequests: 0,
|
|
33
|
+
totalBytesOriginal: 0,
|
|
34
|
+
totalBytesCompressed: 0,
|
|
35
|
+
averageCompressionRatio: 0,
|
|
36
|
+
algorithmUsage: new Map(),
|
|
37
|
+
};
|
|
38
|
+
this.supportedAlgorithms = [];
|
|
39
|
+
this.initializeSupportedAlgorithms();
|
|
40
|
+
this.createCompressionMiddleware();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Initialize compression algorithms based on availability
|
|
44
|
+
*/
|
|
45
|
+
initializeSupportedAlgorithms() {
|
|
46
|
+
const config = this.getCompressionConfig();
|
|
47
|
+
const requestedAlgorithms = config.algorithms || ["gzip", "deflate"];
|
|
48
|
+
// Check which algorithms are available
|
|
49
|
+
for (const algorithm of requestedAlgorithms) {
|
|
50
|
+
if (this.isAlgorithmSupported(algorithm)) {
|
|
51
|
+
this.supportedAlgorithms.push(algorithm);
|
|
52
|
+
this.compressionStats.algorithmUsage.set(algorithm, 0);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Ensure at least gzip is available as fallback
|
|
56
|
+
if (this.supportedAlgorithms.length === 0) {
|
|
57
|
+
this.supportedAlgorithms.push("gzip");
|
|
58
|
+
this.compressionStats.algorithmUsage.set("gzip", 0);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create compression middleware using the compression library
|
|
63
|
+
*/
|
|
64
|
+
createCompressionMiddleware() {
|
|
65
|
+
const config = this.getCompressionConfig();
|
|
66
|
+
this.compressionMiddleware = compression({
|
|
67
|
+
level: config.level || 6,
|
|
68
|
+
threshold: config.threshold || 1024,
|
|
69
|
+
filter: (req, res) => {
|
|
70
|
+
// Use our custom filter logic
|
|
71
|
+
return this.shouldCompress(req, res);
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Check if compression algorithm is supported
|
|
77
|
+
*/
|
|
78
|
+
isAlgorithmSupported(algorithm) {
|
|
79
|
+
switch (algorithm) {
|
|
80
|
+
case "gzip":
|
|
81
|
+
case "deflate":
|
|
82
|
+
return true; // Always available in Node.js
|
|
83
|
+
case "brotli":
|
|
84
|
+
return typeof zlib.createBrotliCompress === "function";
|
|
85
|
+
default:
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Execute compression logic
|
|
91
|
+
*/
|
|
92
|
+
async executeNetwork(context) {
|
|
93
|
+
const startTime = performance.now();
|
|
94
|
+
const { req, res } = context;
|
|
95
|
+
try {
|
|
96
|
+
// Apply compression using the middleware
|
|
97
|
+
await new Promise((resolve, reject) => {
|
|
98
|
+
this.compressionMiddleware(req, res, (err) => {
|
|
99
|
+
if (err)
|
|
100
|
+
reject(err);
|
|
101
|
+
else
|
|
102
|
+
resolve();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
// Update statistics
|
|
106
|
+
this.compressionStats.totalRequests++;
|
|
107
|
+
const wasCompressed = !!res.getHeader("content-encoding");
|
|
108
|
+
if (wasCompressed) {
|
|
109
|
+
this.compressionStats.compressedRequests++;
|
|
110
|
+
const algorithm = res.getHeader("content-encoding");
|
|
111
|
+
const currentUsage = this.compressionStats.algorithmUsage.get(algorithm) || 0;
|
|
112
|
+
this.compressionStats.algorithmUsage.set(algorithm, currentUsage + 1);
|
|
113
|
+
}
|
|
114
|
+
const executionTime = performance.now() - startTime;
|
|
115
|
+
return {
|
|
116
|
+
success: true,
|
|
117
|
+
executionTime,
|
|
118
|
+
shouldContinue: true,
|
|
119
|
+
data: {
|
|
120
|
+
compressed: wasCompressed,
|
|
121
|
+
algorithm: res.getHeader("content-encoding") || "none",
|
|
122
|
+
originalSize: context.networkMetrics.bytesReceived,
|
|
123
|
+
compressedSize: context.networkMetrics.bytesSent,
|
|
124
|
+
},
|
|
125
|
+
modifications: wasCompressed
|
|
126
|
+
? {
|
|
127
|
+
headers: {
|
|
128
|
+
"Content-Encoding": res.getHeader("content-encoding"),
|
|
129
|
+
Vary: "Accept-Encoding",
|
|
130
|
+
},
|
|
131
|
+
compressed: true,
|
|
132
|
+
}
|
|
133
|
+
: undefined,
|
|
134
|
+
networkMetrics: {
|
|
135
|
+
processingTime: executionTime,
|
|
136
|
+
memoryUsage: process.memoryUsage().heapUsed,
|
|
137
|
+
cpuUsage: process.cpuUsage().user,
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
return {
|
|
143
|
+
success: false,
|
|
144
|
+
executionTime: performance.now() - startTime,
|
|
145
|
+
shouldContinue: true,
|
|
146
|
+
error,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Check if response should be compressed
|
|
152
|
+
*/
|
|
153
|
+
shouldCompress(req, res) {
|
|
154
|
+
const config = this.getCompressionConfig();
|
|
155
|
+
// Check if compression is enabled
|
|
156
|
+
if (!config.enabled) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
// Check if client accepts compression
|
|
160
|
+
const acceptEncoding = req.get("Accept-Encoding") || "";
|
|
161
|
+
if (!this.clientSupportsCompression(acceptEncoding)) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
// Check content type
|
|
165
|
+
const contentType = res.get("Content-Type") || "";
|
|
166
|
+
if (!this.shouldCompressContentType(contentType)) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
// Check if already compressed
|
|
170
|
+
if (res.get("Content-Encoding")) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
// Check content length threshold
|
|
174
|
+
const contentLength = parseInt(res.get("Content-Length") || "0");
|
|
175
|
+
if (contentLength > 0 && contentLength < (config.threshold || 1024)) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Check if client supports compression
|
|
182
|
+
*/
|
|
183
|
+
clientSupportsCompression(acceptEncoding) {
|
|
184
|
+
return this.supportedAlgorithms.some((algorithm) => acceptEncoding.toLowerCase().includes(algorithm));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Check if content type should be compressed
|
|
188
|
+
*/
|
|
189
|
+
shouldCompressContentType(contentType) {
|
|
190
|
+
const config = this.getCompressionConfig();
|
|
191
|
+
const contentTypes = config.contentTypes || [
|
|
192
|
+
"text/*",
|
|
193
|
+
"application/json",
|
|
194
|
+
"application/javascript",
|
|
195
|
+
"application/xml",
|
|
196
|
+
"application/rss+xml",
|
|
197
|
+
"application/atom+xml",
|
|
198
|
+
];
|
|
199
|
+
// Check exclude list first
|
|
200
|
+
if (config.excludeContentTypes) {
|
|
201
|
+
for (const excludeType of config.excludeContentTypes) {
|
|
202
|
+
if (this.matchesContentType(contentType, excludeType)) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Check include list
|
|
208
|
+
return contentTypes.some((type) => this.matchesContentType(contentType, type));
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Match content type with pattern (supports wildcards)
|
|
212
|
+
*/
|
|
213
|
+
matchesContentType(contentType, pattern) {
|
|
214
|
+
if (pattern.includes("*")) {
|
|
215
|
+
const regex = new RegExp(pattern.replace("*", ".*"), "i");
|
|
216
|
+
return regex.test(contentType);
|
|
217
|
+
}
|
|
218
|
+
return contentType.toLowerCase().includes(pattern.toLowerCase());
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Select best compression algorithm based on client support and preferences
|
|
222
|
+
*/
|
|
223
|
+
selectCompressionAlgorithm(req) {
|
|
224
|
+
const acceptEncoding = req.get("Accept-Encoding") || "";
|
|
225
|
+
const encodings = acceptEncoding.toLowerCase();
|
|
226
|
+
// Priority order: brotli > gzip > deflate
|
|
227
|
+
const priorityOrder = [
|
|
228
|
+
"brotli",
|
|
229
|
+
"gzip",
|
|
230
|
+
"deflate",
|
|
231
|
+
];
|
|
232
|
+
for (const algorithm of priorityOrder) {
|
|
233
|
+
if (this.supportedAlgorithms.includes(algorithm) &&
|
|
234
|
+
encodings.includes(algorithm)) {
|
|
235
|
+
return algorithm;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Apply compression to response
|
|
242
|
+
*/
|
|
243
|
+
async applyCompression(context, algorithm) {
|
|
244
|
+
const { res } = context;
|
|
245
|
+
const config = this.getCompressionConfig();
|
|
246
|
+
// Set compression headers
|
|
247
|
+
res.setHeader("Content-Encoding", algorithm);
|
|
248
|
+
res.setHeader("Vary", "Accept-Encoding");
|
|
249
|
+
// Remove content-length header as it will change
|
|
250
|
+
res.removeHeader("Content-Length");
|
|
251
|
+
// Create compression stream
|
|
252
|
+
const compressionStream = this.createCompressionStream(algorithm, config.level || 6);
|
|
253
|
+
// Intercept response write/end methods
|
|
254
|
+
this.interceptResponse(res, compressionStream, context);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Create compression stream for algorithm
|
|
258
|
+
*/
|
|
259
|
+
createCompressionStream(algorithm, level) {
|
|
260
|
+
switch (algorithm) {
|
|
261
|
+
case "gzip":
|
|
262
|
+
return zlib.createGzip({ level });
|
|
263
|
+
case "deflate":
|
|
264
|
+
return zlib.createDeflate({ level });
|
|
265
|
+
case "brotli":
|
|
266
|
+
return zlib.createBrotliCompress({
|
|
267
|
+
params: {
|
|
268
|
+
[zlib.constants.BROTLI_PARAM_QUALITY]: level,
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
default:
|
|
272
|
+
throw new Error(`Unsupported compression algorithm: ${algorithm}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Intercept response to apply compression
|
|
277
|
+
*/
|
|
278
|
+
interceptResponse(res, compressionStream, context) {
|
|
279
|
+
res.write.bind(res);
|
|
280
|
+
res.end.bind(res);
|
|
281
|
+
let originalSize = 0;
|
|
282
|
+
let compressedSize = 0;
|
|
283
|
+
// Track compressed data
|
|
284
|
+
compressionStream.on("data", (chunk) => {
|
|
285
|
+
compressedSize += chunk.length;
|
|
286
|
+
});
|
|
287
|
+
// Override write method
|
|
288
|
+
res.write = function (chunk, encoding, callback) {
|
|
289
|
+
if (chunk) {
|
|
290
|
+
originalSize += Buffer.isBuffer(chunk)
|
|
291
|
+
? chunk.length
|
|
292
|
+
: Buffer.byteLength(chunk, encoding);
|
|
293
|
+
return compressionStream.write(chunk, encoding, callback);
|
|
294
|
+
}
|
|
295
|
+
return true;
|
|
296
|
+
};
|
|
297
|
+
// Override end method
|
|
298
|
+
res.end = function (chunk, encoding, callback) {
|
|
299
|
+
if (chunk) {
|
|
300
|
+
originalSize += Buffer.isBuffer(chunk)
|
|
301
|
+
? chunk.length
|
|
302
|
+
: Buffer.byteLength(chunk, encoding);
|
|
303
|
+
}
|
|
304
|
+
compressionStream.end(chunk, encoding, () => {
|
|
305
|
+
// Update metrics
|
|
306
|
+
context.networkMetrics.bytesSent = compressedSize;
|
|
307
|
+
context.networkMetrics.compressionRatio =
|
|
308
|
+
originalSize > 0 ? compressedSize / originalSize : 1;
|
|
309
|
+
if (callback)
|
|
310
|
+
callback();
|
|
311
|
+
});
|
|
312
|
+
return this;
|
|
313
|
+
};
|
|
314
|
+
// Pipe compression stream to response
|
|
315
|
+
compressionStream.pipe(res, { end: false });
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Update compression statistics
|
|
319
|
+
*/
|
|
320
|
+
updateCompressionStats(algorithm) {
|
|
321
|
+
this.compressionStats.totalRequests++;
|
|
322
|
+
this.compressionStats.compressedRequests++;
|
|
323
|
+
const currentUsage = this.compressionStats.algorithmUsage.get(algorithm) || 0;
|
|
324
|
+
this.compressionStats.algorithmUsage.set(algorithm, currentUsage + 1);
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Get compression configuration
|
|
328
|
+
*/
|
|
329
|
+
getCompressionConfig() {
|
|
330
|
+
return this.config;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Validate compression configuration
|
|
334
|
+
*/
|
|
335
|
+
validateNetworkConfig(config) {
|
|
336
|
+
if (config.level && (config.level < 1 || config.level > 9)) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
if (config.threshold && config.threshold < 0) {
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
if (config.algorithms && config.algorithms.length === 0) {
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Check network health
|
|
349
|
+
*/
|
|
350
|
+
async checkNetworkHealth() {
|
|
351
|
+
const errorRate = this.performanceMetrics.errorCount /
|
|
352
|
+
Math.max(this.performanceMetrics.totalExecutions, 1);
|
|
353
|
+
this.compressionStats.compressedRequests /
|
|
354
|
+
Math.max(this.compressionStats.totalRequests, 1);
|
|
355
|
+
return {
|
|
356
|
+
healthy: errorRate < 0.1 &&
|
|
357
|
+
this.performanceMetrics.averageExecutionTime <
|
|
358
|
+
this.maxExecutionTime,
|
|
359
|
+
status: errorRate < 0.05
|
|
360
|
+
? "healthy"
|
|
361
|
+
: errorRate < 0.1
|
|
362
|
+
? "degraded"
|
|
363
|
+
: "unhealthy",
|
|
364
|
+
metrics: {
|
|
365
|
+
responseTime: this.performanceMetrics.averageExecutionTime,
|
|
366
|
+
errorRate,
|
|
367
|
+
throughput: this.performanceMetrics.totalExecutions,
|
|
368
|
+
connections: 0, // Not applicable for compression
|
|
369
|
+
},
|
|
370
|
+
lastCheck: new Date(),
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Get compression statistics
|
|
375
|
+
*/
|
|
376
|
+
getCompressionStats() {
|
|
377
|
+
return {
|
|
378
|
+
...this.compressionStats,
|
|
379
|
+
supportedAlgorithms: [...this.supportedAlgorithms],
|
|
380
|
+
averageCompressionRatio: this.compressionStats.totalBytesOriginal > 0
|
|
381
|
+
? this.compressionStats.totalBytesCompressed /
|
|
382
|
+
this.compressionStats.totalBytesOriginal
|
|
383
|
+
: 0,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export { CompressionPlugin };
|
|
389
|
+
//# sourceMappingURL=CompressionPlugin.js.map
|