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:
		| @@ -3,6 +3,6 @@ | ||||
|  */ | ||||
| export const commitinfo = { | ||||
|   name: '@api.global/typedserver', | ||||
|   version: '3.0.75', | ||||
|   version: '3.0.76', | ||||
|   description: 'A TypeScript-based project for easy serving of static files with support for live reloading, compression, and typed requests.' | ||||
| } | ||||
|   | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -92,7 +92,10 @@ export class UtilityWebsiteServer { | ||||
|         } | ||||
|         const fullOriginAssetUrl = `https://assetbroker.lossless.one/brandfiles/00general/${manifestAssetName}`; | ||||
|         console.log(`Getting ${manifestAssetName} from ${fullOriginAssetUrl}`); | ||||
|         const dataBuffer: Buffer = (await plugins.smartrequest.getBinary(fullOriginAssetUrl)).body; | ||||
|         const smartRequest = plugins.smartrequest.SmartRequest.create(); | ||||
|         const response = await smartRequest.url(fullOriginAssetUrl).get(); | ||||
|         const arrayBuffer = await response.arrayBuffer(); | ||||
|         const dataBuffer: Buffer = Buffer.from(arrayBuffer); | ||||
|         res.type('.png'); | ||||
|         res.write(dataBuffer); | ||||
|         res.end(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user