Skip to content

Commit

Permalink
Set astro cookies in LambdaEdge function response (#62)
Browse files Browse the repository at this point in the history
Set astro cookies in LambdEdge function response
  • Loading branch information
kasleet authored Sep 10, 2023
1 parent 0d7cdfe commit c8d518c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ yarn-debug.log*
yarn-error.log*
package.tgz
apps/www/src/pages/docs/packages/*

.idea
61 changes: 60 additions & 1 deletion packages/adapter/src/lambda/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { App } from "astro/app";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { faker } from "@faker-js/faker";

import { createLambdaFunctionResponse } from "../helpers.js";
import { createLambdaFunctionResponse, createLambdaEdgeFunctionResponse } from "../helpers.js";

const knownBinaryMediaTypes = new Set(["image/png", "image/jpeg"]);

Expand Down Expand Up @@ -45,4 +45,63 @@ describe("helpers", () => {
});
});
});

describe("createLambdaEdgeFunctionResponse", () => {
let response: Response,
app: App,
fnResponse: Awaited<ReturnType<typeof createLambdaEdgeFunctionResponse>>,
body: string,
headers: Record<string, string>;

beforeEach(async () => {
body = faker.datatype.string();
headers = {
"content-type": "text/plain",
};

response = new Response(body, {
headers,
status: 200,
});

app = {
setCookieHeaders: vi.fn(() => [
"newCookie=newValue",
"deleteCookie=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
]),
} as unknown as App;

fnResponse = await createLambdaEdgeFunctionResponse(app, response, knownBinaryMediaTypes);
});

test("creates function response", () => {
expect(app.setCookieHeaders).toHaveBeenCalledTimes(1);
expect(app.setCookieHeaders).toHaveBeenCalledWith(response);

expect(fnResponse).toStrictEqual({
body,
bodyEncoding: "text",
headers: {
"content-type": [
{
key: "content-type",
value: "text/plain",
},
],
"set-cookie": [
{
key: "set-cookie",
value: "newCookie=newValue",
},
{
key: "set-cookie",
value: "deleteCookie=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT",
},
],
},
status: "200",
statusDescription: "",
});
});
});
});
36 changes: 22 additions & 14 deletions packages/adapter/src/lambda/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,31 @@ export const createLambdaEdgeFunctionResponse = async (
response: Response,
knownBinaryMediaTypes: Set<string>,
): Promise<CloudFrontRequestResult> => {
app.setCookieHeaders(response);
const cookies = [...app.setCookieHeaders(response)];

const responseHeadersObj = Object.fromEntries(response.headers.entries());

const headers: CloudFrontHeaders = Object.fromEntries(
Object.entries(responseHeadersObj)
.filter(([key]) => !DISALLOWED_EDGE_HEADERS.some((reg) => reg.test(key.toLowerCase())))
.map(([key, value]) => [
key.toLowerCase(),
[
{
key,
value,
},
],
]),
);
const headers: CloudFrontHeaders = {
...Object.fromEntries(
Object.entries(responseHeadersObj)
.filter(([key]) => !DISALLOWED_EDGE_HEADERS.some((reg) => reg.test(key.toLowerCase())))
.map(([key, value]) => [
key.toLowerCase(),
[
{
key,
value,
},
],
]),
),
...(cookies.length > 0 && {
'set-cookie': cookies.map((cookie) => ({
key: 'set-cookie',
value: cookie,
})),
}),
}
const responseContentType = parseContentType(response.headers.get("content-type"));
const bodyEncoding = knownBinaryMediaTypes.has(responseContentType) ? "base64" : "text";

Expand Down

0 comments on commit c8d518c

Please sign in to comment.