nitro 2.2.27 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -2
- package/README.md +11 -259
- package/dist/_chunks/app.mjs +19797 -0
- package/dist/_chunks/build.mjs +84 -0
- package/dist/_chunks/build2.mjs +318 -0
- package/dist/_chunks/build3.mjs +6452 -0
- package/dist/_chunks/detect-acorn.mjs +503 -0
- package/dist/_chunks/index.mjs +22242 -0
- package/dist/_chunks/index2.mjs +297 -0
- package/dist/_chunks/index3.mjs +1058 -0
- package/dist/_chunks/index4.mjs +1206 -0
- package/dist/_chunks/info.mjs +11157 -0
- package/dist/_chunks/json5.mjs +68 -0
- package/dist/_chunks/jsonc.mjs +51 -0
- package/dist/_chunks/pathe.M-eThtNZ.mjs +204 -0
- package/dist/_chunks/plugin.mjs +960 -0
- package/dist/_chunks/server.mjs +254 -0
- package/dist/_chunks/snapshot.mjs +284 -0
- package/dist/_chunks/toml.mjs +259 -0
- package/dist/_chunks/yaml.mjs +86 -0
- package/dist/cli/build.mjs +54 -0
- package/dist/cli/common.mjs +13 -0
- package/dist/cli/dev.mjs +95 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.mjs +477 -0
- package/dist/cli/index2.mjs +17 -0
- package/dist/cli/list.mjs +34 -0
- package/dist/cli/prepare.mjs +24 -0
- package/dist/cli/run.mjs +60 -0
- package/dist/index.d.mts +52 -0
- package/dist/index.mjs +55 -0
- package/dist/node_modules/@poppinss/colors/build/index.js +169 -0
- package/dist/node_modules/@poppinss/colors/package.json +116 -0
- package/dist/node_modules/@poppinss/dumper/build/chunk-26HALFTP.js +602 -0
- package/dist/node_modules/@poppinss/dumper/build/formatters/console/main.js +441 -0
- package/dist/node_modules/@poppinss/dumper/build/formatters/html/main.js +717 -0
- package/dist/node_modules/@poppinss/dumper/package.json +122 -0
- package/dist/node_modules/@poppinss/exception/build/index.js +63 -0
- package/dist/node_modules/@poppinss/exception/package.json +106 -0
- package/dist/node_modules/@sindresorhus/is/distribution/index.js +1313 -0
- package/dist/node_modules/@sindresorhus/is/distribution/utilities.js +3 -0
- package/dist/node_modules/@sindresorhus/is/package.json +81 -0
- package/dist/node_modules/@speed-highlight/core/dist/index.js +4 -0
- package/dist/node_modules/@speed-highlight/core/dist/terminal.js +2 -0
- package/dist/node_modules/@speed-highlight/core/package.json +90 -0
- package/dist/node_modules/cookie/dist/index.js +239 -0
- package/dist/node_modules/cookie/package.json +42 -0
- package/dist/node_modules/croner/dist/croner.js +1 -0
- package/dist/node_modules/croner/package.json +63 -0
- package/dist/node_modules/defu/dist/defu.mjs +69 -0
- package/dist/node_modules/defu/package.json +43 -0
- package/dist/node_modules/destr/dist/index.mjs +72 -0
- package/dist/node_modules/destr/package.json +47 -0
- package/dist/node_modules/error-stack-parser-es/dist/index.mjs +38 -0
- package/dist/node_modules/error-stack-parser-es/dist/lite.mjs +167 -0
- package/dist/node_modules/error-stack-parser-es/package.json +83 -0
- package/dist/node_modules/get-port-please/dist/index.mjs +430 -0
- package/dist/node_modules/get-port-please/package.json +39 -0
- package/dist/node_modules/hookable/dist/index.mjs +290 -0
- package/dist/node_modules/hookable/package.json +49 -0
- package/dist/node_modules/kleur/index.js +110 -0
- package/dist/node_modules/kleur/index.mjs +110 -0
- package/dist/node_modules/kleur/package.json +51 -0
- package/dist/node_modules/klona/dist/index.mjs +81 -0
- package/dist/node_modules/klona/full/index.mjs +53 -0
- package/dist/node_modules/klona/package.json +74 -0
- package/dist/node_modules/scule/dist/index.mjs +80 -0
- package/dist/node_modules/scule/package.json +43 -0
- package/dist/node_modules/source-map/lib/array-set.js +100 -0
- package/dist/node_modules/source-map/lib/base64-vlq.js +94 -0
- package/dist/node_modules/source-map/lib/base64.js +19 -0
- package/dist/node_modules/source-map/lib/binary-search.js +113 -0
- package/dist/node_modules/source-map/lib/mapping-list.js +83 -0
- package/dist/node_modules/source-map/lib/mappings.wasm +0 -0
- package/dist/node_modules/source-map/lib/read-wasm.js +27 -0
- package/dist/node_modules/source-map/lib/source-map-consumer.js +1081 -0
- package/dist/node_modules/source-map/lib/source-map-generator.js +439 -0
- package/dist/node_modules/source-map/lib/source-node.js +430 -0
- package/dist/node_modules/source-map/lib/url.js +13 -0
- package/dist/node_modules/source-map/lib/util.js +444 -0
- package/dist/node_modules/source-map/lib/wasm.js +138 -0
- package/dist/node_modules/source-map/package.json +79 -0
- package/dist/node_modules/source-map/source-map.js +10 -0
- package/dist/node_modules/std-env/dist/index.mjs +1 -0
- package/dist/node_modules/std-env/package.json +46 -0
- package/dist/node_modules/supports-color/index.js +202 -0
- package/dist/node_modules/supports-color/package.json +64 -0
- package/dist/node_modules/ufo/dist/index.mjs +638 -0
- package/dist/node_modules/ufo/package.json +47 -0
- package/dist/node_modules/unctx/dist/index.mjs +131 -0
- package/dist/node_modules/unctx/package.json +67 -0
- package/dist/node_modules/youch/build/chunk-4L7RY2JA.js +42 -0
- package/dist/node_modules/youch/build/chunk-4XB2BYKC.js +85 -0
- package/dist/node_modules/youch/build/chunk-4YEN7HVQ.js +61 -0
- package/dist/node_modules/youch/build/chunk-EUJBVOYB.js +83 -0
- package/dist/node_modules/youch/build/chunk-HFSXRSKS.js +85 -0
- package/dist/node_modules/youch/build/chunk-JAN2TFI2.js +222 -0
- package/dist/node_modules/youch/build/chunk-OSUFJZHZ.js +6 -0
- package/dist/node_modules/youch/build/chunk-PINJDICN.js +72 -0
- package/dist/node_modules/youch/build/chunk-PUHGL6HA.js +41 -0
- package/dist/node_modules/youch/build/chunk-VE4LENUR.js +45 -0
- package/dist/node_modules/youch/build/index.js +352 -0
- package/dist/node_modules/youch/build/public/error_cause/style.css +5 -0
- package/dist/node_modules/youch/build/public/error_info/script.js +13 -0
- package/dist/node_modules/youch/build/public/error_info/style.css +144 -0
- package/dist/node_modules/youch/build/public/error_stack/script.js +74 -0
- package/dist/node_modules/youch/build/public/error_stack/style.css +220 -0
- package/dist/node_modules/youch/build/public/error_stack_source/style.css +123 -0
- package/dist/node_modules/youch/build/public/header/script.js +16 -0
- package/dist/node_modules/youch/build/public/header/style.css +56 -0
- package/dist/node_modules/youch/build/public/layout/script.js +20 -0
- package/dist/node_modules/youch/build/public/layout/style.css +431 -0
- package/dist/node_modules/youch/package.json +133 -0
- package/dist/node_modules/youch-core/build/index.js +316 -0
- package/dist/node_modules/youch-core/package.json +124 -0
- package/dist/presets/_nitro/runtime/nitro-dev.d.mts +1 -0
- package/dist/presets/_nitro/runtime/nitro-dev.mjs +62 -0
- package/dist/presets/_nitro/runtime/nitro-prerenderer.d.mts +3 -0
- package/dist/presets/_nitro/runtime/nitro-prerenderer.mjs +18 -0
- package/dist/presets/_nitro/runtime/service-worker.d.mts +1 -0
- package/dist/presets/_nitro/runtime/service-worker.mjs +21 -0
- package/dist/presets/aws-amplify/runtime/aws-amplify.d.mts +1 -0
- package/dist/presets/aws-amplify/runtime/aws-amplify.mjs +13 -0
- package/dist/presets/aws-lambda/runtime/_utils.d.mts +18 -0
- package/dist/presets/aws-lambda/runtime/_utils.mjs +102 -0
- package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.d.mts +3 -0
- package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.mjs +33 -0
- package/dist/presets/aws-lambda/runtime/aws-lambda.d.mts +3 -0
- package/dist/presets/aws-lambda/runtime/aws-lambda.mjs +13 -0
- package/dist/presets/azure/runtime/_utils.d.mts +2 -0
- package/dist/presets/azure/runtime/_utils.mjs +50 -0
- package/dist/presets/azure/runtime/azure-swa.d.mts +5 -0
- package/dist/presets/azure/runtime/azure-swa.mjs +28 -0
- package/dist/presets/bun/runtime/bun.d.mts +1 -0
- package/dist/presets/bun/runtime/bun.mjs +27 -0
- package/dist/presets/cloudflare/runtime/_module-handler.d.mts +13 -0
- package/dist/presets/cloudflare/runtime/_module-handler.mjs +93 -0
- package/dist/presets/cloudflare/runtime/cloudflare-durable.d.mts +19 -0
- package/dist/presets/cloudflare/runtime/cloudflare-durable.mjs +74 -0
- package/dist/presets/cloudflare/runtime/cloudflare-module.d.mts +9 -0
- package/dist/presets/cloudflare/runtime/cloudflare-module.mjs +20 -0
- package/dist/presets/cloudflare/runtime/cloudflare-pages.d.mts +20 -0
- package/dist/presets/cloudflare/runtime/cloudflare-pages.mjs +47 -0
- package/dist/presets/cloudflare/runtime/plugin.dev.d.mts +3 -0
- package/dist/presets/cloudflare/runtime/plugin.dev.mjs +89 -0
- package/dist/presets/deno/runtime/deno-deploy.d.mts +1 -0
- package/dist/presets/deno/runtime/deno-deploy.mjs +17 -0
- package/dist/presets/deno/runtime/deno-server.d.mts +6 -0
- package/dist/presets/deno/runtime/deno-server.mjs +59 -0
- package/dist/presets/netlify/runtime/netlify-edge.d.mts +3 -0
- package/dist/presets/netlify/runtime/netlify-edge.mjs +17 -0
- package/dist/presets/netlify/runtime/netlify.d.mts +3 -0
- package/dist/presets/netlify/runtime/netlify.mjs +24 -0
- package/dist/presets/node/runtime/node-cluster.d.mts +1 -0
- package/dist/presets/node/runtime/node-cluster.mjs +58 -0
- package/dist/presets/node/runtime/node-middleware.d.mts +4 -0
- package/dist/presets/node/runtime/node-middleware.mjs +14 -0
- package/dist/presets/node/runtime/node-server.d.mts +3 -0
- package/dist/presets/node/runtime/node-server.mjs +44 -0
- package/dist/presets/standard/runtime/server.d.mts +5 -0
- package/dist/presets/standard/runtime/server.mjs +6 -0
- package/dist/presets/stormkit/runtime/stormkit.d.mts +21 -0
- package/dist/presets/stormkit/runtime/stormkit.mjs +28 -0
- package/dist/presets/vercel/runtime/vercel.d.mts +8 -0
- package/dist/presets/vercel/runtime/vercel.mjs +17 -0
- package/dist/presets/winterjs/runtime/winterjs.d.mts +1 -0
- package/dist/presets/winterjs/runtime/winterjs.mjs +87 -0
- package/dist/presets/zeabur/runtime/zeabur.d.mts +3 -0
- package/dist/presets/zeabur/runtime/zeabur.mjs +4 -0
- package/dist/presets.mjs +2460 -0
- package/dist/runtime/index.d.mts +11 -0
- package/dist/runtime/index.mjs +17 -0
- package/dist/runtime/internal/app.d.mts +2 -0
- package/dist/runtime/internal/app.mjs +153 -0
- package/dist/runtime/internal/cache.d.mts +7 -0
- package/dist/runtime/internal/cache.mjs +236 -0
- package/dist/runtime/internal/context.d.mts +12 -0
- package/dist/runtime/internal/context.mjs +17 -0
- package/dist/runtime/internal/database.d.mts +2 -0
- package/dist/runtime/internal/database.mjs +14 -0
- package/dist/runtime/internal/debug.d.mts +2 -0
- package/dist/runtime/internal/debug.mjs +5 -0
- package/dist/runtime/internal/empty.d.mts +0 -0
- package/dist/runtime/internal/empty.mjs +0 -0
- package/dist/runtime/internal/error/dev.d.mts +9 -0
- package/dist/runtime/internal/error/dev.mjs +122 -0
- package/dist/runtime/internal/error/prod.d.mts +8 -0
- package/dist/runtime/internal/error/prod.mjs +62 -0
- package/dist/runtime/internal/error/utils.d.mts +8 -0
- package/dist/runtime/internal/error/utils.mjs +3 -0
- package/dist/runtime/internal/index.d.mts +3 -0
- package/dist/runtime/internal/index.mjs +3 -0
- package/dist/runtime/internal/lib/http-graceful-shutdown.d.mts +16 -0
- package/dist/runtime/internal/lib/http-graceful-shutdown.mjs +213 -0
- package/dist/runtime/internal/meta.d.mts +2 -0
- package/dist/runtime/internal/meta.mjs +3 -0
- package/dist/runtime/internal/plugin.d.mts +3 -0
- package/dist/runtime/internal/plugin.mjs +4 -0
- package/dist/runtime/internal/renderer.d.mts +3 -0
- package/dist/runtime/internal/renderer.mjs +36 -0
- package/dist/runtime/internal/route-rules.d.mts +8 -0
- package/dist/runtime/internal/route-rules.mjs +62 -0
- package/dist/runtime/internal/routes/dev-tasks.d.mts +3 -0
- package/dist/runtime/internal/routes/dev-tasks.mjs +23 -0
- package/dist/runtime/internal/routes/openapi.d.mts +3 -0
- package/dist/runtime/internal/routes/openapi.mjs +100 -0
- package/dist/runtime/internal/routes/renderer-template.d.mts +2 -0
- package/dist/runtime/internal/routes/renderer-template.dev.d.mts +3 -0
- package/dist/runtime/internal/routes/renderer-template.dev.mjs +21 -0
- package/dist/runtime/internal/routes/renderer-template.mjs +4 -0
- package/dist/runtime/internal/routes/scalar.d.mts +3 -0
- package/dist/runtime/internal/routes/scalar.mjs +194 -0
- package/dist/runtime/internal/routes/swagger.d.mts +3 -0
- package/dist/runtime/internal/routes/swagger.mjs +44 -0
- package/dist/runtime/internal/runtime-config.d.mts +9 -0
- package/dist/runtime/internal/runtime-config.mjs +49 -0
- package/dist/runtime/internal/shutdown.d.mts +9 -0
- package/dist/runtime/internal/shutdown.mjs +34 -0
- package/dist/runtime/internal/static.d.mts +3 -0
- package/dist/runtime/internal/static.mjs +77 -0
- package/dist/runtime/internal/storage.d.mts +2 -0
- package/dist/runtime/internal/storage.mjs +6 -0
- package/dist/runtime/internal/task.d.mts +17 -0
- package/dist/runtime/internal/task.mjs +73 -0
- package/dist/runtime/internal/utils.d.mts +1 -0
- package/dist/runtime/internal/utils.mjs +15 -0
- package/dist/runtime/internal/vite/dev-entry.d.mts +1 -0
- package/dist/runtime/internal/vite/dev-entry.mjs +6 -0
- package/dist/runtime/internal/vite/dev-worker.d.mts +1 -0
- package/dist/runtime/internal/vite/dev-worker.mjs +362 -0
- package/dist/runtime/internal/vite/prod-setup.d.mts +4 -0
- package/dist/runtime/internal/vite/prod-setup.mjs +55 -0
- package/dist/runtime/internal/vite/ssr-renderer.d.mts +4 -0
- package/dist/runtime/internal/vite/ssr-renderer.mjs +4 -0
- package/dist/types/index.d.mts +2810 -0
- package/dist/types/index.mjs +1 -0
- package/dist/vite.d.mts +59 -0
- package/dist/vite.mjs +68 -0
- package/lib/config.d.mts +9 -0
- package/lib/config.mjs +5 -0
- package/lib/deps/h3.d.mts +1 -0
- package/lib/deps/h3.mjs +1 -0
- package/lib/deps/ofetch.mjs +1 -0
- package/lib/deps/ofetch.mts +1 -0
- package/lib/meta.d.mts +5 -0
- package/lib/meta.mjs +21 -0
- package/lib/runtime-meta.d.mts +5 -0
- package/lib/runtime-meta.mjs +38 -0
- package/package.json +170 -38
- package/.editorconfig +0 -12
- package/.eslintrc.js +0 -34
- package/.travis.yml +0 -7
- package/Dockerfile +0 -12
- package/Makefile +0 -37
- package/bin/nitro +0 -55
- package/lib/auto-require.js +0 -44
- package/lib/class-file.js +0 -105
- package/lib/class-files.js +0 -212
- package/lib/color.js +0 -89
- package/lib/cwd.js +0 -27
- package/lib/dir-expand.js +0 -80
- package/lib/dir.js +0 -130
- package/lib/file.js +0 -80
- package/lib/join-paths.js +0 -60
- package/lib/livereload/livereload.js +0 -55
- package/lib/livereload.js +0 -86
- package/lib/nitro.js +0 -161
- package/lib/package.js +0 -211
- package/lib/presets/autoprefixer.js +0 -11
- package/lib/presets/babel.js +0 -33
- package/lib/presets/browserify.js +0 -106
- package/lib/presets/coffee-script.js +0 -29
- package/lib/presets/cssnano.js +0 -11
- package/lib/presets/eslint.js +0 -67
- package/lib/presets/jshint.js +0 -51
- package/lib/presets/less.js +0 -45
- package/lib/presets/log.js +0 -11
- package/lib/presets/ng-annotate.js +0 -34
- package/lib/presets/noop.js +0 -7
- package/lib/presets/postcss/auto-postcss.js +0 -46
- package/lib/presets/postcss/autoprefixer-options.js +0 -9
- package/lib/presets/postcss.js +0 -40
- package/lib/presets/rollup.js +0 -56
- package/lib/presets/sass.js +0 -64
- package/lib/presets/sitemap.js +0 -101
- package/lib/presets/sitemap.xml +0 -12
- package/lib/presets/svg-symbols.js +0 -22
- package/lib/presets/svgo.js +0 -21
- package/lib/presets/uglify.js +0 -72
- package/lib/presets/webpack.js +0 -66
- package/lib/processors.js +0 -84
- package/lib/promisify.js +0 -16
- package/lib/server.js +0 -224
- package/lib/tasks.js +0 -114
- package/lib/watch.js +0 -81
- package/test.js +0 -59
- package/tests/cwd-tests.js +0 -25
- package/tests/dir-tests.js +0 -80
- package/tests/dummy/dummy.coffee +0 -7
- package/tests/dummy/dummy.js +0 -8
- package/tests/dummy/dummy.less +0 -6
- package/tests/dummy/dummy.sass +0 -5
- package/tests/file-tests.js +0 -17
- package/tests/join-paths-tests.js +0 -86
- package/tests/tasks-tests.js +0 -65
- package/wercker.yml +0 -25
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { defineNitroPlugin } from "./internal/plugin.mjs";
|
|
2
|
+
export { defineRouteMeta } from "./internal/meta.mjs";
|
|
3
|
+
export { defineNitroErrorHandler } from "./internal/error/utils.mjs";
|
|
4
|
+
export { useRuntimeConfig } from "./internal/runtime-config.mjs";
|
|
5
|
+
export { useRequest } from "./internal/context.mjs";
|
|
6
|
+
export { defineRenderHandler } from "./internal/renderer.mjs";
|
|
7
|
+
export { defineCachedFunction, defineCachedEventHandler, defineCachedHandler, cachedFunction, cachedEventHandler, } from "./internal/cache.mjs";
|
|
8
|
+
export { useNitroApp } from "./internal/app.mjs";
|
|
9
|
+
export { useStorage } from "./internal/storage.mjs";
|
|
10
|
+
export { useDatabase } from "./internal/database.mjs";
|
|
11
|
+
export { defineTask, runTask } from "./internal/task.mjs";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { defineNitroPlugin } from "./internal/plugin.mjs";
|
|
2
|
+
export { defineRouteMeta } from "./internal/meta.mjs";
|
|
3
|
+
export { defineNitroErrorHandler } from "./internal/error/utils.mjs";
|
|
4
|
+
export { useRuntimeConfig } from "./internal/runtime-config.mjs";
|
|
5
|
+
export { useRequest } from "./internal/context.mjs";
|
|
6
|
+
export { defineRenderHandler } from "./internal/renderer.mjs";
|
|
7
|
+
export {
|
|
8
|
+
defineCachedFunction,
|
|
9
|
+
defineCachedEventHandler,
|
|
10
|
+
defineCachedHandler,
|
|
11
|
+
cachedFunction,
|
|
12
|
+
cachedEventHandler
|
|
13
|
+
} from "./internal/cache.mjs";
|
|
14
|
+
export { useNitroApp } from "./internal/app.mjs";
|
|
15
|
+
export { useStorage } from "./internal/storage.mjs";
|
|
16
|
+
export { useDatabase } from "./internal/database.mjs";
|
|
17
|
+
export { defineTask, runTask } from "./internal/task.mjs";
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { H3Core, toRequest } from "h3";
|
|
2
|
+
import { createHooks } from "hookable";
|
|
3
|
+
import { nitroAsyncContext } from "./context.mjs";
|
|
4
|
+
import errorHandler from "#nitro-internal-virtual/error-handler";
|
|
5
|
+
import { plugins } from "#nitro-internal-virtual/plugins";
|
|
6
|
+
import {
|
|
7
|
+
findRoute,
|
|
8
|
+
findRouteRules,
|
|
9
|
+
globalMiddleware,
|
|
10
|
+
findRoutedMiddleware
|
|
11
|
+
} from "#nitro-internal-virtual/routing";
|
|
12
|
+
export function useNitroApp() {
|
|
13
|
+
return useNitroApp.__instance__ ??= initNitroApp();
|
|
14
|
+
}
|
|
15
|
+
function initNitroApp() {
|
|
16
|
+
const nitroApp = createNitroApp();
|
|
17
|
+
for (const plugin of plugins) {
|
|
18
|
+
try {
|
|
19
|
+
plugin(nitroApp);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
nitroApp.captureError(error, { tags: ["plugin"] });
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return nitroApp;
|
|
26
|
+
}
|
|
27
|
+
function createNitroApp() {
|
|
28
|
+
const hooks = createHooks();
|
|
29
|
+
const captureError = (error, errorCtx) => {
|
|
30
|
+
const promise = hooks.callHookParallel("error", error, errorCtx).catch((hookError) => {
|
|
31
|
+
console.error("Error while capturing another error", hookError);
|
|
32
|
+
});
|
|
33
|
+
if (errorCtx?.event) {
|
|
34
|
+
const errors = errorCtx.event.req.context?.nitro?.errors;
|
|
35
|
+
if (errors) {
|
|
36
|
+
errors.push({ error, context: errorCtx });
|
|
37
|
+
}
|
|
38
|
+
if (typeof errorCtx.event.req.waitUntil === "function") {
|
|
39
|
+
errorCtx.event.req.waitUntil(promise);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const h3App = createH3App(captureError);
|
|
44
|
+
let fetchHandler = async (req) => {
|
|
45
|
+
req.context ??= {};
|
|
46
|
+
req.context.nitro = req.context.nitro || { errors: [] };
|
|
47
|
+
const event = { req };
|
|
48
|
+
const nitroApp = useNitroApp();
|
|
49
|
+
await nitroApp.hooks.callHook("request", event).catch((error) => {
|
|
50
|
+
captureError(error, { event, tags: ["request"] });
|
|
51
|
+
});
|
|
52
|
+
const response = await h3App.request(req, void 0, req.context);
|
|
53
|
+
await nitroApp.hooks.callHook("response", response, event).catch((error) => {
|
|
54
|
+
captureError(error, { event, tags: ["request", "response"] });
|
|
55
|
+
});
|
|
56
|
+
return response;
|
|
57
|
+
};
|
|
58
|
+
if (import.meta._asyncContext) {
|
|
59
|
+
const originalFetchHandler = fetchHandler;
|
|
60
|
+
fetchHandler = (req) => {
|
|
61
|
+
const asyncCtx = { request: req };
|
|
62
|
+
return nitroAsyncContext.callAsync(
|
|
63
|
+
asyncCtx,
|
|
64
|
+
() => originalFetchHandler(req)
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const requestHandler = (input, init, context) => {
|
|
69
|
+
const req = toRequest(input, init);
|
|
70
|
+
req.context = { ...req.context, ...context };
|
|
71
|
+
return Promise.resolve(fetchHandler(req));
|
|
72
|
+
};
|
|
73
|
+
const originalFetch = globalThis.fetch;
|
|
74
|
+
const nitroFetch = (input, init) => {
|
|
75
|
+
if (typeof input === "string" && input.startsWith("/")) {
|
|
76
|
+
return requestHandler(input, init);
|
|
77
|
+
}
|
|
78
|
+
if (input instanceof Request && "_request" in input) {
|
|
79
|
+
input = input._request;
|
|
80
|
+
}
|
|
81
|
+
return originalFetch(input, init);
|
|
82
|
+
};
|
|
83
|
+
globalThis.fetch = nitroFetch;
|
|
84
|
+
const app = {
|
|
85
|
+
_h3: h3App,
|
|
86
|
+
hooks,
|
|
87
|
+
fetch: requestHandler,
|
|
88
|
+
captureError
|
|
89
|
+
};
|
|
90
|
+
return app;
|
|
91
|
+
}
|
|
92
|
+
function createH3App(captureError) {
|
|
93
|
+
const DEBUG_MODE = ["1", "true", "TRUE"].includes(process.env.DEBUG + "");
|
|
94
|
+
const h3App = new H3Core({
|
|
95
|
+
debug: DEBUG_MODE,
|
|
96
|
+
onError: (error, event) => {
|
|
97
|
+
captureError(error, { event, tags: ["request"] });
|
|
98
|
+
return errorHandler(error, event);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
h3App._findRoute = (event) => findRoute(event.req.method, event.url.pathname);
|
|
102
|
+
h3App._getMiddleware = (event, route) => {
|
|
103
|
+
const pathname = event.url.pathname;
|
|
104
|
+
const method = event.req.method;
|
|
105
|
+
const { routeRules, routeRuleMiddleware } = getRouteRules(method, pathname);
|
|
106
|
+
event.context.routeRules = routeRules;
|
|
107
|
+
return [
|
|
108
|
+
...routeRuleMiddleware,
|
|
109
|
+
...globalMiddleware,
|
|
110
|
+
...findRoutedMiddleware(method, pathname).map((r) => r.data),
|
|
111
|
+
...route?.data?.middleware || []
|
|
112
|
+
].filter(Boolean);
|
|
113
|
+
};
|
|
114
|
+
return h3App;
|
|
115
|
+
}
|
|
116
|
+
function getRouteRules(method, pathname) {
|
|
117
|
+
const m = findRouteRules(method, pathname);
|
|
118
|
+
if (!m?.length) {
|
|
119
|
+
return { routeRuleMiddleware: [] };
|
|
120
|
+
}
|
|
121
|
+
const routeRules = {};
|
|
122
|
+
for (const layer of m) {
|
|
123
|
+
for (const rule of layer.data) {
|
|
124
|
+
const currentRule = routeRules[rule.name];
|
|
125
|
+
if (currentRule) {
|
|
126
|
+
if (rule.options === false) {
|
|
127
|
+
delete routeRules[rule.name];
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (typeof currentRule.options === "object" && typeof rule.options === "object") {
|
|
131
|
+
currentRule.options = { ...currentRule.options, ...rule.options };
|
|
132
|
+
} else {
|
|
133
|
+
currentRule.options = rule.options;
|
|
134
|
+
}
|
|
135
|
+
currentRule.route = rule.route;
|
|
136
|
+
currentRule.params = { ...currentRule.params, ...layer.params };
|
|
137
|
+
} else if (rule.options !== false) {
|
|
138
|
+
routeRules[rule.name] = { ...rule, params: layer.params };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const middleware = [];
|
|
143
|
+
for (const rule of Object.values(routeRules)) {
|
|
144
|
+
if (rule.options === false || !rule.handler) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
middleware.push(rule.handler(rule));
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
routeRules,
|
|
151
|
+
routeRuleMiddleware: middleware
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { EventHandler } from "h3";
|
|
2
|
+
import type { CacheOptions, CachedEventHandlerOptions } from "nitro/types";
|
|
3
|
+
export declare function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(fn: (...args: ArgsT) => T | Promise<T>, opts?: CacheOptions<T, ArgsT>): (...args: ArgsT) => Promise<T>;
|
|
4
|
+
export declare function cachedFunction<T, ArgsT extends unknown[] = any[]>(fn: (...args: ArgsT) => T | Promise<T>, opts?: CacheOptions<T>): (...args: ArgsT) => Promise<T | undefined>;
|
|
5
|
+
export declare function defineCachedEventHandler(handler: EventHandler, opts?: CachedEventHandlerOptions): EventHandler;
|
|
6
|
+
export declare const cachedEventHandler: typeof defineCachedEventHandler;
|
|
7
|
+
export declare const defineCachedHandler: typeof defineCachedEventHandler;
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { defineHandler, handleCacheHeaders, isHTTPEvent, toResponse } from "h3";
|
|
2
|
+
import { FastResponse } from "srvx";
|
|
3
|
+
import { parseURL } from "ufo";
|
|
4
|
+
import { hash } from "ohash";
|
|
5
|
+
import { useNitroApp } from "./app.mjs";
|
|
6
|
+
import { useStorage } from "./storage.mjs";
|
|
7
|
+
function defaultCacheOptions() {
|
|
8
|
+
return {
|
|
9
|
+
name: "_",
|
|
10
|
+
base: "/cache",
|
|
11
|
+
swr: true,
|
|
12
|
+
maxAge: 1
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function defineCachedFunction(fn, opts = {}) {
|
|
16
|
+
opts = { ...defaultCacheOptions(), ...opts };
|
|
17
|
+
const pending = {};
|
|
18
|
+
const group = opts.group || "nitro/functions";
|
|
19
|
+
const name = opts.name || fn.name || "_";
|
|
20
|
+
const integrity = opts.integrity || hash([fn, opts]);
|
|
21
|
+
const validate = opts.validate || ((entry) => entry.value !== void 0);
|
|
22
|
+
async function get(key, resolver, shouldInvalidateCache, event) {
|
|
23
|
+
const cacheKey = [opts.base, group, name, key + ".json"].filter(Boolean).join(":").replace(/:\/$/, ":index");
|
|
24
|
+
let entry = await useStorage().getItem(cacheKey).catch((error) => {
|
|
25
|
+
console.error(`[cache] Cache read error.`, error);
|
|
26
|
+
useNitroApp().captureError(error, { event, tags: ["cache"] });
|
|
27
|
+
}) || {};
|
|
28
|
+
if (typeof entry !== "object") {
|
|
29
|
+
entry = {};
|
|
30
|
+
const error = new Error("Malformed data read from cache.");
|
|
31
|
+
console.error("[cache]", error);
|
|
32
|
+
useNitroApp().captureError(error, { event, tags: ["cache"] });
|
|
33
|
+
}
|
|
34
|
+
const ttl = (opts.maxAge ?? 0) * 1e3;
|
|
35
|
+
if (ttl) {
|
|
36
|
+
entry.expires = Date.now() + ttl;
|
|
37
|
+
}
|
|
38
|
+
const expired = shouldInvalidateCache || entry.integrity !== integrity || ttl && Date.now() - (entry.mtime || 0) > ttl || validate(entry) === false;
|
|
39
|
+
const _resolve = async () => {
|
|
40
|
+
const isPending = pending[key];
|
|
41
|
+
if (!isPending) {
|
|
42
|
+
if (entry.value !== void 0 && (opts.staleMaxAge || 0) >= 0 && opts.swr === false) {
|
|
43
|
+
entry.value = void 0;
|
|
44
|
+
entry.integrity = void 0;
|
|
45
|
+
entry.mtime = void 0;
|
|
46
|
+
entry.expires = void 0;
|
|
47
|
+
}
|
|
48
|
+
pending[key] = Promise.resolve(resolver());
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
entry.value = await pending[key];
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (!isPending) {
|
|
54
|
+
delete pending[key];
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
if (!isPending) {
|
|
59
|
+
entry.mtime = Date.now();
|
|
60
|
+
entry.integrity = integrity;
|
|
61
|
+
delete pending[key];
|
|
62
|
+
if (validate(entry) !== false) {
|
|
63
|
+
let setOpts;
|
|
64
|
+
if (opts.maxAge && !opts.swr) {
|
|
65
|
+
setOpts = { ttl: opts.maxAge };
|
|
66
|
+
}
|
|
67
|
+
const promise = useStorage().setItem(cacheKey, entry, setOpts).catch((error) => {
|
|
68
|
+
console.error(`[cache] Cache write error.`, error);
|
|
69
|
+
useNitroApp().captureError(error, { event, tags: ["cache"] });
|
|
70
|
+
});
|
|
71
|
+
if (typeof event?.req?.waitUntil === "function") {
|
|
72
|
+
event.req.waitUntil(promise);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const _resolvePromise = expired ? _resolve() : Promise.resolve();
|
|
78
|
+
if (entry.value === void 0) {
|
|
79
|
+
await _resolvePromise;
|
|
80
|
+
} else if (expired && event && event.req.waitUntil) {
|
|
81
|
+
event.req.waitUntil(_resolvePromise);
|
|
82
|
+
}
|
|
83
|
+
if (opts.swr && validate(entry) !== false) {
|
|
84
|
+
_resolvePromise.catch((error) => {
|
|
85
|
+
console.error(`[cache] SWR handler error.`, error);
|
|
86
|
+
useNitroApp().captureError(error, { event, tags: ["cache"] });
|
|
87
|
+
});
|
|
88
|
+
return entry;
|
|
89
|
+
}
|
|
90
|
+
return _resolvePromise.then(() => entry);
|
|
91
|
+
}
|
|
92
|
+
return async (...args) => {
|
|
93
|
+
const shouldBypassCache = await opts.shouldBypassCache?.(...args);
|
|
94
|
+
if (shouldBypassCache) {
|
|
95
|
+
return fn(...args);
|
|
96
|
+
}
|
|
97
|
+
const key = await (opts.getKey || getKey)(...args);
|
|
98
|
+
const shouldInvalidateCache = await opts.shouldInvalidateCache?.(...args);
|
|
99
|
+
const entry = await get(
|
|
100
|
+
key,
|
|
101
|
+
() => fn(...args),
|
|
102
|
+
shouldInvalidateCache,
|
|
103
|
+
args[0] && isHTTPEvent(args[0]) ? args[0] : void 0
|
|
104
|
+
);
|
|
105
|
+
let value = entry.value;
|
|
106
|
+
if (opts.transform) {
|
|
107
|
+
value = await opts.transform(entry, ...args) || value;
|
|
108
|
+
}
|
|
109
|
+
return value;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export function cachedFunction(fn, opts = {}) {
|
|
113
|
+
return defineCachedFunction(fn, opts);
|
|
114
|
+
}
|
|
115
|
+
function getKey(...args) {
|
|
116
|
+
return args.length > 0 ? hash(args) : "";
|
|
117
|
+
}
|
|
118
|
+
function escapeKey(key) {
|
|
119
|
+
return String(key).replace(/\W/g, "");
|
|
120
|
+
}
|
|
121
|
+
export function defineCachedEventHandler(handler, opts = defaultCacheOptions()) {
|
|
122
|
+
const variableHeaderNames = (opts.varies || []).filter(Boolean).map((h) => h.toLowerCase()).sort();
|
|
123
|
+
const _opts = {
|
|
124
|
+
...opts,
|
|
125
|
+
shouldBypassCache: (event) => {
|
|
126
|
+
return event.req.method !== "GET" && event.req.method !== "HEAD";
|
|
127
|
+
},
|
|
128
|
+
getKey: async (event) => {
|
|
129
|
+
const customKey = await opts.getKey?.(event);
|
|
130
|
+
if (customKey) {
|
|
131
|
+
return escapeKey(customKey);
|
|
132
|
+
}
|
|
133
|
+
const _path = event.url.pathname + event.url.search;
|
|
134
|
+
let _pathname;
|
|
135
|
+
try {
|
|
136
|
+
_pathname = escapeKey(decodeURI(parseURL(_path).pathname)).slice(0, 16) || "index";
|
|
137
|
+
} catch {
|
|
138
|
+
_pathname = "-";
|
|
139
|
+
}
|
|
140
|
+
const _hashedPath = `${_pathname}.${hash(_path)}`;
|
|
141
|
+
const _headers = variableHeaderNames.map((header) => [header, event.req.headers.get(header)]).map(([name, value]) => `${escapeKey(name)}.${hash(value)}`);
|
|
142
|
+
return [_hashedPath, ..._headers].join(":");
|
|
143
|
+
},
|
|
144
|
+
validate: (entry) => {
|
|
145
|
+
if (!entry.value) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
if (entry.value.status >= 400) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
if (entry.value.body === void 0) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
if (entry.value.headers.etag === "undefined" || entry.value.headers["last-modified"] === "undefined") {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
},
|
|
159
|
+
group: opts.group || "nitro/handlers",
|
|
160
|
+
integrity: opts.integrity || hash([handler, opts])
|
|
161
|
+
};
|
|
162
|
+
const _cachedHandler = cachedFunction(
|
|
163
|
+
async (event) => {
|
|
164
|
+
const filteredHeaders = [...event.req.headers.entries()].filter(
|
|
165
|
+
([key]) => !variableHeaderNames.includes(key.toLowerCase())
|
|
166
|
+
);
|
|
167
|
+
try {
|
|
168
|
+
const originalReq = event.req;
|
|
169
|
+
event.req = new Request(event.req.url, {
|
|
170
|
+
method: event.req.method,
|
|
171
|
+
headers: filteredHeaders
|
|
172
|
+
});
|
|
173
|
+
event.req.runtime = originalReq.runtime;
|
|
174
|
+
event.req.waitUntil = originalReq.waitUntil;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.error("[cache] Failed to filter headers:", error);
|
|
177
|
+
}
|
|
178
|
+
const rawValue = await handler(event);
|
|
179
|
+
const res = await toResponse(rawValue, event);
|
|
180
|
+
const body = await res.text();
|
|
181
|
+
if (!res.headers.has("etag")) {
|
|
182
|
+
res.headers.set("etag", `W/"${hash(body)}"`);
|
|
183
|
+
}
|
|
184
|
+
if (!res.headers.has("last-modified")) {
|
|
185
|
+
res.headers.set("last-modified", (/* @__PURE__ */ new Date()).toUTCString());
|
|
186
|
+
}
|
|
187
|
+
const cacheControl = [];
|
|
188
|
+
if (opts.swr) {
|
|
189
|
+
if (opts.maxAge) {
|
|
190
|
+
cacheControl.push(`s-maxage=${opts.maxAge}`);
|
|
191
|
+
}
|
|
192
|
+
if (opts.staleMaxAge) {
|
|
193
|
+
cacheControl.push(`stale-while-revalidate=${opts.staleMaxAge}`);
|
|
194
|
+
} else {
|
|
195
|
+
cacheControl.push("stale-while-revalidate");
|
|
196
|
+
}
|
|
197
|
+
} else if (opts.maxAge) {
|
|
198
|
+
cacheControl.push(`max-age=${opts.maxAge}`);
|
|
199
|
+
}
|
|
200
|
+
if (cacheControl.length > 0) {
|
|
201
|
+
res.headers.set("cache-control", cacheControl.join(", "));
|
|
202
|
+
}
|
|
203
|
+
const cacheEntry = {
|
|
204
|
+
status: res.status,
|
|
205
|
+
statusText: res.statusText,
|
|
206
|
+
headers: Object.fromEntries(res.headers.entries()),
|
|
207
|
+
body
|
|
208
|
+
};
|
|
209
|
+
return cacheEntry;
|
|
210
|
+
},
|
|
211
|
+
_opts
|
|
212
|
+
);
|
|
213
|
+
return defineHandler(async (event) => {
|
|
214
|
+
if (opts.headersOnly) {
|
|
215
|
+
if (handleCacheHeaders(event, { maxAge: opts.maxAge })) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
return handler(event);
|
|
219
|
+
}
|
|
220
|
+
const response = await _cachedHandler(event);
|
|
221
|
+
if (handleCacheHeaders(event, {
|
|
222
|
+
modifiedTime: new Date(response.headers["last-modified"]),
|
|
223
|
+
etag: response.headers.etag,
|
|
224
|
+
maxAge: opts.maxAge
|
|
225
|
+
})) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
return new FastResponse(response.body, {
|
|
229
|
+
status: response.status,
|
|
230
|
+
statusText: response.statusText,
|
|
231
|
+
headers: response.headers
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
export const cachedEventHandler = defineCachedEventHandler;
|
|
236
|
+
export const defineCachedHandler = defineCachedEventHandler;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ServerRequest } from "srvx";
|
|
2
|
+
export declare const nitroAsyncContext: import("unctx/index").UseContext<NitroAsyncContext>;
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* Access to the current Nitro request.
|
|
6
|
+
*
|
|
7
|
+
* @experimental
|
|
8
|
+
* - Requires `experimental.asyncContext: true` config to work.
|
|
9
|
+
* - Works in Node.js and limited runtimes only
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
export declare function useRequest(): ServerRequest;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { HTTPError } from "h3";
|
|
3
|
+
import { getContext } from "unctx";
|
|
4
|
+
export const nitroAsyncContext = /* @__PURE__ */ (() => getContext("nitro-app", {
|
|
5
|
+
asyncContext: import.meta._asyncContext,
|
|
6
|
+
AsyncLocalStorage: import.meta._asyncContext ? AsyncLocalStorage : void 0
|
|
7
|
+
}))();
|
|
8
|
+
export function useRequest() {
|
|
9
|
+
try {
|
|
10
|
+
return nitroAsyncContext.use().request;
|
|
11
|
+
} catch {
|
|
12
|
+
const hint = import.meta._asyncContext ? "Note: This is an experimental feature and might be broken on non-Node.js environments." : "Enable the experimental flag using `experimental.asyncContext: true`.";
|
|
13
|
+
throw new HTTPError({
|
|
14
|
+
message: `Nitro request context is not available. ${hint}`
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createDatabase } from "db0";
|
|
2
|
+
import { connectionConfigs } from "#nitro-internal-virtual/database";
|
|
3
|
+
const instances = /* @__PURE__ */ Object.create(null);
|
|
4
|
+
export function useDatabase(name = "default") {
|
|
5
|
+
if (instances[name]) {
|
|
6
|
+
return instances[name];
|
|
7
|
+
}
|
|
8
|
+
if (!connectionConfigs[name]) {
|
|
9
|
+
throw new Error(`Database connection "${name}" not configured.`);
|
|
10
|
+
}
|
|
11
|
+
return instances[name] = createDatabase(
|
|
12
|
+
connectionConfigs[name].connector(connectionConfigs[name].options || {})
|
|
13
|
+
);
|
|
14
|
+
}
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HTTPError, HTTPEvent } from "h3";
|
|
2
|
+
import type { InternalHandlerResponse } from "./utils.mjs";
|
|
3
|
+
declare const _default: NitroErrorHandler;
|
|
4
|
+
export default _default;
|
|
5
|
+
export declare function defaultHandler(error: HTTPError, event: HTTPEvent, opts?: {
|
|
6
|
+
silent?: boolean;
|
|
7
|
+
json?: boolean;
|
|
8
|
+
}): Promise<InternalHandlerResponse>;
|
|
9
|
+
export declare function loadStackTrace(error: any): Promise<void>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { getRequestURL } from "h3";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { resolve, dirname } from "node:path";
|
|
4
|
+
import consola from "consola";
|
|
5
|
+
import { ErrorParser } from "youch-core";
|
|
6
|
+
import { Youch } from "youch";
|
|
7
|
+
import { SourceMapConsumer } from "source-map";
|
|
8
|
+
import { defineNitroErrorHandler } from "./utils.mjs";
|
|
9
|
+
import { FastResponse } from "srvx";
|
|
10
|
+
export default defineNitroErrorHandler(
|
|
11
|
+
async function defaultNitroErrorHandler(error, event) {
|
|
12
|
+
const res = await defaultHandler(error, event);
|
|
13
|
+
return new FastResponse(
|
|
14
|
+
typeof res.body === "string" ? res.body : JSON.stringify(res.body, null, 2),
|
|
15
|
+
res
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
export async function defaultHandler(error, event, opts) {
|
|
20
|
+
const isSensitive = error.unhandled;
|
|
21
|
+
const status = error.status || 500;
|
|
22
|
+
const url = getRequestURL(event, { xForwardedHost: true, xForwardedProto: true });
|
|
23
|
+
if (status === 404) {
|
|
24
|
+
const baseURL = import.meta.baseURL || "/";
|
|
25
|
+
if (/^\/[^/]/.test(baseURL) && !url.pathname.startsWith(baseURL)) {
|
|
26
|
+
const redirectTo = `${baseURL}${url.pathname.slice(1)}${url.search}`;
|
|
27
|
+
return {
|
|
28
|
+
status: 302,
|
|
29
|
+
statusText: "Found",
|
|
30
|
+
headers: { location: redirectTo },
|
|
31
|
+
body: `Redirecting...`
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
await loadStackTrace(error).catch(consola.error);
|
|
36
|
+
const youch = new Youch();
|
|
37
|
+
if (isSensitive && !opts?.silent) {
|
|
38
|
+
const tags = [error.unhandled && "[unhandled]"].filter(Boolean).join(" ");
|
|
39
|
+
const ansiError = await (await youch.toANSI(error)).replaceAll(process.cwd(), ".");
|
|
40
|
+
consola.error(
|
|
41
|
+
`[request error] ${tags} [${event.req.method}] ${url}
|
|
42
|
+
|
|
43
|
+
`,
|
|
44
|
+
ansiError
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
const useJSON = opts?.json || !event.req.headers.get("accept")?.includes("text/html");
|
|
48
|
+
const headers = {
|
|
49
|
+
"content-type": useJSON ? "application/json" : "text/html",
|
|
50
|
+
// Prevent browser from guessing the MIME types of resources.
|
|
51
|
+
"x-content-type-options": "nosniff",
|
|
52
|
+
// Prevent error page from being embedded in an iframe
|
|
53
|
+
"x-frame-options": "DENY",
|
|
54
|
+
// Prevent browsers from sending the Referer header
|
|
55
|
+
"referrer-policy": "no-referrer",
|
|
56
|
+
// Disable the execution of any js
|
|
57
|
+
"content-security-policy": "script-src 'self' 'unsafe-inline'; object-src 'none'; base-uri 'self';"
|
|
58
|
+
};
|
|
59
|
+
if (status === 404 || !event.res.headers.has("cache-control")) {
|
|
60
|
+
headers["cache-control"] = "no-cache";
|
|
61
|
+
}
|
|
62
|
+
const body = useJSON ? {
|
|
63
|
+
error: true,
|
|
64
|
+
url,
|
|
65
|
+
status,
|
|
66
|
+
statusText: error.statusText,
|
|
67
|
+
message: error.message,
|
|
68
|
+
data: error.data,
|
|
69
|
+
stack: error.stack?.split("\n").map((line) => line.trim())
|
|
70
|
+
} : await youch.toHTML(error, {
|
|
71
|
+
request: {
|
|
72
|
+
url: url.href,
|
|
73
|
+
method: event.req.method,
|
|
74
|
+
headers: Object.fromEntries(event.req.headers.entries())
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
status,
|
|
79
|
+
statusText: error.statusText,
|
|
80
|
+
headers,
|
|
81
|
+
body
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export async function loadStackTrace(error) {
|
|
85
|
+
if (!(error instanceof Error)) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const parsed = await new ErrorParser().defineSourceLoader(sourceLoader).parse(error);
|
|
89
|
+
const stack = error.message + "\n" + parsed.frames.map((frame) => fmtFrame(frame)).join("\n");
|
|
90
|
+
Object.defineProperty(error, "stack", { value: stack });
|
|
91
|
+
if (error.cause) {
|
|
92
|
+
await loadStackTrace(error.cause).catch(consola.error);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function sourceLoader(frame) {
|
|
96
|
+
if (!frame.fileName || frame.fileType !== "fs" || frame.type === "native") {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (frame.type === "app") {
|
|
100
|
+
const rawSourceMap = await readFile(`${frame.fileName}.map`, "utf8").catch(() => {
|
|
101
|
+
});
|
|
102
|
+
if (rawSourceMap) {
|
|
103
|
+
const consumer = await new SourceMapConsumer(rawSourceMap);
|
|
104
|
+
const originalPosition = consumer.originalPositionFor({ line: frame.lineNumber, column: frame.columnNumber });
|
|
105
|
+
if (originalPosition.source && originalPosition.line) {
|
|
106
|
+
frame.fileName = resolve(dirname(frame.fileName), originalPosition.source);
|
|
107
|
+
frame.lineNumber = originalPosition.line;
|
|
108
|
+
frame.columnNumber = originalPosition.column || 0;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const contents = await readFile(frame.fileName, "utf8").catch(() => {
|
|
113
|
+
});
|
|
114
|
+
return contents ? { contents } : void 0;
|
|
115
|
+
}
|
|
116
|
+
function fmtFrame(frame) {
|
|
117
|
+
if (frame.type === "native") {
|
|
118
|
+
return frame.raw;
|
|
119
|
+
}
|
|
120
|
+
const src = `${frame.fileName || ""}:${frame.lineNumber}:${frame.columnNumber})`;
|
|
121
|
+
return frame.functionName ? `at ${frame.functionName} (${src}` : `at ${src}`;
|
|
122
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HTTPError, HTTPEvent } from "h3";
|
|
2
|
+
import type { InternalHandlerResponse } from "./utils.mjs";
|
|
3
|
+
declare const _default: NitroErrorHandler;
|
|
4
|
+
export default _default;
|
|
5
|
+
export declare function defaultHandler(error: HTTPError, event: HTTPEvent, opts?: {
|
|
6
|
+
silent?: boolean;
|
|
7
|
+
json?: boolean;
|
|
8
|
+
}): InternalHandlerResponse;
|