@ultimat3/core 22.14.0 → 23.0.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.
@@ -0,0 +1,206 @@
1
+ // Single responsibility: the series every Ultimate PROCESS emits about itself — memory, CPU,
2
+ // event-loop lag, start time and which role it is. `runtime-metrics.ts` names what a role does;
3
+ // this names what the process costs, so "what is growing?" is a query and not a guess. Runs
4
+ // nothing at import: the first `startProcessMetrics()` declares the instruments.
5
+
6
+ import { type Clock, systemClock } from './clock';
7
+ import { finiteCount } from './finite-option';
8
+ import type { Counter, Gauge, Histogram } from './metrics';
9
+ import { counter, gauge, histogram } from './metrics';
10
+
11
+ /**
12
+ * One reading of the process, in bytes and seconds — everything `process` answers in microseconds.
13
+ * No object count and no collection count: `bun:jsc`'s `heapStats()` runs a full collection to
14
+ * answer (9 ms on an empty framework process, measured 2026-10-01), which a scrape must not cause.
15
+ */
16
+ export interface ProcessReading {
17
+ /** Resident set size: what the kernel charges the container for. */
18
+ readonly rss: number;
19
+ /** Bytes of JavaScript heap in use, garbage not yet collected included. */
20
+ readonly heapUsed: number;
21
+ /** Bytes the engine has reserved for the heap, used or not. */
22
+ readonly heapTotal: number;
23
+ /** Bytes held outside the heap on behalf of heap objects: buffers, strings, compiled code. */
24
+ readonly external: number;
25
+ /** User plus system CPU seconds consumed since the process started. */
26
+ readonly cpuSeconds: number;
27
+ /** Seconds since the process started — what places `process_start_time_seconds`. */
28
+ readonly uptimeSeconds: number;
29
+ }
30
+
31
+ /**
32
+ * How often the event loop is asked how late it is, and the only work this module does unasked.
33
+ * One timer wake a second: measured at 0.12 millicore on top of the 1.8 an idle Bun process
34
+ * already spends (20 s of `process.cpuUsage()`, sampler on against off, 2026-10-01).
35
+ */
36
+ export const EVENT_LOOP_SAMPLE_MS = 1000;
37
+
38
+ /** A stalled loop is the signal, so the buckets run from a millisecond to a frozen process. */
39
+ export const EVENT_LOOP_LAG_BOUNDS: readonly number[] = Object.freeze([
40
+ 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 10,
41
+ ]);
42
+
43
+ /** The default reading: `process.memoryUsage()` and `process.cpuUsage()`, ~12 µs together. */
44
+ export function readProcess(): ProcessReading {
45
+ const memory = process.memoryUsage();
46
+ const cpu = process.cpuUsage();
47
+ return {
48
+ rss: memory.rss,
49
+ heapUsed: memory.heapUsed,
50
+ heapTotal: memory.heapTotal,
51
+ external: memory.external,
52
+ cpuSeconds: (cpu.user + cpu.system) / 1_000_000,
53
+ uptimeSeconds: process.uptime(),
54
+ };
55
+ }
56
+
57
+ export interface ProcessMetricsOptions {
58
+ /** What this process is: a `ROLE`, or `x dev`'s several joined. The `process_info` label. */
59
+ readonly role: string;
60
+ /** Defaults to `readProcess`. Injected by a test. */
61
+ readonly read?: (() => ProcessReading) | undefined;
62
+ readonly clock?: Clock | undefined;
63
+ /** The sampler's timer. Injected by a test; the default is an unref'd `setInterval`. */
64
+ readonly every?: ((tick: () => void, intervalMs: number) => () => void) | undefined;
65
+ readonly sampleMs?: number | undefined;
66
+ }
67
+
68
+ interface Instruments {
69
+ readonly cpu: Counter;
70
+ readonly lag: Histogram;
71
+ readonly info: Gauge;
72
+ }
73
+
74
+ // The live source. The observers below are declared ONCE and read through it, because a gauge
75
+ // redeclared with a different `observe` is refused (`X_METRIC_NAME_INVALID`) and a process may
76
+ // start, stop and start this again — every `x dev` reload does.
77
+ let source: (() => ProcessReading) | undefined;
78
+ let startedAtSeconds = 0;
79
+ let instruments: Instruments | undefined;
80
+ // What the counter already holds, kept across a stop and a start so neither loses the CPU spent
81
+ // before the first sample — a boot is where most of it goes — nor counts it twice.
82
+ let cpuCounted = 0;
83
+ // The start that owns `source` and the sampler, compared by identity: two starts may share a
84
+ // reader (the default one), so the reader cannot say which start a stop belongs to.
85
+ let active: { readonly stopTimer: () => void } | undefined;
86
+ // The role `process_info` last said 1 for, so a start under another role sets it back to 0 —
87
+ // one process, one role, even across a stop and a start.
88
+ let infoRole: string | undefined;
89
+
90
+ /** 0 while stopped: a scrape between a stop and a start reads a flat line, never a throw. */
91
+ const observed = (pick: (reading: ProcessReading) => number) => (): number =>
92
+ source === undefined ? 0 : pick(source());
93
+
94
+ function declare(): Instruments {
95
+ if (instruments !== undefined) return instruments;
96
+ gauge('process_resident_memory_bytes', {
97
+ unit: 'By',
98
+ description: 'Resident set size of this process',
99
+ observe: observed((reading) => reading.rss),
100
+ });
101
+ gauge('process_heap_used_bytes', {
102
+ unit: 'By',
103
+ description: 'JavaScript heap in use, garbage not yet collected included',
104
+ observe: observed((reading) => reading.heapUsed),
105
+ });
106
+ gauge('process_heap_total_bytes', {
107
+ unit: 'By',
108
+ description: 'JavaScript heap reserved by the engine',
109
+ observe: observed((reading) => reading.heapTotal),
110
+ });
111
+ gauge('process_external_memory_bytes', {
112
+ unit: 'By',
113
+ description: 'Memory held outside the heap for heap objects: buffers, strings, compiled code',
114
+ observe: observed((reading) => reading.external),
115
+ });
116
+ gauge('process_start_time_seconds', {
117
+ unit: 's',
118
+ description: 'When this process started, in seconds since the Unix epoch',
119
+ observe: () => startedAtSeconds,
120
+ });
121
+ instruments = {
122
+ cpu: counter('process_cpu_seconds_total', {
123
+ unit: 's',
124
+ description: 'User and system CPU time consumed by this process',
125
+ }),
126
+ lag: histogram('process_event_loop_lag_seconds', {
127
+ unit: 's',
128
+ description: `How late the event loop ran a ${String(EVENT_LOOP_SAMPLE_MS)}ms timer, sampled once per interval`,
129
+ bounds: EVENT_LOOP_LAG_BOUNDS,
130
+ }),
131
+ info: gauge('process_info', {
132
+ unit: '1',
133
+ description: 'Always 1; the labels say which role this process runs',
134
+ }),
135
+ };
136
+ return instruments;
137
+ }
138
+
139
+ const unrefInterval = (tick: () => void, intervalMs: number): (() => void) => {
140
+ const timer = setInterval(tick, intervalMs);
141
+ timer.unref();
142
+ return () => clearInterval(timer);
143
+ };
144
+
145
+ /** Test-only: forget the CPU already counted, beside `resetMetrics()` dropping the counter. */
146
+ export function resetProcessMetrics(): void {
147
+ active?.stopTimer();
148
+ active = undefined;
149
+ source = undefined;
150
+ cpuCounted = 0;
151
+ infoRole = undefined;
152
+ }
153
+
154
+ /**
155
+ * Starts the process series and the one sampler behind two of them, and answers the stop. Called
156
+ * by whatever opens the scrape listener, so every role that can be scraped reports itself.
157
+ *
158
+ * CPU is a COUNTER fed by the sampler rather than a gauge read at scrape time: `rate()` over a
159
+ * counter survives a restart, and an observed gauge named `_total` would lie about its type.
160
+ */
161
+ export function startProcessMetrics(options: ProcessMetricsOptions): () => void {
162
+ const clock = options.clock ?? systemClock;
163
+ const read = options.read ?? readProcess;
164
+ const sampleMs = finiteCount(
165
+ 'startProcessMetrics',
166
+ 'sampleMs',
167
+ options.sampleMs ?? EVENT_LOOP_SAMPLE_MS,
168
+ 1,
169
+ );
170
+ const declared = declare();
171
+ // A start that replaces a live one ends its sampler: two timers would sample twice.
172
+ active?.stopTimer();
173
+ source = read;
174
+ // Uptime subtracted from now: the process started before this module was asked.
175
+ startedAtSeconds = Math.floor(clock.now().getTime() / 1000 - read().uptimeSeconds);
176
+ if (infoRole !== undefined && infoRole !== options.role) {
177
+ declared.info.record(0, { role: infoRole });
178
+ }
179
+ infoRole = options.role;
180
+ declared.info.record(1, { role: options.role });
181
+
182
+ const countCpu = (): void => {
183
+ const cpu = read().cpuSeconds;
184
+ // Monotonic by construction, and guarded anyway: a counter refuses a negative delta.
185
+ if (cpu > cpuCounted) declared.cpu.add(cpu - cpuCounted);
186
+ cpuCounted = Math.max(cpu, cpuCounted);
187
+ };
188
+ countCpu();
189
+ let due = clock.monotonic() + sampleMs;
190
+ const stopTimer = (options.every ?? unrefInterval)(() => {
191
+ const now = clock.monotonic();
192
+ // Late by this much; a timer that fires early reports no lag rather than a negative one.
193
+ declared.lag.record(Math.max(0, now - due) / 1000);
194
+ due = now + sampleMs;
195
+ countCpu();
196
+ }, sampleMs);
197
+
198
+ const owner = { stopTimer };
199
+ active = owner;
200
+ return () => {
201
+ stopTimer();
202
+ if (active !== owner) return;
203
+ active = undefined;
204
+ source = undefined;
205
+ };
206
+ }
@@ -0,0 +1,76 @@
1
+ // Single responsibility: the three X_SEAL_* refusals and the errors that carry them. Three codes
2
+ // rather than one "it will not open" because each names a different fact — no key at all, a key
3
+ // this process was not given, a value that did not authenticate — and a different thing to do
4
+ // next. No error here carries a key, a plaintext or the sealed string itself. Titles live in
5
+ // `core-error-codes.ts` and the retry class in `error-retry.ts`, so this module runs nothing at
6
+ // import and stays out of every bundle that does not seal.
7
+
8
+ import { renderCauseValue } from './error-render';
9
+ import { UltimateError } from './errors';
10
+
11
+ /**
12
+ * No key in the environment and none on disk. Never a pass-through: storing the plaintext because
13
+ * the key was missing is the failure a sealed column exists to prevent, and it would look healthy.
14
+ */
15
+ export class SealKeyMissingError extends UltimateError {
16
+ constructor(input: { envVar: string; keyPath: string }) {
17
+ super({
18
+ code: 'X_SEAL_KEY_MISSING',
19
+ cause: `${input.envVar} is unset and ${input.keyPath} does not exist, so there is no master key to seal or open a value with`,
20
+ fix: 'x secrets init # or, where the key already exists: export ULTIMATE_SECRETS_KEY="$(cat .secrets.key)"',
21
+ meta: { keyPath: input.keyPath },
22
+ });
23
+ }
24
+ }
25
+
26
+ /**
27
+ * The value names a key id the ring does not hold — a rotation whose retired key was dropped
28
+ * before the re-seal finished, or a value copied from another environment. `keyId` is matched
29
+ * against 16 hex characters before it gets here, so it is safe to print; `declared` is computed.
30
+ */
31
+ export class SealKeyUnknownError extends UltimateError {
32
+ constructor(input: { keyId: string; declared: readonly string[] }) {
33
+ super({
34
+ code: 'X_SEAL_KEY_UNKNOWN',
35
+ cause: `the sealed value names master key ${input.keyId}, which is not among the declared keys: ${input.declared.join(', ')} (current first)`,
36
+ // The variable is written out, not interpolated: `x errors explain` prints this line with no
37
+ // instance behind it. `seal.test.ts` holds it equal to `SECRETS_RETIRED_KEYS_ENV`.
38
+ fix: `x secrets edit # put the retired key back in ULTIMATE_SECRETS_RETIRED_KEYS (64 hex characters, comma-separated) and keep it there until the re-seal backfill() has finished`,
39
+ meta: { keyId: input.keyId, declared: [...input.declared] },
40
+ });
41
+ }
42
+ }
43
+
44
+ export type SealInvalidReason = 'malformed' | 'unauthenticated';
45
+
46
+ /**
47
+ * Either the string is not a sealed value at all, or the tag rejected it. AEAD cannot tell a wrong
48
+ * purpose from changed bytes — both are "the tag did not verify" — so the cause names both rather
49
+ * than guessing one and sending the reader after the wrong thing.
50
+ */
51
+ export class SealInvalidError extends UltimateError {
52
+ constructor(
53
+ input:
54
+ | { reason: 'malformed'; purpose?: string | undefined; length: number }
55
+ | { reason: 'unauthenticated'; purpose: string; keyId: string },
56
+ ) {
57
+ const purpose =
58
+ input.purpose === undefined ? '' : ` for purpose ${renderCauseValue(input.purpose)}`;
59
+ super({
60
+ code: 'X_SEAL_INVALID',
61
+ cause:
62
+ input.reason === 'malformed'
63
+ ? `a ${input.length}-character string read${purpose} is not a sealed value (x1.<keyId>.<iv>.<ciphertext>) — it was never sealed, or it was truncated`
64
+ : `the value did not authenticate under master key ${input.keyId}${purpose}: it was sealed for a different purpose, or its bytes changed after it was sealed — AES-GCM cannot tell the two apart`,
65
+ // ONE literal covering both conditions, so `x errors explain` prints it without an instance.
66
+ // A string that was never sealed is almost always a column sealed AFTER rows were written,
67
+ // and no key fixes that: there is no reading of an unsealed value, so the rows are migrated.
68
+ fix: 'x secrets show --json # a value that failed its tag: confirms the key id in force, and the value must be re-entered. A value that was never sealed — a column sealed after rows were written — is migrated: add a NEW .sealed() column, copy into it with a backfill(), drop the old one',
69
+ meta: {
70
+ reason: input.reason,
71
+ ...(input.purpose === undefined ? {} : { purpose: input.purpose }),
72
+ ...(input.reason === 'unauthenticated' ? { keyId: input.keyId } : {}),
73
+ },
74
+ });
75
+ }
76
+ }
@@ -0,0 +1,121 @@
1
+ // Single responsibility: the key ring `seal()` and `open()` work under. The CURRENT key is the one
2
+ // `x secrets` already manages — `ULTIMATE_SECRETS_KEY` first, `.secrets.key` second, through
3
+ // `findMasterKey`, no second variable. RETIRED keys are one more env var, which `x secrets rotate`
4
+ // writes into the committed file and `installSecrets()` carries into the process like any secret.
5
+
6
+ import { SealKeyMissingError } from './seal-errors';
7
+ import { importKey, masterKeyId, parseMasterKey } from './secrets';
8
+ import { findMasterKey, masterKeyPath, SECRETS_KEY_ENV } from './secrets-store';
9
+
10
+ /**
11
+ * Master keys that no longer seal but still open: 64 hex characters each, separated by commas or
12
+ * whitespace. A secret like any other — it lives in `secrets.enc.json` under this name, sealed by
13
+ * the current key, so a deploy is handed ONE key and the ring travels in the repository.
14
+ */
15
+ export const SECRETS_RETIRED_KEYS_ENV = 'ULTIMATE_SECRETS_RETIRED_KEYS';
16
+
17
+ type EnvRecord = Record<string, string | undefined>;
18
+
19
+ /** Where the keys are read from. The same two fields, with the same defaults, as `installSecrets`. */
20
+ export interface SealKeySource {
21
+ /** The app root holding `.secrets.key`. Defaults to the process's working directory. */
22
+ readonly root?: string | undefined;
23
+ /** Read for the current key and the retired ring. Defaults to `process.env`. */
24
+ readonly env?: EnvRecord | undefined;
25
+ }
26
+
27
+ export interface SealKey {
28
+ /** `masterKeyId`'s — the id a sealed string carries. */
29
+ readonly id: string;
30
+ readonly aes: CryptoKey;
31
+ /** HMAC key the deterministic IV is derived under; never the AES key itself. */
32
+ readonly mac: CryptoKey;
33
+ }
34
+
35
+ export interface SealKeyRing {
36
+ readonly current: SealKey;
37
+ /** Current first, then retired in declaration order. */
38
+ readonly keys: readonly SealKey[];
39
+ /** The same keys by the id a sealed string names — built once, read on every `open()`. */
40
+ readonly byId: ReadonlyMap<string, SealKey>;
41
+ }
42
+
43
+ const encoder = new TextEncoder();
44
+ const MAC_DOMAIN = encoder.encode('ultimate.seal.iv.v1');
45
+
46
+ /**
47
+ * The MAC key is DERIVED from the master key under a fixed label rather than being the master key:
48
+ * one key used for both AES-GCM and HMAC has no known break, and no proof either.
49
+ */
50
+ async function sealKey(hex: string, at: string, variable?: string): Promise<SealKey> {
51
+ const raw = parseMasterKey(hex, at, variable);
52
+ const hmac = { name: 'HMAC', hash: 'SHA-256' } as const;
53
+ const master = await crypto.subtle.importKey('raw', raw, hmac, false, ['sign']);
54
+ const derived = await crypto.subtle.sign('HMAC', master, MAC_DOMAIN);
55
+ return {
56
+ id: await masterKeyId(raw),
57
+ aes: await importKey(raw),
58
+ mac: await crypto.subtle.importKey('raw', derived, hmac, false, ['sign']),
59
+ };
60
+ }
61
+
62
+ /** The retired ring as written: hex entries, in order, blanks dropped. Nothing is validated here. */
63
+ export function splitRetiredKeys(raw: string | undefined): readonly string[] {
64
+ return (raw ?? '').split(/[\s,]+/).filter((entry) => entry.length > 0);
65
+ }
66
+
67
+ async function buildRing(
68
+ current: string,
69
+ currentAt: string,
70
+ retired: string,
71
+ ): Promise<SealKeyRing> {
72
+ const first = await sealKey(current, currentAt);
73
+ const keys = [first];
74
+ for (const [index, hex] of splitRetiredKeys(retired).entries()) {
75
+ // A malformed entry is refused by position, never skipped: a ring that silently lost a key
76
+ // surfaces later as X_SEAL_KEY_UNKNOWN on a row, far from the edit that caused it.
77
+ const next = await sealKey(
78
+ hex,
79
+ `${SECRETS_RETIRED_KEYS_ENV} (entry ${index + 1})`,
80
+ SECRETS_RETIRED_KEYS_ENV,
81
+ );
82
+ if (!keys.some((key) => key.id === next.id)) keys.push(next);
83
+ }
84
+ return { current: first, keys, byId: new Map(keys.map((key) => [key.id, key])) };
85
+ }
86
+
87
+ // The last ring, kept by the exact strings it was built from. Importing a key is three WebCrypto
88
+ // calls and a list read opens hundreds of values, so the ring is built once — but it is keyed by
89
+ // its SOURCE, never by time or by process, so a rotated key file or a changed variable is a new
90
+ // ring on the very next call. Only a resolved ring is kept; a refusal is recomputed.
91
+ let memo: { readonly source: string; readonly ring: Promise<SealKeyRing> } | undefined;
92
+
93
+ /** The ring in force now, or `X_SEAL_KEY_MISSING`. A malformed key is `X_SECRETS_KEY_INVALID`. */
94
+ export function resolveSealKeys(source: SealKeySource = {}): Promise<SealKeyRing> {
95
+ const root = source.root ?? process.cwd();
96
+ const env = source.env ?? (process.env as EnvRecord);
97
+ const found = findMasterKey(root, env);
98
+ if (found === undefined) {
99
+ return Promise.reject(
100
+ new SealKeyMissingError({ envVar: SECRETS_KEY_ENV, keyPath: masterKeyPath(root) }),
101
+ );
102
+ }
103
+ const retired = env[SECRETS_RETIRED_KEYS_ENV] ?? '';
104
+ const key = `${found.hex}\n${retired}`;
105
+ if (memo?.source === key) return memo.ring;
106
+ const ring = buildRing(found.hex, found.at, retired);
107
+ const entry = { source: key, ring };
108
+ memo = entry;
109
+ ring.catch(() => {
110
+ if (memo === entry) memo = undefined;
111
+ });
112
+ return ring;
113
+ }
114
+
115
+ /** The ids a sealed value may name right now. Safe to print: an id is not a key. */
116
+ export async function sealKeyIds(
117
+ source: SealKeySource = {},
118
+ ): Promise<{ readonly current: string; readonly retired: readonly string[] }> {
119
+ const ring = await resolveSealKeys(source);
120
+ return { current: ring.current.id, retired: ring.keys.slice(1).map((key) => key.id) };
121
+ }
package/src/seal.ts ADDED
@@ -0,0 +1,259 @@
1
+ // Single responsibility: seal ONE value under the app's master key and open it back. The wire form
2
+ // is one string, `x1.<keyId>.<iv>.<ciphertext+tag>`, AES-256-GCM through WebCrypto with a REQUIRED
3
+ // purpose bound in as additional authenticated data. `secrets.ts` is the env envelope — a file of
4
+ // many values; this is the per-value form a column or a stored session holds.
5
+
6
+ import { assert } from './assert';
7
+ import { UltimateError } from './errors';
8
+ import { SealInvalidError, SealKeyUnknownError } from './seal-errors';
9
+ import type { SealKey, SealKeyRing, SealKeySource } from './seal-keys';
10
+ import { resolveSealKeys } from './seal-keys';
11
+ import {
12
+ decodeBase64,
13
+ encodeBase64,
14
+ SECRETS_ALG,
15
+ SECRETS_IV_BYTES,
16
+ SECRETS_TAG_BYTES,
17
+ } from './secrets';
18
+
19
+ /** The format tag. A string without it was never sealed by this function. */
20
+ export const SEAL_VERSION = 'x1';
21
+
22
+ /** What a value was sealed FOR — `entity:connections.password`, `scrape-session`. Never optional. */
23
+ export interface SealPurposeOptions extends SealKeySource {
24
+ /** Bound into the tag: a value sealed for one purpose does not open as another. */
25
+ readonly purpose: string;
26
+ /**
27
+ * A ring already resolved — `await resolveSealKeys()` — for a caller sealing or opening MANY
28
+ * values in one operation. Without it every call finds the master key again (an environment
29
+ * read, or a file read in a checkout); with it a 500-row page asks once. Never held past the
30
+ * operation: a ring kept across requests would outlive a rotation.
31
+ */
32
+ readonly keys?: SealKeyRing | undefined;
33
+ }
34
+
35
+ const ringFor = (options: SealPurposeOptions): Promise<SealKeyRing> | SealKeyRing =>
36
+ options.keys ?? resolveSealKeys(options);
37
+
38
+ export interface SealOptions extends SealPurposeOptions {
39
+ /**
40
+ * Derive the IV from the purpose and the plaintext instead of drawing it, so equal values seal
41
+ * to equal strings and a column can be looked up by equality. It REVEALS EQUALITY to anyone who
42
+ * can read the stored strings: two rows holding the same value are visibly the same. Never use
43
+ * it for a low-entropy value (a boolean, a status, a PIN, a date of birth) — the handful of
44
+ * possible ciphertexts is a lookup table. Under a rotation the same value seals differently per
45
+ * key; `sealAll` returns every candidate.
46
+ */
47
+ readonly deterministic?: boolean | undefined;
48
+ }
49
+
50
+ // 12 bytes are exactly 16 unpadded base64url characters; a 16-byte tag alone is 22.
51
+ const SEALED = /^x1\.([0-9a-f]{16})\.([A-Za-z0-9_-]{16})\.([A-Za-z0-9_-]{22,})$/;
52
+
53
+ const encoder = new TextEncoder();
54
+
55
+ const toUrl = (bytes: Uint8Array<ArrayBuffer>): string =>
56
+ encodeBase64(bytes).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
57
+
58
+ function fromUrl(text: string): Uint8Array<ArrayBuffer> {
59
+ const standard = text.replaceAll('-', '+').replaceAll('_', '/');
60
+ return decodeBase64(standard.padEnd(standard.length + ((4 - (standard.length % 4)) % 4), '='));
61
+ }
62
+
63
+ /** A fresh copy either way, so a caller's buffer is never the one WebCrypto is handed. */
64
+ const bytesOf = (plaintext: string | Uint8Array): Uint8Array<ArrayBuffer> =>
65
+ typeof plaintext === 'string' ? encoder.encode(plaintext) : new Uint8Array(plaintext);
66
+
67
+ /**
68
+ * The bytes the tag covers besides the ciphertext: the format, the algorithm, the key id and the
69
+ * purpose. A string moved to another column, or relabelled with another key id, fails the tag.
70
+ */
71
+ const additionalData = (keyId: string, purpose: string): Uint8Array<ArrayBuffer> =>
72
+ encoder.encode(
73
+ `ultimate.seal|${SEAL_VERSION}|alg=${SECRETS_ALG}|kid=${keyId}|purpose=${purpose}`,
74
+ );
75
+
76
+ function requirePurpose(purpose: unknown): asserts purpose is string {
77
+ assert(
78
+ typeof purpose === 'string' && purpose.length > 0,
79
+ 'seal() and open() were called without a purpose, and the purpose is what stops a value sealed for one column opening as another',
80
+ "pass purpose: '<what the value is for>' — seal(value, { purpose: 'entity:<table>.<column>' })",
81
+ );
82
+ }
83
+
84
+ /**
85
+ * The deterministic IV: HMAC-SHA-256 over the length-prefixed purpose and the plaintext, cut to
86
+ * GCM's 96 bits. Length-prefixed so (`a`, `bc`) and (`ab`, `c`) are different inputs. A repeated
87
+ * (key, IV) pair then means a repeated (purpose, plaintext) — the same ciphertext, which is the
88
+ * mode's stated leak and not a nonce reuse across two messages.
89
+ */
90
+ async function derivedIv(
91
+ key: SealKey,
92
+ purpose: string,
93
+ plaintext: Uint8Array<ArrayBuffer>,
94
+ ): Promise<Uint8Array<ArrayBuffer>> {
95
+ const label = encoder.encode(purpose);
96
+ const material = new Uint8Array(4 + label.length + plaintext.length);
97
+ new DataView(material.buffer).setUint32(0, label.length);
98
+ material.set(label, 4);
99
+ material.set(plaintext, 4 + label.length);
100
+ const mac = await crypto.subtle.sign('HMAC', key.mac, material);
101
+ return new Uint8Array(mac).slice(0, SECRETS_IV_BYTES);
102
+ }
103
+
104
+ async function sealUnder(
105
+ key: SealKey,
106
+ plaintext: Uint8Array<ArrayBuffer>,
107
+ purpose: string,
108
+ deterministic: boolean,
109
+ ): Promise<string> {
110
+ const iv = deterministic
111
+ ? await derivedIv(key, purpose, plaintext)
112
+ : crypto.getRandomValues(new Uint8Array(SECRETS_IV_BYTES));
113
+ const sealed = await crypto.subtle.encrypt(
114
+ {
115
+ name: 'AES-GCM',
116
+ iv,
117
+ additionalData: additionalData(key.id, purpose),
118
+ tagLength: SECRETS_TAG_BYTES * 8,
119
+ },
120
+ key.aes,
121
+ plaintext,
122
+ );
123
+ return `${SEAL_VERSION}.${key.id}.${toUrl(iv)}.${toUrl(new Uint8Array(sealed))}`;
124
+ }
125
+
126
+ /**
127
+ * Seal one value under the CURRENT master key — the one `x secrets` manages, found where
128
+ * `installSecrets()` finds it.
129
+ *
130
+ * ```ts
131
+ * const stored = await seal(password, { purpose: 'entity:connections.password' });
132
+ * const password = await openText(stored, { purpose: 'entity:connections.password' });
133
+ * ```
134
+ *
135
+ * `X_SEAL_KEY_MISSING` when there is no key: the plaintext is never returned in its place.
136
+ */
137
+ export async function seal(plaintext: string | Uint8Array, options: SealOptions): Promise<string> {
138
+ requirePurpose(options.purpose);
139
+ const ring = await ringFor(options);
140
+ return sealUnder(
141
+ ring.current,
142
+ bytesOf(plaintext),
143
+ options.purpose,
144
+ options.deterministic === true,
145
+ );
146
+ }
147
+
148
+ /**
149
+ * The DETERMINISTIC seal of one value under every declared key, current first. During a rotation
150
+ * a row written before it holds the old key's string and a row written after holds the new one's,
151
+ * so an equality lookup has to match either: `where column in (…sealAll(value))`. Outside a
152
+ * rotation this is one string. Uniqueness cannot be held across keys — one value has two forms.
153
+ */
154
+ export async function sealAll(
155
+ plaintext: string | Uint8Array,
156
+ options: SealPurposeOptions,
157
+ ): Promise<readonly string[]> {
158
+ requirePurpose(options.purpose);
159
+ const ring = await ringFor(options);
160
+ const bytes = bytesOf(plaintext);
161
+ return Promise.all(ring.keys.map((key) => sealUnder(key, bytes, options.purpose, true)));
162
+ }
163
+
164
+ interface SealedParts {
165
+ readonly keyId: string;
166
+ readonly iv: string;
167
+ readonly body: string;
168
+ }
169
+
170
+ function partsOf(value: unknown): SealedParts | undefined {
171
+ const match = typeof value === 'string' ? SEALED.exec(value) : null;
172
+ const [keyId, iv, body] = [match?.[1], match?.[2], match?.[3]];
173
+ if (keyId === undefined || iv === undefined || body === undefined) return undefined;
174
+ // No whole number of bytes encodes to 4n+1 base64 characters: a truncated write, not a value.
175
+ return body.length % 4 === 1 ? undefined : { keyId, iv, body };
176
+ }
177
+
178
+ const malformed = (sealed: unknown, purpose?: string): SealInvalidError =>
179
+ new SealInvalidError({
180
+ reason: 'malformed',
181
+ purpose,
182
+ length: typeof sealed === 'string' ? sealed.length : 0,
183
+ });
184
+
185
+ /**
186
+ * Whether a value has the SHAPE of a sealed string. Not a claim that it opens: it exists so a
187
+ * caller migrating a plaintext column can tell an unsealed legacy row from a sealed one without
188
+ * attempting a decryption — `open()` itself never falls back to the raw string.
189
+ */
190
+ export function isSealed(value: unknown): value is string {
191
+ return partsOf(value) !== undefined;
192
+ }
193
+
194
+ /** The id of the key a sealed string names — what a re-seal `backfill()` compares to the current. */
195
+ export function sealedKeyId(sealed: string): string {
196
+ const parts = partsOf(sealed);
197
+ if (parts === undefined) throw malformed(sealed);
198
+ return parts.keyId;
199
+ }
200
+
201
+ /**
202
+ * Open a sealed string to its bytes. Three refusals, in the order the facts become knowable: the
203
+ * string is not a sealed value (`X_SEAL_INVALID`), it names a key this process does not declare
204
+ * (`X_SEAL_KEY_UNKNOWN` — read off the string before any decryption is attempted, so a rotation
205
+ * is never reported as tampering), or the tag rejected it under the purpose given
206
+ * (`X_SEAL_INVALID`).
207
+ */
208
+ export async function open(
209
+ sealed: string,
210
+ options: SealPurposeOptions,
211
+ ): Promise<Uint8Array<ArrayBuffer>> {
212
+ requirePurpose(options.purpose);
213
+ const parts = partsOf(sealed);
214
+ if (parts === undefined) throw malformed(sealed, options.purpose);
215
+ const { keyId, iv, body } = parts;
216
+ const ring = await ringFor(options);
217
+ // A lookup by the id the string names. An id is public — it is in every sealed value — and a
218
+ // `Map` says so: nothing here is compared byte by byte against a secret.
219
+ const key = ring.byId.get(keyId);
220
+ if (key === undefined) {
221
+ throw new SealKeyUnknownError({
222
+ keyId,
223
+ declared: ring.keys.map((one) => one.id),
224
+ });
225
+ }
226
+ try {
227
+ const plaintext = await crypto.subtle.decrypt(
228
+ {
229
+ name: 'AES-GCM',
230
+ iv: fromUrl(iv),
231
+ additionalData: additionalData(keyId, options.purpose),
232
+ tagLength: SECRETS_TAG_BYTES * 8,
233
+ },
234
+ key.aes,
235
+ fromUrl(body),
236
+ );
237
+ return new Uint8Array(plaintext);
238
+ } catch {
239
+ // No `sourceError`, for `openSecrets`' reason: WebCrypto's OperationError says nothing more.
240
+ throw new SealInvalidError({ reason: 'unauthenticated', purpose: options.purpose, keyId });
241
+ }
242
+ }
243
+
244
+ /**
245
+ * `open()` for a value that was sealed from a string. Bytes that are not UTF-8 are refused rather
246
+ * than decoded with replacement characters: a caller would store the repaired text back.
247
+ */
248
+ export async function openText(sealed: string, options: SealPurposeOptions): Promise<string> {
249
+ const bytes = await open(sealed, options);
250
+ try {
251
+ return new TextDecoder('utf-8', { fatal: true }).decode(bytes);
252
+ } catch {
253
+ throw new UltimateError({
254
+ code: 'X_INVARIANT',
255
+ cause: `the value opened to ${bytes.length} byte(s) that are not UTF-8 text, so it was sealed from bytes and openText() cannot return it`,
256
+ fix: 'call open() instead of openText() for a value sealed from a Uint8Array',
257
+ });
258
+ }
259
+ }