Skip to content

Commit

Permalink
fix(aws-lambda,netlify-lambda): binary body v2 and cookies v1 (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious authored Sep 7, 2023
1 parent 70ebac1 commit 86d05f2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 54 deletions.
31 changes: 12 additions & 19 deletions src/runtime/entries/aws-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,19 @@ export async function handler(
body: event.body, // TODO: handle event.isBase64Encoded
});

// Lambda v2 https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.v2
if ("cookies" in event || "rawPath" in event) {
const cookies = normalizeCookieHeader(r.headers["set-cookie"]);

return {
cookies,
statusCode: r.status,
headers: normalizeLambdaOutgoingHeaders(r.headers, true),
body: await normalizeLambdaOutgoingBody(r.body, r.headers).then(
(r) => r.body
),
};
}

const outBody = await normalizeLambdaOutgoingBody(r.body, r.headers);

// ApiGateway v2 https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.v2
const isApiGwV2 = "cookies" in event || "rawPath" in event;
const awsBody = await normalizeLambdaOutgoingBody(r.body, r.headers);
const cookies = normalizeCookieHeader(r.headers["set-cookie"]);
return {
...(cookies.length > 0 && {
...(isApiGwV2
? { cookies }
: { multiValueHeaders: { "set-cookie": cookies } }),
}),
statusCode: r.status,
headers: normalizeLambdaOutgoingHeaders(r.headers),
body: outBody.body,
isBase64Encoded: outBody.type === "binary",
headers: normalizeLambdaOutgoingHeaders(r.headers, true),
body: awsBody.body,
isBase64Encoded: awsBody.type === "binary",
};
}
12 changes: 6 additions & 6 deletions src/runtime/entries/netlify-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export async function lambda(
});

const cookies = normalizeCookieHeader(String(r.headers["set-cookie"]));
const outBody = await normalizeLambdaOutgoingBody(r.body, r.headers);
const awsBody = await normalizeLambdaOutgoingBody(r.body, r.headers);

return {
statusCode: r.status,
headers: normalizeLambdaOutgoingHeaders(r.headers, true),
body: outBody.body,
isBase64Encoded: outBody.type === "binary",
multiValueHeaders: {
...(cookies.length > 0 ? { "set-cookie": cookies } : {}),
},
body: awsBody.body,
isBase64Encoded: awsBody.type === "binary",
...(cookies.length > 0 && {
multiValueHeaders: { "set-cookie": cookies },
}),
};
}
4 changes: 2 additions & 2 deletions src/runtime/entries/stormkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ export const handler: Handler<StormkitEvent, StormkitResponse> =
body: event.body,
});

const normalizedBody = await normalizeLambdaOutgoingBody(
const awsBody = await normalizeLambdaOutgoingBody(
response.body,
response.headers
);

return <StormkitResponse>{
statusCode: response.status,
headers: normalizeOutgoingHeaders(response.headers),
[normalizedBody.type === "text" ? "body" : "buffer"]: normalizedBody.body,
[awsBody.type === "text" ? "body" : "buffer"]: awsBody.body,
};
};

Expand Down
37 changes: 16 additions & 21 deletions test/presets/aws-lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ describe("nitro:preset:aws-lambda", async () => {
body: body || "",
};
const res = await handler(event);
return {
data: destr(res.body),
status: res.statusCode,
headers: res.headers,
cookies: res.cookies,
};
return makeResponse(res);
};
});
// Lambda v2 paylod
Expand Down Expand Up @@ -62,21 +57,21 @@ describe("nitro:preset:aws-lambda", async () => {
body: body || "",
};
const res = await handler(event);
const resHeaders = { ...res.headers };
if (res.cookies) {
if (!resHeaders["set-cookie"]) {
resHeaders["set-cookie"] = [];
}
if (!Array.isArray(resHeaders["set-cookie"])) {
resHeaders["set-cookie"] = [resHeaders["set-cookie"]];
}
resHeaders["set-cookie"].push(...res.cookies);
}
return {
data: destr(res.body),
status: res.statusCode,
headers: resHeaders,
};
return makeResponse(res);
};
});
});

const makeResponse = (response: any) => {
const headers = response.headers;

// APIgw v2 uses cookies, v1 uses multiValueHeaders
headers["set-cookie"] =
response?.cookies ?? response?.multiValueHeaders?.["set-cookie"];

return {
data: destr(response.body),
status: response.statusCode,
headers,
};
};
6 changes: 0 additions & 6 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,6 @@ export function testNitro(
"foo=bar, bar=baz, test=value; Path=/, test2=value; Path=/";
}

// Aws lambda v1
if (ctx.preset === "aws-lambda" && ctx.lambdaV1) {
expectedCookies =
"foo=bar, bar=baz,test=value; Path=/,test2=value; Path=/";
}

expect(headers["set-cookie"]).toMatchObject(expectedCookies);
});
});
Expand Down

0 comments on commit 86d05f2

Please sign in to comment.