nitro 2.2.27 → 3.0.0
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/LICENSE +2 -2
- package/README.md +11 -259
- package/dist/_chunks/app.mjs +19797 -0
- package/dist/_chunks/build.mjs +84 -0
- package/dist/_chunks/build2.mjs +318 -0
- package/dist/_chunks/build3.mjs +6452 -0
- package/dist/_chunks/detect-acorn.mjs +503 -0
- package/dist/_chunks/index.mjs +22242 -0
- package/dist/_chunks/index2.mjs +297 -0
- package/dist/_chunks/index3.mjs +1058 -0
- package/dist/_chunks/index4.mjs +1206 -0
- package/dist/_chunks/info.mjs +11157 -0
- package/dist/_chunks/json5.mjs +68 -0
- package/dist/_chunks/jsonc.mjs +51 -0
- package/dist/_chunks/pathe.M-eThtNZ.mjs +204 -0
- package/dist/_chunks/plugin.mjs +960 -0
- package/dist/_chunks/server.mjs +254 -0
- package/dist/_chunks/snapshot.mjs +284 -0
- package/dist/_chunks/toml.mjs +259 -0
- package/dist/_chunks/yaml.mjs +86 -0
- package/dist/cli/build.mjs +54 -0
- package/dist/cli/common.mjs +13 -0
- package/dist/cli/dev.mjs +95 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.mjs +477 -0
- package/dist/cli/index2.mjs +17 -0
- package/dist/cli/list.mjs +34 -0
- package/dist/cli/prepare.mjs +24 -0
- package/dist/cli/run.mjs +60 -0
- package/dist/index.d.mts +52 -0
- package/dist/index.mjs +55 -0
- package/dist/node_modules/@poppinss/colors/build/index.js +169 -0
- package/dist/node_modules/@poppinss/colors/package.json +116 -0
- package/dist/node_modules/@poppinss/dumper/build/chunk-26HALFTP.js +602 -0
- package/dist/node_modules/@poppinss/dumper/build/formatters/console/main.js +441 -0
- package/dist/node_modules/@poppinss/dumper/build/formatters/html/main.js +717 -0
- package/dist/node_modules/@poppinss/dumper/package.json +122 -0
- package/dist/node_modules/@poppinss/exception/build/index.js +63 -0
- package/dist/node_modules/@poppinss/exception/package.json +106 -0
- package/dist/node_modules/@sindresorhus/is/distribution/index.js +1313 -0
- package/dist/node_modules/@sindresorhus/is/distribution/utilities.js +3 -0
- package/dist/node_modules/@sindresorhus/is/package.json +81 -0
- package/dist/node_modules/@speed-highlight/core/dist/index.js +4 -0
- package/dist/node_modules/@speed-highlight/core/dist/terminal.js +2 -0
- package/dist/node_modules/@speed-highlight/core/package.json +90 -0
- package/dist/node_modules/cookie/dist/index.js +239 -0
- package/dist/node_modules/cookie/package.json +42 -0
- package/dist/node_modules/croner/dist/croner.js +1 -0
- package/dist/node_modules/croner/package.json +63 -0
- package/dist/node_modules/defu/dist/defu.mjs +69 -0
- package/dist/node_modules/defu/package.json +43 -0
- package/dist/node_modules/destr/dist/index.mjs +72 -0
- package/dist/node_modules/destr/package.json +47 -0
- package/dist/node_modules/error-stack-parser-es/dist/index.mjs +38 -0
- package/dist/node_modules/error-stack-parser-es/dist/lite.mjs +167 -0
- package/dist/node_modules/error-stack-parser-es/package.json +83 -0
- package/dist/node_modules/get-port-please/dist/index.mjs +430 -0
- package/dist/node_modules/get-port-please/package.json +39 -0
- package/dist/node_modules/hookable/dist/index.mjs +290 -0
- package/dist/node_modules/hookable/package.json +49 -0
- package/dist/node_modules/kleur/index.js +110 -0
- package/dist/node_modules/kleur/index.mjs +110 -0
- package/dist/node_modules/kleur/package.json +51 -0
- package/dist/node_modules/klona/dist/index.mjs +81 -0
- package/dist/node_modules/klona/full/index.mjs +53 -0
- package/dist/node_modules/klona/package.json +74 -0
- package/dist/node_modules/scule/dist/index.mjs +80 -0
- package/dist/node_modules/scule/package.json +43 -0
- package/dist/node_modules/source-map/lib/array-set.js +100 -0
- package/dist/node_modules/source-map/lib/base64-vlq.js +94 -0
- package/dist/node_modules/source-map/lib/base64.js +19 -0
- package/dist/node_modules/source-map/lib/binary-search.js +113 -0
- package/dist/node_modules/source-map/lib/mapping-list.js +83 -0
- package/dist/node_modules/source-map/lib/mappings.wasm +0 -0
- package/dist/node_modules/source-map/lib/read-wasm.js +27 -0
- package/dist/node_modules/source-map/lib/source-map-consumer.js +1081 -0
- package/dist/node_modules/source-map/lib/source-map-generator.js +439 -0
- package/dist/node_modules/source-map/lib/source-node.js +430 -0
- package/dist/node_modules/source-map/lib/url.js +13 -0
- package/dist/node_modules/source-map/lib/util.js +444 -0
- package/dist/node_modules/source-map/lib/wasm.js +138 -0
- package/dist/node_modules/source-map/package.json +79 -0
- package/dist/node_modules/source-map/source-map.js +10 -0
- package/dist/node_modules/std-env/dist/index.mjs +1 -0
- package/dist/node_modules/std-env/package.json +46 -0
- package/dist/node_modules/supports-color/index.js +202 -0
- package/dist/node_modules/supports-color/package.json +64 -0
- package/dist/node_modules/ufo/dist/index.mjs +638 -0
- package/dist/node_modules/ufo/package.json +47 -0
- package/dist/node_modules/unctx/dist/index.mjs +131 -0
- package/dist/node_modules/unctx/package.json +67 -0
- package/dist/node_modules/youch/build/chunk-4L7RY2JA.js +42 -0
- package/dist/node_modules/youch/build/chunk-4XB2BYKC.js +85 -0
- package/dist/node_modules/youch/build/chunk-4YEN7HVQ.js +61 -0
- package/dist/node_modules/youch/build/chunk-EUJBVOYB.js +83 -0
- package/dist/node_modules/youch/build/chunk-HFSXRSKS.js +85 -0
- package/dist/node_modules/youch/build/chunk-JAN2TFI2.js +222 -0
- package/dist/node_modules/youch/build/chunk-OSUFJZHZ.js +6 -0
- package/dist/node_modules/youch/build/chunk-PINJDICN.js +72 -0
- package/dist/node_modules/youch/build/chunk-PUHGL6HA.js +41 -0
- package/dist/node_modules/youch/build/chunk-VE4LENUR.js +45 -0
- package/dist/node_modules/youch/build/index.js +352 -0
- package/dist/node_modules/youch/build/public/error_cause/style.css +5 -0
- package/dist/node_modules/youch/build/public/error_info/script.js +13 -0
- package/dist/node_modules/youch/build/public/error_info/style.css +144 -0
- package/dist/node_modules/youch/build/public/error_stack/script.js +74 -0
- package/dist/node_modules/youch/build/public/error_stack/style.css +220 -0
- package/dist/node_modules/youch/build/public/error_stack_source/style.css +123 -0
- package/dist/node_modules/youch/build/public/header/script.js +16 -0
- package/dist/node_modules/youch/build/public/header/style.css +56 -0
- package/dist/node_modules/youch/build/public/layout/script.js +20 -0
- package/dist/node_modules/youch/build/public/layout/style.css +431 -0
- package/dist/node_modules/youch/package.json +133 -0
- package/dist/node_modules/youch-core/build/index.js +316 -0
- package/dist/node_modules/youch-core/package.json +124 -0
- package/dist/presets/_nitro/runtime/nitro-dev.d.mts +1 -0
- package/dist/presets/_nitro/runtime/nitro-dev.mjs +62 -0
- package/dist/presets/_nitro/runtime/nitro-prerenderer.d.mts +3 -0
- package/dist/presets/_nitro/runtime/nitro-prerenderer.mjs +18 -0
- package/dist/presets/_nitro/runtime/service-worker.d.mts +1 -0
- package/dist/presets/_nitro/runtime/service-worker.mjs +21 -0
- package/dist/presets/aws-amplify/runtime/aws-amplify.d.mts +1 -0
- package/dist/presets/aws-amplify/runtime/aws-amplify.mjs +13 -0
- package/dist/presets/aws-lambda/runtime/_utils.d.mts +18 -0
- package/dist/presets/aws-lambda/runtime/_utils.mjs +102 -0
- package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.d.mts +3 -0
- package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.mjs +33 -0
- package/dist/presets/aws-lambda/runtime/aws-lambda.d.mts +3 -0
- package/dist/presets/aws-lambda/runtime/aws-lambda.mjs +13 -0
- package/dist/presets/azure/runtime/_utils.d.mts +2 -0
- package/dist/presets/azure/runtime/_utils.mjs +50 -0
- package/dist/presets/azure/runtime/azure-swa.d.mts +5 -0
- package/dist/presets/azure/runtime/azure-swa.mjs +28 -0
- package/dist/presets/bun/runtime/bun.d.mts +1 -0
- package/dist/presets/bun/runtime/bun.mjs +27 -0
- package/dist/presets/cloudflare/runtime/_module-handler.d.mts +13 -0
- package/dist/presets/cloudflare/runtime/_module-handler.mjs +93 -0
- package/dist/presets/cloudflare/runtime/cloudflare-durable.d.mts +19 -0
- package/dist/presets/cloudflare/runtime/cloudflare-durable.mjs +74 -0
- package/dist/presets/cloudflare/runtime/cloudflare-module.d.mts +9 -0
- package/dist/presets/cloudflare/runtime/cloudflare-module.mjs +20 -0
- package/dist/presets/cloudflare/runtime/cloudflare-pages.d.mts +20 -0
- package/dist/presets/cloudflare/runtime/cloudflare-pages.mjs +47 -0
- package/dist/presets/cloudflare/runtime/plugin.dev.d.mts +3 -0
- package/dist/presets/cloudflare/runtime/plugin.dev.mjs +89 -0
- package/dist/presets/deno/runtime/deno-deploy.d.mts +1 -0
- package/dist/presets/deno/runtime/deno-deploy.mjs +17 -0
- package/dist/presets/deno/runtime/deno-server.d.mts +6 -0
- package/dist/presets/deno/runtime/deno-server.mjs +59 -0
- package/dist/presets/netlify/runtime/netlify-edge.d.mts +3 -0
- package/dist/presets/netlify/runtime/netlify-edge.mjs +17 -0
- package/dist/presets/netlify/runtime/netlify.d.mts +3 -0
- package/dist/presets/netlify/runtime/netlify.mjs +24 -0
- package/dist/presets/node/runtime/node-cluster.d.mts +1 -0
- package/dist/presets/node/runtime/node-cluster.mjs +58 -0
- package/dist/presets/node/runtime/node-middleware.d.mts +4 -0
- package/dist/presets/node/runtime/node-middleware.mjs +14 -0
- package/dist/presets/node/runtime/node-server.d.mts +3 -0
- package/dist/presets/node/runtime/node-server.mjs +44 -0
- package/dist/presets/standard/runtime/server.d.mts +5 -0
- package/dist/presets/standard/runtime/server.mjs +6 -0
- package/dist/presets/stormkit/runtime/stormkit.d.mts +21 -0
- package/dist/presets/stormkit/runtime/stormkit.mjs +28 -0
- package/dist/presets/vercel/runtime/vercel.d.mts +8 -0
- package/dist/presets/vercel/runtime/vercel.mjs +17 -0
- package/dist/presets/winterjs/runtime/winterjs.d.mts +1 -0
- package/dist/presets/winterjs/runtime/winterjs.mjs +87 -0
- package/dist/presets/zeabur/runtime/zeabur.d.mts +3 -0
- package/dist/presets/zeabur/runtime/zeabur.mjs +4 -0
- package/dist/presets.mjs +2460 -0
- package/dist/runtime/index.d.mts +11 -0
- package/dist/runtime/index.mjs +17 -0
- package/dist/runtime/internal/app.d.mts +2 -0
- package/dist/runtime/internal/app.mjs +153 -0
- package/dist/runtime/internal/cache.d.mts +7 -0
- package/dist/runtime/internal/cache.mjs +236 -0
- package/dist/runtime/internal/context.d.mts +12 -0
- package/dist/runtime/internal/context.mjs +17 -0
- package/dist/runtime/internal/database.d.mts +2 -0
- package/dist/runtime/internal/database.mjs +14 -0
- package/dist/runtime/internal/debug.d.mts +2 -0
- package/dist/runtime/internal/debug.mjs +5 -0
- package/dist/runtime/internal/empty.d.mts +0 -0
- package/dist/runtime/internal/empty.mjs +0 -0
- package/dist/runtime/internal/error/dev.d.mts +9 -0
- package/dist/runtime/internal/error/dev.mjs +122 -0
- package/dist/runtime/internal/error/prod.d.mts +8 -0
- package/dist/runtime/internal/error/prod.mjs +62 -0
- package/dist/runtime/internal/error/utils.d.mts +8 -0
- package/dist/runtime/internal/error/utils.mjs +3 -0
- package/dist/runtime/internal/index.d.mts +3 -0
- package/dist/runtime/internal/index.mjs +3 -0
- package/dist/runtime/internal/lib/http-graceful-shutdown.d.mts +16 -0
- package/dist/runtime/internal/lib/http-graceful-shutdown.mjs +213 -0
- package/dist/runtime/internal/meta.d.mts +2 -0
- package/dist/runtime/internal/meta.mjs +3 -0
- package/dist/runtime/internal/plugin.d.mts +3 -0
- package/dist/runtime/internal/plugin.mjs +4 -0
- package/dist/runtime/internal/renderer.d.mts +3 -0
- package/dist/runtime/internal/renderer.mjs +36 -0
- package/dist/runtime/internal/route-rules.d.mts +8 -0
- package/dist/runtime/internal/route-rules.mjs +62 -0
- package/dist/runtime/internal/routes/dev-tasks.d.mts +3 -0
- package/dist/runtime/internal/routes/dev-tasks.mjs +23 -0
- package/dist/runtime/internal/routes/openapi.d.mts +3 -0
- package/dist/runtime/internal/routes/openapi.mjs +100 -0
- package/dist/runtime/internal/routes/renderer-template.d.mts +2 -0
- package/dist/runtime/internal/routes/renderer-template.dev.d.mts +3 -0
- package/dist/runtime/internal/routes/renderer-template.dev.mjs +21 -0
- package/dist/runtime/internal/routes/renderer-template.mjs +4 -0
- package/dist/runtime/internal/routes/scalar.d.mts +3 -0
- package/dist/runtime/internal/routes/scalar.mjs +194 -0
- package/dist/runtime/internal/routes/swagger.d.mts +3 -0
- package/dist/runtime/internal/routes/swagger.mjs +44 -0
- package/dist/runtime/internal/runtime-config.d.mts +9 -0
- package/dist/runtime/internal/runtime-config.mjs +49 -0
- package/dist/runtime/internal/shutdown.d.mts +9 -0
- package/dist/runtime/internal/shutdown.mjs +34 -0
- package/dist/runtime/internal/static.d.mts +3 -0
- package/dist/runtime/internal/static.mjs +77 -0
- package/dist/runtime/internal/storage.d.mts +2 -0
- package/dist/runtime/internal/storage.mjs +6 -0
- package/dist/runtime/internal/task.d.mts +17 -0
- package/dist/runtime/internal/task.mjs +73 -0
- package/dist/runtime/internal/utils.d.mts +1 -0
- package/dist/runtime/internal/utils.mjs +15 -0
- package/dist/runtime/internal/vite/dev-entry.d.mts +1 -0
- package/dist/runtime/internal/vite/dev-entry.mjs +6 -0
- package/dist/runtime/internal/vite/dev-worker.d.mts +1 -0
- package/dist/runtime/internal/vite/dev-worker.mjs +362 -0
- package/dist/runtime/internal/vite/prod-setup.d.mts +4 -0
- package/dist/runtime/internal/vite/prod-setup.mjs +55 -0
- package/dist/runtime/internal/vite/ssr-renderer.d.mts +4 -0
- package/dist/runtime/internal/vite/ssr-renderer.mjs +4 -0
- package/dist/types/index.d.mts +2810 -0
- package/dist/types/index.mjs +1 -0
- package/dist/vite.d.mts +59 -0
- package/dist/vite.mjs +68 -0
- package/lib/config.d.mts +9 -0
- package/lib/config.mjs +5 -0
- package/lib/deps/h3.d.mts +1 -0
- package/lib/deps/h3.mjs +1 -0
- package/lib/deps/ofetch.mjs +1 -0
- package/lib/deps/ofetch.mts +1 -0
- package/lib/meta.d.mts +5 -0
- package/lib/meta.mjs +21 -0
- package/lib/runtime-meta.d.mts +5 -0
- package/lib/runtime-meta.mjs +38 -0
- package/package.json +170 -38
- package/.editorconfig +0 -12
- package/.eslintrc.js +0 -34
- package/.travis.yml +0 -7
- package/Dockerfile +0 -12
- package/Makefile +0 -37
- package/bin/nitro +0 -55
- package/lib/auto-require.js +0 -44
- package/lib/class-file.js +0 -105
- package/lib/class-files.js +0 -212
- package/lib/color.js +0 -89
- package/lib/cwd.js +0 -27
- package/lib/dir-expand.js +0 -80
- package/lib/dir.js +0 -130
- package/lib/file.js +0 -80
- package/lib/join-paths.js +0 -60
- package/lib/livereload/livereload.js +0 -55
- package/lib/livereload.js +0 -86
- package/lib/nitro.js +0 -161
- package/lib/package.js +0 -211
- package/lib/presets/autoprefixer.js +0 -11
- package/lib/presets/babel.js +0 -33
- package/lib/presets/browserify.js +0 -106
- package/lib/presets/coffee-script.js +0 -29
- package/lib/presets/cssnano.js +0 -11
- package/lib/presets/eslint.js +0 -67
- package/lib/presets/jshint.js +0 -51
- package/lib/presets/less.js +0 -45
- package/lib/presets/log.js +0 -11
- package/lib/presets/ng-annotate.js +0 -34
- package/lib/presets/noop.js +0 -7
- package/lib/presets/postcss/auto-postcss.js +0 -46
- package/lib/presets/postcss/autoprefixer-options.js +0 -9
- package/lib/presets/postcss.js +0 -40
- package/lib/presets/rollup.js +0 -56
- package/lib/presets/sass.js +0 -64
- package/lib/presets/sitemap.js +0 -101
- package/lib/presets/sitemap.xml +0 -12
- package/lib/presets/svg-symbols.js +0 -22
- package/lib/presets/svgo.js +0 -21
- package/lib/presets/uglify.js +0 -72
- package/lib/presets/webpack.js +0 -66
- package/lib/processors.js +0 -84
- package/lib/promisify.js +0 -16
- package/lib/server.js +0 -224
- package/lib/tasks.js +0 -114
- package/lib/watch.js +0 -81
- package/test.js +0 -59
- package/tests/cwd-tests.js +0 -25
- package/tests/dir-tests.js +0 -80
- package/tests/dummy/dummy.coffee +0 -7
- package/tests/dummy/dummy.js +0 -8
- package/tests/dummy/dummy.less +0 -6
- package/tests/dummy/dummy.sass +0 -5
- package/tests/file-tests.js +0 -17
- package/tests/join-paths-tests.js +0 -86
- package/tests/tasks-tests.js +0 -65
- package/wercker.yml +0 -25
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { parentPort, threadId, workerData } from "node:worker_threads";
|
|
2
|
+
import { Agent } from "undici";
|
|
3
|
+
import { ModuleRunner, ESModulesEvaluator } from "vite/module-runner";
|
|
4
|
+
import { getSocketAddress, isSocketSupported } from "get-port-please";
|
|
5
|
+
|
|
6
|
+
// ----- Environment runners -----
|
|
7
|
+
|
|
8
|
+
const envs = { nitro: undefined, ssr: undefined };
|
|
9
|
+
|
|
10
|
+
globalThis.__nitro_vite_envs__ = envs;
|
|
11
|
+
|
|
12
|
+
class EnvRunner {
|
|
13
|
+
constructor({ name, entry }) {
|
|
14
|
+
this.name = name;
|
|
15
|
+
this.entryPath = entry;
|
|
16
|
+
|
|
17
|
+
this.entry = undefined;
|
|
18
|
+
this.entryError = undefined;
|
|
19
|
+
|
|
20
|
+
// Create Vite Module Runner
|
|
21
|
+
// https://vite.dev/guide/api-environment-runtimes.html#modulerunner
|
|
22
|
+
this.runnerHooks = {};
|
|
23
|
+
this.runner = new ModuleRunner(
|
|
24
|
+
{
|
|
25
|
+
transport: {
|
|
26
|
+
connect({ onMessage, onDisconnection }) {
|
|
27
|
+
parentPort.on("message", (payload) => {
|
|
28
|
+
if (payload?.type === "custom" && payload.viteEnv === name) {
|
|
29
|
+
onMessage(payload);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
parentPort.on("close", onDisconnection);
|
|
33
|
+
},
|
|
34
|
+
send(payload) {
|
|
35
|
+
parentPort.postMessage({ ...payload, viteEnv: name });
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
new ESModulesEvaluator(),
|
|
40
|
+
process.env.DEBUG ? console.debug : undefined
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
this.reload();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async reload() {
|
|
47
|
+
try {
|
|
48
|
+
this.entry = await this.runner.import(this.entryPath);
|
|
49
|
+
this.entryError = undefined;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error(error);
|
|
52
|
+
this.entryError = error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async fetch(req, init) {
|
|
57
|
+
if (this.entryError) {
|
|
58
|
+
return renderError(req, this.entryError);
|
|
59
|
+
}
|
|
60
|
+
for (let i = 0; i < 5 && !(this.entry || this.entryError); i++) {
|
|
61
|
+
await new Promise((r) => setTimeout(r, 100 * Math.pow(2, i)));
|
|
62
|
+
}
|
|
63
|
+
if (this.entryError) {
|
|
64
|
+
return renderError(req, this.entryError);
|
|
65
|
+
}
|
|
66
|
+
if (!this.entry) {
|
|
67
|
+
throw httpError(503, `Vite environment "${this.name}" is unavailable`);
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const entryFetch = this.entry.fetch || this.entry.default?.fetch;
|
|
71
|
+
if (!entryFetch) {
|
|
72
|
+
throw httpError(
|
|
73
|
+
500,
|
|
74
|
+
`No fetch handler exported from ${this.entryPath}`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return await entryFetch(req, init);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return renderError(req, error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ----- RPC listeners -----
|
|
85
|
+
|
|
86
|
+
const viteHostRequests = new Map();
|
|
87
|
+
|
|
88
|
+
async function requestToViteHost(
|
|
89
|
+
name,
|
|
90
|
+
data,
|
|
91
|
+
id = Math.random().toString(16).slice(2),
|
|
92
|
+
timeout = 3000
|
|
93
|
+
) {
|
|
94
|
+
setTimeout(() => {
|
|
95
|
+
if (viteHostRequests.has(id)) {
|
|
96
|
+
viteHostRequests.delete(id);
|
|
97
|
+
reject(new Error(`Request to vite host timed out (${name}:${id})`));
|
|
98
|
+
}
|
|
99
|
+
}, timeout);
|
|
100
|
+
let resolve, reject;
|
|
101
|
+
const promise = new Promise((_resolve, _reject) => {
|
|
102
|
+
resolve = (value) => {
|
|
103
|
+
viteHostRequests.delete(id);
|
|
104
|
+
return _resolve(value);
|
|
105
|
+
};
|
|
106
|
+
reject = (err) => {
|
|
107
|
+
viteHostRequests.delete(id);
|
|
108
|
+
return _reject(err);
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
viteHostRequests.set(id, { resolve, reject });
|
|
112
|
+
parentPort.postMessage({
|
|
113
|
+
type: "custom",
|
|
114
|
+
event: "nitro:vite-invoke",
|
|
115
|
+
data: { name, id, data },
|
|
116
|
+
});
|
|
117
|
+
return promise;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
parentPort.on("message", (payload) => {
|
|
121
|
+
if (payload?.type !== "custom") {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
switch (payload.event) {
|
|
125
|
+
case "nitro:vite-server-addr": {
|
|
126
|
+
viteServerAddr = payload.data;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case "nitro:vite-env": {
|
|
130
|
+
const { name, entry } = payload.data;
|
|
131
|
+
if (envs[name]) {
|
|
132
|
+
console.error(`Vite environment "${name}" already registered!`);
|
|
133
|
+
} else {
|
|
134
|
+
envs[name] = new EnvRunner({ name, entry });
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case "nitro:vite-invoke-response": {
|
|
139
|
+
const { id, data: response } = payload.data;
|
|
140
|
+
const req = viteHostRequests.get(id);
|
|
141
|
+
if (req) {
|
|
142
|
+
if (response.error) {
|
|
143
|
+
req.reject(response.error);
|
|
144
|
+
} else {
|
|
145
|
+
req.resolve(response.data);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// ----- Fetch Handler -----
|
|
154
|
+
|
|
155
|
+
const originalFetch = globalThis.fetch;
|
|
156
|
+
globalThis.fetch = function nitroViteFetch(input, init) {
|
|
157
|
+
// Only override if viteEnvName is specified
|
|
158
|
+
const viteEnvName = getViteEnv(init) || getViteEnv(input);
|
|
159
|
+
if (!viteEnvName) {
|
|
160
|
+
return originalFetch(input, init);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Validate viteEnv
|
|
164
|
+
const viteEnv = envs[viteEnvName];
|
|
165
|
+
if (!viteEnv) {
|
|
166
|
+
throw httpError(404, `Unknown vite environment "${viteEnvName}"`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Normalize input (relative urls)
|
|
170
|
+
if (typeof input === "string" && input[0] === "/") {
|
|
171
|
+
input = new URL(input, "http://localhost");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Clone headers and set viteEnv header
|
|
175
|
+
const headers = new Headers(init?.headers || {});
|
|
176
|
+
headers.set("x-vite-env", viteEnvName);
|
|
177
|
+
|
|
178
|
+
// Normalize to Request
|
|
179
|
+
if (
|
|
180
|
+
!(input instanceof Request) ||
|
|
181
|
+
(init && Object.keys(init).join("") !== "viteEnv")
|
|
182
|
+
) {
|
|
183
|
+
input = new Request(input, init);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Fetch via vite env
|
|
187
|
+
return viteEnv.fetch(input);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
function getViteEnv(input) {
|
|
191
|
+
if (!input || typeof input !== "object") {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if ("viteEnv" in input) {
|
|
195
|
+
return input.viteEnv;
|
|
196
|
+
}
|
|
197
|
+
if (input.headers) {
|
|
198
|
+
return (
|
|
199
|
+
input.headers["x-vite-env"] ||
|
|
200
|
+
input.headers.get?.("x-vite-env") ||
|
|
201
|
+
(Array.isArray(input.headers) &&
|
|
202
|
+
input.headers.find((h) => h[0].toLowerCase() === "x-vite-env")?.[1])
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ----- Server -----
|
|
208
|
+
|
|
209
|
+
async function reload() {
|
|
210
|
+
try {
|
|
211
|
+
// Apply globals
|
|
212
|
+
for (const [key, value] of Object.entries(workerData.globals || {})) {
|
|
213
|
+
globalThis[key] = value;
|
|
214
|
+
}
|
|
215
|
+
// Reload all envs
|
|
216
|
+
await Promise.all(Object.values(envs).map((env) => env?.reload()));
|
|
217
|
+
} catch (error) {
|
|
218
|
+
console.error(error);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// eslint-disable-next-line unicorn/prefer-top-level-await
|
|
223
|
+
reload();
|
|
224
|
+
|
|
225
|
+
if (workerData.server) {
|
|
226
|
+
const { createServer } = await import("node:http");
|
|
227
|
+
const { toNodeHandler } = await import("srvx/node");
|
|
228
|
+
const server = createServer(
|
|
229
|
+
toNodeHandler(async (req, init) => {
|
|
230
|
+
const viteEnv =
|
|
231
|
+
init?.viteEnv || req?.headers.get("x-vite-env") || "nitro"; // TODO
|
|
232
|
+
const env = envs[viteEnv];
|
|
233
|
+
if (!env) {
|
|
234
|
+
return renderError(
|
|
235
|
+
req,
|
|
236
|
+
httpError(500, `Unknown vite environment "${viteEnv}"`)
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
return env.fetch(req, init);
|
|
240
|
+
})
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
parentPort.on("message", async (message) => {
|
|
244
|
+
if (message?.type === "full-reload") {
|
|
245
|
+
await reload();
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
await listen(server);
|
|
249
|
+
const address = server.address();
|
|
250
|
+
parentPort?.postMessage({
|
|
251
|
+
event: "listen",
|
|
252
|
+
address:
|
|
253
|
+
typeof address === "string"
|
|
254
|
+
? { socketPath: address }
|
|
255
|
+
: { host: "localhost", port: address?.port },
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ----- HTML Transform -----
|
|
260
|
+
|
|
261
|
+
globalThis.__transform_html__ = async function (html) {
|
|
262
|
+
html = await requestToViteHost("transformHTML", html).catch((error) => {
|
|
263
|
+
console.warn("Failed to transform HTML via Vite:", error);
|
|
264
|
+
return html;
|
|
265
|
+
});
|
|
266
|
+
return html;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// ----- Error handling -----
|
|
270
|
+
|
|
271
|
+
function httpError(status, message) {
|
|
272
|
+
const error = new Error(message || `HTTP Error ${status}`);
|
|
273
|
+
error.status = status;
|
|
274
|
+
error.name = "NitroViteError";
|
|
275
|
+
return error;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function renderError(req, error) {
|
|
279
|
+
const { Youch } = await import("youch");
|
|
280
|
+
const youch = new Youch();
|
|
281
|
+
return new Response(await youch.toHTML(error), {
|
|
282
|
+
status: error.status || 500,
|
|
283
|
+
headers: {
|
|
284
|
+
"Content-Type": "text/html",
|
|
285
|
+
"Cache-Control": "no-store, max-age=0, must-revalidate",
|
|
286
|
+
Pragma: "no-cache",
|
|
287
|
+
Expires: "0",
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ----- Internal Utils -----
|
|
293
|
+
|
|
294
|
+
async function listen(server) {
|
|
295
|
+
const listenAddr = (await isSocketSupported())
|
|
296
|
+
? getSocketAddress({
|
|
297
|
+
name: `nitro-vite-${threadId}`,
|
|
298
|
+
pid: true,
|
|
299
|
+
random: true,
|
|
300
|
+
})
|
|
301
|
+
: { port: 0, host: "localhost" };
|
|
302
|
+
return new Promise((resolve, reject) => {
|
|
303
|
+
try {
|
|
304
|
+
server.listen(listenAddr, () => resolve());
|
|
305
|
+
} catch (error) {
|
|
306
|
+
reject(error);
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function fetchAddress(addr, input, inputInit) {
|
|
312
|
+
let url;
|
|
313
|
+
let init;
|
|
314
|
+
if (input instanceof Request) {
|
|
315
|
+
url = new URL(input.url);
|
|
316
|
+
init = {
|
|
317
|
+
method: input.method,
|
|
318
|
+
headers: input.headers,
|
|
319
|
+
body: input.body,
|
|
320
|
+
...inputInit,
|
|
321
|
+
};
|
|
322
|
+
} else {
|
|
323
|
+
url = new URL(input);
|
|
324
|
+
init = inputInit;
|
|
325
|
+
}
|
|
326
|
+
init = {
|
|
327
|
+
duplex: "half",
|
|
328
|
+
redirect: "manual",
|
|
329
|
+
...init,
|
|
330
|
+
};
|
|
331
|
+
if (addr.socketPath) {
|
|
332
|
+
url.protocol = "http:";
|
|
333
|
+
return fetch(url, {
|
|
334
|
+
...init,
|
|
335
|
+
...fetchSocketOptions(addr.socketPath),
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
const origin = `http://${addr.host}${addr.port ? `:${addr.port}` : ""}`;
|
|
339
|
+
const outURL = new URL(url.pathname + url.search, origin);
|
|
340
|
+
return fetch(outURL, init);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function fetchSocketOptions(socketPath) {
|
|
344
|
+
if ("Bun" in globalThis) {
|
|
345
|
+
// https://bun.sh/guides/http/fetch-unix
|
|
346
|
+
return { unix: socketPath };
|
|
347
|
+
}
|
|
348
|
+
if ("Deno" in globalThis) {
|
|
349
|
+
// https://github.com/denoland/deno/pull/29154
|
|
350
|
+
return {
|
|
351
|
+
client: Deno.createHttpClient({
|
|
352
|
+
// @ts-expect-error Missing types?
|
|
353
|
+
transport: "unix",
|
|
354
|
+
path: socketPath,
|
|
355
|
+
}),
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
// https://github.com/nodejs/undici/issues/2970
|
|
359
|
+
return {
|
|
360
|
+
dispatcher: new Agent({ connect: { socketPath } }),
|
|
361
|
+
};
|
|
362
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export function setupVite({ manifest, services }) {
|
|
2
|
+
globalThis.__VITE_MANIFEST__ = manifest;
|
|
3
|
+
|
|
4
|
+
const originalFetch = globalThis.fetch;
|
|
5
|
+
globalThis.fetch = function nitroViteFetch(input, init) {
|
|
6
|
+
// Only override if viteEnvName is specified
|
|
7
|
+
const viteEnvName = getViteEnv(init) || getViteEnv(input);
|
|
8
|
+
if (!viteEnvName) {
|
|
9
|
+
return originalFetch(input, init);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Validate viteEnv
|
|
13
|
+
const viteEnv = services[viteEnvName];
|
|
14
|
+
if (!viteEnv) {
|
|
15
|
+
throw httpError(404);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Normalize input (relative urls)
|
|
19
|
+
if (typeof input === "string" && input[0] === "/") {
|
|
20
|
+
input = new URL(input, "http://localhost");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Clone headers and set viteEnv header
|
|
24
|
+
const headers = new Headers(init?.headers || {});
|
|
25
|
+
headers.set("x-vite-env", viteEnvName);
|
|
26
|
+
|
|
27
|
+
// Normalize to Request
|
|
28
|
+
if (
|
|
29
|
+
!(input instanceof Request) ||
|
|
30
|
+
(init && Object.keys(init).join("") !== "viteEnv")
|
|
31
|
+
) {
|
|
32
|
+
input = new Request(input, init);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Fetch via vite env
|
|
36
|
+
return viteEnv.fetch(input);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getViteEnv(input) {
|
|
41
|
+
if (!input || typeof input !== "object") {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if ("viteEnv" in input) {
|
|
45
|
+
return input.viteEnv;
|
|
46
|
+
}
|
|
47
|
+
if (input.headers) {
|
|
48
|
+
return (
|
|
49
|
+
input.headers["x-vite-env"] ||
|
|
50
|
+
input.headers.get?.("x-vite-env") ||
|
|
51
|
+
(Array.isArray(input.headers) &&
|
|
52
|
+
input.headers.find((h) => h[0].toLowerCase() === "x-vite-env")?.[1])
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|