@git-stunts/git-cas 6.0.1 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +120 -6
- package/CHANGELOG.md +98 -1
- package/GUIDE.md +114 -13
- package/README.md +60 -4
- package/UPGRADING.md +71 -1
- package/build-info.json +2 -2
- package/docs/API.md +934 -7
- package/docs/WALKTHROUGH.md +46 -0
- package/docs/releases/v6.1.0.md +75 -0
- package/docs/releases/v6.2.0.md +131 -0
- package/index.d.ts +974 -1
- package/index.js +354 -1
- package/package.json +8 -6
- package/src/domain/errors/Codes.js +48 -0
- package/src/domain/helpers/assertCanonicalTimestamp.js +15 -0
- package/src/domain/helpers/handleTarget.js +48 -0
- package/src/domain/helpers/isCanonicalCollectionKey.js +35 -0
- package/src/domain/services/AssetService.js +213 -0
- package/src/domain/services/BundleDescriptorCodec.js +286 -0
- package/src/domain/services/BundleFanoutBuilder.js +247 -0
- package/src/domain/services/BundleService.js +848 -0
- package/src/domain/services/CacheCandidateHeap.js +71 -0
- package/src/domain/services/CacheIndex.js +362 -0
- package/src/domain/services/CacheMetadataCodec.js +164 -0
- package/src/domain/services/CachePolicyEnforcer.js +144 -0
- package/src/domain/services/CacheSet.js +703 -0
- package/src/domain/services/CacheSetRegistry.js +88 -0
- package/src/domain/services/CasService.d.ts +3 -1
- package/src/domain/services/ExpiringSet.js +496 -0
- package/src/domain/services/ExpiringSetIndex.js +254 -0
- package/src/domain/services/ExpiringSetMetadataCodec.js +160 -0
- package/src/domain/services/ExpiringSetRegistry.js +101 -0
- package/src/domain/services/PageService.js +175 -0
- package/src/domain/services/PublicationService.js +315 -0
- package/src/domain/services/RepositoryDoctor.js +649 -0
- package/src/domain/services/RetentionService.js +120 -0
- package/src/domain/services/RootSet.js +281 -0
- package/src/domain/services/RootSetMetadataCodec.js +259 -0
- package/src/domain/services/RootSetPersistence.js +335 -0
- package/src/domain/services/RootSetRegistry.js +44 -0
- package/src/domain/services/RootSetRetryPolicy.js +87 -0
- package/src/domain/services/RootSetTreeCodec.js +95 -0
- package/src/domain/services/StagingEvidence.js +39 -0
- package/src/domain/value-objects/ApplicationHandle.js +40 -0
- package/src/domain/value-objects/AssetHandle.js +163 -0
- package/src/domain/value-objects/BundleHandle.js +32 -0
- package/src/domain/value-objects/BundleLimits.js +80 -0
- package/src/domain/value-objects/BundlePath.js +78 -0
- package/src/domain/value-objects/CacheHit.js +93 -0
- package/src/domain/value-objects/CacheKey.js +32 -0
- package/src/domain/value-objects/CachePolicy.js +59 -0
- package/src/domain/value-objects/CacheSetRef.js +40 -0
- package/src/domain/value-objects/CollectionNamespace.js +54 -0
- package/src/domain/value-objects/ExpiringMarker.js +81 -0
- package/src/domain/value-objects/ExpiringSetKey.js +28 -0
- package/src/domain/value-objects/ExpiringSetRef.js +42 -0
- package/src/domain/value-objects/OpaqueHandle.js +171 -0
- package/src/domain/value-objects/PageHandle.js +34 -0
- package/src/domain/value-objects/RetentionWitness.js +101 -0
- package/src/domain/value-objects/RootSetRef.js +88 -0
- package/src/domain/value-objects/StagedAsset.js +63 -0
- package/src/domain/value-objects/StagedBundle.js +66 -0
- package/src/domain/value-objects/StagedPage.js +47 -0
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +57 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +26 -5
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +164 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +20 -1
- package/src/ports/GitRefPort.js +10 -0
- package/src/ports/RepositoryInspectionPort.js +33 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import CachePolicy from '../value-objects/CachePolicy.js';
|
|
2
|
+
import createCasError from '../errors/createCasError.js';
|
|
3
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
4
|
+
import {
|
|
5
|
+
CACHE_ACCOUNTING_VERSION,
|
|
6
|
+
CACHE_METADATA_VERSION,
|
|
7
|
+
} from './CacheMetadataCodec.js';
|
|
8
|
+
|
|
9
|
+
/** Applies expiry and bounded approximate-LRU policy without hydrating an index. */
|
|
10
|
+
export default class CachePolicyEnforcer {
|
|
11
|
+
#index;
|
|
12
|
+
#namespace;
|
|
13
|
+
|
|
14
|
+
constructor({ index, namespace }) {
|
|
15
|
+
this.#index = index;
|
|
16
|
+
this.#namespace = namespace;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async enforce(handle, { policy: value, now, protectedDigest = null }) {
|
|
20
|
+
const policy = CachePolicy.from(value);
|
|
21
|
+
let current = handle;
|
|
22
|
+
let changed = false;
|
|
23
|
+
while (current !== null) {
|
|
24
|
+
const scan = await this.#index.scan(current, { now, protectedDigest });
|
|
25
|
+
assertNamespace(scan.state, this.#namespace);
|
|
26
|
+
const removals = selectRemovals(scan, policy);
|
|
27
|
+
const policyChanged = !samePolicy(scan.state.policy, policy);
|
|
28
|
+
if (removals.length === 0 && !policyChanged) {
|
|
29
|
+
return result({ handle: current, scan, policy, changed });
|
|
30
|
+
}
|
|
31
|
+
const removeDigests = new Set(removals.map((candidate) => candidate.digest));
|
|
32
|
+
const projected = await this.#index.scan(current, {
|
|
33
|
+
now,
|
|
34
|
+
protectedDigest,
|
|
35
|
+
excludeDigests: removeDigests,
|
|
36
|
+
});
|
|
37
|
+
const state = createCacheState({
|
|
38
|
+
namespace: this.#namespace,
|
|
39
|
+
policy,
|
|
40
|
+
summary: projected.summary,
|
|
41
|
+
previous: scan.state,
|
|
42
|
+
now,
|
|
43
|
+
});
|
|
44
|
+
current = await this.#index.rewrite({
|
|
45
|
+
handle: current,
|
|
46
|
+
removeDigests,
|
|
47
|
+
replacement: null,
|
|
48
|
+
state,
|
|
49
|
+
});
|
|
50
|
+
changed = true;
|
|
51
|
+
}
|
|
52
|
+
return result({ handle: null, scan: { summary: emptySummary() }, policy, changed });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createCacheState({ namespace, policy: value, summary, previous, now }) {
|
|
57
|
+
const policy = CachePolicy.from(value);
|
|
58
|
+
return Object.freeze({
|
|
59
|
+
version: CACHE_METADATA_VERSION,
|
|
60
|
+
accountingVersion: CACHE_ACCOUNTING_VERSION,
|
|
61
|
+
namespace,
|
|
62
|
+
policy: Object.freeze(policy.toJSON()),
|
|
63
|
+
entryCount: summary.entryCount,
|
|
64
|
+
logicalBytes: summary.logicalBytes,
|
|
65
|
+
pinnedEntries: summary.pinnedEntries,
|
|
66
|
+
evictableEntries: summary.evictableEntries,
|
|
67
|
+
expiredEntries: summary.expiredEntries,
|
|
68
|
+
createdAt: previous?.createdAt ?? now,
|
|
69
|
+
updatedAt: now,
|
|
70
|
+
oldestAccessedAt: summary.oldestAccessedAt,
|
|
71
|
+
nextExpiry: summary.nextExpiry,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function selectRemovals(scan, policy) {
|
|
76
|
+
if (scan.summary.expiredEntries > 0) {
|
|
77
|
+
return scan.expiredCandidates;
|
|
78
|
+
}
|
|
79
|
+
let entryCount = scan.summary.entryCount;
|
|
80
|
+
let logicalBytes = scan.summary.logicalBytes;
|
|
81
|
+
const selected = [];
|
|
82
|
+
for (const candidate of scan.evictionCandidates) {
|
|
83
|
+
if (withinCapacity(entryCount, logicalBytes, policy)) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
selected.push(candidate);
|
|
87
|
+
entryCount -= 1;
|
|
88
|
+
logicalBytes -= candidate.logicalBytes;
|
|
89
|
+
}
|
|
90
|
+
return selected;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function withinCapacity(entryCount, logicalBytes, policy) {
|
|
94
|
+
return entryCount <= policy.maxEntries &&
|
|
95
|
+
(policy.maxBytes === null || logicalBytes <= policy.maxBytes);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function cachePolicyReport(summary, value) {
|
|
99
|
+
const policy = CachePolicy.from(value);
|
|
100
|
+
return Object.freeze({
|
|
101
|
+
satisfied: withinCapacity(summary.entryCount, summary.logicalBytes, policy),
|
|
102
|
+
entryCount: summary.entryCount,
|
|
103
|
+
logicalBytes: summary.logicalBytes,
|
|
104
|
+
pinnedEntries: summary.pinnedEntries,
|
|
105
|
+
evictableEntries: summary.evictableEntries,
|
|
106
|
+
expiredEntries: summary.expiredEntries,
|
|
107
|
+
limits: Object.freeze(policy.toJSON()),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function result({ handle, scan, policy, changed }) {
|
|
112
|
+
return Object.freeze({
|
|
113
|
+
handle,
|
|
114
|
+
scan,
|
|
115
|
+
changed,
|
|
116
|
+
policy: cachePolicyReport(scan.summary, policy),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function samePolicy(left, right) {
|
|
121
|
+
return JSON.stringify(left) === JSON.stringify(right.toJSON());
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function assertNamespace(state, namespace) {
|
|
125
|
+
if (state.namespace !== namespace) {
|
|
126
|
+
throw createCasError(
|
|
127
|
+
'Cache index namespace does not match its ref',
|
|
128
|
+
ErrorCodes.CACHE_STATE_INVALID,
|
|
129
|
+
{ expected: namespace, actual: state.namespace },
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function emptySummary() {
|
|
135
|
+
return Object.freeze({
|
|
136
|
+
entryCount: 0,
|
|
137
|
+
logicalBytes: 0,
|
|
138
|
+
pinnedEntries: 0,
|
|
139
|
+
evictableEntries: 0,
|
|
140
|
+
expiredEntries: 0,
|
|
141
|
+
oldestAccessedAt: null,
|
|
142
|
+
nextExpiry: null,
|
|
143
|
+
});
|
|
144
|
+
}
|