Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split cookie headers #1452

Merged
merged 21 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createHooks, Hookable } from "hookable";
import type { NitroRuntimeHooks } from "./types";
import { useRuntimeConfig } from "./config";
import { cachedEventHandler } from "./cache";
import { withNormalizedHeaders } from "./utils";
import { createRouteRulesHandler, getRouteRulesForPath } from "./route-rules";
import type { $Fetch, NitroFetchRequest } from "nitropack";
import { plugins } from "#internal/nitro/virtual/plugins";
Expand Down Expand Up @@ -48,7 +49,9 @@ function createNitroApp(): NitroApp {

// Create local fetch callers
const localCall = createCall(toNodeListener(h3App) as any);
const localFetch = createLocalFetch(localCall, globalThis.fetch);
const localFetch = withNormalizedHeaders(
createLocalFetch(localCall, globalThis.fetch)
);
const $fetch = createFetch({
fetch: localFetch,
Headers,
Expand Down
1 change: 1 addition & 0 deletions src/runtime/entries/azure-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function handle(context, req) {
body: req.rawBody,
});

// @todo cookies https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=typescript%2Cwindows%2Cazure-cli&pivots=nodejs-model-v4#http-response
context.res = {
status,
headers,
Expand Down
1 change: 1 addition & 0 deletions src/runtime/entries/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function handle(context, req) {
body: req.rawBody,
});

// @todo cookies https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=typescript%2Cwindows%2Cazure-cli&pivots=nodejs-model-v4#http-response
context.res = {
status,
headers,
Expand Down
5 changes: 1 addition & 4 deletions src/runtime/entries/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ const server = Bun.serve({
body = await request.arrayBuffer();
}

const response = await nitroApp.localFetch(url.pathname + url.search, {
url: url.pathname + url.search,
return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: request.headers,
method: request.method,
redirect: request.redirect,
body,
});

return response;
},
});

Expand Down
25 changes: 5 additions & 20 deletions src/runtime/entries/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,22 @@ async function handleEvent(event: FetchEvent) {
body = Buffer.from(await event.request.arrayBuffer());
}

const r = await nitroApp.localCall({
event,
return nitroApp.localFetch(url.pathname + url.search, {
context: {
// https://developers.cloudflare.com/workers//runtime-apis/request#incomingrequestcfproperties
cf: (event.request as any).cf,
waitUntil: (promise) => event.waitUntil(promise),
cloudflare: {
event,
},
},
url: url.pathname + url.search,
host: url.hostname,
protocol: url.protocol,
headers: Object.fromEntries(event.request.headers.entries()),
headers: event.request.headers,
method: event.request.method,
redirect: event.request.redirect,
body,
});

return new Response(r.body, {
// @ts-ignore TODO: Should be HeadersInit instead of string[][]
headers: normalizeOutgoingHeaders(r.headers),
status: r.status,
statusText: r.statusText,
});
}

function assetsCacheControl(_request) {
Expand All @@ -69,12 +63,3 @@ const baseURLModifier = (request: Request) => {
const url = withoutBase(request.url, useRuntimeConfig().app.baseURL);
return mapRequestToAsset(new Request(url, request));
};

function normalizeOutgoingHeaders(
headers: Record<string, string | string[] | undefined>
) {
return Object.entries(headers).map(([k, v]) => [
k,
Array.isArray(v) ? v.join(",") : v,
]);
}
21 changes: 2 additions & 19 deletions src/runtime/entries/deno-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,12 @@ async function handleRequest(request: Request) {
body = await request.arrayBuffer();
}

const r = await nitroApp.localCall({
url: url.pathname + url.search,
return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: Object.fromEntries(request.headers.entries()),
headers: request.headers,
method: request.method,
redirect: request.redirect,
body,
});

return new Response(r.body || undefined, {
// @ts-ignore TODO: Should be HeadersInit instead of string[][]
headers: normalizeOutgoingHeaders(r.headers),
status: r.status,
statusText: r.statusText,
});
}

function normalizeOutgoingHeaders(
headers: Record<string, string | string[] | undefined>
) {
return Object.entries(headers).map(([k, v]) => [
k,
Array.isArray(v) ? v.join(",") : v,
]);
}
22 changes: 3 additions & 19 deletions src/runtime/entries/deno-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,17 @@ async function handler(request: Request) {
body = await request.arrayBuffer();
}

const r = await nitroApp.localCall({
url: url.pathname + url.search,
const r = await nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: Object.fromEntries(request.headers.entries()),
headers: request.headers,
method: request.method,
redirect: request.redirect,
body,
});

// TODO: fix in runtime/static
const responseBody = r.status === 304 ? null : r.body;
return new Response(responseBody, {
// @ts-ignore TODO: Should be HeadersInit instead of string[][]
headers: normalizeOutgoingHeaders(r.headers),
status: r.status,
statusText: r.statusText,
});
}

function normalizeOutgoingHeaders(
headers: Record<string, string | string[] | undefined>
) {
return Object.entries(headers).map(([k, v]) => [
k,
Array.isArray(v) ? v.join(",") : v,
]);
return new Response(responseBody, r);
}

export default {};
23 changes: 3 additions & 20 deletions src/runtime/entries/lagon.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
import "#internal/nitro/virtual/polyfill";
import { nitroApp } from "#internal/nitro/app";

export async function handler(request: Request): Promise<Response> {
export async function handler(request: Request) {
const url = new URL(request.url);

let body;
if (request.body) {
body = await request.arrayBuffer();
}

const r = await nitroApp.localCall({
url: url.pathname + url.search,
return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: Object.fromEntries(request.headers.entries()),
headers: request.headers,
method: request.method,
redirect: request.redirect,
body,
});

return new Response(r.body, {
// @ts-ignore TODO: Should be HeadersInit instead of string[][]
headers: normalizeOutgoingHeaders(r.headers),
status: r.status,
statusText: r.statusText,
});
}

function normalizeOutgoingHeaders(
headers: Record<string, string | string[] | undefined>
) {
return Object.entries(headers).map(([k, v]) => [
k,
Array.isArray(v) ? v.join(",") : v,
]);
}
10 changes: 1 addition & 9 deletions src/runtime/entries/netlify-edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@ export default async function (request: Request, _context) {
body = await request.arrayBuffer();
}

const r = await nitroApp.localCall({
url: url.pathname + url.search,
return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
// @ts-ignore TODO
headers: request.headers,
method: request.method,
redirect: request.redirect,
body,
});

return new Response(r.body, {
headers: r.headers as HeadersInit,
status: r.status,
statusText: r.statusText,
});
}
2 changes: 1 addition & 1 deletion src/runtime/entries/netlify-lambda.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "#internal/nitro/virtual/polyfill";
import type {
Handler,
HandlerResponse,
HandlerContext,
HandlerEvent,
Expand Down Expand Up @@ -30,6 +29,7 @@ export async function lambda(
body: event.body, // TODO: handle event.isBase64Encoded
});

// @todo Handle cookies
return {
statusCode: r.status,
headers: normalizeOutgoingHeaders(r.headers),
Expand Down
12 changes: 2 additions & 10 deletions src/runtime/entries/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "#internal/nitro/virtual/polyfill";
import { nitroApp } from "../app";
import { isPublicAssetURL } from "#internal/nitro/virtual/public-assets";

addEventListener("fetch", (event: any) => {
addEventListener("fetch", (event: FetchEvent) => {
const url = new URL(event.request.url);
if (isPublicAssetURL(url.pathname) || url.pathname.includes("/_server/")) {
return;
Expand All @@ -17,22 +17,14 @@ async function handleEvent(url, event) {
body = await event.request.arrayBuffer();
}

const r = await nitroApp.localCall({
event,
url: url.pathname + url.search,
return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: event.request.headers,
method: event.request.method,
redirect: event.request.redirect,
body,
});

return new Response(r.body, {
headers: r.headers as HeadersInit,
status: r.status,
statusText: r.statusText,
});
}

declare const self: ServiceWorkerGlobalScope;
Expand Down
1 change: 1 addition & 0 deletions src/runtime/entries/stormkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const handler: Handler<StormkitEvent, StormkitResult> = async function (
body: event.body,
});

// @todo handle cookies
Hebilicious marked this conversation as resolved.
Show resolved Hide resolved
return {
statusCode: r.status,
headers: normalizeOutgoingHeaders(r.headers),
Expand Down
24 changes: 3 additions & 21 deletions src/runtime/entries/vercel-edge.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
import "#internal/nitro/virtual/polyfill";
import { nitroApp } from "#internal/nitro/app";

export default async function handleEvent(request, event) {
export default async function handleEvent(request: Request) {
const url = new URL(request.url);

let body;
if (request.body) {
body = await request.arrayBuffer();
}

const r = await nitroApp.localCall({
event,
url: url.pathname + url.search,
return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: Object.fromEntries(request.headers.entries()),
headers: request.headers,
method: request.method,
body,
});

return new Response(r.body, {
// @ts-ignore TODO: Should be HeadersInit instead of string[][]
headers: normalizeOutgoingHeaders(r.headers),
status: r.status,
statusText: r.statusText,
});
}

function normalizeOutgoingHeaders(
headers: Record<string, string | string[] | undefined>
) {
return Object.entries(headers).map(([k, v]) => [
k,
Array.isArray(v) ? v.join(",") : v,
]);
}
36 changes: 35 additions & 1 deletion src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { H3Event } from "h3";
import { getRequestHeader } from "h3";
import { getRequestHeader, splitCookiesString } from "h3";
import type { NitroApp } from "./app";

const METHOD_WITH_BODY_RE = /post|put|patch/i;
const TEXT_MIME_RE = /application\/text|text\/html/;
const JSON_MIME_RE = /application\/json/;
Expand Down Expand Up @@ -100,3 +102,35 @@ export function trapUnhandledNodeErrors() {
);
}
}

const joinIfArray = (value: string | string[]) =>
Array.isArray(value) ? value.join(",") : value;

export function normalizeOutgoingHeaders(
headers: Awaited<ReturnType<NitroApp["localCall"]>>["headers"] | Headers
) {
const outgoingHeaders = new Headers();
const iterableHeaders =
headers instanceof Headers ? headers : Object.entries(headers);
for (const [name, header] of iterableHeaders) {
if (name === "set-cookie") {
for (const cookie of splitCookiesString(joinIfArray(header))) {
outgoingHeaders.append("set-cookie", cookie);
}
} else {
outgoingHeaders.set(name, joinIfArray(header));
}
}
return outgoingHeaders;
}

export function withNormalizedHeaders(fetch: typeof globalThis.fetch) {
Hebilicious marked this conversation as resolved.
Show resolved Hide resolved
return async (...args: Parameters<typeof fetch>) => {
const r = await fetch(...args);
return new Response(r.body, {
headers: normalizeOutgoingHeaders(r.headers),
status: r.status,
statusText: r.statusText,
});
};
}