mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
01ae91c939
A new service-identity domain registers the installation with places.liketrek.com on its first Places request, in the background and behind a light scrypt proof of work, keeps the key encrypted in app_settings, renews it from day 60 through a daily job, drops it after three 401s in a row with up to six hours of jitter, and freezes it on a revocation until an admin starts over. TREK_SERVICES_KEY wins and turns registration off; TREK_SERVICES=off sends no key; a mirror set with TREK_PLACES_URL never registers and never sees the key, which goes only to the hosts in TREK_SERVICES_HOSTS over https. The Places client takes its headers from the identity port, sends a TREK/<version> User-Agent and keeps the instance token. A 401 or 403 does not open its breaker. GET /api/admin/service-identity reports the state, tier and today's allowance, asking the issuer at most every 20 seconds; POST .../regenerate starts a new identity.
134 lines
6.5 KiB
TypeScript
134 lines
6.5 KiB
TypeScript
import type { AppSettings } from '../entities/AppSettings.entity';
|
|
import { toRow, type AssertRowKeys } from './_shared/rows';
|
|
import { TrekRepository } from './_shared/trek-repository';
|
|
|
|
/** An `app_settings` row as the API emits it. */
|
|
export interface AppSettingsRow {
|
|
key: string | null;
|
|
value: string | null;
|
|
}
|
|
|
|
const _appSettingsRowKeys: AssertRowKeys<AppSettingsRow, AppSettings> = true;
|
|
|
|
export class AppSettingsRepository extends TrekRepository<AppSettings> {
|
|
/**
|
|
* `SELECT value FROM app_settings WHERE key = ?`
|
|
*
|
|
* `disableIdentityMap: true`, applied by the base class's default (Plan 3b
|
|
* interlude B — `_shared/trek-repository.ts`; supersedes Plan 3a's I1
|
|
* "`refresh: true` on every PK-only `findOne`" — see
|
|
* `Users.repository.ts`'s class-level docstring for the full mechanism and
|
|
* `.superpowers/sdd/2026-09-22-orm-phase3b/task-1-review.md` B1): the read
|
|
* is answered from a throwaway forked context that is cleared afterwards,
|
|
* so a raw/native write or `deleteValue` on the same key inside the same
|
|
* request is always seen, and the entity never lands in the request's
|
|
* identity map to become a stale pending change at a later `flush()`.
|
|
*/
|
|
async getValue(key: string): Promise<string | null> {
|
|
const row = await this.findOne({ key }, { fields: ['value'] });
|
|
return row?.value ?? null;
|
|
}
|
|
|
|
/**
|
|
* `SELECT key, value FROM app_settings WHERE key IN (?, ?, ...)` — a
|
|
* NULL-valued row is silently dropped from the returned Map, matching the
|
|
* legacy callers, which never distinguished "row present with a NULL
|
|
* value" from "no row" (both read as absent).
|
|
*/
|
|
async getValues(keys: string[]): Promise<Map<string, string>> {
|
|
const rows = await this.find({ key: { $in: keys } });
|
|
const values = new Map<string, string>();
|
|
for (const row of rows) {
|
|
if (row.key != null && row.value != null) values.set(row.key, row.value);
|
|
}
|
|
return values;
|
|
}
|
|
|
|
/**
|
|
* Upsert ruling: `app_settings` (`Migration20200101000000_baseline_schema.ts`)
|
|
* is `CREATE TABLE app_settings (key TEXT PRIMARY KEY, value TEXT)` and no
|
|
* later migration adds a column or a timestamp — the table has never carried
|
|
* anything beyond `key`/`value`. Every legacy call site across settings,
|
|
* addons and permissions is either `INSERT OR REPLACE INTO app_settings
|
|
* (key, value) VALUES (?, ?)` or `INSERT INTO app_settings (key, value)
|
|
* VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value` —
|
|
* with no other column whose reset by REPLACE could be observable, both
|
|
* spellings are exactly `em.upsert`'s job, with the sole unique field
|
|
* (`key`, the primary key) as the conflict target.
|
|
*
|
|
* The one difference from `INSERT OR REPLACE` — verified, unobservable
|
|
* here — is the row's implicit SQLite rowid: REPLACE deletes and
|
|
* reinserts (rowid moves), `em.upsert`'s `ON CONFLICT ... DO UPDATE`
|
|
* keeps the original row (rowid stays). `app_settings` has no `INTEGER
|
|
* PRIMARY KEY`, no `AUTOINCREMENT`, no trigger and no foreign key
|
|
* referencing it, so nothing here reads the rowid — but the next table
|
|
* converted off `INSERT OR REPLACE` that does have one of those needs its
|
|
* own check, not an assumption from this comment.
|
|
*/
|
|
async setValue(key: string, value: string): Promise<void> {
|
|
await this.upsert({ key, value }, { onConflictFields: ['key'], onConflictAction: 'merge' });
|
|
}
|
|
|
|
/**
|
|
* `SELECT key, value FROM app_settings WHERE key LIKE ?` — the caller
|
|
* passes the bare prefix; this appends the trailing `%` itself, matching
|
|
* every legacy call site (`'perm_%'`, `'default_user_setting_%'`).
|
|
*
|
|
* Escaping: neither the legacy `LIKE 'prefix%'` literals nor `$like`'s
|
|
* parameter binding here escape `%`/`_` in the prefix — both are plain
|
|
* SQL LIKE, where a bound parameter's `%`/`_` are wildcards exactly like a
|
|
* literal's are. The repository test proves the two behave identically for
|
|
* a prefix that itself contains `%`/`_`.
|
|
*/
|
|
async findByKeyPrefix(prefix: string): Promise<AppSettingsRow[]> {
|
|
const rows = await this.find({ key: { $like: `${prefix}%` } });
|
|
return rows.map((row) => toRow(row) as AppSettingsRow);
|
|
}
|
|
|
|
/**
|
|
* `INSERT INTO app_settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO NOTHING`:
|
|
* the first writer keeps the row, so several processes on one database can
|
|
* race to create a value and all read back the same one afterwards.
|
|
*/
|
|
async insertIfAbsent(key: string, value: string): Promise<void> {
|
|
await this.upsert({ key, value }, { onConflictFields: ['key'], onConflictAction: 'ignore' });
|
|
}
|
|
|
|
/** `DELETE FROM app_settings WHERE key = ?` */
|
|
async deleteValue(key: string): Promise<number> {
|
|
return this.nativeDelete({ key });
|
|
}
|
|
|
|
/**
|
|
* `SELECT COUNT(*) AS n FROM app_settings WHERE key IN (?, ?, ...)` (Plan
|
|
* 3i, storage's SR1 — `StorageRegistryService#seedFromFileOnce`'s
|
|
* seed-once boot guard). Counts every ROW whose key matches, including one
|
|
* whose `value` is NULL — unlike `getValues()`, which drops a NULL-valued
|
|
* row from its Map, this mirrors the legacy statement exactly: no WHERE on
|
|
* `value`, so a present-but-NULL row still counts as "already seeded."
|
|
*/
|
|
async countKeysPresent(keys: string[]): Promise<number> {
|
|
return this.count({ key: { $in: keys } });
|
|
}
|
|
|
|
/**
|
|
* `INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)` (Plan
|
|
* 3i, storage's SS1 — `StorageStatsService#scan`). The SAME "one row per
|
|
* key" write as `setValue()`'s `ON CONFLICT(key) DO UPDATE` upsert, but a
|
|
* genuinely DIFFERENT legacy SQL text (R4/R5's ruling: `app_settings` has
|
|
* two distinct upsert dialects across this program's converted call
|
|
* sites — preserve both as separately-named methods, never merged into one
|
|
* with a boolean/dialect flag, even where — as here — their observable
|
|
* behavior on this table is identical). Per `setValue()`'s own docstring,
|
|
* the only real difference between the two dialects (REPLACE deletes and
|
|
* reinserts, moving the SQLite rowid; `ON CONFLICT ... DO UPDATE` keeps the
|
|
* original row) is unobservable on `app_settings` (no `INTEGER PRIMARY
|
|
* KEY`/`AUTOINCREMENT`/trigger/FK references it) — this method exists so
|
|
* the repository's public surface keeps naming the legacy text 1:1, not
|
|
* because the two need different runtime behavior on this table.
|
|
*/
|
|
async upsertOrReplace(key: string, value: string): Promise<void> {
|
|
await this.upsert({ key, value }, { onConflictFields: ['key'], onConflictAction: 'merge' });
|
|
}
|
|
}
|