@yeaft/webchat-agent 0.1.137 → 0.1.139
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/proxy.js +42 -3
package/package.json
CHANGED
package/proxy.js
CHANGED
|
@@ -4,9 +4,19 @@ import https from 'https';
|
|
|
4
4
|
import ctx from './context.js';
|
|
5
5
|
|
|
6
6
|
export function handleProxyHttpRequest(msg) {
|
|
7
|
-
const { requestId, port, method, path, headers, body, scheme, host, basePath } = msg;
|
|
7
|
+
const { requestId, port, method, path, headers, body, scheme, host, basePath, publicOrigin } = msg;
|
|
8
8
|
const effectiveHost = host || 'localhost';
|
|
9
9
|
const effectiveScheme = scheme || 'http';
|
|
10
|
+
|
|
11
|
+
// Build list of local origins to rewrite in response bodies and headers
|
|
12
|
+
const localOrigins = [
|
|
13
|
+
`https://${effectiveHost}:${port}`,
|
|
14
|
+
`http://${effectiveHost}:${port}`,
|
|
15
|
+
];
|
|
16
|
+
if (effectiveHost !== 'localhost') {
|
|
17
|
+
localOrigins.push(`https://localhost:${port}`, `http://localhost:${port}`);
|
|
18
|
+
}
|
|
19
|
+
const publicBase = publicOrigin ? `${publicOrigin}${basePath}` : basePath;
|
|
10
20
|
const httpModule = effectiveScheme === 'https' ? https : http;
|
|
11
21
|
|
|
12
22
|
const options = {
|
|
@@ -63,17 +73,46 @@ export function handleProxyHttpRequest(msg) {
|
|
|
63
73
|
let responseBody = Buffer.concat(chunks);
|
|
64
74
|
const responseHeaders = { ...res.headers };
|
|
65
75
|
|
|
66
|
-
//
|
|
76
|
+
// Determine if this is a text response that may contain localhost URLs
|
|
77
|
+
const isTextContent = (
|
|
78
|
+
contentType.includes('text/html') ||
|
|
79
|
+
contentType.includes('application/javascript') ||
|
|
80
|
+
contentType.includes('text/javascript') ||
|
|
81
|
+
contentType.includes('text/css') ||
|
|
82
|
+
contentType.includes('application/json')
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// Rewrite localhost URLs in text response bodies
|
|
86
|
+
if (publicBase && isTextContent) {
|
|
87
|
+
let text = responseBody.toString('utf-8');
|
|
88
|
+
for (const origin of localOrigins) {
|
|
89
|
+
text = text.split(origin).join(publicBase);
|
|
90
|
+
}
|
|
91
|
+
responseBody = Buffer.from(text, 'utf-8');
|
|
92
|
+
delete responseHeaders['content-length'];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Rewrite absolute paths in HTML responses (relative path → basePath)
|
|
67
96
|
if (basePath && contentType.includes('text/html')) {
|
|
68
97
|
let html = responseBody.toString('utf-8');
|
|
69
98
|
html = html
|
|
70
99
|
.replace(/((?:src|href|action)\s*=\s*["'])\//gi, `$1${basePath}/`)
|
|
71
100
|
.replace(/(url\s*\(\s*["']?)\//gi, `$1${basePath}/`);
|
|
72
101
|
responseBody = Buffer.from(html, 'utf-8');
|
|
73
|
-
// Update content-length since rewritten paths are longer
|
|
74
102
|
delete responseHeaders['content-length'];
|
|
75
103
|
}
|
|
76
104
|
|
|
105
|
+
// Rewrite localhost URLs in Location/Content-Location headers
|
|
106
|
+
if (publicBase) {
|
|
107
|
+
for (const hdr of ['location', 'content-location']) {
|
|
108
|
+
if (responseHeaders[hdr]) {
|
|
109
|
+
for (const origin of localOrigins) {
|
|
110
|
+
responseHeaders[hdr] = responseHeaders[hdr].split(origin).join(publicBase);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
77
116
|
ctx.sendToServer({
|
|
78
117
|
type: 'proxy_response',
|
|
79
118
|
requestId,
|