From e5f095d64eed7e8b8557ebde74e31bf7b0b9c633 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 23 Aug 2023 05:43:49 +0200 Subject: [PATCH] refactor: expose body type for lambda presets this fixes issue with stormkit text streams --- src/runtime/entries/aws-lambda.ts | 8 ++++++-- src/runtime/entries/netlify-lambda.ts | 4 +++- src/runtime/entries/stormkit.ts | 2 +- src/runtime/utils.lambda.ts | 10 +++++----- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/runtime/entries/aws-lambda.ts b/src/runtime/entries/aws-lambda.ts index 059826a3a4..cf28282508 100644 --- a/src/runtime/entries/aws-lambda.ts +++ b/src/runtime/entries/aws-lambda.ts @@ -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 + ), }; } diff --git a/src/runtime/entries/netlify-lambda.ts b/src/runtime/entries/netlify-lambda.ts index b02ed64947..d67042df6c 100644 --- a/src/runtime/entries/netlify-lambda.ts +++ b/src/runtime/entries/netlify-lambda.ts @@ -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 } : {}), }, diff --git a/src/runtime/entries/stormkit.ts b/src/runtime/entries/stormkit.ts index ebdf4db8c3..3b17a0eeff 100644 --- a/src/runtime/entries/stormkit.ts +++ b/src/runtime/entries/stormkit.ts @@ -42,7 +42,7 @@ export const handler: Handler = return { statusCode: response.status, headers: normalizeOutgoingHeaders(response.headers), - [normalizedBody === response.body ? "body" : "buffer"]: normalizedBody, + [normalizedBody.type === "text" ? "body" : "buffer"]: normalizedBody.body, }; }; diff --git a/src/runtime/utils.lambda.ts b/src/runtime/utils.lambda.ts index faafae5b01..74e2ac4083 100644 --- a/src/runtime/utils.lambda.ts +++ b/src/runtime/utils.lambda.ts @@ -31,18 +31,18 @@ export function normalizeLambdaOutgoingHeaders( export async function normalizeLambdaOutgoingBody( body: BodyInit | ReadableStream | Buffer | Readable | Uint8Array, headers: Record -): Promise { +): 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) {