@git-stunts/git-cas 6.0.1 → 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/ARCHITECTURE.md +12 -3
- package/CHANGELOG.md +20 -1
- package/README.md +10 -1
- package/UPGRADING.md +28 -1
- package/build-info.json +2 -2
- package/docs/API.md +199 -7
- package/docs/WALKTHROUGH.md +46 -0
- package/docs/releases/v6.1.0.md +75 -0
- package/index.d.ts +96 -0
- package/index.js +28 -0
- package/package.json +6 -5
- package/src/domain/errors/Codes.js +13 -0
- package/src/domain/services/CasService.d.ts +1 -0
- 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/GitPersistenceAdapter.js +29 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +20 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +9 -0
- package/src/ports/GitRefPort.js +9 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import { utf8Decode, utf8Encode } from '../encoding/utf8.js';
|
|
4
|
+
import Oid from '../value-objects/Oid.js';
|
|
5
|
+
import RootSetRef from '../value-objects/RootSetRef.js';
|
|
6
|
+
|
|
7
|
+
export const ROOT_SET_METADATA_VERSION = 1;
|
|
8
|
+
export const ROOT_SET_METADATA_ENTRY = '.rootset.json';
|
|
9
|
+
export const ROOT_SET_ENTRY_TYPES = Object.freeze(['blob', 'tree']);
|
|
10
|
+
export const ROOT_SET_RETENTION_POLICIES = Object.freeze(['pinned', 'evictable']);
|
|
11
|
+
const SLOT_PATTERN = /^root-[0-9]{8}$/;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Canonical JSON codec for `.rootset.json` snapshots.
|
|
15
|
+
*/
|
|
16
|
+
export default class RootSetMetadataCodec {
|
|
17
|
+
/**
|
|
18
|
+
* @param {{ ref: string, entries: Iterable<object> }} options
|
|
19
|
+
* @returns {Uint8Array}
|
|
20
|
+
*/
|
|
21
|
+
encode({ ref, entries }) {
|
|
22
|
+
return utf8Encode(JSON.stringify(this.create({ ref, entries }), null, 2));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {{ ref: string, entries: Iterable<object> }} options
|
|
27
|
+
* @returns {{ version: 1, ref: string, entries: Array<object> }}
|
|
28
|
+
*/
|
|
29
|
+
create({ ref, entries }) {
|
|
30
|
+
const rootSetRef = RootSetRef.from(ref).toString();
|
|
31
|
+
return {
|
|
32
|
+
version: ROOT_SET_METADATA_VERSION,
|
|
33
|
+
ref: rootSetRef,
|
|
34
|
+
entries: this.normalizeEntries(entries).map((entry, index) => ({
|
|
35
|
+
slot: RootSetMetadataCodec.slotFor(index),
|
|
36
|
+
...entry,
|
|
37
|
+
})),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {Uint8Array} bytes
|
|
43
|
+
* @param {{ expectedRef?: string }} [options]
|
|
44
|
+
* @returns {{ version: 1, ref: string, entries: Array<object> }}
|
|
45
|
+
*/
|
|
46
|
+
decode(bytes, { expectedRef } = {}) {
|
|
47
|
+
let metadata;
|
|
48
|
+
try {
|
|
49
|
+
metadata = JSON.parse(utf8Decode(bytes));
|
|
50
|
+
} catch (err) {
|
|
51
|
+
throw this.#metadataError('Failed to parse .rootset.json', { originalError: err });
|
|
52
|
+
}
|
|
53
|
+
return this.validate(metadata, { expectedRef });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {unknown} metadata
|
|
58
|
+
* @param {{ expectedRef?: string }} [options]
|
|
59
|
+
* @returns {{ version: 1, ref: string, entries: Array<object> }}
|
|
60
|
+
*/
|
|
61
|
+
validate(metadata, { expectedRef } = {}) {
|
|
62
|
+
this.#assertMetadataEnvelope(metadata);
|
|
63
|
+
const canonical = this.#canonicalizeStoredMetadata(metadata);
|
|
64
|
+
this.#assertExpectedRef(canonical.ref, expectedRef);
|
|
65
|
+
this.#assertCanonicalStorage(metadata, canonical);
|
|
66
|
+
return canonical;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {Iterable<object>} entries
|
|
71
|
+
* @returns {Array<{ name: string, oid: string, type: 'blob'|'tree', retention: 'pinned'|'evictable' }>}
|
|
72
|
+
*/
|
|
73
|
+
normalizeEntries(entries) {
|
|
74
|
+
if (!entries || typeof entries[Symbol.iterator] !== 'function') {
|
|
75
|
+
throw this.#entryError('Root-set entries must be iterable', { entries });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const normalized = [...entries].map((entry) => this.#normalizeEntry(entry));
|
|
79
|
+
normalized.sort(RootSetMetadataCodec.#compareNames);
|
|
80
|
+
for (let index = 1; index < normalized.length; index++) {
|
|
81
|
+
if (normalized[index - 1].name === normalized[index].name) {
|
|
82
|
+
throw this.#entryError('Root-set entry names must be unique', {
|
|
83
|
+
name: normalized[index].name,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return normalized;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {number} index
|
|
92
|
+
* @returns {string}
|
|
93
|
+
*/
|
|
94
|
+
static slotFor(index) {
|
|
95
|
+
if (!Number.isSafeInteger(index) || index < 0 || index > 99_999_999) {
|
|
96
|
+
throw new CasError(
|
|
97
|
+
'Root-set entry count exceeds the supported slot range',
|
|
98
|
+
ErrorCodes.ROOT_SET_ENTRY_INVALID,
|
|
99
|
+
{ index },
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return `root-${String(index).padStart(8, '0')}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {object} entry
|
|
107
|
+
* @returns {{ name: string, oid: string, type: 'blob'|'tree', retention: 'pinned'|'evictable' }}
|
|
108
|
+
*/
|
|
109
|
+
#normalizeEntry(entry) {
|
|
110
|
+
this.#assertEntryObject(entry);
|
|
111
|
+
this.#assertEntryName(entry);
|
|
112
|
+
this.#assertEntryType(entry);
|
|
113
|
+
const retention = entry.retention ?? 'pinned';
|
|
114
|
+
if (!ROOT_SET_RETENTION_POLICIES.includes(retention)) {
|
|
115
|
+
throw this.#entryError('Root-set retention must be pinned or evictable', { entry });
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
name: entry.name,
|
|
119
|
+
oid: this.#normalizeOid(entry),
|
|
120
|
+
type: entry.type,
|
|
121
|
+
retention,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
#assertMetadataEnvelope(metadata) {
|
|
126
|
+
if (!metadata || typeof metadata !== 'object' || Array.isArray(metadata)) {
|
|
127
|
+
throw this.#metadataError('Root-set metadata must be an object', { metadata });
|
|
128
|
+
}
|
|
129
|
+
if (metadata.version !== ROOT_SET_METADATA_VERSION) {
|
|
130
|
+
throw this.#metadataError(`Unsupported root-set metadata version: ${metadata.version}`, {
|
|
131
|
+
metadata,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
#canonicalizeStoredMetadata(metadata) {
|
|
137
|
+
try {
|
|
138
|
+
return this.create({ ref: metadata.ref, entries: metadata.entries });
|
|
139
|
+
} catch (err) {
|
|
140
|
+
if (err instanceof CasError && err.code === ErrorCodes.ROOT_SET_ENTRY_INVALID) {
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
throw this.#metadataError('Root-set metadata fields are invalid', {
|
|
144
|
+
metadata,
|
|
145
|
+
originalError: err,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
#assertExpectedRef(actualRef, expectedRef) {
|
|
151
|
+
if (expectedRef === undefined || actualRef === RootSetRef.from(expectedRef).toString()) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
throw this.#metadataError('Root-set metadata ref does not match the requested ref', {
|
|
155
|
+
expectedRef,
|
|
156
|
+
actualRef,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
#assertCanonicalStorage(metadata, canonical) {
|
|
161
|
+
if (!RootSetMetadataCodec.#isCanonicalStoredMetadata(metadata, canonical)) {
|
|
162
|
+
throw this.#metadataError('Root-set metadata is not canonical', { metadata });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
#assertEntryObject(entry) {
|
|
167
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
168
|
+
throw this.#entryError('Root-set entry must be an object', { entry });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#assertEntryName(entry) {
|
|
173
|
+
if (
|
|
174
|
+
typeof entry.name !== 'string' ||
|
|
175
|
+
entry.name.length === 0 ||
|
|
176
|
+
RootSetMetadataCodec.#hasControlCharacters(entry.name)
|
|
177
|
+
) {
|
|
178
|
+
throw this.#entryError('Root-set entry name must be a non-empty string without control characters', {
|
|
179
|
+
entry,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
#assertEntryType(entry) {
|
|
185
|
+
if (!ROOT_SET_ENTRY_TYPES.includes(entry.type)) {
|
|
186
|
+
throw this.#entryError('Root-set entry type must be blob or tree', { entry });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
#normalizeOid(entry) {
|
|
191
|
+
try {
|
|
192
|
+
return Oid.from(entry.oid).toString();
|
|
193
|
+
} catch (err) {
|
|
194
|
+
throw this.#entryError('Root-set entry OID is invalid', { entry, originalError: err });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static #hasControlCharacters(value) {
|
|
199
|
+
for (const character of value) {
|
|
200
|
+
const codePoint = character.codePointAt(0);
|
|
201
|
+
if (codePoint < 0x20 || codePoint === 0x7f) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static #compareNames(left, right) {
|
|
209
|
+
if (left.name < right.name) {
|
|
210
|
+
return -1;
|
|
211
|
+
}
|
|
212
|
+
return left.name > right.name ? 1 : 0;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @param {unknown} stored
|
|
217
|
+
* @param {object} canonical
|
|
218
|
+
* @returns {boolean}
|
|
219
|
+
*/
|
|
220
|
+
static #isCanonicalStoredMetadata(stored, canonical) {
|
|
221
|
+
if (Object.keys(stored).sort().join(',') !== 'entries,ref,version') {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
if (!Array.isArray(stored.entries) || stored.entries.length !== canonical.entries.length) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
return stored.entries.every((entry, index) => (
|
|
228
|
+
entry &&
|
|
229
|
+
typeof entry === 'object' &&
|
|
230
|
+
Object.keys(entry).sort().join(',') === 'name,oid,retention,slot,type' &&
|
|
231
|
+
SLOT_PATTERN.test(entry.slot) &&
|
|
232
|
+
JSON.stringify(entry) === JSON.stringify(canonical.entries[index])
|
|
233
|
+
));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @param {string} message
|
|
238
|
+
* @param {object} meta
|
|
239
|
+
* @returns {CasError}
|
|
240
|
+
*/
|
|
241
|
+
#metadataError(message, meta) {
|
|
242
|
+
return new CasError(message, ErrorCodes.ROOT_SET_METADATA_INVALID, meta);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @param {string} message
|
|
247
|
+
* @param {object} meta
|
|
248
|
+
* @returns {CasError}
|
|
249
|
+
*/
|
|
250
|
+
#entryError(message, meta) {
|
|
251
|
+
return new CasError(message, ErrorCodes.ROOT_SET_ENTRY_INVALID, meta);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
@@ -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
|
+
}
|