56 lines
2.2 KiB
TypeScript
56 lines
2.2 KiB
TypeScript
// Small, stable primitives every module's code may need. No behaviour here changes when a module
|
|
// changes; that is the whole point of it living in the sdk.
|
|
//
|
|
// There was a symmetric seal()/unseal() here, for a provider to seal a credential to a key before
|
|
// writing it. It is gone: a provider is handed the credential the mesh minted and seals nothing
|
|
// (novox/hq ADR 0048), the mesh's own secret delivery is asymmetric and belongs to the host, and
|
|
// nothing else called it. The primitive left with the provisioner that was its only caller.
|
|
|
|
// --- semver ---
|
|
//
|
|
// Enough to compare and satisfy, no dependency spent on it. Pre-release and build metadata are
|
|
// ignored by design — the mesh pins by digest, and versions here order releases, nothing subtler.
|
|
|
|
export interface Version {
|
|
readonly major: number;
|
|
readonly minor: number;
|
|
readonly patch: number;
|
|
}
|
|
|
|
export function parseVersion(v: string): Version {
|
|
const m = /^v?(\d+)\.(\d+)\.(\d+)/.exec(v.trim());
|
|
if (!m) throw new Error(`not a version: ${v}`);
|
|
return { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]) };
|
|
}
|
|
|
|
/** -1, 0, 1 — a before b, equal, a after b. */
|
|
export function compareVersions(a: string, b: string): -1 | 0 | 1 {
|
|
const x = parseVersion(a);
|
|
const y = parseVersion(b);
|
|
for (const k of ["major", "minor", "patch"] as const) {
|
|
if (x[k] < y[k]) return -1;
|
|
if (x[k] > y[k]) return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// --- resolved env ---
|
|
//
|
|
// A module reads its own resolved environment through this, rather than reaching into process.env
|
|
// directly, so the one place that would change if resolution changes is here.
|
|
|
|
/** Read a resolved env var; throws if a required one is absent, which is the right failure. */
|
|
export function requireEnv(name: string, env: NodeJS.ProcessEnv = process.env): string {
|
|
const v = env[name];
|
|
if (v === undefined || v === "") {
|
|
throw new Error(`${name} is not set — the module was deployed without a value it requires`);
|
|
}
|
|
return v;
|
|
}
|
|
|
|
/** Read a resolved env var with a fallback. */
|
|
export function readEnv(name: string, fallback: string, env: NodeJS.ProcessEnv = process.env): string {
|
|
const v = env[name];
|
|
return v === undefined || v === "" ? fallback : v;
|
|
}
|