@git-stunts/git-cas 6.1.0 → 6.3.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.
Files changed (69) hide show
  1. package/ARCHITECTURE.md +116 -3
  2. package/CHANGELOG.md +117 -2
  3. package/GUIDE.md +114 -13
  4. package/README.md +60 -4
  5. package/UPGRADING.md +75 -0
  6. package/build-info.json +2 -2
  7. package/docs/API.md +867 -16
  8. package/docs/releases/v6.2.0.md +131 -0
  9. package/docs/releases/v6.3.0.md +96 -0
  10. package/index.d.ts +972 -2
  11. package/index.js +326 -1
  12. package/package.json +4 -2
  13. package/src/domain/errors/Codes.js +39 -0
  14. package/src/domain/helpers/assertCanonicalTimestamp.js +15 -0
  15. package/src/domain/helpers/handleTarget.js +48 -0
  16. package/src/domain/helpers/isCanonicalCollectionKey.js +35 -0
  17. package/src/domain/services/AssetService.js +213 -0
  18. package/src/domain/services/BundleDescriptorCodec.js +286 -0
  19. package/src/domain/services/BundleFanoutBuilder.js +247 -0
  20. package/src/domain/services/BundleService.js +848 -0
  21. package/src/domain/services/CacheAcquisitionInventory.js +123 -0
  22. package/src/domain/services/CacheAcquisitionRegistry.js +188 -0
  23. package/src/domain/services/CacheCandidateHeap.js +71 -0
  24. package/src/domain/services/CacheIndex.js +362 -0
  25. package/src/domain/services/CacheMetadataCodec.js +164 -0
  26. package/src/domain/services/CachePolicyEnforcer.js +144 -0
  27. package/src/domain/services/CacheSet.js +754 -0
  28. package/src/domain/services/CacheSetRegistry.js +95 -0
  29. package/src/domain/services/CasService.d.ts +2 -1
  30. package/src/domain/services/ExpiringSet.js +496 -0
  31. package/src/domain/services/ExpiringSetIndex.js +254 -0
  32. package/src/domain/services/ExpiringSetMetadataCodec.js +160 -0
  33. package/src/domain/services/ExpiringSetRegistry.js +101 -0
  34. package/src/domain/services/PageService.js +175 -0
  35. package/src/domain/services/PublicationService.js +315 -0
  36. package/src/domain/services/RepositoryDoctor.js +676 -0
  37. package/src/domain/services/RetentionService.js +120 -0
  38. package/src/domain/services/RootSet.js +10 -6
  39. package/src/domain/services/RootSetMetadataCodec.js +8 -2
  40. package/src/domain/services/RootSetPersistence.js +3 -2
  41. package/src/domain/services/StagingEvidence.js +39 -0
  42. package/src/domain/value-objects/ApplicationHandle.js +40 -0
  43. package/src/domain/value-objects/AssetHandle.js +163 -0
  44. package/src/domain/value-objects/BundleHandle.js +32 -0
  45. package/src/domain/value-objects/BundleLimits.js +80 -0
  46. package/src/domain/value-objects/BundlePath.js +78 -0
  47. package/src/domain/value-objects/CacheAcquisition.js +88 -0
  48. package/src/domain/value-objects/CacheAcquisitionRef.js +129 -0
  49. package/src/domain/value-objects/CacheHit.js +93 -0
  50. package/src/domain/value-objects/CacheKey.js +32 -0
  51. package/src/domain/value-objects/CachePolicy.js +59 -0
  52. package/src/domain/value-objects/CacheSetRef.js +40 -0
  53. package/src/domain/value-objects/CollectionNamespace.js +54 -0
  54. package/src/domain/value-objects/ExpiringMarker.js +81 -0
  55. package/src/domain/value-objects/ExpiringSetKey.js +28 -0
  56. package/src/domain/value-objects/ExpiringSetRef.js +42 -0
  57. package/src/domain/value-objects/OpaqueHandle.js +171 -0
  58. package/src/domain/value-objects/PageHandle.js +34 -0
  59. package/src/domain/value-objects/RetentionWitness.js +106 -0
  60. package/src/domain/value-objects/StagedAsset.js +63 -0
  61. package/src/domain/value-objects/StagedBundle.js +66 -0
  62. package/src/domain/value-objects/StagedPage.js +47 -0
  63. package/src/infrastructure/adapters/GitPersistenceAdapter.js +48 -20
  64. package/src/infrastructure/adapters/GitRefAdapter.js +318 -7
  65. package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +182 -0
  66. package/src/package-version.js +1 -1
  67. package/src/ports/GitPersistencePort.js +11 -1
  68. package/src/ports/GitRefPort.js +46 -0
  69. package/src/ports/RepositoryInspectionPort.js +36 -0
@@ -0,0 +1,88 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import CacheAcquisitionRef from './CacheAcquisitionRef.js';
5
+ import CacheHit from './CacheHit.js';
6
+ import RetentionWitness from './RetentionWitness.js';
7
+
8
+ /** One active, explicitly releasable cache-generation retention scope. */
9
+ export default class CacheAcquisition {
10
+ #release;
11
+ #releasePromise = null;
12
+ #releaseResult = null;
13
+
14
+ constructor({ id, hit, evidence, acquiredAt, release }) {
15
+ if (typeof id !== 'string' || id.length === 0) {
16
+ throw invalid('Cache acquisition ID is required', { id });
17
+ }
18
+ if (!(hit instanceof CacheHit)) {
19
+ throw invalid('Cache acquisition requires a CacheHit', { hit });
20
+ }
21
+ if (!(evidence instanceof RetentionWitness)) {
22
+ throw invalid('Cache acquisition requires retention evidence', { evidence });
23
+ }
24
+ assertCanonicalTimestamp(acquiredAt, {
25
+ invalid,
26
+ message: 'Cache acquisition time must be a canonical UTC timestamp',
27
+ });
28
+ assertEvidence({ id, hit, evidence, acquiredAt });
29
+ if (typeof release !== 'function') {
30
+ throw invalid('Cache acquisition requires a release function', { release });
31
+ }
32
+ this.id = id;
33
+ this.hit = hit;
34
+ this.evidence = evidence;
35
+ this.acquiredAt = acquiredAt;
36
+ this.#release = release;
37
+ Object.freeze(this);
38
+ }
39
+
40
+ async release() {
41
+ if (this.#releaseResult !== null) {
42
+ return Object.freeze({
43
+ ...this.#releaseResult,
44
+ changed: false,
45
+ });
46
+ }
47
+ if (this.#releasePromise !== null) {
48
+ return await this.#releasePromise;
49
+ }
50
+ this.#releasePromise = Promise.resolve(this.#release())
51
+ .then((result) => {
52
+ this.#releaseResult = result;
53
+ return result;
54
+ })
55
+ .finally(() => {
56
+ this.#releasePromise = null;
57
+ });
58
+ return await this.#releasePromise;
59
+ }
60
+ }
61
+
62
+ function invalid(message, meta) {
63
+ return createCasError(message, ErrorCodes.CACHE_ACQUISITION_INVALID, meta);
64
+ }
65
+
66
+ function assertEvidence({ id, hit, evidence, acquiredAt }) {
67
+ const acquisitionRef = CacheAcquisitionRef.forId({
68
+ namespace: evidence.root.namespace,
69
+ id,
70
+ });
71
+ const valid = evidence.policy === 'pinned'
72
+ && evidence.reachability === 'anchored'
73
+ && evidence.root.kind === 'cache-set'
74
+ && evidence.root.ref === acquisitionRef.toString()
75
+ && evidence.root.namespace === hit.evidence.root.namespace
76
+ && evidence.root.generation === hit.generation
77
+ && evidence.handle.toString() === hit.handle.toString()
78
+ && evidence.observedAt === acquiredAt
79
+ && acquisitionRef.acquiredAt === acquiredAt;
80
+ if (!valid) {
81
+ throw invalid('Cache acquisition retention evidence does not match its acquired hit', {
82
+ id,
83
+ acquiredAt,
84
+ generation: hit.generation,
85
+ evidence: evidence.toJSON(),
86
+ });
87
+ }
88
+ }
@@ -0,0 +1,129 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import CollectionNamespace from './CollectionNamespace.js';
5
+
6
+ export const CACHE_ACQUISITION_REF_PREFIX = 'refs/cas/cache-acquisitions/';
7
+
8
+ const ID_PATTERN = /^v1-(\d{13})-([0-9a-f]{64})-([0-9a-f]{32})$/u;
9
+
10
+ /** Immutable identity for one scoped cache-generation anchor. */
11
+ export default class CacheAcquisitionRef {
12
+ #acquiredAt;
13
+ #id;
14
+ #keyDigest;
15
+ #namespace;
16
+ #value;
17
+
18
+ constructor(value) {
19
+ if (typeof value !== 'string' || !value.startsWith(CACHE_ACQUISITION_REF_PREFIX)) {
20
+ throw invalid('Cache acquisition ref is outside its managed namespace', { ref: value });
21
+ }
22
+ const parts = value.slice(CACHE_ACQUISITION_REF_PREFIX.length).split('/');
23
+ if (parts.length !== 2) {
24
+ throw invalid('Cache acquisition ref must include a cache namespace and ID', { ref: value });
25
+ }
26
+ const [encodedNamespace, id] = parts;
27
+ const match = ID_PATTERN.exec(id);
28
+ if (!match) {
29
+ throw invalid('Cache acquisition ID is invalid', { ref: value, id });
30
+ }
31
+ const acquiredAt = new Date(Number(match[1])).toISOString();
32
+ this.#namespace = decodeNamespaceSegment(encodedNamespace, { ref: value, id });
33
+ this.#id = id;
34
+ this.#acquiredAt = acquiredAt;
35
+ this.#keyDigest = match[2];
36
+ this.#value = `${CacheAcquisitionRef.prefixForNamespace(this.#namespace)}${id}`;
37
+ Object.freeze(this);
38
+ }
39
+
40
+ static create({ namespace, keyDigest, acquiredAt, nonce }) {
41
+ assertCanonicalTimestamp(acquiredAt, {
42
+ invalid,
43
+ message: 'Cache acquisition time must be a canonical UTC timestamp',
44
+ });
45
+ if (typeof keyDigest !== 'string' || !/^[0-9a-f]{64}$/u.test(keyDigest)) {
46
+ throw invalid('Cache acquisition key digest is invalid', { keyDigest });
47
+ }
48
+ if (typeof nonce !== 'string' || !/^[0-9a-f]{32}$/u.test(nonce)) {
49
+ throw invalid('Cache acquisition nonce is invalid', { nonce });
50
+ }
51
+ const epoch = String(Date.parse(acquiredAt)).padStart(13, '0');
52
+ return new CacheAcquisitionRef(
53
+ `${CacheAcquisitionRef.prefixForNamespace(namespace)}v1-${epoch}-${keyDigest}-${nonce}`,
54
+ );
55
+ }
56
+
57
+ static from(value) {
58
+ return value instanceof CacheAcquisitionRef ? value : new CacheAcquisitionRef(value);
59
+ }
60
+
61
+ static forId({ namespace, id }) {
62
+ if (typeof id !== 'string' || !ID_PATTERN.test(id)) {
63
+ throw invalid('Cache acquisition ID is invalid', { namespace, id });
64
+ }
65
+ return new CacheAcquisitionRef(`${CacheAcquisitionRef.prefixForNamespace(namespace)}${id}`);
66
+ }
67
+
68
+ static prefixForNamespace(namespace) {
69
+ const canonical = acquisitionNamespace(namespace, { namespace }).toString();
70
+ return `${CACHE_ACQUISITION_REF_PREFIX}${encodeURIComponent(canonical)}/`;
71
+ }
72
+
73
+ get acquiredAt() {
74
+ return this.#acquiredAt;
75
+ }
76
+
77
+ get id() {
78
+ return this.#id;
79
+ }
80
+
81
+ get keyDigest() {
82
+ return this.#keyDigest;
83
+ }
84
+
85
+ get namespace() {
86
+ return this.#namespace.toString();
87
+ }
88
+
89
+ toString() {
90
+ return this.#value;
91
+ }
92
+ }
93
+
94
+ function invalid(message, meta) {
95
+ return createCasError(message, ErrorCodes.CACHE_ACQUISITION_INVALID, meta);
96
+ }
97
+
98
+ function acquisitionNamespace(value, meta) {
99
+ try {
100
+ return CollectionNamespace.from(value);
101
+ } catch (error) {
102
+ throw invalid('Cache acquisition namespace is invalid', {
103
+ ...meta,
104
+ namespace: value,
105
+ originalError: error,
106
+ });
107
+ }
108
+ }
109
+
110
+ function decodeNamespaceSegment(value, meta) {
111
+ let decoded;
112
+ try {
113
+ decoded = decodeURIComponent(value);
114
+ } catch (error) {
115
+ throw invalid('Cache acquisition namespace encoding is invalid', {
116
+ ...meta,
117
+ namespace: value,
118
+ originalError: error,
119
+ });
120
+ }
121
+ const namespace = acquisitionNamespace(decoded, meta);
122
+ if (encodeURIComponent(namespace.toString()) !== value) {
123
+ throw invalid('Cache acquisition namespace encoding is not canonical', {
124
+ ...meta,
125
+ namespace: value,
126
+ });
127
+ }
128
+ return namespace;
129
+ }
@@ -0,0 +1,93 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import parseApplicationHandle from './ApplicationHandle.js';
5
+ import CacheKey from './CacheKey.js';
6
+ import Oid from './Oid.js';
7
+ import RetentionWitness from './RetentionWitness.js';
8
+
9
+ const RETENTION = Object.freeze(['pinned', 'evictable']);
10
+
11
+ /** Immutable result for one live cache entry. */
12
+ export default class CacheHit {
13
+ constructor(value) {
14
+ CacheHit.#assertValue(value);
15
+ const key = CacheKey.from(value.key).toString();
16
+ const handle = parseApplicationHandle(value.handle);
17
+ const evidence = value.evidence instanceof RetentionWitness
18
+ ? value.evidence
19
+ : new RetentionWitness(value.evidence);
20
+ const generation = Oid.from(value.generation).toString();
21
+ CacheHit.#assertEvidence({ value, handle, evidence, generation });
22
+ this.key = key;
23
+ this.handle = handle;
24
+ this.policy = value.policy;
25
+ this.expiresAt = value.expiresAt;
26
+ this.logicalBytes = value.logicalBytes;
27
+ this.createdAt = value.createdAt;
28
+ this.accessedAt = value.accessedAt;
29
+ this.generation = generation;
30
+ this.evidence = evidence;
31
+ Object.freeze(this);
32
+ }
33
+
34
+ toJSON() {
35
+ return {
36
+ key: this.key,
37
+ handle: this.handle.toString(),
38
+ policy: this.policy,
39
+ expiresAt: this.expiresAt,
40
+ logicalBytes: this.logicalBytes,
41
+ createdAt: this.createdAt,
42
+ accessedAt: this.accessedAt,
43
+ generation: this.generation,
44
+ evidence: this.evidence.toJSON(),
45
+ };
46
+ }
47
+
48
+ static #assertValue(value) {
49
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
50
+ throw CacheHit.#invalid('Cache hit must be an object', { value });
51
+ }
52
+ if (typeof value.key !== 'string' || value.key.length === 0) {
53
+ throw CacheHit.#invalid('Cache hit key must be a non-empty string', { key: value.key });
54
+ }
55
+ if (!RETENTION.includes(value.policy)) {
56
+ throw CacheHit.#invalid('Cache hit policy must be pinned or evictable', { policy: value.policy });
57
+ }
58
+ if (!Number.isSafeInteger(value.logicalBytes) || value.logicalBytes < 0) {
59
+ throw CacheHit.#invalid('Cache hit logicalBytes must be a non-negative safe integer', {
60
+ logicalBytes: value.logicalBytes,
61
+ });
62
+ }
63
+ CacheHit.#assertTimestamp(value.expiresAt, true);
64
+ CacheHit.#assertTimestamp(value.createdAt, false);
65
+ CacheHit.#assertTimestamp(value.accessedAt, false);
66
+ }
67
+
68
+ static #assertTimestamp(value, nullable) {
69
+ if (nullable && value === null) {
70
+ return;
71
+ }
72
+ assertCanonicalTimestamp(value, {
73
+ invalid: CacheHit.#invalid,
74
+ message: 'Cache hit timestamps must be canonical UTC timestamps',
75
+ });
76
+ }
77
+
78
+ static #assertEvidence({ value, handle, evidence, generation }) {
79
+ if (evidence.policy !== value.policy ||
80
+ evidence.root.generation !== generation ||
81
+ evidence.handle.toString() !== handle.toString() ||
82
+ evidence.root.kind !== 'cache-set') {
83
+ throw CacheHit.#invalid('Cache hit evidence does not match its entry', {
84
+ policy: value.policy,
85
+ generation,
86
+ });
87
+ }
88
+ }
89
+
90
+ static #invalid(message, meta) {
91
+ return createCasError(message, ErrorCodes.CACHE_ENTRY_INVALID, meta);
92
+ }
93
+ }
@@ -0,0 +1,32 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import isCanonicalCollectionKey from '../helpers/isCanonicalCollectionKey.js';
4
+
5
+ /** Immutable canonical cache key. */
6
+ export default class CacheKey {
7
+ #value;
8
+
9
+ constructor(value) {
10
+ if (!CacheKey.isValid(value)) {
11
+ throw createCasError(
12
+ 'Cache key must be canonical Unicode without control characters',
13
+ ErrorCodes.CACHE_KEY_INVALID,
14
+ { key: value },
15
+ );
16
+ }
17
+ this.#value = value;
18
+ Object.freeze(this);
19
+ }
20
+
21
+ static from(value) {
22
+ return value instanceof CacheKey ? value : new CacheKey(value);
23
+ }
24
+
25
+ static isValid(value) {
26
+ return isCanonicalCollectionKey(value);
27
+ }
28
+
29
+ toString() {
30
+ return this.#value;
31
+ }
32
+ }
@@ -0,0 +1,59 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+
4
+ export const MAX_CACHE_ENTRIES = 99_999;
5
+ export const DEFAULT_CACHE_POLICY = Object.freeze({
6
+ maxEntries: 10_000,
7
+ maxBytes: null,
8
+ accessResolutionMs: 3_600_000,
9
+ });
10
+
11
+ /** Immutable admission and eviction policy for one CacheSet. */
12
+ export default class CachePolicy {
13
+ constructor(value = {}) {
14
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
15
+ throw CachePolicy.#invalid('Cache policy must be an object', { policy: value });
16
+ }
17
+ const policy = { ...DEFAULT_CACHE_POLICY, ...value };
18
+ CachePolicy.#assertBound('maxEntries', policy.maxEntries, {
19
+ minimum: 1,
20
+ maximum: MAX_CACHE_ENTRIES,
21
+ });
22
+ CachePolicy.#assertNullableBound('maxBytes', policy.maxBytes);
23
+ CachePolicy.#assertBound('accessResolutionMs', policy.accessResolutionMs, { minimum: 0 });
24
+ this.maxEntries = policy.maxEntries;
25
+ this.maxBytes = policy.maxBytes;
26
+ this.accessResolutionMs = policy.accessResolutionMs;
27
+ Object.freeze(this);
28
+ }
29
+
30
+ static from(value) {
31
+ return value instanceof CachePolicy ? value : new CachePolicy(value);
32
+ }
33
+
34
+ toJSON() {
35
+ return {
36
+ maxEntries: this.maxEntries,
37
+ maxBytes: this.maxBytes,
38
+ accessResolutionMs: this.accessResolutionMs,
39
+ };
40
+ }
41
+
42
+ static #assertNullableBound(name, value) {
43
+ if (value !== null) {
44
+ CachePolicy.#assertBound(name, value, { minimum: 0 });
45
+ }
46
+ }
47
+
48
+ static #assertBound(name, value, { minimum, maximum = Number.MAX_SAFE_INTEGER }) {
49
+ if (!Number.isSafeInteger(value) || value < minimum || value > maximum) {
50
+ throw CachePolicy.#invalid(`Cache policy ${name} is outside its supported range`, {
51
+ name, value, minimum, maximum,
52
+ });
53
+ }
54
+ }
55
+
56
+ static #invalid(message, meta) {
57
+ return createCasError(message, ErrorCodes.CACHE_POLICY_INVALID, meta);
58
+ }
59
+ }
@@ -0,0 +1,40 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import CollectionNamespace from './CollectionNamespace.js';
4
+
5
+ export const CACHE_SET_REF_PREFIX = 'refs/cas/caches/';
6
+
7
+ /** Immutable ref in the managed cache-set namespace. */
8
+ export default class CacheSetRef {
9
+ #namespace;
10
+ #value;
11
+
12
+ constructor(value) {
13
+ if (typeof value !== 'string' || !value.startsWith(CACHE_SET_REF_PREFIX)) {
14
+ throw createCasError(
15
+ `Cache-set ref must be below ${CACHE_SET_REF_PREFIX}`,
16
+ ErrorCodes.COLLECTION_NAMESPACE_INVALID,
17
+ { ref: value },
18
+ );
19
+ }
20
+ this.#namespace = CollectionNamespace.from(value.slice(CACHE_SET_REF_PREFIX.length));
21
+ this.#value = `${CACHE_SET_REF_PREFIX}${this.#namespace}`;
22
+ Object.freeze(this);
23
+ }
24
+
25
+ static from(value) {
26
+ return value instanceof CacheSetRef ? value : new CacheSetRef(value);
27
+ }
28
+
29
+ static forNamespace(namespace) {
30
+ return new CacheSetRef(`${CACHE_SET_REF_PREFIX}${CollectionNamespace.from(namespace)}`);
31
+ }
32
+
33
+ get namespace() {
34
+ return this.#namespace.toString();
35
+ }
36
+
37
+ toString() {
38
+ return this.#value;
39
+ }
40
+ }
@@ -0,0 +1,54 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+
4
+ const MAX_COMPONENTS = 16;
5
+ const MAX_NAMESPACE_BYTES = 240;
6
+ const COMPONENT = /^[a-z0-9](?:[a-z0-9.-]{0,62}[a-z0-9])?$/;
7
+
8
+ /** Immutable namespace shared by managed application collections. */
9
+ export default class CollectionNamespace {
10
+ #value;
11
+
12
+ constructor(value) {
13
+ if (!CollectionNamespace.isValid(value)) {
14
+ throw createCasError(
15
+ 'Collection namespace must be canonical lowercase ASCII',
16
+ ErrorCodes.COLLECTION_NAMESPACE_INVALID,
17
+ { namespace: value },
18
+ );
19
+ }
20
+ this.#value = value;
21
+ Object.freeze(this);
22
+ }
23
+
24
+ static from(value) {
25
+ return value instanceof CollectionNamespace ? value : new CollectionNamespace(value);
26
+ }
27
+
28
+ static isValid(value) {
29
+ if (typeof value !== 'string' || value.length === 0 || value.length > MAX_NAMESPACE_BYTES) {
30
+ return false;
31
+ }
32
+ if (!CollectionNamespace.#isAscii(value) || value.includes('..') || value.includes('//')) {
33
+ return false;
34
+ }
35
+ const components = value.split('/');
36
+ return components.length <= MAX_COMPONENTS && components.every(CollectionNamespace.#validComponent);
37
+ }
38
+
39
+ static #isAscii(value) {
40
+ return [...value].every((character) => character.codePointAt(0) <= 0x7f);
41
+ }
42
+
43
+ static #validComponent(component) {
44
+ return COMPONENT.test(component) &&
45
+ component !== '.' &&
46
+ component !== '..' &&
47
+ !component.endsWith('.lock') &&
48
+ !component.startsWith('git-cas-');
49
+ }
50
+
51
+ toString() {
52
+ return this.#value;
53
+ }
54
+ }
@@ -0,0 +1,81 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import Oid from './Oid.js';
5
+ import RetentionWitness from './RetentionWitness.js';
6
+
7
+ const DIGEST = /^[0-9a-f]{64}$/;
8
+ const INDEX_ROOT_PATH = 'root-00000000';
9
+
10
+ /** Immutable evidence for one live replay marker. */
11
+ export default class ExpiringMarker {
12
+ constructor({ keyDigest, expiresAt, createdAt, generation, evidence }) {
13
+ if (typeof keyDigest !== 'string' || !DIGEST.test(keyDigest)) {
14
+ throw invalid('Expiring marker key digest is invalid', { keyDigest });
15
+ }
16
+ assertCanonicalTimestamp(createdAt, {
17
+ invalid,
18
+ message: 'Expiring marker creation time must be canonical UTC',
19
+ });
20
+ assertCanonicalTimestamp(expiresAt, {
21
+ invalid,
22
+ message: 'Expiring marker expiry must be canonical UTC',
23
+ });
24
+ if (expiresAt <= createdAt) {
25
+ throw invalid('Expiring marker expiry must follow its creation time', {
26
+ createdAt,
27
+ expiresAt,
28
+ });
29
+ }
30
+ const normalizedGeneration = Oid.from(generation).toString();
31
+ if (evidence === null || evidence === undefined) {
32
+ throw invalid('Expiring marker evidence is required', { evidence });
33
+ }
34
+ const normalizedEvidence = evidence instanceof RetentionWitness
35
+ ? evidence
36
+ : new RetentionWitness(evidence);
37
+ assertEvidence({
38
+ evidence: normalizedEvidence,
39
+ expiresAt,
40
+ generation: normalizedGeneration,
41
+ });
42
+
43
+ this.version = 1;
44
+ this.keyDigest = keyDigest;
45
+ this.expiresAt = expiresAt;
46
+ this.createdAt = createdAt;
47
+ this.generation = normalizedGeneration;
48
+ this.evidence = normalizedEvidence;
49
+ Object.freeze(this);
50
+ }
51
+
52
+ toJSON() {
53
+ return {
54
+ version: this.version,
55
+ keyDigest: this.keyDigest,
56
+ expiresAt: this.expiresAt,
57
+ createdAt: this.createdAt,
58
+ generation: this.generation,
59
+ evidence: this.evidence.toJSON(),
60
+ };
61
+ }
62
+ }
63
+
64
+ function invalid(message, meta) {
65
+ return createCasError(message, ErrorCodes.EXPIRING_SET_MARKER_INVALID, meta);
66
+ }
67
+
68
+ function assertEvidence({ evidence, expiresAt, generation }) {
69
+ if (evidence.policy !== 'pinned' ||
70
+ evidence.reachability !== 'anchored' ||
71
+ evidence.root.kind !== 'expiring-set' ||
72
+ evidence.root.generation !== generation ||
73
+ evidence.root.path !== INDEX_ROOT_PATH ||
74
+ evidence.observedAt >= expiresAt ||
75
+ evidence.handle.kind !== 'page') {
76
+ throw invalid('Expiring marker evidence is inconsistent', {
77
+ generation,
78
+ evidence: evidence.toJSON(),
79
+ });
80
+ }
81
+ }
@@ -0,0 +1,28 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import isCanonicalCollectionKey from '../helpers/isCanonicalCollectionKey.js';
4
+
5
+ /** Immutable canonical input key for a replay-safe ExpiringSet. */
6
+ export default class ExpiringSetKey {
7
+ #value;
8
+
9
+ constructor(value) {
10
+ if (!isCanonicalCollectionKey(value)) {
11
+ throw createCasError(
12
+ 'ExpiringSet key must be canonical Unicode without control characters',
13
+ ErrorCodes.EXPIRING_SET_KEY_INVALID,
14
+ { key: value },
15
+ );
16
+ }
17
+ this.#value = value;
18
+ Object.freeze(this);
19
+ }
20
+
21
+ static from(value) {
22
+ return value instanceof ExpiringSetKey ? value : new ExpiringSetKey(value);
23
+ }
24
+
25
+ toString() {
26
+ return this.#value;
27
+ }
28
+ }
@@ -0,0 +1,42 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import CollectionNamespace from './CollectionNamespace.js';
4
+
5
+ export const EXPIRING_SET_REF_PREFIX = 'refs/cas/expiring/';
6
+
7
+ /** Immutable ref in the managed expiring-set namespace. */
8
+ export default class ExpiringSetRef {
9
+ #namespace;
10
+ #value;
11
+
12
+ constructor(value) {
13
+ if (typeof value !== 'string' || !value.startsWith(EXPIRING_SET_REF_PREFIX)) {
14
+ throw createCasError(
15
+ `Expiring-set ref must be below ${EXPIRING_SET_REF_PREFIX}`,
16
+ ErrorCodes.COLLECTION_NAMESPACE_INVALID,
17
+ { ref: value },
18
+ );
19
+ }
20
+ this.#namespace = CollectionNamespace.from(value.slice(EXPIRING_SET_REF_PREFIX.length));
21
+ this.#value = `${EXPIRING_SET_REF_PREFIX}${this.#namespace}`;
22
+ Object.freeze(this);
23
+ }
24
+
25
+ static from(value) {
26
+ return value instanceof ExpiringSetRef ? value : new ExpiringSetRef(value);
27
+ }
28
+
29
+ static forNamespace(namespace) {
30
+ return new ExpiringSetRef(
31
+ `${EXPIRING_SET_REF_PREFIX}${CollectionNamespace.from(namespace)}`,
32
+ );
33
+ }
34
+
35
+ get namespace() {
36
+ return this.#namespace.toString();
37
+ }
38
+
39
+ toString() {
40
+ return this.#value;
41
+ }
42
+ }