@robiki/proxy 1.0.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.
@@ -0,0 +1,180 @@
1
+ import { OutgoingHttpHeaders, IncomingMessage } from 'node:http';
2
+ import { IncomingHttpHeaders } from 'node:http2';
3
+ import { TLSSocket } from 'node:tls';
4
+ import WebSocket from 'ws';
5
+
6
+ declare enum RequestType {
7
+ API = "api",
8
+ STREAM = "stream",
9
+ WEBSOCKET = "websocket"
10
+ }
11
+ interface TLSWebSocket extends WebSocket {
12
+ _socket: TLSSocket;
13
+ }
14
+ type Router = (req: any, res: any) => void;
15
+ type WebSocketRouter = (req: IncomingMessage, socket: TLSWebSocket, headers: IncomingHttpHeaders) => void;
16
+ type Streamer = (stream: any, headers: any, flags: any) => void;
17
+ interface ForwardValidationResult {
18
+ status: boolean;
19
+ message?: string;
20
+ code?: number;
21
+ headers?: OutgoingHttpHeaders;
22
+ }
23
+ interface ConnectionInfo {
24
+ id: number;
25
+ method: string;
26
+ path: string;
27
+ remoteAddress: string;
28
+ scheme: string;
29
+ authority: string;
30
+ origin: string;
31
+ headers: IncomingHttpHeaders;
32
+ query: URLSearchParams;
33
+ type: RequestType;
34
+ respond: (status: number, headers?: Record<string, string>, body?: string) => void;
35
+ end: (body?: string) => void;
36
+ }
37
+
38
+ /**
39
+ * Route configuration for a specific domain/host
40
+ */
41
+ interface RouteConfig {
42
+ /** Target host:port to proxy to */
43
+ target: string;
44
+ /** Enable SSL/TLS for the target */
45
+ ssl?: boolean;
46
+ /** Remap the URL path before forwarding */
47
+ remap?: (url: string) => string;
48
+ /** Custom CORS configuration */
49
+ cors?: CorsConfig;
50
+ /** Validation function for this route */
51
+ validate?: (info: ConnectionInfo) => Promise<ForwardValidationResult>;
52
+ }
53
+ /**
54
+ * CORS configuration
55
+ */
56
+ interface CorsConfig {
57
+ /** Allowed origins (array of strings or '*' for all) */
58
+ origin?: string | string[];
59
+ /** Allowed HTTP methods */
60
+ methods?: string[];
61
+ /** Allowed headers */
62
+ allowedHeaders?: string[];
63
+ /** Exposed headers */
64
+ exposedHeaders?: string[];
65
+ /** Allow credentials */
66
+ credentials?: boolean;
67
+ /** Max age for preflight cache */
68
+ maxAge?: number;
69
+ }
70
+ /**
71
+ * SSL/TLS certificate configuration
72
+ */
73
+ interface CertificateConfig {
74
+ /** Path to private key file or key content */
75
+ key: string;
76
+ /** Path to certificate file or cert content */
77
+ cert: string;
78
+ /** Path to CA file or CA content */
79
+ ca?: string;
80
+ /** Allow HTTP/1.1 fallback */
81
+ allowHTTP1?: boolean;
82
+ }
83
+ /**
84
+ * Server configuration
85
+ */
86
+ interface ServerConfig {
87
+ /** SSL/TLS certificate configuration */
88
+ ssl?: CertificateConfig;
89
+ /** Route configurations mapped by host */
90
+ routes: Record<string, RouteConfig>;
91
+ /** Default CORS configuration */
92
+ cors?: CorsConfig;
93
+ /** Global validation function */
94
+ validate?: (info: ConnectionInfo) => Promise<ForwardValidationResult>;
95
+ }
96
+ /**
97
+ * Proxy configuration manager
98
+ */
99
+ declare class ProxyConfig {
100
+ private config;
101
+ private sslConfig?;
102
+ constructor(config: ServerConfig);
103
+ /**
104
+ * Initialize SSL configuration
105
+ */
106
+ private initializeSSL;
107
+ /**
108
+ * Get SSL configuration
109
+ */
110
+ getSSL(): {
111
+ key: Buffer;
112
+ cert: Buffer;
113
+ ca?: Buffer;
114
+ allowHTTP1?: boolean;
115
+ } | undefined;
116
+ /**
117
+ * Get route configuration for a host
118
+ */
119
+ getRoute(host: string): RouteConfig | undefined;
120
+ /**
121
+ * Get target for a host
122
+ */
123
+ getTarget(host: string): {
124
+ target: undefined;
125
+ ssl: undefined;
126
+ remap: undefined;
127
+ } | {
128
+ target: string;
129
+ ssl: {
130
+ key: Buffer;
131
+ cert: Buffer;
132
+ ca?: Buffer;
133
+ allowHTTP1?: boolean;
134
+ } | undefined;
135
+ remap: ((url: string) => string) | undefined;
136
+ };
137
+ /**
138
+ * Get CORS headers for a request
139
+ */
140
+ getCorsHeaders(origin: string, host?: string): OutgoingHttpHeaders;
141
+ /**
142
+ * Validate a request
143
+ */
144
+ validate(info: ConnectionInfo): Promise<ForwardValidationResult>;
145
+ /**
146
+ * Get ports to listen on
147
+ */
148
+ getPorts(): number[];
149
+ /**
150
+ * Get the full configuration
151
+ */
152
+ getConfig(): ServerConfig;
153
+ }
154
+ /**
155
+ * Initialize the global configuration
156
+ */
157
+ declare function initConfig(config: ServerConfig): ProxyConfig;
158
+ /**
159
+ * Get the global configuration
160
+ */
161
+ declare function getConfig(): ProxyConfig;
162
+ /**
163
+ * Load configuration with cascading priority:
164
+ * 1. Programmatic config (highest priority)
165
+ * 2. Environment variables
166
+ * 3. Config file
167
+ * 4. Defaults (lowest priority)
168
+ */
169
+ declare function loadConfig(programmaticConfig?: Partial<ServerConfig>): ProxyConfig;
170
+ /**
171
+ * Load configuration from a file (deprecated - use loadConfig instead)
172
+ */
173
+ declare function loadConfigFromFile(path: string): ProxyConfig;
174
+ /**
175
+ * Load configuration from environment variables (deprecated - use loadConfig instead)
176
+ */
177
+ declare function loadConfigFromEnv(): ProxyConfig;
178
+
179
+ export { ProxyConfig as P, RequestType as e, loadConfigFromFile as f, getConfig as g, loadConfigFromEnv as h, initConfig as i, loadConfig as l };
180
+ export type { CorsConfig as C, ForwardValidationResult as F, Router as R, ServerConfig as S, TLSWebSocket as T, WebSocketRouter as W, Streamer as a, RouteConfig as b, CertificateConfig as c, ConnectionInfo as d };
@@ -0,0 +1,47 @@
1
+ import { T as TLSWebSocket, P as ProxyConfig, S as ServerConfig, R as Router, a as Streamer, W as WebSocketRouter } from './config-_6LOsppp.js';
2
+ export { c as CertificateConfig, d as ConnectionInfo, C as CorsConfig, F as ForwardValidationResult, e as RequestType, b as RouteConfig, g as getConfig, l as loadConfig } from './config-_6LOsppp.js';
3
+ import { IncomingMessage, ServerResponse, IncomingHttpHeaders as IncomingHttpHeaders$1 } from 'node:http';
4
+ import { ServerHttp2Stream, IncomingHttpHeaders } from 'node:http2';
5
+ import 'node:tls';
6
+ import 'ws';
7
+
8
+ declare const restAPIProxyHandler: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
9
+
10
+ declare const streamAPIProxyHandler: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders) => Promise<void>;
11
+
12
+ declare const websocketAPIProxyHandler: (req: IncomingMessage, socket: TLSWebSocket, headers: IncomingHttpHeaders$1) => Promise<void>;
13
+
14
+ /**
15
+ * Proxy server instance
16
+ */
17
+ declare class ProxyServer {
18
+ private config;
19
+ private servers;
20
+ constructor(config: ProxyConfig);
21
+ /**
22
+ * Start the proxy server
23
+ */
24
+ start(): Promise<void>;
25
+ /**
26
+ * Stop the proxy server
27
+ */
28
+ stop(): Promise<void>;
29
+ /**
30
+ * Get the configuration
31
+ */
32
+ getConfig(): ProxyConfig;
33
+ }
34
+ /**
35
+ * Create and start a proxy server
36
+ */
37
+ declare function createProxy(config?: Partial<ServerConfig>): Promise<ProxyServer>;
38
+ /**
39
+ * Create a proxy server with custom handlers
40
+ */
41
+ declare function createCustomProxy(config: Partial<ServerConfig> | undefined, handlers: {
42
+ rest?: Router;
43
+ stream?: Streamer;
44
+ websocket?: WebSocketRouter;
45
+ }): Promise<ProxyServer>;
46
+
47
+ export { ProxyConfig, ProxyServer, Router, ServerConfig, Streamer, WebSocketRouter, createCustomProxy, createProxy, restAPIProxyHandler, streamAPIProxyHandler, websocketAPIProxyHandler };