-
Notifications
You must be signed in to change notification settings - Fork 511
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(preset): Aws lambda edge #1075
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f36bdb2
add aws-lambda-edge preset
juankamilo ce0cced
remove unused code, add fullUrl
juankamilo a4ca6b0
decode body if the body comes on base64, remove readonly header conteβ¦
juankamilo edca69a
fix test error on other methods
juankamilo b5b8599
[autofix.ci] apply automated fixes
autofix-ci[bot] 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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { defineNitroPreset } from "../preset"; | ||
|
||
export const awsLambdaEdge = defineNitroPreset({ | ||
entry: "#internal/nitro/entries/aws-lambda-edge", | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { | ||
CloudFrontHeaders, | ||
Context, | ||
CloudFrontRequestEvent, | ||
CloudFrontResultResponse, | ||
} from "aws-lambda"; | ||
import "#internal/nitro/virtual/polyfill"; | ||
import { nitroApp } from "../app"; | ||
|
||
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 r = await nitroApp.localCall({ | ||
event, | ||
url, | ||
context, | ||
headers: normalizeIncomingHeaders(request.headers), | ||
method: request.method, | ||
query: request.querystring, | ||
body: normalizeBody(request.body), | ||
}); | ||
|
||
return { | ||
status: r.status.toString(), | ||
headers: normalizeOutgoingHeaders(r.headers), | ||
body: r.body.toString(), | ||
}; | ||
}; | ||
|
||
function normalizeBody( | ||
body: CloudFrontRequestEvent["Records"][0]["cf"]["request"]["body"] | ||
) { | ||
if (typeof body === "undefined") { | ||
return body; | ||
} | ||
|
||
const bodyString = body; | ||
if (typeof body.encoding !== "undefined" && body.encoding === "base64") { | ||
bodyString.data = Buffer.from(body.data, "base64").toString("utf8"); | ||
bodyString.data = decodeURIComponent(bodyString.data); | ||
} | ||
return bodyString; | ||
} | ||
|
||
function normalizeIncomingHeaders(headers: CloudFrontHeaders) { | ||
return Object.fromEntries( | ||
Object.entries(headers).map(([key, keyValues]) => [ | ||
key, | ||
keyValues.map((kv) => kv.value), | ||
]) | ||
); | ||
} | ||
|
||
function normalizeOutgoingHeaders( | ||
headers: Record<string, string | string[] | undefined> | ||
): CloudFrontHeaders { | ||
const entries = Object.fromEntries( | ||
Object.entries(headers).filter(([key]) => !["content-length"].includes(key)) | ||
); | ||
|
||
return Object.fromEntries( | ||
Object.entries(entries).map(([k, v]) => [ | ||
k, | ||
Array.isArray(v) ? v.map((value) => ({ value })) : [{ value: v }], | ||
]) | ||
); | ||
} | ||
|
||
function getFullUrl(uri: string, querystring: string | undefined) { | ||
return uri + (querystring ? "?" + querystring : ""); | ||
} |
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,64 @@ | ||
import { resolve } from "pathe"; | ||
import { describe } from "vitest"; | ||
import destr from "destr"; | ||
import type { | ||
CloudFrontHeaders, | ||
CloudFrontRequestEvent, | ||
CloudFrontResultResponse, | ||
} from "aws-lambda"; | ||
import { setupTest, testNitro } from "../tests"; | ||
|
||
describe("nitro:preset:aws-lambda-edge", async () => { | ||
const ctx = await setupTest("aws-lambda-edge"); | ||
testNitro(ctx, async () => { | ||
const { handler } = await import(resolve(ctx.outDir, "server/index.mjs")); | ||
return async ({ url: rawRelativeUrl, headers, method, body }) => { | ||
// creating new URL object to parse query easier | ||
const url = new URL(`https://example.com${rawRelativeUrl}`); | ||
// modify headers to CloudFrontHeaders. | ||
const reqHeaders: CloudFrontHeaders = Object.fromEntries( | ||
Object.entries(headers || {}).map(([k, v]) => [ | ||
k, | ||
Array.isArray(v) ? v.map((value) => ({ value })) : [{ value: v }], | ||
]) | ||
); | ||
|
||
const event: CloudFrontRequestEvent = { | ||
Records: [ | ||
{ | ||
cf: { | ||
config: { | ||
distributionDomainName: "nitro.cloudfront.net", | ||
distributionId: "EDFDVBD6EXAMPLE", | ||
eventType: "origin-request", | ||
requestId: | ||
"4TyzHTaYWb1GX1qTfsHhEqV6HUDd_BzoBZnwfnvQc_1oF26ClkoUSEQ==", | ||
}, | ||
request: { | ||
clientIp: "203.0.113.178", | ||
method: method || "GET", | ||
uri: url.pathname, | ||
querystring: url.searchParams.toString(), | ||
headers: reqHeaders, | ||
body, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
const res: CloudFrontResultResponse = await handler(event); | ||
// responsed CloudFrontHeaders are special, so modify them for testing. | ||
const resHeaders = Object.fromEntries( | ||
Object.entries(res.headers).map(([key, keyValues]) => [ | ||
key, | ||
keyValues.map((kv) => kv.value).join(","), | ||
]) | ||
); | ||
return { | ||
data: destr(res.body), | ||
status: Number.parseInt(res.status), | ||
headers: resHeaders, | ||
}; | ||
}; | ||
}); | ||
}); |
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.
typo