@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,254 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import BundleHandle from '../value-objects/BundleHandle.js';
|
|
4
|
+
import PageHandle from '../value-objects/PageHandle.js';
|
|
5
|
+
import ExpiringSetMetadataCodec from './ExpiringSetMetadataCodec.js';
|
|
6
|
+
|
|
7
|
+
export const EXPIRING_SET_STATE_PATH = '.expiring/state';
|
|
8
|
+
export const EXPIRING_SET_MARKER_PREFIX = 'markers/';
|
|
9
|
+
const ROOT_ENTRY_PREFIX = 'expiring-index:';
|
|
10
|
+
const METADATA_PAGE_LIMIT = 64 * 1024;
|
|
11
|
+
const DIGEST = /^[0-9a-f]{64}$/;
|
|
12
|
+
|
|
13
|
+
/** Streaming structured-bundle index for one ExpiringSet generation. */
|
|
14
|
+
export default class ExpiringSetIndex {
|
|
15
|
+
#bundles;
|
|
16
|
+
#codec;
|
|
17
|
+
#pages;
|
|
18
|
+
|
|
19
|
+
constructor({ bundles, pages, codec = new ExpiringSetMetadataCodec() }) {
|
|
20
|
+
ExpiringSetIndex.#assertDependencies(bundles, pages);
|
|
21
|
+
this.#bundles = bundles;
|
|
22
|
+
this.#pages = pages;
|
|
23
|
+
this.#codec = codec;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fromRootEntries(entries) {
|
|
27
|
+
if (entries.length === 0) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
if (entries.length !== 1 || !entries[0].name.startsWith(ROOT_ENTRY_PREFIX)) {
|
|
31
|
+
throw invalid('ExpiringSet must anchor exactly one structured index', { entries });
|
|
32
|
+
}
|
|
33
|
+
const handle = BundleHandle.parse(entries[0].name.slice(ROOT_ENTRY_PREFIX.length));
|
|
34
|
+
if (entries[0].oid !== handle.oid || entries[0].type !== 'tree') {
|
|
35
|
+
throw invalid('ExpiringSet root entry does not match its index handle', {
|
|
36
|
+
entry: entries[0],
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return handle;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
toRootEntry(handle) {
|
|
43
|
+
const normalized = BundleHandle.from(handle);
|
|
44
|
+
return Object.freeze({
|
|
45
|
+
name: `${ROOT_ENTRY_PREFIX}${normalized}`,
|
|
46
|
+
oid: normalized.oid,
|
|
47
|
+
type: 'tree',
|
|
48
|
+
retention: 'pinned',
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getState(handle) {
|
|
53
|
+
if (handle === null) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
const member = await this.#bundles.getMemberReference({
|
|
57
|
+
handle,
|
|
58
|
+
path: EXPIRING_SET_STATE_PATH,
|
|
59
|
+
});
|
|
60
|
+
if (!member || member.handle.kind !== 'page') {
|
|
61
|
+
throw invalid('ExpiringSet index is missing its state page', {
|
|
62
|
+
handle: handle.toString(),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return this.#codec.decodeState(await this.#pages.get({
|
|
66
|
+
handle: member.handle,
|
|
67
|
+
maxBytes: METADATA_PAGE_LIMIT,
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async getMarker(handle, digest) {
|
|
72
|
+
assertDigest(digest);
|
|
73
|
+
if (handle === null) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const member = await this.#bundles.getMemberReference({
|
|
77
|
+
handle,
|
|
78
|
+
path: markerPath(digest),
|
|
79
|
+
});
|
|
80
|
+
return member ? await this.#readMarker(member, digest) : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async *markers(handle) {
|
|
84
|
+
if (handle === null) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
for await (const member of this.#bundles.iterateMemberReferences({ handle })) {
|
|
88
|
+
if (member.path === EXPIRING_SET_STATE_PATH) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const digest = digestFromPath(member.path);
|
|
92
|
+
yield await this.#readMarker(member, digest);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async stageMarker(metadata) {
|
|
97
|
+
const normalized = this.#codec.normalizeMarker(metadata);
|
|
98
|
+
const staged = await this.#pages.put({ source: this.#codec.encodeMarker(normalized) });
|
|
99
|
+
return Object.freeze({ metadata: normalized, handle: staged.handle });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async scan(handle, { now, excludeDigest = null, replacement = null } = {}) {
|
|
103
|
+
const summary = emptySummary();
|
|
104
|
+
for await (const marker of this.markers(handle)) {
|
|
105
|
+
if (marker.metadata.keyDigest !== excludeDigest &&
|
|
106
|
+
marker.metadata.keyDigest !== replacement?.metadata.keyDigest) {
|
|
107
|
+
addToSummary(summary, marker.metadata, now);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (replacement !== null) {
|
|
111
|
+
addToSummary(summary, replacement.metadata, now);
|
|
112
|
+
}
|
|
113
|
+
return Object.freeze({
|
|
114
|
+
state: await this.getState(handle),
|
|
115
|
+
summary: Object.freeze(summary),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async rewrite({ handle, removeDigest = null, removeExpiredAt = null, replacement, state }) {
|
|
120
|
+
const statePage = await this.#pages.put({ source: this.#codec.encodeState(state) });
|
|
121
|
+
const staged = await this.#bundles.putOrderedReferences({
|
|
122
|
+
members: this.#rewriteMembers({
|
|
123
|
+
handle,
|
|
124
|
+
removeDigest,
|
|
125
|
+
removeExpiredAt,
|
|
126
|
+
replacement,
|
|
127
|
+
stateHandle: statePage.handle,
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
return staged.handle;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async *#rewriteMembers({
|
|
134
|
+
handle,
|
|
135
|
+
removeDigest,
|
|
136
|
+
removeExpiredAt,
|
|
137
|
+
replacement,
|
|
138
|
+
stateHandle,
|
|
139
|
+
}) {
|
|
140
|
+
yield [EXPIRING_SET_STATE_PATH, stateHandle];
|
|
141
|
+
let inserted = replacement === null;
|
|
142
|
+
for await (const current of this.markers(handle)) {
|
|
143
|
+
const digest = current.metadata.keyDigest;
|
|
144
|
+
if (!inserted && digest > replacement.metadata.keyDigest) {
|
|
145
|
+
yield [markerPath(replacement.metadata.keyDigest), replacement.handle];
|
|
146
|
+
inserted = true;
|
|
147
|
+
}
|
|
148
|
+
const removed = digest === removeDigest ||
|
|
149
|
+
(removeExpiredAt !== null && isExpired(current.metadata, removeExpiredAt));
|
|
150
|
+
if (!removed && digest !== replacement?.metadata.keyDigest) {
|
|
151
|
+
yield [markerPath(digest), current.handle];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (!inserted) {
|
|
155
|
+
yield [markerPath(replacement.metadata.keyDigest), replacement.handle];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async #readMarker(member, digest) {
|
|
160
|
+
const handle = markerPageHandle(member);
|
|
161
|
+
const metadata = this.#codec.decodeMarker(await this.#pages.get({
|
|
162
|
+
handle,
|
|
163
|
+
maxBytes: METADATA_PAGE_LIMIT,
|
|
164
|
+
}));
|
|
165
|
+
if (metadata.keyDigest !== digest) {
|
|
166
|
+
throw invalid('Expiring marker digest does not match its index path', {
|
|
167
|
+
digest,
|
|
168
|
+
metadata,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return Object.freeze({ metadata, handle });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static #assertDependencies(bundles, pages) {
|
|
175
|
+
const bundleMethods = [
|
|
176
|
+
'putOrderedReferences',
|
|
177
|
+
'getMemberReference',
|
|
178
|
+
'iterateMemberReferences',
|
|
179
|
+
];
|
|
180
|
+
const missing = bundleMethods.filter((method) => typeof bundles?.[method] !== 'function');
|
|
181
|
+
if (typeof pages?.put !== 'function' || typeof pages?.get !== 'function') {
|
|
182
|
+
missing.push('pages.put/get');
|
|
183
|
+
}
|
|
184
|
+
if (missing.length > 0) {
|
|
185
|
+
throw invalid('ExpiringSetIndex requires bundle and page services', { missing });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function markerPath(digest) {
|
|
191
|
+
assertDigest(digest);
|
|
192
|
+
return `${EXPIRING_SET_MARKER_PREFIX}${digest}`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function digestFromPath(path) {
|
|
196
|
+
const digest = path.startsWith(EXPIRING_SET_MARKER_PREFIX)
|
|
197
|
+
? path.slice(EXPIRING_SET_MARKER_PREFIX.length)
|
|
198
|
+
: '';
|
|
199
|
+
assertDigest(digest, path);
|
|
200
|
+
return digest;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function markerPageHandle(member) {
|
|
204
|
+
if (member.handle.kind !== 'page') {
|
|
205
|
+
throw invalid('ExpiringSet marker does not reference a page', {
|
|
206
|
+
path: member.path,
|
|
207
|
+
handle: member.handle.toString(),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return PageHandle.from(member.handle);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function assertDigest(digest, path = null) {
|
|
214
|
+
if (typeof digest !== 'string' || !DIGEST.test(digest)) {
|
|
215
|
+
throw invalid('ExpiringSet index contains an invalid marker path', {
|
|
216
|
+
digest,
|
|
217
|
+
path,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function emptySummary() {
|
|
223
|
+
return {
|
|
224
|
+
entryCount: 0,
|
|
225
|
+
liveEntries: 0,
|
|
226
|
+
expiredEntries: 0,
|
|
227
|
+
nextExpiry: null,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function addToSummary(summary, marker, now) {
|
|
232
|
+
if (summary.entryCount === Number.MAX_SAFE_INTEGER) {
|
|
233
|
+
throw invalid('ExpiringSet entry count exceeds safe integer accounting');
|
|
234
|
+
}
|
|
235
|
+
summary.entryCount += 1;
|
|
236
|
+
if (isExpired(marker, now)) {
|
|
237
|
+
summary.expiredEntries += 1;
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
summary.liveEntries += 1;
|
|
241
|
+
summary.nextExpiry = earlier(summary.nextExpiry, marker.expiresAt);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function earlier(current, candidate) {
|
|
245
|
+
return current === null || candidate < current ? candidate : current;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function isExpired(marker, now) {
|
|
249
|
+
return marker.expiresAt <= now;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function invalid(message, meta) {
|
|
253
|
+
return createCasError(message, ErrorCodes.EXPIRING_SET_STATE_INVALID, meta);
|
|
254
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { utf8Decode, utf8Encode } from '../encoding/utf8.js';
|
|
2
|
+
import createCasError from '../errors/createCasError.js';
|
|
3
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
4
|
+
import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
|
|
5
|
+
import CollectionNamespace from '../value-objects/CollectionNamespace.js';
|
|
6
|
+
|
|
7
|
+
export const EXPIRING_SET_METADATA_VERSION = 1;
|
|
8
|
+
const DIGEST = /^[0-9a-f]{64}$/;
|
|
9
|
+
|
|
10
|
+
/** Canonical JSON codecs for immutable ExpiringSet pages. */
|
|
11
|
+
export default class ExpiringSetMetadataCodec {
|
|
12
|
+
encodeMarker(value) {
|
|
13
|
+
return encode(this.normalizeMarker(value));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
decodeMarker(bytes) {
|
|
17
|
+
return decodeCanonical(bytes, (value) => this.normalizeMarker(value), 'marker');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
normalizeMarker(value) {
|
|
21
|
+
assertObject(value, 'marker');
|
|
22
|
+
assertVersion(value, 'marker');
|
|
23
|
+
assertDigest(value.keyDigest, 'keyDigest');
|
|
24
|
+
assertDigest(value.verificationDigest, 'verificationDigest');
|
|
25
|
+
if (value.keyDigest === value.verificationDigest) {
|
|
26
|
+
throw invalid('marker', 'Expiring marker digest domains must be distinct', {
|
|
27
|
+
keyDigest: value.keyDigest,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
assertTimestamp(value.createdAt, false, 'marker');
|
|
31
|
+
assertTimestamp(value.expiresAt, false, 'marker');
|
|
32
|
+
if (value.expiresAt <= value.createdAt) {
|
|
33
|
+
throw invalid('marker', 'Expiring marker expiry must follow its creation time', {
|
|
34
|
+
createdAt: value.createdAt,
|
|
35
|
+
expiresAt: value.expiresAt,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return Object.freeze({
|
|
39
|
+
version: EXPIRING_SET_METADATA_VERSION,
|
|
40
|
+
keyDigest: value.keyDigest,
|
|
41
|
+
verificationDigest: value.verificationDigest,
|
|
42
|
+
expiresAt: value.expiresAt,
|
|
43
|
+
createdAt: value.createdAt,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
encodeState(value) {
|
|
48
|
+
return encode(this.normalizeState(value));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
decodeState(bytes) {
|
|
52
|
+
return decodeCanonical(bytes, (value) => this.normalizeState(value), 'state');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
normalizeState(value) {
|
|
56
|
+
assertObject(value, 'state');
|
|
57
|
+
assertVersion(value, 'state');
|
|
58
|
+
const state = {
|
|
59
|
+
version: EXPIRING_SET_METADATA_VERSION,
|
|
60
|
+
namespace: CollectionNamespace.from(value.namespace).toString(),
|
|
61
|
+
entryCount: count(value, 'entryCount'),
|
|
62
|
+
liveEntries: count(value, 'liveEntries'),
|
|
63
|
+
expiredEntries: count(value, 'expiredEntries'),
|
|
64
|
+
createdAt: value.createdAt,
|
|
65
|
+
updatedAt: value.updatedAt,
|
|
66
|
+
nextExpiry: value.nextExpiry,
|
|
67
|
+
};
|
|
68
|
+
assertTimestamp(state.createdAt, false, 'state');
|
|
69
|
+
assertTimestamp(state.updatedAt, false, 'state');
|
|
70
|
+
assertTimestamp(state.nextExpiry, true, 'state');
|
|
71
|
+
if (state.liveEntries + state.expiredEntries !== state.entryCount) {
|
|
72
|
+
throw invalid('state', 'ExpiringSet state counts are inconsistent', { state });
|
|
73
|
+
}
|
|
74
|
+
if ((state.liveEntries === 0) !== (state.nextExpiry === null)) {
|
|
75
|
+
throw invalid('state', 'ExpiringSet next expiry does not match its live count', { state });
|
|
76
|
+
}
|
|
77
|
+
if (state.nextExpiry !== null && state.nextExpiry <= state.updatedAt) {
|
|
78
|
+
throw invalid('state', 'ExpiringSet next expiry must follow its evaluation time', { state });
|
|
79
|
+
}
|
|
80
|
+
return Object.freeze(state);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function createExpiringSetState({ namespace, summary, previous, now }) {
|
|
85
|
+
return Object.freeze({
|
|
86
|
+
version: EXPIRING_SET_METADATA_VERSION,
|
|
87
|
+
namespace: CollectionNamespace.from(namespace).toString(),
|
|
88
|
+
entryCount: summary.entryCount,
|
|
89
|
+
liveEntries: summary.liveEntries,
|
|
90
|
+
expiredEntries: summary.expiredEntries,
|
|
91
|
+
createdAt: previous?.createdAt ?? now,
|
|
92
|
+
updatedAt: now,
|
|
93
|
+
nextExpiry: summary.nextExpiry,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function encode(value) {
|
|
98
|
+
return utf8Encode(JSON.stringify(value, null, 2));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function decodeCanonical(bytes, normalize, kind) {
|
|
102
|
+
let value;
|
|
103
|
+
try {
|
|
104
|
+
value = JSON.parse(utf8Decode(bytes));
|
|
105
|
+
} catch (error) {
|
|
106
|
+
throw invalid(kind, `ExpiringSet ${kind} metadata is not valid JSON`, {
|
|
107
|
+
originalError: error,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const canonical = normalize(value);
|
|
111
|
+
if (utf8Decode(bytes) !== utf8Decode(encode(canonical))) {
|
|
112
|
+
throw invalid(kind, `ExpiringSet ${kind} metadata is not canonical`, { value });
|
|
113
|
+
}
|
|
114
|
+
return canonical;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function assertObject(value, kind) {
|
|
118
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
119
|
+
throw invalid(kind, `ExpiringSet ${kind} metadata must be an object`, { value });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function assertVersion(value, kind) {
|
|
124
|
+
if (value.version !== EXPIRING_SET_METADATA_VERSION) {
|
|
125
|
+
throw invalid(kind, `ExpiringSet ${kind} metadata version is unsupported`, { value });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function assertDigest(value, name) {
|
|
130
|
+
if (typeof value !== 'string' || !DIGEST.test(value)) {
|
|
131
|
+
throw invalid('marker', `Expiring marker ${name} is invalid`, { [name]: value });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function assertTimestamp(value, nullable, kind) {
|
|
136
|
+
if (nullable && value === null) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
assertCanonicalTimestamp(value, {
|
|
140
|
+
invalid: (message, meta) => invalid(kind, message, meta),
|
|
141
|
+
message: `ExpiringSet ${kind} timestamp must be canonical UTC`,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function count(value, name) {
|
|
146
|
+
const result = value[name];
|
|
147
|
+
if (!Number.isSafeInteger(result) || result < 0) {
|
|
148
|
+
throw invalid('state', `ExpiringSet state ${name} must be a non-negative safe integer`, {
|
|
149
|
+
[name]: result,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function invalid(kind, message, meta) {
|
|
156
|
+
const code = kind === 'marker'
|
|
157
|
+
? ErrorCodes.EXPIRING_SET_MARKER_INVALID
|
|
158
|
+
: ErrorCodes.EXPIRING_SET_STATE_INVALID;
|
|
159
|
+
return createCasError(message, code, meta);
|
|
160
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import CollectionNamespace from '../value-objects/CollectionNamespace.js';
|
|
4
|
+
import ExpiringSetRef from '../value-objects/ExpiringSetRef.js';
|
|
5
|
+
import ExpiringSet from './ExpiringSet.js';
|
|
6
|
+
import ExpiringSetIndex from './ExpiringSetIndex.js';
|
|
7
|
+
import RootSet from './RootSet.js';
|
|
8
|
+
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
9
|
+
import RootSetPersistence from './RootSetPersistence.js';
|
|
10
|
+
|
|
11
|
+
const OPEN_OPTIONS = new Set(['namespace', 'retry']);
|
|
12
|
+
|
|
13
|
+
/** Opens managed ExpiringSet instances over shared Git and application services. */
|
|
14
|
+
export default class ExpiringSetRegistry {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
ExpiringSetRegistry.#assertDependencies(options);
|
|
17
|
+
this.persistence = options.persistence;
|
|
18
|
+
this.ref = options.ref;
|
|
19
|
+
this.bundles = options.bundles;
|
|
20
|
+
this.pages = options.pages;
|
|
21
|
+
this.crypto = options.crypto;
|
|
22
|
+
this.clock = options.clock;
|
|
23
|
+
Object.freeze(this);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
open(options = {}) {
|
|
27
|
+
assertOpenOptions(options);
|
|
28
|
+
const namespace = CollectionNamespace.from(options.namespace).toString();
|
|
29
|
+
const ref = ExpiringSetRef.forNamespace(namespace).toString();
|
|
30
|
+
const metadataCodec = new RootSetMetadataCodec({ refType: ExpiringSetRef });
|
|
31
|
+
const persistence = new RootSetPersistence({
|
|
32
|
+
rootSetRef: ref,
|
|
33
|
+
persistence: this.persistence,
|
|
34
|
+
ref: this.ref,
|
|
35
|
+
refType: ExpiringSetRef,
|
|
36
|
+
metadataCodec,
|
|
37
|
+
});
|
|
38
|
+
const rootSet = new RootSet({
|
|
39
|
+
ref,
|
|
40
|
+
persistence,
|
|
41
|
+
retry: options.retry,
|
|
42
|
+
refType: ExpiringSetRef,
|
|
43
|
+
metadataCodec,
|
|
44
|
+
});
|
|
45
|
+
return new ExpiringSet({
|
|
46
|
+
namespace,
|
|
47
|
+
rootSet,
|
|
48
|
+
index: new ExpiringSetIndex({
|
|
49
|
+
bundles: this.bundles,
|
|
50
|
+
pages: this.pages,
|
|
51
|
+
}),
|
|
52
|
+
crypto: this.crypto,
|
|
53
|
+
clock: this.clock,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static #assertDependencies(options) {
|
|
58
|
+
const value = options ?? {};
|
|
59
|
+
const dependencies = [
|
|
60
|
+
['persistence', hasMethods(value.persistence, [
|
|
61
|
+
'writeBlob', 'writeTree', 'readBlob', 'readTree', 'readObjectType',
|
|
62
|
+
])],
|
|
63
|
+
['ref', hasMethods(value.ref, [
|
|
64
|
+
'resolveRef', 'resolveTree', 'resolveParents', 'createCommit', 'updateRef',
|
|
65
|
+
])],
|
|
66
|
+
['bundles', hasMethods(value.bundles, [
|
|
67
|
+
'putOrderedReferences', 'getMemberReference', 'iterateMemberReferences',
|
|
68
|
+
])],
|
|
69
|
+
['pages', hasMethods(value.pages, ['put', 'get'])],
|
|
70
|
+
['crypto', hasMethods(value.crypto, ['sha256'])],
|
|
71
|
+
];
|
|
72
|
+
const missing = dependencies
|
|
73
|
+
.filter(([, available]) => !available)
|
|
74
|
+
.map(([name]) => name);
|
|
75
|
+
if (missing.length > 0) {
|
|
76
|
+
throw createCasError(
|
|
77
|
+
'ExpiringSetRegistry requires complete dependencies',
|
|
78
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
79
|
+
{ missing },
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function assertOpenOptions(options) {
|
|
86
|
+
if (!options || typeof options !== 'object' || Array.isArray(options)) {
|
|
87
|
+
throw createCasError('ExpiringSet open options must be an object', ErrorCodes.INVALID_OPTIONS);
|
|
88
|
+
}
|
|
89
|
+
const unknown = Object.keys(options).filter((key) => !OPEN_OPTIONS.has(key));
|
|
90
|
+
if (unknown.length > 0) {
|
|
91
|
+
throw createCasError(
|
|
92
|
+
'ExpiringSet does not accept capacity or eviction policy',
|
|
93
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
94
|
+
{ unknown },
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function hasMethods(target, names) {
|
|
100
|
+
return names.every((name) => typeof target?.[name] === 'function');
|
|
101
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { concatBytes, isBytes, normalizeByteChunk } from '../bytes/ByteLayout.js';
|
|
2
|
+
import createCasError from '../errors/createCasError.js';
|
|
3
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
4
|
+
import { assertHandleObjectType, mapHandleTargetError } from '../helpers/handleTarget.js';
|
|
5
|
+
import PageHandle from '../value-objects/PageHandle.js';
|
|
6
|
+
import StagedPage from '../value-objects/StagedPage.js';
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_MAX_PAGE_SIZE = 16 * 1024 * 1024;
|
|
9
|
+
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Stores and streams immutable, bounded page blobs.
|
|
13
|
+
*/
|
|
14
|
+
export default class PageService {
|
|
15
|
+
#clock;
|
|
16
|
+
#maxPageSize;
|
|
17
|
+
#persistence;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {object} options
|
|
21
|
+
* @param {import('../../ports/GitPersistencePort.js').default} options.persistence
|
|
22
|
+
* @param {number} [options.maxPageSize]
|
|
23
|
+
* @param {{ now(): Date }} [options.clock]
|
|
24
|
+
*/
|
|
25
|
+
constructor({ persistence, maxPageSize = DEFAULT_MAX_PAGE_SIZE, clock = DEFAULT_CLOCK }) {
|
|
26
|
+
PageService.#assertDependencies(persistence, clock);
|
|
27
|
+
PageService.#assertLimit(maxPageSize, 'Configured max page size');
|
|
28
|
+
this.#persistence = persistence;
|
|
29
|
+
this.#maxPageSize = maxPageSize;
|
|
30
|
+
this.#clock = clock;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {{ source: Uint8Array|Iterable<Uint8Array>|AsyncIterable<Uint8Array>, maxBytes?: number }} options
|
|
35
|
+
* @returns {Promise<StagedPage>}
|
|
36
|
+
*/
|
|
37
|
+
async put({ source, maxBytes }) {
|
|
38
|
+
const limit = this.#effectiveLimit(maxBytes);
|
|
39
|
+
const observedAt = this.#observedAt();
|
|
40
|
+
const bytes = await PageService.#collect(source, limit);
|
|
41
|
+
const oid = await this.#persistence.writeBlob(bytes);
|
|
42
|
+
return new StagedPage({
|
|
43
|
+
handle: new PageHandle({ oid }),
|
|
44
|
+
size: bytes.length,
|
|
45
|
+
observedAt,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {{ handle: PageHandle|string|object }} options
|
|
51
|
+
* @returns {AsyncIterable<Uint8Array>}
|
|
52
|
+
*/
|
|
53
|
+
async *open({ handle: value }) {
|
|
54
|
+
const root = await this.resolveRoot(value);
|
|
55
|
+
try {
|
|
56
|
+
yield* await this.#persistence.readBlobStream(root.oid);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw mapHandleTargetError(error, root.handle);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {{ handle: PageHandle|string|object, maxBytes?: number }} options
|
|
64
|
+
* @returns {Promise<Uint8Array>}
|
|
65
|
+
*/
|
|
66
|
+
async get({ handle, maxBytes }) {
|
|
67
|
+
return await PageService.#collect(this.open({ handle }), this.#effectiveLimit(maxBytes));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {PageHandle|string|object} value
|
|
72
|
+
* @returns {Promise<object>}
|
|
73
|
+
*/
|
|
74
|
+
async resolveRoot(value) {
|
|
75
|
+
const handle = PageHandle.from(value);
|
|
76
|
+
await assertHandleObjectType({
|
|
77
|
+
persistence: this.#persistence,
|
|
78
|
+
handle,
|
|
79
|
+
oid: handle.oid,
|
|
80
|
+
expectedType: 'blob',
|
|
81
|
+
});
|
|
82
|
+
let size;
|
|
83
|
+
try {
|
|
84
|
+
size = await this.#persistence.readObjectSize(handle.oid);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
throw mapHandleTargetError(error, handle);
|
|
87
|
+
}
|
|
88
|
+
if (size > this.#maxPageSize) {
|
|
89
|
+
throw PageService.#tooLarge(size, this.#maxPageSize);
|
|
90
|
+
}
|
|
91
|
+
return Object.freeze({ handle, oid: handle.oid, type: 'blob', size });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#effectiveLimit(value) {
|
|
95
|
+
if (value === undefined) {
|
|
96
|
+
return this.#maxPageSize;
|
|
97
|
+
}
|
|
98
|
+
PageService.#assertLimit(value, 'Page maxBytes');
|
|
99
|
+
if (value > this.#maxPageSize) {
|
|
100
|
+
throw createCasError(
|
|
101
|
+
'Page maxBytes cannot exceed the configured maximum',
|
|
102
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
103
|
+
{ maxBytes: value, configuredMaxPageSize: this.#maxPageSize }
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#observedAt() {
|
|
110
|
+
const now = this.#clock.now();
|
|
111
|
+
if (!(now instanceof Date) || Number.isNaN(now.getTime())) {
|
|
112
|
+
throw createCasError('PageService clock returned an invalid Date', ErrorCodes.INVALID_OPTIONS);
|
|
113
|
+
}
|
|
114
|
+
return now.toISOString();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static async #collect(source, maxBytes) {
|
|
118
|
+
if (isBytes(source)) {
|
|
119
|
+
if (source.length > maxBytes) {
|
|
120
|
+
throw PageService.#tooLarge(source.length, maxBytes);
|
|
121
|
+
}
|
|
122
|
+
return new Uint8Array(source);
|
|
123
|
+
}
|
|
124
|
+
const iterator = source?.[Symbol.asyncIterator] ?? source?.[Symbol.iterator];
|
|
125
|
+
if (typeof iterator !== 'function') {
|
|
126
|
+
throw createCasError(
|
|
127
|
+
'Page source must be bytes or an iterable of byte chunks',
|
|
128
|
+
ErrorCodes.INVALID_OPTIONS
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const chunks = [];
|
|
132
|
+
let total = 0;
|
|
133
|
+
for await (const chunk of source) {
|
|
134
|
+
let bytes;
|
|
135
|
+
try {
|
|
136
|
+
bytes = normalizeByteChunk(chunk);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
throw createCasError('Page source yielded a non-byte chunk', ErrorCodes.INVALID_OPTIONS, {
|
|
139
|
+
originalError: error,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
total += bytes.length;
|
|
143
|
+
if (total > maxBytes) {
|
|
144
|
+
throw PageService.#tooLarge(total, maxBytes);
|
|
145
|
+
}
|
|
146
|
+
chunks.push(bytes);
|
|
147
|
+
}
|
|
148
|
+
return concatBytes(chunks, total);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static #tooLarge(observedBytes, maxBytes) {
|
|
152
|
+
return createCasError('Page exceeds its configured byte limit', ErrorCodes.PAGE_TOO_LARGE, {
|
|
153
|
+
observedBytes,
|
|
154
|
+
maxBytes,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static #assertLimit(value, label) {
|
|
159
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
160
|
+
throw createCasError(`${label} must be a non-negative safe integer`, ErrorCodes.INVALID_OPTIONS, {
|
|
161
|
+
value,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
static #assertDependencies(persistence, clock) {
|
|
167
|
+
const methods = ['writeBlob', 'readBlobStream', 'readObjectType', 'readObjectSize'];
|
|
168
|
+
if (!persistence || methods.some((method) => typeof persistence[method] !== 'function')) {
|
|
169
|
+
throw createCasError('PageService requires a complete persistence port', ErrorCodes.INVALID_OPTIONS);
|
|
170
|
+
}
|
|
171
|
+
if (!clock || typeof clock.now !== 'function') {
|
|
172
|
+
throw createCasError('PageService clock must provide now()', ErrorCodes.INVALID_OPTIONS);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|