nitro 3.0.260429-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 +119 -21
- 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 +239 -12
- 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 +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 +15 -15
- package/dist/_libs/httpxy.d.mts +1 -1
- package/dist/_libs/httpxy.mjs +8 -4
- 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 +92 -99
- 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/{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 +117 -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/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 +21 -1756
- package/dist/vite.mjs +23 -337
- package/package.json +37 -39
- 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) {
|
|
@@ -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 };
|
package/dist/_chunks/nitro2.mjs
CHANGED
|
@@ -1,100 +1,127 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
async function
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { getBuildInfo, join, prettyPath, resolve } from "../_build/common.mjs";
|
|
2
|
+
import { proxyFetch, proxyUpgrade } from "../_libs/httpxy.mjs";
|
|
3
|
+
import consola from "consola";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
async function startPreview(opts) {
|
|
6
|
+
const { outputDir, buildInfo } = await getBuildInfo(opts.rootDir);
|
|
7
|
+
if (!buildInfo) throw new Error("Cannot load nitro build info. Make sure to build first.");
|
|
8
|
+
const info = [
|
|
9
|
+
["Build Directory:", prettyPath(outputDir)],
|
|
10
|
+
["Date:", buildInfo.date && new Date(buildInfo.date).toLocaleString()],
|
|
11
|
+
["Nitro Version:", buildInfo.versions.nitro],
|
|
12
|
+
["Nitro Preset:", buildInfo.preset],
|
|
13
|
+
buildInfo.framework?.name !== "nitro" && ["Framework:", buildInfo.framework?.name + (buildInfo.framework?.version ? ` (v${buildInfo.framework.version})` : "")]
|
|
14
|
+
].filter((i) => i && i[1]);
|
|
15
|
+
consola.box({
|
|
16
|
+
title: " [Build Info] ",
|
|
17
|
+
message: info.map((i) => `- ${i[0]} ${i[1]}`).join("\n")
|
|
18
|
+
});
|
|
19
|
+
const dotEnvEntries = await loadPreviewDotEnv(opts.rootDir);
|
|
20
|
+
if (dotEnvEntries.length > 0) consola.box({
|
|
21
|
+
title: " [Environment Variables] ",
|
|
22
|
+
message: [
|
|
23
|
+
"Loaded variables from .env files (preview mode only).",
|
|
24
|
+
"Set platform environment variables for production:",
|
|
25
|
+
...dotEnvEntries.map(([key, val]) => ` - ${key}`)
|
|
26
|
+
].join("\n")
|
|
27
|
+
});
|
|
28
|
+
if (buildInfo.preset.includes("cloudflare")) {
|
|
29
|
+
if (!buildInfo.commands?.preview) throw new Error(`No nitro build preview command found for the "${buildInfo.preset}" preset.`);
|
|
30
|
+
return await runPreviewCommand({
|
|
31
|
+
command: buildInfo.commands.preview,
|
|
32
|
+
rootDir: opts.rootDir,
|
|
33
|
+
env: dotEnvEntries
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
let fetchHandler = () => Promise.resolve(new Response("Not Found", { status: 404 }));
|
|
37
|
+
if (buildInfo.serverEntry) {
|
|
38
|
+
for (const [key, val] of dotEnvEntries) if (!process.env[key]) process.env[key] = val;
|
|
39
|
+
const { loadServerEntry } = await import("srvx/loader");
|
|
40
|
+
const entry = await loadServerEntry({
|
|
41
|
+
entry: resolve(outputDir, buildInfo.serverEntry),
|
|
42
|
+
...opts.loader
|
|
43
|
+
});
|
|
44
|
+
if (entry.fetch) fetchHandler = entry.fetch;
|
|
45
|
+
}
|
|
46
|
+
if (buildInfo.publicDir) {
|
|
47
|
+
const { serveStatic } = await import("srvx/static");
|
|
48
|
+
const staticHandler = serveStatic({ dir: join(outputDir, buildInfo.publicDir) });
|
|
49
|
+
const originalFetchHandler = fetchHandler;
|
|
50
|
+
fetchHandler = async (req) => {
|
|
51
|
+
const staticRes = await staticHandler(req, () => void 0);
|
|
52
|
+
if (staticRes) return staticRes;
|
|
53
|
+
return originalFetchHandler(req);
|
|
15
54
|
};
|
|
16
55
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
56
|
+
return {
|
|
57
|
+
fetch: fetchHandler,
|
|
58
|
+
async close() {}
|
|
59
|
+
};
|
|
20
60
|
}
|
|
21
|
-
async function
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return handlers;
|
|
31
|
-
}
|
|
32
|
-
async function scanMiddleware(nitro) {
|
|
33
|
-
return (await scanFiles(nitro, "middleware")).map((file) => {
|
|
34
|
-
return {
|
|
35
|
-
route: "/**",
|
|
36
|
-
middleware: true,
|
|
37
|
-
handler: file.fullPath
|
|
38
|
-
};
|
|
61
|
+
async function loadPreviewDotEnv(root) {
|
|
62
|
+
const { loadDotenv } = await import("../_libs/c12+rc9.mjs").then((n) => n.dist_exports);
|
|
63
|
+
const env = await loadDotenv({
|
|
64
|
+
cwd: root,
|
|
65
|
+
fileName: [
|
|
66
|
+
".env.preview",
|
|
67
|
+
".env.production",
|
|
68
|
+
".env"
|
|
69
|
+
]
|
|
39
70
|
});
|
|
71
|
+
return Object.entries(env).filter(([_key, val]) => val);
|
|
40
72
|
}
|
|
41
|
-
async function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
73
|
+
async function runPreviewCommand(opts) {
|
|
74
|
+
const [arg0, ...args] = opts.command.split(" ");
|
|
75
|
+
consola.info(`Spawning preview server...`);
|
|
76
|
+
consola.info(opts.command);
|
|
77
|
+
console.log("");
|
|
78
|
+
const { getRandomPort, waitForPort } = await import("get-port-please");
|
|
79
|
+
const randomPort = await getRandomPort();
|
|
80
|
+
const child = spawn(arg0, [
|
|
81
|
+
...args,
|
|
82
|
+
"--port",
|
|
83
|
+
String(randomPort),
|
|
84
|
+
"--host",
|
|
85
|
+
"localhost"
|
|
86
|
+
], {
|
|
87
|
+
stdio: "inherit",
|
|
88
|
+
cwd: opts.rootDir,
|
|
89
|
+
env: {
|
|
90
|
+
...process.env,
|
|
91
|
+
...Object.fromEntries(opts.env ?? []),
|
|
92
|
+
PORT: String(randomPort)
|
|
52
93
|
}
|
|
53
|
-
route = route.replace(/\/index$/, "") || "/";
|
|
54
|
-
return {
|
|
55
|
-
handler: file.fullPath,
|
|
56
|
-
lazy: true,
|
|
57
|
-
middleware: false,
|
|
58
|
-
route,
|
|
59
|
-
method,
|
|
60
|
-
env
|
|
61
|
-
};
|
|
62
94
|
});
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return (await scanFiles(nitro, "tasks")).map((f) => {
|
|
69
|
-
return {
|
|
70
|
-
name: f.path.replace(/\/index$/, "").replace(/\.[A-Za-z]+$/, "").replace(/\//g, ":"),
|
|
71
|
-
handler: f.fullPath
|
|
72
|
-
};
|
|
95
|
+
const killChild = (signal) => {
|
|
96
|
+
if (child && !child.killed) child.kill(signal);
|
|
97
|
+
};
|
|
98
|
+
for (const sig of ["SIGINT", "SIGHUP"]) process.once(sig, () => {
|
|
99
|
+
killChild(sig);
|
|
73
100
|
});
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
101
|
+
child.once("exit", (code) => {
|
|
102
|
+
if (code && code !== 0) consola.error(`[nitro] Preview server exited with code ${code}`);
|
|
103
|
+
});
|
|
104
|
+
await waitForPort(randomPort, {
|
|
105
|
+
retries: 20,
|
|
106
|
+
delay: 500,
|
|
107
|
+
host: "localhost"
|
|
108
|
+
});
|
|
109
|
+
return {
|
|
110
|
+
fetch(req) {
|
|
111
|
+
return proxyFetch({
|
|
112
|
+
port: randomPort,
|
|
113
|
+
host: "localhost"
|
|
114
|
+
}, req);
|
|
115
|
+
},
|
|
116
|
+
async upgrade(req, socket, head, opts) {
|
|
117
|
+
await proxyUpgrade({
|
|
118
|
+
port: randomPort,
|
|
119
|
+
host: "localhost"
|
|
120
|
+
}, req, socket, head, opts);
|
|
121
|
+
},
|
|
122
|
+
async close() {
|
|
123
|
+
killChild("SIGTERM");
|
|
91
124
|
}
|
|
92
|
-
|
|
93
|
-
})).map((fullPath) => {
|
|
94
|
-
return {
|
|
95
|
-
fullPath,
|
|
96
|
-
path: relative(join(dir, name), fullPath)
|
|
97
|
-
};
|
|
98
|
-
}).sort((a, b) => a.path.localeCompare(b.path));
|
|
125
|
+
};
|
|
99
126
|
}
|
|
100
|
-
export {
|
|
127
|
+
export { startPreview };
|