@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.
Files changed (50) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +243 -28
  3. package/dist/access-B3qzBHye.js +54 -0
  4. package/dist/auth.d.ts +32 -0
  5. package/dist/auth.js +44 -0
  6. package/dist/bridge-CdNdTN1V.d.ts +374 -0
  7. package/dist/bridge-error-CFuU5zIz.js +65 -0
  8. package/dist/bridge.d.ts +2 -0
  9. package/dist/bridge.js +192 -0
  10. package/dist/cli-CMQPQbii.js +487 -0
  11. package/dist/cli-DEwcUIe0.d.ts +81 -0
  12. package/dist/cli.d.ts +41 -0
  13. package/dist/cli.js +2 -0
  14. package/dist/database.d.ts +21 -0
  15. package/dist/database.js +183 -0
  16. package/dist/declarations-CiRdb1qP.d.ts +32 -0
  17. package/dist/declarations-DAVgctKD.js +236 -0
  18. package/dist/error-diagnostics-CrxjYNYr.js +43 -0
  19. package/dist/http.d.ts +2 -0
  20. package/dist/http.js +122 -0
  21. package/dist/index.d.ts +13 -8
  22. package/dist/index.js +5 -4
  23. package/dist/internal/agent.d.ts +5 -0
  24. package/dist/internal/agent.js +22 -0
  25. package/dist/internal/connections.d.ts +18 -0
  26. package/dist/internal/connections.js +31 -0
  27. package/dist/presets.d.ts +15 -6
  28. package/dist/presets.js +21 -2
  29. package/dist/provider.d.ts +5 -0
  30. package/dist/provider.js +8 -0
  31. package/dist/schema.d.ts +5 -6
  32. package/dist/schema.js +26 -8
  33. package/dist/seal.d.ts +14 -0
  34. package/dist/seal.js +49 -0
  35. package/dist/secret-C2191nAa.d.ts +11 -0
  36. package/dist/secret.d.ts +1 -10
  37. package/dist/server-DufVswRJ.js +670 -0
  38. package/dist/server.d.ts +23 -4
  39. package/dist/server.js +2 -3
  40. package/dist/values-D_r6V774.js +148 -0
  41. package/dist/virtual-module.d.ts +34 -0
  42. package/dist/virtual.d.ts +2 -15
  43. package/dist/vite.d.ts +37 -14
  44. package/dist/vite.js +1058 -292
  45. package/package.json +84 -16
  46. package/dist/declarations-BIBfEwer.d.ts +0 -23
  47. package/dist/declarations-Cy5XamUA.js +0 -144
  48. package/dist/errors-RUmAFDJn.js +0 -112
  49. package/dist/server-B2Dp8Nc0.js +0 -59
  50. package/dist/types-FcnPKias.d.ts +0 -153
package/dist/server.d.ts CHANGED
@@ -1,6 +1,25 @@
1
- import { s as EnvRuntimeRegistry } from "./types-FcnPKias.js";
2
-
1
+ import { B as ServerEnvInspection, C as EnvProviders, F as LoadServerEnvOptions, H as ServerEnvInspectionStatus, R as ServerEnvDescription, T as EnvRuntimeRegistry, V as ServerEnvInspectionEntry, m as DeepReadonly, t as EnvAccessContext } from "./bridge-CdNdTN1V.js";
3
2
  //#region src/server.d.ts
4
- declare function resolveServerEnv<TServerEnv extends Record<string, unknown> = Record<string, unknown>>(registry: EnvRuntimeRegistry, event?: unknown): TServerEnv;
3
+ /**
4
+ * Derives a 32-byte hex secret for one purpose from the root app secret, `VITEHUB_SECRET` (or `NUXT_APP_SECRET`).
5
+ * It uses HKDF-SHA256, so the result is stable while the root is unchanged and distinct for every purpose.
6
+ * Namespace the purpose to its owner, such as `my-module:session`. ViteHub reserves the `vitehub:` prefix.
7
+ * Throws `ENV_REQUIRED_MISSING` when no root secret is set, and `ENV_RUNTIME_VALUE_INVALID` when it is shorter than 32 characters.
8
+ */
9
+ export declare function deriveSecret(purpose: string, event?: unknown, rootOverride?: string, generatedOverride?: boolean, rootEnvKey?: string): Promise<string>;
10
+ /**
11
+ * Apps call `useServerEnv()` on hot paths, often once per listed row. The result is reused while every name the
12
+ * registry reads holds the same value, so a call does not copy the whole env and parse each declaration again. Each
13
+ * call still gets its own records.
14
+ */
15
+ export declare function resolveServerEnv<TServerEnv extends Record<string, unknown> = Record<string, unknown>>(registry: EnvRuntimeRegistry<TServerEnv>, event?: unknown): TServerEnv;
16
+ export declare function loadServerEnv<TServerEnv extends Record<string, unknown> = Record<string, unknown>>(registry: EnvRuntimeRegistry<TServerEnv>, event?: unknown, options?: LoadServerEnvOptions): Promise<DeepReadonly<TServerEnv>>;
17
+ /** Describe declarations without loading host values or calling providers. */
18
+ export declare function describeServerEnv(registry: EnvRuntimeRegistry): ServerEnvDescription;
19
+ /** Returns whether an inspection entry makes `loadServerEnv()` fail: invalid, provider error, or required and missing. */
20
+ export declare function isBlockingServerEnvEntry(entry: ServerEnvInspectionEntry): boolean;
21
+ export declare function inspectServerEnv(registry: EnvRuntimeRegistry, event?: unknown, options?: LoadServerEnvOptions): Promise<ServerEnvInspection>;
22
+ /** Limit management to safe, declared provider paths. Host variables stay read-only. */
23
+ export declare function createServerEnvManagement(registry: EnvRuntimeRegistry, providers: EnvProviders): (request: Request) => Promise<Response>;
5
24
  //#endregion
6
- export { resolveServerEnv };
25
+ export type { EnvAccessContext, ServerEnvInspection, ServerEnvInspectionEntry, ServerEnvInspectionStatus };
package/dist/server.js CHANGED
@@ -1,3 +1,2 @@
1
- import "./secret.js";
2
- import { t as resolveServerEnv } from "./server-B2Dp8Nc0.js";
3
- export { resolveServerEnv };
1
+ import { a as isBlockingServerEnvEntry, i as inspectServerEnv, n as deriveSecret, o as loadServerEnv, r as describeServerEnv, s as resolveServerEnv, t as createServerEnvManagement } from "./server-DufVswRJ.js";
2
+ export { createServerEnvManagement, deriveSecret, describeServerEnv, inspectServerEnv, isBlockingServerEnvEntry, loadServerEnv, resolveServerEnv };
@@ -0,0 +1,148 @@
1
+ import { hasRuntimeType } from "@vite-hub/runtime/internal/runtime-type";
2
+ //#region src/internal/runtime-type.ts
3
+ function isRuntimeRecord(value) {
4
+ return value !== null && hasRuntimeType(value, "object") && !Array.isArray(value);
5
+ }
6
+ //#endregion
7
+ //#region src/core/json-value.ts
8
+ /** JSON values accepted at the environment boundary, without prototype or internal keys. */
9
+ function isEnvSchemaValue(value, depth = 0) {
10
+ if (depth > 64) return false;
11
+ if (value === null || typeof value === "string" || typeof value === "boolean") return true;
12
+ if (typeof value === "number") return Number.isFinite(value);
13
+ if (Array.isArray(value)) return value.every((item) => isEnvSchemaValue(item, depth + 1));
14
+ if (typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype) return false;
15
+ return Object.entries(value).every(([key, item]) => ![
16
+ "__proto__",
17
+ "prototype",
18
+ "constructor",
19
+ "__envSchemaUnset"
20
+ ].includes(key) && isEnvSchemaValue(item, depth + 1));
21
+ }
22
+ //#endregion
23
+ //#region src/core/values.ts
24
+ const stringValueSchema = Object.freeze({ kind: "string" });
25
+ /** Parse one host, provider, or default value with a serializable Server Env value schema. */
26
+ function parseEnvValue(schema, input) {
27
+ switch (schema.kind) {
28
+ case "array":
29
+ case "object": {
30
+ let value = input;
31
+ if (typeof value === "string") try {
32
+ value = JSON.parse(value);
33
+ } catch {
34
+ return {
35
+ success: false,
36
+ message: "Expected valid JSON."
37
+ };
38
+ }
39
+ return isEnvSchemaValue(value) && matchesJsonType(schema, value) ? {
40
+ data: typeof input === "string" ? value : structuredClone(value),
41
+ success: true
42
+ } : {
43
+ success: false,
44
+ message: "JSON value does not match the declared type."
45
+ };
46
+ }
47
+ case "string": return hasRuntimeType(input, "string") && (schema.minLength === void 0 || input.length >= schema.minLength) && (schema.maxLength === void 0 || input.length <= schema.maxLength) ? {
48
+ data: input,
49
+ success: true
50
+ } : {
51
+ message: schema.minLength === void 0 && schema.maxLength === void 0 ? "Expected a string." : "Expected a string within the declared length constraints.",
52
+ success: false
53
+ };
54
+ case "boolean": return parseBoolean(input);
55
+ case "number": return parseNumber(input, schema);
56
+ case "url": return parseUrl(input);
57
+ case "enum": return hasRuntimeType(input, "string") && schema.values.includes(input) ? {
58
+ data: input,
59
+ success: true
60
+ } : {
61
+ message: `Expected one of ${schema.values.map((value) => JSON.stringify(value)).join(", ")}.`,
62
+ success: false
63
+ };
64
+ }
65
+ }
66
+ /** Return the TypeScript type that a value schema produces. */
67
+ function envValueTypeName(schema) {
68
+ switch (schema.kind) {
69
+ case "array": return `Array<${schema.items ? envValueTypeName(schema.items) : "unknown"}>`;
70
+ case "object": {
71
+ if (!schema.properties) return "Record<string, unknown>";
72
+ const properties = Object.entries(schema.properties);
73
+ return properties.length ? `{ ${properties.map(([key, property]) => `${JSON.stringify(key)}: ${envValueTypeName(property)}`).join("; ")} }` : "Record<string, never>";
74
+ }
75
+ case "enum": return schema.values.map((value) => JSON.stringify(value)).join(" | ");
76
+ case "url": return "string";
77
+ default: return schema.kind;
78
+ }
79
+ }
80
+ function parseBoolean(input) {
81
+ if (hasRuntimeType(input, "boolean")) return {
82
+ data: input,
83
+ success: true
84
+ };
85
+ const normalized = hasRuntimeType(input, "string") ? input.trim().toLowerCase() : void 0;
86
+ if (normalized === "true" || normalized === "1") return {
87
+ data: true,
88
+ success: true
89
+ };
90
+ if (normalized === "false" || normalized === "0") return {
91
+ data: false,
92
+ success: true
93
+ };
94
+ return {
95
+ message: "Expected a boolean: true, false, 1, or 0.",
96
+ success: false
97
+ };
98
+ }
99
+ function parseNumber(input, schema) {
100
+ if (schema.decimal && hasRuntimeType(input, "string") && !/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(input.trim())) return {
101
+ message: "Expected a finite decimal number.",
102
+ success: false
103
+ };
104
+ const value = hasRuntimeType(input, "string") && input.trim() ? Number(input) : input;
105
+ if (!hasRuntimeType(value, "number") || !Number.isFinite(value)) return {
106
+ message: "Expected a finite number.",
107
+ success: false
108
+ };
109
+ if (schema.isInt && !Number.isSafeInteger(value) || schema.min !== void 0 && value < schema.min || schema.max !== void 0 && value > schema.max) return {
110
+ message: "Expected a number within the declared constraints.",
111
+ success: false
112
+ };
113
+ return {
114
+ data: value,
115
+ success: true
116
+ };
117
+ }
118
+ function parseUrl(input) {
119
+ if (hasRuntimeType(input, "string")) try {
120
+ if (new URL(input).protocol) return {
121
+ data: input,
122
+ success: true
123
+ };
124
+ } catch {}
125
+ return {
126
+ message: "Expected an absolute URL.",
127
+ success: false
128
+ };
129
+ }
130
+ function matchesJsonType(schema, value) {
131
+ switch (schema.kind) {
132
+ case "array": return Array.isArray(value) && (!schema.items || value.every((item) => schema.items && matchesJsonType(schema.items, item)));
133
+ case "object": return value !== null && typeof value === "object" && !Array.isArray(value) && (!schema.properties || Object.keys(value).length === Object.keys(schema.properties).length && Object.entries(schema.properties).every(([key, property]) => Object.hasOwn(value, key) && matchesJsonType(property, Reflect.get(value, key))));
134
+ case "boolean": return typeof value === "boolean";
135
+ case "number": return typeof value === "number" && parseEnvValue(schema, value).success;
136
+ default: return typeof value === "string" && parseEnvValue(schema, value).success;
137
+ }
138
+ }
139
+ function schemaContainsEnum(schema) {
140
+ switch (schema.kind) {
141
+ case "enum": return true;
142
+ case "array": return !!schema.items && schemaContainsEnum(schema.items);
143
+ case "object": return Object.values(schema.properties ?? {}).some(schemaContainsEnum);
144
+ default: return false;
145
+ }
146
+ }
147
+ //#endregion
148
+ export { isEnvSchemaValue as a, stringValueSchema as i, parseEnvValue as n, hasRuntimeType as o, schemaContainsEnum as r, isRuntimeRecord as s, envValueTypeName as t };
@@ -0,0 +1,34 @@
1
+ declare module "#vitehub/env/public" {
2
+ export interface PublicEnv extends Record<string, unknown> {}
3
+ export const publicEnv: PublicEnv
4
+ export function usePublicEnv(): PublicEnv
5
+ }
6
+
7
+ declare module "#vitehub/env/server" {
8
+ export interface ServerEnv extends Record<string, unknown> {}
9
+ export function manageServerEnv(request: Request): Promise<Response>
10
+ export function describeServerEnv(): import("@vite-hub/env").ServerEnvDescription
11
+ export interface ServerEnvInspectionEntry {
12
+ masked: boolean
13
+ path?: string
14
+ provider?: string
15
+ required: boolean
16
+ source: "env" | "literal" | "provider"
17
+ status: "available" | "defaulted" | "error" | "invalid" | "missing"
18
+ }
19
+ export interface ServerEnvInspection {
20
+ entries: readonly ServerEnvInspectionEntry[]
21
+ }
22
+ export type ReadonlyServerEnv = DeepReadonly<ServerEnv>
23
+ type DeepReadonly<T> = T extends import("@vite-hub/env/secret").SecretEnv<unknown>
24
+ ? T
25
+ : T extends (...args: infer TArguments) => infer TResult
26
+ ? (...args: TArguments) => TResult
27
+ : T extends object
28
+ ? { readonly [TKey in keyof T]: DeepReadonly<T[TKey]> }
29
+ : T
30
+ export function inspectServerEnv(event?: unknown, options?: { signal?: AbortSignal; access?: import("@vite-hub/env/bridge").EnvAccessContext }): Promise<ServerEnvInspection>
31
+ export function loadServerEnv(event?: unknown, options?: { signal?: AbortSignal; access?: import("@vite-hub/env/bridge").EnvAccessContext }): Promise<ReadonlyServerEnv>
32
+ export function useServerEnv(event?: unknown): ServerEnv
33
+ export function runWithServerEnv<T>(event: unknown, callback: (env: ReadonlyServerEnv) => T | Promise<T>, options?: { signal?: AbortSignal; access?: import("@vite-hub/env/bridge").EnvAccessContext }): Promise<T>
34
+ }
package/dist/virtual.d.ts CHANGED
@@ -1,15 +1,2 @@
1
- declare module "#vitehub/env/public" {
2
- export interface PublicEnv extends Record<string, unknown> {}
3
- export const publicEnv: PublicEnv
4
- export function usePublicEnv(): PublicEnv
5
- }
6
-
7
- declare module "#vitehub/env/server" {
8
- import type { SecretEnv } from "@vite-hub/env/secret"
9
-
10
- export interface ServerEnv extends Record<string, unknown> {}
11
- export function useServerEnv(event?: unknown): ServerEnv
12
- export function runWithServerEnv<T>(event: unknown, callback: (env: ServerEnv) => T | Promise<T>): Promise<T>
13
- }
14
-
15
- export {}
1
+ /// <reference path="./virtual-module.d.ts" />
2
+ export {}
package/dist/vite.d.ts CHANGED
@@ -1,33 +1,56 @@
1
- import { i as EnvIntegrationOptions, p as EnvViteConfigOptions, s as EnvRuntimeRegistry } from "./types-FcnPKias.js";
2
- import { t as env } from "./declarations-BIBfEwer.js";
3
- import { Plugin } from "vite";
4
-
1
+ import { N as EnvViteConfigOptions, T as EnvRuntimeRegistry, v as EnvIntegrationOptions, w as EnvRuntimeConfigOptions } from "./bridge-CdNdTN1V.js";
2
+ import { n as ViteHubCliContributor } from "./cli-DEwcUIe0.js";
3
+ import { t as env } from "./declarations-CiRdb1qP.js";
4
+ import { Plugin, UserConfig } from "vite";
5
5
  //#region ../internal/src/build/vite.d.ts
6
6
  declare const VITEHUB_ENV_PUBLIC_ID: "#vitehub/env/public";
7
7
  declare const VITEHUB_ENV_SERVER_ID: "#vitehub/env/server";
8
8
  //#endregion
9
+ //#region src/core/resolve.d.ts
10
+ declare function createRuntimeRegistry(declarations: EnvRuntimeConfigOptions | undefined, options?: {
11
+ path?: string;
12
+ prefix?: string | false;
13
+ }): EnvRuntimeRegistry;
14
+ //#endregion
15
+ //#region src/env-schema/importer.d.ts
16
+ interface EnvSchemaOptions {
17
+ prefix?: string;
18
+ /** Exact schema key to output path. Only Server Env supports nested paths. */
19
+ paths?: Record<string, string>;
20
+ }
21
+ /** Import a committed env-spec-v1 contract. Reads schema metadata, never developer env files. */
22
+ export declare function envSchema(file: string | URL, options?: EnvSchemaOptions): EnvViteConfigOptions;
23
+ //#endregion
9
24
  //#region src/vite.d.ts
10
- declare const ENV_VITE_PLUGIN_NAME = "@vite-hub/env/vite";
11
- declare const ENV_PUBLIC_ID: typeof VITEHUB_ENV_PUBLIC_ID;
12
- declare const ENV_SERVER_ID: typeof VITEHUB_ENV_SERVER_ID;
13
- interface EnvVitePluginAPI {
25
+ export declare const ENV_VITE_PLUGIN_NAME = "@vite-hub/env/vite";
26
+ export declare const ENV_PUBLIC_ID: typeof VITEHUB_ENV_PUBLIC_ID;
27
+ export declare const ENV_SERVER_ID: typeof VITEHUB_ENV_SERVER_ID;
28
+ export interface EnvVitePluginAPI {
29
+ createServerEnvRegistry: (declarations: EnvRuntimeConfigOptions | undefined) => EnvRuntimeRegistry;
14
30
  getPublicEnv: () => Record<string, unknown>;
15
31
  getServerEnvRegistry: () => EnvRuntimeRegistry;
32
+ onServerEnvRegistry: (handler: (registry: EnvRuntimeRegistry, config: UserConfig) => void) => void;
33
+ prepareTypes: (config: EnvViteConfigOptions | undefined, viteRoot: string) => Promise<void>;
34
+ resolveProjectRoot: (viteRoot: string) => string;
16
35
  }
17
- type EnvVitePlugin = Plugin & {
36
+ export type EnvVitePlugin = Plugin & {
18
37
  api: EnvVitePluginAPI;
38
+ /** Contributes `vitehub env inspect` and `vitehub env check`. */
39
+ vitehub: {
40
+ cli: () => ViteHubCliContributor;
41
+ };
19
42
  };
20
- interface EnvGeneratedPathOptions {
43
+ export interface EnvGeneratedPathOptions {
21
44
  projectRoot?: string;
22
45
  relativeTo?: string;
23
46
  }
24
- declare function createEnvImportAliases(options?: EnvGeneratedPathOptions): Record<string, string>;
25
- declare function createEnvTypeScriptPaths(options?: EnvGeneratedPathOptions): Record<string, string[]>;
26
- declare function hubEnv(options?: EnvIntegrationOptions): EnvVitePlugin;
47
+ export declare function createEnvImportAliases(options?: EnvGeneratedPathOptions): Record<string, string>;
48
+ export declare function createEnvTypeScriptPaths(options?: EnvGeneratedPathOptions): Record<string, string[]>;
49
+ export declare function hubEnv(options?: EnvIntegrationOptions): EnvVitePlugin;
27
50
  declare module "vite" {
28
51
  interface UserConfig {
29
52
  env?: EnvViteConfigOptions;
30
53
  }
31
54
  }
32
55
  //#endregion
33
- export { ENV_PUBLIC_ID, ENV_SERVER_ID, ENV_VITE_PLUGIN_NAME, EnvGeneratedPathOptions, EnvVitePlugin, EnvVitePluginAPI, createEnvImportAliases, createEnvTypeScriptPaths, env, hubEnv };
56
+ export { type EnvSchemaOptions, createRuntimeRegistry as createRuntimeEnvRegistry, env };