@git-stunts/git-cas 7.0.0 → 7.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 +15 -0
- package/CHANGELOG.md +57 -0
- package/README.md +16 -4
- package/build-info.json +2 -2
- package/docs/API.md +111 -0
- package/docs/releases/v7.1.0.md +25 -0
- package/docs/releases/v7.2.0.md +15 -0
- package/index.d.ts +15 -0
- package/index.js +30 -0
- package/package.json +5 -3
- package/src/domain/errors/Codes.js +1 -0
- package/src/domain/errors/ReservationStorageError.js +9 -0
- package/src/domain/services/ReservationCustodyObjects.js +171 -0
- package/src/domain/services/ReservationCustodyStore.js +149 -0
- package/src/domain/services/ReservationIndex.js +241 -0
- package/src/domain/services/ReservationIndexNode.js +78 -0
- package/src/domain/services/ReservationLedger.js +326 -0
- package/src/domain/services/ReservationLedgerStore.js +141 -0
- package/src/domain/services/ReservationListing.js +40 -0
- package/src/domain/services/ReservationNamespace.d.ts +135 -0
- package/src/domain/services/ReservationNamespace.js +51 -0
- package/src/domain/services/ReservationNamespaceRegistry.js +89 -0
- package/src/domain/services/ReservationRecord.js +166 -0
- package/src/domain/services/ReservationScan.js +54 -0
- package/src/domain/value-objects/ReservationCursor.d.ts +11 -0
- package/src/domain/value-objects/ReservationCursor.js +31 -0
- package/src/domain/value-objects/ReservationCustodyDescriptor.js +47 -0
- package/src/domain/value-objects/ReservationCustodySnapshot.js +16 -0
- package/src/domain/value-objects/ReservationEvidence.js +26 -0
- package/src/domain/value-objects/ReservationEvidenceRead.js +22 -0
- package/src/domain/value-objects/ReservationEvidenceRoots.js +72 -0
- package/src/domain/value-objects/ReservationIndeterminate.js +17 -0
- package/src/domain/value-objects/ReservationInspection.js +21 -0
- package/src/domain/value-objects/ReservationKey.js +26 -0
- package/src/domain/value-objects/ReservationLedgerHeader.js +15 -0
- package/src/domain/value-objects/ReservationLedgerRefusal.js +21 -0
- package/src/domain/value-objects/ReservationLedgerSnapshot.js +27 -0
- package/src/domain/value-objects/ReservationListRequest.js +35 -0
- package/src/domain/value-objects/ReservationListUnavailable.js +17 -0
- package/src/domain/value-objects/ReservationOpenIndeterminate.js +15 -0
- package/src/domain/value-objects/ReservationOpenRefusal.js +14 -0
- package/src/domain/value-objects/ReservationPage.js +27 -0
- package/src/domain/value-objects/ReservationPolicy.js +24 -0
- package/src/domain/value-objects/ReservationRefusal.js +23 -0
- package/src/domain/value-objects/ReservationResult.js +27 -0
- package/src/domain/value-objects/ReservationSupport.js +22 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +7 -0
- package/src/infrastructure/codecs/ReservationCustodyCodec.js +54 -0
- package/src/infrastructure/codecs/ReservationLedgerCodec.js +48 -0
- package/src/infrastructure/codecs/ReservationRecordCodec.js +54 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitRefPort.js +11 -0
- package/src/ports/ReservationCustodyCodecPort.js +14 -0
- package/src/ports/ReservationLedgerCodecPort.js +14 -0
- package/src/ports/ReservationRecordCodecPort.js +14 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/* @ts-self-types="./ReservationNamespace.d.ts" */
|
|
2
|
+
import ReservationLedger from './ReservationLedger.js';
|
|
3
|
+
import ReservationListing from './ReservationListing.js';
|
|
4
|
+
import ReservationLedgerSnapshot from '../value-objects/ReservationLedgerSnapshot.js';
|
|
5
|
+
import ReservationEvidence from '../value-objects/ReservationEvidence.js';
|
|
6
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
7
|
+
|
|
8
|
+
/** Opened durable authority. No method executes an application effect. */
|
|
9
|
+
export default class ReservationNamespace {
|
|
10
|
+
#ledger;
|
|
11
|
+
#listing;
|
|
12
|
+
|
|
13
|
+
constructor({ snapshot, ledger, listing }) {
|
|
14
|
+
if (!(snapshot instanceof ReservationLedgerSnapshot) || !(ledger instanceof ReservationLedger) ||
|
|
15
|
+
!(listing instanceof ReservationListing)) {
|
|
16
|
+
throw new InvalidOptionsError('Reservation namespace requires its installed snapshot and services');
|
|
17
|
+
}
|
|
18
|
+
this.status = 'opened';
|
|
19
|
+
this.namespace = snapshot.header.policy.namespace;
|
|
20
|
+
this.policy = snapshot.header.policy;
|
|
21
|
+
this.openedRevision = snapshot.revision;
|
|
22
|
+
this.#ledger = ledger;
|
|
23
|
+
this.#listing = listing;
|
|
24
|
+
Object.freeze(this);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async reserve(identity) { return await this.#ledger.reserve(namedOptions(identity)); }
|
|
28
|
+
async arm(identity) { return await this.#ledger.arm(namedOptions(identity)); }
|
|
29
|
+
async cancel(identity) { return await this.#ledger.cancel(namedOptions(identity)); }
|
|
30
|
+
async reconcile(identity) { return await this.#ledger.reconcile(namedOptions(identity)); }
|
|
31
|
+
async inspect(identity) { return await this.#ledger.inspectCustody(namedOptions(identity)); }
|
|
32
|
+
async readEvidence(options) { return await this.#ledger.readEvidence(namedOptions(options)); }
|
|
33
|
+
async list(options = {}) { return await this.#listing.list(namedOptions(options)); }
|
|
34
|
+
|
|
35
|
+
async prepare(options) {
|
|
36
|
+
const { key, binding, evidence } = namedOptions(options);
|
|
37
|
+
return await this.#ledger.prepare({ key, binding, evidence: new ReservationEvidence(evidence) });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async settle(options) {
|
|
41
|
+
const { key, binding, evidence } = namedOptions(options);
|
|
42
|
+
return await this.#ledger.settle({ key, binding, evidence: new ReservationEvidence(evidence) });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function namedOptions(value) {
|
|
47
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
48
|
+
throw new InvalidOptionsError('Reservation operations require named fields');
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import GitPersistencePort from '../../ports/GitPersistencePort.js';
|
|
2
|
+
import GitRefPort from '../../ports/GitRefPort.js';
|
|
3
|
+
import CryptoPort from '../../ports/CryptoPort.js';
|
|
4
|
+
import ReservationCustodyCodecPort from '../../ports/ReservationCustodyCodecPort.js';
|
|
5
|
+
import ReservationPolicy from '../value-objects/ReservationPolicy.js';
|
|
6
|
+
import ReservationLedgerHeader from '../value-objects/ReservationLedgerHeader.js';
|
|
7
|
+
import ReservationOpenRefusal from '../value-objects/ReservationOpenRefusal.js';
|
|
8
|
+
import ReservationOpenIndeterminate from '../value-objects/ReservationOpenIndeterminate.js';
|
|
9
|
+
import ReservationIndex from './ReservationIndex.js';
|
|
10
|
+
import ReservationLedgerStore from './ReservationLedgerStore.js';
|
|
11
|
+
import ReservationLedger from './ReservationLedger.js';
|
|
12
|
+
import ReservationListing from './ReservationListing.js';
|
|
13
|
+
import ReservationCustodyObjects from './ReservationCustodyObjects.js';
|
|
14
|
+
import ReservationCustodyStore from './ReservationCustodyStore.js';
|
|
15
|
+
import ReservationNamespace from './ReservationNamespace.js';
|
|
16
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
17
|
+
import { CasError, ErrorCodes } from '../errors/index.js';
|
|
18
|
+
|
|
19
|
+
const MAX_OPEN_ATTEMPTS = 4;
|
|
20
|
+
|
|
21
|
+
/** Creates policy at open, before the first reservation can select a limit. */
|
|
22
|
+
export default class ReservationNamespaceRegistry {
|
|
23
|
+
#index;
|
|
24
|
+
#store;
|
|
25
|
+
#ports;
|
|
26
|
+
|
|
27
|
+
constructor({ index, store, persistence, refs, crypto, custodyCodec }) {
|
|
28
|
+
if (!(index instanceof ReservationIndex) || !(store instanceof ReservationLedgerStore)) {
|
|
29
|
+
throw new InvalidOptionsError('Reservation registry requires an index and ledger store');
|
|
30
|
+
}
|
|
31
|
+
assertPorts({ persistence, refs, crypto, custodyCodec });
|
|
32
|
+
this.#index = index;
|
|
33
|
+
this.#store = store;
|
|
34
|
+
this.#ports = Object.freeze({ persistence, refs, crypto, custodyCodec });
|
|
35
|
+
Object.freeze(this);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async open(options) {
|
|
39
|
+
const policy = new ReservationPolicy(options);
|
|
40
|
+
for (let retry = 0; retry < MAX_OPEN_ATTEMPTS; retry++) {
|
|
41
|
+
try {
|
|
42
|
+
const before = await this.#store.read(policy.namespace);
|
|
43
|
+
if (before !== null) {
|
|
44
|
+
return this.#opened(policy, before);
|
|
45
|
+
}
|
|
46
|
+
const snapshot = await this.#store.write({
|
|
47
|
+
before: null, header: new ReservationLedgerHeader({ policy, charged: 0 }), indexRoot: await this.#index.empty(),
|
|
48
|
+
});
|
|
49
|
+
return this.#opened(policy, snapshot);
|
|
50
|
+
} catch (cause) {
|
|
51
|
+
if (cause instanceof CasError && cause.code === ErrorCodes.GIT_REF_CONFLICT) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
return await this.#recover(policy, cause);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return new ReservationOpenRefusal(policy, 'contention');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
#opened(policy, snapshot) {
|
|
61
|
+
if (!policy.equals(snapshot.header.policy)) {
|
|
62
|
+
return new ReservationOpenRefusal(policy, 'policy-mismatch');
|
|
63
|
+
}
|
|
64
|
+
const { persistence, refs, crypto, custodyCodec } = this.#ports;
|
|
65
|
+
const objects = new ReservationCustodyObjects({ namespace: policy.namespace, persistence, refs, codec: custodyCodec });
|
|
66
|
+
const custody = new ReservationCustodyStore({ objects, refs, crypto });
|
|
67
|
+
const ledger = new ReservationLedger({ policy, index: this.#index, store: this.#store, custody });
|
|
68
|
+
const listing = new ReservationListing({ policy, index: this.#index, store: this.#store });
|
|
69
|
+
return new ReservationNamespace({ snapshot, ledger, listing });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async #recover(policy, cause) {
|
|
73
|
+
try {
|
|
74
|
+
const snapshot = await this.#store.read(policy.namespace);
|
|
75
|
+
return snapshot === null ? new ReservationOpenIndeterminate(policy, cause) : this.#opened(policy, snapshot);
|
|
76
|
+
} catch (inspectionError) {
|
|
77
|
+
return new ReservationOpenIndeterminate(policy, new CasError(
|
|
78
|
+
'Reservation namespace open and inspection failed', ErrorCodes.GIT_ERROR, { operationError: cause, inspectionError },
|
|
79
|
+
));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function assertPorts({ persistence, refs, crypto, custodyCodec }) {
|
|
85
|
+
if (!(persistence instanceof GitPersistencePort) || !(refs instanceof GitRefPort) ||
|
|
86
|
+
!(crypto instanceof CryptoPort) || !(custodyCodec instanceof ReservationCustodyCodecPort)) {
|
|
87
|
+
throw new InvalidOptionsError('Reservation registry requires injected persistence, ref, crypto and custody codec ports');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import CasError from '../errors/CasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import Oid from '../value-objects/Oid.js';
|
|
4
|
+
import ReservationRefusal from '../value-objects/ReservationRefusal.js';
|
|
5
|
+
import ReservationKey from '../value-objects/ReservationKey.js';
|
|
6
|
+
|
|
7
|
+
const BINDING_PATTERN = /^[0-9a-f]{64}$/u;
|
|
8
|
+
export const RESERVATION_PHASES = Object.freeze(['reserved', 'prepared', 'armed', 'settled', 'cancelled']);
|
|
9
|
+
const PHASES = new Set(RESERVATION_PHASES);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Immutable candidate for one attempt's ledger record.
|
|
13
|
+
*
|
|
14
|
+
* This class performs no storage operation and grants no publication permission.
|
|
15
|
+
* Its owner must persist the winning transition and validate custody before
|
|
16
|
+
* acknowledging any durable outcome. Uncertainty belongs to that observation,
|
|
17
|
+
* not to a made-up durable phase in this record.
|
|
18
|
+
*/
|
|
19
|
+
export default class ReservationRecord {
|
|
20
|
+
constructor(value) {
|
|
21
|
+
assertRecordInput(value);
|
|
22
|
+
const { key, binding, phase, preparation = null, settlement = null } = value;
|
|
23
|
+
assertIdentity(key, binding);
|
|
24
|
+
assertPhase(phase);
|
|
25
|
+
const prepared = canonicalEvidence(preparation);
|
|
26
|
+
const settled = canonicalEvidence(settlement);
|
|
27
|
+
assertPhaseEvidence({ phase, preparation: prepared, settlement: settled });
|
|
28
|
+
this.key = key;
|
|
29
|
+
this.binding = binding;
|
|
30
|
+
this.phase = phase;
|
|
31
|
+
this.preparation = prepared;
|
|
32
|
+
this.settlement = settled;
|
|
33
|
+
Object.freeze(this);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** One charged record slot, or zero for a terminal cancelled tombstone. */
|
|
37
|
+
get charge() {
|
|
38
|
+
return this.phase === 'cancelled' ? 0 : 1;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Bind the original prepared custody generation; never replace its evidence. */
|
|
42
|
+
prepare(commitOid) {
|
|
43
|
+
const preparation = requiredEvidence(commitOid);
|
|
44
|
+
if (this.phase === 'cancelled') {
|
|
45
|
+
return new ReservationRefusal('cancelled');
|
|
46
|
+
}
|
|
47
|
+
if (this.preparation !== null) {
|
|
48
|
+
return this.preparation === preparation
|
|
49
|
+
? this
|
|
50
|
+
: new ReservationRefusal('preparation-mismatch');
|
|
51
|
+
}
|
|
52
|
+
return this.#next('prepared', { preparation });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Propose irreversible arming once; recovered arming is not a second permit. */
|
|
56
|
+
arm() {
|
|
57
|
+
if (this.phase === 'cancelled') {
|
|
58
|
+
return new ReservationRefusal('cancelled');
|
|
59
|
+
}
|
|
60
|
+
if (this.phase === 'settled') {
|
|
61
|
+
return new ReservationRefusal('already-settled');
|
|
62
|
+
}
|
|
63
|
+
if (this.phase === 'armed') {
|
|
64
|
+
return new ReservationRefusal('already-armed');
|
|
65
|
+
}
|
|
66
|
+
if (this.phase !== 'prepared') {
|
|
67
|
+
return new ReservationRefusal('not-prepared');
|
|
68
|
+
}
|
|
69
|
+
return this.#next('armed');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Prepared reads and armed writes may settle after their final pin is durable. */
|
|
73
|
+
settle(commitOid) {
|
|
74
|
+
const settlement = requiredEvidence(commitOid);
|
|
75
|
+
if (this.phase === 'cancelled') {
|
|
76
|
+
return new ReservationRefusal('cancelled');
|
|
77
|
+
}
|
|
78
|
+
if (this.preparation === null) {
|
|
79
|
+
return new ReservationRefusal('not-prepared');
|
|
80
|
+
}
|
|
81
|
+
assertSameObjectFormat(this.preparation, settlement);
|
|
82
|
+
if (this.settlement !== null) {
|
|
83
|
+
return this.settlement === settlement
|
|
84
|
+
? this
|
|
85
|
+
: new ReservationRefusal('settlement-mismatch');
|
|
86
|
+
}
|
|
87
|
+
return this.#next('settled', { settlement });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Cancellation releases a slot only before irreversible arming/settlement. */
|
|
91
|
+
cancel() {
|
|
92
|
+
if (this.phase === 'armed' || this.phase === 'settled') {
|
|
93
|
+
return new ReservationRefusal('irreversible');
|
|
94
|
+
}
|
|
95
|
+
return this.phase === 'cancelled' ? this : this.#next('cancelled');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
#next(phase, { preparation = this.preparation, settlement = this.settlement } = {}) {
|
|
99
|
+
return new ReservationRecord({
|
|
100
|
+
key: this.key,
|
|
101
|
+
binding: this.binding,
|
|
102
|
+
phase,
|
|
103
|
+
preparation,
|
|
104
|
+
settlement,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function assertRecordInput(value) {
|
|
110
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
111
|
+
throw invalid('Reservation record requires named fields');
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function assertIdentity(key, binding) {
|
|
116
|
+
new ReservationKey(key);
|
|
117
|
+
if (typeof binding !== 'string' || !BINDING_PATTERN.test(binding)) {
|
|
118
|
+
throw invalid('Reservation binding must be a canonical SHA-256 digest');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function assertPhase(phase) {
|
|
123
|
+
if (!PHASES.has(phase)) {
|
|
124
|
+
throw invalid('Reservation phase is invalid');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function canonicalEvidence(value) {
|
|
129
|
+
return value === null ? null : requiredEvidence(value);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function requiredEvidence(value) {
|
|
133
|
+
if (typeof value !== 'string' || !Oid.isValid(value)) {
|
|
134
|
+
throw invalid('Reservation evidence must name a canonical Git commit OID');
|
|
135
|
+
}
|
|
136
|
+
const canonical = Oid.from(value).toString();
|
|
137
|
+
if (canonical !== value) {
|
|
138
|
+
throw invalid('Reservation evidence OID must use canonical lowercase encoding');
|
|
139
|
+
}
|
|
140
|
+
return canonical;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function assertPhaseEvidence({ phase, preparation, settlement }) {
|
|
144
|
+
if (phase === 'reserved' && preparation !== null) {
|
|
145
|
+
throw invalid('Reserved attempt cannot already claim prepared custody');
|
|
146
|
+
}
|
|
147
|
+
if (phase !== 'reserved' && phase !== 'cancelled' && preparation === null) {
|
|
148
|
+
throw invalid('This reservation phase requires original preparation evidence');
|
|
149
|
+
}
|
|
150
|
+
if ((phase === 'settled') !== (settlement !== null)) {
|
|
151
|
+
throw invalid('Only a settled reservation has settlement evidence');
|
|
152
|
+
}
|
|
153
|
+
if (settlement !== null) {
|
|
154
|
+
assertSameObjectFormat(preparation, settlement);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function assertSameObjectFormat(preparation, settlement) {
|
|
159
|
+
if (preparation.length !== settlement.length) {
|
|
160
|
+
throw invalid('Reservation evidence must use one Git object format');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function invalid(message) {
|
|
165
|
+
return new CasError(message, ErrorCodes.INVALID_OPTIONS);
|
|
166
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import ReservationListRequest from '../value-objects/ReservationListRequest.js';
|
|
2
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
3
|
+
|
|
4
|
+
/** One bounded DFS; mutable traversal state is private and never a cursor. */
|
|
5
|
+
export default class ReservationScan {
|
|
6
|
+
#request;
|
|
7
|
+
#records = [];
|
|
8
|
+
#visited = 0;
|
|
9
|
+
#after;
|
|
10
|
+
#complete = false;
|
|
11
|
+
|
|
12
|
+
constructor(request) {
|
|
13
|
+
if (!(request instanceof ReservationListRequest)) {
|
|
14
|
+
throw new InvalidOptionsError('Reservation scan requires a validated listing request');
|
|
15
|
+
}
|
|
16
|
+
this.#request = request;
|
|
17
|
+
this.#after = request.cursor?.after ?? null;
|
|
18
|
+
Object.freeze(this);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
skip(prefix) {
|
|
22
|
+
return this.#after !== null && prefix.padEnd(64, 'f') <= this.#after;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A record above full depth can hold a route an earlier page already listed. */
|
|
26
|
+
listed(route) {
|
|
27
|
+
return this.#after !== null && route <= this.#after;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
visit() {
|
|
31
|
+
if (this.#visited === this.#request.nodeBudget || this.#records.length === this.#request.limit) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
this.#visited++;
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
accept(record, route) {
|
|
39
|
+
this.#after = route;
|
|
40
|
+
if (this.#request.phase === null || record.phase === this.#request.phase) {
|
|
41
|
+
this.#records.push(record);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
finish(complete) {
|
|
46
|
+
this.#complete = complete;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get records() { return Object.freeze([...this.#records]); }
|
|
51
|
+
get visitedNodes() { return this.#visited; }
|
|
52
|
+
get after() { return this.#after; }
|
|
53
|
+
get complete() { return this.#complete; }
|
|
54
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ReservationCursorData, ReservationPhase } from '../services/ReservationNamespace.js';
|
|
2
|
+
|
|
3
|
+
/** A validated, serializable ledger position; not a retention pin or capability. */
|
|
4
|
+
export default class ReservationCursor {
|
|
5
|
+
constructor(options: ReservationCursorData);
|
|
6
|
+
readonly namespace: string;
|
|
7
|
+
readonly revision: string;
|
|
8
|
+
readonly after: string;
|
|
9
|
+
readonly phase: ReservationPhase | null;
|
|
10
|
+
static assertPhase(phase: ReservationPhase | null): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* @ts-self-types="./ReservationCursor.d.ts" */
|
|
2
|
+
import CollectionNamespace from './CollectionNamespace.js';
|
|
3
|
+
import Oid from './Oid.js';
|
|
4
|
+
import { RESERVATION_PHASES } from '../services/ReservationRecord.js';
|
|
5
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
6
|
+
|
|
7
|
+
/** Serializable position in one immutable ledger, including its phase filter. */
|
|
8
|
+
export default class ReservationCursor {
|
|
9
|
+
constructor(value) {
|
|
10
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
11
|
+
throw new InvalidOptionsError('Reservation cursor requires named fields');
|
|
12
|
+
}
|
|
13
|
+
const { namespace, revision, after, phase = null } = value;
|
|
14
|
+
if (!Oid.isValid(revision) || revision !== revision.toLowerCase() ||
|
|
15
|
+
typeof after !== 'string' || !/^[0-9a-f]{64}$/u.test(after)) {
|
|
16
|
+
throw new InvalidOptionsError('Reservation cursor requires a canonical snapshot and complete route');
|
|
17
|
+
}
|
|
18
|
+
ReservationCursor.assertPhase(phase);
|
|
19
|
+
this.namespace = CollectionNamespace.from(namespace).toString();
|
|
20
|
+
this.revision = revision;
|
|
21
|
+
this.after = after;
|
|
22
|
+
this.phase = phase;
|
|
23
|
+
Object.freeze(this);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static assertPhase(phase) {
|
|
27
|
+
if (phase !== null && !RESERVATION_PHASES.includes(phase)) {
|
|
28
|
+
throw new InvalidOptionsError('Reservation phase filter is invalid');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import ReservationRecord from '../services/ReservationRecord.js';
|
|
2
|
+
import CollectionNamespace from './CollectionNamespace.js';
|
|
3
|
+
import Oid from './Oid.js';
|
|
4
|
+
import ReservationEvidenceRoots from './ReservationEvidenceRoots.js';
|
|
5
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
6
|
+
|
|
7
|
+
/** Immutable meaning of a custody generation, independent of commit timestamps. */
|
|
8
|
+
export default class ReservationCustodyDescriptor {
|
|
9
|
+
constructor({ namespace, key, binding, stage, recordOid, roots, preparation = null }) {
|
|
10
|
+
const identity = new ReservationRecord({ key, binding, phase: 'reserved' });
|
|
11
|
+
assertStage(stage, preparation);
|
|
12
|
+
if (!(roots instanceof ReservationEvidenceRoots)) {
|
|
13
|
+
throw new InvalidOptionsError('Custody descriptor requires validated evidence roots');
|
|
14
|
+
}
|
|
15
|
+
roots.assertFormat(recordOid);
|
|
16
|
+
if (preparation !== null && (preparation.length !== recordOid.length || roots.parents.includes(preparation))) {
|
|
17
|
+
throw new InvalidOptionsError('Settlement must use the preparation format without duplicating its parent');
|
|
18
|
+
}
|
|
19
|
+
this.namespace = CollectionNamespace.from(namespace).toString();
|
|
20
|
+
this.key = identity.key;
|
|
21
|
+
this.binding = identity.binding;
|
|
22
|
+
this.stage = stage;
|
|
23
|
+
this.recordOid = recordOid;
|
|
24
|
+
this.roots = roots;
|
|
25
|
+
this.preparation = preparation;
|
|
26
|
+
Object.freeze(this);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get parents() {
|
|
30
|
+
return this.preparation === null ? this.roots.parents : Object.freeze([this.preparation, ...this.roots.parents]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
equals(other) {
|
|
34
|
+
return other instanceof ReservationCustodyDescriptor && this.namespace === other.namespace &&
|
|
35
|
+
this.key === other.key && this.binding === other.binding && this.stage === other.stage &&
|
|
36
|
+
this.recordOid === other.recordOid && this.preparation === other.preparation && this.roots.equals(other.roots);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function assertStage(stage, preparation) {
|
|
41
|
+
if (!['preparation', 'settlement'].includes(stage) || (stage === 'settlement') !== (preparation !== null)) {
|
|
42
|
+
throw new InvalidOptionsError('Custody stage and preparation reference disagree');
|
|
43
|
+
}
|
|
44
|
+
if (preparation !== null && (!Oid.isValid(preparation) || preparation !== preparation.toLowerCase())) {
|
|
45
|
+
throw new InvalidOptionsError('Settlement preparation must be a canonical Git OID');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import ReservationCustodyDescriptor from './ReservationCustodyDescriptor.js';
|
|
2
|
+
import Oid from './Oid.js';
|
|
3
|
+
import ReservationStorageError from '../errors/ReservationStorageError.js';
|
|
4
|
+
|
|
5
|
+
/** Validated generation identity; the custody store establishes its live pin. */
|
|
6
|
+
export default class ReservationCustodySnapshot {
|
|
7
|
+
constructor({ descriptor, revision }) {
|
|
8
|
+
if (!(descriptor instanceof ReservationCustodyDescriptor) || !Oid.isValid(revision) ||
|
|
9
|
+
revision !== revision.toLowerCase() || revision.length !== descriptor.recordOid.length) {
|
|
10
|
+
throw new ReservationStorageError('Custody generation requires a valid descriptor and matching commit OID');
|
|
11
|
+
}
|
|
12
|
+
this.descriptor = descriptor;
|
|
13
|
+
this.revision = revision;
|
|
14
|
+
Object.freeze(this);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import ReservationEvidenceRoots from './ReservationEvidenceRoots.js';
|
|
2
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
3
|
+
|
|
4
|
+
export const RESERVATION_PAGE_MAX_BYTES = 16 * 1024 * 1024;
|
|
5
|
+
|
|
6
|
+
/** Copy-owned page and validated roots; constructor performs no storage I/O. */
|
|
7
|
+
export default class ReservationEvidence {
|
|
8
|
+
#record;
|
|
9
|
+
|
|
10
|
+
constructor(value) {
|
|
11
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
12
|
+
throw new InvalidOptionsError('Reservation evidence requires named fields');
|
|
13
|
+
}
|
|
14
|
+
const { record, supports = [], parents = [] } = value;
|
|
15
|
+
if (!(record instanceof Uint8Array) || record.length > RESERVATION_PAGE_MAX_BYTES) {
|
|
16
|
+
throw new InvalidOptionsError('Reservation evidence page must be at most 16 MiB of bytes');
|
|
17
|
+
}
|
|
18
|
+
this.roots = new ReservationEvidenceRoots({ supports, parents });
|
|
19
|
+
this.#record = Uint8Array.from(record);
|
|
20
|
+
Object.freeze(this);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get record() {
|
|
24
|
+
return Uint8Array.from(this.#record);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import ReservationEvidence from './ReservationEvidence.js';
|
|
2
|
+
import ReservationInspection from './ReservationInspection.js';
|
|
3
|
+
import ReservationLedgerSnapshot from './ReservationLedgerSnapshot.js';
|
|
4
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
5
|
+
|
|
6
|
+
/** Copy-owned page bytes at the acknowledged preparation or settlement. */
|
|
7
|
+
export default class ReservationEvidenceRead {
|
|
8
|
+
constructor({ inspection, stage, custodyRevision, evidence }) {
|
|
9
|
+
if (!(inspection instanceof ReservationInspection) || !(evidence instanceof ReservationEvidence) ||
|
|
10
|
+
!['preparation', 'settlement'].includes(stage)) {
|
|
11
|
+
throw new InvalidOptionsError('Reservation evidence read requires an inspection, stage and validated page');
|
|
12
|
+
}
|
|
13
|
+
ReservationLedgerSnapshot.assertOid(custodyRevision);
|
|
14
|
+
this.status = 'read';
|
|
15
|
+
this.attempt = inspection.record;
|
|
16
|
+
this.ledgerRevision = inspection.revision;
|
|
17
|
+
this.stage = stage;
|
|
18
|
+
this.custodyRevision = custodyRevision;
|
|
19
|
+
this.evidence = evidence;
|
|
20
|
+
Object.freeze(this);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import Oid from './Oid.js';
|
|
2
|
+
import ReservationSupport from './ReservationSupport.js';
|
|
3
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
4
|
+
import ReservationStorageError from '../errors/ReservationStorageError.js';
|
|
5
|
+
|
|
6
|
+
export const RESERVATION_MAX_SUPPORTS = 64;
|
|
7
|
+
export const RESERVATION_MAX_PARENTS = 63;
|
|
8
|
+
|
|
9
|
+
/** Canonical bounded support edges; the original preparation gets parent 64. */
|
|
10
|
+
export default class ReservationEvidenceRoots {
|
|
11
|
+
constructor({ supports = [], parents = [] } = {}) {
|
|
12
|
+
if (!Array.isArray(supports) || supports.length > RESERVATION_MAX_SUPPORTS ||
|
|
13
|
+
!Array.isArray(parents) || parents.length > RESERVATION_MAX_PARENTS) {
|
|
14
|
+
throw new InvalidOptionsError('Reservation evidence allows at most 64 supports and 63 explicit parents');
|
|
15
|
+
}
|
|
16
|
+
this.supports = Object.freeze(supports.map((item) => new ReservationSupport(item)).sort(compareSupport));
|
|
17
|
+
this.parents = Object.freeze(parents.map(canonicalParent).sort());
|
|
18
|
+
if (new Set(this.supports.map((item) => `${item.type}:${item.oid}`)).size !== this.supports.length ||
|
|
19
|
+
new Set(this.parents).size !== this.parents.length) {
|
|
20
|
+
throw new InvalidOptionsError('Reservation evidence roots must be unique');
|
|
21
|
+
}
|
|
22
|
+
Object.freeze(this);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
assertFormat(oid) {
|
|
26
|
+
canonicalParent(oid);
|
|
27
|
+
if (this.parents.some((parent) => parent.length !== oid.length) ||
|
|
28
|
+
this.supports.some((item) => item.oid.length !== oid.length)) {
|
|
29
|
+
throw new ReservationStorageError('Reservation evidence roots use different Git object formats');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
equals(other) {
|
|
34
|
+
return other instanceof ReservationEvidenceRoots && this.parents.length === other.parents.length &&
|
|
35
|
+
this.supports.length === other.supports.length && this.parents.every((oid, i) => oid === other.parents[i]) &&
|
|
36
|
+
this.supports.every((item, i) => item.equals(other.supports[i]));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toTreeLines() {
|
|
40
|
+
return this.supports.map((item, i) => `${item.mode} ${item.type} ${item.oid}\t${slot(i)}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
assertTree(entries) {
|
|
44
|
+
if (!Array.isArray(entries) || entries.length !== this.supports.length) {
|
|
45
|
+
throw new ReservationStorageError('Reservation support tree count disagrees with its descriptor');
|
|
46
|
+
}
|
|
47
|
+
for (let i = 0; i < entries.length; i++) {
|
|
48
|
+
const actual = entries[i];
|
|
49
|
+
const expected = this.supports[i];
|
|
50
|
+
if (actual.name !== slot(i) || actual.mode !== expected.mode || actual.type !== expected.type || actual.oid !== expected.oid) {
|
|
51
|
+
throw new ReservationStorageError('Reservation support tree disagrees with its descriptor');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function canonicalParent(oid) {
|
|
58
|
+
if (!Oid.isValid(oid) || oid !== oid.toLowerCase()) {
|
|
59
|
+
throw new InvalidOptionsError('Reservation evidence requires canonical Git OIDs');
|
|
60
|
+
}
|
|
61
|
+
return oid;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function compareSupport(left, right) {
|
|
65
|
+
const a = `${left.type}:${left.oid}`;
|
|
66
|
+
const b = `${right.type}:${right.oid}`;
|
|
67
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function slot(index) {
|
|
71
|
+
return index.toString(16).padStart(2, '0');
|
|
72
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import ReservationRecord from '../services/ReservationRecord.js';
|
|
2
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
3
|
+
import { CasError, ErrorCodes } from '../errors/index.js';
|
|
4
|
+
|
|
5
|
+
/** Failed observation retains its cause; absence after failure is not a refund. */
|
|
6
|
+
export default class ReservationIndeterminate {
|
|
7
|
+
constructor(record, cause) {
|
|
8
|
+
if (!(record instanceof ReservationRecord)) {
|
|
9
|
+
throw new InvalidOptionsError('Indeterminate reservation requires its original attempt');
|
|
10
|
+
}
|
|
11
|
+
this.status = 'indeterminate';
|
|
12
|
+
this.key = record.key;
|
|
13
|
+
this.binding = record.binding;
|
|
14
|
+
this.cause = cause instanceof Error ? cause : new CasError('Reservation operation failed', ErrorCodes.GIT_ERROR, { originalError: cause });
|
|
15
|
+
Object.freeze(this);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import ReservationRecord from '../services/ReservationRecord.js';
|
|
2
|
+
import ReservationLedgerSnapshot from './ReservationLedgerSnapshot.js';
|
|
3
|
+
import ReservationCustodySnapshot from './ReservationCustodySnapshot.js';
|
|
4
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
5
|
+
|
|
6
|
+
/** Ledger observation followed by pin observation, never a cross-ref snapshot. */
|
|
7
|
+
export default class ReservationInspection {
|
|
8
|
+
constructor({ snapshot, record, custody }) {
|
|
9
|
+
if (!(snapshot instanceof ReservationLedgerSnapshot) || !(record instanceof ReservationRecord) ||
|
|
10
|
+
(custody !== null && !(custody instanceof ReservationCustodySnapshot))) {
|
|
11
|
+
throw new InvalidOptionsError('Reservation inspection requires validated ledger and custody observations');
|
|
12
|
+
}
|
|
13
|
+
this.status = 'inspected';
|
|
14
|
+
this.record = record;
|
|
15
|
+
this.revision = snapshot.revision;
|
|
16
|
+
this.policy = snapshot.header.policy;
|
|
17
|
+
this.charged = snapshot.header.charged;
|
|
18
|
+
this.custody = custody;
|
|
19
|
+
Object.freeze(this);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import InvalidOptionsError from '../errors/InvalidOptionsError.js';
|
|
2
|
+
import { utf8Encode } from '../encoding/utf8.js';
|
|
3
|
+
|
|
4
|
+
const KEY_PATTERN = /^[!-~]{1,128}$/u;
|
|
5
|
+
const ROUTE_PATTERN = /^[0-9a-f]{64}$/u;
|
|
6
|
+
const ROUTE_DOMAIN = 'git-cas/reservation-key/v1\0';
|
|
7
|
+
export const RESERVATION_ROUTE_DIGITS = 64;
|
|
8
|
+
|
|
9
|
+
/** Bounded caller identity, hashed with a versioned storage-route domain. */
|
|
10
|
+
export default class ReservationKey {
|
|
11
|
+
constructor(value) {
|
|
12
|
+
if (typeof value !== 'string' || !KEY_PATTERN.test(value)) {
|
|
13
|
+
throw new InvalidOptionsError('Reservation key must be 1 to 128 printable ASCII bytes without spaces');
|
|
14
|
+
}
|
|
15
|
+
this.value = value;
|
|
16
|
+
Object.freeze(this);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async route(crypto) {
|
|
20
|
+
const digest = await crypto.sha256(utf8Encode(ROUTE_DOMAIN + this.value));
|
|
21
|
+
if (typeof digest !== 'string' || !ROUTE_PATTERN.test(digest)) {
|
|
22
|
+
throw new InvalidOptionsError('Reservation routing requires a canonical SHA-256 digest');
|
|
23
|
+
}
|
|
24
|
+
return digest;
|
|
25
|
+
}
|
|
26
|
+
}
|