@git-stunts/git-cas 5.2.1 → 5.2.4
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 +52 -0
- package/README.md +24 -0
- package/bin/actions.js +24 -7
- package/bin/git-cas.js +78 -30
- 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/progress.js +60 -38
- package/index.d.ts +5 -1
- package/index.js +47 -215
- package/package.json +2 -1
- package/src/domain/helpers/buildKdfMetadata.js +18 -0
- package/src/domain/services/CasService.d.ts +1 -1
- package/src/domain/services/CasService.js +38 -213
- package/src/domain/services/KeyResolver.js +220 -0
- package/src/domain/services/Semaphore.js +2 -2
- package/src/domain/services/VaultService.js +49 -27
- 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 +38 -7
- package/src/infrastructure/adapters/SilentObserver.js +15 -0
- package/src/infrastructure/adapters/StatsCollector.js +15 -1
- package/src/infrastructure/adapters/WebCryptoAdapter.js +68 -18
- package/src/infrastructure/adapters/createCryptoAdapter.js +25 -0
- package/src/infrastructure/chunkers/FixedChunker.js +5 -1
- 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
|
@@ -29,21 +29,36 @@ export default class GitRefAdapter extends GitRefPort {
|
|
|
29
29
|
this.policy = policy ?? DEFAULT_POLICY;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* @override
|
|
34
|
+
* @param {string} ref - Git ref to resolve.
|
|
35
|
+
* @returns {Promise<string>} The commit OID.
|
|
36
|
+
*/
|
|
33
37
|
async resolveRef(ref) {
|
|
34
38
|
return this.policy.execute(() =>
|
|
35
39
|
this.plumbing.execute({ args: ['rev-parse', ref] }),
|
|
36
40
|
);
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* @override
|
|
45
|
+
* @param {string} commitOid - Git commit OID.
|
|
46
|
+
* @returns {Promise<string>} The tree OID.
|
|
47
|
+
*/
|
|
40
48
|
async resolveTree(commitOid) {
|
|
41
49
|
return this.policy.execute(() =>
|
|
42
50
|
this.plumbing.execute({ args: ['rev-parse', `${commitOid}^{tree}`] }),
|
|
43
51
|
);
|
|
44
52
|
}
|
|
45
53
|
|
|
46
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* @override
|
|
56
|
+
* @param {Object} options
|
|
57
|
+
* @param {string} options.treeOid - Tree OID for the commit.
|
|
58
|
+
* @param {string|null} [options.parentOid] - Parent commit OID.
|
|
59
|
+
* @param {string} options.message - Commit message.
|
|
60
|
+
* @returns {Promise<string>} The new commit OID.
|
|
61
|
+
*/
|
|
47
62
|
async createCommit({ treeOid, parentOid, message }) {
|
|
48
63
|
const args = ['commit-tree', treeOid, '-m', message];
|
|
49
64
|
if (parentOid) {
|
|
@@ -54,13 +69,20 @@ export default class GitRefAdapter extends GitRefPort {
|
|
|
54
69
|
);
|
|
55
70
|
}
|
|
56
71
|
|
|
57
|
-
/**
|
|
72
|
+
/**
|
|
73
|
+
* @override
|
|
74
|
+
* @param {Object} options
|
|
75
|
+
* @param {string} options.ref - Git ref to update.
|
|
76
|
+
* @param {string} options.newOid - New OID to set.
|
|
77
|
+
* @param {string|null} [options.expectedOldOid] - Expected current OID for CAS.
|
|
78
|
+
* @returns {Promise<void>}
|
|
79
|
+
*/
|
|
58
80
|
async updateRef({ ref, newOid, expectedOldOid }) {
|
|
59
81
|
const args = ['update-ref', ref, newOid];
|
|
60
82
|
if (expectedOldOid) {
|
|
61
83
|
args.push(expectedOldOid);
|
|
62
84
|
}
|
|
63
|
-
|
|
85
|
+
await this.policy.execute(() =>
|
|
64
86
|
this.plumbing.execute({ args }),
|
|
65
87
|
);
|
|
66
88
|
}
|
|
@@ -6,17 +6,30 @@ import CryptoPort from '../../ports/CryptoPort.js';
|
|
|
6
6
|
* Node.js implementation of CryptoPort using node:crypto.
|
|
7
7
|
*/
|
|
8
8
|
export default class NodeCryptoAdapter extends CryptoPort {
|
|
9
|
-
/**
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @override
|
|
11
|
+
* @param {Buffer|Uint8Array} buf - Data to hash.
|
|
12
|
+
* @returns {Promise<string>} 64-char hex digest.
|
|
13
|
+
*/
|
|
14
|
+
async sha256(buf) {
|
|
11
15
|
return createHash('sha256').update(buf).digest('hex');
|
|
12
16
|
}
|
|
13
17
|
|
|
14
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* @override
|
|
20
|
+
* @param {number} n - Number of random bytes.
|
|
21
|
+
* @returns {Buffer}
|
|
22
|
+
*/
|
|
15
23
|
randomBytes(n) {
|
|
16
24
|
return randomBytes(n);
|
|
17
25
|
}
|
|
18
26
|
|
|
19
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* @override
|
|
29
|
+
* @param {Buffer|Uint8Array} buffer - Plaintext to encrypt.
|
|
30
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
31
|
+
* @returns {{ buf: Buffer, meta: import('../../ports/CryptoPort.js').EncryptionMeta }}
|
|
32
|
+
*/
|
|
20
33
|
encryptBuffer(buffer, key) {
|
|
21
34
|
this._validateKey(key);
|
|
22
35
|
const nonce = randomBytes(12);
|
|
@@ -29,7 +42,13 @@ export default class NodeCryptoAdapter extends CryptoPort {
|
|
|
29
42
|
};
|
|
30
43
|
}
|
|
31
44
|
|
|
32
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* @override
|
|
47
|
+
* @param {Buffer|Uint8Array} buffer - Ciphertext to decrypt.
|
|
48
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
49
|
+
* @param {import('../../ports/CryptoPort.js').EncryptionMeta} meta - Encryption metadata.
|
|
50
|
+
* @returns {Buffer}
|
|
51
|
+
*/
|
|
33
52
|
decryptBuffer(buffer, key, meta) {
|
|
34
53
|
const nonce = Buffer.from(meta.nonce, 'base64');
|
|
35
54
|
const tag = Buffer.from(meta.tag, 'base64');
|
|
@@ -38,12 +57,17 @@ export default class NodeCryptoAdapter extends CryptoPort {
|
|
|
38
57
|
return Buffer.concat([decipher.update(buffer), decipher.final()]);
|
|
39
58
|
}
|
|
40
59
|
|
|
41
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* @override
|
|
62
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
63
|
+
* @returns {{ encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>, finalize: () => import('../../ports/CryptoPort.js').EncryptionMeta }}
|
|
64
|
+
*/
|
|
42
65
|
createEncryptionStream(key) {
|
|
43
66
|
this._validateKey(key);
|
|
44
67
|
const nonce = randomBytes(12);
|
|
45
68
|
const cipher = createCipheriv('aes-256-gcm', key, nonce);
|
|
46
69
|
|
|
70
|
+
/** @param {AsyncIterable<Buffer>} source */
|
|
47
71
|
const encrypt = async function* (source) {
|
|
48
72
|
for await (const chunk of source) {
|
|
49
73
|
const encrypted = cipher.update(chunk);
|
|
@@ -65,11 +89,18 @@ export default class NodeCryptoAdapter extends CryptoPort {
|
|
|
65
89
|
return { encrypt, finalize };
|
|
66
90
|
}
|
|
67
91
|
|
|
68
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* @override
|
|
94
|
+
* @param {string} passphrase - The passphrase.
|
|
95
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
96
|
+
* @param {import('../../ports/CryptoPort.js').DeriveKeyParams} params - KDF parameters.
|
|
97
|
+
* @returns {Promise<Buffer>}
|
|
98
|
+
*/
|
|
69
99
|
async _doDeriveKey(passphrase, saltBuf, { algorithm, iterations, cost, blockSize, parallelization, keyLength }) {
|
|
70
100
|
if (algorithm === 'pbkdf2') {
|
|
71
101
|
return await promisify(pbkdf2)(passphrase, saltBuf, iterations, keyLength, 'sha512');
|
|
72
102
|
}
|
|
103
|
+
// @ts-ignore -- promisify(scrypt) accepts options as 4th arg at runtime
|
|
73
104
|
return await promisify(scrypt)(passphrase, saltBuf, keyLength, {
|
|
74
105
|
N: cost,
|
|
75
106
|
r: blockSize,
|
|
@@ -3,8 +3,23 @@
|
|
|
3
3
|
* Used as the default when no observability is configured.
|
|
4
4
|
*/
|
|
5
5
|
export default class SilentObserver {
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} _channel - Metric channel.
|
|
8
|
+
* @param {Record<string, unknown>} _data - Metric payload.
|
|
9
|
+
*/
|
|
6
10
|
metric(_channel, _data) {}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {'debug'|'info'|'warn'|'error'} _level - Log level.
|
|
14
|
+
* @param {string} _msg - Log message.
|
|
15
|
+
* @param {Record<string, unknown>} [_meta] - Optional metadata.
|
|
16
|
+
*/
|
|
7
17
|
log(_level, _msg, _meta) {}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} _name - Span name.
|
|
21
|
+
* @returns {{ end(meta?: Record<string, unknown>): void }}
|
|
22
|
+
*/
|
|
8
23
|
span(_name) {
|
|
9
24
|
return { end() {} };
|
|
10
25
|
}
|
|
@@ -5,15 +5,20 @@ export default class StatsCollector {
|
|
|
5
5
|
#chunksProcessed = 0;
|
|
6
6
|
#bytesTotal = 0;
|
|
7
7
|
#errors = 0;
|
|
8
|
+
/** @type {number|null} */
|
|
8
9
|
#startTime = null;
|
|
9
10
|
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} channel - Metric channel.
|
|
13
|
+
* @param {Record<string, unknown>} data - Metric payload.
|
|
14
|
+
*/
|
|
10
15
|
metric(channel, data) {
|
|
11
16
|
if (!this.#startTime) {
|
|
12
17
|
this.#startTime = Date.now();
|
|
13
18
|
}
|
|
14
19
|
if (channel === 'chunk') {
|
|
15
20
|
this.#chunksProcessed++;
|
|
16
|
-
const size = Number.isFinite(data?.size) ? data.size : 0;
|
|
21
|
+
const size = Number.isFinite(data?.size) ? /** @type {number} */ (data.size) : 0;
|
|
17
22
|
this.#bytesTotal += size;
|
|
18
23
|
}
|
|
19
24
|
if (channel === 'error') {
|
|
@@ -21,8 +26,17 @@ export default class StatsCollector {
|
|
|
21
26
|
}
|
|
22
27
|
}
|
|
23
28
|
|
|
29
|
+
/**
|
|
30
|
+
* @param {'debug'|'info'|'warn'|'error'} _level - Log level.
|
|
31
|
+
* @param {string} _msg - Log message.
|
|
32
|
+
* @param {Record<string, unknown>} [_meta] - Optional metadata.
|
|
33
|
+
*/
|
|
24
34
|
log(_level, _msg, _meta) {}
|
|
25
35
|
|
|
36
|
+
/**
|
|
37
|
+
* @param {string} _name - Span name.
|
|
38
|
+
* @returns {{ end(meta?: Record<string, unknown>): void }}
|
|
39
|
+
*/
|
|
26
40
|
span(_name) {
|
|
27
41
|
return { end() {} };
|
|
28
42
|
}
|
|
@@ -9,15 +9,24 @@ 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
|
+
/**
|
|
13
|
+
* @override
|
|
14
|
+
* @param {Buffer|Uint8Array} buf - Data to hash.
|
|
15
|
+
* @returns {Promise<string>} 64-char hex digest.
|
|
16
|
+
*/
|
|
13
17
|
async sha256(buf) {
|
|
18
|
+
// @ts-ignore -- Buffer satisfies BufferSource at runtime; TS strictness mismatch
|
|
14
19
|
const hashBuffer = await globalThis.crypto.subtle.digest('SHA-256', buf);
|
|
15
20
|
return Array.from(new Uint8Array(hashBuffer))
|
|
16
21
|
.map(b => b.toString(16).padStart(2, '0'))
|
|
17
22
|
.join('');
|
|
18
23
|
}
|
|
19
24
|
|
|
20
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* @override
|
|
27
|
+
* @param {number} n - Number of random bytes.
|
|
28
|
+
* @returns {Buffer|Uint8Array}
|
|
29
|
+
*/
|
|
21
30
|
randomBytes(n) {
|
|
22
31
|
const uint8 = globalThis.crypto.getRandomValues(new Uint8Array(n));
|
|
23
32
|
if (globalThis.Buffer) {
|
|
@@ -26,7 +35,12 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
26
35
|
return uint8;
|
|
27
36
|
}
|
|
28
37
|
|
|
29
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* @override
|
|
40
|
+
* @param {Buffer|Uint8Array} buffer - Plaintext to encrypt.
|
|
41
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
42
|
+
* @returns {Promise<{ buf: Buffer, meta: import('../../ports/CryptoPort.js').EncryptionMeta }>}
|
|
43
|
+
*/
|
|
30
44
|
async encryptBuffer(buffer, key) {
|
|
31
45
|
this._validateKey(key);
|
|
32
46
|
const nonce = this.randomBytes(12);
|
|
@@ -34,7 +48,8 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
34
48
|
|
|
35
49
|
// AES-GCM in Web Crypto includes the tag at the end of the ciphertext
|
|
36
50
|
const encrypted = await globalThis.crypto.subtle.encrypt(
|
|
37
|
-
|
|
51
|
+
// @ts-ignore -- Uint8Array satisfies BufferSource at runtime
|
|
52
|
+
{ name: 'AES-GCM', iv: /** @type {Uint8Array} */ (nonce) },
|
|
38
53
|
cryptoKey,
|
|
39
54
|
buffer
|
|
40
55
|
);
|
|
@@ -50,7 +65,13 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
50
65
|
};
|
|
51
66
|
}
|
|
52
67
|
|
|
53
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* @override
|
|
70
|
+
* @param {Buffer|Uint8Array} buffer - Ciphertext to decrypt.
|
|
71
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
72
|
+
* @param {import('../../ports/CryptoPort.js').EncryptionMeta} meta - Encryption metadata.
|
|
73
|
+
* @returns {Promise<Buffer>}
|
|
74
|
+
*/
|
|
54
75
|
async decryptBuffer(buffer, key, meta) {
|
|
55
76
|
const nonce = this.#fromBase64(meta.nonce);
|
|
56
77
|
const tag = this.#fromBase64(meta.tag);
|
|
@@ -63,7 +84,8 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
63
84
|
|
|
64
85
|
try {
|
|
65
86
|
const decrypted = await globalThis.crypto.subtle.decrypt(
|
|
66
|
-
|
|
87
|
+
// @ts-ignore -- Uint8Array satisfies BufferSource at runtime
|
|
88
|
+
{ name: 'AES-GCM', iv: /** @type {Uint8Array} */ (nonce) },
|
|
67
89
|
cryptoKey,
|
|
68
90
|
combined
|
|
69
91
|
);
|
|
@@ -73,20 +95,24 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
73
95
|
}
|
|
74
96
|
}
|
|
75
97
|
|
|
76
|
-
/**
|
|
98
|
+
/**
|
|
99
|
+
* @override
|
|
100
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
101
|
+
* @returns {{ encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>, finalize: () => import('../../ports/CryptoPort.js').EncryptionMeta }}
|
|
102
|
+
*/
|
|
77
103
|
createEncryptionStream(key) {
|
|
78
104
|
this._validateKey(key);
|
|
79
105
|
const nonce = this.randomBytes(12);
|
|
80
106
|
const cryptoKeyPromise = this.#importKey(key);
|
|
81
107
|
|
|
82
|
-
// Web Crypto
|
|
83
|
-
|
|
84
|
-
// NOTE: This limits the "stream" to memory capacity, matching the project's
|
|
85
|
-
// current CasService.restore limitation.
|
|
108
|
+
// Web Crypto buffers all data for the one-shot AES-GCM call (GCM tag spans the whole plaintext).
|
|
109
|
+
/** @type {Buffer[]} */
|
|
86
110
|
const chunks = [];
|
|
111
|
+
/** @type {Uint8Array|null} */
|
|
87
112
|
let finalTag = null;
|
|
88
113
|
let streamConsumed = false;
|
|
89
114
|
|
|
115
|
+
/** @param {AsyncIterable<Buffer>} source */
|
|
90
116
|
const encrypt = async function* (source) {
|
|
91
117
|
for await (const chunk of source) {
|
|
92
118
|
chunks.push(chunk);
|
|
@@ -95,7 +121,8 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
95
121
|
const buffer = Buffer.concat(chunks);
|
|
96
122
|
const cryptoKey = await cryptoKeyPromise;
|
|
97
123
|
const encrypted = await globalThis.crypto.subtle.encrypt(
|
|
98
|
-
|
|
124
|
+
// @ts-ignore -- Uint8Array satisfies BufferSource at runtime
|
|
125
|
+
{ name: 'AES-GCM', iv: /** @type {Uint8Array} */ (nonce) },
|
|
99
126
|
cryptoKey,
|
|
100
127
|
buffer
|
|
101
128
|
);
|
|
@@ -116,13 +143,19 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
116
143
|
'STREAM_NOT_CONSUMED',
|
|
117
144
|
);
|
|
118
145
|
}
|
|
119
|
-
return this._buildMeta(this.#toBase64(nonce), this.#toBase64(finalTag));
|
|
146
|
+
return this._buildMeta(this.#toBase64(nonce), this.#toBase64(/** @type {Uint8Array} */ (finalTag)));
|
|
120
147
|
};
|
|
121
148
|
|
|
122
149
|
return { encrypt, finalize };
|
|
123
150
|
}
|
|
124
151
|
|
|
125
|
-
/**
|
|
152
|
+
/**
|
|
153
|
+
* @override
|
|
154
|
+
* @param {string} passphrase - The passphrase.
|
|
155
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
156
|
+
* @param {import('../../ports/CryptoPort.js').DeriveKeyParams} params - KDF parameters.
|
|
157
|
+
* @returns {Promise<Buffer>}
|
|
158
|
+
*/
|
|
126
159
|
async _doDeriveKey(passphrase, saltBuf, { algorithm, iterations, cost, blockSize, parallelization, keyLength }) {
|
|
127
160
|
if (algorithm === 'pbkdf2') {
|
|
128
161
|
return this.#derivePbkdf2(passphrase, saltBuf, { iterations, keyLength });
|
|
@@ -130,18 +163,33 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
130
163
|
return this.#deriveScrypt(passphrase, saltBuf, { cost, blockSize, parallelization, keyLength });
|
|
131
164
|
}
|
|
132
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Derives a key using PBKDF2 via Web Crypto.
|
|
168
|
+
* @param {string} passphrase - The passphrase.
|
|
169
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
170
|
+
* @param {{ iterations: number, keyLength: number }} params - PBKDF2 parameters.
|
|
171
|
+
* @returns {Promise<Buffer>}
|
|
172
|
+
*/
|
|
133
173
|
async #derivePbkdf2(passphrase, saltBuf, params) {
|
|
134
174
|
const enc = new globalThis.TextEncoder();
|
|
135
175
|
const baseKey = await globalThis.crypto.subtle.importKey(
|
|
136
176
|
'raw', enc.encode(passphrase), 'PBKDF2', false, ['deriveBits'],
|
|
137
177
|
);
|
|
138
178
|
const bits = await globalThis.crypto.subtle.deriveBits(
|
|
139
|
-
|
|
179
|
+
// @ts-ignore -- Uint8Array satisfies BufferSource at runtime
|
|
180
|
+
{ name: 'PBKDF2', salt: /** @type {Uint8Array} */ (saltBuf), iterations: params.iterations, hash: 'SHA-512' },
|
|
140
181
|
baseKey, params.keyLength * 8,
|
|
141
182
|
);
|
|
142
183
|
return Buffer.from(bits);
|
|
143
184
|
}
|
|
144
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Derives a key using scrypt via Node's crypto module (fallback).
|
|
188
|
+
* @param {string} passphrase - The passphrase.
|
|
189
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
190
|
+
* @param {{ cost: number, blockSize: number, parallelization: number, keyLength: number }} params - scrypt parameters.
|
|
191
|
+
* @returns {Promise<Buffer>}
|
|
192
|
+
*/
|
|
145
193
|
async #deriveScrypt(passphrase, saltBuf, params) {
|
|
146
194
|
let scryptCb;
|
|
147
195
|
let promisifyFn;
|
|
@@ -151,6 +199,7 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
151
199
|
} catch {
|
|
152
200
|
throw new Error('scrypt KDF requires a Node.js-compatible runtime (node:crypto unavailable)');
|
|
153
201
|
}
|
|
202
|
+
// @ts-ignore -- promisify(scrypt) accepts options as 4th arg at runtime
|
|
154
203
|
return promisifyFn(scryptCb)(passphrase, saltBuf, params.keyLength, {
|
|
155
204
|
N: params.cost, r: params.blockSize, p: params.parallelization,
|
|
156
205
|
});
|
|
@@ -164,7 +213,8 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
164
213
|
async #importKey(rawKey) {
|
|
165
214
|
return globalThis.crypto.subtle.importKey(
|
|
166
215
|
'raw',
|
|
167
|
-
|
|
216
|
+
// @ts-ignore -- Buffer/Uint8Array satisfies BufferSource at runtime
|
|
217
|
+
/** @type {Uint8Array} */ (rawKey),
|
|
168
218
|
{ name: 'AES-GCM' },
|
|
169
219
|
false,
|
|
170
220
|
['encrypt', 'decrypt']
|
|
@@ -173,7 +223,7 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
173
223
|
|
|
174
224
|
/**
|
|
175
225
|
* Encodes binary data to base64, using Buffer when available.
|
|
176
|
-
* @param {Uint8Array} buf
|
|
226
|
+
* @param {Buffer|Uint8Array} buf - Binary data to encode.
|
|
177
227
|
* @returns {string}
|
|
178
228
|
*/
|
|
179
229
|
#toBase64(buf) {
|
|
@@ -185,7 +235,7 @@ export default class WebCryptoAdapter extends CryptoPort {
|
|
|
185
235
|
|
|
186
236
|
/**
|
|
187
237
|
* Decodes a base64 string to binary, using Buffer when available.
|
|
188
|
-
* @param {string} str
|
|
238
|
+
* @param {string} str - Base64-encoded string.
|
|
189
239
|
* @returns {Buffer|Uint8Array}
|
|
190
240
|
*/
|
|
191
241
|
#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
|
+
}
|
|
@@ -30,7 +30,11 @@ export default class FixedChunker extends ChunkingPort {
|
|
|
30
30
|
return { chunkSize: this.#chunkSize };
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* @override
|
|
35
|
+
* @param {AsyncIterable<Buffer>} source - The input byte stream.
|
|
36
|
+
* @yields {Buffer}
|
|
37
|
+
*/
|
|
34
38
|
async *chunk(source) {
|
|
35
39
|
let buffer = Buffer.alloc(0);
|
|
36
40
|
|
|
@@ -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');
|