@git-stunts/git-cas 2.0.0 → 3.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/CHANGELOG.md +66 -1
- package/README.md +63 -15
- package/bin/git-cas.js +286 -41
- package/bin/ui/context.js +54 -0
- package/bin/ui/dashboard-cmds.js +34 -0
- package/bin/ui/dashboard-view.js +132 -0
- package/bin/ui/dashboard.js +204 -0
- package/bin/ui/encryption-card.js +49 -0
- package/bin/ui/heatmap.js +103 -0
- package/bin/ui/history-timeline.js +84 -0
- package/bin/ui/manifest-view.js +135 -0
- package/bin/ui/progress.js +126 -0
- package/index.d.ts +133 -2
- package/index.js +76 -9
- package/package.json +10 -1
- package/src/domain/services/VaultService.js +445 -0
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +11 -11
- package/src/infrastructure/adapters/GitRefAdapter.js +67 -0
- package/src/ports/GitRefPort.js +48 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Domain service for vault (GC-safe ref-based asset index) operations.
|
|
3
|
+
*/
|
|
4
|
+
import CasError from '../errors/CasError.js';
|
|
5
|
+
|
|
6
|
+
const VAULT_REF = 'refs/cas/vault';
|
|
7
|
+
const MAX_CAS_RETRIES = 3;
|
|
8
|
+
const CAS_RETRY_BASE_MS = 50;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Percent-encodes a vault slug for use as a git tree entry name.
|
|
12
|
+
* Git tree entry names cannot contain '/'.
|
|
13
|
+
* @param {string} slug
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
function encodeSlug(slug) {
|
|
17
|
+
return slug.replaceAll('%', '%25').replaceAll('/', '%2F');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Decodes a percent-encoded tree entry name back to a vault slug.
|
|
22
|
+
* @param {string} name
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
function decodeSlug(name) {
|
|
26
|
+
return name.replaceAll('%2F', '/').replaceAll('%25', '%');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns true if the string contains ASCII control characters (0x00–0x1f, 0x7f).
|
|
31
|
+
* @param {string} str
|
|
32
|
+
* @returns {boolean}
|
|
33
|
+
*/
|
|
34
|
+
function hasControlChars(str) {
|
|
35
|
+
for (let i = 0; i < str.length; i++) {
|
|
36
|
+
const code = str.charCodeAt(i);
|
|
37
|
+
if (code <= 0x1f || code === 0x7f) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Domain service for vault operations.
|
|
46
|
+
*
|
|
47
|
+
* The vault is a GC-safe ref-based index that maps slugs to Git tree OIDs.
|
|
48
|
+
* It is backed by a single Git ref (`refs/cas/vault`) pointing to a commit
|
|
49
|
+
* chain. Each commit's tree contains one entry per stored asset plus a
|
|
50
|
+
* `.vault.json` metadata blob.
|
|
51
|
+
*
|
|
52
|
+
* Requires three ports:
|
|
53
|
+
* - `persistence` ({@link GitPersistencePort}) for blob/tree read/write
|
|
54
|
+
* - `ref` ({@link GitRefPort}) for ref resolution, commits, and atomic updates
|
|
55
|
+
* - `crypto` ({@link CryptoPort}) for KDF when vault-level encryption is enabled
|
|
56
|
+
*/
|
|
57
|
+
export default class VaultService {
|
|
58
|
+
static VAULT_REF = VAULT_REF;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {Object} options
|
|
62
|
+
* @param {import('../../../src/ports/GitPersistencePort.js').default} options.persistence
|
|
63
|
+
* @param {import('../../../src/ports/GitRefPort.js').default} options.ref
|
|
64
|
+
* @param {import('../../../src/ports/CryptoPort.js').default} options.crypto
|
|
65
|
+
*/
|
|
66
|
+
constructor({ persistence, ref, crypto }) {
|
|
67
|
+
this.persistence = persistence;
|
|
68
|
+
this.ref = ref;
|
|
69
|
+
this.crypto = crypto;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Slug validation
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Validates a single slug segment.
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
static #validateSegment(seg, slug) {
|
|
81
|
+
if (seg.length === 0) {
|
|
82
|
+
throw new CasError('Slug contains empty segment', 'INVALID_SLUG', { slug });
|
|
83
|
+
}
|
|
84
|
+
if (seg === '.' || seg === '..') {
|
|
85
|
+
throw new CasError('Slug contains "." or ".." segment', 'INVALID_SLUG', { slug });
|
|
86
|
+
}
|
|
87
|
+
if (Buffer.byteLength(seg, 'utf8') > 255) {
|
|
88
|
+
throw new CasError('Slug segment exceeds 255 bytes', 'INVALID_SLUG', { slug });
|
|
89
|
+
}
|
|
90
|
+
if (hasControlChars(seg)) {
|
|
91
|
+
throw new CasError('Slug contains control characters', 'INVALID_SLUG', { slug });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Validates a vault slug.
|
|
97
|
+
* @param {string} slug
|
|
98
|
+
* @throws {CasError} INVALID_SLUG if the slug is invalid.
|
|
99
|
+
*/
|
|
100
|
+
validateSlug(slug) {
|
|
101
|
+
if (typeof slug !== 'string' || slug.length === 0) {
|
|
102
|
+
throw new CasError('Slug must be a non-empty string', 'INVALID_SLUG', { slug });
|
|
103
|
+
}
|
|
104
|
+
if (slug.startsWith('/') || slug.endsWith('/')) {
|
|
105
|
+
throw new CasError('Slug must not start or end with "/"', 'INVALID_SLUG', { slug });
|
|
106
|
+
}
|
|
107
|
+
if (Buffer.byteLength(slug, 'utf8') > 1024) {
|
|
108
|
+
throw new CasError('Slug exceeds 1024 bytes total', 'INVALID_SLUG', { slug });
|
|
109
|
+
}
|
|
110
|
+
for (const seg of slug.split('/')) {
|
|
111
|
+
VaultService.#validateSegment(seg, slug);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// Metadata validation
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Validates encryption-specific metadata fields.
|
|
121
|
+
* @private
|
|
122
|
+
*/
|
|
123
|
+
static #validateEncryption(encryption, metadata) {
|
|
124
|
+
const { cipher, kdf } = encryption;
|
|
125
|
+
if (!cipher || !kdf?.algorithm || !kdf?.salt || !kdf?.keyLength) {
|
|
126
|
+
throw new CasError(
|
|
127
|
+
'Vault encryption metadata missing required fields',
|
|
128
|
+
'VAULT_METADATA_INVALID',
|
|
129
|
+
{ metadata },
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Validates vault metadata object structure.
|
|
136
|
+
* @private
|
|
137
|
+
*/
|
|
138
|
+
static #validateMetadata(metadata) {
|
|
139
|
+
if (typeof metadata.version !== 'number' || metadata.version !== 1) {
|
|
140
|
+
throw new CasError(
|
|
141
|
+
`Unsupported vault metadata version: ${metadata.version}`,
|
|
142
|
+
'VAULT_METADATA_INVALID',
|
|
143
|
+
{ metadata },
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
if (metadata.encryption) {
|
|
147
|
+
VaultService.#validateEncryption(metadata.encryption, metadata);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Reads and validates vault metadata from a blob OID.
|
|
153
|
+
* @private
|
|
154
|
+
*/
|
|
155
|
+
async #readMetadataBlob(blobOid) {
|
|
156
|
+
try {
|
|
157
|
+
const blob = await this.persistence.readBlob(blobOid);
|
|
158
|
+
const metadata = JSON.parse(blob.toString());
|
|
159
|
+
VaultService.#validateMetadata(metadata);
|
|
160
|
+
return metadata;
|
|
161
|
+
} catch (err) {
|
|
162
|
+
if (err instanceof CasError) { throw err; }
|
|
163
|
+
throw new CasError(
|
|
164
|
+
`Failed to parse .vault.json: ${err.message}`,
|
|
165
|
+
'VAULT_METADATA_INVALID',
|
|
166
|
+
{ originalError: err },
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ---------------------------------------------------------------------------
|
|
172
|
+
// State read / write
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Separates vault tree entries into slug→OID map and metadata blob OID.
|
|
177
|
+
* @private
|
|
178
|
+
* @param {Array<{ mode: string, type: string, oid: string, name: string }>} treeEntries
|
|
179
|
+
*/
|
|
180
|
+
static #parseTreeEntries(treeEntries) {
|
|
181
|
+
const entries = new Map();
|
|
182
|
+
let metadataBlobOid = null;
|
|
183
|
+
for (const entry of treeEntries) {
|
|
184
|
+
if (entry.name === '.vault.json') {
|
|
185
|
+
metadataBlobOid = entry.oid;
|
|
186
|
+
} else {
|
|
187
|
+
entries.set(decodeSlug(entry.name), entry.oid);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { entries, metadataBlobOid };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Reads the current vault state from refs/cas/vault.
|
|
195
|
+
* @returns {Promise<{ entries: Map<string, string>, parentCommitOid: string|null, metadata: object|null }>}
|
|
196
|
+
*/
|
|
197
|
+
async readState() {
|
|
198
|
+
let commitOid;
|
|
199
|
+
try {
|
|
200
|
+
commitOid = await this.ref.resolveRef(VAULT_REF);
|
|
201
|
+
} catch {
|
|
202
|
+
return { entries: new Map(), parentCommitOid: null, metadata: null };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const treeOid = await this.ref.resolveTree(commitOid);
|
|
206
|
+
const rawEntries = await this.persistence.readTree(treeOid);
|
|
207
|
+
const { entries, metadataBlobOid } = VaultService.#parseTreeEntries(rawEntries);
|
|
208
|
+
const metadata = metadataBlobOid
|
|
209
|
+
? await this.#readMetadataBlob(metadataBlobOid)
|
|
210
|
+
: null;
|
|
211
|
+
|
|
212
|
+
return { entries, parentCommitOid: commitOid, metadata };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Writes a new vault commit and updates the ref atomically.
|
|
217
|
+
* @param {Object} options
|
|
218
|
+
* @param {Map<string, string>} options.entries - Slug→treeOid map.
|
|
219
|
+
* @param {object} options.metadata - Vault metadata (.vault.json contents).
|
|
220
|
+
* @param {string|null} options.parentCommitOid - Parent commit OID (null for first commit).
|
|
221
|
+
* @param {string} options.message - Commit message.
|
|
222
|
+
* @returns {Promise<{ commitOid: string }>}
|
|
223
|
+
*/
|
|
224
|
+
async writeCommit({ entries, metadata, parentCommitOid, message }) {
|
|
225
|
+
const metadataBlob = await this.persistence.writeBlob(
|
|
226
|
+
JSON.stringify(metadata, null, 2),
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
const treeLines = [`100644 blob ${metadataBlob}\t.vault.json`];
|
|
230
|
+
for (const [slug, treeOid] of entries) {
|
|
231
|
+
treeLines.push(`040000 tree ${treeOid}\t${encodeSlug(slug)}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const newTreeOid = await this.persistence.writeTree(treeLines);
|
|
235
|
+
|
|
236
|
+
const commitOid = await this.ref.createCommit({
|
|
237
|
+
treeOid: newTreeOid,
|
|
238
|
+
parentOid: parentCommitOid,
|
|
239
|
+
message,
|
|
240
|
+
});
|
|
241
|
+
await this.#casUpdateRef(commitOid, parentCommitOid);
|
|
242
|
+
return { commitOid };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Atomically updates the vault ref with CAS semantics.
|
|
247
|
+
* @private
|
|
248
|
+
*/
|
|
249
|
+
async #casUpdateRef(newOid, expectedOldOid) {
|
|
250
|
+
try {
|
|
251
|
+
await this.ref.updateRef({
|
|
252
|
+
ref: VAULT_REF,
|
|
253
|
+
newOid,
|
|
254
|
+
expectedOldOid,
|
|
255
|
+
});
|
|
256
|
+
} catch (err) {
|
|
257
|
+
throw new CasError(
|
|
258
|
+
'Concurrent vault update detected',
|
|
259
|
+
'VAULT_CONFLICT',
|
|
260
|
+
{ expectedParent: expectedOldOid, newCommit: newOid, originalError: err },
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Wraps a vault mutation with CAS retry logic.
|
|
267
|
+
* @private
|
|
268
|
+
* @param {function} mutationFn - Async function(state) → { entries, metadata, message }
|
|
269
|
+
* @returns {Promise<{ commitOid: string }>}
|
|
270
|
+
*/
|
|
271
|
+
async #retryMutation(mutationFn) {
|
|
272
|
+
for (let attempt = 0; attempt < MAX_CAS_RETRIES; attempt++) {
|
|
273
|
+
const state = await this.readState();
|
|
274
|
+
const { entries, metadata, message } = await mutationFn(state);
|
|
275
|
+
try {
|
|
276
|
+
return await this.writeCommit({
|
|
277
|
+
entries, metadata, parentCommitOid: state.parentCommitOid, message,
|
|
278
|
+
});
|
|
279
|
+
} catch (err) {
|
|
280
|
+
const isRetryable = err instanceof CasError && err.code === 'VAULT_CONFLICT';
|
|
281
|
+
if (!isRetryable || attempt >= MAX_CAS_RETRIES - 1) {
|
|
282
|
+
throw err;
|
|
283
|
+
}
|
|
284
|
+
const delay = CAS_RETRY_BASE_MS * (2 ** attempt);
|
|
285
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/* c8 ignore next 2 */
|
|
289
|
+
throw new CasError('Vault CAS retries exhausted', 'VAULT_CONFLICT');
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ---------------------------------------------------------------------------
|
|
293
|
+
// Encryption helpers
|
|
294
|
+
// ---------------------------------------------------------------------------
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Builds vault encryption metadata from KDF result.
|
|
298
|
+
* @private
|
|
299
|
+
*/
|
|
300
|
+
static #buildEncryptionMeta(salt, params) {
|
|
301
|
+
return {
|
|
302
|
+
cipher: 'aes-256-gcm',
|
|
303
|
+
kdf: {
|
|
304
|
+
algorithm: params.algorithm,
|
|
305
|
+
salt: salt.toString('base64'),
|
|
306
|
+
...('iterations' in params && { iterations: params.iterations }),
|
|
307
|
+
...('cost' in params && { cost: params.cost }),
|
|
308
|
+
...('blockSize' in params && { blockSize: params.blockSize }),
|
|
309
|
+
...('parallelization' in params && { parallelization: params.parallelization }),
|
|
310
|
+
keyLength: params.keyLength,
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ---------------------------------------------------------------------------
|
|
316
|
+
// Public API
|
|
317
|
+
// ---------------------------------------------------------------------------
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Initializes the vault, optionally with encryption.
|
|
321
|
+
* @param {Object} [options]
|
|
322
|
+
* @param {string} [options.passphrase] - Passphrase for vault-level encryption.
|
|
323
|
+
* @param {Object} [options.kdfOptions] - KDF options (algorithm, iterations, etc.).
|
|
324
|
+
* @returns {Promise<{ commitOid: string }>}
|
|
325
|
+
*/
|
|
326
|
+
async initVault({ passphrase, kdfOptions } = {}) {
|
|
327
|
+
const state = await this.readState();
|
|
328
|
+
|
|
329
|
+
if (state.metadata?.encryption) {
|
|
330
|
+
throw new CasError(
|
|
331
|
+
'Vault encryption is already configured',
|
|
332
|
+
'VAULT_ENCRYPTION_ALREADY_CONFIGURED',
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const metadata = { version: 1 };
|
|
337
|
+
if (passphrase) {
|
|
338
|
+
const { salt, params } = await this.crypto.deriveKey({ passphrase, ...kdfOptions });
|
|
339
|
+
metadata.encryption = VaultService.#buildEncryptionMeta(salt, params);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return await this.writeCommit({
|
|
343
|
+
entries: state.entries,
|
|
344
|
+
metadata,
|
|
345
|
+
parentCommitOid: state.parentCommitOid,
|
|
346
|
+
message: 'vault: init',
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Adds or updates an entry in the vault.
|
|
352
|
+
* @param {Object} options
|
|
353
|
+
* @param {string} options.slug - Entry slug.
|
|
354
|
+
* @param {string} options.treeOid - Git tree OID.
|
|
355
|
+
* @param {boolean} [options.force=false] - Overwrite existing entry.
|
|
356
|
+
* @returns {Promise<{ commitOid: string }>}
|
|
357
|
+
*/
|
|
358
|
+
async addToVault({ slug, treeOid, force = false }) {
|
|
359
|
+
this.validateSlug(slug);
|
|
360
|
+
|
|
361
|
+
return await this.#retryMutation((state) => {
|
|
362
|
+
if (state.entries.has(slug) && !force) {
|
|
363
|
+
throw new CasError(
|
|
364
|
+
`Vault entry "${slug}" already exists (use force to overwrite)`,
|
|
365
|
+
'VAULT_ENTRY_EXISTS',
|
|
366
|
+
{ slug },
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
const isUpdate = state.entries.has(slug);
|
|
370
|
+
state.entries.set(slug, treeOid);
|
|
371
|
+
return {
|
|
372
|
+
entries: state.entries,
|
|
373
|
+
metadata: state.metadata || { version: 1 },
|
|
374
|
+
message: isUpdate ? `vault: update ${slug}` : `vault: add ${slug}`,
|
|
375
|
+
};
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Lists all vault entries.
|
|
381
|
+
* @returns {Promise<Array<{ slug: string, treeOid: string }>>}
|
|
382
|
+
*/
|
|
383
|
+
async listVault() {
|
|
384
|
+
const { entries } = await this.readState();
|
|
385
|
+
return [...entries.entries()]
|
|
386
|
+
.map(([slug, treeOid]) => ({ slug, treeOid }))
|
|
387
|
+
.sort((a, b) => a.slug.localeCompare(b.slug));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Removes an entry from the vault.
|
|
392
|
+
* @param {Object} options
|
|
393
|
+
* @param {string} options.slug - Entry slug to remove.
|
|
394
|
+
* @returns {Promise<{ commitOid: string, removedTreeOid: string }>}
|
|
395
|
+
*/
|
|
396
|
+
async removeFromVault({ slug }) {
|
|
397
|
+
let removedTreeOid;
|
|
398
|
+
|
|
399
|
+
const result = await this.#retryMutation((state) => {
|
|
400
|
+
if (!state.entries.has(slug)) {
|
|
401
|
+
throw new CasError(
|
|
402
|
+
`Vault entry "${slug}" not found`,
|
|
403
|
+
'VAULT_ENTRY_NOT_FOUND',
|
|
404
|
+
{ slug },
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
removedTreeOid = state.entries.get(slug);
|
|
408
|
+
state.entries.delete(slug);
|
|
409
|
+
return {
|
|
410
|
+
entries: state.entries,
|
|
411
|
+
metadata: state.metadata || { version: 1 },
|
|
412
|
+
message: `vault: remove ${slug}`,
|
|
413
|
+
};
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
return { commitOid: result.commitOid, removedTreeOid };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Resolves a vault entry slug to its tree OID.
|
|
421
|
+
* @param {Object} options
|
|
422
|
+
* @param {string} options.slug - Entry slug.
|
|
423
|
+
* @returns {Promise<string>} The tree OID.
|
|
424
|
+
*/
|
|
425
|
+
async resolveVaultEntry({ slug }) {
|
|
426
|
+
const { entries } = await this.readState();
|
|
427
|
+
if (!entries.has(slug)) {
|
|
428
|
+
throw new CasError(
|
|
429
|
+
`Vault entry "${slug}" not found`,
|
|
430
|
+
'VAULT_ENTRY_NOT_FOUND',
|
|
431
|
+
{ slug },
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
return entries.get(slug);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Returns the vault metadata, or null if no vault exists.
|
|
439
|
+
* @returns {Promise<object|null>}
|
|
440
|
+
*/
|
|
441
|
+
async getVaultMetadata() {
|
|
442
|
+
const { metadata } = await this.readState();
|
|
443
|
+
return metadata;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
@@ -2,27 +2,27 @@ import { Policy } from '@git-stunts/alfred';
|
|
|
2
2
|
import GitPersistencePort from '../../ports/GitPersistencePort.js';
|
|
3
3
|
import CasError from '../../domain/errors/CasError.js';
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
);
|
|
5
|
+
/**
|
|
6
|
+
* Default resilience policy: 30 s timeout (no retry).
|
|
7
|
+
*
|
|
8
|
+
* Plumbing already retries lock-contention errors internally via
|
|
9
|
+
* {@link ExecutionOrchestrator}, so an additional alfred retry layer is
|
|
10
|
+
* unnecessary and causes premature process exit: alfred's retry sleep uses
|
|
11
|
+
* an unref'd timer that allows Node to exit before the next attempt starts.
|
|
12
|
+
*/
|
|
13
|
+
const DEFAULT_POLICY = Policy.timeout(30_000);
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* {@link GitPersistencePort} implementation backed by `@git-stunts/plumbing`.
|
|
17
17
|
*
|
|
18
18
|
* All Git I/O is wrapped with a configurable resilience {@link Policy}
|
|
19
|
-
* (
|
|
19
|
+
* (30 s timeout by default).
|
|
20
20
|
*/
|
|
21
21
|
export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
22
22
|
/**
|
|
23
23
|
* @param {Object} options
|
|
24
24
|
* @param {import('@git-stunts/plumbing').default} options.plumbing - GitPlumbing instance.
|
|
25
|
-
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy (defaults to 30 s timeout
|
|
25
|
+
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy (defaults to 30 s timeout, no retry).
|
|
26
26
|
*/
|
|
27
27
|
constructor({ plumbing, policy }) {
|
|
28
28
|
super();
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Policy } from '@git-stunts/alfred';
|
|
2
|
+
import GitRefPort from '../../ports/GitRefPort.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Default resilience policy: 30 s timeout (no retry).
|
|
6
|
+
*
|
|
7
|
+
* Plumbing already retries lock-contention errors internally via
|
|
8
|
+
* {@link ExecutionOrchestrator}, so an additional alfred retry layer is
|
|
9
|
+
* unnecessary and causes premature process exit: alfred's retry sleep uses
|
|
10
|
+
* an unref'd timer that allows Node to exit before the next attempt starts.
|
|
11
|
+
*/
|
|
12
|
+
const DEFAULT_POLICY = Policy.timeout(30_000);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* {@link GitRefPort} implementation backed by `@git-stunts/plumbing`.
|
|
16
|
+
*
|
|
17
|
+
* All Git I/O is wrapped with a configurable resilience {@link Policy}
|
|
18
|
+
* (30 s timeout by default).
|
|
19
|
+
*/
|
|
20
|
+
export default class GitRefAdapter extends GitRefPort {
|
|
21
|
+
/**
|
|
22
|
+
* @param {Object} options
|
|
23
|
+
* @param {import('@git-stunts/plumbing').default} options.plumbing - GitPlumbing instance.
|
|
24
|
+
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy.
|
|
25
|
+
*/
|
|
26
|
+
constructor({ plumbing, policy }) {
|
|
27
|
+
super();
|
|
28
|
+
this.plumbing = plumbing;
|
|
29
|
+
this.policy = policy ?? DEFAULT_POLICY;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @override */
|
|
33
|
+
async resolveRef(ref) {
|
|
34
|
+
return this.policy.execute(() =>
|
|
35
|
+
this.plumbing.execute({ args: ['rev-parse', ref] }),
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @override */
|
|
40
|
+
async resolveTree(commitOid) {
|
|
41
|
+
return this.policy.execute(() =>
|
|
42
|
+
this.plumbing.execute({ args: ['rev-parse', `${commitOid}^{tree}`] }),
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @override */
|
|
47
|
+
async createCommit({ treeOid, parentOid, message }) {
|
|
48
|
+
const args = ['commit-tree', treeOid, '-m', message];
|
|
49
|
+
if (parentOid) {
|
|
50
|
+
args.push('-p', parentOid);
|
|
51
|
+
}
|
|
52
|
+
return this.policy.execute(() =>
|
|
53
|
+
this.plumbing.execute({ args }),
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** @override */
|
|
58
|
+
async updateRef({ ref, newOid, expectedOldOid }) {
|
|
59
|
+
const args = ['update-ref', ref, newOid];
|
|
60
|
+
if (expectedOldOid) {
|
|
61
|
+
args.push(expectedOldOid);
|
|
62
|
+
}
|
|
63
|
+
return this.policy.execute(() =>
|
|
64
|
+
this.plumbing.execute({ args }),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract port for Git ref and commit operations.
|
|
3
|
+
* @abstract
|
|
4
|
+
*/
|
|
5
|
+
export default class GitRefPort {
|
|
6
|
+
/**
|
|
7
|
+
* Resolves a Git ref to its commit OID.
|
|
8
|
+
* @param {string} ref - Git ref (e.g. 'refs/cas/vault').
|
|
9
|
+
* @returns {Promise<string>} The commit OID.
|
|
10
|
+
* @throws If the ref does not exist.
|
|
11
|
+
*/
|
|
12
|
+
async resolveRef(_ref) {
|
|
13
|
+
throw new Error('Not implemented');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Resolves the tree OID from a commit OID.
|
|
18
|
+
* @param {string} commitOid - Git commit OID.
|
|
19
|
+
* @returns {Promise<string>} The tree OID.
|
|
20
|
+
*/
|
|
21
|
+
async resolveTree(_commitOid) {
|
|
22
|
+
throw new Error('Not implemented');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Creates a Git commit object.
|
|
27
|
+
* @param {Object} options
|
|
28
|
+
* @param {string} options.treeOid - Tree OID for the commit.
|
|
29
|
+
* @param {string|null} [options.parentOid] - Parent commit OID (null for root commit).
|
|
30
|
+
* @param {string} options.message - Commit message.
|
|
31
|
+
* @returns {Promise<string>} The new commit OID.
|
|
32
|
+
*/
|
|
33
|
+
async createCommit(_options) {
|
|
34
|
+
throw new Error('Not implemented');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Atomically updates a Git ref with optional CAS (compare-and-swap) semantics.
|
|
39
|
+
* @param {Object} options
|
|
40
|
+
* @param {string} options.ref - Git ref to update.
|
|
41
|
+
* @param {string} options.newOid - New OID to set.
|
|
42
|
+
* @param {string|null} [options.expectedOldOid] - Expected current OID for CAS. If provided and mismatched, throws.
|
|
43
|
+
* @returns {Promise<void>}
|
|
44
|
+
*/
|
|
45
|
+
async updateRef(_options) {
|
|
46
|
+
throw new Error('Not implemented');
|
|
47
|
+
}
|
|
48
|
+
}
|