@serve.zone/interfaces 32.112.0 → 32.114.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.
@@ -0,0 +1,386 @@
1
+ import type { ITypedRequest, implementsTR } from '@api.global/typedrequest-interfaces';
2
+ import type * as plugins from '../plugins.js';
3
+ import type { IIdentityCredential } from '../data/user.js';
4
+ import { isGatewayAreaRowId } from '../data/gatewayareas.js';
5
+ import { isSha256Digest, type TSha256Digest } from '../data/immutableimage.js';
6
+ import { compareWireTokens } from '../private/canonicaljson.js';
7
+ import { readDataElements, readDataProperties, readExactDataRecord } from '../private/dataproperties.js';
8
+
9
+ type ICert = plugins.tsclass.network.ICert;
10
+
11
+ /**
12
+ * A stored legacy selector, never an exact-issuance record. The gateway issues the ID and changes
13
+ * it on recreation. Content revision covers every selected backing document, including secrets;
14
+ * usage revision covers all current consumers and the selection's effects on shared base domains.
15
+ * Neither revision reveals its inputs. Both are compared again inside the gateway's mutation lock.
16
+ */
17
+ export interface IGatewayCertificateStoreEntry {
18
+ id: string;
19
+ /** Display-safe preview. Export by ID to obtain the original legacy domain string. */
20
+ domainName: string;
21
+ domainNameAltered: boolean;
22
+ backingKinds: ('legacy-acme' | 'proxy')[];
23
+ created: number;
24
+ validUntil: number;
25
+ exportable: boolean;
26
+ contentRevision: TSha256Digest;
27
+ usageRevision: TSha256Digest;
28
+ /** Counts cover the complete consumer set, including members omitted from the display sample. */
29
+ consumerCounts: IGatewayCertificateStoreConsumerCounts;
30
+ consumersTruncated: boolean;
31
+ consumers: IGatewayCertificateStoreConsumer[];
32
+ }
33
+
34
+ export interface IGatewayCertificateStoreConsumer {
35
+ kind: 'route' | 'smtp' | 'remoteIngress';
36
+ id: string;
37
+ }
38
+
39
+ export interface IGatewayCertificateStoreConsumerCounts {
40
+ routes: number;
41
+ smtp: number;
42
+ remoteIngress: number;
43
+ }
44
+
45
+ export interface IGatewayCertificateStorePage {
46
+ entries: IGatewayCertificateStoreEntry[];
47
+ /** The last returned ID, only when a later entry exists. */
48
+ nextAfter?: string;
49
+ }
50
+
51
+ export const gatewayCertificateStoreBounds = Object.freeze({
52
+ page: 8,
53
+ /** One extra row from a storage query proves whether another page exists. */
54
+ storageWindow: 9,
55
+ consumersPerEntry: 64,
56
+ domainNamePreview: 253,
57
+ });
58
+
59
+ /** The boolean flags explicitly acknowledge stored-only effects on the named consumer classes. */
60
+ export interface IGatewayCertificateStoreImpactConfirmation {
61
+ routes: boolean;
62
+ smtp: boolean;
63
+ remoteIngress: boolean;
64
+ }
65
+
66
+ export interface IGatewayCertificateStoreImport {
67
+ certificate: ICert;
68
+ /** Null creates; an observed gateway-issued selector ID replaces. */
69
+ id: string | null;
70
+ expectedContentRevision: TSha256Digest | null;
71
+ expectedUsageRevision: TSha256Digest | null;
72
+ confirmImpact: IGatewayCertificateStoreImpactConfirmation;
73
+ }
74
+
75
+ export interface IGatewayCertificateStoreTarget {
76
+ id: string;
77
+ expectedContentRevision: TSha256Digest;
78
+ }
79
+
80
+ export interface IGatewayCertificateStoreDelete extends IGatewayCertificateStoreTarget {
81
+ expectedUsageRevision: TSha256Digest;
82
+ confirmImpact: IGatewayCertificateStoreImpactConfirmation;
83
+ }
84
+
85
+ type TGatewayCertificateStoreCommonRefusal = 'invalid-request' | 'store-unavailable';
86
+ type TGatewayCertificateStoreMutationRefusal = TGatewayCertificateStoreCommonRefusal
87
+ | 'stale-content' | 'stale-usage' | 'impact-confirmation-required' | 'outcome-unknown';
88
+ type TGatewayCertificateStoreExportRefusal = TGatewayCertificateStoreCommonRefusal
89
+ | 'not-found' | 'not-exportable' | 'stale-content';
90
+ type TGatewayCertificateStoreAdminRefusal = 'administrator-required' | 'gateway-disconnected'
91
+ | 'gateway-unavailable' | 'gateway-unsupported';
92
+ export type TGatewayCertificateStoreInventoryRefusal = TGatewayCertificateStoreCommonRefusal;
93
+ export type TGatewayCertificateStoreImportRefusal = TGatewayCertificateStoreMutationRefusal;
94
+ export type TGatewayCertificateStoreDeleteRefusal = TGatewayCertificateStoreMutationRefusal;
95
+ export type TGatewayCertificateStorePrivateExportRefusal = TGatewayCertificateStoreExportRefusal;
96
+ export type TGatewayCertificateStoreAdminInventoryRefusal = TGatewayCertificateStoreInventoryRefusal
97
+ | TGatewayCertificateStoreAdminRefusal;
98
+ export type TGatewayCertificateStoreAdminMutationRefusal = TGatewayCertificateStoreMutationRefusal
99
+ | TGatewayCertificateStoreAdminRefusal | 'fresh-sign-in-required';
100
+ export type TGatewayCertificateStoreAdminExportRefusal = TGatewayCertificateStoreExportRefusal
101
+ | TGatewayCertificateStoreAdminRefusal | 'fresh-sign-in-required';
102
+ export type TGatewayCertificateStoreWriteResult<TRefusal extends string> =
103
+ | { ok: true; outcome: 'written' | 'unchanged'; id: string; contentRevision: TSha256Digest; usageRevision: TSha256Digest }
104
+ | { ok: false; reason: TRefusal };
105
+ export type TGatewayCertificateStoreDeleteResult<TRefusal extends string> =
106
+ | { ok: true; outcome: 'deleted' | 'unchanged' }
107
+ | { ok: false; reason: TRefusal };
108
+ export type TGatewayCertificateStoreExportResult<TRefusal extends string> =
109
+ | { ok: true; certificate: ICert }
110
+ | { ok: false; reason: TRefusal };
111
+ export type TGatewayCertificateStoreInventoryResult<TRefusal extends string> =
112
+ | { ok: true; page: IGatewayCertificateStorePage }
113
+ | { ok: false; reason: TRefusal };
114
+
115
+ interface IAdminTarget { gatewayId: string; identity: IIdentityCredential }
116
+
117
+ /** Only the adopted controller session can inspect the gateway's legacy stores. */
118
+ export interface IReq_Cloudly_Gateway_ReadCertificateStore extends implementsTR<ITypedRequest, IReq_Cloudly_Gateway_ReadCertificateStore> {
119
+ method: 'readGatewayCertificateStore';
120
+ request: { after?: string };
121
+ response: TGatewayCertificateStoreInventoryResult<TGatewayCertificateStoreInventoryRefusal>;
122
+ }
123
+ export interface IReq_Admin_Cloudly_GetCertificateStore extends implementsTR<ITypedRequest, IReq_Admin_Cloudly_GetCertificateStore> {
124
+ method: 'getGatewayCertificateStore';
125
+ request: IAdminTarget & { after?: string };
126
+ response: TGatewayCertificateStoreInventoryResult<TGatewayCertificateStoreAdminInventoryRefusal>;
127
+ }
128
+ /** Credential-bearing connector request; Cloudly's HTTP leg requires credential-safe transport. */
129
+ export interface IReq_Cloudly_Gateway_ImportStoredCertificate extends implementsTR<ITypedRequest, IReq_Cloudly_Gateway_ImportStoredCertificate> {
130
+ method: 'importGatewayStoredCertificate';
131
+ request: IGatewayCertificateStoreImport;
132
+ response: TGatewayCertificateStoreWriteResult<TGatewayCertificateStoreImportRefusal>;
133
+ }
134
+ export interface IReq_Admin_Cloudly_ImportStoredCertificate extends implementsTR<ITypedRequest, IReq_Admin_Cloudly_ImportStoredCertificate> {
135
+ method: 'importGatewayStoredCertificateForAdmin';
136
+ request: IAdminTarget & IGatewayCertificateStoreImport;
137
+ response: TGatewayCertificateStoreWriteResult<TGatewayCertificateStoreAdminMutationRefusal>;
138
+ }
139
+ /** Credential-bearing answer: never log, cache, or put it into browser storage. */
140
+ export interface IReq_Cloudly_Gateway_ExportStoredCertificate extends implementsTR<ITypedRequest, IReq_Cloudly_Gateway_ExportStoredCertificate> {
141
+ method: 'exportGatewayStoredCertificate';
142
+ request: IGatewayCertificateStoreTarget;
143
+ response: TGatewayCertificateStoreExportResult<TGatewayCertificateStorePrivateExportRefusal>;
144
+ }
145
+ export interface IReq_Admin_Cloudly_ExportStoredCertificate extends implementsTR<ITypedRequest, IReq_Admin_Cloudly_ExportStoredCertificate> {
146
+ method: 'exportGatewayStoredCertificateForAdmin';
147
+ request: IAdminTarget & IGatewayCertificateStoreTarget;
148
+ response: TGatewayCertificateStoreExportResult<TGatewayCertificateStoreAdminExportRefusal>;
149
+ }
150
+ export interface IReq_Cloudly_Gateway_DeleteStoredCertificate extends implementsTR<ITypedRequest, IReq_Cloudly_Gateway_DeleteStoredCertificate> {
151
+ method: 'deleteGatewayStoredCertificate';
152
+ request: IGatewayCertificateStoreDelete;
153
+ response: TGatewayCertificateStoreDeleteResult<TGatewayCertificateStoreDeleteRefusal>;
154
+ }
155
+ export interface IReq_Admin_Cloudly_DeleteStoredCertificate extends implementsTR<ITypedRequest, IReq_Admin_Cloudly_DeleteStoredCertificate> {
156
+ method: 'deleteGatewayStoredCertificateForAdmin';
157
+ request: IAdminTarget & IGatewayCertificateStoreDelete;
158
+ response: TGatewayCertificateStoreDeleteResult<TGatewayCertificateStoreAdminMutationRefusal>;
159
+ }
160
+
161
+ const isRevision = (value: unknown): value is TSha256Digest =>
162
+ typeof value === 'string' && isSha256Digest(value);
163
+ // Legacy ICert imports did not restrict the two numeric timestamps beyond JSON number syntax.
164
+ const isTime = (value: unknown): value is number =>
165
+ typeof value === 'number' && Number.isFinite(value);
166
+ const isDomainPreview = (value: unknown): value is string =>
167
+ typeof value === 'string' && value.length > 0 && value.length <= gatewayCertificateStoreBounds.domainNamePreview
168
+ && !/[\u0000-\u001f\u007f]/.test(value);
169
+ const isCount = (value: unknown): value is number =>
170
+ typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
171
+
172
+ /** A legacy domain can be arbitrarily long or contain controls; keep it exportable by ID. */
173
+ export const previewLegacyCertificateDomainName = (value: string): { domainName: string; domainNameAltered: boolean } => {
174
+ let domainName = '';
175
+ let domainNameAltered = false;
176
+ for (const character of value) {
177
+ const code = character.charCodeAt(0);
178
+ const safeCharacter = code < 32 || code === 127 || (character.length === 1 && code >= 0xd800 && code <= 0xdfff)
179
+ ? '\ufffd' : character;
180
+ if (domainName.length + safeCharacter.length > gatewayCertificateStoreBounds.domainNamePreview) {
181
+ domainNameAltered = true;
182
+ break;
183
+ }
184
+ if (safeCharacter !== character) domainNameAltered = true;
185
+ domainName += safeCharacter;
186
+ }
187
+ return domainName.length > 0 ? { domainName, domainNameAltered }
188
+ : { domainName: '[empty domain]', domainNameAltered: true };
189
+ };
190
+
191
+ export const isGatewayCertificateStorePageRequest = (value: unknown): value is { after?: string } => {
192
+ const record = readDataProperties(value);
193
+ return record !== null && Object.keys(record).every((key) => key === 'after')
194
+ && (!Object.hasOwn(record, 'after') || isGatewayAreaRowId(record.after));
195
+ };
196
+
197
+ /** Select from a storage query ordered by ID, strictly after the cursor, limited to page + 1. */
198
+ export const selectGatewayCertificateStorePage = <T extends { id: string }>(
199
+ storageWindow: readonly T[], request: unknown,
200
+ ): { entries: T[]; nextAfter?: string } | null => {
201
+ if (!isGatewayCertificateStorePageRequest(request) || !Array.isArray(storageWindow)
202
+ || storageWindow.length > gatewayCertificateStoreBounds.storageWindow) return null;
203
+ let previous: string | null = request.after ?? null;
204
+ for (const entry of storageWindow) {
205
+ const row = readDataProperties(entry);
206
+ if (!row || !isGatewayAreaRowId(row.id)
207
+ || (previous !== null && compareWireTokens(previous, row.id) >= 0)) return null;
208
+ previous = row.id;
209
+ }
210
+ const entries = storageWindow.slice(0, gatewayCertificateStoreBounds.page);
211
+ return storageWindow.length > entries.length ? { entries, nextAfter: entries[entries.length - 1].id } : { entries };
212
+ };
213
+
214
+ /** Rebuilds a metadata-only page; unknown members, including a stray PEM or key, refuse whole. */
215
+ export const projectGatewayCertificateStorePage = (value: unknown, request: unknown): IGatewayCertificateStorePage | null => {
216
+ if (!isGatewayCertificateStorePageRequest(request)) return null;
217
+ const page = readDataProperties(value);
218
+ if (!page || Object.keys(page).some((key) => key !== 'entries' && key !== 'nextAfter')) return null;
219
+ const rows = readDataElements(page.entries, gatewayCertificateStoreBounds.page);
220
+ if (!rows || (Object.hasOwn(page, 'nextAfter') && (rows.length === 0 || !isGatewayAreaRowId(page.nextAfter)))) return null;
221
+ const entries: IGatewayCertificateStoreEntry[] = [];
222
+ let previous: string | null = request.after ?? null;
223
+ for (const rowArg of rows) {
224
+ const row = readExactDataRecord(rowArg, [
225
+ 'id', 'domainName', 'domainNameAltered', 'backingKinds', 'created', 'validUntil', 'exportable',
226
+ 'contentRevision', 'usageRevision', 'consumerCounts', 'consumersTruncated', 'consumers',
227
+ ]);
228
+ if (!row || !isGatewayAreaRowId(row.id) || (previous && compareWireTokens(previous, row.id) >= 0)
229
+ || !isDomainPreview(row.domainName) || typeof row.domainNameAltered !== 'boolean'
230
+ || !isTime(row.created) || !isTime(row.validUntil)
231
+ || typeof row.exportable !== 'boolean' || !isRevision(row.contentRevision)
232
+ || !isRevision(row.usageRevision) || typeof row.consumersTruncated !== 'boolean') return null;
233
+ const kinds = readDataElements(row.backingKinds, 2);
234
+ if (!kinds || !((kinds.length === 1 && (kinds[0] === 'legacy-acme' || kinds[0] === 'proxy'))
235
+ || (kinds.length === 2 && kinds[0] === 'legacy-acme' && kinds[1] === 'proxy'))) return null;
236
+ const consumersArg = readDataElements(row.consumers, gatewayCertificateStoreBounds.consumersPerEntry);
237
+ if (!consumersArg) return null;
238
+ const counts = readExactDataRecord(row.consumerCounts, ['routes', 'smtp', 'remoteIngress']);
239
+ if (!counts || !isCount(counts.routes) || !isCount(counts.smtp)
240
+ || !isCount(counts.remoteIngress)) return null;
241
+ const consumers: IGatewayCertificateStoreConsumer[] = [];
242
+ const sampledCounts = { routes: 0, smtp: 0, remoteIngress: 0 };
243
+ let previousConsumer: string | null = null;
244
+ for (const consumerArg of consumersArg) {
245
+ const consumer = readExactDataRecord(consumerArg, ['kind', 'id']);
246
+ if (!consumer || (consumer.kind !== 'route' && consumer.kind !== 'smtp' && consumer.kind !== 'remoteIngress')
247
+ || !isGatewayAreaRowId(consumer.id)) return null;
248
+ const key = `${consumer.kind}\u0000${consumer.id}`;
249
+ if (previousConsumer !== null && compareWireTokens(previousConsumer, key) >= 0) return null;
250
+ previousConsumer = key;
251
+ consumers.push({ kind: consumer.kind, id: consumer.id });
252
+ if (consumer.kind === 'route') sampledCounts.routes++;
253
+ else if (consumer.kind === 'smtp') sampledCounts.smtp++;
254
+ else sampledCounts.remoteIngress++;
255
+ }
256
+ if (counts.routes < sampledCounts.routes || counts.smtp < sampledCounts.smtp
257
+ || counts.remoteIngress < sampledCounts.remoteIngress
258
+ || row.consumersTruncated !== (counts.routes + counts.smtp + counts.remoteIngress > consumers.length)) return null;
259
+ entries.push({ id: row.id, domainName: row.domainName, domainNameAltered: row.domainNameAltered,
260
+ backingKinds: kinds as IGatewayCertificateStoreEntry['backingKinds'],
261
+ created: row.created, validUntil: row.validUntil, exportable: row.exportable,
262
+ contentRevision: row.contentRevision, usageRevision: row.usageRevision,
263
+ consumerCounts: { routes: counts.routes, smtp: counts.smtp, remoteIngress: counts.remoteIngress },
264
+ consumersTruncated: row.consumersTruncated, consumers });
265
+ previous = row.id;
266
+ }
267
+ if (Object.hasOwn(page, 'nextAfter') && page.nextAfter !== entries[entries.length - 1]?.id) return null;
268
+ return Object.hasOwn(page, 'nextAfter') ? { entries, nextAfter: page.nextAfter as string } : { entries };
269
+ };
270
+
271
+ const readConfirmation = (value: unknown): IGatewayCertificateStoreImpactConfirmation | null => {
272
+ const confirmation = readExactDataRecord(value, ['routes', 'smtp', 'remoteIngress']);
273
+ return confirmation && typeof confirmation.routes === 'boolean' && typeof confirmation.smtp === 'boolean'
274
+ && typeof confirmation.remoteIngress === 'boolean'
275
+ ? { routes: confirmation.routes, smtp: confirmation.smtp, remoteIngress: confirmation.remoteIngress } : null;
276
+ };
277
+
278
+ /** The old ICert file's required fields are copied without extra fields; legacy PEM checks stay in the gateway. */
279
+ export const projectLegacyCertificateFile = (value: unknown): ICert | null => {
280
+ const file = readDataProperties(value);
281
+ if (!file || typeof file.id !== 'string' || typeof file.domainName !== 'string'
282
+ || !isTime(file.created) || !isTime(file.validUntil)
283
+ || typeof file.privateKey !== 'string' || typeof file.publicKey !== 'string'
284
+ || (file.csr !== undefined && typeof file.csr !== 'string')) return null;
285
+ return { id: file.id, domainName: file.domainName, created: file.created,
286
+ validUntil: file.validUntil, privateKey: file.privateKey, publicKey: file.publicKey,
287
+ csr: file.csr ?? '' };
288
+ };
289
+
290
+ export const projectGatewayCertificateStoreImport = (value: unknown): IGatewayCertificateStoreImport | null => {
291
+ const input = readExactDataRecord(value, [
292
+ 'certificate', 'id', 'expectedContentRevision', 'expectedUsageRevision', 'confirmImpact',
293
+ ]);
294
+ if (!input || !((input.id === null && input.expectedContentRevision === null && input.expectedUsageRevision === null)
295
+ || (isGatewayAreaRowId(input.id) && isRevision(input.expectedContentRevision)
296
+ && isRevision(input.expectedUsageRevision)))) return null;
297
+ const certificate = projectLegacyCertificateFile(input.certificate);
298
+ const confirmImpact = readConfirmation(input.confirmImpact);
299
+ return certificate && confirmImpact ? { certificate, id: input.id as string | null,
300
+ expectedContentRevision: input.expectedContentRevision as TSha256Digest | null,
301
+ expectedUsageRevision: input.expectedUsageRevision as TSha256Digest | null, confirmImpact } : null;
302
+ };
303
+
304
+ export const projectGatewayCertificateStoreTarget = (value: unknown): IGatewayCertificateStoreTarget | null => {
305
+ const target = readExactDataRecord(value, ['id', 'expectedContentRevision']);
306
+ return target && isGatewayAreaRowId(target.id) && isRevision(target.expectedContentRevision)
307
+ ? { id: target.id, expectedContentRevision: target.expectedContentRevision } : null;
308
+ };
309
+
310
+ export const projectGatewayCertificateStoreDelete = (value: unknown): IGatewayCertificateStoreDelete | null => {
311
+ const target = readExactDataRecord(value, ['id', 'expectedContentRevision', 'expectedUsageRevision', 'confirmImpact']);
312
+ const confirmImpact = readConfirmation(target?.confirmImpact);
313
+ return target && isGatewayAreaRowId(target.id) && isRevision(target.expectedContentRevision)
314
+ && isRevision(target.expectedUsageRevision) && confirmImpact
315
+ ? { id: target.id, expectedContentRevision: target.expectedContentRevision,
316
+ expectedUsageRevision: target.expectedUsageRevision, confirmImpact } : null;
317
+ };
318
+
319
+ /** A revision fence is not a substitute for visible acknowledgement of each affected consumer. */
320
+ export const confirmsGatewayCertificateStoreImpact = (
321
+ counts: IGatewayCertificateStoreConsumerCounts, confirmation: IGatewayCertificateStoreImpactConfirmation,
322
+ ): boolean => (counts.routes === 0 || confirmation.routes)
323
+ && (counts.smtp === 0 || confirmation.smtp)
324
+ && (counts.remoteIngress === 0 || confirmation.remoteIngress);
325
+
326
+ export const isGatewayCertificateStoreInventoryRefusal = (value: unknown): value is TGatewayCertificateStoreInventoryRefusal =>
327
+ value === 'invalid-request' || value === 'store-unavailable';
328
+ export const isGatewayCertificateStoreImportRefusal = (value: unknown): value is TGatewayCertificateStoreImportRefusal =>
329
+ isGatewayCertificateStoreInventoryRefusal(value) || value === 'stale-content' || value === 'stale-usage'
330
+ || value === 'impact-confirmation-required' || value === 'outcome-unknown';
331
+ export const isGatewayCertificateStoreDeleteRefusal = isGatewayCertificateStoreImportRefusal;
332
+ export const isGatewayCertificateStorePrivateExportRefusal = (value: unknown): value is TGatewayCertificateStorePrivateExportRefusal =>
333
+ isGatewayCertificateStoreInventoryRefusal(value) || value === 'not-found'
334
+ || value === 'not-exportable' || value === 'stale-content';
335
+
336
+ export const projectGatewayCertificateStoreInventoryResult = (
337
+ value: unknown, request: unknown,
338
+ ): TGatewayCertificateStoreInventoryResult<TGatewayCertificateStoreInventoryRefusal> | null => {
339
+ const refusal = readExactDataRecord(value, ['ok', 'reason']);
340
+ if (refusal?.ok === false && isGatewayCertificateStoreInventoryRefusal(refusal.reason)) {
341
+ return { ok: false, reason: refusal.reason };
342
+ }
343
+ const result = readExactDataRecord(value, ['ok', 'page']);
344
+ const page = projectGatewayCertificateStorePage(result?.page, request);
345
+ return result?.ok === true && page ? { ok: true, page } : null;
346
+ };
347
+
348
+ /** Read a gateway write verdict without accepting credentials in a refusal. */
349
+ export const projectGatewayCertificateStoreWriteResult = (
350
+ value: unknown,
351
+ ): TGatewayCertificateStoreWriteResult<TGatewayCertificateStoreImportRefusal> | null => {
352
+ const refusal = readExactDataRecord(value, ['ok', 'reason']);
353
+ if (refusal?.ok === false && isGatewayCertificateStoreImportRefusal(refusal.reason)) {
354
+ return { ok: false, reason: refusal.reason };
355
+ }
356
+ const result = readExactDataRecord(value, ['ok', 'outcome', 'id', 'contentRevision', 'usageRevision']);
357
+ return result?.ok === true && (result.outcome === 'written' || result.outcome === 'unchanged')
358
+ && isGatewayAreaRowId(result.id) && isRevision(result.contentRevision) && isRevision(result.usageRevision)
359
+ ? { ok: true, outcome: result.outcome, id: result.id,
360
+ contentRevision: result.contentRevision, usageRevision: result.usageRevision } : null;
361
+ };
362
+
363
+ export const projectGatewayCertificateStoreDeleteResult = (
364
+ value: unknown,
365
+ ): TGatewayCertificateStoreDeleteResult<TGatewayCertificateStoreDeleteRefusal> | null => {
366
+ const refusal = readExactDataRecord(value, ['ok', 'reason']);
367
+ if (refusal?.ok === false && isGatewayCertificateStoreDeleteRefusal(refusal.reason)) {
368
+ return { ok: false, reason: refusal.reason };
369
+ }
370
+ const result = readExactDataRecord(value, ['ok', 'outcome']);
371
+ return result?.ok === true && (result.outcome === 'deleted' || result.outcome === 'unchanged')
372
+ ? { ok: true, outcome: result.outcome } : null;
373
+ };
374
+
375
+ /** Credential-bearing answer: a malformed file or any extra result member is refused whole. */
376
+ export const projectGatewayCertificateStoreExportResult = (
377
+ value: unknown,
378
+ ): TGatewayCertificateStoreExportResult<TGatewayCertificateStorePrivateExportRefusal> | null => {
379
+ const refusal = readExactDataRecord(value, ['ok', 'reason']);
380
+ if (refusal?.ok === false && isGatewayCertificateStorePrivateExportRefusal(refusal.reason)) {
381
+ return { ok: false, reason: refusal.reason };
382
+ }
383
+ const result = readExactDataRecord(value, ['ok', 'certificate']);
384
+ const certificate = projectLegacyCertificateFile(result?.certificate);
385
+ return result?.ok === true && certificate ? { ok: true, certificate } : null;
386
+ };
@@ -1,7 +1,8 @@
1
1
  import type { ITypedRequest, implementsTR } from '@api.global/typedrequest-interfaces';
2
- import { isGatewayAreaRowId } from '../data/gatewayareas.js';
2
+ import { gatewayAreaContentContract, gatewayDnsRecordTypes, isGatewayAreaRowId, type TGatewayDnsRecordType } from '../data/gatewayareas.js';
3
3
  import { isGatewayClientHostname } from '../data/gateway.js';
4
4
  import type { IIdentityCredential } from '../data/user.js';
5
+ import { compareWireTokens } from '../private/canonicaljson.js';
5
6
 
6
7
  interface IGatewayDnsTarget {
7
8
  gatewayId: string;
@@ -31,15 +32,36 @@ export interface IGatewayDnsAuthorityZone {
31
32
  verifiedAt: number | null;
32
33
  }
33
34
 
35
+ /** One gateway-local mirror row. Native provider identifiers and management keys stay on the gateway. */
36
+ export interface IGatewayDnsProviderRecord {
37
+ id: string;
38
+ name: string;
39
+ type: TGatewayDnsRecordType;
40
+ value: string;
41
+ ttl: number;
42
+ proxied?: boolean;
43
+ updatedAt: number;
44
+ ownership: 'unmanaged' | 'managed';
45
+ protectedHostname: boolean;
46
+ }
47
+
48
+ /** A fixed-size page in ascending local record-id order. Null ends the traversal. */
49
+ export interface IGatewayDnsProviderRecordPage {
50
+ rows: IGatewayDnsProviderRecord[];
51
+ nextAfter: string | null;
52
+ }
53
+
34
54
  export type TGatewayDnsRead =
35
55
  | { kind: 'providers' }
36
56
  | { kind: 'visible-zones'; providerId: string }
57
+ | { kind: 'provider-records'; zoneId: string; after?: string }
37
58
  | { kind: 'authority' }
38
59
  | { kind: 'delegation'; zone: string };
39
60
 
40
61
  export type TGatewayDnsObservation =
41
62
  | { kind: 'providers'; providers: IGatewayDnsList<IGatewayDnsProvider> }
42
63
  | { kind: 'visible-zones'; providerId: string; zones: IGatewayDnsList<IGatewayDnsVisibleZone> }
64
+ | { kind: 'provider-records'; zoneId: string; provenance: 'stored-mirror'; records: IGatewayDnsProviderRecordPage }
43
65
  | { kind: 'authority'; zones: IGatewayDnsList<IGatewayDnsAuthorityZone>; expectedNameservers: IGatewayDnsList<string> }
44
66
  | {
45
67
  kind: 'delegation';
@@ -64,6 +86,9 @@ export type TGatewayDnsLocalRefusal =
64
86
  | 'provider-not-found'
65
87
  | 'built-in-provider'
66
88
  | 'provider-listing-unavailable'
89
+ | 'zone-not-found'
90
+ | 'not-provider-zone'
91
+ | 'records-unavailable'
67
92
  | 'delegation-unavailable'
68
93
  | 'outcome-unknown';
69
94
 
@@ -114,6 +139,10 @@ export function readGatewayDnsOperation(value: unknown): TGatewayDnsRead | null
114
139
  case 'authority': return { kind: 'authority' };
115
140
  case 'visible-zones': return isGatewayDnsProviderId(operation.providerId)
116
141
  ? { kind: 'visible-zones', providerId: operation.providerId } : null;
142
+ case 'provider-records': return isGatewayAreaRowId(operation.zoneId)
143
+ && (operation.after === undefined || isGatewayAreaRowId(operation.after))
144
+ ? { kind: 'provider-records', zoneId: operation.zoneId,
145
+ ...(operation.after === undefined ? {} : { after: operation.after }) } : null;
117
146
  case 'delegation': return isGatewayClientHostname(operation.zone) ? { kind: 'delegation', zone: operation.zone } : null;
118
147
  default: return null;
119
148
  }
@@ -122,6 +151,7 @@ export function readGatewayDnsOperation(value: unknown): TGatewayDnsRead | null
122
151
  export function isGatewayDnsLocalRefusal(value: unknown): value is TGatewayDnsLocalRefusal {
123
152
  return value === 'invalid-request' || value === 'dns-unavailable' || value === 'provider-not-found'
124
153
  || value === 'built-in-provider' || value === 'provider-listing-unavailable'
154
+ || value === 'zone-not-found' || value === 'not-provider-zone' || value === 'records-unavailable'
125
155
  || value === 'delegation-unavailable' || value === 'outcome-unknown';
126
156
  }
127
157
 
@@ -130,7 +160,8 @@ export function gatewayDnsListOf<TRow>(rows: TRow[], maximum: number): IGatewayD
130
160
  }
131
161
 
132
162
  // At most 100 zones with 16 nameservers of at most 253 ASCII characters per row.
133
- export const gatewayDnsOperationsBounds = Object.freeze({ providers: 100, zones: 100, nameservers: 16, providerName: 160 });
163
+ export const gatewayDnsOperationsBounds = Object.freeze({ providers: 100, zones: 100, nameservers: 16, providerName: 160,
164
+ providerRecords: 32, recordValue: gatewayAreaContentContract.maximumRecordValueLength });
134
165
 
135
166
  const timestampOf = (value: unknown): number | null => typeof value === 'number' && Number.isSafeInteger(value)
136
167
  && value > 0 && value <= 8.64e15 ? value : null;
@@ -164,6 +195,40 @@ function providerOf(value: unknown): IGatewayDnsProvider | null {
164
195
  return { id, name, type, status, lastTestedAt: timestampOf(row.lastTestedAt) };
165
196
  }
166
197
 
198
+ function providerRecordOf(value: unknown): IGatewayDnsProviderRecord | null {
199
+ const row = recordOf(value);
200
+ if (!row || !isGatewayAreaRowId(row.id) || typeof row.name !== 'string' || !row.name
201
+ || row.name.length > 253 || /[\u0000-\u001f\u007f]/.test(row.name)
202
+ || typeof row.type !== 'string' || !gatewayDnsRecordTypes.includes(row.type as TGatewayDnsRecordType)
203
+ || typeof row.value !== 'string' || row.value.length > gatewayDnsOperationsBounds.recordValue
204
+ || !Number.isSafeInteger(row.ttl) || (row.ttl as number) < 0
205
+ || (row.proxied !== undefined && typeof row.proxied !== 'boolean')
206
+ || (row.ownership !== 'unmanaged' && row.ownership !== 'managed')
207
+ || typeof row.protectedHostname !== 'boolean') return null;
208
+ const updatedAt = timestampOf(row.updatedAt);
209
+ if (updatedAt === null) return null;
210
+ return { id: row.id, name: row.name, type: row.type as TGatewayDnsRecordType, value: row.value,
211
+ ttl: row.ttl as number, ...(row.proxied === undefined ? {} : { proxied: row.proxied }),
212
+ updatedAt, ownership: row.ownership, protectedHostname: row.protectedHostname };
213
+ }
214
+
215
+ function providerRecordPageOf(value: unknown, after?: string): IGatewayDnsProviderRecordPage | null {
216
+ const page = recordOf(value);
217
+ if (!page || !Array.isArray(page.rows) || page.rows.length > gatewayDnsOperationsBounds.providerRecords
218
+ || (page.nextAfter !== null && !isGatewayAreaRowId(page.nextAfter))) return null;
219
+ const rows: IGatewayDnsProviderRecord[] = [];
220
+ let previous = after;
221
+ for (const entry of page.rows) {
222
+ const row = providerRecordOf(entry);
223
+ if (!row || (previous !== undefined && compareWireTokens(previous, row.id) >= 0)) return null;
224
+ rows.push(row);
225
+ previous = row.id;
226
+ }
227
+ if (page.nextAfter !== null && (rows.length !== gatewayDnsOperationsBounds.providerRecords
228
+ || page.nextAfter !== rows[rows.length - 1].id)) return null;
229
+ return { rows, nextAfter: page.nextAfter };
230
+ }
231
+
167
232
  /** Read a bounded answer for this exact operation and discard every non-contract field. */
168
233
  export function projectGatewayDnsObservation(value: unknown, operation: TGatewayDnsRead): TGatewayDnsObservation | null {
169
234
  const answer = recordOf(value);
@@ -183,6 +248,11 @@ export function projectGatewayDnsObservation(value: unknown, operation: TGateway
183
248
  });
184
249
  return zones ? { kind: 'visible-zones', providerId: operation.providerId, zones } : null;
185
250
  }
251
+ case 'provider-records': {
252
+ if (answer.zoneId !== operation.zoneId || answer.provenance !== 'stored-mirror') return null;
253
+ const records = providerRecordPageOf(answer.records, operation.after);
254
+ return records ? { kind: 'provider-records', zoneId: operation.zoneId, provenance: 'stored-mirror', records } : null;
255
+ }
186
256
  case 'authority': {
187
257
  const expectedNameservers = nameserversOf(answer.expectedNameservers);
188
258
  const zones = projectList(answer.zones, gatewayDnsOperationsBounds.zones, (value) => {
@@ -105,3 +105,4 @@ export * as gatewaydnsoperations from './gatewaydnsoperations.js';
105
105
  export * as gatewayremoteingress from './gatewayremoteingress.js';
106
106
  export * as gatewaysmtpaccounts from './gatewaysmtpaccounts.js';
107
107
  export * as gatewayopstokens from './gatewayopstokens.js';
108
+ export * as gatewaycertificatestore from './gatewaycertificatestore.js';