feat: complete Epic 1 production foundation
This commit is contained in:
@@ -7,7 +7,9 @@ const { URL } = require('url');
|
||||
const ROOT_DIR = path.resolve(__dirname, '..', 'dist');
|
||||
const UI_PORT = Number(process.env.CRANK_E2E_UI_PORT || 3300);
|
||||
const ADMIN_PORT = Number(process.env.CRANK_E2E_ADMIN_PORT || 3301);
|
||||
const MCP_PORT = Number(process.env.CRANK_E2E_MCP_PORT || 3302);
|
||||
const ADMIN_BASE = new URL(`http://127.0.0.1:${ADMIN_PORT}`);
|
||||
const MCP_BASE = new URL(`http://127.0.0.1:${MCP_PORT}`);
|
||||
|
||||
const MIME_TYPES = {
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
@@ -91,6 +93,9 @@ function serveFile(filePath, response) {
|
||||
}
|
||||
|
||||
fs.stat(normalized, (error, stats) => {
|
||||
if (response.destroyed || response.writableEnded) {
|
||||
return;
|
||||
}
|
||||
if (error || !stats.isFile()) {
|
||||
sendNotFound(response);
|
||||
return;
|
||||
@@ -103,19 +108,32 @@ function serveFile(filePath, response) {
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
|
||||
pipeline(fs.createReadStream(normalized), response, () => {});
|
||||
if (response.destroyed || response.writableEnded) {
|
||||
return;
|
||||
}
|
||||
var fileStream = fs.createReadStream(normalized);
|
||||
fileStream.on('error', function() {
|
||||
if (!response.destroyed && !response.writableEnded) response.destroy();
|
||||
});
|
||||
response.on('close', function() {
|
||||
if (!fileStream.destroyed) fileStream.destroy();
|
||||
});
|
||||
try {
|
||||
fileStream.pipe(response);
|
||||
} catch (_error) {
|
||||
fileStream.destroy();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function proxyRequest(request, response) {
|
||||
const target = new URL(request.url, ADMIN_BASE);
|
||||
function proxyRequest(request, response, base, targetPath) {
|
||||
const target = new URL(targetPath || request.url, base);
|
||||
const proxy = http.request(
|
||||
target,
|
||||
{
|
||||
method: request.method,
|
||||
headers: {
|
||||
...request.headers,
|
||||
host: `127.0.0.1:${ADMIN_PORT}`,
|
||||
},
|
||||
},
|
||||
(proxyResponse) => {
|
||||
@@ -130,16 +148,25 @@ function proxyRequest(request, response) {
|
||||
response.on('close', function() {
|
||||
if (!proxyResponse.destroyed) proxyResponse.destroy();
|
||||
});
|
||||
proxyResponse.pipe(response);
|
||||
try {
|
||||
proxyResponse.pipe(response);
|
||||
} catch (_error) {
|
||||
proxyResponse.destroy();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
proxy.on('error', () => {
|
||||
if (response.destroyed || response.writableEnded) return;
|
||||
response.writeHead(502, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||||
response.end('Bad Gateway');
|
||||
});
|
||||
|
||||
pipeline(request, proxy, () => {});
|
||||
try {
|
||||
pipeline(request, proxy, () => {});
|
||||
} catch (_error) {
|
||||
proxy.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
const server = http.createServer((request, response) => {
|
||||
@@ -189,7 +216,12 @@ const server = http.createServer((request, response) => {
|
||||
}
|
||||
|
||||
if (urlPath.startsWith('/api/auth/') || urlPath.startsWith('/api/admin/')) {
|
||||
proxyRequest(request, response);
|
||||
proxyRequest(request, response, ADMIN_BASE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (urlPath.startsWith('/mcp/')) {
|
||||
proxyRequest(request, response, MCP_BASE, request.url.slice('/mcp'.length));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user