@push.rocks/smartproxy 15.0.2 → 16.0.3

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 (160) hide show
  1. package/dist_ts/00_commitinfo_data.js +1 -1
  2. package/dist_ts/certificate/index.d.ts +10 -4
  3. package/dist_ts/certificate/index.js +5 -7
  4. package/dist_ts/certificate/models/certificate-types.d.ts +35 -15
  5. package/dist_ts/certificate/providers/cert-provisioner.d.ts +41 -15
  6. package/dist_ts/certificate/providers/cert-provisioner.js +201 -41
  7. package/dist_ts/core/models/index.d.ts +2 -0
  8. package/dist_ts/core/models/index.js +3 -1
  9. package/dist_ts/core/models/route-context.d.ts +62 -0
  10. package/dist_ts/core/models/route-context.js +43 -0
  11. package/dist_ts/core/models/socket-augmentation.d.ts +12 -0
  12. package/dist_ts/core/models/socket-augmentation.js +18 -0
  13. package/dist_ts/core/utils/event-system.d.ts +200 -0
  14. package/dist_ts/core/utils/event-system.js +224 -0
  15. package/dist_ts/core/utils/index.d.ts +7 -0
  16. package/dist_ts/core/utils/index.js +8 -1
  17. package/dist_ts/core/utils/route-manager.d.ts +118 -0
  18. package/dist_ts/core/utils/route-manager.js +383 -0
  19. package/dist_ts/core/utils/route-utils.d.ts +94 -0
  20. package/dist_ts/core/utils/route-utils.js +264 -0
  21. package/dist_ts/core/utils/security-utils.d.ts +111 -0
  22. package/dist_ts/core/utils/security-utils.js +212 -0
  23. package/dist_ts/core/utils/shared-security-manager.d.ts +110 -0
  24. package/dist_ts/core/utils/shared-security-manager.js +252 -0
  25. package/dist_ts/core/utils/template-utils.d.ts +37 -0
  26. package/dist_ts/core/utils/template-utils.js +104 -0
  27. package/dist_ts/core/utils/websocket-utils.d.ts +23 -0
  28. package/dist_ts/core/utils/websocket-utils.js +86 -0
  29. package/dist_ts/forwarding/config/forwarding-types.d.ts +40 -76
  30. package/dist_ts/forwarding/config/forwarding-types.js +19 -18
  31. package/dist_ts/forwarding/config/index.d.ts +4 -2
  32. package/dist_ts/forwarding/config/index.js +5 -3
  33. package/dist_ts/forwarding/handlers/base-handler.js +3 -1
  34. package/dist_ts/forwarding/index.d.ts +5 -6
  35. package/dist_ts/forwarding/index.js +3 -3
  36. package/dist_ts/http/models/http-types.js +1 -1
  37. package/dist_ts/http/port80/acme-interfaces.d.ts +30 -0
  38. package/dist_ts/http/port80/acme-interfaces.js +46 -1
  39. package/dist_ts/http/port80/port80-handler.d.ts +17 -2
  40. package/dist_ts/http/port80/port80-handler.js +49 -11
  41. package/dist_ts/http/router/index.d.ts +5 -1
  42. package/dist_ts/http/router/index.js +4 -2
  43. package/dist_ts/http/router/route-router.d.ts +108 -0
  44. package/dist_ts/http/router/route-router.js +393 -0
  45. package/dist_ts/index.d.ts +8 -2
  46. package/dist_ts/index.js +10 -3
  47. package/dist_ts/proxies/index.d.ts +7 -2
  48. package/dist_ts/proxies/index.js +10 -4
  49. package/dist_ts/proxies/network-proxy/certificate-manager.d.ts +21 -0
  50. package/dist_ts/proxies/network-proxy/certificate-manager.js +92 -1
  51. package/dist_ts/proxies/network-proxy/context-creator.d.ts +34 -0
  52. package/dist_ts/proxies/network-proxy/context-creator.js +108 -0
  53. package/dist_ts/proxies/network-proxy/function-cache.d.ts +90 -0
  54. package/dist_ts/proxies/network-proxy/function-cache.js +198 -0
  55. package/dist_ts/proxies/network-proxy/http-request-handler.d.ts +40 -0
  56. package/dist_ts/proxies/network-proxy/http-request-handler.js +256 -0
  57. package/dist_ts/proxies/network-proxy/http2-request-handler.d.ts +24 -0
  58. package/dist_ts/proxies/network-proxy/http2-request-handler.js +201 -0
  59. package/dist_ts/proxies/network-proxy/models/types.d.ts +73 -1
  60. package/dist_ts/proxies/network-proxy/models/types.js +242 -1
  61. package/dist_ts/proxies/network-proxy/network-proxy.d.ts +23 -20
  62. package/dist_ts/proxies/network-proxy/network-proxy.js +147 -60
  63. package/dist_ts/proxies/network-proxy/request-handler.d.ts +38 -5
  64. package/dist_ts/proxies/network-proxy/request-handler.js +584 -198
  65. package/dist_ts/proxies/network-proxy/security-manager.d.ts +65 -0
  66. package/dist_ts/proxies/network-proxy/security-manager.js +255 -0
  67. package/dist_ts/proxies/network-proxy/websocket-handler.d.ts +13 -2
  68. package/dist_ts/proxies/network-proxy/websocket-handler.js +238 -20
  69. package/dist_ts/proxies/smart-proxy/index.d.ts +1 -1
  70. package/dist_ts/proxies/smart-proxy/index.js +3 -3
  71. package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +5 -66
  72. package/dist_ts/proxies/smart-proxy/models/interfaces.js +5 -4
  73. package/dist_ts/proxies/smart-proxy/models/route-types.d.ts +173 -6
  74. package/dist_ts/proxies/smart-proxy/network-proxy-bridge.d.ts +20 -7
  75. package/dist_ts/proxies/smart-proxy/network-proxy-bridge.js +49 -108
  76. package/dist_ts/proxies/smart-proxy/port-manager.d.ts +81 -0
  77. package/dist_ts/proxies/smart-proxy/port-manager.js +166 -0
  78. package/dist_ts/proxies/smart-proxy/route-connection-handler.d.ts +7 -5
  79. package/dist_ts/proxies/smart-proxy/route-connection-handler.js +155 -160
  80. package/dist_ts/proxies/smart-proxy/route-helpers/index.d.ts +9 -0
  81. package/dist_ts/proxies/smart-proxy/route-helpers/index.js +11 -0
  82. package/dist_ts/proxies/smart-proxy/route-helpers.d.ts +5 -125
  83. package/dist_ts/proxies/smart-proxy/route-helpers.js +8 -195
  84. package/dist_ts/proxies/smart-proxy/route-manager.d.ts +14 -11
  85. package/dist_ts/proxies/smart-proxy/route-manager.js +81 -124
  86. package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +101 -12
  87. package/dist_ts/proxies/smart-proxy/smart-proxy.js +178 -306
  88. package/dist_ts/proxies/smart-proxy/timeout-manager.js +3 -3
  89. package/dist_ts/proxies/smart-proxy/utils/index.d.ts +12 -0
  90. package/dist_ts/proxies/smart-proxy/utils/index.js +19 -0
  91. package/dist_ts/proxies/smart-proxy/utils/route-helpers.d.ts +240 -0
  92. package/dist_ts/proxies/smart-proxy/utils/route-helpers.js +451 -0
  93. package/dist_ts/proxies/smart-proxy/utils/route-migration-utils.d.ts +51 -0
  94. package/dist_ts/proxies/smart-proxy/utils/route-migration-utils.js +124 -0
  95. package/dist_ts/proxies/smart-proxy/utils/route-patterns.d.ts +131 -0
  96. package/dist_ts/proxies/smart-proxy/utils/route-patterns.js +217 -0
  97. package/dist_ts/proxies/smart-proxy/utils/route-utils.d.ts +79 -0
  98. package/dist_ts/proxies/smart-proxy/utils/route-utils.js +266 -0
  99. package/dist_ts/proxies/smart-proxy/utils/route-validators.d.ts +73 -0
  100. package/dist_ts/proxies/smart-proxy/utils/route-validators.js +264 -0
  101. package/package.json +1 -1
  102. package/readme.md +241 -125
  103. package/readme.plan.md +73 -286
  104. package/ts/00_commitinfo_data.ts +1 -1
  105. package/ts/certificate/index.ts +17 -9
  106. package/ts/certificate/models/certificate-types.ts +37 -16
  107. package/ts/certificate/providers/cert-provisioner.ts +247 -54
  108. package/ts/core/models/index.ts +2 -0
  109. package/ts/core/models/route-context.ts +113 -0
  110. package/ts/core/models/socket-augmentation.ts +33 -0
  111. package/ts/core/utils/event-system.ts +376 -0
  112. package/ts/core/utils/index.ts +7 -0
  113. package/ts/core/utils/route-manager.ts +489 -0
  114. package/ts/core/utils/route-utils.ts +312 -0
  115. package/ts/core/utils/security-utils.ts +309 -0
  116. package/ts/core/utils/shared-security-manager.ts +333 -0
  117. package/ts/core/utils/template-utils.ts +124 -0
  118. package/ts/core/utils/websocket-utils.ts +81 -0
  119. package/ts/forwarding/config/forwarding-types.ts +79 -107
  120. package/ts/forwarding/config/index.ts +4 -2
  121. package/ts/forwarding/handlers/base-handler.ts +4 -2
  122. package/ts/forwarding/index.ts +3 -2
  123. package/ts/http/models/http-types.ts +0 -1
  124. package/ts/http/port80/acme-interfaces.ts +84 -0
  125. package/ts/http/port80/port80-handler.ts +61 -15
  126. package/ts/http/router/index.ts +8 -1
  127. package/ts/http/router/route-router.ts +482 -0
  128. package/ts/index.ts +14 -2
  129. package/ts/proxies/index.ts +12 -3
  130. package/ts/proxies/network-proxy/certificate-manager.ts +114 -10
  131. package/ts/proxies/network-proxy/context-creator.ts +145 -0
  132. package/ts/proxies/network-proxy/function-cache.ts +259 -0
  133. package/ts/proxies/network-proxy/http-request-handler.ts +330 -0
  134. package/ts/proxies/network-proxy/http2-request-handler.ts +255 -0
  135. package/ts/proxies/network-proxy/models/types.ts +312 -1
  136. package/ts/proxies/network-proxy/network-proxy.ts +195 -86
  137. package/ts/proxies/network-proxy/request-handler.ts +698 -246
  138. package/ts/proxies/network-proxy/security-manager.ts +298 -0
  139. package/ts/proxies/network-proxy/websocket-handler.ts +276 -33
  140. package/ts/proxies/smart-proxy/index.ts +2 -12
  141. package/ts/proxies/smart-proxy/models/interfaces.ts +13 -67
  142. package/ts/proxies/smart-proxy/models/route-types.ts +223 -25
  143. package/ts/proxies/smart-proxy/network-proxy-bridge.ts +57 -123
  144. package/ts/proxies/smart-proxy/port-manager.ts +195 -0
  145. package/ts/proxies/smart-proxy/route-connection-handler.ts +191 -225
  146. package/ts/proxies/smart-proxy/route-manager.ts +101 -144
  147. package/ts/proxies/smart-proxy/smart-proxy.ts +206 -377
  148. package/ts/proxies/smart-proxy/timeout-manager.ts +2 -2
  149. package/ts/proxies/smart-proxy/utils/index.ts +40 -0
  150. package/ts/proxies/smart-proxy/utils/route-helpers.ts +621 -0
  151. package/ts/proxies/smart-proxy/utils/route-migration-utils.ts +165 -0
  152. package/ts/proxies/smart-proxy/utils/route-patterns.ts +309 -0
  153. package/ts/proxies/smart-proxy/utils/route-utils.ts +330 -0
  154. package/ts/proxies/smart-proxy/utils/route-validators.ts +288 -0
  155. package/ts/forwarding/config/domain-config.ts +0 -28
  156. package/ts/forwarding/config/domain-manager.ts +0 -283
  157. package/ts/proxies/smart-proxy/connection-handler.ts +0 -1240
  158. package/ts/proxies/smart-proxy/domain-config-manager.ts +0 -441
  159. package/ts/proxies/smart-proxy/port-range-manager.ts +0 -211
  160. package/ts/proxies/smart-proxy/route-helpers.ts +0 -344
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Route Migration Utilities
3
+ *
4
+ * This file provides utility functions for migrating from legacy domain-based
5
+ * configuration to the new route-based configuration system. These functions
6
+ * are temporary and will be removed after the migration is complete.
7
+ */
8
+
9
+ import type { TForwardingType } from '../../../forwarding/config/forwarding-types.js';
10
+ import type { IRouteConfig, IRouteMatch, IRouteAction, IRouteTarget } from '../models/route-types.js';
11
+
12
+ /**
13
+ * Legacy domain config interface (for migration only)
14
+ * @deprecated This interface will be removed in a future version
15
+ */
16
+ export interface ILegacyDomainConfig {
17
+ domains: string[];
18
+ forwarding: {
19
+ type: TForwardingType;
20
+ target: {
21
+ host: string | string[];
22
+ port: number;
23
+ };
24
+ [key: string]: any;
25
+ };
26
+ }
27
+
28
+ /**
29
+ * Convert a legacy domain config to a route-based config
30
+ * @param domainConfig Legacy domain configuration
31
+ * @param additionalOptions Additional options to add to the route
32
+ * @returns Route configuration
33
+ * @deprecated This function will be removed in a future version
34
+ */
35
+ export function domainConfigToRouteConfig(
36
+ domainConfig: ILegacyDomainConfig,
37
+ additionalOptions: Partial<IRouteConfig> = {}
38
+ ): IRouteConfig {
39
+ // Default port based on forwarding type
40
+ let defaultPort = 80;
41
+ let tlsMode: 'passthrough' | 'terminate' | 'terminate-and-reencrypt' | undefined;
42
+
43
+ switch (domainConfig.forwarding.type) {
44
+ case 'http-only':
45
+ defaultPort = 80;
46
+ break;
47
+ case 'https-passthrough':
48
+ defaultPort = 443;
49
+ tlsMode = 'passthrough';
50
+ break;
51
+ case 'https-terminate-to-http':
52
+ defaultPort = 443;
53
+ tlsMode = 'terminate';
54
+ break;
55
+ case 'https-terminate-to-https':
56
+ defaultPort = 443;
57
+ tlsMode = 'terminate-and-reencrypt';
58
+ break;
59
+ }
60
+
61
+ // Create route match criteria
62
+ const match: IRouteMatch = {
63
+ ports: additionalOptions.match?.ports || defaultPort,
64
+ domains: domainConfig.domains
65
+ };
66
+
67
+ // Create route target
68
+ const target: IRouteTarget = {
69
+ host: domainConfig.forwarding.target.host,
70
+ port: domainConfig.forwarding.target.port
71
+ };
72
+
73
+ // Create route action
74
+ const action: IRouteAction = {
75
+ type: 'forward',
76
+ target
77
+ };
78
+
79
+ // Add TLS configuration if needed
80
+ if (tlsMode) {
81
+ action.tls = {
82
+ mode: tlsMode,
83
+ certificate: 'auto'
84
+ };
85
+
86
+ // If the legacy config has custom certificates, use them
87
+ if (domainConfig.forwarding.https?.customCert) {
88
+ action.tls.certificate = {
89
+ key: domainConfig.forwarding.https.customCert.key,
90
+ cert: domainConfig.forwarding.https.customCert.cert
91
+ };
92
+ }
93
+ }
94
+
95
+ // Add security options if present
96
+ if (domainConfig.forwarding.security) {
97
+ action.security = domainConfig.forwarding.security;
98
+ }
99
+
100
+ // Create the route config
101
+ const routeConfig: IRouteConfig = {
102
+ match,
103
+ action,
104
+ // Include a name based on domains if not provided
105
+ name: additionalOptions.name || `Legacy route for ${domainConfig.domains.join(', ')}`,
106
+ // Include a note that this was converted from a legacy config
107
+ description: additionalOptions.description || 'Converted from legacy domain configuration'
108
+ };
109
+
110
+ // Add optional properties if provided
111
+ if (additionalOptions.priority !== undefined) {
112
+ routeConfig.priority = additionalOptions.priority;
113
+ }
114
+
115
+ if (additionalOptions.tags) {
116
+ routeConfig.tags = additionalOptions.tags;
117
+ }
118
+
119
+ return routeConfig;
120
+ }
121
+
122
+ /**
123
+ * Convert an array of legacy domain configs to route configurations
124
+ * @param domainConfigs Array of legacy domain configurations
125
+ * @returns Array of route configurations
126
+ * @deprecated This function will be removed in a future version
127
+ */
128
+ export function domainConfigsToRouteConfigs(
129
+ domainConfigs: ILegacyDomainConfig[]
130
+ ): IRouteConfig[] {
131
+ return domainConfigs.map(config => domainConfigToRouteConfig(config));
132
+ }
133
+
134
+ /**
135
+ * Extract domains from a route configuration
136
+ * @param route Route configuration
137
+ * @returns Array of domains
138
+ */
139
+ export function extractDomainsFromRoute(route: IRouteConfig): string[] {
140
+ if (!route.match.domains) {
141
+ return [];
142
+ }
143
+
144
+ return Array.isArray(route.match.domains)
145
+ ? route.match.domains
146
+ : [route.match.domains];
147
+ }
148
+
149
+ /**
150
+ * Extract domains from an array of route configurations
151
+ * @param routes Array of route configurations
152
+ * @returns Array of unique domains
153
+ */
154
+ export function extractDomainsFromRoutes(routes: IRouteConfig[]): string[] {
155
+ const domains = new Set<string>();
156
+
157
+ for (const route of routes) {
158
+ const routeDomains = extractDomainsFromRoute(route);
159
+ for (const domain of routeDomains) {
160
+ domains.add(domain);
161
+ }
162
+ }
163
+
164
+ return Array.from(domains);
165
+ }
@@ -0,0 +1,309 @@
1
+ /**
2
+ * Route Patterns
3
+ *
4
+ * This file provides pre-defined route patterns for common use cases.
5
+ * These patterns can be used as templates for creating route configurations.
6
+ */
7
+
8
+ import type { IRouteConfig } from '../models/route-types.js';
9
+ import { createHttpRoute, createHttpsTerminateRoute, createHttpsPassthroughRoute, createCompleteHttpsServer } from './route-helpers.js';
10
+ import { mergeRouteConfigs } from './route-utils.js';
11
+
12
+ /**
13
+ * Create an API Gateway route pattern
14
+ * @param domains Domain(s) to match
15
+ * @param apiBasePath Base path for API endpoints (e.g., '/api')
16
+ * @param target Target host and port
17
+ * @param options Additional route options
18
+ * @returns API route configuration
19
+ */
20
+ export function createApiGatewayRoute(
21
+ domains: string | string[],
22
+ apiBasePath: string,
23
+ target: { host: string | string[]; port: number },
24
+ options: {
25
+ useTls?: boolean;
26
+ certificate?: 'auto' | { key: string; cert: string };
27
+ addCorsHeaders?: boolean;
28
+ [key: string]: any;
29
+ } = {}
30
+ ): IRouteConfig {
31
+ // Normalize apiBasePath to ensure it starts with / and doesn't end with /
32
+ const normalizedPath = apiBasePath.startsWith('/')
33
+ ? apiBasePath
34
+ : `/${apiBasePath}`;
35
+
36
+ // Add wildcard to path to match all API endpoints
37
+ const apiPath = normalizedPath.endsWith('/')
38
+ ? `${normalizedPath}*`
39
+ : `${normalizedPath}/*`;
40
+
41
+ // Create base route
42
+ const baseRoute = options.useTls
43
+ ? createHttpsTerminateRoute(domains, target, {
44
+ certificate: options.certificate || 'auto'
45
+ })
46
+ : createHttpRoute(domains, target);
47
+
48
+ // Add API-specific configurations
49
+ const apiRoute: Partial<IRouteConfig> = {
50
+ match: {
51
+ ...baseRoute.match,
52
+ path: apiPath
53
+ },
54
+ name: options.name || `API Gateway: ${apiPath} -> ${Array.isArray(target.host) ? target.host.join(', ') : target.host}:${target.port}`,
55
+ priority: options.priority || 100 // Higher priority for specific path matching
56
+ };
57
+
58
+ // Add CORS headers if requested
59
+ if (options.addCorsHeaders) {
60
+ apiRoute.headers = {
61
+ response: {
62
+ 'Access-Control-Allow-Origin': '*',
63
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
64
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
65
+ 'Access-Control-Max-Age': '86400'
66
+ }
67
+ };
68
+ }
69
+
70
+ return mergeRouteConfigs(baseRoute, apiRoute);
71
+ }
72
+
73
+ /**
74
+ * Create a static file server route pattern
75
+ * @param domains Domain(s) to match
76
+ * @param rootDirectory Root directory for static files
77
+ * @param options Additional route options
78
+ * @returns Static file server route configuration
79
+ */
80
+ export function createStaticFileServerRoute(
81
+ domains: string | string[],
82
+ rootDirectory: string,
83
+ options: {
84
+ useTls?: boolean;
85
+ certificate?: 'auto' | { key: string; cert: string };
86
+ indexFiles?: string[];
87
+ cacheControl?: string;
88
+ path?: string;
89
+ [key: string]: any;
90
+ } = {}
91
+ ): IRouteConfig {
92
+ // Create base route with static action
93
+ const baseRoute: IRouteConfig = {
94
+ match: {
95
+ domains,
96
+ ports: options.useTls ? 443 : 80,
97
+ path: options.path || '/'
98
+ },
99
+ action: {
100
+ type: 'static',
101
+ static: {
102
+ root: rootDirectory,
103
+ index: options.indexFiles || ['index.html', 'index.htm'],
104
+ headers: {
105
+ 'Cache-Control': options.cacheControl || 'public, max-age=3600'
106
+ }
107
+ }
108
+ },
109
+ name: options.name || `Static Server: ${Array.isArray(domains) ? domains.join(', ') : domains}`,
110
+ priority: options.priority || 50
111
+ };
112
+
113
+ // Add TLS configuration if requested
114
+ if (options.useTls) {
115
+ baseRoute.action.tls = {
116
+ mode: 'terminate',
117
+ certificate: options.certificate || 'auto'
118
+ };
119
+ }
120
+
121
+ return baseRoute;
122
+ }
123
+
124
+ /**
125
+ * Create a WebSocket route pattern
126
+ * @param domains Domain(s) to match
127
+ * @param target WebSocket server host and port
128
+ * @param options Additional route options
129
+ * @returns WebSocket route configuration
130
+ */
131
+ export function createWebSocketRoute(
132
+ domains: string | string[],
133
+ target: { host: string | string[]; port: number },
134
+ options: {
135
+ useTls?: boolean;
136
+ certificate?: 'auto' | { key: string; cert: string };
137
+ path?: string;
138
+ [key: string]: any;
139
+ } = {}
140
+ ): IRouteConfig {
141
+ // Create base route
142
+ const baseRoute = options.useTls
143
+ ? createHttpsTerminateRoute(domains, target, {
144
+ certificate: options.certificate || 'auto'
145
+ })
146
+ : createHttpRoute(domains, target);
147
+
148
+ // Add WebSocket-specific configurations
149
+ const wsRoute: Partial<IRouteConfig> = {
150
+ match: {
151
+ ...baseRoute.match,
152
+ path: options.path || '/ws',
153
+ headers: {
154
+ 'Upgrade': 'websocket'
155
+ }
156
+ },
157
+ action: {
158
+ ...baseRoute.action,
159
+ websocket: {
160
+ enabled: true,
161
+ pingInterval: options.pingInterval || 30000, // 30 seconds
162
+ pingTimeout: options.pingTimeout || 5000 // 5 seconds
163
+ }
164
+ },
165
+ name: options.name || `WebSocket: ${Array.isArray(domains) ? domains.join(', ') : domains} -> ${Array.isArray(target.host) ? target.host.join(', ') : target.host}:${target.port}`,
166
+ priority: options.priority || 100 // Higher priority for WebSocket routes
167
+ };
168
+
169
+ return mergeRouteConfigs(baseRoute, wsRoute);
170
+ }
171
+
172
+ /**
173
+ * Create a load balancer route pattern
174
+ * @param domains Domain(s) to match
175
+ * @param backends Array of backend servers
176
+ * @param options Additional route options
177
+ * @returns Load balancer route configuration
178
+ */
179
+ export function createLoadBalancerRoute(
180
+ domains: string | string[],
181
+ backends: Array<{ host: string; port: number }>,
182
+ options: {
183
+ useTls?: boolean;
184
+ certificate?: 'auto' | { key: string; cert: string };
185
+ algorithm?: 'round-robin' | 'least-connections' | 'ip-hash';
186
+ healthCheck?: {
187
+ path: string;
188
+ interval: number;
189
+ timeout: number;
190
+ unhealthyThreshold: number;
191
+ healthyThreshold: number;
192
+ };
193
+ [key: string]: any;
194
+ } = {}
195
+ ): IRouteConfig {
196
+ // Extract hosts and ensure all backends use the same port
197
+ const port = backends[0].port;
198
+ const hosts = backends.map(backend => backend.host);
199
+
200
+ // Create route with multiple hosts for load balancing
201
+ const baseRoute = options.useTls
202
+ ? createHttpsTerminateRoute(domains, { host: hosts, port }, {
203
+ certificate: options.certificate || 'auto'
204
+ })
205
+ : createHttpRoute(domains, { host: hosts, port });
206
+
207
+ // Add load balancing specific configurations
208
+ const lbRoute: Partial<IRouteConfig> = {
209
+ action: {
210
+ ...baseRoute.action,
211
+ loadBalancing: {
212
+ algorithm: options.algorithm || 'round-robin',
213
+ healthCheck: options.healthCheck
214
+ }
215
+ },
216
+ name: options.name || `Load Balancer: ${Array.isArray(domains) ? domains.join(', ') : domains}`,
217
+ priority: options.priority || 50
218
+ };
219
+
220
+ return mergeRouteConfigs(baseRoute, lbRoute);
221
+ }
222
+
223
+ /**
224
+ * Create a rate limiting route pattern
225
+ * @param baseRoute Base route to add rate limiting to
226
+ * @param rateLimit Rate limiting configuration
227
+ * @returns Route with rate limiting
228
+ */
229
+ export function addRateLimiting(
230
+ baseRoute: IRouteConfig,
231
+ rateLimit: {
232
+ maxRequests: number;
233
+ window: number; // Time window in seconds
234
+ keyBy?: 'ip' | 'path' | 'header';
235
+ headerName?: string; // Required if keyBy is 'header'
236
+ errorMessage?: string;
237
+ }
238
+ ): IRouteConfig {
239
+ return mergeRouteConfigs(baseRoute, {
240
+ security: {
241
+ rateLimit: {
242
+ enabled: true,
243
+ maxRequests: rateLimit.maxRequests,
244
+ window: rateLimit.window,
245
+ keyBy: rateLimit.keyBy || 'ip',
246
+ headerName: rateLimit.headerName,
247
+ errorMessage: rateLimit.errorMessage || 'Rate limit exceeded. Please try again later.'
248
+ }
249
+ }
250
+ });
251
+ }
252
+
253
+ /**
254
+ * Create a basic authentication route pattern
255
+ * @param baseRoute Base route to add authentication to
256
+ * @param auth Authentication configuration
257
+ * @returns Route with basic authentication
258
+ */
259
+ export function addBasicAuth(
260
+ baseRoute: IRouteConfig,
261
+ auth: {
262
+ users: Array<{ username: string; password: string }>;
263
+ realm?: string;
264
+ excludePaths?: string[];
265
+ }
266
+ ): IRouteConfig {
267
+ return mergeRouteConfigs(baseRoute, {
268
+ security: {
269
+ basicAuth: {
270
+ enabled: true,
271
+ users: auth.users,
272
+ realm: auth.realm || 'Restricted Area',
273
+ excludePaths: auth.excludePaths || []
274
+ }
275
+ }
276
+ });
277
+ }
278
+
279
+ /**
280
+ * Create a JWT authentication route pattern
281
+ * @param baseRoute Base route to add JWT authentication to
282
+ * @param jwt JWT authentication configuration
283
+ * @returns Route with JWT authentication
284
+ */
285
+ export function addJwtAuth(
286
+ baseRoute: IRouteConfig,
287
+ jwt: {
288
+ secret: string;
289
+ algorithm?: string;
290
+ issuer?: string;
291
+ audience?: string;
292
+ expiresIn?: number; // Time in seconds
293
+ excludePaths?: string[];
294
+ }
295
+ ): IRouteConfig {
296
+ return mergeRouteConfigs(baseRoute, {
297
+ security: {
298
+ jwtAuth: {
299
+ enabled: true,
300
+ secret: jwt.secret,
301
+ algorithm: jwt.algorithm || 'HS256',
302
+ issuer: jwt.issuer,
303
+ audience: jwt.audience,
304
+ expiresIn: jwt.expiresIn,
305
+ excludePaths: jwt.excludePaths || []
306
+ }
307
+ }
308
+ });
309
+ }