@12-apps/tenant 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ADOPTING.md +94 -0
- package/README.md +4 -2
- package/package.json +26 -3
- package/src/domains/checks.ts +124 -0
- package/src/domains/dns-names.ts +215 -0
- package/src/domains/engine.ts +309 -0
- package/src/domains/hostname.ts +38 -0
- package/src/domains/index.ts +45 -0
- package/src/domains/plan.ts +273 -0
- package/src/domains/port.ts +105 -0
- package/src/domains/react/card.tsx +216 -0
- package/src/domains/react/context.tsx +102 -0
- package/src/domains/react/copy.ts +151 -0
- package/src/domains/react/index.ts +41 -0
- package/src/domains/react/records.tsx +353 -0
- package/src/domains/react/removed.tsx +133 -0
- package/src/domains/react/row.tsx +335 -0
- package/src/domains/react/screen.ts +92 -0
- package/src/domains/record.ts +141 -0
- package/src/domains/server.ts +70 -0
- package/src/domains/tls.ts +50 -0
- package/src/index.ts +28 -1
- package/src/server/index.ts +45 -3
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One claimed domain: its name and status, a pair's principal address
|
|
3
|
+
* (pending or active), the records to publish, the pointing warning, where an
|
|
4
|
+
* active one answers, and the verify / remove buttons.
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect, useRef, type JSX } from 'react';
|
|
7
|
+
|
|
8
|
+
import { Alert } from '@12-apps/ui/data-display/Alert';
|
|
9
|
+
import { Chip } from '@12-apps/ui/data-display/Chip';
|
|
10
|
+
import { Button } from '@12-apps/ui/form/Button';
|
|
11
|
+
import { Box } from '@12-apps/ui/mui/Box';
|
|
12
|
+
import { Stack } from '@12-apps/ui/mui/Stack';
|
|
13
|
+
import { Text } from '@12-apps/ui/typography/Text';
|
|
14
|
+
|
|
15
|
+
import { usePhoneCopy, useDomainsScreen, type TenantDomainItem } from './context';
|
|
16
|
+
import {
|
|
17
|
+
PairPointingRecord,
|
|
18
|
+
PointingRecord,
|
|
19
|
+
PointingWarning,
|
|
20
|
+
VerificationRecord,
|
|
21
|
+
domainName,
|
|
22
|
+
removalConsequence,
|
|
23
|
+
statusLabelFor,
|
|
24
|
+
} from './records';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* One domain's refused write, told INSIDE that domain's row, by the control
|
|
28
|
+
* that was tapped: at the head of the card it sat a phone's height above the
|
|
29
|
+
* tap, and the tap looked like it did nothing. It comes into view as it
|
|
30
|
+
* appears (nearest, with room for the control under it). No title: the row's
|
|
31
|
+
* own heading already names the domain.
|
|
32
|
+
*/
|
|
33
|
+
export function RowRefusal({
|
|
34
|
+
message,
|
|
35
|
+
testId,
|
|
36
|
+
onDismiss,
|
|
37
|
+
}: {
|
|
38
|
+
message: string;
|
|
39
|
+
testId: string;
|
|
40
|
+
onDismiss: () => void;
|
|
41
|
+
}): JSX.Element {
|
|
42
|
+
const { copy } = useDomainsScreen();
|
|
43
|
+
const told = useRef<HTMLDivElement>(null);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
told.current?.scrollIntoView?.({ block: 'nearest' });
|
|
46
|
+
}, [message]);
|
|
47
|
+
return (
|
|
48
|
+
<Box ref={told} sx={{ alignSelf: 'stretch', scrollMarginBottom: (theme) => theme.spacing(7) }}>
|
|
49
|
+
<Alert
|
|
50
|
+
variant="danger"
|
|
51
|
+
description={message}
|
|
52
|
+
closable
|
|
53
|
+
closeLabel={copy.closeAlert}
|
|
54
|
+
onClose={onDismiss}
|
|
55
|
+
data-testid={testId}
|
|
56
|
+
/>
|
|
57
|
+
</Box>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The row's action buttons: verify (while pending) and the confirm-gated
|
|
63
|
+
* remove, from the left, the primary solid and the secondary its inverse.
|
|
64
|
+
*/
|
|
65
|
+
function DomainActions({
|
|
66
|
+
domain,
|
|
67
|
+
pending,
|
|
68
|
+
busy,
|
|
69
|
+
disabled,
|
|
70
|
+
onVerify,
|
|
71
|
+
onRemove,
|
|
72
|
+
}: {
|
|
73
|
+
domain: TenantDomainItem;
|
|
74
|
+
pending: boolean;
|
|
75
|
+
busy: boolean;
|
|
76
|
+
disabled: boolean;
|
|
77
|
+
onVerify: () => void;
|
|
78
|
+
onRemove: () => Promise<void>;
|
|
79
|
+
}): JSX.Element {
|
|
80
|
+
const { copy, components } = useDomainsScreen();
|
|
81
|
+
const { ConfirmButton } = components;
|
|
82
|
+
return (
|
|
83
|
+
<Stack direction="row" spacing={1}>
|
|
84
|
+
{pending ? (
|
|
85
|
+
<Button
|
|
86
|
+
variant="solid"
|
|
87
|
+
color="primary"
|
|
88
|
+
size="sm"
|
|
89
|
+
disabled={disabled || busy}
|
|
90
|
+
onClick={onVerify}
|
|
91
|
+
dataTestId="domain-verify"
|
|
92
|
+
>
|
|
93
|
+
{copy.records.verifyAction}
|
|
94
|
+
</Button>
|
|
95
|
+
) : null}
|
|
96
|
+
{/* Confirm-gated: it archives the domain and frees its name, which the popup says. */}
|
|
97
|
+
<ConfirmButton
|
|
98
|
+
variant="outline"
|
|
99
|
+
color="primary"
|
|
100
|
+
size="sm"
|
|
101
|
+
disabled={busy}
|
|
102
|
+
onClick={onRemove}
|
|
103
|
+
confirm={{
|
|
104
|
+
title: copy.records.removeTitle,
|
|
105
|
+
entityName: domainName(domain, copy.pair),
|
|
106
|
+
description: removalConsequence(pending, copy.pair),
|
|
107
|
+
confirmText: copy.records.removeAction,
|
|
108
|
+
}}
|
|
109
|
+
dataTestId="domain-remove"
|
|
110
|
+
>
|
|
111
|
+
{copy.records.removeAction}
|
|
112
|
+
</ConfirmButton>
|
|
113
|
+
</Stack>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A pair's one-line choice of its principal address, the current one
|
|
119
|
+
* selected. On a pending pair, and on an ACTIVE one too, with one line saying
|
|
120
|
+
* the change applies at once: it flips the redirect, and tapping back undoes
|
|
121
|
+
* it, so no confirmation.
|
|
122
|
+
*/
|
|
123
|
+
function PrincipalChoice({
|
|
124
|
+
domain,
|
|
125
|
+
busy,
|
|
126
|
+
disabled,
|
|
127
|
+
onPick,
|
|
128
|
+
}: {
|
|
129
|
+
domain: TenantDomainItem;
|
|
130
|
+
busy: boolean;
|
|
131
|
+
disabled: boolean;
|
|
132
|
+
onPick: (principal: 'root' | 'www') => void;
|
|
133
|
+
}): JSX.Element {
|
|
134
|
+
const { copy, components } = useDomainsScreen();
|
|
135
|
+
const { Segmented } = components;
|
|
136
|
+
const pair = copy.pair;
|
|
137
|
+
const phone = usePhoneCopy();
|
|
138
|
+
const active = domain.status === 'ACTIVE';
|
|
139
|
+
// Said only while the pair answers here: with its DNS elsewhere, no request reaches the redirect yet.
|
|
140
|
+
const saysAtOnce = active && domain.dnsPointsHere !== false;
|
|
141
|
+
return (
|
|
142
|
+
<Stack spacing={0.75} sx={{ maxWidth: '35rem' }} data-testid={active ? 'domain-principal-active' : undefined}>
|
|
143
|
+
<Text variant="caption" size="xs" as="span">
|
|
144
|
+
{pair.principalQuestion}
|
|
145
|
+
</Text>
|
|
146
|
+
<Segmented
|
|
147
|
+
label={pair.principalQuestion}
|
|
148
|
+
value={domain.principal}
|
|
149
|
+
options={[
|
|
150
|
+
{ value: 'root', label: phone ? pair.principalRootPhone : domain.hostname },
|
|
151
|
+
{ value: 'www', label: phone ? pair.principalWwwPhone : (domain.wwwHostname ?? '') },
|
|
152
|
+
]}
|
|
153
|
+
disabled={disabled}
|
|
154
|
+
busy={busy}
|
|
155
|
+
dataTestId="domain-principal"
|
|
156
|
+
onPick={onPick}
|
|
157
|
+
/>
|
|
158
|
+
{saysAtOnce ? (
|
|
159
|
+
<Text variant="caption" size="xs" as="span" data-testid="domain-principal-change">
|
|
160
|
+
{pair.principalChangeActive}
|
|
161
|
+
</Text>
|
|
162
|
+
) : null}
|
|
163
|
+
</Stack>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* An active domain's one line: where the tenant answers and what is sent
|
|
169
|
+
* there (the pair's other name, and the platform's address when this is the
|
|
170
|
+
* main domain). Silent while the DNS is known to point elsewhere: the warning
|
|
171
|
+
* says what is true then.
|
|
172
|
+
*/
|
|
173
|
+
function AnswersLine({ domain }: { domain: TenantDomainItem }): JSX.Element | null {
|
|
174
|
+
const { copy } = useDomainsScreen();
|
|
175
|
+
const pair = copy.pair;
|
|
176
|
+
const phone = usePhoneCopy();
|
|
177
|
+
if (domain.status !== 'ACTIVE' || domain.dnsPointsHere === false) return null;
|
|
178
|
+
const other = domain.wwwHostname === null ? null : domain.principal === 'root' ? domain.wwwHostname : domain.hostname;
|
|
179
|
+
const answers = pair.answersAt(domain.principalHostname);
|
|
180
|
+
const redirects = pair.redirectsFrom(other, domain.canonical);
|
|
181
|
+
const strong = (text: string) => (
|
|
182
|
+
<Text as="span" variant="body" size="sm" weight="semibold">
|
|
183
|
+
{text}
|
|
184
|
+
</Text>
|
|
185
|
+
);
|
|
186
|
+
return (
|
|
187
|
+
<Text variant="body" size="sm" data-testid="domain-answers" style={{ overflowWrap: 'anywhere' }}>
|
|
188
|
+
{answers.before}
|
|
189
|
+
{strong(domain.principalHostname)}
|
|
190
|
+
{answers.after}
|
|
191
|
+
{redirects.before}
|
|
192
|
+
{other === null ? null : phone ? pair.otherNamePhone(domain.principal) : strong(other)}
|
|
193
|
+
{redirects.after}
|
|
194
|
+
</Text>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The chip may take a second line rather than lose its end; its title carries the words whole as well. */
|
|
199
|
+
const STATUS_CHIP_SX = {
|
|
200
|
+
display: 'inline-flex',
|
|
201
|
+
maxWidth: '100%',
|
|
202
|
+
'& .MuiChip-root': { height: 'auto', maxWidth: '100%' },
|
|
203
|
+
'& .MuiChip-label': { whiteSpace: 'normal', overflowWrap: 'anywhere', py: 0.25 },
|
|
204
|
+
} as const;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The status pill: amber while it waits or does not answer here yet, green
|
|
208
|
+
* once it serves. OUTLINED, not `soft`: the soft chip inks its label with the
|
|
209
|
+
* colour's dark step, which sinks on a night ground; the outline inks it with
|
|
210
|
+
* the colour's main, measured for text on both grounds.
|
|
211
|
+
*/
|
|
212
|
+
function StatusChip({ domain }: { domain: TenantDomainItem }): JSX.Element {
|
|
213
|
+
const { copy } = useDomainsScreen();
|
|
214
|
+
const serving = domain.status === 'ACTIVE' && domain.dnsPointsHere !== false;
|
|
215
|
+
const label = statusLabelFor(domain, copy.records);
|
|
216
|
+
return (
|
|
217
|
+
<Box component="span" title={label} sx={STATUS_CHIP_SX}>
|
|
218
|
+
<Chip label={label} size="sm" variant="outlined" color={serving ? 'success' : 'warning'} dataTestId="domain-status" />
|
|
219
|
+
</Box>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The records a row still needs published, and the pointing warning. */
|
|
224
|
+
function RowRecords({ domain, dnsTargets }: { domain: TenantDomainItem; dnsTargets: string[] }): JSX.Element {
|
|
225
|
+
const { copy } = useDomainsScreen();
|
|
226
|
+
const pending = domain.status !== 'ACTIVE';
|
|
227
|
+
return (
|
|
228
|
+
<>
|
|
229
|
+
{pending ? <VerificationRecord domain={domain} /> : null}
|
|
230
|
+
{/* The pointing record stays on screen while the name still resolves
|
|
231
|
+
elsewhere: an ACTIVE domain that is not pointed here is exactly the
|
|
232
|
+
one whose owner needs it, and the warning below sends them to it. */}
|
|
233
|
+
{pending || domain.dnsPointsHere === false ? (
|
|
234
|
+
domain.wwwHostname !== null ? (
|
|
235
|
+
<PairPointingRecord domain={domain} dnsTargets={dnsTargets} numbered={pending} />
|
|
236
|
+
) : (
|
|
237
|
+
<PointingRecord dnsTargets={dnsTargets} hostname={domain.hostname} numbered={pending} copy={copy.records} />
|
|
238
|
+
)
|
|
239
|
+
) : null}
|
|
240
|
+
<PointingWarning domain={domain} copy={copy.records} />
|
|
241
|
+
</>
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** This row's own refused write: where it is told, and how it closes. */
|
|
246
|
+
export interface RowRefusalState {
|
|
247
|
+
/** What was refused: the principal is told under its control, anything else above the buttons. */
|
|
248
|
+
kind: 'principal' | 'other';
|
|
249
|
+
message: string;
|
|
250
|
+
testId: string;
|
|
251
|
+
onDismiss: () => void;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** A restored row is brought into view and focused once the list shows it. */
|
|
255
|
+
function useReveal(reveal: boolean, onRevealed: () => void) {
|
|
256
|
+
const row = useRef<HTMLElement>(null);
|
|
257
|
+
useEffect(() => {
|
|
258
|
+
if (!reveal) return;
|
|
259
|
+
// Head first (its name and status), the scroll margin above it: on a narrow phone a restored row is taller than the screen.
|
|
260
|
+
row.current?.scrollIntoView?.({ block: 'start' });
|
|
261
|
+
row.current?.focus({ preventScroll: true });
|
|
262
|
+
onRevealed();
|
|
263
|
+
}, [reveal, onRevealed]);
|
|
264
|
+
return row;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function DomainRow({
|
|
268
|
+
domain,
|
|
269
|
+
dnsTargets,
|
|
270
|
+
disabled,
|
|
271
|
+
busy,
|
|
272
|
+
refusal = null,
|
|
273
|
+
reveal = false,
|
|
274
|
+
onRevealed = () => undefined,
|
|
275
|
+
onVerify,
|
|
276
|
+
onPrincipal,
|
|
277
|
+
onRemove,
|
|
278
|
+
}: {
|
|
279
|
+
domain: TenantDomainItem;
|
|
280
|
+
dnsTargets: string[];
|
|
281
|
+
disabled: boolean;
|
|
282
|
+
/** THIS domain's write is in flight, never another domain's. */
|
|
283
|
+
busy: boolean;
|
|
284
|
+
refusal?: RowRefusalState | null;
|
|
285
|
+
/** Just restored: scroll it into view and focus it. */
|
|
286
|
+
reveal?: boolean;
|
|
287
|
+
onRevealed?: () => void;
|
|
288
|
+
onVerify: () => void;
|
|
289
|
+
onPrincipal: (principal: 'root' | 'www') => void;
|
|
290
|
+
onRemove: () => Promise<void>;
|
|
291
|
+
}): JSX.Element {
|
|
292
|
+
const { copy } = useDomainsScreen();
|
|
293
|
+
const pending = domain.status !== 'ACTIVE';
|
|
294
|
+
const isPair = domain.wwwHostname !== null;
|
|
295
|
+
const row = useReveal(reveal, onRevealed);
|
|
296
|
+
const told = (kind: RowRefusalState['kind']) =>
|
|
297
|
+
refusal !== null && refusal.kind === kind ? (
|
|
298
|
+
<RowRefusal message={refusal.message} testId={refusal.testId} onDismiss={refusal.onDismiss} />
|
|
299
|
+
) : null;
|
|
300
|
+
|
|
301
|
+
return (
|
|
302
|
+
<Box
|
|
303
|
+
ref={row}
|
|
304
|
+
component="section"
|
|
305
|
+
tabIndex={-1}
|
|
306
|
+
aria-label={domainName(domain, copy.pair)}
|
|
307
|
+
sx={{ border: 1, borderColor: 'divider', borderRadius: 1, p: 2, scrollMarginBlock: (theme) => theme.spacing(2) }}
|
|
308
|
+
data-testid="domain-row"
|
|
309
|
+
>
|
|
310
|
+
<Stack spacing={1.5}>
|
|
311
|
+
<Stack direction="row" spacing={1.5} useFlexGap sx={{ alignItems: 'baseline', flexWrap: 'wrap' }}>
|
|
312
|
+
<Text variant="body" size="sm" weight="semibold" as="span" style={{ overflowWrap: 'anywhere' }}>
|
|
313
|
+
{domainName(domain, copy.pair)}
|
|
314
|
+
</Text>
|
|
315
|
+
<StatusChip domain={domain} />
|
|
316
|
+
</Stack>
|
|
317
|
+
|
|
318
|
+
{isPair ? <PrincipalChoice domain={domain} busy={busy} disabled={disabled} onPick={onPrincipal} /> : null}
|
|
319
|
+
{told('principal')}
|
|
320
|
+
<RowRecords domain={domain} dnsTargets={dnsTargets} />
|
|
321
|
+
<AnswersLine domain={domain} />
|
|
322
|
+
|
|
323
|
+
{told('other')}
|
|
324
|
+
<DomainActions
|
|
325
|
+
domain={domain}
|
|
326
|
+
pending={pending}
|
|
327
|
+
busy={busy}
|
|
328
|
+
disabled={disabled}
|
|
329
|
+
onVerify={onVerify}
|
|
330
|
+
onRemove={onRemove}
|
|
331
|
+
/>
|
|
332
|
+
</Stack>
|
|
333
|
+
</Box>
|
|
334
|
+
);
|
|
335
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createDomainsScreen`: the host's packs and widgets, checked at startup.
|
|
3
|
+
* A pack missing a key, or a widget left out, fails when the host boots,
|
|
4
|
+
* named (`domainsScreen.copy.locales.<tag>.records.<key>`), instead of a blank
|
|
5
|
+
* line on screen.
|
|
6
|
+
*/
|
|
7
|
+
import { TenantConfigError } from '../../validate';
|
|
8
|
+
import { PairAnnouncement, makeDomainsCard } from './card';
|
|
9
|
+
import type { TenantDomainsComponents } from './context';
|
|
10
|
+
import { TENANT_DOMAINS_COPY_SHAPE, type TenantDomainsCopy } from './copy';
|
|
11
|
+
|
|
12
|
+
export interface TenantDomainsScreenConfig {
|
|
13
|
+
copy: { locales: Record<string, TenantDomainsCopy>; defaultLocale: string };
|
|
14
|
+
components: TenantDomainsComponents;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Bag = Record<string, unknown>;
|
|
18
|
+
const isBag = (value: unknown): value is Bag => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
19
|
+
|
|
20
|
+
function requireBag(value: unknown, key: string): Bag {
|
|
21
|
+
if (value === undefined || value === null) throw new TenantConfigError(key, 'is required and missing');
|
|
22
|
+
if (!isBag(value)) throw new TenantConfigError(key, 'must be an object');
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function checkEntry(value: unknown, kind: 'text' | 'sentence', key: string): void {
|
|
27
|
+
if (value === undefined || value === null) throw new TenantConfigError(key, 'is required and missing');
|
|
28
|
+
if (kind === 'text' && (typeof value !== 'string' || value.trim() === '')) {
|
|
29
|
+
throw new TenantConfigError(key, 'must be a non-empty string');
|
|
30
|
+
}
|
|
31
|
+
if (kind === 'sentence' && typeof value !== 'function') {
|
|
32
|
+
throw new TenantConfigError(key, 'must be a function that writes the whole sentence');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Every grouped key of a pack, once: `[group, name, kind]`. */
|
|
37
|
+
const GROUPED_KEYS = (['records', 'pair'] as const).flatMap((group) =>
|
|
38
|
+
Object.entries(TENANT_DOMAINS_COPY_SHAPE[group]).map(([name, kind]) => [group, name, kind] as const),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
function checkPack(value: unknown, key: string): void {
|
|
42
|
+
const pack = requireBag(value, key);
|
|
43
|
+
const groups = { records: requireBag(pack.records, `${key}.records`), pair: requireBag(pack.pair, `${key}.pair`) };
|
|
44
|
+
for (const [group, name, kind] of GROUPED_KEYS) checkEntry(groups[group][name], kind, `${key}.${group}.${name}`);
|
|
45
|
+
checkEntry(pack.closeAlert, TENANT_DOMAINS_COPY_SHAPE.closeAlert, `${key}.closeAlert`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function checkCopy(value: unknown): void {
|
|
49
|
+
const copy = requireBag(value, 'domainsScreen.copy');
|
|
50
|
+
const locales = requireBag(copy.locales, 'domainsScreen.copy.locales');
|
|
51
|
+
const tags = Object.keys(locales);
|
|
52
|
+
if (tags.length === 0) throw new TenantConfigError('domainsScreen.copy.locales', 'must hold at least one locale pack');
|
|
53
|
+
for (const tag of tags) checkPack(locales[tag], `domainsScreen.copy.locales.${tag}`);
|
|
54
|
+
const fallback = copy.defaultLocale;
|
|
55
|
+
if (typeof fallback !== 'string' || fallback === '') {
|
|
56
|
+
throw new TenantConfigError('domainsScreen.copy.defaultLocale', 'is required and missing');
|
|
57
|
+
}
|
|
58
|
+
if (!tags.includes(fallback)) {
|
|
59
|
+
throw new TenantConfigError('domainsScreen.copy.defaultLocale', `names "${fallback}", which copy.locales does not hold`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function checkComponents(value: unknown): void {
|
|
64
|
+
const components = requireBag(value, 'domainsScreen.components');
|
|
65
|
+
for (const name of ['ConfirmButton', 'Segmented'] as const) {
|
|
66
|
+
const component = components[name];
|
|
67
|
+
if (component === undefined || component === null) {
|
|
68
|
+
throw new TenantConfigError(`domainsScreen.components.${name}`, 'is required and missing');
|
|
69
|
+
}
|
|
70
|
+
// A function component, or an exotic one (memo, forwardRef): an object with `$$typeof`.
|
|
71
|
+
if (typeof component !== 'function' && !(isBag(component) && '$$typeof' in component)) {
|
|
72
|
+
throw new TenantConfigError(`domainsScreen.components.${name}`, 'must be a React component');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Check the screen's config. Throws {@link TenantConfigError}. */
|
|
78
|
+
export function validateDomainsScreen(config: unknown): void {
|
|
79
|
+
const bag = requireBag(config, 'domainsScreen');
|
|
80
|
+
checkCopy(bag.copy);
|
|
81
|
+
checkComponents(bag.components);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function createDomainsScreen(config: TenantDomainsScreenConfig) {
|
|
85
|
+
validateDomainsScreen(config);
|
|
86
|
+
return {
|
|
87
|
+
/** The claims, the records, verify / remove and the removed list; `copy` is this render's resolved pack. */
|
|
88
|
+
DomainsCard: makeDomainsCard(config.components),
|
|
89
|
+
/** The claim field's note that a root is claimed with its `www.`. */
|
|
90
|
+
PairAnnouncement,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tenant's custom domain AS ONE THING: the rows of a root + `www` pair
|
|
3
|
+
* folded into the one domain every reader answers with. Storage (the host's)
|
|
4
|
+
* keeps a row per name, so the TLS ask and the host-to-tenant lookup stay one
|
|
5
|
+
* indexed probe by hostname; nothing outside the host's repository ever sees
|
|
6
|
+
* the `www.` row on its own.
|
|
7
|
+
*
|
|
8
|
+
* Pure: rows in, records out. The host reads the rows (its own table, its own
|
|
9
|
+
* column names) and maps them to {@link TenantDomainRow}.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Statuses a domain row can hold. */
|
|
13
|
+
export type TenantDomainStatus = 'PENDING' | 'VERIFIED' | 'ACTIVE' | 'DISABLED' | 'FAILED';
|
|
14
|
+
|
|
15
|
+
/** One stored row, as the host reads it. */
|
|
16
|
+
export interface TenantDomainRow {
|
|
17
|
+
id: string;
|
|
18
|
+
hostname: string;
|
|
19
|
+
status: string;
|
|
20
|
+
verificationToken: string;
|
|
21
|
+
verifiedAt: Date | null;
|
|
22
|
+
dnsPointsHere: boolean | null;
|
|
23
|
+
createdAt: Date;
|
|
24
|
+
/** The root row's id on a `www.` row; null on a root (or a lone subdomain). */
|
|
25
|
+
pairRootId: string | null;
|
|
26
|
+
/** On a root row: the `www.` name is the principal address. */
|
|
27
|
+
wwwPrincipal: boolean;
|
|
28
|
+
archivedAt: Date | null;
|
|
29
|
+
archivedByUserId: string | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One name a domain answers on. */
|
|
33
|
+
export interface TenantDomainName {
|
|
34
|
+
hostname: string;
|
|
35
|
+
/** Did this name resolve to this deploy at the last check? `null` is unknown. */
|
|
36
|
+
dnsPointsHere: boolean | null;
|
|
37
|
+
/** The address the tenant answers on; the other name redirects to it. */
|
|
38
|
+
principal: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A domain as everything outside the host's repository reads it. */
|
|
42
|
+
export interface TenantDomainRecord {
|
|
43
|
+
/** The ROOT row's id: the only id a domain is ever addressed by. */
|
|
44
|
+
id: string;
|
|
45
|
+
/** The claimed name: the root of a pair, or the one subdomain. */
|
|
46
|
+
hostname: string;
|
|
47
|
+
/** The pair's `www.` name, or null for a subdomain claim. */
|
|
48
|
+
wwwHostname: string | null;
|
|
49
|
+
/** Which of the pair is the principal address; always `root` for a subdomain. */
|
|
50
|
+
principal: 'root' | 'www';
|
|
51
|
+
status: TenantDomainStatus;
|
|
52
|
+
verificationToken: string;
|
|
53
|
+
verifiedAt: Date | null;
|
|
54
|
+
/**
|
|
55
|
+
* Did the domain resolve to this deploy at the last check? For a pair: `false`
|
|
56
|
+
* when either name points elsewhere, `true` when both point here, else unknown.
|
|
57
|
+
*/
|
|
58
|
+
dnsPointsHere: boolean | null;
|
|
59
|
+
/** Each name, root first. */
|
|
60
|
+
names: TenantDomainName[];
|
|
61
|
+
createdAt: Date;
|
|
62
|
+
archivedAt: Date | null;
|
|
63
|
+
archivedByUserId: string | null;
|
|
64
|
+
/** Each name's own row id: writes address a row, never a hostname (an archived row may share it). */
|
|
65
|
+
rowIds: Readonly<Record<string, string>>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A pair's pointing answer: a known `false` on either name wins, then a known `true` on both. */
|
|
69
|
+
function combinedPointing(values: readonly (boolean | null)[]): boolean | null {
|
|
70
|
+
if (values.some((value) => value === false)) return false;
|
|
71
|
+
return values.every((value) => value === true) ? true : null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Fold one root row and its `www.` row (if any) into the domain. */
|
|
75
|
+
export function domainOf(root: TenantDomainRow, www?: TenantDomainRow): TenantDomainRecord {
|
|
76
|
+
const principal = www !== undefined && root.wwwPrincipal ? 'www' : 'root';
|
|
77
|
+
const names: TenantDomainName[] = [
|
|
78
|
+
{ hostname: root.hostname, dnsPointsHere: root.dnsPointsHere, principal: principal === 'root' },
|
|
79
|
+
...(www === undefined ? [] : [{ hostname: www.hostname, dnsPointsHere: www.dnsPointsHere, principal: principal === 'www' }]),
|
|
80
|
+
];
|
|
81
|
+
return {
|
|
82
|
+
id: root.id,
|
|
83
|
+
hostname: root.hostname,
|
|
84
|
+
wwwHostname: www?.hostname ?? null,
|
|
85
|
+
principal,
|
|
86
|
+
status: root.status as TenantDomainStatus,
|
|
87
|
+
verificationToken: root.verificationToken,
|
|
88
|
+
verifiedAt: root.verifiedAt,
|
|
89
|
+
dnsPointsHere: combinedPointing(names.map((name) => name.dnsPointsHere)),
|
|
90
|
+
names,
|
|
91
|
+
createdAt: root.createdAt,
|
|
92
|
+
archivedAt: root.archivedAt,
|
|
93
|
+
archivedByUserId: root.archivedByUserId,
|
|
94
|
+
rowIds: { [root.hostname]: root.id, ...(www === undefined ? {} : { [www.hostname]: www.id }) },
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Group rows (any order) into domains, keeping the roots' order. */
|
|
99
|
+
export function domainsOf(rows: readonly TenantDomainRow[]): TenantDomainRecord[] {
|
|
100
|
+
const wwwOf = new Map(
|
|
101
|
+
rows.flatMap((row) => (row.pairRootId === null ? [] : [[row.pairRootId, row] as const])),
|
|
102
|
+
);
|
|
103
|
+
return rows.filter((row) => row.pairRootId === null).map((root) => domainOf(root, wwwOf.get(root.id)));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The principal hostname of a domain. */
|
|
107
|
+
export function principalHostOf(domain: TenantDomainRecord): string {
|
|
108
|
+
return domain.principal === 'www' && domain.wwwHostname !== null ? domain.wwwHostname : domain.hostname;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The tenant's canonical host: a pair answers at its PRINCIPAL name, and only
|
|
113
|
+
* while that name is not known to point elsewhere; the oldest such domain wins,
|
|
114
|
+
* so the answer is stable when a second domain is added. Give it the tenant's
|
|
115
|
+
* ACTIVE rows, oldest first. The plan gate is the caller's.
|
|
116
|
+
*/
|
|
117
|
+
export function canonicalHostOf(activeRowsOldestFirst: readonly TenantDomainRow[]): string | null {
|
|
118
|
+
for (const domain of domainsOf(activeRowsOldestFirst)) {
|
|
119
|
+
const principal = principalHostOf(domain);
|
|
120
|
+
if (domain.names.find((name) => name.hostname === principal)?.dnsPointsHere !== false) return principal;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Where a request on `hostname` must be sent instead: a pair's other name
|
|
127
|
+
* redirects to its principal, or null to serve it as it is. Give it the pair's
|
|
128
|
+
* rows (the root and its `www.`). Only an ACTIVE pair redirects, and only to a
|
|
129
|
+
* principal not known to point elsewhere: the same condition the canonical
|
|
130
|
+
* host holds, so neither ever bounces a visitor to a name that does not answer.
|
|
131
|
+
*/
|
|
132
|
+
export function pairRedirectOf(rows: readonly TenantDomainRow[], hostname: string): string | null {
|
|
133
|
+
const root = rows.find((row) => row.pairRootId === null);
|
|
134
|
+
if (root === undefined) return null;
|
|
135
|
+
const domain = domainOf(root, rows.find((row) => row.pairRootId === root.id));
|
|
136
|
+
const principal = principalHostOf(domain);
|
|
137
|
+
if (principal === hostname || domain.wwwHostname === null) return null;
|
|
138
|
+
const target = domain.names.find((name) => name.hostname === principal);
|
|
139
|
+
if (target?.dnsPointsHere === false || domain.status !== 'ACTIVE') return null;
|
|
140
|
+
return principal;
|
|
141
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server half of custom domains: the ports, the DNS checks through the
|
|
3
|
+
* host's resolver, the engine and the TLS ask. Runtime-free (no Node module):
|
|
4
|
+
* exported from `@12-apps/tenant` and `@12-apps/tenant/server`.
|
|
5
|
+
*/
|
|
6
|
+
import { TenantConfigError } from '../validate';
|
|
7
|
+
import { createDnsChecks, validateDnsChecksConfig, type TenantDomainDnsChecks, type TenantDomainDnsChecksConfig } from './checks';
|
|
8
|
+
import { createTenantDomains, validateDomainPorts, type TenantDomains } from './engine';
|
|
9
|
+
import type { TenantDomainPort, TenantDomainQuota } from './port';
|
|
10
|
+
import { createTlsAsk, validateTlsConfig, type TenantDomainTlsAnswer, type TenantDomainTlsConfig } from './tls';
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
createDnsChecks,
|
|
14
|
+
type TenantDomainDnsChecks,
|
|
15
|
+
type TenantDomainDnsChecksConfig,
|
|
16
|
+
} from './checks';
|
|
17
|
+
export {
|
|
18
|
+
createTenantDomains,
|
|
19
|
+
type TenantDomainAnswer,
|
|
20
|
+
type TenantDomainContext,
|
|
21
|
+
type TenantDomainOutcome,
|
|
22
|
+
type TenantDomainResult,
|
|
23
|
+
type TenantDomains,
|
|
24
|
+
type TenantDomainsConfig,
|
|
25
|
+
} from './engine';
|
|
26
|
+
export {
|
|
27
|
+
TENANT_DOMAIN_PORT_METHODS,
|
|
28
|
+
TENANT_DOMAIN_QUOTA_METHODS,
|
|
29
|
+
TENANT_DOMAIN_RESOLVER_METHODS,
|
|
30
|
+
type TenantDomainPort,
|
|
31
|
+
type TenantDomainQuota,
|
|
32
|
+
type TenantDomainResolver,
|
|
33
|
+
} from './port';
|
|
34
|
+
export { createTlsAsk, type TenantDomainTls, type TenantDomainTlsAnswer, type TenantDomainTlsConfig } from './tls';
|
|
35
|
+
|
|
36
|
+
/** Custom domains' whole config, as `createApiTenant` takes it (`domains`). Every member required. */
|
|
37
|
+
export interface TenantDomainsServerConfig<Tx = unknown> {
|
|
38
|
+
port: TenantDomainPort<Tx>;
|
|
39
|
+
quota: TenantDomainQuota<Tx>;
|
|
40
|
+
dns: TenantDomainDnsChecksConfig;
|
|
41
|
+
tls: TenantDomainTlsConfig;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `tenant.domains`: the engine's `run`, the DNS checks, and the TLS ask. */
|
|
45
|
+
export interface ApiTenantDomains extends TenantDomains {
|
|
46
|
+
dns: TenantDomainDnsChecks;
|
|
47
|
+
ask(hostname: string): Promise<TenantDomainTlsAnswer>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Custom domains, when the host opts in: every member named under `domains.`.
|
|
52
|
+
* Left out (`undefined`), there is nothing to check.
|
|
53
|
+
*/
|
|
54
|
+
export function validateDomains(value: unknown): void {
|
|
55
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
56
|
+
throw new TenantConfigError('domains', 'must be the domains config object ({ port, quota, dns, tls }), or be left out');
|
|
57
|
+
}
|
|
58
|
+
const bag = value as Record<string, unknown>;
|
|
59
|
+
validateDomainPorts(bag, 'domains.');
|
|
60
|
+
validateDnsChecksConfig(bag.dns, 'domains.dns.');
|
|
61
|
+
validateTlsConfig(bag.tls, 'domains.tls.');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Build `tenant.domains` from a validated config. */
|
|
65
|
+
export function createApiTenantDomains<Tx>(config: TenantDomainsServerConfig<Tx>): ApiTenantDomains {
|
|
66
|
+
const dns = createDnsChecks(config.dns);
|
|
67
|
+
const engine = createTenantDomains({ port: config.port, quota: config.quota, dns });
|
|
68
|
+
const tls = createTlsAsk(config.tls);
|
|
69
|
+
return { run: engine.run, dns, ask: tls.ask };
|
|
70
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The TLS "ask": a reverse proxy with on-demand certificates asks, BEFORE
|
|
3
|
+
* ordering one for a name it has never seen, whether it may. It is the only
|
|
4
|
+
* thing between "a name points at us" and "we asked a CA for a certificate",
|
|
5
|
+
* so it is deny-by-default and two conditions must BOTH hold: the name is a
|
|
6
|
+
* servable (verified) domain of a live tenant, and that tenant is entitled to
|
|
7
|
+
* custom domains, so a downgrade stops renewal, not just serving.
|
|
8
|
+
*/
|
|
9
|
+
import { TenantConfigError } from '../validate';
|
|
10
|
+
|
|
11
|
+
export interface TenantDomainTlsConfig {
|
|
12
|
+
/** The tenant that may be served on `hostname` (an ACTIVE domain of a live tenant), or null. */
|
|
13
|
+
servable(hostname: string): Promise<{ tenantId: string } | null>;
|
|
14
|
+
/** Whether the tenant's plan carries custom domains now. */
|
|
15
|
+
entitled(tenantId: string): Promise<boolean>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type TenantDomainTlsAnswer = 'allow' | 'unknown' | 'unentitled';
|
|
19
|
+
|
|
20
|
+
export interface TenantDomainTls {
|
|
21
|
+
/** `allow` only when both hold; `unknown` no servable domain; `unentitled` the plan does not carry it. */
|
|
22
|
+
ask(hostname: string): Promise<TenantDomainTlsAnswer>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function requireFunction(bag: Record<string, unknown>, member: string, key: string): void {
|
|
26
|
+
if (bag[member] === undefined || bag[member] === null) throw new TenantConfigError(key, 'is required and missing');
|
|
27
|
+
if (typeof bag[member] !== 'function') throw new TenantConfigError(key, 'must be a function');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Check the ask's config; keys are named under `prefix` (`domains.tls.`). */
|
|
31
|
+
export function validateTlsConfig(value: unknown, prefix: string): void {
|
|
32
|
+
const where = prefix === '' ? 'tls' : prefix.replace(/\.$/, '');
|
|
33
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
34
|
+
throw new TenantConfigError(where, value === undefined || value === null ? 'is required and missing' : 'must be an object');
|
|
35
|
+
}
|
|
36
|
+
for (const member of ['servable', 'entitled'] as const) {
|
|
37
|
+
requireFunction(value as Record<string, unknown>, member, `${prefix}${member}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createTlsAsk(config: TenantDomainTlsConfig): TenantDomainTls {
|
|
42
|
+
validateTlsConfig(config, '');
|
|
43
|
+
return {
|
|
44
|
+
ask: async (hostname) => {
|
|
45
|
+
const servable = await config.servable(hostname.trim().toLowerCase());
|
|
46
|
+
if (servable === null) return 'unknown';
|
|
47
|
+
return (await config.entitled(servable.tenantId)) ? 'allow' : 'unentitled';
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|