@furystack/rest-service 10.0.27 → 10.1.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 (74) hide show
  1. package/README.md +249 -12
  2. package/esm/api-manager.d.ts +1 -3
  3. package/esm/api-manager.d.ts.map +1 -1
  4. package/esm/api-manager.js +4 -6
  5. package/esm/api-manager.js.map +1 -1
  6. package/esm/header-processor.d.ts +39 -0
  7. package/esm/header-processor.d.ts.map +1 -0
  8. package/esm/header-processor.js +113 -0
  9. package/esm/header-processor.js.map +1 -0
  10. package/esm/header-processor.spec.d.ts +2 -0
  11. package/esm/header-processor.spec.d.ts.map +1 -0
  12. package/esm/header-processor.spec.js +420 -0
  13. package/esm/header-processor.spec.js.map +1 -0
  14. package/esm/helpers.d.ts +69 -1
  15. package/esm/helpers.d.ts.map +1 -1
  16. package/esm/helpers.js +70 -1
  17. package/esm/helpers.js.map +1 -1
  18. package/esm/helpers.spec.js +21 -5
  19. package/esm/helpers.spec.js.map +1 -1
  20. package/esm/http-proxy-handler.d.ts +53 -0
  21. package/esm/http-proxy-handler.d.ts.map +1 -0
  22. package/esm/http-proxy-handler.js +179 -0
  23. package/esm/http-proxy-handler.js.map +1 -0
  24. package/esm/http-user-context.d.ts +4 -4
  25. package/esm/http-user-context.d.ts.map +1 -1
  26. package/esm/http-user-context.js +4 -4
  27. package/esm/http-user-context.js.map +1 -1
  28. package/esm/index.d.ts +1 -0
  29. package/esm/index.d.ts.map +1 -1
  30. package/esm/index.js +1 -0
  31. package/esm/index.js.map +1 -1
  32. package/esm/path-processor.d.ts +33 -0
  33. package/esm/path-processor.d.ts.map +1 -0
  34. package/esm/path-processor.js +58 -0
  35. package/esm/path-processor.js.map +1 -0
  36. package/esm/path-processor.spec.d.ts +2 -0
  37. package/esm/path-processor.spec.d.ts.map +1 -0
  38. package/esm/path-processor.spec.js +256 -0
  39. package/esm/path-processor.spec.js.map +1 -0
  40. package/esm/proxy-manager.d.ts +52 -0
  41. package/esm/proxy-manager.d.ts.map +1 -0
  42. package/esm/proxy-manager.js +84 -0
  43. package/esm/proxy-manager.js.map +1 -0
  44. package/esm/proxy-manager.spec.d.ts +2 -0
  45. package/esm/proxy-manager.spec.d.ts.map +1 -0
  46. package/esm/proxy-manager.spec.js +1781 -0
  47. package/esm/proxy-manager.spec.js.map +1 -0
  48. package/esm/server-manager.d.ts +7 -0
  49. package/esm/server-manager.d.ts.map +1 -1
  50. package/esm/server-manager.js +12 -0
  51. package/esm/server-manager.js.map +1 -1
  52. package/esm/static-server-manager.d.ts.map +1 -1
  53. package/esm/static-server-manager.js +5 -7
  54. package/esm/static-server-manager.js.map +1 -1
  55. package/esm/websocket-proxy-handler.d.ts +44 -0
  56. package/esm/websocket-proxy-handler.d.ts.map +1 -0
  57. package/esm/websocket-proxy-handler.js +157 -0
  58. package/esm/websocket-proxy-handler.js.map +1 -0
  59. package/package.json +12 -10
  60. package/src/api-manager.ts +5 -15
  61. package/src/header-processor.spec.ts +514 -0
  62. package/src/header-processor.ts +140 -0
  63. package/src/helpers.spec.ts +23 -5
  64. package/src/helpers.ts +72 -1
  65. package/src/http-proxy-handler.ts +215 -0
  66. package/src/http-user-context.ts +6 -6
  67. package/src/index.ts +1 -0
  68. package/src/path-processor.spec.ts +318 -0
  69. package/src/path-processor.ts +69 -0
  70. package/src/proxy-manager.spec.ts +2094 -0
  71. package/src/proxy-manager.ts +101 -0
  72. package/src/server-manager.ts +19 -0
  73. package/src/static-server-manager.ts +5 -7
  74. package/src/websocket-proxy-handler.ts +204 -0
@@ -0,0 +1,69 @@
1
+ import { PathHelper } from '@furystack/utils'
2
+
3
+ /**
4
+ * Handles URL validation, path extraction, and path rewriting for proxy requests
5
+ */
6
+ export class PathProcessor {
7
+ /**
8
+ * Validates that a URL string is properly formatted
9
+ * @throws Error if URL is invalid
10
+ */
11
+ public validateUrl(url: string, context = 'URL'): URL {
12
+ try {
13
+ return new URL(url)
14
+ } catch (error) {
15
+ throw new Error(`Invalid ${context}: ${url}${error instanceof Error ? ` (${error.message})` : ''}`)
16
+ }
17
+ }
18
+
19
+ /**
20
+ * Validates that a URL uses HTTP or HTTPS protocol
21
+ * @throws Error if protocol is not HTTP/HTTPS
22
+ */
23
+ public validateHttpProtocol(url: URL): void {
24
+ if (!url.protocol.startsWith('http')) {
25
+ throw new Error(`Invalid targetBaseUrl protocol: ${url.protocol} (must be http or https)`)
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Extracts the path portion after the source base URL
31
+ */
32
+ public extractSourcePath(requestUrl: string, sourceBaseUrl: string): string {
33
+ return PathHelper.extractPath(requestUrl, sourceBaseUrl)
34
+ }
35
+
36
+ /**
37
+ * Applies path rewrite function if provided, otherwise returns the path as-is
38
+ */
39
+ public applyPathRewrite(sourcePath: string, pathRewrite?: (path: string) => string): string {
40
+ return pathRewrite ? pathRewrite(sourcePath) : sourcePath
41
+ }
42
+
43
+ /**
44
+ * Builds the complete target URL from base URL and processed path
45
+ */
46
+ public buildTargetUrl(targetBaseUrl: string, targetPath: string): string {
47
+ return PathHelper.joinUrl(targetBaseUrl, targetPath)
48
+ }
49
+
50
+ /**
51
+ * Processes the full request URL to produce a target URL
52
+ * @throws Error if the resulting target URL is invalid
53
+ */
54
+ public processUrl(
55
+ requestUrl: string,
56
+ sourceBaseUrl: string,
57
+ targetBaseUrl: string,
58
+ pathRewrite?: (path: string) => string,
59
+ ): string {
60
+ const sourcePath = this.extractSourcePath(requestUrl, sourceBaseUrl)
61
+ const targetPath = this.applyPathRewrite(sourcePath, pathRewrite)
62
+ const targetUrl = this.buildTargetUrl(targetBaseUrl, targetPath)
63
+
64
+ // Validate the resulting target URL
65
+ this.validateUrl(targetUrl, 'target URL')
66
+
67
+ return targetUrl
68
+ }
69
+ }