-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
1102 lines (1023 loc) · 44.6 KB
/
Copy pathindex.js
File metadata and controls
1102 lines (1023 loc) · 44.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @ts-self-types="./index.d.ts" */
/**
* @fileoverview Content Addressable Store - Managed blob storage in Git.
*/
// ---------------------------------------------------------------------------
// Imports used in the class body
// ---------------------------------------------------------------------------
import CasService from './src/domain/services/CasService.js';
import VaultService from './src/domain/services/VaultService.js';
import RootSet from './src/domain/services/RootSet.js';
import RootSetRegistry from './src/domain/services/RootSetRegistry.js';
import AssetService from './src/domain/services/AssetService.js';
import BundleService from './src/domain/services/BundleService.js';
import CacheSet from './src/domain/services/CacheSet.js';
import CacheSetRegistry from './src/domain/services/CacheSetRegistry.js';
import ExpiringSet from './src/domain/services/ExpiringSet.js';
import ExpiringSetRegistry from './src/domain/services/ExpiringSetRegistry.js';
import StagingWorkspace from './src/domain/services/StagingWorkspace.js';
import StagingWorkspaceRegistry from './src/domain/services/StagingWorkspaceRegistry.js';
import RepositoryDoctor from './src/domain/services/RepositoryDoctor.js';
import PageService from './src/domain/services/PageService.js';
import PublicationService from './src/domain/services/PublicationService.js';
import RetentionService from './src/domain/services/RetentionService.js';
import rotateVaultPassphrase from './src/domain/services/rotateVaultPassphrase.js';
import GitPersistenceAdapter from './src/infrastructure/adapters/GitPersistenceAdapter.js';
import GitRefAdapter from './src/infrastructure/adapters/GitRefAdapter.js';
import GitRepositoryInspectionAdapter from './src/infrastructure/adapters/GitRepositoryInspectionAdapter.js';
import createCryptoAdapter from './src/infrastructure/adapters/createCryptoAdapter.js';
import { createGitPlumbing } from './src/infrastructure/createGitPlumbing.js';
import { storeFile, restoreFile } from './src/infrastructure/adapters/FileIOHelper.js';
import JsonCodec from './src/infrastructure/codecs/JsonCodec.js';
import CborCodec from './src/infrastructure/codecs/CborCodec.js';
import SilentObserver from './src/infrastructure/adapters/SilentObserver.js';
import resolveChunker from './src/infrastructure/chunkers/resolveChunker.js';
import { CasError, createCasError, ErrorCodes } from './src/domain/errors/index.js';
import FixedChunker from './src/infrastructure/chunkers/FixedChunker.js';
import NodeCompressionAdapter from './src/infrastructure/adapters/NodeCompressionAdapter.js';
import { PACKAGE_VERSION } from './src/package-version.js';
import parseApplicationHandle from './src/domain/value-objects/ApplicationHandle.js';
/** @typedef {import('./src/domain/value-objects/Manifest.js').default} Manifest */
const PKG_VERSION = PACKAGE_VERSION;
const FIXED_CHUNKING_STRATEGY = 'fixed';
const MAX_VALIDATION_CACHE_ENTRIES = 1024;
const RESTORE_FILE_DOCS_URL = `https://github.com/git-stunts/git-cas/blob/v${PKG_VERSION}/docs/API.md#restorefile`;
function withOperationChunker(options) {
const { chunking, ...rest } = options;
if (!chunking) {
return rest;
}
const chunker = resolveChunker({ chunking });
if (chunker) {
return { ...rest, chunker };
}
if (chunking.strategy === FIXED_CHUNKING_STRATEGY && chunking.chunkSize === undefined) {
return { ...rest, chunker: new FixedChunker() };
}
return rest;
}
// ---------------------------------------------------------------------------
// Re-exports — modules used in the class body
// ---------------------------------------------------------------------------
export {
CasService,
VaultService,
RootSet,
RootSetRegistry,
CacheSet,
ExpiringSet,
StagingWorkspace,
RepositoryDoctor,
GitPersistenceAdapter,
GitRefAdapter,
GitRepositoryInspectionAdapter,
JsonCodec,
CborCodec,
SilentObserver,
CasError,
};
// ---------------------------------------------------------------------------
// Re-exports — barrel-only (no local binding needed)
// ---------------------------------------------------------------------------
export { default as NodeCryptoAdapter } from './src/infrastructure/adapters/NodeCryptoAdapter.js';
export { default as CryptoPort } from './src/ports/CryptoPort.js';
export { default as ChunkingPort } from './src/ports/ChunkingPort.js';
export { default as ObservabilityPort } from './src/ports/ObservabilityPort.js';
export { default as Manifest } from './src/domain/value-objects/Manifest.js';
export { default as Chunk } from './src/domain/value-objects/Chunk.js';
export { default as AssetHandle } from './src/domain/value-objects/AssetHandle.js';
export { default as BundleHandle } from './src/domain/value-objects/BundleHandle.js';
export { default as PageHandle } from './src/domain/value-objects/PageHandle.js';
export { default as StagedAsset } from './src/domain/value-objects/StagedAsset.js';
export { default as StagedBundle } from './src/domain/value-objects/StagedBundle.js';
export { default as StagedPage } from './src/domain/value-objects/StagedPage.js';
export { default as RetentionWitness } from './src/domain/value-objects/RetentionWitness.js';
export { default as CacheHit } from './src/domain/value-objects/CacheHit.js';
export { default as CachePolicy } from './src/domain/value-objects/CachePolicy.js';
export { default as ExpiringMarker } from './src/domain/value-objects/ExpiringMarker.js';
export { default as EventEmitterObserver } from './src/infrastructure/adapters/EventEmitterObserver.js';
export { default as StatsCollector } from './src/infrastructure/adapters/StatsCollector.js';
export { default as FixedChunker } from './src/infrastructure/chunkers/FixedChunker.js';
export { default as CdcChunker } from './src/infrastructure/chunkers/CdcChunker.js';
export { default as CompressionPort } from './src/ports/CompressionPort.js';
export { default as RepositoryInspectionPort } from './src/ports/RepositoryInspectionPort.js';
export { default as NodeCompressionAdapter } from './src/infrastructure/adapters/NodeCompressionAdapter.js';
export { default as diffManifests } from './src/domain/services/ManifestDiff.js';
export { SCHEME_WHOLE, SCHEME_FRAMED, SCHEME_CONVERGENT } from './src/domain/encryption/schemes.js';
/**
* High-level facade for the Content Addressable Store library.
*
* Wraps {@link CasService} and {@link VaultService} with lazy initialization,
* runtime-adaptive crypto selection, and convenience helpers for file I/O.
*/
export default class ContentAddressableStore {
/**
* @param {Object} options
* @param {import('@git-stunts/plumbing').default} options.plumbing - GitPlumbing instance for Git operations.
* @param {number} [options.chunkSize] - Chunk size in bytes (default 256 KiB).
* @param {import('./src/ports/CodecPort.js').default} [options.codec] - Manifest codec (default JsonCodec).
* @param {import('./src/ports/CryptoPort.js').default} [options.crypto] - Crypto adapter (auto-detected if omitted).
* @param {import('./src/ports/ObservabilityPort.js').default} [options.observability] - Observability adapter (SilentObserver if omitted).
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy for Git I/O.
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
* @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Chunking strategy config.
* @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance (advanced).
* @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes for encrypted/compressed restores (default 512 MiB).
* @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes (default 10 MiB).
* @param {number} [options.maxPageSize=16777216] - Maximum immutable page size in bytes (default 16 MiB).
* @param {number} [options.pageCacheEntries=128] - Maximum immutable page payloads retained in memory.
* @param {number} [options.pageCacheBytes=8388608] - Maximum immutable page payload bytes retained in memory.
* @param {object} [options.bundleLimits] - Repository-wide maximum bundle admission limits.
* @param {number} [options.maxBundleNestingDepth=32] - Maximum nested bundle depth.
* @param {import('./src/ports/CompressionPort.js').default} [options.compressionAdapter] - Compression adapter (default NodeCompressionAdapter).
* @param {string[]} [options.applicationRefPrefixes] - Explicit application ref namespaces allowed for generic publication.
* @param {{ now(): Date }} [options.clock] - Injectable clock for deterministic evidence.
*/
constructor({
plumbing,
chunkSize,
codec,
policy,
crypto,
observability,
merkleThreshold,
concurrency,
chunking,
chunker,
maxRestoreBufferSize,
maxBlobSize,
maxPageSize,
pageCacheEntries,
pageCacheBytes,
bundleLimits,
maxBundleNestingDepth,
compressionAdapter,
applicationRefPrefixes,
clock,
}) {
this.#config = {
plumbing,
chunkSize,
codec,
policy,
crypto,
observability,
merkleThreshold,
concurrency,
chunking,
chunker,
maxRestoreBufferSize,
maxBlobSize,
maxPageSize,
pageCacheEntries,
pageCacheBytes,
bundleLimits,
maxBundleNestingDepth,
compressionAdapter,
applicationRefPrefixes,
clock,
};
this.service = null;
this.#servicePromise = null;
this.#installCapabilities();
}
#installCapabilities() {
this.rootSets = Object.freeze({
open: async (options) => (await this.#getRootSetRegistry()).open(options),
});
this.caches = Object.freeze({
open: async (options) => (await this.#getCacheSetRegistry()).open(options),
});
this.expiringSets = Object.freeze({
open: async (options) => (await this.#getExpiringSetRegistry()).open(options),
});
this.workspaces = Object.freeze({
open: async (options) => (await this.#getStagingWorkspaceRegistry()).open(options),
inspect: async (options) => (await this.#getStagingWorkspaceRegistry()).inspect(options),
sweep: async (options) => (await this.#getStagingWorkspaceRegistry()).sweep(options),
});
this.diagnostics = Object.freeze({
doctor: async (options) => (await this.#getRepositoryDoctor()).doctor(options),
});
this.assets = Object.freeze({
put: async (options) => (await this.#getAssetService()).put(options),
adopt: async (options) => (await this.#getAssetService()).adopt(options),
open: (options) => this.#openAsset(options),
});
this.pages = Object.freeze({
put: async (options) => (await this.#getPageService()).put(options),
putBatch: async (options) => (await this.#getPageService()).putBatch(options),
get: async (options) => (await this.#getPageService()).get(options),
open: (options) => this.#openPage(options),
});
this.bundles = Object.freeze({
put: async (options) => (await this.#getBundleService()).put(options),
putOrdered: async (options) => (await this.#getBundleService()).putOrdered(options),
getMember: async (options) => (await this.#getBundleService()).getMember(options),
getMemberReference: async (options) => (
await this.#getBundleService()
).getMemberReference(options),
iterateMembers: (options) => this.#iterateBundleMembers(options),
iterateMemberReferences: (options) => this.#iterateBundleMemberReferences(options),
openMember: (options) => this.#openBundleMember(options),
});
this.retention = Object.freeze({
retain: async (options) => (await this.#getRetentionService()).retain(options),
});
this.publications = Object.freeze({
commit: async (options) => (await this.#getPublicationService()).commit(options),
});
}
/** @type {{ plumbing: *, chunkSize?: number, codec?: *, policy?: *, crypto?: *, observability?: *, merkleThreshold?: number, concurrency?: number, chunking?: *, chunker?: *, maxRestoreBufferSize?: number, maxBlobSize?: number, maxPageSize?: number, pageCacheEntries?: number, pageCacheBytes?: number, bundleLimits?: object, maxBundleNestingDepth?: number, compressionAdapter?: *, applicationRefPrefixes?: string[], clock?: { now(): Date } }} */
#config;
/** @type {AssetService|null} */
#assetService = null;
/** @type {BundleService|null} */
#bundleService = null;
/** @type {PageService|null} */
#pageService = null;
/** @type {PublicationService|null} */
#publicationService = null;
/** @type {RetentionService|null} */
#retentionService = null;
/** @type {VaultService|null} */
#vault = null;
/** @type {RootSetRegistry|null} */
#rootSetRegistry = null;
/** @type {CacheSetRegistry|null} */
#cacheSetRegistry = null;
/** @type {ExpiringSetRegistry|null} */
#expiringSetRegistry = null;
/** @type {StagingWorkspaceRegistry|null} */
#stagingWorkspaceRegistry = null;
/** @type {RepositoryDoctor|null} */
#repositoryDoctor = null;
#closePromise = null;
#closed = false;
#servicePromise = null;
/**
* Lazily initializes the service, handling async adapter discovery.
* @private
* @returns {Promise<CasService>}
*/
async #getService() {
this.#assertOpen();
if (!this.#servicePromise) {
this.#servicePromise = this.#initService();
}
return await this.#servicePromise;
}
/**
* Constructs adapters, resolves crypto, and creates CasService + VaultService.
* @private
* @returns {Promise<CasService>}
*/
async #initService() {
const cfg = this.#config;
const persistence = new GitPersistenceAdapter({
plumbing: cfg.plumbing,
policy: cfg.policy,
});
const crypto = cfg.crypto || (await createCryptoAdapter());
const chunkSize = cfg.chunkSize || 256 * 1024;
const chunker =
resolveChunker({ chunker: cfg.chunker, chunking: cfg.chunking }) ||
new FixedChunker({ chunkSize });
this.service = new CasService({
persistence,
chunkSize,
codec: cfg.codec || new JsonCodec(),
crypto,
observability: cfg.observability || new SilentObserver(),
merkleThreshold: cfg.merkleThreshold,
concurrency: cfg.concurrency,
chunker,
maxRestoreBufferSize: cfg.maxRestoreBufferSize,
maxBlobSize: cfg.maxBlobSize,
compressionAdapter: cfg.compressionAdapter || new NodeCompressionAdapter(),
formatVersion: PKG_VERSION,
});
const ref = new GitRefAdapter({
plumbing: cfg.plumbing,
policy: cfg.policy,
});
this.#vault = new VaultService({
persistence,
ref,
crypto,
observability: this.service.observability,
});
this.#rootSetRegistry = new RootSetRegistry({ persistence, ref });
this.#initApplicationServices({ ref, cfg });
this.#initCollectionServices({ persistence, ref, crypto, cfg });
return this.service;
}
#initRepositoryDoctor() {
const cfg = this.#config;
this.#repositoryDoctor = new RepositoryDoctor({
repository: new GitRepositoryInspectionAdapter({
plumbing: cfg.plumbing,
policy: cfg.policy,
}),
rootSets: this.#rootSetRegistry,
caches: this.#cacheSetRegistry,
expiringSets: this.#expiringSetRegistry,
workspaces: this.#stagingWorkspaceRegistry,
vault: this.#vault,
clock: cfg.clock,
});
}
#initCollectionServices({ persistence, ref, crypto, cfg }) {
this.#cacheSetRegistry = new CacheSetRegistry({
persistence,
ref,
bundles: this.#bundleService,
pages: this.#pageService,
resolveHandle: (handle) => this.#resolveApplicationRoot(handle),
crypto,
clock: cfg.clock,
});
this.#expiringSetRegistry = new ExpiringSetRegistry({
persistence,
ref,
bundles: this.#bundleService,
pages: this.#pageService,
crypto,
clock: cfg.clock,
});
this.#stagingWorkspaceRegistry = new StagingWorkspaceRegistry({
persistence,
ref,
assets: this.#assetService,
pages: this.#pageService,
bundles: this.#bundleService,
publications: this.#publicationService,
resolveHandle: (handle) => this.#resolveApplicationRoot(handle),
crypto,
clock: cfg.clock,
});
}
#initApplicationServices({ ref, cfg }) {
this.#assetService = new AssetService({ cas: this.service, clock: cfg.clock });
this.#pageService = new PageService({
persistence: this.service.persistence,
maxPageSize: cfg.maxPageSize,
pageCacheEntries: cfg.pageCacheEntries,
pageCacheBytes: cfg.pageCacheBytes,
clock: cfg.clock,
});
this.#bundleService = new BundleService({
persistence: this.service.persistence,
codec: this.service.codec,
pages: this.#pageService,
resolveHandle: (handle, context) => this.#resolveApplicationRoot(handle, context),
openHandle: (handle) => this.#openApplicationHandle(handle),
limits: cfg.bundleLimits,
maxNestingDepth: cfg.maxBundleNestingDepth,
clock: cfg.clock,
});
const resolveRoot = (handle) => this.#resolveApplicationRoot(handle);
this.#retentionService = new RetentionService({
rootSets: this.#rootSetRegistry,
resolveRoot,
clock: cfg.clock,
});
this.#publicationService = new PublicationService({
ref,
resolveRoot: (handle) => this.#resolvePublicationRoot(handle),
applicationRefPrefixes: cfg.applicationRefPrefixes,
clock: cfg.clock,
});
}
/**
* Lazily initializes and returns the underlying {@link VaultService}.
* @private
* @returns {Promise<VaultService>}
*/
async #getVault() {
await this.#getService();
return this.#vault;
}
/**
* Lazily initializes and returns the root-set registry.
* @private
* @returns {Promise<RootSetRegistry>}
*/
async #getRootSetRegistry() {
await this.#getService();
return this.#rootSetRegistry;
}
/** @returns {Promise<CacheSetRegistry>} */
async #getCacheSetRegistry() {
await this.#getService();
return this.#cacheSetRegistry;
}
/** @returns {Promise<ExpiringSetRegistry>} */
async #getExpiringSetRegistry() {
await this.#getService();
return this.#expiringSetRegistry;
}
/** @returns {Promise<StagingWorkspaceRegistry>} */
async #getStagingWorkspaceRegistry() {
await this.#getService();
return this.#stagingWorkspaceRegistry;
}
/** @returns {Promise<RepositoryDoctor>} */
async #getRepositoryDoctor() {
await this.#getService();
if (this.#repositoryDoctor === null) {
this.#initRepositoryDoctor();
}
return this.#repositoryDoctor;
}
/** @returns {Promise<AssetService>} */
async #getAssetService() {
await this.#getService();
return this.#assetService;
}
/** @returns {Promise<PageService>} */
async #getPageService() {
await this.#getService();
return this.#pageService;
}
/** @returns {Promise<BundleService>} */
async #getBundleService() {
await this.#getService();
return this.#bundleService;
}
/** @returns {Promise<RetentionService>} */
async #getRetentionService() {
await this.#getService();
return this.#retentionService;
}
/** @returns {Promise<PublicationService>} */
async #getPublicationService() {
await this.#getService();
return this.#publicationService;
}
/** @returns {AsyncIterable<Uint8Array>} */
async *#openAsset(options) {
const assets = await this.#getAssetService();
yield* assets.open(options);
}
/** @returns {AsyncIterable<Uint8Array>} */
async *#openPage(options) {
const pages = await this.#getPageService();
yield* pages.open(options);
}
/** @returns {AsyncIterable<Uint8Array>} */
async *#openBundleMember(options) {
const bundles = await this.#getBundleService();
yield* bundles.openMember(options);
}
/** @returns {AsyncIterable<object>} */
async *#iterateBundleMemberReferences(options) {
const bundles = await this.#getBundleService();
yield* bundles.iterateMemberReferences(options);
}
/** @returns {AsyncIterable<object>} */
async *#iterateBundleMembers(options) {
const bundles = await this.#getBundleService();
yield* bundles.iterateMembers(options);
}
async #resolveApplicationRoot(value, context = {}) {
const handle = parseApplicationHandle(value);
const validation = context.validation ?? { active: new Set(), cache: new Map() };
const nestingDepth = context.nestingDepth ?? 0;
const cacheKey = handle.kind === 'bundle'
? `${nestingDepth}:${handle.toString()}`
: handle.toString();
if (validation.cache.has(cacheKey)) {
const cached = validation.cache.get(cacheKey);
validation.cache.delete(cacheKey);
validation.cache.set(cacheKey, cached);
return cached;
}
if (validation.active.has(cacheKey)) {
throw createCasError('Application handle graph contains a cycle', ErrorCodes.BUNDLE_CORRUPT, {
handle: handle.toString(),
nestingDepth,
});
}
validation.active.add(cacheKey);
try {
let target;
switch (handle.kind) {
case 'asset':
target = await this.#assetService.resolveRoot(handle);
break;
case 'page':
target = await this.#pageService.resolveRoot(handle);
break;
case 'bundle':
target = await this.#bundleService.resolveRoot(handle, { nestingDepth, validation });
break;
default:
throw createCasError(
'Unsupported application handle kind',
ErrorCodes.HANDLE_KIND_MISMATCH,
{ kind: handle.kind }
);
}
cacheApplicationTarget(validation.cache, cacheKey, target);
return target;
} finally {
validation.active.delete(cacheKey);
}
}
async *#openApplicationHandle(value) {
const handle = parseApplicationHandle(value);
if (handle.kind === 'asset') {
yield* this.#assetService.open({ handle });
return;
}
if (handle.kind === 'page') {
yield* this.#pageService.open({ handle });
return;
}
throw createCasError(
'Structured bundle handles cannot be opened as byte streams',
ErrorCodes.BUNDLE_MEMBER_NOT_STREAMABLE,
{ handle: handle.toString() }
);
}
async #resolvePublicationRoot(value) {
const target = await this.#resolveApplicationRoot(value);
if (target.type === 'tree') {
return target;
}
const oid = await this.service.persistence.writeTree([
`100644 blob ${target.oid}\tpage`,
]);
return Object.freeze({ ...target, oid, type: 'tree', publicationTargetOid: target.oid });
}
/**
* Lazily initializes and returns the underlying {@link CasService}.
* @returns {Promise<CasService>}
*/
async getService() {
return await this.#getService();
}
/**
* Lazily initializes and returns the underlying {@link VaultService}.
* @returns {Promise<VaultService>}
*/
async getVaultService() {
return await this.#getVault();
}
/**
* Lazily initializes and returns the root-set registry.
* @returns {Promise<RootSetRegistry>}
*/
async getRootSetRegistry() {
return await this.#getRootSetRegistry();
}
/**
* Releases local adapter resources. This does not mutate stored objects,
* refs, retention, or publication state.
* @returns {Promise<void>}
*/
async close() {
if (this.#closePromise !== null) {
return await this.#closePromise;
}
this.#closed = true;
this.#closePromise = (async () => {
if (this.#servicePromise === null) {
return;
}
const service = await this.#servicePromise;
await service.persistence.close();
})();
return await this.#closePromise;
}
/** @returns {Promise<void>} */
async [Symbol.asyncDispose]() {
await this.close();
}
#assertOpen() {
if (this.#closed) {
throw createCasError('Content-addressable store is closed', ErrorCodes.RESOURCE_CLOSED);
}
}
/**
* Factory to create a default JSON CAS from a Git working directory.
*
* This is the shortest path for normal callers: the facade constructs the
* runtime-aware Git plumbing adapter and keeps all other options available.
*
* @param {Object} [options]
* @param {string} [options.cwd='.'] - Git working directory.
* @param {string} [options.env] - Shell runner environment override.
* @param {number} [options.chunkSize] - Chunk size in bytes.
* @param {import('./src/ports/CodecPort.js').default} [options.codec] - Manifest codec.
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy.
* @param {import('./src/ports/CryptoPort.js').default} [options.crypto] - Crypto adapter.
* @param {import('./src/ports/ObservabilityPort.js').default} [options.observability] - Observability adapter.
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
* @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Chunking strategy config.
* @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance.
* @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes.
* @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes.
* @param {number} [options.maxPageSize=16777216] - Maximum immutable page size in bytes.
* @param {number} [options.pageCacheEntries=128] - Maximum immutable page payloads retained in memory.
* @param {number} [options.pageCacheBytes=8388608] - Maximum immutable page payload bytes retained in memory.
* @param {import('./src/ports/CompressionPort.js').default} [options.compressionAdapter] - Compression adapter.
* @returns {Promise<ContentAddressableStore>}
*/
static async open({ cwd = '.', env, ...options } = {}) {
return new ContentAddressableStore({
...options,
plumbing: await createGitPlumbing({ cwd, env }),
});
}
/**
* Factory to create a CAS with JSON codec.
* @param {Object} options
* @param {import('@git-stunts/plumbing').default} options.plumbing - GitPlumbing instance.
* @param {number} [options.chunkSize] - Chunk size in bytes.
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy.
* @param {import('./src/ports/CryptoPort.js').default} [options.crypto] - Crypto adapter.
* @param {import('./src/ports/ObservabilityPort.js').default} [options.observability] - Observability adapter.
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
* @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Chunking strategy config.
* @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance.
* @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes.
* @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes.
* @param {number} [options.maxPageSize=16777216] - Maximum immutable page size in bytes.
* @param {number} [options.pageCacheEntries=128] - Maximum immutable page payloads retained in memory.
* @param {number} [options.pageCacheBytes=8388608] - Maximum immutable page payload bytes retained in memory.
* @param {import('./src/ports/CompressionPort.js').default} [options.compressionAdapter] - Compression adapter.
* @returns {ContentAddressableStore}
*/
static createJson({ plumbing, ...options }) {
return new ContentAddressableStore({ ...options, plumbing, codec: new JsonCodec() });
}
/**
* Factory to create a CAS with CBOR codec.
* @param {Object} options
* @param {import('@git-stunts/plumbing').default} options.plumbing - GitPlumbing instance.
* @param {number} [options.chunkSize] - Chunk size in bytes.
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy.
* @param {import('./src/ports/CryptoPort.js').default} [options.crypto] - Crypto adapter.
* @param {import('./src/ports/ObservabilityPort.js').default} [options.observability] - Observability adapter.
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
* @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Chunking strategy config.
* @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance.
* @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes.
* @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes.
* @param {number} [options.maxPageSize=16777216] - Maximum immutable page size in bytes.
* @param {number} [options.pageCacheEntries=128] - Maximum immutable page payloads retained in memory.
* @param {number} [options.pageCacheBytes=8388608] - Maximum immutable page payload bytes retained in memory.
* @param {import('./src/ports/CompressionPort.js').default} [options.compressionAdapter] - Compression adapter.
* @returns {ContentAddressableStore}
*/
static createCbor({ plumbing, ...options }) {
return new ContentAddressableStore({ ...options, plumbing, codec: new CborCodec() });
}
/**
* Returns the configured chunk size in bytes.
* @returns {number}
*/
get chunkSize() {
return this.service?.chunkSize || this.#config.chunkSize || 256 * 1024;
}
/**
* Encrypts bytes using AES-256-GCM.
* @param {Object} options
* @param {Uint8Array} options.buffer - Plaintext data to encrypt.
* @param {Uint8Array} options.key - 32-byte encryption key.
* @returns {Promise<{ buf: Uint8Array, meta: { algorithm: string, nonce: string, tag: string, encrypted: boolean } }>}
*/
async encrypt(options) {
const service = await this.#getService();
return await service.encrypt(options);
}
/**
* Decrypts bytes. Returns them unchanged if `meta.encrypted` is falsy.
* @param {Object} options
* @param {Uint8Array} options.buffer - Ciphertext to decrypt.
* @param {Uint8Array} options.key - 32-byte encryption key.
* @param {{ encrypted: boolean, algorithm: string, nonce: string, tag: string }} options.meta - Encryption metadata.
* @returns {Promise<Uint8Array>}
*/
async decrypt(options) {
const service = await this.#getService();
return await service.decrypt(options);
}
/**
* Reads a file from disk and stores it in Git as chunked blobs.
* @param {Object} options
* @param {string} options.filePath - Absolute or relative path to the file.
* @param {string} options.slug - Logical identifier for the stored asset.
* @param {string} [options.filename] - Override filename (defaults to basename of filePath).
* @param {Uint8Array} [options.encryptionKey] - 32-byte key for AES-256-GCM encryption.
* @param {string} [options.passphrase] - Derive encryption key from passphrase.
* @param {{ scheme?: 'whole'|'framed'|'convergent', frameBytes?: number, convergent?: boolean }} [options.encryption] - Explicit encryption scheme selection.
* @param {Object} [options.kdfOptions] - KDF options when using passphrase.
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
* @param {Array<{label: string, key: Uint8Array}>} [options.recipients] - Envelope recipients (mutually exclusive with encryptionKey/passphrase).
* @param {number} [options.merkleThreshold] - Per-operation chunk count threshold for Merkle tree publication.
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Per-operation chunking strategy config.
* @returns {Promise<Manifest>} The resulting manifest.
*/
async storeFile(options) {
const service = await this.#getService();
return await storeFile(service, withOperationChunker(options));
}
/**
* Stores an async iterable source in Git as chunked blobs.
* @param {Object} options
* @param {AsyncIterable<Uint8Array>} options.source - Data to store.
* @param {string} options.slug - Logical identifier for the stored asset.
* @param {string} options.filename - Filename for the manifest.
* @param {Uint8Array} [options.encryptionKey] - 32-byte key for AES-256-GCM encryption.
* @param {string} [options.passphrase] - Derive encryption key from passphrase.
* @param {{ scheme?: 'whole'|'framed'|'convergent', frameBytes?: number, convergent?: boolean }} [options.encryption] - Explicit encryption scheme selection.
* @param {Object} [options.kdfOptions] - KDF options when using passphrase.
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
* @param {Array<{label: string, key: Uint8Array}>} [options.recipients] - Envelope recipients (mutually exclusive with encryptionKey/passphrase).
* @param {number} [options.merkleThreshold] - Per-operation chunk count threshold for Merkle tree publication.
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Per-operation chunking strategy config.
* @returns {Promise<Manifest>} The resulting manifest.
*/
async store(options) {
const service = await this.#getService();
return await service.store(withOperationChunker(options));
}
/**
* Restores a file from its manifest and writes it to disk.
* @param {Object} options
* @param {Manifest} options.manifest - The file manifest.
* @param {Uint8Array} [options.encryptionKey] - 32-byte key, required if manifest is encrypted.
* @param {string} [options.passphrase] - Passphrase for KDF-based decryption.
* @param {string} options.outputPath - Destination file path.
* @param {string} options.baseDirectory - Directory boundary that outputPath must stay inside.
* @returns {Promise<{ bytesWritten: number }>}
*/
async restoreFile(options) {
if (!options?.baseDirectory) {
throw createCasError({
message:
'baseDirectory is required for safe restoration. If you are restoring in a trusted local environment, pass baseDirectory: process.cwd().',
code: ErrorCodes.INVALID_OPTIONS,
meta: { option: 'baseDirectory' },
documentationUrl: RESTORE_FILE_DOCS_URL,
});
}
const service = await this.#getService();
return await restoreFile(service, options);
}
/**
* Restores a file from its manifest, returning the bytes directly.
* @param {Object} options
* @param {Manifest} options.manifest - The file manifest.
* @param {Uint8Array} [options.encryptionKey] - 32-byte key, required if manifest is encrypted.
* @param {string} [options.passphrase] - Passphrase for KDF-based decryption.
* @returns {Promise<{ buffer: Uint8Array, bytesWritten: number }>}
*/
async restore(options) {
const service = await this.#getService();
return await service.restore(options);
}
/**
* Restores a file from its manifest as an async iterable of byte chunks.
* @param {Object} options
* @param {Manifest} options.manifest - The file manifest.
* @param {Uint8Array} [options.encryptionKey] - 32-byte key, required if manifest is encrypted.
* @param {string} [options.passphrase] - Passphrase for KDF-based decryption.
* @returns {AsyncIterable<Uint8Array>}
*/
async *restoreStream(options) {
const service = await this.#getService();
yield* service.restoreStream(options);
}
/**
* Creates a Git tree object from a manifest.
* @param {Object} options
* @param {Manifest} options.manifest - The file manifest.
* @param {number} [options.merkleThreshold] - Override chunk count threshold for this tree publication.
* @returns {Promise<string>} Git OID of the created tree.
*/
async createTree(options) {
const service = await this.#getService();
return await service.createTree(options);
}
/**
* Verifies the integrity of a stored file by re-hashing its chunks.
* @param {Manifest} manifest - The file manifest.
* @param {{ encryptionKey?: Uint8Array, passphrase?: string }} [options] - Optional decryption credentials for encrypted manifests.
* @returns {Promise<boolean>} `true` if all chunks pass verification.
*/
async verifyIntegrity(manifest, options) {
const service = await this.#getService();
return await service.verifyIntegrity(manifest, options);
}
/**
* Reads a manifest from a Git tree OID.
* @param {Object} options
* @param {string} options.treeOid - Git tree OID to read the manifest from.
* @returns {Promise<Manifest>}
*/
async readManifest(options) {
const service = await this.#getService();
return await service.readManifest(options);
}
/**
* Compares two manifests by chunk digest.
* Pure function — no I/O needed. Does not require initialization.
* @param {Manifest} oldManifest
* @param {Manifest} newManifest
* @returns {import('./src/domain/services/ManifestDiff.js').ManifestDiffResult}
*/
static diffManifests(oldManifest, newManifest) {
return CasService.diffManifests(oldManifest, newManifest);
}
/**
* Reads a manifest from a Git tree and returns inspection metadata.
* @param {Object} options
* @param {string} options.treeOid - Git tree OID of the asset.
* @returns {Promise<{ slug: string, chunksOrphaned: number }>}
*/
async inspectAsset(options) {
const service = await this.#getService();
return await service.inspectAsset(options);
}
/**
* @deprecated Use {@link inspectAsset} instead.
* @param {Object} options
* @param {string} options.treeOid - Git tree OID of the asset.
* @returns {Promise<{ slug: string, chunksOrphaned: number }>}
*/
async deleteAsset(options) {
const service = await this.#getService();
return await service.deleteAsset(options);
}
/**
* Aggregates referenced chunk blob OIDs across multiple stored assets.
* @param {Object} options
* @param {string[]} options.treeOids - Git tree OIDs to analyze.
* @returns {Promise<{ referenced: Set<string>, total: number }>}
*/
async collectReferencedChunks(options) {
const service = await this.#getService();
return await service.collectReferencedChunks(options);
}
/**
* @deprecated Use {@link collectReferencedChunks} instead.
* @param {Object} options
* @param {string[]} options.treeOids - Git tree OIDs to analyze.
* @returns {Promise<{ referenced: Set<string>, total: number }>}
*/
async findOrphanedChunks(options) {
const service = await this.#getService();
return await service.findOrphanedChunks(options);
}
/**
* Derives an encryption key from a passphrase using PBKDF2 or scrypt.
* @param {Object} options
* @param {string} options.passphrase - The passphrase.
* @param {Uint8Array} [options.salt] - Salt (random if omitted).
* @param {'pbkdf2'|'scrypt'} [options.algorithm='pbkdf2'] - KDF algorithm.
* @param {number} [options.iterations] - PBKDF2 iterations.
* @param {number} [options.cost] - scrypt cost (N).
* @param {number} [options.blockSize] - scrypt block size (r).
* @param {number} [options.parallelization] - scrypt parallelization (p).
* @param {number} [options.keyLength=32] - Derived key length.
* @returns {Promise<{ key: Uint8Array, salt: Uint8Array, params: Object }>}
*/
async deriveKey(options) {
const service = await this.#getService();
return await service.deriveKey(options);
}
// ---------------------------------------------------------------------------
// Recipient management — delegates to CasService
// ---------------------------------------------------------------------------
/**
* Adds a recipient to an envelope-encrypted manifest.
* @param {Object} options
* @param {Manifest} options.manifest
* @param {Uint8Array} options.existingKey - KEK of an existing recipient.
* @param {Uint8Array} options.newRecipientKey - KEK for the new recipient.
* @param {string} options.label - Label for the new recipient.
* @returns {Promise<Manifest>}
*/
async addRecipient(options) {
const service = await this.#getService();
return await service.addRecipient(options);
}
/**
* Removes a recipient from an envelope-encrypted manifest.
* @param {Object} options
* @param {Manifest} options.manifest
* @param {string} options.label - Label to remove.
* @returns {Promise<Manifest>}
*/
async removeRecipient(options) {
const service = await this.#getService();
return await service.removeRecipient(options);
}
/**
* Lists recipient labels from an envelope-encrypted manifest.
* @param {Manifest} manifest
* @returns {Promise<string[]>}
*/
async listRecipients(manifest) {
const service = await this.#getService();
return service.listRecipients(manifest);
}
/**
* Rotates a recipient's key without re-encrypting data blobs.