@yeaft/webchat-agent 0.1.40 → 0.1.42
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 +16 -3
package/package.json
CHANGED
package/proxy.js
CHANGED
|
@@ -4,7 +4,7 @@ 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 } = msg;
|
|
7
|
+
const { requestId, port, method, path, headers, body, scheme, host, basePath } = msg;
|
|
8
8
|
const effectiveHost = host || 'localhost';
|
|
9
9
|
const effectiveScheme = scheme || 'http';
|
|
10
10
|
const httpModule = effectiveScheme === 'https' ? https : http;
|
|
@@ -60,12 +60,25 @@ export function handleProxyHttpRequest(msg) {
|
|
|
60
60
|
const chunks = [];
|
|
61
61
|
res.on('data', (chunk) => chunks.push(chunk));
|
|
62
62
|
res.on('end', () => {
|
|
63
|
-
|
|
63
|
+
let responseBody = Buffer.concat(chunks);
|
|
64
|
+
const responseHeaders = { ...res.headers };
|
|
65
|
+
|
|
66
|
+
// Rewrite absolute paths in HTML responses
|
|
67
|
+
if (basePath && contentType.includes('text/html')) {
|
|
68
|
+
let html = responseBody.toString('utf-8');
|
|
69
|
+
html = html
|
|
70
|
+
.replace(/((?:src|href|action)\s*=\s*["'])\//gi, `$1${basePath}/`)
|
|
71
|
+
.replace(/(url\s*\(\s*["']?)\//gi, `$1${basePath}/`);
|
|
72
|
+
responseBody = Buffer.from(html, 'utf-8');
|
|
73
|
+
// Update content-length since rewritten paths are longer
|
|
74
|
+
delete responseHeaders['content-length'];
|
|
75
|
+
}
|
|
76
|
+
|
|
64
77
|
ctx.sendToServer({
|
|
65
78
|
type: 'proxy_response',
|
|
66
79
|
requestId,
|
|
67
80
|
statusCode: res.statusCode,
|
|
68
|
-
headers:
|
|
81
|
+
headers: responseHeaders,
|
|
69
82
|
body: responseBody.toString('base64')
|
|
70
83
|
});
|
|
71
84
|
});
|