Open History to see the last commit
1/**
2 * Type-safe secret references for sandbox workspace definitions.
3 *
4 * Values are stored in a Map under a non-enumerable symbol key on the returned
5 * object so that `Object.keys(secrets)` only yields the ref names, never the
6 * registry or the underlying plaintext values.
7 */
8
9/** A reference to a named secret — carries only the name, never the value. */
10export type SecretRef = { readonly __secretName: string }
11
12/**
13 * A map of named SecretRef properties. The underlying value registry is stored
14 * under a non-enumerable symbol so iterating the object never exposes it.
15 */
16export type Secrets<TKeys extends string = string> = {
17 readonly [P in TKeys]: SecretRef
18}
19
20/** Internal symbol used to store the value registry on a Secrets object. */
21const REGISTRY = Symbol('secrets.registry')
22
23/** Create a typed secrets object from a plain record of name→value pairs. */
24export function createSecrets<T extends Record<string, string>>(
25 values: T,
26): Secrets<keyof T & string> {
27 const registry = new Map<string, string>(Object.entries(values))
28 const obj = {} as Record<string, SecretRef>
29
30 for (const name of Object.keys(values)) {
31 obj[name] = Object.freeze({ __secretName: name })
32 }
33
34 Object.defineProperty(obj, REGISTRY, {
35 value: registry,
36 enumerable: false,
37 configurable: false,
38 writable: false,
39 })
40
41 return obj as Secrets<keyof T & string>
42}
43
44/** Marker type for a bearer-token value derived from a SecretRef. */
45export type BearerRef = { readonly __bearerRef: SecretRef }
46
47/** Create a bearer-token marker that resolves to `Bearer <value>` at runtime. */
48export function bearer(ref: SecretRef): BearerRef {
49 return Object.freeze({ __bearerRef: ref })
50}
51
52/** Return true when `x` is a SecretRef. */
53export function isSecretRef(x: unknown): x is SecretRef {
54 return (
55 typeof x === 'object' &&
56 x !== null &&
57 typeof (x as Record<string, unknown>)['__secretName'] === 'string'
58 )
59}
60
61/** Resolve a SecretRef to its plaintext value using the secrets object. */
62export function resolveSecret(secrets: Secrets, ref: SecretRef): string {
63 const registry = Reflect.get(secrets, REGISTRY) as
64 | Map<string, string>
65 | undefined
66 if (registry === undefined) {
67 throw new Error(
68 'resolveSecret: secrets object was not created by createSecrets',
69 )
70 }
71 const value = registry.get(ref.__secretName)
72 if (value === undefined) {
73 throw new Error(`resolveSecret: unknown secret "${ref.__secretName}"`)
74 }
75 return value
76}
77
78/** Resolve a BearerRef to a `Bearer <value>` string. */
79export function resolveBearer(secrets: Secrets, ref: BearerRef): string {
80 return `Bearer ${resolveSecret(secrets, ref.__bearerRef)}`
81}
82
83/**
84 * Resolve all secrets in a Secrets object to a plain `Record<string, string>`
85 * suitable for injecting into a process environment.
86 */
87export function resolveAllSecrets(secrets: Secrets): Record<string, string> {
88 const registry = Reflect.get(secrets, REGISTRY) as
89 | Map<string, string>
90 | undefined
91 if (registry === undefined) {
92 throw new Error(
93 'resolveAllSecrets: secrets object was not created by createSecrets',
94 )
95 }
96 const result: Record<string, string> = {}
97 for (const [key, value] of registry.entries()) {
98 result[key] = value
99 }
100 return result
101}