UNPKG

nitropack

Version:

Build and Deploy Universal JavaScript Servers

58 lines (57 loc) • 1.75 kB
import { eventHandler, sendRedirect, setHeaders, proxyRequest } from "h3"; import defu from "defu"; import { createRouter as createRadixRouter, toRouteMatcher } from "radix3"; import { joinURL, withoutBase } from "ufo"; import { useRuntimeConfig } from "./config.mjs"; const config = useRuntimeConfig(); const _routeRulesMatcher = toRouteMatcher( createRadixRouter({ routes: config.nitro.routeRules }) ); export function createRouteRulesHandler() { return eventHandler((event) => { const routeRules = getRouteRules(event); if (routeRules.headers) { setHeaders(event, routeRules.headers); } if (routeRules.redirect) { return sendRedirect( event, routeRules.redirect.to, routeRules.redirect.statusCode ); } if (routeRules.proxy) { let target = routeRules.proxy.to; if (target.endsWith("/**")) { let targetPath = event.path; const strpBase = routeRules.proxy._proxyStripBase; if (strpBase) { targetPath = withoutBase(targetPath, strpBase); } target = joinURL(target.slice(0, -3), targetPath); } return proxyRequest(event, target, { fetch: $fetch.raw, ...routeRules.proxy }); } }); } export function getRouteRules(event) { event.context._nitro = event.context._nitro || {}; if (!event.context._nitro.routeRules) { const path = new URL(event.node.req.url, "http://localhost").pathname; event.context._nitro.routeRules = getRouteRulesForPath( withoutBase(path, useRuntimeConfig().app.baseURL) ); } return event.context._nitro.routeRules; } export function getRouteRulesForPath(path) { return defu({}, ..._routeRulesMatcher.matchAll(path).reverse()); }