nitro-nightly 4.0.0-20251010-091516-7cafddba → 4.0.0-20251030-091344-d4418b98
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/{_chunks/build2.mjs → _build/build.mjs} +108 -58
- package/dist/_build/build2.mjs +556 -0
- package/dist/_build/info.mjs +1002 -0
- package/dist/_build/prepare.mjs +1511 -0
- package/dist/_build/snapshot.mjs +185 -0
- package/dist/{_chunks/plugin.mjs → _build/vite.mjs} +295 -296
- package/dist/_build/vite2.mjs +149 -0
- package/dist/_chunks/_deps/@jridgewell/gen-mapping.mjs +189 -0
- package/dist/_chunks/_deps/@jridgewell/remapping.mjs +137 -0
- package/dist/_chunks/_deps/@jridgewell/resolve-uri.mjs +231 -0
- package/dist/_chunks/_deps/@jridgewell/sourcemap-codec.mjs +173 -0
- package/dist/_chunks/_deps/@jridgewell/trace-mapping.mjs +170 -0
- package/dist/_chunks/_deps/@pi0/vite-plugin-fullstack.mjs +575 -0
- package/dist/_chunks/_deps/@rollup/plugin-alias.mjs +89 -0
- package/dist/_chunks/_deps/@rollup/plugin-commonjs.mjs +2376 -0
- package/dist/_chunks/{index2.mjs → _deps/@rollup/plugin-inject.mjs} +5 -90
- package/dist/_chunks/_deps/@rollup/plugin-json.mjs +37 -0
- package/dist/_chunks/_deps/@rollup/plugin-node-resolve.mjs +1386 -0
- package/dist/_chunks/_deps/@rollup/plugin-replace.mjs +133 -0
- package/dist/_chunks/_deps/@rollup/pluginutils.mjs +346 -0
- package/dist/_chunks/_deps/acorn.mjs +6225 -0
- package/dist/_chunks/_deps/c12.mjs +510 -0
- package/dist/_chunks/_deps/chokidar.mjs +1428 -0
- package/dist/_chunks/_deps/citty.mjs +460 -0
- package/dist/_chunks/_deps/commondir.mjs +77 -0
- package/dist/_chunks/_deps/compatx.mjs +76 -0
- package/dist/_chunks/_deps/confbox.mjs +300 -0
- package/dist/_chunks/_deps/debug.mjs +885 -0
- package/dist/_chunks/_deps/deepmerge.mjs +147 -0
- package/dist/_chunks/_deps/depd.mjs +550 -0
- package/dist/_chunks/_deps/dot-prop.mjs +282 -0
- package/dist/_chunks/_deps/dotenv.mjs +555 -0
- package/dist/_chunks/_deps/duplexer.mjs +1 -0
- package/dist/_chunks/_deps/ee-first.mjs +104 -0
- package/dist/_chunks/_deps/encodeurl.mjs +69 -0
- package/dist/_chunks/_deps/escape-html.mjs +87 -0
- package/dist/_chunks/_deps/escape-string-regexp.mjs +13 -0
- package/dist/_chunks/_deps/estree-walker.mjs +433 -0
- package/dist/_chunks/_deps/etag.mjs +147 -0
- package/dist/_chunks/_deps/exsolve.mjs +1416 -0
- package/dist/_chunks/_deps/fdir.mjs +569 -0
- package/dist/_chunks/_deps/fresh.mjs +145 -0
- package/dist/_chunks/_deps/function-bind.mjs +106 -0
- package/dist/_chunks/{index4.mjs → _deps/giget.mjs} +21 -776
- package/dist/_chunks/_deps/gzip-size.mjs +19 -0
- package/dist/_chunks/_deps/hasown.mjs +19 -0
- package/dist/_chunks/_deps/http-errors.mjs +307 -0
- package/dist/_chunks/_deps/httpxy.mjs +580 -0
- package/dist/_chunks/_deps/inherits.mjs +57 -0
- package/dist/_chunks/_deps/is-core-module.mjs +596 -0
- package/dist/_chunks/_deps/is-module.mjs +25 -0
- package/dist/_chunks/_deps/is-reference.mjs +31 -0
- package/dist/_chunks/_deps/js-tokens.mjs +411 -0
- package/dist/_chunks/_deps/klona.mjs +137 -0
- package/dist/_chunks/_deps/knitwork.mjs +172 -0
- package/dist/_chunks/_deps/local-pkg.mjs +163 -0
- package/dist/_chunks/_deps/magic-string.mjs +1296 -0
- package/dist/_chunks/_deps/mime-db.mjs +11685 -0
- package/dist/_chunks/_deps/mime-types.mjs +287 -0
- package/dist/_chunks/_deps/mime.mjs +1172 -0
- package/dist/_chunks/_deps/mlly.mjs +2413 -0
- package/dist/_chunks/_deps/ms.mjs +172 -0
- package/dist/_chunks/_deps/node-fetch-native.mjs +3 -0
- package/dist/_chunks/_deps/nypm.mjs +219 -0
- package/dist/_chunks/_deps/on-finished.mjs +246 -0
- package/dist/_chunks/_deps/parseurl.mjs +168 -0
- package/dist/_chunks/_deps/path-parse.mjs +85 -0
- package/dist/_chunks/{pathe.M-eThtNZ.mjs → _deps/pathe.mjs} +48 -1
- package/dist/_chunks/_deps/perfect-debounce.mjs +88 -0
- package/dist/_chunks/_deps/picomatch.mjs +2144 -0
- package/dist/_chunks/_deps/pkg-types.mjs +247 -0
- package/dist/_chunks/{snapshot.mjs → _deps/pretty-bytes.mjs} +1 -105
- package/dist/_chunks/_deps/quansync.mjs +99 -0
- package/dist/_chunks/_deps/range-parser.mjs +171 -0
- package/dist/_chunks/_deps/rc9.mjs +219 -0
- package/dist/_chunks/_deps/readdirp.mjs +245 -0
- package/dist/_chunks/_deps/resolve.mjs +1260 -0
- package/dist/_chunks/_deps/rou3.mjs +326 -0
- package/dist/_chunks/_deps/send.mjs +1022 -0
- package/dist/_chunks/_deps/serve-static.mjs +228 -0
- package/dist/_chunks/_deps/setprototypeof.mjs +26 -0
- package/dist/_chunks/_deps/statuses.mjs +457 -0
- package/dist/_chunks/_deps/std-env.mjs +3 -0
- package/dist/_chunks/_deps/strip-literal.mjs +67 -0
- package/dist/_chunks/_deps/supports-color.mjs +44 -0
- package/dist/_chunks/_deps/tinyexec.mjs +552 -0
- package/dist/_chunks/_deps/tinyglobby.mjs +293 -0
- package/dist/_chunks/_deps/toidentifier.mjs +41 -0
- package/dist/_chunks/_deps/ultrahtml.mjs +3 -0
- package/dist/_chunks/_deps/unimport.mjs +2267 -0
- package/dist/_chunks/_deps/unplugin-utils.mjs +65 -0
- package/dist/_chunks/_deps/unplugin.mjs +1294 -0
- package/dist/_chunks/_deps/untyped.mjs +375 -0
- package/dist/_chunks/{info.mjs → _deps/unwasm.mjs} +8 -4206
- package/dist/_chunks/_deps/webpack-virtual-modules.mjs +360 -0
- package/dist/_chunks/_presets/_all.mjs +59 -0
- package/dist/_chunks/_presets/_nitro.mjs +74 -0
- package/dist/_chunks/_presets/_resolve.mjs +64 -0
- package/dist/_chunks/_presets/_static.mjs +69 -0
- package/dist/_chunks/_presets/_types.mjs +3 -0
- package/dist/_chunks/_presets/_utils.mjs +31 -0
- package/dist/_chunks/_presets/alwaysdata.mjs +17 -0
- package/dist/_chunks/_presets/aws-amplify.mjs +111 -0
- package/dist/_chunks/_presets/aws-lambda.mjs +23 -0
- package/dist/_chunks/_presets/azure.mjs +162 -0
- package/dist/_chunks/_presets/bun.mjs +19 -0
- package/dist/_chunks/_presets/cleavr.mjs +15 -0
- package/dist/_chunks/_presets/cloudflare.mjs +608 -0
- package/dist/_chunks/_presets/deno.mjs +196 -0
- package/dist/_chunks/_presets/digitalocean.mjs +14 -0
- package/dist/_chunks/_presets/firebase.mjs +47 -0
- package/dist/_chunks/_presets/flightcontrol.mjs +14 -0
- package/dist/_chunks/_presets/genezio.mjs +13 -0
- package/dist/_chunks/_presets/heroku.mjs +14 -0
- package/dist/_chunks/_presets/iis.mjs +194 -0
- package/dist/_chunks/_presets/index.mjs +62 -0
- package/dist/_chunks/_presets/koyeb.mjs +14 -0
- package/dist/_chunks/_presets/netlify.mjs +241 -0
- package/dist/_chunks/_presets/node.mjs +54 -0
- package/dist/_chunks/_presets/platform.mjs +14 -0
- package/dist/_chunks/_presets/render.mjs +14 -0
- package/dist/_chunks/_presets/standard.mjs +23 -0
- package/dist/_chunks/_presets/stormkit.mjs +18 -0
- package/dist/_chunks/_presets/vercel.mjs +375 -0
- package/dist/_chunks/_presets/winterjs.mjs +22 -0
- package/dist/_chunks/_presets/zeabur.mjs +69 -0
- package/dist/_chunks/_presets/zerops.mjs +27 -0
- package/dist/_chunks/app.mjs +9 -19206
- package/dist/_chunks/{index3.mjs → builder.mjs} +560 -970
- package/dist/_chunks/server.mjs +6 -4
- package/dist/{cli → _cli}/build.mjs +3 -8
- package/dist/{cli → _cli}/dev.mjs +38 -12
- package/dist/{cli/index2.mjs → _cli/index.mjs} +1 -2
- package/dist/{cli → _cli}/list.mjs +3 -4
- package/dist/{cli → _cli}/prepare.mjs +3 -4
- package/dist/{cli → _cli}/run.mjs +3 -4
- package/dist/{index.d.mts → builder.d.mts} +11 -11
- package/dist/builder.mjs +117 -0
- package/dist/cli/index.mjs +7 -464
- package/dist/node_modules/@speed-highlight/core/dist/index.js +1 -1
- package/dist/node_modules/@speed-highlight/core/dist/terminal.js +1 -1
- package/dist/node_modules/@speed-highlight/core/package.json +7 -3
- package/dist/node_modules/cookie-es/dist/index.mjs +262 -0
- package/dist/node_modules/cookie-es/package.json +37 -0
- package/dist/node_modules/hookable/dist/index.mjs +243 -266
- package/dist/node_modules/hookable/package.json +29 -26
- package/dist/node_modules/rendu/dist/index.mjs +380 -0
- package/dist/node_modules/rendu/package.json +47 -0
- package/dist/presets/_nitro/runtime/nitro-dev.mjs +4 -7
- package/dist/presets/_nitro/runtime/nitro-prerenderer.mjs +5 -4
- package/dist/presets/azure/runtime/azure-swa.mjs +1 -1
- package/dist/presets/cloudflare/runtime/_module-handler.mjs +7 -6
- package/dist/presets/cloudflare/runtime/cloudflare-durable.mjs +4 -5
- package/dist/presets/cloudflare/runtime/plugin.dev.mjs +7 -10
- package/dist/presets/cloudflare/runtime/shims/workers.dev.d.mts +21 -0
- package/dist/presets/cloudflare/runtime/shims/workers.dev.mjs +27 -0
- package/dist/presets/node/runtime/node-server.mjs +1 -1
- package/dist/runtime/index.d.mts +1 -1
- package/dist/runtime/index.mjs +1 -1
- package/dist/runtime/internal/app.d.mts +3 -1
- package/dist/runtime/internal/app.mjs +90 -64
- package/dist/runtime/internal/error/prod.d.mts +3 -2
- package/dist/runtime/internal/error/prod.mjs +9 -13
- package/dist/runtime/internal/renderer.mjs +4 -4
- package/dist/runtime/internal/routes/dev-tasks.d.mts +30 -2
- package/dist/runtime/internal/shutdown.d.mts +1 -2
- package/dist/runtime/internal/shutdown.mjs +3 -2
- package/dist/runtime/internal/task.mjs +1 -2
- package/dist/types/index.d.mts +1603 -10
- package/dist/vite.d.mts +5 -0
- package/dist/vite.mjs +94 -30
- package/lib/index.mjs +1 -0
- package/lib/indexd.mts +1 -0
- package/lib/runtime/meta.mjs +35 -0
- package/package.json +40 -41
- package/dist/_chunks/build.mjs +0 -84
- package/dist/_chunks/build3.mjs +0 -6452
- package/dist/_chunks/detect-acorn.mjs +0 -503
- package/dist/_chunks/index.mjs +0 -22242
- package/dist/_chunks/json5.mjs +0 -68
- package/dist/_chunks/jsonc.mjs +0 -51
- package/dist/_chunks/toml.mjs +0 -259
- package/dist/_chunks/yaml.mjs +0 -86
- package/dist/index.mjs +0 -55
- package/dist/node_modules/klona/dist/index.mjs +0 -81
- package/dist/node_modules/klona/full/index.mjs +0 -53
- package/dist/node_modules/klona/package.json +0 -74
- package/dist/node_modules/std-env/dist/index.mjs +0 -1
- package/dist/node_modules/std-env/package.json +0 -46
- package/dist/presets.mjs +0 -2460
- package/dist/runtime/internal/debug.d.mts +0 -2
- package/dist/runtime/internal/debug.mjs +0 -5
- package/lib/runtime-meta.mjs +0 -38
- /package/dist/{cli → _cli}/common.mjs +0 -0
- /package/lib/{runtime-meta.d.mts → runtime/meta.d.mts} +0 -0
|
@@ -0,0 +1,1511 @@
|
|
|
1
|
+
import consola, { consola as consola$1 } from 'consola';
|
|
2
|
+
import { Hookable, createDebugger } from 'hookable';
|
|
3
|
+
import { w as watchConfig, l as loadConfig } from '../_chunks/_deps/c12.mjs';
|
|
4
|
+
import { r as resolveCompatibilityDatesFromEnv, a as resolveCompatibilityDates } from '../_chunks/_deps/compatx.mjs';
|
|
5
|
+
import { k as klona, a as klona$1 } from '../_chunks/_deps/klona.mjs';
|
|
6
|
+
import { runtimeDir, pkgDir } from 'nitro/runtime/meta';
|
|
7
|
+
import { d, a } from '../_chunks/_deps/std-env.mjs';
|
|
8
|
+
import { r as resolve, j as join, a as relative, d as dirname, n as normalize } from '../_chunks/_deps/pathe.mjs';
|
|
9
|
+
import { existsSync, promises } from 'node:fs';
|
|
10
|
+
import defu$1, { defu } from 'defu';
|
|
11
|
+
import { withLeadingSlash, withoutTrailingSlash, withTrailingSlash, withBase } from 'ufo';
|
|
12
|
+
import { e as escapeStringRegexp } from '../_chunks/_deps/escape-string-regexp.mjs';
|
|
13
|
+
import { g as resolveModuleExportNames } from '../_chunks/_deps/mlly.mjs';
|
|
14
|
+
import { f as findWorkspaceDir } from '../_chunks/_deps/pkg-types.mjs';
|
|
15
|
+
import { r as resolveModulePath } from '../_chunks/_deps/exsolve.mjs';
|
|
16
|
+
import { createJiti } from 'jiti';
|
|
17
|
+
import { g as glob } from '../_chunks/_deps/tinyglobby.mjs';
|
|
18
|
+
import fsp, { stat, mkdir, writeFile as writeFile$1, readFile } from 'node:fs/promises';
|
|
19
|
+
import { ofetch } from 'ofetch';
|
|
20
|
+
import { createStorage as createStorage$1, builtinDrivers } from 'unstorage';
|
|
21
|
+
import { c as createRouter, a as addRoute, b as compileRouterToString, d as findRoute, f as findAllRoutes } from '../_chunks/_deps/rou3.mjs';
|
|
22
|
+
import { hash } from 'ohash';
|
|
23
|
+
import { c as createUnimport } from '../_chunks/_deps/unimport.mjs';
|
|
24
|
+
import { colors } from 'consola/utils';
|
|
25
|
+
import { g as getProperty } from '../_chunks/_deps/dot-prop.mjs';
|
|
26
|
+
import zlib from 'node:zlib';
|
|
27
|
+
import { m as mime } from '../_chunks/_deps/mime.mjs';
|
|
28
|
+
|
|
29
|
+
const NitroDefaults = {
|
|
30
|
+
// General
|
|
31
|
+
compatibilityDate: "latest",
|
|
32
|
+
debug: d,
|
|
33
|
+
logLevel: a ? 1 : 3,
|
|
34
|
+
runtimeConfig: { app: {}, nitro: {} },
|
|
35
|
+
// Dirs
|
|
36
|
+
scanDirs: [],
|
|
37
|
+
buildDir: ".nitro",
|
|
38
|
+
output: {
|
|
39
|
+
dir: "{{ rootDir }}/.output",
|
|
40
|
+
serverDir: "{{ output.dir }}/server",
|
|
41
|
+
publicDir: "{{ output.dir }}/public"
|
|
42
|
+
},
|
|
43
|
+
// Features
|
|
44
|
+
experimental: {},
|
|
45
|
+
future: {},
|
|
46
|
+
storage: {},
|
|
47
|
+
devStorage: {},
|
|
48
|
+
bundledStorage: [],
|
|
49
|
+
publicAssets: [],
|
|
50
|
+
serverAssets: [],
|
|
51
|
+
plugins: [],
|
|
52
|
+
tasks: {},
|
|
53
|
+
scheduledTasks: {},
|
|
54
|
+
imports: false,
|
|
55
|
+
virtual: {},
|
|
56
|
+
compressPublicAssets: false,
|
|
57
|
+
ignore: [],
|
|
58
|
+
// Dev
|
|
59
|
+
dev: false,
|
|
60
|
+
devServer: { watch: [] },
|
|
61
|
+
watchOptions: { ignoreInitial: true },
|
|
62
|
+
devProxy: {},
|
|
63
|
+
// Logging
|
|
64
|
+
logging: {
|
|
65
|
+
compressedSizes: true,
|
|
66
|
+
buildSuccess: true
|
|
67
|
+
},
|
|
68
|
+
// Routing
|
|
69
|
+
baseURL: process.env.NITRO_APP_BASE_URL || "/",
|
|
70
|
+
handlers: [],
|
|
71
|
+
devHandlers: [],
|
|
72
|
+
errorHandler: void 0,
|
|
73
|
+
routeRules: {},
|
|
74
|
+
prerender: {
|
|
75
|
+
autoSubfolderIndex: true,
|
|
76
|
+
concurrency: 1,
|
|
77
|
+
interval: 0,
|
|
78
|
+
retry: 3,
|
|
79
|
+
retryDelay: 500,
|
|
80
|
+
failOnError: false,
|
|
81
|
+
crawlLinks: false,
|
|
82
|
+
ignore: [],
|
|
83
|
+
routes: []
|
|
84
|
+
},
|
|
85
|
+
// Rollup
|
|
86
|
+
builder: void 0,
|
|
87
|
+
moduleSideEffects: ["unenv/polyfill/", resolve(runtimeDir, "polyfill/")],
|
|
88
|
+
replace: {},
|
|
89
|
+
node: true,
|
|
90
|
+
sourceMap: true,
|
|
91
|
+
esbuild: {
|
|
92
|
+
options: {
|
|
93
|
+
jsxFactory: "h",
|
|
94
|
+
jsxFragment: "Fragment"
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
// Advanced
|
|
98
|
+
typescript: {
|
|
99
|
+
strict: true,
|
|
100
|
+
generateRuntimeConfigTypes: false,
|
|
101
|
+
generateTsConfig: false,
|
|
102
|
+
tsconfigPath: "types/tsconfig.json",
|
|
103
|
+
internalPaths: false,
|
|
104
|
+
tsConfig: {}
|
|
105
|
+
},
|
|
106
|
+
nodeModulesDirs: [],
|
|
107
|
+
hooks: {},
|
|
108
|
+
commands: {},
|
|
109
|
+
// Framework
|
|
110
|
+
framework: {
|
|
111
|
+
name: "nitro",
|
|
112
|
+
version: ""
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
async function resolveAssetsOptions(options) {
|
|
117
|
+
for (const publicAsset of options.publicAssets) {
|
|
118
|
+
publicAsset.dir = resolve(options.srcDir, publicAsset.dir);
|
|
119
|
+
publicAsset.baseURL = withLeadingSlash(
|
|
120
|
+
withoutTrailingSlash(publicAsset.baseURL || "/")
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
for (const dir of options.scanDirs) {
|
|
124
|
+
const publicDir = resolve(dir, "public");
|
|
125
|
+
if (!existsSync(publicDir)) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (options.publicAssets.some((asset) => asset.dir === publicDir)) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
options.publicAssets.push({ dir: publicDir });
|
|
132
|
+
}
|
|
133
|
+
for (const serverAsset of options.serverAssets) {
|
|
134
|
+
serverAsset.dir = resolve(options.srcDir, serverAsset.dir);
|
|
135
|
+
}
|
|
136
|
+
options.serverAssets.push({
|
|
137
|
+
baseName: "server",
|
|
138
|
+
dir: resolve(options.srcDir, "assets")
|
|
139
|
+
});
|
|
140
|
+
for (const asset of options.publicAssets) {
|
|
141
|
+
asset.baseURL = asset.baseURL || "/";
|
|
142
|
+
const isTopLevel = asset.baseURL === "/";
|
|
143
|
+
asset.fallthrough = asset.fallthrough ?? isTopLevel;
|
|
144
|
+
const routeRule = options.routeRules[asset.baseURL + "/**"];
|
|
145
|
+
asset.maxAge = routeRule?.cache?.maxAge ?? asset.maxAge ?? 0;
|
|
146
|
+
if (asset.maxAge && !asset.fallthrough) {
|
|
147
|
+
options.routeRules[asset.baseURL + "/**"] = defu(routeRule, {
|
|
148
|
+
headers: {
|
|
149
|
+
"cache-control": `public, max-age=${asset.maxAge}, immutable`
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function resolveCompatibilityOptions(options) {
|
|
157
|
+
options.compatibilityDate = resolveCompatibilityDatesFromEnv(
|
|
158
|
+
options.compatibilityDate
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function resolveDatabaseOptions(options) {
|
|
163
|
+
if (options.experimental.database && options.imports) {
|
|
164
|
+
options.imports.presets.push({
|
|
165
|
+
from: "nitro/runtime/internal/database",
|
|
166
|
+
imports: ["useDatabase"]
|
|
167
|
+
});
|
|
168
|
+
if (options.dev && !options.database && !options.devDatabase) {
|
|
169
|
+
options.devDatabase = {
|
|
170
|
+
default: {
|
|
171
|
+
connector: "sqlite",
|
|
172
|
+
options: {
|
|
173
|
+
cwd: options.rootDir
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
} else if (options.node && !options.database) {
|
|
178
|
+
options.database = {
|
|
179
|
+
default: {
|
|
180
|
+
connector: "sqlite",
|
|
181
|
+
options: {}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function resolveExportConditionsOptions(options) {
|
|
189
|
+
options.exportConditions = _resolveExportConditions(
|
|
190
|
+
options.exportConditions || [],
|
|
191
|
+
{ dev: options.dev, node: options.node, wasm: options.experimental.wasm }
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
function _resolveExportConditions(conditions, opts) {
|
|
195
|
+
const resolvedConditions = [];
|
|
196
|
+
resolvedConditions.push(opts.dev ? "development" : "production");
|
|
197
|
+
resolvedConditions.push(...conditions);
|
|
198
|
+
if (opts.node) {
|
|
199
|
+
resolvedConditions.push("node");
|
|
200
|
+
} else {
|
|
201
|
+
resolvedConditions.push(
|
|
202
|
+
"wintercg",
|
|
203
|
+
"worker",
|
|
204
|
+
"web",
|
|
205
|
+
"browser",
|
|
206
|
+
"workerd",
|
|
207
|
+
"edge-light",
|
|
208
|
+
"netlify",
|
|
209
|
+
"edge-routine",
|
|
210
|
+
"deno"
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
if (opts.wasm) {
|
|
214
|
+
resolvedConditions.push("wasm", "unwasm");
|
|
215
|
+
}
|
|
216
|
+
resolvedConditions.push("import", "default");
|
|
217
|
+
if ("Bun" in globalThis) {
|
|
218
|
+
resolvedConditions.push("bun");
|
|
219
|
+
} else if ("Deno" in globalThis) {
|
|
220
|
+
resolvedConditions.push("deno");
|
|
221
|
+
}
|
|
222
|
+
return resolvedConditions.filter(
|
|
223
|
+
(c, i) => resolvedConditions.indexOf(c) === i
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function resolveImportsOptions(options) {
|
|
228
|
+
if (options.imports === false) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
options.imports.presets ??= [];
|
|
232
|
+
options.imports.presets.push(...getNitroImportsPreset());
|
|
233
|
+
const h3Exports = await resolveModuleExportNames("h3", {
|
|
234
|
+
url: import.meta.url
|
|
235
|
+
});
|
|
236
|
+
options.imports.presets ??= [];
|
|
237
|
+
options.imports.presets.push({
|
|
238
|
+
from: "h3",
|
|
239
|
+
imports: h3Exports.filter((n) => !/^[A-Z]/.test(n) && n !== "use")
|
|
240
|
+
});
|
|
241
|
+
options.imports.dirs ??= [];
|
|
242
|
+
options.imports.dirs.push(
|
|
243
|
+
...options.scanDirs.map((dir) => join(dir, "utils/**/*"))
|
|
244
|
+
);
|
|
245
|
+
if (Array.isArray(options.imports.exclude) && options.imports.exclude.length === 0) {
|
|
246
|
+
options.imports.exclude.push(/[/\\]\.git[/\\]/);
|
|
247
|
+
options.imports.exclude.push(options.buildDir);
|
|
248
|
+
const scanDirsInNodeModules = options.scanDirs.map((dir) => dir.match(/(?<=\/)node_modules\/(.+)$/)?.[1]).filter(Boolean);
|
|
249
|
+
options.imports.exclude.push(
|
|
250
|
+
scanDirsInNodeModules.length > 0 ? new RegExp(
|
|
251
|
+
`node_modules\\/(?!${scanDirsInNodeModules.map((dir) => escapeStringRegexp(dir)).join("|")})`
|
|
252
|
+
) : /[/\\]node_modules[/\\]/
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function getNitroImportsPreset() {
|
|
257
|
+
return [
|
|
258
|
+
{
|
|
259
|
+
from: "nitro/runtime/internal/app",
|
|
260
|
+
imports: ["useNitroApp"]
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
from: "nitro/runtime/internal/runtime-config",
|
|
264
|
+
imports: ["useRuntimeConfig"]
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
from: "nitro/runtime/internal/plugin",
|
|
268
|
+
imports: ["defineNitroPlugin", "nitroPlugin"]
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
from: "nitro/runtime/internal/cache",
|
|
272
|
+
imports: [
|
|
273
|
+
"defineCachedFunction",
|
|
274
|
+
"defineCachedEventHandler",
|
|
275
|
+
"defineCachedHandler",
|
|
276
|
+
"cachedFunction",
|
|
277
|
+
"cachedEventHandler"
|
|
278
|
+
]
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
from: "nitro/runtime/internal/storage",
|
|
282
|
+
imports: ["useStorage"]
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
from: "nitro/runtime/internal/renderer",
|
|
286
|
+
imports: ["defineRenderHandler"]
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
from: "nitro/runtime/internal/meta",
|
|
290
|
+
imports: ["defineRouteMeta"]
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
from: "nitro/runtime/internal/route-rules",
|
|
294
|
+
imports: ["getRouteRules"]
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
from: "nitro/runtime/internal/context",
|
|
298
|
+
imports: ["useRequest"]
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
from: "nitro/runtime/internal/task",
|
|
302
|
+
imports: ["defineTask", "runTask"]
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
from: "nitro/runtime/internal/error/utils",
|
|
306
|
+
imports: ["defineNitroErrorHandler"]
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
from: "nitro/deps/ofetch",
|
|
310
|
+
imports: ["$fetch"]
|
|
311
|
+
}
|
|
312
|
+
];
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async function resolveOpenAPIOptions(options) {
|
|
316
|
+
if (!options.experimental.openAPI) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (!options.dev && !options.openAPI?.production) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
const shouldPrerender = !options.dev && options.openAPI?.production === "prerender";
|
|
323
|
+
const handlersEnv = shouldPrerender ? "prerender" : "";
|
|
324
|
+
const prerenderRoutes = [];
|
|
325
|
+
const jsonRoute = options.openAPI?.route || "/_openapi.json";
|
|
326
|
+
prerenderRoutes.push(jsonRoute);
|
|
327
|
+
options.handlers.push({
|
|
328
|
+
route: jsonRoute,
|
|
329
|
+
env: handlersEnv,
|
|
330
|
+
handler: join(runtimeDir, "internal/routes/openapi")
|
|
331
|
+
});
|
|
332
|
+
if (options.openAPI?.ui?.scalar !== false) {
|
|
333
|
+
const scalarRoute = options.openAPI?.ui?.scalar?.route || "/_scalar";
|
|
334
|
+
prerenderRoutes.push(scalarRoute);
|
|
335
|
+
options.handlers.push({
|
|
336
|
+
route: options.openAPI?.ui?.scalar?.route || "/_scalar",
|
|
337
|
+
env: handlersEnv,
|
|
338
|
+
handler: join(runtimeDir, "internal/routes/scalar")
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
if (options.openAPI?.ui?.swagger !== false) {
|
|
342
|
+
const swaggerRoute = options.openAPI?.ui?.swagger?.route || "/_swagger";
|
|
343
|
+
prerenderRoutes.push(swaggerRoute);
|
|
344
|
+
options.handlers.push({
|
|
345
|
+
route: swaggerRoute,
|
|
346
|
+
env: handlersEnv,
|
|
347
|
+
handler: join(runtimeDir, "internal/routes/swagger")
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
if (shouldPrerender) {
|
|
351
|
+
options.prerender ??= {};
|
|
352
|
+
options.prerender.routes ??= [];
|
|
353
|
+
options.prerender.routes.push(...prerenderRoutes);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function prettyPath(p, highlight = true) {
|
|
358
|
+
p = relative(process.cwd(), p);
|
|
359
|
+
return highlight ? colors.cyan(p) : p;
|
|
360
|
+
}
|
|
361
|
+
function resolveNitroPath(path, nitroOptions, base) {
|
|
362
|
+
if (typeof path !== "string") {
|
|
363
|
+
throw new TypeError("Invalid path: " + path);
|
|
364
|
+
}
|
|
365
|
+
path = _compilePathTemplate(path)(nitroOptions);
|
|
366
|
+
for (const base2 in nitroOptions.alias) {
|
|
367
|
+
if (path.startsWith(base2)) {
|
|
368
|
+
path = nitroOptions.alias[base2] + path.slice(base2.length);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return resolve(base || nitroOptions.srcDir, path);
|
|
372
|
+
}
|
|
373
|
+
function _compilePathTemplate(contents) {
|
|
374
|
+
return (params) => contents.replace(/{{ ?([\w.]+) ?}}/g, (_, match) => {
|
|
375
|
+
const val = getProperty(params, match);
|
|
376
|
+
if (!val) {
|
|
377
|
+
consola.warn(
|
|
378
|
+
`cannot resolve template param '${match}' in ${contents.slice(0, 20)}`
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
return val || `${match}`;
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
async function writeFile(file, contents, log = false) {
|
|
385
|
+
await mkdir(dirname(file), { recursive: true });
|
|
386
|
+
await writeFile$1(
|
|
387
|
+
file,
|
|
388
|
+
contents,
|
|
389
|
+
typeof contents === "string" ? "utf8" : void 0
|
|
390
|
+
);
|
|
391
|
+
if (log) {
|
|
392
|
+
consola.info("Generated", prettyPath(file));
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
async function isDirectory(path) {
|
|
396
|
+
try {
|
|
397
|
+
return (await stat(path)).isDirectory();
|
|
398
|
+
} catch {
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const RESOLVE_EXTENSIONS = [".ts", ".js", ".mts", ".mjs", ".tsx", ".jsx"];
|
|
404
|
+
async function resolvePathOptions(options) {
|
|
405
|
+
options.rootDir = resolve(options.rootDir || ".") + "/";
|
|
406
|
+
options.workspaceDir ||= await findWorkspaceDir(options.rootDir).catch(() => options.rootDir) + "/";
|
|
407
|
+
for (const key of ["srcDir", "buildDir"]) {
|
|
408
|
+
options[key] = resolve(options.rootDir, options[key] || ".");
|
|
409
|
+
}
|
|
410
|
+
options.alias ??= {};
|
|
411
|
+
if (!options.static && !options.entry) {
|
|
412
|
+
throw new Error(
|
|
413
|
+
`Nitro entry is missing! Is "${options.preset}" preset correct?`
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
if (options.entry) {
|
|
417
|
+
options.entry = resolveNitroPath(options.entry, options);
|
|
418
|
+
}
|
|
419
|
+
options.output.dir = resolveNitroPath(
|
|
420
|
+
options.output.dir || NitroDefaults.output.dir,
|
|
421
|
+
options,
|
|
422
|
+
options.rootDir
|
|
423
|
+
) + "/";
|
|
424
|
+
options.output.publicDir = resolveNitroPath(
|
|
425
|
+
options.output.publicDir || NitroDefaults.output.publicDir,
|
|
426
|
+
options,
|
|
427
|
+
options.rootDir
|
|
428
|
+
) + "/";
|
|
429
|
+
options.output.serverDir = resolveNitroPath(
|
|
430
|
+
options.output.serverDir || NitroDefaults.output.serverDir,
|
|
431
|
+
options,
|
|
432
|
+
options.rootDir
|
|
433
|
+
) + "/";
|
|
434
|
+
options.nodeModulesDirs.push(resolve(options.rootDir, "node_modules"));
|
|
435
|
+
options.nodeModulesDirs.push(resolve(options.workspaceDir, "node_modules"));
|
|
436
|
+
options.nodeModulesDirs.push(resolve(pkgDir, "dist/node_modules"));
|
|
437
|
+
options.nodeModulesDirs.push(resolve(pkgDir, "node_modules"));
|
|
438
|
+
options.nodeModulesDirs.push(resolve(pkgDir, ".."));
|
|
439
|
+
options.nodeModulesDirs = [
|
|
440
|
+
...new Set(
|
|
441
|
+
// Adding trailing slash to optimize resolve performance (path is explicitly a dir)
|
|
442
|
+
options.nodeModulesDirs.map((dir) => resolve(options.rootDir, dir) + "/")
|
|
443
|
+
)
|
|
444
|
+
];
|
|
445
|
+
options.plugins = options.plugins.map((p) => resolveNitroPath(p, options));
|
|
446
|
+
options.scanDirs.unshift(options.srcDir);
|
|
447
|
+
options.scanDirs = options.scanDirs.map(
|
|
448
|
+
(dir) => resolve(options.srcDir, dir)
|
|
449
|
+
);
|
|
450
|
+
options.scanDirs = [...new Set(options.scanDirs.map((dir) => dir + "/"))];
|
|
451
|
+
if (options.serverEntry) {
|
|
452
|
+
options.serverEntry = resolveModulePath(
|
|
453
|
+
resolveNitroPath(options.serverEntry, options),
|
|
454
|
+
{
|
|
455
|
+
from: options.scanDirs,
|
|
456
|
+
extensions: RESOLVE_EXTENSIONS
|
|
457
|
+
}
|
|
458
|
+
);
|
|
459
|
+
} else {
|
|
460
|
+
const defaultServerEntry = resolveModulePath("./server", {
|
|
461
|
+
from: options.scanDirs,
|
|
462
|
+
extensions: RESOLVE_EXTENSIONS,
|
|
463
|
+
try: true
|
|
464
|
+
});
|
|
465
|
+
if (defaultServerEntry) {
|
|
466
|
+
options.serverEntry = defaultServerEntry;
|
|
467
|
+
consola.info(
|
|
468
|
+
`Using \`${prettyPath(defaultServerEntry)}\` as server entry.`
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (options.renderer?.entry) {
|
|
473
|
+
options.renderer.entry = resolveModulePath(
|
|
474
|
+
resolveNitroPath(options.renderer?.entry, options),
|
|
475
|
+
{
|
|
476
|
+
from: options.scanDirs,
|
|
477
|
+
extensions: RESOLVE_EXTENSIONS
|
|
478
|
+
}
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
if (options.renderer?.template) {
|
|
482
|
+
options.renderer.template = resolveModulePath(
|
|
483
|
+
resolveNitroPath(options.renderer?.template, options),
|
|
484
|
+
{
|
|
485
|
+
from: options.scanDirs,
|
|
486
|
+
extensions: [".html"]
|
|
487
|
+
}
|
|
488
|
+
);
|
|
489
|
+
} else if (!options.renderer?.entry) {
|
|
490
|
+
const defaultIndex = resolveModulePath("./index.html", {
|
|
491
|
+
from: options.scanDirs,
|
|
492
|
+
extensions: [".html"],
|
|
493
|
+
try: true
|
|
494
|
+
});
|
|
495
|
+
if (defaultIndex) {
|
|
496
|
+
options.renderer ??= {};
|
|
497
|
+
options.renderer.template = defaultIndex;
|
|
498
|
+
consola.info(
|
|
499
|
+
`Using \`${prettyPath(defaultIndex)}\` as renderer template.`
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (options.renderer?.template && !options.renderer?.entry) {
|
|
504
|
+
options.renderer ??= {};
|
|
505
|
+
options.renderer.entry = join(
|
|
506
|
+
runtimeDir,
|
|
507
|
+
"internal/routes/renderer-template" + (options.dev ? ".dev" : "")
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
async function resolveRouteRulesOptions(options) {
|
|
513
|
+
options.routeRules = defu(options.routeRules, options.routes || {});
|
|
514
|
+
options.routeRules = normalizeRouteRules(options);
|
|
515
|
+
}
|
|
516
|
+
function normalizeRouteRules(config) {
|
|
517
|
+
const normalizedRules = {};
|
|
518
|
+
for (let path in config.routeRules) {
|
|
519
|
+
const routeConfig = config.routeRules[path];
|
|
520
|
+
path = withLeadingSlash(path);
|
|
521
|
+
const routeRules = {
|
|
522
|
+
...routeConfig,
|
|
523
|
+
redirect: void 0,
|
|
524
|
+
proxy: void 0
|
|
525
|
+
};
|
|
526
|
+
if (routeConfig.redirect) {
|
|
527
|
+
routeRules.redirect = {
|
|
528
|
+
// @ts-ignore
|
|
529
|
+
to: "/",
|
|
530
|
+
status: 307,
|
|
531
|
+
...typeof routeConfig.redirect === "string" ? { to: routeConfig.redirect } : routeConfig.redirect
|
|
532
|
+
};
|
|
533
|
+
if (path.endsWith("/**")) {
|
|
534
|
+
routeRules.redirect._redirectStripBase = path.slice(0, -3);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if (routeConfig.proxy) {
|
|
538
|
+
routeRules.proxy = typeof routeConfig.proxy === "string" ? { to: routeConfig.proxy } : routeConfig.proxy;
|
|
539
|
+
if (path.endsWith("/**")) {
|
|
540
|
+
routeRules.proxy._proxyStripBase = path.slice(0, -3);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (routeConfig.cors) {
|
|
544
|
+
routeRules.headers = {
|
|
545
|
+
"access-control-allow-origin": "*",
|
|
546
|
+
"access-control-allow-methods": "*",
|
|
547
|
+
"access-control-allow-headers": "*",
|
|
548
|
+
"access-control-max-age": "0",
|
|
549
|
+
...routeRules.headers
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
if (routeConfig.swr) {
|
|
553
|
+
routeRules.cache = routeRules.cache || {};
|
|
554
|
+
routeRules.cache.swr = true;
|
|
555
|
+
if (typeof routeConfig.swr === "number") {
|
|
556
|
+
routeRules.cache.maxAge = routeConfig.swr;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (routeConfig.cache === false) {
|
|
560
|
+
routeRules.cache = false;
|
|
561
|
+
}
|
|
562
|
+
normalizedRules[path] = routeRules;
|
|
563
|
+
}
|
|
564
|
+
return normalizedRules;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
async function resolveRuntimeConfigOptions(options) {
|
|
568
|
+
options.runtimeConfig = normalizeRuntimeConfig(options);
|
|
569
|
+
}
|
|
570
|
+
function normalizeRuntimeConfig(config) {
|
|
571
|
+
provideFallbackValues(config.runtimeConfig || {});
|
|
572
|
+
const runtimeConfig = defu$1(
|
|
573
|
+
config.runtimeConfig,
|
|
574
|
+
{
|
|
575
|
+
app: {
|
|
576
|
+
baseURL: config.baseURL
|
|
577
|
+
},
|
|
578
|
+
nitro: {
|
|
579
|
+
envExpansion: config.experimental?.envExpansion,
|
|
580
|
+
openAPI: config.openAPI
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
);
|
|
584
|
+
runtimeConfig.nitro.routeRules = config.routeRules;
|
|
585
|
+
checkSerializableRuntimeConfig(runtimeConfig);
|
|
586
|
+
return runtimeConfig;
|
|
587
|
+
}
|
|
588
|
+
function provideFallbackValues(obj) {
|
|
589
|
+
for (const key in obj) {
|
|
590
|
+
if (obj[key] === void 0 || obj[key] === null) {
|
|
591
|
+
obj[key] = "";
|
|
592
|
+
} else if (typeof obj[key] === "object") {
|
|
593
|
+
provideFallbackValues(obj[key]);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
function checkSerializableRuntimeConfig(obj, path = []) {
|
|
598
|
+
if (isPrimitiveValue(obj)) {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
for (const key in obj) {
|
|
602
|
+
const value = obj[key];
|
|
603
|
+
if (value === null || value === void 0 || isPrimitiveValue(value)) {
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
606
|
+
if (Array.isArray(value)) {
|
|
607
|
+
for (const [index, item] of value.entries())
|
|
608
|
+
checkSerializableRuntimeConfig(item, [...path, `${key}[${index}]`]);
|
|
609
|
+
} else if (typeof value === "object" && value.constructor === Object && (!value.constructor?.name || value.constructor.name === "Object")) {
|
|
610
|
+
checkSerializableRuntimeConfig(value, [...path, key]);
|
|
611
|
+
} else {
|
|
612
|
+
console.warn(
|
|
613
|
+
`Runtime config option \`${[...path, key].join(".")}\` may not be able to be serialized.`
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function isPrimitiveValue(value) {
|
|
619
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
async function resolveStorageOptions(options) {
|
|
623
|
+
const fsMounts = {
|
|
624
|
+
root: resolve(options.rootDir),
|
|
625
|
+
src: resolve(options.srcDir),
|
|
626
|
+
build: resolve(options.buildDir),
|
|
627
|
+
cache: resolve(options.buildDir, "cache")
|
|
628
|
+
};
|
|
629
|
+
for (const p in fsMounts) {
|
|
630
|
+
options.devStorage[p] = options.devStorage[p] || {
|
|
631
|
+
driver: "fs",
|
|
632
|
+
readOnly: p === "root" || p === "src",
|
|
633
|
+
base: fsMounts[p]
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
if (options.dev && options.storage.data === void 0 && options.devStorage.data === void 0) {
|
|
637
|
+
options.devStorage.data = {
|
|
638
|
+
driver: "fs",
|
|
639
|
+
base: resolve(options.rootDir, ".data/kv")
|
|
640
|
+
};
|
|
641
|
+
} else if (options.node && options.storage.data === void 0) {
|
|
642
|
+
options.storage.data = {
|
|
643
|
+
driver: "fsLite",
|
|
644
|
+
base: "./.data/kv"
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
async function resolveURLOptions(options) {
|
|
650
|
+
options.baseURL = withLeadingSlash(withTrailingSlash(options.baseURL));
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
async function resolveErrorOptions(options) {
|
|
654
|
+
if (!options.errorHandler) {
|
|
655
|
+
options.errorHandler = [];
|
|
656
|
+
} else if (!Array.isArray(options.errorHandler)) {
|
|
657
|
+
options.errorHandler = [options.errorHandler];
|
|
658
|
+
}
|
|
659
|
+
options.errorHandler.push(
|
|
660
|
+
join(runtimeDir, `internal/error/${options.dev ? "dev" : "prod"}`)
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const common = {
|
|
665
|
+
meta: {
|
|
666
|
+
name: "nitro-common",
|
|
667
|
+
url: import.meta.url
|
|
668
|
+
},
|
|
669
|
+
alias: {
|
|
670
|
+
"buffer/": "node:buffer",
|
|
671
|
+
"buffer/index": "node:buffer",
|
|
672
|
+
"buffer/index.js": "node:buffer",
|
|
673
|
+
"string_decoder/": "node:string_decoder",
|
|
674
|
+
"process/": "node:process"
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
const nodeless = {
|
|
678
|
+
meta: {
|
|
679
|
+
name: "nitro-nodeless",
|
|
680
|
+
url: import.meta.url
|
|
681
|
+
},
|
|
682
|
+
inject: {
|
|
683
|
+
global: "unenv/polyfill/globalthis",
|
|
684
|
+
process: "node:process",
|
|
685
|
+
Buffer: ["node:buffer", "Buffer"],
|
|
686
|
+
clearImmediate: ["node:timers", "clearImmediate"],
|
|
687
|
+
setImmediate: ["node:timers", "setImmediate"],
|
|
688
|
+
performance: "unenv/polyfill/performance",
|
|
689
|
+
PerformanceObserver: ["node:perf_hooks", "PerformanceObserver"],
|
|
690
|
+
BroadcastChannel: ["node:worker_threads", "BroadcastChannel"]
|
|
691
|
+
},
|
|
692
|
+
polyfill: [
|
|
693
|
+
"unenv/polyfill/globalthis-global",
|
|
694
|
+
"unenv/polyfill/process",
|
|
695
|
+
"unenv/polyfill/buffer",
|
|
696
|
+
"unenv/polyfill/timers"
|
|
697
|
+
]
|
|
698
|
+
};
|
|
699
|
+
async function resolveUnenv(options) {
|
|
700
|
+
options.unenv ??= [];
|
|
701
|
+
if (!Array.isArray(options.unenv)) {
|
|
702
|
+
options.unenv = [options.unenv];
|
|
703
|
+
}
|
|
704
|
+
options.unenv = options.unenv.filter(Boolean);
|
|
705
|
+
if (!options.node) {
|
|
706
|
+
options.unenv.unshift(nodeless);
|
|
707
|
+
}
|
|
708
|
+
options.unenv.unshift(common);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
async function resolveBuilder(options) {
|
|
712
|
+
if (!options.builder) {
|
|
713
|
+
options.builder = process.env.NITRO_BUILDER || "rollup";
|
|
714
|
+
}
|
|
715
|
+
if (options.builder === "rolldown") {
|
|
716
|
+
try {
|
|
717
|
+
await import('rolldown');
|
|
718
|
+
} catch {
|
|
719
|
+
throw new Error(
|
|
720
|
+
`Builder "rolldown" is not available. Make sure to install "rolldown" package.`
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
} else if (options.builder === "vite") {
|
|
724
|
+
try {
|
|
725
|
+
await import('vite');
|
|
726
|
+
} catch {
|
|
727
|
+
throw new Error(
|
|
728
|
+
`Builder "vite" is not available. Make sure to install "vite" package.`
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (!["rollup", "rolldown", "vite"].includes(options.builder)) {
|
|
733
|
+
throw new Error(`Builder "${options.builder}" is not supported.`);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
const configResolvers = [
|
|
738
|
+
resolveCompatibilityOptions,
|
|
739
|
+
resolvePathOptions,
|
|
740
|
+
resolveImportsOptions,
|
|
741
|
+
resolveRouteRulesOptions,
|
|
742
|
+
resolveDatabaseOptions,
|
|
743
|
+
resolveExportConditionsOptions,
|
|
744
|
+
resolveRuntimeConfigOptions,
|
|
745
|
+
resolveOpenAPIOptions,
|
|
746
|
+
resolveURLOptions,
|
|
747
|
+
resolveAssetsOptions,
|
|
748
|
+
resolveStorageOptions,
|
|
749
|
+
resolveErrorOptions,
|
|
750
|
+
resolveUnenv,
|
|
751
|
+
resolveBuilder
|
|
752
|
+
];
|
|
753
|
+
async function loadOptions(configOverrides = {}, opts = {}) {
|
|
754
|
+
const options = await _loadUserConfig(configOverrides, opts);
|
|
755
|
+
for (const resolver of configResolvers) {
|
|
756
|
+
await resolver(options);
|
|
757
|
+
}
|
|
758
|
+
return options;
|
|
759
|
+
}
|
|
760
|
+
async function _loadUserConfig(configOverrides = {}, opts = {}) {
|
|
761
|
+
configOverrides = klona(configOverrides);
|
|
762
|
+
globalThis.defineNitroConfig = globalThis.defineNitroConfig || ((c) => c);
|
|
763
|
+
let compatibilityDate = configOverrides.compatibilityDate || opts.compatibilityDate || (process.env.NITRO_COMPATIBILITY_DATE || process.env.SERVER_COMPATIBILITY_DATE || process.env.COMPATIBILITY_DATE);
|
|
764
|
+
const { resolvePreset } = await import('../_chunks/_presets/index.mjs');
|
|
765
|
+
let preset = configOverrides.preset || process.env.NITRO_PRESET || process.env.SERVER_PRESET;
|
|
766
|
+
const _dotenv = opts.dotenv ?? (configOverrides.dev && { fileName: [".env", ".env.local"] });
|
|
767
|
+
const loadedConfig = await (opts.watch ? watchConfig : loadConfig)({
|
|
768
|
+
name: "nitro",
|
|
769
|
+
cwd: configOverrides.rootDir,
|
|
770
|
+
dotenv: _dotenv,
|
|
771
|
+
extend: { extendKey: ["extends", "preset"] },
|
|
772
|
+
defaults: NitroDefaults,
|
|
773
|
+
jitiOptions: {
|
|
774
|
+
alias: {
|
|
775
|
+
nitropack: "nitro/config",
|
|
776
|
+
"nitro/config": "nitro/config"
|
|
777
|
+
}
|
|
778
|
+
},
|
|
779
|
+
async overrides({ rawConfigs }) {
|
|
780
|
+
const getConf = (key) => configOverrides[key] ?? rawConfigs.main?.[key] ?? rawConfigs.rc?.[key] ?? rawConfigs.packageJson?.[key];
|
|
781
|
+
if (!compatibilityDate) {
|
|
782
|
+
compatibilityDate = getConf("compatibilityDate");
|
|
783
|
+
}
|
|
784
|
+
const framework = getConf("framework");
|
|
785
|
+
const isCustomFramework = framework?.name && framework.name !== "nitro";
|
|
786
|
+
if (!preset) {
|
|
787
|
+
preset = getConf("preset");
|
|
788
|
+
}
|
|
789
|
+
if (configOverrides.dev) {
|
|
790
|
+
preset = preset && preset !== "nitro-dev" ? await resolvePreset(preset, {
|
|
791
|
+
static: getConf("static"),
|
|
792
|
+
dev: true,
|
|
793
|
+
compatibilityDate: compatibilityDate || "latest"
|
|
794
|
+
}).then((p) => p?._meta?.name || "nitro-dev").catch(() => "nitro-dev") : "nitro-dev";
|
|
795
|
+
} else if (!preset) {
|
|
796
|
+
preset = await resolvePreset("", {
|
|
797
|
+
static: getConf("static"),
|
|
798
|
+
dev: false,
|
|
799
|
+
compatibilityDate: compatibilityDate || "latest"
|
|
800
|
+
}).then((p) => p?._meta?.name);
|
|
801
|
+
}
|
|
802
|
+
return {
|
|
803
|
+
...configOverrides,
|
|
804
|
+
preset,
|
|
805
|
+
typescript: {
|
|
806
|
+
generateRuntimeConfigTypes: !isCustomFramework,
|
|
807
|
+
...getConf("typescript"),
|
|
808
|
+
...configOverrides.typescript
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
},
|
|
812
|
+
async resolve(id) {
|
|
813
|
+
const preset2 = await resolvePreset(id, {
|
|
814
|
+
static: configOverrides.static,
|
|
815
|
+
compatibilityDate: compatibilityDate || "latest",
|
|
816
|
+
dev: configOverrides.dev
|
|
817
|
+
});
|
|
818
|
+
if (preset2) {
|
|
819
|
+
return {
|
|
820
|
+
config: klona(preset2)
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
},
|
|
824
|
+
...opts.c12
|
|
825
|
+
});
|
|
826
|
+
const options = klona(loadedConfig.config);
|
|
827
|
+
options._config = configOverrides;
|
|
828
|
+
options._c12 = loadedConfig;
|
|
829
|
+
const _presetName = (loadedConfig.layers || []).find((l) => l.config?._meta?.name)?.config?._meta?.name || preset;
|
|
830
|
+
options.preset = _presetName;
|
|
831
|
+
options.compatibilityDate = resolveCompatibilityDates(
|
|
832
|
+
compatibilityDate,
|
|
833
|
+
options.compatibilityDate
|
|
834
|
+
);
|
|
835
|
+
if (options.dev && options.preset !== "nitro-dev") {
|
|
836
|
+
consola.info(`Using \`${options.preset}\` emulation in development mode.`);
|
|
837
|
+
}
|
|
838
|
+
return options;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
async function updateNitroConfig(nitro, config) {
|
|
842
|
+
nitro.options.routeRules = normalizeRouteRules(
|
|
843
|
+
config.routeRules ? config : nitro.options
|
|
844
|
+
);
|
|
845
|
+
nitro.options.runtimeConfig = normalizeRuntimeConfig(
|
|
846
|
+
config.runtimeConfig ? config : nitro.options
|
|
847
|
+
);
|
|
848
|
+
await nitro.hooks.callHook("rollup:reload");
|
|
849
|
+
consola.success("Nitro config hot reloaded!");
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
async function installModules(nitro) {
|
|
853
|
+
const _modules = [...nitro.options.modules || []];
|
|
854
|
+
const modules = await Promise.all(
|
|
855
|
+
_modules.map((mod) => _resolveNitroModule(mod, nitro.options))
|
|
856
|
+
);
|
|
857
|
+
const _installedURLs = /* @__PURE__ */ new Set();
|
|
858
|
+
for (const mod of modules) {
|
|
859
|
+
if (mod._url) {
|
|
860
|
+
if (_installedURLs.has(mod._url)) {
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
_installedURLs.add(mod._url);
|
|
864
|
+
}
|
|
865
|
+
await mod.setup(nitro);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
async function _resolveNitroModule(mod, nitroOptions) {
|
|
869
|
+
let _url;
|
|
870
|
+
if (typeof mod === "string") {
|
|
871
|
+
globalThis.defineNitroModule = // @ts-ignore
|
|
872
|
+
globalThis.defineNitroModule || ((mod2) => mod2);
|
|
873
|
+
const jiti = createJiti(nitroOptions.rootDir, {
|
|
874
|
+
alias: nitroOptions.alias
|
|
875
|
+
});
|
|
876
|
+
const _modPath = jiti.esmResolve(mod);
|
|
877
|
+
_url = _modPath;
|
|
878
|
+
mod = await jiti.import(_modPath, { default: true });
|
|
879
|
+
}
|
|
880
|
+
if (typeof mod === "function") {
|
|
881
|
+
mod = { setup: mod };
|
|
882
|
+
}
|
|
883
|
+
if (!mod.setup) {
|
|
884
|
+
mod.setup = () => {
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
return {
|
|
888
|
+
_url,
|
|
889
|
+
...mod
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}";
|
|
894
|
+
const suffixRegex = /(\.(?<method>connect|delete|get|head|options|patch|post|put|trace))?(\.(?<env>dev|prod|prerender))?$/;
|
|
895
|
+
async function scanAndSyncOptions(nitro) {
|
|
896
|
+
const scannedPlugins = await scanPlugins(nitro);
|
|
897
|
+
for (const plugin of scannedPlugins) {
|
|
898
|
+
if (!nitro.options.plugins.includes(plugin)) {
|
|
899
|
+
nitro.options.plugins.push(plugin);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
if (nitro.options.experimental.tasks) {
|
|
903
|
+
const scannedTasks = await scanTasks(nitro);
|
|
904
|
+
for (const scannedTask of scannedTasks) {
|
|
905
|
+
if (scannedTask.name in nitro.options.tasks) {
|
|
906
|
+
if (!nitro.options.tasks[scannedTask.name].handler) {
|
|
907
|
+
nitro.options.tasks[scannedTask.name].handler = scannedTask.handler;
|
|
908
|
+
}
|
|
909
|
+
} else {
|
|
910
|
+
nitro.options.tasks[scannedTask.name] = {
|
|
911
|
+
handler: scannedTask.handler,
|
|
912
|
+
description: ""
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
const scannedModules = await scanModules(nitro);
|
|
918
|
+
nitro.options.modules = nitro.options.modules || [];
|
|
919
|
+
for (const modPath of scannedModules) {
|
|
920
|
+
if (!nitro.options.modules.includes(modPath)) {
|
|
921
|
+
nitro.options.modules.push(modPath);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
async function scanHandlers(nitro) {
|
|
926
|
+
const middleware = await scanMiddleware(nitro);
|
|
927
|
+
const handlers = await Promise.all([
|
|
928
|
+
scanServerRoutes(
|
|
929
|
+
nitro,
|
|
930
|
+
nitro.options.apiDir || "api",
|
|
931
|
+
nitro.options.apiBaseURL || "/api"
|
|
932
|
+
),
|
|
933
|
+
scanServerRoutes(nitro, nitro.options.routesDir || "routes")
|
|
934
|
+
]).then((r) => r.flat());
|
|
935
|
+
nitro.scannedHandlers = [
|
|
936
|
+
...middleware,
|
|
937
|
+
...handlers.filter((h, index, array) => {
|
|
938
|
+
return array.findIndex(
|
|
939
|
+
(h2) => h.route === h2.route && h.method === h2.method && h.env === h2.env
|
|
940
|
+
) === index;
|
|
941
|
+
})
|
|
942
|
+
];
|
|
943
|
+
return handlers;
|
|
944
|
+
}
|
|
945
|
+
async function scanMiddleware(nitro) {
|
|
946
|
+
const files = await scanFiles(nitro, "middleware");
|
|
947
|
+
return files.map((file) => {
|
|
948
|
+
return {
|
|
949
|
+
route: "/**",
|
|
950
|
+
middleware: true,
|
|
951
|
+
handler: file.fullPath
|
|
952
|
+
};
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
async function scanServerRoutes(nitro, dir, prefix = "/") {
|
|
956
|
+
const files = await scanFiles(nitro, dir);
|
|
957
|
+
return files.map((file) => {
|
|
958
|
+
let route = file.path.replace(/\.[A-Za-z]+$/, "").replace(/\(([^(/\\]+)\)[/\\]/g, "").replace(/\[\.{3}]/g, "**").replace(/\[\.{3}(\w+)]/g, "**:$1").replace(/\[([^/\]]+)]/g, ":$1");
|
|
959
|
+
route = withLeadingSlash(withoutTrailingSlash(withBase(route, prefix)));
|
|
960
|
+
const suffixMatch = route.match(suffixRegex);
|
|
961
|
+
let method;
|
|
962
|
+
let env;
|
|
963
|
+
if (suffixMatch?.index && suffixMatch?.index >= 0) {
|
|
964
|
+
route = route.slice(0, suffixMatch.index);
|
|
965
|
+
method = suffixMatch.groups?.method;
|
|
966
|
+
env = suffixMatch.groups?.env;
|
|
967
|
+
}
|
|
968
|
+
route = route.replace(/\/index$/, "") || "/";
|
|
969
|
+
return {
|
|
970
|
+
handler: file.fullPath,
|
|
971
|
+
lazy: true,
|
|
972
|
+
middleware: false,
|
|
973
|
+
route,
|
|
974
|
+
method,
|
|
975
|
+
env
|
|
976
|
+
};
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
async function scanPlugins(nitro) {
|
|
980
|
+
const files = await scanFiles(nitro, "plugins");
|
|
981
|
+
return files.map((f) => f.fullPath);
|
|
982
|
+
}
|
|
983
|
+
async function scanTasks(nitro) {
|
|
984
|
+
const files = await scanFiles(nitro, "tasks");
|
|
985
|
+
return files.map((f) => {
|
|
986
|
+
const name = f.path.replace(/\/index$/, "").replace(/\.[A-Za-z]+$/, "").replace(/\//g, ":");
|
|
987
|
+
return { name, handler: f.fullPath };
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
async function scanModules(nitro) {
|
|
991
|
+
const files = await scanFiles(nitro, "modules");
|
|
992
|
+
return files.map((f) => f.fullPath);
|
|
993
|
+
}
|
|
994
|
+
async function scanFiles(nitro, name) {
|
|
995
|
+
const files = await Promise.all(
|
|
996
|
+
nitro.options.scanDirs.map((dir) => scanDir(nitro, dir, name))
|
|
997
|
+
).then((r) => r.flat());
|
|
998
|
+
return files;
|
|
999
|
+
}
|
|
1000
|
+
async function scanDir(nitro, dir, name) {
|
|
1001
|
+
const fileNames = await glob(join(name, GLOB_SCAN_PATTERN), {
|
|
1002
|
+
cwd: dir,
|
|
1003
|
+
dot: true,
|
|
1004
|
+
ignore: nitro.options.ignore,
|
|
1005
|
+
absolute: true
|
|
1006
|
+
}).catch((error) => {
|
|
1007
|
+
if (error?.code === "ENOTDIR") {
|
|
1008
|
+
nitro.logger.warn(
|
|
1009
|
+
`Ignoring \`${join(dir, name)}\`. It must be a directory.`
|
|
1010
|
+
);
|
|
1011
|
+
return [];
|
|
1012
|
+
}
|
|
1013
|
+
throw error;
|
|
1014
|
+
});
|
|
1015
|
+
return fileNames.map((fullPath) => {
|
|
1016
|
+
return {
|
|
1017
|
+
fullPath,
|
|
1018
|
+
path: relative(join(dir, name), fullPath)
|
|
1019
|
+
};
|
|
1020
|
+
}).sort((a, b) => a.path.localeCompare(b.path));
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
async function runTask(taskEvent, opts) {
|
|
1024
|
+
const ctx = await _getTasksContext(opts);
|
|
1025
|
+
const result = await ctx.devFetch(`/_nitro/tasks/${taskEvent.name}`, {
|
|
1026
|
+
method: "POST",
|
|
1027
|
+
body: taskEvent
|
|
1028
|
+
});
|
|
1029
|
+
return result;
|
|
1030
|
+
}
|
|
1031
|
+
async function listTasks(opts) {
|
|
1032
|
+
const ctx = await _getTasksContext(opts);
|
|
1033
|
+
const res = await ctx.devFetch("/_nitro/tasks");
|
|
1034
|
+
return res.tasks;
|
|
1035
|
+
}
|
|
1036
|
+
function addNitroTasksVirtualFile(nitro) {
|
|
1037
|
+
nitro.options.virtual["#nitro-internal-virtual/tasks"] = () => {
|
|
1038
|
+
const _scheduledTasks = Object.entries(nitro.options.scheduledTasks || {}).map(([cron, _tasks]) => {
|
|
1039
|
+
const tasks = (Array.isArray(_tasks) ? _tasks : [_tasks]).filter(
|
|
1040
|
+
(name) => {
|
|
1041
|
+
if (!nitro.options.tasks[name]) {
|
|
1042
|
+
nitro.logger.warn(`Scheduled task \`${name}\` is not defined!`);
|
|
1043
|
+
return false;
|
|
1044
|
+
}
|
|
1045
|
+
return true;
|
|
1046
|
+
}
|
|
1047
|
+
);
|
|
1048
|
+
return { cron, tasks };
|
|
1049
|
+
}).filter((e) => e.tasks.length > 0);
|
|
1050
|
+
const scheduledTasks = _scheduledTasks.length > 0 ? _scheduledTasks : false;
|
|
1051
|
+
return (
|
|
1052
|
+
/* js */
|
|
1053
|
+
`
|
|
1054
|
+
export const scheduledTasks = ${JSON.stringify(scheduledTasks)};
|
|
1055
|
+
|
|
1056
|
+
export const tasks = {
|
|
1057
|
+
${Object.entries(nitro.options.tasks).map(
|
|
1058
|
+
([name, task]) => `"${name}": {
|
|
1059
|
+
meta: {
|
|
1060
|
+
description: ${JSON.stringify(task.description)},
|
|
1061
|
+
},
|
|
1062
|
+
resolve: ${task.handler ? `() => import("${normalize(
|
|
1063
|
+
task.handler
|
|
1064
|
+
)}").then(r => r.default || r)` : "undefined"},
|
|
1065
|
+
}`
|
|
1066
|
+
).join(",\n")}
|
|
1067
|
+
};`
|
|
1068
|
+
);
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
const _devHint = `(is dev server running?)`;
|
|
1072
|
+
async function _getTasksContext(opts) {
|
|
1073
|
+
const cwd = resolve(process.cwd(), opts?.cwd || ".");
|
|
1074
|
+
const outDir = resolve(cwd, opts?.buildDir || ".nitro");
|
|
1075
|
+
const buildInfoPath = resolve(outDir, "nitro.json");
|
|
1076
|
+
if (!existsSync(buildInfoPath)) {
|
|
1077
|
+
throw new Error(`Missing info file: \`${buildInfoPath}\` ${_devHint}`);
|
|
1078
|
+
}
|
|
1079
|
+
const buildInfo = JSON.parse(
|
|
1080
|
+
await readFile(buildInfoPath, "utf8")
|
|
1081
|
+
);
|
|
1082
|
+
if (!buildInfo.dev?.pid || !buildInfo.dev?.workerAddress) {
|
|
1083
|
+
throw new Error(
|
|
1084
|
+
`Missing dev server info in: \`${buildInfoPath}\` ${_devHint}`
|
|
1085
|
+
);
|
|
1086
|
+
}
|
|
1087
|
+
if (!_pidIsRunning(buildInfo.dev.pid)) {
|
|
1088
|
+
throw new Error(`Dev server is not running (pid: ${buildInfo.dev.pid})`);
|
|
1089
|
+
}
|
|
1090
|
+
const devFetch = ofetch.create({
|
|
1091
|
+
baseURL: `http://${buildInfo.dev.workerAddress.host || "localhost"}:${buildInfo.dev.workerAddress.port || "3000"}`,
|
|
1092
|
+
// @ts-expect-error
|
|
1093
|
+
socketPath: buildInfo.dev.workerAddress.socketPath
|
|
1094
|
+
});
|
|
1095
|
+
return {
|
|
1096
|
+
buildInfo,
|
|
1097
|
+
devFetch
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
function _pidIsRunning(pid) {
|
|
1101
|
+
try {
|
|
1102
|
+
process.kill(pid, 0);
|
|
1103
|
+
return true;
|
|
1104
|
+
} catch {
|
|
1105
|
+
return false;
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
async function createStorage(nitro) {
|
|
1110
|
+
const storage = createStorage$1();
|
|
1111
|
+
const mounts = klona$1({
|
|
1112
|
+
...nitro.options.storage,
|
|
1113
|
+
...nitro.options.devStorage
|
|
1114
|
+
});
|
|
1115
|
+
for (const [path, opts] of Object.entries(mounts)) {
|
|
1116
|
+
if (opts.driver) {
|
|
1117
|
+
const driver = await import(builtinDrivers[opts.driver] || opts.driver).then((r) => r.default || r);
|
|
1118
|
+
storage.mount(path, driver(opts));
|
|
1119
|
+
} else {
|
|
1120
|
+
nitro.logger.warn(`No \`driver\` set for storage mount point "${path}".`);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
return storage;
|
|
1124
|
+
}
|
|
1125
|
+
async function snapshotStorage(nitro) {
|
|
1126
|
+
const data = {};
|
|
1127
|
+
const allKeys = [
|
|
1128
|
+
...new Set(
|
|
1129
|
+
await Promise.all(
|
|
1130
|
+
nitro.options.bundledStorage.map((base) => nitro.storage.getKeys(base))
|
|
1131
|
+
).then((r) => r.flat())
|
|
1132
|
+
)
|
|
1133
|
+
];
|
|
1134
|
+
await Promise.all(
|
|
1135
|
+
allKeys.map(async (key) => {
|
|
1136
|
+
data[key] = await nitro.storage.getItem(key);
|
|
1137
|
+
})
|
|
1138
|
+
);
|
|
1139
|
+
return data;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
const isGlobalMiddleware = (h) => !h.method && (!h.route || h.route === "/**");
|
|
1143
|
+
function initNitroRouting(nitro) {
|
|
1144
|
+
const envConditions = new Set(
|
|
1145
|
+
[
|
|
1146
|
+
nitro.options.dev ? "dev" : "prod",
|
|
1147
|
+
nitro.options.preset,
|
|
1148
|
+
nitro.options.preset === "nitro-prerender" ? "prerender" : void 0
|
|
1149
|
+
].filter(Boolean)
|
|
1150
|
+
);
|
|
1151
|
+
const matchesEnv = (h) => {
|
|
1152
|
+
const hEnv = Array.isArray(h.env) ? h.env : [h.env];
|
|
1153
|
+
const envs = hEnv.filter(Boolean);
|
|
1154
|
+
return envs.length === 0 || envs.some((env) => envConditions.has(env));
|
|
1155
|
+
};
|
|
1156
|
+
const routes = new Router();
|
|
1157
|
+
const routeRules = new Router(
|
|
1158
|
+
true
|
|
1159
|
+
/* matchAll */
|
|
1160
|
+
);
|
|
1161
|
+
const globalMiddleware = [];
|
|
1162
|
+
const routedMiddleware = new Router(
|
|
1163
|
+
true
|
|
1164
|
+
/* matchAll */
|
|
1165
|
+
);
|
|
1166
|
+
const warns = /* @__PURE__ */ new Set();
|
|
1167
|
+
const sync = () => {
|
|
1168
|
+
routeRules._update(
|
|
1169
|
+
Object.entries(nitro.options.routeRules).map(([route, data]) => ({
|
|
1170
|
+
route,
|
|
1171
|
+
method: "",
|
|
1172
|
+
data: {
|
|
1173
|
+
...data,
|
|
1174
|
+
_route: route
|
|
1175
|
+
}
|
|
1176
|
+
}))
|
|
1177
|
+
);
|
|
1178
|
+
const _routes = [
|
|
1179
|
+
...nitro.scannedHandlers,
|
|
1180
|
+
...nitro.options.handlers
|
|
1181
|
+
].filter((h) => h && !h.middleware && matchesEnv(h));
|
|
1182
|
+
if (nitro.options.renderer?.entry) {
|
|
1183
|
+
const existingWildcard = _routes.findIndex(
|
|
1184
|
+
(h) => /^\/\*\*(:.+)?$/.test(h.route) && (!h.method || h.method === "GET")
|
|
1185
|
+
);
|
|
1186
|
+
if (existingWildcard !== -1) {
|
|
1187
|
+
const h = _routes[existingWildcard];
|
|
1188
|
+
const warn = `The renderer will override \`${relative(".", h.handler)}\` (route: \`${h.route}\`). Use a more specific route or different HTTP method.`;
|
|
1189
|
+
if (!warns.has(warn)) {
|
|
1190
|
+
warns.add(warn);
|
|
1191
|
+
nitro.logger.warn(warn);
|
|
1192
|
+
}
|
|
1193
|
+
_routes.splice(existingWildcard, 1);
|
|
1194
|
+
}
|
|
1195
|
+
_routes.push({
|
|
1196
|
+
route: "/**",
|
|
1197
|
+
lazy: true,
|
|
1198
|
+
handler: nitro.options.renderer?.entry
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
routes._update(
|
|
1202
|
+
_routes.map((h) => ({
|
|
1203
|
+
...h,
|
|
1204
|
+
method: h.method || "",
|
|
1205
|
+
data: handlerWithImportHash(h)
|
|
1206
|
+
}))
|
|
1207
|
+
);
|
|
1208
|
+
const _middleware = [
|
|
1209
|
+
...nitro.scannedHandlers,
|
|
1210
|
+
...nitro.options.handlers
|
|
1211
|
+
].filter((h) => h && h.middleware && matchesEnv(h));
|
|
1212
|
+
if (nitro.options.serveStatic) {
|
|
1213
|
+
_middleware.unshift({
|
|
1214
|
+
route: "/**",
|
|
1215
|
+
middleware: true,
|
|
1216
|
+
handler: join(runtimeDir, "internal/static")
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
globalMiddleware.splice(
|
|
1220
|
+
0,
|
|
1221
|
+
globalMiddleware.length,
|
|
1222
|
+
..._middleware.filter((h) => isGlobalMiddleware(h)).map((m) => handlerWithImportHash(m))
|
|
1223
|
+
);
|
|
1224
|
+
routedMiddleware._update(
|
|
1225
|
+
_middleware.filter((h) => !isGlobalMiddleware(h)).map((h) => ({
|
|
1226
|
+
...h,
|
|
1227
|
+
method: h.method || "",
|
|
1228
|
+
data: handlerWithImportHash(h)
|
|
1229
|
+
}))
|
|
1230
|
+
);
|
|
1231
|
+
};
|
|
1232
|
+
nitro.routing = Object.freeze({
|
|
1233
|
+
sync,
|
|
1234
|
+
routes,
|
|
1235
|
+
routeRules,
|
|
1236
|
+
globalMiddleware,
|
|
1237
|
+
routedMiddleware
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
function handlerWithImportHash(h) {
|
|
1241
|
+
const id = (h.lazy ? "_lazy_" : "_") + hash(h.handler).replace(/-/g, "").slice(0, 6);
|
|
1242
|
+
return { ...h, _importHash: id };
|
|
1243
|
+
}
|
|
1244
|
+
class Router {
|
|
1245
|
+
#routes;
|
|
1246
|
+
#router;
|
|
1247
|
+
#compiled;
|
|
1248
|
+
constructor(matchAll) {
|
|
1249
|
+
this._update([]);
|
|
1250
|
+
}
|
|
1251
|
+
get routes() {
|
|
1252
|
+
return this.#routes;
|
|
1253
|
+
}
|
|
1254
|
+
_update(routes) {
|
|
1255
|
+
this.#routes = routes;
|
|
1256
|
+
this.#router = createRouter();
|
|
1257
|
+
this.#compiled = void 0;
|
|
1258
|
+
for (const route of routes) {
|
|
1259
|
+
addRoute(this.#router, route.method, route.route, route.data);
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
hasRoutes() {
|
|
1263
|
+
return this.#routes.length > 0;
|
|
1264
|
+
}
|
|
1265
|
+
compileToString(opts) {
|
|
1266
|
+
return this.#compiled || (this.#compiled = compileRouterToString(this.#router, void 0, opts));
|
|
1267
|
+
}
|
|
1268
|
+
match(method, path) {
|
|
1269
|
+
return findRoute(this.#router, method, path)?.data;
|
|
1270
|
+
}
|
|
1271
|
+
matchAll(method, path) {
|
|
1272
|
+
return findAllRoutes(this.#router, method, path).map(
|
|
1273
|
+
(route) => route.data
|
|
1274
|
+
);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
async function createNitro(config = {}, opts = {}) {
|
|
1279
|
+
const options = await loadOptions(config, opts);
|
|
1280
|
+
const nitro = {
|
|
1281
|
+
options,
|
|
1282
|
+
hooks: new Hookable(),
|
|
1283
|
+
vfs: {},
|
|
1284
|
+
routing: {},
|
|
1285
|
+
logger: consola$1.withTag("nitro"),
|
|
1286
|
+
scannedHandlers: [],
|
|
1287
|
+
close: () => Promise.resolve(nitro.hooks.callHook("close")),
|
|
1288
|
+
storage: void 0,
|
|
1289
|
+
async updateConfig(config2) {
|
|
1290
|
+
updateNitroConfig(nitro, config2);
|
|
1291
|
+
}
|
|
1292
|
+
};
|
|
1293
|
+
initNitroRouting(nitro);
|
|
1294
|
+
await scanAndSyncOptions(nitro);
|
|
1295
|
+
nitro.storage = await createStorage(nitro);
|
|
1296
|
+
nitro.hooks.hook("close", async () => {
|
|
1297
|
+
await nitro.storage.dispose();
|
|
1298
|
+
});
|
|
1299
|
+
if (nitro.options.debug) {
|
|
1300
|
+
createDebugger(nitro.hooks, { tag: "nitro" });
|
|
1301
|
+
}
|
|
1302
|
+
if (nitro.options.logLevel !== void 0) {
|
|
1303
|
+
nitro.logger.level = nitro.options.logLevel;
|
|
1304
|
+
}
|
|
1305
|
+
nitro.hooks.addHooks(nitro.options.hooks);
|
|
1306
|
+
addNitroTasksVirtualFile(nitro);
|
|
1307
|
+
await installModules(nitro);
|
|
1308
|
+
if (nitro.options.imports) {
|
|
1309
|
+
nitro.unimport = createUnimport(nitro.options.imports);
|
|
1310
|
+
await nitro.unimport.init();
|
|
1311
|
+
nitro.options.virtual["#imports"] = () => nitro.unimport?.toExports() || "";
|
|
1312
|
+
nitro.options.virtual["#nitro"] = 'export * from "#imports"';
|
|
1313
|
+
}
|
|
1314
|
+
await scanHandlers(nitro);
|
|
1315
|
+
nitro.routing.sync();
|
|
1316
|
+
return nitro;
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
async function compressPublicAssets(nitro) {
|
|
1320
|
+
const publicFiles = await glob("**", {
|
|
1321
|
+
cwd: nitro.options.output.publicDir,
|
|
1322
|
+
absolute: false,
|
|
1323
|
+
dot: true,
|
|
1324
|
+
ignore: ["**/*.gz", "**/*.br"]
|
|
1325
|
+
});
|
|
1326
|
+
await Promise.all(
|
|
1327
|
+
publicFiles.map(async (fileName) => {
|
|
1328
|
+
const filePath = resolve(nitro.options.output.publicDir, fileName);
|
|
1329
|
+
if (existsSync(filePath + ".gz") || existsSync(filePath + ".br")) {
|
|
1330
|
+
return;
|
|
1331
|
+
}
|
|
1332
|
+
const mimeType = mime.getType(fileName) || "text/plain";
|
|
1333
|
+
const fileContents = await fsp.readFile(filePath);
|
|
1334
|
+
if (fileContents.length < 1024 || fileName.endsWith(".map") || !isCompressibleMime(mimeType)) {
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
const { gzip, brotli } = nitro.options.compressPublicAssets || {};
|
|
1338
|
+
const encodings = [
|
|
1339
|
+
gzip !== false && "gzip",
|
|
1340
|
+
brotli !== false && "br"
|
|
1341
|
+
].filter(Boolean);
|
|
1342
|
+
await Promise.all(
|
|
1343
|
+
encodings.map(async (encoding) => {
|
|
1344
|
+
const suffix = "." + (encoding === "gzip" ? "gz" : "br");
|
|
1345
|
+
const compressedPath = filePath + suffix;
|
|
1346
|
+
if (existsSync(compressedPath)) {
|
|
1347
|
+
return;
|
|
1348
|
+
}
|
|
1349
|
+
const gzipOptions = { level: zlib.constants.Z_BEST_COMPRESSION };
|
|
1350
|
+
const brotliOptions = {
|
|
1351
|
+
[zlib.constants.BROTLI_PARAM_MODE]: isTextMime(mimeType) ? zlib.constants.BROTLI_MODE_TEXT : zlib.constants.BROTLI_MODE_GENERIC,
|
|
1352
|
+
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
|
|
1353
|
+
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: fileContents.length
|
|
1354
|
+
};
|
|
1355
|
+
const compressedBuff = await new Promise(
|
|
1356
|
+
(resolve2, reject) => {
|
|
1357
|
+
const cb = (error, result) => error ? reject(error) : resolve2(result);
|
|
1358
|
+
if (encoding === "gzip") {
|
|
1359
|
+
zlib.gzip(fileContents, gzipOptions, cb);
|
|
1360
|
+
} else {
|
|
1361
|
+
zlib.brotliCompress(fileContents, brotliOptions, cb);
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
);
|
|
1365
|
+
await fsp.writeFile(compressedPath, compressedBuff);
|
|
1366
|
+
})
|
|
1367
|
+
);
|
|
1368
|
+
})
|
|
1369
|
+
);
|
|
1370
|
+
}
|
|
1371
|
+
function isTextMime(mimeType) {
|
|
1372
|
+
return /text|javascript|json|xml/.test(mimeType);
|
|
1373
|
+
}
|
|
1374
|
+
const COMPRESSIBLE_MIMES_RE = /* @__PURE__ */ new Set([
|
|
1375
|
+
"application/dash+xml",
|
|
1376
|
+
"application/eot",
|
|
1377
|
+
"application/font",
|
|
1378
|
+
"application/font-sfnt",
|
|
1379
|
+
"application/javascript",
|
|
1380
|
+
"application/json",
|
|
1381
|
+
"application/opentype",
|
|
1382
|
+
"application/otf",
|
|
1383
|
+
"application/pdf",
|
|
1384
|
+
"application/pkcs7-mime",
|
|
1385
|
+
"application/protobuf",
|
|
1386
|
+
"application/rss+xml",
|
|
1387
|
+
"application/truetype",
|
|
1388
|
+
"application/ttf",
|
|
1389
|
+
"application/vnd.apple.mpegurl",
|
|
1390
|
+
"application/vnd.mapbox-vector-tile",
|
|
1391
|
+
"application/vnd.ms-fontobject",
|
|
1392
|
+
"application/wasm",
|
|
1393
|
+
"application/xhtml+xml",
|
|
1394
|
+
"application/xml",
|
|
1395
|
+
"application/x-font-opentype",
|
|
1396
|
+
"application/x-font-truetype",
|
|
1397
|
+
"application/x-font-ttf",
|
|
1398
|
+
"application/x-httpd-cgi",
|
|
1399
|
+
"application/x-javascript",
|
|
1400
|
+
"application/x-mpegurl",
|
|
1401
|
+
"application/x-opentype",
|
|
1402
|
+
"application/x-otf",
|
|
1403
|
+
"application/x-perl",
|
|
1404
|
+
"application/x-ttf",
|
|
1405
|
+
"font/eot",
|
|
1406
|
+
"font/opentype",
|
|
1407
|
+
"font/otf",
|
|
1408
|
+
"font/ttf",
|
|
1409
|
+
"image/svg+xml",
|
|
1410
|
+
"text/css",
|
|
1411
|
+
"text/csv",
|
|
1412
|
+
"text/html",
|
|
1413
|
+
"text/javascript",
|
|
1414
|
+
"text/js",
|
|
1415
|
+
"text/plain",
|
|
1416
|
+
"text/richtext",
|
|
1417
|
+
"text/tab-separated-values",
|
|
1418
|
+
"text/xml",
|
|
1419
|
+
"text/x-component",
|
|
1420
|
+
"text/x-java-source",
|
|
1421
|
+
"text/x-script",
|
|
1422
|
+
"vnd.apple.mpegurl"
|
|
1423
|
+
]);
|
|
1424
|
+
function isCompressibleMime(mimeType) {
|
|
1425
|
+
return COMPRESSIBLE_MIMES_RE.has(mimeType);
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
const NEGATION_RE = /^(!?)(.*)$/;
|
|
1429
|
+
const PARENT_DIR_GLOB_RE = /!?\.\.\//;
|
|
1430
|
+
async function scanUnprefixedPublicAssets(nitro) {
|
|
1431
|
+
const scannedPaths = [];
|
|
1432
|
+
for (const asset of nitro.options.publicAssets) {
|
|
1433
|
+
if (asset.baseURL && asset.baseURL !== "/" && !asset.fallthrough) {
|
|
1434
|
+
continue;
|
|
1435
|
+
}
|
|
1436
|
+
if (!await isDirectory(asset.dir)) {
|
|
1437
|
+
continue;
|
|
1438
|
+
}
|
|
1439
|
+
const includePatterns = getIncludePatterns(nitro, asset.dir);
|
|
1440
|
+
const publicAssets = await glob(includePatterns, {
|
|
1441
|
+
cwd: asset.dir,
|
|
1442
|
+
absolute: false,
|
|
1443
|
+
dot: true
|
|
1444
|
+
});
|
|
1445
|
+
scannedPaths.push(
|
|
1446
|
+
...publicAssets.map((file) => join(asset.baseURL || "/", file))
|
|
1447
|
+
);
|
|
1448
|
+
}
|
|
1449
|
+
return scannedPaths;
|
|
1450
|
+
}
|
|
1451
|
+
async function copyPublicAssets(nitro) {
|
|
1452
|
+
if (nitro.options.noPublicDir) {
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1455
|
+
for (const asset of nitro.options.publicAssets) {
|
|
1456
|
+
const srcDir = asset.dir;
|
|
1457
|
+
const dstDir = join(nitro.options.output.publicDir, asset.baseURL);
|
|
1458
|
+
if (await isDirectory(srcDir)) {
|
|
1459
|
+
const includePatterns = getIncludePatterns(nitro, srcDir);
|
|
1460
|
+
const publicAssets = await glob(includePatterns, {
|
|
1461
|
+
cwd: srcDir,
|
|
1462
|
+
absolute: false,
|
|
1463
|
+
dot: true
|
|
1464
|
+
});
|
|
1465
|
+
await Promise.all(
|
|
1466
|
+
publicAssets.map(async (file) => {
|
|
1467
|
+
const src = join(srcDir, file);
|
|
1468
|
+
const dst = join(dstDir, file);
|
|
1469
|
+
if (!existsSync(dst)) {
|
|
1470
|
+
await promises.cp(src, dst);
|
|
1471
|
+
}
|
|
1472
|
+
})
|
|
1473
|
+
);
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
if (nitro.options.compressPublicAssets) {
|
|
1477
|
+
await compressPublicAssets(nitro);
|
|
1478
|
+
}
|
|
1479
|
+
nitro.logger.success(
|
|
1480
|
+
"Generated public " + prettyPath(nitro.options.output.publicDir)
|
|
1481
|
+
);
|
|
1482
|
+
}
|
|
1483
|
+
function getIncludePatterns(nitro, srcDir) {
|
|
1484
|
+
return [
|
|
1485
|
+
"**",
|
|
1486
|
+
...nitro.options.ignore.map((p) => {
|
|
1487
|
+
const [_, negation, pattern] = p.match(NEGATION_RE) || [];
|
|
1488
|
+
return (
|
|
1489
|
+
// Convert ignore to include patterns
|
|
1490
|
+
(negation ? "" : "!") + // Make non-glob patterns relative to publicAssetDir
|
|
1491
|
+
(pattern.startsWith("*") ? pattern : relative(srcDir, resolve(nitro.options.srcDir, pattern)))
|
|
1492
|
+
);
|
|
1493
|
+
})
|
|
1494
|
+
].filter((p) => !PARENT_DIR_GLOB_RE.test(p));
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
async function prepare(nitro) {
|
|
1498
|
+
await prepareDir(nitro.options.output.dir);
|
|
1499
|
+
if (!nitro.options.noPublicDir) {
|
|
1500
|
+
await prepareDir(nitro.options.output.publicDir);
|
|
1501
|
+
}
|
|
1502
|
+
if (!nitro.options.static) {
|
|
1503
|
+
await prepareDir(nitro.options.output.serverDir);
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
async function prepareDir(dir) {
|
|
1507
|
+
await fsp.rm(dir, { recursive: true, force: true });
|
|
1508
|
+
await fsp.mkdir(dir, { recursive: true });
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
export { snapshotStorage as a, scanUnprefixedPublicAssets as b, createNitro as c, compressPublicAssets as d, copyPublicAssets as e, runTask as f, listTasks as g, prettyPath as h, isDirectory as i, loadOptions as l, prepare as p, resolveNitroPath as r, scanHandlers as s, writeFile as w };
|