@serve.zone/interfaces 32.75.0 → 32.77.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.
Files changed (50) hide show
  1. package/changelog.md +22 -0
  2. package/dist_ts/00_commitinfo_data.js +1 -1
  3. package/dist_ts/data/backup.d.ts +3 -1
  4. package/dist_ts/data/backup.js +1 -1
  5. package/dist_ts/data/controller.d.ts +92 -0
  6. package/dist_ts/data/controller.js +138 -0
  7. package/dist_ts/data/gatewayconnector.d.ts +347 -0
  8. package/dist_ts/data/gatewayconnector.js +594 -0
  9. package/dist_ts/data/index.d.ts +2 -0
  10. package/dist_ts/data/index.js +3 -1
  11. package/dist_ts/data/location.d.ts +3 -1
  12. package/dist_ts/data/location.js +7 -5
  13. package/dist_ts/data/user.d.ts +8 -2
  14. package/dist_ts/private/controllercontract.d.ts +10 -0
  15. package/dist_ts/private/controllercontract.js +64 -0
  16. package/dist_ts/private/gatewayclientid.d.ts +6 -0
  17. package/dist_ts/private/gatewayclientid.js +11 -0
  18. package/dist_ts/requests/baremetal.d.ts +2 -1
  19. package/dist_ts/requests/cluster.d.ts +5 -3
  20. package/dist_ts/requests/gateway.d.ts +20 -3
  21. package/dist_ts/requests/gatewayconnector.d.ts +142 -0
  22. package/dist_ts/requests/gatewayconnector.js +2 -0
  23. package/dist_ts/requests/index.d.ts +2 -1
  24. package/dist_ts/requests/index.js +3 -2
  25. package/dist_ts/requests/node.d.ts +4 -1
  26. package/dist_ts/requests/settings.js +3 -5
  27. package/dist_ts/runtime.corestore.database.d.ts +23 -0
  28. package/dist_ts/runtime.corestore.database.js +41 -0
  29. package/dist_ts/runtime.d.ts +1 -0
  30. package/dist_ts/runtime.js +2 -1
  31. package/package.json +2 -2
  32. package/readme.md +146 -5
  33. package/ts/00_commitinfo_data.ts +1 -1
  34. package/ts/data/backup.ts +3 -1
  35. package/ts/data/controller.ts +218 -0
  36. package/ts/data/gatewayconnector.ts +955 -0
  37. package/ts/data/index.ts +2 -0
  38. package/ts/data/location.ts +6 -4
  39. package/ts/data/user.ts +8 -2
  40. package/ts/private/controllercontract.ts +74 -0
  41. package/ts/private/gatewayclientid.ts +13 -0
  42. package/ts/requests/baremetal.ts +2 -1
  43. package/ts/requests/cluster.ts +5 -3
  44. package/ts/requests/gateway.ts +20 -3
  45. package/ts/requests/gatewayconnector.ts +179 -0
  46. package/ts/requests/index.ts +2 -0
  47. package/ts/requests/node.ts +4 -1
  48. package/ts/requests/settings.ts +2 -4
  49. package/ts/runtime.corestore.database.ts +64 -0
  50. package/ts/runtime.ts +1 -0
@@ -0,0 +1,218 @@
1
+ import {
2
+ validateControllerCounter,
3
+ validateControllerIdentifier,
4
+ validateControllerNonNegativeInteger,
5
+ validateControllerOrigin,
6
+ validateControllerStrictJson,
7
+ } from '../private/controllercontract.js';
8
+ import { readDataElements, readDataProperty, readExactDataRecord } from '../private/dataproperties.js';
9
+ import type { TCompleteKeyList } from '../private/keylist.js';
10
+
11
+ /**
12
+ * The controller as the components that dial it know it: which controller they belong to, which
13
+ * epoch it is in and where to reach it.
14
+ *
15
+ * A dialling component (a gateway today) stores this when it is adopted and replaces it with every
16
+ * answer the controller gives it. It refuses any other `controllerId` and any older `epoch`: a
17
+ * takeover and a database restore both raise the epoch, so the controller that answers with the
18
+ * highest one is the one whose revisions count. Later controller contracts extend this shape and
19
+ * never define a second one.
20
+ */
21
+ export interface IControllerPeerSet {
22
+ /** Fixed for the controller's life; stored at adoption. */
23
+ controllerId: string;
24
+ /** A positive safe integer; never lowered while the controller lives. */
25
+ epoch: number;
26
+ /** The canonical `https` origin of the active controller, dialled first. */
27
+ activeOrigin: string;
28
+ /** The canonical `https` origins of the shadow controllers, dialled in this order after the active one. */
29
+ shadowOrigins: string[];
30
+ }
31
+
32
+ export const controllerPeerSetContract = Object.freeze({
33
+ maximumOriginLength: 2048,
34
+ maximumShadowOrigins: 16,
35
+ } as const);
36
+
37
+ /**
38
+ * Negative, zero or positive as the left controller position is older than, the same as or newer
39
+ * than the right one: the epoch decides first and the revision only within one epoch, so a higher
40
+ * epoch with a lower revision (after a takeover or a restore) is still newer.
41
+ */
42
+ export const compareControllerRevisionOrder = (
43
+ leftArg: Readonly<{ epoch: number; revision: number }>,
44
+ rightArg: Readonly<{ epoch: number; revision: number }>,
45
+ ): number => Math.sign(leftArg.epoch - rightArg.epoch) || Math.sign(leftArg.revision - rightArg.revision);
46
+
47
+ /** The bounds of `validateControllerRevision`; they may only rise, never fall. */
48
+ export const controllerRevisionContract = Object.freeze({
49
+ maximumDisplayNameLength: 256,
50
+ maximumChanges: 1024,
51
+ } as const);
52
+
53
+ /**
54
+ * One record of the controller-wide revision sequence, written in the same transaction as the
55
+ * change it records (`validateControllerRevision`). A secret appears in `changes` by its ids and
56
+ * versions only, never by value.
57
+ */
58
+ export interface IControllerRevision {
59
+ /** A positive safe integer from the one controller-wide sequence. */
60
+ revision: number;
61
+ /** The controller epoch the revision was written in. */
62
+ epoch: number;
63
+ /** Unix milliseconds on the controller's clock. */
64
+ at: number;
65
+ actor: {
66
+ kind: 'person' | 'machine' | 'system';
67
+ /**
68
+ * The acting identity's subject, the controller's user id of the person or machine; `null`
69
+ * exactly for the controller itself (`system`).
70
+ */
71
+ sub: string | null;
72
+ /** 1 to `controllerRevisionContract.maximumDisplayNameLength` characters. */
73
+ displayName: string;
74
+ };
75
+ kind: 'change' | 'adoption' | 'import' | 'revert';
76
+ /**
77
+ * 1 to `controllerRevisionContract.maximumChanges` changes, each object (`objectKind`,
78
+ * `objectId`) at most once.
79
+ */
80
+ changes: {
81
+ objectKind: string;
82
+ objectId: string;
83
+ /** The object before the change; `null` when the change created it. */
84
+ before: unknown;
85
+ /** The object after the change; `null` when the change deleted it. */
86
+ after: unknown;
87
+ }[];
88
+ }
89
+
90
+ /**
91
+ * Validates one controller peer set and names every reason it is refused.
92
+ *
93
+ * Shape only: whether the controller id is the one a gateway stored, and whether the epoch is not
94
+ * older than its stored one, is the dialling component's decision against its own state.
95
+ */
96
+ export const validateControllerPeerSet = (
97
+ valueArg: unknown,
98
+ pathArg = 'controller peer set',
99
+ ): string[] => {
100
+ const record = readExactDataRecord(valueArg, ['controllerId', 'epoch', 'activeOrigin', 'shadowOrigins']);
101
+ if (record === null) {
102
+ return [`${pathArg} must use its exact schema`];
103
+ }
104
+ const { maximumOriginLength, maximumShadowOrigins } = controllerPeerSetContract;
105
+ const errors = [
106
+ ...validateControllerIdentifier(record.controllerId, `${pathArg} controllerId`),
107
+ ...validateControllerCounter(record.epoch, `${pathArg} epoch`),
108
+ ...validateControllerOrigin(record.activeOrigin, `${pathArg} activeOrigin`, maximumOriginLength),
109
+ ];
110
+ const shadowOrigins = readDataElements(record.shadowOrigins, maximumShadowOrigins);
111
+ if (shadowOrigins === null) {
112
+ errors.push(`${pathArg} shadowOrigins must be a dense list of at most ${maximumShadowOrigins} origins`);
113
+ return errors;
114
+ }
115
+ shadowOrigins.forEach((originArg, indexArg) => {
116
+ errors.push(...validateControllerOrigin(originArg, `${pathArg} shadowOrigins[${indexArg}]`,
117
+ maximumOriginLength));
118
+ });
119
+ const origins = [record.activeOrigin, ...shadowOrigins];
120
+ if (new Set(origins).size !== origins.length) {
121
+ errors.push(`${pathArg} must name each origin once`);
122
+ }
123
+ return errors;
124
+ };
125
+
126
+ const revisionKeyList = ['revision', 'epoch', 'at', 'actor', 'kind', 'changes'] as const;
127
+ const revisionKeys: TCompleteKeyList<IControllerRevision, typeof revisionKeyList> = revisionKeyList;
128
+ const actorKeyList = ['kind', 'sub', 'displayName'] as const;
129
+ const actorKeys: TCompleteKeyList<IControllerRevision['actor'], typeof actorKeyList> = actorKeyList;
130
+ const changeKeyList = ['objectKind', 'objectId', 'before', 'after'] as const;
131
+ const changeKeys: TCompleteKeyList<IControllerRevision['changes'][number], typeof changeKeyList> = changeKeyList;
132
+ const actorKinds: readonly IControllerRevision['actor']['kind'][] = ['person', 'machine', 'system'];
133
+ const revisionKinds: readonly IControllerRevision['kind'][] = ['change', 'adoption', 'import', 'revert'];
134
+
135
+ const validateRevisionActor = (valueArg: unknown, pathArg: string): string[] => {
136
+ const actor = readExactDataRecord(valueArg, actorKeys);
137
+ if (actor === null) {
138
+ return [`${pathArg} must use its exact schema`];
139
+ }
140
+ const errors: string[] = [];
141
+ if (!actorKinds.some((kindArg) => kindArg === actor.kind)) {
142
+ errors.push(`${pathArg} kind must be one of ${actorKinds.join(', ')}`);
143
+ } else if (actor.kind === 'system') {
144
+ if (actor.sub !== null) {
145
+ errors.push(`${pathArg} sub must be null for the system actor`);
146
+ }
147
+ } else {
148
+ errors.push(...validateControllerIdentifier(actor.sub, `${pathArg} sub`));
149
+ }
150
+ const { maximumDisplayNameLength } = controllerRevisionContract;
151
+ if (typeof actor.displayName !== 'string' || actor.displayName.length === 0
152
+ || actor.displayName.length > maximumDisplayNameLength) {
153
+ errors.push(`${pathArg} displayName must be a string of 1 to ${maximumDisplayNameLength} characters`);
154
+ }
155
+ return errors;
156
+ };
157
+
158
+ const validateRevisionChange = (valueArg: unknown, pathArg: string): string[] => {
159
+ const change = readExactDataRecord(valueArg, changeKeys);
160
+ if (change === null) {
161
+ return [`${pathArg} must use its exact schema`];
162
+ }
163
+ const errors = [
164
+ ...validateControllerIdentifier(change.objectKind, `${pathArg} objectKind`),
165
+ ...validateControllerIdentifier(change.objectId, `${pathArg} objectId`),
166
+ ...validateControllerStrictJson(change.before, `${pathArg} before`),
167
+ ...validateControllerStrictJson(change.after, `${pathArg} after`),
168
+ ];
169
+ if (change.before === null && change.after === null) {
170
+ errors.push(`${pathArg} must state the object before or after the change`);
171
+ }
172
+ return errors;
173
+ };
174
+
175
+ /**
176
+ * Validates one controller revision and names every reason it is refused.
177
+ *
178
+ * The record alone: whether the revision is the next of the controller's sequence, its epoch the
179
+ * controller's current one, and its changes what the transaction wrote, is the controller's to
180
+ * judge against its own store.
181
+ */
182
+ export const validateControllerRevision = (
183
+ valueArg: unknown,
184
+ pathArg = 'controller revision',
185
+ ): string[] => {
186
+ const record = readExactDataRecord(valueArg, revisionKeys);
187
+ if (record === null) {
188
+ return [`${pathArg} must use its exact schema`];
189
+ }
190
+ const errors = [
191
+ ...validateControllerCounter(record.revision, `${pathArg} revision`),
192
+ ...validateControllerCounter(record.epoch, `${pathArg} epoch`),
193
+ ...validateControllerNonNegativeInteger(record.at, `${pathArg} at`),
194
+ ...validateRevisionActor(record.actor, `${pathArg} actor`),
195
+ ];
196
+ if (!revisionKinds.some((kindArg) => kindArg === record.kind)) {
197
+ errors.push(`${pathArg} kind must be one of ${revisionKinds.join(', ')}`);
198
+ }
199
+ const { maximumChanges } = controllerRevisionContract;
200
+ const changes = readDataElements(record.changes, maximumChanges);
201
+ if (changes === null || changes.length === 0) {
202
+ errors.push(`${pathArg} changes must be a dense list of 1 to ${maximumChanges} changes`);
203
+ return errors;
204
+ }
205
+ changes.forEach((changeArg, indexArg) => {
206
+ errors.push(...validateRevisionChange(changeArg, `${pathArg} changes[${indexArg}]`));
207
+ });
208
+ // A change whose object is not stated as two strings is refused by its own validator and left out.
209
+ const objects = changes.flatMap((changeArg) => {
210
+ const objectKind = readDataProperty(changeArg, 'objectKind')?.value;
211
+ const objectId = readDataProperty(changeArg, 'objectId')?.value;
212
+ return typeof objectKind === 'string' && typeof objectId === 'string' ? [`${objectKind}\u0000${objectId}`] : [];
213
+ });
214
+ if (new Set(objects).size !== objects.length) {
215
+ errors.push(`${pathArg} changes must name each object once`);
216
+ }
217
+ return errors;
218
+ };