@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
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-ignore -- 'bun' module only available in Bun runtime
|
|
1
2
|
import { CryptoHasher } from 'bun';
|
|
2
3
|
import CryptoPort from '../../ports/CryptoPort.js';
|
|
3
4
|
import CasError from '../../domain/errors/CasError.js';
|
|
@@ -14,18 +15,31 @@ import { promisify } from 'node:util';
|
|
|
14
15
|
* AES-256-GCM (Bun's Node compat layer is heavily optimized for these APIs).
|
|
15
16
|
*/
|
|
16
17
|
export default class BunCryptoAdapter extends CryptoPort {
|
|
17
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* @override
|
|
20
|
+
* @param {Buffer|Uint8Array} buf - Data to hash.
|
|
21
|
+
* @returns {Promise<string>} 64-char hex digest.
|
|
22
|
+
*/
|
|
18
23
|
async sha256(buf) {
|
|
19
24
|
return new CryptoHasher('sha256').update(buf).digest('hex');
|
|
20
25
|
}
|
|
21
26
|
|
|
22
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* @override
|
|
29
|
+
* @param {number} n - Number of random bytes.
|
|
30
|
+
* @returns {Buffer}
|
|
31
|
+
*/
|
|
23
32
|
randomBytes(n) {
|
|
24
33
|
const uint8 = globalThis.crypto.getRandomValues(new Uint8Array(n));
|
|
25
34
|
return Buffer.from(uint8.buffer, uint8.byteOffset, uint8.byteLength);
|
|
26
35
|
}
|
|
27
36
|
|
|
28
|
-
/**
|
|
37
|
+
/**
|
|
38
|
+
* @override
|
|
39
|
+
* @param {Buffer|Uint8Array} buffer - Plaintext to encrypt.
|
|
40
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
41
|
+
* @returns {Promise<{ buf: Buffer, meta: import('../../ports/CryptoPort.js').EncryptionMeta }>}
|
|
42
|
+
*/
|
|
29
43
|
async encryptBuffer(buffer, key) {
|
|
30
44
|
this._validateKey(key);
|
|
31
45
|
const nonce = this.randomBytes(12);
|
|
@@ -38,7 +52,13 @@ export default class BunCryptoAdapter extends CryptoPort {
|
|
|
38
52
|
};
|
|
39
53
|
}
|
|
40
54
|
|
|
41
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* @override
|
|
57
|
+
* @param {Buffer|Uint8Array} buffer - Ciphertext to decrypt.
|
|
58
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
59
|
+
* @param {import('../../ports/CryptoPort.js').EncryptionMeta} meta - Encryption metadata.
|
|
60
|
+
* @returns {Promise<Buffer>}
|
|
61
|
+
*/
|
|
42
62
|
async decryptBuffer(buffer, key, meta) {
|
|
43
63
|
this._validateKey(key);
|
|
44
64
|
const nonce = Buffer.from(meta.nonce, 'base64');
|
|
@@ -48,13 +68,18 @@ export default class BunCryptoAdapter extends CryptoPort {
|
|
|
48
68
|
return Buffer.concat([decipher.update(buffer), decipher.final()]);
|
|
49
69
|
}
|
|
50
70
|
|
|
51
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* @override
|
|
73
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
74
|
+
* @returns {{ encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>, finalize: () => import('../../ports/CryptoPort.js').EncryptionMeta }}
|
|
75
|
+
*/
|
|
52
76
|
createEncryptionStream(key) {
|
|
53
77
|
this._validateKey(key);
|
|
54
78
|
const nonce = this.randomBytes(12);
|
|
55
79
|
const cipher = createCipheriv('aes-256-gcm', key, nonce);
|
|
56
80
|
let streamFinalized = false;
|
|
57
81
|
|
|
82
|
+
/** @param {AsyncIterable<Buffer>} source */
|
|
58
83
|
const encrypt = async function* (source) {
|
|
59
84
|
for await (const chunk of source) {
|
|
60
85
|
const encrypted = cipher.update(chunk);
|
|
@@ -83,11 +108,18 @@ export default class BunCryptoAdapter extends CryptoPort {
|
|
|
83
108
|
return { encrypt, finalize };
|
|
84
109
|
}
|
|
85
110
|
|
|
86
|
-
/**
|
|
111
|
+
/**
|
|
112
|
+
* @override
|
|
113
|
+
* @param {string} passphrase - The passphrase.
|
|
114
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
115
|
+
* @param {import('../../ports/CryptoPort.js').DeriveKeyParams} params - KDF parameters.
|
|
116
|
+
* @returns {Promise<Buffer>}
|
|
117
|
+
*/
|
|
87
118
|
async _doDeriveKey(passphrase, saltBuf, { algorithm, iterations, cost, blockSize, parallelization, keyLength }) {
|
|
88
119
|
if (algorithm === 'pbkdf2') {
|
|
89
120
|
return promisify(pbkdf2)(passphrase, saltBuf, iterations, keyLength, 'sha512');
|
|
90
121
|
}
|
|
122
|
+
// @ts-ignore -- promisify(scrypt) accepts options as 4th arg at runtime
|
|
91
123
|
return promisify(scrypt)(passphrase, saltBuf, keyLength, {
|
|
92
124
|
N: cost,
|
|
93
125
|
r: blockSize,
|
|
@@ -16,8 +16,8 @@ export default class EventEmitterObserver {
|
|
|
16
16
|
* Error metrics are only emitted when listeners are attached (matching
|
|
17
17
|
* the previous CasService behavior that guarded `this.emit('error', ...)`).
|
|
18
18
|
*
|
|
19
|
-
* @param {string} channel
|
|
20
|
-
* @param {
|
|
19
|
+
* @param {string} channel - Metric channel.
|
|
20
|
+
* @param {Record<string, unknown> & { action: string }} data - Must include `action` to form the event name.
|
|
21
21
|
*/
|
|
22
22
|
metric(channel, data) {
|
|
23
23
|
if (channel === 'error') {
|
|
@@ -26,21 +26,33 @@ export default class EventEmitterObserver {
|
|
|
26
26
|
}
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
|
+
if (typeof data.action !== 'string') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
29
32
|
const eventName = `${channel}:${data.action}`;
|
|
30
33
|
const payload = Object.fromEntries(Object.entries(data).filter(([k]) => k !== 'action'));
|
|
31
34
|
this.#emitter.emit(eventName, payload);
|
|
32
35
|
}
|
|
33
36
|
|
|
37
|
+
/**
|
|
38
|
+
* @param {'debug'|'info'|'warn'|'error'} _level - Log level.
|
|
39
|
+
* @param {string} _msg - Log message.
|
|
40
|
+
* @param {Record<string, unknown>} [_meta] - Optional metadata.
|
|
41
|
+
*/
|
|
34
42
|
log(_level, _msg, _meta) {}
|
|
35
43
|
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} _name - Span name.
|
|
46
|
+
* @returns {{ end(meta?: Record<string, unknown>): void }}
|
|
47
|
+
*/
|
|
36
48
|
span(_name) {
|
|
37
49
|
return { end() {} };
|
|
38
50
|
}
|
|
39
51
|
|
|
40
52
|
/**
|
|
41
53
|
* Subscribe to an event.
|
|
42
|
-
* @param {string} event
|
|
43
|
-
* @param {
|
|
54
|
+
* @param {string} event - Event name.
|
|
55
|
+
* @param {(...args: unknown[]) => void} listener - Event listener.
|
|
44
56
|
* @returns {this}
|
|
45
57
|
*/
|
|
46
58
|
on(event, listener) {
|
|
@@ -50,8 +62,8 @@ export default class EventEmitterObserver {
|
|
|
50
62
|
|
|
51
63
|
/**
|
|
52
64
|
* Remove a listener.
|
|
53
|
-
* @param {string} event
|
|
54
|
-
* @param {
|
|
65
|
+
* @param {string} event - Event name.
|
|
66
|
+
* @param {(...args: unknown[]) => void} listener - Event listener.
|
|
55
67
|
* @returns {this}
|
|
56
68
|
*/
|
|
57
69
|
removeListener(event, listener) {
|
|
@@ -61,7 +73,7 @@ export default class EventEmitterObserver {
|
|
|
61
73
|
|
|
62
74
|
/**
|
|
63
75
|
* Return the number of listeners for an event.
|
|
64
|
-
* @param {string} event
|
|
76
|
+
* @param {string} event - Event name.
|
|
65
77
|
* @returns {number}
|
|
66
78
|
*/
|
|
67
79
|
listenerCount(event) {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview File I/O helpers for storing and restoring files via CasService.
|
|
3
|
+
*/
|
|
4
|
+
import { createReadStream, createWriteStream } from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { Readable, Transform } from 'node:stream';
|
|
7
|
+
import { pipeline } from 'node:stream/promises';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Reads a file from disk and stores it in Git as chunked blobs via
|
|
11
|
+
* the given {@link import('../../domain/services/CasService.js').default CasService}.
|
|
12
|
+
*
|
|
13
|
+
* @param {import('../../domain/services/CasService.js').default} service - Initialized CasService.
|
|
14
|
+
* @param {Object} options
|
|
15
|
+
* @param {string} options.filePath - Absolute or relative path to the file.
|
|
16
|
+
* @param {string} options.slug - Logical identifier for the stored asset.
|
|
17
|
+
* @param {string} [options.filename] - Override filename (defaults to basename of filePath).
|
|
18
|
+
* @param {Buffer} [options.encryptionKey] - 32-byte key for AES-256-GCM encryption.
|
|
19
|
+
* @param {string} [options.passphrase] - Derive encryption key from passphrase.
|
|
20
|
+
* @param {Object} [options.kdfOptions] - KDF options when using passphrase.
|
|
21
|
+
* @param {{ algorithm: 'gzip' }} [options.compression] - Enable compression.
|
|
22
|
+
* @param {Array<{label: string, key: Buffer}>} [options.recipients] - Envelope recipients.
|
|
23
|
+
* @returns {Promise<import('../../domain/value-objects/Manifest.js').default>} The resulting manifest.
|
|
24
|
+
*/
|
|
25
|
+
export async function storeFile(service, { filePath, slug, filename, encryptionKey, passphrase, kdfOptions, compression, recipients }) {
|
|
26
|
+
const source = createReadStream(filePath);
|
|
27
|
+
return await service.store({
|
|
28
|
+
source,
|
|
29
|
+
slug,
|
|
30
|
+
filename: filename || path.basename(filePath),
|
|
31
|
+
encryptionKey,
|
|
32
|
+
passphrase,
|
|
33
|
+
kdfOptions,
|
|
34
|
+
compression,
|
|
35
|
+
recipients,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Restores a file from its manifest and writes it to disk via the given
|
|
41
|
+
* {@link import('../../domain/services/CasService.js').default CasService}.
|
|
42
|
+
*
|
|
43
|
+
* @param {import('../../domain/services/CasService.js').default} service - Initialized CasService.
|
|
44
|
+
* @param {Object} options
|
|
45
|
+
* @param {import('../../domain/value-objects/Manifest.js').default} options.manifest - The file manifest.
|
|
46
|
+
* @param {Buffer} [options.encryptionKey] - 32-byte key, required if manifest is encrypted.
|
|
47
|
+
* @param {string} [options.passphrase] - Passphrase for KDF-based decryption.
|
|
48
|
+
* @param {string} options.outputPath - Destination file path.
|
|
49
|
+
* @returns {Promise<{ bytesWritten: number }>}
|
|
50
|
+
*/
|
|
51
|
+
export async function restoreFile(service, { manifest, encryptionKey, passphrase, outputPath }) {
|
|
52
|
+
const iterable = service.restoreStream({ manifest, encryptionKey, passphrase });
|
|
53
|
+
const readable = Readable.from(iterable);
|
|
54
|
+
const writable = createWriteStream(outputPath);
|
|
55
|
+
let bytesWritten = 0;
|
|
56
|
+
const counter = new Transform({
|
|
57
|
+
transform(chunk, _encoding, cb) {
|
|
58
|
+
bytesWritten += chunk.length;
|
|
59
|
+
cb(null, chunk);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
await pipeline(readable, counter, writable);
|
|
63
|
+
return { bytesWritten };
|
|
64
|
+
}
|
|
@@ -30,7 +30,11 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
30
30
|
this.policy = policy ?? DEFAULT_POLICY;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* @override
|
|
35
|
+
* @param {Buffer|string} content - Data to store.
|
|
36
|
+
* @returns {Promise<string>} The Git OID of the stored blob.
|
|
37
|
+
*/
|
|
34
38
|
async writeBlob(content) {
|
|
35
39
|
return this.policy.execute(() =>
|
|
36
40
|
this.plumbing.execute({
|
|
@@ -40,7 +44,11 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
40
44
|
);
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* @override
|
|
49
|
+
* @param {string[]} entries - Lines in `git mktree` format.
|
|
50
|
+
* @returns {Promise<string>} The Git OID of the created tree.
|
|
51
|
+
*/
|
|
44
52
|
async writeTree(entries) {
|
|
45
53
|
return this.policy.execute(() =>
|
|
46
54
|
this.plumbing.execute({
|
|
@@ -50,7 +58,11 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
50
58
|
);
|
|
51
59
|
}
|
|
52
60
|
|
|
53
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* @override
|
|
63
|
+
* @param {string} oid - Git object ID.
|
|
64
|
+
* @returns {Promise<Buffer>} The blob content.
|
|
65
|
+
*/
|
|
54
66
|
async readBlob(oid) {
|
|
55
67
|
return this.policy.execute(async () => {
|
|
56
68
|
const stream = await this.plumbing.executeStream({
|
|
@@ -62,7 +74,11 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
62
74
|
});
|
|
63
75
|
}
|
|
64
76
|
|
|
65
|
-
/**
|
|
77
|
+
/**
|
|
78
|
+
* @override
|
|
79
|
+
* @param {string} treeOid - Git tree OID.
|
|
80
|
+
* @returns {Promise<Array<{ mode: string, type: string, oid: string, name: string }>>}
|
|
81
|
+
*/
|
|
66
82
|
async readTree(treeOid) {
|
|
67
83
|
return this.policy.execute(async () => {
|
|
68
84
|
const output = await this.plumbing.execute({
|
|
@@ -73,7 +89,7 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
73
89
|
return [];
|
|
74
90
|
}
|
|
75
91
|
|
|
76
|
-
return output.split('\0').filter(Boolean).map((entry) => {
|
|
92
|
+
return output.split('\0').filter(Boolean).map((/** @type {string} */ entry) => {
|
|
77
93
|
// Format: <mode> <type> <oid>\t<name>
|
|
78
94
|
const tabIndex = entry.indexOf('\t');
|
|
79
95
|
if (tabIndex === -1) {
|
|
@@ -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
|
}
|
|
@@ -1,23 +1,37 @@
|
|
|
1
1
|
import { createHash, createCipheriv, createDecipheriv, randomBytes, pbkdf2, scrypt } from 'node:crypto';
|
|
2
2
|
import { promisify } from 'node:util';
|
|
3
3
|
import CryptoPort from '../../ports/CryptoPort.js';
|
|
4
|
+
import CasError from '../../domain/errors/CasError.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Node.js implementation of CryptoPort using node:crypto.
|
|
7
8
|
*/
|
|
8
9
|
export default class NodeCryptoAdapter extends CryptoPort {
|
|
9
|
-
/**
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* @override
|
|
12
|
+
* @param {Buffer|Uint8Array} buf - Data to hash.
|
|
13
|
+
* @returns {Promise<string>} 64-char hex digest.
|
|
14
|
+
*/
|
|
15
|
+
async sha256(buf) {
|
|
11
16
|
return createHash('sha256').update(buf).digest('hex');
|
|
12
17
|
}
|
|
13
18
|
|
|
14
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* @override
|
|
21
|
+
* @param {number} n - Number of random bytes.
|
|
22
|
+
* @returns {Buffer}
|
|
23
|
+
*/
|
|
15
24
|
randomBytes(n) {
|
|
16
25
|
return randomBytes(n);
|
|
17
26
|
}
|
|
18
27
|
|
|
19
|
-
/**
|
|
20
|
-
|
|
28
|
+
/**
|
|
29
|
+
* @override
|
|
30
|
+
* @param {Buffer|Uint8Array} buffer - Plaintext to encrypt.
|
|
31
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
32
|
+
* @returns {Promise<{ buf: Buffer, meta: import('../../ports/CryptoPort.js').EncryptionMeta }>}
|
|
33
|
+
*/
|
|
34
|
+
async encryptBuffer(buffer, key) {
|
|
21
35
|
this._validateKey(key);
|
|
22
36
|
const nonce = randomBytes(12);
|
|
23
37
|
const cipher = createCipheriv('aes-256-gcm', key, nonce);
|
|
@@ -29,8 +43,15 @@ export default class NodeCryptoAdapter extends CryptoPort {
|
|
|
29
43
|
};
|
|
30
44
|
}
|
|
31
45
|
|
|
32
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* @override
|
|
48
|
+
* @param {Buffer|Uint8Array} buffer - Ciphertext to decrypt.
|
|
49
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
50
|
+
* @param {import('../../ports/CryptoPort.js').EncryptionMeta} meta - Encryption metadata.
|
|
51
|
+
* @returns {Buffer}
|
|
52
|
+
*/
|
|
33
53
|
decryptBuffer(buffer, key, meta) {
|
|
54
|
+
this._validateKey(key);
|
|
34
55
|
const nonce = Buffer.from(meta.nonce, 'base64');
|
|
35
56
|
const tag = Buffer.from(meta.tag, 'base64');
|
|
36
57
|
const decipher = createDecipheriv('aes-256-gcm', key, nonce);
|
|
@@ -38,12 +59,18 @@ export default class NodeCryptoAdapter extends CryptoPort {
|
|
|
38
59
|
return Buffer.concat([decipher.update(buffer), decipher.final()]);
|
|
39
60
|
}
|
|
40
61
|
|
|
41
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* @override
|
|
64
|
+
* @param {Buffer|Uint8Array} key - 32-byte encryption key.
|
|
65
|
+
* @returns {{ encrypt: (source: AsyncIterable<Buffer>) => AsyncIterable<Buffer>, finalize: () => import('../../ports/CryptoPort.js').EncryptionMeta }}
|
|
66
|
+
*/
|
|
42
67
|
createEncryptionStream(key) {
|
|
43
68
|
this._validateKey(key);
|
|
44
69
|
const nonce = randomBytes(12);
|
|
45
70
|
const cipher = createCipheriv('aes-256-gcm', key, nonce);
|
|
71
|
+
let streamFinalized = false;
|
|
46
72
|
|
|
73
|
+
/** @param {AsyncIterable<Buffer>} source */
|
|
47
74
|
const encrypt = async function* (source) {
|
|
48
75
|
for await (const chunk of source) {
|
|
49
76
|
const encrypted = cipher.update(chunk);
|
|
@@ -55,9 +82,16 @@ export default class NodeCryptoAdapter extends CryptoPort {
|
|
|
55
82
|
if (final.length > 0) {
|
|
56
83
|
yield final;
|
|
57
84
|
}
|
|
85
|
+
streamFinalized = true;
|
|
58
86
|
};
|
|
59
87
|
|
|
60
88
|
const finalize = () => {
|
|
89
|
+
if (!streamFinalized) {
|
|
90
|
+
throw new CasError(
|
|
91
|
+
'Cannot finalize before the encrypt stream is fully consumed',
|
|
92
|
+
'STREAM_NOT_CONSUMED',
|
|
93
|
+
);
|
|
94
|
+
}
|
|
61
95
|
const tag = cipher.getAuthTag();
|
|
62
96
|
return this._buildMeta(nonce.toString('base64'), tag.toString('base64'));
|
|
63
97
|
};
|
|
@@ -65,11 +99,18 @@ export default class NodeCryptoAdapter extends CryptoPort {
|
|
|
65
99
|
return { encrypt, finalize };
|
|
66
100
|
}
|
|
67
101
|
|
|
68
|
-
/**
|
|
102
|
+
/**
|
|
103
|
+
* @override
|
|
104
|
+
* @param {string} passphrase - The passphrase.
|
|
105
|
+
* @param {Buffer|Uint8Array} saltBuf - Salt bytes.
|
|
106
|
+
* @param {import('../../ports/CryptoPort.js').DeriveKeyParams} params - KDF parameters.
|
|
107
|
+
* @returns {Promise<Buffer>}
|
|
108
|
+
*/
|
|
69
109
|
async _doDeriveKey(passphrase, saltBuf, { algorithm, iterations, cost, blockSize, parallelization, keyLength }) {
|
|
70
110
|
if (algorithm === 'pbkdf2') {
|
|
71
111
|
return await promisify(pbkdf2)(passphrase, saltBuf, iterations, keyLength, 'sha512');
|
|
72
112
|
}
|
|
113
|
+
// @ts-ignore -- promisify(scrypt) accepts options as 4th arg at runtime
|
|
73
114
|
return await promisify(scrypt)(passphrase, saltBuf, keyLength, {
|
|
74
115
|
N: cost,
|
|
75
116
|
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
|
}
|