Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions integration/scripts/proxyServer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IncomingMessage, ServerResponse } from 'node:http';
import http from 'node:http';
import type { createServer as _createServer, Server, ServerOptions } from 'node:https';
import https from 'node:https';
Expand All @@ -14,14 +15,23 @@ type ProxyServerOptions = {
* The server will listen on port 80 (http) or 443 (https) depending on whether SSL options are provided.
*/
export const createProxyServer = (opts: ProxyServerOptions) => {
const proxy = httpProxy.createProxyServer({ xfwd: true });
const usingSSL = !!opts.ssl;

const proxy = httpProxy.createProxyServer({
secure: usingSSL,
xfwd: true,
});

// We need to handle errors to avoid crashing the proxy server
proxy.on('error', (err: Error, req: IncomingMessage, res: ServerResponse) => {
console.error(`[Proxy Error]: ${req.url}`, err);
res.writeHead(502);
res.end('Proxy error');
});

const createServer: typeof _createServer = usingSSL ? https.createServer.bind(https) : http.createServer.bind(http);

return createServer(opts.ssl, (req, res) => {
console.log(`/n/n/n/n------------------------------------`);
console.log('Proxying request', req.headers.host, req.url);
console.log('Headers', req.headers);
const hostHeader = req.headers.host || '';
if (opts.targets[hostHeader]) {
proxy.web(req, res, { target: opts.targets[hostHeader] });
Expand Down
Loading