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 4 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
6 changes: 6 additions & 0 deletions 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 All @@ -30,6 +31,7 @@ export interface NitroApp {
hooks: Hookable<NitroRuntimeHooks>;
localCall: ReturnType<typeof createCall>;
localFetch: ReturnType<typeof createLocalFetch>;
localFetchWithNormalizedHeaders: ReturnType<typeof createLocalFetch>;
}

function createNitroApp(): NitroApp {
Expand All @@ -49,6 +51,9 @@ function createNitroApp(): NitroApp {
// Create local fetch callers
const localCall = createCall(toNodeListener(h3App) as any);
const localFetch = createLocalFetch(localCall, globalThis.fetch);
const localFetchWithNormalizedHeaders = withNormalizedHeaders(
createLocalFetch(localCall, globalThis.fetch)
);
const $fetch = createFetch({
fetch: localFetch,
Headers,
Expand Down Expand Up @@ -120,6 +125,7 @@ function createNitroApp(): NitroApp {
router,
localCall,
localFetch,
localFetchWithNormalizedHeaders,
Hebilicious marked this conversation as resolved.
Show resolved Hide resolved
};

for (const plugin of plugins) {
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.localFetchWithNormalizedHeaders(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: request.headers,
method: request.method,
redirect: request.redirect,
body,
});

return response;
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/entries/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default {
// Expose latest env to the global context
globalThis.__env__ = env;

return nitroApp.localFetch(url.pathname + url.search, {
return nitroApp.localFetchWithNormalizedHeaders(url.pathname + url.search, {
context: {
cf: (request as any).cf,
waitUntil: (promise) => context.waitUntil(promise),
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/entries/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default {
// Expose latest env to the global context
globalThis.__env__ = env;

return nitroApp.localFetch(url.pathname + url.search, {
return nitroApp.localFetchWithNormalizedHeaders(url.pathname + url.search, {
context: {
cf: request.cf,
waitUntil: (promise) => context.waitUntil(promise),
Expand Down
23 changes: 4 additions & 19 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.localFetchWithNormalizedHeaders(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()),
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.localFetchWithNormalizedHeaders(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,
]);
}
37 changes: 12 additions & 25 deletions src/runtime/entries/deno-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,20 @@ async function handler(request: Request) {
body = await request.arrayBuffer();
}

const r = await nitroApp.localCall({
url: url.pathname + url.search,
host: url.hostname,
protocol: url.protocol,
headers: Object.fromEntries(request.headers.entries()),
method: request.method,
redirect: request.redirect,
body,
});
const r = await nitroApp.localFetchWithNormalizedHeaders(
url.pathname + url.search,
{
host: url.hostname,
protocol: url.protocol,
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.localFetchWithNormalizedHeaders(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.localFetchWithNormalizedHeaders(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.localFetchWithNormalizedHeaders(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.localFetchWithNormalizedHeaders(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,
]);
}
Loading