@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,247 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import { BUNDLE_DESCRIPTOR_VERSION, BUNDLE_INDEX_ENTRY, BUNDLE_LAYOUT } from './BundleDescriptorCodec.js';
|
|
4
|
+
|
|
5
|
+
const SLOT_WIDTH = 6;
|
|
6
|
+
const BLOB_MODE = '100644';
|
|
7
|
+
const TREE_MODE = '040000';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Streaming bulk loader for deterministic bounded bundle fanout trees.
|
|
11
|
+
*/
|
|
12
|
+
export default class BundleFanoutBuilder {
|
|
13
|
+
#codec;
|
|
14
|
+
#descriptorBytes = 0;
|
|
15
|
+
#levels = [[]];
|
|
16
|
+
#limits;
|
|
17
|
+
#memberCount = 0;
|
|
18
|
+
#persistence;
|
|
19
|
+
#staging;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} options
|
|
23
|
+
* @param {import('../../ports/GitPersistencePort.js').default} options.persistence
|
|
24
|
+
* @param {import('./BundleDescriptorCodec.js').default} options.codec
|
|
25
|
+
* @param {import('../value-objects/BundleLimits.js').default} options.limits
|
|
26
|
+
* @param {{ record(oid: string, type: string): void, snapshot(): object }} options.staging
|
|
27
|
+
*/
|
|
28
|
+
constructor({ persistence, codec, limits, staging }) {
|
|
29
|
+
this.#persistence = persistence;
|
|
30
|
+
this.#codec = codec;
|
|
31
|
+
this.#limits = limits;
|
|
32
|
+
this.#staging = staging;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {{ path: string, handle: object, oid: string, type: 'blob'|'tree', size: number|null }} member
|
|
37
|
+
*/
|
|
38
|
+
async add(member) {
|
|
39
|
+
const leaf = this.#levels[0];
|
|
40
|
+
if (leaf.length === this.#capacity) {
|
|
41
|
+
await this.#flushLevel(0);
|
|
42
|
+
}
|
|
43
|
+
this.#levels[0].push(member);
|
|
44
|
+
this.#memberCount += 1;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @returns {Promise<object>} */
|
|
48
|
+
async finish() {
|
|
49
|
+
if (this.#memberCount === 0) {
|
|
50
|
+
this.#levels[0].push(null);
|
|
51
|
+
}
|
|
52
|
+
while (true) {
|
|
53
|
+
const nonEmpty = this.#nonEmptyLevels();
|
|
54
|
+
if (nonEmpty.length === 1 && this.#levels[nonEmpty[0]].length === 1 && nonEmpty[0] > 0) {
|
|
55
|
+
return await this.#writeRoot(this.#levels[nonEmpty[0]][0]);
|
|
56
|
+
}
|
|
57
|
+
await this.#flushLevel(nonEmpty[0]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get #capacity() {
|
|
62
|
+
return this.#limits.maxFanoutEntries - 1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#nonEmptyLevels() {
|
|
66
|
+
const result = [];
|
|
67
|
+
for (let index = 0; index < this.#levels.length; index++) {
|
|
68
|
+
if (this.#levels[index].length > 0) {
|
|
69
|
+
result.push(index);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async #flushLevel(level) {
|
|
76
|
+
const entries = this.#levels[level];
|
|
77
|
+
this.#levels[level] = [];
|
|
78
|
+
const summary = level === 0
|
|
79
|
+
? await this.#writeLeaf(entries[0] === null ? [] : entries)
|
|
80
|
+
: await this.#writeBranch(entries);
|
|
81
|
+
await this.#pushSummary(level + 1, summary);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async #pushSummary(level, summary) {
|
|
85
|
+
this.#levels[level] ??= [];
|
|
86
|
+
if (this.#levels[level].length === this.#capacity) {
|
|
87
|
+
await this.#flushLevel(level);
|
|
88
|
+
}
|
|
89
|
+
this.#levels[level].push(summary);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async #writeLeaf(members) {
|
|
93
|
+
const entries = members.map((member, index) => ({
|
|
94
|
+
path: member.path,
|
|
95
|
+
slot: slotFor(index),
|
|
96
|
+
handle: member.handle.toString(),
|
|
97
|
+
type: member.type,
|
|
98
|
+
size: member.size,
|
|
99
|
+
}));
|
|
100
|
+
return await this.#writeNode({
|
|
101
|
+
kind: 'leaf',
|
|
102
|
+
depth: 1,
|
|
103
|
+
count: entries.length,
|
|
104
|
+
firstPath: entries[0]?.path ?? null,
|
|
105
|
+
lastPath: entries.at(-1)?.path ?? null,
|
|
106
|
+
entries,
|
|
107
|
+
targets: members.map((member, index) => ({
|
|
108
|
+
slot: slotFor(index),
|
|
109
|
+
oid: member.oid,
|
|
110
|
+
type: member.type,
|
|
111
|
+
})),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async #writeBranch(children) {
|
|
116
|
+
const depth = children[0].depth + 1;
|
|
117
|
+
if (depth > this.#limits.maxFanoutDepth) {
|
|
118
|
+
throw this.#error('Bundle fanout exceeds its configured depth', ErrorCodes.BUNDLE_FANOUT_LIMIT, {
|
|
119
|
+
attemptedDepth: depth,
|
|
120
|
+
maxFanoutDepth: this.#limits.maxFanoutDepth,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const entries = children.map((child, index) => ({
|
|
124
|
+
firstPath: child.firstPath,
|
|
125
|
+
lastPath: child.lastPath,
|
|
126
|
+
slot: slotFor(index),
|
|
127
|
+
count: child.count,
|
|
128
|
+
depth: child.depth,
|
|
129
|
+
}));
|
|
130
|
+
return await this.#writeNode({
|
|
131
|
+
kind: 'branch',
|
|
132
|
+
depth,
|
|
133
|
+
count: children.reduce((total, child) => total + child.count, 0),
|
|
134
|
+
firstPath: children[0].firstPath,
|
|
135
|
+
lastPath: children.at(-1).lastPath,
|
|
136
|
+
entries,
|
|
137
|
+
targets: children.map((child, index) => ({
|
|
138
|
+
slot: slotFor(index),
|
|
139
|
+
oid: child.oid,
|
|
140
|
+
type: 'tree',
|
|
141
|
+
})),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async #writeNode({ kind, depth, count, firstPath, lastPath, entries, targets }) {
|
|
146
|
+
const descriptor = {
|
|
147
|
+
version: BUNDLE_DESCRIPTOR_VERSION,
|
|
148
|
+
kind,
|
|
149
|
+
depth,
|
|
150
|
+
count,
|
|
151
|
+
firstPath,
|
|
152
|
+
lastPath,
|
|
153
|
+
entries,
|
|
154
|
+
};
|
|
155
|
+
const descriptorOid = await this.#writeDescriptor(this.#codec.encodeNode(descriptor));
|
|
156
|
+
const treeEntries = [blobEntry(descriptorOid, this.#codec.nodeEntryName)];
|
|
157
|
+
treeEntries.push(...targets.map((target) => objectEntry(target.oid, target.slot, target.type)));
|
|
158
|
+
this.#assertFanout(treeEntries.length);
|
|
159
|
+
const oid = await this.#writeTree(treeEntries);
|
|
160
|
+
return Object.freeze({ oid, depth, count, firstPath, lastPath });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async #writeRoot(index) {
|
|
164
|
+
const descriptor = {
|
|
165
|
+
version: BUNDLE_DESCRIPTOR_VERSION,
|
|
166
|
+
kind: 'bundle',
|
|
167
|
+
layout: BUNDLE_LAYOUT,
|
|
168
|
+
codec: this.#codec.extension,
|
|
169
|
+
memberCount: this.#memberCount,
|
|
170
|
+
index: {
|
|
171
|
+
entry: BUNDLE_INDEX_ENTRY,
|
|
172
|
+
depth: index.depth,
|
|
173
|
+
firstPath: index.firstPath,
|
|
174
|
+
lastPath: index.lastPath,
|
|
175
|
+
},
|
|
176
|
+
limits: this.#limits.toJSON(),
|
|
177
|
+
};
|
|
178
|
+
const descriptorOid = await this.#writeDescriptor(this.#codec.encodeRoot(descriptor));
|
|
179
|
+
const entries = [
|
|
180
|
+
blobEntry(descriptorOid, this.#codec.rootEntryName),
|
|
181
|
+
objectEntry(index.oid, BUNDLE_INDEX_ENTRY, 'tree'),
|
|
182
|
+
];
|
|
183
|
+
this.#assertFanout(entries.length);
|
|
184
|
+
const oid = await this.#writeTree(entries);
|
|
185
|
+
return Object.freeze({
|
|
186
|
+
oid,
|
|
187
|
+
memberCount: this.#memberCount,
|
|
188
|
+
indexDepth: index.depth,
|
|
189
|
+
descriptorBytes: this.#descriptorBytes,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async #writeDescriptor(bytes) {
|
|
194
|
+
const next = this.#descriptorBytes + bytes.length;
|
|
195
|
+
if (next > this.#limits.maxDescriptorBytes) {
|
|
196
|
+
throw this.#error(
|
|
197
|
+
'Bundle descriptors exceed their configured byte limit',
|
|
198
|
+
ErrorCodes.BUNDLE_DESCRIPTOR_LIMIT,
|
|
199
|
+
{ descriptorBytes: next, maxDescriptorBytes: this.#limits.maxDescriptorBytes }
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
this.#descriptorBytes = next;
|
|
203
|
+
const oid = await this.#persistence.writeBlob(bytes);
|
|
204
|
+
this.#staging.record(oid, 'blob');
|
|
205
|
+
return oid;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async #writeTree(entries) {
|
|
209
|
+
entries.sort((left, right) => compareText(entryName(left), entryName(right)));
|
|
210
|
+
const oid = await this.#persistence.writeTree(entries);
|
|
211
|
+
this.#staging.record(oid, 'tree');
|
|
212
|
+
return oid;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#assertFanout(entries) {
|
|
216
|
+
if (entries > this.#limits.maxFanoutEntries) {
|
|
217
|
+
throw this.#error('Bundle tree node exceeds its fanout limit', ErrorCodes.BUNDLE_FANOUT_LIMIT, {
|
|
218
|
+
entries,
|
|
219
|
+
maxFanoutEntries: this.#limits.maxFanoutEntries,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
#error(message, code, meta) {
|
|
225
|
+
return createCasError(message, code, { ...meta, staging: this.#staging.snapshot() });
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function slotFor(index) {
|
|
230
|
+
return `slot-${String(index).padStart(SLOT_WIDTH, '0')}`;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function blobEntry(oid, name) {
|
|
234
|
+
return `${BLOB_MODE} blob ${oid}\t${name}`;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function objectEntry(oid, name, type) {
|
|
238
|
+
return `${type === 'tree' ? TREE_MODE : BLOB_MODE} ${type} ${oid}\t${name}`;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function entryName(value) {
|
|
242
|
+
return value.slice(value.indexOf('\t') + 1);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function compareText(left, right) {
|
|
246
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
247
|
+
}
|