@vite-hub/env 0.0.3 → 0.1.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 +201 -0
- package/README.md +243 -28
- package/dist/access-B3qzBHye.js +54 -0
- package/dist/auth.d.ts +32 -0
- package/dist/auth.js +44 -0
- package/dist/bridge-CdNdTN1V.d.ts +374 -0
- package/dist/bridge-error-CFuU5zIz.js +65 -0
- package/dist/bridge.d.ts +2 -0
- package/dist/bridge.js +192 -0
- package/dist/cli-CMQPQbii.js +487 -0
- package/dist/cli-DEwcUIe0.d.ts +81 -0
- package/dist/cli.d.ts +41 -0
- package/dist/cli.js +2 -0
- package/dist/database.d.ts +21 -0
- package/dist/database.js +183 -0
- package/dist/declarations-CiRdb1qP.d.ts +32 -0
- package/dist/declarations-DAVgctKD.js +236 -0
- package/dist/error-diagnostics-CrxjYNYr.js +43 -0
- package/dist/http.d.ts +2 -0
- package/dist/http.js +122 -0
- package/dist/index.d.ts +13 -8
- package/dist/index.js +5 -4
- package/dist/internal/agent.d.ts +5 -0
- package/dist/internal/agent.js +22 -0
- package/dist/internal/connections.d.ts +18 -0
- package/dist/internal/connections.js +31 -0
- package/dist/presets.d.ts +15 -6
- package/dist/presets.js +21 -2
- package/dist/provider.d.ts +5 -0
- package/dist/provider.js +8 -0
- package/dist/schema.d.ts +5 -6
- package/dist/schema.js +26 -8
- package/dist/seal.d.ts +14 -0
- package/dist/seal.js +49 -0
- package/dist/secret-C2191nAa.d.ts +11 -0
- package/dist/secret.d.ts +1 -10
- package/dist/server-DufVswRJ.js +670 -0
- package/dist/server.d.ts +23 -4
- package/dist/server.js +2 -3
- package/dist/values-D_r6V774.js +148 -0
- package/dist/virtual-module.d.ts +34 -0
- package/dist/virtual.d.ts +2 -15
- package/dist/vite.d.ts +37 -14
- package/dist/vite.js +1058 -292
- package/package.json +84 -16
- package/dist/declarations-BIBfEwer.d.ts +0 -23
- package/dist/declarations-Cy5XamUA.js +0 -144
- package/dist/errors-RUmAFDJn.js +0 -112
- package/dist/server-B2Dp8Nc0.js +0 -59
- package/dist/types-FcnPKias.d.ts +0 -153
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { t as envErrorDiagnostics } from "./error-diagnostics-CrxjYNYr.js";
|
|
2
|
+
import { _ as readViteHubSecret, a as isBlockingServerEnvEntry, g as deriveViteHubSecret } from "./server-DufVswRJ.js";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import * as v from "valibot";
|
|
5
|
+
import { existsSync, statSync } from "node:fs";
|
|
6
|
+
import { basename, dirname, join, parse, resolve } from "node:path";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { randomBytes } from "node:crypto";
|
|
10
|
+
//#region ../internal/src/build/vite.ts
|
|
11
|
+
const projectRootDirectoryMarkers = [
|
|
12
|
+
["server", "agents"],
|
|
13
|
+
["server", "channels"],
|
|
14
|
+
["server", "connections"],
|
|
15
|
+
["server", "browsers"],
|
|
16
|
+
["server", "emails"],
|
|
17
|
+
["server", "schedules"],
|
|
18
|
+
["server", "workspaces"]
|
|
19
|
+
];
|
|
20
|
+
const projectRootFileMarkers = [
|
|
21
|
+
["package.json"],
|
|
22
|
+
["nuxt.config.ts"],
|
|
23
|
+
["nuxt.config.mts"],
|
|
24
|
+
["nuxt.config.js"],
|
|
25
|
+
["nuxt.config.mjs"]
|
|
26
|
+
];
|
|
27
|
+
const VITEHUB_ENV_PUBLIC_ID = "#vitehub/env/public";
|
|
28
|
+
const VITEHUB_ENV_SERVER_ID = "#vitehub/env/server";
|
|
29
|
+
const VITEHUB_ENV_DESCRIPTION_ID = "#vitehub/env/description";
|
|
30
|
+
function resolveViteHubProjectRoot(root, options = {}) {
|
|
31
|
+
const resolvedRoot = resolve(root);
|
|
32
|
+
const temporaryRoot = resolve(tmpdir());
|
|
33
|
+
const sharedTemporaryRoot = dirname(temporaryRoot) === parse(temporaryRoot).root ? temporaryRoot : void 0;
|
|
34
|
+
if (options.projectRoot) return resolve(resolvedRoot, options.projectRoot);
|
|
35
|
+
if (basename(resolvedRoot) === "app") {
|
|
36
|
+
const parent = dirname(resolvedRoot);
|
|
37
|
+
if (parent !== sharedTemporaryRoot && (hasProjectRootDirectoryMarker(parent) || hasProjectRootFileMarker(parent, [
|
|
38
|
+
"nuxt.config.ts",
|
|
39
|
+
"nuxt.config.mts",
|
|
40
|
+
"nuxt.config.js",
|
|
41
|
+
"nuxt.config.mjs"
|
|
42
|
+
]))) return parent;
|
|
43
|
+
}
|
|
44
|
+
let current = resolvedRoot;
|
|
45
|
+
while (true) {
|
|
46
|
+
if (current === sharedTemporaryRoot && resolvedRoot !== current) return resolvedRoot;
|
|
47
|
+
if (hasProjectRootMarker(current)) return current;
|
|
48
|
+
const parent = dirname(current);
|
|
49
|
+
if (parent === current) return resolvedRoot;
|
|
50
|
+
current = parent;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function hasProjectRootDirectoryMarker(root) {
|
|
54
|
+
for (const marker of projectRootDirectoryMarkers) try {
|
|
55
|
+
if (statSync(resolve(root, ...marker)).isDirectory()) return true;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error.code !== "ENOENT") throw error;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
function hasProjectRootMarker(root) {
|
|
62
|
+
if (hasProjectRootDirectoryMarker(root)) return true;
|
|
63
|
+
return hasProjectRootFileMarker(root, projectRootFileMarkers.flat());
|
|
64
|
+
}
|
|
65
|
+
function hasProjectRootFileMarker(root, markers) {
|
|
66
|
+
for (const marker of markers) {
|
|
67
|
+
const path = Array.isArray(marker) ? marker : [marker];
|
|
68
|
+
try {
|
|
69
|
+
if (statSync(resolve(root, ...path)).isFile()) return true;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (error.code !== "ENOENT") throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
function viteHubEnvAmbientTypesPath(root) {
|
|
77
|
+
return resolve(root, ".vitehub", "types", "env.d.ts");
|
|
78
|
+
}
|
|
79
|
+
function viteHubEnvPublicModulePath(root) {
|
|
80
|
+
return resolve(root, ".vitehub", "env", "public.mjs");
|
|
81
|
+
}
|
|
82
|
+
function viteHubEnvPublicModuleTypesPath(root) {
|
|
83
|
+
return resolve(root, ".vitehub", "env", "public.d.ts");
|
|
84
|
+
}
|
|
85
|
+
function viteHubEnvServerModulePath(root) {
|
|
86
|
+
return resolve(root, ".vitehub", "env", "server.mjs");
|
|
87
|
+
}
|
|
88
|
+
function viteHubEnvDescriptionModulePath(root) {
|
|
89
|
+
return resolve(root, ".vitehub", "env", "description.mjs");
|
|
90
|
+
}
|
|
91
|
+
function viteHubEnvServerModuleTypesPath(root) {
|
|
92
|
+
return resolve(root, ".vitehub", "env", "server.d.ts");
|
|
93
|
+
}
|
|
94
|
+
function createViteHubEnvImportAliases(root) {
|
|
95
|
+
return {
|
|
96
|
+
[VITEHUB_ENV_PUBLIC_ID]: viteHubEnvPublicModulePath(root),
|
|
97
|
+
[VITEHUB_ENV_SERVER_ID]: viteHubEnvServerModulePath(root),
|
|
98
|
+
[VITEHUB_ENV_DESCRIPTION_ID]: viteHubEnvDescriptionModulePath(root)
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region ../internal/src/cli.ts
|
|
103
|
+
const stdoutRedirectQueueKey = Symbol.for("vite-hub.internal.stdout-redirect-queue");
|
|
104
|
+
const stdoutRedirectWriterKey = Symbol.for("vite-hub.internal.stdout-redirect-writer");
|
|
105
|
+
const stdoutRedirectSourceKey = Symbol.for("vite-hub.internal.stdout-redirect-source");
|
|
106
|
+
const stdoutRedirectActiveKey = Symbol.for("vite-hub.internal.stdout-redirect-active");
|
|
107
|
+
const stdoutRedirectGlobal = globalThis;
|
|
108
|
+
/**
|
|
109
|
+
* Sends `process.stdout` writes to stderr while `run` loads project config.
|
|
110
|
+
* Vite, Nitro, Nuxt, and their plugins log info lines to stdout. Those lines must not mix with JSON or raw command output.
|
|
111
|
+
*/
|
|
112
|
+
async function withStdoutOnStderr(run, stderr = process.stderr) {
|
|
113
|
+
getStableStdoutWrite();
|
|
114
|
+
const previous = stdoutRedirectGlobal[stdoutRedirectQueueKey] ?? Promise.resolve();
|
|
115
|
+
let release;
|
|
116
|
+
stdoutRedirectGlobal[stdoutRedirectQueueKey] = new Promise((resolve) => {
|
|
117
|
+
release = resolve;
|
|
118
|
+
});
|
|
119
|
+
await previous;
|
|
120
|
+
const write = process.stdout.write;
|
|
121
|
+
if (stdoutRedirectGlobal[stdoutRedirectWriterKey] === void 0) {
|
|
122
|
+
stdoutRedirectGlobal[stdoutRedirectSourceKey] = write;
|
|
123
|
+
stdoutRedirectGlobal[stdoutRedirectWriterKey] = write.bind(process.stdout);
|
|
124
|
+
}
|
|
125
|
+
stdoutRedirectGlobal[stdoutRedirectActiveKey] = true;
|
|
126
|
+
process.stdout.write = ((chunk, encoding, callback) => {
|
|
127
|
+
const done = typeof encoding === "function" ? encoding : callback;
|
|
128
|
+
let called = false;
|
|
129
|
+
const onComplete = (error) => {
|
|
130
|
+
if (called) return;
|
|
131
|
+
called = true;
|
|
132
|
+
done?.(error);
|
|
133
|
+
};
|
|
134
|
+
const result = typeof encoding === "function" ? stderr.write(chunk, onComplete) : stderr.write(chunk, encoding, onComplete);
|
|
135
|
+
if (done && !called) {
|
|
136
|
+
if (result && typeof result.then === "function") Promise.resolve(result).then(() => onComplete(), (error) => onComplete(error instanceof Error ? error : new Error(String(error))));
|
|
137
|
+
else if (stderr.write.length < 2) onComplete();
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
});
|
|
141
|
+
try {
|
|
142
|
+
return await run();
|
|
143
|
+
} finally {
|
|
144
|
+
process.stdout.write = write;
|
|
145
|
+
stdoutRedirectGlobal[stdoutRedirectActiveKey] = false;
|
|
146
|
+
release();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/** Returns the unredirected process stdout writer for payload output. */
|
|
150
|
+
function getStableStdoutWrite() {
|
|
151
|
+
const current = process.stdout.write;
|
|
152
|
+
if (!stdoutRedirectGlobal[stdoutRedirectActiveKey] && stdoutRedirectGlobal[stdoutRedirectSourceKey] !== current) {
|
|
153
|
+
stdoutRedirectGlobal[stdoutRedirectSourceKey] = current;
|
|
154
|
+
stdoutRedirectGlobal[stdoutRedirectWriterKey] = current.bind(process.stdout);
|
|
155
|
+
}
|
|
156
|
+
return stdoutRedirectGlobal[stdoutRedirectWriterKey] ?? current.bind(process.stdout);
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region ../internal/src/vite-stage.ts
|
|
160
|
+
const viteStageQueueKey = Symbol.for("vitehub.cli.vite-stage-queue");
|
|
161
|
+
function definedEntries(env) {
|
|
162
|
+
return Object.entries(env).filter((entry) => entry[1] !== void 0);
|
|
163
|
+
}
|
|
164
|
+
function replaceProcessEnv(entries) {
|
|
165
|
+
for (const key of Object.keys(process.env)) delete process.env[key];
|
|
166
|
+
for (const [key, value] of entries) process.env[key] = value;
|
|
167
|
+
}
|
|
168
|
+
async function runExclusive(vite, input, callback) {
|
|
169
|
+
let server;
|
|
170
|
+
const previousEnvironment = definedEntries(process.env);
|
|
171
|
+
const selectedEnvironment = definedEntries(input.env);
|
|
172
|
+
try {
|
|
173
|
+
replaceProcessEnv(selectedEnvironment);
|
|
174
|
+
server = await withStdoutOnStderr(async () => await vite.createServer({
|
|
175
|
+
appType: "custom",
|
|
176
|
+
logLevel: "silent",
|
|
177
|
+
mode: input.stage,
|
|
178
|
+
root: input.rootDir,
|
|
179
|
+
server: {
|
|
180
|
+
hmr: false,
|
|
181
|
+
middlewareMode: true
|
|
182
|
+
}
|
|
183
|
+
}), input.stderr);
|
|
184
|
+
for (const [key, value] of [...Object.entries(vite.loadEnv(input.stage, server.config.envDir, "")), ...selectedEnvironment]) process.env[key] = value;
|
|
185
|
+
return await callback(server);
|
|
186
|
+
} finally {
|
|
187
|
+
try {
|
|
188
|
+
await withStdoutOnStderr(async () => await server?.close(), input.stderr);
|
|
189
|
+
} finally {
|
|
190
|
+
replaceProcessEnv(previousEnvironment);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Run a CLI operation against a Vite Development Server in the selected stage mode.
|
|
196
|
+
*
|
|
197
|
+
* During the callback, `process.env` contains the stage-specific Vite env files
|
|
198
|
+
* (`.env`, `.env.local`, `.env.<stage>`, `.env.<stage>.local`) with `input.env`
|
|
199
|
+
* taking precedence. The previous `process.env` is restored afterwards. Calls run
|
|
200
|
+
* one at a time because `process.env` is process-wide.
|
|
201
|
+
*/
|
|
202
|
+
async function withViteStageServer(vite, input, callback) {
|
|
203
|
+
const carrier = globalThis;
|
|
204
|
+
const previous = carrier[viteStageQueueKey] ?? Promise.resolve();
|
|
205
|
+
let release;
|
|
206
|
+
carrier[viteStageQueueKey] = new Promise((resolve) => {
|
|
207
|
+
release = resolve;
|
|
208
|
+
});
|
|
209
|
+
await previous;
|
|
210
|
+
try {
|
|
211
|
+
return await runExclusive(vite, input, callback);
|
|
212
|
+
} finally {
|
|
213
|
+
release();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region src/secret-cli.ts
|
|
218
|
+
const generateUsage = "vitehub secret generate";
|
|
219
|
+
const deriveUsage = "vitehub secret derive <purpose>";
|
|
220
|
+
function help(args) {
|
|
221
|
+
return args.includes("-h") || args.includes("--help");
|
|
222
|
+
}
|
|
223
|
+
function generate(args, context) {
|
|
224
|
+
if (help(args)) {
|
|
225
|
+
context.stdout.write(`Usage: ${generateUsage}\n\nPrint a new random root secret for VITEHUB_SECRET.\n`);
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
if (args.length) {
|
|
229
|
+
context.stderr.write(`Unexpected secret generate argument: ${args[0]}\n`);
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
context.stdout.write(`${randomBytes(32).toString("base64")}\n`);
|
|
233
|
+
return 0;
|
|
234
|
+
}
|
|
235
|
+
async function derive(args, context) {
|
|
236
|
+
if (help(args)) {
|
|
237
|
+
context.stdout.write([
|
|
238
|
+
`Usage: ${deriveUsage}`,
|
|
239
|
+
"",
|
|
240
|
+
"Print the secret that deriveSecret(<purpose>) returns for VITEHUB_SECRET (or NUXT_APP_SECRET).",
|
|
241
|
+
"For example, `vitehub secret derive vitehub:dispatch` prints the VITEHUB_DISPATCH_KEY of a deployment.",
|
|
242
|
+
""
|
|
243
|
+
].join("\n"));
|
|
244
|
+
return 0;
|
|
245
|
+
}
|
|
246
|
+
const [purpose, ...rest] = args;
|
|
247
|
+
if (!purpose || rest.length) {
|
|
248
|
+
context.stderr.write(`Usage: ${deriveUsage}\n`);
|
|
249
|
+
return 1;
|
|
250
|
+
}
|
|
251
|
+
const root = readViteHubSecret(context.env);
|
|
252
|
+
if (!root) {
|
|
253
|
+
context.stderr.write("Set VITEHUB_SECRET to the root secret of the deployment.\n");
|
|
254
|
+
return 1;
|
|
255
|
+
}
|
|
256
|
+
if (root.length < 32) {
|
|
257
|
+
context.stderr.write(`VITEHUB_SECRET must be at least 32 characters.\n`);
|
|
258
|
+
return 1;
|
|
259
|
+
}
|
|
260
|
+
context.stdout.write(`${await deriveViteHubSecret(root, purpose)}\n`);
|
|
261
|
+
return 0;
|
|
262
|
+
}
|
|
263
|
+
/** `vitehub secret` features. They read only the process env and never load the project. */
|
|
264
|
+
const secretCliFeatures = [{
|
|
265
|
+
description: "Print a new random root secret for VITEHUB_SECRET.",
|
|
266
|
+
name: "generate",
|
|
267
|
+
run: async (args, context) => generate(args, context),
|
|
268
|
+
usage: generateUsage
|
|
269
|
+
}, {
|
|
270
|
+
description: "Print the secret derived from VITEHUB_SECRET for one purpose.",
|
|
271
|
+
name: "derive",
|
|
272
|
+
run: async (args, context) => await derive(args, context),
|
|
273
|
+
usage: deriveUsage
|
|
274
|
+
}];
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/cli.ts
|
|
277
|
+
/** CLI discovery resolves Vite config in development mode, so the default stage matches it. */
|
|
278
|
+
const defaultEnvCliStage = "development";
|
|
279
|
+
function usage(command) {
|
|
280
|
+
return `vitehub env ${command} [--stage <name>] [--json]`;
|
|
281
|
+
}
|
|
282
|
+
function writeUsage(command, stream) {
|
|
283
|
+
stream.write([
|
|
284
|
+
`Usage: ${usage(command)}`,
|
|
285
|
+
"",
|
|
286
|
+
command === "check" ? "Checks declared Server Env values. Exits with 1 when loadServerEnv() would fail." : "Lists declared Server Env variables and their status. Values are never printed.",
|
|
287
|
+
"",
|
|
288
|
+
"Options:",
|
|
289
|
+
` --stage <name> Load Vite env files for this mode. Defaults to ${defaultEnvCliStage}.`,
|
|
290
|
+
" Variables from the current process take precedence.",
|
|
291
|
+
" --json Print machine-readable output.",
|
|
292
|
+
" -h, --help Show this help.",
|
|
293
|
+
""
|
|
294
|
+
].join("\n"));
|
|
295
|
+
}
|
|
296
|
+
function parseArgs(args) {
|
|
297
|
+
const parsed = {
|
|
298
|
+
help: false,
|
|
299
|
+
json: false
|
|
300
|
+
};
|
|
301
|
+
for (let index = 0; index < args.length; index++) {
|
|
302
|
+
const arg = args[index];
|
|
303
|
+
if (arg === "-h" || arg === "--help") parsed.help = true;
|
|
304
|
+
else if (arg === "--json") parsed.json = true;
|
|
305
|
+
else if (arg === "--stage" || arg.startsWith("--stage=")) {
|
|
306
|
+
const value = arg === "--stage" ? args[++index] : arg.slice(8);
|
|
307
|
+
if (!value || value.startsWith("-")) throw envErrorDiagnostics.ENV_R0025({ message: "--stage requires a name." });
|
|
308
|
+
parsed.stage = value;
|
|
309
|
+
} else throw envErrorDiagnostics.ENV_R0026({ message: `Unknown option: ${arg}` });
|
|
310
|
+
}
|
|
311
|
+
return parsed;
|
|
312
|
+
}
|
|
313
|
+
function isInspection(value) {
|
|
314
|
+
return v.is(v.object({ entries: v.array(v.object({
|
|
315
|
+
masked: v.boolean(),
|
|
316
|
+
path: v.optional(v.string()),
|
|
317
|
+
provider: v.optional(v.string()),
|
|
318
|
+
required: v.boolean(),
|
|
319
|
+
source: v.picklist([
|
|
320
|
+
"env",
|
|
321
|
+
"literal",
|
|
322
|
+
"provider"
|
|
323
|
+
]),
|
|
324
|
+
status: v.picklist([
|
|
325
|
+
"available",
|
|
326
|
+
"defaulted",
|
|
327
|
+
"error",
|
|
328
|
+
"invalid",
|
|
329
|
+
"missing"
|
|
330
|
+
])
|
|
331
|
+
})) }), value);
|
|
332
|
+
}
|
|
333
|
+
async function inspectStage(input) {
|
|
334
|
+
const explicitEnv = { ...input.env };
|
|
335
|
+
const vite = await import("vite");
|
|
336
|
+
const projectRoot = input.applicationRoot ?? input.rootDir;
|
|
337
|
+
const hasNuxtConfig = [
|
|
338
|
+
"js",
|
|
339
|
+
"mjs",
|
|
340
|
+
"cjs",
|
|
341
|
+
"ts",
|
|
342
|
+
"mts",
|
|
343
|
+
"cts"
|
|
344
|
+
].some((extension) => existsSync(join(projectRoot, `nuxt.config.${extension}`)));
|
|
345
|
+
return await withViteStageServer({
|
|
346
|
+
loadEnv: vite.loadEnv,
|
|
347
|
+
async createServer(config) {
|
|
348
|
+
if (!hasNuxtConfig) return await vite.createServer(config);
|
|
349
|
+
const require = createRequire(join(projectRoot, "package.json"));
|
|
350
|
+
const kit = await import(pathToFileURL(require.resolve("nuxt/kit")).href);
|
|
351
|
+
Object.assign(process.env, vite.loadEnv(input.stage, projectRoot, ""));
|
|
352
|
+
for (const [key, value] of Object.entries(explicitEnv)) if (value !== void 0) process.env[key] = value;
|
|
353
|
+
const nuxt = await kit.loadNuxt({
|
|
354
|
+
cwd: projectRoot,
|
|
355
|
+
dev: true,
|
|
356
|
+
envName: input.stage,
|
|
357
|
+
overrides: {
|
|
358
|
+
devtools: { enabled: false },
|
|
359
|
+
vite: { mode: input.stage },
|
|
360
|
+
vitehubCliDiscovery: true
|
|
361
|
+
},
|
|
362
|
+
ready: true
|
|
363
|
+
});
|
|
364
|
+
try {
|
|
365
|
+
const server = await vite.createServer({
|
|
366
|
+
...nuxt.options.vite,
|
|
367
|
+
...config,
|
|
368
|
+
configFile: false
|
|
369
|
+
});
|
|
370
|
+
const close = server.close.bind(server);
|
|
371
|
+
server.close = async () => {
|
|
372
|
+
try {
|
|
373
|
+
await close();
|
|
374
|
+
} finally {
|
|
375
|
+
await nuxt.close();
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
return server;
|
|
379
|
+
} catch (error) {
|
|
380
|
+
await nuxt.close();
|
|
381
|
+
throw error;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}, {
|
|
385
|
+
...input,
|
|
386
|
+
env: explicitEnv
|
|
387
|
+
}, async (server) => {
|
|
388
|
+
const selectedPlugin = server.config.plugins.find((plugin) => plugin.name === "@vite-hub/env/vite");
|
|
389
|
+
const selectedAPI = v.safeParse(v.object({ resolveProjectRoot: v.function() }), selectedPlugin?.api);
|
|
390
|
+
if (!selectedAPI.success) throw envErrorDiagnostics.ENV_R0027({ message: "[vitehub] The selected stage does not configure the Env plugin." });
|
|
391
|
+
const selectedRoot = selectedAPI.output.resolveProjectRoot(server.config.root);
|
|
392
|
+
if (!v.is(v.string(), selectedRoot)) throw envErrorDiagnostics.ENV_R0027({ message: "[vitehub] The selected Env plugin returned an invalid project root." });
|
|
393
|
+
const modulePath = viteHubEnvServerModulePath(selectedRoot);
|
|
394
|
+
const inspect = (await server.ssrLoadModule(pathToFileURL(modulePath).href)).inspectServerEnv;
|
|
395
|
+
if (!v.is(v.function(), inspect)) throw envErrorDiagnostics.ENV_R0027({ message: `[vitehub] The generated Server Env module does not export inspectServerEnv(): ${modulePath}` });
|
|
396
|
+
const inspection = await inspect();
|
|
397
|
+
if (!isInspection(inspection)) throw envErrorDiagnostics.ENV_R0028({ message: "[vitehub] inspectServerEnv() returned an invalid result." });
|
|
398
|
+
return inspection;
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
function displayPath(entry) {
|
|
402
|
+
return entry.path?.replace(/^env\.server\./, "") ?? "(undisclosed name)";
|
|
403
|
+
}
|
|
404
|
+
function displaySource(entry) {
|
|
405
|
+
if (entry.source === "provider") return `provider:${entry.provider ?? "unknown"}`;
|
|
406
|
+
return entry.source === "env" ? "host env" : "literal";
|
|
407
|
+
}
|
|
408
|
+
function formatTable(rows) {
|
|
409
|
+
const widths = rows[0].map((_, column) => Math.max(...rows.map((row) => row[column].length)));
|
|
410
|
+
return rows.map((row) => row.map((cell, column) => column === row.length - 1 ? cell : cell.padEnd(widths[column])).join(" "));
|
|
411
|
+
}
|
|
412
|
+
function writeReport(command, report, context) {
|
|
413
|
+
const lines = [`Server Env (stage: ${report.stage})`, ""];
|
|
414
|
+
if (!report.entries.length) lines.push("No Server Env variables declared.");
|
|
415
|
+
else lines.push(...formatTable([[
|
|
416
|
+
"VARIABLE",
|
|
417
|
+
"STATUS",
|
|
418
|
+
"SOURCE",
|
|
419
|
+
"REQUIRED",
|
|
420
|
+
"SECRET"
|
|
421
|
+
], ...report.entries.map((entry) => [
|
|
422
|
+
displayPath(entry),
|
|
423
|
+
isBlockingServerEnvEntry(entry) ? `${entry.status} !` : entry.status,
|
|
424
|
+
displaySource(entry),
|
|
425
|
+
entry.required ? "yes" : "no",
|
|
426
|
+
entry.masked ? "yes" : "no"
|
|
427
|
+
])]));
|
|
428
|
+
const blocking = report.entries.filter(isBlockingServerEnvEntry).length;
|
|
429
|
+
lines.push("", blocking ? `${blocking} of ${report.entries.length} variables would make loadServerEnv() fail.` : `${report.entries.length} variables checked. Values are not shown.`);
|
|
430
|
+
if (command === "check") lines.push(report.ok ? "Server Env check passed." : "Server Env check failed.");
|
|
431
|
+
context.stdout.write(`${lines.join("\n")}\n`);
|
|
432
|
+
}
|
|
433
|
+
async function runEnvCli(command, args, context, options) {
|
|
434
|
+
let parsed;
|
|
435
|
+
try {
|
|
436
|
+
parsed = parseArgs(args);
|
|
437
|
+
} catch (error) {
|
|
438
|
+
context.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
439
|
+
writeUsage(command, context.stderr);
|
|
440
|
+
return 1;
|
|
441
|
+
}
|
|
442
|
+
if (parsed.help) {
|
|
443
|
+
writeUsage(command, context.stdout);
|
|
444
|
+
return 0;
|
|
445
|
+
}
|
|
446
|
+
const input = {
|
|
447
|
+
env: context.env,
|
|
448
|
+
rootDir: context.rootDir,
|
|
449
|
+
stderr: context.stderr,
|
|
450
|
+
stage: parsed.stage ?? defaultEnvCliStage
|
|
451
|
+
};
|
|
452
|
+
const inspection = options.inspect ? await options.inspect(input) : await inspectStage({
|
|
453
|
+
...input,
|
|
454
|
+
applicationRoot: context.cwd
|
|
455
|
+
});
|
|
456
|
+
const report = {
|
|
457
|
+
entries: inspection.entries,
|
|
458
|
+
ok: !inspection.entries.some(isBlockingServerEnvEntry),
|
|
459
|
+
stage: input.stage
|
|
460
|
+
};
|
|
461
|
+
if (parsed.json) context.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
462
|
+
else writeReport(command, report, context);
|
|
463
|
+
return command === "check" && !report.ok ? 1 : 0;
|
|
464
|
+
}
|
|
465
|
+
function createEnvCliContributor(options) {
|
|
466
|
+
return { namespaces: [{
|
|
467
|
+
description: "Server Env inspection workflows.",
|
|
468
|
+
features: [{
|
|
469
|
+
description: "List declared Server Env variables and their status without values.",
|
|
470
|
+
name: "inspect",
|
|
471
|
+
run: async (args, context) => await runEnvCli("inspect", args, context, options),
|
|
472
|
+
usage: usage("inspect")
|
|
473
|
+
}, {
|
|
474
|
+
description: "Exit with 1 when a required Server Env value is missing or any value is invalid.",
|
|
475
|
+
name: "check",
|
|
476
|
+
run: async (args, context) => await runEnvCli("check", args, context, options),
|
|
477
|
+
usage: usage("check")
|
|
478
|
+
}],
|
|
479
|
+
name: "env"
|
|
480
|
+
}, {
|
|
481
|
+
description: "Generate the root app secret and print derived secrets.",
|
|
482
|
+
features: secretCliFeatures,
|
|
483
|
+
name: "secret"
|
|
484
|
+
}] };
|
|
485
|
+
}
|
|
486
|
+
//#endregion
|
|
487
|
+
export { createViteHubEnvImportAliases as a, viteHubEnvDescriptionModulePath as c, viteHubEnvServerModulePath as d, viteHubEnvServerModuleTypesPath as f, VITEHUB_ENV_SERVER_ID as i, viteHubEnvPublicModulePath as l, secretCliFeatures as n, resolveViteHubProjectRoot as o, VITEHUB_ENV_PUBLIC_ID as r, viteHubEnvAmbientTypesPath as s, createEnvCliContributor as t, viteHubEnvPublicModuleTypesPath as u };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Diagnostic } from "nostics";
|
|
2
|
+
//#region ../internal/src/provision.d.ts
|
|
3
|
+
type ProvisionProvider = "cloudflare" | "vercel";
|
|
4
|
+
interface ProvisionLogger {
|
|
5
|
+
log: (message: string) => void;
|
|
6
|
+
warn: (message: string) => void;
|
|
7
|
+
}
|
|
8
|
+
interface ProvisionContext {
|
|
9
|
+
env: Record<string, string | undefined>;
|
|
10
|
+
fetch: typeof globalThis.fetch;
|
|
11
|
+
logger: ProvisionLogger;
|
|
12
|
+
markPlanUnchecked?: () => void;
|
|
13
|
+
}
|
|
14
|
+
interface ProvisionState {
|
|
15
|
+
cloudflare?: Record<string, Record<string, string>>;
|
|
16
|
+
vercel?: Record<string, Record<string, string>>;
|
|
17
|
+
}
|
|
18
|
+
interface ProvisionResult {
|
|
19
|
+
ids?: ProvisionState;
|
|
20
|
+
}
|
|
21
|
+
interface ProvisionAction {
|
|
22
|
+
kind: string;
|
|
23
|
+
name: string;
|
|
24
|
+
exists: boolean;
|
|
25
|
+
pending?: boolean;
|
|
26
|
+
apply: () => Promise<ProvisionResult>;
|
|
27
|
+
}
|
|
28
|
+
interface ProvisionStep {
|
|
29
|
+
id: string;
|
|
30
|
+
provider: ProvisionProvider;
|
|
31
|
+
plan: (context: ProvisionContext) => Promise<ProvisionAction[]>;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region ../internal/src/cli.d.ts
|
|
35
|
+
interface ViteHubCliStreams {
|
|
36
|
+
stderr: {
|
|
37
|
+
write: (chunk: string | Uint8Array, encoding?: BufferEncoding | StdoutWriteCallback, callback?: StdoutWriteCallback) => unknown;
|
|
38
|
+
};
|
|
39
|
+
stdout: {
|
|
40
|
+
write: (chunk: string | Uint8Array) => unknown;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
interface ViteHubCliSpawnResult {
|
|
44
|
+
exitCode: number | null;
|
|
45
|
+
signal?: NodeJS.Signals | null;
|
|
46
|
+
}
|
|
47
|
+
interface ViteHubCliSpawnOptions {
|
|
48
|
+
cwd?: string;
|
|
49
|
+
env?: NodeJS.ProcessEnv;
|
|
50
|
+
stderr?: "inherit" | "pipe";
|
|
51
|
+
stdout?: "inherit" | "pipe";
|
|
52
|
+
}
|
|
53
|
+
type ViteHubCliSpawn = (command: string, args: string[], options?: ViteHubCliSpawnOptions) => Promise<ViteHubCliSpawnResult>;
|
|
54
|
+
interface ViteHubCliContext extends ViteHubCliStreams {
|
|
55
|
+
cwd: string;
|
|
56
|
+
env: NodeJS.ProcessEnv;
|
|
57
|
+
/** Replaces `fetch` for commands that call a running app, so tests need no server. */
|
|
58
|
+
fetch?: typeof fetch;
|
|
59
|
+
rootDir: string;
|
|
60
|
+
spawn: ViteHubCliSpawn;
|
|
61
|
+
}
|
|
62
|
+
interface ViteHubCliFeature {
|
|
63
|
+
description?: string;
|
|
64
|
+
name: string;
|
|
65
|
+
run: (args: string[], context: ViteHubCliContext) => Promise<number | void> | number | void;
|
|
66
|
+
usage?: string;
|
|
67
|
+
}
|
|
68
|
+
interface ViteHubCliCommandNamespace {
|
|
69
|
+
/** The feature that `vitehub <name>` runs when no feature is named. */
|
|
70
|
+
defaultFeature?: string;
|
|
71
|
+
description?: string;
|
|
72
|
+
features: ViteHubCliFeature[];
|
|
73
|
+
name: string;
|
|
74
|
+
}
|
|
75
|
+
interface ViteHubCliContributor {
|
|
76
|
+
namespaces: ViteHubCliCommandNamespace[];
|
|
77
|
+
provision?: ProvisionStep[];
|
|
78
|
+
}
|
|
79
|
+
type StdoutWriteCallback = (error?: Error | null) => void;
|
|
80
|
+
//#endregion
|
|
81
|
+
export { ViteHubCliContributor as n, ViteHubCliContext as t };
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { B as ServerEnvInspection, V as ServerEnvInspectionEntry } from "./bridge-CdNdTN1V.js";
|
|
2
|
+
import { n as ViteHubCliContributor, t as ViteHubCliContext } from "./cli-DEwcUIe0.js";
|
|
3
|
+
//#region src/secret-cli.d.ts
|
|
4
|
+
interface SecretCliContext {
|
|
5
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
6
|
+
stderr: {
|
|
7
|
+
write: (chunk: string) => unknown;
|
|
8
|
+
};
|
|
9
|
+
stdout: {
|
|
10
|
+
write: (chunk: string) => unknown;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
interface SecretCliFeature {
|
|
14
|
+
description: string;
|
|
15
|
+
name: string;
|
|
16
|
+
run: (args: string[], context: SecretCliContext) => Promise<number>;
|
|
17
|
+
usage: string;
|
|
18
|
+
}
|
|
19
|
+
/** `vitehub secret` features. They read only the process env and never load the project. */
|
|
20
|
+
export declare const secretCliFeatures: SecretCliFeature[];
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/cli.d.ts
|
|
23
|
+
export interface EnvCliInspectInput {
|
|
24
|
+
applicationRoot?: string;
|
|
25
|
+
env: NodeJS.ProcessEnv;
|
|
26
|
+
rootDir: string;
|
|
27
|
+
stderr: ViteHubCliContext["stderr"];
|
|
28
|
+
stage: string;
|
|
29
|
+
}
|
|
30
|
+
export interface EnvCliOptions {
|
|
31
|
+
/** Replaces stage loading in tests. */
|
|
32
|
+
inspect?: (input: EnvCliInspectInput) => Promise<ServerEnvInspection>;
|
|
33
|
+
resolveProjectRoot: (viteRoot: string) => string;
|
|
34
|
+
}
|
|
35
|
+
export interface EnvCliReport {
|
|
36
|
+
entries: readonly ServerEnvInspectionEntry[];
|
|
37
|
+
ok: boolean;
|
|
38
|
+
stage: string;
|
|
39
|
+
}
|
|
40
|
+
export declare function createEnvCliContributor(options: EnvCliOptions): ViteHubCliContributor;
|
|
41
|
+
//#endregion
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { f as EnvSecretStore, r as EnvAccessStore } from "./bridge-CdNdTN1V.js";
|
|
2
|
+
import { SQL } from "drizzle-orm";
|
|
3
|
+
//#region src/database.d.ts
|
|
4
|
+
export interface EnvDatabase {
|
|
5
|
+
run(query: SQL): unknown;
|
|
6
|
+
all(query: SQL): unknown[] | PromiseLike<unknown[]>;
|
|
7
|
+
}
|
|
8
|
+
export interface DatabaseEnvStore {
|
|
9
|
+
/** SQL predicate for atomically updating related records at a secret revision. */
|
|
10
|
+
revisionCondition(key: string, revision: string | null): Promise<SQL>;
|
|
11
|
+
secrets: EnvSecretStore;
|
|
12
|
+
access: EnvAccessStore;
|
|
13
|
+
}
|
|
14
|
+
/** Persistent encrypted values, activity, and grants on a ViteHub SQLite/Drizzle database. */
|
|
15
|
+
export declare function createDatabaseEnvStore(options: {
|
|
16
|
+
db: EnvDatabase;
|
|
17
|
+
encryptionKey: Uint8Array;
|
|
18
|
+
namespace?: string;
|
|
19
|
+
previews?: boolean;
|
|
20
|
+
}): DatabaseEnvStore;
|
|
21
|
+
//#endregion
|