@git-stunts/git-cas 6.0.0 → 6.1.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/ADVANCED_GUIDE.md +111 -90
- package/ARCHITECTURE.md +12 -3
- package/CHANGELOG.md +78 -3
- package/GUIDE.md +242 -188
- package/README.md +67 -212
- package/UPGRADING.md +28 -1
- package/bin/ui/dashboard.js +442 -183
- package/bin/ui/store-wizard.js +90 -19
- package/build-info.json +2 -2
- package/docs/API.md +273 -51
- package/docs/WALKTHROUGH.md +46 -0
- package/docs/releases/v6.1.0.md +75 -0
- package/index.d.ts +185 -39
- package/index.js +95 -15
- package/package.json +6 -5
- package/src/domain/errors/Codes.js +13 -0
- package/src/domain/services/CasService.d.ts +42 -26
- package/src/domain/services/CasService.js +53 -32
- package/src/domain/services/ChunkRepository.js +50 -12
- package/src/domain/services/RootSet.js +277 -0
- package/src/domain/services/RootSetMetadataCodec.js +253 -0
- package/src/domain/services/RootSetPersistence.js +334 -0
- package/src/domain/services/RootSetRegistry.js +44 -0
- package/src/domain/services/RootSetRetryPolicy.js +87 -0
- package/src/domain/services/RootSetTreeCodec.js +95 -0
- package/src/domain/value-objects/RootSetRef.js +88 -0
- package/src/infrastructure/adapters/FileIOHelper.js +33 -31
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +29 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +20 -0
- package/src/infrastructure/chunkers/resolveChunker.js +7 -5
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +9 -0
- package/src/ports/GitRefPort.js +9 -0
package/index.js
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
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';
|
|
11
13
|
import rotateVaultPassphrase from './src/domain/services/rotateVaultPassphrase.js';
|
|
12
14
|
import GitPersistenceAdapter from './src/infrastructure/adapters/GitPersistenceAdapter.js';
|
|
13
15
|
import GitRefAdapter from './src/infrastructure/adapters/GitRefAdapter.js';
|
|
@@ -26,8 +28,23 @@ import { PACKAGE_VERSION } from './src/package-version.js';
|
|
|
26
28
|
/** @typedef {import('./src/domain/value-objects/Manifest.js').default} Manifest */
|
|
27
29
|
|
|
28
30
|
const PKG_VERSION = PACKAGE_VERSION;
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
+
const FIXED_CHUNKING_STRATEGY = 'fixed';
|
|
32
|
+
const RESTORE_FILE_DOCS_URL = `https://github.com/git-stunts/git-cas/blob/v${PKG_VERSION}/docs/API.md#restorefile`;
|
|
33
|
+
|
|
34
|
+
function withOperationChunker(options) {
|
|
35
|
+
const { chunking, ...rest } = options;
|
|
36
|
+
if (!chunking) {
|
|
37
|
+
return rest;
|
|
38
|
+
}
|
|
39
|
+
const chunker = resolveChunker({ chunking });
|
|
40
|
+
if (chunker) {
|
|
41
|
+
return { ...rest, chunker };
|
|
42
|
+
}
|
|
43
|
+
if (chunking.strategy === FIXED_CHUNKING_STRATEGY && chunking.chunkSize === undefined) {
|
|
44
|
+
return { ...rest, chunker: new FixedChunker() };
|
|
45
|
+
}
|
|
46
|
+
return rest;
|
|
47
|
+
}
|
|
31
48
|
|
|
32
49
|
// ---------------------------------------------------------------------------
|
|
33
50
|
// Re-exports — modules used in the class body
|
|
@@ -35,6 +52,8 @@ const RESTORE_FILE_DOCS_URL =
|
|
|
35
52
|
export {
|
|
36
53
|
CasService,
|
|
37
54
|
VaultService,
|
|
55
|
+
RootSet,
|
|
56
|
+
RootSetRegistry,
|
|
38
57
|
GitPersistenceAdapter,
|
|
39
58
|
GitRefAdapter,
|
|
40
59
|
JsonCodec,
|
|
@@ -78,22 +97,55 @@ export default class ContentAddressableStore {
|
|
|
78
97
|
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy for Git I/O.
|
|
79
98
|
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
|
|
80
99
|
* @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
|
|
81
|
-
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number }} [options.chunking] - Chunking strategy config.
|
|
100
|
+
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Chunking strategy config.
|
|
82
101
|
* @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance (advanced).
|
|
83
102
|
* @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes for encrypted/compressed restores (default 512 MiB).
|
|
84
103
|
* @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes (default 10 MiB).
|
|
85
104
|
* @param {import('./src/ports/CompressionPort.js').default} [options.compressionAdapter] - Compression adapter (default NodeCompressionAdapter).
|
|
86
105
|
*/
|
|
87
|
-
constructor({
|
|
88
|
-
|
|
106
|
+
constructor({
|
|
107
|
+
plumbing,
|
|
108
|
+
chunkSize,
|
|
109
|
+
codec,
|
|
110
|
+
policy,
|
|
111
|
+
crypto,
|
|
112
|
+
observability,
|
|
113
|
+
merkleThreshold,
|
|
114
|
+
concurrency,
|
|
115
|
+
chunking,
|
|
116
|
+
chunker,
|
|
117
|
+
maxRestoreBufferSize,
|
|
118
|
+
maxBlobSize,
|
|
119
|
+
compressionAdapter,
|
|
120
|
+
}) {
|
|
121
|
+
this.#config = {
|
|
122
|
+
plumbing,
|
|
123
|
+
chunkSize,
|
|
124
|
+
codec,
|
|
125
|
+
policy,
|
|
126
|
+
crypto,
|
|
127
|
+
observability,
|
|
128
|
+
merkleThreshold,
|
|
129
|
+
concurrency,
|
|
130
|
+
chunking,
|
|
131
|
+
chunker,
|
|
132
|
+
maxRestoreBufferSize,
|
|
133
|
+
maxBlobSize,
|
|
134
|
+
compressionAdapter,
|
|
135
|
+
};
|
|
89
136
|
this.service = null;
|
|
90
137
|
this.#servicePromise = null;
|
|
138
|
+
this.rootSets = Object.freeze({
|
|
139
|
+
open: async (options) => (await this.#getRootSetRegistry()).open(options),
|
|
140
|
+
});
|
|
91
141
|
}
|
|
92
142
|
|
|
93
143
|
/** @type {{ plumbing: *, chunkSize?: number, codec?: *, policy?: *, crypto?: *, observability?: *, merkleThreshold?: number, concurrency?: number, chunking?: *, chunker?: *, maxRestoreBufferSize?: number, maxBlobSize?: number, compressionAdapter?: * }} */
|
|
94
144
|
#config;
|
|
95
145
|
/** @type {VaultService|null} */
|
|
96
146
|
#vault = null;
|
|
147
|
+
/** @type {RootSetRegistry|null} */
|
|
148
|
+
#rootSetRegistry = null;
|
|
97
149
|
#servicePromise = null;
|
|
98
150
|
|
|
99
151
|
/**
|
|
@@ -119,10 +171,11 @@ export default class ContentAddressableStore {
|
|
|
119
171
|
plumbing: cfg.plumbing,
|
|
120
172
|
policy: cfg.policy,
|
|
121
173
|
});
|
|
122
|
-
const crypto = cfg.crypto || await createCryptoAdapter();
|
|
174
|
+
const crypto = cfg.crypto || (await createCryptoAdapter());
|
|
123
175
|
const chunkSize = cfg.chunkSize || 256 * 1024;
|
|
124
|
-
const chunker =
|
|
125
|
-
|
|
176
|
+
const chunker =
|
|
177
|
+
resolveChunker({ chunker: cfg.chunker, chunking: cfg.chunking }) ||
|
|
178
|
+
new FixedChunker({ chunkSize });
|
|
126
179
|
this.service = new CasService({
|
|
127
180
|
persistence,
|
|
128
181
|
chunkSize,
|
|
@@ -142,7 +195,13 @@ export default class ContentAddressableStore {
|
|
|
142
195
|
plumbing: cfg.plumbing,
|
|
143
196
|
policy: cfg.policy,
|
|
144
197
|
});
|
|
145
|
-
this.#vault = new VaultService({
|
|
198
|
+
this.#vault = new VaultService({
|
|
199
|
+
persistence,
|
|
200
|
+
ref,
|
|
201
|
+
crypto,
|
|
202
|
+
observability: this.service.observability,
|
|
203
|
+
});
|
|
204
|
+
this.#rootSetRegistry = new RootSetRegistry({ persistence, ref });
|
|
146
205
|
|
|
147
206
|
return this.service;
|
|
148
207
|
}
|
|
@@ -157,6 +216,16 @@ export default class ContentAddressableStore {
|
|
|
157
216
|
return this.#vault;
|
|
158
217
|
}
|
|
159
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Lazily initializes and returns the root-set registry.
|
|
221
|
+
* @private
|
|
222
|
+
* @returns {Promise<RootSetRegistry>}
|
|
223
|
+
*/
|
|
224
|
+
async #getRootSetRegistry() {
|
|
225
|
+
await this.#getService();
|
|
226
|
+
return this.#rootSetRegistry;
|
|
227
|
+
}
|
|
228
|
+
|
|
160
229
|
/**
|
|
161
230
|
* Lazily initializes and returns the underlying {@link CasService}.
|
|
162
231
|
* @returns {Promise<CasService>}
|
|
@@ -173,6 +242,14 @@ export default class ContentAddressableStore {
|
|
|
173
242
|
return await this.#getVault();
|
|
174
243
|
}
|
|
175
244
|
|
|
245
|
+
/**
|
|
246
|
+
* Lazily initializes and returns the root-set registry.
|
|
247
|
+
* @returns {Promise<RootSetRegistry>}
|
|
248
|
+
*/
|
|
249
|
+
async getRootSetRegistry() {
|
|
250
|
+
return await this.#getRootSetRegistry();
|
|
251
|
+
}
|
|
252
|
+
|
|
176
253
|
/**
|
|
177
254
|
* Factory to create a default JSON CAS from a Git working directory.
|
|
178
255
|
*
|
|
@@ -189,7 +266,7 @@ export default class ContentAddressableStore {
|
|
|
189
266
|
* @param {import('./src/ports/ObservabilityPort.js').default} [options.observability] - Observability adapter.
|
|
190
267
|
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
|
|
191
268
|
* @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
|
|
192
|
-
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number }} [options.chunking] - Chunking strategy config.
|
|
269
|
+
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Chunking strategy config.
|
|
193
270
|
* @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance.
|
|
194
271
|
* @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes.
|
|
195
272
|
* @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes.
|
|
@@ -213,7 +290,7 @@ export default class ContentAddressableStore {
|
|
|
213
290
|
* @param {import('./src/ports/ObservabilityPort.js').default} [options.observability] - Observability adapter.
|
|
214
291
|
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
|
|
215
292
|
* @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
|
|
216
|
-
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number }} [options.chunking] - Chunking strategy config.
|
|
293
|
+
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Chunking strategy config.
|
|
217
294
|
* @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance.
|
|
218
295
|
* @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes.
|
|
219
296
|
* @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes.
|
|
@@ -234,7 +311,7 @@ export default class ContentAddressableStore {
|
|
|
234
311
|
* @param {import('./src/ports/ObservabilityPort.js').default} [options.observability] - Observability adapter.
|
|
235
312
|
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
|
|
236
313
|
* @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
|
|
237
|
-
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number }} [options.chunking] - Chunking strategy config.
|
|
314
|
+
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Chunking strategy config.
|
|
238
315
|
* @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance.
|
|
239
316
|
* @param {number} [options.maxRestoreBufferSize=536870912] - Max buffered restore size in bytes.
|
|
240
317
|
* @param {number} [options.maxBlobSize=10485760] - Safety limit for readBlob metadata in bytes.
|
|
@@ -291,11 +368,12 @@ export default class ContentAddressableStore {
|
|
|
291
368
|
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
|
|
292
369
|
* @param {Array<{label: string, key: Uint8Array}>} [options.recipients] - Envelope recipients (mutually exclusive with encryptionKey/passphrase).
|
|
293
370
|
* @param {number} [options.merkleThreshold] - Per-operation chunk count threshold for Merkle tree publication.
|
|
371
|
+
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Per-operation chunking strategy config.
|
|
294
372
|
* @returns {Promise<Manifest>} The resulting manifest.
|
|
295
373
|
*/
|
|
296
374
|
async storeFile(options) {
|
|
297
375
|
const service = await this.#getService();
|
|
298
|
-
return await storeFile(service, options);
|
|
376
|
+
return await storeFile(service, withOperationChunker(options));
|
|
299
377
|
}
|
|
300
378
|
|
|
301
379
|
/**
|
|
@@ -311,11 +389,12 @@ export default class ContentAddressableStore {
|
|
|
311
389
|
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
|
|
312
390
|
* @param {Array<{label: string, key: Uint8Array}>} [options.recipients] - Envelope recipients (mutually exclusive with encryptionKey/passphrase).
|
|
313
391
|
* @param {number} [options.merkleThreshold] - Per-operation chunk count threshold for Merkle tree publication.
|
|
392
|
+
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number, normalized?: boolean }} [options.chunking] - Per-operation chunking strategy config.
|
|
314
393
|
* @returns {Promise<Manifest>} The resulting manifest.
|
|
315
394
|
*/
|
|
316
395
|
async store(options) {
|
|
317
396
|
const service = await this.#getService();
|
|
318
|
-
return await service.store(options);
|
|
397
|
+
return await service.store(withOperationChunker(options));
|
|
319
398
|
}
|
|
320
399
|
|
|
321
400
|
/**
|
|
@@ -331,7 +410,8 @@ export default class ContentAddressableStore {
|
|
|
331
410
|
async restoreFile(options) {
|
|
332
411
|
if (!options?.baseDirectory) {
|
|
333
412
|
throw createCasError({
|
|
334
|
-
message:
|
|
413
|
+
message:
|
|
414
|
+
'baseDirectory is required for safe restoration. If you are restoring in a trusted local environment, pass baseDirectory: process.cwd().',
|
|
335
415
|
code: ErrorCodes.INVALID_OPTIONS,
|
|
336
416
|
meta: { option: 'baseDirectory' },
|
|
337
417
|
documentationUrl: RESTORE_FILE_DOCS_URL,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@git-stunts/git-cas",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.1.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,7 @@
|
|
|
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",
|
|
35
36
|
"docs/THREAT_MODEL.md",
|
|
36
37
|
"docs/VAULT_INTERNALS.md",
|
|
37
38
|
"docs/WALKTHROUGH.md",
|
|
@@ -76,10 +77,10 @@
|
|
|
76
77
|
"test:node": "docker compose run --build --rm test-node",
|
|
77
78
|
"test:bun": "docker compose run --build --rm test-bun",
|
|
78
79
|
"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",
|
|
80
|
+
"test:integration": "vitest run test/integration --no-file-parallelism",
|
|
81
|
+
"test:integration:node": "docker compose run --build --rm test-node npx vitest run test/integration --no-file-parallelism",
|
|
82
|
+
"test:integration:bun": "docker compose run --build --rm test-bun bunx vitest run test/integration --no-file-parallelism",
|
|
83
|
+
"test:integration:deno": "docker compose run --build --rm test-deno deno run -A npm:vitest run test/integration --no-file-parallelism",
|
|
83
84
|
"test:platforms": "bats --jobs 3 test/platform/runtimes.bats",
|
|
84
85
|
"benchmark": "vitest bench test/benchmark",
|
|
85
86
|
"benchmark:local": "vitest bench test/benchmark",
|
|
@@ -4,6 +4,7 @@ const ErrorCodes = Object.freeze({
|
|
|
4
4
|
DEK_UNWRAP_FAILED: 'DEK_UNWRAP_FAILED',
|
|
5
5
|
ENCRYPTION_BUFFER_EXCEEDED: 'ENCRYPTION_BUFFER_EXCEEDED',
|
|
6
6
|
GIT_ERROR: 'GIT_ERROR',
|
|
7
|
+
GIT_OBJECT_NOT_FOUND: 'GIT_OBJECT_NOT_FOUND',
|
|
7
8
|
GIT_PLUMBING_INITIALIZATION_FAILED: 'GIT_PLUMBING_INITIALIZATION_FAILED',
|
|
8
9
|
GIT_REF_NOT_FOUND: 'GIT_REF_NOT_FOUND',
|
|
9
10
|
INTEGRITY_ERROR: 'INTEGRITY_ERROR',
|
|
@@ -27,6 +28,18 @@ const ErrorCodes = Object.freeze({
|
|
|
27
28
|
RECIPIENT_ALREADY_EXISTS: 'RECIPIENT_ALREADY_EXISTS',
|
|
28
29
|
RECIPIENT_NOT_FOUND: 'RECIPIENT_NOT_FOUND',
|
|
29
30
|
RESTORE_TOO_LARGE: 'RESTORE_TOO_LARGE',
|
|
31
|
+
ROOT_SET_CONFLICT: 'ROOT_SET_CONFLICT',
|
|
32
|
+
ROOT_SET_DEPENDENCY_INVALID: 'ROOT_SET_DEPENDENCY_INVALID',
|
|
33
|
+
ROOT_SET_ENTRY_INVALID: 'ROOT_SET_ENTRY_INVALID',
|
|
34
|
+
ROOT_SET_HEAD_INVALID: 'ROOT_SET_HEAD_INVALID',
|
|
35
|
+
ROOT_SET_METADATA_INVALID: 'ROOT_SET_METADATA_INVALID',
|
|
36
|
+
ROOT_SET_REF_INVALID: 'ROOT_SET_REF_INVALID',
|
|
37
|
+
ROOT_SET_REF_UPDATE_FAILED: 'ROOT_SET_REF_UPDATE_FAILED',
|
|
38
|
+
ROOT_SET_RETRY_POLICY_INVALID: 'ROOT_SET_RETRY_POLICY_INVALID',
|
|
39
|
+
ROOT_SET_TARGET_MISSING: 'ROOT_SET_TARGET_MISSING',
|
|
40
|
+
ROOT_SET_TARGET_TYPE_MISMATCH: 'ROOT_SET_TARGET_TYPE_MISMATCH',
|
|
41
|
+
ROOT_SET_TARGET_UNREADABLE: 'ROOT_SET_TARGET_UNREADABLE',
|
|
42
|
+
ROOT_SET_TREE_INVALID: 'ROOT_SET_TREE_INVALID',
|
|
30
43
|
ROTATION_NOT_SUPPORTED: 'ROTATION_NOT_SUPPORTED',
|
|
31
44
|
SECURITY_BOUNDARY_VIOLATION: 'SECURITY_BOUNDARY_VIOLATION',
|
|
32
45
|
STORE_ERROR: 'STORE_ERROR',
|
|
@@ -3,8 +3,13 @@
|
|
|
3
3
|
* Domain service for Content Addressable Storage operations.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import Manifest, {
|
|
7
|
-
|
|
6
|
+
import Manifest, {
|
|
7
|
+
type EncryptionMeta,
|
|
8
|
+
type CompressionMeta,
|
|
9
|
+
type KdfParams,
|
|
10
|
+
type EncryptionScheme,
|
|
11
|
+
} from '../value-objects/Manifest.js';
|
|
12
|
+
import Chunk from '../value-objects/Chunk.js';
|
|
8
13
|
|
|
9
14
|
/** Port interface for cryptographic operations (hashing, encryption, random bytes). */
|
|
10
15
|
export interface CryptoPort {
|
|
@@ -13,27 +18,39 @@ export interface CryptoPort {
|
|
|
13
18
|
encryptBuffer(
|
|
14
19
|
buffer: Uint8Array,
|
|
15
20
|
key: Uint8Array,
|
|
16
|
-
aad?: Uint8Array
|
|
21
|
+
aad?: Uint8Array
|
|
17
22
|
): { buf: Uint8Array; meta: EncryptionMeta } | Promise<{ buf: Uint8Array; meta: EncryptionMeta }>;
|
|
18
|
-
decryptBuffer(
|
|
19
|
-
|
|
23
|
+
decryptBuffer(
|
|
24
|
+
buffer: Uint8Array,
|
|
25
|
+
key: Uint8Array,
|
|
26
|
+
meta: EncryptionMeta,
|
|
27
|
+
aad?: Uint8Array
|
|
28
|
+
): Uint8Array | Promise<Uint8Array>;
|
|
29
|
+
createEncryptionStream(
|
|
30
|
+
key: Uint8Array,
|
|
31
|
+
aad?: Uint8Array
|
|
32
|
+
): {
|
|
20
33
|
encrypt: (source: AsyncIterable<Uint8Array>) => AsyncIterable<Uint8Array>;
|
|
21
34
|
finalize: () => EncryptionMeta;
|
|
22
35
|
};
|
|
23
|
-
createDecryptionStream(
|
|
36
|
+
createDecryptionStream(
|
|
37
|
+
key: Uint8Array,
|
|
38
|
+
meta: EncryptionMeta,
|
|
39
|
+
aad?: Uint8Array
|
|
40
|
+
): {
|
|
24
41
|
decrypt: (source: AsyncIterable<Uint8Array>) => AsyncIterable<Uint8Array>;
|
|
25
42
|
};
|
|
26
43
|
hmacSha256(key: Uint8Array, data: Uint8Array): Uint8Array | Promise<Uint8Array>;
|
|
27
44
|
encryptBufferWithNonce(
|
|
28
45
|
buffer: Uint8Array,
|
|
29
46
|
key: Uint8Array,
|
|
30
|
-
nonce: Uint8Array
|
|
47
|
+
nonce: Uint8Array
|
|
31
48
|
): { buf: Uint8Array; tag: Uint8Array } | Promise<{ buf: Uint8Array; tag: Uint8Array }>;
|
|
32
49
|
decryptBufferWithNonceTag(
|
|
33
50
|
buffer: Uint8Array,
|
|
34
51
|
key: Uint8Array,
|
|
35
52
|
nonce: Uint8Array,
|
|
36
|
-
tag: Uint8Array
|
|
53
|
+
tag: Uint8Array
|
|
37
54
|
): Uint8Array | Promise<Uint8Array>;
|
|
38
55
|
deriveKey(options: DeriveKeyOptions): Promise<DeriveKeyResult>;
|
|
39
56
|
}
|
|
@@ -52,22 +69,27 @@ export interface GitPersistencePort {
|
|
|
52
69
|
readBlob(oid: string): Promise<Uint8Array>;
|
|
53
70
|
readBlobStream(oid: string): Promise<AsyncIterable<Uint8Array>>;
|
|
54
71
|
readTree(
|
|
55
|
-
treeOid: string
|
|
72
|
+
treeOid: string
|
|
56
73
|
): Promise<Array<{ mode: string; type: string; oid: string; name: string }>>;
|
|
57
74
|
readTreeEntry(
|
|
58
75
|
treeOid: string,
|
|
59
|
-
treePath: string
|
|
76
|
+
treePath: string
|
|
60
77
|
): Promise<{ mode: string; type: string; oid: string; name: string } | null>;
|
|
61
78
|
iterateTree(
|
|
62
|
-
treeOid: string
|
|
79
|
+
treeOid: string
|
|
63
80
|
): AsyncIterable<{ mode: string; type: string; oid: string; name: string }>;
|
|
81
|
+
readObjectType(oid: string): Promise<string>;
|
|
64
82
|
setMaxBlobSize?(maxBlobSize: number): void;
|
|
65
83
|
}
|
|
66
84
|
|
|
67
85
|
/** Port interface for observability (metrics, logging, tracing). */
|
|
68
86
|
export interface ObservabilityPort {
|
|
69
87
|
metric(channel: string, data: Record<string, unknown>): void;
|
|
70
|
-
log(
|
|
88
|
+
log(
|
|
89
|
+
level: 'debug' | 'info' | 'warn' | 'error',
|
|
90
|
+
msg: string,
|
|
91
|
+
meta?: Record<string, unknown>
|
|
92
|
+
): void;
|
|
71
93
|
span(name: string): { end(meta?: Record<string, unknown>): void };
|
|
72
94
|
}
|
|
73
95
|
|
|
@@ -108,7 +130,7 @@ export interface CasServiceOptions {
|
|
|
108
130
|
export interface DeriveKeyOptions {
|
|
109
131
|
passphrase: string;
|
|
110
132
|
salt?: Uint8Array;
|
|
111
|
-
algorithm?:
|
|
133
|
+
algorithm?: 'pbkdf2' | 'scrypt';
|
|
112
134
|
iterations?: number;
|
|
113
135
|
cost?: number;
|
|
114
136
|
blockSize?: number;
|
|
@@ -136,7 +158,7 @@ export interface StoreEncryptionOptions {
|
|
|
136
158
|
}
|
|
137
159
|
|
|
138
160
|
export interface FileRestorePlan {
|
|
139
|
-
mode:
|
|
161
|
+
mode: 'stream' | 'bounded-file';
|
|
140
162
|
source: AsyncIterable<Uint8Array>;
|
|
141
163
|
encryptionMeta?: EncryptionMeta;
|
|
142
164
|
}
|
|
@@ -178,10 +200,11 @@ export default class CasService {
|
|
|
178
200
|
encryptionKey?: Uint8Array;
|
|
179
201
|
passphrase?: string;
|
|
180
202
|
encryption?: StoreEncryptionOptions;
|
|
181
|
-
kdfOptions?: Omit<DeriveKeyOptions,
|
|
182
|
-
compression?: { algorithm:
|
|
203
|
+
kdfOptions?: Omit<DeriveKeyOptions, 'passphrase'>;
|
|
204
|
+
compression?: { algorithm: 'gzip' };
|
|
183
205
|
recipients?: Array<{ label: string; key: Uint8Array }>;
|
|
184
206
|
merkleThreshold?: number;
|
|
207
|
+
chunker?: ChunkingPort;
|
|
185
208
|
}): Promise<Manifest>;
|
|
186
209
|
|
|
187
210
|
createTree(options: { manifest: Manifest; merkleThreshold?: number }): Promise<string>;
|
|
@@ -209,14 +232,10 @@ export default class CasService {
|
|
|
209
232
|
/** Reads a raw manifest without scheme assertion or Manifest construction. */
|
|
210
233
|
readManifestRaw(options: { treeOid: string }): Promise<Record<string, unknown>>;
|
|
211
234
|
|
|
212
|
-
inspectAsset(options: {
|
|
213
|
-
treeOid: string;
|
|
214
|
-
}): Promise<{ slug: string; chunksOrphaned: number }>;
|
|
235
|
+
inspectAsset(options: { treeOid: string }): Promise<{ slug: string; chunksOrphaned: number }>;
|
|
215
236
|
|
|
216
237
|
/** @deprecated Use {@link inspectAsset} instead. */
|
|
217
|
-
deleteAsset(options: {
|
|
218
|
-
treeOid: string;
|
|
219
|
-
}): Promise<{ slug: string; chunksOrphaned: number }>;
|
|
238
|
+
deleteAsset(options: { treeOid: string }): Promise<{ slug: string; chunksOrphaned: number }>;
|
|
220
239
|
|
|
221
240
|
collectReferencedChunks(options: {
|
|
222
241
|
treeOids: string[];
|
|
@@ -234,10 +253,7 @@ export default class CasService {
|
|
|
234
253
|
label: string;
|
|
235
254
|
}): Promise<Manifest>;
|
|
236
255
|
|
|
237
|
-
removeRecipient(options: {
|
|
238
|
-
manifest: Manifest;
|
|
239
|
-
label: string;
|
|
240
|
-
}): Promise<Manifest>;
|
|
256
|
+
removeRecipient(options: { manifest: Manifest; label: string }): Promise<Manifest>;
|
|
241
257
|
|
|
242
258
|
listRecipients(manifest: Manifest): string[];
|
|
243
259
|
|
|
@@ -149,12 +149,12 @@ export default class CasService {
|
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
#buildStoreStrategies({ crypto }) {
|
|
152
|
+
#buildStoreStrategies({ crypto, chunks = this.#chunkRepository }) {
|
|
153
153
|
return Object.freeze({
|
|
154
|
-
plain: new StorePlain(
|
|
155
|
-
convergent: new StoreConvergent(
|
|
156
|
-
framed: new StoreFramed({ chunks
|
|
157
|
-
whole: new StoreWhole({ chunks
|
|
154
|
+
plain: new StorePlain(chunks),
|
|
155
|
+
convergent: new StoreConvergent(chunks),
|
|
156
|
+
framed: new StoreFramed({ chunks, framed: this.#framed }),
|
|
157
|
+
whole: new StoreWhole({ chunks, crypto }),
|
|
158
158
|
});
|
|
159
159
|
}
|
|
160
160
|
|
|
@@ -243,14 +243,27 @@ export default class CasService {
|
|
|
243
243
|
return;
|
|
244
244
|
}
|
|
245
245
|
if (!['fixed', 'cdc'].includes(chunking.strategy)) {
|
|
246
|
-
throw createCasError(
|
|
247
|
-
`Unsupported chunking strategy: ${chunking.strategy}`,
|
|
248
|
-
ErrorCodes.INVALID_CHUNKING_STRATEGY,
|
|
249
|
-
{ strategy: chunking.strategy },
|
|
250
|
-
);
|
|
246
|
+
throw createCasError(`Unsupported chunking strategy: ${chunking.strategy}`, ErrorCodes.INVALID_CHUNKING_STRATEGY, { strategy: chunking.strategy });
|
|
251
247
|
}
|
|
252
248
|
}
|
|
253
249
|
|
|
250
|
+
#validateOperationChunker(chunker) {
|
|
251
|
+
if (!chunker) {
|
|
252
|
+
return this.chunker;
|
|
253
|
+
}
|
|
254
|
+
if (typeof chunker.chunk !== 'function' || typeof chunker.strategy !== 'string') {
|
|
255
|
+
throw createCasError('chunker must implement ChunkingPort', ErrorCodes.INVALID_OPTIONS, { strategy: chunker.strategy });
|
|
256
|
+
}
|
|
257
|
+
return chunker;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
#storeStrategiesFor(chunker) {
|
|
261
|
+
if (chunker === this.chunker) {
|
|
262
|
+
return this.#storeStrategies;
|
|
263
|
+
}
|
|
264
|
+
return this.#buildStoreStrategies({ crypto: this.crypto, chunks: this.#chunkRepository.withChunker(chunker) });
|
|
265
|
+
}
|
|
266
|
+
|
|
254
267
|
/**
|
|
255
268
|
* @param {Object} options
|
|
256
269
|
* @param {AsyncIterable<Uint8Array>} options.source
|
|
@@ -263,20 +276,21 @@ export default class CasService {
|
|
|
263
276
|
* @param {{ algorithm: 'gzip' }} [options.compression]
|
|
264
277
|
* @param {Array<{label: string, key: Uint8Array}>} [options.recipients]
|
|
265
278
|
* @param {number} [options.merkleThreshold]
|
|
279
|
+
* @param {import('../../ports/ChunkingPort.js').default} [options.chunker]
|
|
266
280
|
* @returns {Promise<Manifest>}
|
|
267
281
|
*/
|
|
268
|
-
async store({
|
|
269
|
-
source,
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}) {
|
|
282
|
+
async store(options) {
|
|
283
|
+
const { source, slug, filename, compression, merkleThreshold } = options;
|
|
284
|
+
this.#validateStoreOptions(options);
|
|
285
|
+
const plan = await this.#buildStorePlan(options);
|
|
286
|
+
const manifestData = this._buildManifestData({ slug, filename, compression, chunker: plan.chunker });
|
|
287
|
+
const processedSource = compression ? this.#compression.compress(source) : source;
|
|
288
|
+
|
|
289
|
+
await this._dispatchStore({ processedSource, manifestData, keyInfo: plan.keyInfo, encryptionConfig: plan.encryptionConfig, chunker: plan.chunker });
|
|
290
|
+
return this.#finalizeStore({ manifestData, merkleThreshold, keyInfo: plan.keyInfo, slug });
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
#validateStoreOptions({ source, recipients, encryptionKey, passphrase, compression, merkleThreshold }) {
|
|
280
294
|
if (!source || typeof source[Symbol.asyncIterator] !== 'function') {
|
|
281
295
|
throw createCasError('source must be an async iterable', ErrorCodes.INVALID_OPTIONS, { sourceType: typeof source });
|
|
282
296
|
}
|
|
@@ -286,15 +300,18 @@ export default class CasService {
|
|
|
286
300
|
KeyResolver.validateKeySourceExclusive(encryptionKey, passphrase);
|
|
287
301
|
this._validateCompression(compression);
|
|
288
302
|
CasService.#validateMerkleThreshold(merkleThreshold);
|
|
303
|
+
}
|
|
289
304
|
|
|
305
|
+
async #buildStorePlan({ encryptionKey, passphrase, encryption, kdfOptions, recipients, chunker }) {
|
|
306
|
+
const operationChunker = this.#validateOperationChunker(chunker);
|
|
290
307
|
const keyInfo = recipients
|
|
291
308
|
? await this.#keyResolver.resolveRecipients(recipients)
|
|
292
309
|
: await this.#keyResolver.resolveForStore(encryptionKey, passphrase, kdfOptions);
|
|
293
|
-
const encryptionConfig = this.
|
|
294
|
-
|
|
295
|
-
|
|
310
|
+
const encryptionConfig = this._resolveStoreEncryptionConfigForChunker({ encryption, hasEncryptionKey: !!keyInfo.key, chunker: operationChunker });
|
|
311
|
+
return { chunker: operationChunker, keyInfo, encryptionConfig };
|
|
312
|
+
}
|
|
296
313
|
|
|
297
|
-
|
|
314
|
+
#finalizeStore({ manifestData, merkleThreshold, keyInfo, slug }) {
|
|
298
315
|
const manifest = new Manifest(manifestData);
|
|
299
316
|
this.#rememberMerkleThreshold(manifest, merkleThreshold);
|
|
300
317
|
this.observability.metric('file', {
|
|
@@ -307,6 +324,10 @@ export default class CasService {
|
|
|
307
324
|
return new StoreSuccess({ manifest }).manifest;
|
|
308
325
|
}
|
|
309
326
|
|
|
327
|
+
_resolveStoreEncryptionConfigForChunker({ encryption, hasEncryptionKey, chunker }) {
|
|
328
|
+
return StoreEncryptionConfig.resolve({ encryption, hasEncryptionKey, chunker, observability: this.observability });
|
|
329
|
+
}
|
|
330
|
+
|
|
310
331
|
/**
|
|
311
332
|
* @param {Manifest} manifest
|
|
312
333
|
* @param {number|undefined} merkleThreshold
|
|
@@ -317,24 +338,24 @@ export default class CasService {
|
|
|
317
338
|
}
|
|
318
339
|
}
|
|
319
340
|
|
|
320
|
-
async _dispatchStore({ processedSource, manifestData, keyInfo, encryptionConfig }) {
|
|
341
|
+
async _dispatchStore({ processedSource, manifestData, keyInfo, encryptionConfig, chunker = this.chunker }) {
|
|
321
342
|
const strategy = StoreStrategy.for({
|
|
322
343
|
keyInfo,
|
|
323
344
|
encryptionConfig,
|
|
324
|
-
chunker
|
|
345
|
+
chunker,
|
|
325
346
|
observability: this.observability,
|
|
326
|
-
strategies: this.#
|
|
347
|
+
strategies: this.#storeStrategiesFor(chunker),
|
|
327
348
|
});
|
|
328
349
|
await strategy.execute({ processedSource, manifestData, keyInfo, encryptionConfig });
|
|
329
350
|
}
|
|
330
351
|
|
|
331
|
-
_buildManifestData(slug, filename, compression) {
|
|
352
|
+
_buildManifestData({ slug, filename, compression, chunker = this.chunker }) {
|
|
332
353
|
const data = { slug, filename, size: 0, chunks: [] };
|
|
333
354
|
if (this.formatVersion) {
|
|
334
355
|
data.formatVersion = this.formatVersion;
|
|
335
356
|
}
|
|
336
|
-
if (
|
|
337
|
-
data.chunking = { strategy:
|
|
357
|
+
if (chunker.strategy !== 'fixed') {
|
|
358
|
+
data.chunking = { strategy: chunker.strategy, params: chunker.params };
|
|
338
359
|
}
|
|
339
360
|
if (compression) {
|
|
340
361
|
data.compression = { algorithm: 'gzip' };
|