From 5ee3292120a59e8388ba94b8961c469cec85a39f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 22 Aug 2023 12:01:45 +0200 Subject: [PATCH] fix(stormkit): properly send buffer responses --- src/runtime/entries/stormkit.ts | 59 +++++++++++++++++---------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/runtime/entries/stormkit.ts b/src/runtime/entries/stormkit.ts index 82d91f0b0d..18c428b73f 100644 --- a/src/runtime/entries/stormkit.ts +++ b/src/runtime/entries/stormkit.ts @@ -3,7 +3,7 @@ import "#internal/nitro/virtual/polyfill"; import { nitroApp } from "../app"; import { normalizeLambdaOutgoingBody } from "#internal/nitro/utils.lambda"; -interface StormkitEvent { +type StormkitEvent = { url: string; // e.g. /my/path, /my/path?with=query path: string; method: string; @@ -11,45 +11,48 @@ interface StormkitEvent { query?: Record>; headers?: Record; rawHeaders?: Array; -} +}; -export interface StormkitResult { +type StormkitResponse = { + headers?: Record; + body?: string; + buffer?: string; statusCode: number; - headers?: { [header: string]: boolean | number | string } | undefined; - body?: string | undefined; -} + errorMessage?: string; + errorStack?: string; +}; -export const handler: Handler = async function ( - event, - context -) { - const method = event.method || "get"; +export const handler: Handler = + async function (event, context) { + const response = await nitroApp.localCall({ + event, + url: event.url, + context, + headers: event.headers, + method: event.method || "GET", + query: event.query, + body: event.body, + }); - const r = await nitroApp.localCall({ - event, - url: event.url, - context, - headers: event.headers, // TODO: Normalize headers - method, - query: event.query, - body: event.body, - }); + const normalizedBody = normalizeLambdaOutgoingBody( + response.body, + response.headers + ); - // TODO: Handle cookies with lambda v1 or v2 ? - return { - statusCode: r.status, - headers: normalizeOutgoingHeaders(r.headers), - body: normalizeLambdaOutgoingBody(r.body, r.headers), + return { + statusCode: response.status, + headers: normalizeOutgoingHeaders(response.headers), + [normalizedBody === response.body ? "body" : "buffer"]: normalizedBody, + }; }; -}; function normalizeOutgoingHeaders( headers: Record -) { +): Record { return Object.fromEntries( Object.entries(headers).map(([k, v]) => [ k, - Array.isArray(v) ? v.join(",") : v!, + Array.isArray(v) ? v.join(",") : String(v), ]) ); }