chore: exclude local resource artifacts from main sync
This commit is contained in:
67
scripts/serve-app-dist.mjs
Executable file → Normal file
67
scripts/serve-app-dist.mjs
Executable file → Normal file
@@ -1,13 +1,15 @@
|
||||
import { createReadStream, existsSync, statSync } from 'node:fs';
|
||||
import { extname, isAbsolute, join, normalize } from 'node:path';
|
||||
import { createServer } from 'node:http';
|
||||
import { connect as connectNet } from 'node:net';
|
||||
import { Readable } from 'node:stream';
|
||||
import { connect as connectTls } from 'node:tls';
|
||||
|
||||
const port = Number(process.env.PORT ?? 5173);
|
||||
const distDirName = process.env.APP_DIST_DIR ?? 'app-dist';
|
||||
const rootDir = normalize(isAbsolute(distDirName) ? distDirName : join(process.cwd(), distDirName));
|
||||
const workServerUrl = new URL(process.env.WORK_SERVER_URL ?? 'http://127.0.0.1:3100');
|
||||
const proxyPrefixes = ['/api', '/.codex_chat'];
|
||||
const proxyPrefixes = ['/api', '/.codex_chat', '/ws/chat'];
|
||||
|
||||
const mimeTypes = {
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
@@ -65,7 +67,8 @@ function resolvePath(urlPath) {
|
||||
}
|
||||
|
||||
function shouldProxyRequest(urlPath = '/') {
|
||||
return proxyPrefixes.some((prefix) => urlPath === prefix || urlPath.startsWith(`${prefix}/`));
|
||||
const normalizedPath = urlPath.split('?')[0] ?? urlPath;
|
||||
return proxyPrefixes.some((prefix) => normalizedPath === prefix || normalizedPath.startsWith(`${prefix}/`));
|
||||
}
|
||||
|
||||
function readRequestBody(request) {
|
||||
@@ -155,6 +158,66 @@ const server = createServer(async (request, response) => {
|
||||
createReadStream(resolvedPath).pipe(response);
|
||||
});
|
||||
|
||||
server.on('upgrade', (request, socket, head) => {
|
||||
if (!shouldProxyRequest(request.url ?? '/')) {
|
||||
socket.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
const upstreamPort = Number(
|
||||
workServerUrl.port || (workServerUrl.protocol === 'https:' ? '443' : '80'),
|
||||
);
|
||||
const upstreamSocket =
|
||||
workServerUrl.protocol === 'https:'
|
||||
? connectTls(upstreamPort, workServerUrl.hostname, { servername: workServerUrl.hostname })
|
||||
: connectNet(upstreamPort, workServerUrl.hostname);
|
||||
|
||||
upstreamSocket.on('connect', () => {
|
||||
const headerLines = Object.entries(request.headers)
|
||||
.flatMap(([key, value]) => {
|
||||
if (value == null || key.toLowerCase() === 'host') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Array.isArray(value)
|
||||
? value.map((item) => `${key}: ${item}\r\n`)
|
||||
: [`${key}: ${value}\r\n`];
|
||||
})
|
||||
.join('');
|
||||
|
||||
const requestLine = `${request.method ?? 'GET'} ${request.url ?? '/'} HTTP/${request.httpVersion}\r\n`;
|
||||
upstreamSocket.write(`${requestLine}host: ${workServerUrl.host}\r\n${headerLines}\r\n`);
|
||||
|
||||
if (head?.length) {
|
||||
upstreamSocket.write(head);
|
||||
}
|
||||
});
|
||||
|
||||
upstreamSocket.on('data', (chunk) => {
|
||||
socket.write(chunk);
|
||||
});
|
||||
|
||||
upstreamSocket.on('end', () => {
|
||||
socket.end();
|
||||
});
|
||||
|
||||
upstreamSocket.on('error', () => {
|
||||
socket.destroy();
|
||||
});
|
||||
|
||||
socket.on('data', (chunk) => {
|
||||
upstreamSocket.write(chunk);
|
||||
});
|
||||
|
||||
socket.on('end', () => {
|
||||
upstreamSocket.end();
|
||||
});
|
||||
|
||||
socket.on('error', () => {
|
||||
upstreamSocket.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, '0.0.0.0', () => {
|
||||
console.log(`${distDirName} server listening on http://0.0.0.0:${port}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user