@git-stunts/git-cas 6.0.0 → 6.1.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/ADVANCED_GUIDE.md +111 -90
- package/ARCHITECTURE.md +12 -3
- package/CHANGELOG.md +78 -3
- package/GUIDE.md +242 -188
- package/README.md +67 -212
- package/UPGRADING.md +28 -1
- package/bin/ui/dashboard.js +442 -183
- package/bin/ui/store-wizard.js +90 -19
- package/build-info.json +2 -2
- package/docs/API.md +273 -51
- package/docs/WALKTHROUGH.md +46 -0
- package/docs/releases/v6.1.0.md +75 -0
- package/index.d.ts +185 -39
- package/index.js +95 -15
- package/package.json +6 -5
- package/src/domain/errors/Codes.js +13 -0
- package/src/domain/services/CasService.d.ts +42 -26
- package/src/domain/services/CasService.js +53 -32
- package/src/domain/services/ChunkRepository.js +50 -12
- package/src/domain/services/RootSet.js +277 -0
- package/src/domain/services/RootSetMetadataCodec.js +253 -0
- package/src/domain/services/RootSetPersistence.js +334 -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/value-objects/RootSetRef.js +88 -0
- package/src/infrastructure/adapters/FileIOHelper.js +33 -31
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +29 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +20 -0
- package/src/infrastructure/chunkers/resolveChunker.js +7 -5
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +9 -0
- package/src/ports/GitRefPort.js +9 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import { errorDetailsText, isGitMissingRefError } from '../helpers/gitRefErrors.js';
|
|
4
|
+
import RootSetRef from '../value-objects/RootSetRef.js';
|
|
5
|
+
import RootSetMetadataCodec, { ROOT_SET_METADATA_ENTRY } from './RootSetMetadataCodec.js';
|
|
6
|
+
import RootSetTreeCodec from './RootSetTreeCodec.js';
|
|
7
|
+
|
|
8
|
+
const UPDATE_REF_CONFLICT_MARKERS = Object.freeze({
|
|
9
|
+
butExpected: 'but expected',
|
|
10
|
+
cannotLockRef: 'cannot lock ref',
|
|
11
|
+
referenceAlreadyExists: 'reference already exists',
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Stateless persistence boundary for one root-set ref and snapshot format.
|
|
16
|
+
*/
|
|
17
|
+
export default class RootSetPersistence {
|
|
18
|
+
/**
|
|
19
|
+
* @param {object} options
|
|
20
|
+
* @param {string} options.rootSetRef
|
|
21
|
+
* @param {import('../../ports/GitPersistencePort.js').default} options.persistence
|
|
22
|
+
* @param {import('../../ports/GitRefPort.js').default} options.ref
|
|
23
|
+
* @param {RootSetMetadataCodec} [options.metadataCodec]
|
|
24
|
+
* @param {RootSetTreeCodec} [options.treeCodec]
|
|
25
|
+
*/
|
|
26
|
+
constructor({
|
|
27
|
+
rootSetRef,
|
|
28
|
+
persistence,
|
|
29
|
+
ref,
|
|
30
|
+
metadataCodec = new RootSetMetadataCodec(),
|
|
31
|
+
treeCodec = new RootSetTreeCodec(),
|
|
32
|
+
}) {
|
|
33
|
+
this.rootSetRef = RootSetRef.from(rootSetRef).toString();
|
|
34
|
+
RootSetPersistence.#validateDependencies(persistence, ref);
|
|
35
|
+
this.persistence = persistence;
|
|
36
|
+
this.ref = ref;
|
|
37
|
+
this.metadataCodec = metadataCodec;
|
|
38
|
+
this.treeCodec = treeCodec;
|
|
39
|
+
Object.freeze(this);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @returns {Promise<{ commitOid: string, treeOid: string }|null>}
|
|
44
|
+
*/
|
|
45
|
+
async resolveHead() {
|
|
46
|
+
const commitOid = await this.resolveRefOnly();
|
|
47
|
+
if (commitOid === null) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
const parentOids = await this.ref.resolveParents(commitOid);
|
|
52
|
+
if (parentOids.length !== 0) {
|
|
53
|
+
throw new CasError(
|
|
54
|
+
'Root-set head must be a parentless current-generation commit',
|
|
55
|
+
ErrorCodes.ROOT_SET_HEAD_INVALID,
|
|
56
|
+
{ ref: this.rootSetRef, commitOid, parentOids },
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return { commitOid, treeOid: await this.ref.resolveTree(commitOid) };
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (err?.code === ErrorCodes.ROOT_SET_HEAD_INVALID) {
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
throw this.#headError('Root-set head commit does not resolve to a tree', err, { commitOid });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Resolves the ref without requiring its commit or tree to be healthy.
|
|
70
|
+
* Used by authoritative repair.
|
|
71
|
+
*
|
|
72
|
+
* @returns {Promise<string|null>}
|
|
73
|
+
*/
|
|
74
|
+
async resolveRefOnly() {
|
|
75
|
+
try {
|
|
76
|
+
return await this.ref.resolveRef(this.rootSetRef);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (this.#isMissingRefError(err)) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
throw this.#headError('Root-set ref could not be resolved', err);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @returns {Promise<{ ref: string, headOid: string|null, treeOid: string|null, entries: Array<object> }>}
|
|
87
|
+
*/
|
|
88
|
+
async read() {
|
|
89
|
+
const head = await this.resolveHead();
|
|
90
|
+
if (!head) {
|
|
91
|
+
return { ref: this.rootSetRef, headOid: null, treeOid: null, entries: [] };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let rawEntries;
|
|
95
|
+
try {
|
|
96
|
+
rawEntries = await this.persistence.readTree(head.treeOid);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
throw new CasError(
|
|
99
|
+
'Root-set tree could not be read',
|
|
100
|
+
ErrorCodes.ROOT_SET_TREE_INVALID,
|
|
101
|
+
{ ref: this.rootSetRef, ...head, originalError: err },
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
const metadataEntry = rawEntries.find((entry) => entry.name === ROOT_SET_METADATA_ENTRY);
|
|
105
|
+
if (!metadataEntry) {
|
|
106
|
+
throw new CasError(
|
|
107
|
+
'Root-set tree is missing .rootset.json',
|
|
108
|
+
ErrorCodes.ROOT_SET_TREE_INVALID,
|
|
109
|
+
{ ref: this.rootSetRef, ...head },
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const metadata = this.metadataCodec.decode(
|
|
114
|
+
await this.persistence.readBlob(metadataEntry.oid),
|
|
115
|
+
{ expectedRef: this.rootSetRef },
|
|
116
|
+
);
|
|
117
|
+
this.treeCodec.validate(rawEntries, metadata);
|
|
118
|
+
return {
|
|
119
|
+
ref: this.rootSetRef,
|
|
120
|
+
headOid: head.commitOid,
|
|
121
|
+
treeOid: head.treeOid,
|
|
122
|
+
entries: metadata.entries.map(RootSetPersistence.#publicEntry),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {object} options
|
|
128
|
+
* @param {Iterable<object>} options.entries
|
|
129
|
+
* @param {string|null} options.expectedHeadOid
|
|
130
|
+
* @param {string} [options.message]
|
|
131
|
+
* @returns {Promise<{ commitOid: string, treeOid: string, entries: Array<object> }>}
|
|
132
|
+
*/
|
|
133
|
+
async write({ entries, expectedHeadOid, message = 'root-set: replace current roots' }) {
|
|
134
|
+
const metadata = this.metadataCodec.create({ ref: this.rootSetRef, entries });
|
|
135
|
+
await this.#assertTargets(metadata.entries.map(RootSetPersistence.#publicEntry));
|
|
136
|
+
const metadataBlobOid = await this.persistence.writeBlob(
|
|
137
|
+
this.metadataCodec.encode({ ref: this.rootSetRef, entries }),
|
|
138
|
+
);
|
|
139
|
+
const treeOid = await this.persistence.writeTree(
|
|
140
|
+
this.treeCodec.toTreeLines(metadata, metadataBlobOid),
|
|
141
|
+
);
|
|
142
|
+
const commitOid = await this.ref.createCommit({
|
|
143
|
+
treeOid,
|
|
144
|
+
parentOid: null,
|
|
145
|
+
message,
|
|
146
|
+
});
|
|
147
|
+
await this.#updateRef({ commitOid, expectedHeadOid });
|
|
148
|
+
return {
|
|
149
|
+
commitOid,
|
|
150
|
+
treeOid,
|
|
151
|
+
entries: metadata.entries.map(RootSetPersistence.#publicEntry),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @param {Iterable<object>} entries
|
|
157
|
+
* @returns {Promise<{ healthy: boolean, targets: Array<object>, issues: Array<object> }>}
|
|
158
|
+
*/
|
|
159
|
+
async inspectTargets(entries) {
|
|
160
|
+
const targets = [];
|
|
161
|
+
const issues = [];
|
|
162
|
+
for (const entry of entries) {
|
|
163
|
+
try {
|
|
164
|
+
const actualType = await this.persistence.readObjectType(entry.oid);
|
|
165
|
+
const target = {
|
|
166
|
+
...entry,
|
|
167
|
+
exists: true,
|
|
168
|
+
actualType,
|
|
169
|
+
reachability: 'anchored',
|
|
170
|
+
};
|
|
171
|
+
targets.push(target);
|
|
172
|
+
if (actualType !== entry.type) {
|
|
173
|
+
issues.push({
|
|
174
|
+
code: ErrorCodes.ROOT_SET_TARGET_TYPE_MISMATCH,
|
|
175
|
+
name: entry.name,
|
|
176
|
+
oid: entry.oid,
|
|
177
|
+
expectedType: entry.type,
|
|
178
|
+
actualType,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
} catch (err) {
|
|
182
|
+
const missing = err?.code === ErrorCodes.GIT_OBJECT_NOT_FOUND;
|
|
183
|
+
const issueCode = missing
|
|
184
|
+
? ErrorCodes.ROOT_SET_TARGET_MISSING
|
|
185
|
+
: ErrorCodes.ROOT_SET_TARGET_UNREADABLE;
|
|
186
|
+
targets.push({
|
|
187
|
+
...entry,
|
|
188
|
+
exists: missing ? false : null,
|
|
189
|
+
actualType: null,
|
|
190
|
+
reachability: missing ? 'missing' : 'unknown',
|
|
191
|
+
});
|
|
192
|
+
issues.push({
|
|
193
|
+
code: issueCode,
|
|
194
|
+
name: entry.name,
|
|
195
|
+
oid: entry.oid,
|
|
196
|
+
expectedType: entry.type,
|
|
197
|
+
originalError: err,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { healthy: issues.length === 0, targets, issues };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @param {{ commitOid: string, expectedHeadOid: string|null }} options
|
|
206
|
+
*/
|
|
207
|
+
async #updateRef({ commitOid, expectedHeadOid }) {
|
|
208
|
+
try {
|
|
209
|
+
await this.ref.updateRef({
|
|
210
|
+
ref: this.rootSetRef,
|
|
211
|
+
newOid: commitOid,
|
|
212
|
+
expectedOldOid: expectedHeadOid,
|
|
213
|
+
});
|
|
214
|
+
} catch (err) {
|
|
215
|
+
const meta = await this.#updateFailureMeta(err, commitOid, expectedHeadOid);
|
|
216
|
+
if (this.#isConflict(err)) {
|
|
217
|
+
throw new CasError(
|
|
218
|
+
'Concurrent root-set update detected',
|
|
219
|
+
ErrorCodes.ROOT_SET_CONFLICT,
|
|
220
|
+
meta,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
throw new CasError('Root-set ref update failed', ErrorCodes.ROOT_SET_REF_UPDATE_FAILED, meta);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async #updateFailureMeta(originalError, newCommit, expectedHeadOid) {
|
|
228
|
+
const errorMeta = originalError?.meta && typeof originalError.meta === 'object'
|
|
229
|
+
? originalError.meta
|
|
230
|
+
: {};
|
|
231
|
+
let actualHeadOid = Object.hasOwn(errorMeta, 'actualOldOid')
|
|
232
|
+
? errorMeta.actualOldOid
|
|
233
|
+
: undefined;
|
|
234
|
+
if (actualHeadOid === undefined) {
|
|
235
|
+
try {
|
|
236
|
+
actualHeadOid = await this.ref.resolveRef(this.rootSetRef);
|
|
237
|
+
} catch {
|
|
238
|
+
actualHeadOid = null;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
ref: this.rootSetRef,
|
|
243
|
+
expectedHeadOid,
|
|
244
|
+
actualHeadOid,
|
|
245
|
+
newCommit,
|
|
246
|
+
originalError,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
#isConflict(err) {
|
|
251
|
+
const meta = err?.meta && typeof err.meta === 'object' ? err.meta : {};
|
|
252
|
+
if (Object.hasOwn(meta, 'expectedOldOid') && Object.hasOwn(meta, 'actualOldOid')) {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
const normalized = errorDetailsText(err).toLowerCase();
|
|
256
|
+
if (!normalized.includes(this.rootSetRef.toLowerCase())) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
return normalized.includes(UPDATE_REF_CONFLICT_MARKERS.cannotLockRef) && (
|
|
260
|
+
normalized.includes(UPDATE_REF_CONFLICT_MARKERS.butExpected) ||
|
|
261
|
+
normalized.includes(UPDATE_REF_CONFLICT_MARKERS.referenceAlreadyExists)
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
#isMissingRefError(err) {
|
|
266
|
+
if (err?.code === ErrorCodes.GIT_REF_NOT_FOUND) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
return isGitMissingRefError(err, this.rootSetRef);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
#headError(message, originalError, meta = {}) {
|
|
273
|
+
return new CasError(message, ErrorCodes.ROOT_SET_HEAD_INVALID, {
|
|
274
|
+
ref: this.rootSetRef,
|
|
275
|
+
...meta,
|
|
276
|
+
originalError,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
static #publicEntry(entry) {
|
|
281
|
+
return {
|
|
282
|
+
name: entry.name,
|
|
283
|
+
oid: entry.oid,
|
|
284
|
+
type: entry.type,
|
|
285
|
+
retention: entry.retention,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
static #validateDependencies(persistence, ref) {
|
|
290
|
+
const persistenceMethods = [
|
|
291
|
+
'writeBlob',
|
|
292
|
+
'writeTree',
|
|
293
|
+
'readBlob',
|
|
294
|
+
'readTree',
|
|
295
|
+
'readObjectType',
|
|
296
|
+
];
|
|
297
|
+
const refMethods = [
|
|
298
|
+
'resolveRef',
|
|
299
|
+
'resolveTree',
|
|
300
|
+
'resolveParents',
|
|
301
|
+
'createCommit',
|
|
302
|
+
'updateRef',
|
|
303
|
+
];
|
|
304
|
+
const missing = [
|
|
305
|
+
...persistenceMethods.filter((method) => typeof persistence?.[method] !== 'function'),
|
|
306
|
+
...refMethods.filter((method) => typeof ref?.[method] !== 'function'),
|
|
307
|
+
];
|
|
308
|
+
if (missing.length > 0) {
|
|
309
|
+
throw new CasError(
|
|
310
|
+
'RootSetPersistence requires complete Git persistence and ref ports',
|
|
311
|
+
ErrorCodes.ROOT_SET_DEPENDENCY_INVALID,
|
|
312
|
+
{ missing },
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async #assertTargets(entries) {
|
|
318
|
+
const report = await this.inspectTargets(entries);
|
|
319
|
+
const issue = report.issues[0];
|
|
320
|
+
if (!issue) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const messages = {
|
|
324
|
+
[ErrorCodes.ROOT_SET_TARGET_MISSING]: `Root-set target does not exist: ${issue.oid}`,
|
|
325
|
+
[ErrorCodes.ROOT_SET_TARGET_UNREADABLE]: `Root-set target could not be inspected: ${issue.oid}`,
|
|
326
|
+
[ErrorCodes.ROOT_SET_TARGET_TYPE_MISMATCH]: `Root-set target type mismatch for ${issue.oid}`,
|
|
327
|
+
};
|
|
328
|
+
const message = messages[issue.code];
|
|
329
|
+
throw new CasError(message, issue.code, {
|
|
330
|
+
ref: this.rootSetRef,
|
|
331
|
+
...issue,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import RootSet from './RootSet.js';
|
|
4
|
+
import RootSetPersistence from './RootSetPersistence.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Opens ref-backed RootSet instances over shared Git adapters.
|
|
8
|
+
*/
|
|
9
|
+
export default class RootSetRegistry {
|
|
10
|
+
/**
|
|
11
|
+
* @param {object} options
|
|
12
|
+
* @param {import('../../ports/GitPersistencePort.js').default} options.persistence
|
|
13
|
+
* @param {import('../../ports/GitRefPort.js').default} options.ref
|
|
14
|
+
*/
|
|
15
|
+
constructor({ persistence, ref }) {
|
|
16
|
+
if (!persistence || !ref) {
|
|
17
|
+
throw new CasError(
|
|
18
|
+
'RootSetRegistry requires Git persistence and ref adapters',
|
|
19
|
+
ErrorCodes.ROOT_SET_DEPENDENCY_INVALID,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
this.persistence = persistence;
|
|
23
|
+
this.ref = ref;
|
|
24
|
+
Object.freeze(this);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {object} options
|
|
29
|
+
* @param {string} options.ref
|
|
30
|
+
* @param {object} [options.retry]
|
|
31
|
+
* @returns {RootSet}
|
|
32
|
+
*/
|
|
33
|
+
open({ ref, retry } = {}) {
|
|
34
|
+
return new RootSet({
|
|
35
|
+
ref,
|
|
36
|
+
retry,
|
|
37
|
+
persistence: new RootSetPersistence({
|
|
38
|
+
rootSetRef: ref,
|
|
39
|
+
persistence: this.persistence,
|
|
40
|
+
ref: this.ref,
|
|
41
|
+
}),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
|
|
4
|
+
export const DEFAULT_ROOT_SET_RETRY_MAX_ATTEMPTS = 3;
|
|
5
|
+
export const DEFAULT_ROOT_SET_RETRY_BASE_DELAY_MS = 25;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Retry policy for optimistic root-set mutations.
|
|
9
|
+
*/
|
|
10
|
+
export default class RootSetRetryPolicy {
|
|
11
|
+
#maxAttempts;
|
|
12
|
+
#baseDelayMs;
|
|
13
|
+
#random;
|
|
14
|
+
#sleep;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {object} [options]
|
|
18
|
+
* @param {number} [options.maxAttempts]
|
|
19
|
+
* @param {number} [options.baseDelayMs]
|
|
20
|
+
* @param {() => number} [options.random]
|
|
21
|
+
* @param {(delayMs: number) => Promise<void>} [options.sleep]
|
|
22
|
+
*/
|
|
23
|
+
constructor({
|
|
24
|
+
maxAttempts = DEFAULT_ROOT_SET_RETRY_MAX_ATTEMPTS,
|
|
25
|
+
baseDelayMs = DEFAULT_ROOT_SET_RETRY_BASE_DELAY_MS,
|
|
26
|
+
random = Math.random,
|
|
27
|
+
sleep = (delayMs) => new Promise((resolve) => setTimeout(resolve, delayMs)),
|
|
28
|
+
} = {}) {
|
|
29
|
+
RootSetRetryPolicy.#assertPositiveInteger('maxAttempts', maxAttempts);
|
|
30
|
+
RootSetRetryPolicy.#assertNonNegativeNumber('baseDelayMs', baseDelayMs);
|
|
31
|
+
RootSetRetryPolicy.#assertFunction('random', random);
|
|
32
|
+
RootSetRetryPolicy.#assertFunction('sleep', sleep);
|
|
33
|
+
this.#maxAttempts = maxAttempts;
|
|
34
|
+
this.#baseDelayMs = baseDelayMs;
|
|
35
|
+
this.#random = random;
|
|
36
|
+
this.#sleep = sleep;
|
|
37
|
+
Object.freeze(this);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get maxAttempts() {
|
|
41
|
+
return this.#maxAttempts;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {unknown} err
|
|
46
|
+
* @returns {boolean}
|
|
47
|
+
*/
|
|
48
|
+
isRetryable(err) {
|
|
49
|
+
return err instanceof CasError && err.code === ErrorCodes.ROOT_SET_CONFLICT;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {number} attempt
|
|
54
|
+
* @returns {Promise<void>}
|
|
55
|
+
*/
|
|
56
|
+
async waitBeforeRetry(attempt) {
|
|
57
|
+
const exponentialDelay = this.#baseDelayMs * (2 ** attempt);
|
|
58
|
+
const jitter = Math.floor(this.#random() * (exponentialDelay / 2));
|
|
59
|
+
await this.#sleep(exponentialDelay + jitter);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static #assertPositiveInteger(label, value) {
|
|
63
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
64
|
+
throw RootSetRetryPolicy.#invalid(label, value, 'a positive integer');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static #assertNonNegativeNumber(label, value) {
|
|
69
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
70
|
+
throw RootSetRetryPolicy.#invalid(label, value, 'a non-negative number');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static #assertFunction(label, value) {
|
|
75
|
+
if (typeof value !== 'function') {
|
|
76
|
+
throw RootSetRetryPolicy.#invalid(label, value, 'a function');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static #invalid(label, value, expectation) {
|
|
81
|
+
return new CasError(
|
|
82
|
+
`Root-set retry ${label} must be ${expectation}`,
|
|
83
|
+
ErrorCodes.ROOT_SET_RETRY_POLICY_INVALID,
|
|
84
|
+
{ [label]: value },
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import { ROOT_SET_METADATA_ENTRY } from './RootSetMetadataCodec.js';
|
|
4
|
+
|
|
5
|
+
const GIT_TREE_MODE = '040000';
|
|
6
|
+
const GIT_BLOB_MODE = '100644';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Encodes and validates the Git tree that provides root-set reachability.
|
|
10
|
+
*/
|
|
11
|
+
export default class RootSetTreeCodec {
|
|
12
|
+
/**
|
|
13
|
+
* @param {object} metadata
|
|
14
|
+
* @param {string} metadataBlobOid
|
|
15
|
+
* @returns {string[]}
|
|
16
|
+
*/
|
|
17
|
+
toTreeLines(metadata, metadataBlobOid) {
|
|
18
|
+
const lines = [`${GIT_BLOB_MODE} blob ${metadataBlobOid}\t${ROOT_SET_METADATA_ENTRY}`];
|
|
19
|
+
for (const entry of metadata.entries) {
|
|
20
|
+
const mode = entry.type === 'tree' ? GIT_TREE_MODE : GIT_BLOB_MODE;
|
|
21
|
+
lines.push(`${mode} ${entry.type} ${entry.oid}\t${entry.slot}`);
|
|
22
|
+
}
|
|
23
|
+
return lines;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {Array<object>} rawEntries
|
|
28
|
+
* @param {object} metadata
|
|
29
|
+
*/
|
|
30
|
+
validate(rawEntries, metadata) {
|
|
31
|
+
if (!Array.isArray(rawEntries)) {
|
|
32
|
+
throw this.#treeError('Root-set tree entries must be an array', { rawEntries });
|
|
33
|
+
}
|
|
34
|
+
const { actual, metadataCount } = this.#indexTreeEntries(rawEntries);
|
|
35
|
+
this.#assertEntryCounts(actual, metadataCount, metadata);
|
|
36
|
+
this.#assertReachabilityEdges(actual, metadata.entries);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
#indexTreeEntries(rawEntries) {
|
|
40
|
+
const actual = new Map();
|
|
41
|
+
let metadataCount = 0;
|
|
42
|
+
for (const entry of rawEntries) {
|
|
43
|
+
if (entry.name === ROOT_SET_METADATA_ENTRY) {
|
|
44
|
+
metadataCount++;
|
|
45
|
+
if (entry.type !== 'blob' || entry.mode !== GIT_BLOB_MODE) {
|
|
46
|
+
throw this.#treeError('Root-set metadata entry must be a regular blob', { entry });
|
|
47
|
+
}
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (actual.has(entry.name)) {
|
|
51
|
+
throw this.#treeError('Root-set tree contains duplicate entry slots', {
|
|
52
|
+
slot: entry.name,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
actual.set(entry.name, entry);
|
|
56
|
+
}
|
|
57
|
+
return { actual, metadataCount };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
#assertEntryCounts(actual, metadataCount, metadata) {
|
|
61
|
+
if (metadataCount !== 1) {
|
|
62
|
+
throw this.#treeError('Root-set tree must contain exactly one metadata blob', {
|
|
63
|
+
metadataCount,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (actual.size !== metadata.entries.length) {
|
|
67
|
+
throw this.#treeError('Root-set metadata and tree entry counts differ', {
|
|
68
|
+
metadataEntryCount: metadata.entries.length,
|
|
69
|
+
treeEntryCount: actual.size,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#assertReachabilityEdges(actual, expectedEntries) {
|
|
75
|
+
for (const expected of expectedEntries) {
|
|
76
|
+
const entry = actual.get(expected.slot);
|
|
77
|
+
const mode = expected.type === 'tree' ? GIT_TREE_MODE : GIT_BLOB_MODE;
|
|
78
|
+
if (!entry || entry.oid !== expected.oid || entry.type !== expected.type || entry.mode !== mode) {
|
|
79
|
+
throw this.#treeError('Root-set metadata does not match its Git reachability edge', {
|
|
80
|
+
expected,
|
|
81
|
+
actual: entry ?? null,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param {string} message
|
|
89
|
+
* @param {object} meta
|
|
90
|
+
* @returns {CasError}
|
|
91
|
+
*/
|
|
92
|
+
#treeError(message, meta) {
|
|
93
|
+
return new CasError(message, ErrorCodes.ROOT_SET_TREE_INVALID, meta);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
|
|
4
|
+
export const ROOT_SET_REF_PREFIX = 'refs/cas/rootsets/';
|
|
5
|
+
const FORBIDDEN_REF_CHARS = '~^:?*[\\';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Immutable, validated ref in the git-cas root-set namespace.
|
|
9
|
+
*/
|
|
10
|
+
export default class RootSetRef {
|
|
11
|
+
#value;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} value
|
|
15
|
+
*/
|
|
16
|
+
constructor(value) {
|
|
17
|
+
if (!RootSetRef.isValid(value)) {
|
|
18
|
+
throw new CasError(
|
|
19
|
+
`Root-set ref must be a valid ref below ${ROOT_SET_REF_PREFIX}`,
|
|
20
|
+
ErrorCodes.ROOT_SET_REF_INVALID,
|
|
21
|
+
{ ref: value, prefix: ROOT_SET_REF_PREFIX },
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
this.#value = value;
|
|
25
|
+
Object.freeze(this);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {string|RootSetRef} value
|
|
30
|
+
* @returns {RootSetRef}
|
|
31
|
+
*/
|
|
32
|
+
static from(value) {
|
|
33
|
+
return value instanceof RootSetRef ? value : new RootSetRef(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {unknown} value
|
|
38
|
+
* @returns {boolean}
|
|
39
|
+
*/
|
|
40
|
+
static isValid(value) {
|
|
41
|
+
if (typeof value !== 'string' || !value.startsWith(ROOT_SET_REF_PREFIX)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const suffix = value.slice(ROOT_SET_REF_PREFIX.length);
|
|
45
|
+
if (RootSetRef.#hasInvalidSuffixShape(suffix) || RootSetRef.#hasForbiddenChar(suffix)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return suffix.split('/').every(RootSetRef.#isValidComponent);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static #hasInvalidSuffixShape(suffix) {
|
|
52
|
+
return [
|
|
53
|
+
suffix.length === 0,
|
|
54
|
+
suffix.startsWith('/'),
|
|
55
|
+
suffix.endsWith('/'),
|
|
56
|
+
suffix.endsWith('.'),
|
|
57
|
+
suffix.includes('//'),
|
|
58
|
+
suffix.includes('..'),
|
|
59
|
+
suffix.includes('@{'),
|
|
60
|
+
].some(Boolean);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static #hasForbiddenChar(value) {
|
|
64
|
+
for (const character of value) {
|
|
65
|
+
const codePoint = character.codePointAt(0);
|
|
66
|
+
if (codePoint <= 0x20 || codePoint === 0x7f || FORBIDDEN_REF_CHARS.includes(character)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static #isValidComponent(component) {
|
|
74
|
+
return [
|
|
75
|
+
component.length > 0,
|
|
76
|
+
component !== '.',
|
|
77
|
+
component !== '..',
|
|
78
|
+
!component.endsWith('.lock'),
|
|
79
|
+
].every(Boolean);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @returns {string}
|
|
84
|
+
*/
|
|
85
|
+
toString() {
|
|
86
|
+
return this.#value;
|
|
87
|
+
}
|
|
88
|
+
}
|