@vite-hub/env 0.1.0 → 0.3.1
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/README.md +178 -33
- package/dist/{access-B3qzBHye.js → access-B8lSDip4.js} +1 -1
- package/dist/{bridge-CdNdTN1V.d.ts → bridge-Cs9YfQWk.d.ts} +111 -38
- package/dist/{bridge-error-CFuU5zIz.js → bridge-error-BhJ77age.js} +14 -13
- package/dist/bridge.d.ts +2 -2
- package/dist/bridge.js +168 -6
- package/dist/cli-BPp7rdcA.d.ts +159 -0
- package/dist/{cli-CMQPQbii.js → cli-DLpY0QAo.js} +212 -30
- package/dist/cli.d.ts +2 -2
- package/dist/cli.js +1 -1
- package/dist/database.d.ts +29 -3
- package/dist/database.js +106 -15
- package/dist/declarations-C5XR3aKw.d.ts +58 -0
- package/dist/error-diagnostics-B8RFjQ1V.js +56 -0
- package/dist/index.d.ts +145 -10
- package/dist/index.js +240 -7
- package/dist/internal/agent.d.ts +1 -1
- package/dist/internal/agent.js +2 -2
- package/dist/internal/connections.d.ts +1 -1
- package/dist/internal/connections.js +2 -2
- package/dist/internal/server.d.ts +3 -0
- package/dist/internal/server.js +2 -0
- package/dist/object-_dlsZ4lP.js +276 -0
- package/dist/operation-definition-8gOe-ubD.d.ts +234 -0
- package/dist/operations-X1w--wds.js +30 -0
- package/dist/operations.d.ts +37 -0
- package/dist/operations.js +833 -0
- package/dist/runtime/public-env.d.ts +5 -0
- package/dist/runtime/public-env.js +8 -0
- package/dist/runtime/server-env.d.ts +22 -0
- package/dist/runtime/server-env.js +35 -0
- package/dist/schema-DMZ1_yhY.js +360 -0
- package/dist/seal.d.ts +8 -5
- package/dist/seal.js +9 -6
- package/dist/{secret.js → secret-DJd-fy5T.js} +1 -1
- package/dist/server-ChNL_Xc3.js +1279 -0
- package/dist/server-Dd8bjIKd.d.ts +145 -0
- package/dist/server.d.ts +3 -25
- package/dist/server.js +2 -2
- package/dist/virtual-module.d.ts +6 -6
- package/dist/vite.d.ts +59 -14
- package/dist/vite.js +304 -115
- package/package.json +27 -38
- package/dist/auth.d.ts +0 -32
- package/dist/auth.js +0 -44
- package/dist/cli-DEwcUIe0.d.ts +0 -81
- package/dist/declarations-CiRdb1qP.d.ts +0 -32
- package/dist/declarations-DAVgctKD.js +0 -236
- package/dist/error-diagnostics-CrxjYNYr.js +0 -43
- package/dist/http.d.ts +0 -2
- package/dist/http.js +0 -122
- package/dist/presets.d.ts +0 -20
- package/dist/presets.js +0 -44
- package/dist/provider.d.ts +0 -5
- package/dist/provider.js +0 -8
- package/dist/schema.d.ts +0 -15
- package/dist/schema.js +0 -55
- package/dist/secret-C2191nAa.d.ts +0 -11
- package/dist/secret.d.ts +0 -2
- package/dist/server-DufVswRJ.js +0 -670
- package/dist/values-D_r6V774.js +0 -148
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Diagnostic } from "nostics";
|
|
2
|
+
//#region ../internal/src/host-credentials.d.ts
|
|
3
|
+
interface HostCliRunResult {
|
|
4
|
+
exitCode: number | null;
|
|
5
|
+
stdout: string;
|
|
6
|
+
}
|
|
7
|
+
/** Runs a host CLI with an argument array and no shell, and returns its stdout. */
|
|
8
|
+
type HostCliRun = (command: string, args: readonly string[], options: {
|
|
9
|
+
cwd?: string;
|
|
10
|
+
env: NodeJS.ProcessEnv;
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
}) => Promise<HostCliRunResult>;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region ../internal/src/provision.d.ts
|
|
15
|
+
type ProvisionProvider = "cloudflare" | "vercel";
|
|
16
|
+
interface ProvisionLogger {
|
|
17
|
+
log: (message: string) => void;
|
|
18
|
+
warn: (message: string) => void;
|
|
19
|
+
}
|
|
20
|
+
interface ProvisionContext {
|
|
21
|
+
env: Record<string, string | undefined>;
|
|
22
|
+
fetch: typeof globalThis.fetch;
|
|
23
|
+
logger: ProvisionLogger;
|
|
24
|
+
markPlanUnchecked?: () => void;
|
|
25
|
+
}
|
|
26
|
+
interface ProvisionState {
|
|
27
|
+
cloudflare?: Record<string, Record<string, string>>;
|
|
28
|
+
vercel?: Record<string, Record<string, string>>;
|
|
29
|
+
}
|
|
30
|
+
interface ProvisionResult {
|
|
31
|
+
ids?: ProvisionState;
|
|
32
|
+
}
|
|
33
|
+
interface ProvisionAction {
|
|
34
|
+
kind: string;
|
|
35
|
+
name: string;
|
|
36
|
+
exists: boolean;
|
|
37
|
+
pending?: boolean;
|
|
38
|
+
/** Describes the change that `apply()` makes to an existing resource, for example `read replication: disabled -> auto`. */
|
|
39
|
+
update?: string;
|
|
40
|
+
apply: () => Promise<ProvisionResult>;
|
|
41
|
+
}
|
|
42
|
+
/** One Provision State category that a step records, with the keys that current Definitions use. */
|
|
43
|
+
interface ProvisionStateCategory {
|
|
44
|
+
category: string;
|
|
45
|
+
declared: readonly string[];
|
|
46
|
+
/** Provider resource identity for shared-resource checks. Return undefined when the recorded id lacks enough information. Defaults to the recorded id. */
|
|
47
|
+
resourceIdentity?: (entry: {
|
|
48
|
+
id: string;
|
|
49
|
+
key: string;
|
|
50
|
+
}) => string | undefined;
|
|
51
|
+
/** Identity expected by current config for a declared key. Omit when it cannot be established without provider access. */
|
|
52
|
+
declaredResourceIdentity?: (key: string) => string | undefined;
|
|
53
|
+
/** Tells a developer how to delete the resource of a recorded entry that no Definition declares. ViteHub never runs it. */
|
|
54
|
+
deleteHint: (entry: {
|
|
55
|
+
id: string;
|
|
56
|
+
key: string;
|
|
57
|
+
}) => string;
|
|
58
|
+
}
|
|
59
|
+
interface ProvisionStep {
|
|
60
|
+
id: string;
|
|
61
|
+
provider: ProvisionProvider;
|
|
62
|
+
plan: (context: ProvisionContext) => Promise<ProvisionAction[]>;
|
|
63
|
+
/** Describes the Provision State this step records. Runs without network access, also without credentials. */
|
|
64
|
+
recordedState?: (context: Pick<ProvisionContext, "env">) => ProvisionStateCategory[];
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region ../internal/src/cli.d.ts
|
|
68
|
+
interface ViteHubCliStreams {
|
|
69
|
+
stderr: {
|
|
70
|
+
write: (chunk: string | Uint8Array, encoding?: BufferEncoding | StdoutWriteCallback, callback?: StdoutWriteCallback) => unknown;
|
|
71
|
+
};
|
|
72
|
+
stdout: {
|
|
73
|
+
write: (chunk: string | Uint8Array) => unknown;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
interface ViteHubCliSpawnResult {
|
|
77
|
+
exitCode: number | null;
|
|
78
|
+
signal?: NodeJS.Signals | null;
|
|
79
|
+
/** The output of the child when `stdout` is `pipe`. */
|
|
80
|
+
stdout?: string;
|
|
81
|
+
}
|
|
82
|
+
interface ViteHubCliSpawnOptions {
|
|
83
|
+
cwd?: string;
|
|
84
|
+
env?: NodeJS.ProcessEnv;
|
|
85
|
+
stderr?: "inherit" | "pipe";
|
|
86
|
+
/** `ignore` gives the child no terminal input, so a tool that asks questions on a terminal does not ask. */
|
|
87
|
+
stdin?: "ignore" | "inherit";
|
|
88
|
+
/** `stderr` sends the child's stdout to the parent's stderr, so the parent's stdout keeps only its own output. */
|
|
89
|
+
stdout?: "inherit" | "pipe" | "stderr";
|
|
90
|
+
}
|
|
91
|
+
type ViteHubCliSpawn = (command: string, args: string[], options?: ViteHubCliSpawnOptions) => Promise<ViteHubCliSpawnResult>;
|
|
92
|
+
interface ViteHubCliContext extends ViteHubCliStreams {
|
|
93
|
+
cwd: string;
|
|
94
|
+
env: NodeJS.ProcessEnv;
|
|
95
|
+
/** Replaces `fetch` for commands that call a running app, so tests need no server. */
|
|
96
|
+
fetch?: typeof fetch;
|
|
97
|
+
/**
|
|
98
|
+
* Loads the project config for a build on `host` and returns the Provision Steps of its plugins, with the config root
|
|
99
|
+
* that holds `.vitehub/provision.json`. Setup commands, such as `vitehub deploy`, call it only when they need the
|
|
100
|
+
* steps, so they still run on a config that throws.
|
|
101
|
+
*/
|
|
102
|
+
loadProvision?: (host: ProvisionProvider) => Promise<ViteHubCliProvision>;
|
|
103
|
+
/** The deployed app's public origin from `vitehub({ publicUrl })`, when it is one string. */
|
|
104
|
+
publicUrl?: string;
|
|
105
|
+
rootDir: string;
|
|
106
|
+
/** Replaces the host CLI runner, such as for `wrangler auth token`, so tests need no real host CLI. */
|
|
107
|
+
runHostCli?: HostCliRun;
|
|
108
|
+
spawn: ViteHubCliSpawn;
|
|
109
|
+
}
|
|
110
|
+
interface ViteHubCliProvision {
|
|
111
|
+
rootDir: string;
|
|
112
|
+
steps: readonly ProvisionStep[];
|
|
113
|
+
}
|
|
114
|
+
interface ViteHubCliFeature {
|
|
115
|
+
description?: string;
|
|
116
|
+
name: string;
|
|
117
|
+
run: (args: string[], context: ViteHubCliContext) => Promise<number | void> | number | void;
|
|
118
|
+
usage?: string;
|
|
119
|
+
}
|
|
120
|
+
interface ViteHubCliCommandNamespace {
|
|
121
|
+
/** The feature that `vitehub <name>` runs when the next argument names no other feature. It gets every argument after `<name>`. */
|
|
122
|
+
defaultFeature?: string;
|
|
123
|
+
description?: string;
|
|
124
|
+
features: ViteHubCliFeature[];
|
|
125
|
+
name: string;
|
|
126
|
+
}
|
|
127
|
+
/** The flags of `vitehub seed` that every seed gets. */
|
|
128
|
+
interface ViteHubCliSeedOptions {
|
|
129
|
+
/** `--database <name>`: seed only this Database. Only the Database seed runs. */
|
|
130
|
+
database?: string;
|
|
131
|
+
/** The process environment with the env files of `--stage`. */
|
|
132
|
+
env: NodeJS.ProcessEnv;
|
|
133
|
+
/** `--remote`: write the deployed stores. Without it, a seed writes only local data. */
|
|
134
|
+
remote: boolean;
|
|
135
|
+
/** `--timeout <ms>` of each call to the app. */
|
|
136
|
+
timeout?: number;
|
|
137
|
+
/** The deployed app with `--remote`: `--url`, or `vitehub({ publicUrl })`. */
|
|
138
|
+
url?: string;
|
|
139
|
+
}
|
|
140
|
+
/** The seed files of one primitive, which `vitehub seed` writes. */
|
|
141
|
+
interface ViteHubCliSeed {
|
|
142
|
+
/** Where the seed files are, such as `server/seed/kv/`. `vitehub seed` names it when it finds no seed files. */
|
|
143
|
+
files: string;
|
|
144
|
+
/** The primitive, such as `database` or `kv`. `vitehub seed <name>` runs only this seed. */
|
|
145
|
+
name: string;
|
|
146
|
+
/** Writes the seed files and returns the exit code, or `undefined` when the project has no seed files for this primitive. */
|
|
147
|
+
run: (options: ViteHubCliSeedOptions, context: ViteHubCliContext) => Promise<number | undefined>;
|
|
148
|
+
}
|
|
149
|
+
interface ViteHubCliContributor {
|
|
150
|
+
namespaces: ViteHubCliCommandNamespace[];
|
|
151
|
+
provision?: ProvisionStep[];
|
|
152
|
+
/** The seed files that `vitehub seed` writes. */
|
|
153
|
+
seeds?: ViteHubCliSeed[];
|
|
154
|
+
/** The deployed app's public origin. Commands receive it as `context.publicUrl`. */
|
|
155
|
+
publicUrl?: string;
|
|
156
|
+
}
|
|
157
|
+
type StdoutWriteCallback = (error?: Error | null) => void;
|
|
158
|
+
//#endregion
|
|
159
|
+
export { ViteHubCliContributor as n, ViteHubCliContext as t };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as envErrorDiagnostics } from "./error-diagnostics-
|
|
2
|
-
import {
|
|
1
|
+
import { t as envErrorDiagnostics } from "./error-diagnostics-B8RFjQ1V.js";
|
|
2
|
+
import { C as deriveViteHubSecret, E as readViteHubSecret, a as isBlockingServerEnvEntry, f as invariant } from "./server-ChNL_Xc3.js";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import * as v from "valibot";
|
|
5
5
|
import { existsSync, statSync } from "node:fs";
|
|
@@ -27,6 +27,22 @@ const projectRootFileMarkers = [
|
|
|
27
27
|
const VITEHUB_ENV_PUBLIC_ID = "#vitehub/env/public";
|
|
28
28
|
const VITEHUB_ENV_SERVER_ID = "#vitehub/env/server";
|
|
29
29
|
const VITEHUB_ENV_DESCRIPTION_ID = "#vitehub/env/description";
|
|
30
|
+
function createNoExternalAddition(...packageNames) {
|
|
31
|
+
return (current) => {
|
|
32
|
+
if (current === true) return;
|
|
33
|
+
const values = Array.isArray(current) ? current : [current];
|
|
34
|
+
const missing = packageNames.filter((packageName) => !values.includes(packageName));
|
|
35
|
+
return missing.length ? missing : void 0;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function isServerEnvironment(name, config) {
|
|
39
|
+
return name === "ssr" || config.consumer === "server";
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Server Env declarations that ViteHub features add before `hubEnv()` builds the registry, such as the Server Env of
|
|
43
|
+
* discovered Channels. The application's `env.server` wins field by field.
|
|
44
|
+
*/
|
|
45
|
+
const VITEHUB_ENV_SERVER = "__vitehubEnvServer";
|
|
30
46
|
function resolveViteHubProjectRoot(root, options = {}) {
|
|
31
47
|
const resolvedRoot = resolve(root);
|
|
32
48
|
const temporaryRoot = resolve(tmpdir());
|
|
@@ -146,6 +162,26 @@ async function withStdoutOnStderr(run, stderr = process.stderr) {
|
|
|
146
162
|
release();
|
|
147
163
|
}
|
|
148
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* A Vite plugin for CLI discovery that collects the Nitro instances that Vite's config hooks create, and `close()` to stop them.
|
|
167
|
+
* Nitro's Vite plugin starts its dev env runner (a worker thread, or Miniflare for Cloudflare) while Vite resolves a `serve`
|
|
168
|
+
* config. Discovery never uses that runner. If it stays open, the process does not exit by itself, and when the CLI
|
|
169
|
+
* then calls `process.exit()`, a StackBlitz WebContainer stops running later processes such as `vite`.
|
|
170
|
+
*/
|
|
171
|
+
function createCliDiscoveryNitroCloser() {
|
|
172
|
+
const instances = [];
|
|
173
|
+
return {
|
|
174
|
+
close: async () => {
|
|
175
|
+
await Promise.all(instances.splice(0).map((nitro) => nitro.close()));
|
|
176
|
+
},
|
|
177
|
+
plugin: {
|
|
178
|
+
name: "vite-hub/cli-discovery-nitro",
|
|
179
|
+
nitro: { setup: (nitro) => {
|
|
180
|
+
instances.push(nitro);
|
|
181
|
+
} }
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
149
185
|
/** Returns the unredirected process stdout writer for payload output. */
|
|
150
186
|
function getStableStdoutWrite() {
|
|
151
187
|
const current = process.stdout.write;
|
|
@@ -157,6 +193,19 @@ function getStableStdoutWrite() {
|
|
|
157
193
|
}
|
|
158
194
|
//#endregion
|
|
159
195
|
//#region ../internal/src/vite-stage.ts
|
|
196
|
+
/**
|
|
197
|
+
* Vite's `loadEnv(mode, envDir, "")`: every `.env` value of the mode and the whole shell. With `DEBUG=vite:env`, Vite
|
|
198
|
+
* logs that result to stderr, secrets included. loadEnv is synchronous, so this drops only the writes of this call.
|
|
199
|
+
*/
|
|
200
|
+
function loadAllEnv(loadEnv, mode, envDir) {
|
|
201
|
+
const write = process.stderr.write;
|
|
202
|
+
process.stderr.write = () => true;
|
|
203
|
+
try {
|
|
204
|
+
return loadEnv(mode, envDir, "");
|
|
205
|
+
} finally {
|
|
206
|
+
process.stderr.write = write;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
160
209
|
const viteStageQueueKey = Symbol.for("vitehub.cli.vite-stage-queue");
|
|
161
210
|
function definedEntries(env) {
|
|
162
211
|
return Object.entries(env).filter((entry) => entry[1] !== void 0);
|
|
@@ -165,14 +214,13 @@ function replaceProcessEnv(entries) {
|
|
|
165
214
|
for (const key of Object.keys(process.env)) delete process.env[key];
|
|
166
215
|
for (const [key, value] of entries) process.env[key] = value;
|
|
167
216
|
}
|
|
168
|
-
async function
|
|
217
|
+
async function runStageServer(vite, input, callback) {
|
|
169
218
|
let server;
|
|
170
|
-
const previousEnvironment = definedEntries(process.env);
|
|
171
219
|
const selectedEnvironment = definedEntries(input.env);
|
|
172
220
|
try {
|
|
173
|
-
replaceProcessEnv(selectedEnvironment);
|
|
174
221
|
server = await withStdoutOnStderr(async () => await vite.createServer({
|
|
175
222
|
appType: "custom",
|
|
223
|
+
...input.envDir !== void 0 ? { envDir: input.envDir } : {},
|
|
176
224
|
logLevel: "silent",
|
|
177
225
|
mode: input.stage,
|
|
178
226
|
root: input.rootDir,
|
|
@@ -181,14 +229,10 @@ async function runExclusive(vite, input, callback) {
|
|
|
181
229
|
middlewareMode: true
|
|
182
230
|
}
|
|
183
231
|
}), input.stderr);
|
|
184
|
-
for (const [key, value] of [...Object.entries(vite.loadEnv
|
|
232
|
+
for (const [key, value] of [...Object.entries(loadAllEnv(vite.loadEnv, input.stage, server.config.envDir)), ...selectedEnvironment]) process.env[key] = value;
|
|
185
233
|
return await callback(server);
|
|
186
234
|
} finally {
|
|
187
|
-
|
|
188
|
-
await withStdoutOnStderr(async () => await server?.close(), input.stderr);
|
|
189
|
-
} finally {
|
|
190
|
-
replaceProcessEnv(previousEnvironment);
|
|
191
|
-
}
|
|
235
|
+
await withStdoutOnStderr(async () => await server?.close(), input.stderr);
|
|
192
236
|
}
|
|
193
237
|
}
|
|
194
238
|
/**
|
|
@@ -200,6 +244,10 @@ async function runExclusive(vite, input, callback) {
|
|
|
200
244
|
* one at a time because `process.env` is process-wide.
|
|
201
245
|
*/
|
|
202
246
|
async function withViteStageServer(vite, input, callback) {
|
|
247
|
+
return await withViteStageEnvironment(input.env, async () => await runStageServer(vite, input, callback));
|
|
248
|
+
}
|
|
249
|
+
/** Load stage config and modules under the same serialized, temporary process environment. */
|
|
250
|
+
async function withViteStageEnvironment(env, callback) {
|
|
203
251
|
const carrier = globalThis;
|
|
204
252
|
const previous = carrier[viteStageQueueKey] ?? Promise.resolve();
|
|
205
253
|
let release;
|
|
@@ -207,16 +255,51 @@ async function withViteStageServer(vite, input, callback) {
|
|
|
207
255
|
release = resolve;
|
|
208
256
|
});
|
|
209
257
|
await previous;
|
|
258
|
+
const previousEnvironment = definedEntries(process.env);
|
|
259
|
+
const selectedEnvironment = definedEntries(env);
|
|
210
260
|
try {
|
|
211
|
-
|
|
261
|
+
replaceProcessEnv(selectedEnvironment);
|
|
262
|
+
return await callback();
|
|
212
263
|
} finally {
|
|
264
|
+
replaceProcessEnv(previousEnvironment);
|
|
213
265
|
release();
|
|
214
266
|
}
|
|
215
267
|
}
|
|
216
268
|
//#endregion
|
|
269
|
+
//#region ../internal/src/console-signature.ts
|
|
270
|
+
/** The app reads the public key of the caller `<NAME>` from `VITEHUB_CONSOLE_KEY_<NAME>`. */
|
|
271
|
+
const consolePublicKeyPrefix = "VITEHUB_CONSOLE_KEY_";
|
|
272
|
+
/** The CLI reads its private key from `VITEHUB_CONSOLE_PRIVATE_KEY_<NAME>`. `<NAME>` is the key name that it signs with. */
|
|
273
|
+
const consolePrivateKeyPrefix = "VITEHUB_CONSOLE_PRIVATE_KEY_";
|
|
274
|
+
const keyNamePattern = /^[A-Z0-9_]{1,64}$/;
|
|
275
|
+
new TextEncoder();
|
|
276
|
+
new TextDecoder();
|
|
277
|
+
const ed25519 = { name: "Ed25519" };
|
|
278
|
+
function base64url(bytes) {
|
|
279
|
+
let binary = "";
|
|
280
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
281
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
282
|
+
}
|
|
283
|
+
/** Whether a name can be the `<NAME>` of `VITEHUB_CONSOLE_KEY_<NAME>`: up to 64 of `A-Z`, `0-9`, and `_`. */
|
|
284
|
+
function isConsoleKeyName(name) {
|
|
285
|
+
return keyNamePattern.test(name);
|
|
286
|
+
}
|
|
287
|
+
/** A new key pair: the public key for the app, the private key (the seed) for the caller. Both are base64url. */
|
|
288
|
+
async function createConsoleKeyPair() {
|
|
289
|
+
const pair = await crypto.subtle.generateKey(ed25519, true, ["sign", "verify"]);
|
|
290
|
+
invariant("privateKey" in pair, "Ed25519 generateKey() returns a key pair");
|
|
291
|
+
const pkcs8 = new Uint8Array(await crypto.subtle.exportKey("pkcs8", pair.privateKey));
|
|
292
|
+
const publicKey = new Uint8Array(await crypto.subtle.exportKey("raw", pair.publicKey));
|
|
293
|
+
return {
|
|
294
|
+
privateKey: base64url(pkcs8.slice(-32)),
|
|
295
|
+
publicKey: base64url(publicKey)
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
//#endregion
|
|
217
299
|
//#region src/secret-cli.ts
|
|
218
300
|
const generateUsage = "vitehub secret generate";
|
|
219
301
|
const deriveUsage = "vitehub secret derive <purpose>";
|
|
302
|
+
const consoleKeyUsage = "vitehub secret console-key [name]";
|
|
220
303
|
function help(args) {
|
|
221
304
|
return args.includes("-h") || args.includes("--help");
|
|
222
305
|
}
|
|
@@ -237,8 +320,8 @@ async function derive(args, context) {
|
|
|
237
320
|
context.stdout.write([
|
|
238
321
|
`Usage: ${deriveUsage}`,
|
|
239
322
|
"",
|
|
240
|
-
"Print the secret that deriveSecret(<purpose>) returns for VITEHUB_SECRET
|
|
241
|
-
"For example, `vitehub secret derive
|
|
323
|
+
"Print the secret that deriveSecret(<purpose>) returns for VITEHUB_SECRET.",
|
|
324
|
+
"For example, `vitehub secret derive myapp:webhooks` prints the same key on every machine with the same root secret.",
|
|
242
325
|
""
|
|
243
326
|
].join("\n"));
|
|
244
327
|
return 0;
|
|
@@ -260,18 +343,58 @@ async function derive(args, context) {
|
|
|
260
343
|
context.stdout.write(`${await deriveViteHubSecret(root, purpose)}\n`);
|
|
261
344
|
return 0;
|
|
262
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* Print a new Ed25519 key pair for signed Console requests: the public key line for the app, and the private key line
|
|
348
|
+
* for the machine or CI job that calls it with `--url`. The name defaults to `CLI`.
|
|
349
|
+
*/
|
|
350
|
+
async function consoleKey(args, context) {
|
|
351
|
+
if (help(args)) {
|
|
352
|
+
context.stdout.write([
|
|
353
|
+
`Usage: ${consoleKeyUsage}`,
|
|
354
|
+
"",
|
|
355
|
+
"Print a new key pair for signed Console requests. Set the first line on the app (console: true),",
|
|
356
|
+
"and the second line where the CLI runs, such as a CI secret. The name defaults to CLI.",
|
|
357
|
+
""
|
|
358
|
+
].join("\n"));
|
|
359
|
+
return 0;
|
|
360
|
+
}
|
|
361
|
+
const [input = "CLI", ...rest] = args;
|
|
362
|
+
const name = input.toUpperCase().replace(/[^A-Z0-9]+/g, "_");
|
|
363
|
+
if (rest.length || !isConsoleKeyName(name)) {
|
|
364
|
+
context.stderr.write(`Usage: ${consoleKeyUsage}\nThe name has 1 to 64 letters, digits, or underscores.\n`);
|
|
365
|
+
return 1;
|
|
366
|
+
}
|
|
367
|
+
const { privateKey, publicKey } = await createConsoleKeyPair();
|
|
368
|
+
context.stdout.write([
|
|
369
|
+
"# The app: set this variable on the deployment.",
|
|
370
|
+
`${consolePublicKeyPrefix}${name}=${publicKey}`,
|
|
371
|
+
"# The CLI: keep this one secret, such as in a CI secret. Remove the app variable to revoke it.",
|
|
372
|
+
`${consolePrivateKeyPrefix}${name}=${privateKey}`,
|
|
373
|
+
""
|
|
374
|
+
].join("\n"));
|
|
375
|
+
return 0;
|
|
376
|
+
}
|
|
263
377
|
/** `vitehub secret` features. They read only the process env and never load the project. */
|
|
264
|
-
const secretCliFeatures = [
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
378
|
+
const secretCliFeatures = [
|
|
379
|
+
{
|
|
380
|
+
description: "Print a new random root secret for VITEHUB_SECRET.",
|
|
381
|
+
name: "generate",
|
|
382
|
+
run: async (args, context) => generate(args, context),
|
|
383
|
+
usage: generateUsage
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
description: "Print the secret derived from VITEHUB_SECRET for one purpose.",
|
|
387
|
+
name: "derive",
|
|
388
|
+
run: async (args, context) => await derive(args, context),
|
|
389
|
+
usage: deriveUsage
|
|
390
|
+
},
|
|
391
|
+
{
|
|
392
|
+
description: "Print a new key pair for signed Console requests from the CLI.",
|
|
393
|
+
name: "console-key",
|
|
394
|
+
run: async (args, context) => await consoleKey(args, context),
|
|
395
|
+
usage: consoleKeyUsage
|
|
396
|
+
}
|
|
397
|
+
];
|
|
275
398
|
//#endregion
|
|
276
399
|
//#region src/cli.ts
|
|
277
400
|
/** CLI discovery resolves Vite config in development mode, so the default stage matches it. */
|
|
@@ -330,6 +453,65 @@ function isInspection(value) {
|
|
|
330
453
|
])
|
|
331
454
|
})) }), value);
|
|
332
455
|
}
|
|
456
|
+
/**
|
|
457
|
+
* Resolves the stage config and loads modules in its SSR environment. A dev server would also start every other
|
|
458
|
+
* environment, such as Nitro's dev worker, and the check never uses them.
|
|
459
|
+
*/
|
|
460
|
+
async function createStageModuleLoader(vite, config, allowNuxtFallback = false) {
|
|
461
|
+
const nitro = createCliDiscoveryNitroCloser();
|
|
462
|
+
const ws = {
|
|
463
|
+
...vite.createServerHotChannel(),
|
|
464
|
+
clients: /* @__PURE__ */ new Set(),
|
|
465
|
+
handleInvoke: async () => ({ error: {
|
|
466
|
+
name: "TransportError",
|
|
467
|
+
message: "WebSocket transport is disabled during Env inspection."
|
|
468
|
+
} })
|
|
469
|
+
};
|
|
470
|
+
let environment;
|
|
471
|
+
let fallback;
|
|
472
|
+
const close = async () => {
|
|
473
|
+
try {
|
|
474
|
+
await fallback?.close();
|
|
475
|
+
} finally {
|
|
476
|
+
try {
|
|
477
|
+
await environment?.close();
|
|
478
|
+
} finally {
|
|
479
|
+
try {
|
|
480
|
+
await ws.close();
|
|
481
|
+
} finally {
|
|
482
|
+
await nitro.close();
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
try {
|
|
488
|
+
const resolved = await vite.resolveConfig({
|
|
489
|
+
...config,
|
|
490
|
+
plugins: [...config.plugins ?? [], nitro.plugin]
|
|
491
|
+
}, "serve");
|
|
492
|
+
environment = await resolved.environments.ssr.dev.createEnvironment("ssr", resolved, { ws });
|
|
493
|
+
await environment.init();
|
|
494
|
+
let runnable;
|
|
495
|
+
if (vite.isRunnableDevEnvironment(environment)) runnable = environment;
|
|
496
|
+
else {
|
|
497
|
+
if (!allowNuxtFallback) throw envErrorDiagnostics.ENV_R0030({ message: "The SSR environment does not support runnable imports." });
|
|
498
|
+
fallback = vite.createRunnableDevEnvironment("ssr", resolved, {
|
|
499
|
+
hot: false,
|
|
500
|
+
options: { dev: { moduleRunnerTransform: true } }
|
|
501
|
+
});
|
|
502
|
+
await fallback.init();
|
|
503
|
+
runnable = fallback;
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
close,
|
|
507
|
+
config: resolved,
|
|
508
|
+
ssrLoadModule: async (url) => await runnable.runner.import(url)
|
|
509
|
+
};
|
|
510
|
+
} catch (error) {
|
|
511
|
+
await close().catch(() => {});
|
|
512
|
+
throw error;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
333
515
|
async function inspectStage(input) {
|
|
334
516
|
const explicitEnv = { ...input.env };
|
|
335
517
|
const vite = await import("vite");
|
|
@@ -345,10 +527,10 @@ async function inspectStage(input) {
|
|
|
345
527
|
return await withViteStageServer({
|
|
346
528
|
loadEnv: vite.loadEnv,
|
|
347
529
|
async createServer(config) {
|
|
348
|
-
if (!hasNuxtConfig) return await vite
|
|
530
|
+
if (!hasNuxtConfig) return await createStageModuleLoader(vite, config);
|
|
349
531
|
const require = createRequire(join(projectRoot, "package.json"));
|
|
350
532
|
const kit = await import(pathToFileURL(require.resolve("nuxt/kit")).href);
|
|
351
|
-
Object.assign(process.env, vite.loadEnv
|
|
533
|
+
Object.assign(process.env, loadAllEnv(vite.loadEnv, input.stage, projectRoot));
|
|
352
534
|
for (const [key, value] of Object.entries(explicitEnv)) if (value !== void 0) process.env[key] = value;
|
|
353
535
|
const nuxt = await kit.loadNuxt({
|
|
354
536
|
cwd: projectRoot,
|
|
@@ -362,11 +544,11 @@ async function inspectStage(input) {
|
|
|
362
544
|
ready: true
|
|
363
545
|
});
|
|
364
546
|
try {
|
|
365
|
-
const server = await vite
|
|
547
|
+
const server = await createStageModuleLoader(vite, {
|
|
366
548
|
...nuxt.options.vite,
|
|
367
549
|
...config,
|
|
368
550
|
configFile: false
|
|
369
|
-
});
|
|
551
|
+
}, true);
|
|
370
552
|
const close = server.close.bind(server);
|
|
371
553
|
server.close = async () => {
|
|
372
554
|
try {
|
|
@@ -484,4 +666,4 @@ function createEnvCliContributor(options) {
|
|
|
484
666
|
}] };
|
|
485
667
|
}
|
|
486
668
|
//#endregion
|
|
487
|
-
export {
|
|
669
|
+
export { VITEHUB_ENV_SERVER as a, createViteHubEnvImportAliases as c, viteHubEnvAmbientTypesPath as d, viteHubEnvDescriptionModulePath as f, viteHubEnvServerModuleTypesPath as g, viteHubEnvServerModulePath as h, VITEHUB_ENV_PUBLIC_ID as i, isServerEnvironment as l, viteHubEnvPublicModuleTypesPath as m, secretCliFeatures as n, VITEHUB_ENV_SERVER_ID as o, viteHubEnvPublicModulePath as p, loadAllEnv as r, createNoExternalAddition as s, createEnvCliContributor as t, resolveViteHubProjectRoot as u };
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as ViteHubCliContributor, t as ViteHubCliContext } from "./cli-
|
|
1
|
+
import { G as ServerEnvInspection, K as ServerEnvInspectionEntry } from "./bridge-Cs9YfQWk.js";
|
|
2
|
+
import { n as ViteHubCliContributor, t as ViteHubCliContext } from "./cli-BPp7rdcA.js";
|
|
3
3
|
//#region src/secret-cli.d.ts
|
|
4
4
|
interface SecretCliContext {
|
|
5
5
|
env: Readonly<Record<string, string | undefined>>;
|
package/dist/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as secretCliFeatures, t as createEnvCliContributor } from "./cli-
|
|
1
|
+
import { n as secretCliFeatures, t as createEnvCliContributor } from "./cli-DLpY0QAo.js";
|
|
2
2
|
export { createEnvCliContributor, secretCliFeatures };
|
package/dist/database.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as EnvSecretStore, r as EnvAccessStore } from "./bridge-
|
|
1
|
+
import { d as EnvSecretMetadata, f as EnvSecretStore, r as EnvAccessStore } from "./bridge-Cs9YfQWk.js";
|
|
2
2
|
import { SQL } from "drizzle-orm";
|
|
3
3
|
//#region src/database.d.ts
|
|
4
4
|
export interface EnvDatabase {
|
|
@@ -8,13 +8,39 @@ export interface EnvDatabase {
|
|
|
8
8
|
export interface DatabaseEnvStore {
|
|
9
9
|
/** SQL predicate for atomically updating related records at a secret revision. */
|
|
10
10
|
revisionCondition(key: string, revision: string | null): Promise<SQL>;
|
|
11
|
+
/** The keys of the namespace with their metadata, by key. It never reads a value. */
|
|
12
|
+
list(): Promise<(EnvSecretMetadata & {
|
|
13
|
+
key: string;
|
|
14
|
+
})[]>;
|
|
15
|
+
/** Delete a key. Returns `false` when the namespace has no such key. Activity and grants stay. */
|
|
16
|
+
remove(key: string): Promise<boolean>;
|
|
17
|
+
/**
|
|
18
|
+
* Re-encrypts every value of the namespace that another key sealed with the current key, the first of
|
|
19
|
+
* `encryptionKeys`. Revisions stay the same. A value that a write replaces during the rotation keeps the new write.
|
|
20
|
+
*/
|
|
21
|
+
rotate(): Promise<EnvStoreRotation>;
|
|
11
22
|
secrets: EnvSecretStore;
|
|
12
23
|
access: EnvAccessStore;
|
|
13
24
|
}
|
|
14
|
-
/**
|
|
25
|
+
/** The result of `DatabaseEnvStore.rotate()`. */
|
|
26
|
+
export interface EnvStoreRotation {
|
|
27
|
+
/** Values re-encrypted with the current key. */
|
|
28
|
+
rotated: number;
|
|
29
|
+
/** Values that the current key already sealed, or that a concurrent write replaced. */
|
|
30
|
+
skipped: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The namespaces that start with `prefix` and hold at least one key, in text order. A Console that keeps one namespace
|
|
34
|
+
* for each project and stage, such as `<project>:production`, finds the stages of a project with it.
|
|
35
|
+
*/
|
|
36
|
+
export declare function listDatabaseEnvNamespaces(db: EnvDatabase, prefix: string): Promise<string[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Persistent encrypted values, activity, and grants on a ViteHub SQLite/Drizzle database.
|
|
39
|
+
* `encryptionKeys` is the keyring: the current 32-byte key first, then previous keys that still decrypt older values.
|
|
40
|
+
*/
|
|
15
41
|
export declare function createDatabaseEnvStore(options: {
|
|
16
42
|
db: EnvDatabase;
|
|
17
|
-
|
|
43
|
+
encryptionKeys: readonly Uint8Array[];
|
|
18
44
|
namespace?: string;
|
|
19
45
|
previews?: boolean;
|
|
20
46
|
}): DatabaseEnvStore;
|