Ask a registry which store to install

The client had our store's config URL compiled into it, which meant a second store
— or anybody else's site — needed a release of this app. It now asks
`GET /api/stores` and installs what the site offers: one record and there is
nothing to decide, several and the setup screen shows a picker.

From a record the client works the rest out. `storeRepositoryUrl` gives the
`config.json` to read, and that file stays the authority on how the store behaves;
`catalogUrl` and `name` override its `store.base_url` and `store.name`, because the
registry is what says which catalog a store is *for*. The store id — which names
the store home and the folder games land in — comes from the repository name, so
`ttg-desktop-store` becomes `ttg`.

A repository with no `config.json` still installs: the engine merges whatever it
is handed onto its own defaults, so the client writes a three-field config and the
store behaves like the default one pointed at that catalog. That was worth having
rather than an error, and it is tested.

The registry address is now the single thing about a particular site left in the
client, and `STORES_API` overrides it — which is how this was tested, against a
local endpoint serving the same payload the site returns, with one store that has a
config and one that has not. Both installed; the engine listed all ten titles with
the synthesised config.

`npm run uitest` now passes on either outcome — the grid when a store is present,
the setup gate with a populated picker when there is none — and it reports both, so
the gate cannot silently regress into an empty screen. Run with the registry
unreachable it produces the retry gate, and fails, which is the honest verdict: a
client that cannot reach the registry cannot set anything up.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-18 13:13:36 +02:00
co-authored by Claude Opus 5
parent 85c6d33b05
commit cb8a28b156
10 changed files with 276 additions and 55 deletions
+110 -33
View File
@@ -1,36 +1,50 @@
'use strict'
// Setting up the store when there is none yet.
// Setting up a store when there is none yet.
//
// This is the reason the client exists on Windows at all: the shell installer is
// `curl … | sh`, which Windows does not have. The three files it would place are
// downloaded here instead, into the very same store home — so the CLI and the
// client stay one installation, and running install.sh afterwards only adds the
// launcher script.
//
// Which store, though, is not baked in. The client asks a registry — `GET
// /api/stores` on the site — and each record says what the store is called, which
// catalog it serves and where its configuration lives. A different site, or a
// second store on ours, needs no change here. The registry address is the one
// address the client does know, and even that is overridable.
const fs = require('node:fs')
const https = require('node:https')
const http = require('node:http')
const path = require('node:path')
const FORGE = 'https://git.teletypegames.org'
const SOURCES = {
engine: `${FORGE}/stores/warp-engine-desktop-store/raw/branch/master/desktop_store.py`,
core: `${FORGE}/engines/warpstore/raw/branch/master/warpstore.py`,
config: `${FORGE}/stores/ttg-desktop-store/raw/branch/master/config.json`
// Where the engine itself comes from. Not part of the registry: this is the
// client's own machinery, the same for every store it can drive.
const FORGE = process.env.FORGE_BASE || 'https://git.teletypegames.org'
const ENGINE_SOURCES = {
'desktop_store.py': `${FORGE}/stores/warp-engine-desktop-store/raw/branch/master/desktop_store.py`,
'warpstore.py': `${FORGE}/engines/warpstore/raw/branch/master/warpstore.py`
}
// The registry. One address, and the only thing about a particular site left in
// the client.
const REGISTRY_URL = process.env.STORES_API || 'https://teletypegames.org/api/stores'
/** GET a URL as a string, following redirects — a moved repo answers 301. */
function fetchText (url, redirects = 5) {
return new Promise((resolve, reject) => {
const request = https.get(url, { headers: { 'User-Agent': 'warp-engine-desktop-gui' } }, (res) => {
const client = url.startsWith('http://') ? http : https
const request = client.get(url, { headers: { 'User-Agent': 'warp-engine-desktop-gui' } }, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
res.resume()
if (redirects <= 0) return reject(new Error(`too many redirects for ${url}`))
const next = new URL(res.headers.location, url).toString()
return fetchText(next, redirects - 1).then(resolve, reject)
return fetchText(new URL(res.headers.location, url).toString(), redirects - 1).then(resolve, reject)
}
if (res.statusCode !== 200) {
res.resume()
return reject(new Error(`${url} answered ${res.statusCode}`))
const error = new Error(`${url} answered ${res.statusCode}`)
error.statusCode = res.statusCode
return reject(error)
}
let body = ''
res.setEncoding('utf8')
@@ -43,40 +57,103 @@ function fetchText (url, redirects = 5) {
}
/**
* Download the engine, the shared core and the store's config into `home`.
* The stores this client can install, from the site's registry.
*
* Each record carries a name, the catalog it serves and the repository holding
* its config. Anything without those three is dropped rather than half-used.
*/
async function registry (url = REGISTRY_URL) {
const body = await fetchText(url)
const rows = JSON.parse(body)
if (!Array.isArray(rows)) throw new Error(`${url} did not answer with a list of stores`)
return rows
.map((row) => ({
name: String(row.name || '').trim(),
catalogUrl: String(row.catalogUrl || row.catalog_url || '').trim(),
storeRepositoryUrl: String(row.storeRepositoryUrl || row.store_repository_url || '').trim()
}))
.filter((row) => row.name && row.catalogUrl && row.storeRepositoryUrl)
}
/** `…/stores/ttg-desktop-store` -> the raw config.json on its default branch. */
function configUrl (repositoryUrl, branch = 'master') {
return `${repositoryUrl.replace(/\/+$/, '')}/raw/branch/${branch}/config.json`
}
/**
* A store id from its repository name: `ttg-desktop-store` -> `ttg`.
*
* The id names the store home and the folder the games land in, so it has to be
* short and filesystem-safe. The repository name is the best source we have; the
* store's own config.json overrides it whenever it exists.
*/
function storeId (store) {
const last = store.storeRepositoryUrl.replace(/\/+$/, '').split('/').pop() || ''
const base = last.replace(/-(desktop-)?store$/, '') || store.name
return base.toLowerCase().replace(/[^a-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '') || 'store'
}
/**
* The store's configuration.
*
* Its repository is the authority on how the store behaves — which platforms,
* which statuses, where things land. A repository without a config.json still
* works: the engine merges whatever it is given onto its own defaults, so a
* three-field config is a complete one.
*/
async function storeConfig (store, { onLog = () => {} } = {}) {
const id = storeId(store)
let config = null
try {
onLog(`reading the store config from ${store.storeRepositoryUrl}`)
config = JSON.parse(await fetchText(configUrl(store.storeRepositoryUrl)))
} catch (err) {
if (err.statusCode !== 404) throw err
onLog('no config.json in the store repository — using the engine defaults')
config = { paths: { subfolder: id }, catalog: { statuses: ['released', 'archived', 'demo'] } }
}
// The registry is the authority on identity and on which catalog to read, so
// those two win over whatever the file says.
config.store = { ...(config.store || {}) }
config.store.id = config.store.id || id
config.store.name = store.name
config.store.base_url = store.catalogUrl
return config
}
/** Where this store's home goes. */
function homeFor (store, root) {
return path.join(root, `${storeId(store)}-desktop`)
}
/**
* Install the engine, the shared core and the store's config into `home`.
*
* `onLog` reports each step, because on a slow line this takes a few seconds and
* silence looks like a hang. An existing config is left alone: a store that is
* already set up keeps its settings.
* silence looks like a hang.
*/
async function install (home, { onLog = () => {} } = {}) {
async function install (home, store, { onLog = () => {} } = {}) {
if (!store) throw new Error('no store was chosen')
fs.mkdirSync(home, { recursive: true })
const wrote = []
for (const [name, file] of [['engine', 'desktop_store.py'], ['core', 'warpstore.py']]) {
for (const [file, url] of Object.entries(ENGINE_SOURCES)) {
onLog(`downloading ${file}`)
const body = await fetchText(SOURCES[name])
const body = await fetchText(url)
if (!body.startsWith('#!/usr/bin/env python3')) {
throw new Error(`${file} does not look like the store engine — refusing to install it`)
}
const dest = path.join(home, file)
fs.writeFileSync(dest, body, { mode: 0o755 })
wrote.push(dest)
fs.writeFileSync(path.join(home, file), body, { mode: 0o755 })
}
const config = path.join(home, 'config.json')
if (fs.existsSync(config)) {
onLog('keeping the config already in place')
} else {
onLog('downloading config.json')
const body = await fetchText(SOURCES.config)
JSON.parse(body) // a broken config would fail later and less clearly
fs.writeFileSync(config, body)
wrote.push(config)
}
const configPath = path.join(home, 'config.json')
const config = await storeConfig(store, { onLog })
fs.writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`)
onLog(`the store is set up in ${home}`)
return { home, config, script: path.join(home, 'desktop_store.py'), wrote }
onLog(`${store.name} is set up in ${home}`)
return { home, config: configPath, script: path.join(home, 'desktop_store.py'), id: config.store.id }
}
module.exports = { FORGE, SOURCES, fetchText, install }
module.exports = {
ENGINE_SOURCES, FORGE, REGISTRY_URL,
configUrl, fetchText, homeFor, install, registry, storeConfig, storeId
}