Skip to content

Commit

Permalink
fix: Fix error in Lambda@Edge by replacing return with callback (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjwu95 authored Feb 18, 2025
1 parent 49384a6 commit 6b632d1
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions plugins/aws/lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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`;
Expand All @@ -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 =
Expand All @@ -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(
Expand All @@ -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),
};
});
};

0 comments on commit 6b632d1

Please sign in to comment.