fix(handlerproxy): Use SmartRequest API and improve proxy/asset response handling; update tests and bump dependencies; add local project configuration files

This commit is contained in:
2025-08-17 08:14:08 +00:00
parent db05fc91c4
commit 8cbae75ae4
8 changed files with 140 additions and 35 deletions

View File

@@ -19,18 +19,40 @@ export class HandlerProxy extends Handler {
const relativeRequestPath = req.path.slice(req.route.path.length - 1);
const proxyRequestUrl = remoteMountPointArg + relativeRequestPath;
console.log(`proxy ${req.path} to ${proxyRequestUrl}`);
let proxiedResponse: plugins.smartrequest.IExtendedIncomingMessage;
let proxiedResponse: plugins.smartrequest.ICoreResponse;
try {
proxiedResponse = await plugins.smartrequest.request(proxyRequestUrl, {
method: req.method,
autoJsonParse: false,
});
const smartRequest = plugins.smartrequest.SmartRequest.create()
.url(proxyRequestUrl);
// Execute request based on method
switch (req.method.toUpperCase()) {
case 'GET':
proxiedResponse = await smartRequest.get();
break;
case 'POST':
proxiedResponse = await smartRequest.post();
break;
case 'PUT':
proxiedResponse = await smartRequest.put();
break;
case 'DELETE':
proxiedResponse = await smartRequest.delete();
break;
case 'PATCH':
proxiedResponse = await smartRequest.patch();
break;
default:
// For other methods, default to GET
proxiedResponse = await smartRequest.get();
break;
}
} catch {
res.end('failed to fullfill request');
return;
}
for (const header of Object.keys(proxiedResponse.headers)) {
res.set(header, proxiedResponse.headers[header] as string);
const headers = proxiedResponse.headers;
for (const header of Object.keys(headers)) {
res.set(header, headers[header] as string);
}
// set additional headers
@@ -40,21 +62,20 @@ export class HandlerProxy extends Handler {
}
}
// Ensure body exists and convert it to Buffer consistently
// Get response body as buffer
let responseToSend: Buffer;
if (proxiedResponse.body !== undefined && proxiedResponse.body !== null) {
if (Buffer.isBuffer(proxiedResponse.body)) {
responseToSend = proxiedResponse.body;
} else if (typeof proxiedResponse.body === 'string') {
responseToSend = Buffer.from(proxiedResponse.body);
} else {
// Handle other types (like objects) by JSON stringifying them
responseToSend = Buffer.from(JSON.stringify(proxiedResponse.body));
try {
const arrayBuffer = await proxiedResponse.arrayBuffer();
responseToSend = Buffer.from(arrayBuffer);
} catch {
// If we can't get arrayBuffer, try text
try {
const text = await proxiedResponse.text();
responseToSend = Buffer.from(text);
} catch {
// Provide a default empty buffer if body cannot be read
responseToSend = Buffer.from('');
}
} else {
// Provide a default empty buffer if body is undefined/null
responseToSend = Buffer.from('');
}
if (optionsArg && optionsArg.responseModifier) {