Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix error in Lambda@Edge by replacing return with callback #116

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
};
});
};
Loading