nitro 3.0.260429-beta → 3.0.260603-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 +142 -35
- package/dist/_build/rolldown.mjs +12 -8
- 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 +252 -17
- package/dist/_chunks/nitro2.mjs +117 -90
- package/dist/_chunks/utils.mjs +4 -4
- package/dist/_common.mjs +1 -1
- package/dist/_dev.d.mts +1 -1
- package/dist/_dev.mjs +5 -5
- package/dist/_libs/c12+rc9.mjs +11 -10
- 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 +5 -5
- 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 +15 -15
- package/dist/_libs/httpxy.d.mts +2 -1
- package/dist/_libs/httpxy.mjs +8 -4
- 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 +150 -434
- package/dist/_libs/openapi-types.d.mts +730 -0
- package/dist/_libs/perfect-debounce.mjs +1 -1
- package/dist/_libs/pkg-types.d.mts +1 -1
- package/dist/_libs/plugin-alias.mjs +2 -2
- package/dist/_libs/plugin-inject.mjs +3 -3
- package/dist/_libs/plugin-json.mjs +2 -2
- package/dist/_libs/pluginutils.mjs +16 -5
- package/dist/_libs/readdirp+chokidar.mjs +2 -2
- 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/undici+jsonc-parser.d.mts +2418 -0
- package/dist/_libs/{unimport+unplugin+oxc-parser.mjs → unimport+unplugin.mjs} +18 -8
- package/dist/_libs/unplugin+unimport.d.mts +3 -3
- package/dist/_libs/unwasm.d.mts +1 -1
- package/dist/_presets.mjs +131 -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/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/2.config/0.index/index.md +14 -0
- package/dist/docs/3.examples/27.vite-ssr-tsr-react.md +1 -1
- package/dist/presets/vercel/runtime/queue.dev.mjs +41 -0
- package/dist/runtime/internal/app.mjs +4 -111
- package/dist/runtime/virtual/app.d.mts +4 -0
- package/dist/runtime/virtual/app.mjs +32 -0
- package/dist/types/index.d.mts +51 -1756
- package/dist/vite.mjs +29 -339
- package/package.json +39 -41
- package/dist/THIRD-PARTY-LICENSES.md +0 -1676
- package/dist/_chunks/nitro3.mjs +0 -136
- package/dist/_chunks/nitro4.mjs +0 -127
|
@@ -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,13 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { i as findAllRoutes, n as addRoute, r as createRouter } from "../_libs/rou3.mjs";
|
|
8
|
-
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";
|
|
9
7
|
import "../_dev.mjs";
|
|
10
|
-
import {
|
|
8
|
+
import { P, z } from "../_libs/ultrahtml.mjs";
|
|
11
9
|
import { createRequire } from "node:module";
|
|
12
10
|
import consola, { consola as consola$1 } from "consola";
|
|
13
11
|
import { Hookable, createDebugger } from "hookable";
|
|
@@ -18,6 +16,7 @@ import { pathToFileURL } from "node:url";
|
|
|
18
16
|
import { defu } from "defu";
|
|
19
17
|
import { joinURL, parseURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutTrailingSlash } from "ufo";
|
|
20
18
|
import { colors } from "consola/utils";
|
|
19
|
+
import { hash } from "ohash";
|
|
21
20
|
import { toRequest } from "h3";
|
|
22
21
|
import http from "node:http";
|
|
23
22
|
const NitroDefaults = {
|
|
@@ -474,7 +473,7 @@ function isPkgInstalled(pkg, root) {
|
|
|
474
473
|
}
|
|
475
474
|
}
|
|
476
475
|
async function installPkg(pkg, root) {
|
|
477
|
-
const { addDevDependency } = await import("../_libs/nypm+tinyexec.mjs").then((n) => n.
|
|
476
|
+
const { addDevDependency } = await import("../_libs/nypm+tinyexec.mjs").then((n) => n.dist_exports);
|
|
478
477
|
return addDevDependency(pkg, { cwd: root });
|
|
479
478
|
}
|
|
480
479
|
function hasNitroViteConfig(options) {
|
|
@@ -531,6 +530,7 @@ async function _loadUserConfig(configOverrides = {}, opts = {}) {
|
|
|
531
530
|
let compatibilityDate = configOverrides.compatibilityDate || opts.compatibilityDate || process.env.NITRO_COMPATIBILITY_DATE || process.env.SERVER_COMPATIBILITY_DATE || process.env.COMPATIBILITY_DATE;
|
|
532
531
|
const { resolvePreset } = await import("../_presets.mjs");
|
|
533
532
|
let preset = configOverrides.preset || process.env.NITRO_PRESET || process.env.SERVER_PRESET;
|
|
533
|
+
let inlineDefaultPreset;
|
|
534
534
|
const _dotenv = opts.dotenv ?? (configOverrides.dev && { fileName: [".env", ".env.local"] });
|
|
535
535
|
const envName = opts.c12?.envName ?? (configOverrides.dev ? "development" : "production");
|
|
536
536
|
const loadedConfig = await (opts.watch ? watchConfig : loadConfig)({
|
|
@@ -551,11 +551,17 @@ async function _loadUserConfig(configOverrides = {}, opts = {}) {
|
|
|
551
551
|
dev: true,
|
|
552
552
|
compatibilityDate: compatibilityDate || "latest"
|
|
553
553
|
}).then((p) => p?._meta?.name || "nitro-dev").catch(() => "nitro-dev") : "nitro-dev";
|
|
554
|
-
else if (!preset)
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
554
|
+
else if (!preset) {
|
|
555
|
+
const defaultPreset = getConf("defaultPreset");
|
|
556
|
+
const resolved = await resolvePreset("", {
|
|
557
|
+
static: getConf("static"),
|
|
558
|
+
dev: false,
|
|
559
|
+
compatibilityDate: compatibilityDate || "latest",
|
|
560
|
+
defaultPreset
|
|
561
|
+
});
|
|
562
|
+
preset = resolved?._meta?.name;
|
|
563
|
+
if (resolved && defaultPreset && typeof defaultPreset !== "string") inlineDefaultPreset = resolved;
|
|
564
|
+
}
|
|
559
565
|
return {
|
|
560
566
|
...configOverrides,
|
|
561
567
|
preset,
|
|
@@ -567,6 +573,7 @@ async function _loadUserConfig(configOverrides = {}, opts = {}) {
|
|
|
567
573
|
};
|
|
568
574
|
},
|
|
569
575
|
async resolve(id) {
|
|
576
|
+
if (inlineDefaultPreset && id === inlineDefaultPreset._meta?.name) return { config: klona(inlineDefaultPreset) };
|
|
570
577
|
const preset = await resolvePreset(id, {
|
|
571
578
|
static: configOverrides.static,
|
|
572
579
|
compatibilityDate: compatibilityDate || "latest",
|
|
@@ -626,6 +633,234 @@ async function _resolveNitroModule(mod, nitroOptions) {
|
|
|
626
633
|
...mod
|
|
627
634
|
};
|
|
628
635
|
}
|
|
636
|
+
const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}";
|
|
637
|
+
const suffixRegex = /(\.(?<method>connect|delete|get|head|options|patch|post|put|trace))?(\.(?<env>dev|prod|prerender))?$/;
|
|
638
|
+
async function scanAndSyncOptions(nitro) {
|
|
639
|
+
const scannedPlugins = await scanPlugins(nitro);
|
|
640
|
+
for (const plugin of scannedPlugins) if (!nitro.options.plugins.includes(plugin)) nitro.options.plugins.push(plugin);
|
|
641
|
+
if (nitro.options.experimental.tasks) {
|
|
642
|
+
const scannedTasks = await scanTasks(nitro);
|
|
643
|
+
for (const scannedTask of scannedTasks) if (scannedTask.name in nitro.options.tasks) {
|
|
644
|
+
if (!nitro.options.tasks[scannedTask.name].handler) nitro.options.tasks[scannedTask.name].handler = scannedTask.handler;
|
|
645
|
+
} else nitro.options.tasks[scannedTask.name] = {
|
|
646
|
+
handler: scannedTask.handler,
|
|
647
|
+
description: ""
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
const scannedModules = await scanModules(nitro);
|
|
651
|
+
nitro.options.modules = nitro.options.modules || [];
|
|
652
|
+
for (const modPath of scannedModules) if (!nitro.options.modules.includes(modPath)) nitro.options.modules.push(modPath);
|
|
653
|
+
}
|
|
654
|
+
async function scanHandlers(nitro) {
|
|
655
|
+
const middleware = await scanMiddleware(nitro);
|
|
656
|
+
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());
|
|
657
|
+
const seenHandlers = /* @__PURE__ */ new Set();
|
|
658
|
+
nitro.scannedHandlers = [...middleware, ...handlers.filter((h) => {
|
|
659
|
+
const key = `${h.route}\0${h.method}\0${h.env}`;
|
|
660
|
+
return seenHandlers.has(key) ? false : (seenHandlers.add(key), true);
|
|
661
|
+
})];
|
|
662
|
+
nitro.routing.sync();
|
|
663
|
+
return handlers;
|
|
664
|
+
}
|
|
665
|
+
async function scanMiddleware(nitro) {
|
|
666
|
+
return (await scanFiles(nitro, "middleware")).map((file) => {
|
|
667
|
+
return {
|
|
668
|
+
route: "/**",
|
|
669
|
+
middleware: true,
|
|
670
|
+
handler: file.fullPath
|
|
671
|
+
};
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
async function scanServerRoutes(nitro, dir, prefix = "/") {
|
|
675
|
+
return (await scanFiles(nitro, dir)).map((file) => {
|
|
676
|
+
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, "_"));
|
|
677
|
+
route = withLeadingSlash(withoutTrailingSlash(withBase(route, prefix)));
|
|
678
|
+
const suffixMatch = route.match(suffixRegex);
|
|
679
|
+
let method;
|
|
680
|
+
let env;
|
|
681
|
+
if (suffixMatch?.index && suffixMatch?.index >= 0) {
|
|
682
|
+
route = route.slice(0, suffixMatch.index);
|
|
683
|
+
method = suffixMatch.groups?.method;
|
|
684
|
+
env = suffixMatch.groups?.env;
|
|
685
|
+
}
|
|
686
|
+
route = route.replace(/\/index$/, "") || "/";
|
|
687
|
+
return {
|
|
688
|
+
handler: file.fullPath,
|
|
689
|
+
lazy: true,
|
|
690
|
+
middleware: false,
|
|
691
|
+
route,
|
|
692
|
+
method,
|
|
693
|
+
env
|
|
694
|
+
};
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
async function scanPlugins(nitro) {
|
|
698
|
+
return (await scanFiles(nitro, "plugins")).map((f) => f.fullPath);
|
|
699
|
+
}
|
|
700
|
+
async function scanTasks(nitro) {
|
|
701
|
+
return (await scanFiles(nitro, "tasks")).map((f) => {
|
|
702
|
+
return {
|
|
703
|
+
name: f.path.replace(/\/index$/, "").replace(/\.[A-Za-z]+$/, "").replace(/\//g, ":"),
|
|
704
|
+
handler: f.fullPath
|
|
705
|
+
};
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
async function scanModules(nitro) {
|
|
709
|
+
return (await scanFiles(nitro, "modules")).map((f) => f.fullPath);
|
|
710
|
+
}
|
|
711
|
+
async function scanFiles(nitro, name) {
|
|
712
|
+
return await Promise.all(nitro.options.scanDirs.map((dir) => scanDir(nitro, dir, name))).then((r) => r.flat());
|
|
713
|
+
}
|
|
714
|
+
async function scanDir(nitro, dir, name) {
|
|
715
|
+
return (await glob(join(name, GLOB_SCAN_PATTERN), {
|
|
716
|
+
cwd: dir,
|
|
717
|
+
dot: true,
|
|
718
|
+
ignore: nitro.options.ignore,
|
|
719
|
+
absolute: true
|
|
720
|
+
}).catch((error) => {
|
|
721
|
+
if (error?.code === "ENOTDIR") {
|
|
722
|
+
nitro.logger.warn(`Ignoring \`${join(dir, name)}\`. It must be a directory.`);
|
|
723
|
+
return [];
|
|
724
|
+
}
|
|
725
|
+
throw error;
|
|
726
|
+
})).map((fullPath) => {
|
|
727
|
+
return {
|
|
728
|
+
fullPath,
|
|
729
|
+
path: relative(join(dir, name), fullPath)
|
|
730
|
+
};
|
|
731
|
+
}).sort((a, b) => a.path.localeCompare(b.path));
|
|
732
|
+
}
|
|
733
|
+
const isGlobalMiddleware = (h) => !h.method && (!h.route || h.route === "/**");
|
|
734
|
+
function initNitroRouting(nitro) {
|
|
735
|
+
const envConditions = new Set([
|
|
736
|
+
nitro.options.dev ? "dev" : "prod",
|
|
737
|
+
nitro.options.preset,
|
|
738
|
+
nitro.options.preset === "nitro-prerender" ? "prerender" : void 0
|
|
739
|
+
].filter(Boolean));
|
|
740
|
+
const matchesEnv = (h) => {
|
|
741
|
+
const envs = (Array.isArray(h.env) ? h.env : [h.env]).filter(Boolean);
|
|
742
|
+
return envs.length === 0 || envs.some((env) => envConditions.has(env));
|
|
743
|
+
};
|
|
744
|
+
const routes = new Router(nitro.options.baseURL);
|
|
745
|
+
const routeRules = new Router(nitro.options.baseURL);
|
|
746
|
+
const globalMiddleware = [];
|
|
747
|
+
const routedMiddleware = new Router(nitro.options.baseURL);
|
|
748
|
+
const sync = () => {
|
|
749
|
+
routeRules._update(Object.entries(nitro.options.routeRules).map(([route, data]) => ({
|
|
750
|
+
route,
|
|
751
|
+
method: "",
|
|
752
|
+
data: {
|
|
753
|
+
...data,
|
|
754
|
+
_route: route
|
|
755
|
+
}
|
|
756
|
+
})));
|
|
757
|
+
const _routes = [
|
|
758
|
+
...Object.entries(nitro.options.routes).flatMap(([route, handler]) => {
|
|
759
|
+
if (typeof handler === "string") handler = { handler };
|
|
760
|
+
return {
|
|
761
|
+
...handler,
|
|
762
|
+
route,
|
|
763
|
+
middleware: false
|
|
764
|
+
};
|
|
765
|
+
}),
|
|
766
|
+
...nitro.options.handlers,
|
|
767
|
+
...nitro.scannedHandlers
|
|
768
|
+
].filter((h) => h && !h.middleware && matchesEnv(h));
|
|
769
|
+
if (nitro.options.serverEntry && nitro.options.serverEntry.handler) _routes.push({
|
|
770
|
+
route: "/**",
|
|
771
|
+
lazy: false,
|
|
772
|
+
format: nitro.options.serverEntry.format,
|
|
773
|
+
handler: nitro.options.serverEntry.handler
|
|
774
|
+
});
|
|
775
|
+
if (nitro.options.renderer?.handler) _routes.push({
|
|
776
|
+
route: "/**",
|
|
777
|
+
lazy: true,
|
|
778
|
+
handler: nitro.options.renderer?.handler
|
|
779
|
+
});
|
|
780
|
+
routes._update(_routes.map((h) => ({
|
|
781
|
+
...h,
|
|
782
|
+
method: h.method || "",
|
|
783
|
+
data: handlerWithImportHash(h)
|
|
784
|
+
})), { merge: true });
|
|
785
|
+
const _middleware = [...nitro.scannedHandlers, ...nitro.options.handlers].filter((h) => h && h.middleware && matchesEnv(h));
|
|
786
|
+
if (nitro.options.serveStatic) _middleware.unshift({
|
|
787
|
+
route: "/**",
|
|
788
|
+
middleware: true,
|
|
789
|
+
handler: join(runtimeDir, "internal/static")
|
|
790
|
+
});
|
|
791
|
+
globalMiddleware.splice(0, globalMiddleware.length, ..._middleware.filter((h) => isGlobalMiddleware(h)).map((m) => handlerWithImportHash(m)));
|
|
792
|
+
routedMiddleware._update(_middleware.filter((h) => !isGlobalMiddleware(h)).map((h) => ({
|
|
793
|
+
...h,
|
|
794
|
+
method: h.method || "",
|
|
795
|
+
data: handlerWithImportHash(h)
|
|
796
|
+
})));
|
|
797
|
+
};
|
|
798
|
+
nitro.routing = Object.freeze({
|
|
799
|
+
sync,
|
|
800
|
+
routes,
|
|
801
|
+
routeRules,
|
|
802
|
+
globalMiddleware,
|
|
803
|
+
routedMiddleware
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
function handlerWithImportHash(h) {
|
|
807
|
+
const id = (h.lazy ? "_lazy_" : "_") + hash(h.handler).replace(/-/g, "").slice(0, 6);
|
|
808
|
+
return {
|
|
809
|
+
...h,
|
|
810
|
+
_importHash: id
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
var Router = class {
|
|
814
|
+
_routes;
|
|
815
|
+
_router;
|
|
816
|
+
_compiled;
|
|
817
|
+
_baseURL;
|
|
818
|
+
constructor(baseURL) {
|
|
819
|
+
this._update([]);
|
|
820
|
+
this._baseURL = baseURL || "";
|
|
821
|
+
if (this._baseURL.endsWith("/")) this._baseURL = this._baseURL.slice(0, -1);
|
|
822
|
+
}
|
|
823
|
+
get routes() {
|
|
824
|
+
return this._routes;
|
|
825
|
+
}
|
|
826
|
+
_update(routes, opts) {
|
|
827
|
+
this._routes = routes;
|
|
828
|
+
this._router = createRouter();
|
|
829
|
+
this._compiled = void 0;
|
|
830
|
+
for (const route of routes) addRoute(this._router, route.method, this._baseURL + route.route, route.data);
|
|
831
|
+
if (opts?.merge) mergeCatchAll(this._router);
|
|
832
|
+
}
|
|
833
|
+
hasRoutes() {
|
|
834
|
+
return this._routes.length > 0;
|
|
835
|
+
}
|
|
836
|
+
compileToString(opts) {
|
|
837
|
+
const key = opts ? hash(opts) : "";
|
|
838
|
+
this._compiled ||= {};
|
|
839
|
+
if (this._compiled[key]) return this._compiled[key];
|
|
840
|
+
this._compiled[key] = compileRouterToString(this._router, void 0, opts);
|
|
841
|
+
if (this.routes.length === 1 && this.routes[0].route === "/**" && this.routes[0].method === "") {
|
|
842
|
+
const data = (opts?.serialize || JSON.stringify)(this.routes[0].data);
|
|
843
|
+
let retCode = `{data,params:{"_":p.slice(1)}}`;
|
|
844
|
+
if (opts?.matchAll) retCode = `[${retCode}]`;
|
|
845
|
+
this._compiled[key] = `/* @__PURE__ */ (() => {const data=${data};return ((_m, p)=>{return ${retCode};})})()`;
|
|
846
|
+
}
|
|
847
|
+
return this._compiled[key];
|
|
848
|
+
}
|
|
849
|
+
match(method, path) {
|
|
850
|
+
return findRoute(this._router, method, path)?.data;
|
|
851
|
+
}
|
|
852
|
+
matchAll(method, path) {
|
|
853
|
+
return findAllRoutes(this._router, method, path).map((route) => route.data);
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
function mergeCatchAll(router) {
|
|
857
|
+
const handlers = router.root?.wildcard?.methods?.[""];
|
|
858
|
+
if (!handlers || handlers.length < 2) return;
|
|
859
|
+
handlers.splice(0, handlers.length, {
|
|
860
|
+
...handlers[0],
|
|
861
|
+
data: handlers.map((h) => h.data)
|
|
862
|
+
});
|
|
863
|
+
}
|
|
629
864
|
const nitroInstances = globalThis.__nitro_instances__ ||= [];
|
|
630
865
|
const globalKey = "__nitro_builder__";
|
|
631
866
|
function registerNitroInstance(nitro) {
|
|
@@ -675,7 +910,7 @@ async function createNitro(config = {}, opts = {}) {
|
|
|
675
910
|
nitro.hooks.addHooks(nitro.options.hooks);
|
|
676
911
|
await installModules(nitro);
|
|
677
912
|
if (nitro.options.imports) {
|
|
678
|
-
const { createUnimport } = await import("../_build/common.mjs").then((n) => n.
|
|
913
|
+
const { createUnimport } = await import("../_build/common.mjs").then((n) => n.dist_exports);
|
|
679
914
|
nitro.unimport = createUnimport(nitro.options.imports);
|
|
680
915
|
await nitro.unimport.init();
|
|
681
916
|
nitro.options.virtual["#imports"] = () => nitro.unimport?.toExports() || "";
|
|
@@ -963,4 +1198,4 @@ function _pidIsRunning(pid) {
|
|
|
963
1198
|
return false;
|
|
964
1199
|
}
|
|
965
1200
|
}
|
|
966
|
-
export {
|
|
1201
|
+
export { Router, createNitro, listTasks, loadOptions, prerender, runTask, scanHandlers };
|