@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
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import parseApplicationHandle from '../value-objects/ApplicationHandle.js';
|
|
4
|
+
import RetentionWitness from '../value-objects/RetentionWitness.js';
|
|
5
|
+
import WorkspaceRef from '../value-objects/WorkspaceRef.js';
|
|
6
|
+
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
7
|
+
import {
|
|
8
|
+
MAX_WORKSPACE_TARGETS,
|
|
9
|
+
WORKSPACE_DESCRIPTOR_ENTRY,
|
|
10
|
+
} from './WorkspaceDescriptorCodec.js';
|
|
11
|
+
|
|
12
|
+
const TARGET_ENTRY_PREFIX = 'target:';
|
|
13
|
+
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
14
|
+
|
|
15
|
+
/** Resource scope that retains every returned staged handle until promotion or release. */
|
|
16
|
+
export default class StagingWorkspace {
|
|
17
|
+
#assets;
|
|
18
|
+
#bundles;
|
|
19
|
+
#clock;
|
|
20
|
+
#descriptorCodec;
|
|
21
|
+
#expiresAt = null;
|
|
22
|
+
#generation = null;
|
|
23
|
+
#pages;
|
|
24
|
+
#publications;
|
|
25
|
+
#refs;
|
|
26
|
+
#released = false;
|
|
27
|
+
#resolveHandle;
|
|
28
|
+
#rootSet;
|
|
29
|
+
#tail = Promise.resolve();
|
|
30
|
+
#targets = new Map();
|
|
31
|
+
#ttlMs;
|
|
32
|
+
#workspaceRef;
|
|
33
|
+
|
|
34
|
+
constructor({ workspaceRef, ttlMs, rootSet, refs, assets, pages, bundles, publications,
|
|
35
|
+
resolveHandle, descriptorCodec, clock = DEFAULT_CLOCK }) {
|
|
36
|
+
StagingWorkspace.#assertDependencies({
|
|
37
|
+
rootSet,
|
|
38
|
+
refs,
|
|
39
|
+
assets,
|
|
40
|
+
pages,
|
|
41
|
+
bundles,
|
|
42
|
+
resolveHandle,
|
|
43
|
+
descriptorCodec,
|
|
44
|
+
clock,
|
|
45
|
+
});
|
|
46
|
+
this.#workspaceRef = WorkspaceRef.from(workspaceRef);
|
|
47
|
+
this.#ttlMs = ttlMs;
|
|
48
|
+
this.#rootSet = rootSet;
|
|
49
|
+
this.#refs = refs;
|
|
50
|
+
this.#assets = assets;
|
|
51
|
+
this.#pages = pages;
|
|
52
|
+
this.#bundles = bundles;
|
|
53
|
+
this.#publications = publications;
|
|
54
|
+
this.#resolveHandle = resolveHandle;
|
|
55
|
+
this.#descriptorCodec = descriptorCodec;
|
|
56
|
+
this.#clock = clock;
|
|
57
|
+
|
|
58
|
+
this.assets = Object.freeze({
|
|
59
|
+
put: (options) => this.#enqueue(() => this.#stage(this.#assets, 'put', options)),
|
|
60
|
+
adopt: (options) => this.#enqueue(() => this.#stage(this.#assets, 'adopt', options)),
|
|
61
|
+
});
|
|
62
|
+
this.pages = Object.freeze({
|
|
63
|
+
put: (options) => this.#enqueue(() => this.#stage(this.#pages, 'put', options)),
|
|
64
|
+
});
|
|
65
|
+
this.bundles = Object.freeze({
|
|
66
|
+
put: (options) => this.#enqueue(() => this.#stage(this.#bundles, 'put', options)),
|
|
67
|
+
putOrdered: (options) => (
|
|
68
|
+
this.#enqueue(() => this.#stage(this.#bundles, 'putOrdered', options))
|
|
69
|
+
),
|
|
70
|
+
});
|
|
71
|
+
Object.freeze(this);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get id() {
|
|
75
|
+
return this.#workspaceRef.id;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get namespace() {
|
|
79
|
+
return this.#workspaceRef.namespace;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get createdAt() {
|
|
83
|
+
return this.#workspaceRef.createdAt;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
get expiresAt() {
|
|
87
|
+
return this.#expiresAt;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
checkpoint({ handles }) {
|
|
91
|
+
return this.#enqueue(async () => {
|
|
92
|
+
this.#assertActive();
|
|
93
|
+
const targets = await this.#resolveTargets(handles);
|
|
94
|
+
return await this.#install(targets);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
renew() {
|
|
99
|
+
return this.#enqueue(async () => {
|
|
100
|
+
this.#assertActive();
|
|
101
|
+
return await this.#install([...this.#targets.values()]);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
promoteToCache({ cache, key, handle, options = {} }) {
|
|
106
|
+
return this.#enqueue(async () => {
|
|
107
|
+
this.#assertActive();
|
|
108
|
+
if (!cache || typeof cache.put !== 'function' || typeof cache.ref !== 'string') {
|
|
109
|
+
throw createCasError(
|
|
110
|
+
'Workspace cache promotion requires a CacheSet with ref and put()',
|
|
111
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
const target = this.#retainedTarget(handle);
|
|
115
|
+
await this.#install([...this.#targets.values()]);
|
|
116
|
+
const destination = await cache.put(key, target.handle, options);
|
|
117
|
+
this.#assertPromotionDestination({
|
|
118
|
+
destination,
|
|
119
|
+
target,
|
|
120
|
+
operation: 'cache',
|
|
121
|
+
rootKind: 'cache-set',
|
|
122
|
+
ref: cache.ref,
|
|
123
|
+
generation: destination?.generation,
|
|
124
|
+
confirmed: destination?.accepted === true,
|
|
125
|
+
});
|
|
126
|
+
return await this.#finishPromotion(destination);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
promoteToPublication({ handle, commit, ref }) {
|
|
131
|
+
return this.#enqueue(async () => {
|
|
132
|
+
this.#assertActive();
|
|
133
|
+
if (!this.#publications || typeof this.#publications.commit !== 'function') {
|
|
134
|
+
throw createCasError(
|
|
135
|
+
'Workspace publication promotion is unavailable',
|
|
136
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
const target = this.#retainedTarget(handle);
|
|
140
|
+
await this.#install([...this.#targets.values()]);
|
|
141
|
+
const destination = await this.#publications.commit({
|
|
142
|
+
root: target.handle,
|
|
143
|
+
commit,
|
|
144
|
+
ref,
|
|
145
|
+
});
|
|
146
|
+
this.#assertPromotionDestination({
|
|
147
|
+
destination,
|
|
148
|
+
target,
|
|
149
|
+
operation: 'publication',
|
|
150
|
+
rootKind: 'publication',
|
|
151
|
+
ref: ref.name,
|
|
152
|
+
generation: destination?.commitId,
|
|
153
|
+
confirmed: destination?.operation === 'publication',
|
|
154
|
+
});
|
|
155
|
+
return await this.#finishPromotion(destination);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
release() {
|
|
160
|
+
return this.#enqueue(() => this.#releaseExact());
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async #stage(service, method, options) {
|
|
164
|
+
this.#assertActive();
|
|
165
|
+
const staged = await service[method](options);
|
|
166
|
+
if (!staged?.handle) {
|
|
167
|
+
throw createCasError(
|
|
168
|
+
`Workspace ${method}() did not return a staged handle`,
|
|
169
|
+
ErrorCodes.WORKSPACE_STATE_INVALID,
|
|
170
|
+
{ method },
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
const target = await this.#resolveTarget(staged.handle);
|
|
175
|
+
const targets = new Map(this.#targets);
|
|
176
|
+
targets.set(target.handle.toString(), target);
|
|
177
|
+
const installation = await this.#install([...targets.values()]);
|
|
178
|
+
const witness = installation.witnesses.find(
|
|
179
|
+
(candidate) => candidate.handle.toString() === target.handle.toString(),
|
|
180
|
+
);
|
|
181
|
+
if (!witness) {
|
|
182
|
+
throw createCasError(
|
|
183
|
+
'Workspace generation omitted the newly staged handle',
|
|
184
|
+
ErrorCodes.WORKSPACE_STATE_INVALID,
|
|
185
|
+
{ handle: target.handle.toString(), generation: installation.generation },
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
return StagingWorkspace.#retainedStage(staged, witness);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
if (error?.code === ErrorCodes.WORKSPACE_TTL_INVALID) {
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
throw createCasError(
|
|
194
|
+
'Workspace staged an object but could not establish retention',
|
|
195
|
+
ErrorCodes.WORKSPACE_RETENTION_FAILED,
|
|
196
|
+
{
|
|
197
|
+
method,
|
|
198
|
+
workspaceId: this.id,
|
|
199
|
+
staged: typeof staged.toJSON === 'function'
|
|
200
|
+
? staged.toJSON()
|
|
201
|
+
: { handle: staged.handle.toString() },
|
|
202
|
+
originalError: error,
|
|
203
|
+
},
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async #install(targets) {
|
|
209
|
+
if (targets.length > MAX_WORKSPACE_TARGETS) {
|
|
210
|
+
throw createCasError(
|
|
211
|
+
'Workspace target count exceeds the supported maximum',
|
|
212
|
+
ErrorCodes.WORKSPACE_STATE_INVALID,
|
|
213
|
+
{ targetCount: targets.length, maxTargetCount: MAX_WORKSPACE_TARGETS },
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
const observedAt = this.#observedAt();
|
|
217
|
+
const expiresAt = this.#expiryFrom(observedAt);
|
|
218
|
+
const descriptor = await this.#pages.put({
|
|
219
|
+
source: this.#descriptorCodec.encode({
|
|
220
|
+
ref: this.#workspaceRef.toString(),
|
|
221
|
+
createdAt: this.#workspaceRef.createdAt,
|
|
222
|
+
expiresAt,
|
|
223
|
+
targetCount: targets.length,
|
|
224
|
+
}),
|
|
225
|
+
});
|
|
226
|
+
const entries = [
|
|
227
|
+
{
|
|
228
|
+
name: WORKSPACE_DESCRIPTOR_ENTRY,
|
|
229
|
+
oid: descriptor.handle.oid,
|
|
230
|
+
type: 'blob',
|
|
231
|
+
retention: 'evictable',
|
|
232
|
+
},
|
|
233
|
+
...targets.map(StagingWorkspace.#targetEntry),
|
|
234
|
+
];
|
|
235
|
+
const mutation = await this.#rootSet.replace({
|
|
236
|
+
entries,
|
|
237
|
+
expectedHeadOid: this.#generation,
|
|
238
|
+
});
|
|
239
|
+
this.#generation = mutation.commitOid;
|
|
240
|
+
this.#expiresAt = expiresAt;
|
|
241
|
+
this.#targets = new Map(targets.map((target) => [target.handle.toString(), target]));
|
|
242
|
+
const witnesses = targets.map((target) => this.#witness({
|
|
243
|
+
target,
|
|
244
|
+
entries: mutation.entries,
|
|
245
|
+
observedAt,
|
|
246
|
+
}));
|
|
247
|
+
return Object.freeze({
|
|
248
|
+
changed: mutation.changed,
|
|
249
|
+
ref: this.#workspaceRef.toString(),
|
|
250
|
+
generation: mutation.commitOid,
|
|
251
|
+
expiresAt,
|
|
252
|
+
handles: Object.freeze(targets.map((target) => target.handle)),
|
|
253
|
+
witnesses: Object.freeze(witnesses),
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
#witness({ target, entries, observedAt }) {
|
|
258
|
+
const name = StagingWorkspace.#targetName(target.handle);
|
|
259
|
+
const index = entries.findIndex((entry) => entry.name === name);
|
|
260
|
+
if (index === -1) {
|
|
261
|
+
throw createCasError(
|
|
262
|
+
'Workspace target is absent from its committed generation',
|
|
263
|
+
ErrorCodes.WORKSPACE_STATE_INVALID,
|
|
264
|
+
{ handle: target.handle.toString(), generation: this.#generation },
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
return new RetentionWitness({
|
|
268
|
+
handle: target.handle,
|
|
269
|
+
policy: 'evictable',
|
|
270
|
+
reachability: 'anchored',
|
|
271
|
+
root: {
|
|
272
|
+
kind: 'root-set',
|
|
273
|
+
namespace: this.#workspaceRef.namespace,
|
|
274
|
+
ref: this.#workspaceRef.toString(),
|
|
275
|
+
generation: this.#generation,
|
|
276
|
+
path: RootSetMetadataCodec.slotFor(index),
|
|
277
|
+
},
|
|
278
|
+
observedAt,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async #resolveTargets(handles) {
|
|
283
|
+
if (!handles || typeof handles[Symbol.iterator] !== 'function') {
|
|
284
|
+
throw createCasError(
|
|
285
|
+
'Workspace checkpoint handles must be iterable',
|
|
286
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
287
|
+
{ handles },
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
const targets = new Map();
|
|
291
|
+
let handleCount = 0;
|
|
292
|
+
for (const value of handles) {
|
|
293
|
+
handleCount += 1;
|
|
294
|
+
if (handleCount > MAX_WORKSPACE_TARGETS) {
|
|
295
|
+
throw createCasError(
|
|
296
|
+
'Workspace checkpoint handle count exceeds the supported maximum',
|
|
297
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
298
|
+
{ handleCount, maxHandleCount: MAX_WORKSPACE_TARGETS },
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
const handle = parseApplicationHandle(value);
|
|
302
|
+
if (targets.has(handle.toString())) {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const target = await this.#resolveTarget(handle);
|
|
306
|
+
targets.set(target.handle.toString(), target);
|
|
307
|
+
}
|
|
308
|
+
return [...targets.values()].sort(StagingWorkspace.#compareTargets);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async #resolveTarget(value) {
|
|
312
|
+
const handle = parseApplicationHandle(value);
|
|
313
|
+
const resolved = await this.#resolveHandle(handle);
|
|
314
|
+
const resolvedHandle = parseApplicationHandle(resolved?.handle ?? handle);
|
|
315
|
+
if (resolvedHandle.toString() !== handle.toString()) {
|
|
316
|
+
throw createCasError(
|
|
317
|
+
'Workspace handle resolver changed the requested handle',
|
|
318
|
+
ErrorCodes.WORKSPACE_STATE_INVALID,
|
|
319
|
+
{ requested: handle.toString(), resolved: resolvedHandle.toString() },
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
if (!resolved || resolved.oid !== handle.oid || !['blob', 'tree'].includes(resolved.type)) {
|
|
323
|
+
throw createCasError(
|
|
324
|
+
'Workspace handle resolver returned an invalid Git root',
|
|
325
|
+
ErrorCodes.WORKSPACE_STATE_INVALID,
|
|
326
|
+
{ handle: handle.toString(), resolved },
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
return Object.freeze({ handle, oid: resolved.oid, type: resolved.type });
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
#retainedTarget(value) {
|
|
333
|
+
const handle = parseApplicationHandle(value);
|
|
334
|
+
const target = this.#targets.get(handle.toString());
|
|
335
|
+
if (!target) {
|
|
336
|
+
throw createCasError(
|
|
337
|
+
'Workspace can promote only a handle retained by its active generation',
|
|
338
|
+
ErrorCodes.WORKSPACE_HANDLE_NOT_RETAINED,
|
|
339
|
+
{ handle: handle.toString(), workspaceId: this.id },
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
return target;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
#assertPromotionDestination({
|
|
346
|
+
destination,
|
|
347
|
+
target,
|
|
348
|
+
operation,
|
|
349
|
+
rootKind,
|
|
350
|
+
ref,
|
|
351
|
+
generation,
|
|
352
|
+
confirmed,
|
|
353
|
+
}) {
|
|
354
|
+
let witness = null;
|
|
355
|
+
let originalError = null;
|
|
356
|
+
try {
|
|
357
|
+
witness = new RetentionWitness(destination?.witness);
|
|
358
|
+
} catch (error) {
|
|
359
|
+
originalError = error;
|
|
360
|
+
}
|
|
361
|
+
const retained = confirmed
|
|
362
|
+
&& witness?.handle.toString() === target.handle.toString()
|
|
363
|
+
&& witness.reachability === 'anchored'
|
|
364
|
+
&& witness.root.kind === rootKind
|
|
365
|
+
&& witness.root.ref === ref
|
|
366
|
+
&& witness.root.generation === generation;
|
|
367
|
+
if (!retained) {
|
|
368
|
+
throw createCasError(
|
|
369
|
+
'Workspace promotion destination did not prove retention of the requested handle',
|
|
370
|
+
ErrorCodes.WORKSPACE_PROMOTION_NOT_RETAINED,
|
|
371
|
+
{
|
|
372
|
+
operation,
|
|
373
|
+
workspaceId: this.id,
|
|
374
|
+
handle: target.handle.toString(),
|
|
375
|
+
destinationRef: ref,
|
|
376
|
+
destinationGeneration: generation,
|
|
377
|
+
originalError,
|
|
378
|
+
},
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async #finishPromotion(destination) {
|
|
384
|
+
try {
|
|
385
|
+
const release = await this.#releaseExact();
|
|
386
|
+
return Object.freeze({ destination, release });
|
|
387
|
+
} catch (error) {
|
|
388
|
+
throw createCasError(
|
|
389
|
+
'Workspace destination was retained but temporary cleanup remains pending',
|
|
390
|
+
ErrorCodes.WORKSPACE_PROMOTION_CLEANUP_PENDING,
|
|
391
|
+
{ destination, workspaceId: this.id, originalError: error },
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
async #releaseExact() {
|
|
397
|
+
if (this.#released) {
|
|
398
|
+
return Object.freeze({
|
|
399
|
+
changed: false,
|
|
400
|
+
ref: this.#workspaceRef.toString(),
|
|
401
|
+
generation: null,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
if (this.#generation === null) {
|
|
405
|
+
this.#released = true;
|
|
406
|
+
return Object.freeze({
|
|
407
|
+
changed: false,
|
|
408
|
+
ref: this.#workspaceRef.toString(),
|
|
409
|
+
generation: null,
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
const generation = this.#generation;
|
|
413
|
+
const changed = await this.#refs.deleteRef({
|
|
414
|
+
ref: this.#workspaceRef.toString(),
|
|
415
|
+
expectedOldOid: generation,
|
|
416
|
+
});
|
|
417
|
+
this.#released = true;
|
|
418
|
+
this.#generation = null;
|
|
419
|
+
this.#targets = new Map();
|
|
420
|
+
this.#expiresAt = null;
|
|
421
|
+
return Object.freeze({
|
|
422
|
+
changed,
|
|
423
|
+
ref: this.#workspaceRef.toString(),
|
|
424
|
+
generation,
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
#assertActive() {
|
|
429
|
+
if (this.#released) {
|
|
430
|
+
throw createCasError(
|
|
431
|
+
'Workspace has already been released',
|
|
432
|
+
ErrorCodes.WORKSPACE_RELEASED,
|
|
433
|
+
{ workspaceId: this.id },
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
#observedAt() {
|
|
439
|
+
const now = this.#clock.now();
|
|
440
|
+
if (!(now instanceof Date) || Number.isNaN(now.getTime())) {
|
|
441
|
+
throw createCasError(
|
|
442
|
+
'Workspace clock returned an invalid Date',
|
|
443
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
return now.toISOString();
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
#expiryFrom(observedAt) {
|
|
450
|
+
const expiresAt = new Date(Date.parse(observedAt) + this.#ttlMs);
|
|
451
|
+
if (Number.isNaN(expiresAt.getTime())) {
|
|
452
|
+
throw createCasError(
|
|
453
|
+
'Workspace expiry is outside the supported timestamp range',
|
|
454
|
+
ErrorCodes.WORKSPACE_TTL_INVALID,
|
|
455
|
+
{ observedAt, ttlMs: this.#ttlMs },
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
return expiresAt.toISOString();
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
#enqueue(operation) {
|
|
462
|
+
const result = this.#tail.then(operation, operation);
|
|
463
|
+
this.#tail = result.then(() => undefined, () => undefined);
|
|
464
|
+
return result;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
static #targetName(handle) {
|
|
468
|
+
return `${TARGET_ENTRY_PREFIX}${handle.toString()}`;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
static #targetEntry(target) {
|
|
472
|
+
return {
|
|
473
|
+
name: StagingWorkspace.#targetName(target.handle),
|
|
474
|
+
oid: target.oid,
|
|
475
|
+
type: target.type,
|
|
476
|
+
retention: 'evictable',
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
static #compareTargets(left, right) {
|
|
481
|
+
return left.handle.toString().localeCompare(right.handle.toString());
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
static #retainedStage(staged, witness) {
|
|
485
|
+
const retention = Object.freeze({
|
|
486
|
+
policy: 'evictable',
|
|
487
|
+
reachability: 'anchored',
|
|
488
|
+
protection: 'workspace',
|
|
489
|
+
});
|
|
490
|
+
return Object.freeze({
|
|
491
|
+
...staged,
|
|
492
|
+
state: 'retained',
|
|
493
|
+
retention,
|
|
494
|
+
witness,
|
|
495
|
+
toJSON() {
|
|
496
|
+
return {
|
|
497
|
+
...staged.toJSON(),
|
|
498
|
+
state: 'retained',
|
|
499
|
+
retention: { ...retention },
|
|
500
|
+
witness: witness.toJSON(),
|
|
501
|
+
};
|
|
502
|
+
},
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
static #assertDependencies({ rootSet, refs, assets, pages, bundles, resolveHandle,
|
|
507
|
+
descriptorCodec, clock }) {
|
|
508
|
+
const missing = [
|
|
509
|
+
['rootSet', StagingWorkspace.#hasMethods(rootSet, ['replace'])],
|
|
510
|
+
['refs', StagingWorkspace.#hasMethods(refs, ['deleteRef'])],
|
|
511
|
+
['assets', StagingWorkspace.#hasMethods(assets, ['put', 'adopt'])],
|
|
512
|
+
['pages', StagingWorkspace.#hasMethods(pages, ['put'])],
|
|
513
|
+
['bundles', StagingWorkspace.#hasMethods(bundles, ['put', 'putOrdered'])],
|
|
514
|
+
['resolveHandle', typeof resolveHandle === 'function'],
|
|
515
|
+
['descriptorCodec', StagingWorkspace.#hasMethods(descriptorCodec, ['encode'])],
|
|
516
|
+
['clock', StagingWorkspace.#hasMethods(clock, ['now'])],
|
|
517
|
+
].filter(([, valid]) => !valid).map(([name]) => name);
|
|
518
|
+
if (missing.length > 0) {
|
|
519
|
+
throw createCasError(
|
|
520
|
+
'StagingWorkspace requires complete dependencies',
|
|
521
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
522
|
+
{ missing },
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
static #hasMethods(value, methods) {
|
|
528
|
+
return value && methods.every((method) => typeof value[method] === 'function');
|
|
529
|
+
}
|
|
530
|
+
}
|