@git-stunts/git-cas 6.2.0 → 6.4.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 +16 -8
- package/CHANGELOG.md +63 -0
- package/README.md +20 -4
- package/UPGRADING.md +74 -0
- package/build-info.json +2 -2
- package/docs/API.md +283 -21
- package/docs/releases/v6.3.0.md +96 -0
- package/docs/releases/v6.4.0.md +128 -0
- package/index.d.ts +286 -2
- package/index.js +28 -0
- package/package.json +3 -1
- package/src/domain/errors/Codes.js +14 -0
- package/src/domain/services/CacheAcquisitionInventory.js +123 -0
- package/src/domain/services/CacheAcquisitionRegistry.js +188 -0
- package/src/domain/services/CacheSet.js +56 -5
- package/src/domain/services/CacheSetRegistry.js +7 -0
- package/src/domain/services/RepositoryDoctor.js +127 -5
- package/src/domain/services/RootSetPersistence.js +34 -2
- package/src/domain/services/StagingWorkspace.js +530 -0
- package/src/domain/services/StagingWorkspaceRegistry.js +545 -0
- package/src/domain/services/WorkspaceDescriptorCodec.js +144 -0
- package/src/domain/value-objects/CacheAcquisition.js +88 -0
- package/src/domain/value-objects/CacheAcquisitionRef.js +129 -0
- package/src/domain/value-objects/RetentionWitness.js +6 -1
- package/src/domain/value-objects/WorkspaceRef.js +151 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +354 -2
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +28 -10
- package/src/package-version.js +1 -1
- package/src/ports/GitRefPort.js +46 -0
- package/src/ports/RepositoryInspectionPort.js +5 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@git-stunts/git-cas",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.4.0",
|
|
4
4
|
"description": "Content-addressed storage backed by Git's object database, with optional encryption and pluggable codecs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -34,6 +34,8 @@
|
|
|
34
34
|
"docs/releases/v6.0.0.md",
|
|
35
35
|
"docs/releases/v6.1.0.md",
|
|
36
36
|
"docs/releases/v6.2.0.md",
|
|
37
|
+
"docs/releases/v6.3.0.md",
|
|
38
|
+
"docs/releases/v6.4.0.md",
|
|
37
39
|
"docs/THREAT_MODEL.md",
|
|
38
40
|
"docs/VAULT_INTERNALS.md",
|
|
39
41
|
"docs/WALKTHROUGH.md",
|
|
@@ -11,6 +11,9 @@ const ErrorCodes = Object.freeze({
|
|
|
11
11
|
BUNDLE_MEMBER_ORDER: 'BUNDLE_MEMBER_ORDER',
|
|
12
12
|
BUNDLE_PATH_INVALID: 'BUNDLE_PATH_INVALID',
|
|
13
13
|
BUNDLE_PATH_LIMIT: 'BUNDLE_PATH_LIMIT',
|
|
14
|
+
CACHE_ACQUISITION_CONFLICT: 'CACHE_ACQUISITION_CONFLICT',
|
|
15
|
+
CACHE_ACQUISITION_INVALID: 'CACHE_ACQUISITION_INVALID',
|
|
16
|
+
CACHE_ACQUISITION_RELEASE_CONFLICT: 'CACHE_ACQUISITION_RELEASE_CONFLICT',
|
|
14
17
|
CACHE_CONFLICT: 'CACHE_CONFLICT',
|
|
15
18
|
CACHE_ENTRY_INVALID: 'CACHE_ENTRY_INVALID',
|
|
16
19
|
CACHE_KEY_INVALID: 'CACHE_KEY_INVALID',
|
|
@@ -29,6 +32,7 @@ const ErrorCodes = Object.freeze({
|
|
|
29
32
|
GIT_ERROR: 'GIT_ERROR',
|
|
30
33
|
GIT_OBJECT_NOT_FOUND: 'GIT_OBJECT_NOT_FOUND',
|
|
31
34
|
GIT_PLUMBING_INITIALIZATION_FAILED: 'GIT_PLUMBING_INITIALIZATION_FAILED',
|
|
35
|
+
GIT_REF_CONFLICT: 'GIT_REF_CONFLICT',
|
|
32
36
|
GIT_REF_NOT_FOUND: 'GIT_REF_NOT_FOUND',
|
|
33
37
|
HANDLE_CODEC_MISMATCH: 'HANDLE_CODEC_MISMATCH',
|
|
34
38
|
HANDLE_INVALID: 'HANDLE_INVALID',
|
|
@@ -96,6 +100,16 @@ const ErrorCodes = Object.freeze({
|
|
|
96
100
|
VAULT_REF_MISSING: 'VAULT_REF_MISSING',
|
|
97
101
|
VAULT_REF_UPDATE_FAILED: 'VAULT_REF_UPDATE_FAILED',
|
|
98
102
|
VAULT_RETRY_POLICY_INVALID: 'VAULT_RETRY_POLICY_INVALID',
|
|
103
|
+
WORKSPACE_CONFLICT: 'WORKSPACE_CONFLICT',
|
|
104
|
+
WORKSPACE_DESCRIPTOR_INVALID: 'WORKSPACE_DESCRIPTOR_INVALID',
|
|
105
|
+
WORKSPACE_HANDLE_NOT_RETAINED: 'WORKSPACE_HANDLE_NOT_RETAINED',
|
|
106
|
+
WORKSPACE_PROMOTION_CLEANUP_PENDING: 'WORKSPACE_PROMOTION_CLEANUP_PENDING',
|
|
107
|
+
WORKSPACE_PROMOTION_NOT_RETAINED: 'WORKSPACE_PROMOTION_NOT_RETAINED',
|
|
108
|
+
WORKSPACE_REF_INVALID: 'WORKSPACE_REF_INVALID',
|
|
109
|
+
WORKSPACE_RELEASED: 'WORKSPACE_RELEASED',
|
|
110
|
+
WORKSPACE_RETENTION_FAILED: 'WORKSPACE_RETENTION_FAILED',
|
|
111
|
+
WORKSPACE_STATE_INVALID: 'WORKSPACE_STATE_INVALID',
|
|
112
|
+
WORKSPACE_TTL_INVALID: 'WORKSPACE_TTL_INVALID',
|
|
99
113
|
});
|
|
100
114
|
|
|
101
115
|
export { ErrorCodes };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import CacheAcquisitionRef from '../value-objects/CacheAcquisitionRef.js';
|
|
4
|
+
import Oid from '../value-objects/Oid.js';
|
|
5
|
+
|
|
6
|
+
export function createCacheAcquisitionInventory(detailLimit) {
|
|
7
|
+
return {
|
|
8
|
+
observed: 0,
|
|
9
|
+
inspected: 0,
|
|
10
|
+
healthy: true,
|
|
11
|
+
detailLimit,
|
|
12
|
+
entries: [],
|
|
13
|
+
hasUnknownAge: false,
|
|
14
|
+
totals: {
|
|
15
|
+
activeCount: 0,
|
|
16
|
+
oldestAcquiredAt: null,
|
|
17
|
+
newestAcquiredAt: null,
|
|
18
|
+
maxAgeMs: null,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function recordCacheAcquisition(inventory, record, observedAt) {
|
|
24
|
+
inventory.observed += 1;
|
|
25
|
+
inventory.inspected += 1;
|
|
26
|
+
inventory.totals.activeCount += 1;
|
|
27
|
+
let entry;
|
|
28
|
+
try {
|
|
29
|
+
assertDirectRefEvidence(record);
|
|
30
|
+
const acquisitionRef = CacheAcquisitionRef.from(record.ref);
|
|
31
|
+
const ageMs = Date.parse(observedAt) - Date.parse(acquisitionRef.acquiredAt);
|
|
32
|
+
if (!Number.isSafeInteger(ageMs)) {
|
|
33
|
+
throw createCasError(
|
|
34
|
+
'Cache acquisition age cannot be represented safely',
|
|
35
|
+
ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
36
|
+
{ ref: record.ref, acquiredAt: acquisitionRef.acquiredAt, observedAt },
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const clockSkewed = ageMs < 0;
|
|
40
|
+
entry = {
|
|
41
|
+
id: acquisitionRef.id,
|
|
42
|
+
namespace: acquisitionRef.namespace,
|
|
43
|
+
generation: Oid.from(record.oid).toString(),
|
|
44
|
+
acquiredAt: acquisitionRef.acquiredAt,
|
|
45
|
+
ageMs: clockSkewed ? null : ageMs,
|
|
46
|
+
healthy: true,
|
|
47
|
+
issues: clockSkewed ? [{
|
|
48
|
+
code: 'CACHE_ACQUISITION_CLOCK_SKEW',
|
|
49
|
+
message: 'Cache acquisition time is later than repository inspection',
|
|
50
|
+
acquiredAt: acquisitionRef.acquiredAt,
|
|
51
|
+
observedAt,
|
|
52
|
+
}] : [],
|
|
53
|
+
};
|
|
54
|
+
updateTotals(inventory, entry);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
inventory.healthy = false;
|
|
57
|
+
entry = {
|
|
58
|
+
ref: record.ref,
|
|
59
|
+
generation: record.oid,
|
|
60
|
+
healthy: false,
|
|
61
|
+
issues: [{
|
|
62
|
+
code: error?.code ?? ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
63
|
+
message: error instanceof Error ? error.message : String(error),
|
|
64
|
+
}],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (inventory.entries.length < inventory.detailLimit) {
|
|
68
|
+
inventory.entries.push(entry);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function assertDirectRefEvidence(record) {
|
|
73
|
+
if (record.symref === undefined) {
|
|
74
|
+
throw createCasError(
|
|
75
|
+
'Cache acquisition direct-ref evidence is unavailable',
|
|
76
|
+
ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
77
|
+
{ ref: record.ref },
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
if (record.symref !== null) {
|
|
81
|
+
throw createCasError(
|
|
82
|
+
'Symbolic cache acquisition refs are unsafe',
|
|
83
|
+
ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
84
|
+
{ ref: record.ref, symref: record.symref },
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function cacheAcquisitionGroup(inventory) {
|
|
90
|
+
return {
|
|
91
|
+
healthy: inventory.healthy,
|
|
92
|
+
coverage: {
|
|
93
|
+
observed: inventory.observed,
|
|
94
|
+
inspected: inventory.inspected,
|
|
95
|
+
detailed: inventory.entries.length,
|
|
96
|
+
complete: inventory.observed === inventory.inspected,
|
|
97
|
+
},
|
|
98
|
+
totals: inventory.totals,
|
|
99
|
+
entries: inventory.entries,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function updateTotals(inventory, entry) {
|
|
104
|
+
const { totals } = inventory;
|
|
105
|
+
totals.oldestAcquiredAt = earlier(totals.oldestAcquiredAt, entry.acquiredAt);
|
|
106
|
+
totals.newestAcquiredAt = later(totals.newestAcquiredAt, entry.acquiredAt);
|
|
107
|
+
if (entry.ageMs === null) {
|
|
108
|
+
inventory.hasUnknownAge = true;
|
|
109
|
+
totals.maxAgeMs = null;
|
|
110
|
+
} else if (!inventory.hasUnknownAge) {
|
|
111
|
+
totals.maxAgeMs = totals.maxAgeMs === null
|
|
112
|
+
? entry.ageMs
|
|
113
|
+
: Math.max(totals.maxAgeMs, entry.ageMs);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function earlier(current, candidate) {
|
|
118
|
+
return current === null || candidate < current ? candidate : current;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function later(current, candidate) {
|
|
122
|
+
return current === null || candidate > current ? candidate : current;
|
|
123
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import CacheAcquisition from '../value-objects/CacheAcquisition.js';
|
|
4
|
+
import CacheAcquisitionRef from '../value-objects/CacheAcquisitionRef.js';
|
|
5
|
+
import CollectionNamespace from '../value-objects/CollectionNamespace.js';
|
|
6
|
+
import Oid from '../value-objects/Oid.js';
|
|
7
|
+
import RetentionWitness from '../value-objects/RetentionWitness.js';
|
|
8
|
+
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
9
|
+
|
|
10
|
+
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
11
|
+
|
|
12
|
+
/** Creates and releases git-cas-managed cache-generation anchors. */
|
|
13
|
+
export default class CacheAcquisitionRegistry {
|
|
14
|
+
constructor({ ref, crypto, clock = DEFAULT_CLOCK }) {
|
|
15
|
+
this.ref = ref;
|
|
16
|
+
this.crypto = crypto;
|
|
17
|
+
this.clock = clock;
|
|
18
|
+
Object.freeze(this);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async acquire({ namespace, cacheRef, keyDigest, generation, hit }) {
|
|
22
|
+
this.#assertAcquireCapabilities();
|
|
23
|
+
const acquiredAt = this.#now();
|
|
24
|
+
const acquisitionRef = CacheAcquisitionRef.create({
|
|
25
|
+
namespace,
|
|
26
|
+
keyDigest,
|
|
27
|
+
acquiredAt,
|
|
28
|
+
nonce: bytesToHex(await this.crypto.randomBytes(16)),
|
|
29
|
+
});
|
|
30
|
+
const anchored = await this.ref.anchorRef({
|
|
31
|
+
sourceRef: cacheRef,
|
|
32
|
+
expectedSourceOid: generation,
|
|
33
|
+
targetRef: acquisitionRef.toString(),
|
|
34
|
+
});
|
|
35
|
+
if (!anchored) {
|
|
36
|
+
throw createCasError(
|
|
37
|
+
'Cache generation changed before it could be acquired',
|
|
38
|
+
ErrorCodes.CACHE_ACQUISITION_CONFLICT,
|
|
39
|
+
{ cacheRef, generation },
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
const evidence = new RetentionWitness({
|
|
43
|
+
handle: hit.handle,
|
|
44
|
+
policy: 'pinned',
|
|
45
|
+
reachability: 'anchored',
|
|
46
|
+
root: {
|
|
47
|
+
kind: 'cache-set',
|
|
48
|
+
namespace,
|
|
49
|
+
ref: acquisitionRef.toString(),
|
|
50
|
+
generation,
|
|
51
|
+
path: RootSetMetadataCodec.slotFor(0),
|
|
52
|
+
},
|
|
53
|
+
observedAt: acquiredAt,
|
|
54
|
+
});
|
|
55
|
+
return new CacheAcquisition({
|
|
56
|
+
id: acquisitionRef.id,
|
|
57
|
+
hit,
|
|
58
|
+
evidence,
|
|
59
|
+
acquiredAt,
|
|
60
|
+
release: () => this.release({
|
|
61
|
+
namespace,
|
|
62
|
+
id: acquisitionRef.id,
|
|
63
|
+
expectedGeneration: generation,
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async inspect({ namespace: value, limit = 100 }) {
|
|
69
|
+
assertMethod(
|
|
70
|
+
this.ref,
|
|
71
|
+
'iterateRefs',
|
|
72
|
+
'Cache acquisition inspection requires ref iteration',
|
|
73
|
+
);
|
|
74
|
+
const namespace = CollectionNamespace.from(value).toString();
|
|
75
|
+
assertInspectionLimit(limit);
|
|
76
|
+
const prefix = CacheAcquisitionRef.prefixForNamespace(namespace);
|
|
77
|
+
const entries = [];
|
|
78
|
+
for await (const record of this.ref.iterateRefs({ prefix, limit: limit + 1 })) {
|
|
79
|
+
if (record.symref !== null) {
|
|
80
|
+
throw invalid('Symbolic cache acquisition refs are unsafe', {
|
|
81
|
+
ref: record.ref,
|
|
82
|
+
symref: record.symref,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const acquisitionRef = CacheAcquisitionRef.from(record.ref);
|
|
86
|
+
if (acquisitionRef.namespace !== namespace) {
|
|
87
|
+
throw invalid('Cache acquisition iterator escaped its requested namespace', {
|
|
88
|
+
namespace,
|
|
89
|
+
ref: record.ref,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
entries.push(Object.freeze({
|
|
93
|
+
id: acquisitionRef.id,
|
|
94
|
+
generation: Oid.from(record.oid).toString(),
|
|
95
|
+
acquiredAt: acquisitionRef.acquiredAt,
|
|
96
|
+
keyDigest: acquisitionRef.keyDigest,
|
|
97
|
+
}));
|
|
98
|
+
if (entries.length > limit) {
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const truncated = entries.length > limit;
|
|
103
|
+
if (truncated) {
|
|
104
|
+
entries.pop();
|
|
105
|
+
}
|
|
106
|
+
return Object.freeze({
|
|
107
|
+
namespace,
|
|
108
|
+
entries: Object.freeze(entries),
|
|
109
|
+
truncated,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async release({ namespace, id, expectedGeneration }) {
|
|
114
|
+
assertMethod(
|
|
115
|
+
this.ref,
|
|
116
|
+
'deleteRef',
|
|
117
|
+
'Cache acquisition release requires checked ref deletion',
|
|
118
|
+
);
|
|
119
|
+
const acquisitionRef = CacheAcquisitionRef.forId({ namespace, id });
|
|
120
|
+
const generation = Oid.from(expectedGeneration).toString();
|
|
121
|
+
let changed;
|
|
122
|
+
try {
|
|
123
|
+
changed = await this.ref.deleteRef({
|
|
124
|
+
ref: acquisitionRef.toString(),
|
|
125
|
+
expectedOldOid: generation,
|
|
126
|
+
});
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error?.code !== ErrorCodes.GIT_REF_CONFLICT) {
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
throw createCasError(
|
|
132
|
+
'Cache acquisition release could not prove the expected direct generation',
|
|
133
|
+
ErrorCodes.CACHE_ACQUISITION_RELEASE_CONFLICT,
|
|
134
|
+
{ id: acquisitionRef.id, generation, originalError: error },
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
return Object.freeze({
|
|
138
|
+
id: acquisitionRef.id,
|
|
139
|
+
generation,
|
|
140
|
+
changed,
|
|
141
|
+
releasedAt: this.#now(),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#now() {
|
|
146
|
+
const value = this.clock.now();
|
|
147
|
+
if (!(value instanceof Date) || Number.isNaN(value.getTime())) {
|
|
148
|
+
throw invalid('Cache acquisition clock returned an invalid Date');
|
|
149
|
+
}
|
|
150
|
+
return value.toISOString();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#assertAcquireCapabilities() {
|
|
154
|
+
assertMethod(
|
|
155
|
+
this.ref,
|
|
156
|
+
'anchorRef',
|
|
157
|
+
'Cache acquisition requires atomic ref anchoring',
|
|
158
|
+
);
|
|
159
|
+
assertMethod(
|
|
160
|
+
this.crypto,
|
|
161
|
+
'randomBytes',
|
|
162
|
+
'Cache acquisition requires random bytes',
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function assertInspectionLimit(limit) {
|
|
168
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1000) {
|
|
169
|
+
throw invalid('Cache acquisition inspection limit must be between 1 and 1000', { limit });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function bytesToHex(bytes) {
|
|
174
|
+
if (!(bytes instanceof Uint8Array) || bytes.length !== 16) {
|
|
175
|
+
throw invalid('Cache acquisition random source returned invalid bytes');
|
|
176
|
+
}
|
|
177
|
+
return [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function invalid(message, meta) {
|
|
181
|
+
return createCasError(message, ErrorCodes.CACHE_ACQUISITION_INVALID, meta);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function assertMethod(target, name, message) {
|
|
185
|
+
if (typeof target?.[name] !== 'function') {
|
|
186
|
+
throw invalid(message, { capability: name });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -14,9 +14,11 @@ import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
|
14
14
|
|
|
15
15
|
const RETENTION = Object.freeze(['pinned', 'evictable']);
|
|
16
16
|
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
17
|
+
const MAX_ACQUISITION_ATTEMPTS = 5;
|
|
17
18
|
|
|
18
19
|
/** RootSet-backed lifecycle manager for application cache handles. */
|
|
19
20
|
export default class CacheSet {
|
|
21
|
+
#acquisitions;
|
|
20
22
|
#clock;
|
|
21
23
|
#configuredPolicy;
|
|
22
24
|
#crypto;
|
|
@@ -29,6 +31,7 @@ export default class CacheSet {
|
|
|
29
31
|
constructor(options) {
|
|
30
32
|
CacheSet.#assertDependencies(options);
|
|
31
33
|
this.#namespace = options.namespace;
|
|
34
|
+
this.#acquisitions = options.acquisitions;
|
|
32
35
|
this.#rootSet = options.rootSet;
|
|
33
36
|
this.#index = options.index;
|
|
34
37
|
this.#resolveHandle = options.resolveHandle;
|
|
@@ -40,6 +43,48 @@ export default class CacheSet {
|
|
|
40
43
|
Object.freeze(this);
|
|
41
44
|
}
|
|
42
45
|
|
|
46
|
+
async acquire(keyValue) {
|
|
47
|
+
const key = CacheKey.from(keyValue).toString();
|
|
48
|
+
const digest = await this.#digest(key);
|
|
49
|
+
for (let attempt = 1; attempt <= MAX_ACQUISITION_ATTEMPTS; attempt += 1) {
|
|
50
|
+
const state = await this.#rootSet.read();
|
|
51
|
+
const handle = this.#index.fromRootEntries(state.entries);
|
|
52
|
+
const entry = await this.#index.getEntry(handle, digest);
|
|
53
|
+
const now = this.#now();
|
|
54
|
+
assertNoKeyCollision(entry, key);
|
|
55
|
+
if (!entry || isExpired(entry.metadata, now)) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const hit = this.#hit(entry, state.headOid, now);
|
|
59
|
+
try {
|
|
60
|
+
return await this.#acquisitions.acquire({
|
|
61
|
+
namespace: this.#namespace,
|
|
62
|
+
cacheRef: this.ref,
|
|
63
|
+
keyDigest: digest,
|
|
64
|
+
generation: state.headOid,
|
|
65
|
+
hit,
|
|
66
|
+
});
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (error?.code !== ErrorCodes.CACHE_ACQUISITION_CONFLICT) {
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
throw createCasError(
|
|
74
|
+
'Cache generation changed throughout every acquisition attempt',
|
|
75
|
+
ErrorCodes.CACHE_ACQUISITION_CONFLICT,
|
|
76
|
+
{ ref: this.ref, key, attempts: MAX_ACQUISITION_ATTEMPTS },
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async inspectAcquisitions(options = {}) {
|
|
81
|
+
return await this.#acquisitions.inspect({ ...options, namespace: this.#namespace });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async releaseAcquisition(options) {
|
|
85
|
+
return await this.#acquisitions.release({ ...options, namespace: this.#namespace });
|
|
86
|
+
}
|
|
87
|
+
|
|
43
88
|
async get(keyValue) {
|
|
44
89
|
const key = CacheKey.from(keyValue).toString();
|
|
45
90
|
const digest = await this.#digest(key);
|
|
@@ -533,14 +578,16 @@ export default class CacheSet {
|
|
|
533
578
|
}
|
|
534
579
|
|
|
535
580
|
static #assertDependencies(options) {
|
|
581
|
+
const value = options ?? {};
|
|
536
582
|
const dependencies = [
|
|
537
|
-
['rootSet',
|
|
538
|
-
['
|
|
539
|
-
['
|
|
540
|
-
['
|
|
583
|
+
['rootSet', hasMethod(value.rootSet, 'mutate')],
|
|
584
|
+
['acquisitions', hasMethod(value.acquisitions, 'acquire')],
|
|
585
|
+
['index', hasMethod(value.index, 'scan')],
|
|
586
|
+
['resolveHandle', typeof value.resolveHandle === 'function'],
|
|
587
|
+
['crypto', hasMethod(value.crypto, 'sha256')],
|
|
541
588
|
];
|
|
542
589
|
const missing = dependencies
|
|
543
|
-
.filter(([,
|
|
590
|
+
.filter(([, available]) => !available)
|
|
544
591
|
.map(([name]) => name);
|
|
545
592
|
if (missing.length > 0) {
|
|
546
593
|
throw createCasError('CacheSet requires complete lifecycle dependencies', ErrorCodes.INVALID_OPTIONS, {
|
|
@@ -550,6 +597,10 @@ export default class CacheSet {
|
|
|
550
597
|
}
|
|
551
598
|
}
|
|
552
599
|
|
|
600
|
+
function hasMethod(target, name) {
|
|
601
|
+
return typeof target?.[name] === 'function';
|
|
602
|
+
}
|
|
603
|
+
|
|
553
604
|
function canReplace({ previous, key, expectedHandle, requireExisting }) {
|
|
554
605
|
if (!previous || previous.metadata.key !== key) {
|
|
555
606
|
return !requireExisting && expectedHandle === null;
|
|
@@ -4,6 +4,7 @@ import CollectionNamespace from '../value-objects/CollectionNamespace.js';
|
|
|
4
4
|
import createCasError from '../errors/createCasError.js';
|
|
5
5
|
import { ErrorCodes } from '../errors/index.js';
|
|
6
6
|
import CacheIndex from './CacheIndex.js';
|
|
7
|
+
import CacheAcquisitionRegistry from './CacheAcquisitionRegistry.js';
|
|
7
8
|
import CacheSet from './CacheSet.js';
|
|
8
9
|
import RootSet from './RootSet.js';
|
|
9
10
|
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
@@ -20,6 +21,11 @@ export default class CacheSetRegistry {
|
|
|
20
21
|
this.resolveHandle = options.resolveHandle;
|
|
21
22
|
this.crypto = options.crypto;
|
|
22
23
|
this.clock = options.clock;
|
|
24
|
+
this.acquisitions = new CacheAcquisitionRegistry({
|
|
25
|
+
ref: this.ref,
|
|
26
|
+
crypto: this.crypto,
|
|
27
|
+
clock: this.clock,
|
|
28
|
+
});
|
|
23
29
|
Object.freeze(this);
|
|
24
30
|
}
|
|
25
31
|
|
|
@@ -51,6 +57,7 @@ export default class CacheSetRegistry {
|
|
|
51
57
|
crypto: this.crypto,
|
|
52
58
|
}),
|
|
53
59
|
resolveHandle: this.resolveHandle,
|
|
60
|
+
acquisitions: this.acquisitions,
|
|
54
61
|
crypto: this.crypto,
|
|
55
62
|
clock: this.clock,
|
|
56
63
|
});
|