nitro 3.0.260415-beta → 3.0.260522-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.
- package/dist/THIRD-PARTY-LICENSES.md.gz +0 -0
- package/dist/_build/common.mjs +157 -45
- package/dist/_build/rolldown.mjs +6 -6
- package/dist/_build/rollup.mjs +11 -11
- package/dist/_build/vite.build.mjs +3 -3
- package/dist/_build/vite.dev.mjs +149 -0
- package/dist/_build/vite.env.mjs +177 -0
- package/dist/_chunks/nitro.mjs +241 -14
- package/dist/_chunks/nitro2.mjs +117 -90
- package/dist/_chunks/utils.mjs +4 -4
- package/dist/_common.mjs +2 -2
- package/dist/_dev.d.mts +1 -1
- package/dist/_dev.mjs +7 -6
- package/dist/_libs/c12+rc9.mjs +24 -11
- package/dist/_libs/c12+readdirp+chokidar.d.mts +7 -8
- package/dist/_libs/citty.mjs +1 -1
- package/dist/_libs/common.d.mts +37 -0
- package/dist/_libs/commondir+is-reference.mjs +3 -3
- package/dist/_libs/compatx.d.mts +1 -1
- package/dist/_libs/compatx.mjs +1 -1
- package/dist/_libs/estree+pluginutils.d.mts +1 -1
- package/dist/_libs/estree-walker.mjs +1 -1
- package/dist/_libs/hasown+resolve+deepmerge.mjs +62 -49
- package/dist/_libs/httpxy.d.mts +2 -4
- package/dist/_libs/httpxy.mjs +13 -9
- package/dist/_libs/jsonc-parser+workers-utils.d.mts +1579 -0
- package/dist/_libs/klona.mjs +1 -1
- package/dist/_libs/magic-string.d.mts +1 -1
- package/dist/_libs/mlly.d.mts +1 -1
- package/dist/_libs/nypm+tinyexec.mjs +137 -113
- package/dist/_libs/openapi-types.d.mts +730 -0
- package/dist/_libs/perfect-debounce.mjs +1 -1
- package/dist/_libs/pkg-types.d.mts +8 -5
- package/dist/_libs/plugin-alias.mjs +2 -2
- package/dist/_libs/plugin-inject.mjs +4 -2
- package/dist/_libs/plugin-json.mjs +2 -2
- package/dist/_libs/pluginutils.mjs +18 -5
- package/dist/_libs/readdirp+chokidar.mjs +4 -3
- package/dist/_libs/remapping.mjs +2 -2
- package/dist/_libs/resolve-uri+gen-mapping.mjs +2 -5
- package/dist/_libs/rou3.d.mts +1 -1
- package/dist/_libs/rou3.mjs +1 -1
- package/dist/_libs/std-env.d.mts +1 -1
- package/dist/_libs/tsconfck.mjs +1 -1
- package/dist/_libs/ultrahtml.mjs +1 -1
- package/dist/_libs/unimport+unplugin.mjs +61 -39
- package/dist/_libs/unplugin+unimport.d.mts +4 -4
- package/dist/_libs/unwasm.d.mts +1 -1
- package/dist/_presets.mjs +118 -91
- package/dist/builder.d.mts +1 -1
- package/dist/builder.mjs +3 -3
- package/dist/cli/_chunks/build.mjs +3 -3
- package/dist/cli/_chunks/common.mjs +1 -1
- package/dist/cli/_chunks/deploy.mjs +2 -2
- package/dist/cli/_chunks/dev.mjs +4 -4
- package/dist/cli/_chunks/docs.mjs +1 -1
- package/dist/cli/_chunks/list.mjs +2 -2
- package/dist/cli/_chunks/prepare.mjs +3 -3
- package/dist/cli/_chunks/preview.mjs +4 -4
- package/dist/cli/_chunks/run.mjs +2 -2
- package/dist/cli/_chunks/task.mjs +1 -1
- package/dist/cli/index.mjs +1 -1
- package/dist/docs/0.docs/5.cache.md +53 -9
- package/dist/docs/1.deploy/2.providers/2.aws-amplify.md +3 -3
- package/dist/docs/1.deploy/2.providers/21.vercel.md +6 -0
- package/dist/docs/3.examples/27.vite-ssr-tsr-react.md +1 -1
- package/dist/node_modules/ufo/dist/index.mjs +2 -2
- package/dist/node_modules/ufo/package.json +9 -9
- package/dist/presets/azure/runtime/azure-swa.mjs +2 -0
- package/dist/presets/vercel/runtime/queue.dev.mjs +41 -0
- package/dist/runtime/internal/app.mjs +4 -111
- package/dist/runtime/internal/route-rules.d.mts +1 -0
- package/dist/runtime/internal/route-rules.mjs +29 -1
- package/dist/runtime/internal/routes/scalar.mjs +1 -0
- package/dist/runtime/virtual/app.d.mts +4 -0
- package/dist/runtime/virtual/app.mjs +32 -0
- package/dist/types/index.d.mts +959 -1836
- package/dist/vite.d.mts +1 -1
- package/dist/vite.mjs +23 -331
- package/package.json +43 -44
- package/dist/THIRD-PARTY-LICENSES.md +0 -1646
- package/dist/_chunks/nitro3.mjs +0 -136
- package/dist/_chunks/nitro4.mjs +0 -127
- package/dist/_libs/_.mjs +0 -2
- package/dist/_libs/_2.mjs +0 -2
- package/dist/_libs/_3.mjs +0 -2
- package/dist/_libs/_4.mjs +0 -2
- package/dist/_libs/_5.mjs +0 -2
- package/dist/_libs/_6.mjs +0 -2
- package/dist/_libs/_7.mjs +0 -2
- package/dist/_libs/escape-string-regexp.mjs +0 -5
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { isAbsolute as isAbsolute$1, resolveModulePath } from "./common.mjs";
|
|
2
|
+
import { runtimeDependencies, runtimeDir } from "nitro/meta";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { RunnerManager, loadRunner } from "env-runner";
|
|
5
|
+
function createNitroEnvironment(ctx) {
|
|
6
|
+
const isWorkerdRunner = _isWorkerdRunner(ctx);
|
|
7
|
+
return {
|
|
8
|
+
consumer: "server",
|
|
9
|
+
build: {
|
|
10
|
+
rollupOptions: ctx.bundlerConfig.rollupConfig,
|
|
11
|
+
rolldownOptions: ctx.bundlerConfig.rolldownConfig,
|
|
12
|
+
minify: ctx.nitro.options.minify,
|
|
13
|
+
emptyOutDir: false,
|
|
14
|
+
sourcemap: ctx.nitro.options.sourcemap,
|
|
15
|
+
commonjsOptions: ctx.nitro.options.commonJS,
|
|
16
|
+
copyPublicDir: false
|
|
17
|
+
},
|
|
18
|
+
resolve: {
|
|
19
|
+
noExternal: ctx.nitro.options.dev ? isWorkerdRunner ? true : [
|
|
20
|
+
/^nitro$/,
|
|
21
|
+
new RegExp(`^(${runtimeDependencies.join("|")})$`),
|
|
22
|
+
...ctx.bundlerConfig.base.noExternal
|
|
23
|
+
] : true,
|
|
24
|
+
conditions: isWorkerdRunner ? [
|
|
25
|
+
"workerd",
|
|
26
|
+
"worker",
|
|
27
|
+
...ctx.nitro.options.exportConditions.filter((c) => c !== "node")
|
|
28
|
+
] : ctx.nitro.options.exportConditions,
|
|
29
|
+
externalConditions: ctx.nitro.options.exportConditions?.filter((c) => !/browser|wasm|module/.test(c))
|
|
30
|
+
},
|
|
31
|
+
define: { "process.env.NODE_ENV": JSON.stringify(ctx.nitro.options.dev ? "development" : "production") },
|
|
32
|
+
dev: { createEnvironment: async (envName, envConfig) => {
|
|
33
|
+
const entry = resolve(runtimeDir, "internal/vite/dev-entry.mjs");
|
|
34
|
+
const { createFetchableDevEnvironment } = await import("./vite.dev.mjs");
|
|
35
|
+
const env = createFetchableDevEnvironment(envName, envConfig, getEnvRunner(ctx), entry, { preventExternalize: isWorkerdRunner });
|
|
36
|
+
ctx._transformRequest = (id) => env.transformRequest(id);
|
|
37
|
+
(ctx._viteEnvs ??= /* @__PURE__ */ new Map()).set(envName, entry);
|
|
38
|
+
return env;
|
|
39
|
+
} }
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function createServiceEnvironment(ctx, name, serviceConfig) {
|
|
43
|
+
const isWorkerdRunner = _isWorkerdRunner(ctx);
|
|
44
|
+
return {
|
|
45
|
+
consumer: "server",
|
|
46
|
+
build: {
|
|
47
|
+
rollupOptions: {
|
|
48
|
+
input: { index: serviceConfig.entry },
|
|
49
|
+
external: [/^nitro(\/|$)/]
|
|
50
|
+
},
|
|
51
|
+
minify: ctx.nitro.options.minify,
|
|
52
|
+
sourcemap: ctx.nitro.options.sourcemap,
|
|
53
|
+
outDir: join(ctx.nitro.options.buildDir, "vite/services", name),
|
|
54
|
+
emptyOutDir: true,
|
|
55
|
+
copyPublicDir: false
|
|
56
|
+
},
|
|
57
|
+
resolve: {
|
|
58
|
+
...isWorkerdRunner ? { noExternal: true } : {},
|
|
59
|
+
conditions: isWorkerdRunner ? [
|
|
60
|
+
"workerd",
|
|
61
|
+
"worker",
|
|
62
|
+
...ctx.nitro.options.exportConditions.filter((c) => c !== "node")
|
|
63
|
+
] : ctx.nitro.options.exportConditions,
|
|
64
|
+
externalConditions: ctx.nitro.options.exportConditions?.filter((c) => !/browser|wasm|module/.test(c))
|
|
65
|
+
},
|
|
66
|
+
dev: { createEnvironment: async (envName, envConfig) => {
|
|
67
|
+
const entry = tryResolve(serviceConfig.entry);
|
|
68
|
+
(ctx._viteEnvs ??= /* @__PURE__ */ new Map()).set(envName, entry);
|
|
69
|
+
const { createFetchableDevEnvironment } = await import("./vite.dev.mjs");
|
|
70
|
+
return createFetchableDevEnvironment(envName, envConfig, getEnvRunner(ctx), entry, { preventExternalize: isWorkerdRunner });
|
|
71
|
+
} }
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function createServiceEnvironments(ctx) {
|
|
75
|
+
return Object.fromEntries(Object.entries(ctx.services).map(([name, config]) => [name, createServiceEnvironment(ctx, name, config)]));
|
|
76
|
+
}
|
|
77
|
+
async function initEnvRunner(ctx) {
|
|
78
|
+
if (ctx._envRunner) return ctx._envRunner;
|
|
79
|
+
if (!ctx._initPromise) ctx._initPromise = (async () => {
|
|
80
|
+
const manager = new RunnerManager();
|
|
81
|
+
let _retries = 0;
|
|
82
|
+
manager.onClose((_runner, cause) => {
|
|
83
|
+
if (_retries++ < 3) {
|
|
84
|
+
ctx.nitro.logger.info("Restarting env runner...", cause ? `Cause: ${cause}` : "");
|
|
85
|
+
_loadRunner(ctx, manager);
|
|
86
|
+
} else ctx.nitro.logger.error("Env runner failed after 3 retries.", cause ? `Last cause: ${cause}` : "");
|
|
87
|
+
});
|
|
88
|
+
manager.onReady(() => {
|
|
89
|
+
_retries = 0;
|
|
90
|
+
if (ctx._viteEnvs) for (const [name, entry] of ctx._viteEnvs) manager.sendMessage({
|
|
91
|
+
type: "custom",
|
|
92
|
+
event: "nitro:vite-env",
|
|
93
|
+
data: {
|
|
94
|
+
name,
|
|
95
|
+
entry
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
await _loadRunner(ctx, manager);
|
|
100
|
+
ctx._envRunner = manager;
|
|
101
|
+
return manager;
|
|
102
|
+
})();
|
|
103
|
+
return await ctx._initPromise;
|
|
104
|
+
}
|
|
105
|
+
function getEnvRunner(ctx) {
|
|
106
|
+
if (!ctx._envRunner) throw new Error("Env runner not initialized. Call initEnvRunner() first.");
|
|
107
|
+
return ctx._envRunner;
|
|
108
|
+
}
|
|
109
|
+
async function _loadRunner(ctx, manager) {
|
|
110
|
+
const runnerName = ctx.nitro.options.devServer.runner || process.env.NITRO_DEV_RUNNER || "node-worker";
|
|
111
|
+
const entry = resolve(runtimeDir, "internal/vite/dev-worker.mjs");
|
|
112
|
+
let runner;
|
|
113
|
+
if (runnerName === "miniflare") {
|
|
114
|
+
const { MiniflareEnvRunner } = await import("env-runner/runners/miniflare");
|
|
115
|
+
runner = new MiniflareEnvRunner({
|
|
116
|
+
name: "nitro-vite",
|
|
117
|
+
data: { entry }
|
|
118
|
+
});
|
|
119
|
+
} else runner = await loadRunner(runnerName, {
|
|
120
|
+
name: "nitro-vite",
|
|
121
|
+
data: { entry }
|
|
122
|
+
});
|
|
123
|
+
await manager.reload(runner);
|
|
124
|
+
}
|
|
125
|
+
const NITRO_PROXY_PREFIX = "\0nitro-env-proxy:";
|
|
126
|
+
function nitroServiceProxy() {
|
|
127
|
+
return {
|
|
128
|
+
name: "nitro:service-proxy",
|
|
129
|
+
enforce: "pre",
|
|
130
|
+
applyToEnvironment: (env) => env.name !== "nitro" && env.config.consumer === "server",
|
|
131
|
+
apply: (_config, configEnv) => configEnv.command === "serve",
|
|
132
|
+
resolveId: {
|
|
133
|
+
filter: { id: /^nitro(\/|$)/ },
|
|
134
|
+
handler(id) {
|
|
135
|
+
if (id === "nitro" || id.startsWith("nitro/")) return {
|
|
136
|
+
id: NITRO_PROXY_PREFIX + id,
|
|
137
|
+
moduleSideEffects: false
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
load: {
|
|
142
|
+
filter: { id: /^\0nitro-env-proxy:/ },
|
|
143
|
+
handler(id) {
|
|
144
|
+
if (!id.startsWith(NITRO_PROXY_PREFIX)) return;
|
|
145
|
+
const originalId = id.slice(17);
|
|
146
|
+
return {
|
|
147
|
+
code: [
|
|
148
|
+
`const _mod = await globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__("nitro", ${JSON.stringify(originalId)});`,
|
|
149
|
+
`__vite_ssr_exportAll__(_mod);`,
|
|
150
|
+
`export default _mod.default;`
|
|
151
|
+
].join("\n"),
|
|
152
|
+
map: null
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function _isWorkerdRunner(ctx) {
|
|
159
|
+
return (ctx.nitro.options.devServer.runner || process.env.NITRO_DEV_RUNNER || "node-worker") === "miniflare";
|
|
160
|
+
}
|
|
161
|
+
function tryResolve(id) {
|
|
162
|
+
if (/^[~#/\0]/.test(id) || isAbsolute$1(id)) return id;
|
|
163
|
+
return resolveModulePath(id, {
|
|
164
|
+
suffixes: ["", "/index"],
|
|
165
|
+
extensions: [
|
|
166
|
+
"",
|
|
167
|
+
".ts",
|
|
168
|
+
".mjs",
|
|
169
|
+
".cjs",
|
|
170
|
+
".js",
|
|
171
|
+
".mts",
|
|
172
|
+
".cts"
|
|
173
|
+
],
|
|
174
|
+
try: true
|
|
175
|
+
}) || id;
|
|
176
|
+
}
|
|
177
|
+
export { createNitroEnvironment, createServiceEnvironment, createServiceEnvironments, getEnvRunner, initEnvRunner, nitroServiceProxy };
|
package/dist/_chunks/nitro.mjs
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { n as scanHandlers, t as scanAndSyncOptions } from "./nitro2.mjs";
|
|
8
|
-
import { i as findAllRoutes, n as addRoute, r as createRouter } from "../_libs/rou3.mjs";
|
|
9
|
-
import { n as initNitroRouting } from "./nitro3.mjs";
|
|
1
|
+
import { loadConfig, watchConfig } from "../_libs/c12+rc9.mjs";
|
|
2
|
+
import { _, build, compressPublicAssets, escapeRegExp, findWorkspaceDir, glob, join, prettyPath, relative, resolve, resolveModulePath, resolveModuleURL, resolveNitroPath, runParallel, scanUnprefixedPublicAssets, src_default, v, writeFile as writeFile$1 } from "../_build/common.mjs";
|
|
3
|
+
import { resolveCompatibilityDates, resolveCompatibilityDatesFromEnv } from "../_libs/compatx.mjs";
|
|
4
|
+
import { klona } from "../_libs/klona.mjs";
|
|
5
|
+
import { TSConfckCache, parse } from "../_libs/tsconfck.mjs";
|
|
6
|
+
import { addRoute, compileRouterToString, createRouter, findAllRoutes, findRoute } from "../_libs/rou3.mjs";
|
|
10
7
|
import "../_dev.mjs";
|
|
11
|
-
import {
|
|
8
|
+
import { P, z } from "../_libs/ultrahtml.mjs";
|
|
12
9
|
import { createRequire } from "node:module";
|
|
13
10
|
import consola, { consola as consola$1 } from "consola";
|
|
14
11
|
import { Hookable, createDebugger } from "hookable";
|
|
@@ -19,6 +16,7 @@ import { pathToFileURL } from "node:url";
|
|
|
19
16
|
import { defu } from "defu";
|
|
20
17
|
import { joinURL, parseURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutTrailingSlash } from "ufo";
|
|
21
18
|
import { colors } from "consola/utils";
|
|
19
|
+
import { hash } from "ohash";
|
|
22
20
|
import { toRequest } from "h3";
|
|
23
21
|
import http from "node:http";
|
|
24
22
|
const NitroDefaults = {
|
|
@@ -165,7 +163,7 @@ async function resolveImportsOptions(options) {
|
|
|
165
163
|
options.imports.exclude.push(/[/\\]\.git[/\\]/);
|
|
166
164
|
options.imports.exclude.push(options.buildDir);
|
|
167
165
|
const scanDirsInNodeModules = options.scanDirs.map((dir) => dir.match(/(?<=\/)node_modules\/(.+)$/)?.[1]).filter(Boolean);
|
|
168
|
-
options.imports.exclude.push(scanDirsInNodeModules.length > 0 ? new RegExp(`node_modules\\/(?!${scanDirsInNodeModules.map((dir) =>
|
|
166
|
+
options.imports.exclude.push(scanDirsInNodeModules.length > 0 ? new RegExp(`node_modules\\/(?!${scanDirsInNodeModules.map((dir) => escapeRegExp(dir)).join("|")})`) : /[/\\]node_modules[/\\]/);
|
|
169
167
|
}
|
|
170
168
|
}
|
|
171
169
|
async function resolveOpenAPIOptions(options) {
|
|
@@ -475,7 +473,7 @@ function isPkgInstalled(pkg, root) {
|
|
|
475
473
|
}
|
|
476
474
|
}
|
|
477
475
|
async function installPkg(pkg, root) {
|
|
478
|
-
const { addDevDependency } = await import("../_libs/
|
|
476
|
+
const { addDevDependency } = await import("../_libs/nypm+tinyexec.mjs").then((n) => n.dist_exports);
|
|
479
477
|
return addDevDependency(pkg, { cwd: root });
|
|
480
478
|
}
|
|
481
479
|
function hasNitroViteConfig(options) {
|
|
@@ -497,6 +495,7 @@ async function resolveTracingOptions(options) {
|
|
|
497
495
|
options.tracingChannel = {
|
|
498
496
|
srvx: true,
|
|
499
497
|
h3: true,
|
|
498
|
+
unstorage: true,
|
|
500
499
|
...typeof options.tracingChannel === "object" ? options.tracingChannel : {}
|
|
501
500
|
};
|
|
502
501
|
options.plugins = options.plugins || [];
|
|
@@ -626,6 +625,234 @@ async function _resolveNitroModule(mod, nitroOptions) {
|
|
|
626
625
|
...mod
|
|
627
626
|
};
|
|
628
627
|
}
|
|
628
|
+
const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}";
|
|
629
|
+
const suffixRegex = /(\.(?<method>connect|delete|get|head|options|patch|post|put|trace))?(\.(?<env>dev|prod|prerender))?$/;
|
|
630
|
+
async function scanAndSyncOptions(nitro) {
|
|
631
|
+
const scannedPlugins = await scanPlugins(nitro);
|
|
632
|
+
for (const plugin of scannedPlugins) if (!nitro.options.plugins.includes(plugin)) nitro.options.plugins.push(plugin);
|
|
633
|
+
if (nitro.options.experimental.tasks) {
|
|
634
|
+
const scannedTasks = await scanTasks(nitro);
|
|
635
|
+
for (const scannedTask of scannedTasks) if (scannedTask.name in nitro.options.tasks) {
|
|
636
|
+
if (!nitro.options.tasks[scannedTask.name].handler) nitro.options.tasks[scannedTask.name].handler = scannedTask.handler;
|
|
637
|
+
} else nitro.options.tasks[scannedTask.name] = {
|
|
638
|
+
handler: scannedTask.handler,
|
|
639
|
+
description: ""
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
const scannedModules = await scanModules(nitro);
|
|
643
|
+
nitro.options.modules = nitro.options.modules || [];
|
|
644
|
+
for (const modPath of scannedModules) if (!nitro.options.modules.includes(modPath)) nitro.options.modules.push(modPath);
|
|
645
|
+
}
|
|
646
|
+
async function scanHandlers(nitro) {
|
|
647
|
+
const middleware = await scanMiddleware(nitro);
|
|
648
|
+
const handlers = await Promise.all([scanServerRoutes(nitro, nitro.options.apiDir || "api", nitro.options.apiBaseURL || "/api"), scanServerRoutes(nitro, nitro.options.routesDir || "routes")]).then((r) => r.flat());
|
|
649
|
+
const seenHandlers = /* @__PURE__ */ new Set();
|
|
650
|
+
nitro.scannedHandlers = [...middleware, ...handlers.filter((h) => {
|
|
651
|
+
const key = `${h.route}\0${h.method}\0${h.env}`;
|
|
652
|
+
return seenHandlers.has(key) ? false : (seenHandlers.add(key), true);
|
|
653
|
+
})];
|
|
654
|
+
nitro.routing.sync();
|
|
655
|
+
return handlers;
|
|
656
|
+
}
|
|
657
|
+
async function scanMiddleware(nitro) {
|
|
658
|
+
return (await scanFiles(nitro, "middleware")).map((file) => {
|
|
659
|
+
return {
|
|
660
|
+
route: "/**",
|
|
661
|
+
middleware: true,
|
|
662
|
+
handler: file.fullPath
|
|
663
|
+
};
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
async function scanServerRoutes(nitro, dir, prefix = "/") {
|
|
667
|
+
return (await scanFiles(nitro, dir)).map((file) => {
|
|
668
|
+
let route = file.path.replace(/\.[A-Za-z]+$/, "").replace(/\(([^(/\\]+)\)[/\\]/g, "").replace(/\[\.{3}]/g, "**").replace(/\[\.{3}([^\]]+)]/g, (_, p) => "**:" + p.replace(/[^\w-]/g, "_")).replace(/\[([^/\]]+)]/g, (_, p) => ":" + p.replace(/[^\w-]/g, "_"));
|
|
669
|
+
route = withLeadingSlash(withoutTrailingSlash(withBase(route, prefix)));
|
|
670
|
+
const suffixMatch = route.match(suffixRegex);
|
|
671
|
+
let method;
|
|
672
|
+
let env;
|
|
673
|
+
if (suffixMatch?.index && suffixMatch?.index >= 0) {
|
|
674
|
+
route = route.slice(0, suffixMatch.index);
|
|
675
|
+
method = suffixMatch.groups?.method;
|
|
676
|
+
env = suffixMatch.groups?.env;
|
|
677
|
+
}
|
|
678
|
+
route = route.replace(/\/index$/, "") || "/";
|
|
679
|
+
return {
|
|
680
|
+
handler: file.fullPath,
|
|
681
|
+
lazy: true,
|
|
682
|
+
middleware: false,
|
|
683
|
+
route,
|
|
684
|
+
method,
|
|
685
|
+
env
|
|
686
|
+
};
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
async function scanPlugins(nitro) {
|
|
690
|
+
return (await scanFiles(nitro, "plugins")).map((f) => f.fullPath);
|
|
691
|
+
}
|
|
692
|
+
async function scanTasks(nitro) {
|
|
693
|
+
return (await scanFiles(nitro, "tasks")).map((f) => {
|
|
694
|
+
return {
|
|
695
|
+
name: f.path.replace(/\/index$/, "").replace(/\.[A-Za-z]+$/, "").replace(/\//g, ":"),
|
|
696
|
+
handler: f.fullPath
|
|
697
|
+
};
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
async function scanModules(nitro) {
|
|
701
|
+
return (await scanFiles(nitro, "modules")).map((f) => f.fullPath);
|
|
702
|
+
}
|
|
703
|
+
async function scanFiles(nitro, name) {
|
|
704
|
+
return await Promise.all(nitro.options.scanDirs.map((dir) => scanDir(nitro, dir, name))).then((r) => r.flat());
|
|
705
|
+
}
|
|
706
|
+
async function scanDir(nitro, dir, name) {
|
|
707
|
+
return (await glob(join(name, GLOB_SCAN_PATTERN), {
|
|
708
|
+
cwd: dir,
|
|
709
|
+
dot: true,
|
|
710
|
+
ignore: nitro.options.ignore,
|
|
711
|
+
absolute: true
|
|
712
|
+
}).catch((error) => {
|
|
713
|
+
if (error?.code === "ENOTDIR") {
|
|
714
|
+
nitro.logger.warn(`Ignoring \`${join(dir, name)}\`. It must be a directory.`);
|
|
715
|
+
return [];
|
|
716
|
+
}
|
|
717
|
+
throw error;
|
|
718
|
+
})).map((fullPath) => {
|
|
719
|
+
return {
|
|
720
|
+
fullPath,
|
|
721
|
+
path: relative(join(dir, name), fullPath)
|
|
722
|
+
};
|
|
723
|
+
}).sort((a, b) => a.path.localeCompare(b.path));
|
|
724
|
+
}
|
|
725
|
+
const isGlobalMiddleware = (h) => !h.method && (!h.route || h.route === "/**");
|
|
726
|
+
function initNitroRouting(nitro) {
|
|
727
|
+
const envConditions = new Set([
|
|
728
|
+
nitro.options.dev ? "dev" : "prod",
|
|
729
|
+
nitro.options.preset,
|
|
730
|
+
nitro.options.preset === "nitro-prerender" ? "prerender" : void 0
|
|
731
|
+
].filter(Boolean));
|
|
732
|
+
const matchesEnv = (h) => {
|
|
733
|
+
const envs = (Array.isArray(h.env) ? h.env : [h.env]).filter(Boolean);
|
|
734
|
+
return envs.length === 0 || envs.some((env) => envConditions.has(env));
|
|
735
|
+
};
|
|
736
|
+
const routes = new Router(nitro.options.baseURL);
|
|
737
|
+
const routeRules = new Router(nitro.options.baseURL);
|
|
738
|
+
const globalMiddleware = [];
|
|
739
|
+
const routedMiddleware = new Router(nitro.options.baseURL);
|
|
740
|
+
const sync = () => {
|
|
741
|
+
routeRules._update(Object.entries(nitro.options.routeRules).map(([route, data]) => ({
|
|
742
|
+
route,
|
|
743
|
+
method: "",
|
|
744
|
+
data: {
|
|
745
|
+
...data,
|
|
746
|
+
_route: route
|
|
747
|
+
}
|
|
748
|
+
})));
|
|
749
|
+
const _routes = [
|
|
750
|
+
...Object.entries(nitro.options.routes).flatMap(([route, handler]) => {
|
|
751
|
+
if (typeof handler === "string") handler = { handler };
|
|
752
|
+
return {
|
|
753
|
+
...handler,
|
|
754
|
+
route,
|
|
755
|
+
middleware: false
|
|
756
|
+
};
|
|
757
|
+
}),
|
|
758
|
+
...nitro.options.handlers,
|
|
759
|
+
...nitro.scannedHandlers
|
|
760
|
+
].filter((h) => h && !h.middleware && matchesEnv(h));
|
|
761
|
+
if (nitro.options.serverEntry && nitro.options.serverEntry.handler) _routes.push({
|
|
762
|
+
route: "/**",
|
|
763
|
+
lazy: false,
|
|
764
|
+
format: nitro.options.serverEntry.format,
|
|
765
|
+
handler: nitro.options.serverEntry.handler
|
|
766
|
+
});
|
|
767
|
+
if (nitro.options.renderer?.handler) _routes.push({
|
|
768
|
+
route: "/**",
|
|
769
|
+
lazy: true,
|
|
770
|
+
handler: nitro.options.renderer?.handler
|
|
771
|
+
});
|
|
772
|
+
routes._update(_routes.map((h) => ({
|
|
773
|
+
...h,
|
|
774
|
+
method: h.method || "",
|
|
775
|
+
data: handlerWithImportHash(h)
|
|
776
|
+
})), { merge: true });
|
|
777
|
+
const _middleware = [...nitro.scannedHandlers, ...nitro.options.handlers].filter((h) => h && h.middleware && matchesEnv(h));
|
|
778
|
+
if (nitro.options.serveStatic) _middleware.unshift({
|
|
779
|
+
route: "/**",
|
|
780
|
+
middleware: true,
|
|
781
|
+
handler: join(runtimeDir, "internal/static")
|
|
782
|
+
});
|
|
783
|
+
globalMiddleware.splice(0, globalMiddleware.length, ..._middleware.filter((h) => isGlobalMiddleware(h)).map((m) => handlerWithImportHash(m)));
|
|
784
|
+
routedMiddleware._update(_middleware.filter((h) => !isGlobalMiddleware(h)).map((h) => ({
|
|
785
|
+
...h,
|
|
786
|
+
method: h.method || "",
|
|
787
|
+
data: handlerWithImportHash(h)
|
|
788
|
+
})));
|
|
789
|
+
};
|
|
790
|
+
nitro.routing = Object.freeze({
|
|
791
|
+
sync,
|
|
792
|
+
routes,
|
|
793
|
+
routeRules,
|
|
794
|
+
globalMiddleware,
|
|
795
|
+
routedMiddleware
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
function handlerWithImportHash(h) {
|
|
799
|
+
const id = (h.lazy ? "_lazy_" : "_") + hash(h.handler).replace(/-/g, "").slice(0, 6);
|
|
800
|
+
return {
|
|
801
|
+
...h,
|
|
802
|
+
_importHash: id
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
var Router = class {
|
|
806
|
+
_routes;
|
|
807
|
+
_router;
|
|
808
|
+
_compiled;
|
|
809
|
+
_baseURL;
|
|
810
|
+
constructor(baseURL) {
|
|
811
|
+
this._update([]);
|
|
812
|
+
this._baseURL = baseURL || "";
|
|
813
|
+
if (this._baseURL.endsWith("/")) this._baseURL = this._baseURL.slice(0, -1);
|
|
814
|
+
}
|
|
815
|
+
get routes() {
|
|
816
|
+
return this._routes;
|
|
817
|
+
}
|
|
818
|
+
_update(routes, opts) {
|
|
819
|
+
this._routes = routes;
|
|
820
|
+
this._router = createRouter();
|
|
821
|
+
this._compiled = void 0;
|
|
822
|
+
for (const route of routes) addRoute(this._router, route.method, this._baseURL + route.route, route.data);
|
|
823
|
+
if (opts?.merge) mergeCatchAll(this._router);
|
|
824
|
+
}
|
|
825
|
+
hasRoutes() {
|
|
826
|
+
return this._routes.length > 0;
|
|
827
|
+
}
|
|
828
|
+
compileToString(opts) {
|
|
829
|
+
const key = opts ? hash(opts) : "";
|
|
830
|
+
this._compiled ||= {};
|
|
831
|
+
if (this._compiled[key]) return this._compiled[key];
|
|
832
|
+
this._compiled[key] = compileRouterToString(this._router, void 0, opts);
|
|
833
|
+
if (this.routes.length === 1 && this.routes[0].route === "/**" && this.routes[0].method === "") {
|
|
834
|
+
const data = (opts?.serialize || JSON.stringify)(this.routes[0].data);
|
|
835
|
+
let retCode = `{data,params:{"_":p.slice(1)}}`;
|
|
836
|
+
if (opts?.matchAll) retCode = `[${retCode}]`;
|
|
837
|
+
this._compiled[key] = `/* @__PURE__ */ (() => {const data=${data};return ((_m, p)=>{return ${retCode};})})()`;
|
|
838
|
+
}
|
|
839
|
+
return this._compiled[key];
|
|
840
|
+
}
|
|
841
|
+
match(method, path) {
|
|
842
|
+
return findRoute(this._router, method, path)?.data;
|
|
843
|
+
}
|
|
844
|
+
matchAll(method, path) {
|
|
845
|
+
return findAllRoutes(this._router, method, path).map((route) => route.data);
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
function mergeCatchAll(router) {
|
|
849
|
+
const handlers = router.root?.wildcard?.methods?.[""];
|
|
850
|
+
if (!handlers || handlers.length < 2) return;
|
|
851
|
+
handlers.splice(0, handlers.length, {
|
|
852
|
+
...handlers[0],
|
|
853
|
+
data: handlers.map((h) => h.data)
|
|
854
|
+
});
|
|
855
|
+
}
|
|
629
856
|
const nitroInstances = globalThis.__nitro_instances__ ||= [];
|
|
630
857
|
const globalKey = "__nitro_builder__";
|
|
631
858
|
function registerNitroInstance(nitro) {
|
|
@@ -675,7 +902,7 @@ async function createNitro(config = {}, opts = {}) {
|
|
|
675
902
|
nitro.hooks.addHooks(nitro.options.hooks);
|
|
676
903
|
await installModules(nitro);
|
|
677
904
|
if (nitro.options.imports) {
|
|
678
|
-
const { createUnimport } = await import("../_build/common.mjs").then((n) => n.
|
|
905
|
+
const { createUnimport } = await import("../_build/common.mjs").then((n) => n.dist_exports);
|
|
679
906
|
nitro.unimport = createUnimport(nitro.options.imports);
|
|
680
907
|
await nitro.unimport.init();
|
|
681
908
|
nitro.options.virtual["#imports"] = () => nitro.unimport?.toExports() || "";
|
|
@@ -963,4 +1190,4 @@ function _pidIsRunning(pid) {
|
|
|
963
1190
|
return false;
|
|
964
1191
|
}
|
|
965
1192
|
}
|
|
966
|
-
export {
|
|
1193
|
+
export { Router, createNitro, listTasks, loadOptions, prerender, runTask, scanHandlers };
|