From 533df1ba1cd316d953163588eba2d66abc50653e Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Mon, 21 Nov 2022 17:04:06 +0800 Subject: [PATCH] AWS Lambda: Special case API Gateway responses to recompress data, to work around base64/binary content issues. --- serverless/aws/src/index.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/serverless/aws/src/index.ts b/serverless/aws/src/index.ts index c1f3a08..c2139ff 100644 --- a/serverless/aws/src/index.ts +++ b/serverless/aws/src/index.ts @@ -201,14 +201,26 @@ export const handler = async ( const tile_result = await p.getZxy(tile[0], tile[1], tile[2]); if (tile_result) { - // returns uncompressed response - // TODO: may need to special case API gateway to return compressed response with gzip content-encoding header - return apiResp( - 200, - Buffer.from(tile_result.data).toString("base64"), - true, - headers - ); + if (is_api_gateway) { + // this is wasted work, but we need to force API Gateway to interpret the Lambda response as binary + // without depending on clients sending matching Accept: headers in the request. + const recompressed_data = zlib.gzipSync(tile_result.data); + headers["Content-Encoding"] = "gzip"; + return apiResp( + 200, + Buffer.from(recompressed_data).toString("base64"), + true, + headers + ); + } else { + // returns uncompressed response + return apiResp( + 200, + Buffer.from(tile_result.data).toString("base64"), + true, + headers + ); + } } else { return apiResp(204, "", false, headers); }