-
Notifications
You must be signed in to change notification settings - Fork 513
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
feat: add edge support to aws-lambda presets #1557
Draft
Hebilicious
wants to merge
5
commits into
main
Choose a base branch
from
feat/aws
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3463de
feat: add edge support to aws-lambda preset
Hebilicious f38a07b
chore: remove logs and comments
Hebilicious 7779b9c
feat: add sst
Hebilicious 44f8e67
refactor: simplify and typos
Hebilicious 8c4bf2c
chore: rebase error
Hebilicious File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,34 @@ | ||
import { defineNitroPreset } from "../preset"; | ||
import { generateCdkApp } from "../utils/cdk"; | ||
import { generateSST } from "../utils/sst"; | ||
|
||
export const awsLambda = defineNitroPreset({ | ||
entry: "#internal/nitro/entries/aws-lambda", | ||
entry: `#internal/nitro/entries/aws-lambda-{{ awsLambda.target }}`, | ||
awsLambda: { | ||
// we need this defined here so it's picked up by the template in lambda entries | ||
target: (process.env.NITRO_AWS_LAMBDA_TARGET || "default") as any, | ||
}, | ||
hooks: { | ||
"rollup:before": (nitro) => { | ||
const target = nitro.options.awsLambda?.target as unknown; | ||
if (!target || target === "default") { | ||
nitro.logger.warn( | ||
"Neither `awsLambda.target` or `NITRO_AWS_LAMBDA_TARGET` is set. Set the target to remove this warning. See https://nitro.unjs.io/deploy/providers/aws-lambda for more information." | ||
); | ||
// Default to single region lambda | ||
nitro.options.awsLambda = { target: "single" }; | ||
} | ||
}, | ||
async compiled(nitro) { | ||
if (nitro.options.awsLambda.sst) { | ||
await generateSST(nitro); | ||
} | ||
if ( | ||
nitro.options.awsLambda.target === "edge" && | ||
nitro.options.awsLambda.cdk === true | ||
) { | ||
await generateCdkApp(nitro); | ||
} | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// we need this file to detect if the user is passing a `target` property | ||
export { handler } from "./aws-lambda-single"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { | ||
CloudFrontRequestEvent, | ||
CloudFrontResultResponse, | ||
Context, | ||
} from "aws-lambda"; | ||
import "#internal/nitro/virtual/polyfill"; | ||
import { nitroApp } from "../app"; | ||
import { | ||
normalizeCloudfrontBody, | ||
normalizeCloudfrontIncomingHeaders, | ||
normalizeCloudfrontOutgoingHeaders, | ||
normalizeLambdaOutgoingBody, | ||
} from "../utils.lambda"; | ||
|
||
export const handler = async function handler( | ||
event: CloudFrontRequestEvent, | ||
context: Context | ||
): Promise<CloudFrontResultResponse> { | ||
const request = event.Records[0].cf.request; | ||
|
||
const url = getFullUrl(request.uri, request.querystring); | ||
const headers = normalizeCloudfrontIncomingHeaders(request.headers); | ||
const body = normalizeCloudfrontBody(request.body); | ||
|
||
const r = await nitroApp.localCall({ | ||
event, | ||
url, | ||
context, | ||
headers, | ||
method: request.method, | ||
body, | ||
}); | ||
|
||
return { | ||
status: r.status.toString(), | ||
headers: normalizeCloudfrontOutgoingHeaders(r.headers), | ||
body: normalizeLambdaOutgoingBody(r.body, r.headers), | ||
}; | ||
}; | ||
|
||
function getFullUrl(uri: string, queryString?: string) { | ||
return queryString ? `${uri}?${queryString}` : uri; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have no way to know which entry a preset use, maybe this should be in another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted in #1649