Skip to content

Commit

Permalink
refactor: expose body type for lambda presets
Browse files Browse the repository at this point in the history
this fixes issue with stormkit text streams
  • Loading branch information
pi0 committed Aug 23, 2023
1 parent 58b4853 commit e5f095d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/runtime/entries/aws-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ export async function handler(
cookies,
statusCode: r.status,
headers: normalizeLambdaOutgoingHeaders(r.headers, true),
body: await normalizeLambdaOutgoingBody(r.body, r.headers),
body: await normalizeLambdaOutgoingBody(r.body, r.headers).then(
(r) => r.body
),
};
}

return {
statusCode: r.status,
headers: normalizeLambdaOutgoingHeaders(r.headers),
body: await normalizeLambdaOutgoingBody(r.body, r.headers),
body: await normalizeLambdaOutgoingBody(r.body, r.headers).then(
(r) => r.body
),
};
}
4 changes: 3 additions & 1 deletion src/runtime/entries/netlify-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export async function lambda(
return {
statusCode: r.status,
headers: normalizeLambdaOutgoingHeaders(r.headers, true),
body: await normalizeLambdaOutgoingBody(r.body, r.headers),
body: await normalizeLambdaOutgoingBody(r.body, r.headers).then(
(r) => r.body
),
multiValueHeaders: {
...(cookies.length > 0 ? { "set-cookie": cookies } : {}),
},
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/entries/stormkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const handler: Handler<StormkitEvent, StormkitResponse> =
return <StormkitResponse>{
statusCode: response.status,
headers: normalizeOutgoingHeaders(response.headers),
[normalizedBody === response.body ? "body" : "buffer"]: normalizedBody,
[normalizedBody.type === "text" ? "body" : "buffer"]: normalizedBody.body,
};
};

Expand Down
10 changes: 5 additions & 5 deletions src/runtime/utils.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ export function normalizeLambdaOutgoingHeaders(
export async function normalizeLambdaOutgoingBody(
body: BodyInit | ReadableStream | Buffer | Readable | Uint8Array,
headers: Record<string, number | string | string[] | undefined>
): Promise<string> {
): Promise<{ type: "text" | "binary"; body: string }> {
if (typeof body === "string") {
return body;
return { type: "text", body };
}
if (!body) {
return "";
return { type: "text", body: "" };
}
const buffer = await _toBuffer(body as any);
const contentType = (headers["content-type"] as string) || "";
return isTextType(contentType)
? buffer.toString("utf8")
: buffer.toString("base64");
? { type: "text", body: buffer.toString("utf8") }
: { type: "binary", body: buffer.toString("base64") };
}

function _toBuffer(data: ReadableStream | Readable | Uint8Array) {
Expand Down

0 comments on commit e5f095d

Please sign in to comment.