From a21c6dd4dfa36da17f292b933791c8a081547472 Mon Sep 17 00:00:00 2001 From: rjwu95 Date: Tue, 18 Feb 2025 15:58:18 +0900 Subject: [PATCH] fix: Fix error in Lambda@Edge by replacing return with callback --- plugins/aws/lambda/index.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/plugins/aws/lambda/index.ts b/plugins/aws/lambda/index.ts index 1bacc427..a0b1d335 100644 --- a/plugins/aws/lambda/index.ts +++ b/plugins/aws/lambda/index.ts @@ -2,7 +2,7 @@ import type { Bundle } from "@hot-updater/core"; import { filterCompatibleAppVersions, getUpdateInfo } from "@hot-updater/js"; import type { CloudFrontRequestHandler } from "aws-lambda"; -export const handler: CloudFrontRequestHandler = async (event) => { +export const handler: CloudFrontRequestHandler = async (event, context, callback) => { const request = event.Records[0].cf.request; const headers = request.headers; @@ -12,17 +12,18 @@ export const handler: CloudFrontRequestHandler = async (event) => { const distributionDomain = headers["host"][0]?.value; - const bundleId = headers["x-bundle-id"][0]?.value as string; - const appPlatform = headers["x-app-platform"][0]?.value as "ios" | "android"; - const appVersion = headers["x-app-version"][0]?.value as string; + const bundleId = headers["x-bundle-id"]?.[0]?.value as string; + const appPlatform = headers["x-app-platform"]?.[0]?.value as "ios" | "android"; + const appVersion = headers["x-app-version"]?.[0]?.value as string; if (!bundleId || !appPlatform || !appVersion) { - return { + callback(null, { status: "400", body: JSON.stringify({ error: "Missing bundleId, appPlatform, or appVersion", }), - }; + }); + return; } const targetAppVersionListUrl = `https://${distributionDomain}/${appPlatform}/target-app-versions.json`; @@ -31,12 +32,13 @@ export const handler: CloudFrontRequestHandler = async (event) => { method: "GET", }); if (!targetAppVersionListResponse.ok) { - return { + callback(null, { status: "404", body: JSON.stringify({ error: `Failed to fetch ${appPlatform}/target-app-versions.json`, }), - }; + }); + return; } const targetAppVersionList = @@ -48,13 +50,14 @@ export const handler: CloudFrontRequestHandler = async (event) => { ); if (!matchingVersionList) { - return { + callback(null, { status: "200", headers: { "Content-Type": [{ key: "Content-Type", value: "application/json" }], }, body: JSON.stringify(null), - }; + }); + return; } const results = await Promise.allSettled( @@ -75,11 +78,11 @@ export const handler: CloudFrontRequestHandler = async (event) => { appVersion, }); - return { + callback(null, { status: "200", headers: { "Content-Type": [{ key: "Content-Type", value: "application/json" }], }, body: JSON.stringify(updateInfo), - }; + }); };