Skip to content

Commit

Permalink
fix(stormkit): properly send buffer responses
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 22, 2023
1 parent 6cb72e6 commit 5ee3292
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions src/runtime/entries/stormkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,56 @@ 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;
body?: string;
query?: Record<string, Array<string>>;
headers?: Record<string, string>;
rawHeaders?: Array<string>;
}
};

export interface StormkitResult {
type StormkitResponse = {
headers?: Record<string, string>;
body?: string;
buffer?: string;
statusCode: number;
headers?: { [header: string]: boolean | number | string } | undefined;
body?: string | undefined;
}
errorMessage?: string;
errorStack?: string;
};

export const handler: Handler<StormkitEvent, StormkitResult> = async function (
event,
context
) {
const method = event.method || "get";
export const handler: Handler<StormkitEvent, StormkitResponse> =
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 <StormkitResponse>{
statusCode: response.status,
headers: normalizeOutgoingHeaders(response.headers),
[normalizedBody === response.body ? "body" : "buffer"]: normalizedBody,
};
};
};

function normalizeOutgoingHeaders(
headers: Record<string, number | string | string[] | undefined>
) {
): Record<string, string> {
return Object.fromEntries(
Object.entries(headers).map(([k, v]) => [
k,
Array.isArray(v) ? v.join(",") : v!,
Array.isArray(v) ? v.join(",") : String(v),
])
);
}

0 comments on commit 5ee3292

Please sign in to comment.