@serve.zone/interfaces 32.85.0 → 32.87.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.
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  canonicalizeStrictJson,
3
+ compareWireTokens,
3
4
  createCanonicalJsonSha256Hex,
4
5
  } from '../private/canonicaljson.js';
5
6
  import {
@@ -15,6 +16,7 @@ import { networkDigest } from '../private/networkcontract.js';
15
16
  import { isCanonicalTokenList } from '../private/tokenlist.js';
16
17
  import { isGatewayClientIdText } from '../private/gatewayclientid.js';
17
18
  import { clusterIngressLimits, validateClusterRouteSourceAllowList } from './clusteringress.js';
19
+ import { validateGatewayRouteDnsResult } from './gateway.js';
18
20
  import { type IControllerPeerSet, validateControllerPeerSet } from './controller.js';
19
21
  import type { TSha256Digest } from './immutableimage.js';
20
22
  import { isLocationName } from './location.js';
@@ -63,6 +65,15 @@ export const gatewayConnectorContract = Object.freeze({
63
65
  maximumRefusalDetailLength: 1024,
64
66
  maximumStorageBackendLength: 64,
65
67
  maximumStoragePathLength: 4096,
68
+ /** Hostnames per `getGatewayRouteDnsResults` request. */
69
+ maximumRouteDnsHostnames: 256,
70
+ /**
71
+ * How long a controller waits for the answer to one `applyDesiredState` request, in milliseconds:
72
+ * the one number both sides read (Linus ruling 125, 12(b)). Cloudly's push timeout is this value,
73
+ * and a gateway derives the deadline of its takeover wait as half of it, so the wait ends with an
74
+ * answer the controller still receives. No deadline travels in the request.
75
+ */
76
+ applyDesiredStateTimeoutMs: 10_000,
66
77
  /**
67
78
  * The longest `adoptGateway` credential bearer, in UTF-8 bytes: the credential bound of the
68
79
  * Cloudly client session the bearer authenticates (`runtimeSessionContract.maximumCredentialBytes`).
@@ -249,6 +260,11 @@ export interface IGatewayDesiredState {
249
260
  * without reading prose. A refusal of one area inside an applied document is none of these: the
250
261
  * gateway reports it per area (`gatewayApplyRefusals`). An adoption code that names no location is
251
262
  * refused `location-not-found` from `locationRefusals`.
263
+ *
264
+ * A momentary one (`gatewayConnectorMomentaryRefusals`) is answered with
265
+ * `IGatewayConnectorMomentaryRefusalErrorData` (`retryable: true`) instead, and only it:
266
+ * `readGatewayConnectorRefusalErrorData` reads a payload whose `retryable` disagrees with its code
267
+ * as no answer.
252
268
  */
253
269
  export const gatewayConnectorRefusals = Object.freeze([
254
270
  /** No adoption code has the value the gateway redeemed. */
@@ -335,6 +351,15 @@ export const gatewayConnectorRefusals = Object.freeze([
335
351
  * stays unconsumed.
336
352
  */
337
353
  'forward-sources-exceed-allowlist',
354
+ /**
355
+ * Momentary (`gatewayConnectorMomentaryRefusals`): an `exportGatewayArea` whose read local writes
356
+ * of the area kept overtaking until its bounded tries ran out (Linus ruling 124, 6(c)). The
357
+ * gateway answers no table rather than one a local write changed under the read; nothing changed,
358
+ * and the controller asks again later. A reader built on an earlier release of this package does
359
+ * not know the code, so it reads the answer as a failure without a named refusal, never as an
360
+ * export.
361
+ */
362
+ 'local-changing-during-export',
338
363
  ] as const);
339
364
 
340
365
  /** One of the reasons one side of the gateway connector answers instead of a request. */
@@ -342,6 +367,7 @@ export type TGatewayConnectorRefusal = typeof gatewayConnectorRefusals[number];
342
367
 
343
368
  /** A connector refusal as `TypedResponseError.errorData`; the error text carries its detail. */
344
369
  export interface IGatewayConnectorRefusalErrorData {
370
+ /** A verdict: never one of `gatewayConnectorMomentaryRefusals` (`IGatewayConnectorMomentaryRefusalErrorData`). */
345
371
  code: TGatewayConnectorRefusal;
346
372
  /** Never: the same request is refused again until the state the code names has changed. */
347
373
  retryable: false;
@@ -352,6 +378,74 @@ export const isGatewayConnectorRefusal = (valueArg: unknown): valueArg is TGatew
352
378
  (gatewayConnectorRefusals as readonly unknown[]).includes(valueArg)
353
379
  );
354
380
 
381
+ /**
382
+ * The connector refusals that describe the refusing side's own momentary state rather than a
383
+ * verdict on the request: the same request may be sent again later. Only these are answered with
384
+ * `retryable: true` (`IGatewayConnectorMomentaryRefusalErrorData`); every other refusal keeps
385
+ * `IGatewayConnectorRefusalErrorData` and `retryable: false`.
386
+ */
387
+ export const gatewayConnectorMomentaryRefusals = Object.freeze([
388
+ 'local-changing-during-export',
389
+ ] as const satisfies readonly TGatewayConnectorRefusal[]);
390
+
391
+ /** One of the momentary connector refusals. */
392
+ export type TGatewayConnectorMomentaryRefusal = typeof gatewayConnectorMomentaryRefusals[number];
393
+
394
+ /** A momentary connector refusal as `TypedResponseError.errorData`; the error text carries its detail. */
395
+ export interface IGatewayConnectorMomentaryRefusalErrorData {
396
+ code: TGatewayConnectorMomentaryRefusal;
397
+ /** Always: the refusing side is busy, and the same request may be sent again later. */
398
+ retryable: true;
399
+ }
400
+
401
+ /**
402
+ * Exactly one connector refusal payload: `retryable` is true exactly for a momentary code. A new
403
+ * reader's answer type; `IGatewayConnectorRefusalErrorData` keeps its shape for the readers and
404
+ * writers built before the momentary list.
405
+ */
406
+ export type TGatewayConnectorRefusalErrorData =
407
+ | (IGatewayConnectorRefusalErrorData & {
408
+ code: Exclude<TGatewayConnectorRefusal, TGatewayConnectorMomentaryRefusal>;
409
+ })
410
+ | IGatewayConnectorMomentaryRefusalErrorData;
411
+
412
+ /** True for one of `gatewayConnectorMomentaryRefusals`. */
413
+ export const isGatewayConnectorMomentaryRefusal = (
414
+ valueArg: unknown,
415
+ ): valueArg is TGatewayConnectorMomentaryRefusal => (
416
+ (gatewayConnectorMomentaryRefusals as readonly unknown[]).includes(valueArg)
417
+ );
418
+
419
+ /** The `errorData` a connector refusal is answered with: `retryable` true exactly for a momentary code. */
420
+ export const createGatewayConnectorRefusalErrorData = (
421
+ codeArg: TGatewayConnectorRefusal,
422
+ ): TGatewayConnectorRefusalErrorData => Object.freeze(isGatewayConnectorMomentaryRefusal(codeArg)
423
+ ? { code: codeArg, retryable: true as const }
424
+ : { code: codeArg, retryable: false as const });
425
+
426
+ const refusalErrorDataKeyList = ['code', 'retryable'] as const;
427
+ const refusalErrorDataKeys: TCompleteKeyList<IGatewayConnectorRefusalErrorData, typeof refusalErrorDataKeyList> =
428
+ refusalErrorDataKeyList;
429
+
430
+ /**
431
+ * The connector refusal an error payload states, or `null` when it is not exactly one: a payload
432
+ * with another member, an unknown code, or a `retryable` that disagrees with the code (a momentary
433
+ * code with `false`, a verdict with `true`) reads as no answer from the peer, never as a verdict.
434
+ *
435
+ * Readers built on an earlier release of this package (dcrouter's and Cloudly's
436
+ * `readGatewayConnectorRefusal` before they move to this release) look at `code` alone and test it
437
+ * with their own `isGatewayConnectorRefusal`: a momentary code is unknown to them, so they read the
438
+ * answer as a failure that names no refusal, never as a result, and they never read `retryable`.
439
+ */
440
+ export const readGatewayConnectorRefusalErrorData = (valueArg: unknown): TGatewayConnectorRefusalErrorData | null => {
441
+ const record = readExactDataRecord(valueArg, refusalErrorDataKeys);
442
+ if (record === null || !isGatewayConnectorRefusal(record.code)
443
+ || record.retryable !== isGatewayConnectorMomentaryRefusal(record.code)) {
444
+ return null;
445
+ }
446
+ return createGatewayConnectorRefusalErrorData(record.code);
447
+ };
448
+
355
449
  /**
356
450
  * Every reason a gateway refuses an area of a desired state it accepted as a whole. Each is
357
451
  * reported per area in `IGatewayAreaApplyState.refusal`. A refusal of the whole document (stale,
@@ -959,6 +1053,98 @@ export const validateGatewayAreaExport = (
959
1053
  ];
960
1054
  };
961
1055
 
1056
+ /**
1057
+ * Validates a `getGatewayRouteDnsResults` request and names every reason it is refused: 1 to
1058
+ * `gatewayConnectorContract.maximumRouteDnsHostnames` lowercase hostnames (a leading `*.` label
1059
+ * allowed), strictly ascending by `compareWireTokens`, exact keys.
1060
+ */
1061
+ export const validateGatewayRouteDnsResultsRequest = (
1062
+ valueArg: unknown,
1063
+ pathArg = 'gateway route DNS results request',
1064
+ ): string[] => {
1065
+ const record = readExactDataRecord(valueArg, ['hostnames']);
1066
+ if (record === null) {
1067
+ return [`${pathArg} must use its exact schema`];
1068
+ }
1069
+ const maximum = gatewayConnectorContract.maximumRouteDnsHostnames;
1070
+ const hostnames = readDataElements(record.hostnames, maximum);
1071
+ if (hostnames === null || hostnames.length === 0) {
1072
+ return [`${pathArg} hostnames must be a dense list of 1 to ${maximum} hostnames`];
1073
+ }
1074
+ const errors: string[] = [];
1075
+ hostnames.forEach((hostnameArg, indexArg) => {
1076
+ const labels = typeof hostnameArg === 'string' ? hostnameArg.split('.') : [];
1077
+ if (typeof hostnameArg !== 'string' || hostnameArg.length > gatewayConnectorContract.maximumHostnameLength
1078
+ || labels.length < 2
1079
+ || !labels.every((labelArg, labelIndexArg) => (labelIndexArg === 0 && labelArg === '*')
1080
+ || dnsZoneLabel.test(labelArg))) {
1081
+ errors.push(`${pathArg} hostnames[${indexArg}] must be a lowercase hostname without a trailing dot`);
1082
+ } else if (indexArg > 0 && typeof hostnames[indexArg - 1] === 'string'
1083
+ && compareWireTokens(hostnames[indexArg - 1] as string, hostnameArg) >= 0) {
1084
+ errors.push(`${pathArg} hostnames must be strictly ascending`);
1085
+ }
1086
+ });
1087
+ return errors;
1088
+ };
1089
+
1090
+ /**
1091
+ * Validates a `getGatewayRouteDnsResults` answer against the request it answers and names every
1092
+ * reason it is refused: exact keys; `routesRevision` `null` or exactly `{ epoch, revision }`, the
1093
+ * positive safe integers a desired state is ordered by (`compareControllerRevisionOrder`); `results`
1094
+ * one entry per requested hostname, each once and in the request's strictly ascending order, each
1095
+ * exactly `{ hostname, result }` with `result` judged by `validateGatewayRouteDnsResult`. A request
1096
+ * that fails `validateGatewayRouteDnsResultsRequest` is refused first, since no answer can match it.
1097
+ * The controller judges every answer with this before it acts on one, never with a copy (Linus
1098
+ * ruling 124, part 2 (ii)).
1099
+ */
1100
+ export const validateGatewayRouteDnsResultsResponse = (
1101
+ valueArg: unknown,
1102
+ requestArg: Readonly<{ hostnames: readonly string[] }>,
1103
+ pathArg = 'gateway route DNS results response',
1104
+ ): string[] => {
1105
+ const requestErrors = validateGatewayRouteDnsResultsRequest(requestArg, `${pathArg} request`);
1106
+ if (requestErrors.length > 0) {
1107
+ return requestErrors;
1108
+ }
1109
+ const record = readExactDataRecord(valueArg, ['routesRevision', 'results']);
1110
+ if (record === null) {
1111
+ return [`${pathArg} must use its exact schema`];
1112
+ }
1113
+ const errors: string[] = [];
1114
+ if (record.routesRevision !== null) {
1115
+ const revision = readExactDataRecord(record.routesRevision, ['epoch', 'revision']);
1116
+ errors.push(...(revision === null
1117
+ ? [`${pathArg} routesRevision must be null or use its exact schema`]
1118
+ : [
1119
+ ...validateControllerCounter(revision.epoch, `${pathArg} routesRevision epoch`),
1120
+ ...validateControllerCounter(revision.revision, `${pathArg} routesRevision revision`),
1121
+ ]));
1122
+ }
1123
+ const hostnames = requestArg.hostnames;
1124
+ const results = readDataElements(record.results, gatewayConnectorContract.maximumRouteDnsHostnames);
1125
+ if (results === null) {
1126
+ errors.push(`${pathArg} results must be a dense list of at most `
1127
+ + `${gatewayConnectorContract.maximumRouteDnsHostnames} entries`);
1128
+ return errors;
1129
+ }
1130
+ if (results.length !== hostnames.length) {
1131
+ errors.push(`${pathArg} results must answer each of the ${hostnames.length} requested hostnames once`);
1132
+ }
1133
+ results.forEach((entryArg, indexArg) => {
1134
+ const entryPath = `${pathArg} results[${indexArg}]`;
1135
+ const entry = readExactDataRecord(entryArg, ['hostname', 'result']);
1136
+ if (entry === null) {
1137
+ errors.push(`${entryPath} must use its exact schema`);
1138
+ return;
1139
+ }
1140
+ if (indexArg >= hostnames.length || entry.hostname !== hostnames[indexArg]) {
1141
+ errors.push(`${entryPath} hostname must be requested hostname ${indexArg + 1}, in request order`);
1142
+ }
1143
+ errors.push(...validateGatewayRouteDnsResult(entry.result, `${entryPath} result`));
1144
+ });
1145
+ return errors;
1146
+ };
1147
+
962
1148
  const validateGatewayClientId = (valueArg: unknown, pathArg: string): string[] => (
963
1149
  isGatewayClientIdText(valueArg)
964
1150
  ? []
package/ts/data/index.ts CHANGED
@@ -28,6 +28,8 @@ export * from './event.js';
28
28
  export * from './externalregistry.js';
29
29
  export * from './fleetcutover.js';
30
30
  export * from './gateway.js';
31
+ export * from './gatewayareas.js';
32
+ export * from './gatewayareas.validation.js';
31
33
  export * from './gatewayconnector.js';
32
34
  export * from './hostedapp.js';
33
35
  export * from './image.js';
@@ -0,0 +1,38 @@
1
+ /**
2
+ * The one reader of engine key vocabulary entries (`gatewayAreaRefusedEngineKeys`): an entry is a
3
+ * bare key name (refused under any parent) or exactly `parent.name` (refused only directly inside
4
+ * `parent`). Answers each refused name with the parent keys it is refused under, `null` standing for
5
+ * any parent.
6
+ *
7
+ * Throws for any other entry (an empty name, an empty part, three or more parts such as `a.b.c`):
8
+ * the matcher looks at one parent key only, so such an entry would never match and its key would
9
+ * pass unrefused. Its callers run it at module load, so a wrong vocabulary entry fails the import.
10
+ */
11
+ export const readEngineKeyEntries = (entriesArg: readonly string[]): ReadonlyMap<string, ReadonlySet<string | null>> => {
12
+ const parents = new Map<string, Set<string | null>>();
13
+ for (const entry of entriesArg) {
14
+ const parts = entry.split('.');
15
+ if (parts.length > 2 || parts.some((partArg) => partArg === '')) {
16
+ throw new Error(`Engine key vocabulary entry ${JSON.stringify(entry)} is neither a key name nor parent.name`);
17
+ }
18
+ const name = parts[parts.length - 1];
19
+ const parentsOfName = parents.get(name) ?? new Set<string | null>();
20
+ parentsOfName.add(parts.length === 1 ? null : parts[0]);
21
+ parents.set(name, parentsOfName);
22
+ }
23
+ return parents;
24
+ };
25
+
26
+ /**
27
+ * Whether `readEngineKeyEntries`' answer refuses `keyArg` directly inside the key `parentArg`
28
+ * (`null` at the root of the walked value): its bare name refuses it under any parent, its
29
+ * `parent.name` only there.
30
+ */
31
+ export const matchesEngineKeyEntry = (
32
+ entriesArg: ReadonlyMap<string, ReadonlySet<string | null>>,
33
+ parentArg: string | null,
34
+ keyArg: string,
35
+ ): boolean => {
36
+ const parents = entriesArg.get(keyArg);
37
+ return parents !== undefined && (parents.has(null) || (parentArg !== null && parents.has(parentArg)));
38
+ };
@@ -11,6 +11,7 @@ import type {
11
11
  IRegisterGatewaySessionRequest,
12
12
  TGatewayConfigArea,
13
13
  } from '../data/gatewayconnector.js';
14
+ import type { IGatewayRouteDnsResult } from '../data/gateway.js';
14
15
  import type { ILocationScope } from '../data/location.js';
15
16
  import type { IIdentityCredential } from '../data/user.js';
16
17
 
@@ -21,6 +22,9 @@ import type { IIdentityCredential } from '../data/user.js';
21
22
  * the socket the gateway opened, and `Admin_Cloudly` requests are an administrator's. Either side
22
23
  * refuses a request by one of `data.gatewayConnectorRefusals`, as a `TypedResponseError` whose
23
24
  * `errorData` is `data.IGatewayConnectorRefusalErrorData`.
25
+ * A momentary refusal (`data.gatewayConnectorMomentaryRefusals`) carries
26
+ * `data.IGatewayConnectorMomentaryRefusalErrorData` (`retryable: true`) instead;
27
+ * `data.readGatewayConnectorRefusalErrorData` reads both.
24
28
  */
25
29
 
26
30
  /**
@@ -110,6 +114,9 @@ export interface IReq_Cloudly_Gateway_ApplyDesiredState extends plugins.typedreq
110
114
  * writes an import revision of it and pushes it back with `takeover.localDigest` set to `digest`.
111
115
  * An area the gateway does not take from a controller (`controllerAreas`) is refused
112
116
  * `unsupported-area` (`data.gatewayConnectorRefusals`).
117
+ * Its only momentary refusal is `local-changing-during-export`
118
+ * (`data.gatewayConnectorMomentaryRefusals`, answered `retryable: true`): local writes of the area
119
+ * kept overtaking the read, so the gateway answers no table; the controller asks again later.
113
120
  */
114
121
  export interface IReq_Cloudly_Gateway_ExportGatewayArea extends plugins.typedrequestInterfaces.implementsTR<
115
122
  plugins.typedrequestInterfaces.ITypedRequest,
@@ -123,6 +130,44 @@ export interface IReq_Cloudly_Gateway_ExportGatewayArea extends plugins.typedreq
123
130
  response: IGatewayAreaExport;
124
131
  }
125
132
 
133
+ /**
134
+ * The controller reads what the gateway's route DNS reconciler last observed for some hostnames.
135
+ * It is observed state, never a member of an area table, and the status document does not carry
136
+ * it. The answer holds one entry per requested hostname, in request order, each an
137
+ * `IGatewayRouteDnsResult`; a hostname no enabled route serves is answered with status
138
+ * `not-applicable`. `routesRevision` is the controller position of the routes table the reconciler
139
+ * pass behind the answer ran against, ordered by `data.compareControllerRevisionOrder` like a
140
+ * desired state, and `null` when that pass ran against a routes table no controller pushed or when
141
+ * no pass has observed one of the requested hostnames yet (Linus ruling 125, Q-b2; such a hostname
142
+ * answers the retryable `zone-unavailable`). A request that fails
143
+ * `data.validateGatewayRouteDnsResultsRequest` is refused `request-invalid`. The controller judges
144
+ * every answer with `data.validateGatewayRouteDnsResultsResponse` before it acts on it, and one text
145
+ * over `data.gatewayRouteDnsResultContract.maximumTextLength` refuses the whole answer: the gateway
146
+ * shortens every text (a provider's error in `message`, for example) to that bound before it answers.
147
+ *
148
+ * Peer gate: a gateway built before this request answers no handler for it. The controller calls it
149
+ * only on a gateway whose hello (`data.IRegisterGatewaySessionRequest.interfacesVersion`) states the
150
+ * release of this package that declares it or a later one, the rule this package's validators set
151
+ * for every new member and method (a controller sends one only to a gateway whose stated
152
+ * `interfacesVersion` knows it); a gateway therefore moves to that release only together with
153
+ * serving the call.
154
+ */
155
+ export interface IReq_Cloudly_Gateway_GetGatewayRouteDnsResults extends plugins.typedrequestInterfaces.implementsTR<
156
+ plugins.typedrequestInterfaces.ITypedRequest,
157
+ IReq_Cloudly_Gateway_GetGatewayRouteDnsResults
158
+ > {
159
+ method: 'getGatewayRouteDnsResults';
160
+ /** `data.validateGatewayRouteDnsResultsRequest`. */
161
+ request: {
162
+ hostnames: string[];
163
+ };
164
+ /** `data.validateGatewayRouteDnsResultsResponse`. */
165
+ response: {
166
+ routesRevision: { epoch: number; revision: number } | null;
167
+ results: { hostname: string; result: IGatewayRouteDnsResult }[];
168
+ };
169
+ }
170
+
126
171
  /**
127
172
  * An administrator makes a one-time adoption code, either for a new gateway in an existing
128
173
  * location or for an existing gateway record (re-adoption). Adoption never creates a location.