@git-stunts/git-cas 5.2.1 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +100 -0
- package/README.md +67 -5
- package/bin/actions.js +38 -8
- package/bin/config.js +203 -0
- package/bin/git-cas.js +200 -54
- package/bin/ui/context.js +13 -0
- package/bin/ui/dashboard-cmds.js +14 -6
- package/bin/ui/dashboard-view.js +55 -6
- package/bin/ui/dashboard.js +109 -7
- package/bin/ui/encryption-card.js +2 -2
- package/bin/ui/heatmap.js +20 -2
- package/bin/ui/history-timeline.js +8 -6
- package/bin/ui/manifest-view.js +37 -8
- package/bin/ui/passphrase-prompt.js +100 -0
- package/bin/ui/progress.js +60 -38
- package/index.d.ts +20 -1
- package/index.js +74 -218
- package/package.json +2 -1
- package/src/domain/errors/CasError.js +3 -1
- package/src/domain/helpers/buildKdfMetadata.js +18 -0
- package/src/domain/services/CasService.d.ts +21 -1
- package/src/domain/services/CasService.js +134 -228
- package/src/domain/services/KeyResolver.js +220 -0
- package/src/domain/services/Semaphore.js +2 -2
- package/src/domain/services/VaultService.js +72 -29
- package/src/domain/services/rotateVaultPassphrase.js +143 -0
- package/src/infrastructure/adapters/BunCryptoAdapter.js +38 -6
- package/src/infrastructure/adapters/EventEmitterObserver.js +19 -7
- package/src/infrastructure/adapters/FileIOHelper.js +64 -0
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +21 -5
- package/src/infrastructure/adapters/GitRefAdapter.js +27 -5
- package/src/infrastructure/adapters/NodeCryptoAdapter.js +49 -8
- package/src/infrastructure/adapters/SilentObserver.js +15 -0
- package/src/infrastructure/adapters/StatsCollector.js +15 -1
- package/src/infrastructure/adapters/WebCryptoAdapter.js +120 -42
- package/src/infrastructure/adapters/createCryptoAdapter.js +25 -0
- package/src/infrastructure/chunkers/CdcChunker.js +5 -0
- package/src/infrastructure/chunkers/FixedChunker.js +28 -8
- package/src/infrastructure/chunkers/resolveChunker.js +45 -0
- package/src/infrastructure/codecs/CborCodec.js +11 -3
- package/src/infrastructure/codecs/JsonCodec.js +10 -2
- package/src/ports/CodecPort.js +3 -3
- package/src/ports/CryptoPort.js +56 -23
- package/src/ports/GitPersistencePort.js +4 -4
- package/src/ports/GitRefPort.js +10 -10
- package/src/types/ambient.d.ts +37 -0
|
@@ -9,15 +9,39 @@ import CasError from '../../domain/errors/CasError.js';
|
|
|
9
9
|
* AES-GCM is a one-shot API (the GCM tag is computed over the entire plaintext).
|
|
10
10
|
*/
|
|
11
11
|
export default class WebCryptoAdapter extends CryptoPort {
|
|
12
|
-
/** @
|
|
12
|
+
/** @type {number} */
|
|
13
|
+
#maxEncryptionBufferSize;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {Object} [options]
|
|
17
|
+
* @param {number} [options.maxEncryptionBufferSize=536870912] - Max bytes to buffer during streaming encryption (default 512 MiB).
|
|
18
|
+
*/
|
|
19
|
+
constructor({ maxEncryptionBufferSize = 512 * 1024 * 1024 } = {}) {
|
|
20
|
+
super();
|
|
21
|
+
if (!Number.isFinite(maxEncryptionBufferSize) || maxEncryptionBufferSize <= 0) {
|
|
22
|
+
throw new RangeError('maxEncryptionBufferSize must be a finite positive number');
|
|
23
|
+
}
|
|
24
|
+
this.#maxEncryptionBufferSize = maxEncryptionBufferSize;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @override
|
|
29
|
+
* @param {Buffer|Uint8Array} buf - Data to hash.
|
|
30
|
+
* @returns {Promise<string>} 64-char hex digest.
|
|
31
|
+
*/
|
|
13
32
|
async sha256(buf) {
|
|
33
|
+
// @ts-ignore -- Buffer satisfies BufferSource at runtime; TS strictness mismatch
|
|
14
34
|
const hashBuffer = await globalThis.crypto.subtle.digest('SHA-256', buf);
|
|
15
35
|
return Array.from(new Uint8Array(hashBuffer))
|
|
16
36
|
.map(b => b.toString(16).padStart(2, '0'))
|
|
17
37
|
.join('');
|
|
18
38
|
}
|
|
19
39
|
|
|
20
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* @override
|
|
42
|
+
* @param {number} n - Number of random bytes.
|
|
43
|
+
* @returns {Buffer|Uint8Array}
|
|
44
|
+
*/
|
|
21
45
|
randomBytes(n) {
|
|
22
46
|
const uint8 = globalThis.crypto.getRandomValues(new Uint8Array(n));
|
|
23
47
|
if (globalThis.Buffer) {
|
|
@@ -26,7 +50,12 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
26
50
|
return uint8;
|
|
27
51
|
}
|
|
28
52
|
|
|
29
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* @override
|
|
55
|
+
* @param {Buffer|Uint8Array} buffer - Plaintext to encrypt.
|
|
56
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
57
|
+
* @returns {Promise<{ buf: Buffer, meta: import('../../ports/CryptoPort.js').EncryptionMeta }>}
|
|
58
|
+
*/
|
|
30
59
|
async encryptBuffer(buffer, key) {
|
|
31
60
|
this._validateKey(key);
|
|
32
61
|
const nonce = this.randomBytes(12);
|
|
@@ -34,7 +63,8 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
34
63
|
|
|
35
64
|
// AES-GCM in Web Crypto includes the tag at the end of the ciphertext
|
|
36
65
|
const encrypted = await globalThis.crypto.subtle.encrypt(
|
|
37
|
-
|
|
66
|
+
// @ts-ignore -- Uint8Array satisfies BufferSource at runtime
|
|
67
|
+
{ name: 'AES-GCM', iv: /** @type {Uint8Array} */ (nonce) },
|
|
38
68
|
cryptoKey,
|
|
39
69
|
buffer
|
|
40
70
|
);
|
|
@@ -50,8 +80,15 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
50
80
|
};
|
|
51
81
|
}
|
|
52
82
|
|
|
53
|
-
/**
|
|
83
|
+
/**
|
|
84
|
+
* @override
|
|
85
|
+
* @param {Buffer|Uint8Array} buffer - Ciphertext to decrypt.
|
|
86
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
87
|
+
* @param {import('../../ports/CryptoPort.js').EncryptionMeta} meta - Encryption metadata.
|
|
88
|
+
* @returns {Promise<Buffer>}
|
|
89
|
+
*/
|
|
54
90
|
async decryptBuffer(buffer, key, meta) {
|
|
91
|
+
this._validateKey(key);
|
|
55
92
|
const nonce = this.#fromBase64(meta.nonce);
|
|
56
93
|
const tag = this.#fromBase64(meta.tag);
|
|
57
94
|
const cryptoKey = await this.#importKey(key);
|
|
@@ -63,7 +100,8 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
63
100
|
|
|
64
101
|
try {
|
|
65
102
|
const decrypted = await globalThis.crypto.subtle.decrypt(
|
|
66
|
-
|
|
103
|
+
// @ts-ignore -- Uint8Array satisfies BufferSource at runtime
|
|
104
|
+
{ name: 'AES-GCM', iv: /** @type {Uint8Array} */ (nonce) },
|
|
67
105
|
cryptoKey,
|
|
68
106
|
combined
|
|
69
107
|
);
|
|
@@ -73,56 +111,79 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
73
111
|
}
|
|
74
112
|
}
|
|
75
113
|
|
|
76
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* @override
|
|
116
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
117
|
+
* @returns {{ encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>, finalize: () => import('../../ports/CryptoPort.js').EncryptionMeta }}
|
|
118
|
+
*/
|
|
77
119
|
createEncryptionStream(key) {
|
|
78
120
|
this._validateKey(key);
|
|
79
121
|
const nonce = this.randomBytes(12);
|
|
80
122
|
const cryptoKeyPromise = this.#importKey(key);
|
|
123
|
+
const maxBuf = this.#maxEncryptionBufferSize;
|
|
124
|
+
const state = { /** @type {Uint8Array|null} */ tag: null, consumed: false };
|
|
125
|
+
|
|
126
|
+
const encrypt = WebCryptoAdapter.#makeEncryptGenerator({ cryptoKeyPromise, nonce, maxBuf, state });
|
|
81
127
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
let streamConsumed = false;
|
|
128
|
+
const finalize = () => {
|
|
129
|
+
if (!state.consumed) {
|
|
130
|
+
throw new CasError('Cannot finalize before the encrypt stream is fully consumed', 'STREAM_NOT_CONSUMED');
|
|
131
|
+
}
|
|
132
|
+
return this._buildMeta(this.#toBase64(nonce), this.#toBase64(/** @type {Uint8Array} */ (state.tag)));
|
|
133
|
+
};
|
|
89
134
|
|
|
90
|
-
|
|
135
|
+
return { encrypt, finalize };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Builds the encrypt async generator for createEncryptionStream.
|
|
140
|
+
*
|
|
141
|
+
* A static method is used (rather than closures) because `async function*`
|
|
142
|
+
* cannot be an arrow function — `this` binding would be lost. The `state`
|
|
143
|
+
* object bridges mutable data between the generator and `finalize()`.
|
|
144
|
+
*
|
|
145
|
+
* @param {{ cryptoKeyPromise: Promise<CryptoKey>, nonce: Buffer|Uint8Array, maxBuf: number, state: { tag: Uint8Array|null, consumed: boolean } }} ctx
|
|
146
|
+
* @returns {(source: AsyncIterable<Buffer>) => AsyncGenerator<Buffer>}
|
|
147
|
+
*/
|
|
148
|
+
static #makeEncryptGenerator({ cryptoKeyPromise, nonce, maxBuf, state }) {
|
|
149
|
+
return async function* (source) {
|
|
150
|
+
/** @type {Buffer[]} */
|
|
151
|
+
const chunks = [];
|
|
152
|
+
let accumulatedBytes = 0;
|
|
91
153
|
for await (const chunk of source) {
|
|
154
|
+
accumulatedBytes += chunk.length;
|
|
155
|
+
if (accumulatedBytes > maxBuf) {
|
|
156
|
+
throw new CasError(
|
|
157
|
+
`Streaming encryption buffered ${accumulatedBytes} bytes (limit: ${maxBuf}). ` +
|
|
158
|
+
'Web Crypto AES-GCM buffers all data. Use Node.js/Bun or store without encryption for large files.',
|
|
159
|
+
'ENCRYPTION_BUFFER_EXCEEDED',
|
|
160
|
+
{ accumulated: accumulatedBytes, limit: maxBuf },
|
|
161
|
+
);
|
|
162
|
+
}
|
|
92
163
|
chunks.push(chunk);
|
|
93
164
|
}
|
|
94
|
-
|
|
95
165
|
const buffer = Buffer.concat(chunks);
|
|
96
166
|
const cryptoKey = await cryptoKeyPromise;
|
|
97
167
|
const encrypted = await globalThis.crypto.subtle.encrypt(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
buffer
|
|
168
|
+
// @ts-ignore -- Uint8Array satisfies BufferSource at runtime
|
|
169
|
+
{ name: 'AES-GCM', iv: /** @type {Uint8Array} */ (nonce) },
|
|
170
|
+
cryptoKey, buffer,
|
|
101
171
|
);
|
|
102
|
-
|
|
103
172
|
const fullBuffer = new Uint8Array(encrypted);
|
|
104
173
|
const tagLength = 16;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
yield Buffer.from(ciphertext);
|
|
174
|
+
state.tag = fullBuffer.slice(-tagLength);
|
|
175
|
+
state.consumed = true;
|
|
176
|
+
yield Buffer.from(fullBuffer.slice(0, -tagLength));
|
|
110
177
|
};
|
|
111
|
-
|
|
112
|
-
const finalize = () => {
|
|
113
|
-
if (!streamConsumed) {
|
|
114
|
-
throw new CasError(
|
|
115
|
-
'Cannot finalize before the encrypt stream is fully consumed',
|
|
116
|
-
'STREAM_NOT_CONSUMED',
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
return this._buildMeta(this.#toBase64(nonce), this.#toBase64(finalTag));
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
return { encrypt, finalize };
|
|
123
178
|
}
|
|
124
179
|
|
|
125
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* @override
|
|
182
|
+
* @param {string} passphrase - The passphrase.
|
|
183
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
184
|
+
* @param {import('../../ports/CryptoPort.js').DeriveKeyParams} params - KDF parameters.
|
|
185
|
+
* @returns {Promise<Buffer>}
|
|
186
|
+
*/
|
|
126
187
|
async _doDeriveKey(passphrase, saltBuf, { algorithm, iterations, cost, blockSize, parallelization, keyLength }) {
|
|
127
188
|
if (algorithm === 'pbkdf2') {
|
|
128
189
|
return this.#derivePbkdf2(passphrase, saltBuf, { iterations, keyLength });
|
|
@@ -130,18 +191,33 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
130
191
|
return this.#deriveScrypt(passphrase, saltBuf, { cost, blockSize, parallelization, keyLength });
|
|
131
192
|
}
|
|
132
193
|
|
|
194
|
+
/**
|
|
195
|
+
* Derives a key using PBKDF2 via Web Crypto.
|
|
196
|
+
* @param {string} passphrase - The passphrase.
|
|
197
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
198
|
+
* @param {{ iterations: number, keyLength: number }} params - PBKDF2 parameters.
|
|
199
|
+
* @returns {Promise<Buffer>}
|
|
200
|
+
*/
|
|
133
201
|
async #derivePbkdf2(passphrase, saltBuf, params) {
|
|
134
202
|
const enc = new globalThis.TextEncoder();
|
|
135
203
|
const baseKey = await globalThis.crypto.subtle.importKey(
|
|
136
204
|
'raw', enc.encode(passphrase), 'PBKDF2', false, ['deriveBits'],
|
|
137
205
|
);
|
|
138
206
|
const bits = await globalThis.crypto.subtle.deriveBits(
|
|
139
|
-
|
|
207
|
+
// @ts-ignore -- Uint8Array satisfies BufferSource at runtime
|
|
208
|
+
{ name: 'PBKDF2', salt: /** @type {Uint8Array} */ (saltBuf), iterations: params.iterations, hash: 'SHA-512' },
|
|
140
209
|
baseKey, params.keyLength * 8,
|
|
141
210
|
);
|
|
142
211
|
return Buffer.from(bits);
|
|
143
212
|
}
|
|
144
213
|
|
|
214
|
+
/**
|
|
215
|
+
* Derives a key using scrypt via Node's crypto module (fallback).
|
|
216
|
+
* @param {string} passphrase - The passphrase.
|
|
217
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
218
|
+
* @param {{ cost: number, blockSize: number, parallelization: number, keyLength: number }} params - scrypt parameters.
|
|
219
|
+
* @returns {Promise<Buffer>}
|
|
220
|
+
*/
|
|
145
221
|
async #deriveScrypt(passphrase, saltBuf, params) {
|
|
146
222
|
let scryptCb;
|
|
147
223
|
let promisifyFn;
|
|
@@ -151,6 +227,7 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
151
227
|
} catch {
|
|
152
228
|
throw new Error('scrypt KDF requires a Node.js-compatible runtime (node:crypto unavailable)');
|
|
153
229
|
}
|
|
230
|
+
// @ts-ignore -- promisify(scrypt) accepts options as 4th arg at runtime
|
|
154
231
|
return promisifyFn(scryptCb)(passphrase, saltBuf, params.keyLength, {
|
|
155
232
|
N: params.cost, r: params.blockSize, p: params.parallelization,
|
|
156
233
|
});
|
|
@@ -164,7 +241,8 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
164
241
|
async #importKey(rawKey) {
|
|
165
242
|
return globalThis.crypto.subtle.importKey(
|
|
166
243
|
'raw',
|
|
167
|
-
|
|
244
|
+
// @ts-ignore -- Buffer/Uint8Array satisfies BufferSource at runtime
|
|
245
|
+
/** @type {Uint8Array} */ (rawKey),
|
|
168
246
|
{ name: 'AES-GCM' },
|
|
169
247
|
false,
|
|
170
248
|
['encrypt', 'decrypt']
|
|
@@ -173,7 +251,7 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
173
251
|
|
|
174
252
|
/**
|
|
175
253
|
* Encodes binary data to base64, using Buffer when available.
|
|
176
|
-
* @param {Uint8Array} buf
|
|
254
|
+
* @param {Buffer|Uint8Array} buf - Binary data to encode.
|
|
177
255
|
* @returns {string}
|
|
178
256
|
*/
|
|
179
257
|
#toBase64(buf) {
|
|
@@ -185,7 +263,7 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
185
263
|
|
|
186
264
|
/**
|
|
187
265
|
* Decodes a base64 string to binary, using Buffer when available.
|
|
188
|
-
* @param {string} str
|
|
266
|
+
* @param {string} str - Base64-encoded string.
|
|
189
267
|
* @returns {Buffer|Uint8Array}
|
|
190
268
|
*/
|
|
191
269
|
#fromBase64(str) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Runtime-adaptive crypto adapter factory.
|
|
3
|
+
*/
|
|
4
|
+
import NodeCryptoAdapter from './NodeCryptoAdapter.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Detects the best crypto adapter for the current runtime.
|
|
8
|
+
*
|
|
9
|
+
* - Bun → BunCryptoAdapter (dynamic import)
|
|
10
|
+
* - Deno → WebCryptoAdapter (dynamic import)
|
|
11
|
+
* - Node → NodeCryptoAdapter (static import)
|
|
12
|
+
*
|
|
13
|
+
* @returns {Promise<import('../../ports/CryptoPort.js').default>} A runtime-appropriate CryptoPort implementation.
|
|
14
|
+
*/
|
|
15
|
+
export default async function createCryptoAdapter() {
|
|
16
|
+
if (globalThis.Bun) {
|
|
17
|
+
const { default: BunCryptoAdapter } = await import('./BunCryptoAdapter.js');
|
|
18
|
+
return new BunCryptoAdapter();
|
|
19
|
+
}
|
|
20
|
+
if (globalThis.Deno) {
|
|
21
|
+
const { default: WebCryptoAdapter } = await import('./WebCryptoAdapter.js');
|
|
22
|
+
return new WebCryptoAdapter();
|
|
23
|
+
}
|
|
24
|
+
return new NodeCryptoAdapter();
|
|
25
|
+
}
|
|
@@ -277,6 +277,11 @@ export default class CdcChunker extends ChunkingPort {
|
|
|
277
277
|
`targetChunkSize (${targetChunkSize}) must be in [${minChunkSize}, ${maxChunkSize}]`,
|
|
278
278
|
);
|
|
279
279
|
}
|
|
280
|
+
if (maxChunkSize > 100 * 1024 * 1024) {
|
|
281
|
+
throw new RangeError(
|
|
282
|
+
`maxChunkSize must not exceed 104857600 bytes (100 MiB), got ${maxChunkSize}`,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
280
285
|
|
|
281
286
|
this.#minChunkSize = minChunkSize;
|
|
282
287
|
this.#maxChunkSize = maxChunkSize;
|
|
@@ -17,6 +17,14 @@ export default class FixedChunker extends ChunkingPort {
|
|
|
17
17
|
*/
|
|
18
18
|
constructor({ chunkSize = 262144 } = {}) {
|
|
19
19
|
super();
|
|
20
|
+
if (!Number.isInteger(chunkSize) || chunkSize < 1) {
|
|
21
|
+
throw new RangeError(`chunkSize must be a positive integer, got ${chunkSize}`);
|
|
22
|
+
}
|
|
23
|
+
if (chunkSize > 100 * 1024 * 1024) {
|
|
24
|
+
throw new RangeError(
|
|
25
|
+
`Chunk size must not exceed 104857600 bytes (100 MiB), got ${chunkSize}`,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
20
28
|
this.#chunkSize = chunkSize;
|
|
21
29
|
}
|
|
22
30
|
|
|
@@ -30,20 +38,32 @@ export default class FixedChunker extends ChunkingPort {
|
|
|
30
38
|
return { chunkSize: this.#chunkSize };
|
|
31
39
|
}
|
|
32
40
|
|
|
33
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* @override
|
|
43
|
+
* @param {AsyncIterable<Buffer>} source - The input byte stream.
|
|
44
|
+
* @yields {Buffer}
|
|
45
|
+
*/
|
|
34
46
|
async *chunk(source) {
|
|
35
|
-
|
|
47
|
+
const cs = this.#chunkSize;
|
|
48
|
+
const buf = Buffer.allocUnsafe(cs);
|
|
49
|
+
let offset = 0;
|
|
36
50
|
|
|
37
51
|
for await (const data of source) {
|
|
38
|
-
|
|
39
|
-
while (
|
|
40
|
-
|
|
41
|
-
|
|
52
|
+
let srcPos = 0;
|
|
53
|
+
while (srcPos < data.length) {
|
|
54
|
+
const n = Math.min(cs - offset, data.length - srcPos);
|
|
55
|
+
data.copy(buf, offset, srcPos, srcPos + n);
|
|
56
|
+
offset += n;
|
|
57
|
+
srcPos += n;
|
|
58
|
+
if (offset === cs) {
|
|
59
|
+
yield Buffer.from(buf);
|
|
60
|
+
offset = 0;
|
|
61
|
+
}
|
|
42
62
|
}
|
|
43
63
|
}
|
|
44
64
|
|
|
45
|
-
if (
|
|
46
|
-
yield
|
|
65
|
+
if (offset > 0) {
|
|
66
|
+
yield Buffer.from(buf.subarray(0, offset));
|
|
47
67
|
}
|
|
48
68
|
}
|
|
49
69
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Chunker factory — resolves a ChunkingPort from facade config.
|
|
3
|
+
*/
|
|
4
|
+
import FixedChunker from './FixedChunker.js';
|
|
5
|
+
import CdcChunker from './CdcChunker.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Resolves a {@link import('../../ports/ChunkingPort.js').default ChunkingPort}
|
|
9
|
+
* instance from facade configuration options.
|
|
10
|
+
*
|
|
11
|
+
* Resolution order:
|
|
12
|
+
* 1. A pre-built `chunker` instance takes precedence.
|
|
13
|
+
* 2. A declarative `chunking` config is used to construct the appropriate chunker.
|
|
14
|
+
* 3. `undefined` — CasService will fall back to its built-in FixedChunker default.
|
|
15
|
+
*
|
|
16
|
+
* @param {Object} options
|
|
17
|
+
* @param {import('../../ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance (advanced).
|
|
18
|
+
* @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number }} [options.chunking] - Declarative chunking strategy config.
|
|
19
|
+
* @returns {import('../../ports/ChunkingPort.js').default|undefined}
|
|
20
|
+
*/
|
|
21
|
+
export default function resolveChunker({ chunker, chunking } = {}) {
|
|
22
|
+
// Direct ChunkingPort instance takes precedence
|
|
23
|
+
if (chunker) {
|
|
24
|
+
return chunker;
|
|
25
|
+
}
|
|
26
|
+
// Build from declarative chunking config
|
|
27
|
+
if (chunking) {
|
|
28
|
+
if (chunking.strategy === 'cdc') {
|
|
29
|
+
return new CdcChunker({
|
|
30
|
+
targetChunkSize: chunking.targetChunkSize,
|
|
31
|
+
minChunkSize: chunking.minChunkSize,
|
|
32
|
+
maxChunkSize: chunking.maxChunkSize,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
// 'fixed' or unrecognized — fall through to default (FixedChunker via CasService)
|
|
36
|
+
if (chunking.strategy === 'fixed'
|
|
37
|
+
&& typeof chunking.chunkSize === 'number'
|
|
38
|
+
&& Number.isFinite(chunking.chunkSize)
|
|
39
|
+
&& chunking.chunkSize > 0) {
|
|
40
|
+
return new FixedChunker({ chunkSize: chunking.chunkSize });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// undefined → CasService will default to FixedChunker
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
@@ -5,14 +5,22 @@ import { encode, decode } from 'cbor-x';
|
|
|
5
5
|
* {@link CodecPort} implementation that serializes manifests as CBOR (binary).
|
|
6
6
|
*/
|
|
7
7
|
export default class CborCodec extends CodecPort {
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* @override
|
|
10
|
+
* @param {Record<string, unknown>} data - Data to encode.
|
|
11
|
+
* @returns {Buffer}
|
|
12
|
+
*/
|
|
9
13
|
encode(data) {
|
|
10
14
|
return encode(data);
|
|
11
15
|
}
|
|
12
16
|
|
|
13
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* @override
|
|
19
|
+
* @param {Buffer|string} buffer - CBOR-encoded data.
|
|
20
|
+
* @returns {Record<string, unknown>}
|
|
21
|
+
*/
|
|
14
22
|
decode(buffer) {
|
|
15
|
-
return decode(buffer);
|
|
23
|
+
return decode(/** @type {Buffer} */ (buffer));
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
/** @override */
|
|
@@ -4,14 +4,22 @@ import CodecPort from '../../ports/CodecPort.js';
|
|
|
4
4
|
* {@link CodecPort} implementation that serializes manifests as pretty-printed JSON.
|
|
5
5
|
*/
|
|
6
6
|
export default class JsonCodec extends CodecPort {
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* @override
|
|
9
|
+
* @param {Record<string, unknown>} data - Data to encode.
|
|
10
|
+
* @returns {string}
|
|
11
|
+
*/
|
|
8
12
|
encode(data) {
|
|
9
13
|
// Determine if we need to handle Buffers specially for JSON
|
|
10
14
|
// For now, we assume data is JSON-safe or uses toJSON() methods
|
|
11
15
|
return JSON.stringify(data, null, 2);
|
|
12
16
|
}
|
|
13
17
|
|
|
14
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* @override
|
|
20
|
+
* @param {Buffer|string} buffer - JSON-encoded data.
|
|
21
|
+
* @returns {Record<string, unknown>}
|
|
22
|
+
*/
|
|
15
23
|
decode(buffer) {
|
|
16
24
|
return JSON.parse(buffer.toString('utf8'));
|
|
17
25
|
}
|
package/src/ports/CodecPort.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
export default class CodecPort {
|
|
6
6
|
/**
|
|
7
7
|
* Encodes data to a Buffer or string.
|
|
8
|
-
* @param {
|
|
8
|
+
* @param {Record<string, unknown>} _data - Data to encode.
|
|
9
9
|
* @returns {Buffer|string}
|
|
10
10
|
*/
|
|
11
11
|
encode(_data) {
|
|
@@ -14,8 +14,8 @@ export default class CodecPort {
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Decodes data from a Buffer or string.
|
|
17
|
-
* @param {Buffer|string}
|
|
18
|
-
* @returns {
|
|
17
|
+
* @param {Buffer|string} _buffer - Encoded data to decode.
|
|
18
|
+
* @returns {Record<string, unknown>}
|
|
19
19
|
*/
|
|
20
20
|
decode(_buffer) {
|
|
21
21
|
throw new Error('Not implemented');
|
package/src/ports/CryptoPort.js
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
import CasError from '../domain/errors/CasError.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Encryption metadata returned by AES-256-GCM operations.
|
|
5
|
+
* @typedef {Object} EncryptionMeta
|
|
6
|
+
* @property {string} algorithm - Cipher algorithm identifier (e.g. `'aes-256-gcm'`).
|
|
7
|
+
* @property {string} nonce - Base64-encoded 12-byte nonce.
|
|
8
|
+
* @property {string} tag - Base64-encoded 16-byte GCM authentication tag.
|
|
9
|
+
* @property {boolean} encrypted - Whether the data is encrypted.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* KDF parameter set stored alongside derived keys.
|
|
14
|
+
* @typedef {Object} KdfParamSet
|
|
15
|
+
* @property {'pbkdf2'|'scrypt'} algorithm - KDF algorithm.
|
|
16
|
+
* @property {string} salt - Base64-encoded salt.
|
|
17
|
+
* @property {number} [iterations] - PBKDF2 iterations (present when algorithm is pbkdf2).
|
|
18
|
+
* @property {number} [cost] - scrypt cost N (present when algorithm is scrypt).
|
|
19
|
+
* @property {number} [blockSize] - scrypt block size r.
|
|
20
|
+
* @property {number} [parallelization] - scrypt parallelization p.
|
|
21
|
+
* @property {number} keyLength - Derived key length in bytes.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Normalized KDF options passed to `_doDeriveKey`.
|
|
26
|
+
* @typedef {Object} DeriveKeyParams
|
|
27
|
+
* @property {string} algorithm - KDF algorithm.
|
|
28
|
+
* @property {number} iterations - PBKDF2 iteration count.
|
|
29
|
+
* @property {number} cost - scrypt cost (N).
|
|
30
|
+
* @property {number} blockSize - scrypt block size (r).
|
|
31
|
+
* @property {number} parallelization - scrypt parallelization (p).
|
|
32
|
+
* @property {number} keyLength - Derived key length in bytes.
|
|
33
|
+
*/
|
|
34
|
+
|
|
3
35
|
/**
|
|
4
36
|
* Abstract port for cryptographic operations (hashing, random bytes, AES-256-GCM).
|
|
5
37
|
* @abstract
|
|
@@ -7,8 +39,8 @@ import CasError from '../domain/errors/CasError.js';
|
|
|
7
39
|
export default class CryptoPort {
|
|
8
40
|
/**
|
|
9
41
|
* Returns the SHA-256 hex digest of a buffer.
|
|
10
|
-
* @param {Buffer}
|
|
11
|
-
* @returns {string} 64-char hex digest
|
|
42
|
+
* @param {Buffer|Uint8Array} _buf - Data to hash.
|
|
43
|
+
* @returns {Promise<string>} 64-char hex digest.
|
|
12
44
|
*/
|
|
13
45
|
sha256(_buf) {
|
|
14
46
|
throw new Error('Not implemented');
|
|
@@ -16,8 +48,8 @@ export default class CryptoPort {
|
|
|
16
48
|
|
|
17
49
|
/**
|
|
18
50
|
* Returns a Buffer of n cryptographically random bytes.
|
|
19
|
-
* @param {number}
|
|
20
|
-
* @returns {Buffer}
|
|
51
|
+
* @param {number} _n - Number of random bytes.
|
|
52
|
+
* @returns {Buffer|Uint8Array}
|
|
21
53
|
*/
|
|
22
54
|
randomBytes(_n) {
|
|
23
55
|
throw new Error('Not implemented');
|
|
@@ -25,9 +57,9 @@ export default class CryptoPort {
|
|
|
25
57
|
|
|
26
58
|
/**
|
|
27
59
|
* Encrypts a buffer using AES-256-GCM.
|
|
28
|
-
* @param {Buffer}
|
|
29
|
-
* @param {Buffer}
|
|
30
|
-
* @returns {{ buf: Buffer, meta: {
|
|
60
|
+
* @param {Buffer|Uint8Array} _buffer - Plaintext to encrypt.
|
|
61
|
+
* @param {Buffer|Uint8Array} _key - 32-byte encryption key.
|
|
62
|
+
* @returns {{ buf: Buffer, meta: EncryptionMeta }|Promise<{ buf: Buffer, meta: EncryptionMeta }>}
|
|
31
63
|
*/
|
|
32
64
|
encryptBuffer(_buffer, _key) {
|
|
33
65
|
throw new Error('Not implemented');
|
|
@@ -35,11 +67,11 @@ export default class CryptoPort {
|
|
|
35
67
|
|
|
36
68
|
/**
|
|
37
69
|
* Decrypts a buffer using AES-256-GCM.
|
|
38
|
-
* @param {Buffer}
|
|
39
|
-
* @param {Buffer}
|
|
40
|
-
* @param {
|
|
41
|
-
* @returns {Buffer}
|
|
42
|
-
* @throws on authentication failure
|
|
70
|
+
* @param {Buffer|Uint8Array} _buffer - Ciphertext to decrypt.
|
|
71
|
+
* @param {Buffer|Uint8Array} _key - 32-byte encryption key.
|
|
72
|
+
* @param {EncryptionMeta} _meta - Encryption metadata from the encrypt operation.
|
|
73
|
+
* @returns {Buffer|Promise<Buffer>}
|
|
74
|
+
* @throws on authentication failure.
|
|
43
75
|
*/
|
|
44
76
|
decryptBuffer(_buffer, _key, _meta) {
|
|
45
77
|
throw new Error('Not implemented');
|
|
@@ -47,8 +79,8 @@ export default class CryptoPort {
|
|
|
47
79
|
|
|
48
80
|
/**
|
|
49
81
|
* Creates a streaming encryption context.
|
|
50
|
-
* @param {Buffer}
|
|
51
|
-
* @returns {{ encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>, finalize: () =>
|
|
82
|
+
* @param {Buffer|Uint8Array} _key - 32-byte encryption key.
|
|
83
|
+
* @returns {{ encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>, finalize: () => EncryptionMeta }}
|
|
52
84
|
*/
|
|
53
85
|
createEncryptionStream(_key) {
|
|
54
86
|
throw new Error('Not implemented');
|
|
@@ -62,14 +94,14 @@ export default class CryptoPort {
|
|
|
62
94
|
*
|
|
63
95
|
* @param {Object} options
|
|
64
96
|
* @param {string} options.passphrase - The passphrase to derive a key from.
|
|
65
|
-
* @param {Buffer} [options.salt] - Salt for the KDF (random if omitted).
|
|
97
|
+
* @param {Buffer|Uint8Array} [options.salt] - Salt for the KDF (random if omitted).
|
|
66
98
|
* @param {'pbkdf2'|'scrypt'} [options.algorithm='pbkdf2'] - KDF algorithm.
|
|
67
99
|
* @param {number} [options.iterations=100000] - PBKDF2 iteration count.
|
|
68
100
|
* @param {number} [options.cost=16384] - scrypt cost parameter (N).
|
|
69
101
|
* @param {number} [options.blockSize=8] - scrypt block size (r).
|
|
70
102
|
* @param {number} [options.parallelization=1] - scrypt parallelization (p).
|
|
71
103
|
* @param {number} [options.keyLength=32] - Derived key length in bytes.
|
|
72
|
-
* @returns {Promise<{ key: Buffer, salt: Buffer, params:
|
|
104
|
+
* @returns {Promise<{ key: Buffer, salt: Buffer, params: KdfParamSet }>}
|
|
73
105
|
*/
|
|
74
106
|
async deriveKey({
|
|
75
107
|
passphrase,
|
|
@@ -83,6 +115,7 @@ export default class CryptoPort {
|
|
|
83
115
|
}) {
|
|
84
116
|
const saltBuf = salt || this.randomBytes(32);
|
|
85
117
|
|
|
118
|
+
/** @type {KdfParamSet} */
|
|
86
119
|
const params = {
|
|
87
120
|
algorithm,
|
|
88
121
|
salt: Buffer.from(saltBuf).toString('base64'),
|
|
@@ -114,9 +147,9 @@ export default class CryptoPort {
|
|
|
114
147
|
/**
|
|
115
148
|
* Adapter-specific key derivation. Override in subclasses.
|
|
116
149
|
* @abstract
|
|
117
|
-
* @param {string} passphrase
|
|
118
|
-
* @param {Buffer|Uint8Array}
|
|
119
|
-
* @param {
|
|
150
|
+
* @param {string} _passphrase - The passphrase.
|
|
151
|
+
* @param {Buffer|Uint8Array} _saltBuf - Salt bytes.
|
|
152
|
+
* @param {DeriveKeyParams} _params - Normalized KDF parameters.
|
|
120
153
|
* @returns {Promise<Buffer|Uint8Array>} Derived key bytes.
|
|
121
154
|
*/
|
|
122
155
|
async _doDeriveKey(_passphrase, _saltBuf, _params) {
|
|
@@ -125,9 +158,9 @@ export default class CryptoPort {
|
|
|
125
158
|
|
|
126
159
|
/**
|
|
127
160
|
* Validates that a key is a 32-byte Buffer or Uint8Array.
|
|
128
|
-
* @param {Buffer|Uint8Array} key
|
|
129
|
-
* @throws {CasError} INVALID_KEY_TYPE if key is not a Buffer or Uint8Array
|
|
130
|
-
* @throws {CasError} INVALID_KEY_LENGTH if key is not 32 bytes
|
|
161
|
+
* @param {Buffer|Uint8Array} key - Key to validate.
|
|
162
|
+
* @throws {CasError} INVALID_KEY_TYPE if key is not a Buffer or Uint8Array.
|
|
163
|
+
* @throws {CasError} INVALID_KEY_LENGTH if key is not 32 bytes.
|
|
131
164
|
*/
|
|
132
165
|
_validateKey(key) {
|
|
133
166
|
if (!globalThis.Buffer?.isBuffer(key) && !(key instanceof Uint8Array)) {
|
|
@@ -149,7 +182,7 @@ export default class CryptoPort {
|
|
|
149
182
|
* Builds the encryption metadata object from base64-encoded nonce and tag.
|
|
150
183
|
* @param {string} nonce64 - Base64-encoded 12-byte AES-GCM nonce.
|
|
151
184
|
* @param {string} tag64 - Base64-encoded 16-byte GCM authentication tag.
|
|
152
|
-
* @returns {
|
|
185
|
+
* @returns {EncryptionMeta}
|
|
153
186
|
*/
|
|
154
187
|
_buildMeta(nonce64, tag64) {
|
|
155
188
|
return {
|