Skip to content

Commit e5f095d

Browse files
committed
refactor: expose body type for lambda presets
this fixes issue with stormkit text streams
1 parent 58b4853 commit e5f095d

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/runtime/entries/aws-lambda.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ export async function handler(
6363
cookies,
6464
statusCode: r.status,
6565
headers: normalizeLambdaOutgoingHeaders(r.headers, true),
66-
body: await normalizeLambdaOutgoingBody(r.body, r.headers),
66+
body: await normalizeLambdaOutgoingBody(r.body, r.headers).then(
67+
(r) => r.body
68+
),
6769
};
6870
}
6971

7072
return {
7173
statusCode: r.status,
7274
headers: normalizeLambdaOutgoingHeaders(r.headers),
73-
body: await normalizeLambdaOutgoingBody(r.body, r.headers),
75+
body: await normalizeLambdaOutgoingBody(r.body, r.headers).then(
76+
(r) => r.body
77+
),
7478
};
7579
}

src/runtime/entries/netlify-lambda.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export async function lambda(
4040
return {
4141
statusCode: r.status,
4242
headers: normalizeLambdaOutgoingHeaders(r.headers, true),
43-
body: await normalizeLambdaOutgoingBody(r.body, r.headers),
43+
body: await normalizeLambdaOutgoingBody(r.body, r.headers).then(
44+
(r) => r.body
45+
),
4446
multiValueHeaders: {
4547
...(cookies.length > 0 ? { "set-cookie": cookies } : {}),
4648
},

src/runtime/entries/stormkit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const handler: Handler<StormkitEvent, StormkitResponse> =
4242
return <StormkitResponse>{
4343
statusCode: response.status,
4444
headers: normalizeOutgoingHeaders(response.headers),
45-
[normalizedBody === response.body ? "body" : "buffer"]: normalizedBody,
45+
[normalizedBody.type === "text" ? "body" : "buffer"]: normalizedBody.body,
4646
};
4747
};
4848

src/runtime/utils.lambda.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ export function normalizeLambdaOutgoingHeaders(
3131
export async function normalizeLambdaOutgoingBody(
3232
body: BodyInit | ReadableStream | Buffer | Readable | Uint8Array,
3333
headers: Record<string, number | string | string[] | undefined>
34-
): Promise<string> {
34+
): Promise<{ type: "text" | "binary"; body: string }> {
3535
if (typeof body === "string") {
36-
return body;
36+
return { type: "text", body };
3737
}
3838
if (!body) {
39-
return "";
39+
return { type: "text", body: "" };
4040
}
4141
const buffer = await _toBuffer(body as any);
4242
const contentType = (headers["content-type"] as string) || "";
4343
return isTextType(contentType)
44-
? buffer.toString("utf8")
45-
: buffer.toString("base64");
44+
? { type: "text", body: buffer.toString("utf8") }
45+
: { type: "binary", body: buffer.toString("base64") };
4646
}
4747

4848
function _toBuffer(data: ReadableStream | Readable | Uint8Array) {

0 commit comments

Comments
 (0)