UNPKG

nitropack

Version:

Build and Deploy Universal JavaScript Servers

32 lines (31 loc) • 952 B
import "#internal/nitro/virtual/polyfill"; import { requestHasBody, useRequestBody } from "#internal/nitro/utils"; import { nitroApp } from "#internal/nitro/app"; export async function handler(request) { const url = new URL(request.url); let body; if (requestHasBody(request)) { body = await useRequestBody(request); } 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 }); 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) { return Object.entries(headers).map(([k, v]) => [ k, Array.isArray(v) ? v.join(",") : v ]); }