@@ -25,10 +25,10 @@ let config = {
2525// Parse command line arguments
2626function parseArgs ( ) {
2727 const args = process . argv . slice ( 2 ) ;
28-
28+
2929 for ( let i = 0 ; i < args . length ; i ++ ) {
3030 const arg = args [ i ] ;
31-
31+
3232 if ( arg === '-p' && args [ i + 1 ] ) {
3333 config . port = parseInt ( args [ i + 1 ] ) ;
3434 i ++ ;
@@ -73,38 +73,38 @@ proxy.on('proxyReq', (proxyReq, req) => {
7373 // Transform localhost:8000 to appear as phcode.dev domain
7474 const originalReferer = req . headers . referer ;
7575 const originalOrigin = req . headers . origin ;
76-
76+
7777 // Set target host
7878 const accountHost = new URL ( ACCOUNT_SERVER ) . hostname ;
7979 proxyReq . setHeader ( 'Host' , accountHost ) ;
80-
80+
8181 // Transform referer from localhost:8000 to phcode.dev
8282 if ( originalReferer && originalReferer . includes ( 'localhost:8000' ) ) {
8383 const newReferer = originalReferer . replace ( / l o c a l h o s t : 8 0 0 0 / g, 'phcode.dev' ) ;
8484 proxyReq . setHeader ( 'Referer' , newReferer ) ;
8585 } else if ( ! originalReferer ) {
8686 proxyReq . setHeader ( 'Referer' , 'https://phcode.dev/' ) ;
8787 }
88-
88+
8989 // Transform origin from localhost:8000 to phcode.dev
9090 if ( originalOrigin && originalOrigin . includes ( 'localhost:8000' ) ) {
9191 const newOrigin = originalOrigin . replace ( / l o c a l h o s t : 8 0 0 0 / g, 'phcode.dev' ) ;
9292 proxyReq . setHeader ( 'Origin' , newOrigin ) ;
9393 } else if ( ! originalOrigin ) {
9494 proxyReq . setHeader ( 'Origin' , 'https://phcode.dev' ) ;
9595 }
96-
96+
9797 // Ensure HTTPS scheme
9898 proxyReq . setHeader ( 'X-Forwarded-Proto' , 'https' ) ;
9999 proxyReq . setHeader ( 'X-Forwarded-For' , req . connection . remoteAddress ) ;
100-
100+
101101} ) ;
102102
103103// Modify proxy response headers
104104proxy . on ( 'proxyRes' , ( proxyRes , req , res ) => {
105105 // Pass through cache control and other security headers
106106 // But translate any domain references back to localhost for the browser
107-
107+
108108 const setCookieHeader = proxyRes . headers [ 'set-cookie' ] ;
109109 if ( setCookieHeader ) {
110110 // Transform any phcode.dev domain cookies back to localhost
@@ -113,7 +113,7 @@ proxy.on('proxyRes', (proxyRes, req, res) => {
113113 } ) ;
114114 proxyRes . headers [ 'set-cookie' ] = modifiedCookies ;
115115 }
116-
116+
117117 // Ensure CORS headers if needed
118118 if ( config . cors ) {
119119 proxyRes . headers [ 'Access-Control-Allow-Origin' ] = '*' ;
@@ -153,7 +153,7 @@ function serveStaticFile(req, res, filePath) {
153153 res . end ( 'File not found' ) ;
154154 return ;
155155 }
156-
156+
157157 if ( stats . isDirectory ( ) ) {
158158 // Try to serve index.html from directory
159159 const indexPath = path . join ( filePath , 'index.html' ) ;
@@ -168,7 +168,7 @@ function serveStaticFile(req, res, filePath) {
168168 res . end ( 'Error reading directory' ) ;
169169 return ;
170170 }
171-
171+
172172 const html = `
173173 <!DOCTYPE html>
174174 <html>
@@ -183,56 +183,56 @@ function serveStaticFile(req, res, filePath) {
183183 </body>
184184 </html>
185185 ` ;
186-
186+
187187 const headers = {
188188 'Content-Type' : 'text/html' ,
189189 'Content-Length' : Buffer . byteLength ( html )
190190 } ;
191-
191+
192192 if ( ! config . cache ) {
193193 headers [ 'Cache-Control' ] = 'no-cache, no-store, must-revalidate' ;
194194 headers [ 'Pragma' ] = 'no-cache' ;
195195 headers [ 'Expires' ] = '0' ;
196196 }
197-
197+
198198 if ( config . cors ) {
199199 headers [ 'Access-Control-Allow-Origin' ] = '*' ;
200200 headers [ 'Access-Control-Allow-Methods' ] = 'GET, POST, PUT, DELETE, OPTIONS' ;
201201 headers [ 'Access-Control-Allow-Headers' ] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control' ;
202202 }
203-
203+
204204 res . writeHead ( 200 , headers ) ;
205205 res . end ( html ) ;
206206 } ) ;
207207 }
208208 } ) ;
209209 return ;
210210 }
211-
211+
212212 // Serve file
213213 const mimeType = getMimeType ( filePath ) ;
214214 const headers = {
215215 'Content-Type' : mimeType ,
216216 'Content-Length' : stats . size
217217 } ;
218-
218+
219219 if ( ! config . cache ) {
220220 headers [ 'Cache-Control' ] = 'no-cache, no-store, must-revalidate' ;
221221 headers [ 'Pragma' ] = 'no-cache' ;
222222 headers [ 'Expires' ] = '0' ;
223223 }
224-
224+
225225 if ( config . cors ) {
226226 headers [ 'Access-Control-Allow-Origin' ] = '*' ;
227227 headers [ 'Access-Control-Allow-Methods' ] = 'GET, POST, PUT, DELETE, OPTIONS' ;
228228 headers [ 'Access-Control-Allow-Headers' ] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control' ;
229229 }
230-
230+
231231 res . writeHead ( 200 , headers ) ;
232-
232+
233233 const stream = fs . createReadStream ( filePath ) ;
234234 stream . pipe ( res ) ;
235-
235+
236236 stream . on ( 'error' , ( err ) => {
237237 res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
238238 res . end ( 'Error reading file' ) ;
@@ -243,7 +243,7 @@ function serveStaticFile(req, res, filePath) {
243243// Create HTTP server
244244const server = http . createServer ( ( req , res ) => {
245245 const parsedUrl = url . parse ( req . url , true ) ;
246-
246+
247247 // Handle CORS preflight
248248 if ( req . method === 'OPTIONS' && config . cors ) {
249249 res . writeHead ( 200 , {
@@ -254,20 +254,20 @@ const server = http.createServer((req, res) => {
254254 res . end ( ) ;
255255 return ;
256256 }
257-
257+
258258 // Check if this is a proxy request
259259 if ( parsedUrl . pathname . startsWith ( '/proxy/accounts' ) ) {
260260 // Extract the path after /proxy/accounts
261261 const targetPath = parsedUrl . pathname . replace ( '/proxy/accounts' , '' ) ;
262262 const originalUrl = req . url ;
263-
263+
264264 // Modify the request URL for the proxy
265265 req . url = targetPath + ( parsedUrl . search || '' ) ;
266-
266+
267267 if ( ! config . silent ) {
268268 console . log ( `[PROXY] ${ req . method } ${ originalUrl } -> ${ ACCOUNT_SERVER } ${ req . url } ` ) ;
269269 }
270-
270+
271271 // Proxy the request
272272 proxy . web ( req , res , {
273273 target : ACCOUNT_SERVER ,
@@ -276,23 +276,23 @@ const server = http.createServer((req, res) => {
276276 } ) ;
277277 return ;
278278 }
279-
279+
280280 // Serve static files
281281 let filePath = path . join ( config . root , parsedUrl . pathname ) ;
282-
282+
283283 // Security: prevent directory traversal
284284 const normalizedPath = path . normalize ( filePath ) ;
285285 if ( ! normalizedPath . startsWith ( config . root ) ) {
286286 res . writeHead ( 403 , { 'Content-Type' : 'text/plain' } ) ;
287287 res . end ( 'Forbidden' ) ;
288288 return ;
289289 }
290-
290+
291291 if ( ! config . silent ) {
292292 const clientIp = req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress ;
293293 console . log ( `[${ new Date ( ) . toISOString ( ) } ] ${ req . method } ${ req . url } ${ config . logIp ? ` (${ clientIp } )` : '' } ` ) ;
294294 }
295-
295+
296296 // Handle directory requests without trailing slash
297297 fs . stat ( filePath , ( err , stats ) => {
298298 if ( err ) {
@@ -334,4 +334,4 @@ process.on('SIGTERM', () => {
334334 server . close ( ( ) => {
335335 process . exit ( 0 ) ;
336336 } ) ;
337- } ) ;
337+ } ) ;
0 commit comments