@git-stunts/git-cas 6.0.1 → 6.2.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.
- package/ARCHITECTURE.md +120 -6
- package/CHANGELOG.md +98 -1
- package/GUIDE.md +114 -13
- package/README.md +60 -4
- package/UPGRADING.md +71 -1
- package/build-info.json +2 -2
- package/docs/API.md +934 -7
- package/docs/WALKTHROUGH.md +46 -0
- package/docs/releases/v6.1.0.md +75 -0
- package/docs/releases/v6.2.0.md +131 -0
- package/index.d.ts +974 -1
- package/index.js +354 -1
- package/package.json +8 -6
- package/src/domain/errors/Codes.js +48 -0
- package/src/domain/helpers/assertCanonicalTimestamp.js +15 -0
- package/src/domain/helpers/handleTarget.js +48 -0
- package/src/domain/helpers/isCanonicalCollectionKey.js +35 -0
- package/src/domain/services/AssetService.js +213 -0
- package/src/domain/services/BundleDescriptorCodec.js +286 -0
- package/src/domain/services/BundleFanoutBuilder.js +247 -0
- package/src/domain/services/BundleService.js +848 -0
- package/src/domain/services/CacheCandidateHeap.js +71 -0
- package/src/domain/services/CacheIndex.js +362 -0
- package/src/domain/services/CacheMetadataCodec.js +164 -0
- package/src/domain/services/CachePolicyEnforcer.js +144 -0
- package/src/domain/services/CacheSet.js +703 -0
- package/src/domain/services/CacheSetRegistry.js +88 -0
- package/src/domain/services/CasService.d.ts +3 -1
- package/src/domain/services/ExpiringSet.js +496 -0
- package/src/domain/services/ExpiringSetIndex.js +254 -0
- package/src/domain/services/ExpiringSetMetadataCodec.js +160 -0
- package/src/domain/services/ExpiringSetRegistry.js +101 -0
- package/src/domain/services/PageService.js +175 -0
- package/src/domain/services/PublicationService.js +315 -0
- package/src/domain/services/RepositoryDoctor.js +649 -0
- package/src/domain/services/RetentionService.js +120 -0
- package/src/domain/services/RootSet.js +281 -0
- package/src/domain/services/RootSetMetadataCodec.js +259 -0
- package/src/domain/services/RootSetPersistence.js +335 -0
- package/src/domain/services/RootSetRegistry.js +44 -0
- package/src/domain/services/RootSetRetryPolicy.js +87 -0
- package/src/domain/services/RootSetTreeCodec.js +95 -0
- package/src/domain/services/StagingEvidence.js +39 -0
- package/src/domain/value-objects/ApplicationHandle.js +40 -0
- package/src/domain/value-objects/AssetHandle.js +163 -0
- package/src/domain/value-objects/BundleHandle.js +32 -0
- package/src/domain/value-objects/BundleLimits.js +80 -0
- package/src/domain/value-objects/BundlePath.js +78 -0
- package/src/domain/value-objects/CacheHit.js +93 -0
- package/src/domain/value-objects/CacheKey.js +32 -0
- package/src/domain/value-objects/CachePolicy.js +59 -0
- package/src/domain/value-objects/CacheSetRef.js +40 -0
- package/src/domain/value-objects/CollectionNamespace.js +54 -0
- package/src/domain/value-objects/ExpiringMarker.js +81 -0
- package/src/domain/value-objects/ExpiringSetKey.js +28 -0
- package/src/domain/value-objects/ExpiringSetRef.js +42 -0
- package/src/domain/value-objects/OpaqueHandle.js +171 -0
- package/src/domain/value-objects/PageHandle.js +34 -0
- package/src/domain/value-objects/RetentionWitness.js +101 -0
- package/src/domain/value-objects/RootSetRef.js +88 -0
- package/src/domain/value-objects/StagedAsset.js +63 -0
- package/src/domain/value-objects/StagedBundle.js +66 -0
- package/src/domain/value-objects/StagedPage.js +47 -0
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +57 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +26 -5
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +164 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +20 -1
- package/src/ports/GitRefPort.js +10 -0
- package/src/ports/RepositoryInspectionPort.js +33 -0
|
@@ -0,0 +1,848 @@
|
|
|
1
|
+
import { isBytes } from '../bytes/ByteLayout.js';
|
|
2
|
+
import createCasError from '../errors/createCasError.js';
|
|
3
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
4
|
+
import { assertHandleObjectType } from '../helpers/handleTarget.js';
|
|
5
|
+
import parseApplicationHandle from '../value-objects/ApplicationHandle.js';
|
|
6
|
+
import BundleHandle from '../value-objects/BundleHandle.js';
|
|
7
|
+
import BundleLimits from '../value-objects/BundleLimits.js';
|
|
8
|
+
import normalizeBundlePath from '../value-objects/BundlePath.js';
|
|
9
|
+
import StagedBundle from '../value-objects/StagedBundle.js';
|
|
10
|
+
import BundleDescriptorCodec, { BUNDLE_INDEX_ENTRY } from './BundleDescriptorCodec.js';
|
|
11
|
+
import BundleFanoutBuilder from './BundleFanoutBuilder.js';
|
|
12
|
+
import StagingEvidence from './StagingEvidence.js';
|
|
13
|
+
|
|
14
|
+
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
15
|
+
const DEFAULT_MAX_NESTING_DEPTH = 32;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Builds and traverses deterministic, targeted structured bundle trees.
|
|
19
|
+
*/
|
|
20
|
+
export default class BundleService {
|
|
21
|
+
#clock;
|
|
22
|
+
#codec;
|
|
23
|
+
#limits;
|
|
24
|
+
#maxNestingDepth;
|
|
25
|
+
#openHandle;
|
|
26
|
+
#pages;
|
|
27
|
+
#persistence;
|
|
28
|
+
#resolveHandle;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {object} options
|
|
32
|
+
* @param {import('../../ports/GitPersistencePort.js').default} options.persistence
|
|
33
|
+
* @param {import('../../ports/CodecPort.js').default} options.codec
|
|
34
|
+
* @param {import('./PageService.js').default} options.pages
|
|
35
|
+
* @param {(handle: unknown, context?: object) => Promise<object>} options.resolveHandle
|
|
36
|
+
* @param {(handle: unknown) => AsyncIterable<Uint8Array>|Promise<AsyncIterable<Uint8Array>>} options.openHandle
|
|
37
|
+
* @param {object} [options.limits]
|
|
38
|
+
* @param {number} [options.maxNestingDepth]
|
|
39
|
+
* @param {{ now(): Date }} [options.clock]
|
|
40
|
+
*/
|
|
41
|
+
constructor({
|
|
42
|
+
persistence,
|
|
43
|
+
codec,
|
|
44
|
+
pages,
|
|
45
|
+
resolveHandle,
|
|
46
|
+
openHandle,
|
|
47
|
+
limits,
|
|
48
|
+
maxNestingDepth = DEFAULT_MAX_NESTING_DEPTH,
|
|
49
|
+
clock = DEFAULT_CLOCK,
|
|
50
|
+
}) {
|
|
51
|
+
BundleService.#assertDependencies({ persistence, codec, pages, resolveHandle, openHandle, clock });
|
|
52
|
+
if (!Number.isSafeInteger(maxNestingDepth) || maxNestingDepth < 1) {
|
|
53
|
+
throw createCasError('Bundle nesting depth must be a positive safe integer', ErrorCodes.INVALID_OPTIONS);
|
|
54
|
+
}
|
|
55
|
+
this.#persistence = persistence;
|
|
56
|
+
this.#codec = new BundleDescriptorCodec(codec);
|
|
57
|
+
this.#pages = pages;
|
|
58
|
+
this.#resolveHandle = resolveHandle;
|
|
59
|
+
this.#openHandle = openHandle;
|
|
60
|
+
this.#limits = new BundleLimits(limits);
|
|
61
|
+
this.#maxNestingDepth = maxNestingDepth;
|
|
62
|
+
this.#clock = clock;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Convenience builder for an object, Map, or array of member pairs.
|
|
67
|
+
*
|
|
68
|
+
* @param {{ members: object|Map<string, unknown>|Array<[string, unknown]>, limits?: object }} options
|
|
69
|
+
*/
|
|
70
|
+
async put({ members, limits }) {
|
|
71
|
+
const entries = BundleService.#sortableEntries(members);
|
|
72
|
+
entries.sort((left, right) => compareText(left[0], right[0]));
|
|
73
|
+
return await this.#putOrdered(entries, limits);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Bounded builder for members already ordered by canonical path.
|
|
78
|
+
*
|
|
79
|
+
* @param {{ members: Iterable<[string, unknown]>|AsyncIterable<[string, unknown]>, limits?: object }} options
|
|
80
|
+
*/
|
|
81
|
+
async putOrdered({ members, limits }) {
|
|
82
|
+
if (!isIterable(members)) {
|
|
83
|
+
throw createCasError('Ordered bundle members must be an iterable', ErrorCodes.BUNDLE_MEMBER_INVALID);
|
|
84
|
+
}
|
|
85
|
+
return await this.#putOrdered(members, limits);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @internal Builds from handles whose complete support graphs were already validated. */
|
|
89
|
+
async putOrderedReferences({ members, limits }) {
|
|
90
|
+
if (!isIterable(members)) {
|
|
91
|
+
throw createCasError('Ordered bundle members must be an iterable', ErrorCodes.BUNDLE_MEMBER_INVALID);
|
|
92
|
+
}
|
|
93
|
+
return await this.#putOrdered(members, limits, { validateTargets: false });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Returns one targeted member descriptor without opening its payload.
|
|
98
|
+
*
|
|
99
|
+
* @param {{ handle: BundleHandle|string|object, path: string }} options
|
|
100
|
+
* @returns {Promise<object|null>}
|
|
101
|
+
*/
|
|
102
|
+
async getMember({ handle: value, path: rawPath }) {
|
|
103
|
+
const member = await this.getMemberReference({ handle: value, path: rawPath });
|
|
104
|
+
return member === null ? null : await this.#resolveMemberReference(member, BundleHandle.from(value));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** @internal Returns one descriptor after validating only its direct Git edge. */
|
|
108
|
+
async getMemberReference({ handle: value, path: rawPath }) {
|
|
109
|
+
const handle = BundleHandle.from(value);
|
|
110
|
+
const path = normalizeBundlePath(rawPath, this.#limits.maxMemberPathBytes);
|
|
111
|
+
const root = await this.#readRoot(handle);
|
|
112
|
+
let nodeOid = root.index.oid;
|
|
113
|
+
while (true) {
|
|
114
|
+
const node = await this.#readNode(nodeOid, root.descriptor.limits);
|
|
115
|
+
if (node.descriptor.kind === 'leaf') {
|
|
116
|
+
return await this.#referenceFromLeaf({
|
|
117
|
+
bundleHandle: handle,
|
|
118
|
+
nodeOid,
|
|
119
|
+
descriptor: node.descriptor,
|
|
120
|
+
path,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const child = findRange(node.descriptor.entries, path);
|
|
124
|
+
if (!child) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const edge = await this.#requiredEdge(nodeOid, child.slot, 'tree');
|
|
128
|
+
nodeOid = edge.oid;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Streams one byte-addressable member without hydrating unrelated members.
|
|
134
|
+
*
|
|
135
|
+
* @param {{ handle: BundleHandle|string|object, path: string }} options
|
|
136
|
+
*/
|
|
137
|
+
async *openMember({ handle, path }) {
|
|
138
|
+
const member = await this.getMember({ handle, path });
|
|
139
|
+
if (!member) {
|
|
140
|
+
throw createCasError('Bundle member was not found', ErrorCodes.BUNDLE_MEMBER_NOT_FOUND, {
|
|
141
|
+
handle: BundleHandle.from(handle).toString(),
|
|
142
|
+
path,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
if (member.handle.kind === 'bundle') {
|
|
146
|
+
throw createCasError(
|
|
147
|
+
'Bundle members are structured handles and cannot be opened as bytes',
|
|
148
|
+
ErrorCodes.BUNDLE_MEMBER_NOT_STREAMABLE,
|
|
149
|
+
{ handle: BundleHandle.from(handle).toString(), path, memberHandle: member.handle.toString() }
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
yield* await this.#openHandle(member.handle);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
throw augmentError(error, { bundleHandle: BundleHandle.from(handle).toString(), memberPath: path });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Validates the complete bundle support graph.
|
|
161
|
+
*
|
|
162
|
+
* @param {BundleHandle|string|object} value
|
|
163
|
+
* @param {{ nestingDepth?: number }} [context]
|
|
164
|
+
*/
|
|
165
|
+
async resolveRoot(value, { nestingDepth = 0, validation } = {}) {
|
|
166
|
+
if (nestingDepth > this.#maxNestingDepth) {
|
|
167
|
+
throw createCasError('Bundle nesting exceeds its configured limit', ErrorCodes.BUNDLE_FANOUT_LIMIT, {
|
|
168
|
+
nestingDepth,
|
|
169
|
+
maxNestingDepth: this.#maxNestingDepth,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
const handle = BundleHandle.from(value);
|
|
173
|
+
const root = await this.#readRoot(handle);
|
|
174
|
+
const budget = {
|
|
175
|
+
memberCount: 0,
|
|
176
|
+
descriptorBytes: root.descriptorBytes,
|
|
177
|
+
logicalBytes: root.descriptorBytes,
|
|
178
|
+
handles: new Set(),
|
|
179
|
+
};
|
|
180
|
+
const summary = await this.#validateNode(root.index.oid, {
|
|
181
|
+
nestingDepth,
|
|
182
|
+
limits: root.descriptor.limits,
|
|
183
|
+
budget,
|
|
184
|
+
validation,
|
|
185
|
+
});
|
|
186
|
+
assertSummary(root.descriptor, summary);
|
|
187
|
+
if (budget.memberCount !== summary.count) {
|
|
188
|
+
throw corrupt('Bundle validation budget does not match its member count', {
|
|
189
|
+
expectedMembers: summary.count,
|
|
190
|
+
observedMembers: budget.memberCount,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return Object.freeze({
|
|
194
|
+
handle,
|
|
195
|
+
oid: handle.oid,
|
|
196
|
+
type: 'tree',
|
|
197
|
+
size: null,
|
|
198
|
+
logicalBytes: budget.logicalBytes,
|
|
199
|
+
memberCount: summary.count,
|
|
200
|
+
indexDepth: summary.depth,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Streams validated member descriptors in canonical path order. */
|
|
205
|
+
async *iterateMembers({ handle: value }) {
|
|
206
|
+
yield* this.#iterateMemberReferences(value, { validateTargets: true });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** @internal Streams descriptors after validating only their direct Git edges. */
|
|
210
|
+
async *iterateMemberReferences({ handle: value }) {
|
|
211
|
+
yield* this.#iterateMemberReferences(value, { validateTargets: false });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async *#iterateMemberReferences(value, { validateTargets }) {
|
|
215
|
+
const handle = BundleHandle.from(value);
|
|
216
|
+
const root = await this.#readRoot(handle);
|
|
217
|
+
const budget = {
|
|
218
|
+
memberCount: 0,
|
|
219
|
+
descriptorBytes: root.descriptorBytes,
|
|
220
|
+
};
|
|
221
|
+
const context = { limits: root.descriptor.limits, budget, validateTargets };
|
|
222
|
+
const summary = yield* this.#iterateNode(handle, root.index.oid, context);
|
|
223
|
+
assertSummary(root.descriptor, summary);
|
|
224
|
+
if (budget.memberCount !== summary.count) {
|
|
225
|
+
throw corrupt('Bundle iteration budget does not match its member count', {
|
|
226
|
+
expectedMembers: summary.count,
|
|
227
|
+
observedMembers: budget.memberCount,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async *#iterateNode(handle, treeOid, context) {
|
|
233
|
+
const { limits, budget, validateTargets } = context;
|
|
234
|
+
const node = await this.#readNode(treeOid, limits);
|
|
235
|
+
budget.descriptorBytes += node.descriptorBytes;
|
|
236
|
+
if (budget.descriptorBytes > limits.maxDescriptorBytes) {
|
|
237
|
+
throw corrupt('Bundle descriptors exceed their persisted byte limit', {
|
|
238
|
+
descriptorBytes: budget.descriptorBytes,
|
|
239
|
+
maxDescriptorBytes: limits.maxDescriptorBytes,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
if (node.descriptor.kind === 'leaf') {
|
|
243
|
+
budget.memberCount += node.descriptor.count;
|
|
244
|
+
if (budget.memberCount > limits.maxMembers) {
|
|
245
|
+
throw corrupt('Bundle members exceed their persisted limit', {
|
|
246
|
+
memberCount: budget.memberCount,
|
|
247
|
+
maxMembers: limits.maxMembers,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
for (const entry of node.descriptor.entries) {
|
|
251
|
+
const reference = await this.#referenceFromLeaf({
|
|
252
|
+
bundleHandle: handle,
|
|
253
|
+
nodeOid: treeOid,
|
|
254
|
+
descriptor: node.descriptor,
|
|
255
|
+
path: entry.path,
|
|
256
|
+
});
|
|
257
|
+
yield validateTargets
|
|
258
|
+
? await this.#resolveMemberReference(reference, handle)
|
|
259
|
+
: reference;
|
|
260
|
+
}
|
|
261
|
+
return summaryOf(node.descriptor, treeOid);
|
|
262
|
+
}
|
|
263
|
+
for (const child of node.descriptor.entries) {
|
|
264
|
+
const edge = await this.#requiredEdge(treeOid, child.slot, 'tree');
|
|
265
|
+
const actual = yield* this.#iterateNode(handle, edge.oid, context);
|
|
266
|
+
assertChildSummary(child, actual, treeOid);
|
|
267
|
+
}
|
|
268
|
+
return summaryOf(node.descriptor, treeOid);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async #putOrdered(members, overrides, { validateTargets = true } = {}) {
|
|
272
|
+
const limits = this.#limits.lower(overrides);
|
|
273
|
+
const observedAt = this.#observedAt();
|
|
274
|
+
const staging = new StagingEvidence();
|
|
275
|
+
const builder = new BundleFanoutBuilder({
|
|
276
|
+
persistence: this.#persistence,
|
|
277
|
+
codec: this.#codec,
|
|
278
|
+
limits,
|
|
279
|
+
staging,
|
|
280
|
+
});
|
|
281
|
+
let previousPath = null;
|
|
282
|
+
let memberCount = 0;
|
|
283
|
+
const validation = { active: new Set(), cache: new Map() };
|
|
284
|
+
try {
|
|
285
|
+
for await (const pair of members) {
|
|
286
|
+
const [rawPath, value] = BundleService.#memberPair(pair);
|
|
287
|
+
const path = normalizeBundlePath(rawPath, limits.maxMemberPathBytes);
|
|
288
|
+
BundleService.#assertOrder(path, previousPath);
|
|
289
|
+
memberCount += 1;
|
|
290
|
+
if (memberCount > limits.maxMembers) {
|
|
291
|
+
throw createCasError('Bundle exceeds its member limit', ErrorCodes.BUNDLE_MEMBER_LIMIT, {
|
|
292
|
+
observedMembers: memberCount,
|
|
293
|
+
maxMembers: limits.maxMembers,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
const member = await this.#prepareMember(path, value, {
|
|
297
|
+
staging,
|
|
298
|
+
validation,
|
|
299
|
+
validateTargets,
|
|
300
|
+
});
|
|
301
|
+
await builder.add(member);
|
|
302
|
+
previousPath = path;
|
|
303
|
+
}
|
|
304
|
+
const result = await builder.finish();
|
|
305
|
+
return new StagedBundle({
|
|
306
|
+
handle: new BundleHandle({ codec: this.#codec.extension, oid: result.oid }),
|
|
307
|
+
memberCount: result.memberCount,
|
|
308
|
+
indexDepth: result.indexDepth,
|
|
309
|
+
descriptorBytes: result.descriptorBytes,
|
|
310
|
+
limits,
|
|
311
|
+
observedAt,
|
|
312
|
+
});
|
|
313
|
+
} catch (error) {
|
|
314
|
+
throw augmentError(error, { staging: staging.snapshot() });
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async #prepareMember(path, value, { staging, validation, validateTargets }) {
|
|
319
|
+
const inline = inlinePageOptions(value);
|
|
320
|
+
if (inline) {
|
|
321
|
+
const staged = await this.#pages.put(inline);
|
|
322
|
+
staging.record(staged.handle.oid, 'blob');
|
|
323
|
+
staging.recordHandle(staged.handle);
|
|
324
|
+
return Object.freeze({
|
|
325
|
+
path,
|
|
326
|
+
handle: staged.handle,
|
|
327
|
+
oid: staged.handle.oid,
|
|
328
|
+
type: 'blob',
|
|
329
|
+
size: staged.page.size,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
return await this.#prepareHandleMember(path, value, { validation, validateTargets });
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async #prepareHandleMember(path, value, { validation, validateTargets }) {
|
|
336
|
+
let handle;
|
|
337
|
+
try {
|
|
338
|
+
const reference = validateTargets ? null : bundleReference(value);
|
|
339
|
+
handle = parseApplicationHandle(reference?.handle ?? value);
|
|
340
|
+
const target = validateTargets
|
|
341
|
+
? await this.#resolveHandle(handle, { nestingDepth: 1, validation })
|
|
342
|
+
: await this.#resolveHandleReference(handle, reference?.size);
|
|
343
|
+
assertResolvedTarget(handle, target);
|
|
344
|
+
return Object.freeze({
|
|
345
|
+
path,
|
|
346
|
+
handle,
|
|
347
|
+
oid: target.oid,
|
|
348
|
+
type: target.type,
|
|
349
|
+
size: target.size ?? null,
|
|
350
|
+
});
|
|
351
|
+
} catch (error) {
|
|
352
|
+
throw augmentError(error, { memberPath: path, memberHandle: handle?.toString() ?? null });
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async #resolveHandleReference(handle, declaredSize) {
|
|
357
|
+
const type = handle.kind === 'page' ? 'blob' : 'tree';
|
|
358
|
+
await assertHandleObjectType({
|
|
359
|
+
persistence: this.#persistence,
|
|
360
|
+
handle,
|
|
361
|
+
oid: handle.oid,
|
|
362
|
+
expectedType: type,
|
|
363
|
+
});
|
|
364
|
+
const objectSize = type === 'blob' ? await this.#persistence.readObjectSize(handle.oid) : null;
|
|
365
|
+
const size = declaredSize === undefined ? objectSize : declaredSize;
|
|
366
|
+
if ((size !== null && (!Number.isSafeInteger(size) || size < 0)) ||
|
|
367
|
+
(type === 'blob' && size !== objectSize)) {
|
|
368
|
+
throw createCasError(
|
|
369
|
+
'Prevalidated bundle reference size does not match its target',
|
|
370
|
+
ErrorCodes.BUNDLE_MEMBER_INVALID,
|
|
371
|
+
{ handle: handle.toString(), size, objectSize },
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
return Object.freeze({ oid: handle.oid, type, size });
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async #readRoot(handle) {
|
|
378
|
+
this.#assertCodec(handle);
|
|
379
|
+
await assertHandleObjectType({
|
|
380
|
+
persistence: this.#persistence,
|
|
381
|
+
handle,
|
|
382
|
+
oid: handle.oid,
|
|
383
|
+
expectedType: 'tree',
|
|
384
|
+
});
|
|
385
|
+
const descriptorEntry = await this.#requiredEdge(handle.oid, this.#codec.rootEntryName, 'blob');
|
|
386
|
+
const descriptorBytes = await this.#readDescriptorBlob(
|
|
387
|
+
descriptorEntry.oid,
|
|
388
|
+
{ handle: handle.toString(), kind: 'root' },
|
|
389
|
+
this.#limits.maxDescriptorBytes
|
|
390
|
+
);
|
|
391
|
+
const descriptor = this.#codec.decodeRoot(descriptorBytes);
|
|
392
|
+
this.#assertReadableLimits(descriptor.limits);
|
|
393
|
+
if (descriptorBytes.length > descriptor.limits.maxDescriptorBytes) {
|
|
394
|
+
throw corrupt('Bundle root descriptor exceeds its persisted byte limit', {
|
|
395
|
+
descriptorBytes: descriptorBytes.length,
|
|
396
|
+
maxDescriptorBytes: descriptor.limits.maxDescriptorBytes,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
const index = await this.#requiredEdge(handle.oid, BUNDLE_INDEX_ENTRY, 'tree');
|
|
400
|
+
return Object.freeze({ descriptor, descriptorBytes: descriptorBytes.length, index });
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
async #readNode(treeOid, limits) {
|
|
404
|
+
const descriptorEntry = await this.#requiredEdge(treeOid, this.#codec.nodeEntryName, 'blob');
|
|
405
|
+
const descriptorBytes = await this.#readDescriptorBlob(descriptorEntry.oid, {
|
|
406
|
+
treeOid,
|
|
407
|
+
kind: 'node',
|
|
408
|
+
}, limits.maxDescriptorBytes);
|
|
409
|
+
const descriptor = this.#codec.decodeNode(descriptorBytes, limits);
|
|
410
|
+
if (descriptor.depth > limits.maxFanoutDepth) {
|
|
411
|
+
throw corrupt('Bundle node exceeds its persisted depth limit', {
|
|
412
|
+
treeOid,
|
|
413
|
+
depth: descriptor.depth,
|
|
414
|
+
maxFanoutDepth: limits.maxFanoutDepth,
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
if (descriptor.count > limits.maxMembers) {
|
|
418
|
+
throw corrupt('Bundle node exceeds its persisted member limit', {
|
|
419
|
+
treeOid,
|
|
420
|
+
memberCount: descriptor.count,
|
|
421
|
+
maxMembers: limits.maxMembers,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
if (descriptor.entries.length + 1 > limits.maxFanoutEntries) {
|
|
425
|
+
throw corrupt('Bundle node exceeds its persisted fanout limit', {
|
|
426
|
+
treeOid,
|
|
427
|
+
entries: descriptor.entries.length + 1,
|
|
428
|
+
maxFanoutEntries: limits.maxFanoutEntries,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
return Object.freeze({
|
|
432
|
+
descriptor,
|
|
433
|
+
descriptorBytes: descriptorBytes.length,
|
|
434
|
+
descriptorOid: descriptorEntry.oid,
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
async #referenceFromLeaf({ bundleHandle, nodeOid, descriptor, path }) {
|
|
439
|
+
const entry = findPath(descriptor.entries, path);
|
|
440
|
+
if (!entry) {
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
const handle = parseApplicationHandle(entry.handle);
|
|
444
|
+
const edge = await this.#requiredEdge(nodeOid, entry.slot, entry.type);
|
|
445
|
+
if (edge.oid !== handle.oid) {
|
|
446
|
+
throw corrupt('Bundle member edge does not match its handle', {
|
|
447
|
+
bundleHandle: bundleHandle.toString(),
|
|
448
|
+
memberPath: path,
|
|
449
|
+
expectedOid: handle.oid,
|
|
450
|
+
actualOid: edge.oid,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
try {
|
|
454
|
+
await assertHandleObjectType({
|
|
455
|
+
persistence: this.#persistence,
|
|
456
|
+
handle,
|
|
457
|
+
oid: handle.oid,
|
|
458
|
+
expectedType: entry.type,
|
|
459
|
+
});
|
|
460
|
+
} catch (error) {
|
|
461
|
+
throw augmentError(error, {
|
|
462
|
+
bundleHandle: bundleHandle.toString(),
|
|
463
|
+
memberPath: path,
|
|
464
|
+
memberHandle: handle.toString(),
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
return Object.freeze({ version: 1, path, handle, type: entry.type, size: entry.size });
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
async #resolveMemberReference(reference, bundleHandle) {
|
|
471
|
+
let target;
|
|
472
|
+
try {
|
|
473
|
+
target = await this.#resolveHandle(reference.handle, { nestingDepth: 1 });
|
|
474
|
+
assertMemberTarget({
|
|
475
|
+
member: reference,
|
|
476
|
+
handle: reference.handle,
|
|
477
|
+
target,
|
|
478
|
+
meta: { bundleHandle: bundleHandle.toString(), memberPath: reference.path },
|
|
479
|
+
});
|
|
480
|
+
} catch (error) {
|
|
481
|
+
throw augmentError(error, {
|
|
482
|
+
bundleHandle: bundleHandle.toString(),
|
|
483
|
+
memberPath: reference.path,
|
|
484
|
+
memberHandle: reference.handle.toString(),
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
return Object.freeze({
|
|
488
|
+
...reference,
|
|
489
|
+
logicalBytes: target.logicalBytes ?? target.size ?? null,
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
async #validateNode(treeOid, context) {
|
|
494
|
+
const node = await this.#readNode(treeOid, context.limits);
|
|
495
|
+
context.budget.descriptorBytes += node.descriptorBytes;
|
|
496
|
+
context.budget.logicalBytes = addLogicalBytes(
|
|
497
|
+
context.budget.logicalBytes,
|
|
498
|
+
node.descriptorBytes,
|
|
499
|
+
);
|
|
500
|
+
if (context.budget.descriptorBytes > context.limits.maxDescriptorBytes) {
|
|
501
|
+
throw corrupt('Bundle descriptors exceed their persisted byte limit', {
|
|
502
|
+
descriptorBytes: context.budget.descriptorBytes,
|
|
503
|
+
maxDescriptorBytes: context.limits.maxDescriptorBytes,
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
if (node.descriptor.kind === 'leaf') {
|
|
507
|
+
context.budget.memberCount += node.descriptor.count;
|
|
508
|
+
if (context.budget.memberCount > context.limits.maxMembers) {
|
|
509
|
+
throw corrupt('Bundle members exceed their persisted limit', {
|
|
510
|
+
memberCount: context.budget.memberCount,
|
|
511
|
+
maxMembers: context.limits.maxMembers,
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
await this.#validateLeafTargets(treeOid, node.descriptor, context);
|
|
515
|
+
return summaryOf(node.descriptor, treeOid);
|
|
516
|
+
}
|
|
517
|
+
for (const child of node.descriptor.entries) {
|
|
518
|
+
const edge = await this.#requiredEdge(treeOid, child.slot, 'tree');
|
|
519
|
+
const actual = await this.#validateNode(edge.oid, context);
|
|
520
|
+
assertChildSummary(child, actual, treeOid);
|
|
521
|
+
}
|
|
522
|
+
return summaryOf(node.descriptor, treeOid);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
async #validateLeafTargets(treeOid, descriptor, context) {
|
|
526
|
+
for (const member of descriptor.entries) {
|
|
527
|
+
const handle = parseApplicationHandle(member.handle);
|
|
528
|
+
const edge = await this.#requiredEdge(treeOid, member.slot, member.type);
|
|
529
|
+
if (edge.oid !== handle.oid) {
|
|
530
|
+
throw corrupt('Bundle member target edge is inconsistent', {
|
|
531
|
+
treeOid,
|
|
532
|
+
memberPath: member.path,
|
|
533
|
+
expectedOid: handle.oid,
|
|
534
|
+
actualOid: edge.oid,
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
try {
|
|
538
|
+
const target = await this.#resolveHandle(handle, {
|
|
539
|
+
nestingDepth: context.nestingDepth + 1,
|
|
540
|
+
validation: context.validation,
|
|
541
|
+
});
|
|
542
|
+
assertMemberTarget({
|
|
543
|
+
member,
|
|
544
|
+
handle,
|
|
545
|
+
target,
|
|
546
|
+
meta: { treeOid, memberPath: member.path },
|
|
547
|
+
});
|
|
548
|
+
const token = handle.toString();
|
|
549
|
+
if (!context.budget.handles.has(token)) {
|
|
550
|
+
context.budget.handles.add(token);
|
|
551
|
+
context.budget.logicalBytes = addLogicalBytes(
|
|
552
|
+
context.budget.logicalBytes,
|
|
553
|
+
target.logicalBytes ?? target.size,
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
} catch (error) {
|
|
557
|
+
throw augmentError(error, { treeOid, memberPath: member.path, memberHandle: handle.toString() });
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
async #requiredEdge(treeOid, name, expectedType) {
|
|
563
|
+
let entry;
|
|
564
|
+
try {
|
|
565
|
+
entry = await this.#readTreeEntry(treeOid, name);
|
|
566
|
+
} catch (error) {
|
|
567
|
+
throw corrupt('Bundle tree entry could not be read', { treeOid, name, originalError: error });
|
|
568
|
+
}
|
|
569
|
+
if (!entry || entry.type !== expectedType) {
|
|
570
|
+
throw corrupt('Bundle tree entry is missing or has the wrong type', {
|
|
571
|
+
treeOid,
|
|
572
|
+
name,
|
|
573
|
+
expectedType,
|
|
574
|
+
actualType: entry?.type ?? null,
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
return entry;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
async #readTreeEntry(treeOid, name) {
|
|
581
|
+
if (typeof this.#persistence.readTreeEntry === 'function') {
|
|
582
|
+
return await this.#persistence.readTreeEntry(treeOid, name);
|
|
583
|
+
}
|
|
584
|
+
const entries = await this.#persistence.readTree(treeOid);
|
|
585
|
+
return entries.find((entry) => entry.name === name) ?? null;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
async #readDescriptorBlob(oid, meta, maxBytes) {
|
|
589
|
+
try {
|
|
590
|
+
return await this.#persistence.readBlob(oid, maxBytes);
|
|
591
|
+
} catch (error) {
|
|
592
|
+
throw corrupt('Bundle descriptor blob is missing or unreadable', { ...meta, oid, originalError: error });
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
#assertCodec(handle) {
|
|
597
|
+
if (handle.codec !== this.#codec.extension) {
|
|
598
|
+
throw createCasError(
|
|
599
|
+
'Bundle handle codec does not match this CAS instance',
|
|
600
|
+
ErrorCodes.HANDLE_CODEC_MISMATCH,
|
|
601
|
+
{ handle: handle.toString(), expectedCodec: this.#codec.extension, actualCodec: handle.codec }
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
#assertReadableLimits(limits) {
|
|
607
|
+
const codes = {
|
|
608
|
+
maxMembers: ErrorCodes.BUNDLE_MEMBER_LIMIT,
|
|
609
|
+
maxMemberPathBytes: ErrorCodes.BUNDLE_PATH_LIMIT,
|
|
610
|
+
maxDescriptorBytes: ErrorCodes.BUNDLE_DESCRIPTOR_LIMIT,
|
|
611
|
+
maxFanoutEntries: ErrorCodes.BUNDLE_FANOUT_LIMIT,
|
|
612
|
+
maxFanoutDepth: ErrorCodes.BUNDLE_FANOUT_LIMIT,
|
|
613
|
+
};
|
|
614
|
+
for (const field of Object.keys(codes)) {
|
|
615
|
+
if (limits[field] > this.#limits[field]) {
|
|
616
|
+
throw createCasError(
|
|
617
|
+
'Bundle admission policy exceeds this repository read limit',
|
|
618
|
+
codes[field],
|
|
619
|
+
{ field, observed: limits[field], configured: this.#limits[field] }
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
#observedAt() {
|
|
626
|
+
const now = this.#clock.now();
|
|
627
|
+
if (!(now instanceof Date) || Number.isNaN(now.getTime())) {
|
|
628
|
+
throw createCasError('BundleService clock returned an invalid Date', ErrorCodes.INVALID_OPTIONS);
|
|
629
|
+
}
|
|
630
|
+
return now.toISOString();
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
static #sortableEntries(members) {
|
|
634
|
+
if (members instanceof Map || Array.isArray(members)) {
|
|
635
|
+
return [...members];
|
|
636
|
+
}
|
|
637
|
+
if (members && typeof members === 'object' && !isIterable(members)) {
|
|
638
|
+
return Object.entries(members);
|
|
639
|
+
}
|
|
640
|
+
throw createCasError(
|
|
641
|
+
'bundles.put() requires an object, Map, or array; use putOrdered() for iterables',
|
|
642
|
+
ErrorCodes.BUNDLE_MEMBER_INVALID
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
static #memberPair(value) {
|
|
647
|
+
if (!Array.isArray(value) || value.length !== 2) {
|
|
648
|
+
throw createCasError('Bundle member must be a [path, value] pair', ErrorCodes.BUNDLE_MEMBER_INVALID, {
|
|
649
|
+
member: value,
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
return value;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
static #assertOrder(path, previousPath) {
|
|
656
|
+
if (previousPath === null || path > previousPath) {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
const duplicate = path === previousPath;
|
|
660
|
+
throw createCasError(
|
|
661
|
+
duplicate ? 'Bundle contains a duplicate member path' : 'Ordered bundle members are out of order',
|
|
662
|
+
duplicate ? ErrorCodes.BUNDLE_DUPLICATE_PATH : ErrorCodes.BUNDLE_MEMBER_ORDER,
|
|
663
|
+
{ path, previousPath }
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
static #assertDependencies({ persistence, codec, pages, resolveHandle, openHandle, clock }) {
|
|
668
|
+
BundleService.#assertPersistence(persistence);
|
|
669
|
+
BundleService.#assertCodecDependency(codec);
|
|
670
|
+
BundleService.#assertPageDependency(pages);
|
|
671
|
+
BundleService.#assertHandleDependencies(resolveHandle, openHandle);
|
|
672
|
+
if (!clock || typeof clock.now !== 'function') {
|
|
673
|
+
throw createCasError('BundleService clock must provide now()', ErrorCodes.INVALID_OPTIONS);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
static #assertPersistence(persistence) {
|
|
678
|
+
const methods = [
|
|
679
|
+
'writeBlob',
|
|
680
|
+
'writeTree',
|
|
681
|
+
'readBlob',
|
|
682
|
+
'readTree',
|
|
683
|
+
'readObjectType',
|
|
684
|
+
'readObjectSize',
|
|
685
|
+
];
|
|
686
|
+
if (!persistence || methods.some((method) => typeof persistence[method] !== 'function')) {
|
|
687
|
+
throw createCasError('BundleService requires a complete persistence port', ErrorCodes.INVALID_OPTIONS);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
static #assertCodecDependency(codec) {
|
|
692
|
+
if (!codec || typeof codec.encode !== 'function' || typeof codec.decode !== 'function') {
|
|
693
|
+
throw createCasError('BundleService requires a complete codec', ErrorCodes.INVALID_OPTIONS);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
static #assertPageDependency(pages) {
|
|
698
|
+
if (!pages || typeof pages.put !== 'function') {
|
|
699
|
+
throw createCasError('BundleService requires PageService', ErrorCodes.INVALID_OPTIONS);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
static #assertHandleDependencies(resolveHandle, openHandle) {
|
|
704
|
+
if (typeof resolveHandle !== 'function' || typeof openHandle !== 'function') {
|
|
705
|
+
throw createCasError('BundleService requires handle resolution and open callbacks', ErrorCodes.INVALID_OPTIONS);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function inlinePageOptions(value) {
|
|
711
|
+
if (isBytes(value) || (isIterable(value) && typeof value !== 'string')) {
|
|
712
|
+
return { source: value };
|
|
713
|
+
}
|
|
714
|
+
if (value && typeof value === 'object' && Object.hasOwn(value, 'source')) {
|
|
715
|
+
return { source: value.source, maxBytes: value.maxBytes };
|
|
716
|
+
}
|
|
717
|
+
return null;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
function bundleReference(value) {
|
|
721
|
+
if (value && typeof value === 'object' &&
|
|
722
|
+
Object.hasOwn(value, 'handle') && Object.hasOwn(value, 'size')) {
|
|
723
|
+
return value;
|
|
724
|
+
}
|
|
725
|
+
return null;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
function isIterable(value) {
|
|
729
|
+
return Boolean(value?.[Symbol.asyncIterator] || value?.[Symbol.iterator]);
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
function assertResolvedTarget(handle, target) {
|
|
733
|
+
const expectedType = handle.kind === 'page' ? 'blob' : 'tree';
|
|
734
|
+
if (!target || target.oid !== handle.oid || target.type !== expectedType) {
|
|
735
|
+
throw createCasError('Resolved bundle member does not match its handle', ErrorCodes.BUNDLE_MEMBER_INVALID, {
|
|
736
|
+
handle: handle.toString(),
|
|
737
|
+
expectedType,
|
|
738
|
+
actualOid: target?.oid ?? null,
|
|
739
|
+
actualType: target?.type ?? null,
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function assertMemberTarget({ member, handle, target, meta }) {
|
|
745
|
+
assertResolvedTarget(handle, target);
|
|
746
|
+
if (member.size !== (target.size ?? null)) {
|
|
747
|
+
throw corrupt('Bundle member size does not match its resolved target', {
|
|
748
|
+
...meta,
|
|
749
|
+
memberHandle: handle.toString(),
|
|
750
|
+
expectedSize: member.size,
|
|
751
|
+
actualSize: target.size ?? null,
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
function findPath(entries, path) {
|
|
757
|
+
let low = 0;
|
|
758
|
+
let high = entries.length - 1;
|
|
759
|
+
while (low <= high) {
|
|
760
|
+
const middle = Math.floor((low + high) / 2);
|
|
761
|
+
if (entries[middle].path === path) {
|
|
762
|
+
return entries[middle];
|
|
763
|
+
}
|
|
764
|
+
if (entries[middle].path < path) {
|
|
765
|
+
low = middle + 1;
|
|
766
|
+
} else {
|
|
767
|
+
high = middle - 1;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return null;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function findRange(entries, path) {
|
|
774
|
+
let low = 0;
|
|
775
|
+
let high = entries.length - 1;
|
|
776
|
+
while (low <= high) {
|
|
777
|
+
const middle = Math.floor((low + high) / 2);
|
|
778
|
+
const entry = entries[middle];
|
|
779
|
+
if (path < entry.firstPath) {
|
|
780
|
+
high = middle - 1;
|
|
781
|
+
} else if (path > entry.lastPath) {
|
|
782
|
+
low = middle + 1;
|
|
783
|
+
} else {
|
|
784
|
+
return entry;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return null;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
function summaryOf(descriptor, oid) {
|
|
791
|
+
return Object.freeze({
|
|
792
|
+
oid,
|
|
793
|
+
depth: descriptor.depth,
|
|
794
|
+
count: descriptor.count,
|
|
795
|
+
firstPath: descriptor.firstPath,
|
|
796
|
+
lastPath: descriptor.lastPath,
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
function assertChildSummary(expected, actual, parentTreeOid) {
|
|
801
|
+
for (const field of ['depth', 'count', 'firstPath', 'lastPath']) {
|
|
802
|
+
if (expected[field] !== actual[field]) {
|
|
803
|
+
throw corrupt('Bundle branch summary does not match its child', {
|
|
804
|
+
parentTreeOid,
|
|
805
|
+
childTreeOid: actual.oid,
|
|
806
|
+
field,
|
|
807
|
+
expected: expected[field],
|
|
808
|
+
actual: actual[field],
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
function assertSummary(root, actual) {
|
|
815
|
+
const expected = {
|
|
816
|
+
depth: root.index.depth,
|
|
817
|
+
count: root.memberCount,
|
|
818
|
+
firstPath: root.index.firstPath,
|
|
819
|
+
lastPath: root.index.lastPath,
|
|
820
|
+
};
|
|
821
|
+
assertChildSummary(expected, actual, root.index.entry);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
function compareText(left, right) {
|
|
825
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
function augmentError(error, meta) {
|
|
829
|
+
if (error && typeof error === 'object') {
|
|
830
|
+
error.meta = { ...error.meta, ...meta };
|
|
831
|
+
}
|
|
832
|
+
return error;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function corrupt(message, meta) {
|
|
836
|
+
return createCasError(message, ErrorCodes.BUNDLE_CORRUPT, meta);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
function addLogicalBytes(current, added) {
|
|
840
|
+
if (!Number.isSafeInteger(added) || added < 0 || !Number.isSafeInteger(current + added)) {
|
|
841
|
+
throw createCasError(
|
|
842
|
+
'Bundle logical size could not be represented safely',
|
|
843
|
+
ErrorCodes.CACHE_LOGICAL_SIZE_UNKNOWN,
|
|
844
|
+
{ current, added },
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
return current + added;
|
|
848
|
+
}
|