|
| 1 | +import type { Nitro } from "nitro/types"; |
| 2 | + |
| 3 | +export default function app(nitro: Nitro) { |
| 4 | + return { |
| 5 | + id: "#nitro/virtual/app", |
| 6 | + template: () => { |
| 7 | + const hasRoutes = nitro.routing.routes.hasRoutes(); |
| 8 | + const hasRouteRules = nitro.routing.routeRules.hasRoutes(); |
| 9 | + const hasRoutedMiddleware = nitro.routing.routedMiddleware.hasRoutes(); |
| 10 | + const hasGlobalMiddleware = nitro.routing.globalMiddleware.length > 0; |
| 11 | + const hasPlugins = nitro.options.plugins.length > 0; |
| 12 | + const hasHooks = nitro.options.features?.runtimeHooks ?? hasPlugins; |
| 13 | + const hasGetMiddleware = hasRouteRules || hasRoutedMiddleware; |
| 14 | + const hasAsyncContext = !!nitro.options.experimental.asyncContext; |
| 15 | + |
| 16 | + const routingImports = [ |
| 17 | + hasRoutes && "findRoute", |
| 18 | + hasRoutedMiddleware && "findRoutedMiddleware", |
| 19 | + hasGlobalMiddleware && "globalMiddleware", |
| 20 | + ].filter(Boolean); |
| 21 | + |
| 22 | + const imports: string[] = []; |
| 23 | + const code: string[] = []; |
| 24 | + |
| 25 | + imports.push( |
| 26 | + `import { H3Core } from "h3";`, |
| 27 | + `import errorHandler from "#nitro/virtual/error-handler";` |
| 28 | + ); |
| 29 | + |
| 30 | + // --- createNitroApp() --- |
| 31 | + |
| 32 | + code.push(``, `export function createNitroApp() {`); |
| 33 | + |
| 34 | + if (hasHooks) { |
| 35 | + imports.push(`import { HookableCore } from "hookable";`); |
| 36 | + code.push(` const hooks = new HookableCore();`); |
| 37 | + } |
| 38 | + |
| 39 | + code.push(``, `const captureError = (error, errorCtx) => {`); |
| 40 | + if (hasHooks) { |
| 41 | + code.push( |
| 42 | + ` const promise = hooks.callHook("error", error, errorCtx)?.catch?.((hookError) => {`, |
| 43 | + ` console.error("Error while capturing another error", hookError);`, |
| 44 | + ` });` |
| 45 | + ); |
| 46 | + } |
| 47 | + code.push( |
| 48 | + ` if (errorCtx?.event) {`, |
| 49 | + ` const errors = errorCtx.event.req.context?.nitro?.errors;`, |
| 50 | + ` if (errors) {`, |
| 51 | + ` errors.push({ error, context: errorCtx });`, |
| 52 | + ` }` |
| 53 | + ); |
| 54 | + if (hasHooks) { |
| 55 | + code.push( |
| 56 | + ` if (promise && typeof errorCtx.event.req.waitUntil === "function") {`, |
| 57 | + ` errorCtx.event.req.waitUntil(promise);`, |
| 58 | + ` }` |
| 59 | + ); |
| 60 | + } |
| 61 | + code.push(` }`, ` };`); |
| 62 | + |
| 63 | + code.push(``, ` const h3App = createH3App({`, ` onError(error, event) {`); |
| 64 | + if (hasHooks) { |
| 65 | + code.push(` captureError(error, { event });`); |
| 66 | + } |
| 67 | + code.push(` return errorHandler(error, event);`, ` },`, ` });`); |
| 68 | + |
| 69 | + if (hasHooks) { |
| 70 | + code.push( |
| 71 | + ``, |
| 72 | + ` h3App.config.onRequest = (event) => {`, |
| 73 | + ` return hooks.callHook("request", event)?.catch?.((error) => {`, |
| 74 | + ` captureError(error, { event, tags: ["request"] });`, |
| 75 | + ` });`, |
| 76 | + ` };`, |
| 77 | + ` h3App.config.onResponse = (res, event) => {`, |
| 78 | + ` return hooks.callHook("response", res, event)?.catch?.((error) => {`, |
| 79 | + ` captureError(error, { event, tags: ["response"] });`, |
| 80 | + ` });`, |
| 81 | + ` };` |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + code.push( |
| 86 | + ``, |
| 87 | + ` let appHandler = (req) => {`, |
| 88 | + ` req.context ||= {};`, |
| 89 | + ` req.context.nitro = req.context.nitro || { errors: [] };`, |
| 90 | + ` return h3App.fetch(req);`, |
| 91 | + ` };` |
| 92 | + ); |
| 93 | + |
| 94 | + if (hasAsyncContext) { |
| 95 | + imports.push(`import { nitroAsyncContext } from "#nitro/runtime/context";`); |
| 96 | + code.push( |
| 97 | + ``, |
| 98 | + ` const originalHandler = appHandler;`, |
| 99 | + ` appHandler = (req) => {`, |
| 100 | + ` return nitroAsyncContext.callAsync({ request: req }, () => originalHandler(req));`, |
| 101 | + ` };` |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + code.push( |
| 106 | + ``, |
| 107 | + ` return {`, |
| 108 | + ` fetch: appHandler,`, |
| 109 | + ` h3: h3App,`, |
| 110 | + ` hooks: ${hasHooks ? "hooks" : "undefined"},`, |
| 111 | + ` captureError,`, |
| 112 | + ` };`, |
| 113 | + `}` |
| 114 | + ); |
| 115 | + |
| 116 | + // --- initNitroPlugins() --- |
| 117 | + |
| 118 | + code.push(``, `export function initNitroPlugins(app) {`); |
| 119 | + if (hasPlugins) { |
| 120 | + imports.push(`import { plugins } from "#nitro/virtual/plugins";`); |
| 121 | + code.push( |
| 122 | + ` for (const plugin of plugins) {`, |
| 123 | + ` try {`, |
| 124 | + ` plugin(app);`, |
| 125 | + ` } catch (error) {`, |
| 126 | + ` app.captureError?.(error, { tags: ["plugin"] });`, |
| 127 | + ` throw error;`, |
| 128 | + ` }`, |
| 129 | + ` }` |
| 130 | + ); |
| 131 | + } |
| 132 | + code.push(` return app;`, `}`); |
| 133 | + |
| 134 | + // --- createH3App() --- |
| 135 | + |
| 136 | + if (routingImports.length) { |
| 137 | + imports.push(`import { ${routingImports.join(", ")} } from "#nitro/virtual/routing";`); |
| 138 | + } |
| 139 | + |
| 140 | + code.push(``, `function createH3App(config) {`, ` const h3App = new H3Core(config);`); |
| 141 | + if (hasRoutes) { |
| 142 | + code.push( |
| 143 | + ` h3App["~findRoute"] = (event) => findRoute(event.req.method, event.url.pathname);` |
| 144 | + ); |
| 145 | + } |
| 146 | + if (hasGlobalMiddleware) { |
| 147 | + code.push(` h3App["~middleware"].push(...globalMiddleware);`); |
| 148 | + } |
| 149 | + if (hasGetMiddleware) { |
| 150 | + code.push( |
| 151 | + ` h3App["~getMiddleware"] = (event, route) => {`, |
| 152 | + ` const pathname = event.url.pathname;`, |
| 153 | + ` const method = event.req.method;`, |
| 154 | + ` const middleware = [];` |
| 155 | + ); |
| 156 | + if (hasRouteRules) { |
| 157 | + imports.push(`import { getRouteRules } from "#nitro/runtime/app";`); |
| 158 | + code.push( |
| 159 | + ` const routeRules = getRouteRules(method, pathname);`, |
| 160 | + ` event.context.routeRules = routeRules?.routeRules;`, |
| 161 | + ` if (routeRules?.routeRuleMiddleware.length) {`, |
| 162 | + ` middleware.push(...routeRules.routeRuleMiddleware);`, |
| 163 | + ` }` |
| 164 | + ); |
| 165 | + } |
| 166 | + if (hasGlobalMiddleware) { |
| 167 | + code.push(` middleware.push(...h3App["~middleware"]);`); |
| 168 | + } |
| 169 | + if (hasRoutedMiddleware) { |
| 170 | + code.push( |
| 171 | + ` middleware.push(...findRoutedMiddleware(method, pathname).map((r) => r.data));` |
| 172 | + ); |
| 173 | + } |
| 174 | + if (hasRoutes) { |
| 175 | + code.push( |
| 176 | + ` if (route?.data?.middleware?.length) {`, |
| 177 | + ` middleware.push(...route.data.middleware);`, |
| 178 | + ` }` |
| 179 | + ); |
| 180 | + } |
| 181 | + code.push(` return middleware;`, ` };`); |
| 182 | + } |
| 183 | + code.push(` return h3App;`, `}`); |
| 184 | + |
| 185 | + return [...imports, ...code].join("\n"); |
| 186 | + }, |
| 187 | + }; |
| 188 | +} |
0 commit comments