@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.
- package/ARCHITECTURE.md +108 -3
- package/CHANGELOG.md +80 -2
- package/GUIDE.md +114 -13
- package/README.md +51 -4
- package/UPGRADING.md +43 -0
- package/build-info.json +2 -2
- package/docs/API.md +751 -16
- package/docs/releases/v6.2.0.md +131 -0
- package/index.d.ts +879 -2
- package/index.js +326 -1
- package/package.json +3 -2
- package/src/domain/errors/Codes.js +35 -0
- package/src/domain/helpers/assertCanonicalTimestamp.js +15 -0
- package/src/domain/helpers/handleTarget.js +48 -0
- package/src/domain/helpers/isCanonicalCollectionKey.js +35 -0
- package/src/domain/services/AssetService.js +213 -0
- package/src/domain/services/BundleDescriptorCodec.js +286 -0
- package/src/domain/services/BundleFanoutBuilder.js +247 -0
- package/src/domain/services/BundleService.js +848 -0
- package/src/domain/services/CacheCandidateHeap.js +71 -0
- package/src/domain/services/CacheIndex.js +362 -0
- package/src/domain/services/CacheMetadataCodec.js +164 -0
- package/src/domain/services/CachePolicyEnforcer.js +144 -0
- package/src/domain/services/CacheSet.js +703 -0
- package/src/domain/services/CacheSetRegistry.js +88 -0
- package/src/domain/services/CasService.d.ts +2 -1
- package/src/domain/services/ExpiringSet.js +496 -0
- package/src/domain/services/ExpiringSetIndex.js +254 -0
- package/src/domain/services/ExpiringSetMetadataCodec.js +160 -0
- package/src/domain/services/ExpiringSetRegistry.js +101 -0
- package/src/domain/services/PageService.js +175 -0
- package/src/domain/services/PublicationService.js +315 -0
- package/src/domain/services/RepositoryDoctor.js +649 -0
- package/src/domain/services/RetentionService.js +120 -0
- package/src/domain/services/RootSet.js +10 -6
- package/src/domain/services/RootSetMetadataCodec.js +8 -2
- package/src/domain/services/RootSetPersistence.js +3 -2
- package/src/domain/services/StagingEvidence.js +39 -0
- package/src/domain/value-objects/ApplicationHandle.js +40 -0
- package/src/domain/value-objects/AssetHandle.js +163 -0
- package/src/domain/value-objects/BundleHandle.js +32 -0
- package/src/domain/value-objects/BundleLimits.js +80 -0
- package/src/domain/value-objects/BundlePath.js +78 -0
- package/src/domain/value-objects/CacheHit.js +93 -0
- package/src/domain/value-objects/CacheKey.js +32 -0
- package/src/domain/value-objects/CachePolicy.js +59 -0
- package/src/domain/value-objects/CacheSetRef.js +40 -0
- package/src/domain/value-objects/CollectionNamespace.js +54 -0
- package/src/domain/value-objects/ExpiringMarker.js +81 -0
- package/src/domain/value-objects/ExpiringSetKey.js +28 -0
- package/src/domain/value-objects/ExpiringSetRef.js +42 -0
- package/src/domain/value-objects/OpaqueHandle.js +171 -0
- package/src/domain/value-objects/PageHandle.js +34 -0
- package/src/domain/value-objects/RetentionWitness.js +101 -0
- package/src/domain/value-objects/StagedAsset.js +63 -0
- package/src/domain/value-objects/StagedBundle.js +66 -0
- package/src/domain/value-objects/StagedPage.js +47 -0
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +48 -20
- package/src/infrastructure/adapters/GitRefAdapter.js +6 -5
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +164 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +11 -1
- package/src/ports/GitRefPort.js +1 -0
- package/src/ports/RepositoryInspectionPort.js +33 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import RootSetMetadataCodec from './RootSetMetadataCodec.js';
|
|
4
|
+
import RootSetRef, { ROOT_SET_REF_PREFIX } from '../value-objects/RootSetRef.js';
|
|
5
|
+
import RetentionWitness from '../value-objects/RetentionWitness.js';
|
|
6
|
+
|
|
7
|
+
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Converts a validated content handle into a RootSet reachability edge.
|
|
11
|
+
*/
|
|
12
|
+
export default class RetentionService {
|
|
13
|
+
#clock;
|
|
14
|
+
#resolveRoot;
|
|
15
|
+
#rootSets;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {object} options
|
|
19
|
+
* @param {{ open(options: object): import('./RootSet.js').default }} options.rootSets
|
|
20
|
+
* @param {(handle: unknown) => Promise<object>} options.resolveRoot
|
|
21
|
+
* @param {{ now(): Date }} [options.clock]
|
|
22
|
+
*/
|
|
23
|
+
constructor({ rootSets, resolveRoot, clock = DEFAULT_CLOCK }) {
|
|
24
|
+
RetentionService.#assertDependencies(rootSets, resolveRoot, clock);
|
|
25
|
+
this.#rootSets = rootSets;
|
|
26
|
+
this.#resolveRoot = resolveRoot;
|
|
27
|
+
this.#clock = clock;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {object} options
|
|
32
|
+
* @param {unknown} options.handle
|
|
33
|
+
* @param {{ ref: string, name: string }} options.root
|
|
34
|
+
* @param {'pinned'|'evictable'} [options.policy]
|
|
35
|
+
* @returns {Promise<{ changed: boolean, witness: RetentionWitness }>}
|
|
36
|
+
*/
|
|
37
|
+
async retain({ handle, root, policy = 'pinned' }) {
|
|
38
|
+
RetentionService.#assertRoot(root);
|
|
39
|
+
const target = await this.#resolveRoot(handle);
|
|
40
|
+
const rootSet = this.#rootSets.open({ ref: root.ref });
|
|
41
|
+
const result = await rootSet.put({
|
|
42
|
+
name: root.name,
|
|
43
|
+
oid: target.oid,
|
|
44
|
+
type: target.type,
|
|
45
|
+
retention: policy,
|
|
46
|
+
});
|
|
47
|
+
const path = RetentionService.#evidencePath(result.entries, root.name);
|
|
48
|
+
const witness = new RetentionWitness({
|
|
49
|
+
handle: target.handle,
|
|
50
|
+
policy,
|
|
51
|
+
reachability: 'anchored',
|
|
52
|
+
root: {
|
|
53
|
+
kind: 'root-set',
|
|
54
|
+
namespace: root.ref.slice(ROOT_SET_REF_PREFIX.length),
|
|
55
|
+
ref: root.ref,
|
|
56
|
+
generation: result.commitOid,
|
|
57
|
+
path,
|
|
58
|
+
},
|
|
59
|
+
observedAt: this.#observedAt(),
|
|
60
|
+
});
|
|
61
|
+
return Object.freeze({ changed: result.changed, witness });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#observedAt() {
|
|
65
|
+
const now = this.#clock.now();
|
|
66
|
+
if (!(now instanceof Date) || Number.isNaN(now.getTime())) {
|
|
67
|
+
throw createCasError(
|
|
68
|
+
'RetentionService clock returned an invalid Date',
|
|
69
|
+
ErrorCodes.INVALID_OPTIONS
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
return now.toISOString();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static #evidencePath(entries, name) {
|
|
76
|
+
const index = entries.findIndex((entry) => entry.name === name);
|
|
77
|
+
if (index === -1) {
|
|
78
|
+
throw createCasError(
|
|
79
|
+
'Retained entry is absent from the committed root generation',
|
|
80
|
+
ErrorCodes.ROOT_SET_TREE_INVALID,
|
|
81
|
+
{ name }
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return RootSetMetadataCodec.slotFor(index);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static #assertRoot(root) {
|
|
88
|
+
if (!root || typeof root !== 'object' || Array.isArray(root)) {
|
|
89
|
+
throw createCasError('Retention root must be an object', ErrorCodes.INVALID_OPTIONS, {
|
|
90
|
+
root,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (typeof root.ref !== 'string' || typeof root.name !== 'string' || root.name.length === 0) {
|
|
94
|
+
throw createCasError(
|
|
95
|
+
'Retention root requires ref and name strings',
|
|
96
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
97
|
+
{ root }
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
RootSetRef.from(root.ref);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static #assertDependencies(rootSets, resolveRoot, clock) {
|
|
104
|
+
if (!rootSets || typeof rootSets.open !== 'function') {
|
|
105
|
+
throw createCasError(
|
|
106
|
+
'RetentionService requires a RootSet registry',
|
|
107
|
+
ErrorCodes.INVALID_OPTIONS
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (typeof resolveRoot !== 'function') {
|
|
111
|
+
throw createCasError(
|
|
112
|
+
'RetentionService requires a handle resolver',
|
|
113
|
+
ErrorCodes.INVALID_OPTIONS
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
if (!clock || typeof clock.now !== 'function') {
|
|
117
|
+
throw createCasError('RetentionService clock must provide now()', ErrorCodes.INVALID_OPTIONS);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -25,9 +25,10 @@ export default class RootSet {
|
|
|
25
25
|
ref,
|
|
26
26
|
persistence,
|
|
27
27
|
retry,
|
|
28
|
-
|
|
28
|
+
refType = RootSetRef,
|
|
29
|
+
metadataCodec = new RootSetMetadataCodec({ refType }),
|
|
29
30
|
}) {
|
|
30
|
-
this.ref =
|
|
31
|
+
this.ref = refType.from(ref).toString();
|
|
31
32
|
RootSet.#validatePersistence(persistence);
|
|
32
33
|
this.persistence = persistence;
|
|
33
34
|
this.#metadataCodec = metadataCodec;
|
|
@@ -108,7 +109,7 @@ export default class RootSet {
|
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
/**
|
|
111
|
-
* @param {(entries: ReadonlyArray<object>) => Iterable<object>|Promise<Iterable<object>>} mutator
|
|
112
|
+
* @param {(entries: ReadonlyArray<object>, state: Readonly<object>) => Iterable<object>|Promise<Iterable<object>>} mutator
|
|
112
113
|
* @param {{ expectedHeadOid?: string|null }} [options]
|
|
113
114
|
* @returns {Promise<object>}
|
|
114
115
|
*/
|
|
@@ -120,8 +121,11 @@ export default class RootSet {
|
|
|
120
121
|
{ mutatorType: typeof mutator },
|
|
121
122
|
);
|
|
122
123
|
}
|
|
123
|
-
return await this.#mutateEntries(async (entries) => (
|
|
124
|
-
await mutator(
|
|
124
|
+
return await this.#mutateEntries(async (entries, state) => (
|
|
125
|
+
await mutator(
|
|
126
|
+
Object.freeze(entries.map((entry) => Object.freeze({ ...entry }))),
|
|
127
|
+
Object.freeze({ ref: state.ref, headOid: state.headOid, treeOid: state.treeOid }),
|
|
128
|
+
)
|
|
125
129
|
), 'root-set: mutate current roots', RootSet.#normalizeExpectedHead(expectedHeadOid));
|
|
126
130
|
}
|
|
127
131
|
|
|
@@ -194,7 +198,7 @@ export default class RootSet {
|
|
|
194
198
|
for (let attempt = 0; attempt < this.#retry.maxAttempts; attempt++) {
|
|
195
199
|
const state = await this.read();
|
|
196
200
|
this.#assertExpectedHead(state.headOid, expectedHeadOid);
|
|
197
|
-
const entries = this.#metadataCodec.normalizeEntries(await transform(state.entries));
|
|
201
|
+
const entries = this.#metadataCodec.normalizeEntries(await transform(state.entries, state));
|
|
198
202
|
if (RootSet.#entriesEqual(state.entries, entries)) {
|
|
199
203
|
return {
|
|
200
204
|
changed: false,
|
|
@@ -14,6 +14,12 @@ const SLOT_PATTERN = /^root-[0-9]{8}$/;
|
|
|
14
14
|
* Canonical JSON codec for `.rootset.json` snapshots.
|
|
15
15
|
*/
|
|
16
16
|
export default class RootSetMetadataCodec {
|
|
17
|
+
#refType;
|
|
18
|
+
|
|
19
|
+
constructor({ refType = RootSetRef } = {}) {
|
|
20
|
+
this.#refType = refType;
|
|
21
|
+
}
|
|
22
|
+
|
|
17
23
|
/**
|
|
18
24
|
* @param {{ ref: string, entries: Iterable<object> }} options
|
|
19
25
|
* @returns {Uint8Array}
|
|
@@ -27,7 +33,7 @@ export default class RootSetMetadataCodec {
|
|
|
27
33
|
* @returns {{ version: 1, ref: string, entries: Array<object> }}
|
|
28
34
|
*/
|
|
29
35
|
create({ ref, entries }) {
|
|
30
|
-
const rootSetRef =
|
|
36
|
+
const rootSetRef = this.#refType.from(ref).toString();
|
|
31
37
|
return {
|
|
32
38
|
version: ROOT_SET_METADATA_VERSION,
|
|
33
39
|
ref: rootSetRef,
|
|
@@ -148,7 +154,7 @@ export default class RootSetMetadataCodec {
|
|
|
148
154
|
}
|
|
149
155
|
|
|
150
156
|
#assertExpectedRef(actualRef, expectedRef) {
|
|
151
|
-
if (expectedRef === undefined || actualRef ===
|
|
157
|
+
if (expectedRef === undefined || actualRef === this.#refType.from(expectedRef).toString()) {
|
|
152
158
|
return;
|
|
153
159
|
}
|
|
154
160
|
throw this.#metadataError('Root-set metadata ref does not match the requested ref', {
|
|
@@ -27,10 +27,11 @@ export default class RootSetPersistence {
|
|
|
27
27
|
rootSetRef,
|
|
28
28
|
persistence,
|
|
29
29
|
ref,
|
|
30
|
-
|
|
30
|
+
refType = RootSetRef,
|
|
31
|
+
metadataCodec = new RootSetMetadataCodec({ refType }),
|
|
31
32
|
treeCodec = new RootSetTreeCodec(),
|
|
32
33
|
}) {
|
|
33
|
-
this.rootSetRef =
|
|
34
|
+
this.rootSetRef = refType.from(rootSetRef).toString();
|
|
34
35
|
RootSetPersistence.#validateDependencies(persistence, ref);
|
|
35
36
|
this.persistence = persistence;
|
|
36
37
|
this.ref = ref;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const SAMPLE_LIMIT = 32;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Bounded evidence about objects written before a durable root exists.
|
|
5
|
+
*/
|
|
6
|
+
export default class StagingEvidence {
|
|
7
|
+
#handles = [];
|
|
8
|
+
#handleCount = 0;
|
|
9
|
+
#objectCount = 0;
|
|
10
|
+
#objects = [];
|
|
11
|
+
|
|
12
|
+
/** @param {string} oid @param {string} type */
|
|
13
|
+
record(oid, type) {
|
|
14
|
+
this.#objectCount += 1;
|
|
15
|
+
if (this.#objects.length < SAMPLE_LIMIT) {
|
|
16
|
+
this.#objects.push(Object.freeze({ oid, type }));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** @param {{ toString(): string }} handle */
|
|
21
|
+
recordHandle(handle) {
|
|
22
|
+
this.#handleCount += 1;
|
|
23
|
+
if (this.#handles.length < SAMPLE_LIMIT) {
|
|
24
|
+
this.#handles.push(handle.toString());
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @returns {object} */
|
|
29
|
+
snapshot() {
|
|
30
|
+
return Object.freeze({
|
|
31
|
+
objectCount: this.#objectCount,
|
|
32
|
+
stagedHandleCount: this.#handleCount,
|
|
33
|
+
objectSample: Object.freeze([...this.#objects]),
|
|
34
|
+
stagedHandleSample: Object.freeze([...this.#handles]),
|
|
35
|
+
sampleTruncated:
|
|
36
|
+
this.#objectCount > this.#objects.length || this.#handleCount > this.#handles.length,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import AssetHandle from './AssetHandle.js';
|
|
4
|
+
import BundleHandle from './BundleHandle.js';
|
|
5
|
+
import PageHandle from './PageHandle.js';
|
|
6
|
+
|
|
7
|
+
const TYPES = Object.freeze({
|
|
8
|
+
asset: AssetHandle,
|
|
9
|
+
bundle: BundleHandle,
|
|
10
|
+
page: PageHandle,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parses any supported application-facing content handle.
|
|
15
|
+
*
|
|
16
|
+
* @param {unknown} value
|
|
17
|
+
* @returns {AssetHandle|BundleHandle|PageHandle}
|
|
18
|
+
*/
|
|
19
|
+
export default function parseApplicationHandle(value) {
|
|
20
|
+
if (value instanceof AssetHandle || value instanceof BundleHandle || value instanceof PageHandle) {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
const kind = kindOf(value);
|
|
24
|
+
const Type = TYPES[kind];
|
|
25
|
+
if (!Type) {
|
|
26
|
+
throw createCasError('Handle kind is not supported', ErrorCodes.HANDLE_KIND_MISMATCH, {
|
|
27
|
+
expectedKinds: Object.keys(TYPES),
|
|
28
|
+
actualKind: kind ?? null,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return Type.from(value);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function kindOf(value) {
|
|
35
|
+
if (typeof value === 'string') {
|
|
36
|
+
const fields = value.split(':');
|
|
37
|
+
return fields.length === 7 && fields[0] === 'git-cas' ? fields[2] : null;
|
|
38
|
+
}
|
|
39
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value.kind : null;
|
|
40
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import Oid from './Oid.js';
|
|
4
|
+
|
|
5
|
+
export const ASSET_HANDLE_VERSION = 1;
|
|
6
|
+
export const ASSET_HANDLE_KIND = 'asset';
|
|
7
|
+
export const ASSET_HANDLE_FORMAT = 'manifest-tree';
|
|
8
|
+
|
|
9
|
+
const TOKEN_PREFIX = 'git-cas';
|
|
10
|
+
const CODEC_PATTERN = /^[a-z0-9][a-z0-9.-]{0,31}$/u;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Immutable, repository-independent locator for a git-cas asset graph.
|
|
14
|
+
*/
|
|
15
|
+
export default class AssetHandle {
|
|
16
|
+
/**
|
|
17
|
+
* @param {object} value
|
|
18
|
+
* @param {number} [value.version]
|
|
19
|
+
* @param {string} [value.kind]
|
|
20
|
+
* @param {string} [value.format]
|
|
21
|
+
* @param {string} value.codec
|
|
22
|
+
* @param {string} [value.hashAlgorithm]
|
|
23
|
+
* @param {string} value.oid
|
|
24
|
+
*/
|
|
25
|
+
constructor(value) {
|
|
26
|
+
AssetHandle.#assertObject(value);
|
|
27
|
+
AssetHandle.#assertEnvelope(value);
|
|
28
|
+
|
|
29
|
+
let oid;
|
|
30
|
+
try {
|
|
31
|
+
oid = Oid.from(value.oid).toString();
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throw AssetHandle.#invalid('Asset handle contains an invalid Git object identifier', {
|
|
34
|
+
value,
|
|
35
|
+
originalError: error,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const hashAlgorithm = oid.length === 40 ? 'sha1' : 'sha256';
|
|
39
|
+
if (value.hashAlgorithm !== undefined && value.hashAlgorithm !== hashAlgorithm) {
|
|
40
|
+
throw AssetHandle.#invalid(
|
|
41
|
+
'Asset handle hash algorithm does not match its object identifier',
|
|
42
|
+
{
|
|
43
|
+
hashAlgorithm: value.hashAlgorithm,
|
|
44
|
+
oid,
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.version = ASSET_HANDLE_VERSION;
|
|
50
|
+
this.kind = ASSET_HANDLE_KIND;
|
|
51
|
+
this.format = ASSET_HANDLE_FORMAT;
|
|
52
|
+
this.codec = value.codec;
|
|
53
|
+
this.hashAlgorithm = hashAlgorithm;
|
|
54
|
+
this.oid = oid;
|
|
55
|
+
Object.freeze(this);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {AssetHandle|string|object} value
|
|
60
|
+
* @returns {AssetHandle}
|
|
61
|
+
*/
|
|
62
|
+
static from(value) {
|
|
63
|
+
if (value instanceof AssetHandle) {
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
if (typeof value === 'string') {
|
|
67
|
+
return AssetHandle.parse(value);
|
|
68
|
+
}
|
|
69
|
+
return new AssetHandle(value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {string} token
|
|
74
|
+
* @returns {AssetHandle}
|
|
75
|
+
*/
|
|
76
|
+
static parse(token) {
|
|
77
|
+
if (typeof token !== 'string') {
|
|
78
|
+
throw AssetHandle.#invalid('Asset handle token must be a string', { token });
|
|
79
|
+
}
|
|
80
|
+
const fields = token.split(':');
|
|
81
|
+
if (fields.length !== 7 || fields[0] !== TOKEN_PREFIX) {
|
|
82
|
+
throw AssetHandle.#invalid('Asset handle token has an invalid shape', { token });
|
|
83
|
+
}
|
|
84
|
+
const [, version, kind, format, codec, hashAlgorithm, oid] = fields;
|
|
85
|
+
if (kind !== ASSET_HANDLE_KIND) {
|
|
86
|
+
throw createCasError('Handle kind is not asset', ErrorCodes.HANDLE_KIND_MISMATCH, {
|
|
87
|
+
expectedKind: ASSET_HANDLE_KIND,
|
|
88
|
+
actualKind: kind,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const handle = new AssetHandle({
|
|
92
|
+
version: Number(version),
|
|
93
|
+
kind,
|
|
94
|
+
format,
|
|
95
|
+
codec,
|
|
96
|
+
hashAlgorithm,
|
|
97
|
+
oid,
|
|
98
|
+
});
|
|
99
|
+
if (handle.toString() !== token) {
|
|
100
|
+
throw AssetHandle.#invalid('Asset handle token is not canonical', { token });
|
|
101
|
+
}
|
|
102
|
+
return handle;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @returns {string}
|
|
107
|
+
*/
|
|
108
|
+
toString() {
|
|
109
|
+
return [
|
|
110
|
+
TOKEN_PREFIX,
|
|
111
|
+
this.version,
|
|
112
|
+
this.kind,
|
|
113
|
+
this.format,
|
|
114
|
+
this.codec,
|
|
115
|
+
this.hashAlgorithm,
|
|
116
|
+
this.oid,
|
|
117
|
+
].join(':');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @returns {object}
|
|
122
|
+
*/
|
|
123
|
+
toJSON() {
|
|
124
|
+
return {
|
|
125
|
+
version: this.version,
|
|
126
|
+
kind: this.kind,
|
|
127
|
+
format: this.format,
|
|
128
|
+
codec: this.codec,
|
|
129
|
+
hashAlgorithm: this.hashAlgorithm,
|
|
130
|
+
oid: this.oid,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
static #assertObject(value) {
|
|
135
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
136
|
+
throw AssetHandle.#invalid('Asset handle must be an object or canonical token', { value });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
static #assertEnvelope(value) {
|
|
141
|
+
if (value.version !== undefined && value.version !== ASSET_HANDLE_VERSION) {
|
|
142
|
+
throw AssetHandle.#invalid(`Unsupported asset handle version: ${value.version}`, { value });
|
|
143
|
+
}
|
|
144
|
+
if (value.kind !== undefined && value.kind !== ASSET_HANDLE_KIND) {
|
|
145
|
+
throw createCasError('Handle kind is not asset', ErrorCodes.HANDLE_KIND_MISMATCH, {
|
|
146
|
+
expectedKind: ASSET_HANDLE_KIND,
|
|
147
|
+
actualKind: value.kind,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
if (value.format !== undefined && value.format !== ASSET_HANDLE_FORMAT) {
|
|
151
|
+
throw AssetHandle.#invalid(`Unsupported asset handle format: ${value.format}`, { value });
|
|
152
|
+
}
|
|
153
|
+
if (typeof value.codec !== 'string' || !CODEC_PATTERN.test(value.codec)) {
|
|
154
|
+
throw AssetHandle.#invalid('Asset handle codec must be a canonical lowercase identifier', {
|
|
155
|
+
codec: value.codec,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
static #invalid(message, meta) {
|
|
161
|
+
return createCasError(message, ErrorCodes.HANDLE_INVALID, meta);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import OpaqueHandle from './OpaqueHandle.js';
|
|
2
|
+
|
|
3
|
+
export const BUNDLE_HANDLE_VERSION = 1;
|
|
4
|
+
export const BUNDLE_HANDLE_KIND = 'bundle';
|
|
5
|
+
export const BUNDLE_HANDLE_FORMAT = 'fanout-tree';
|
|
6
|
+
|
|
7
|
+
const SPEC = Object.freeze({
|
|
8
|
+
version: BUNDLE_HANDLE_VERSION,
|
|
9
|
+
kind: BUNDLE_HANDLE_KIND,
|
|
10
|
+
format: BUNDLE_HANDLE_FORMAT,
|
|
11
|
+
label: 'Bundle',
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Immutable, repository-independent locator for one structured bundle tree.
|
|
16
|
+
*/
|
|
17
|
+
export default class BundleHandle extends OpaqueHandle {
|
|
18
|
+
/** @param {object} value */
|
|
19
|
+
constructor(value) {
|
|
20
|
+
super(value, SPEC);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @param {BundleHandle|string|object} value */
|
|
24
|
+
static from(value) {
|
|
25
|
+
return /** @type {BundleHandle} */ (OpaqueHandle.from(value, BundleHandle, SPEC));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @param {string} token */
|
|
29
|
+
static parse(token) {
|
|
30
|
+
return /** @type {BundleHandle} */ (OpaqueHandle.parse(token, BundleHandle, SPEC));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
|
|
4
|
+
export const DEFAULT_BUNDLE_LIMITS = Object.freeze({
|
|
5
|
+
maxMembers: 100_000,
|
|
6
|
+
maxMemberPathBytes: 512,
|
|
7
|
+
maxDescriptorBytes: 64 * 1024 * 1024,
|
|
8
|
+
maxFanoutEntries: 1024,
|
|
9
|
+
maxFanoutDepth: 8,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const BOUNDS = Object.freeze({
|
|
13
|
+
maxMembers: Object.freeze({ min: 0, max: 10_000_000 }),
|
|
14
|
+
maxMemberPathBytes: Object.freeze({ min: 1, max: 64 * 1024 }),
|
|
15
|
+
maxDescriptorBytes: Object.freeze({ min: 1, max: 1024 * 1024 * 1024 }),
|
|
16
|
+
maxFanoutEntries: Object.freeze({ min: 3, max: 65_536 }),
|
|
17
|
+
maxFanoutDepth: Object.freeze({ min: 1, max: 64 }),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Validated immutable admission limits for one bundle operation.
|
|
22
|
+
*/
|
|
23
|
+
export default class BundleLimits {
|
|
24
|
+
/** @param {Partial<typeof DEFAULT_BUNDLE_LIMITS>} [value] */
|
|
25
|
+
constructor(value = {}) {
|
|
26
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
27
|
+
throw BundleLimits.#invalid('Bundle limits must be an object', { limits: value });
|
|
28
|
+
}
|
|
29
|
+
for (const key of Object.keys(value)) {
|
|
30
|
+
if (!Object.hasOwn(DEFAULT_BUNDLE_LIMITS, key)) {
|
|
31
|
+
throw BundleLimits.#invalid('Bundle limits contain an unknown field', { field: key });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
for (const [key, fallback] of Object.entries(DEFAULT_BUNDLE_LIMITS)) {
|
|
35
|
+
const candidate = value[key] ?? fallback;
|
|
36
|
+
BundleLimits.#assertBound(key, candidate);
|
|
37
|
+
this[key] = candidate;
|
|
38
|
+
}
|
|
39
|
+
Object.freeze(this);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns per-operation limits that cannot exceed this configured maximum.
|
|
44
|
+
*
|
|
45
|
+
* @param {Partial<typeof DEFAULT_BUNDLE_LIMITS>} [overrides]
|
|
46
|
+
*/
|
|
47
|
+
lower(overrides = {}) {
|
|
48
|
+
const effective = new BundleLimits({ ...this.toJSON(), ...overrides });
|
|
49
|
+
for (const key of Object.keys(DEFAULT_BUNDLE_LIMITS)) {
|
|
50
|
+
if (effective[key] > this[key]) {
|
|
51
|
+
throw BundleLimits.#invalid('Per-operation bundle limit exceeds configured maximum', {
|
|
52
|
+
field: key,
|
|
53
|
+
requested: effective[key],
|
|
54
|
+
configured: this[key],
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return effective;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @returns {object} */
|
|
62
|
+
toJSON() {
|
|
63
|
+
return Object.fromEntries(Object.keys(DEFAULT_BUNDLE_LIMITS).map((key) => [key, this[key]]));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static #assertBound(key, value) {
|
|
67
|
+
const bounds = BOUNDS[key];
|
|
68
|
+
if (!Number.isSafeInteger(value) || value < bounds.min || value > bounds.max) {
|
|
69
|
+
throw BundleLimits.#invalid('Bundle limit is outside its supported range', {
|
|
70
|
+
field: key,
|
|
71
|
+
value,
|
|
72
|
+
...bounds,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static #invalid(message, meta) {
|
|
78
|
+
return createCasError(message, ErrorCodes.BUNDLE_LIMIT_INVALID, meta);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import { utf8Encode } from '../encoding/utf8.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validates and returns one canonical logical bundle member path.
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} value
|
|
9
|
+
* @param {number} maxBytes
|
|
10
|
+
* @returns {string}
|
|
11
|
+
*/
|
|
12
|
+
export default function normalizeBundlePath(value, maxBytes) {
|
|
13
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
14
|
+
throw invalid(value, 'Bundle member path must be a non-empty string');
|
|
15
|
+
}
|
|
16
|
+
if (value.normalize('NFC') !== value) {
|
|
17
|
+
throw invalid(value, 'Bundle member path must use canonical NFC normalization');
|
|
18
|
+
}
|
|
19
|
+
if (hasInvalidShape(value)) {
|
|
20
|
+
throw invalid(value, 'Bundle member path has an invalid or unsafe shape');
|
|
21
|
+
}
|
|
22
|
+
const bytes = utf8Encode(value).length;
|
|
23
|
+
if (bytes > maxBytes) {
|
|
24
|
+
throw createCasError('Bundle member path exceeds its byte limit', ErrorCodes.BUNDLE_PATH_LIMIT, {
|
|
25
|
+
path: value,
|
|
26
|
+
pathBytes: bytes,
|
|
27
|
+
maxMemberPathBytes: maxBytes,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function hasInvalidShape(value) {
|
|
34
|
+
if (
|
|
35
|
+
value.startsWith('/') ||
|
|
36
|
+
value.endsWith('/') ||
|
|
37
|
+
value.includes('//') ||
|
|
38
|
+
value.includes('\\') ||
|
|
39
|
+
hasUnpairedSurrogate(value) ||
|
|
40
|
+
hasControlCharacter(value)
|
|
41
|
+
) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return value.split('/').some((component) => component === '.' || component === '..');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function hasControlCharacter(value) {
|
|
48
|
+
for (const character of value) {
|
|
49
|
+
const codePoint = character.codePointAt(0);
|
|
50
|
+
if (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f)) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function hasUnpairedSurrogate(value) {
|
|
58
|
+
for (let index = 0; index < value.length; index++) {
|
|
59
|
+
const code = value.charCodeAt(index);
|
|
60
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
61
|
+
if (index + 1 >= value.length) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
const next = value.charCodeAt(index + 1);
|
|
65
|
+
if (next < 0xdc00 || next > 0xdfff) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
index += 1;
|
|
69
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function invalid(path, message) {
|
|
77
|
+
return createCasError(message, ErrorCodes.BUNDLE_PATH_INVALID, { path });
|
|
78
|
+
}
|