nitro 2.2.28 → 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.
Files changed (306) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +11 -259
  3. package/dist/_chunks/app.mjs +19797 -0
  4. package/dist/_chunks/build.mjs +84 -0
  5. package/dist/_chunks/build2.mjs +318 -0
  6. package/dist/_chunks/build3.mjs +6452 -0
  7. package/dist/_chunks/detect-acorn.mjs +503 -0
  8. package/dist/_chunks/index.mjs +22242 -0
  9. package/dist/_chunks/index2.mjs +297 -0
  10. package/dist/_chunks/index3.mjs +1058 -0
  11. package/dist/_chunks/index4.mjs +1206 -0
  12. package/dist/_chunks/info.mjs +11157 -0
  13. package/dist/_chunks/json5.mjs +68 -0
  14. package/dist/_chunks/jsonc.mjs +51 -0
  15. package/dist/_chunks/pathe.M-eThtNZ.mjs +204 -0
  16. package/dist/_chunks/plugin.mjs +960 -0
  17. package/dist/_chunks/server.mjs +254 -0
  18. package/dist/_chunks/snapshot.mjs +284 -0
  19. package/dist/_chunks/toml.mjs +259 -0
  20. package/dist/_chunks/yaml.mjs +86 -0
  21. package/dist/cli/build.mjs +54 -0
  22. package/dist/cli/common.mjs +13 -0
  23. package/dist/cli/dev.mjs +95 -0
  24. package/dist/cli/index.d.mts +1 -0
  25. package/dist/cli/index.mjs +477 -0
  26. package/dist/cli/index2.mjs +17 -0
  27. package/dist/cli/list.mjs +34 -0
  28. package/dist/cli/prepare.mjs +24 -0
  29. package/dist/cli/run.mjs +60 -0
  30. package/dist/index.d.mts +52 -0
  31. package/dist/index.mjs +55 -0
  32. package/dist/node_modules/@poppinss/colors/build/index.js +169 -0
  33. package/dist/node_modules/@poppinss/colors/package.json +116 -0
  34. package/dist/node_modules/@poppinss/dumper/build/chunk-26HALFTP.js +602 -0
  35. package/dist/node_modules/@poppinss/dumper/build/formatters/console/main.js +441 -0
  36. package/dist/node_modules/@poppinss/dumper/build/formatters/html/main.js +717 -0
  37. package/dist/node_modules/@poppinss/dumper/package.json +122 -0
  38. package/dist/node_modules/@poppinss/exception/build/index.js +63 -0
  39. package/dist/node_modules/@poppinss/exception/package.json +106 -0
  40. package/dist/node_modules/@sindresorhus/is/distribution/index.js +1313 -0
  41. package/dist/node_modules/@sindresorhus/is/distribution/utilities.js +3 -0
  42. package/dist/node_modules/@sindresorhus/is/package.json +81 -0
  43. package/dist/node_modules/@speed-highlight/core/dist/index.js +4 -0
  44. package/dist/node_modules/@speed-highlight/core/dist/terminal.js +2 -0
  45. package/dist/node_modules/@speed-highlight/core/package.json +90 -0
  46. package/dist/node_modules/cookie/dist/index.js +239 -0
  47. package/dist/node_modules/cookie/package.json +42 -0
  48. package/dist/node_modules/croner/dist/croner.js +1 -0
  49. package/dist/node_modules/croner/package.json +63 -0
  50. package/dist/node_modules/defu/dist/defu.mjs +69 -0
  51. package/dist/node_modules/defu/package.json +43 -0
  52. package/dist/node_modules/destr/dist/index.mjs +72 -0
  53. package/dist/node_modules/destr/package.json +47 -0
  54. package/dist/node_modules/error-stack-parser-es/dist/index.mjs +38 -0
  55. package/dist/node_modules/error-stack-parser-es/dist/lite.mjs +167 -0
  56. package/dist/node_modules/error-stack-parser-es/package.json +83 -0
  57. package/dist/node_modules/get-port-please/dist/index.mjs +430 -0
  58. package/dist/node_modules/get-port-please/package.json +39 -0
  59. package/dist/node_modules/hookable/dist/index.mjs +290 -0
  60. package/dist/node_modules/hookable/package.json +49 -0
  61. package/dist/node_modules/kleur/index.js +110 -0
  62. package/dist/node_modules/kleur/index.mjs +110 -0
  63. package/dist/node_modules/kleur/package.json +51 -0
  64. package/dist/node_modules/klona/dist/index.mjs +81 -0
  65. package/dist/node_modules/klona/full/index.mjs +53 -0
  66. package/dist/node_modules/klona/package.json +74 -0
  67. package/dist/node_modules/scule/dist/index.mjs +80 -0
  68. package/dist/node_modules/scule/package.json +43 -0
  69. package/dist/node_modules/source-map/lib/array-set.js +100 -0
  70. package/dist/node_modules/source-map/lib/base64-vlq.js +94 -0
  71. package/dist/node_modules/source-map/lib/base64.js +19 -0
  72. package/dist/node_modules/source-map/lib/binary-search.js +113 -0
  73. package/dist/node_modules/source-map/lib/mapping-list.js +83 -0
  74. package/dist/node_modules/source-map/lib/mappings.wasm +0 -0
  75. package/dist/node_modules/source-map/lib/read-wasm.js +27 -0
  76. package/dist/node_modules/source-map/lib/source-map-consumer.js +1081 -0
  77. package/dist/node_modules/source-map/lib/source-map-generator.js +439 -0
  78. package/dist/node_modules/source-map/lib/source-node.js +430 -0
  79. package/dist/node_modules/source-map/lib/url.js +13 -0
  80. package/dist/node_modules/source-map/lib/util.js +444 -0
  81. package/dist/node_modules/source-map/lib/wasm.js +138 -0
  82. package/dist/node_modules/source-map/package.json +79 -0
  83. package/dist/node_modules/source-map/source-map.js +10 -0
  84. package/dist/node_modules/std-env/dist/index.mjs +1 -0
  85. package/dist/node_modules/std-env/package.json +46 -0
  86. package/dist/node_modules/supports-color/index.js +202 -0
  87. package/dist/node_modules/supports-color/package.json +64 -0
  88. package/dist/node_modules/ufo/dist/index.mjs +638 -0
  89. package/dist/node_modules/ufo/package.json +47 -0
  90. package/dist/node_modules/unctx/dist/index.mjs +131 -0
  91. package/dist/node_modules/unctx/package.json +67 -0
  92. package/dist/node_modules/youch/build/chunk-4L7RY2JA.js +42 -0
  93. package/dist/node_modules/youch/build/chunk-4XB2BYKC.js +85 -0
  94. package/dist/node_modules/youch/build/chunk-4YEN7HVQ.js +61 -0
  95. package/dist/node_modules/youch/build/chunk-EUJBVOYB.js +83 -0
  96. package/dist/node_modules/youch/build/chunk-HFSXRSKS.js +85 -0
  97. package/dist/node_modules/youch/build/chunk-JAN2TFI2.js +222 -0
  98. package/dist/node_modules/youch/build/chunk-OSUFJZHZ.js +6 -0
  99. package/dist/node_modules/youch/build/chunk-PINJDICN.js +72 -0
  100. package/dist/node_modules/youch/build/chunk-PUHGL6HA.js +41 -0
  101. package/dist/node_modules/youch/build/chunk-VE4LENUR.js +45 -0
  102. package/dist/node_modules/youch/build/index.js +352 -0
  103. package/dist/node_modules/youch/build/public/error_cause/style.css +5 -0
  104. package/dist/node_modules/youch/build/public/error_info/script.js +13 -0
  105. package/dist/node_modules/youch/build/public/error_info/style.css +144 -0
  106. package/dist/node_modules/youch/build/public/error_stack/script.js +74 -0
  107. package/dist/node_modules/youch/build/public/error_stack/style.css +220 -0
  108. package/dist/node_modules/youch/build/public/error_stack_source/style.css +123 -0
  109. package/dist/node_modules/youch/build/public/header/script.js +16 -0
  110. package/dist/node_modules/youch/build/public/header/style.css +56 -0
  111. package/dist/node_modules/youch/build/public/layout/script.js +20 -0
  112. package/dist/node_modules/youch/build/public/layout/style.css +431 -0
  113. package/dist/node_modules/youch/package.json +133 -0
  114. package/dist/node_modules/youch-core/build/index.js +316 -0
  115. package/dist/node_modules/youch-core/package.json +124 -0
  116. package/dist/presets/_nitro/runtime/nitro-dev.d.mts +1 -0
  117. package/dist/presets/_nitro/runtime/nitro-dev.mjs +62 -0
  118. package/dist/presets/_nitro/runtime/nitro-prerenderer.d.mts +3 -0
  119. package/dist/presets/_nitro/runtime/nitro-prerenderer.mjs +18 -0
  120. package/dist/presets/_nitro/runtime/service-worker.d.mts +1 -0
  121. package/dist/presets/_nitro/runtime/service-worker.mjs +21 -0
  122. package/dist/presets/aws-amplify/runtime/aws-amplify.d.mts +1 -0
  123. package/dist/presets/aws-amplify/runtime/aws-amplify.mjs +13 -0
  124. package/dist/presets/aws-lambda/runtime/_utils.d.mts +18 -0
  125. package/dist/presets/aws-lambda/runtime/_utils.mjs +102 -0
  126. package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.d.mts +3 -0
  127. package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.mjs +33 -0
  128. package/dist/presets/aws-lambda/runtime/aws-lambda.d.mts +3 -0
  129. package/dist/presets/aws-lambda/runtime/aws-lambda.mjs +13 -0
  130. package/dist/presets/azure/runtime/_utils.d.mts +2 -0
  131. package/dist/presets/azure/runtime/_utils.mjs +50 -0
  132. package/dist/presets/azure/runtime/azure-swa.d.mts +5 -0
  133. package/dist/presets/azure/runtime/azure-swa.mjs +28 -0
  134. package/dist/presets/bun/runtime/bun.d.mts +1 -0
  135. package/dist/presets/bun/runtime/bun.mjs +27 -0
  136. package/dist/presets/cloudflare/runtime/_module-handler.d.mts +13 -0
  137. package/dist/presets/cloudflare/runtime/_module-handler.mjs +93 -0
  138. package/dist/presets/cloudflare/runtime/cloudflare-durable.d.mts +19 -0
  139. package/dist/presets/cloudflare/runtime/cloudflare-durable.mjs +74 -0
  140. package/dist/presets/cloudflare/runtime/cloudflare-module.d.mts +9 -0
  141. package/dist/presets/cloudflare/runtime/cloudflare-module.mjs +20 -0
  142. package/dist/presets/cloudflare/runtime/cloudflare-pages.d.mts +20 -0
  143. package/dist/presets/cloudflare/runtime/cloudflare-pages.mjs +47 -0
  144. package/dist/presets/cloudflare/runtime/plugin.dev.d.mts +3 -0
  145. package/dist/presets/cloudflare/runtime/plugin.dev.mjs +89 -0
  146. package/dist/presets/deno/runtime/deno-deploy.d.mts +1 -0
  147. package/dist/presets/deno/runtime/deno-deploy.mjs +17 -0
  148. package/dist/presets/deno/runtime/deno-server.d.mts +6 -0
  149. package/dist/presets/deno/runtime/deno-server.mjs +59 -0
  150. package/dist/presets/netlify/runtime/netlify-edge.d.mts +3 -0
  151. package/dist/presets/netlify/runtime/netlify-edge.mjs +17 -0
  152. package/dist/presets/netlify/runtime/netlify.d.mts +3 -0
  153. package/dist/presets/netlify/runtime/netlify.mjs +24 -0
  154. package/dist/presets/node/runtime/node-cluster.d.mts +1 -0
  155. package/dist/presets/node/runtime/node-cluster.mjs +58 -0
  156. package/dist/presets/node/runtime/node-middleware.d.mts +4 -0
  157. package/dist/presets/node/runtime/node-middleware.mjs +14 -0
  158. package/dist/presets/node/runtime/node-server.d.mts +3 -0
  159. package/dist/presets/node/runtime/node-server.mjs +44 -0
  160. package/dist/presets/standard/runtime/server.d.mts +5 -0
  161. package/dist/presets/standard/runtime/server.mjs +6 -0
  162. package/dist/presets/stormkit/runtime/stormkit.d.mts +21 -0
  163. package/dist/presets/stormkit/runtime/stormkit.mjs +28 -0
  164. package/dist/presets/vercel/runtime/vercel.d.mts +8 -0
  165. package/dist/presets/vercel/runtime/vercel.mjs +17 -0
  166. package/dist/presets/winterjs/runtime/winterjs.d.mts +1 -0
  167. package/dist/presets/winterjs/runtime/winterjs.mjs +87 -0
  168. package/dist/presets/zeabur/runtime/zeabur.d.mts +3 -0
  169. package/dist/presets/zeabur/runtime/zeabur.mjs +4 -0
  170. package/dist/presets.mjs +2460 -0
  171. package/dist/runtime/index.d.mts +11 -0
  172. package/dist/runtime/index.mjs +17 -0
  173. package/dist/runtime/internal/app.d.mts +2 -0
  174. package/dist/runtime/internal/app.mjs +153 -0
  175. package/dist/runtime/internal/cache.d.mts +7 -0
  176. package/dist/runtime/internal/cache.mjs +236 -0
  177. package/dist/runtime/internal/context.d.mts +12 -0
  178. package/dist/runtime/internal/context.mjs +17 -0
  179. package/dist/runtime/internal/database.d.mts +2 -0
  180. package/dist/runtime/internal/database.mjs +14 -0
  181. package/dist/runtime/internal/debug.d.mts +2 -0
  182. package/dist/runtime/internal/debug.mjs +5 -0
  183. package/dist/runtime/internal/empty.d.mts +0 -0
  184. package/dist/runtime/internal/empty.mjs +0 -0
  185. package/dist/runtime/internal/error/dev.d.mts +9 -0
  186. package/dist/runtime/internal/error/dev.mjs +122 -0
  187. package/dist/runtime/internal/error/prod.d.mts +8 -0
  188. package/dist/runtime/internal/error/prod.mjs +62 -0
  189. package/dist/runtime/internal/error/utils.d.mts +8 -0
  190. package/dist/runtime/internal/error/utils.mjs +3 -0
  191. package/dist/runtime/internal/index.d.mts +3 -0
  192. package/dist/runtime/internal/index.mjs +3 -0
  193. package/dist/runtime/internal/lib/http-graceful-shutdown.d.mts +16 -0
  194. package/dist/runtime/internal/lib/http-graceful-shutdown.mjs +213 -0
  195. package/dist/runtime/internal/meta.d.mts +2 -0
  196. package/dist/runtime/internal/meta.mjs +3 -0
  197. package/dist/runtime/internal/plugin.d.mts +3 -0
  198. package/dist/runtime/internal/plugin.mjs +4 -0
  199. package/dist/runtime/internal/renderer.d.mts +3 -0
  200. package/dist/runtime/internal/renderer.mjs +36 -0
  201. package/dist/runtime/internal/route-rules.d.mts +8 -0
  202. package/dist/runtime/internal/route-rules.mjs +62 -0
  203. package/dist/runtime/internal/routes/dev-tasks.d.mts +3 -0
  204. package/dist/runtime/internal/routes/dev-tasks.mjs +23 -0
  205. package/dist/runtime/internal/routes/openapi.d.mts +3 -0
  206. package/dist/runtime/internal/routes/openapi.mjs +100 -0
  207. package/dist/runtime/internal/routes/renderer-template.d.mts +2 -0
  208. package/dist/runtime/internal/routes/renderer-template.dev.d.mts +3 -0
  209. package/dist/runtime/internal/routes/renderer-template.dev.mjs +21 -0
  210. package/dist/runtime/internal/routes/renderer-template.mjs +4 -0
  211. package/dist/runtime/internal/routes/scalar.d.mts +3 -0
  212. package/dist/runtime/internal/routes/scalar.mjs +194 -0
  213. package/dist/runtime/internal/routes/swagger.d.mts +3 -0
  214. package/dist/runtime/internal/routes/swagger.mjs +44 -0
  215. package/dist/runtime/internal/runtime-config.d.mts +9 -0
  216. package/dist/runtime/internal/runtime-config.mjs +49 -0
  217. package/dist/runtime/internal/shutdown.d.mts +9 -0
  218. package/dist/runtime/internal/shutdown.mjs +34 -0
  219. package/dist/runtime/internal/static.d.mts +3 -0
  220. package/dist/runtime/internal/static.mjs +77 -0
  221. package/dist/runtime/internal/storage.d.mts +2 -0
  222. package/dist/runtime/internal/storage.mjs +6 -0
  223. package/dist/runtime/internal/task.d.mts +17 -0
  224. package/dist/runtime/internal/task.mjs +73 -0
  225. package/dist/runtime/internal/utils.d.mts +1 -0
  226. package/dist/runtime/internal/utils.mjs +15 -0
  227. package/dist/runtime/internal/vite/dev-entry.d.mts +1 -0
  228. package/dist/runtime/internal/vite/dev-entry.mjs +6 -0
  229. package/dist/runtime/internal/vite/dev-worker.d.mts +1 -0
  230. package/dist/runtime/internal/vite/dev-worker.mjs +362 -0
  231. package/dist/runtime/internal/vite/prod-setup.d.mts +4 -0
  232. package/dist/runtime/internal/vite/prod-setup.mjs +55 -0
  233. package/dist/runtime/internal/vite/ssr-renderer.d.mts +4 -0
  234. package/dist/runtime/internal/vite/ssr-renderer.mjs +4 -0
  235. package/dist/types/index.d.mts +2810 -0
  236. package/dist/types/index.mjs +1 -0
  237. package/dist/vite.d.mts +59 -0
  238. package/dist/vite.mjs +68 -0
  239. package/lib/config.d.mts +9 -0
  240. package/lib/config.mjs +5 -0
  241. package/lib/deps/h3.d.mts +1 -0
  242. package/lib/deps/h3.mjs +1 -0
  243. package/lib/deps/ofetch.mjs +1 -0
  244. package/lib/deps/ofetch.mts +1 -0
  245. package/lib/meta.d.mts +5 -0
  246. package/lib/meta.mjs +21 -0
  247. package/lib/runtime-meta.d.mts +5 -0
  248. package/lib/runtime-meta.mjs +38 -0
  249. package/package.json +170 -38
  250. package/.editorconfig +0 -12
  251. package/.eslintrc.js +0 -34
  252. package/.travis.yml +0 -7
  253. package/Dockerfile +0 -12
  254. package/Makefile +0 -37
  255. package/bin/nitro +0 -55
  256. package/lib/auto-require.js +0 -44
  257. package/lib/class-file.js +0 -105
  258. package/lib/class-files.js +0 -212
  259. package/lib/color.js +0 -89
  260. package/lib/cwd.js +0 -27
  261. package/lib/dir-expand.js +0 -80
  262. package/lib/dir.js +0 -130
  263. package/lib/file.js +0 -80
  264. package/lib/join-paths.js +0 -60
  265. package/lib/livereload/livereload.js +0 -55
  266. package/lib/livereload.js +0 -86
  267. package/lib/nitro.js +0 -161
  268. package/lib/package.js +0 -211
  269. package/lib/presets/autoprefixer.js +0 -11
  270. package/lib/presets/babel.js +0 -33
  271. package/lib/presets/browserify.js +0 -106
  272. package/lib/presets/coffee-script.js +0 -29
  273. package/lib/presets/cssnano.js +0 -11
  274. package/lib/presets/eslint.js +0 -67
  275. package/lib/presets/jshint.js +0 -51
  276. package/lib/presets/less.js +0 -45
  277. package/lib/presets/log.js +0 -11
  278. package/lib/presets/ng-annotate.js +0 -34
  279. package/lib/presets/noop.js +0 -7
  280. package/lib/presets/postcss/auto-postcss.js +0 -46
  281. package/lib/presets/postcss/autoprefixer-options.js +0 -9
  282. package/lib/presets/postcss.js +0 -40
  283. package/lib/presets/rollup.js +0 -56
  284. package/lib/presets/sass.js +0 -64
  285. package/lib/presets/sitemap.js +0 -101
  286. package/lib/presets/sitemap.xml +0 -12
  287. package/lib/presets/svg-symbols.js +0 -22
  288. package/lib/presets/svgo.js +0 -21
  289. package/lib/presets/uglify.js +0 -72
  290. package/lib/presets/webpack.js +0 -66
  291. package/lib/processors.js +0 -84
  292. package/lib/promisify.js +0 -16
  293. package/lib/server.js +0 -224
  294. package/lib/tasks.js +0 -114
  295. package/lib/watch.js +0 -81
  296. package/test.js +0 -59
  297. package/tests/cwd-tests.js +0 -25
  298. package/tests/dir-tests.js +0 -80
  299. package/tests/dummy/dummy.coffee +0 -7
  300. package/tests/dummy/dummy.js +0 -8
  301. package/tests/dummy/dummy.less +0 -6
  302. package/tests/dummy/dummy.sass +0 -5
  303. package/tests/file-tests.js +0 -17
  304. package/tests/join-paths-tests.js +0 -86
  305. package/tests/tasks-tests.js +0 -65
  306. 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,2 @@
1
+ import type { NitroApp } from "nitro/types";
2
+ export declare function useNitroApp(): NitroApp;
@@ -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,2 @@
1
+ import type { Database } from "db0";
2
+ export declare function useDatabase(name?: string): Database;
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: NitroAppPlugin;
2
+ export default _default;
@@ -0,0 +1,5 @@
1
+ import { createDebugger } from "hookable";
2
+ import { defineNitroPlugin } from "./plugin.mjs";
3
+ export default defineNitroPlugin((nitro) => {
4
+ createDebugger(nitro.hooks, { tag: "nitro-runtime" });
5
+ });
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;