@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/proxy.js +16 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",
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
- const responseBody = Buffer.concat(chunks);
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: res.headers,
81
+ headers: responseHeaders,
69
82
  body: responseBody.toString('base64')
70
83
  });
71
84
  });