@git-stunts/git-cas 6.5.7 → 6.5.8
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/CHANGELOG.md +17 -0
- package/GUIDE.md +29 -0
- package/README.md +11 -7
- package/UPGRADING.md +29 -0
- package/build-info.json +2 -2
- package/docs/API.md +66 -12
- package/docs/releases/v6.5.8.md +89 -0
- package/index.d.ts +50 -0
- package/index.js +15 -1
- package/package.json +3 -2
- package/src/domain/services/AssetService.js +201 -10
- package/src/domain/services/BoundedWriteWavePersistence.js +235 -0
- package/src/domain/services/BundleBatchPlanner.js +349 -0
- package/src/domain/services/BundleService.js +225 -2
- package/src/domain/services/CasService.d.ts +16 -0
- package/src/domain/services/CasService.js +9 -18
- package/src/domain/services/ManifestRepository.js +129 -50
- package/src/domain/services/ManifestTreeService.js +47 -0
- package/src/domain/services/PageService.js +26 -9
- package/src/domain/services/RootSet.js +32 -0
- package/src/domain/services/RootSetPersistence.js +58 -1
- package/src/domain/services/StagedTarget.js +19 -0
- package/src/domain/services/StagingWorkspace.js +21 -7
- package/src/domain/services/forkCasPersistence.js +20 -0
- package/src/infrastructure/adapters/GitObjectSessionPool.js +47 -5
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +169 -0
- package/src/infrastructure/adapters/GitPersistenceWriteScope.js +154 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +38 -3
- package/src/infrastructure/adapters/GitUpdateRefSessionPool.js +115 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +43 -0
- package/src/ports/GitRefPort.js +8 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import { utf8ByteLength } from '../encoding/utf8.js';
|
|
4
|
+
import {
|
|
5
|
+
BUNDLE_DESCRIPTOR_VERSION,
|
|
6
|
+
BUNDLE_INDEX_ENTRY,
|
|
7
|
+
BUNDLE_LAYOUT,
|
|
8
|
+
} from './BundleDescriptorCodec.js';
|
|
9
|
+
|
|
10
|
+
const BLOB_MODE = '100644';
|
|
11
|
+
const TREE_MODE = '040000';
|
|
12
|
+
const SLOT_WIDTH = 6;
|
|
13
|
+
const MAX_PROTOCOL_OBJECTS = 256;
|
|
14
|
+
const MAX_PROTOCOL_BYTES = 64 * 1024 * 1024;
|
|
15
|
+
const MAX_PROTOCOL_TREE_ENTRIES = 65_536;
|
|
16
|
+
const CONSERVATIVE_OID = '0'.repeat(64);
|
|
17
|
+
|
|
18
|
+
/** Plans bounded bundle graphs before writing descriptors and dependency layers. */
|
|
19
|
+
export default class BundleBatchPlanner {
|
|
20
|
+
#codec;
|
|
21
|
+
#maxBatchBytes;
|
|
22
|
+
#maxBatchObjects;
|
|
23
|
+
#persistence;
|
|
24
|
+
#staging;
|
|
25
|
+
|
|
26
|
+
constructor({ persistence, codec, staging, maxBatchObjects, maxBatchBytes }) {
|
|
27
|
+
this.#persistence = persistence;
|
|
28
|
+
this.#codec = codec;
|
|
29
|
+
this.#staging = staging;
|
|
30
|
+
this.#maxBatchObjects = maxBatchObjects;
|
|
31
|
+
this.#maxBatchBytes = maxBatchBytes;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static estimateObjectCount(requests) {
|
|
35
|
+
return requests.reduce(
|
|
36
|
+
(total, request) => total + objectCount(request.members.length, request.limits),
|
|
37
|
+
0,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
create({ members, limits, observedAt }) {
|
|
42
|
+
const levels = [leafNodes(members, limits)];
|
|
43
|
+
while (levels.at(-1).length > 1) {
|
|
44
|
+
levels.push(branchNodes(levels.at(-1), limits));
|
|
45
|
+
}
|
|
46
|
+
const index = levels.at(-1)[0];
|
|
47
|
+
const root = rootNode({ index, memberCount: members.length, limits, codec: this.#codec });
|
|
48
|
+
const descriptors = [...levels.flat(), root];
|
|
49
|
+
let descriptorBytes = 0;
|
|
50
|
+
for (const record of descriptors) {
|
|
51
|
+
record.bytes = record.root
|
|
52
|
+
? this.#codec.encodeRoot(record.descriptor)
|
|
53
|
+
: this.#codec.encodeNode(record.descriptor);
|
|
54
|
+
descriptorBytes += record.bytes.length;
|
|
55
|
+
if (descriptorBytes > limits.maxDescriptorBytes) {
|
|
56
|
+
throw createCasError(
|
|
57
|
+
'Bundle descriptors exceed their configured byte limit',
|
|
58
|
+
ErrorCodes.BUNDLE_DESCRIPTOR_LIMIT,
|
|
59
|
+
{ descriptorBytes, maxDescriptorBytes: limits.maxDescriptorBytes },
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { levels, root, descriptors, descriptorBytes, limits, observedAt };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async write(plans) {
|
|
67
|
+
this.#assertBounds(plans);
|
|
68
|
+
await this.#writeDescriptors(plans.flatMap((plan) => plan.descriptors));
|
|
69
|
+
const maximumDepth = Math.max(...plans.map((plan) => plan.levels.length));
|
|
70
|
+
for (let depth = 0; depth < maximumDepth; depth += 1) {
|
|
71
|
+
await this.#writeTreeLayer(plans.flatMap((plan) => plan.levels[depth] ?? []));
|
|
72
|
+
}
|
|
73
|
+
await this.#writeTreeLayer(plans.map((plan) => plan.root));
|
|
74
|
+
return plans.map((plan) => ({
|
|
75
|
+
oid: plan.root.oid,
|
|
76
|
+
memberCount: plan.root.descriptor.memberCount,
|
|
77
|
+
indexDepth: plan.root.descriptor.index.depth,
|
|
78
|
+
descriptorBytes: plan.descriptorBytes,
|
|
79
|
+
limits: plan.limits,
|
|
80
|
+
observedAt: plan.observedAt,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#assertBounds(plans) {
|
|
85
|
+
const objects = plans.reduce((total, plan) => total + plan.descriptors.length * 2, 0);
|
|
86
|
+
const bytes = plans.reduce((total, plan) => total + plannedBytes(plan, this.#codec), 0);
|
|
87
|
+
if (objects > this.#maxBatchObjects) {
|
|
88
|
+
throw batchLimit('objects', objects, this.#maxBatchObjects);
|
|
89
|
+
}
|
|
90
|
+
if (bytes > this.#maxBatchBytes) {
|
|
91
|
+
throw batchLimit('bytes', bytes, this.#maxBatchBytes);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async #writeDescriptors(records) {
|
|
96
|
+
for (const batch of windows(records, descriptorWeight, {
|
|
97
|
+
maxObjects: Math.min(this.#maxBatchObjects, MAX_PROTOCOL_OBJECTS),
|
|
98
|
+
maxBytes: Math.min(this.#maxBatchBytes, MAX_PROTOCOL_BYTES),
|
|
99
|
+
})) {
|
|
100
|
+
const contents = batch.map((record) => record.bytes);
|
|
101
|
+
const oids = typeof this.#persistence.writeBlobs === 'function'
|
|
102
|
+
? await this.#persistence.writeBlobs(contents)
|
|
103
|
+
: await writeIndividually(contents, (content) => this.#persistence.writeBlob(content));
|
|
104
|
+
assertCardinality('descriptor', batch, oids);
|
|
105
|
+
for (let index = 0; index < batch.length; index += 1) {
|
|
106
|
+
batch[index].descriptorOid = oids[index];
|
|
107
|
+
this.#staging.record(oids[index], 'blob');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async #writeTreeLayer(records) {
|
|
113
|
+
const prepared = records.map((record) => ({ record, lines: treeLines(record, this.#codec) }));
|
|
114
|
+
for (const batch of treeWindows(prepared, this.#maxBatchObjects, this.#maxBatchBytes)) {
|
|
115
|
+
const trees = batch.map((item) => item.lines);
|
|
116
|
+
const oids = typeof this.#persistence.writeTrees === 'function'
|
|
117
|
+
? await this.#persistence.writeTrees(trees)
|
|
118
|
+
: await writeIndividually(trees, (entries) => this.#persistence.writeTree(entries));
|
|
119
|
+
assertCardinality('tree', batch, oids);
|
|
120
|
+
for (let index = 0; index < batch.length; index += 1) {
|
|
121
|
+
batch[index].record.oid = oids[index];
|
|
122
|
+
this.#staging.record(oids[index], 'tree');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function leafNodes(members, limits) {
|
|
129
|
+
const groups = members.length === 0 ? [[]] : chunks(members, limits.maxFanoutEntries - 1);
|
|
130
|
+
return groups.map((group) => node('leaf', 1, group));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function branchNodes(children, limits) {
|
|
134
|
+
const depth = children[0].descriptor.depth + 1;
|
|
135
|
+
if (depth > limits.maxFanoutDepth) {
|
|
136
|
+
throw createCasError('Bundle fanout exceeds its configured depth', ErrorCodes.BUNDLE_FANOUT_LIMIT, {
|
|
137
|
+
attemptedDepth: depth,
|
|
138
|
+
maxFanoutDepth: limits.maxFanoutDepth,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return chunks(children, limits.maxFanoutEntries - 1).map((group) =>
|
|
142
|
+
node('branch', depth, group)
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function node(kind, depth, targets) {
|
|
147
|
+
const entries = kind === 'leaf' ? leafEntries(targets) : branchEntries(targets);
|
|
148
|
+
const descriptor = {
|
|
149
|
+
version: BUNDLE_DESCRIPTOR_VERSION,
|
|
150
|
+
kind,
|
|
151
|
+
depth,
|
|
152
|
+
count: kind === 'leaf'
|
|
153
|
+
? entries.length
|
|
154
|
+
: targets.reduce((total, child) => total + child.descriptor.count, 0),
|
|
155
|
+
firstPath: rangePath(entries, kind, false),
|
|
156
|
+
lastPath: rangePath(entries, kind, true),
|
|
157
|
+
entries,
|
|
158
|
+
};
|
|
159
|
+
return { root: false, descriptor, targets };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function rootNode({ index, memberCount, limits, codec }) {
|
|
163
|
+
return {
|
|
164
|
+
root: true,
|
|
165
|
+
descriptor: {
|
|
166
|
+
version: BUNDLE_DESCRIPTOR_VERSION,
|
|
167
|
+
kind: 'bundle',
|
|
168
|
+
layout: BUNDLE_LAYOUT,
|
|
169
|
+
codec: codec.extension,
|
|
170
|
+
memberCount,
|
|
171
|
+
index: {
|
|
172
|
+
entry: BUNDLE_INDEX_ENTRY,
|
|
173
|
+
depth: index.descriptor.depth,
|
|
174
|
+
firstPath: index.descriptor.firstPath,
|
|
175
|
+
lastPath: index.descriptor.lastPath,
|
|
176
|
+
},
|
|
177
|
+
limits: limits.toJSON(),
|
|
178
|
+
},
|
|
179
|
+
targets: [index],
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function leafEntries(members) {
|
|
184
|
+
return members.map((member, index) => ({
|
|
185
|
+
path: member.path,
|
|
186
|
+
slot: slotFor(index),
|
|
187
|
+
handle: member.handle.toString(),
|
|
188
|
+
type: member.type,
|
|
189
|
+
size: member.size,
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function branchEntries(children) {
|
|
194
|
+
return children.map((child, index) => ({
|
|
195
|
+
firstPath: child.descriptor.firstPath,
|
|
196
|
+
lastPath: child.descriptor.lastPath,
|
|
197
|
+
slot: slotFor(index),
|
|
198
|
+
count: child.descriptor.count,
|
|
199
|
+
depth: child.descriptor.depth,
|
|
200
|
+
}));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function rangePath(entries, kind, last) {
|
|
204
|
+
const entry = last ? entries.at(-1) : entries[0];
|
|
205
|
+
if (entry === undefined) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
if (kind === 'leaf') {
|
|
209
|
+
return entry.path;
|
|
210
|
+
}
|
|
211
|
+
return last ? entry.lastPath : entry.firstPath;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function treeLines(record, codec, oid = undefined) {
|
|
215
|
+
const descriptorName = record.root ? codec.rootEntryName : codec.nodeEntryName;
|
|
216
|
+
const lines = [blobEntry(record.descriptorOid ?? oid, descriptorName)];
|
|
217
|
+
if (record.root) {
|
|
218
|
+
lines.push(objectEntry(record.targets[0].oid ?? oid, BUNDLE_INDEX_ENTRY, 'tree'));
|
|
219
|
+
} else {
|
|
220
|
+
lines.push(...record.targets.map((target, index) => {
|
|
221
|
+
const targetOid = target.oid ?? oid;
|
|
222
|
+
const type = record.descriptor.kind === 'leaf' ? target.type : 'tree';
|
|
223
|
+
return objectEntry(targetOid, slotFor(index), type);
|
|
224
|
+
}));
|
|
225
|
+
}
|
|
226
|
+
lines.sort((left, right) => compareText(entryName(left), entryName(right)));
|
|
227
|
+
return lines;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function plannedBytes(plan, codec) {
|
|
231
|
+
const descriptorBytes = plan.descriptors.reduce((total, record) => total + record.bytes.length, 0);
|
|
232
|
+
const treeBytes = plan.descriptors.reduce(
|
|
233
|
+
(total, record) => total + linesWeight(treeLines(record, codec, CONSERVATIVE_OID)),
|
|
234
|
+
0,
|
|
235
|
+
);
|
|
236
|
+
return descriptorBytes + treeBytes;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function treeWindows(records, configuredObjects, configuredBytes) {
|
|
240
|
+
return windows(records, treeWeight, {
|
|
241
|
+
maxObjects: Math.min(configuredObjects, MAX_PROTOCOL_OBJECTS),
|
|
242
|
+
maxBytes: Math.min(configuredBytes, MAX_PROTOCOL_BYTES),
|
|
243
|
+
maxEntries: MAX_PROTOCOL_TREE_ENTRIES,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function windows(records, weightOf, limits) {
|
|
248
|
+
const batches = [];
|
|
249
|
+
let batch = [];
|
|
250
|
+
let bytes = 0;
|
|
251
|
+
let entries = 0;
|
|
252
|
+
for (const record of records) {
|
|
253
|
+
const weight = weightOf(record);
|
|
254
|
+
const full = batch.length > 0 && (
|
|
255
|
+
batch.length + 1 > limits.maxObjects ||
|
|
256
|
+
bytes + weight.bytes > limits.maxBytes ||
|
|
257
|
+
entries + weight.entries > (limits.maxEntries ?? Number.POSITIVE_INFINITY)
|
|
258
|
+
);
|
|
259
|
+
if (full) {
|
|
260
|
+
batches.push(batch);
|
|
261
|
+
batch = [];
|
|
262
|
+
bytes = 0;
|
|
263
|
+
entries = 0;
|
|
264
|
+
}
|
|
265
|
+
batch.push(record);
|
|
266
|
+
bytes += weight.bytes;
|
|
267
|
+
entries += weight.entries;
|
|
268
|
+
}
|
|
269
|
+
if (batch.length > 0) {
|
|
270
|
+
batches.push(batch);
|
|
271
|
+
}
|
|
272
|
+
return batches;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function descriptorWeight(record) {
|
|
276
|
+
return { bytes: record.bytes.length, entries: 0 };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function treeWeight(item) {
|
|
280
|
+
return { bytes: linesWeight(item.lines), entries: item.lines.length };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function linesWeight(lines) {
|
|
284
|
+
return lines.reduce((total, line) => total + utf8ByteLength(line) + 1, 0);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function objectCount(memberCount, limits) {
|
|
288
|
+
let nodes = memberCount === 0 ? 1 : Math.ceil(memberCount / (limits.maxFanoutEntries - 1));
|
|
289
|
+
let level = nodes;
|
|
290
|
+
while (level > 1) {
|
|
291
|
+
level = Math.ceil(level / (limits.maxFanoutEntries - 1));
|
|
292
|
+
nodes += level;
|
|
293
|
+
}
|
|
294
|
+
return (nodes + 1) * 2;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function chunks(values, size) {
|
|
298
|
+
const result = [];
|
|
299
|
+
for (let index = 0; index < values.length; index += size) {
|
|
300
|
+
result.push(values.slice(index, index + size));
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function assertCardinality(kind, input, output) {
|
|
306
|
+
if (output.length !== input.length) {
|
|
307
|
+
throw createCasError(
|
|
308
|
+
`Persistence returned the wrong number of ${kind} object identifiers`,
|
|
309
|
+
ErrorCodes.GIT_ERROR,
|
|
310
|
+
{ expected: input.length, actual: output.length },
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async function writeIndividually(values, write) {
|
|
316
|
+
const oids = [];
|
|
317
|
+
for (const value of values) {
|
|
318
|
+
oids.push(await write(value));
|
|
319
|
+
}
|
|
320
|
+
return oids;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function batchLimit(kind, observed, maximum) {
|
|
324
|
+
return createCasError(
|
|
325
|
+
`Bundle batch exceeds its configured ${kind} limit`,
|
|
326
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
327
|
+
{ kind, observed, maximum },
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function slotFor(index) {
|
|
332
|
+
return `slot-${String(index).padStart(SLOT_WIDTH, '0')}`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function blobEntry(oid, name) {
|
|
336
|
+
return `${BLOB_MODE} blob ${oid}\t${name}`;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function objectEntry(oid, name, type) {
|
|
340
|
+
return `${type === 'tree' ? TREE_MODE : BLOB_MODE} ${type} ${oid}\t${name}`;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function entryName(value) {
|
|
344
|
+
return value.slice(value.indexOf('\t') + 1);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function compareText(left, right) {
|
|
348
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
349
|
+
}
|
|
@@ -9,13 +9,19 @@ import normalizeBundlePath from '../value-objects/BundlePath.js';
|
|
|
9
9
|
import StagedBundle from '../value-objects/StagedBundle.js';
|
|
10
10
|
import BoundedPromiseCache from '../../helpers/boundedPromiseCache.js';
|
|
11
11
|
import BundleDescriptorCodec, { BUNDLE_INDEX_ENTRY } from './BundleDescriptorCodec.js';
|
|
12
|
+
import BundleBatchPlanner from './BundleBatchPlanner.js';
|
|
12
13
|
import BundleFanoutBuilder from './BundleFanoutBuilder.js';
|
|
13
14
|
import StagingEvidence from './StagingEvidence.js';
|
|
15
|
+
import { recordStagedTarget } from './StagedTarget.js';
|
|
14
16
|
|
|
15
17
|
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
16
18
|
const DEFAULT_MAX_NESTING_DEPTH = 32;
|
|
17
19
|
const DESCRIPTOR_CACHE_ENTRIES = 1_024;
|
|
18
20
|
const DESCRIPTOR_CACHE_BYTES = 16 * 1024 * 1024;
|
|
21
|
+
export const DEFAULT_BUNDLE_WRITE_BATCH_BUNDLES = 64;
|
|
22
|
+
export const DEFAULT_BUNDLE_WRITE_BATCH_MEMBERS = 8_192;
|
|
23
|
+
export const DEFAULT_BUNDLE_WRITE_BATCH_OBJECTS = 256;
|
|
24
|
+
export const DEFAULT_BUNDLE_WRITE_BATCH_BYTES = 64 * 1024 * 1024;
|
|
19
25
|
|
|
20
26
|
/**
|
|
21
27
|
* Builds and traverses deterministic, targeted structured bundle trees.
|
|
@@ -92,6 +98,32 @@ export default class BundleService {
|
|
|
92
98
|
return await this.#putOrdered(members, limits);
|
|
93
99
|
}
|
|
94
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Builds an explicitly bounded group of ordered bundles through shared
|
|
103
|
+
* descriptor and tree dependency waves.
|
|
104
|
+
*/
|
|
105
|
+
async putOrderedBatch(options = {}) {
|
|
106
|
+
const batch = BundleService.#batchOptions(options);
|
|
107
|
+
const staging = new StagingEvidence();
|
|
108
|
+
try {
|
|
109
|
+
const admitted = await this.#admitOrderedBatch(batch.bundles, batch.maxBatchMembers);
|
|
110
|
+
if (admitted.length === 0) {
|
|
111
|
+
return Object.freeze([]);
|
|
112
|
+
}
|
|
113
|
+
return await withWriteScope(
|
|
114
|
+
this.#persistence,
|
|
115
|
+
async (persistence) => await this.#writeAdmittedBatch({
|
|
116
|
+
admitted,
|
|
117
|
+
batch,
|
|
118
|
+
persistence,
|
|
119
|
+
staging,
|
|
120
|
+
}),
|
|
121
|
+
);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
throw augmentError(error, { staging: staging.snapshot() });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
95
127
|
/** @internal Builds from handles whose complete support graphs were already validated. */
|
|
96
128
|
async putOrderedReferences({ members, limits }) {
|
|
97
129
|
if (!isIterable(members)) {
|
|
@@ -327,19 +359,129 @@ export default class BundleService {
|
|
|
327
359
|
previousPath = path;
|
|
328
360
|
}
|
|
329
361
|
const result = await builder.finish();
|
|
330
|
-
return new StagedBundle({
|
|
362
|
+
return recordStagedTarget(new StagedBundle({
|
|
331
363
|
handle: new BundleHandle({ codec: this.#codec.extension, oid: result.oid }),
|
|
332
364
|
memberCount: result.memberCount,
|
|
333
365
|
indexDepth: result.indexDepth,
|
|
334
366
|
descriptorBytes: result.descriptorBytes,
|
|
335
367
|
limits,
|
|
336
368
|
observedAt,
|
|
337
|
-
});
|
|
369
|
+
}));
|
|
338
370
|
} catch (error) {
|
|
339
371
|
throw augmentError(error, { staging: staging.snapshot() });
|
|
340
372
|
}
|
|
341
373
|
}
|
|
342
374
|
|
|
375
|
+
async #admitOrderedBatch(requests, maxBatchMembers) {
|
|
376
|
+
const admitted = [];
|
|
377
|
+
let aggregateMembers = 0;
|
|
378
|
+
for (const request of requests) {
|
|
379
|
+
if (!request || typeof request !== 'object' || !isIterable(request.members)) {
|
|
380
|
+
throw createCasError(
|
|
381
|
+
'Ordered bundle batch entries must provide iterable members',
|
|
382
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
const limits = this.#limits.lower(request.limits);
|
|
386
|
+
const members = [];
|
|
387
|
+
let previousPath = null;
|
|
388
|
+
for await (const pair of request.members) {
|
|
389
|
+
const [rawPath, value] = BundleService.#memberPair(pair);
|
|
390
|
+
const path = normalizeBundlePath(rawPath, limits.maxMemberPathBytes);
|
|
391
|
+
BundleService.#assertOrder(path, previousPath);
|
|
392
|
+
aggregateMembers += 1;
|
|
393
|
+
BundleService.#assertMemberCounts(members.length + 1, aggregateMembers, {
|
|
394
|
+
maxMembers: limits.maxMembers,
|
|
395
|
+
maxBatchMembers,
|
|
396
|
+
});
|
|
397
|
+
members.push({ path, value, inline: inlinePageOptions(value) });
|
|
398
|
+
previousPath = path;
|
|
399
|
+
}
|
|
400
|
+
admitted.push({ members, limits, observedAt: this.#observedAt() });
|
|
401
|
+
}
|
|
402
|
+
return admitted;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async #writeAdmittedBatch({ admitted, batch, persistence, staging }) {
|
|
406
|
+
const inlinePages = admitted.flatMap((request) =>
|
|
407
|
+
request.members.filter((member) => member.inline).map((member) => member.inline)
|
|
408
|
+
);
|
|
409
|
+
const plannedObjects = BundleBatchPlanner.estimateObjectCount(admitted);
|
|
410
|
+
BundleService.#assertBatchObjects(plannedObjects + inlinePages.length, batch.maxBatchObjects);
|
|
411
|
+
const pages = await this.#writeInlinePageBatch(inlinePages, {
|
|
412
|
+
batch,
|
|
413
|
+
persistence,
|
|
414
|
+
staging,
|
|
415
|
+
});
|
|
416
|
+
const prepared = await this.#prepareAdmittedBatch(admitted, pages);
|
|
417
|
+
const pageBytes = pages.reduce((total, page) => total + page.page.size, 0);
|
|
418
|
+
const planner = new BundleBatchPlanner({
|
|
419
|
+
persistence,
|
|
420
|
+
codec: this.#codec,
|
|
421
|
+
staging,
|
|
422
|
+
maxBatchObjects: batch.maxBatchObjects - pages.length,
|
|
423
|
+
maxBatchBytes: batch.maxBatchBytes - pageBytes,
|
|
424
|
+
});
|
|
425
|
+
const plans = prepared.map((request) => planner.create(request));
|
|
426
|
+
const results = await planner.write(plans);
|
|
427
|
+
return Object.freeze(results.map((result) => this.#stagedBatchBundle(result)));
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
async #writeInlinePageBatch(inlinePages, { batch, staging, persistence }) {
|
|
431
|
+
if (inlinePages.length === 0) {
|
|
432
|
+
return [];
|
|
433
|
+
}
|
|
434
|
+
if (typeof this.#pages.putBatch !== 'function') {
|
|
435
|
+
throw createCasError('Bundle batching requires page batch support', ErrorCodes.INVALID_OPTIONS);
|
|
436
|
+
}
|
|
437
|
+
const options = {
|
|
438
|
+
pages: inlinePages,
|
|
439
|
+
maxBatchBytes: batch.maxBatchBytes,
|
|
440
|
+
maxBatchPages: batch.maxBatchObjects,
|
|
441
|
+
};
|
|
442
|
+
const pages = typeof this.#pages.putBatchWithPersistence === 'function'
|
|
443
|
+
? await this.#pages.putBatchWithPersistence(options, persistence)
|
|
444
|
+
: await this.#pages.putBatch(options);
|
|
445
|
+
for (const page of pages) {
|
|
446
|
+
staging.record(page.handle.oid, 'blob');
|
|
447
|
+
staging.recordHandle(page.handle);
|
|
448
|
+
}
|
|
449
|
+
return pages;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
async #prepareAdmittedBatch(admitted, pages) {
|
|
453
|
+
const validation = { active: new Set(), cache: new Map() };
|
|
454
|
+
const prepared = [];
|
|
455
|
+
let pageIndex = 0;
|
|
456
|
+
for (const request of admitted) {
|
|
457
|
+
const members = [];
|
|
458
|
+
for (const member of request.members) {
|
|
459
|
+
if (member.inline) {
|
|
460
|
+
members.push(preparedPageMember(member.path, pages[pageIndex]));
|
|
461
|
+
pageIndex += 1;
|
|
462
|
+
} else {
|
|
463
|
+
members.push(await this.#prepareHandleMember(member.path, member.value, {
|
|
464
|
+
validation,
|
|
465
|
+
validateTargets: true,
|
|
466
|
+
}));
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
prepared.push({ ...request, members });
|
|
470
|
+
}
|
|
471
|
+
return prepared;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
#stagedBatchBundle(result) {
|
|
475
|
+
return recordStagedTarget(new StagedBundle({
|
|
476
|
+
handle: new BundleHandle({ codec: this.#codec.extension, oid: result.oid }),
|
|
477
|
+
memberCount: result.memberCount,
|
|
478
|
+
indexDepth: result.indexDepth,
|
|
479
|
+
descriptorBytes: result.descriptorBytes,
|
|
480
|
+
limits: result.limits,
|
|
481
|
+
observedAt: result.observedAt,
|
|
482
|
+
}));
|
|
483
|
+
}
|
|
484
|
+
|
|
343
485
|
async #prepareMember(path, value, { staging, validation, validateTargets }) {
|
|
344
486
|
const inline = inlinePageOptions(value);
|
|
345
487
|
if (inline) {
|
|
@@ -693,6 +835,54 @@ export default class BundleService {
|
|
|
693
835
|
);
|
|
694
836
|
}
|
|
695
837
|
|
|
838
|
+
static #batchOptions(options) {
|
|
839
|
+
if (!options || typeof options !== 'object' || !Array.isArray(options.bundles)) {
|
|
840
|
+
throw createCasError('Bundle batch must provide a bundle array', ErrorCodes.INVALID_OPTIONS);
|
|
841
|
+
}
|
|
842
|
+
const batch = {
|
|
843
|
+
bundles: options.bundles,
|
|
844
|
+
maxBatchBundles: options.maxBatchBundles ?? DEFAULT_BUNDLE_WRITE_BATCH_BUNDLES,
|
|
845
|
+
maxBatchMembers: options.maxBatchMembers ?? DEFAULT_BUNDLE_WRITE_BATCH_MEMBERS,
|
|
846
|
+
maxBatchObjects: options.maxBatchObjects ?? DEFAULT_BUNDLE_WRITE_BATCH_OBJECTS,
|
|
847
|
+
maxBatchBytes: options.maxBatchBytes ?? DEFAULT_BUNDLE_WRITE_BATCH_BYTES,
|
|
848
|
+
};
|
|
849
|
+
assertBatchLimit(batch.maxBatchBundles, 'bundle count', DEFAULT_BUNDLE_WRITE_BATCH_OBJECTS);
|
|
850
|
+
assertBatchLimit(batch.maxBatchMembers, 'member count', 100_000);
|
|
851
|
+
assertBatchLimit(batch.maxBatchObjects, 'object count', DEFAULT_BUNDLE_WRITE_BATCH_OBJECTS);
|
|
852
|
+
assertBatchLimit(batch.maxBatchBytes, 'byte count', DEFAULT_BUNDLE_WRITE_BATCH_BYTES);
|
|
853
|
+
if (batch.bundles.length > batch.maxBatchBundles) {
|
|
854
|
+
throw createCasError('Bundle batch exceeds its configured bundle limit', ErrorCodes.INVALID_OPTIONS, {
|
|
855
|
+
observedBundles: batch.bundles.length,
|
|
856
|
+
maxBatchBundles: batch.maxBatchBundles,
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
return batch;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
static #assertBatchObjects(observedObjects, maxBatchObjects) {
|
|
863
|
+
if (observedObjects > maxBatchObjects) {
|
|
864
|
+
throw createCasError('Bundle batch exceeds its configured object limit', ErrorCodes.INVALID_OPTIONS, {
|
|
865
|
+
observedObjects,
|
|
866
|
+
maxBatchObjects,
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
static #assertMemberCounts(bundleMembers, batchMembers, limits) {
|
|
872
|
+
if (bundleMembers > limits.maxMembers) {
|
|
873
|
+
throw createCasError('Bundle exceeds its member limit', ErrorCodes.BUNDLE_MEMBER_LIMIT, {
|
|
874
|
+
observedMembers: bundleMembers,
|
|
875
|
+
maxMembers: limits.maxMembers,
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
if (batchMembers > limits.maxBatchMembers) {
|
|
879
|
+
throw createCasError('Bundle batch exceeds its member limit', ErrorCodes.BUNDLE_MEMBER_LIMIT, {
|
|
880
|
+
observedMembers: batchMembers,
|
|
881
|
+
maxBatchMembers: limits.maxBatchMembers,
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
696
886
|
static #assertDependencies({ persistence, codec, pages, resolveHandle, openHandle, clock }) {
|
|
697
887
|
BundleService.#assertPersistence(persistence);
|
|
698
888
|
BundleService.#assertCodecDependency(codec);
|
|
@@ -746,6 +936,33 @@ function inlinePageOptions(value) {
|
|
|
746
936
|
return null;
|
|
747
937
|
}
|
|
748
938
|
|
|
939
|
+
function preparedPageMember(path, staged) {
|
|
940
|
+
if (!staged?.handle) {
|
|
941
|
+
throw createCasError(
|
|
942
|
+
'Page batch did not return a staged handle for every inline member',
|
|
943
|
+
ErrorCodes.GIT_ERROR,
|
|
944
|
+
{ path },
|
|
945
|
+
);
|
|
946
|
+
}
|
|
947
|
+
return Object.freeze({
|
|
948
|
+
path,
|
|
949
|
+
handle: staged.handle,
|
|
950
|
+
oid: staged.handle.oid,
|
|
951
|
+
type: 'blob',
|
|
952
|
+
size: staged.page.size,
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
function assertBatchLimit(value, label, maximum) {
|
|
957
|
+
if (!Number.isSafeInteger(value) || value < 1 || value > maximum) {
|
|
958
|
+
throw createCasError(
|
|
959
|
+
`Bundle batch ${label} must be a positive safe integer within its supported maximum`,
|
|
960
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
961
|
+
{ label, value, maximum },
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
|
|
749
966
|
function bundleReference(value) {
|
|
750
967
|
if (value && typeof value === 'object' &&
|
|
751
968
|
Object.hasOwn(value, 'handle') && Object.hasOwn(value, 'size')) {
|
|
@@ -854,6 +1071,12 @@ function compareText(left, right) {
|
|
|
854
1071
|
return left < right ? -1 : left > right ? 1 : 0;
|
|
855
1072
|
}
|
|
856
1073
|
|
|
1074
|
+
async function withWriteScope(persistence, operation) {
|
|
1075
|
+
return typeof persistence.withWriteScope === 'function'
|
|
1076
|
+
? await persistence.withWriteScope(operation)
|
|
1077
|
+
: await operation(persistence);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
857
1080
|
function augmentError(error, meta) {
|
|
858
1081
|
if (error && typeof error === 'object') {
|
|
859
1082
|
error.meta = { ...error.meta, ...meta };
|
|
@@ -65,7 +65,9 @@ export interface CodecPort {
|
|
|
65
65
|
/** Port interface for persisting data to Git's object database. */
|
|
66
66
|
export interface GitPersistencePort {
|
|
67
67
|
writeBlob(content: Uint8Array): Promise<string>;
|
|
68
|
+
writeBlobs?(contents: Iterable<Uint8Array>): Promise<string[]>;
|
|
68
69
|
writeTree(entries: string[]): Promise<string>;
|
|
70
|
+
writeTrees?(trees: Iterable<string[]>): Promise<string[]>;
|
|
69
71
|
readBlob(oid: string, maxBytes?: number): Promise<Uint8Array>;
|
|
70
72
|
readBlobStream(oid: string): Promise<AsyncIterable<Uint8Array>>;
|
|
71
73
|
readTree(
|
|
@@ -80,6 +82,12 @@ export interface GitPersistencePort {
|
|
|
80
82
|
): AsyncIterable<{ mode: string; type: string; oid: string; name: string }>;
|
|
81
83
|
readObjectType(oid: string): Promise<string>;
|
|
82
84
|
readObjectSize(oid: string): Promise<number>;
|
|
85
|
+
readObjectInfos?(
|
|
86
|
+
oids: Iterable<string>
|
|
87
|
+
): Promise<Array<{ oid: string; type: string; size: number }>>;
|
|
88
|
+
withWriteScope?<T>(
|
|
89
|
+
operation: (persistence: GitPersistencePort) => Promise<T>
|
|
90
|
+
): Promise<T>;
|
|
83
91
|
setMaxBlobSize?(maxBlobSize: number): void;
|
|
84
92
|
}
|
|
85
93
|
|
|
@@ -173,7 +181,11 @@ export interface FileRestorePlan {
|
|
|
173
181
|
export default class CasService {
|
|
174
182
|
readonly persistence: GitPersistencePort;
|
|
175
183
|
readonly codec: CodecPort;
|
|
184
|
+
readonly compressionAdapter: CompressionPort;
|
|
176
185
|
readonly crypto: CryptoPort;
|
|
186
|
+
readonly chunker: ChunkingPort;
|
|
187
|
+
readonly formatVersion?: string;
|
|
188
|
+
readonly legacyMode: boolean;
|
|
177
189
|
readonly observability: ObservabilityPort;
|
|
178
190
|
readonly chunkSize: number;
|
|
179
191
|
readonly merkleThreshold: number;
|
|
@@ -210,6 +222,10 @@ export default class CasService {
|
|
|
210
222
|
|
|
211
223
|
createTree(options: { manifest: Manifest; merkleThreshold?: number }): Promise<string>;
|
|
212
224
|
|
|
225
|
+
createTrees(
|
|
226
|
+
requests: Array<{ manifest: Manifest; merkleThreshold?: number }>,
|
|
227
|
+
): Promise<string[]>;
|
|
228
|
+
|
|
213
229
|
restore(options: {
|
|
214
230
|
manifest: Manifest;
|
|
215
231
|
encryptionKey?: Uint8Array;
|