@git-stunts/git-cas 6.1.0 → 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 (64) hide show
  1. package/ARCHITECTURE.md +108 -3
  2. package/CHANGELOG.md +80 -2
  3. package/GUIDE.md +114 -13
  4. package/README.md +51 -4
  5. package/UPGRADING.md +43 -0
  6. package/build-info.json +2 -2
  7. package/docs/API.md +751 -16
  8. package/docs/releases/v6.2.0.md +131 -0
  9. package/index.d.ts +879 -2
  10. package/index.js +326 -1
  11. package/package.json +3 -2
  12. package/src/domain/errors/Codes.js +35 -0
  13. package/src/domain/helpers/assertCanonicalTimestamp.js +15 -0
  14. package/src/domain/helpers/handleTarget.js +48 -0
  15. package/src/domain/helpers/isCanonicalCollectionKey.js +35 -0
  16. package/src/domain/services/AssetService.js +213 -0
  17. package/src/domain/services/BundleDescriptorCodec.js +286 -0
  18. package/src/domain/services/BundleFanoutBuilder.js +247 -0
  19. package/src/domain/services/BundleService.js +848 -0
  20. package/src/domain/services/CacheCandidateHeap.js +71 -0
  21. package/src/domain/services/CacheIndex.js +362 -0
  22. package/src/domain/services/CacheMetadataCodec.js +164 -0
  23. package/src/domain/services/CachePolicyEnforcer.js +144 -0
  24. package/src/domain/services/CacheSet.js +703 -0
  25. package/src/domain/services/CacheSetRegistry.js +88 -0
  26. package/src/domain/services/CasService.d.ts +2 -1
  27. package/src/domain/services/ExpiringSet.js +496 -0
  28. package/src/domain/services/ExpiringSetIndex.js +254 -0
  29. package/src/domain/services/ExpiringSetMetadataCodec.js +160 -0
  30. package/src/domain/services/ExpiringSetRegistry.js +101 -0
  31. package/src/domain/services/PageService.js +175 -0
  32. package/src/domain/services/PublicationService.js +315 -0
  33. package/src/domain/services/RepositoryDoctor.js +649 -0
  34. package/src/domain/services/RetentionService.js +120 -0
  35. package/src/domain/services/RootSet.js +10 -6
  36. package/src/domain/services/RootSetMetadataCodec.js +8 -2
  37. package/src/domain/services/RootSetPersistence.js +3 -2
  38. package/src/domain/services/StagingEvidence.js +39 -0
  39. package/src/domain/value-objects/ApplicationHandle.js +40 -0
  40. package/src/domain/value-objects/AssetHandle.js +163 -0
  41. package/src/domain/value-objects/BundleHandle.js +32 -0
  42. package/src/domain/value-objects/BundleLimits.js +80 -0
  43. package/src/domain/value-objects/BundlePath.js +78 -0
  44. package/src/domain/value-objects/CacheHit.js +93 -0
  45. package/src/domain/value-objects/CacheKey.js +32 -0
  46. package/src/domain/value-objects/CachePolicy.js +59 -0
  47. package/src/domain/value-objects/CacheSetRef.js +40 -0
  48. package/src/domain/value-objects/CollectionNamespace.js +54 -0
  49. package/src/domain/value-objects/ExpiringMarker.js +81 -0
  50. package/src/domain/value-objects/ExpiringSetKey.js +28 -0
  51. package/src/domain/value-objects/ExpiringSetRef.js +42 -0
  52. package/src/domain/value-objects/OpaqueHandle.js +171 -0
  53. package/src/domain/value-objects/PageHandle.js +34 -0
  54. package/src/domain/value-objects/RetentionWitness.js +101 -0
  55. package/src/domain/value-objects/StagedAsset.js +63 -0
  56. package/src/domain/value-objects/StagedBundle.js +66 -0
  57. package/src/domain/value-objects/StagedPage.js +47 -0
  58. package/src/infrastructure/adapters/GitPersistenceAdapter.js +48 -20
  59. package/src/infrastructure/adapters/GitRefAdapter.js +6 -5
  60. package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +164 -0
  61. package/src/package-version.js +1 -1
  62. package/src/ports/GitPersistencePort.js +11 -1
  63. package/src/ports/GitRefPort.js +1 -0
  64. package/src/ports/RepositoryInspectionPort.js +33 -0
@@ -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
+ }
@@ -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
+ }