feat(config): optional corescopeUrl, normalized to an origin

Falls back to the origin of resolveUrl; both empty turns the account
feature off. Runtime config only, nothing hardcoded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
efitenandClaude Opus 5.5 committed 2026-10-09 10:52:35 +02:00
1 parent 9b053d6537
commit cea751fa7c
2 files changed
+54 -1

No files matched your search

+20
View File
@@ -58,6 +58,21 @@ function positiveSeconds(value, fallback) {
return Number.isFinite(n) && n >= 0 ? n : fallback;
}
// originOf returns scheme://host[:port] of an http(s) URL, or '' for anything else.
// An origin never carries a path or a trailing slash, so `origin + '/api/…'` is always
// well-formed. Used for corescopeUrl, which the account feature (src/account.js)
// binds its token to.
export function originOf(value) {
const s = String(value ?? '').trim();
if (!s) return '';
try {
const u = new URL(s);
return u.protocol === 'https:' || u.protocol === 'http:' ? u.origin : '';
} catch (e) {
return '';
}
}
export function normalizeConfig(raw) {
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Error('config.json: expected a JSON object');
const c = {
@@ -65,6 +80,11 @@ export function normalizeConfig(raw) {
mqttUsername: String(raw.mqttUsername || '').trim(),
mqttPassword: raw.mqttPassword == null ? '' : String(raw.mqttPassword),
resolveUrl: String(raw.resolveUrl || '').trim(),
// CoreScope's own origin, for login and companion linking (src/account.js).
// Optional: absent or unusable falls back to the origin of resolveUrl, and both
// empty turns the account feature off. Normalized here so nothing downstream ever
// builds a URL out of a path or a trailing slash.
corescopeUrl: originOf(raw.corescopeUrl) || originOf(raw.resolveUrl),
// Absent → FEATURE_DEFAULTS; present → coerced. See FEATURE_DEFAULTS above for
// why the two logging flags and the transmitting one differ.
fullRfLog: featureEnabled(raw, 'fullRfLog'),
+34 -1
View File
@@ -2,7 +2,7 @@
// Run: node --test
import { test } from 'node:test';
import assert from 'node:assert';
import { normalizeConfig, featureEnabled } from '../src/config.js';
import { normalizeConfig, featureEnabled, originOf } from '../src/config.js';
import {
DEFAULT_ASK_GAP_MS, DEFAULT_TARGET_GAP_MS, DEFAULT_MAX_ASKS, DEFAULT_FORGET_MS,
DEFAULT_BONUS_SNR_DB,
@@ -209,3 +209,36 @@ test('an unusable region pacing value falls back to the default, never to no lim
assert.equal(c.regionMaxAsks, DEFAULT_MAX_ASKS);
assert.equal(c.regionTargetGapSec, DEFAULT_TARGET_GAP_MS / 1000);
});
// --- corescopeUrl: the CoreScope origin the account feature talks to ----------
test('corescopeUrl is normalized to an origin: no path, no trailing slash', () => {
const base = { mqttUrl: 'wss://b/ws' };
assert.strictEqual(normalizeConfig({ ...base, corescopeUrl: 'https://corescope.example/' }).corescopeUrl, 'https://corescope.example');
assert.strictEqual(normalizeConfig({ ...base, corescopeUrl: ' https://corescope.example/some/path?q=1 ' }).corescopeUrl, 'https://corescope.example');
assert.strictEqual(normalizeConfig({ ...base, corescopeUrl: 'http://localhost:3000/' }).corescopeUrl, 'http://localhost:3000');
});
test('corescopeUrl falls back to the origin of resolveUrl when absent', () => {
const c = normalizeConfig({ mqttUrl: 'wss://b/ws', resolveUrl: 'https://cs.example:8443/api/nodes/resolve' });
assert.strictEqual(c.corescopeUrl, 'https://cs.example:8443');
});
test('an unusable corescopeUrl also falls back to resolveUrl, never to garbage', () => {
const r = 'https://cs.example/api/nodes/resolve';
assert.strictEqual(normalizeConfig({ mqttUrl: 'wss://b/ws', resolveUrl: r, corescopeUrl: 'not a url' }).corescopeUrl, 'https://cs.example');
assert.strictEqual(normalizeConfig({ mqttUrl: 'wss://b/ws', resolveUrl: r, corescopeUrl: 'ftp://cs.example' }).corescopeUrl, 'https://cs.example');
});
test('corescopeUrl is empty — feature off — when both it and resolveUrl are empty', () => {
assert.strictEqual(normalizeConfig({ mqttUrl: 'wss://b/ws' }).corescopeUrl, '');
assert.strictEqual(normalizeConfig({ mqttUrl: 'wss://b/ws', corescopeUrl: '', resolveUrl: '' }).corescopeUrl, '');
});
test('originOf accepts only http(s) and returns "" for anything else', () => {
assert.strictEqual(originOf('https://a.example/x/'), 'https://a.example');
assert.strictEqual(originOf(''), '');
assert.strictEqual(originOf(undefined), '');
assert.strictEqual(originOf('wss://a.example/ws'), '');
assert.strictEqual(originOf('::'), '');
});