@git-stunts/git-cas 6.0.1 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/ARCHITECTURE.md +120 -6
  2. package/CHANGELOG.md +98 -1
  3. package/GUIDE.md +114 -13
  4. package/README.md +60 -4
  5. package/UPGRADING.md +71 -1
  6. package/build-info.json +2 -2
  7. package/docs/API.md +934 -7
  8. package/docs/WALKTHROUGH.md +46 -0
  9. package/docs/releases/v6.1.0.md +75 -0
  10. package/docs/releases/v6.2.0.md +131 -0
  11. package/index.d.ts +974 -1
  12. package/index.js +354 -1
  13. package/package.json +8 -6
  14. package/src/domain/errors/Codes.js +48 -0
  15. package/src/domain/helpers/assertCanonicalTimestamp.js +15 -0
  16. package/src/domain/helpers/handleTarget.js +48 -0
  17. package/src/domain/helpers/isCanonicalCollectionKey.js +35 -0
  18. package/src/domain/services/AssetService.js +213 -0
  19. package/src/domain/services/BundleDescriptorCodec.js +286 -0
  20. package/src/domain/services/BundleFanoutBuilder.js +247 -0
  21. package/src/domain/services/BundleService.js +848 -0
  22. package/src/domain/services/CacheCandidateHeap.js +71 -0
  23. package/src/domain/services/CacheIndex.js +362 -0
  24. package/src/domain/services/CacheMetadataCodec.js +164 -0
  25. package/src/domain/services/CachePolicyEnforcer.js +144 -0
  26. package/src/domain/services/CacheSet.js +703 -0
  27. package/src/domain/services/CacheSetRegistry.js +88 -0
  28. package/src/domain/services/CasService.d.ts +3 -1
  29. package/src/domain/services/ExpiringSet.js +496 -0
  30. package/src/domain/services/ExpiringSetIndex.js +254 -0
  31. package/src/domain/services/ExpiringSetMetadataCodec.js +160 -0
  32. package/src/domain/services/ExpiringSetRegistry.js +101 -0
  33. package/src/domain/services/PageService.js +175 -0
  34. package/src/domain/services/PublicationService.js +315 -0
  35. package/src/domain/services/RepositoryDoctor.js +649 -0
  36. package/src/domain/services/RetentionService.js +120 -0
  37. package/src/domain/services/RootSet.js +281 -0
  38. package/src/domain/services/RootSetMetadataCodec.js +259 -0
  39. package/src/domain/services/RootSetPersistence.js +335 -0
  40. package/src/domain/services/RootSetRegistry.js +44 -0
  41. package/src/domain/services/RootSetRetryPolicy.js +87 -0
  42. package/src/domain/services/RootSetTreeCodec.js +95 -0
  43. package/src/domain/services/StagingEvidence.js +39 -0
  44. package/src/domain/value-objects/ApplicationHandle.js +40 -0
  45. package/src/domain/value-objects/AssetHandle.js +163 -0
  46. package/src/domain/value-objects/BundleHandle.js +32 -0
  47. package/src/domain/value-objects/BundleLimits.js +80 -0
  48. package/src/domain/value-objects/BundlePath.js +78 -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 +101 -0
  60. package/src/domain/value-objects/RootSetRef.js +88 -0
  61. package/src/domain/value-objects/StagedAsset.js +63 -0
  62. package/src/domain/value-objects/StagedBundle.js +66 -0
  63. package/src/domain/value-objects/StagedPage.js +47 -0
  64. package/src/infrastructure/adapters/GitPersistenceAdapter.js +57 -0
  65. package/src/infrastructure/adapters/GitRefAdapter.js +26 -5
  66. package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +164 -0
  67. package/src/package-version.js +1 -1
  68. package/src/ports/GitPersistencePort.js +20 -1
  69. package/src/ports/GitRefPort.js +10 -0
  70. package/src/ports/RepositoryInspectionPort.js +33 -0
@@ -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
+ }
@@ -0,0 +1,171 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import Oid from './Oid.js';
4
+
5
+ const TOKEN_PREFIX = 'git-cas';
6
+ const CODEC_PATTERN = /^[a-z0-9][a-z0-9.-]{0,31}$/u;
7
+
8
+ /**
9
+ * Shared immutable envelope for application-facing content handles.
10
+ */
11
+ export default class OpaqueHandle {
12
+ /**
13
+ * @param {object} value
14
+ * @param {{ version: number, kind: string, format: string, codec?: string, label: string }} spec
15
+ */
16
+ constructor(value, spec) {
17
+ assertObject(value, spec);
18
+ assertEnvelope(value, spec);
19
+
20
+ let oid;
21
+ try {
22
+ oid = Oid.from(value.oid).toString();
23
+ } catch (error) {
24
+ throw invalid(spec, `${spec.label} handle contains an invalid Git object identifier`, {
25
+ value,
26
+ originalError: error,
27
+ });
28
+ }
29
+ const hashAlgorithm = oid.length === 40 ? 'sha1' : 'sha256';
30
+ if (value.hashAlgorithm !== undefined && value.hashAlgorithm !== hashAlgorithm) {
31
+ throw invalid(spec, `${spec.label} handle hash algorithm does not match its object identifier`, {
32
+ hashAlgorithm: value.hashAlgorithm,
33
+ oid,
34
+ });
35
+ }
36
+
37
+ this.version = spec.version;
38
+ this.kind = spec.kind;
39
+ this.format = spec.format;
40
+ this.codec = spec.codec ?? value.codec;
41
+ this.hashAlgorithm = hashAlgorithm;
42
+ this.oid = oid;
43
+ Object.freeze(this);
44
+ }
45
+
46
+ /**
47
+ * @param {unknown} value
48
+ * @param {new (value: object) => OpaqueHandle} Type
49
+ * @param {object} spec
50
+ * @returns {OpaqueHandle}
51
+ */
52
+ static from(value, Type, spec) {
53
+ if (value instanceof Type) {
54
+ return value;
55
+ }
56
+ if (typeof value === 'string') {
57
+ return OpaqueHandle.parse(value, Type, spec);
58
+ }
59
+ return new Type(value);
60
+ }
61
+
62
+ /**
63
+ * @param {unknown} token
64
+ * @param {new (value: object) => OpaqueHandle} Type
65
+ * @param {object} spec
66
+ * @returns {OpaqueHandle}
67
+ */
68
+ static parse(token, Type, spec) {
69
+ if (typeof token !== 'string') {
70
+ throw invalid(spec, `${spec.label} handle token must be a string`, { token });
71
+ }
72
+ const fields = token.split(':');
73
+ if (fields.length !== 7 || fields[0] !== TOKEN_PREFIX) {
74
+ throw invalid(spec, `${spec.label} handle token has an invalid shape`, { token });
75
+ }
76
+ const [, version, kind, format, codec, hashAlgorithm, oid] = fields;
77
+ if (kind !== spec.kind) {
78
+ throw createCasError(`Handle kind is not ${spec.kind}`, ErrorCodes.HANDLE_KIND_MISMATCH, {
79
+ expectedKind: spec.kind,
80
+ actualKind: kind,
81
+ });
82
+ }
83
+ const handle = new Type({
84
+ version: Number(version),
85
+ kind,
86
+ format,
87
+ codec,
88
+ hashAlgorithm,
89
+ oid,
90
+ });
91
+ if (handle.toString() !== token) {
92
+ throw invalid(spec, `${spec.label} handle token is not canonical`, { token });
93
+ }
94
+ return handle;
95
+ }
96
+
97
+ /** @returns {string} */
98
+ toString() {
99
+ return [
100
+ TOKEN_PREFIX,
101
+ this.version,
102
+ this.kind,
103
+ this.format,
104
+ this.codec,
105
+ this.hashAlgorithm,
106
+ this.oid,
107
+ ].join(':');
108
+ }
109
+
110
+ /** @returns {object} */
111
+ toJSON() {
112
+ return {
113
+ version: this.version,
114
+ kind: this.kind,
115
+ format: this.format,
116
+ codec: this.codec,
117
+ hashAlgorithm: this.hashAlgorithm,
118
+ oid: this.oid,
119
+ };
120
+ }
121
+ }
122
+
123
+ function assertObject(value, spec) {
124
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
125
+ throw invalid(spec, `${spec.label} handle must be an object or canonical token`, { value });
126
+ }
127
+ }
128
+
129
+ function assertEnvelope(value, spec) {
130
+ assertVersion(value, spec);
131
+ assertKind(value, spec);
132
+ assertFormat(value, spec);
133
+ assertCodec(value, spec);
134
+ }
135
+
136
+ function assertVersion(value, spec) {
137
+ if (value.version !== undefined && value.version !== spec.version) {
138
+ throw invalid(spec, `Unsupported ${spec.kind} handle version: ${value.version}`, { value });
139
+ }
140
+ }
141
+
142
+ function assertKind(value, spec) {
143
+ if (value.kind !== undefined && value.kind !== spec.kind) {
144
+ throw createCasError(`Handle kind is not ${spec.kind}`, ErrorCodes.HANDLE_KIND_MISMATCH, {
145
+ expectedKind: spec.kind,
146
+ actualKind: value.kind,
147
+ });
148
+ }
149
+ }
150
+
151
+ function assertFormat(value, spec) {
152
+ if (value.format !== undefined && value.format !== spec.format) {
153
+ throw invalid(spec, `Unsupported ${spec.kind} handle format: ${value.format}`, { value });
154
+ }
155
+ }
156
+
157
+ function assertCodec(value, spec) {
158
+ const codec = spec.codec ?? value.codec;
159
+ if (typeof codec !== 'string' || !CODEC_PATTERN.test(codec) || value.codec === '') {
160
+ throw invalid(spec, `${spec.label} handle codec must be a canonical lowercase identifier`, {
161
+ codec: value.codec,
162
+ });
163
+ }
164
+ if (spec.codec !== undefined && value.codec !== undefined && value.codec !== spec.codec) {
165
+ throw invalid(spec, `${spec.label} handle codec must be ${spec.codec}`, { codec: value.codec });
166
+ }
167
+ }
168
+
169
+ function invalid(spec, message, meta) {
170
+ return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
171
+ }
@@ -0,0 +1,34 @@
1
+ import OpaqueHandle from './OpaqueHandle.js';
2
+
3
+ export const PAGE_HANDLE_VERSION = 1;
4
+ export const PAGE_HANDLE_KIND = 'page';
5
+ export const PAGE_HANDLE_FORMAT = 'blob';
6
+ export const PAGE_HANDLE_CODEC = 'raw';
7
+
8
+ const SPEC = Object.freeze({
9
+ version: PAGE_HANDLE_VERSION,
10
+ kind: PAGE_HANDLE_KIND,
11
+ format: PAGE_HANDLE_FORMAT,
12
+ codec: PAGE_HANDLE_CODEC,
13
+ label: 'Page',
14
+ });
15
+
16
+ /**
17
+ * Immutable, repository-independent locator for one raw page blob.
18
+ */
19
+ export default class PageHandle extends OpaqueHandle {
20
+ /** @param {object} value */
21
+ constructor(value) {
22
+ super(value, SPEC);
23
+ }
24
+
25
+ /** @param {PageHandle|string|object} value */
26
+ static from(value) {
27
+ return /** @type {PageHandle} */ (OpaqueHandle.from(value, PageHandle, SPEC));
28
+ }
29
+
30
+ /** @param {string} token */
31
+ static parse(token) {
32
+ return /** @type {PageHandle} */ (OpaqueHandle.parse(token, PageHandle, SPEC));
33
+ }
34
+ }
@@ -0,0 +1,101 @@
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 Oid from './Oid.js';
6
+
7
+ const POLICIES = Object.freeze(['pinned', 'evictable']);
8
+ const REACHABILITY = Object.freeze(['anchored', 'orphaned', 'volatile']);
9
+ const ROOT_KINDS = Object.freeze(['root-set', 'publication', 'cache-set', 'expiring-set']);
10
+
11
+ /**
12
+ * Immutable evidence that one observed Git generation retained a handle.
13
+ */
14
+ export default class RetentionWitness {
15
+ /**
16
+ * @param {object} value
17
+ * @param {import('./AssetHandle.js').default|import('./BundleHandle.js').default|import('./PageHandle.js').default|string|object} value.handle
18
+ * @param {'pinned'|'evictable'} value.policy
19
+ * @param {'anchored'|'orphaned'|'volatile'} value.reachability
20
+ * @param {object} value.root
21
+ * @param {string} value.observedAt
22
+ */
23
+ constructor({ handle, policy, reachability, root, observedAt }) {
24
+ if (!POLICIES.includes(policy)) {
25
+ throw RetentionWitness.#invalid('Retention witness policy must be pinned or evictable', {
26
+ policy,
27
+ });
28
+ }
29
+ if (!REACHABILITY.includes(reachability)) {
30
+ throw RetentionWitness.#invalid('Retention witness has an invalid reachability state', {
31
+ reachability,
32
+ });
33
+ }
34
+ const normalizedRoot = RetentionWitness.#normalizeRoot(root);
35
+ assertCanonicalTimestamp(observedAt, {
36
+ invalid: RetentionWitness.#invalid,
37
+ message: 'Retention witness observation time must be a canonical UTC timestamp',
38
+ });
39
+
40
+ this.version = 1;
41
+ this.handle = parseApplicationHandle(handle);
42
+ this.policy = policy;
43
+ this.reachability = reachability;
44
+ this.root = Object.freeze(normalizedRoot);
45
+ this.observedAt = observedAt;
46
+ Object.freeze(this);
47
+ }
48
+
49
+ /**
50
+ * @returns {object}
51
+ */
52
+ toJSON() {
53
+ return {
54
+ version: this.version,
55
+ handle: this.handle.toString(),
56
+ policy: this.policy,
57
+ reachability: this.reachability,
58
+ root: { ...this.root },
59
+ observedAt: this.observedAt,
60
+ };
61
+ }
62
+
63
+ static #normalizeRoot(root) {
64
+ if (!root || typeof root !== 'object' || Array.isArray(root)) {
65
+ throw RetentionWitness.#invalid('Retention witness root must be an object', { root });
66
+ }
67
+ if (!ROOT_KINDS.includes(root.kind)) {
68
+ throw RetentionWitness.#invalid('Retention witness root kind is invalid', { root });
69
+ }
70
+ for (const field of ['namespace', 'ref', 'path']) {
71
+ if (typeof root[field] !== 'string' || root[field].length === 0) {
72
+ throw RetentionWitness.#invalid(`Retention witness root ${field} is required`, { root });
73
+ }
74
+ }
75
+ if (!root.ref.startsWith('refs/')) {
76
+ throw RetentionWitness.#invalid('Retention witness root ref must be fully qualified', {
77
+ root,
78
+ });
79
+ }
80
+ let generation;
81
+ try {
82
+ generation = Oid.from(root.generation).toString();
83
+ } catch (error) {
84
+ throw RetentionWitness.#invalid('Retention witness root generation is invalid', {
85
+ root,
86
+ originalError: error,
87
+ });
88
+ }
89
+ return {
90
+ kind: root.kind,
91
+ namespace: root.namespace,
92
+ ref: root.ref,
93
+ generation,
94
+ path: root.path,
95
+ };
96
+ }
97
+
98
+ static #invalid(message, meta) {
99
+ return createCasError(message, ErrorCodes.RETENTION_WITNESS_INVALID, meta);
100
+ }
101
+ }
@@ -0,0 +1,88 @@
1
+ import CasError from '../errors/CasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+
4
+ export const ROOT_SET_REF_PREFIX = 'refs/cas/rootsets/';
5
+ const FORBIDDEN_REF_CHARS = '~^:?*[\\';
6
+
7
+ /**
8
+ * Immutable, validated ref in the git-cas root-set namespace.
9
+ */
10
+ export default class RootSetRef {
11
+ #value;
12
+
13
+ /**
14
+ * @param {string} value
15
+ */
16
+ constructor(value) {
17
+ if (!RootSetRef.isValid(value)) {
18
+ throw new CasError(
19
+ `Root-set ref must be a valid ref below ${ROOT_SET_REF_PREFIX}`,
20
+ ErrorCodes.ROOT_SET_REF_INVALID,
21
+ { ref: value, prefix: ROOT_SET_REF_PREFIX },
22
+ );
23
+ }
24
+ this.#value = value;
25
+ Object.freeze(this);
26
+ }
27
+
28
+ /**
29
+ * @param {string|RootSetRef} value
30
+ * @returns {RootSetRef}
31
+ */
32
+ static from(value) {
33
+ return value instanceof RootSetRef ? value : new RootSetRef(value);
34
+ }
35
+
36
+ /**
37
+ * @param {unknown} value
38
+ * @returns {boolean}
39
+ */
40
+ static isValid(value) {
41
+ if (typeof value !== 'string' || !value.startsWith(ROOT_SET_REF_PREFIX)) {
42
+ return false;
43
+ }
44
+ const suffix = value.slice(ROOT_SET_REF_PREFIX.length);
45
+ if (RootSetRef.#hasInvalidSuffixShape(suffix) || RootSetRef.#hasForbiddenChar(suffix)) {
46
+ return false;
47
+ }
48
+ return suffix.split('/').every(RootSetRef.#isValidComponent);
49
+ }
50
+
51
+ static #hasInvalidSuffixShape(suffix) {
52
+ return [
53
+ suffix.length === 0,
54
+ suffix.startsWith('/'),
55
+ suffix.endsWith('/'),
56
+ suffix.endsWith('.'),
57
+ suffix.includes('//'),
58
+ suffix.includes('..'),
59
+ suffix.includes('@{'),
60
+ ].some(Boolean);
61
+ }
62
+
63
+ static #hasForbiddenChar(value) {
64
+ for (const character of value) {
65
+ const codePoint = character.codePointAt(0);
66
+ if (codePoint <= 0x20 || codePoint === 0x7f || FORBIDDEN_REF_CHARS.includes(character)) {
67
+ return true;
68
+ }
69
+ }
70
+ return false;
71
+ }
72
+
73
+ static #isValidComponent(component) {
74
+ return [
75
+ component.length > 0,
76
+ component !== '.',
77
+ component !== '..',
78
+ !component.endsWith('.lock'),
79
+ ].every(Boolean);
80
+ }
81
+
82
+ /**
83
+ * @returns {string}
84
+ */
85
+ toString() {
86
+ return this.#value;
87
+ }
88
+ }
@@ -0,0 +1,63 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import AssetHandle from './AssetHandle.js';
5
+
6
+ /**
7
+ * Immutable result for an asset graph written without a reachability root.
8
+ */
9
+ export default class StagedAsset {
10
+ /**
11
+ * @param {object} value
12
+ * @param {AssetHandle|string|object} value.handle
13
+ * @param {string} value.slug
14
+ * @param {string} value.filename
15
+ * @param {number} value.size
16
+ * @param {string} value.observedAt
17
+ */
18
+ constructor({ handle, slug, filename, size, observedAt }) {
19
+ if (typeof slug !== 'string' || slug.length === 0) {
20
+ throw StagedAsset.#invalid('Staged asset slug must be a non-empty string', { slug });
21
+ }
22
+ if (typeof filename !== 'string' || filename.length === 0) {
23
+ throw StagedAsset.#invalid('Staged asset filename must be a non-empty string', { filename });
24
+ }
25
+ if (!Number.isSafeInteger(size) || size < 0) {
26
+ throw StagedAsset.#invalid('Staged asset size must be a non-negative safe integer', { size });
27
+ }
28
+ assertCanonicalTimestamp(observedAt, {
29
+ invalid: StagedAsset.#invalid,
30
+ message: 'Staged asset observation time must be a canonical UTC timestamp',
31
+ });
32
+
33
+ this.version = 1;
34
+ this.state = 'staged';
35
+ this.handle = AssetHandle.from(handle);
36
+ this.asset = Object.freeze({ slug, filename, size });
37
+ this.retention = Object.freeze({
38
+ policy: null,
39
+ reachability: 'unanchored',
40
+ protection: 'not-established',
41
+ });
42
+ this.observedAt = observedAt;
43
+ Object.freeze(this);
44
+ }
45
+
46
+ /**
47
+ * @returns {object}
48
+ */
49
+ toJSON() {
50
+ return {
51
+ version: this.version,
52
+ state: this.state,
53
+ handle: this.handle.toString(),
54
+ asset: { ...this.asset },
55
+ retention: { ...this.retention },
56
+ observedAt: this.observedAt,
57
+ };
58
+ }
59
+
60
+ static #invalid(message, meta) {
61
+ return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
62
+ }
63
+ }
@@ -0,0 +1,66 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+ import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
4
+ import BundleHandle from './BundleHandle.js';
5
+ import BundleLimits from './BundleLimits.js';
6
+
7
+ /**
8
+ * Immutable result for a bundle graph written without a reachability root.
9
+ */
10
+ export default class StagedBundle {
11
+ /**
12
+ * @param {object} value
13
+ * @param {BundleHandle|string|object} value.handle
14
+ * @param {number} value.memberCount
15
+ * @param {number} value.indexDepth
16
+ * @param {number} value.descriptorBytes
17
+ * @param {BundleLimits|object} value.limits
18
+ * @param {string} value.observedAt
19
+ */
20
+ constructor({ handle, memberCount, indexDepth, descriptorBytes, limits, observedAt }) {
21
+ for (const [field, number] of Object.entries({ memberCount, descriptorBytes })) {
22
+ if (!Number.isSafeInteger(number) || number < 0) {
23
+ throw StagedBundle.#invalid('Staged bundle metric is invalid', { field, value: number });
24
+ }
25
+ }
26
+ if (!Number.isSafeInteger(indexDepth) || indexDepth < 1) {
27
+ throw StagedBundle.#invalid('Staged bundle index depth is invalid', {
28
+ field: 'indexDepth',
29
+ value: indexDepth,
30
+ });
31
+ }
32
+ assertCanonicalTimestamp(observedAt, {
33
+ invalid: StagedBundle.#invalid,
34
+ message: 'Staged bundle observation time must be a canonical UTC timestamp',
35
+ });
36
+ this.version = 1;
37
+ this.state = 'staged';
38
+ this.handle = BundleHandle.from(handle);
39
+ this.bundle = Object.freeze({ memberCount, indexDepth, descriptorBytes });
40
+ this.limits = Object.freeze(new BundleLimits(limits).toJSON());
41
+ this.retention = Object.freeze({
42
+ policy: null,
43
+ reachability: 'unanchored',
44
+ protection: 'not-established',
45
+ });
46
+ this.observedAt = observedAt;
47
+ Object.freeze(this);
48
+ }
49
+
50
+ /** @returns {object} */
51
+ toJSON() {
52
+ return {
53
+ version: this.version,
54
+ state: this.state,
55
+ handle: this.handle.toString(),
56
+ bundle: { ...this.bundle },
57
+ limits: { ...this.limits },
58
+ retention: { ...this.retention },
59
+ observedAt: this.observedAt,
60
+ };
61
+ }
62
+
63
+ static #invalid(message, meta) {
64
+ return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
65
+ }
66
+ }