@serve.zone/interfaces 32.100.0 → 32.101.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.
@@ -1312,6 +1312,265 @@ export const validateGatewayRouteDnsResultsResponse = (
1312
1312
  return errors;
1313
1313
  };
1314
1314
 
1315
+ // ---------------------------------------------------------------------------------------------
1316
+ // Mail observations (`getGatewayMailObservations`, Linus ruling 376 item 2)
1317
+ // ---------------------------------------------------------------------------------------------
1318
+
1319
+ /**
1320
+ * What a gateway observes about one domain its controller asked for: observed state, never a
1321
+ * member of an area table.
1322
+ */
1323
+ export interface IGatewayMailDomainObservation {
1324
+ /** The requested domain. */
1325
+ domain: string;
1326
+ /**
1327
+ * The gateway's email domain of exactly this name: `lifecycleStatus` is its mail DNS lifecycle
1328
+ * status (dcrouter's `TEmailDomainLifecycleStatus`, which its 34.x gateway-client mail overview
1329
+ * answered as `status`), `null` while the gateway has not reconciled it yet. An open word a reader
1330
+ * shows as text (Linus ruling 378 item 2), bounded by
1331
+ * `gatewayMailObservationsContract.maximumLifecycleStatusLength` like an email's `status`, so a new
1332
+ * gateway state needs no release here. `null` when the gateway configures no email domain of this
1333
+ * name.
1334
+ */
1335
+ emailDomain: { lifecycleStatus: string | null } | null;
1336
+ /**
1337
+ * The nameservers the gateway lists for its DNS zone of exactly this name, 1 to
1338
+ * `gatewayMailObservationsContract.maximumNameservers` distinct lowercase hostnames without a
1339
+ * trailing dot, in the gateway's order. `null` when the gateway holds no zone of this name or
1340
+ * lists no nameservers for it; a reader keeps what it knew before.
1341
+ */
1342
+ nameservers: string[] | null;
1343
+ }
1344
+
1345
+ /**
1346
+ * One recently cached email (dcrouter's `CachedEmail`): the entry shape of the 34.x gateway-client
1347
+ * mail overview (`requests.mail.IMailOverviewEmail`). `to` is the first recipient, `timestamp` the
1348
+ * Unix milliseconds the gateway accepted the email at; `direction` and `status` are the gateway's
1349
+ * open words, shown as text.
1350
+ */
1351
+ export interface IGatewayObservedEmail {
1352
+ id: string;
1353
+ direction?: string;
1354
+ from?: string;
1355
+ to?: string;
1356
+ subject?: string;
1357
+ status?: string;
1358
+ timestamp?: number;
1359
+ }
1360
+
1361
+ /**
1362
+ * The bounds of `getGatewayMailObservations`. Every text is judged in UTF-16 code units and holds no
1363
+ * control character (U+0000 to U+001F, U+007F) and no unpaired surrogate, so each unit costs at most
1364
+ * three bytes of JSON: at every bound the answer, with the request it answers, stays below the
1365
+ * 2 MiB message cap of the controller's socket server (Cloudly) at 1,779,407 bytes
1366
+ * (`test.gatewaymailobservations`). The gateway removes such characters from an email's texts and
1367
+ * shortens each text to its bound, on a code point boundary, before it answers; one text over its
1368
+ * bound refuses the whole answer.
1369
+ */
1370
+ export const gatewayMailObservationsContract = Object.freeze({
1371
+ /** Domains per request, and so domain entries per answer. */
1372
+ maximumDomains: 256,
1373
+ /** Nameservers per domain entry. */
1374
+ maximumNameservers: 16,
1375
+ /** Recent emails per answer: the 34.x overview's default limit. */
1376
+ maximumRecentEmails: 100,
1377
+ /** Characters of an email's `id`. */
1378
+ maximumEmailIdLength: 128,
1379
+ /** Characters of an email's `direction` and `status`, each. */
1380
+ maximumEmailWordLength: 32,
1381
+ /** Characters of an email domain's `lifecycleStatus`. */
1382
+ maximumLifecycleStatusLength: 32,
1383
+ /** Characters of an email's `from` and `to`, each. */
1384
+ maximumEmailAddressLength: 320,
1385
+ /** Characters of an email's `subject`: the RFC 5322 line limit. */
1386
+ maximumEmailSubjectLength: 998,
1387
+ } as const);
1388
+
1389
+ const observationControlCharacter = /[\u0000-\u001f\u007f]/;
1390
+ const unpairedSurrogate = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/;
1391
+
1392
+ /** A lowercase hostname without a trailing dot and without a wildcard label. */
1393
+ const isObservationHostname = (valueArg: unknown): valueArg is string => (
1394
+ typeof valueArg === 'string' && valueArg.length <= gatewayConnectorContract.maximumHostnameLength
1395
+ && valueArg.split('.').length >= 2
1396
+ && valueArg.split('.').every((labelArg) => dnsZoneLabel.test(labelArg))
1397
+ );
1398
+
1399
+ const validateObservationText = (valueArg: unknown, pathArg: string, maximumArg: number): string[] => (
1400
+ typeof valueArg === 'string' && valueArg.length > 0 && valueArg.length <= maximumArg
1401
+ && !observationControlCharacter.test(valueArg) && !unpairedSurrogate.test(valueArg)
1402
+ ? []
1403
+ : [`${pathArg} must be a text of 1 to ${maximumArg} characters without a control character or an unpaired `
1404
+ + 'UTF-16 surrogate']
1405
+ );
1406
+
1407
+ const observedEmailRequiredKeys = ['id'] as const;
1408
+ const observedEmailKeyList = ['id', 'direction', 'from', 'to', 'subject', 'status', 'timestamp'] as const;
1409
+ const observedEmailKeys: TCompleteKeyList<IGatewayObservedEmail, typeof observedEmailKeyList> = observedEmailKeyList;
1410
+ const mailDomainObservationKeyList = ['domain', 'emailDomain', 'nameservers'] as const;
1411
+ const mailDomainObservationKeys: TCompleteKeyList<IGatewayMailDomainObservation, typeof mailDomainObservationKeyList> =
1412
+ mailDomainObservationKeyList;
1413
+
1414
+ const validateObservedEmail = (valueArg: unknown, pathArg: string): string[] => {
1415
+ const email = readDataProperties(valueArg);
1416
+ if (email === null || !hasExactKeys(email, observedEmailRequiredKeys, observedEmailKeys)) {
1417
+ return [`${pathArg} must use its exact schema`];
1418
+ }
1419
+ const contract = gatewayMailObservationsContract;
1420
+ const errors = validateObservationText(email.id, `${pathArg} id`, contract.maximumEmailIdLength);
1421
+ const optionalTexts = [
1422
+ ['direction', contract.maximumEmailWordLength],
1423
+ ['from', contract.maximumEmailAddressLength],
1424
+ ['to', contract.maximumEmailAddressLength],
1425
+ ['subject', contract.maximumEmailSubjectLength],
1426
+ ['status', contract.maximumEmailWordLength],
1427
+ ] as const;
1428
+ for (const [key, maximum] of optionalTexts) {
1429
+ if (Object.hasOwn(email, key)) {
1430
+ errors.push(...validateObservationText(email[key], `${pathArg} ${key}`, maximum));
1431
+ }
1432
+ }
1433
+ if (Object.hasOwn(email, 'timestamp')
1434
+ && !(typeof email.timestamp === 'number' && Number.isSafeInteger(email.timestamp) && email.timestamp >= 0)) {
1435
+ errors.push(`${pathArg} timestamp must be a non-negative safe integer`);
1436
+ }
1437
+ return errors;
1438
+ };
1439
+
1440
+ const validateMailDomainObservation = (
1441
+ valueArg: unknown,
1442
+ requestedArg: readonly string[],
1443
+ indexArg: number,
1444
+ pathArg: string,
1445
+ ): string[] => {
1446
+ const entry = readExactDataRecord(valueArg, mailDomainObservationKeys);
1447
+ if (entry === null) {
1448
+ return [`${pathArg} must use its exact schema`];
1449
+ }
1450
+ const errors: string[] = [];
1451
+ if (indexArg >= requestedArg.length || entry.domain !== requestedArg[indexArg]) {
1452
+ errors.push(`${pathArg} domain must be requested domain ${indexArg + 1}, in request order`);
1453
+ }
1454
+ if (entry.emailDomain !== null) {
1455
+ const emailDomain = readExactDataRecord(entry.emailDomain, ['lifecycleStatus']);
1456
+ if (emailDomain === null) {
1457
+ errors.push(`${pathArg} emailDomain must be null or exactly { lifecycleStatus }`);
1458
+ } else if (emailDomain.lifecycleStatus !== null) {
1459
+ errors.push(...validateObservationText(emailDomain.lifecycleStatus, `${pathArg} emailDomain lifecycleStatus`,
1460
+ gatewayMailObservationsContract.maximumLifecycleStatusLength));
1461
+ }
1462
+ }
1463
+ if (entry.nameservers !== null) {
1464
+ const maximum = gatewayMailObservationsContract.maximumNameservers;
1465
+ const nameservers = readDataElements(entry.nameservers, maximum);
1466
+ if (nameservers === null || nameservers.length === 0) {
1467
+ errors.push(`${pathArg} nameservers must be null or a dense list of 1 to ${maximum} hostnames`);
1468
+ } else {
1469
+ nameservers.forEach((nameserverArg, indexArg) => {
1470
+ if (!isObservationHostname(nameserverArg)) {
1471
+ errors.push(`${pathArg} nameservers[${indexArg}] must be a lowercase hostname without a trailing dot`);
1472
+ } else if (nameservers.indexOf(nameserverArg) !== indexArg) {
1473
+ errors.push(`${pathArg} nameservers[${indexArg}] repeats an earlier nameserver`);
1474
+ }
1475
+ });
1476
+ }
1477
+ }
1478
+ return errors;
1479
+ };
1480
+
1481
+ /**
1482
+ * Validates a `getGatewayMailObservations` request and names every reason it is refused: exactly
1483
+ * `{ domains }`, 1 to `gatewayMailObservationsContract.maximumDomains` lowercase hostnames without a
1484
+ * trailing dot or a wildcard label, strictly ascending by `compareWireTokens`.
1485
+ */
1486
+ export const validateGatewayMailObservationsRequest = (
1487
+ valueArg: unknown,
1488
+ pathArg = 'gateway mail observations request',
1489
+ ): string[] => {
1490
+ const record = readExactDataRecord(valueArg, ['domains']);
1491
+ if (record === null) {
1492
+ return [`${pathArg} must use its exact schema`];
1493
+ }
1494
+ const maximum = gatewayMailObservationsContract.maximumDomains;
1495
+ const domains = readDataElements(record.domains, maximum);
1496
+ if (domains === null || domains.length === 0) {
1497
+ return [`${pathArg} domains must be a dense list of 1 to ${maximum} domains`];
1498
+ }
1499
+ const errors: string[] = [];
1500
+ domains.forEach((domainArg, indexArg) => {
1501
+ if (!isObservationHostname(domainArg)) {
1502
+ errors.push(`${pathArg} domains[${indexArg}] must be a lowercase domain without a trailing dot or a wildcard`);
1503
+ } else if (indexArg > 0 && typeof domains[indexArg - 1] === 'string'
1504
+ && compareWireTokens(domains[indexArg - 1] as string, domainArg) >= 0) {
1505
+ errors.push(`${pathArg} domains must be strictly ascending`);
1506
+ }
1507
+ });
1508
+ return errors;
1509
+ };
1510
+
1511
+ /**
1512
+ * Validates a `getGatewayMailObservations` answer against the request it answers and names every
1513
+ * reason it is refused: exactly `{ domains, emails }`; `domains` one `IGatewayMailDomainObservation`
1514
+ * per requested domain, each once and in the request's order, exact keys, a lifecycle status that
1515
+ * is `null` or a clean text of at most `gatewayMailObservationsContract.maximumLifecycleStatusLength`
1516
+ * characters, nameservers `null` or 1 to
1517
+ * `gatewayMailObservationsContract.maximumNameservers` distinct lowercase hostnames; `emails` at most
1518
+ * `gatewayMailObservationsContract.maximumRecentEmails` `IGatewayObservedEmail` entries with distinct
1519
+ * ids, the keys of the type and no other, each text within its bound. Which emails the gateway
1520
+ * chose (a sender or a recipient in a requested domain, newest first) is the gateway's statement,
1521
+ * which no shape can show. A request that fails `validateGatewayMailObservationsRequest` is refused
1522
+ * first, since no answer can match it. The controller judges every answer with this before it uses
1523
+ * one, never with a copy.
1524
+ */
1525
+ export const validateGatewayMailObservationsResponse = (
1526
+ valueArg: unknown,
1527
+ requestArg: Readonly<{ domains: readonly string[] }>,
1528
+ pathArg = 'gateway mail observations response',
1529
+ ): string[] => {
1530
+ const requestErrors = validateGatewayMailObservationsRequest(requestArg, `${pathArg} request`);
1531
+ if (requestErrors.length > 0) {
1532
+ return requestErrors;
1533
+ }
1534
+ const record = readExactDataRecord(valueArg, ['domains', 'emails']);
1535
+ if (record === null) {
1536
+ return [`${pathArg} must use its exact schema`];
1537
+ }
1538
+ const errors: string[] = [];
1539
+ const requested = requestArg.domains;
1540
+ const domains = readDataElements(record.domains, gatewayMailObservationsContract.maximumDomains);
1541
+ if (domains === null) {
1542
+ errors.push(`${pathArg} domains must be a dense list of at most ${gatewayMailObservationsContract.maximumDomains} `
1543
+ + 'entries');
1544
+ } else {
1545
+ if (domains.length !== requested.length) {
1546
+ errors.push(`${pathArg} domains must answer each of the ${requested.length} requested domains once`);
1547
+ }
1548
+ domains.forEach((entryArg, indexArg) => {
1549
+ errors.push(...validateMailDomainObservation(entryArg, requested, indexArg, `${pathArg} domains[${indexArg}]`));
1550
+ });
1551
+ }
1552
+ const maximumEmails = gatewayMailObservationsContract.maximumRecentEmails;
1553
+ const emails = readDataElements(record.emails, maximumEmails);
1554
+ if (emails === null) {
1555
+ errors.push(`${pathArg} emails must be a dense list of at most ${maximumEmails} entries`);
1556
+ return errors;
1557
+ }
1558
+ const seenIds = new Set<string>();
1559
+ emails.forEach((emailArg, indexArg) => {
1560
+ const emailPath = `${pathArg} emails[${indexArg}]`;
1561
+ const emailErrors = validateObservedEmail(emailArg, emailPath);
1562
+ errors.push(...emailErrors);
1563
+ const id = emailErrors.length === 0 ? readDataProperties(emailArg)?.id : undefined;
1564
+ if (typeof id === 'string') {
1565
+ if (seenIds.has(id)) {
1566
+ errors.push(`${emailPath} id repeats an earlier email`);
1567
+ }
1568
+ seenIds.add(id);
1569
+ }
1570
+ });
1571
+ return errors;
1572
+ };
1573
+
1315
1574
  const validateGatewayClientId = (valueArg: unknown, pathArg: string): string[] => (
1316
1575
  isGatewayClientIdText(valueArg)
1317
1576
  ? []
@@ -7,6 +7,8 @@ import type {
7
7
  IGatewayAreaExport,
8
8
  IGatewayApplyReport,
9
9
  IGatewayDesiredState,
10
+ IGatewayMailDomainObservation,
11
+ IGatewayObservedEmail,
10
12
  IGatewayStatusDocument,
11
13
  IRegisterGatewaySessionRequest,
12
14
  TGatewayConfigArea,
@@ -160,10 +162,11 @@ export interface IReq_Cloudly_Gateway_ExportGatewayArea extends plugins.typedreq
160
162
  *
161
163
  * Peer gate: a gateway built before this request answers no handler for it. The controller calls it
162
164
  * only on a gateway whose hello (`data.IRegisterGatewaySessionRequest.interfacesVersion`) states the
163
- * release of this package that declares it or a later one, the rule this package's validators set
164
- * for every new member and method (a controller sends one only to a gateway whose stated
165
- * `interfacesVersion` knows it); a gateway therefore moves to that release only together with
166
- * serving the call.
165
+ * release of this package that declares it or a later one (a controller sends a new member only to
166
+ * a gateway whose stated `interfacesVersion` knows it); a gateway therefore moves to that release
167
+ * only together with serving the call. A later controller-session read may instead be gated by an
168
+ * `IGatewayCapabilities` flag, as `getGatewayMailObservations` is (`email.observations`), because a
169
+ * gateway can move to the release that declares a read before it serves it.
167
170
  */
168
171
  export interface IReq_Cloudly_Gateway_GetGatewayRouteDnsResults extends plugins.typedrequestInterfaces.implementsTR<
169
172
  plugins.typedrequestInterfaces.ITypedRequest,
@@ -181,6 +184,54 @@ export interface IReq_Cloudly_Gateway_GetGatewayRouteDnsResults extends plugins.
181
184
  };
182
185
  }
183
186
 
187
+ /**
188
+ * The controller reads what the gateway observes about mail for some domains (Linus ruling 376
189
+ * item 2): per domain the mail DNS lifecycle status of the gateway's email domain of that name and
190
+ * the nameservers of its DNS zone of that name, and the gateway's recent cached emails whose sender
191
+ * or a recipient is in one of the domains, newest first, at most
192
+ * `data.gatewayMailObservationsContract.maximumRecentEmails`. An address is in a domain when the part
193
+ * after its last `@`, lowercased, equals that requested domain; there is no suffix or subdomain
194
+ * matching (Linus ruling 378 item 4). It is observed state, never a member of an area table, and
195
+ * nothing is written. It replaces, for a gateway that serves it, what the gateway-client reads
196
+ * `getGatewayClientMailOverview` and `getGatewayClientDomains` answered about these observations;
197
+ * the controller names the domains (from its own records), because a gateway that serves this read
198
+ * has no gateway-client scope to derive them from. The scope is the whole domain: on a gateway with
199
+ * the flag the view shows the cached mail of the controller's whole domains, not only the bound
200
+ * addresses the 34.x gateway-client overview filtered to.
201
+ *
202
+ * The gateway answers only after the session's registration, with one domain entry per requested
203
+ * domain, in request order (`data.IGatewayMailDomainObservation`; `emailDomain` and `nameservers`
204
+ * `null` for what it does not hold). A request that fails `data.validateGatewayMailObservationsRequest`
205
+ * is refused `request-invalid`. "Cannot read" and "holds nothing" are different answers (Linus
206
+ * ruling 378 item 1): a gateway with mail not configured answers the truth (`emailDomain: null`,
207
+ * `emails: []`), while a gateway whose email domain store, zone store or email cache exists but is
208
+ * not ready or fails answers an error without a named refusal, never a silent empty or partial
209
+ * answer. The
210
+ * controller judges every answer with `data.validateGatewayMailObservationsResponse` before it uses
211
+ * it; one text over its bound refuses the whole answer, so the gateway cleans and shortens every
212
+ * email text first (`data.gatewayMailObservationsContract`).
213
+ *
214
+ * Capability gate: the controller calls it only on a gateway whose `getGatewayCapabilities` answers
215
+ * `email.observations: true` (`data.IGatewayCapabilities`), and reads a gateway without the flag
216
+ * through the gateway-client reads as before. It never chooses by calling and catching an unknown
217
+ * method.
218
+ */
219
+ export interface IReq_Cloudly_Gateway_GetGatewayMailObservations extends plugins.typedrequestInterfaces.implementsTR<
220
+ plugins.typedrequestInterfaces.ITypedRequest,
221
+ IReq_Cloudly_Gateway_GetGatewayMailObservations
222
+ > {
223
+ method: 'getGatewayMailObservations';
224
+ /** `data.validateGatewayMailObservationsRequest`. */
225
+ request: {
226
+ domains: string[];
227
+ };
228
+ /** `data.validateGatewayMailObservationsResponse`. */
229
+ response: {
230
+ domains: IGatewayMailDomainObservation[];
231
+ emails: IGatewayObservedEmail[];
232
+ };
233
+ }
234
+
184
235
  /**
185
236
  * An administrator makes a one-time adoption code, either for a new gateway in an existing
186
237
  * location or for an existing gateway record (re-adoption). Adoption never creates a location.