From f92db4fa59ea53bc3c7efbd9d1d4f80c7cf94895 Mon Sep 17 00:00:00 2001 From: Douglas Gubert Date: Wed, 19 Jan 2022 17:46:01 -0300 Subject: [PATCH] Regression: Fix handling of http requests in apps bridge (#24211) --- app/apps/server/bridges/http.ts | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/app/apps/server/bridges/http.ts b/app/apps/server/bridges/http.ts index 02c171cd3fc..7deddd3a6ed 100644 --- a/app/apps/server/bridges/http.ts +++ b/app/apps/server/bridges/http.ts @@ -78,16 +78,33 @@ export class AppHttpBridge extends HttpBridge { url: info.url, method: info.method, statusCode: response.status, - data: await response.json(), headers: Object.fromEntries(response.headers as unknown as any), }; - if (request.hasOwnProperty('encoding')) { - if (request.encoding === null) { - result.content = Buffer.from(await response.arrayBuffer()).toString(); - } else { - result.content = await response.text(); - } + const body = Buffer.from(await response.arrayBuffer()); + + if (request.encoding === null) { + /** + * The property `content` is not appropriately typed in the + * Apps-engine definition, and we can't simply change it there + * as it would be a breaking change. Thus, we're left with this + * type assertion. + */ + result.content = body as any; + } else { + result.content = body.toString(request.encoding); + result.data = ((): any => { + const contentType = (response.headers.get('content-type') || '').split(';')[0]; + if (!['application/json', 'text/javascript', 'application/javascript', 'application/x-javascript'].includes(contentType)) { + return null; + } + + try { + return JSON.parse(result.content); + } catch { + return null; + } + })(); } return result;