@vite-hub/env 0.0.4 → 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 (47) hide show
  1. package/README.md +125 -5
  2. package/dist/access-B3qzBHye.js +54 -0
  3. package/dist/auth.d.ts +32 -0
  4. package/dist/auth.js +44 -0
  5. package/dist/bridge-CdNdTN1V.d.ts +374 -0
  6. package/dist/bridge-error-CFuU5zIz.js +65 -0
  7. package/dist/bridge.d.ts +2 -0
  8. package/dist/bridge.js +192 -0
  9. package/dist/cli-CMQPQbii.js +487 -0
  10. package/dist/cli-DEwcUIe0.d.ts +81 -0
  11. package/dist/cli.d.ts +41 -0
  12. package/dist/cli.js +2 -0
  13. package/dist/database.d.ts +21 -0
  14. package/dist/database.js +183 -0
  15. package/dist/declarations-CiRdb1qP.d.ts +32 -0
  16. package/dist/declarations-DAVgctKD.js +236 -0
  17. package/dist/error-diagnostics-CrxjYNYr.js +43 -0
  18. package/dist/http.d.ts +2 -0
  19. package/dist/http.js +122 -0
  20. package/dist/index.d.ts +6 -6
  21. package/dist/index.js +4 -4
  22. package/dist/internal/agent.d.ts +5 -0
  23. package/dist/internal/agent.js +22 -0
  24. package/dist/internal/connections.d.ts +18 -0
  25. package/dist/internal/connections.js +31 -0
  26. package/dist/presets.d.ts +15 -6
  27. package/dist/presets.js +21 -2
  28. package/dist/provider.d.ts +3 -4
  29. package/dist/provider.js +2 -1
  30. package/dist/schema.d.ts +5 -6
  31. package/dist/schema.js +26 -8
  32. package/dist/seal.d.ts +14 -0
  33. package/dist/seal.js +49 -0
  34. package/dist/server-DufVswRJ.js +670 -0
  35. package/dist/server.d.ts +23 -6
  36. package/dist/server.js +2 -3
  37. package/dist/values-D_r6V774.js +148 -0
  38. package/dist/virtual-module.d.ts +7 -3
  39. package/dist/virtual.d.ts +1 -1
  40. package/dist/vite.d.ts +27 -14
  41. package/dist/vite.js +840 -273
  42. package/package.json +81 -17
  43. package/dist/declarations-CoMmvRqH.js +0 -159
  44. package/dist/declarations-U6P3rvxC.d.ts +0 -24
  45. package/dist/errors-C4Xcs6Fi.js +0 -121
  46. package/dist/server-C16sFc1E.js +0 -303
  47. package/dist/types-CsB6DTGE.d.ts +0 -190
@@ -0,0 +1,65 @@
1
+ import { defineDiagnostics } from "nostics";
2
+ import { ViteHubError, isViteHubError } from "@vite-hub/runtime";
3
+ //#region src/bridge-error.ts
4
+ const messages = {
5
+ denied: "Env access denied.",
6
+ conflict: "The credential changed. Refresh before replacing it.",
7
+ missing: "Credential unavailable.",
8
+ invalid: "Invalid Env request.",
9
+ operation_failed: "Env operation failed.",
10
+ audit_failed: "Env activity could not be persisted.",
11
+ untrusted: "Env did not create this access context."
12
+ };
13
+ function envBridgeError(code) {
14
+ return new ViteHubError(`ENV_BRIDGE_${code.toUpperCase()}`, messages[code]);
15
+ }
16
+ const docs = "https://vitehub.dev/docs/env/bridge#structured-errors";
17
+ const guidance = (fix) => ({
18
+ why: ({ message }) => message,
19
+ fix,
20
+ docs
21
+ });
22
+ /** Repair guidance that the management route adds to each bridge error body. */
23
+ const envBridgeDiagnostics = /*#__PURE__*/ defineDiagnostics({ codes: {
24
+ ENV_BRIDGE_DENIED: guidance("Ask an Env admin to grant the permission for this key."),
25
+ ENV_BRIDGE_CONFLICT: guidance("Reload the credential details, then replace it again."),
26
+ ENV_BRIDGE_MISSING: guidance("Store a value for this credential first."),
27
+ ENV_BRIDGE_INVALID: guidance("Check the request fields and the stored value limits, then send it again."),
28
+ ENV_BRIDGE_OPERATION_FAILED: guidance("Try again. If this continues, read the server logs for this request."),
29
+ ENV_BRIDGE_AUDIT_FAILED: guidance("Check the Env activity store, then try again."),
30
+ ENV_BRIDGE_UNTRUSTED: guidance("Create the access context with the Env bridge authenticate function.")
31
+ } });
32
+ /** The JSON error body of a sanitized bridge error: `code`, `message` (the why), `fix`, and `docs`. */
33
+ function envBridgeErrorBody(error) {
34
+ const code = error.code;
35
+ if (!isEnvBridgeDiagnosticCode(code)) return {
36
+ code,
37
+ message: error.message
38
+ };
39
+ const diagnostic = envBridgeDiagnostics[code]({ message: error.message });
40
+ return {
41
+ code,
42
+ docs: diagnostic.docs,
43
+ fix: diagnostic.fix,
44
+ message: error.message
45
+ };
46
+ }
47
+ function isEnvBridgeDiagnosticCode(code) {
48
+ return Object.hasOwn(envBridgeDiagnostics, code);
49
+ }
50
+ function sanitizeEnvBridgeError(error) {
51
+ if (isViteHubError(error)) {
52
+ for (const code of [
53
+ "denied",
54
+ "conflict",
55
+ "missing",
56
+ "invalid",
57
+ "operation_failed",
58
+ "audit_failed",
59
+ "untrusted"
60
+ ]) if (error.code === `ENV_BRIDGE_${code.toUpperCase()}`) return envBridgeError(code);
61
+ }
62
+ return envBridgeError("operation_failed");
63
+ }
64
+ //#endregion
65
+ export { envBridgeErrorBody as n, sanitizeEnvBridgeError as r, envBridgeError as t };
@@ -0,0 +1,2 @@
1
+ import { a as EnvActor, c as EnvGrant, d as EnvSecretMetadata, f as EnvSecretStore, i as EnvActivity, l as EnvPermission, n as EnvAccessScope, o as EnvBridge, p as createEnvBridge, r as EnvAccessStore, s as EnvBridgeOptions, t as EnvAccessContext, u as EnvReplacement } from "./bridge-CdNdTN1V.js";
2
+ export { EnvAccessContext, EnvAccessScope, EnvAccessStore, EnvActivity, EnvActor, EnvBridge, EnvBridgeOptions, EnvGrant, EnvPermission, EnvReplacement, EnvSecretMetadata, EnvSecretStore, createEnvBridge };
package/dist/bridge.js ADDED
@@ -0,0 +1,192 @@
1
+ import { SecretEnv } from "./secret.js";
2
+ import { r as sanitizeEnvBridgeError, t as envBridgeError } from "./bridge-error-CFuU5zIz.js";
3
+ import { i as validateEnvActor, n as envIdentifier, r as grantEnvAccess, t as envAccessAuthority } from "./access-B3qzBHye.js";
4
+ //#region src/bridge.ts
5
+ async function safe(run) {
6
+ try {
7
+ return await run();
8
+ } catch (error) {
9
+ throw sanitizeEnvBridgeError(error);
10
+ }
11
+ }
12
+ function createEnvBridge(options) {
13
+ async function persist(event) {
14
+ try {
15
+ await options.access.append(event);
16
+ } catch {
17
+ throw envBridgeError("audit_failed");
18
+ }
19
+ try {
20
+ await options.emit?.(structuredClone(event));
21
+ } catch {}
22
+ }
23
+ async function allowed(context, key, permission) {
24
+ const authority = envAccessAuthority(context);
25
+ envIdentifier(key);
26
+ validateEnvActor(context.actor);
27
+ if (authority.kind === "admin") return true;
28
+ if (authority.bridge && authority.bridge !== bridge) return false;
29
+ if (authority.kind === "key") return authority.key === key && permission !== "admin" && authority.permissions.includes(permission);
30
+ if (permission === "admin" || permission === "activity") return false;
31
+ if (authority.scope && !authority.scope.some((grant) => grant.key === key && grant.permissions.includes(permission))) return false;
32
+ return await safe(async () => {
33
+ const grants = await options.access.grants(key);
34
+ if (!Array.isArray(grants)) throw new TypeError("Invalid access grants.");
35
+ for (const grant of grants) {
36
+ if (!grant || typeof grant !== "object" || typeof grant.key !== "string" || !grant.actor || typeof grant.actor !== "object" || typeof grant.actor.id !== "string" || ![
37
+ "user",
38
+ "agent",
39
+ "service"
40
+ ].includes(grant.actor.kind) || !Array.isArray(grant.permissions) || !grant.permissions.every((value) => [
41
+ "inspect",
42
+ "preview",
43
+ "replace",
44
+ "use"
45
+ ].includes(value))) throw new TypeError("Invalid access grant.");
46
+ if (grant.key === key && grant.actor.id === context.actor.id && grant.actor.kind === context.actor.kind && grant.permissions.includes(permission)) return true;
47
+ }
48
+ return false;
49
+ });
50
+ }
51
+ async function audited(context, key, action, permission, run, operation, metadata) {
52
+ envAccessAuthority(context);
53
+ envIdentifier(key);
54
+ validateEnvActor(context.actor);
55
+ const base = {
56
+ operationId: crypto.randomUUID(),
57
+ actor: {
58
+ id: context.actor.id,
59
+ kind: context.actor.kind
60
+ },
61
+ key,
62
+ action,
63
+ ...context.traceId ? { traceId: context.traceId } : {},
64
+ ...context.invocationId ? { invocationId: context.invocationId } : {},
65
+ ...operation ? { operation } : {},
66
+ ...metadata?.target ? { target: metadata.target } : {},
67
+ ...metadata?.permissions ? { permissions: metadata.permissions } : {}
68
+ };
69
+ const event = (outcome) => ({
70
+ ...base,
71
+ id: crypto.randomUUID(),
72
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
73
+ outcome
74
+ });
75
+ if (!await allowed(context, key, permission)) {
76
+ await persist(event("denied"));
77
+ throw envBridgeError("denied");
78
+ }
79
+ await persist(event("started"));
80
+ let value;
81
+ try {
82
+ value = await run();
83
+ } catch (error) {
84
+ const revision = metadata?.revision?.();
85
+ await persist({
86
+ ...event("failed"),
87
+ ...revision ? { revision } : {}
88
+ });
89
+ throw sanitizeEnvBridgeError(error);
90
+ }
91
+ const revision = metadata?.revision?.();
92
+ await persist({
93
+ ...event("succeeded"),
94
+ ...revision ? { revision } : {}
95
+ });
96
+ return value;
97
+ }
98
+ const bridge = {
99
+ async permissions(context, key) {
100
+ return (await Promise.all([
101
+ "inspect",
102
+ "preview",
103
+ "replace",
104
+ "use"
105
+ ].map(async (permission) => await allowed(context, key, permission) ? permission : void 0))).filter((permission) => permission !== void 0);
106
+ },
107
+ async read({ keys, signal, access }) {
108
+ const context = access ?? grantEnvAccess({
109
+ ...await options.runtimeAttribution?.(),
110
+ actor: options.runtimeActor
111
+ }, {
112
+ kind: "actor",
113
+ bridge
114
+ });
115
+ const values = [];
116
+ for (const key of keys) {
117
+ signal?.throwIfAborted();
118
+ let revision;
119
+ const secret = await audited(context, key, "resolve", "use", async () => {
120
+ const secret = await options.secrets.read(key);
121
+ revision = secret?.revision;
122
+ return secret;
123
+ }, void 0, { revision: () => revision });
124
+ signal?.throwIfAborted();
125
+ values.push([key, secret?.value]);
126
+ }
127
+ return Object.fromEntries(values);
128
+ },
129
+ inspect: (context, key) => audited(context, key, "inspect", "inspect", async () => {
130
+ const value = await options.secrets.inspect(key);
131
+ return value ? {
132
+ revision: value.revision,
133
+ updatedAt: value.updatedAt
134
+ } : void 0;
135
+ }),
136
+ preview: (context, key) => audited(context, key, "preview", "preview", async () => {
137
+ const value = await options.secrets.inspect(key);
138
+ return value?.preview ? { preview: value.preview } : void 0;
139
+ }),
140
+ replace: (context, input) => {
141
+ let revision;
142
+ return audited(context, input.key, "replace", "replace", async () => {
143
+ if (!input.value || new TextEncoder().encode(input.value).length > 32768) throw envBridgeError("invalid");
144
+ const replacement = await options.secrets.replace(input);
145
+ revision = replacement.revision;
146
+ return replacement;
147
+ }, void 0, { revision: () => revision });
148
+ },
149
+ use: (context, key, operation, run) => {
150
+ envIdentifier(operation);
151
+ let revision;
152
+ return audited(context, key, "use", "use", async () => {
153
+ const secret = await options.secrets.read(key);
154
+ if (!secret) throw envBridgeError("missing");
155
+ revision = secret.revision;
156
+ return await run(new SecretEnv(secret.value), { revision });
157
+ }, operation, { revision: () => revision });
158
+ },
159
+ async activity(context, key, before) {
160
+ if (!await allowed(context, key, "activity")) throw envBridgeError("denied");
161
+ return await safe(() => options.access.activity({
162
+ key,
163
+ before,
164
+ limit: 100
165
+ }));
166
+ },
167
+ async grants(context, key) {
168
+ if (!await allowed(context, key, "admin")) throw envBridgeError("denied");
169
+ return await safe(() => options.access.grants(key));
170
+ },
171
+ async grant(context, grant) {
172
+ validateEnvActor(grant.actor);
173
+ if (!Array.isArray(grant.permissions) || !grant.permissions.length || grant.permissions.some((permission) => ![
174
+ "inspect",
175
+ "preview",
176
+ "replace",
177
+ "use"
178
+ ].includes(permission))) throw envBridgeError("invalid");
179
+ return audited(context, grant.key, "grant", "admin", () => options.access.setGrant(grant), void 0, {
180
+ target: grant.actor,
181
+ permissions: grant.permissions
182
+ });
183
+ },
184
+ async revoke(context, actor, key) {
185
+ validateEnvActor(actor);
186
+ return audited(context, key, "revoke", "admin", () => options.access.revokeGrant(actor, key), void 0, { target: actor });
187
+ }
188
+ };
189
+ return bridge;
190
+ }
191
+ //#endregion
192
+ export { createEnvBridge };