@git-stunts/git-cas 6.1.0 → 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 +108 -3
- package/CHANGELOG.md +80 -2
- package/GUIDE.md +114 -13
- package/README.md +51 -4
- package/UPGRADING.md +43 -0
- package/build-info.json +2 -2
- package/docs/API.md +751 -16
- package/docs/releases/v6.2.0.md +131 -0
- package/index.d.ts +879 -2
- package/index.js +326 -1
- package/package.json +3 -2
- package/src/domain/errors/Codes.js +35 -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 +2 -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 +10 -6
- package/src/domain/services/RootSetMetadataCodec.js +8 -2
- package/src/domain/services/RootSetPersistence.js +3 -2
- 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/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 +48 -20
- package/src/infrastructure/adapters/GitRefAdapter.js +6 -5
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +164 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +11 -1
- package/src/ports/GitRefPort.js +1 -0
- package/src/ports/RepositoryInspectionPort.js +33 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
|
|
4
|
+
import parseApplicationHandle from './ApplicationHandle.js';
|
|
5
|
+
import Oid from './Oid.js';
|
|
6
|
+
|
|
7
|
+
const POLICIES = Object.freeze(['pinned', 'evictable']);
|
|
8
|
+
const REACHABILITY = Object.freeze(['anchored', 'orphaned', 'volatile']);
|
|
9
|
+
const ROOT_KINDS = Object.freeze(['root-set', 'publication', 'cache-set', 'expiring-set']);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Immutable evidence that one observed Git generation retained a handle.
|
|
13
|
+
*/
|
|
14
|
+
export default class RetentionWitness {
|
|
15
|
+
/**
|
|
16
|
+
* @param {object} value
|
|
17
|
+
* @param {import('./AssetHandle.js').default|import('./BundleHandle.js').default|import('./PageHandle.js').default|string|object} value.handle
|
|
18
|
+
* @param {'pinned'|'evictable'} value.policy
|
|
19
|
+
* @param {'anchored'|'orphaned'|'volatile'} value.reachability
|
|
20
|
+
* @param {object} value.root
|
|
21
|
+
* @param {string} value.observedAt
|
|
22
|
+
*/
|
|
23
|
+
constructor({ handle, policy, reachability, root, observedAt }) {
|
|
24
|
+
if (!POLICIES.includes(policy)) {
|
|
25
|
+
throw RetentionWitness.#invalid('Retention witness policy must be pinned or evictable', {
|
|
26
|
+
policy,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (!REACHABILITY.includes(reachability)) {
|
|
30
|
+
throw RetentionWitness.#invalid('Retention witness has an invalid reachability state', {
|
|
31
|
+
reachability,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
const normalizedRoot = RetentionWitness.#normalizeRoot(root);
|
|
35
|
+
assertCanonicalTimestamp(observedAt, {
|
|
36
|
+
invalid: RetentionWitness.#invalid,
|
|
37
|
+
message: 'Retention witness observation time must be a canonical UTC timestamp',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
this.version = 1;
|
|
41
|
+
this.handle = parseApplicationHandle(handle);
|
|
42
|
+
this.policy = policy;
|
|
43
|
+
this.reachability = reachability;
|
|
44
|
+
this.root = Object.freeze(normalizedRoot);
|
|
45
|
+
this.observedAt = observedAt;
|
|
46
|
+
Object.freeze(this);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @returns {object}
|
|
51
|
+
*/
|
|
52
|
+
toJSON() {
|
|
53
|
+
return {
|
|
54
|
+
version: this.version,
|
|
55
|
+
handle: this.handle.toString(),
|
|
56
|
+
policy: this.policy,
|
|
57
|
+
reachability: this.reachability,
|
|
58
|
+
root: { ...this.root },
|
|
59
|
+
observedAt: this.observedAt,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static #normalizeRoot(root) {
|
|
64
|
+
if (!root || typeof root !== 'object' || Array.isArray(root)) {
|
|
65
|
+
throw RetentionWitness.#invalid('Retention witness root must be an object', { root });
|
|
66
|
+
}
|
|
67
|
+
if (!ROOT_KINDS.includes(root.kind)) {
|
|
68
|
+
throw RetentionWitness.#invalid('Retention witness root kind is invalid', { root });
|
|
69
|
+
}
|
|
70
|
+
for (const field of ['namespace', 'ref', 'path']) {
|
|
71
|
+
if (typeof root[field] !== 'string' || root[field].length === 0) {
|
|
72
|
+
throw RetentionWitness.#invalid(`Retention witness root ${field} is required`, { root });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (!root.ref.startsWith('refs/')) {
|
|
76
|
+
throw RetentionWitness.#invalid('Retention witness root ref must be fully qualified', {
|
|
77
|
+
root,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
let generation;
|
|
81
|
+
try {
|
|
82
|
+
generation = Oid.from(root.generation).toString();
|
|
83
|
+
} catch (error) {
|
|
84
|
+
throw RetentionWitness.#invalid('Retention witness root generation is invalid', {
|
|
85
|
+
root,
|
|
86
|
+
originalError: error,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
kind: root.kind,
|
|
91
|
+
namespace: root.namespace,
|
|
92
|
+
ref: root.ref,
|
|
93
|
+
generation,
|
|
94
|
+
path: root.path,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static #invalid(message, meta) {
|
|
99
|
+
return createCasError(message, ErrorCodes.RETENTION_WITNESS_INVALID, meta);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
|
|
4
|
+
import AssetHandle from './AssetHandle.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Immutable result for an asset graph written without a reachability root.
|
|
8
|
+
*/
|
|
9
|
+
export default class StagedAsset {
|
|
10
|
+
/**
|
|
11
|
+
* @param {object} value
|
|
12
|
+
* @param {AssetHandle|string|object} value.handle
|
|
13
|
+
* @param {string} value.slug
|
|
14
|
+
* @param {string} value.filename
|
|
15
|
+
* @param {number} value.size
|
|
16
|
+
* @param {string} value.observedAt
|
|
17
|
+
*/
|
|
18
|
+
constructor({ handle, slug, filename, size, observedAt }) {
|
|
19
|
+
if (typeof slug !== 'string' || slug.length === 0) {
|
|
20
|
+
throw StagedAsset.#invalid('Staged asset slug must be a non-empty string', { slug });
|
|
21
|
+
}
|
|
22
|
+
if (typeof filename !== 'string' || filename.length === 0) {
|
|
23
|
+
throw StagedAsset.#invalid('Staged asset filename must be a non-empty string', { filename });
|
|
24
|
+
}
|
|
25
|
+
if (!Number.isSafeInteger(size) || size < 0) {
|
|
26
|
+
throw StagedAsset.#invalid('Staged asset size must be a non-negative safe integer', { size });
|
|
27
|
+
}
|
|
28
|
+
assertCanonicalTimestamp(observedAt, {
|
|
29
|
+
invalid: StagedAsset.#invalid,
|
|
30
|
+
message: 'Staged asset observation time must be a canonical UTC timestamp',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
this.version = 1;
|
|
34
|
+
this.state = 'staged';
|
|
35
|
+
this.handle = AssetHandle.from(handle);
|
|
36
|
+
this.asset = Object.freeze({ slug, filename, size });
|
|
37
|
+
this.retention = Object.freeze({
|
|
38
|
+
policy: null,
|
|
39
|
+
reachability: 'unanchored',
|
|
40
|
+
protection: 'not-established',
|
|
41
|
+
});
|
|
42
|
+
this.observedAt = observedAt;
|
|
43
|
+
Object.freeze(this);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @returns {object}
|
|
48
|
+
*/
|
|
49
|
+
toJSON() {
|
|
50
|
+
return {
|
|
51
|
+
version: this.version,
|
|
52
|
+
state: this.state,
|
|
53
|
+
handle: this.handle.toString(),
|
|
54
|
+
asset: { ...this.asset },
|
|
55
|
+
retention: { ...this.retention },
|
|
56
|
+
observedAt: this.observedAt,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static #invalid(message, meta) {
|
|
61
|
+
return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
|
|
4
|
+
import BundleHandle from './BundleHandle.js';
|
|
5
|
+
import BundleLimits from './BundleLimits.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Immutable result for a bundle graph written without a reachability root.
|
|
9
|
+
*/
|
|
10
|
+
export default class StagedBundle {
|
|
11
|
+
/**
|
|
12
|
+
* @param {object} value
|
|
13
|
+
* @param {BundleHandle|string|object} value.handle
|
|
14
|
+
* @param {number} value.memberCount
|
|
15
|
+
* @param {number} value.indexDepth
|
|
16
|
+
* @param {number} value.descriptorBytes
|
|
17
|
+
* @param {BundleLimits|object} value.limits
|
|
18
|
+
* @param {string} value.observedAt
|
|
19
|
+
*/
|
|
20
|
+
constructor({ handle, memberCount, indexDepth, descriptorBytes, limits, observedAt }) {
|
|
21
|
+
for (const [field, number] of Object.entries({ memberCount, descriptorBytes })) {
|
|
22
|
+
if (!Number.isSafeInteger(number) || number < 0) {
|
|
23
|
+
throw StagedBundle.#invalid('Staged bundle metric is invalid', { field, value: number });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!Number.isSafeInteger(indexDepth) || indexDepth < 1) {
|
|
27
|
+
throw StagedBundle.#invalid('Staged bundle index depth is invalid', {
|
|
28
|
+
field: 'indexDepth',
|
|
29
|
+
value: indexDepth,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
assertCanonicalTimestamp(observedAt, {
|
|
33
|
+
invalid: StagedBundle.#invalid,
|
|
34
|
+
message: 'Staged bundle observation time must be a canonical UTC timestamp',
|
|
35
|
+
});
|
|
36
|
+
this.version = 1;
|
|
37
|
+
this.state = 'staged';
|
|
38
|
+
this.handle = BundleHandle.from(handle);
|
|
39
|
+
this.bundle = Object.freeze({ memberCount, indexDepth, descriptorBytes });
|
|
40
|
+
this.limits = Object.freeze(new BundleLimits(limits).toJSON());
|
|
41
|
+
this.retention = Object.freeze({
|
|
42
|
+
policy: null,
|
|
43
|
+
reachability: 'unanchored',
|
|
44
|
+
protection: 'not-established',
|
|
45
|
+
});
|
|
46
|
+
this.observedAt = observedAt;
|
|
47
|
+
Object.freeze(this);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** @returns {object} */
|
|
51
|
+
toJSON() {
|
|
52
|
+
return {
|
|
53
|
+
version: this.version,
|
|
54
|
+
state: this.state,
|
|
55
|
+
handle: this.handle.toString(),
|
|
56
|
+
bundle: { ...this.bundle },
|
|
57
|
+
limits: { ...this.limits },
|
|
58
|
+
retention: { ...this.retention },
|
|
59
|
+
observedAt: this.observedAt,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static #invalid(message, meta) {
|
|
64
|
+
return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
|
|
4
|
+
import PageHandle from './PageHandle.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Immutable result for a page blob written without a reachability root.
|
|
8
|
+
*/
|
|
9
|
+
export default class StagedPage {
|
|
10
|
+
/** @param {{ handle: PageHandle|string|object, size: number, observedAt: string }} value */
|
|
11
|
+
constructor({ handle, size, observedAt }) {
|
|
12
|
+
if (!Number.isSafeInteger(size) || size < 0) {
|
|
13
|
+
throw StagedPage.#invalid('Staged page size must be a non-negative safe integer', { size });
|
|
14
|
+
}
|
|
15
|
+
assertCanonicalTimestamp(observedAt, {
|
|
16
|
+
invalid: StagedPage.#invalid,
|
|
17
|
+
message: 'Staged page observation time must be a canonical UTC timestamp',
|
|
18
|
+
});
|
|
19
|
+
this.version = 1;
|
|
20
|
+
this.state = 'staged';
|
|
21
|
+
this.handle = PageHandle.from(handle);
|
|
22
|
+
this.page = Object.freeze({ size });
|
|
23
|
+
this.retention = Object.freeze({
|
|
24
|
+
policy: null,
|
|
25
|
+
reachability: 'unanchored',
|
|
26
|
+
protection: 'not-established',
|
|
27
|
+
});
|
|
28
|
+
this.observedAt = observedAt;
|
|
29
|
+
Object.freeze(this);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @returns {object} */
|
|
33
|
+
toJSON() {
|
|
34
|
+
return {
|
|
35
|
+
version: this.version,
|
|
36
|
+
state: this.state,
|
|
37
|
+
handle: this.handle.toString(),
|
|
38
|
+
page: { ...this.page },
|
|
39
|
+
retention: { ...this.retention },
|
|
40
|
+
observedAt: this.observedAt,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static #invalid(message, meta) {
|
|
45
|
+
return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -4,7 +4,6 @@ import os from 'node:os';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import GitPersistencePort from '../../ports/GitPersistencePort.js';
|
|
6
6
|
import { CasError, createCasError, ErrorCodes } from '../../domain/errors/index.js';
|
|
7
|
-
import { errorDetailsText } from '../../domain/helpers/gitRefErrors.js';
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Default resilience policy: 30 s timeout (no retry).
|
|
@@ -19,6 +18,8 @@ export const DEFAULT_MAX_BLOB_SIZE = 10 * 1024 * 1024;
|
|
|
19
18
|
const MIN_READ_BLOB_LIMIT = 1;
|
|
20
19
|
const MIN_MAX_BLOB_SIZE = 1024;
|
|
21
20
|
const MAX_BLOB_SIZE_LIMIT = Number.MAX_SAFE_INTEGER;
|
|
21
|
+
const OBJECT_INFO_ARGUMENT = '--batch-check=%(objectname) %(objecttype) %(objectsize)';
|
|
22
|
+
const GIT_OBJECT_TYPES = new Set(['blob', 'tree', 'commit', 'tag']);
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* {@link GitPersistencePort} implementation backed by `@git-stunts/plumbing`.
|
|
@@ -187,19 +188,53 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
187
188
|
* @returns {Promise<string>} Git object type.
|
|
188
189
|
*/
|
|
189
190
|
async readObjectType(oid) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
191
|
+
return (await this.#readObjectInfo(oid)).type;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @override
|
|
196
|
+
* @param {string} oid - Git object ID.
|
|
197
|
+
* @returns {Promise<number>} Git object size in bytes.
|
|
198
|
+
*/
|
|
199
|
+
async readObjectSize(oid) {
|
|
200
|
+
return (await this.#readObjectInfo(oid)).size;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Reads object metadata through Git's structured batch protocol. Missing
|
|
205
|
+
* objects are reported on stdout with exit zero, avoiding runtime-specific
|
|
206
|
+
* stderr capture while preserving genuine command failures.
|
|
207
|
+
*
|
|
208
|
+
* @param {string} oid - Git object ID.
|
|
209
|
+
* @returns {Promise<{ oid: string, type: string, size: number }>} Object metadata.
|
|
210
|
+
*/
|
|
211
|
+
async #readObjectInfo(oid) {
|
|
212
|
+
const rawOutput = await this.policy.execute(() => this.plumbing.execute({
|
|
213
|
+
args: ['cat-file', OBJECT_INFO_ARGUMENT],
|
|
214
|
+
input: `${oid}\n`,
|
|
215
|
+
}));
|
|
216
|
+
const output = typeof rawOutput === 'string' ? rawOutput.trim() : '';
|
|
217
|
+
if (output === `${oid} missing`) {
|
|
218
|
+
throw new CasError(`Git object not found: ${oid}`, ErrorCodes.GIT_OBJECT_NOT_FOUND, {
|
|
219
|
+
oid,
|
|
220
|
+
});
|
|
202
221
|
}
|
|
222
|
+
|
|
223
|
+
const fields = output.split(' ');
|
|
224
|
+
const size = Number(fields[2]);
|
|
225
|
+
if (
|
|
226
|
+
fields.length !== 3 ||
|
|
227
|
+
fields[0] !== oid ||
|
|
228
|
+
!GIT_OBJECT_TYPES.has(fields[1]) ||
|
|
229
|
+
!Number.isSafeInteger(size) ||
|
|
230
|
+
size < 0
|
|
231
|
+
) {
|
|
232
|
+
throw new CasError(`Git object has invalid metadata: ${oid}`, ErrorCodes.GIT_ERROR, {
|
|
233
|
+
oid,
|
|
234
|
+
output: rawOutput,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
return Object.freeze({ oid: fields[0], type: fields[1], size });
|
|
203
238
|
}
|
|
204
239
|
|
|
205
240
|
/**
|
|
@@ -332,10 +367,3 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
332
367
|
};
|
|
333
368
|
}
|
|
334
369
|
}
|
|
335
|
-
|
|
336
|
-
function isMissingGitObjectError(err) {
|
|
337
|
-
const message = errorDetailsText(err).toLowerCase();
|
|
338
|
-
return message.includes('could not get object info') ||
|
|
339
|
-
message.includes('not a valid object name') ||
|
|
340
|
-
message.includes('bad object');
|
|
341
|
-
}
|
|
@@ -12,7 +12,6 @@ import { isGitMissingRefError } from '../../domain/helpers/gitRefErrors.js';
|
|
|
12
12
|
* an unref'd timer that allows Node to exit before the next attempt starts.
|
|
13
13
|
*/
|
|
14
14
|
const DEFAULT_POLICY = Policy.timeout(30_000);
|
|
15
|
-
const GIT_NULL_OID = '0'.repeat(40);
|
|
16
15
|
|
|
17
16
|
/**
|
|
18
17
|
* {@link GitRefPort} implementation backed by `@git-stunts/plumbing`.
|
|
@@ -89,13 +88,15 @@ export default class GitRefAdapter extends GitRefPort {
|
|
|
89
88
|
* @param {Object} options
|
|
90
89
|
* @param {string} options.treeOid - Tree OID for the commit.
|
|
91
90
|
* @param {string|null} [options.parentOid] - Parent commit OID.
|
|
91
|
+
* @param {string[]} [options.parentOids] - Ordered parent commit OIDs.
|
|
92
92
|
* @param {string} options.message - Commit message.
|
|
93
93
|
* @returns {Promise<string>} The new commit OID.
|
|
94
94
|
*/
|
|
95
|
-
async createCommit({ treeOid, parentOid, message }) {
|
|
95
|
+
async createCommit({ treeOid, parentOid, parentOids, message }) {
|
|
96
96
|
const args = ['commit-tree', treeOid, '-m', message];
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
const parents = parentOids ?? (parentOid ? [parentOid] : []);
|
|
98
|
+
for (const parent of parents) {
|
|
99
|
+
args.push('-p', parent);
|
|
99
100
|
}
|
|
100
101
|
return this.policy.execute(() =>
|
|
101
102
|
this.plumbing.execute({ args }),
|
|
@@ -113,7 +114,7 @@ export default class GitRefAdapter extends GitRefPort {
|
|
|
113
114
|
async updateRef({ ref, newOid, expectedOldOid }) {
|
|
114
115
|
const args = ['update-ref', ref, newOid];
|
|
115
116
|
if (expectedOldOid !== undefined) {
|
|
116
|
-
args.push(expectedOldOid ??
|
|
117
|
+
args.push(expectedOldOid ?? '0'.repeat(newOid.length));
|
|
117
118
|
}
|
|
118
119
|
await this.policy.execute(() =>
|
|
119
120
|
this.plumbing.execute({ args }),
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Policy } from '@git-stunts/alfred';
|
|
2
|
+
import { CasError, ErrorCodes } from '../../domain/errors/index.js';
|
|
3
|
+
import Oid from '../../domain/value-objects/Oid.js';
|
|
4
|
+
import RepositoryInspectionPort from '../../ports/RepositoryInspectionPort.js';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_POLICY = Policy.timeout(30_000);
|
|
7
|
+
const DEFAULT_FULL_SCAN_POLICY = Policy.timeout(5 * 60_000);
|
|
8
|
+
const OBJECT_TYPES = new Set(['blob', 'tree', 'commit', 'tag']);
|
|
9
|
+
const OBJECT_FORMAT = '--batch-check=%(objectname) %(objecttype) %(objectsize) %(objectsize:disk)';
|
|
10
|
+
|
|
11
|
+
/** Non-mutating repository inspection backed by safe Git plumbing commands. */
|
|
12
|
+
export default class GitRepositoryInspectionAdapter extends RepositoryInspectionPort {
|
|
13
|
+
constructor({ plumbing, policy }) {
|
|
14
|
+
super();
|
|
15
|
+
if (
|
|
16
|
+
typeof plumbing?.execute !== 'function' ||
|
|
17
|
+
typeof plumbing?.executeStream !== 'function' ||
|
|
18
|
+
typeof plumbing?.inspectPrunableObjects !== 'function'
|
|
19
|
+
) {
|
|
20
|
+
throw new CasError(
|
|
21
|
+
'Repository inspection requires GitPlumbing 3.1.0 or newer',
|
|
22
|
+
ErrorCodes.REPOSITORY_INSPECTION_INVALID
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
this.plumbing = plumbing;
|
|
26
|
+
this.policy = policy ?? DEFAULT_POLICY;
|
|
27
|
+
this.fullScanPolicy = policy ?? DEFAULT_FULL_SCAN_POLICY;
|
|
28
|
+
Object.freeze(this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async *iterateObjects() {
|
|
32
|
+
const stream = await this.#stream({
|
|
33
|
+
args: ['cat-file', '--batch-all-objects', OBJECT_FORMAT],
|
|
34
|
+
});
|
|
35
|
+
for await (const line of consumeLines(stream, 'object inventory')) {
|
|
36
|
+
const fields = line.split(' ');
|
|
37
|
+
if (fields.length !== 4) {
|
|
38
|
+
throw invalidOutput('object inventory', line);
|
|
39
|
+
}
|
|
40
|
+
const [oid, type] = fields;
|
|
41
|
+
yield Object.freeze({
|
|
42
|
+
oid: parseOid(oid, 'object inventory', line),
|
|
43
|
+
type: parseType(type, 'object inventory', line),
|
|
44
|
+
logicalBytes: parseBytes(fields[2], 'object inventory', line),
|
|
45
|
+
physicalBytes: parseBytes(fields[3], 'object inventory', line),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async *iterateReachableObjectIds() {
|
|
51
|
+
const stream = await this.#stream({
|
|
52
|
+
args: ['rev-list', '--all', '--reflog', '--objects', '--no-object-names'],
|
|
53
|
+
});
|
|
54
|
+
for await (const line of consumeLines(stream, 'reachable object inventory')) {
|
|
55
|
+
yield parseOid(line, 'reachable object inventory', line);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async *iteratePrunableObjects({ expiresBefore }) {
|
|
60
|
+
const stream = await this.policy.execute(() =>
|
|
61
|
+
this.plumbing.inspectPrunableObjects({ expiresBefore })
|
|
62
|
+
);
|
|
63
|
+
for await (const line of consumeLines(stream, 'prunable object inspection')) {
|
|
64
|
+
const fields = line.trim().split(/\s+/u);
|
|
65
|
+
if (fields.length !== 2) {
|
|
66
|
+
throw invalidOutput('prunable object inspection', line);
|
|
67
|
+
}
|
|
68
|
+
yield Object.freeze({
|
|
69
|
+
oid: parseOid(fields[0], 'prunable object inspection', line),
|
|
70
|
+
type: parseType(fields[1], 'prunable object inspection', line),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async *iterateRefs() {
|
|
76
|
+
const stream = await this.#stream({
|
|
77
|
+
args: ['for-each-ref', '--format=%(refname)%09%(objectname)', 'refs/'],
|
|
78
|
+
});
|
|
79
|
+
for await (const line of consumeLines(stream, 'ref inventory')) {
|
|
80
|
+
const fields = line.split('\t');
|
|
81
|
+
if (fields.length !== 2 || !fields[0].startsWith('refs/')) {
|
|
82
|
+
throw invalidOutput('ref inventory', line);
|
|
83
|
+
}
|
|
84
|
+
yield Object.freeze({
|
|
85
|
+
ref: fields[0],
|
|
86
|
+
oid: parseOid(fields[1], 'ref inventory', line),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async reachablePhysicalBytes() {
|
|
92
|
+
const output = await this.fullScanPolicy.execute(() =>
|
|
93
|
+
this.plumbing.execute({
|
|
94
|
+
args: ['rev-list', '--all', '--reflog', '--objects', '--disk-usage'],
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
return parseBytes(String(output).trim(), 'reachable disk usage', output);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async #stream(options) {
|
|
101
|
+
return await this.policy.execute(() => this.plumbing.executeStream(options));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function* consumeLines(stream, operation) {
|
|
106
|
+
const decoder = new globalThis.TextDecoder();
|
|
107
|
+
let pending = '';
|
|
108
|
+
for await (const chunk of stream) {
|
|
109
|
+
pending += typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
|
|
110
|
+
let newline = pending.indexOf('\n');
|
|
111
|
+
while (newline !== -1) {
|
|
112
|
+
const line = pending.slice(0, newline).replace(/\r$/u, '');
|
|
113
|
+
pending = pending.slice(newline + 1);
|
|
114
|
+
if (line.length > 0) {
|
|
115
|
+
yield line;
|
|
116
|
+
}
|
|
117
|
+
newline = pending.indexOf('\n');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
pending += decoder.decode();
|
|
121
|
+
if (pending.length > 0) {
|
|
122
|
+
yield pending.replace(/\r$/u, '');
|
|
123
|
+
}
|
|
124
|
+
const result = await stream.finished;
|
|
125
|
+
if (result?.code !== 0) {
|
|
126
|
+
throw new CasError(`Git ${operation} failed`, ErrorCodes.REPOSITORY_INSPECTION_INVALID, {
|
|
127
|
+
operation,
|
|
128
|
+
stderr: result?.stderr,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function parseOid(value, operation, output) {
|
|
134
|
+
if (!Oid.isValid(value)) {
|
|
135
|
+
throw invalidOutput(operation, output);
|
|
136
|
+
}
|
|
137
|
+
return Oid.from(value).toString();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function parseType(value, operation, output) {
|
|
141
|
+
if (!OBJECT_TYPES.has(value)) {
|
|
142
|
+
throw invalidOutput(operation, output);
|
|
143
|
+
}
|
|
144
|
+
return value;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function parseBytes(value, operation, output) {
|
|
148
|
+
if (!/^\d+$/u.test(String(value))) {
|
|
149
|
+
throw invalidOutput(operation, output);
|
|
150
|
+
}
|
|
151
|
+
const bytes = Number(value);
|
|
152
|
+
if (!Number.isSafeInteger(bytes)) {
|
|
153
|
+
throw invalidOutput(operation, output);
|
|
154
|
+
}
|
|
155
|
+
return bytes;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function invalidOutput(operation, output) {
|
|
159
|
+
return new CasError(
|
|
160
|
+
`Git ${operation} returned invalid structured output`,
|
|
161
|
+
ErrorCodes.REPOSITORY_INSPECTION_INVALID,
|
|
162
|
+
{ operation, output: String(output) }
|
|
163
|
+
);
|
|
164
|
+
}
|
package/src/package-version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const PACKAGE_VERSION = '6.
|
|
1
|
+
export const PACKAGE_VERSION = '6.2.0';
|
|
@@ -24,9 +24,10 @@ export default class GitPersistencePort {
|
|
|
24
24
|
/**
|
|
25
25
|
* Reads a Git blob by its OID.
|
|
26
26
|
* @param {string} _oid - Git object ID.
|
|
27
|
+
* @param {number} [_maxBytes] - Maximum bytes the adapter may materialize.
|
|
27
28
|
* @returns {Promise<Uint8Array>} The blob content.
|
|
28
29
|
*/
|
|
29
|
-
async readBlob(_oid) {
|
|
30
|
+
async readBlob(_oid, _maxBytes) {
|
|
30
31
|
throw new Error('Not implemented');
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -77,4 +78,13 @@ export default class GitPersistencePort {
|
|
|
77
78
|
async readObjectType(_oid) {
|
|
78
79
|
throw new Error('Not implemented');
|
|
79
80
|
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Reads the Git object size without materializing object content.
|
|
84
|
+
* @param {string} _oid - Git object ID.
|
|
85
|
+
* @returns {Promise<number>} Object size in bytes.
|
|
86
|
+
*/
|
|
87
|
+
async readObjectSize(_oid) {
|
|
88
|
+
throw new Error('Not implemented');
|
|
89
|
+
}
|
|
80
90
|
}
|
package/src/ports/GitRefPort.js
CHANGED
|
@@ -36,6 +36,7 @@ export default class GitRefPort {
|
|
|
36
36
|
* @param {Object} _options
|
|
37
37
|
* @param {string} _options.treeOid - Tree OID for the commit.
|
|
38
38
|
* @param {string|null} [_options.parentOid] - Parent commit OID (null for root commit).
|
|
39
|
+
* @param {string[]} [_options.parentOids] - Ordered parent commit OIDs.
|
|
39
40
|
* @param {string} _options.message - Commit message.
|
|
40
41
|
* @returns {Promise<string>} The new commit OID.
|
|
41
42
|
*/
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract port for non-mutating, repository-wide Git object inspection.
|
|
3
|
+
* @abstract
|
|
4
|
+
*/
|
|
5
|
+
export default class RepositoryInspectionPort {
|
|
6
|
+
/** @returns {AsyncIterable<{ oid: string, type: string, logicalBytes: number, physicalBytes: number }>} */
|
|
7
|
+
iterateObjects() {
|
|
8
|
+
throw new Error('Not implemented');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** @returns {AsyncIterable<string>} */
|
|
12
|
+
iterateReachableObjectIds() {
|
|
13
|
+
throw new Error('Not implemented');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {{ expiresBefore: string }} _options
|
|
18
|
+
* @returns {AsyncIterable<{ oid: string, type: string }>}
|
|
19
|
+
*/
|
|
20
|
+
iteratePrunableObjects(_options) {
|
|
21
|
+
throw new Error('Not implemented');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @returns {AsyncIterable<{ ref: string, oid: string }>} */
|
|
25
|
+
iterateRefs() {
|
|
26
|
+
throw new Error('Not implemented');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @returns {Promise<number>} */
|
|
30
|
+
async reachablePhysicalBytes() {
|
|
31
|
+
throw new Error('Not implemented');
|
|
32
|
+
}
|
|
33
|
+
}
|