Files
CatoandClaude Opus 5.5 12d1080388 feat: the sites engine v0 — one process serves every data site by domain
Reads Loki's sites / site_versions / site_domains with a SELECT-only role and
renders the published version with @bitbaum/sitekit's SiteDocument and the
site's theme (siteThemeCss + self-hosted faces). Answers:

- <slug>.orangecat.ch and verified own domains → the published version
- sites.orangecat.ch/<site id>/… → the preview version, never indexed
- /_sites/ask → Caddy's on-demand TLS check, falling back to hirnli's
- localhost:<port>/ → the deploy's health probe

A row that does not parse is a 404 and a log line. sitekit installs from the
kits commit until 0.8.0 is on npm.

Step 3 of loki ADR-0001 (the sites platform).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 11:42:16 +01:00

71 lines
2.7 KiB
TypeScript

/**
* Which address a request came in on decides which site — and whether any —
* is shown. These pin that decision, and the links of a preview.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { classifyHost, isSiteId, normaliseHost, prefixedHref } from '../src/lib/host';
import { pagePath, splitPath } from '../src/lib/resolve-path';
test('the box health probe is local', () => {
for (const h of ['localhost', 'localhost:4042', '127.0.0.1:4042', '[::1]:4042']) {
assert.deepEqual(classifyHost(h), { kind: 'local' }, h);
}
});
test('a free address is one label under the base domain', () => {
assert.deepEqual(classifyHost('Camille.OrangeCat.ch:443'), { kind: 'label', label: 'camille' });
assert.deepEqual(classifyHost('camille.orangecat.ch.'), { kind: 'label', label: 'camille' });
assert.deepEqual(classifyHost('a.b.orangecat.ch'), { kind: 'invalid' });
assert.deepEqual(classifyHost('-x.orangecat.ch'), { kind: 'invalid' });
assert.deepEqual(classifyHost('orangecat.ch'), { kind: 'invalid' });
});
test('the preview host is its own kind', () => {
assert.deepEqual(classifyHost('sites.orangecat.ch'), { kind: 'preview' });
});
test('any other well-formed name is a customer domain; junk is invalid', () => {
assert.deepEqual(classifyHost('www.Camille.ch'), { kind: 'domain', host: 'www.camille.ch' });
assert.deepEqual(classifyHost('evil-orangecat.ch'), {
kind: 'domain',
host: 'evil-orangecat.ch',
});
for (const bad of [
'',
null,
undefined,
'no_tld',
'1.2.3.4',
'a..b.ch',
'x'.repeat(260) + '.ch',
]) {
assert.deepEqual(classifyHost(bad), { kind: 'invalid' }, String(bad));
}
assert.equal(normaliseHost(' A.CH:80 '), 'a.ch');
});
test('a preview id must be a uuid', () => {
assert.ok(isSiteId('3f0c5a8e-1b2c-4d5e-8f90-a1b2c3d4e5f6'));
for (const bad of [undefined, '', 'camille', '3f0c5a8e1b2c4d5e8f90a1b2c3d4e5f6']) {
assert.ok(!isSiteId(bad), String(bad));
}
});
test('preview links stay under the site id; others pass through', () => {
const p = '/3f0c5a8e-1b2c-4d5e-8f90-a1b2c3d4e5f6';
assert.equal(prefixedHref(p, '/'), p);
assert.equal(prefixedHref(p, '/angebot'), `${p}/angebot`);
assert.equal(prefixedHref(p, '#kontakt'), '#kontakt');
assert.equal(prefixedHref(p, 'https://osm.org'), 'https://osm.org');
assert.equal(prefixedHref(p, '//cdn.example'), '//cdn.example');
assert.equal(prefixedHref('', '/angebot'), '/angebot');
});
test('a page path is one segment or home; deeper paths are not pages', () => {
assert.equal(pagePath(splitPath('/')), '');
assert.equal(pagePath(splitPath('/angebot')), 'angebot');
assert.equal(pagePath(splitPath('/angebot/')), 'angebot');
assert.equal(pagePath(splitPath('/a/b')), null);
});