@git-stunts/git-cas 1.6.2 → 3.0.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/CHANGELOG.md +75 -1
- package/README.md +56 -8
- package/bin/git-cas.js +205 -38
- package/index.d.ts +149 -4
- package/index.js +112 -12
- package/package.json +1 -1
- package/src/domain/schemas/ManifestSchema.d.ts +27 -0
- package/src/domain/schemas/ManifestSchema.js +27 -0
- package/src/domain/services/CasService.d.ts +29 -1
- package/src/domain/services/CasService.js +253 -30
- package/src/domain/services/VaultService.js +445 -0
- package/src/domain/value-objects/Manifest.d.ts +30 -0
- package/src/domain/value-objects/Manifest.js +6 -0
- package/src/infrastructure/adapters/BunCryptoAdapter.js +35 -2
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +11 -11
- package/src/infrastructure/adapters/GitRefAdapter.js +67 -0
- package/src/infrastructure/adapters/NodeCryptoAdapter.js +40 -1
- package/src/infrastructure/adapters/WebCryptoAdapter.js +58 -0
- package/src/ports/CryptoPort.js +17 -0
- package/src/ports/GitRefPort.js +48 -0
package/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import Manifest from "./src/domain/value-objects/Manifest.js";
|
|
7
|
-
import type { EncryptionMeta, ManifestData } from "./src/domain/value-objects/Manifest.js";
|
|
7
|
+
import type { EncryptionMeta, ManifestData, CompressionMeta, KdfParams, SubManifestRef } from "./src/domain/value-objects/Manifest.js";
|
|
8
8
|
import Chunk from "./src/domain/value-objects/Chunk.js";
|
|
9
9
|
import CasService from "./src/domain/services/CasService.js";
|
|
10
10
|
import type {
|
|
@@ -12,10 +12,12 @@ import type {
|
|
|
12
12
|
CodecPort,
|
|
13
13
|
GitPersistencePort,
|
|
14
14
|
CasServiceOptions,
|
|
15
|
+
DeriveKeyOptions,
|
|
16
|
+
DeriveKeyResult,
|
|
15
17
|
} from "./src/domain/services/CasService.js";
|
|
16
18
|
|
|
17
19
|
export { CasService, Manifest, Chunk };
|
|
18
|
-
export type { EncryptionMeta, ManifestData, CryptoPort, CodecPort, GitPersistencePort, CasServiceOptions };
|
|
20
|
+
export type { EncryptionMeta, ManifestData, CompressionMeta, KdfParams, SubManifestRef, CryptoPort, CodecPort, GitPersistencePort, CasServiceOptions, DeriveKeyOptions, DeriveKeyResult };
|
|
19
21
|
|
|
20
22
|
/** Abstract port for cryptographic operations. */
|
|
21
23
|
export declare class CryptoPortBase {
|
|
@@ -30,6 +32,7 @@ export declare class CryptoPortBase {
|
|
|
30
32
|
encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>;
|
|
31
33
|
finalize: () => EncryptionMeta;
|
|
32
34
|
};
|
|
35
|
+
deriveKey(options: DeriveKeyOptions): Promise<DeriveKeyResult>;
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
/** Abstract port for persisting data to Git's object database. */
|
|
@@ -42,11 +45,32 @@ export declare class GitPersistencePortBase {
|
|
|
42
45
|
): Promise<Array<{ mode: string; type: string; oid: string; name: string }>>;
|
|
43
46
|
}
|
|
44
47
|
|
|
48
|
+
/** Abstract port for Git ref and commit operations. */
|
|
49
|
+
export declare class GitRefPortBase {
|
|
50
|
+
resolveRef(ref: string): Promise<string>;
|
|
51
|
+
resolveTree(commitOid: string): Promise<string>;
|
|
52
|
+
createCommit(options: {
|
|
53
|
+
treeOid: string;
|
|
54
|
+
parentOid?: string | null;
|
|
55
|
+
message: string;
|
|
56
|
+
}): Promise<string>;
|
|
57
|
+
updateRef(options: {
|
|
58
|
+
ref: string;
|
|
59
|
+
newOid: string;
|
|
60
|
+
expectedOldOid?: string | null;
|
|
61
|
+
}): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
|
|
45
64
|
/** Git-backed implementation of the persistence port. */
|
|
46
65
|
export declare class GitPersistenceAdapter extends GitPersistencePortBase {
|
|
47
66
|
constructor(options: { plumbing: unknown; policy?: unknown });
|
|
48
67
|
}
|
|
49
68
|
|
|
69
|
+
/** Git-backed implementation of the ref port. */
|
|
70
|
+
export declare class GitRefAdapter extends GitRefPortBase {
|
|
71
|
+
constructor(options: { plumbing: unknown; policy?: unknown });
|
|
72
|
+
}
|
|
73
|
+
|
|
50
74
|
/** Node.js crypto implementation of CryptoPort. */
|
|
51
75
|
export declare class NodeCryptoAdapter extends CryptoPortBase {
|
|
52
76
|
constructor();
|
|
@@ -76,13 +100,98 @@ export interface ContentAddressableStoreOptions {
|
|
|
76
100
|
codec?: CodecPort;
|
|
77
101
|
crypto?: CryptoPort;
|
|
78
102
|
policy?: unknown;
|
|
103
|
+
merkleThreshold?: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A single vault entry. */
|
|
107
|
+
export interface VaultEntry {
|
|
108
|
+
slug: string;
|
|
109
|
+
treeOid: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Vault metadata stored in .vault.json. */
|
|
113
|
+
export interface VaultMetadata {
|
|
114
|
+
version: number;
|
|
115
|
+
encryption?: {
|
|
116
|
+
cipher: string;
|
|
117
|
+
kdf: {
|
|
118
|
+
algorithm: string;
|
|
119
|
+
salt: string;
|
|
120
|
+
iterations?: number;
|
|
121
|
+
cost?: number;
|
|
122
|
+
blockSize?: number;
|
|
123
|
+
parallelization?: number;
|
|
124
|
+
keyLength: number;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Internal vault state returned by VaultService.readState(). */
|
|
130
|
+
export interface VaultState {
|
|
131
|
+
entries: Map<string, string>;
|
|
132
|
+
parentCommitOid: string | null;
|
|
133
|
+
metadata: VaultMetadata | null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Domain service for vault (GC-safe ref-based asset index) operations.
|
|
138
|
+
*/
|
|
139
|
+
export declare class VaultService {
|
|
140
|
+
static VAULT_REF: string;
|
|
141
|
+
|
|
142
|
+
constructor(options: {
|
|
143
|
+
persistence: GitPersistencePortBase;
|
|
144
|
+
ref: GitRefPortBase;
|
|
145
|
+
crypto: CryptoPortBase;
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
/** Validates a vault slug. Throws CasError with code INVALID_SLUG on failure. */
|
|
149
|
+
validateSlug(slug: string): void;
|
|
150
|
+
|
|
151
|
+
/** Reads the current vault state from refs/cas/vault. */
|
|
152
|
+
readState(): Promise<VaultState>;
|
|
153
|
+
|
|
154
|
+
/** Writes a new vault commit and updates the ref atomically. */
|
|
155
|
+
writeCommit(options: {
|
|
156
|
+
entries: Map<string, string>;
|
|
157
|
+
metadata: VaultMetadata;
|
|
158
|
+
parentCommitOid: string | null;
|
|
159
|
+
message: string;
|
|
160
|
+
}): Promise<{ commitOid: string }>;
|
|
161
|
+
|
|
162
|
+
/** Initializes the vault, optionally with encryption. */
|
|
163
|
+
initVault(options?: {
|
|
164
|
+
passphrase?: string;
|
|
165
|
+
kdfOptions?: Omit<DeriveKeyOptions, "passphrase">;
|
|
166
|
+
}): Promise<{ commitOid: string }>;
|
|
167
|
+
|
|
168
|
+
/** Adds or updates an entry in the vault. */
|
|
169
|
+
addToVault(options: {
|
|
170
|
+
slug: string;
|
|
171
|
+
treeOid: string;
|
|
172
|
+
force?: boolean;
|
|
173
|
+
}): Promise<{ commitOid: string }>;
|
|
174
|
+
|
|
175
|
+
/** Lists all vault entries sorted by slug. */
|
|
176
|
+
listVault(): Promise<VaultEntry[]>;
|
|
177
|
+
|
|
178
|
+
/** Removes an entry from the vault. */
|
|
179
|
+
removeFromVault(options: {
|
|
180
|
+
slug: string;
|
|
181
|
+
}): Promise<{ commitOid: string; removedTreeOid: string }>;
|
|
182
|
+
|
|
183
|
+
/** Resolves a vault entry slug to its tree OID. */
|
|
184
|
+
resolveVaultEntry(options: { slug: string }): Promise<string>;
|
|
185
|
+
|
|
186
|
+
/** Returns the vault metadata, or null if no vault exists. */
|
|
187
|
+
getVaultMetadata(): Promise<VaultMetadata | null>;
|
|
79
188
|
}
|
|
80
189
|
|
|
81
190
|
/**
|
|
82
191
|
* High-level facade for the Content Addressable Store library.
|
|
83
192
|
*
|
|
84
|
-
* Wraps CasService with lazy initialization, runtime-adaptive
|
|
85
|
-
* selection, and convenience helpers for file I/O.
|
|
193
|
+
* Wraps CasService and VaultService with lazy initialization, runtime-adaptive
|
|
194
|
+
* crypto selection, and convenience helpers for file I/O.
|
|
86
195
|
*/
|
|
87
196
|
export default class ContentAddressableStore {
|
|
88
197
|
constructor(options: ContentAddressableStoreOptions);
|
|
@@ -90,6 +199,7 @@ export default class ContentAddressableStore {
|
|
|
90
199
|
get chunkSize(): number;
|
|
91
200
|
|
|
92
201
|
getService(): Promise<CasService>;
|
|
202
|
+
getVaultService(): Promise<VaultService>;
|
|
93
203
|
|
|
94
204
|
static createJson(options: {
|
|
95
205
|
plumbing: unknown;
|
|
@@ -119,6 +229,9 @@ export default class ContentAddressableStore {
|
|
|
119
229
|
slug: string;
|
|
120
230
|
filename?: string;
|
|
121
231
|
encryptionKey?: Buffer;
|
|
232
|
+
passphrase?: string;
|
|
233
|
+
kdfOptions?: Omit<DeriveKeyOptions, "passphrase">;
|
|
234
|
+
compression?: { algorithm: "gzip" };
|
|
122
235
|
}): Promise<Manifest>;
|
|
123
236
|
|
|
124
237
|
store(options: {
|
|
@@ -126,17 +239,22 @@ export default class ContentAddressableStore {
|
|
|
126
239
|
slug: string;
|
|
127
240
|
filename: string;
|
|
128
241
|
encryptionKey?: Buffer;
|
|
242
|
+
passphrase?: string;
|
|
243
|
+
kdfOptions?: Omit<DeriveKeyOptions, "passphrase">;
|
|
244
|
+
compression?: { algorithm: "gzip" };
|
|
129
245
|
}): Promise<Manifest>;
|
|
130
246
|
|
|
131
247
|
restoreFile(options: {
|
|
132
248
|
manifest: Manifest;
|
|
133
249
|
encryptionKey?: Buffer;
|
|
250
|
+
passphrase?: string;
|
|
134
251
|
outputPath: string;
|
|
135
252
|
}): Promise<{ bytesWritten: number }>;
|
|
136
253
|
|
|
137
254
|
restore(options: {
|
|
138
255
|
manifest: Manifest;
|
|
139
256
|
encryptionKey?: Buffer;
|
|
257
|
+
passphrase?: string;
|
|
140
258
|
}): Promise<{ buffer: Buffer; bytesWritten: number }>;
|
|
141
259
|
|
|
142
260
|
createTree(options: { manifest: Manifest }): Promise<string>;
|
|
@@ -152,4 +270,31 @@ export default class ContentAddressableStore {
|
|
|
152
270
|
findOrphanedChunks(options: {
|
|
153
271
|
treeOids: string[];
|
|
154
272
|
}): Promise<{ referenced: Set<string>; total: number }>;
|
|
273
|
+
|
|
274
|
+
deriveKey(options: DeriveKeyOptions): Promise<DeriveKeyResult>;
|
|
275
|
+
|
|
276
|
+
// Vault — delegates to VaultService
|
|
277
|
+
|
|
278
|
+
static VAULT_REF: string;
|
|
279
|
+
|
|
280
|
+
initVault(options?: {
|
|
281
|
+
passphrase?: string;
|
|
282
|
+
kdfOptions?: Omit<DeriveKeyOptions, "passphrase">;
|
|
283
|
+
}): Promise<{ commitOid: string }>;
|
|
284
|
+
|
|
285
|
+
addToVault(options: {
|
|
286
|
+
slug: string;
|
|
287
|
+
treeOid: string;
|
|
288
|
+
force?: boolean;
|
|
289
|
+
}): Promise<{ commitOid: string }>;
|
|
290
|
+
|
|
291
|
+
listVault(): Promise<VaultEntry[]>;
|
|
292
|
+
|
|
293
|
+
removeFromVault(options: {
|
|
294
|
+
slug: string;
|
|
295
|
+
}): Promise<{ commitOid: string; removedTreeOid: string }>;
|
|
296
|
+
|
|
297
|
+
resolveVaultEntry(options: { slug: string }): Promise<string>;
|
|
298
|
+
|
|
299
|
+
getVaultMetadata(): Promise<VaultMetadata | null>;
|
|
155
300
|
}
|
package/index.js
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
import { createReadStream, writeFileSync } from 'node:fs';
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import CasService from './src/domain/services/CasService.js';
|
|
9
|
+
import VaultService from './src/domain/services/VaultService.js';
|
|
9
10
|
import GitPersistenceAdapter from './src/infrastructure/adapters/GitPersistenceAdapter.js';
|
|
11
|
+
import GitRefAdapter from './src/infrastructure/adapters/GitRefAdapter.js';
|
|
10
12
|
import NodeCryptoAdapter from './src/infrastructure/adapters/NodeCryptoAdapter.js';
|
|
11
13
|
import Manifest from './src/domain/value-objects/Manifest.js';
|
|
12
14
|
import Chunk from './src/domain/value-objects/Chunk.js';
|
|
@@ -16,7 +18,9 @@ import CborCodec from './src/infrastructure/codecs/CborCodec.js';
|
|
|
16
18
|
|
|
17
19
|
export {
|
|
18
20
|
CasService,
|
|
21
|
+
VaultService,
|
|
19
22
|
GitPersistenceAdapter,
|
|
23
|
+
GitRefAdapter,
|
|
20
24
|
NodeCryptoAdapter,
|
|
21
25
|
CryptoPort,
|
|
22
26
|
Manifest,
|
|
@@ -44,8 +48,8 @@ async function getDefaultCryptoAdapter() {
|
|
|
44
48
|
/**
|
|
45
49
|
* High-level facade for the Content Addressable Store library.
|
|
46
50
|
*
|
|
47
|
-
* Wraps {@link CasService} with lazy initialization,
|
|
48
|
-
* selection, and convenience helpers for file I/O.
|
|
51
|
+
* Wraps {@link CasService} and {@link VaultService} with lazy initialization,
|
|
52
|
+
* runtime-adaptive crypto selection, and convenience helpers for file I/O.
|
|
49
53
|
*/
|
|
50
54
|
export default class ContentAddressableStore {
|
|
51
55
|
/**
|
|
@@ -55,17 +59,21 @@ export default class ContentAddressableStore {
|
|
|
55
59
|
* @param {import('./src/ports/CodecPort.js').default} [options.codec] - Manifest codec (default JsonCodec).
|
|
56
60
|
* @param {import('./src/ports/CryptoPort.js').default} [options.crypto] - Crypto adapter (auto-detected if omitted).
|
|
57
61
|
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy for Git I/O.
|
|
62
|
+
* @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
|
|
58
63
|
*/
|
|
59
|
-
constructor({ plumbing, chunkSize, codec, policy, crypto }) {
|
|
64
|
+
constructor({ plumbing, chunkSize, codec, policy, crypto, merkleThreshold }) {
|
|
60
65
|
this.plumbing = plumbing;
|
|
61
66
|
this.chunkSizeConfig = chunkSize;
|
|
62
67
|
this.codecConfig = codec;
|
|
63
68
|
this.policyConfig = policy;
|
|
64
69
|
this.cryptoConfig = crypto;
|
|
70
|
+
this.merkleThresholdConfig = merkleThreshold;
|
|
65
71
|
this.service = null;
|
|
66
72
|
this.#servicePromise = null;
|
|
67
73
|
}
|
|
68
74
|
|
|
75
|
+
/** @type {VaultService|null} */
|
|
76
|
+
#vault = null;
|
|
69
77
|
#servicePromise = null;
|
|
70
78
|
|
|
71
79
|
/**
|
|
@@ -81,7 +89,7 @@ export default class ContentAddressableStore {
|
|
|
81
89
|
}
|
|
82
90
|
|
|
83
91
|
/**
|
|
84
|
-
* Constructs
|
|
92
|
+
* Constructs adapters, resolves crypto, and creates CasService + VaultService.
|
|
85
93
|
* @private
|
|
86
94
|
* @returns {Promise<CasService>}
|
|
87
95
|
*/
|
|
@@ -96,10 +104,28 @@ export default class ContentAddressableStore {
|
|
|
96
104
|
chunkSize: this.chunkSizeConfig,
|
|
97
105
|
codec: this.codecConfig || new JsonCodec(),
|
|
98
106
|
crypto,
|
|
107
|
+
merkleThreshold: this.merkleThresholdConfig,
|
|
99
108
|
});
|
|
109
|
+
|
|
110
|
+
const ref = new GitRefAdapter({
|
|
111
|
+
plumbing: this.plumbing,
|
|
112
|
+
policy: this.policyConfig,
|
|
113
|
+
});
|
|
114
|
+
this.#vault = new VaultService({ persistence, ref, crypto });
|
|
115
|
+
|
|
100
116
|
return this.service;
|
|
101
117
|
}
|
|
102
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Lazily initializes and returns the underlying {@link VaultService}.
|
|
121
|
+
* @private
|
|
122
|
+
* @returns {Promise<VaultService>}
|
|
123
|
+
*/
|
|
124
|
+
async #getVault() {
|
|
125
|
+
await this.#getService();
|
|
126
|
+
return this.#vault;
|
|
127
|
+
}
|
|
128
|
+
|
|
103
129
|
/**
|
|
104
130
|
* Lazily initializes and returns the underlying {@link CasService}.
|
|
105
131
|
* @returns {Promise<CasService>}
|
|
@@ -108,6 +134,14 @@ export default class ContentAddressableStore {
|
|
|
108
134
|
return await this.#getService();
|
|
109
135
|
}
|
|
110
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Lazily initializes and returns the underlying {@link VaultService}.
|
|
139
|
+
* @returns {Promise<VaultService>}
|
|
140
|
+
*/
|
|
141
|
+
async getVaultService() {
|
|
142
|
+
return await this.#getVault();
|
|
143
|
+
}
|
|
144
|
+
|
|
111
145
|
/**
|
|
112
146
|
* Factory to create a CAS with JSON codec.
|
|
113
147
|
* @param {Object} options
|
|
@@ -167,18 +201,17 @@ export default class ContentAddressableStore {
|
|
|
167
201
|
|
|
168
202
|
/**
|
|
169
203
|
* Reads a file from disk and stores it in Git as chunked blobs.
|
|
170
|
-
*
|
|
171
|
-
* Convenience wrapper that opens a read stream and delegates to
|
|
172
|
-
* {@link CasService#store}.
|
|
173
|
-
*
|
|
174
204
|
* @param {Object} options
|
|
175
205
|
* @param {string} options.filePath - Absolute or relative path to the file.
|
|
176
206
|
* @param {string} options.slug - Logical identifier for the stored asset.
|
|
177
207
|
* @param {string} [options.filename] - Override filename (defaults to basename of filePath).
|
|
178
208
|
* @param {Buffer} [options.encryptionKey] - 32-byte key for AES-256-GCM encryption.
|
|
209
|
+
* @param {string} [options.passphrase] - Derive encryption key from passphrase.
|
|
210
|
+
* @param {Object} [options.kdfOptions] - KDF options when using passphrase.
|
|
211
|
+
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
|
|
179
212
|
* @returns {Promise<import('./src/domain/value-objects/Manifest.js').default>} The resulting manifest.
|
|
180
213
|
*/
|
|
181
|
-
async storeFile({ filePath, slug, filename, encryptionKey }) {
|
|
214
|
+
async storeFile({ filePath, slug, filename, encryptionKey, passphrase, kdfOptions, compression }) {
|
|
182
215
|
const source = createReadStream(filePath);
|
|
183
216
|
const service = await this.#getService();
|
|
184
217
|
return await service.store({
|
|
@@ -186,6 +219,9 @@ export default class ContentAddressableStore {
|
|
|
186
219
|
slug,
|
|
187
220
|
filename: filename || path.basename(filePath),
|
|
188
221
|
encryptionKey,
|
|
222
|
+
passphrase,
|
|
223
|
+
kdfOptions,
|
|
224
|
+
compression,
|
|
189
225
|
});
|
|
190
226
|
}
|
|
191
227
|
|
|
@@ -196,6 +232,9 @@ export default class ContentAddressableStore {
|
|
|
196
232
|
* @param {string} options.slug - Logical identifier for the stored asset.
|
|
197
233
|
* @param {string} options.filename - Filename for the manifest.
|
|
198
234
|
* @param {Buffer} [options.encryptionKey] - 32-byte key for AES-256-GCM encryption.
|
|
235
|
+
* @param {string} [options.passphrase] - Derive encryption key from passphrase.
|
|
236
|
+
* @param {Object} [options.kdfOptions] - KDF options when using passphrase.
|
|
237
|
+
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
|
|
199
238
|
* @returns {Promise<import('./src/domain/value-objects/Manifest.js').default>} The resulting manifest.
|
|
200
239
|
*/
|
|
201
240
|
async store(options) {
|
|
@@ -208,14 +247,16 @@ export default class ContentAddressableStore {
|
|
|
208
247
|
* @param {Object} options
|
|
209
248
|
* @param {import('./src/domain/value-objects/Manifest.js').default} options.manifest - The file manifest.
|
|
210
249
|
* @param {Buffer} [options.encryptionKey] - 32-byte key, required if manifest is encrypted.
|
|
250
|
+
* @param {string} [options.passphrase] - Passphrase for KDF-based decryption.
|
|
211
251
|
* @param {string} options.outputPath - Destination file path.
|
|
212
252
|
* @returns {Promise<{ bytesWritten: number }>}
|
|
213
253
|
*/
|
|
214
|
-
async restoreFile({ manifest, encryptionKey, outputPath }) {
|
|
254
|
+
async restoreFile({ manifest, encryptionKey, passphrase, outputPath }) {
|
|
215
255
|
const service = await this.#getService();
|
|
216
256
|
const { buffer, bytesWritten } = await service.restore({
|
|
217
257
|
manifest,
|
|
218
258
|
encryptionKey,
|
|
259
|
+
passphrase,
|
|
219
260
|
});
|
|
220
261
|
writeFileSync(outputPath, buffer);
|
|
221
262
|
return { bytesWritten };
|
|
@@ -226,6 +267,7 @@ export default class ContentAddressableStore {
|
|
|
226
267
|
* @param {Object} options
|
|
227
268
|
* @param {import('./src/domain/value-objects/Manifest.js').default} options.manifest - The file manifest.
|
|
228
269
|
* @param {Buffer} [options.encryptionKey] - 32-byte key, required if manifest is encrypted.
|
|
270
|
+
* @param {string} [options.passphrase] - Passphrase for KDF-based decryption.
|
|
229
271
|
* @returns {Promise<{ buffer: Buffer, bytesWritten: number }>}
|
|
230
272
|
*/
|
|
231
273
|
async restore(options) {
|
|
@@ -267,7 +309,6 @@ export default class ContentAddressableStore {
|
|
|
267
309
|
|
|
268
310
|
/**
|
|
269
311
|
* Returns deletion metadata for an asset stored in a Git tree.
|
|
270
|
-
* Does not perform any destructive Git operations.
|
|
271
312
|
* @param {Object} options
|
|
272
313
|
* @param {string} options.treeOid - Git tree OID of the asset.
|
|
273
314
|
* @returns {Promise<{ slug: string, chunksOrphaned: number }>}
|
|
@@ -279,7 +320,6 @@ export default class ContentAddressableStore {
|
|
|
279
320
|
|
|
280
321
|
/**
|
|
281
322
|
* Aggregates referenced chunk blob OIDs across multiple stored assets.
|
|
282
|
-
* Analysis only — does not delete or modify anything.
|
|
283
323
|
* @param {Object} options
|
|
284
324
|
* @param {string[]} options.treeOids - Git tree OIDs to analyze.
|
|
285
325
|
* @returns {Promise<{ referenced: Set<string>, total: number }>}
|
|
@@ -288,4 +328,64 @@ export default class ContentAddressableStore {
|
|
|
288
328
|
const service = await this.#getService();
|
|
289
329
|
return await service.findOrphanedChunks(options);
|
|
290
330
|
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Derives an encryption key from a passphrase using PBKDF2 or scrypt.
|
|
334
|
+
* @param {Object} options
|
|
335
|
+
* @param {string} options.passphrase - The passphrase.
|
|
336
|
+
* @param {Buffer} [options.salt] - Salt (random if omitted).
|
|
337
|
+
* @param {'pbkdf2'|'scrypt'} [options.algorithm='pbkdf2'] - KDF algorithm.
|
|
338
|
+
* @param {number} [options.iterations] - PBKDF2 iterations.
|
|
339
|
+
* @param {number} [options.cost] - scrypt cost (N).
|
|
340
|
+
* @param {number} [options.blockSize] - scrypt block size (r).
|
|
341
|
+
* @param {number} [options.parallelization] - scrypt parallelization (p).
|
|
342
|
+
* @param {number} [options.keyLength=32] - Derived key length.
|
|
343
|
+
* @returns {Promise<{ key: Buffer, salt: Buffer, params: Object }>}
|
|
344
|
+
*/
|
|
345
|
+
async deriveKey(options) {
|
|
346
|
+
const service = await this.#getService();
|
|
347
|
+
return await service.deriveKey(options);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ---------------------------------------------------------------------------
|
|
351
|
+
// Vault — delegates to VaultService
|
|
352
|
+
// ---------------------------------------------------------------------------
|
|
353
|
+
|
|
354
|
+
static VAULT_REF = VaultService.VAULT_REF;
|
|
355
|
+
|
|
356
|
+
/** @see VaultService#initVault */
|
|
357
|
+
async initVault(options) {
|
|
358
|
+
const vault = await this.#getVault();
|
|
359
|
+
return vault.initVault(options);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** @see VaultService#addToVault */
|
|
363
|
+
async addToVault(options) {
|
|
364
|
+
const vault = await this.#getVault();
|
|
365
|
+
return vault.addToVault(options);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** @see VaultService#listVault */
|
|
369
|
+
async listVault() {
|
|
370
|
+
const vault = await this.#getVault();
|
|
371
|
+
return vault.listVault();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** @see VaultService#removeFromVault */
|
|
375
|
+
async removeFromVault(options) {
|
|
376
|
+
const vault = await this.#getVault();
|
|
377
|
+
return vault.removeFromVault(options);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** @see VaultService#resolveVaultEntry */
|
|
381
|
+
async resolveVaultEntry(options) {
|
|
382
|
+
const vault = await this.#getVault();
|
|
383
|
+
return vault.resolveVaultEntry(options);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** @see VaultService#getVaultMetadata */
|
|
387
|
+
async getVaultMetadata() {
|
|
388
|
+
const vault = await this.#getVault();
|
|
389
|
+
return vault.getVaultMetadata();
|
|
390
|
+
}
|
|
291
391
|
}
|
package/package.json
CHANGED
|
@@ -13,19 +13,46 @@ export declare const ChunkSchema: z.ZodObject<{
|
|
|
13
13
|
blob: z.ZodString;
|
|
14
14
|
}>;
|
|
15
15
|
|
|
16
|
+
/** Validates KDF parameters stored alongside encryption metadata. */
|
|
17
|
+
export declare const KdfSchema: z.ZodObject<{
|
|
18
|
+
algorithm: z.ZodEnum<["pbkdf2", "scrypt"]>;
|
|
19
|
+
salt: z.ZodString;
|
|
20
|
+
iterations: z.ZodOptional<z.ZodNumber>;
|
|
21
|
+
cost: z.ZodOptional<z.ZodNumber>;
|
|
22
|
+
blockSize: z.ZodOptional<z.ZodNumber>;
|
|
23
|
+
parallelization: z.ZodOptional<z.ZodNumber>;
|
|
24
|
+
keyLength: z.ZodDefault<z.ZodNumber>;
|
|
25
|
+
}>;
|
|
26
|
+
|
|
16
27
|
/** Validates the encryption metadata attached to an encrypted manifest. */
|
|
17
28
|
export declare const EncryptionSchema: z.ZodObject<{
|
|
18
29
|
algorithm: z.ZodString;
|
|
19
30
|
nonce: z.ZodString;
|
|
20
31
|
tag: z.ZodString;
|
|
21
32
|
encrypted: z.ZodDefault<z.ZodBoolean>;
|
|
33
|
+
kdf: z.ZodOptional<typeof KdfSchema>;
|
|
34
|
+
}>;
|
|
35
|
+
|
|
36
|
+
/** Validates compression metadata. */
|
|
37
|
+
export declare const CompressionSchema: z.ZodObject<{
|
|
38
|
+
algorithm: z.ZodEnum<["gzip"]>;
|
|
39
|
+
}>;
|
|
40
|
+
|
|
41
|
+
/** Validates a sub-manifest reference in a v2 Merkle manifest. */
|
|
42
|
+
export declare const SubManifestRefSchema: z.ZodObject<{
|
|
43
|
+
oid: z.ZodString;
|
|
44
|
+
chunkCount: z.ZodNumber;
|
|
45
|
+
startIndex: z.ZodNumber;
|
|
22
46
|
}>;
|
|
23
47
|
|
|
24
48
|
/** Validates a complete file manifest. */
|
|
25
49
|
export declare const ManifestSchema: z.ZodObject<{
|
|
50
|
+
version: z.ZodDefault<z.ZodNumber>;
|
|
26
51
|
slug: z.ZodString;
|
|
27
52
|
filename: z.ZodString;
|
|
28
53
|
size: z.ZodNumber;
|
|
29
54
|
chunks: z.ZodArray<typeof ChunkSchema>;
|
|
30
55
|
encryption: z.ZodOptional<typeof EncryptionSchema>;
|
|
56
|
+
compression: z.ZodOptional<typeof CompressionSchema>;
|
|
57
|
+
subManifests: z.ZodOptional<z.ZodArray<typeof SubManifestRefSchema>>;
|
|
31
58
|
}>;
|
|
@@ -13,19 +13,46 @@ export const ChunkSchema = z.object({
|
|
|
13
13
|
blob: z.string().min(1), // Git OID
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
+
/** Validates KDF parameters stored alongside encryption metadata. */
|
|
17
|
+
export const KdfSchema = z.object({
|
|
18
|
+
algorithm: z.enum(['pbkdf2', 'scrypt']),
|
|
19
|
+
salt: z.string().min(1),
|
|
20
|
+
iterations: z.number().int().positive().optional(),
|
|
21
|
+
cost: z.number().int().positive().optional(),
|
|
22
|
+
blockSize: z.number().int().positive().optional(),
|
|
23
|
+
parallelization: z.number().int().positive().optional(),
|
|
24
|
+
keyLength: z.number().int().positive().default(32),
|
|
25
|
+
});
|
|
26
|
+
|
|
16
27
|
/** Validates the encryption metadata attached to an encrypted manifest. */
|
|
17
28
|
export const EncryptionSchema = z.object({
|
|
18
29
|
algorithm: z.string(),
|
|
19
30
|
nonce: z.string(),
|
|
20
31
|
tag: z.string(),
|
|
21
32
|
encrypted: z.boolean().default(true),
|
|
33
|
+
kdf: KdfSchema.optional(),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/** Validates compression metadata. */
|
|
37
|
+
export const CompressionSchema = z.object({
|
|
38
|
+
algorithm: z.enum(['gzip']),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/** Validates a sub-manifest reference in a v2 Merkle manifest. */
|
|
42
|
+
export const SubManifestRefSchema = z.object({
|
|
43
|
+
oid: z.string().min(1),
|
|
44
|
+
chunkCount: z.number().int().positive(),
|
|
45
|
+
startIndex: z.number().int().min(0),
|
|
22
46
|
});
|
|
23
47
|
|
|
24
48
|
/** Validates a complete file manifest. */
|
|
25
49
|
export const ManifestSchema = z.object({
|
|
50
|
+
version: z.number().int().min(1).max(2).default(1),
|
|
26
51
|
slug: z.string().min(1),
|
|
27
52
|
filename: z.string().min(1),
|
|
28
53
|
size: z.number().int().min(0),
|
|
29
54
|
chunks: z.array(ChunkSchema),
|
|
30
55
|
encryption: EncryptionSchema.optional(),
|
|
56
|
+
compression: CompressionSchema.optional(),
|
|
57
|
+
subManifests: z.array(SubManifestRefSchema).optional(),
|
|
31
58
|
});
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { EventEmitter } from "node:events";
|
|
7
7
|
import Manifest from "../value-objects/Manifest.js";
|
|
8
|
-
import type { EncryptionMeta } from "../value-objects/Manifest.js";
|
|
8
|
+
import type { EncryptionMeta, CompressionMeta, KdfParams } from "../value-objects/Manifest.js";
|
|
9
9
|
|
|
10
10
|
/** Port interface for cryptographic operations (hashing, encryption, random bytes). */
|
|
11
11
|
export interface CryptoPort {
|
|
@@ -20,6 +20,7 @@ export interface CryptoPort {
|
|
|
20
20
|
encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>;
|
|
21
21
|
finalize: () => EncryptionMeta;
|
|
22
22
|
};
|
|
23
|
+
deriveKey(options: DeriveKeyOptions): Promise<DeriveKeyResult>;
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
/** Port interface for encoding and decoding manifest data. */
|
|
@@ -45,6 +46,26 @@ export interface CasServiceOptions {
|
|
|
45
46
|
codec: CodecPort;
|
|
46
47
|
crypto: CryptoPort;
|
|
47
48
|
chunkSize?: number;
|
|
49
|
+
merkleThreshold?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Options for key derivation. */
|
|
53
|
+
export interface DeriveKeyOptions {
|
|
54
|
+
passphrase: string;
|
|
55
|
+
salt?: Buffer;
|
|
56
|
+
algorithm?: "pbkdf2" | "scrypt";
|
|
57
|
+
iterations?: number;
|
|
58
|
+
cost?: number;
|
|
59
|
+
blockSize?: number;
|
|
60
|
+
parallelization?: number;
|
|
61
|
+
keyLength?: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Result from key derivation. */
|
|
65
|
+
export interface DeriveKeyResult {
|
|
66
|
+
key: Buffer;
|
|
67
|
+
salt: Buffer;
|
|
68
|
+
params: KdfParams;
|
|
48
69
|
}
|
|
49
70
|
|
|
50
71
|
/**
|
|
@@ -58,6 +79,7 @@ export default class CasService extends EventEmitter {
|
|
|
58
79
|
readonly codec: CodecPort;
|
|
59
80
|
readonly crypto: CryptoPort;
|
|
60
81
|
readonly chunkSize: number;
|
|
82
|
+
readonly merkleThreshold: number;
|
|
61
83
|
|
|
62
84
|
constructor(options: CasServiceOptions);
|
|
63
85
|
|
|
@@ -77,6 +99,9 @@ export default class CasService extends EventEmitter {
|
|
|
77
99
|
slug: string;
|
|
78
100
|
filename: string;
|
|
79
101
|
encryptionKey?: Buffer;
|
|
102
|
+
passphrase?: string;
|
|
103
|
+
kdfOptions?: Omit<DeriveKeyOptions, "passphrase">;
|
|
104
|
+
compression?: { algorithm: "gzip" };
|
|
80
105
|
}): Promise<Manifest>;
|
|
81
106
|
|
|
82
107
|
createTree(options: { manifest: Manifest }): Promise<string>;
|
|
@@ -84,6 +109,7 @@ export default class CasService extends EventEmitter {
|
|
|
84
109
|
restore(options: {
|
|
85
110
|
manifest: Manifest;
|
|
86
111
|
encryptionKey?: Buffer;
|
|
112
|
+
passphrase?: string;
|
|
87
113
|
}): Promise<{ buffer: Buffer; bytesWritten: number }>;
|
|
88
114
|
|
|
89
115
|
readManifest(options: { treeOid: string }): Promise<Manifest>;
|
|
@@ -97,4 +123,6 @@ export default class CasService extends EventEmitter {
|
|
|
97
123
|
}): Promise<{ referenced: Set<string>; total: number }>;
|
|
98
124
|
|
|
99
125
|
verifyIntegrity(manifest: Manifest): Promise<boolean>;
|
|
126
|
+
|
|
127
|
+
deriveKey(options: DeriveKeyOptions): Promise<DeriveKeyResult>;
|
|
100
128
|
}
|