@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,88 @@
|
|
|
1
|
+
import CachePolicy from '../value-objects/CachePolicy.js';
|
|
2
|
+
import CacheSetRef from '../value-objects/CacheSetRef.js';
|
|
3
|
+
import CollectionNamespace from '../value-objects/CollectionNamespace.js';
|
|
4
|
+
import createCasError from '../errors/createCasError.js';
|
|
5
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
6
|
+
import CacheIndex from './CacheIndex.js';
|
|
7
|
+
import CacheSet from './CacheSet.js';
|
|
8
|
+
import RootSet from './RootSet.js';
|
|
9
|
+
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
10
|
+
import RootSetPersistence from './RootSetPersistence.js';
|
|
11
|
+
|
|
12
|
+
/** Opens managed CacheSet instances over shared Git and application services. */
|
|
13
|
+
export default class CacheSetRegistry {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
CacheSetRegistry.#assertDependencies(options);
|
|
16
|
+
this.persistence = options.persistence;
|
|
17
|
+
this.ref = options.ref;
|
|
18
|
+
this.bundles = options.bundles;
|
|
19
|
+
this.pages = options.pages;
|
|
20
|
+
this.resolveHandle = options.resolveHandle;
|
|
21
|
+
this.crypto = options.crypto;
|
|
22
|
+
this.clock = options.clock;
|
|
23
|
+
Object.freeze(this);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
open({ namespace: value, policy, retry } = {}) {
|
|
27
|
+
const namespace = CollectionNamespace.from(value).toString();
|
|
28
|
+
const ref = CacheSetRef.forNamespace(namespace).toString();
|
|
29
|
+
const metadataCodec = new RootSetMetadataCodec({ refType: CacheSetRef });
|
|
30
|
+
const persistence = new RootSetPersistence({
|
|
31
|
+
rootSetRef: ref,
|
|
32
|
+
persistence: this.persistence,
|
|
33
|
+
ref: this.ref,
|
|
34
|
+
refType: CacheSetRef,
|
|
35
|
+
metadataCodec,
|
|
36
|
+
});
|
|
37
|
+
const rootSet = new RootSet({
|
|
38
|
+
ref,
|
|
39
|
+
persistence,
|
|
40
|
+
retry,
|
|
41
|
+
refType: CacheSetRef,
|
|
42
|
+
metadataCodec,
|
|
43
|
+
});
|
|
44
|
+
return new CacheSet({
|
|
45
|
+
namespace,
|
|
46
|
+
policy: policy === undefined ? undefined : CachePolicy.from(policy),
|
|
47
|
+
rootSet,
|
|
48
|
+
index: new CacheIndex({
|
|
49
|
+
bundles: this.bundles,
|
|
50
|
+
pages: this.pages,
|
|
51
|
+
crypto: this.crypto,
|
|
52
|
+
}),
|
|
53
|
+
resolveHandle: this.resolveHandle,
|
|
54
|
+
crypto: this.crypto,
|
|
55
|
+
clock: this.clock,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static #assertDependencies(options) {
|
|
60
|
+
const value = options ?? {};
|
|
61
|
+
const dependencies = [
|
|
62
|
+
['persistence', hasMethods(value.persistence, [
|
|
63
|
+
'writeBlob', 'writeTree', 'readBlob', 'readTree', 'readObjectType',
|
|
64
|
+
])],
|
|
65
|
+
['ref', hasMethods(value.ref, [
|
|
66
|
+
'resolveRef', 'resolveTree', 'resolveParents', 'createCommit', 'updateRef',
|
|
67
|
+
])],
|
|
68
|
+
['bundles', hasMethods(value.bundles, [
|
|
69
|
+
'putOrderedReferences', 'getMemberReference', 'iterateMemberReferences',
|
|
70
|
+
])],
|
|
71
|
+
['pages', hasMethods(value.pages, ['put', 'get'])],
|
|
72
|
+
['resolveHandle', typeof value.resolveHandle === 'function'],
|
|
73
|
+
['crypto', hasMethods(value.crypto, ['sha256'])],
|
|
74
|
+
];
|
|
75
|
+
const missing = dependencies
|
|
76
|
+
.filter(([, available]) => !available)
|
|
77
|
+
.map(([name]) => name);
|
|
78
|
+
if (missing.length > 0) {
|
|
79
|
+
throw createCasError('CacheSetRegistry requires complete dependencies', ErrorCodes.INVALID_OPTIONS, {
|
|
80
|
+
missing,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function hasMethods(target, names) {
|
|
87
|
+
return names.every((name) => typeof target?.[name] === 'function');
|
|
88
|
+
}
|
|
@@ -66,7 +66,7 @@ export interface CodecPort {
|
|
|
66
66
|
export interface GitPersistencePort {
|
|
67
67
|
writeBlob(content: Uint8Array): Promise<string>;
|
|
68
68
|
writeTree(entries: string[]): Promise<string>;
|
|
69
|
-
readBlob(oid: string): Promise<Uint8Array>;
|
|
69
|
+
readBlob(oid: string, maxBytes?: number): Promise<Uint8Array>;
|
|
70
70
|
readBlobStream(oid: string): Promise<AsyncIterable<Uint8Array>>;
|
|
71
71
|
readTree(
|
|
72
72
|
treeOid: string
|
|
@@ -78,6 +78,8 @@ export interface GitPersistencePort {
|
|
|
78
78
|
iterateTree(
|
|
79
79
|
treeOid: string
|
|
80
80
|
): AsyncIterable<{ mode: string; type: string; oid: string; name: string }>;
|
|
81
|
+
readObjectType(oid: string): Promise<string>;
|
|
82
|
+
readObjectSize(oid: string): Promise<number>;
|
|
81
83
|
setMaxBlobSize?(maxBlobSize: number): void;
|
|
82
84
|
}
|
|
83
85
|
|
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
import { utf8Encode } from '../encoding/utf8.js';
|
|
2
|
+
import createCasError from '../errors/createCasError.js';
|
|
3
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
4
|
+
import ExpiringMarker from '../value-objects/ExpiringMarker.js';
|
|
5
|
+
import ExpiringSetKey from '../value-objects/ExpiringSetKey.js';
|
|
6
|
+
import RetentionWitness from '../value-objects/RetentionWitness.js';
|
|
7
|
+
import { createExpiringSetState } from './ExpiringSetMetadataCodec.js';
|
|
8
|
+
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
9
|
+
|
|
10
|
+
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
11
|
+
const PRIMARY_DOMAIN = 'git-cas:expiring-set:v1:index\0';
|
|
12
|
+
const VERIFICATION_DOMAIN = 'git-cas:expiring-set:v1:verify\0';
|
|
13
|
+
const INSPECTION_LIMIT = 1000;
|
|
14
|
+
|
|
15
|
+
/** RootSet-backed replay marker set with expiry-only release semantics. */
|
|
16
|
+
export default class ExpiringSet {
|
|
17
|
+
#clock;
|
|
18
|
+
#crypto;
|
|
19
|
+
#index;
|
|
20
|
+
#namespace;
|
|
21
|
+
#rootSet;
|
|
22
|
+
|
|
23
|
+
constructor(options) {
|
|
24
|
+
ExpiringSet.#assertDependencies(options);
|
|
25
|
+
this.#namespace = options.namespace;
|
|
26
|
+
this.#rootSet = options.rootSet;
|
|
27
|
+
this.#index = options.index;
|
|
28
|
+
this.#crypto = options.crypto;
|
|
29
|
+
this.#clock = options.clock ?? DEFAULT_CLOCK;
|
|
30
|
+
this.ref = this.#rootSet.ref;
|
|
31
|
+
Object.freeze(this);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async contains(keyValue) {
|
|
35
|
+
const key = ExpiringSetKey.from(keyValue).toString();
|
|
36
|
+
const digests = await this.#digests(key);
|
|
37
|
+
const now = this.#now();
|
|
38
|
+
const root = await this.#rootSet.read();
|
|
39
|
+
const handle = this.#index.fromRootEntries(root.entries);
|
|
40
|
+
const marker = await this.#index.getMarker(handle, digests.keyDigest);
|
|
41
|
+
assertVerification(marker, digests);
|
|
42
|
+
return marker !== null && !isExpired(marker.metadata, now);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async addIfAbsent(keyValue, options) {
|
|
46
|
+
assertAddOptions(options);
|
|
47
|
+
const key = ExpiringSetKey.from(keyValue).toString();
|
|
48
|
+
const expiresAt = normalizeFutureExpiry(options.expiresAt, this.#now());
|
|
49
|
+
const digests = await this.#digests(key);
|
|
50
|
+
const attempt = { observed: null, replacement: null, lifecycle: null };
|
|
51
|
+
const context = { digests, expiresAt, attempt };
|
|
52
|
+
const mutation = await this.#mutateRoot(
|
|
53
|
+
(entries) => this.#admitMarker(entries, context),
|
|
54
|
+
);
|
|
55
|
+
const record = attempt.lifecycle ? attempt.replacement : attempt.observed;
|
|
56
|
+
const observedAt = this.#now();
|
|
57
|
+
const marker = record
|
|
58
|
+
? this.#marker(record, mutation.commitOid, observedAt)
|
|
59
|
+
: null;
|
|
60
|
+
return Object.freeze({
|
|
61
|
+
changed: mutation.changed,
|
|
62
|
+
admitted: attempt.lifecycle !== null,
|
|
63
|
+
marker,
|
|
64
|
+
generation: mutation.commitOid,
|
|
65
|
+
witness: marker?.evidence ?? null,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async #admitMarker(entries, context) {
|
|
70
|
+
const { attempt, digests, expiresAt } = context;
|
|
71
|
+
const now = this.#now();
|
|
72
|
+
normalizeFutureExpiry(expiresAt, now);
|
|
73
|
+
attempt.observed = null;
|
|
74
|
+
attempt.replacement = null;
|
|
75
|
+
attempt.lifecycle = null;
|
|
76
|
+
const current = this.#index.fromRootEntries(entries);
|
|
77
|
+
const previous = await this.#index.getMarker(current, digests.keyDigest);
|
|
78
|
+
assertVerification(previous, digests);
|
|
79
|
+
attempt.observed = previous;
|
|
80
|
+
if (previous && !isExpired(previous.metadata, now)) {
|
|
81
|
+
return entries;
|
|
82
|
+
}
|
|
83
|
+
await this.#assertIndexState(current);
|
|
84
|
+
attempt.replacement = await this.#index.stageMarker({
|
|
85
|
+
version: 1,
|
|
86
|
+
keyDigest: digests.keyDigest,
|
|
87
|
+
verificationDigest: digests.verificationDigest,
|
|
88
|
+
expiresAt,
|
|
89
|
+
createdAt: now,
|
|
90
|
+
});
|
|
91
|
+
const scan = await this.#index.scan(current, {
|
|
92
|
+
now,
|
|
93
|
+
excludeDigest: digests.keyDigest,
|
|
94
|
+
replacement: attempt.replacement,
|
|
95
|
+
});
|
|
96
|
+
this.#assertState(scan.state);
|
|
97
|
+
const state = createExpiringSetState({
|
|
98
|
+
namespace: this.#namespace,
|
|
99
|
+
summary: scan.summary,
|
|
100
|
+
previous: scan.state,
|
|
101
|
+
now,
|
|
102
|
+
});
|
|
103
|
+
const handle = await this.#index.rewrite({
|
|
104
|
+
handle: current,
|
|
105
|
+
removeDigest: digests.keyDigest,
|
|
106
|
+
replacement: attempt.replacement,
|
|
107
|
+
state,
|
|
108
|
+
});
|
|
109
|
+
attempt.lifecycle = { handle };
|
|
110
|
+
return [this.#index.toRootEntry(handle)];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async sweep() {
|
|
114
|
+
let observedAt = null;
|
|
115
|
+
let lifecycle = null;
|
|
116
|
+
let removed = 0;
|
|
117
|
+
const mutation = await this.#mutateRoot(async (entries) => {
|
|
118
|
+
const now = this.#now();
|
|
119
|
+
observedAt = now;
|
|
120
|
+
lifecycle = null;
|
|
121
|
+
removed = 0;
|
|
122
|
+
const current = this.#index.fromRootEntries(entries);
|
|
123
|
+
if (current === null) {
|
|
124
|
+
return entries;
|
|
125
|
+
}
|
|
126
|
+
await this.#assertIndexState(current);
|
|
127
|
+
const scan = await this.#index.scan(current, { now });
|
|
128
|
+
this.#assertState(scan.state);
|
|
129
|
+
if (scan.summary.expiredEntries === 0) {
|
|
130
|
+
return entries;
|
|
131
|
+
}
|
|
132
|
+
removed = scan.summary.expiredEntries;
|
|
133
|
+
const summary = Object.freeze({
|
|
134
|
+
entryCount: scan.summary.liveEntries,
|
|
135
|
+
liveEntries: scan.summary.liveEntries,
|
|
136
|
+
expiredEntries: 0,
|
|
137
|
+
nextExpiry: scan.summary.nextExpiry,
|
|
138
|
+
});
|
|
139
|
+
const state = createExpiringSetState({
|
|
140
|
+
namespace: this.#namespace,
|
|
141
|
+
summary,
|
|
142
|
+
previous: scan.state,
|
|
143
|
+
now,
|
|
144
|
+
});
|
|
145
|
+
const handle = await this.#index.rewrite({
|
|
146
|
+
handle: current,
|
|
147
|
+
removeExpiredAt: now,
|
|
148
|
+
replacement: null,
|
|
149
|
+
state,
|
|
150
|
+
});
|
|
151
|
+
lifecycle = { handle };
|
|
152
|
+
return [this.#index.toRootEntry(handle)];
|
|
153
|
+
});
|
|
154
|
+
return Object.freeze({
|
|
155
|
+
changed: mutation.changed,
|
|
156
|
+
removed,
|
|
157
|
+
generation: mutation.commitOid,
|
|
158
|
+
witness: lifecycle
|
|
159
|
+
? this.#retentionWitness(lifecycle.handle, mutation.commitOid, observedAt)
|
|
160
|
+
: null,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async inspect({ limit = 100, cursor = null } = {}) {
|
|
165
|
+
assertInspectOptions(limit, cursor);
|
|
166
|
+
const now = this.#now();
|
|
167
|
+
const root = await this.#rootSet.read();
|
|
168
|
+
const handle = this.#index.fromRootEntries(root.entries);
|
|
169
|
+
if (handle === null) {
|
|
170
|
+
return emptyInspection(this.#namespace, this.ref);
|
|
171
|
+
}
|
|
172
|
+
const scan = await this.#index.scan(handle, { now });
|
|
173
|
+
this.#assertState(scan.state);
|
|
174
|
+
const page = await this.#inspectMarkers({
|
|
175
|
+
handle,
|
|
176
|
+
generation: root.headOid,
|
|
177
|
+
now,
|
|
178
|
+
limit,
|
|
179
|
+
cursor,
|
|
180
|
+
});
|
|
181
|
+
return Object.freeze({
|
|
182
|
+
namespace: this.#namespace,
|
|
183
|
+
ref: this.ref,
|
|
184
|
+
generation: root.headOid,
|
|
185
|
+
state: Object.freeze({ ...scan.state }),
|
|
186
|
+
observed: scan.summary,
|
|
187
|
+
markers: page.markers,
|
|
188
|
+
nextCursor: page.nextCursor,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async #inspectMarkers({ handle, generation, now, limit, cursor }) {
|
|
193
|
+
const markers = [];
|
|
194
|
+
for await (const marker of this.#index.markers(handle)) {
|
|
195
|
+
if (cursor === null || marker.metadata.keyDigest > cursor) {
|
|
196
|
+
markers.push(this.#inspectionMarker(marker, generation, now));
|
|
197
|
+
}
|
|
198
|
+
if (markers.length > limit) {
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const hasMore = markers.length > limit;
|
|
203
|
+
if (hasMore) {
|
|
204
|
+
markers.pop();
|
|
205
|
+
}
|
|
206
|
+
return Object.freeze({
|
|
207
|
+
markers: Object.freeze(markers),
|
|
208
|
+
nextCursor: hasMore ? markers.at(-1).keyDigest : null,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async doctor() {
|
|
213
|
+
const root = await this.#rootSet.doctor();
|
|
214
|
+
if (!root.healthy) {
|
|
215
|
+
return Object.freeze({
|
|
216
|
+
healthy: false,
|
|
217
|
+
root,
|
|
218
|
+
issues: freezeIssues(root.issues ?? (root.error ? [root.error] : [])),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
try {
|
|
222
|
+
return await this.#doctorHealthyRoot(root);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return Object.freeze({
|
|
225
|
+
healthy: false,
|
|
226
|
+
root,
|
|
227
|
+
issues: freezeIssues([{
|
|
228
|
+
code: error?.code ?? ErrorCodes.EXPIRING_SET_STATE_INVALID,
|
|
229
|
+
message: error instanceof Error ? error.message : String(error),
|
|
230
|
+
}]),
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async #doctorHealthyRoot(root) {
|
|
236
|
+
const current = await this.#rootSet.read();
|
|
237
|
+
if (current.headOid !== root.headOid) {
|
|
238
|
+
return Object.freeze({
|
|
239
|
+
healthy: false,
|
|
240
|
+
root,
|
|
241
|
+
issues: freezeIssues([{
|
|
242
|
+
code: ErrorCodes.EXPIRING_SET_CONFLICT,
|
|
243
|
+
message: 'ExpiringSet changed while doctor was inspecting it',
|
|
244
|
+
expectedGeneration: root.headOid,
|
|
245
|
+
actualGeneration: current.headOid,
|
|
246
|
+
}]),
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
const handle = this.#index.fromRootEntries(current.entries);
|
|
250
|
+
if (handle === null) {
|
|
251
|
+
return Object.freeze({
|
|
252
|
+
healthy: true,
|
|
253
|
+
root,
|
|
254
|
+
state: null,
|
|
255
|
+
observed: emptySummary(),
|
|
256
|
+
issues: Object.freeze([]),
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
const persisted = await this.#index.getState(handle);
|
|
260
|
+
this.#assertState(persisted);
|
|
261
|
+
const historical = await this.#index.scan(handle, { now: persisted.updatedAt });
|
|
262
|
+
const expected = createExpiringSetState({
|
|
263
|
+
namespace: this.#namespace,
|
|
264
|
+
summary: historical.summary,
|
|
265
|
+
previous: persisted,
|
|
266
|
+
now: persisted.updatedAt,
|
|
267
|
+
});
|
|
268
|
+
const consistent = JSON.stringify(expected) === JSON.stringify(persisted);
|
|
269
|
+
const observed = (await this.#index.scan(handle, { now: this.#now() })).summary;
|
|
270
|
+
return Object.freeze({
|
|
271
|
+
healthy: consistent,
|
|
272
|
+
root,
|
|
273
|
+
state: Object.freeze({ ...persisted }),
|
|
274
|
+
observed,
|
|
275
|
+
issues: consistent
|
|
276
|
+
? Object.freeze([])
|
|
277
|
+
: freezeIssues([{ code: ErrorCodes.EXPIRING_SET_STATE_INVALID }]),
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
#marker(record, generation, observedAt) {
|
|
282
|
+
const evidence = this.#retentionWitness(record.handle, generation, observedAt);
|
|
283
|
+
return new ExpiringMarker({
|
|
284
|
+
...record.metadata,
|
|
285
|
+
generation,
|
|
286
|
+
evidence,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
#inspectionMarker(record, generation, observedAt) {
|
|
291
|
+
return Object.freeze({
|
|
292
|
+
keyDigest: record.metadata.keyDigest,
|
|
293
|
+
expiresAt: record.metadata.expiresAt,
|
|
294
|
+
createdAt: record.metadata.createdAt,
|
|
295
|
+
status: isExpired(record.metadata, observedAt) ? 'expired' : 'live',
|
|
296
|
+
evidence: this.#retentionWitness(record.handle, generation, observedAt),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
#retentionWitness(handle, generation, observedAt) {
|
|
301
|
+
return new RetentionWitness({
|
|
302
|
+
handle,
|
|
303
|
+
policy: 'pinned',
|
|
304
|
+
reachability: 'anchored',
|
|
305
|
+
root: {
|
|
306
|
+
kind: 'expiring-set',
|
|
307
|
+
namespace: this.#namespace,
|
|
308
|
+
ref: this.ref,
|
|
309
|
+
generation,
|
|
310
|
+
path: RootSetMetadataCodec.slotFor(0),
|
|
311
|
+
},
|
|
312
|
+
observedAt,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
#assertState(state) {
|
|
317
|
+
if (state !== null && state.namespace !== this.#namespace) {
|
|
318
|
+
throw createCasError(
|
|
319
|
+
'ExpiringSet index namespace does not match its ref',
|
|
320
|
+
ErrorCodes.EXPIRING_SET_STATE_INVALID,
|
|
321
|
+
{ expected: this.#namespace, actual: state.namespace },
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async #assertIndexState(handle) {
|
|
327
|
+
if (handle === null) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const persisted = await this.#index.getState(handle);
|
|
331
|
+
this.#assertState(persisted);
|
|
332
|
+
const historical = await this.#index.scan(handle, { now: persisted.updatedAt });
|
|
333
|
+
const expected = createExpiringSetState({
|
|
334
|
+
namespace: this.#namespace,
|
|
335
|
+
summary: historical.summary,
|
|
336
|
+
previous: persisted,
|
|
337
|
+
now: persisted.updatedAt,
|
|
338
|
+
});
|
|
339
|
+
if (JSON.stringify(expected) !== JSON.stringify(persisted)) {
|
|
340
|
+
throw createCasError(
|
|
341
|
+
'ExpiringSet persisted state does not match its marker edges',
|
|
342
|
+
ErrorCodes.EXPIRING_SET_STATE_INVALID,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async #digests(key) {
|
|
348
|
+
const [keyDigest, verificationDigest] = await Promise.all([
|
|
349
|
+
this.#crypto.sha256(utf8Encode(`${PRIMARY_DOMAIN}${key}`)),
|
|
350
|
+
this.#crypto.sha256(utf8Encode(`${VERIFICATION_DOMAIN}${key}`)),
|
|
351
|
+
]);
|
|
352
|
+
for (const [name, value] of Object.entries({ keyDigest, verificationDigest })) {
|
|
353
|
+
if (typeof value !== 'string' || !/^[0-9a-f]{64}$/.test(value)) {
|
|
354
|
+
throw createCasError(
|
|
355
|
+
'ExpiringSet crypto adapter returned an invalid digest',
|
|
356
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
357
|
+
{ [name]: value },
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (keyDigest === verificationDigest) {
|
|
362
|
+
throw createCasError(
|
|
363
|
+
'ExpiringSet digest domains must produce distinct values',
|
|
364
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
365
|
+
{ keyDigest },
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
return Object.freeze({ keyDigest, verificationDigest });
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async #mutateRoot(mutator) {
|
|
372
|
+
try {
|
|
373
|
+
return await this.#rootSet.mutate(mutator);
|
|
374
|
+
} catch (error) {
|
|
375
|
+
if (error?.code !== ErrorCodes.ROOT_SET_CONFLICT) {
|
|
376
|
+
throw error;
|
|
377
|
+
}
|
|
378
|
+
throw createCasError(
|
|
379
|
+
'Concurrent ExpiringSet update detected',
|
|
380
|
+
ErrorCodes.EXPIRING_SET_CONFLICT,
|
|
381
|
+
{ ref: this.ref, originalError: error },
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
#now() {
|
|
387
|
+
const value = this.#clock.now();
|
|
388
|
+
if (!(value instanceof Date) || Number.isNaN(value.getTime())) {
|
|
389
|
+
throw createCasError('ExpiringSet clock returned an invalid Date', ErrorCodes.INVALID_OPTIONS);
|
|
390
|
+
}
|
|
391
|
+
return value.toISOString();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
static #assertDependencies(options) {
|
|
395
|
+
const dependencies = [
|
|
396
|
+
['rootSet', options?.rootSet?.mutate],
|
|
397
|
+
['index', options?.index?.scan],
|
|
398
|
+
['crypto', options?.crypto?.sha256],
|
|
399
|
+
];
|
|
400
|
+
const missing = dependencies
|
|
401
|
+
.filter(([, value]) => typeof value !== 'function')
|
|
402
|
+
.map(([name]) => name);
|
|
403
|
+
if (missing.length > 0) {
|
|
404
|
+
throw createCasError(
|
|
405
|
+
'ExpiringSet requires complete lifecycle dependencies',
|
|
406
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
407
|
+
{ missing },
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function assertAddOptions(options) {
|
|
414
|
+
if (!options || typeof options !== 'object' || Array.isArray(options)) {
|
|
415
|
+
throw createCasError(
|
|
416
|
+
'ExpiringSet add options must be an object',
|
|
417
|
+
ErrorCodes.EXPIRING_SET_MARKER_INVALID,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
const unknown = Object.keys(options).filter((key) => key !== 'expiresAt');
|
|
421
|
+
if (unknown.length > 0 || !Object.hasOwn(options, 'expiresAt')) {
|
|
422
|
+
throw createCasError(
|
|
423
|
+
'ExpiringSet add options require only expiresAt',
|
|
424
|
+
ErrorCodes.EXPIRING_SET_MARKER_INVALID,
|
|
425
|
+
{ unknown },
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function normalizeFutureExpiry(value, now) {
|
|
431
|
+
const timestamp = value instanceof Date && !Number.isNaN(value.getTime())
|
|
432
|
+
? value.toISOString()
|
|
433
|
+
: value;
|
|
434
|
+
if (typeof timestamp !== 'string' ||
|
|
435
|
+
Number.isNaN(Date.parse(timestamp)) ||
|
|
436
|
+
new Date(timestamp).toISOString() !== timestamp ||
|
|
437
|
+
timestamp <= now) {
|
|
438
|
+
throw createCasError(
|
|
439
|
+
'ExpiringSet expiry must be a future canonical UTC timestamp',
|
|
440
|
+
ErrorCodes.EXPIRING_SET_MARKER_INVALID,
|
|
441
|
+
{ expiresAt: value, now },
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
return timestamp;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function assertVerification(marker, digests) {
|
|
448
|
+
if (marker && marker.metadata.verificationDigest !== digests.verificationDigest) {
|
|
449
|
+
throw createCasError(
|
|
450
|
+
'ExpiringSet key digest collision detected',
|
|
451
|
+
ErrorCodes.EXPIRING_SET_MARKER_INVALID,
|
|
452
|
+
{ keyDigest: digests.keyDigest },
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function isExpired(marker, now) {
|
|
458
|
+
return marker.expiresAt <= now;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function assertInspectOptions(limit, cursor) {
|
|
462
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > INSPECTION_LIMIT) {
|
|
463
|
+
throw createCasError(
|
|
464
|
+
`ExpiringSet inspection limit must be between 1 and ${INSPECTION_LIMIT}`,
|
|
465
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
if (cursor !== null && !/^[0-9a-f]{64}$/.test(cursor)) {
|
|
469
|
+
throw createCasError('ExpiringSet inspection cursor is invalid', ErrorCodes.INVALID_OPTIONS);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function emptyInspection(namespace, ref) {
|
|
474
|
+
return Object.freeze({
|
|
475
|
+
namespace,
|
|
476
|
+
ref,
|
|
477
|
+
generation: null,
|
|
478
|
+
state: null,
|
|
479
|
+
observed: emptySummary(),
|
|
480
|
+
markers: Object.freeze([]),
|
|
481
|
+
nextCursor: null,
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function emptySummary() {
|
|
486
|
+
return Object.freeze({
|
|
487
|
+
entryCount: 0,
|
|
488
|
+
liveEntries: 0,
|
|
489
|
+
expiredEntries: 0,
|
|
490
|
+
nextExpiry: null,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function freezeIssues(issues) {
|
|
495
|
+
return Object.freeze(issues.map((issue) => Object.freeze({ ...issue })));
|
|
496
|
+
}
|