@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.
- package/ARCHITECTURE.md +116 -3
- package/CHANGELOG.md +117 -2
- package/GUIDE.md +114 -13
- package/README.md +60 -4
- package/UPGRADING.md +75 -0
- package/build-info.json +2 -2
- package/docs/API.md +867 -16
- package/docs/releases/v6.2.0.md +131 -0
- package/docs/releases/v6.3.0.md +96 -0
- package/index.d.ts +972 -2
- package/index.js +326 -1
- package/package.json +4 -2
- package/src/domain/errors/Codes.js +39 -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/CacheAcquisitionInventory.js +123 -0
- package/src/domain/services/CacheAcquisitionRegistry.js +188 -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 +754 -0
- package/src/domain/services/CacheSetRegistry.js +95 -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 +676 -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/CacheAcquisition.js +88 -0
- package/src/domain/value-objects/CacheAcquisitionRef.js +129 -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 +106 -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 +318 -7
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +182 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +11 -1
- package/src/ports/GitRefPort.js +46 -0
- package/src/ports/RepositoryInspectionPort.js +36 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import AssetHandle from '../value-objects/AssetHandle.js';
|
|
4
|
+
import StagedAsset from '../value-objects/StagedAsset.js';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* High-level asset handle boundary over the existing streaming CAS pipeline.
|
|
10
|
+
*/
|
|
11
|
+
export default class AssetService {
|
|
12
|
+
#cas;
|
|
13
|
+
#clock;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {object} options
|
|
17
|
+
* @param {import('./CasService.js').default} options.cas
|
|
18
|
+
* @param {{ now(): Date }} [options.clock]
|
|
19
|
+
*/
|
|
20
|
+
constructor({ cas, clock = DEFAULT_CLOCK }) {
|
|
21
|
+
AssetService.#assertDependencies(cas, clock);
|
|
22
|
+
this.#cas = cas;
|
|
23
|
+
this.#clock = clock;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Streams one asset into CAS and returns an explicitly unanchored handle.
|
|
28
|
+
*
|
|
29
|
+
* @param {object} options
|
|
30
|
+
* @returns {Promise<StagedAsset>}
|
|
31
|
+
*/
|
|
32
|
+
async put(options) {
|
|
33
|
+
const filename = options?.filename ?? options?.slug;
|
|
34
|
+
const manifest = await this.#cas.store({ ...options, filename });
|
|
35
|
+
const oid = await this.#cas.createTree({
|
|
36
|
+
manifest,
|
|
37
|
+
merkleThreshold: options?.merkleThreshold,
|
|
38
|
+
});
|
|
39
|
+
return this.#staged({ manifest, oid });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Adopts a validated existing git-cas manifest tree into an asset handle.
|
|
44
|
+
*
|
|
45
|
+
* @param {{ treeOid: string }} options
|
|
46
|
+
* @returns {Promise<StagedAsset>}
|
|
47
|
+
*/
|
|
48
|
+
async adopt({ treeOid }) {
|
|
49
|
+
const handle = new AssetHandle({ codec: this.#cas.codec.extension, oid: treeOid });
|
|
50
|
+
const root = await this.resolveRoot(handle);
|
|
51
|
+
return this.#staged({ manifest: root.manifest, oid: root.oid });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Streams plaintext bytes from a validated asset handle.
|
|
56
|
+
*
|
|
57
|
+
* @param {object} options
|
|
58
|
+
* @param {AssetHandle|string|object} options.handle
|
|
59
|
+
* @param {Uint8Array} [options.encryptionKey]
|
|
60
|
+
* @param {string} [options.passphrase]
|
|
61
|
+
* @returns {AsyncIterable<Uint8Array>}
|
|
62
|
+
*/
|
|
63
|
+
async *open({ handle: value, encryptionKey, passphrase }) {
|
|
64
|
+
const root = await this.resolveRoot(value);
|
|
65
|
+
try {
|
|
66
|
+
yield* this.#cas.restoreStream({
|
|
67
|
+
manifest: root.manifest,
|
|
68
|
+
encryptionKey,
|
|
69
|
+
passphrase,
|
|
70
|
+
});
|
|
71
|
+
} catch (error) {
|
|
72
|
+
throw this.#mapTargetError(error, root.handle);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Validates the handle and its complete manifest/chunk object graph.
|
|
78
|
+
*
|
|
79
|
+
* @param {AssetHandle|string|object} value
|
|
80
|
+
* @returns {Promise<object>}
|
|
81
|
+
*/
|
|
82
|
+
async resolveRoot(value) {
|
|
83
|
+
const handle = AssetHandle.from(value);
|
|
84
|
+
this.#assertCodec(handle);
|
|
85
|
+
await this.#assertObjectType(handle, handle.oid, 'tree');
|
|
86
|
+
|
|
87
|
+
let manifest;
|
|
88
|
+
try {
|
|
89
|
+
manifest = await this.#cas.readManifest({ treeOid: handle.oid });
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw this.#mapTargetError(error, handle);
|
|
92
|
+
}
|
|
93
|
+
await this.#assertChunkGraph(handle, manifest);
|
|
94
|
+
return Object.freeze({
|
|
95
|
+
handle,
|
|
96
|
+
oid: handle.oid,
|
|
97
|
+
type: 'tree',
|
|
98
|
+
size: manifest.size,
|
|
99
|
+
manifest,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async #assertChunkGraph(handle, manifest) {
|
|
104
|
+
const blobs = new Set();
|
|
105
|
+
for (const chunk of manifest.chunks) {
|
|
106
|
+
blobs.add(chunk.blob);
|
|
107
|
+
}
|
|
108
|
+
const iterator = blobs.values();
|
|
109
|
+
const workerCount = Math.min(this.#cas.concurrency, blobs.size);
|
|
110
|
+
|
|
111
|
+
const validateNext = async () => {
|
|
112
|
+
while (true) {
|
|
113
|
+
const next = iterator.next();
|
|
114
|
+
if (next.done) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
await this.#assertObjectType(handle, next.value, 'blob');
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
await Promise.all(Array.from({ length: workerCount }, () => validateNext()));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async #assertObjectType(handle, oid, expectedType) {
|
|
125
|
+
let actualType;
|
|
126
|
+
try {
|
|
127
|
+
actualType = await this.#cas.persistence.readObjectType(oid);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
throw this.#mapTargetError(error, handle, oid);
|
|
130
|
+
}
|
|
131
|
+
if (actualType !== expectedType) {
|
|
132
|
+
throw createCasError(
|
|
133
|
+
'Asset handle target has the wrong Git object type',
|
|
134
|
+
ErrorCodes.HANDLE_TARGET_TYPE_MISMATCH,
|
|
135
|
+
{ handle: handle.toString(), targetOid: oid, expectedType, actualType }
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#assertCodec(handle) {
|
|
141
|
+
const expectedCodec = this.#cas.codec.extension;
|
|
142
|
+
if (handle.codec !== expectedCodec) {
|
|
143
|
+
throw createCasError(
|
|
144
|
+
'Asset handle codec does not match this CAS instance',
|
|
145
|
+
ErrorCodes.HANDLE_CODEC_MISMATCH,
|
|
146
|
+
{
|
|
147
|
+
handle: handle.toString(),
|
|
148
|
+
expectedCodec,
|
|
149
|
+
actualCodec: handle.codec,
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#mapTargetError(error, handle, targetOid = handle.oid) {
|
|
156
|
+
if (error?.code === ErrorCodes.HANDLE_TARGET_MISSING) {
|
|
157
|
+
return error;
|
|
158
|
+
}
|
|
159
|
+
const missing =
|
|
160
|
+
error?.code === ErrorCodes.GIT_OBJECT_NOT_FOUND ||
|
|
161
|
+
/(?:object|blob|tree) not found/iu.test(
|
|
162
|
+
error instanceof Error ? error.message : String(error)
|
|
163
|
+
);
|
|
164
|
+
if (!missing) {
|
|
165
|
+
return error;
|
|
166
|
+
}
|
|
167
|
+
return createCasError(
|
|
168
|
+
'Asset handle target graph is missing from this repository',
|
|
169
|
+
ErrorCodes.HANDLE_TARGET_MISSING,
|
|
170
|
+
{ handle: handle.toString(), targetOid, originalError: error }
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
#staged({ manifest, oid }) {
|
|
175
|
+
return new StagedAsset({
|
|
176
|
+
handle: new AssetHandle({ codec: this.#cas.codec.extension, oid }),
|
|
177
|
+
slug: manifest.slug,
|
|
178
|
+
filename: manifest.filename,
|
|
179
|
+
size: manifest.size,
|
|
180
|
+
observedAt: this.#observedAt(),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
#observedAt() {
|
|
185
|
+
const now = this.#clock.now();
|
|
186
|
+
if (!(now instanceof Date) || Number.isNaN(now.getTime())) {
|
|
187
|
+
throw createCasError(
|
|
188
|
+
'AssetService clock returned an invalid Date',
|
|
189
|
+
ErrorCodes.INVALID_OPTIONS
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
return now.toISOString();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static #assertDependencies(cas, clock) {
|
|
196
|
+
const methods = ['store', 'createTree', 'readManifest', 'restoreStream'];
|
|
197
|
+
if (!cas || methods.some((method) => typeof cas[method] !== 'function')) {
|
|
198
|
+
throw createCasError(
|
|
199
|
+
'AssetService requires a complete CasService',
|
|
200
|
+
ErrorCodes.INVALID_OPTIONS
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
if (!cas.persistence || typeof cas.persistence.readObjectType !== 'function') {
|
|
204
|
+
throw createCasError(
|
|
205
|
+
'AssetService requires object-type inspection',
|
|
206
|
+
ErrorCodes.INVALID_OPTIONS
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
if (!clock || typeof clock.now !== 'function') {
|
|
210
|
+
throw createCasError('AssetService clock must provide now()', ErrorCodes.INVALID_OPTIONS);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import { normalizeCodecBytes } from '../helpers/codecBytes.js';
|
|
4
|
+
import parseApplicationHandle from '../value-objects/ApplicationHandle.js';
|
|
5
|
+
import BundleLimits from '../value-objects/BundleLimits.js';
|
|
6
|
+
import normalizeBundlePath from '../value-objects/BundlePath.js';
|
|
7
|
+
|
|
8
|
+
export const BUNDLE_LAYOUT = 'fanout-v1';
|
|
9
|
+
export const BUNDLE_DESCRIPTOR_VERSION = 1;
|
|
10
|
+
export const BUNDLE_INDEX_ENTRY = 'index';
|
|
11
|
+
const SLOT_PATTERN = /^slot-[0-9]{6}$/u;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Deterministic codec and schema guard for bundle root/node descriptors.
|
|
15
|
+
*/
|
|
16
|
+
export default class BundleDescriptorCodec {
|
|
17
|
+
#codec;
|
|
18
|
+
|
|
19
|
+
/** @param {import('../../ports/CodecPort.js').default} codec */
|
|
20
|
+
constructor(codec) {
|
|
21
|
+
if (!codec || typeof codec.encode !== 'function' || typeof codec.decode !== 'function') {
|
|
22
|
+
throw createCasError('Bundle descriptor codec is incomplete', ErrorCodes.INVALID_OPTIONS);
|
|
23
|
+
}
|
|
24
|
+
this.#codec = codec;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @returns {string} */
|
|
28
|
+
get extension() {
|
|
29
|
+
return this.#codec.extension;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @returns {string} */
|
|
33
|
+
get rootEntryName() {
|
|
34
|
+
return `bundle.${this.extension}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** @returns {string} */
|
|
38
|
+
get nodeEntryName() {
|
|
39
|
+
return `node.${this.extension}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @param {object} descriptor */
|
|
43
|
+
encodeRoot(descriptor) {
|
|
44
|
+
return this.#encode(descriptor);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @param {object} descriptor */
|
|
48
|
+
encodeNode(descriptor) {
|
|
49
|
+
return this.#encode(descriptor);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** @param {Uint8Array} bytes */
|
|
53
|
+
decodeRoot(bytes) {
|
|
54
|
+
const value = this.#decode(bytes, 'root');
|
|
55
|
+
assertRoot(value, this.extension);
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @param {Uint8Array} bytes */
|
|
60
|
+
decodeNode(bytes, limits) {
|
|
61
|
+
const value = this.#decode(bytes, 'node');
|
|
62
|
+
assertNode(value, limits);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#encode(value) {
|
|
67
|
+
try {
|
|
68
|
+
return normalizeCodecBytes(this.#codec.encode(value));
|
|
69
|
+
} catch (error) {
|
|
70
|
+
throw corrupt('Bundle descriptor encoding failed', { originalError: error });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#decode(bytes, descriptorKind) {
|
|
75
|
+
let value;
|
|
76
|
+
try {
|
|
77
|
+
value = this.#codec.decode(bytes);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw corrupt('Bundle descriptor decoding failed', { descriptorKind, originalError: error });
|
|
80
|
+
}
|
|
81
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
82
|
+
throw corrupt('Bundle descriptor must decode to an object', { descriptorKind });
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function assertRoot(value, extension) {
|
|
89
|
+
assertRootEnvelope(value, extension);
|
|
90
|
+
assertSafeCount(value.memberCount, 'memberCount');
|
|
91
|
+
if (!value.index || typeof value.index !== 'object' || Array.isArray(value.index)) {
|
|
92
|
+
throw corrupt('Bundle root index descriptor is invalid', { descriptor: value });
|
|
93
|
+
}
|
|
94
|
+
assertSafeCount(value.index.depth, 'index.depth', 1);
|
|
95
|
+
assertRange(value.index.firstPath, value.index.lastPath, value.memberCount);
|
|
96
|
+
if (value.index.entry !== BUNDLE_INDEX_ENTRY) {
|
|
97
|
+
throw corrupt('Bundle root index entry name is invalid', { descriptor: value });
|
|
98
|
+
}
|
|
99
|
+
assertPersistedLimits(value.limits);
|
|
100
|
+
if (value.memberCount > value.limits.maxMembers) {
|
|
101
|
+
throw corrupt('Bundle root member count exceeds its persisted limit', { descriptor: value });
|
|
102
|
+
}
|
|
103
|
+
if (value.index.depth > value.limits.maxFanoutDepth) {
|
|
104
|
+
throw corrupt('Bundle root depth exceeds its persisted fanout limit', { descriptor: value });
|
|
105
|
+
}
|
|
106
|
+
assertCanonicalRange(value.index, value.limits.maxMemberPathBytes);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function assertRootEnvelope(value, extension) {
|
|
110
|
+
if (
|
|
111
|
+
value.version !== BUNDLE_DESCRIPTOR_VERSION ||
|
|
112
|
+
value.kind !== 'bundle' ||
|
|
113
|
+
value.layout !== BUNDLE_LAYOUT ||
|
|
114
|
+
value.codec !== extension
|
|
115
|
+
) {
|
|
116
|
+
throw corrupt('Bundle root descriptor envelope is invalid', { descriptor: value });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function assertNode(value, limits) {
|
|
121
|
+
if (
|
|
122
|
+
value.version !== BUNDLE_DESCRIPTOR_VERSION ||
|
|
123
|
+
!['leaf', 'branch'].includes(value.kind) ||
|
|
124
|
+
!Array.isArray(value.entries)
|
|
125
|
+
) {
|
|
126
|
+
throw corrupt('Bundle node descriptor envelope is invalid', { descriptor: value });
|
|
127
|
+
}
|
|
128
|
+
assertSafeCount(value.depth, 'depth', 1);
|
|
129
|
+
assertSafeCount(value.count, 'count');
|
|
130
|
+
assertRange(value.firstPath, value.lastPath, value.count);
|
|
131
|
+
if (value.kind === 'leaf') {
|
|
132
|
+
assertLeafEntries(value, limits);
|
|
133
|
+
} else {
|
|
134
|
+
assertBranchEntries(value, limits);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function assertLeafEntries(value, limits) {
|
|
139
|
+
if (value.depth !== 1 || value.entries.length !== value.count) {
|
|
140
|
+
throw corrupt('Bundle leaf count or depth is invalid', { descriptor: value });
|
|
141
|
+
}
|
|
142
|
+
let previous = null;
|
|
143
|
+
for (const entry of value.entries) {
|
|
144
|
+
assertCanonicalPath(entry?.path, limits.maxMemberPathBytes);
|
|
145
|
+
previous = assertLeafEntry(entry, previous, value);
|
|
146
|
+
}
|
|
147
|
+
assertDescriptorRange(value);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function assertBranchEntries(value, limits) {
|
|
151
|
+
if (value.depth < 2 || value.entries.length === 0) {
|
|
152
|
+
throw corrupt('Bundle branch depth or entries are invalid', { descriptor: value });
|
|
153
|
+
}
|
|
154
|
+
let previousLast = null;
|
|
155
|
+
let count = 0;
|
|
156
|
+
for (const entry of value.entries) {
|
|
157
|
+
assertCanonicalRange(entry, limits.maxMemberPathBytes);
|
|
158
|
+
previousLast = assertBranchEntry(entry, previousLast, value);
|
|
159
|
+
count += entry.count;
|
|
160
|
+
}
|
|
161
|
+
if (count !== value.count) {
|
|
162
|
+
throw corrupt('Bundle branch member count is invalid', { descriptor: value });
|
|
163
|
+
}
|
|
164
|
+
assertDescriptorRange(value);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function assertLeafEntry(entry, previous, descriptor) {
|
|
168
|
+
assertSlot(entry?.slot);
|
|
169
|
+
if (typeof entry.path !== 'string' || (previous !== null && entry.path <= previous)) {
|
|
170
|
+
throw corrupt('Bundle leaf paths are not strictly ordered', { descriptor });
|
|
171
|
+
}
|
|
172
|
+
const handle = parseDescriptorHandle(entry.handle);
|
|
173
|
+
const expectedType = handle.kind === 'page' ? 'blob' : 'tree';
|
|
174
|
+
if (entry.type !== expectedType || !isOptionalSize(entry.size)) {
|
|
175
|
+
throw corrupt('Bundle leaf member metadata is inconsistent', { entry });
|
|
176
|
+
}
|
|
177
|
+
return entry.path;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function assertBranchEntry(entry, previousLast, descriptor) {
|
|
181
|
+
assertSlot(entry?.slot);
|
|
182
|
+
assertSafeCount(entry.count, 'entry.count', 1);
|
|
183
|
+
assertSafeCount(entry.depth, 'entry.depth', 1);
|
|
184
|
+
if (!isValidBranchRange(entry, previousLast, descriptor.depth)) {
|
|
185
|
+
throw corrupt('Bundle branch ranges are invalid', { descriptor });
|
|
186
|
+
}
|
|
187
|
+
return entry.lastPath;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function isValidBranchRange(entry, previousLast, parentDepth) {
|
|
191
|
+
return (
|
|
192
|
+
typeof entry.firstPath === 'string' &&
|
|
193
|
+
typeof entry.lastPath === 'string' &&
|
|
194
|
+
entry.firstPath <= entry.lastPath &&
|
|
195
|
+
(previousLast === null || entry.firstPath > previousLast) &&
|
|
196
|
+
entry.depth === parentDepth - 1
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function assertDescriptorRange(value) {
|
|
201
|
+
if (value.entries.length === 0) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const first = value.entries[0];
|
|
205
|
+
const last = value.entries.at(-1);
|
|
206
|
+
const firstPath = value.kind === 'leaf' ? first.path : first.firstPath;
|
|
207
|
+
const lastPath = value.kind === 'leaf' ? last.path : last.lastPath;
|
|
208
|
+
if (value.firstPath !== firstPath || value.lastPath !== lastPath) {
|
|
209
|
+
throw corrupt('Bundle node range does not match its entries', { descriptor: value });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function assertPersistedLimits(value) {
|
|
214
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
215
|
+
throw corrupt('Bundle persisted fanout limits are invalid', { limits: value });
|
|
216
|
+
}
|
|
217
|
+
const expected = Object.keys(new BundleLimits().toJSON());
|
|
218
|
+
if (
|
|
219
|
+
Object.keys(value).length !== expected.length ||
|
|
220
|
+
expected.some((field) => !Object.hasOwn(value, field))
|
|
221
|
+
) {
|
|
222
|
+
throw corrupt('Bundle persisted limits are incomplete', { limits: value });
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
new BundleLimits(value);
|
|
226
|
+
} catch (error) {
|
|
227
|
+
throw corrupt('Bundle persisted limits are invalid', { limits: value, originalError: error });
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function assertCanonicalRange(value, maxBytes) {
|
|
232
|
+
if (value.firstPath !== null) {
|
|
233
|
+
assertCanonicalPath(value.firstPath, maxBytes);
|
|
234
|
+
assertCanonicalPath(value.lastPath, maxBytes);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function assertCanonicalPath(value, maxBytes) {
|
|
239
|
+
let normalizedPath;
|
|
240
|
+
try {
|
|
241
|
+
normalizedPath = normalizeBundlePath(value, maxBytes);
|
|
242
|
+
} catch (error) {
|
|
243
|
+
throw corrupt('Bundle descriptor path is invalid', { path: value, originalError: error });
|
|
244
|
+
}
|
|
245
|
+
if (normalizedPath !== value) {
|
|
246
|
+
throw corrupt('Bundle descriptor path is not canonical', { path: value, normalizedPath });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function assertRange(firstPath, lastPath, count) {
|
|
251
|
+
const empty = count === 0;
|
|
252
|
+
if (
|
|
253
|
+
(empty && (firstPath !== null || lastPath !== null)) ||
|
|
254
|
+
(!empty && (typeof firstPath !== 'string' || typeof lastPath !== 'string' || firstPath > lastPath))
|
|
255
|
+
) {
|
|
256
|
+
throw corrupt('Bundle descriptor range is invalid', { firstPath, lastPath, count });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function assertSafeCount(value, field, minimum = 0) {
|
|
261
|
+
if (!Number.isSafeInteger(value) || value < minimum) {
|
|
262
|
+
throw corrupt('Bundle descriptor count is invalid', { field, value, minimum });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function assertSlot(value) {
|
|
267
|
+
if (typeof value !== 'string' || !SLOT_PATTERN.test(value)) {
|
|
268
|
+
throw corrupt('Bundle descriptor slot is invalid', { slot: value });
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function parseDescriptorHandle(value) {
|
|
273
|
+
try {
|
|
274
|
+
return parseApplicationHandle(value);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
throw corrupt('Bundle member handle is invalid', { handle: value, originalError: error });
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function isOptionalSize(value) {
|
|
281
|
+
return value === null || (Number.isSafeInteger(value) && value >= 0);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function corrupt(message, meta) {
|
|
285
|
+
return createCasError(message, ErrorCodes.BUNDLE_CORRUPT, meta);
|
|
286
|
+
}
|