2
2
* proxy.js – zero-dependency worker-based HTTP/WS forwarder
3
3
*/
4
4
5
- const {
6
- Worker,
7
- isMainThread,
8
- parentPort,
9
- workerData,
10
- } = require ( "worker_threads" ) ;
5
+ const { parentPort, workerData } = require ( "worker_threads" ) ;
11
6
12
7
const http = require ( "http" ) ;
13
8
const https = require ( "https" ) ;
@@ -16,33 +11,14 @@ const { URL } = require("url");
16
11
const fs = require ( "fs" ) ;
17
12
const path = require ( "path" ) ;
18
13
19
- /* ─────────────────── configuration (main thread only) ─────────────────── */
20
-
21
- const LISTEN_HOST = "localhost" ;
22
-
23
- if ( isMainThread ) {
24
- // Stand-alone mode: fork the worker and pass through the env as-is
25
- const w = new Worker ( __filename , {
26
- workerData : {
27
- targetOrigin : process . env . TARGET_URL , // may be undefined
28
- } ,
29
- } ) ;
30
-
31
- w . on ( "message" , ( m ) => console . log ( "[proxy-worker]" , m ) ) ;
32
- w . on ( "error" , ( e ) => console . error ( "[proxy-worker] error:" , e ) ) ;
33
- w . on ( "exit" , ( c ) => console . log ( "[proxy-worker] exited" , c ) ) ;
34
- console . log ( "proxy worker launching …" ) ;
35
- return ; // do not execute the rest of the file in the main thread
36
- }
37
-
38
14
/* ──────────────────────────── worker code ─────────────────────────────── */
39
-
40
- const LISTEN_PORT = process . env . LISTEN_PORT || workerData . port ;
15
+ const LISTEN_HOST = "localhost" ;
16
+ const LISTEN_PORT = workerData . port ;
41
17
let rememberedOrigin = null ; // e.g. "http://localhost:5173"
42
18
43
- /* ---------- pre-configure rememberedOrigin from env or workerData ------- */
19
+ /* ---------- pre-configure rememberedOrigin from workerData ------- */
44
20
{
45
- const fixed = process . env . TARGET_URL || workerData ?. targetOrigin ;
21
+ const fixed = workerData ?. targetOrigin ;
46
22
if ( fixed ) {
47
23
try {
48
24
rememberedOrigin = new URL ( fixed ) . origin ;
@@ -51,7 +27,7 @@ let rememberedOrigin = null; // e.g. "http://localhost:5173"
51
27
) ;
52
28
} catch {
53
29
throw new Error (
54
- `Invalid TARGET_URL "${ fixed } ". Must be absolute http/https URL.` ,
30
+ `Invalid target origin "${ fixed } ". Must be absolute http/https URL.` ,
55
31
) ;
56
32
}
57
33
}
@@ -162,21 +138,7 @@ function injectHTML(buf) {
162
138
163
139
/* ---------------- helper: build upstream URL from request -------------- */
164
140
function buildTargetURL ( clientReq ) {
165
- // Support the old "?url=" mechanism
166
- const parsedLocal = new URL ( clientReq . url , `http://${ LISTEN_HOST } ` ) ;
167
- const urlParam = parsedLocal . searchParams . get ( "url" ) ;
168
- if ( urlParam ) {
169
- const abs = new URL ( urlParam ) ;
170
- if ( ! / ^ h t t p s ? : $ / . test ( abs . protocol ) )
171
- throw new Error ( "only http/https targets allowed" ) ;
172
- rememberedOrigin = abs . origin ; // remember for later
173
- return abs ;
174
- }
175
-
176
- if ( ! rememberedOrigin )
177
- throw new Error (
178
- "No upstream configured. Use ?url=… once or set TARGET_URL env var." ,
179
- ) ;
141
+ if ( ! rememberedOrigin ) throw new Error ( "No upstream configured." ) ;
180
142
181
143
// Forward to the remembered origin keeping path & query
182
144
return new URL ( clientReq . url , rememberedOrigin ) ;
0 commit comments