nitro 3.0.1-alpha.2 → 3.0.260311-beta

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 (255) hide show
  1. package/LICENSE +6 -0
  2. package/README.md +2 -2
  3. package/dist/THIRD-PARTY-LICENSES.md +1646 -0
  4. package/dist/_build/common.mjs +6977 -2556
  5. package/dist/_build/rolldown.mjs +32 -31
  6. package/dist/_build/rollup.mjs +28 -35
  7. package/dist/_build/vite.build.mjs +10 -8
  8. package/dist/_chunks/nitro.mjs +80 -112
  9. package/dist/_chunks/nitro2.mjs +8 -7
  10. package/dist/_chunks/nitro3.mjs +129 -0
  11. package/dist/_chunks/utils.mjs +42 -24
  12. package/dist/_common.mjs +14 -24
  13. package/dist/{_chunks/dev.mjs → _dev.mjs} +132 -320
  14. package/dist/_libs/_.mjs +3 -0
  15. package/dist/_libs/_2.mjs +2 -0
  16. package/dist/_libs/_3.mjs +3 -0
  17. package/dist/_libs/_4.mjs +2 -0
  18. package/dist/_libs/_5.mjs +3 -0
  19. package/dist/_libs/_6.mjs +4 -0
  20. package/dist/_libs/_7.mjs +5 -0
  21. package/dist/_libs/c12+rc9.mjs +812 -0
  22. package/dist/_libs/{c12+giget+readdirp+chokidar.d.mts → c12+readdirp+chokidar.d.mts} +52 -36
  23. package/dist/_libs/citty.mjs +10 -12
  24. package/dist/_libs/commondir+is-reference.mjs +60 -49
  25. package/dist/_libs/compatx.mjs +1 -2
  26. package/dist/_libs/esbuild.d.mts +1 -1
  27. package/dist/_libs/escape-string-regexp.mjs +1 -2
  28. package/dist/_libs/{pluginutils+plugin-commonjs.d.mts → estree+pluginutils.d.mts} +2 -2
  29. package/dist/_libs/estree-walker.mjs +1 -4
  30. package/dist/_libs/exsolve.d.mts +1 -0
  31. package/dist/_libs/hasown+resolve+deepmerge.mjs +146 -169
  32. package/dist/_libs/httpxy.d.mts +12 -9
  33. package/dist/_libs/httpxy.mjs +299 -43
  34. package/dist/_libs/js-tokens.d.mts +1 -0
  35. package/dist/_libs/klona.mjs +1 -2
  36. package/dist/_libs/mlly.d.mts +1 -1
  37. package/dist/_libs/nypm+tinyexec.mjs +845 -0
  38. package/dist/_libs/perfect-debounce.mjs +88 -0
  39. package/dist/_libs/plugin-alias.mjs +2 -4
  40. package/dist/_libs/plugin-inject.mjs +2 -6
  41. package/dist/_libs/plugin-json.mjs +2 -4
  42. package/dist/_libs/pluginutils.mjs +5 -11
  43. package/dist/_libs/readdirp+chokidar.mjs +187 -192
  44. package/dist/_libs/remapping.mjs +1 -3
  45. package/dist/_libs/resolve-uri+gen-mapping.mjs +15 -21
  46. package/dist/_libs/rou3.d.mts +3 -2
  47. package/dist/_libs/rou3.mjs +225 -51
  48. package/dist/_libs/std-env.d.mts +10 -2
  49. package/dist/_libs/strip-literal.d.mts +1 -0
  50. package/dist/_libs/tsconfck.mjs +21 -40
  51. package/dist/_libs/ultrahtml.mjs +3 -14
  52. package/dist/_libs/unimport+unplugin.mjs +250 -351
  53. package/dist/_libs/unplugin+unimport.d.mts +7 -10
  54. package/dist/_presets.mjs +389 -432
  55. package/dist/builder.d.mts +5 -4
  56. package/dist/builder.mjs +12 -6
  57. package/dist/cli/_chunks/build.mjs +22 -23
  58. package/dist/cli/_chunks/common.mjs +1 -2
  59. package/dist/cli/_chunks/deploy.mjs +40 -0
  60. package/dist/cli/_chunks/dev.mjs +9 -9
  61. package/dist/cli/_chunks/docs.mjs +30 -0
  62. package/dist/cli/_chunks/list.mjs +4 -6
  63. package/dist/cli/_chunks/prepare.mjs +2 -4
  64. package/dist/cli/_chunks/preview.mjs +51 -0
  65. package/dist/cli/_chunks/run.mjs +7 -9
  66. package/dist/cli/_chunks/task.mjs +1 -3
  67. package/dist/cli/index.mjs +5 -4
  68. package/dist/node_modules/@poppinss/dumper/build/formatters/html/main.js +8 -0
  69. package/dist/node_modules/@poppinss/dumper/package.json +1 -1
  70. package/dist/node_modules/croner/dist/croner.js +1 -1
  71. package/dist/node_modules/croner/package.json +11 -1
  72. package/dist/node_modules/youch/build/helpers-B9BQYaS6.js +23 -0
  73. package/dist/node_modules/youch/build/index.js +204 -351
  74. package/dist/node_modules/youch/build/public/error_info/script.js +1 -1
  75. package/dist/node_modules/youch/build/public_dir-C5bujZKB.js +25 -0
  76. package/dist/node_modules/youch/build/src/templates/error_cause/main.js +44 -0
  77. package/dist/node_modules/youch/build/src/templates/error_info/main.js +72 -0
  78. package/dist/node_modules/youch/build/src/templates/error_metadata/main.js +65 -0
  79. package/dist/node_modules/youch/build/src/templates/error_stack/main.js +152 -0
  80. package/dist/node_modules/youch/build/src/templates/error_stack_source/main.js +46 -0
  81. package/dist/node_modules/youch/build/src/templates/header/main.js +24 -0
  82. package/dist/node_modules/youch/build/src/templates/layout/main.js +27 -0
  83. package/dist/node_modules/youch/package.json +34 -31
  84. package/dist/presets/_nitro/runtime/nitro-dev.mjs +9 -56
  85. package/dist/presets/_nitro/runtime/nitro-prerenderer.mjs +1 -0
  86. package/dist/presets/_nitro/runtime/service-worker.mjs +0 -1
  87. package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.mjs +3 -1
  88. package/dist/presets/bun/runtime/bun.mjs +4 -3
  89. package/dist/presets/deno/runtime/deno-server.mjs +4 -3
  90. package/dist/presets/netlify/runtime/netlify-edge.mjs +0 -1
  91. package/dist/presets/netlify/runtime/netlify.mjs +1 -1
  92. package/dist/presets/node/runtime/node-cluster.mjs +3 -2
  93. package/dist/presets/node/runtime/node-server.mjs +3 -2
  94. package/dist/presets/stormkit/runtime/stormkit.mjs +0 -1
  95. package/dist/presets/vercel/runtime/cron-handler.mjs +25 -0
  96. package/dist/presets/vercel/runtime/vercel.web.mjs +2 -4
  97. package/dist/presets/zephyr/runtime/server.mjs +3 -0
  98. package/dist/runtime/internal/cache.d.mts +0 -4
  99. package/dist/runtime/internal/cache.mjs +32 -256
  100. package/dist/runtime/internal/context.d.mts +3 -1
  101. package/dist/runtime/internal/error/dev.d.mts +5 -4
  102. package/dist/runtime/internal/error/dev.mjs +38 -40
  103. package/dist/runtime/internal/error/hooks.d.mts +1 -1
  104. package/dist/runtime/internal/error/prod.d.mts +2 -5
  105. package/dist/runtime/internal/error/prod.mjs +19 -33
  106. package/dist/runtime/internal/error/utils.d.mts +4 -4
  107. package/dist/runtime/internal/meta.d.mts +1 -1
  108. package/dist/runtime/internal/plugin.d.mts +2 -2
  109. package/dist/runtime/internal/route-rules.d.mts +9 -4
  110. package/dist/runtime/internal/route-rules.mjs +9 -1
  111. package/dist/runtime/internal/routes/dev-tasks.d.mts +3 -2
  112. package/dist/runtime/internal/routes/dev-tasks.mjs +6 -2
  113. package/dist/runtime/internal/routes/renderer-template.d.mts +1 -1
  114. package/dist/runtime/internal/routes/renderer-template.dev.d.mts +2 -1
  115. package/dist/runtime/internal/runtime-config.d.mts +1 -1
  116. package/dist/runtime/internal/static.mjs +5 -4
  117. package/dist/runtime/internal/task.d.mts +3 -1
  118. package/dist/runtime/internal/task.mjs +2 -2
  119. package/dist/runtime/internal/vite/dev-entry.mjs +6 -3
  120. package/dist/runtime/internal/vite/dev-worker.mjs +264 -0
  121. package/dist/runtime/meta.mjs +1 -0
  122. package/dist/runtime/nitro.d.mts +2 -0
  123. package/dist/runtime/nitro.mjs +2 -0
  124. package/dist/runtime/virtual/error-handler.mjs +1 -1
  125. package/dist/runtime/virtual/server-assets.d.mts +2 -1
  126. package/dist/runtime/vite.d.mts +1 -1
  127. package/dist/types/index.d.mts +68 -117
  128. package/dist/types/index.mjs +1 -11
  129. package/dist/vite.d.mts +3 -3
  130. package/dist/vite.mjs +236 -233
  131. package/package.json +83 -68
  132. package/skills/nitro/SKILL.md +10 -0
  133. package/skills/nitro/docs/TOC.md +83 -0
  134. package/skills/nitro/docs/_navigation.json +679 -0
  135. package/skills/nitro/docs/config/index.md +1147 -0
  136. package/skills/nitro/docs/deploy/index.md +64 -0
  137. package/skills/nitro/docs/deploy/providers/alwaysdata.md +50 -0
  138. package/skills/nitro/docs/deploy/providers/aws-amplify.md +81 -0
  139. package/skills/nitro/docs/deploy/providers/aws.md +47 -0
  140. package/skills/nitro/docs/deploy/providers/azure.md +72 -0
  141. package/skills/nitro/docs/deploy/providers/cleavr.md +33 -0
  142. package/skills/nitro/docs/deploy/providers/cloudflare.md +375 -0
  143. package/skills/nitro/docs/deploy/providers/deno-deploy.md +66 -0
  144. package/skills/nitro/docs/deploy/providers/digitalocean.md +46 -0
  145. package/skills/nitro/docs/deploy/providers/firebase.md +32 -0
  146. package/skills/nitro/docs/deploy/providers/flightcontrol.md +67 -0
  147. package/skills/nitro/docs/deploy/providers/genezio.md +67 -0
  148. package/skills/nitro/docs/deploy/providers/github-pages.md +68 -0
  149. package/skills/nitro/docs/deploy/providers/gitlab-pages.md +37 -0
  150. package/skills/nitro/docs/deploy/providers/heroku.md +84 -0
  151. package/skills/nitro/docs/deploy/providers/iis.md +38 -0
  152. package/skills/nitro/docs/deploy/providers/koyeb.md +109 -0
  153. package/skills/nitro/docs/deploy/providers/netlify.md +47 -0
  154. package/skills/nitro/docs/deploy/providers/platform-sh.md +37 -0
  155. package/skills/nitro/docs/deploy/providers/render.md +37 -0
  156. package/skills/nitro/docs/deploy/providers/stormkit.md +24 -0
  157. package/skills/nitro/docs/deploy/providers/vercel.md +179 -0
  158. package/skills/nitro/docs/deploy/providers/zeabur.md +19 -0
  159. package/skills/nitro/docs/deploy/providers/zephyr.md +99 -0
  160. package/skills/nitro/docs/deploy/providers/zerops.md +102 -0
  161. package/skills/nitro/docs/deploy/runtimes/bun.md +15 -0
  162. package/skills/nitro/docs/deploy/runtimes/deno.md +19 -0
  163. package/skills/nitro/docs/deploy/runtimes/node.md +63 -0
  164. package/skills/nitro/docs/docs/assets.md +166 -0
  165. package/skills/nitro/docs/docs/cache.md +408 -0
  166. package/skills/nitro/docs/docs/configuration.md +256 -0
  167. package/skills/nitro/docs/docs/database.md +185 -0
  168. package/skills/nitro/docs/docs/index.md +56 -0
  169. package/skills/nitro/docs/docs/lifecycle.md +204 -0
  170. package/skills/nitro/docs/docs/migration.md +215 -0
  171. package/skills/nitro/docs/docs/nightly.md +31 -0
  172. package/skills/nitro/docs/docs/plugins.md +164 -0
  173. package/skills/nitro/docs/docs/quick-start.md +110 -0
  174. package/skills/nitro/docs/docs/renderer.md +322 -0
  175. package/skills/nitro/docs/docs/routing.md +711 -0
  176. package/skills/nitro/docs/docs/server-entry.md +239 -0
  177. package/skills/nitro/docs/docs/storage.md +261 -0
  178. package/skills/nitro/docs/docs/tasks.md +277 -0
  179. package/skills/nitro/docs/examples/api-routes.md +145 -0
  180. package/skills/nitro/docs/examples/auto-imports.md +93 -0
  181. package/skills/nitro/docs/examples/cached-handler.md +81 -0
  182. package/skills/nitro/docs/examples/custom-error-handler.md +98 -0
  183. package/skills/nitro/docs/examples/database.md +157 -0
  184. package/skills/nitro/docs/examples/elysia.md +70 -0
  185. package/skills/nitro/docs/examples/express.md +78 -0
  186. package/skills/nitro/docs/examples/fastify.md +79 -0
  187. package/skills/nitro/docs/examples/hello-world.md +68 -0
  188. package/skills/nitro/docs/examples/hono.md +74 -0
  189. package/skills/nitro/docs/examples/import-alias.md +100 -0
  190. package/skills/nitro/docs/examples/index.md +3 -0
  191. package/skills/nitro/docs/examples/middleware.md +91 -0
  192. package/skills/nitro/docs/examples/mono-jsx.md +68 -0
  193. package/skills/nitro/docs/examples/nano-jsx.md +70 -0
  194. package/skills/nitro/docs/examples/plugins.md +91 -0
  195. package/skills/nitro/docs/examples/renderer.md +113 -0
  196. package/skills/nitro/docs/examples/runtime-config.md +97 -0
  197. package/skills/nitro/docs/examples/server-fetch.md +87 -0
  198. package/skills/nitro/docs/examples/shiki.md +198 -0
  199. package/skills/nitro/docs/examples/virtual-routes.md +74 -0
  200. package/skills/nitro/docs/examples/vite-nitro-plugin.md +79 -0
  201. package/skills/nitro/docs/examples/vite-rsc.md +1041 -0
  202. package/skills/nitro/docs/examples/vite-ssr-html.md +217 -0
  203. package/skills/nitro/docs/examples/vite-ssr-preact.md +247 -0
  204. package/skills/nitro/docs/examples/vite-ssr-react.md +226 -0
  205. package/skills/nitro/docs/examples/vite-ssr-solid.md +256 -0
  206. package/skills/nitro/docs/examples/vite-ssr-tsr-react.md +442 -0
  207. package/skills/nitro/docs/examples/vite-ssr-tss-react.md +444 -0
  208. package/skills/nitro/docs/examples/vite-ssr-vue-router.md +616 -0
  209. package/skills/nitro/docs/examples/vite-trpc.md +335 -0
  210. package/skills/nitro/docs/examples/websocket.md +282 -0
  211. package/skills/nitro/docs/index.md +121 -0
  212. package/dist/_libs/confbox.mjs +0 -2509
  213. package/dist/_libs/nypm+giget+tinyexec.mjs +0 -3603
  214. package/dist/_libs/rc9+c12+dotenv.mjs +0 -972
  215. package/dist/node_modules/hookable/dist/index.mjs +0 -266
  216. package/dist/node_modules/hookable/package.json +0 -52
  217. package/dist/node_modules/youch/build/chunk-4L7RY2JA.js +0 -42
  218. package/dist/node_modules/youch/build/chunk-7QV3D5YX.js +0 -83
  219. package/dist/node_modules/youch/build/chunk-AUGPHE32.js +0 -39
  220. package/dist/node_modules/youch/build/chunk-CM7DWJNZ.js +0 -44
  221. package/dist/node_modules/youch/build/chunk-EJH674NB.js +0 -223
  222. package/dist/node_modules/youch/build/chunk-OIJ3WD7L.js +0 -81
  223. package/dist/node_modules/youch/build/chunk-P36L72PL.js +0 -87
  224. package/dist/node_modules/youch/build/chunk-PE3GG3TN.js +0 -65
  225. package/dist/node_modules/youch/build/chunk-X53OIOJH.js +0 -70
  226. package/dist/presets/_nitro/runtime/nitro-dev.d.mts +0 -2
  227. package/dist/presets/_nitro/runtime/nitro-prerenderer.d.mts +0 -3
  228. package/dist/presets/_nitro/runtime/service-worker.d.mts +0 -2
  229. package/dist/presets/aws-amplify/runtime/aws-amplify.d.mts +0 -2
  230. package/dist/presets/aws-lambda/runtime/_utils.d.mts +0 -8
  231. package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.d.mts +0 -2
  232. package/dist/presets/aws-lambda/runtime/aws-lambda.d.mts +0 -3
  233. package/dist/presets/azure/runtime/_utils.d.mts +0 -2
  234. package/dist/presets/azure/runtime/azure-swa.d.mts +0 -5
  235. package/dist/presets/bun/runtime/bun.d.mts +0 -3
  236. package/dist/presets/cloudflare/runtime/_module-handler.d.mts +0 -16
  237. package/dist/presets/cloudflare/runtime/cloudflare-durable.d.mts +0 -11
  238. package/dist/presets/cloudflare/runtime/cloudflare-module.d.mts +0 -3
  239. package/dist/presets/cloudflare/runtime/cloudflare-pages.d.mts +0 -20
  240. package/dist/presets/cloudflare/runtime/plugin.dev.d.mts +0 -3
  241. package/dist/presets/deno/runtime/deno-deploy.d.mts +0 -5
  242. package/dist/presets/deno/runtime/deno-server.d.mts +0 -3
  243. package/dist/presets/netlify/runtime/netlify-edge.d.mts +0 -3
  244. package/dist/presets/netlify/runtime/netlify.d.mts +0 -4
  245. package/dist/presets/node/runtime/node-cluster.d.mts +0 -3
  246. package/dist/presets/node/runtime/node-middleware.d.mts +0 -3
  247. package/dist/presets/node/runtime/node-server.d.mts +0 -3
  248. package/dist/presets/standard/runtime/server.d.mts +0 -3
  249. package/dist/presets/stormkit/runtime/stormkit.d.mts +0 -21
  250. package/dist/presets/vercel/runtime/isr.d.mts +0 -2
  251. package/dist/presets/vercel/runtime/vercel.node.d.mts +0 -3
  252. package/dist/presets/vercel/runtime/vercel.web.d.mts +0 -8
  253. package/dist/presets/winterjs/runtime/winterjs.d.mts +0 -2
  254. package/dist/presets/zeabur/runtime/zeabur.d.mts +0 -3
  255. package/dist/runtime/internal/vite/node-runner.mjs +0 -359
@@ -1,11 +1,12 @@
1
- import http, { IncomingMessage, OutgoingMessage } from "node:http";
2
1
  import { EventEmitter } from "node:events";
3
2
  import * as stream from "node:stream";
3
+ import http, { IncomingMessage } from "node:http";
4
+ import { Socket } from "node:net";
4
5
 
5
- //#region node_modules/.pnpm/httpxy@0.1.7/node_modules/httpxy/dist/index.d.ts
6
+ //#region node_modules/.pnpm/httpxy@0.3.1/node_modules/httpxy/dist/index.d.ts
6
7
  interface ProxyTargetDetailed {
7
- host: string;
8
- port: number;
8
+ host?: string;
9
+ port?: number | string;
9
10
  protocol?: string;
10
11
  hostname?: string;
11
12
  socketPath?: string;
@@ -17,13 +18,13 @@ interface ProxyTargetDetailed {
17
18
  ciphers?: string;
18
19
  secureProtocol?: string;
19
20
  }
20
- type ProxyTarget = ProxyTargetUrl | ProxyTargetDetailed;
21
- type ProxyTargetUrl = string | Partial<URL>;
21
+ type ProxyTarget = string | URL | ProxyTargetDetailed;
22
+ /** Resolved proxy address — either TCP (host + port) or Unix socket. */
22
23
  interface ProxyServerOptions {
23
- /** URL string to be parsed with the url module. */
24
+ /** URL string to be parsed. */
24
25
  target?: ProxyTarget;
25
- /** URL string to be parsed with the url module. */
26
- forward?: ProxyTargetUrl;
26
+ /** URL string to be parsed. */
27
+ forward?: ProxyTarget;
27
28
  /** Object to be passed to http(s).request. */
28
29
  agent?: any;
29
30
  /** Object to be passed to https.createServer(). */
@@ -72,6 +73,8 @@ interface ProxyServerOptions {
72
73
  timeout?: number;
73
74
  /** If set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the proxyRes event */
74
75
  selfHandleResponse?: boolean;
76
+ /** Follow HTTP redirects from target. `true` = max 5 hops; number = custom max. */
77
+ followRedirects?: boolean | number;
75
78
  /** Buffer */
76
79
  buffer?: stream.Stream;
77
80
  }
@@ -1,17 +1,12 @@
1
- import { n as __exportAll } from "../_common.mjs";
2
- import http from "node:http";
3
- import https from "node:https";
4
1
  import { EventEmitter } from "node:events";
5
-
6
- //#region node_modules/.pnpm/httpxy@0.1.7/node_modules/httpxy/dist/index.mjs
7
- var dist_exports = /* @__PURE__ */ __exportAll({
8
- ProxyServer: () => ProxyServer,
9
- createProxyServer: () => createProxyServer
10
- });
2
+ import { Readable } from "node:stream";
3
+ import http, { request } from "node:http";
4
+ import nodeHTTPS, { request as request$1 } from "node:https";
5
+ //#region node_modules/.pnpm/httpxy@0.3.1/node_modules/httpxy/dist/index.mjs
11
6
  const upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i;
12
7
  const isSSL = /^https|wss/;
13
8
  function setupOutgoing(outgoing, options, req, forward) {
14
- outgoing.port = options[forward || "target"].port || (isSSL.test(options[forward || "target"].protocol) ? 443 : 80);
9
+ outgoing.port = options[forward || "target"].port || (isSSL.test(options[forward || "target"].protocol ?? "http") ? 443 : 80);
15
10
  for (const e of [
16
11
  "host",
17
12
  "hostname",
@@ -32,7 +27,7 @@ function setupOutgoing(outgoing, options, req, forward) {
32
27
  };
33
28
  if (options.auth) outgoing.auth = options.auth;
34
29
  if (options.ca) outgoing.ca = options.ca;
35
- if (isSSL.test(options[forward || "target"].protocol)) outgoing.rejectUnauthorized = options.secure === void 0 ? true : options.secure;
30
+ if (isSSL.test(options[forward || "target"].protocol ?? "http")) outgoing.rejectUnauthorized = options.secure === void 0 ? true : options.secure;
36
31
  outgoing.agent = options.agent || false;
37
32
  outgoing.localAddress = options.localAddress;
38
33
  if (!outgoing.agent) {
@@ -40,12 +35,16 @@ function setupOutgoing(outgoing, options, req, forward) {
40
35
  if (typeof outgoing.headers.connection !== "string" || !upgradeHeader.test(outgoing.headers.connection)) outgoing.headers.connection = "close";
41
36
  }
42
37
  const target = options[forward || "target"];
43
- const targetPath = target && options.prependPath !== false ? target.pathname || target.path || "" : "";
44
- const parsed = new URL(req.url, "http://localhost");
45
- let outgoingPath = options.toProxy ? req.url : parsed.pathname + parsed.search || "";
38
+ const targetPath = target && options.prependPath !== false ? target.pathname || "" : "";
39
+ const reqUrl = req.url || "";
40
+ const qIdx = reqUrl.indexOf("?");
41
+ const reqPath = qIdx === -1 ? reqUrl : reqUrl.slice(0, qIdx);
42
+ const reqSearch = qIdx === -1 ? "" : reqUrl.slice(qIdx);
43
+ const normalizedPath = reqPath ? reqPath[0] === "/" ? reqPath : "/" + reqPath : "/";
44
+ let outgoingPath = options.toProxy ? "/" + reqUrl : normalizedPath + reqSearch;
46
45
  outgoingPath = options.ignorePath ? "" : outgoingPath;
47
46
  outgoing.path = joinURL(targetPath, outgoingPath);
48
- if (options.changeOrigin) outgoing.headers.host = requiresPort(outgoing.port, options[forward || "target"].protocol) && !hasPort(outgoing.host) ? outgoing.host + ":" + outgoing.port : outgoing.host;
47
+ if (options.changeOrigin) outgoing.headers.host = requiresPort(outgoing.port, options[forward || "target"].protocol) && !hasPort(outgoing.host) ? outgoing.host + ":" + outgoing.port : outgoing.host ?? void 0;
49
48
  return outgoing;
50
49
  }
51
50
  function joinURL(base, path) {
@@ -83,11 +82,23 @@ function rewriteCookieProperty(header, config, property) {
83
82
  return newValue ? prefix + newValue : "";
84
83
  });
85
84
  }
85
+ function parseAddr(addr) {
86
+ if (typeof addr === "string") {
87
+ if (addr.startsWith("unix:")) return { socketPath: addr.slice(5) };
88
+ const url = new URL(addr);
89
+ return {
90
+ host: url.hostname,
91
+ port: Number(url.port) || (isSSL.test(url.protocol) ? 443 : 80)
92
+ };
93
+ }
94
+ if (!addr.socketPath && !addr.port) throw new Error("ProxyAddr must have either `port` or `socketPath`");
95
+ return addr;
96
+ }
86
97
  function hasPort(host) {
87
- return !!~host.indexOf(":");
98
+ return host ? !!~host.indexOf(":") : false;
88
99
  }
89
100
  function requiresPort(_port, _protocol) {
90
- const protocol = _protocol.split(":")[0];
101
+ const protocol = _protocol?.split(":")[0];
91
102
  const port = +_port;
92
103
  if (!port) return false;
93
104
  switch (protocol) {
@@ -118,18 +129,18 @@ const webOutgoingMiddleware = [
118
129
  }),
119
130
  defineProxyOutgoingMiddleware((req, res, proxyRes, options) => {
120
131
  if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite) && proxyRes.headers.location && redirectRegex.test(String(proxyRes.statusCode))) {
121
- const target = new URL(options.target);
132
+ const target = options.target instanceof URL ? options.target : new URL(options.target);
122
133
  const u = new URL(proxyRes.headers.location);
123
134
  if (target.host !== u.host) return;
124
135
  if (options.hostRewrite) u.host = options.hostRewrite;
125
- else if (options.autoRewrite) u.host = req.headers.host;
136
+ else if (options.autoRewrite && req.headers.host) u.host = req.headers.host;
126
137
  if (options.protocolRewrite) u.protocol = options.protocolRewrite;
127
138
  proxyRes.headers.location = u.toString();
128
139
  }
129
140
  }),
130
141
  defineProxyOutgoingMiddleware((req, res, proxyRes, options) => {
131
- let rewriteCookieDomainConfig = options.cookieDomainRewrite;
132
- let rewriteCookiePathConfig = options.cookiePathRewrite;
142
+ const rewriteCookieDomainConfig = typeof options.cookieDomainRewrite === "string" ? { "*": options.cookieDomainRewrite } : options.cookieDomainRewrite;
143
+ const rewriteCookiePathConfig = typeof options.cookiePathRewrite === "string" ? { "*": options.cookiePathRewrite } : options.cookiePathRewrite;
133
144
  const preserveHeaderKeyCase = options.preserveHeaderKeyCase;
134
145
  let rawHeaderKeyMap;
135
146
  const setHeader = function(key, header) {
@@ -138,8 +149,6 @@ const webOutgoingMiddleware = [
138
149
  if (rewriteCookiePathConfig && key.toLowerCase() === "set-cookie") header = rewriteCookieProperty(header, rewriteCookiePathConfig, "path");
139
150
  res.setHeader(String(key).trim(), header);
140
151
  };
141
- if (typeof rewriteCookieDomainConfig === "string") rewriteCookieDomainConfig = { "*": rewriteCookieDomainConfig };
142
- if (typeof rewriteCookiePathConfig === "string") rewriteCookiePathConfig = { "*": rewriteCookiePathConfig };
143
152
  if (preserveHeaderKeyCase && proxyRes.rawHeaders !== void 0) {
144
153
  rawHeaderKeyMap = {};
145
154
  for (let i = 0; i < proxyRes.rawHeaders.length; i += 2) {
@@ -162,8 +171,15 @@ const webOutgoingMiddleware = [
162
171
  ];
163
172
  const nativeAgents = {
164
173
  http,
165
- https
174
+ https: nodeHTTPS
166
175
  };
176
+ const redirectStatuses = /* @__PURE__ */ new Set([
177
+ 301,
178
+ 302,
179
+ 303,
180
+ 307,
181
+ 308
182
+ ]);
167
183
  const webIncomingMiddleware = [
168
184
  defineProxyMiddleware((req) => {
169
185
  if ((req.method === "DELETE" || req.method === "OPTIONS") && !req.headers["content-length"]) {
@@ -172,7 +188,9 @@ const webIncomingMiddleware = [
172
188
  }
173
189
  }),
174
190
  defineProxyMiddleware((req, res, options) => {
175
- if (options.timeout) req.socket.setTimeout(options.timeout);
191
+ if (options.timeout) req.socket.setTimeout(options.timeout, () => {
192
+ req.socket.destroy();
193
+ });
176
194
  }),
177
195
  defineProxyMiddleware((req, res, options) => {
178
196
  if (!options.xfwd) return;
@@ -191,11 +209,11 @@ const webIncomingMiddleware = [
191
209
  }),
192
210
  defineProxyMiddleware((req, res, options, server, head, callback) => {
193
211
  server.emit("start", req, res, options.target || options.forward);
194
- const agents = nativeAgents;
195
- const http$1 = agents.http;
196
- const https$1 = agents.https;
212
+ const http = nativeAgents.http;
213
+ const https = nativeAgents.https;
214
+ const maxRedirects = typeof options.followRedirects === "number" ? options.followRedirects : options.followRedirects ? 5 : 0;
197
215
  if (options.forward) {
198
- const forwardReq = (options.forward.protocol === "https:" ? https$1 : http$1).request(setupOutgoing(options.ssl || {}, options, req, "forward"));
216
+ const forwardReq = (options.forward.protocol === "https:" ? https : http).request(setupOutgoing(options.ssl || {}, options, req, "forward"));
199
217
  const forwardError = createErrorHandler(forwardReq, options.forward);
200
218
  req.on("error", forwardError);
201
219
  forwardReq.on("error", forwardError);
@@ -205,8 +223,8 @@ const webIncomingMiddleware = [
205
223
  return;
206
224
  }
207
225
  }
208
- const proxyReq = (options.target.protocol === "https:" ? https$1 : http$1).request(setupOutgoing(options.ssl || {}, options, req));
209
- proxyReq.on("socket", (socket) => {
226
+ const proxyReq = (options.target.protocol === "https:" ? https : http).request(setupOutgoing(options.ssl || {}, options, req));
227
+ proxyReq.on("socket", (_socket) => {
210
228
  if (server && !proxyReq.getHeader("expect")) server.emit("proxyReq", proxyReq, req, res, options);
211
229
  });
212
230
  if (options.proxyTimeout) proxyReq.setTimeout(options.proxyTimeout, function() {
@@ -215,6 +233,9 @@ const webIncomingMiddleware = [
215
233
  req.on("aborted", function() {
216
234
  proxyReq.abort();
217
235
  });
236
+ res.on("close", function() {
237
+ if (!res.writableFinished) proxyReq.destroy();
238
+ });
218
239
  const proxyError = createErrorHandler(proxyReq, options.target);
219
240
  req.on("error", proxyError);
220
241
  proxyReq.on("error", proxyError);
@@ -228,8 +249,66 @@ const webIncomingMiddleware = [
228
249
  else server.emit("error", err, req, res, url);
229
250
  };
230
251
  }
231
- (options.buffer || req).pipe(proxyReq);
232
- proxyReq.on("response", function(proxyRes) {
252
+ let bodyBuffer;
253
+ if (maxRedirects > 0) {
254
+ const chunks = [];
255
+ const source = options.buffer || req;
256
+ source.on("data", (chunk) => {
257
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
258
+ proxyReq.write(chunk);
259
+ });
260
+ source.on("end", () => {
261
+ bodyBuffer = Buffer.concat(chunks);
262
+ proxyReq.end();
263
+ });
264
+ source.on("error", (err) => {
265
+ proxyReq.destroy(err);
266
+ });
267
+ } else (options.buffer || req).pipe(proxyReq);
268
+ function handleResponse(proxyRes, redirectCount, currentUrl) {
269
+ const statusCode = proxyRes.statusCode;
270
+ if (maxRedirects > 0 && redirectStatuses.has(statusCode) && redirectCount < maxRedirects && proxyRes.headers.location) {
271
+ proxyRes.resume();
272
+ const location = new URL(proxyRes.headers.location, currentUrl);
273
+ const preserveMethod = statusCode === 307 || statusCode === 308;
274
+ const redirectMethod = preserveMethod ? req.method || "GET" : "GET";
275
+ const isHTTPS = location.protocol === "https:";
276
+ const agent = isHTTPS ? https : http;
277
+ const redirectHeaders = { ...req.headers };
278
+ if (options.headers) Object.assign(redirectHeaders, options.headers);
279
+ redirectHeaders.host = location.host;
280
+ if (location.host !== currentUrl.host) {
281
+ delete redirectHeaders.authorization;
282
+ delete redirectHeaders.cookie;
283
+ }
284
+ if (!preserveMethod) {
285
+ delete redirectHeaders["content-length"];
286
+ delete redirectHeaders["content-type"];
287
+ delete redirectHeaders["transfer-encoding"];
288
+ }
289
+ const redirectOpts = {
290
+ hostname: location.hostname,
291
+ port: location.port || (isHTTPS ? 443 : 80),
292
+ path: location.pathname + location.search,
293
+ method: redirectMethod,
294
+ headers: redirectHeaders,
295
+ agent: options.agent || false
296
+ };
297
+ if (isHTTPS) redirectOpts.rejectUnauthorized = options.secure === void 0 ? true : options.secure;
298
+ const redirectReq = agent.request(redirectOpts);
299
+ if (server && !redirectReq.getHeader("expect")) server.emit("proxyReq", redirectReq, req, res, options);
300
+ if (options.proxyTimeout) redirectReq.setTimeout(options.proxyTimeout, () => {
301
+ redirectReq.abort();
302
+ });
303
+ const redirectError = createErrorHandler(redirectReq, location);
304
+ redirectReq.on("error", redirectError);
305
+ redirectReq.on("response", (nextRes) => {
306
+ handleResponse(nextRes, redirectCount + 1, location);
307
+ });
308
+ if (preserveMethod && bodyBuffer && bodyBuffer.length > 0) redirectReq.end(bodyBuffer);
309
+ else redirectReq.end();
310
+ return;
311
+ }
233
312
  if (server) server.emit("proxyRes", proxyRes, req, res);
234
313
  if (!res.headersSent && !options.selfHandleResponse) {
235
314
  for (const pass of webOutgoingMiddleware) if (pass(req, res, proxyRes, options)) break;
@@ -245,6 +324,9 @@ const webIncomingMiddleware = [
245
324
  });
246
325
  if (!options.selfHandleResponse) proxyRes.pipe(res);
247
326
  }
327
+ }
328
+ proxyReq.on("response", function(proxyRes) {
329
+ handleResponse(proxyRes, 0, options.target);
248
330
  });
249
331
  })
250
332
  ];
@@ -286,7 +368,8 @@ const websocketIncomingMiddleware = [
286
368
  };
287
369
  setupSocket(socket);
288
370
  if (head && head.length > 0) socket.unshift(head);
289
- const proxyReq = (isSSL.test(options.target.protocol) ? https : http).request(setupOutgoing(options.ssl || {}, options, req));
371
+ socket.on("error", onSocketError);
372
+ const proxyReq = (isSSL.test(options.target.protocol || "http") ? nodeHTTPS : http).request(setupOutgoing(options.ssl || {}, options, req));
290
373
  if (server) server.emit("proxyReqWs", proxyReq, req, socket, options, head);
291
374
  proxyReq.on("error", onOutgoingError);
292
375
  proxyReq.on("response", function(res) {
@@ -311,6 +394,11 @@ const websocketIncomingMiddleware = [
311
394
  server.emit("proxySocket", proxySocket);
312
395
  });
313
396
  proxyReq.end();
397
+ function onSocketError(err) {
398
+ if (callback) callback(err, req, socket);
399
+ else server.emit("error", err, req, socket);
400
+ proxyReq.destroy();
401
+ }
314
402
  function onOutgoingError(err) {
315
403
  if (callback) callback(err, req, socket);
316
404
  else server.emit("error", err, req, socket);
@@ -345,9 +433,9 @@ var ProxyServer = class extends EventEmitter {
345
433
  const closure = (req, res) => {
346
434
  this.web(req, res);
347
435
  };
348
- this._server = this.options.ssl ? https.createServer(this.options.ssl, closure) : http.createServer(closure);
436
+ this._server = this.options.ssl ? nodeHTTPS.createServer(this.options.ssl, closure) : http.createServer(closure);
349
437
  if (this.options.ws) this._server.on("upgrade", (req, socket, head) => {
350
- this._ws(req, socket, head);
438
+ this.ws(req, socket, head).catch(() => {});
351
439
  });
352
440
  this._server.listen(port, hostname);
353
441
  return this;
@@ -363,7 +451,7 @@ var ProxyServer = class extends EventEmitter {
363
451
  }
364
452
  before(type, passName, pass) {
365
453
  if (type !== "ws" && type !== "web") throw new Error("type must be `web` or `ws`");
366
- const passes = type === "ws" ? this._wsPasses : this._webPasses;
454
+ const passes = this._getPasses(type);
367
455
  let i = false;
368
456
  for (const [idx, v] of passes.entries()) if (v.name === passName) i = idx;
369
457
  if (i === false) throw new Error("No such pass");
@@ -371,12 +459,16 @@ var ProxyServer = class extends EventEmitter {
371
459
  }
372
460
  after(type, passName, pass) {
373
461
  if (type !== "ws" && type !== "web") throw new Error("type must be `web` or `ws`");
374
- const passes = type === "ws" ? this._wsPasses : this._webPasses;
462
+ const passes = this._getPasses(type);
375
463
  let i = false;
376
464
  for (const [idx, v] of passes.entries()) if (v.name === passName) i = idx;
377
465
  if (i === false) throw new Error("No such pass");
378
466
  passes.splice(i++, 0, pass);
379
467
  }
468
+ /** @internal */
469
+ _getPasses(type) {
470
+ return type === "ws" ? this._wsPasses : this._webPasses;
471
+ }
380
472
  };
381
473
  function createProxyServer(options = {}) {
382
474
  return new ProxyServer(options);
@@ -388,7 +480,10 @@ function _createProxyFn(type, server) {
388
480
  ...server.options
389
481
  };
390
482
  for (const key of ["target", "forward"]) if (typeof requestOptions[key] === "string") requestOptions[key] = new URL(requestOptions[key]);
391
- if (!requestOptions.target && !requestOptions.forward) return this.emit("error", /* @__PURE__ */ new Error("Must provide a proper URL as target"));
483
+ if (!requestOptions.target && !requestOptions.forward) {
484
+ this.emit("error", /* @__PURE__ */ new Error("Must provide a proper URL as target"));
485
+ return Promise.resolve();
486
+ }
392
487
  let _resolve;
393
488
  let _reject;
394
489
  const callbackPromise = new Promise((resolve, reject) => {
@@ -401,8 +496,11 @@ function _createProxyFn(type, server) {
401
496
  res.on("error", (error) => {
402
497
  _reject(error);
403
498
  });
404
- for (const pass of type === "ws" ? server._wsPasses : server._webPasses) if (pass(req, res, requestOptions, server, head, (error) => {
405
- _reject(error);
499
+ for (const pass of server._getPasses(type)) if (pass(req, res, requestOptions, server, head, (error) => {
500
+ if (server.listenerCount("error") > 0) {
501
+ server.emit("error", error, req, res);
502
+ _resolve();
503
+ } else _reject(error);
406
504
  })) {
407
505
  _resolve();
408
506
  break;
@@ -410,6 +508,164 @@ function _createProxyFn(type, server) {
410
508
  return callbackPromise;
411
509
  };
412
510
  }
413
-
511
+ async function proxyFetch(addr, input, inputInit) {
512
+ const resolvedAddr = parseAddr(addr);
513
+ let url;
514
+ let init;
515
+ if (input instanceof Request) {
516
+ url = new URL(input.url);
517
+ init = {
518
+ ...toInit(input),
519
+ ...toInit(inputInit)
520
+ };
521
+ } else {
522
+ url = new URL(input);
523
+ init = toInit(inputInit);
524
+ }
525
+ init = {
526
+ redirect: "manual",
527
+ ...init
528
+ };
529
+ if (init.body) init.duplex = "half";
530
+ const path = url.pathname + url.search;
531
+ const reqHeaders = {};
532
+ if (init.headers) {
533
+ const h = init.headers instanceof Headers ? init.headers : new Headers(init.headers);
534
+ for (const [key, value] of h) reqHeaders[key] = value;
535
+ }
536
+ const res = await new Promise((resolve, reject) => {
537
+ const reqOpts = {
538
+ method: init.method || "GET",
539
+ path,
540
+ headers: reqHeaders
541
+ };
542
+ if (resolvedAddr.socketPath) reqOpts.socketPath = resolvedAddr.socketPath;
543
+ else {
544
+ reqOpts.hostname = resolvedAddr.host || "localhost";
545
+ reqOpts.port = resolvedAddr.port;
546
+ }
547
+ const req = request(reqOpts, resolve);
548
+ req.on("error", reject);
549
+ if (init.body instanceof ReadableStream) {
550
+ const readable = Readable.fromWeb(init.body);
551
+ readable.on("error", reject);
552
+ readable.pipe(req);
553
+ } else if (init.body) req.end(init.body);
554
+ else req.end();
555
+ });
556
+ const headers = new Headers();
557
+ for (const [key, value] of Object.entries(res.headers)) {
558
+ if (key === "transfer-encoding" || key === "keep-alive" || key === "connection") continue;
559
+ if (Array.isArray(value)) for (const v of value) headers.append(key, v);
560
+ else if (value) headers.set(key, value);
561
+ }
562
+ const hasBody = res.statusCode !== 204 && res.statusCode !== 304;
563
+ return new Response(hasBody ? Readable.toWeb(res) : null, {
564
+ status: res.statusCode,
565
+ statusText: res.statusMessage,
566
+ headers
567
+ });
568
+ }
569
+ function toInit(init) {
570
+ if (!init) return;
571
+ if (init instanceof Request) return {
572
+ method: init.method,
573
+ headers: init.headers,
574
+ body: init.body,
575
+ duplex: init.body ? "half" : void 0
576
+ };
577
+ return init;
578
+ }
579
+ function proxyUpgrade(addr, req, socket, head, opts) {
580
+ const resolvedAddr = parseAddr(addr);
581
+ if (req.method !== "GET" || req.headers.upgrade?.toLowerCase() !== "websocket") {
582
+ socket.destroy();
583
+ return Promise.reject(/* @__PURE__ */ new Error("Not a valid WebSocket upgrade request"));
584
+ }
585
+ if (opts?.xfwd !== false) {
586
+ const xfFor = req.headers["x-forwarded-for"];
587
+ const xfPort = req.headers["x-forwarded-port"];
588
+ const xfProto = req.headers["x-forwarded-proto"];
589
+ req.headers["x-forwarded-for"] = `${xfFor ? `${xfFor},` : ""}${req.socket?.remoteAddress}`;
590
+ req.headers["x-forwarded-port"] = `${xfPort ? `${xfPort},` : ""}${getPort(req)}`;
591
+ req.headers["x-forwarded-proto"] = `${xfProto ? `${xfProto},` : ""}${hasEncryptedConnection(req) ? "wss" : "ws"}`;
592
+ }
593
+ const target = _buildTargetURL(resolvedAddr);
594
+ const requestOptions = {
595
+ ...opts,
596
+ target,
597
+ prependPath: opts?.prependPath !== false
598
+ };
599
+ const outgoing = setupOutgoing(requestOptions.ssl || {}, requestOptions, req);
600
+ const sock = socket;
601
+ return new Promise((resolve, reject) => {
602
+ let settled = false;
603
+ setupSocket(sock);
604
+ if (head && head.length > 0) sock.unshift(head);
605
+ sock.once("error", onSocketError);
606
+ const proxyReq = (isSSL.test(target.protocol) ? request$1 : request)(outgoing);
607
+ proxyReq.once("error", onOutgoingError);
608
+ proxyReq.once("response", (res) => {
609
+ if (!res.upgrade) {
610
+ sock.write(_createHttpHeader(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`, res.headers));
611
+ res.pipe(sock);
612
+ if (!settled) {
613
+ settled = true;
614
+ reject(/* @__PURE__ */ new Error("Upstream server did not upgrade the connection"));
615
+ }
616
+ }
617
+ });
618
+ proxyReq.once("upgrade", (proxyRes, proxySocket, proxyHead) => {
619
+ proxySocket.once("error", onOutgoingError);
620
+ sock.removeListener("error", onSocketError);
621
+ sock.once("error", () => {
622
+ proxySocket.end();
623
+ });
624
+ setupSocket(proxySocket);
625
+ if (proxyHead && proxyHead.length > 0) proxySocket.unshift(proxyHead);
626
+ sock.write(_createHttpHeader("HTTP/1.1 101 Switching Protocols", proxyRes.headers));
627
+ proxySocket.pipe(sock).pipe(proxySocket);
628
+ settled = true;
629
+ resolve(proxySocket);
630
+ });
631
+ proxyReq.end();
632
+ function onSocketError(err) {
633
+ proxyReq.destroy();
634
+ if (!settled) {
635
+ settled = true;
636
+ reject(err);
637
+ }
638
+ }
639
+ function onOutgoingError(err) {
640
+ sock.end();
641
+ if (!settled) {
642
+ settled = true;
643
+ reject(err);
644
+ }
645
+ }
646
+ });
647
+ }
648
+ function _buildTargetURL(addr) {
649
+ if (addr.socketPath) {
650
+ const url = new URL("http://unix");
651
+ url.socketPath = addr.socketPath;
652
+ return url;
653
+ }
654
+ return new URL(`http://${addr.host || "localhost"}${addr.port ? `:${addr.port}` : ""}`);
655
+ }
656
+ function _createHttpHeader(line, headers) {
657
+ let result = line;
658
+ for (const key of Object.keys(headers)) {
659
+ const value = headers[key];
660
+ if (value === void 0) continue;
661
+ if (Array.isArray(value)) for (const element of value) result += `\r
662
+ ${key}: ${element}`;
663
+ else result += `\r
664
+ ${key}: ${value}`;
665
+ }
666
+ return `${result}\r
667
+ \r
668
+ `;
669
+ }
414
670
  //#endregion
415
- export { dist_exports as n, createProxyServer as t };
671
+ export { proxyFetch as n, proxyUpgrade as r, createProxyServer as t };
@@ -0,0 +1 @@
1
+ export { };
@@ -33,6 +33,5 @@ function klona(x) {
33
33
  }
34
34
  return tmp || x;
35
35
  }
36
-
37
36
  //#endregion
38
- export { klona as t };
37
+ export { klona as t };
@@ -1,4 +1,4 @@
1
- //#region node_modules/.pnpm/mlly@1.8.0/node_modules/mlly/dist/index.d.ts
1
+ //#region node_modules/.pnpm/mlly@1.8.1/node_modules/mlly/dist/index.d.ts
2
2
  /**
3
3
  * Represents a general structure for ECMAScript module exports.
4
4
  */