@serve.zone/interfaces 32.103.0 → 32.104.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,583 @@
1
+ import { validateServiceId } from './data/coremail.runtime.js';
2
+ import { validatePlatformObjectStorageBucketName } from './platform/objectstorage.js';
3
+ import { canonicalizeStrictJson, createCanonicalJsonSha256Hex } from './private/canonicaljson.js';
4
+ import { isSha256Hex } from './private/bearerencoding.js';
5
+ import { deepFreezeValue } from './private/canonicaljson.js';
6
+ import { readDataElements, readExactDataRecord } from './private/dataproperties.js';
7
+
8
+ /** Corestore's 8 GiB snapshot encoder and 16 GiB closure, within ContainerArchive's own bounds. */
9
+ export const corestoreObjectStorageBackupLimits = Object.freeze({
10
+ maximumControlBytes: 16 * 1024,
11
+ /** A 10 TiB bucket at 8 GiB per unit needs 1,280 full receipts. */
12
+ /** Leaves headroom below TypedSocket's 16 MiB text-frame ceiling. */
13
+ maximumManifestBytes: 8 * 1024 * 1024,
14
+ maximumCursorBytes: 4096,
15
+ maximumSegments: 8192,
16
+ maximumSegmentBytes: 8 * 1024 ** 3,
17
+ maximumClosureBytes: 16 * 1024 ** 3,
18
+ maximumClosureObjects: 400_506,
19
+ maximumClosurePacks: 200_000,
20
+ maximumClosureChunks: 350_000,
21
+ });
22
+
23
+ /** Corestore's segmented capture control routes; each requires control authentication. */
24
+ export const corestoreObjectStorageBackupPaths = Object.freeze({
25
+ begin: '/resources/objectstorage/backup/begin',
26
+ status: '/resources/objectstorage/backup/status',
27
+ seal: '/resources/objectstorage/backup/seal',
28
+ chunk: '/resources/objectstorage/backup/chunk',
29
+ release: '/resources/objectstorage/backup/release',
30
+ restoreBegin: '/resources/objectstorage/backup/restore/begin',
31
+ restoreStatus: '/resources/objectstorage/backup/restore/status',
32
+ restoreSegment: '/resources/objectstorage/backup/restore/segment',
33
+ restoreSeal: '/resources/objectstorage/backup/restore/seal',
34
+ restorePublish: '/resources/objectstorage/backup/restore/publish',
35
+ restoreCancel: '/resources/objectstorage/backup/restore/cancel',
36
+ });
37
+
38
+ /** A nonempty chunk streams the exact ContainerArchive closure bytes. */
39
+ export const corestoreObjectStorageBackupMediaType = 'application/octet-stream';
40
+ /** Base64url JSON of the normalized chunk response that describes the closure body. */
41
+ export const corestoreObjectStorageBackupChunkHeader = 'x-corestore-objectstorage-backup-chunk';
42
+
43
+ /** The caller's durable identity for one bucket capture; no credential or endpoint travels here. */
44
+ export interface ICorestoreObjectStorageBackupScope {
45
+ backupId: string;
46
+ serviceId: string;
47
+ /** Caller-owned canonical lowercase UUID, distinct from each engine-generated segment ID. */
48
+ snapshotId: string;
49
+ allocation: {
50
+ bindingId: string;
51
+ resourceRef: string;
52
+ generation: number;
53
+ };
54
+ bucketName: string;
55
+ }
56
+
57
+ export interface ICorestoreObjectStorageBackupBeginRequest extends ICorestoreObjectStorageBackupScope {}
58
+ export interface ICorestoreObjectStorageBackupStatusRequest extends ICorestoreObjectStorageBackupScope {}
59
+ export interface ICorestoreObjectStorageBackupSealRequest extends ICorestoreObjectStorageBackupScope {}
60
+ export interface ICorestoreObjectStorageBackupReleaseRequest extends ICorestoreObjectStorageBackupScope {}
61
+
62
+ /** Seal reports ObjectStorage's actual catalog counts, not estimates from frame sizes. */
63
+ export type TCorestoreObjectStorageBackupStatus =
64
+ | (ICorestoreObjectStorageBackupScope & { phase: 'capturing' })
65
+ | (ICorestoreObjectStorageBackupScope & {
66
+ phase: 'sealed';
67
+ objectCount: number;
68
+ totalPayloadBytes: number;
69
+ });
70
+
71
+ /** Exact replay of durable logical retirement returns this receipt; physical reclamation is independent. */
72
+ export interface ICorestoreObjectStorageBackupReleaseResponse extends ICorestoreObjectStorageBackupScope {
73
+ released: true;
74
+ }
75
+
76
+ /** One request obtains at most one segment. The cursor is transport state, never authority. */
77
+ export interface ICorestoreObjectStorageBackupChunkRequest extends ICorestoreObjectStorageBackupScope {
78
+ cursor?: string;
79
+ }
80
+
81
+ /** Exact ContainerArchive 0.15.0 closure-v1 completion receipt wire shape. */
82
+ export interface ICorestoreObjectStorageClosureReceipt {
83
+ /** Foreign ContainerArchive member; the normalizer admits exactly 1. */
84
+ formatVersion: number;
85
+ snapshotIds: [string];
86
+ objectCount: number;
87
+ packCount: number;
88
+ chunkCount: number;
89
+ payloadBytes: number;
90
+ archiveBytes: number;
91
+ sha256: string;
92
+ }
93
+
94
+ /** The logical segment and the exact opaque artifact that carries it. */
95
+ export interface ICorestoreObjectStorageBackupSegmentReceipt extends ICorestoreObjectStorageBackupScope {
96
+ segmentId: string;
97
+ ordinal: number;
98
+ offset: number;
99
+ length: number;
100
+ logicalSha256: string;
101
+ closure: ICorestoreObjectStorageClosureReceipt;
102
+ }
103
+
104
+ /** Announced before bytes move; the closure receipt is completion evidence, not backup completion. */
105
+ export interface ICorestoreObjectStorageBackupSegmentDescriptor {
106
+ size: number;
107
+ sha256: string;
108
+ receipt: ICorestoreObjectStorageBackupSegmentReceipt;
109
+ }
110
+
111
+ /** A null segment has no cursor; a present segment without one is the last artifact. */
112
+ export interface ICorestoreObjectStorageBackupChunkResponse {
113
+ segment: ICorestoreObjectStorageBackupSegmentDescriptor | null;
114
+ nextCursor?: string;
115
+ }
116
+
117
+ /** Relative to the backup's immutable artifact namespace. Cloudly owns its physical target. */
118
+ export interface ICorestoreObjectStorageBackupManifestSegment {
119
+ descriptor: ICorestoreObjectStorageBackupSegmentDescriptor;
120
+ artifactPath: string;
121
+ }
122
+
123
+ /** Published only after every segment artifact was durably accepted and read back. */
124
+ export interface ICorestoreObjectStorageBackupManifest extends ICorestoreObjectStorageBackupScope {
125
+ format: 'corestore.objectstorage.backup.manifest';
126
+ objectCount: number;
127
+ totalPayloadBytes: number;
128
+ logicalBytes: number;
129
+ segments: ICorestoreObjectStorageBackupManifestSegment[];
130
+ }
131
+
132
+ /** One restore attempt, bound to the exact admitted manifest and target allocation. */
133
+ export interface ICorestoreObjectStorageRestoreScope {
134
+ restoreId: string;
135
+ backupId: string;
136
+ snapshotId: string;
137
+ manifestSha256: string;
138
+ targetServiceId: string;
139
+ targetAllocation: ICorestoreObjectStorageBackupScope['allocation'];
140
+ /** Native fenced publication currently requires this bucket to be absent. */
141
+ targetBucketName: string;
142
+ }
143
+
144
+ /** The large manifest is admitted once; later control requests carry only its digest. */
145
+ export interface ICorestoreObjectStorageRestoreBeginRequest extends ICorestoreObjectStorageRestoreScope {
146
+ manifest: ICorestoreObjectStorageBackupManifest;
147
+ }
148
+
149
+ /** Used by status, seal, publish and cancel; segment adds only its ordinal. */
150
+ export interface ICorestoreObjectStorageRestoreRequest extends ICorestoreObjectStorageRestoreScope {}
151
+
152
+ export interface ICorestoreObjectStorageRestoreSegmentRequest extends ICorestoreObjectStorageRestoreScope {
153
+ ordinal: number;
154
+ }
155
+
156
+ export interface ICorestoreObjectStorageRestoreStatus extends ICorestoreObjectStorageRestoreScope {
157
+ nextOrdinal: number;
158
+ objectCount: number;
159
+ totalPayloadBytes: number;
160
+ sealed: boolean;
161
+ }
162
+
163
+ export interface ICorestoreObjectStorageRestorePublishResponse extends ICorestoreObjectStorageRestoreScope {
164
+ published: true;
165
+ replayed: boolean;
166
+ }
167
+
168
+ export interface ICorestoreObjectStorageRestoreCancelResponse extends ICorestoreObjectStorageRestoreScope {
169
+ done: boolean;
170
+ }
171
+
172
+ const identifierPattern = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,255}$/;
173
+ const engineSnapshotIdPattern = /^[A-Za-z0-9_-]{1,128}$/;
174
+
175
+ const fail = (label: string, reason: string): never => {
176
+ throw new Error(`${label} ${reason}`);
177
+ };
178
+
179
+ const exact = (value: unknown, keys: readonly string[], label: string): Record<string, unknown> =>
180
+ readExactDataRecord(value, keys) ?? fail(label, 'must use its exact schema');
181
+
182
+ const identifier = (value: unknown, label: string): string =>
183
+ typeof value === 'string' && identifierPattern.test(value)
184
+ ? value
185
+ : fail(label, 'must be a canonical identifier');
186
+
187
+ const serviceId = (value: unknown, label: string): string =>
188
+ typeof value === 'string' && validateServiceId(value).length === 0
189
+ ? value
190
+ : fail(label, 'must be a canonical Service ID');
191
+
192
+ const bucketName = (value: unknown, label: string): string =>
193
+ typeof value === 'string' && validatePlatformObjectStorageBucketName(value, label).length === 0
194
+ ? value
195
+ : fail(label, 'must be a canonical S3 bucket name');
196
+
197
+ const integer = (value: unknown, min: number, max: number, label: string): number =>
198
+ typeof value === 'number' && Number.isSafeInteger(value) && !Object.is(value, -0)
199
+ && value >= min && value <= max
200
+ ? value
201
+ : fail(label, 'must be a bounded safe integer');
202
+
203
+ const digest = (value: unknown, label: string): string =>
204
+ isSha256Hex(value) ? value : fail(label, 'must be bare lowercase SHA-256');
205
+
206
+ const cursor = (value: unknown, label: string): string =>
207
+ typeof value === 'string' && value.length > 0
208
+ && Buffer.byteLength(value, 'utf8') <= corestoreObjectStorageBackupLimits.maximumCursorBytes
209
+ && !/\p{Cc}/u.test(value)
210
+ ? value
211
+ : fail(label, 'must be bounded opaque text without control characters');
212
+
213
+ const scopeKeys = ['backupId', 'serviceId', 'snapshotId', 'allocation', 'bucketName'] as const;
214
+
215
+ const readScope = (
216
+ value: Record<string, unknown>,
217
+ label: string,
218
+ ): ICorestoreObjectStorageBackupScope => {
219
+ const allocation = exact(
220
+ value.allocation,
221
+ ['bindingId', 'resourceRef', 'generation'],
222
+ `${label}.allocation`,
223
+ );
224
+ return {
225
+ backupId: identifier(value.backupId, `${label}.backupId`),
226
+ serviceId: serviceId(value.serviceId, `${label}.serviceId`),
227
+ snapshotId: typeof value.snapshotId === 'string'
228
+ && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(value.snapshotId)
229
+ ? value.snapshotId
230
+ : fail(`${label}.snapshotId`, 'must be a canonical lowercase UUID'),
231
+ allocation: {
232
+ bindingId: identifier(allocation.bindingId, `${label}.allocation.bindingId`),
233
+ resourceRef: identifier(allocation.resourceRef, `${label}.allocation.resourceRef`),
234
+ generation: integer(allocation.generation, 1, Number.MAX_SAFE_INTEGER, `${label}.allocation.generation`),
235
+ },
236
+ bucketName: bucketName(value.bucketName, `${label}.bucketName`),
237
+ };
238
+ };
239
+
240
+ const bound = <T>(value: T, label: string, maximumBytes = corestoreObjectStorageBackupLimits.maximumControlBytes): Readonly<T> => {
241
+ if (Buffer.byteLength(JSON.stringify(value), 'utf8') > maximumBytes) {
242
+ fail(label, 'exceeds its control byte limit');
243
+ }
244
+ return deepFreezeValue(value);
245
+ };
246
+
247
+ export const normalizeCorestoreObjectStorageBackupScope = (
248
+ value: unknown,
249
+ ): Readonly<ICorestoreObjectStorageBackupScope> => {
250
+ const record = exact(value, scopeKeys, 'objectStorageBackupScope');
251
+ return bound(readScope(record, 'objectStorageBackupScope'), 'objectStorageBackupScope');
252
+ };
253
+
254
+ export const normalizeCorestoreObjectStorageBackupStatus = (
255
+ value: unknown,
256
+ ): Readonly<TCorestoreObjectStorageBackupStatus> => {
257
+ const label = 'objectStorageBackupStatus';
258
+ const capturing = readExactDataRecord(value, [...scopeKeys, 'phase']);
259
+ if (capturing) {
260
+ if (capturing.phase === 'sealed') fail(label, 'sealed status requires native catalog counts');
261
+ if (capturing.phase !== 'capturing') fail(`${label}.phase`, 'must name a capture phase');
262
+ return bound({ ...readScope(capturing, label), phase: 'capturing' as const }, label);
263
+ }
264
+ const sealed = exact(value, [...scopeKeys, 'phase', 'objectCount', 'totalPayloadBytes'], label);
265
+ if (sealed.phase !== 'sealed') fail(`${label}.phase`, 'must name a capture phase');
266
+ return bound({
267
+ ...readScope(sealed, label), phase: 'sealed' as const,
268
+ objectCount: integer(sealed.objectCount, 0, Number.MAX_SAFE_INTEGER, `${label}.objectCount`),
269
+ totalPayloadBytes: integer(sealed.totalPayloadBytes, 0, Number.MAX_SAFE_INTEGER, `${label}.totalPayloadBytes`),
270
+ }, label);
271
+ };
272
+
273
+ export const normalizeCorestoreObjectStorageBackupReleaseResponse = (
274
+ value: unknown,
275
+ ): Readonly<ICorestoreObjectStorageBackupReleaseResponse> => {
276
+ const label = 'objectStorageBackupReleaseResponse';
277
+ const record = exact(value, [...scopeKeys, 'released'], label);
278
+ if (record.released !== true) fail(`${label}.released`, 'must be true');
279
+ return bound({ ...readScope(record, label), released: true }, label);
280
+ };
281
+
282
+ export const normalizeCorestoreObjectStorageBackupChunkRequest = (
283
+ value: unknown,
284
+ ): Readonly<ICorestoreObjectStorageBackupChunkRequest> => {
285
+ const record = readExactDataRecord(value, scopeKeys)
286
+ ?? readExactDataRecord(value, [...scopeKeys, 'cursor'])
287
+ ?? fail('objectStorageBackupChunkRequest', 'must use its exact schema');
288
+ return bound({
289
+ ...readScope(record, 'objectStorageBackupChunkRequest'),
290
+ ...(Object.hasOwn(record, 'cursor')
291
+ ? { cursor: cursor(record.cursor, 'objectStorageBackupChunkRequest.cursor') }
292
+ : {}),
293
+ }, 'objectStorageBackupChunkRequest');
294
+ };
295
+
296
+ const readClosure = (value: unknown, segmentId: string): ICorestoreObjectStorageClosureReceipt => {
297
+ const label = 'objectStorageBackupSegmentReceipt.closure';
298
+ const record = exact(value, [
299
+ 'formatVersion', 'snapshotIds', 'objectCount', 'packCount',
300
+ 'chunkCount', 'payloadBytes', 'archiveBytes', 'sha256',
301
+ ], label);
302
+ const formatVersion = record.formatVersion === 1
303
+ ? record.formatVersion
304
+ : fail(`${label}.formatVersion`, 'must be 1');
305
+ const snapshotIds = readDataElements(record.snapshotIds, 1);
306
+ if (!snapshotIds || snapshotIds.length !== 1 || snapshotIds[0] !== segmentId) {
307
+ fail(`${label}.snapshotIds`, 'must name only this segment ID');
308
+ }
309
+ const result: ICorestoreObjectStorageClosureReceipt = {
310
+ formatVersion,
311
+ snapshotIds: [segmentId],
312
+ objectCount: integer(record.objectCount, 1, corestoreObjectStorageBackupLimits.maximumClosureObjects, `${label}.objectCount`),
313
+ packCount: integer(record.packCount, 0, corestoreObjectStorageBackupLimits.maximumClosurePacks, `${label}.packCount`),
314
+ chunkCount: integer(record.chunkCount, 0, corestoreObjectStorageBackupLimits.maximumClosureChunks, `${label}.chunkCount`),
315
+ payloadBytes: integer(record.payloadBytes, 0, corestoreObjectStorageBackupLimits.maximumClosureBytes, `${label}.payloadBytes`),
316
+ archiveBytes: integer(record.archiveBytes, 1, corestoreObjectStorageBackupLimits.maximumClosureBytes, `${label}.archiveBytes`),
317
+ sha256: digest(record.sha256, `${label}.sha256`),
318
+ };
319
+ if (result.payloadBytes > result.archiveBytes) fail(label, 'payloadBytes exceeds archiveBytes');
320
+ return result;
321
+ };
322
+
323
+ export const normalizeCorestoreObjectStorageBackupSegmentReceipt = (
324
+ value: unknown,
325
+ ): Readonly<ICorestoreObjectStorageBackupSegmentReceipt> => {
326
+ const label = 'objectStorageBackupSegmentReceipt';
327
+ const record = exact(value, [
328
+ ...scopeKeys, 'segmentId', 'ordinal', 'offset', 'length', 'logicalSha256', 'closure',
329
+ ], label);
330
+ const segmentId = typeof record.segmentId === 'string' && engineSnapshotIdPattern.test(record.segmentId)
331
+ ? record.segmentId
332
+ : fail(`${label}.segmentId`, 'must be a ContainerArchive snapshot ID');
333
+ const offset = integer(record.offset, 0, Number.MAX_SAFE_INTEGER, `${label}.offset`);
334
+ const length = integer(record.length, 0, corestoreObjectStorageBackupLimits.maximumSegmentBytes, `${label}.length`);
335
+ if (!Number.isSafeInteger(offset + length)) fail(label, 'logical end exceeds the safe integer range');
336
+ return bound({
337
+ ...readScope(record, label),
338
+ segmentId,
339
+ ordinal: integer(record.ordinal, 0, corestoreObjectStorageBackupLimits.maximumSegments - 1, `${label}.ordinal`),
340
+ offset,
341
+ length,
342
+ logicalSha256: digest(record.logicalSha256, `${label}.logicalSha256`),
343
+ closure: readClosure(record.closure, segmentId),
344
+ }, label);
345
+ };
346
+
347
+ export const normalizeCorestoreObjectStorageBackupSegmentDescriptor = (
348
+ value: unknown,
349
+ ): Readonly<ICorestoreObjectStorageBackupSegmentDescriptor> => {
350
+ const label = 'objectStorageBackupSegmentDescriptor';
351
+ const record = exact(value, ['size', 'sha256', 'receipt'], label);
352
+ const receipt = normalizeCorestoreObjectStorageBackupSegmentReceipt(record.receipt);
353
+ const size = integer(record.size, 1, corestoreObjectStorageBackupLimits.maximumClosureBytes, `${label}.size`);
354
+ const sha256 = digest(record.sha256, `${label}.sha256`);
355
+ if (size !== receipt.closure.archiveBytes || sha256 !== receipt.closure.sha256) {
356
+ fail(label, 'artifact size or sha256 disagrees with its closure receipt');
357
+ }
358
+ return bound({ size, sha256, receipt }, label);
359
+ };
360
+
361
+ export const normalizeCorestoreObjectStorageBackupChunkResponse = (
362
+ value: unknown,
363
+ ): Readonly<ICorestoreObjectStorageBackupChunkResponse> => {
364
+ const label = 'objectStorageBackupChunkResponse';
365
+ const record = readExactDataRecord(value, ['segment'])
366
+ ?? readExactDataRecord(value, ['segment', 'nextCursor'])
367
+ ?? fail(label, 'must use its exact schema');
368
+ if (record.segment === null) {
369
+ if (Object.hasOwn(record, 'nextCursor')) fail(label, 'a final page must not carry a cursor');
370
+ return { segment: null };
371
+ }
372
+ return bound({
373
+ segment: normalizeCorestoreObjectStorageBackupSegmentDescriptor(record.segment),
374
+ ...(Object.hasOwn(record, 'nextCursor')
375
+ ? { nextCursor: cursor(record.nextCursor, `${label}.nextCursor`) }
376
+ : {}),
377
+ }, label);
378
+ };
379
+
380
+ const sameScope = (
381
+ left: ICorestoreObjectStorageBackupScope,
382
+ right: ICorestoreObjectStorageBackupScope,
383
+ ): boolean => left.backupId === right.backupId
384
+ && left.serviceId === right.serviceId
385
+ && left.snapshotId === right.snapshotId
386
+ && left.bucketName === right.bucketName
387
+ && left.allocation.bindingId === right.allocation.bindingId
388
+ && left.allocation.resourceRef === right.allocation.resourceRef
389
+ && left.allocation.generation === right.allocation.generation;
390
+
391
+ /** The path is relative to a backup, with the artifact digest in its immutable name. */
392
+ export const corestoreObjectStorageSegmentArtifactPath = (
393
+ snapshotId: string,
394
+ ordinal: number,
395
+ sha256: string,
396
+ ): string => `objectstorage/${snapshotId}/segments/${ordinal}-${sha256}.closure`;
397
+
398
+ export const normalizeCorestoreObjectStorageBackupManifest = (
399
+ value: unknown,
400
+ ): Readonly<ICorestoreObjectStorageBackupManifest> => {
401
+ const label = 'objectStorageBackupManifest';
402
+ const record = exact(value, [
403
+ ...scopeKeys, 'format', 'objectCount', 'totalPayloadBytes', 'logicalBytes', 'segments',
404
+ ], label);
405
+ if (record.format !== 'corestore.objectstorage.backup.manifest') {
406
+ fail(`${label}.format`, 'must name the bucket backup manifest format');
407
+ }
408
+ const scope = readScope(record, label);
409
+ const rawSegments = readDataElements(record.segments, corestoreObjectStorageBackupLimits.maximumSegments)
410
+ ?? fail(`${label}.segments`, 'must be a bounded dense list');
411
+ if (rawSegments.length === 0) fail(`${label}.segments`, 'must contain at least one segment');
412
+ const objectCount = integer(record.objectCount, 0, Number.MAX_SAFE_INTEGER, `${label}.objectCount`);
413
+ const totalPayloadBytes = integer(
414
+ record.totalPayloadBytes, 0, Number.MAX_SAFE_INTEGER, `${label}.totalPayloadBytes`,
415
+ );
416
+ const logicalBytes = integer(record.logicalBytes, 0, Number.MAX_SAFE_INTEGER, `${label}.logicalBytes`);
417
+ let nextOffset = 0;
418
+ let segmentEnvelopeBytes = 0;
419
+ const segmentIds = new Set<string>();
420
+ const segments = rawSegments.map((raw, ordinal) => {
421
+ const entry = exact(raw, ['descriptor', 'artifactPath'], `${label}.segments[${ordinal}]`);
422
+ const descriptor = normalizeCorestoreObjectStorageBackupSegmentDescriptor(entry.descriptor);
423
+ const receipt = descriptor.receipt;
424
+ if (!sameScope(scope, receipt)) fail(`${label}.segments[${ordinal}]`, 'has a different backup scope');
425
+ if (receipt.ordinal !== ordinal || receipt.offset !== nextOffset) {
426
+ fail(`${label}.segments[${ordinal}]`, 'has a gap or wrong ordinal');
427
+ }
428
+ if (segmentIds.has(receipt.segmentId)) {
429
+ fail(`${label}.segments[${ordinal}].segmentId`, 'must be unique within the manifest');
430
+ }
431
+ segmentIds.add(receipt.segmentId);
432
+ if (receipt.length === 0 && (
433
+ rawSegments.length !== 1 || objectCount !== 0 || totalPayloadBytes !== 0 || logicalBytes !== 0
434
+ )) {
435
+ fail(`${label}.segments[${ordinal}]`, 'a zero-length segment requires the sole unit of an empty manifest');
436
+ }
437
+ nextOffset += receipt.length;
438
+ if (!Number.isSafeInteger(nextOffset)) fail(`${label}.segments`, 'aggregate logical bytes exceed the safe integer range');
439
+ const artifactPath = corestoreObjectStorageSegmentArtifactPath(scope.snapshotId, ordinal, descriptor.sha256);
440
+ if (entry.artifactPath !== artifactPath) {
441
+ fail(`${label}.segments[${ordinal}].artifactPath`, 'must name this immutable segment artifact');
442
+ }
443
+ segmentEnvelopeBytes += Buffer.byteLength(JSON.stringify({ descriptor, artifactPath }), 'utf8');
444
+ if (segmentEnvelopeBytes > corestoreObjectStorageBackupLimits.maximumManifestBytes) {
445
+ fail(label, 'exceeds its manifest byte limit');
446
+ }
447
+ return { descriptor, artifactPath };
448
+ });
449
+ if (logicalBytes !== nextOffset) fail(`${label}.logicalBytes`, 'disagrees with the contiguous segments');
450
+ return bound({
451
+ ...scope,
452
+ format: 'corestore.objectstorage.backup.manifest' as const,
453
+ objectCount,
454
+ totalPayloadBytes,
455
+ logicalBytes,
456
+ segments,
457
+ }, label, corestoreObjectStorageBackupLimits.maximumManifestBytes);
458
+ };
459
+
460
+ /** Hashes the exact normalized manifest; its digest is stored separately from the manifest. */
461
+ export const computeCorestoreObjectStorageBackupManifestSha256 = async (value: unknown): Promise<string> => {
462
+ const manifest = normalizeCorestoreObjectStorageBackupManifest(value);
463
+ return createCanonicalJsonSha256Hex(
464
+ canonicalizeStrictJson(manifest, (reason) => fail('objectStorageBackupManifest', reason)),
465
+ (reason) => fail('objectStorageBackupManifest', reason),
466
+ );
467
+ };
468
+
469
+ /** Admits a restore's manifest only when its independent digest matches. */
470
+ export const verifyCorestoreObjectStorageRestoreBeginRequest = async (
471
+ value: unknown,
472
+ ): Promise<Readonly<ICorestoreObjectStorageRestoreBeginRequest>> => {
473
+ const request = normalizeCorestoreObjectStorageRestoreBeginRequest(value);
474
+ if (request.manifestSha256 !== await computeCorestoreObjectStorageBackupManifestSha256(request.manifest)) {
475
+ fail('objectStorageRestoreBeginRequest.manifestSha256', 'disagrees with the admitted manifest');
476
+ }
477
+ return request;
478
+ };
479
+
480
+ const restoreScopeKeys = [
481
+ 'restoreId', 'backupId', 'snapshotId', 'manifestSha256',
482
+ 'targetServiceId', 'targetAllocation', 'targetBucketName',
483
+ ] as const;
484
+
485
+ const readRestoreScope = (record: Record<string, unknown>, label: string): ICorestoreObjectStorageRestoreScope => {
486
+ const targetAllocation = exact(
487
+ record.targetAllocation, ['bindingId', 'resourceRef', 'generation'], `${label}.targetAllocation`,
488
+ );
489
+ const restoreId = typeof record.restoreId === 'string'
490
+ && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(record.restoreId)
491
+ ? record.restoreId
492
+ : fail(`${label}.restoreId`, 'must be a canonical lowercase UUID');
493
+ const snapshotId = typeof record.snapshotId === 'string'
494
+ && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(record.snapshotId)
495
+ ? record.snapshotId
496
+ : fail(`${label}.snapshotId`, 'must be a canonical lowercase UUID');
497
+ return {
498
+ restoreId,
499
+ backupId: identifier(record.backupId, `${label}.backupId`),
500
+ snapshotId,
501
+ manifestSha256: digest(record.manifestSha256, `${label}.manifestSha256`),
502
+ targetServiceId: serviceId(record.targetServiceId, `${label}.targetServiceId`),
503
+ targetAllocation: {
504
+ bindingId: identifier(targetAllocation.bindingId, `${label}.targetAllocation.bindingId`),
505
+ resourceRef: identifier(targetAllocation.resourceRef, `${label}.targetAllocation.resourceRef`),
506
+ generation: integer(targetAllocation.generation, 1, Number.MAX_SAFE_INTEGER, `${label}.targetAllocation.generation`),
507
+ },
508
+ targetBucketName: bucketName(record.targetBucketName, `${label}.targetBucketName`),
509
+ };
510
+ };
511
+
512
+ export const normalizeCorestoreObjectStorageRestoreScope = (
513
+ value: unknown,
514
+ ): Readonly<ICorestoreObjectStorageRestoreScope> => {
515
+ const label = 'objectStorageRestoreScope';
516
+ return bound(readRestoreScope(exact(value, restoreScopeKeys, label), label), label);
517
+ };
518
+
519
+ /** Structural admission only; compare `manifestSha256` with the computed digest before storing. */
520
+ export const normalizeCorestoreObjectStorageRestoreBeginRequest = (
521
+ value: unknown,
522
+ ): Readonly<ICorestoreObjectStorageRestoreBeginRequest> => {
523
+ const label = 'objectStorageRestoreBeginRequest';
524
+ const record = exact(value, [...restoreScopeKeys, 'manifest'], label);
525
+ const scope = readRestoreScope(record, label);
526
+ const manifest = normalizeCorestoreObjectStorageBackupManifest(record.manifest);
527
+ if (scope.backupId !== manifest.backupId || scope.snapshotId !== manifest.snapshotId) {
528
+ fail(label, 'names a different backup or snapshot than its manifest');
529
+ }
530
+ return bound({ ...scope, manifest }, label,
531
+ corestoreObjectStorageBackupLimits.maximumManifestBytes
532
+ + corestoreObjectStorageBackupLimits.maximumControlBytes);
533
+ };
534
+
535
+ export const normalizeCorestoreObjectStorageRestoreSegmentRequest = (
536
+ value: unknown,
537
+ ): Readonly<ICorestoreObjectStorageRestoreSegmentRequest> => {
538
+ const label = 'objectStorageRestoreSegmentRequest';
539
+ const record = exact(value, [...restoreScopeKeys, 'ordinal'], label);
540
+ return bound({
541
+ ...readRestoreScope(record, label),
542
+ ordinal: integer(record.ordinal, 0, corestoreObjectStorageBackupLimits.maximumSegments - 1, `${label}.ordinal`),
543
+ }, label);
544
+ };
545
+
546
+ export const normalizeCorestoreObjectStorageRestoreStatus = (
547
+ value: unknown,
548
+ ): Readonly<ICorestoreObjectStorageRestoreStatus> => {
549
+ const label = 'objectStorageRestoreStatus';
550
+ const record = exact(value, [
551
+ ...restoreScopeKeys, 'nextOrdinal', 'objectCount', 'totalPayloadBytes', 'sealed',
552
+ ], label);
553
+ const sealed = typeof record.sealed === 'boolean'
554
+ ? record.sealed : fail(`${label}.sealed`, 'must be boolean');
555
+ return bound({
556
+ ...readRestoreScope(record, label),
557
+ nextOrdinal: integer(record.nextOrdinal, 0, corestoreObjectStorageBackupLimits.maximumSegments, `${label}.nextOrdinal`),
558
+ objectCount: integer(record.objectCount, 0, Number.MAX_SAFE_INTEGER, `${label}.objectCount`),
559
+ totalPayloadBytes: integer(record.totalPayloadBytes, 0, Number.MAX_SAFE_INTEGER, `${label}.totalPayloadBytes`),
560
+ sealed,
561
+ }, label);
562
+ };
563
+
564
+ export const normalizeCorestoreObjectStorageRestorePublishResponse = (
565
+ value: unknown,
566
+ ): Readonly<ICorestoreObjectStorageRestorePublishResponse> => {
567
+ const label = 'objectStorageRestorePublishResponse';
568
+ const record = exact(value, [...restoreScopeKeys, 'published', 'replayed'], label);
569
+ if (record.published !== true) fail(`${label}.published`, 'must be true');
570
+ const replayed = typeof record.replayed === 'boolean'
571
+ ? record.replayed : fail(`${label}.replayed`, 'must be boolean');
572
+ return bound({ ...readRestoreScope(record, label), published: true, replayed }, label);
573
+ };
574
+
575
+ export const normalizeCorestoreObjectStorageRestoreCancelResponse = (
576
+ value: unknown,
577
+ ): Readonly<ICorestoreObjectStorageRestoreCancelResponse> => {
578
+ const label = 'objectStorageRestoreCancelResponse';
579
+ const record = exact(value, [...restoreScopeKeys, 'done'], label);
580
+ const done = typeof record.done === 'boolean'
581
+ ? record.done : fail(`${label}.done`, 'must be boolean');
582
+ return bound({ ...readRestoreScope(record, label), done }, label);
583
+ };
package/ts/runtime.ts CHANGED
@@ -46,6 +46,7 @@ export * from './runtime.corestore.js';
46
46
  export * from './runtime.corestore.database.js';
47
47
  export * from './runtime.corestore.databaseexportpackage.js';
48
48
  export * from './runtime.corestore.relay.js';
49
+ export * from './runtime.corestore.objectstoragebackup.js';
49
50
  export * from './runtime.assignmentsecret.js';
50
51
  export * from './runtime.appstorecatalog.js';
51
52