@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
package/index.js CHANGED
@@ -8,9 +8,22 @@
8
8
  // ---------------------------------------------------------------------------
9
9
  import CasService from './src/domain/services/CasService.js';
10
10
  import VaultService from './src/domain/services/VaultService.js';
11
+ import RootSet from './src/domain/services/RootSet.js';
12
+ import RootSetRegistry from './src/domain/services/RootSetRegistry.js';
13
+ import AssetService from './src/domain/services/AssetService.js';
14
+ import BundleService from './src/domain/services/BundleService.js';
15
+ import CacheSet from './src/domain/services/CacheSet.js';
16
+ import CacheSetRegistry from './src/domain/services/CacheSetRegistry.js';
17
+ import ExpiringSet from './src/domain/services/ExpiringSet.js';
18
+ import ExpiringSetRegistry from './src/domain/services/ExpiringSetRegistry.js';
19
+ import RepositoryDoctor from './src/domain/services/RepositoryDoctor.js';
20
+ import PageService from './src/domain/services/PageService.js';
21
+ import PublicationService from './src/domain/services/PublicationService.js';
22
+ import RetentionService from './src/domain/services/RetentionService.js';
11
23
  import rotateVaultPassphrase from './src/domain/services/rotateVaultPassphrase.js';
12
24
  import GitPersistenceAdapter from './src/infrastructure/adapters/GitPersistenceAdapter.js';
13
25
  import GitRefAdapter from './src/infrastructure/adapters/GitRefAdapter.js';
26
+ import GitRepositoryInspectionAdapter from './src/infrastructure/adapters/GitRepositoryInspectionAdapter.js';
14
27
  import createCryptoAdapter from './src/infrastructure/adapters/createCryptoAdapter.js';
15
28
  import { createGitPlumbing } from './src/infrastructure/createGitPlumbing.js';
16
29
  import { storeFile, restoreFile } from './src/infrastructure/adapters/FileIOHelper.js';
@@ -22,11 +35,13 @@ import { CasError, createCasError, ErrorCodes } from './src/domain/errors/index.
22
35
  import FixedChunker from './src/infrastructure/chunkers/FixedChunker.js';
23
36
  import NodeCompressionAdapter from './src/infrastructure/adapters/NodeCompressionAdapter.js';
24
37
  import { PACKAGE_VERSION } from './src/package-version.js';
38
+ import parseApplicationHandle from './src/domain/value-objects/ApplicationHandle.js';
25
39
 
26
40
  /** @typedef {import('./src/domain/value-objects/Manifest.js').default} Manifest */
27
41
 
28
42
  const PKG_VERSION = PACKAGE_VERSION;
29
43
  const FIXED_CHUNKING_STRATEGY = 'fixed';
44
+ const MAX_VALIDATION_CACHE_ENTRIES = 1024;
30
45
  const RESTORE_FILE_DOCS_URL = `https://github.com/git-stunts/git-cas/blob/v${PKG_VERSION}/docs/API.md#restorefile`;
31
46
 
32
47
  function withOperationChunker(options) {
@@ -50,8 +65,14 @@ function withOperationChunker(options) {
50
65
  export {
51
66
  CasService,
52
67
  VaultService,
68
+ RootSet,
69
+ RootSetRegistry,
70
+ CacheSet,
71
+ ExpiringSet,
72
+ RepositoryDoctor,
53
73
  GitPersistenceAdapter,
54
74
  GitRefAdapter,
75
+ GitRepositoryInspectionAdapter,
55
76
  JsonCodec,
56
77
  CborCodec,
57
78
  SilentObserver,
@@ -67,11 +88,22 @@ export { default as ChunkingPort } from './src/ports/ChunkingPort.js';
67
88
  export { default as ObservabilityPort } from './src/ports/ObservabilityPort.js';
68
89
  export { default as Manifest } from './src/domain/value-objects/Manifest.js';
69
90
  export { default as Chunk } from './src/domain/value-objects/Chunk.js';
91
+ export { default as AssetHandle } from './src/domain/value-objects/AssetHandle.js';
92
+ export { default as BundleHandle } from './src/domain/value-objects/BundleHandle.js';
93
+ export { default as PageHandle } from './src/domain/value-objects/PageHandle.js';
94
+ export { default as StagedAsset } from './src/domain/value-objects/StagedAsset.js';
95
+ export { default as StagedBundle } from './src/domain/value-objects/StagedBundle.js';
96
+ export { default as StagedPage } from './src/domain/value-objects/StagedPage.js';
97
+ export { default as RetentionWitness } from './src/domain/value-objects/RetentionWitness.js';
98
+ export { default as CacheHit } from './src/domain/value-objects/CacheHit.js';
99
+ export { default as CachePolicy } from './src/domain/value-objects/CachePolicy.js';
100
+ export { default as ExpiringMarker } from './src/domain/value-objects/ExpiringMarker.js';
70
101
  export { default as EventEmitterObserver } from './src/infrastructure/adapters/EventEmitterObserver.js';
71
102
  export { default as StatsCollector } from './src/infrastructure/adapters/StatsCollector.js';
72
103
  export { default as FixedChunker } from './src/infrastructure/chunkers/FixedChunker.js';
73
104
  export { default as CdcChunker } from './src/infrastructure/chunkers/CdcChunker.js';
74
105
  export { default as CompressionPort } from './src/ports/CompressionPort.js';
106
+ export { default as RepositoryInspectionPort } from './src/ports/RepositoryInspectionPort.js';
75
107
  export { default as NodeCompressionAdapter } from './src/infrastructure/adapters/NodeCompressionAdapter.js';
76
108
  export { default as diffManifests } from './src/domain/services/ManifestDiff.js';
77
109
  export { SCHEME_WHOLE, SCHEME_FRAMED, SCHEME_CONVERGENT } from './src/domain/encryption/schemes.js';
@@ -97,7 +129,12 @@ export default class ContentAddressableStore {
97
129
  * @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance (advanced).
98
130
  * @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes for encrypted/compressed restores (default 512 MiB).
99
131
  * @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes (default 10 MiB).
132
+ * @param {number} [options.maxPageSize=16777216] - Maximum immutable page size in bytes (default 16 MiB).
133
+ * @param {object} [options.bundleLimits] - Repository-wide maximum bundle admission limits.
134
+ * @param {number} [options.maxBundleNestingDepth=32] - Maximum nested bundle depth.
100
135
  * @param {import('./src/ports/CompressionPort.js').default} [options.compressionAdapter] - Compression adapter (default NodeCompressionAdapter).
136
+ * @param {string[]} [options.applicationRefPrefixes] - Explicit application ref namespaces allowed for generic publication.
137
+ * @param {{ now(): Date }} [options.clock] - Injectable clock for deterministic evidence.
101
138
  */
102
139
  constructor({
103
140
  plumbing,
@@ -112,7 +149,12 @@ export default class ContentAddressableStore {
112
149
  chunker,
113
150
  maxRestoreBufferSize,
114
151
  maxBlobSize,
152
+ maxPageSize,
153
+ bundleLimits,
154
+ maxBundleNestingDepth,
115
155
  compressionAdapter,
156
+ applicationRefPrefixes,
157
+ clock,
116
158
  }) {
117
159
  this.#config = {
118
160
  plumbing,
@@ -127,16 +169,78 @@ export default class ContentAddressableStore {
127
169
  chunker,
128
170
  maxRestoreBufferSize,
129
171
  maxBlobSize,
172
+ maxPageSize,
173
+ bundleLimits,
174
+ maxBundleNestingDepth,
130
175
  compressionAdapter,
176
+ applicationRefPrefixes,
177
+ clock,
131
178
  };
132
179
  this.service = null;
133
180
  this.#servicePromise = null;
181
+ this.#installCapabilities();
134
182
  }
135
183
 
136
- /** @type {{ plumbing: *, chunkSize?: number, codec?: *, policy?: *, crypto?: *, observability?: *, merkleThreshold?: number, concurrency?: number, chunking?: *, chunker?: *, maxRestoreBufferSize?: number, maxBlobSize?: number, compressionAdapter?: * }} */
184
+ #installCapabilities() {
185
+ this.rootSets = Object.freeze({
186
+ open: async (options) => (await this.#getRootSetRegistry()).open(options),
187
+ });
188
+ this.caches = Object.freeze({
189
+ open: async (options) => (await this.#getCacheSetRegistry()).open(options),
190
+ });
191
+ this.expiringSets = Object.freeze({
192
+ open: async (options) => (await this.#getExpiringSetRegistry()).open(options),
193
+ });
194
+ this.diagnostics = Object.freeze({
195
+ doctor: async (options) => (await this.#getRepositoryDoctor()).doctor(options),
196
+ });
197
+ this.assets = Object.freeze({
198
+ put: async (options) => (await this.#getAssetService()).put(options),
199
+ adopt: async (options) => (await this.#getAssetService()).adopt(options),
200
+ open: (options) => this.#openAsset(options),
201
+ });
202
+ this.pages = Object.freeze({
203
+ put: async (options) => (await this.#getPageService()).put(options),
204
+ get: async (options) => (await this.#getPageService()).get(options),
205
+ open: (options) => this.#openPage(options),
206
+ });
207
+ this.bundles = Object.freeze({
208
+ put: async (options) => (await this.#getBundleService()).put(options),
209
+ putOrdered: async (options) => (await this.#getBundleService()).putOrdered(options),
210
+ getMember: async (options) => (await this.#getBundleService()).getMember(options),
211
+ iterateMembers: (options) => this.#iterateBundleMembers(options),
212
+ openMember: (options) => this.#openBundleMember(options),
213
+ });
214
+ this.retention = Object.freeze({
215
+ retain: async (options) => (await this.#getRetentionService()).retain(options),
216
+ });
217
+ this.publications = Object.freeze({
218
+ commit: async (options) => (await this.#getPublicationService()).commit(options),
219
+ });
220
+ }
221
+
222
+ /** @type {{ plumbing: *, chunkSize?: number, codec?: *, policy?: *, crypto?: *, observability?: *, merkleThreshold?: number, concurrency?: number, chunking?: *, chunker?: *, maxRestoreBufferSize?: number, maxBlobSize?: number, maxPageSize?: number, bundleLimits?: object, maxBundleNestingDepth?: number, compressionAdapter?: *, applicationRefPrefixes?: string[], clock?: { now(): Date } }} */
137
223
  #config;
224
+ /** @type {AssetService|null} */
225
+ #assetService = null;
226
+ /** @type {BundleService|null} */
227
+ #bundleService = null;
228
+ /** @type {PageService|null} */
229
+ #pageService = null;
230
+ /** @type {PublicationService|null} */
231
+ #publicationService = null;
232
+ /** @type {RetentionService|null} */
233
+ #retentionService = null;
138
234
  /** @type {VaultService|null} */
139
235
  #vault = null;
236
+ /** @type {RootSetRegistry|null} */
237
+ #rootSetRegistry = null;
238
+ /** @type {CacheSetRegistry|null} */
239
+ #cacheSetRegistry = null;
240
+ /** @type {ExpiringSetRegistry|null} */
241
+ #expiringSetRegistry = null;
242
+ /** @type {RepositoryDoctor|null} */
243
+ #repositoryDoctor = null;
140
244
  #servicePromise = null;
141
245
 
142
246
  /**
@@ -192,10 +296,79 @@ export default class ContentAddressableStore {
192
296
  crypto,
193
297
  observability: this.service.observability,
194
298
  });
299
+ this.#rootSetRegistry = new RootSetRegistry({ persistence, ref });
300
+ this.#initApplicationServices({ ref, cfg });
301
+ this.#initCollectionServices({ persistence, ref, crypto, cfg });
195
302
 
196
303
  return this.service;
197
304
  }
198
305
 
306
+ #initRepositoryDoctor() {
307
+ const cfg = this.#config;
308
+ this.#repositoryDoctor = new RepositoryDoctor({
309
+ repository: new GitRepositoryInspectionAdapter({
310
+ plumbing: cfg.plumbing,
311
+ policy: cfg.policy,
312
+ }),
313
+ rootSets: this.#rootSetRegistry,
314
+ caches: this.#cacheSetRegistry,
315
+ expiringSets: this.#expiringSetRegistry,
316
+ vault: this.#vault,
317
+ clock: cfg.clock,
318
+ });
319
+ }
320
+
321
+ #initCollectionServices({ persistence, ref, crypto, cfg }) {
322
+ this.#cacheSetRegistry = new CacheSetRegistry({
323
+ persistence,
324
+ ref,
325
+ bundles: this.#bundleService,
326
+ pages: this.#pageService,
327
+ resolveHandle: (handle) => this.#resolveApplicationRoot(handle),
328
+ crypto,
329
+ clock: cfg.clock,
330
+ });
331
+ this.#expiringSetRegistry = new ExpiringSetRegistry({
332
+ persistence,
333
+ ref,
334
+ bundles: this.#bundleService,
335
+ pages: this.#pageService,
336
+ crypto,
337
+ clock: cfg.clock,
338
+ });
339
+ }
340
+
341
+ #initApplicationServices({ ref, cfg }) {
342
+ this.#assetService = new AssetService({ cas: this.service, clock: cfg.clock });
343
+ this.#pageService = new PageService({
344
+ persistence: this.service.persistence,
345
+ maxPageSize: cfg.maxPageSize,
346
+ clock: cfg.clock,
347
+ });
348
+ this.#bundleService = new BundleService({
349
+ persistence: this.service.persistence,
350
+ codec: this.service.codec,
351
+ pages: this.#pageService,
352
+ resolveHandle: (handle, context) => this.#resolveApplicationRoot(handle, context),
353
+ openHandle: (handle) => this.#openApplicationHandle(handle),
354
+ limits: cfg.bundleLimits,
355
+ maxNestingDepth: cfg.maxBundleNestingDepth,
356
+ clock: cfg.clock,
357
+ });
358
+ const resolveRoot = (handle) => this.#resolveApplicationRoot(handle);
359
+ this.#retentionService = new RetentionService({
360
+ rootSets: this.#rootSetRegistry,
361
+ resolveRoot,
362
+ clock: cfg.clock,
363
+ });
364
+ this.#publicationService = new PublicationService({
365
+ ref,
366
+ resolveRoot: (handle) => this.#resolvePublicationRoot(handle),
367
+ applicationRefPrefixes: cfg.applicationRefPrefixes,
368
+ clock: cfg.clock,
369
+ });
370
+ }
371
+
199
372
  /**
200
373
  * Lazily initializes and returns the underlying {@link VaultService}.
201
374
  * @private
@@ -206,6 +379,165 @@ export default class ContentAddressableStore {
206
379
  return this.#vault;
207
380
  }
208
381
 
382
+ /**
383
+ * Lazily initializes and returns the root-set registry.
384
+ * @private
385
+ * @returns {Promise<RootSetRegistry>}
386
+ */
387
+ async #getRootSetRegistry() {
388
+ await this.#getService();
389
+ return this.#rootSetRegistry;
390
+ }
391
+
392
+ /** @returns {Promise<CacheSetRegistry>} */
393
+ async #getCacheSetRegistry() {
394
+ await this.#getService();
395
+ return this.#cacheSetRegistry;
396
+ }
397
+
398
+ /** @returns {Promise<ExpiringSetRegistry>} */
399
+ async #getExpiringSetRegistry() {
400
+ await this.#getService();
401
+ return this.#expiringSetRegistry;
402
+ }
403
+
404
+ /** @returns {Promise<RepositoryDoctor>} */
405
+ async #getRepositoryDoctor() {
406
+ await this.#getService();
407
+ if (this.#repositoryDoctor === null) {
408
+ this.#initRepositoryDoctor();
409
+ }
410
+ return this.#repositoryDoctor;
411
+ }
412
+
413
+ /** @returns {Promise<AssetService>} */
414
+ async #getAssetService() {
415
+ await this.#getService();
416
+ return this.#assetService;
417
+ }
418
+
419
+ /** @returns {Promise<PageService>} */
420
+ async #getPageService() {
421
+ await this.#getService();
422
+ return this.#pageService;
423
+ }
424
+
425
+ /** @returns {Promise<BundleService>} */
426
+ async #getBundleService() {
427
+ await this.#getService();
428
+ return this.#bundleService;
429
+ }
430
+
431
+ /** @returns {Promise<RetentionService>} */
432
+ async #getRetentionService() {
433
+ await this.#getService();
434
+ return this.#retentionService;
435
+ }
436
+
437
+ /** @returns {Promise<PublicationService>} */
438
+ async #getPublicationService() {
439
+ await this.#getService();
440
+ return this.#publicationService;
441
+ }
442
+
443
+ /** @returns {AsyncIterable<Uint8Array>} */
444
+ async *#openAsset(options) {
445
+ const assets = await this.#getAssetService();
446
+ yield* assets.open(options);
447
+ }
448
+
449
+ /** @returns {AsyncIterable<Uint8Array>} */
450
+ async *#openPage(options) {
451
+ const pages = await this.#getPageService();
452
+ yield* pages.open(options);
453
+ }
454
+
455
+ /** @returns {AsyncIterable<Uint8Array>} */
456
+ async *#openBundleMember(options) {
457
+ const bundles = await this.#getBundleService();
458
+ yield* bundles.openMember(options);
459
+ }
460
+
461
+ /** @returns {AsyncIterable<object>} */
462
+ async *#iterateBundleMembers(options) {
463
+ const bundles = await this.#getBundleService();
464
+ yield* bundles.iterateMembers(options);
465
+ }
466
+
467
+ async #resolveApplicationRoot(value, context = {}) {
468
+ const handle = parseApplicationHandle(value);
469
+ const validation = context.validation ?? { active: new Set(), cache: new Map() };
470
+ const nestingDepth = context.nestingDepth ?? 0;
471
+ const cacheKey = handle.kind === 'bundle'
472
+ ? `${nestingDepth}:${handle.toString()}`
473
+ : handle.toString();
474
+ if (validation.cache.has(cacheKey)) {
475
+ const cached = validation.cache.get(cacheKey);
476
+ validation.cache.delete(cacheKey);
477
+ validation.cache.set(cacheKey, cached);
478
+ return cached;
479
+ }
480
+ if (validation.active.has(cacheKey)) {
481
+ throw createCasError('Application handle graph contains a cycle', ErrorCodes.BUNDLE_CORRUPT, {
482
+ handle: handle.toString(),
483
+ nestingDepth,
484
+ });
485
+ }
486
+ validation.active.add(cacheKey);
487
+ try {
488
+ let target;
489
+ switch (handle.kind) {
490
+ case 'asset':
491
+ target = await this.#assetService.resolveRoot(handle);
492
+ break;
493
+ case 'page':
494
+ target = await this.#pageService.resolveRoot(handle);
495
+ break;
496
+ case 'bundle':
497
+ target = await this.#bundleService.resolveRoot(handle, { nestingDepth, validation });
498
+ break;
499
+ default:
500
+ throw createCasError(
501
+ 'Unsupported application handle kind',
502
+ ErrorCodes.HANDLE_KIND_MISMATCH,
503
+ { kind: handle.kind }
504
+ );
505
+ }
506
+ cacheApplicationTarget(validation.cache, cacheKey, target);
507
+ return target;
508
+ } finally {
509
+ validation.active.delete(cacheKey);
510
+ }
511
+ }
512
+
513
+ async *#openApplicationHandle(value) {
514
+ const handle = parseApplicationHandle(value);
515
+ if (handle.kind === 'asset') {
516
+ yield* this.#assetService.open({ handle });
517
+ return;
518
+ }
519
+ if (handle.kind === 'page') {
520
+ yield* this.#pageService.open({ handle });
521
+ return;
522
+ }
523
+ throw createCasError(
524
+ 'Structured bundle handles cannot be opened as byte streams',
525
+ ErrorCodes.BUNDLE_MEMBER_NOT_STREAMABLE,
526
+ { handle: handle.toString() }
527
+ );
528
+ }
529
+
530
+ async #resolvePublicationRoot(value) {
531
+ const target = await this.#resolveApplicationRoot(value);
532
+ if (target.type === 'tree') {
533
+ return target;
534
+ }
535
+ const oid = await this.service.persistence.writeTree([
536
+ `100644 blob ${target.oid}\tpage`,
537
+ ]);
538
+ return Object.freeze({ ...target, oid, type: 'tree', publicationTargetOid: target.oid });
539
+ }
540
+
209
541
  /**
210
542
  * Lazily initializes and returns the underlying {@link CasService}.
211
543
  * @returns {Promise<CasService>}
@@ -222,6 +554,14 @@ export default class ContentAddressableStore {
222
554
  return await this.#getVault();
223
555
  }
224
556
 
557
+ /**
558
+ * Lazily initializes and returns the root-set registry.
559
+ * @returns {Promise<RootSetRegistry>}
560
+ */
561
+ async getRootSetRegistry() {
562
+ return await this.#getRootSetRegistry();
563
+ }
564
+
225
565
  /**
226
566
  * Factory to create a default JSON CAS from a Git working directory.
227
567
  *
@@ -657,3 +997,16 @@ export default class ContentAddressableStore {
657
997
  return await rotateVaultPassphrase({ service, vault }, options);
658
998
  }
659
999
  }
1000
+
1001
+ function cacheApplicationTarget(cache, key, target) {
1002
+ if (cache.size >= MAX_VALIDATION_CACHE_ENTRIES) {
1003
+ cache.delete(cache.keys().next().value);
1004
+ }
1005
+ cache.set(key, Object.freeze({
1006
+ handle: target.handle,
1007
+ oid: target.oid,
1008
+ type: target.type,
1009
+ size: target.size ?? null,
1010
+ logicalBytes: target.logicalBytes ?? target.size ?? null,
1011
+ }));
1012
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@git-stunts/git-cas",
3
- "version": "6.0.1",
3
+ "version": "6.2.0",
4
4
  "description": "Content-addressed storage backed by Git's object database, with optional encryption and pluggable codecs",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -32,6 +32,8 @@
32
32
  "docs/EXTENDING.md",
33
33
  "docs/STORE_RESTORE_PIPELINE.md",
34
34
  "docs/releases/v6.0.0.md",
35
+ "docs/releases/v6.1.0.md",
36
+ "docs/releases/v6.2.0.md",
35
37
  "docs/THREAT_MODEL.md",
36
38
  "docs/VAULT_INTERNALS.md",
37
39
  "docs/WALKTHROUGH.md",
@@ -76,10 +78,10 @@
76
78
  "test:node": "docker compose run --build --rm test-node",
77
79
  "test:bun": "docker compose run --build --rm test-bun",
78
80
  "test:deno": "docker compose run --build --rm test-deno",
79
- "test:integration": "vitest run test/integration",
80
- "test:integration:node": "docker compose run --build --rm test-node npx vitest run test/integration",
81
- "test:integration:bun": "docker compose run --build --rm test-bun bunx vitest run test/integration",
82
- "test:integration:deno": "docker compose run --build --rm test-deno deno run -A npm:vitest run test/integration",
81
+ "test:integration": "vitest run test/integration --no-file-parallelism",
82
+ "test:integration:node": "docker compose run --build --rm test-node npx vitest run test/integration --no-file-parallelism",
83
+ "test:integration:bun": "docker compose run --build --rm test-bun bunx vitest run test/integration --no-file-parallelism",
84
+ "test:integration:deno": "docker compose run --build --rm test-deno deno run -A npm:vitest run test/integration --no-file-parallelism",
83
85
  "test:platforms": "bats --jobs 3 test/platform/runtimes.bats",
84
86
  "benchmark": "vitest bench test/benchmark",
85
87
  "benchmark:local": "vitest bench test/benchmark",
@@ -97,7 +99,7 @@
97
99
  "@flyingrobots/bijou-tui": "^5.0.0",
98
100
  "@flyingrobots/bijou-tui-app": "^5.0.0",
99
101
  "@git-stunts/alfred": "^0.10.0",
100
- "@git-stunts/plumbing": "^3.0.3",
102
+ "@git-stunts/plumbing": "^3.1.0",
101
103
  "@git-stunts/vault": "^1.0.1",
102
104
  "cbor-x": "^1.6.0",
103
105
  "commander": "14.0.3",
@@ -1,11 +1,40 @@
1
1
  const ErrorCodes = Object.freeze({
2
+ BUNDLE_CORRUPT: 'BUNDLE_CORRUPT',
3
+ BUNDLE_DESCRIPTOR_LIMIT: 'BUNDLE_DESCRIPTOR_LIMIT',
4
+ BUNDLE_DUPLICATE_PATH: 'BUNDLE_DUPLICATE_PATH',
5
+ BUNDLE_FANOUT_LIMIT: 'BUNDLE_FANOUT_LIMIT',
6
+ BUNDLE_LIMIT_INVALID: 'BUNDLE_LIMIT_INVALID',
7
+ BUNDLE_MEMBER_INVALID: 'BUNDLE_MEMBER_INVALID',
8
+ BUNDLE_MEMBER_LIMIT: 'BUNDLE_MEMBER_LIMIT',
9
+ BUNDLE_MEMBER_NOT_FOUND: 'BUNDLE_MEMBER_NOT_FOUND',
10
+ BUNDLE_MEMBER_NOT_STREAMABLE: 'BUNDLE_MEMBER_NOT_STREAMABLE',
11
+ BUNDLE_MEMBER_ORDER: 'BUNDLE_MEMBER_ORDER',
12
+ BUNDLE_PATH_INVALID: 'BUNDLE_PATH_INVALID',
13
+ BUNDLE_PATH_LIMIT: 'BUNDLE_PATH_LIMIT',
14
+ CACHE_CONFLICT: 'CACHE_CONFLICT',
15
+ CACHE_ENTRY_INVALID: 'CACHE_ENTRY_INVALID',
16
+ CACHE_KEY_INVALID: 'CACHE_KEY_INVALID',
17
+ CACHE_LOGICAL_SIZE_UNKNOWN: 'CACHE_LOGICAL_SIZE_UNKNOWN',
18
+ CACHE_POLICY_INVALID: 'CACHE_POLICY_INVALID',
19
+ CACHE_STATE_INVALID: 'CACHE_STATE_INVALID',
20
+ COLLECTION_NAMESPACE_INVALID: 'COLLECTION_NAMESPACE_INVALID',
2
21
  CANNOT_REMOVE_LAST_RECIPIENT: 'CANNOT_REMOVE_LAST_RECIPIENT',
3
22
  DECRYPTION_BUFFER_EXCEEDED: 'DECRYPTION_BUFFER_EXCEEDED',
4
23
  DEK_UNWRAP_FAILED: 'DEK_UNWRAP_FAILED',
5
24
  ENCRYPTION_BUFFER_EXCEEDED: 'ENCRYPTION_BUFFER_EXCEEDED',
25
+ EXPIRING_SET_CONFLICT: 'EXPIRING_SET_CONFLICT',
26
+ EXPIRING_SET_KEY_INVALID: 'EXPIRING_SET_KEY_INVALID',
27
+ EXPIRING_SET_MARKER_INVALID: 'EXPIRING_SET_MARKER_INVALID',
28
+ EXPIRING_SET_STATE_INVALID: 'EXPIRING_SET_STATE_INVALID',
6
29
  GIT_ERROR: 'GIT_ERROR',
30
+ GIT_OBJECT_NOT_FOUND: 'GIT_OBJECT_NOT_FOUND',
7
31
  GIT_PLUMBING_INITIALIZATION_FAILED: 'GIT_PLUMBING_INITIALIZATION_FAILED',
8
32
  GIT_REF_NOT_FOUND: 'GIT_REF_NOT_FOUND',
33
+ HANDLE_CODEC_MISMATCH: 'HANDLE_CODEC_MISMATCH',
34
+ HANDLE_INVALID: 'HANDLE_INVALID',
35
+ HANDLE_KIND_MISMATCH: 'HANDLE_KIND_MISMATCH',
36
+ HANDLE_TARGET_MISSING: 'HANDLE_TARGET_MISSING',
37
+ HANDLE_TARGET_TYPE_MISMATCH: 'HANDLE_TARGET_TYPE_MISMATCH',
9
38
  INTEGRITY_ERROR: 'INTEGRITY_ERROR',
10
39
  INVALID_CHUNKING_STRATEGY: 'INVALID_CHUNKING_STRATEGY',
11
40
  INVALID_ENCRYPTION_SCHEME: 'INVALID_ENCRYPTION_SCHEME',
@@ -22,11 +51,30 @@ const ErrorCodes = Object.freeze({
22
51
  MANIFEST_NOT_FOUND: 'MANIFEST_NOT_FOUND',
23
52
  MISSING_KEY: 'MISSING_KEY',
24
53
  NO_MATCHING_RECIPIENT: 'NO_MATCHING_RECIPIENT',
54
+ PAGE_TOO_LARGE: 'PAGE_TOO_LARGE',
25
55
  PERSISTENCE_CAPABILITY_REQUIRED: 'PERSISTENCE_CAPABILITY_REQUIRED',
26
56
  PORT_NOT_IMPLEMENTED: 'PORT_NOT_IMPLEMENTED',
57
+ PUBLICATION_CONFLICT: 'PUBLICATION_CONFLICT',
58
+ PUBLICATION_INVALID: 'PUBLICATION_INVALID',
59
+ PUBLICATION_REF_FORBIDDEN: 'PUBLICATION_REF_FORBIDDEN',
60
+ PUBLICATION_REF_UPDATE_FAILED: 'PUBLICATION_REF_UPDATE_FAILED',
27
61
  RECIPIENT_ALREADY_EXISTS: 'RECIPIENT_ALREADY_EXISTS',
28
62
  RECIPIENT_NOT_FOUND: 'RECIPIENT_NOT_FOUND',
63
+ REPOSITORY_INSPECTION_INVALID: 'REPOSITORY_INSPECTION_INVALID',
29
64
  RESTORE_TOO_LARGE: 'RESTORE_TOO_LARGE',
65
+ RETENTION_WITNESS_INVALID: 'RETENTION_WITNESS_INVALID',
66
+ ROOT_SET_CONFLICT: 'ROOT_SET_CONFLICT',
67
+ ROOT_SET_DEPENDENCY_INVALID: 'ROOT_SET_DEPENDENCY_INVALID',
68
+ ROOT_SET_ENTRY_INVALID: 'ROOT_SET_ENTRY_INVALID',
69
+ ROOT_SET_HEAD_INVALID: 'ROOT_SET_HEAD_INVALID',
70
+ ROOT_SET_METADATA_INVALID: 'ROOT_SET_METADATA_INVALID',
71
+ ROOT_SET_REF_INVALID: 'ROOT_SET_REF_INVALID',
72
+ ROOT_SET_REF_UPDATE_FAILED: 'ROOT_SET_REF_UPDATE_FAILED',
73
+ ROOT_SET_RETRY_POLICY_INVALID: 'ROOT_SET_RETRY_POLICY_INVALID',
74
+ ROOT_SET_TARGET_MISSING: 'ROOT_SET_TARGET_MISSING',
75
+ ROOT_SET_TARGET_TYPE_MISMATCH: 'ROOT_SET_TARGET_TYPE_MISMATCH',
76
+ ROOT_SET_TARGET_UNREADABLE: 'ROOT_SET_TARGET_UNREADABLE',
77
+ ROOT_SET_TREE_INVALID: 'ROOT_SET_TREE_INVALID',
30
78
  ROTATION_NOT_SUPPORTED: 'ROTATION_NOT_SUPPORTED',
31
79
  SECURITY_BOUNDARY_VIOLATION: 'SECURITY_BOUNDARY_VIOLATION',
32
80
  STORE_ERROR: 'STORE_ERROR',
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @param {unknown} value
3
+ * @param {object} options
4
+ * @param {(message: string, meta: object) => Error} options.invalid
5
+ * @param {string} options.message
6
+ */
7
+ export default function assertCanonicalTimestamp(value, { invalid, message }) {
8
+ if (
9
+ typeof value !== 'string' ||
10
+ Number.isNaN(Date.parse(value)) ||
11
+ new Date(value).toISOString() !== value
12
+ ) {
13
+ throw invalid(message, { observedAt: value });
14
+ }
15
+ }
@@ -0,0 +1,48 @@
1
+ import createCasError from '../errors/createCasError.js';
2
+ import { ErrorCodes } from '../errors/index.js';
3
+
4
+ /**
5
+ * @param {object} options
6
+ * @param {{ readObjectType(oid: string): Promise<string> }} options.persistence
7
+ * @param {{ toString(): string }} options.handle
8
+ * @param {string} options.oid
9
+ * @param {'blob'|'tree'} options.expectedType
10
+ */
11
+ export async function assertHandleObjectType({ persistence, handle, oid, expectedType }) {
12
+ let actualType;
13
+ try {
14
+ actualType = await persistence.readObjectType(oid);
15
+ } catch (error) {
16
+ throw mapHandleTargetError(error, handle, oid);
17
+ }
18
+ if (actualType !== expectedType) {
19
+ throw createCasError(
20
+ 'Handle target has the wrong Git object type',
21
+ ErrorCodes.HANDLE_TARGET_TYPE_MISMATCH,
22
+ { handle: handle.toString(), targetOid: oid, expectedType, actualType }
23
+ );
24
+ }
25
+ }
26
+
27
+ /**
28
+ * @param {unknown} error
29
+ * @param {{ toString(): string, oid: string }} handle
30
+ * @param {string} [targetOid]
31
+ */
32
+ export function mapHandleTargetError(error, handle, targetOid = handle.oid) {
33
+ if (error?.code === ErrorCodes.HANDLE_TARGET_MISSING) {
34
+ return error;
35
+ }
36
+ const message = typeof error?.message === 'string' ? error.message : String(error);
37
+ const missing =
38
+ error?.code === ErrorCodes.GIT_OBJECT_NOT_FOUND ||
39
+ /(?:object|blob|tree) not found/iu.test(message);
40
+ if (!missing) {
41
+ return error;
42
+ }
43
+ return createCasError(
44
+ 'Handle target graph is missing from this repository',
45
+ ErrorCodes.HANDLE_TARGET_MISSING,
46
+ { handle: handle.toString(), targetOid, originalError: error }
47
+ );
48
+ }
@@ -0,0 +1,35 @@
1
+ import { utf8ByteLength } from '../encoding/utf8.js';
2
+
3
+ export const MAX_COLLECTION_KEY_BYTES = 1024;
4
+
5
+ export default function isCanonicalCollectionKey(value) {
6
+ return typeof value === 'string' &&
7
+ value.length > 0 &&
8
+ isWellFormed(value) &&
9
+ value.normalize('NFC') === value &&
10
+ utf8ByteLength(value) <= MAX_COLLECTION_KEY_BYTES &&
11
+ !hasControl(value);
12
+ }
13
+
14
+ function hasControl(value) {
15
+ return [...value].some((character) => {
16
+ const codePoint = character.codePointAt(0);
17
+ return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
18
+ });
19
+ }
20
+
21
+ function isWellFormed(value) {
22
+ for (let index = 0; index < value.length; index++) {
23
+ const codeUnit = value.charCodeAt(index);
24
+ if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
25
+ const next = value.charCodeAt(index + 1);
26
+ if (index + 1 >= value.length || next < 0xdc00 || next > 0xdfff) {
27
+ return false;
28
+ }
29
+ index += 1;
30
+ } else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
31
+ return false;
32
+ }
33
+ }
34
+ return true;
35
+ }