/** * Lets any Node service in this workspace receive AuthGuard's and * CarrierNode's current base URL(s) pushed from ProjectConfigurator, instead * of hardcoding them in its own env vars. Mirrors shared/backend/coreconfig * (Go) and shared/packages/python-backend/src/shared_backend/coreconfig.py * (Python) exactly - same Info shape, same Store semantics - translated to * Node idioms; keep all three in sync if any changes shape. See * ProjectConfigurator/README.md's "Pushing core service info to other * projects" section for the wire contract this module implements the * receiving end of. * * Framework-agnostic: this module has no HTTP server of its own. A service * wires the actual `PUT /api/core-services/internal` route in its own * framework (Express, plain `http`, ...), gated behind its own * SECRET check the same way its other internal-service * routes already are, decodes the JSON body with `Info.fromJSON`, and calls * `store.set(info)` - see `Store.set`'s doc comment for a minimal Express * example. */ /** * @typedef {{authGuardUrl?: string, carrierNodeUrl?: string, carrierNodeUrls?: string[]}} InfoLike */ export class Info { /** * @param {InfoLike} [fields] */ constructor({ authGuardUrl = '', carrierNodeUrl = '', carrierNodeUrls = [] } = {}) { this.authGuardUrl = authGuardUrl; this.carrierNodeUrl = carrierNodeUrl; this.carrierNodeUrls = carrierNodeUrls; } /** Decodes the JSON body ProjectConfigurator's push carries (coreServiceInfo). */ static fromJSON(data) { const raw = data ?? {}; let urls = Array.isArray(raw.carrierNodeUrls) ? raw.carrierNodeUrls.map((u) => String(u ?? '').trim()).filter(Boolean) : []; if (urls.length === 0) { const single = String(raw.carrierNodeUrl ?? '').trim(); if (single) urls = [single]; } return new Info({ authGuardUrl: String(raw.authGuardUrl ?? '').trim(), carrierNodeUrl: urls[0] ?? '', carrierNodeUrls: urls, }); } toJSON() { return { authGuardUrl: this.authGuardUrl, carrierNodeUrl: this.carrierNodeUrl, carrierNodeUrls: [...this.carrierNodeUrls], }; } } function normalize(info) { let cleaned = (info.carrierNodeUrls ?? []).map((u) => String(u ?? '').trim()).filter(Boolean); if (cleaned.length === 0 && String(info.carrierNodeUrl ?? '').trim()) { cleaned = [String(info.carrierNodeUrl).trim()]; } return new Info({ authGuardUrl: String(info.authGuardUrl ?? '').trim(), carrierNodeUrl: cleaned[0] ?? '', carrierNodeUrls: cleaned, }); } export class Store { /** * Holds the most recently pushed Info, defaulting to whatever fallback was * given (typically this service's own AUTHGUARD_URL/CARRIERNODE_URL env * vars, or "" now that they're optional) until ProjectConfigurator's first * push arrives - or forever, if this deployment isn't registered with a * ProjectConfigurator instance at all, in which case those env vars (if * set) keep working exactly as before. * @param {InfoLike} [fallback] */ constructor(fallback = {}) { this._info = normalize(fallback instanceof Info ? fallback : new Info(fallback)); this._onUpdate = null; } get() { return this._info; } get authGuardUrl() { return this._info.authGuardUrl; } get carrierNodeUrl() { return this._info.carrierNodeUrl; } get carrierNodeUrls() { return this._info.carrierNodeUrls; } /** * Registers fn to be called (synchronously, so keep it fast) with the new * Info after every set() - e.g. an AuthClient's setBaseUrl for a client * that isn't already reading through Store on every call. Only one * callback is kept; a second registration replaces the first. * @param {(info: Info) => void} fn */ onUpdate(fn) { this._onUpdate = fn; } /** * Replaces the current Info, field by field, ignoring a blank/empty field * so a partial push (e.g. only carrierNodeUrl(s) changed) never clobbers * the other value. Returns the resulting Info. Minimal Express usage: * * app.put('/api/core-services/internal', (req, res) => { * if (req.get('X-Internal-Auth') !== process.env.SECRET) { * return res.status(401).json({ error: 'internal service authentication required' }); * } * const info = coreConfigStore.set(Info.fromJSON(req.body)); * res.json({ ok: true, info: info.toJSON() }); * }); * * @param {InfoLike | Info} nextInfo * @returns {Info} */ set(nextInfo) { const next = normalize(nextInfo instanceof Info ? nextInfo : new Info(nextInfo)); if (next.authGuardUrl) { this._info.authGuardUrl = next.authGuardUrl; } if (next.carrierNodeUrls.length > 0) { this._info.carrierNodeUrls = next.carrierNodeUrls; this._info.carrierNodeUrl = next.carrierNodeUrls[0]; } if (this._onUpdate) { this._onUpdate(this._info); } return this._info; } }