@git-stunts/git-cas 6.5.0 → 6.5.2
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/ARCHITECTURE.md +8 -1
- package/CHANGELOG.md +29 -0
- package/GUIDE.md +19 -3
- package/README.md +16 -4
- package/UPGRADING.md +45 -0
- package/build-info.json +2 -2
- package/docs/API.md +50 -1
- package/docs/releases/v6.5.1.md +44 -0
- package/docs/releases/v6.5.2.md +63 -0
- package/index.d.ts +21 -0
- package/index.js +53 -1
- package/package.json +4 -2
- package/src/domain/errors/Codes.js +2 -0
- package/src/domain/services/PageService.js +154 -4
- package/src/helpers/boundedPromiseCache.js +43 -35
- package/src/infrastructure/adapters/GitObjectSessionPool.js +333 -0
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +463 -69
- package/src/infrastructure/codecs/GitTreeObjectCodec.js +124 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +26 -0
- package/src/types/ambient.d.ts +46 -1
|
@@ -2,9 +2,12 @@ import { Policy } from '@git-stunts/alfred';
|
|
|
2
2
|
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
3
|
import os from 'node:os';
|
|
4
4
|
import path from 'node:path';
|
|
5
|
+
import { GitObjectMissingError } from '@git-stunts/plumbing';
|
|
5
6
|
import GitPersistencePort from '../../ports/GitPersistencePort.js';
|
|
6
7
|
import { CasError, createCasError, ErrorCodes } from '../../domain/errors/index.js';
|
|
7
8
|
import BoundedPromiseCache from '../../helpers/boundedPromiseCache.js';
|
|
9
|
+
import GitTreeObjectCodec from '../codecs/GitTreeObjectCodec.js';
|
|
10
|
+
import GitObjectSessionPool from './GitObjectSessionPool.js';
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Default resilience policy: 30 s timeout (no retry).
|
|
@@ -17,6 +20,9 @@ import BoundedPromiseCache from '../../helpers/boundedPromiseCache.js';
|
|
|
17
20
|
const DEFAULT_POLICY = Policy.timeout(30_000);
|
|
18
21
|
export const DEFAULT_MAX_BLOB_SIZE = 10 * 1024 * 1024;
|
|
19
22
|
const DEFAULT_METADATA_CACHE_ENTRIES = 2_048;
|
|
23
|
+
const DEFAULT_SESSION_IDLE_TIMEOUT_MS = 1_000;
|
|
24
|
+
const DEFAULT_TREE_CACHE_BYTES = 8 * 1024 * 1024;
|
|
25
|
+
const DEFAULT_TREE_CACHE_ENTRIES = 256;
|
|
20
26
|
const MIN_READ_BLOB_LIMIT = 1;
|
|
21
27
|
const MIN_MAX_BLOB_SIZE = 1024;
|
|
22
28
|
const MIN_METADATA_CACHE_ENTRIES = 1;
|
|
@@ -33,20 +39,46 @@ const GIT_OBJECT_TYPES = new Set(['blob', 'tree', 'commit', 'tag']);
|
|
|
33
39
|
* must not race active operations.
|
|
34
40
|
*/
|
|
35
41
|
export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
42
|
+
#activeOperations = new Set();
|
|
43
|
+
#activeStreams = new Set();
|
|
44
|
+
#closePromise = null;
|
|
45
|
+
#closed = false;
|
|
36
46
|
#maxBlobSize = DEFAULT_MAX_BLOB_SIZE;
|
|
37
47
|
#metadataCache;
|
|
48
|
+
#sessions;
|
|
49
|
+
#treeCache;
|
|
50
|
+
#treeReadMaxBytes;
|
|
38
51
|
/**
|
|
39
52
|
* @param {Object} options
|
|
40
53
|
* @param {import('@git-stunts/plumbing').default} options.plumbing - GitPlumbing instance.
|
|
41
54
|
* @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy (defaults to 30 s timeout, no retry).
|
|
42
55
|
* @param {number} [options.metadataCacheEntries=2048] - Maximum immutable metadata entries retained by this adapter.
|
|
56
|
+
* @param {number} [options.sessionIdleTimeoutMs=1000] - Idle delay before a reusable Git process closes automatically.
|
|
57
|
+
* @param {number} [options.treeCacheEntries=256] - Maximum immutable parsed tree objects retained by this adapter.
|
|
58
|
+
* @param {number} [options.treeCacheBytes=8388608] - Maximum estimated parsed tree bytes retained by this adapter.
|
|
43
59
|
*/
|
|
44
|
-
constructor({
|
|
60
|
+
constructor({
|
|
61
|
+
plumbing,
|
|
62
|
+
policy,
|
|
63
|
+
metadataCacheEntries = DEFAULT_METADATA_CACHE_ENTRIES,
|
|
64
|
+
sessionIdleTimeoutMs = DEFAULT_SESSION_IDLE_TIMEOUT_MS,
|
|
65
|
+
treeCacheEntries = DEFAULT_TREE_CACHE_ENTRIES,
|
|
66
|
+
treeCacheBytes = DEFAULT_TREE_CACHE_BYTES,
|
|
67
|
+
}) {
|
|
45
68
|
super();
|
|
46
69
|
GitPersistenceAdapter.#assertMetadataCacheEntries(metadataCacheEntries);
|
|
70
|
+
GitPersistenceAdapter.#assertSessionIdleTimeout(sessionIdleTimeoutMs);
|
|
71
|
+
GitPersistenceAdapter.#assertTreeCacheEntries(treeCacheEntries);
|
|
72
|
+
GitPersistenceAdapter.#assertTreeCacheBytes(treeCacheBytes);
|
|
47
73
|
this.plumbing = plumbing;
|
|
48
74
|
this.policy = policy ?? DEFAULT_POLICY;
|
|
49
75
|
this.#metadataCache = new BoundedPromiseCache(metadataCacheEntries);
|
|
76
|
+
this.#sessions = new GitObjectSessionPool({ plumbing, idleTimeoutMs: sessionIdleTimeoutMs });
|
|
77
|
+
this.#treeCache = new BoundedPromiseCache(treeCacheEntries, {
|
|
78
|
+
maxWeight: treeCacheBytes,
|
|
79
|
+
weightOf: (tree) => tree.weight,
|
|
80
|
+
});
|
|
81
|
+
this.#treeReadMaxBytes = Math.max(MIN_READ_BLOB_LIMIT, treeCacheBytes);
|
|
50
82
|
}
|
|
51
83
|
|
|
52
84
|
/**
|
|
@@ -55,14 +87,69 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
55
87
|
* @returns {Promise<string>} The Git OID of the stored blob.
|
|
56
88
|
*/
|
|
57
89
|
async writeBlob(content) {
|
|
58
|
-
return this
|
|
90
|
+
return await this.#runOperation(() => this.#writeBlob(content));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async #writeBlob(content) {
|
|
94
|
+
const oid = await this.policy.execute(() =>
|
|
59
95
|
typeof globalThis.Bun !== 'undefined'
|
|
60
96
|
? this.#writeBlobFromTempFile(content)
|
|
61
97
|
: this.plumbing.execute({
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
)
|
|
98
|
+
args: ['hash-object', '-w', '--stdin'],
|
|
99
|
+
input: content,
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
await this.#retireSessions(['catFile', 'mktree']);
|
|
103
|
+
return oid;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Writes a bounded group through one scoped fast-import session when the
|
|
108
|
+
* injected plumbing supports it. The session closes before OIDs are exposed,
|
|
109
|
+
* so later pruning cannot poison duplicate writes in a reused process.
|
|
110
|
+
* @override
|
|
111
|
+
* @param {Iterable<Uint8Array|string>} contents
|
|
112
|
+
* @returns {Promise<string[]>}
|
|
113
|
+
*/
|
|
114
|
+
async writeBlobs(contents) {
|
|
115
|
+
return await this.#runOperation(async () => {
|
|
116
|
+
const replayableContents = [...contents];
|
|
117
|
+
if (!this.#sessions.supports('fastImport')) {
|
|
118
|
+
const oids = [];
|
|
119
|
+
for (const content of replayableContents) {
|
|
120
|
+
oids.push(await this.#writeBlob(content));
|
|
121
|
+
}
|
|
122
|
+
return oids;
|
|
123
|
+
}
|
|
124
|
+
let result;
|
|
125
|
+
let operationFailed = false;
|
|
126
|
+
let operationError;
|
|
127
|
+
try {
|
|
128
|
+
const oids = await this.#sessions.writeBlobs(replayableContents, (operation) =>
|
|
129
|
+
this.policy.execute(operation)
|
|
130
|
+
);
|
|
131
|
+
await this.#retireSessions(['catFile', 'mktree']);
|
|
132
|
+
result = [...oids];
|
|
133
|
+
} catch (error) {
|
|
134
|
+
operationFailed = true;
|
|
135
|
+
operationError = error;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
await this.#sessions.retire('fastImport');
|
|
139
|
+
} catch (retirementError) {
|
|
140
|
+
if (operationFailed) {
|
|
141
|
+
throw new AggregateError(
|
|
142
|
+
[operationError, retirementError],
|
|
143
|
+
'Bulk Git object write and session retirement both failed'
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
throw retirementError;
|
|
147
|
+
}
|
|
148
|
+
if (operationFailed) {
|
|
149
|
+
throw operationError;
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
});
|
|
66
153
|
}
|
|
67
154
|
|
|
68
155
|
/**
|
|
@@ -71,12 +158,24 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
71
158
|
* @returns {Promise<string>} The Git OID of the created tree.
|
|
72
159
|
*/
|
|
73
160
|
async writeTree(entries) {
|
|
74
|
-
return this
|
|
75
|
-
this.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
161
|
+
return await this.#runOperation(async () => {
|
|
162
|
+
if (this.#sessions.supports('mktree')) {
|
|
163
|
+
const structured = GitTreeObjectCodec.parseMktreeLines(entries);
|
|
164
|
+
const oid = await this.#sessions.writeTree(structured, (operation) =>
|
|
165
|
+
this.policy.execute(operation)
|
|
166
|
+
);
|
|
167
|
+
await this.#sessions.retire('catFile');
|
|
168
|
+
return oid;
|
|
169
|
+
}
|
|
170
|
+
const oid = await this.policy.execute(() =>
|
|
171
|
+
this.plumbing.execute({
|
|
172
|
+
args: ['mktree'],
|
|
173
|
+
input: `${entries.join('\n')}\n`,
|
|
174
|
+
})
|
|
175
|
+
);
|
|
176
|
+
await this.#sessions.retire('catFile');
|
|
177
|
+
return oid;
|
|
178
|
+
});
|
|
80
179
|
}
|
|
81
180
|
|
|
82
181
|
/**
|
|
@@ -86,23 +185,43 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
86
185
|
* @returns {Promise<Buffer>} The blob content.
|
|
87
186
|
*/
|
|
88
187
|
async readBlob(oid, maxBytes) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
188
|
+
return await this.#runOperation(async () => {
|
|
189
|
+
const limit =
|
|
190
|
+
maxBytes === undefined
|
|
191
|
+
? this.#maxBlobSize
|
|
192
|
+
: GitPersistenceAdapter.#validatedReadBlobLimit(maxBytes);
|
|
193
|
+
if (this.#sessions.supports('catFile')) {
|
|
194
|
+
let object;
|
|
195
|
+
try {
|
|
196
|
+
object = await this.#sessions.read(oid, { maxBytes: limit }, (operation) =>
|
|
197
|
+
this.policy.execute(operation)
|
|
198
|
+
);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
throw this.#normalizeObjectReadError(error, oid, limit);
|
|
201
|
+
}
|
|
202
|
+
if (object.type !== 'blob') {
|
|
203
|
+
throw createCasError(`Git object is not a blob: ${oid}`, ErrorCodes.GIT_ERROR, {
|
|
204
|
+
oid,
|
|
205
|
+
actualType: object.type,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
return GitPersistenceAdapter.#toBuffer(object.content);
|
|
102
209
|
}
|
|
103
|
-
chunks
|
|
104
|
-
|
|
105
|
-
|
|
210
|
+
const chunks = [];
|
|
211
|
+
let bytesRead = 0;
|
|
212
|
+
for await (const chunk of await this.#openBufferStream(['cat-file', 'blob', oid])) {
|
|
213
|
+
bytesRead += chunk.length;
|
|
214
|
+
if (bytesRead > limit) {
|
|
215
|
+
throw new CasError(
|
|
216
|
+
`Blob ${oid} exceeds safety limit of ${limit} bytes`,
|
|
217
|
+
ErrorCodes.RESTORE_TOO_LARGE,
|
|
218
|
+
{ oid, maxBytes: limit }
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
chunks.push(chunk);
|
|
222
|
+
}
|
|
223
|
+
return Buffer.concat(chunks);
|
|
224
|
+
});
|
|
106
225
|
}
|
|
107
226
|
|
|
108
227
|
/**
|
|
@@ -113,6 +232,7 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
113
232
|
* @returns {void}
|
|
114
233
|
*/
|
|
115
234
|
setMaxBlobSize(maxBlobSize) {
|
|
235
|
+
this.#assertOpen();
|
|
116
236
|
GitPersistenceAdapter.#assertMaxBlobSize(maxBlobSize);
|
|
117
237
|
this.#maxBlobSize = maxBlobSize;
|
|
118
238
|
}
|
|
@@ -123,13 +243,7 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
123
243
|
* @returns {Promise<AsyncIterable<Buffer>>} The blob content stream.
|
|
124
244
|
*/
|
|
125
245
|
async readBlobStream(oid) {
|
|
126
|
-
|
|
127
|
-
await this.plumbing.executeStream({
|
|
128
|
-
args: ['cat-file', 'blob', oid],
|
|
129
|
-
})
|
|
130
|
-
));
|
|
131
|
-
|
|
132
|
-
return this.#bufferStream(stream);
|
|
246
|
+
return await this.#runOperation(() => this.#openBufferStream(['cat-file', 'blob', oid]));
|
|
133
247
|
}
|
|
134
248
|
|
|
135
249
|
/**
|
|
@@ -138,6 +252,22 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
138
252
|
* @returns {Promise<Array<{ mode: string, type: string, oid: string, name: string }>>}
|
|
139
253
|
*/
|
|
140
254
|
async readTree(treeOid) {
|
|
255
|
+
return await this.#runOperation(async () => {
|
|
256
|
+
if (this.#sessions.supports('catFile')) {
|
|
257
|
+
try {
|
|
258
|
+
const tree = await this.#readTreeObject(treeOid);
|
|
259
|
+
return tree.entries.map((entry) => ({ ...entry }));
|
|
260
|
+
} catch (error) {
|
|
261
|
+
if (!GitPersistenceAdapter.#isObjectBufferLimit(error)) {
|
|
262
|
+
throw this.#normalizeTreeReadError(error, treeOid);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return await this.#readTreeWithCommand(treeOid);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async #readTreeWithCommand(treeOid) {
|
|
141
271
|
return this.policy.execute(async () => {
|
|
142
272
|
const output = await this.plumbing.execute({
|
|
143
273
|
args: ['ls-tree', '-z', treeOid],
|
|
@@ -154,15 +284,35 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
154
284
|
* @returns {Promise<{ mode: string, type: string, oid: string, name: string }|null>}
|
|
155
285
|
*/
|
|
156
286
|
async readTreeEntry(treeOid, treePath) {
|
|
157
|
-
|
|
158
|
-
|
|
287
|
+
return await this.#runOperation(async () => {
|
|
288
|
+
const key = `tree\0${treeOid}\0${treePath}`;
|
|
289
|
+
const entry = await this.#metadataCache.getOrCreate(key, async () => {
|
|
290
|
+
let found;
|
|
291
|
+
if (this.#sessions.supports('catFile')) {
|
|
292
|
+
try {
|
|
293
|
+
found = await this.#readExactTreePath(treeOid, treePath);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (!GitPersistenceAdapter.#isObjectBufferLimit(error)) {
|
|
296
|
+
throw this.#normalizeTreeReadError(error, treeOid);
|
|
297
|
+
}
|
|
298
|
+
found = await this.#readTreeEntryWithCommand(treeOid, treePath);
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
found = await this.#readTreeEntryWithCommand(treeOid, treePath);
|
|
302
|
+
}
|
|
303
|
+
return found === null ? null : Object.freeze(found);
|
|
304
|
+
});
|
|
305
|
+
return entry === null ? null : { ...entry };
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async #readTreeEntryWithCommand(treeOid, treePath) {
|
|
310
|
+
return this.policy.execute(async () => {
|
|
159
311
|
const output = await this.plumbing.execute({
|
|
160
312
|
args: ['ls-tree', '-z', treeOid, '--', treePath],
|
|
161
313
|
});
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}));
|
|
165
|
-
return entry === null ? null : { ...entry };
|
|
314
|
+
return GitPersistenceAdapter.#parseTreeOutput(output)[0] || null;
|
|
315
|
+
});
|
|
166
316
|
}
|
|
167
317
|
|
|
168
318
|
/**
|
|
@@ -171,11 +321,9 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
171
321
|
* @returns {AsyncIterable<{ mode: string, type: string, oid: string, name: string }>}
|
|
172
322
|
*/
|
|
173
323
|
async *iterateTree(treeOid) {
|
|
174
|
-
const stream = await this
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
})
|
|
178
|
-
));
|
|
324
|
+
const stream = await this.#runOperation(() =>
|
|
325
|
+
this.#openBufferStream(['ls-tree', '-z', treeOid])
|
|
326
|
+
);
|
|
179
327
|
let pending = '';
|
|
180
328
|
for await (const chunk of stream) {
|
|
181
329
|
pending += GitPersistenceAdapter.#toBuffer(chunk).toString();
|
|
@@ -200,7 +348,7 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
200
348
|
* @returns {Promise<string>} Git object type.
|
|
201
349
|
*/
|
|
202
350
|
async readObjectType(oid) {
|
|
203
|
-
return (await this.#readObjectInfo(oid)).type;
|
|
351
|
+
return await this.#runOperation(async () => (await this.#readObjectInfo(oid)).type);
|
|
204
352
|
}
|
|
205
353
|
|
|
206
354
|
/**
|
|
@@ -209,7 +357,7 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
209
357
|
* @returns {Promise<number>} Git object size in bytes.
|
|
210
358
|
*/
|
|
211
359
|
async readObjectSize(oid) {
|
|
212
|
-
return (await this.#readObjectInfo(oid)).size;
|
|
360
|
+
return await this.#runOperation(async () => (await this.#readObjectInfo(oid)).size);
|
|
213
361
|
}
|
|
214
362
|
|
|
215
363
|
/**
|
|
@@ -222,10 +370,22 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
222
370
|
*/
|
|
223
371
|
async #readObjectInfo(oid) {
|
|
224
372
|
return this.#metadataCache.getOrCreate(`object\0${oid}`, async () => {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
373
|
+
if (this.#sessions.supports('catFile')) {
|
|
374
|
+
try {
|
|
375
|
+
const info = await this.#sessions.info(oid, (operation) =>
|
|
376
|
+
this.policy.execute(operation)
|
|
377
|
+
);
|
|
378
|
+
return Object.freeze({ oid: info.oid, type: info.type, size: info.size });
|
|
379
|
+
} catch (error) {
|
|
380
|
+
throw this.#normalizeObjectReadError(error, oid, this.#maxBlobSize);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const rawOutput = await this.policy.execute(() =>
|
|
384
|
+
this.plumbing.execute({
|
|
385
|
+
args: ['cat-file', OBJECT_INFO_ARGUMENT],
|
|
386
|
+
input: `${oid}\n`,
|
|
387
|
+
})
|
|
388
|
+
);
|
|
229
389
|
const output = typeof rawOutput === 'string' ? rawOutput.trim() : '';
|
|
230
390
|
if (output === `${oid} missing`) {
|
|
231
391
|
throw new CasError(`Git object not found: ${oid}`, ErrorCodes.GIT_OBJECT_NOT_FOUND, {
|
|
@@ -251,6 +411,167 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
251
411
|
});
|
|
252
412
|
}
|
|
253
413
|
|
|
414
|
+
async #readTreeObject(treeOid) {
|
|
415
|
+
return this.#treeCache.getOrCreate(treeOid, async () => {
|
|
416
|
+
const object = await this.#sessions.read(
|
|
417
|
+
treeOid,
|
|
418
|
+
{ maxBytes: this.#treeReadMaxBytes },
|
|
419
|
+
(operation) => this.policy.execute(operation)
|
|
420
|
+
);
|
|
421
|
+
if (object.type !== 'tree') {
|
|
422
|
+
throw createCasError(`Git object is not a tree: ${treeOid}`, ErrorCodes.TREE_PARSE_ERROR, {
|
|
423
|
+
treeOid,
|
|
424
|
+
actualType: object.type,
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
try {
|
|
428
|
+
return GitTreeObjectCodec.decode(object.content, treeOid);
|
|
429
|
+
} catch (error) {
|
|
430
|
+
throw createCasError(
|
|
431
|
+
`Git tree object is malformed: ${treeOid}`,
|
|
432
|
+
ErrorCodes.TREE_PARSE_ERROR,
|
|
433
|
+
{ treeOid, originalError: error }
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
async #readExactTreePath(treeOid, treePath) {
|
|
440
|
+
if (typeof treePath !== 'string' || treePath.length === 0) {
|
|
441
|
+
throw createCasError('Git tree path must be a non-empty string', ErrorCodes.INVALID_OPTIONS, {
|
|
442
|
+
treeOid,
|
|
443
|
+
treePath,
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
const components = treePath.split('/');
|
|
447
|
+
if (components.some((component) => component.length === 0)) {
|
|
448
|
+
throw createCasError(
|
|
449
|
+
'Git tree path contains an empty component',
|
|
450
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
451
|
+
{
|
|
452
|
+
treeOid,
|
|
453
|
+
treePath,
|
|
454
|
+
}
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
let currentOid = treeOid;
|
|
459
|
+
for (let index = 0; index < components.length; index += 1) {
|
|
460
|
+
const tree = await this.#readTreeObject(currentOid);
|
|
461
|
+
const entry = tree.entries.find((candidate) => candidate.name === components[index]) ?? null;
|
|
462
|
+
if (entry === null) {
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
if (index === components.length - 1) {
|
|
466
|
+
return { ...entry, name: treePath };
|
|
467
|
+
}
|
|
468
|
+
if (entry.type !== 'tree') {
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
currentOid = entry.oid;
|
|
472
|
+
}
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
#normalizeObjectReadError(error, oid, maxBytes) {
|
|
477
|
+
if (error instanceof GitObjectMissingError) {
|
|
478
|
+
return new CasError(`Git object not found: ${oid}`, ErrorCodes.GIT_OBJECT_NOT_FOUND, { oid });
|
|
479
|
+
}
|
|
480
|
+
if (GitPersistenceAdapter.#isObjectBufferLimit(error)) {
|
|
481
|
+
return new CasError(
|
|
482
|
+
`Blob ${oid} exceeds safety limit of ${maxBytes} bytes`,
|
|
483
|
+
ErrorCodes.RESTORE_TOO_LARGE,
|
|
484
|
+
{ oid, maxBytes }
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
return error;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
#normalizeTreeReadError(error, treeOid) {
|
|
491
|
+
if (error instanceof GitObjectMissingError) {
|
|
492
|
+
return new CasError(`Git object not found: ${treeOid}`, ErrorCodes.GIT_OBJECT_NOT_FOUND, {
|
|
493
|
+
oid: treeOid,
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
return error;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
async close() {
|
|
500
|
+
if (this.#closePromise !== null) {
|
|
501
|
+
return await this.#closePromise;
|
|
502
|
+
}
|
|
503
|
+
this.#closed = true;
|
|
504
|
+
this.#closePromise = (async () => {
|
|
505
|
+
const failures = [];
|
|
506
|
+
const active = [...this.#activeOperations];
|
|
507
|
+
await Promise.allSettled(active);
|
|
508
|
+
|
|
509
|
+
const streams = [...this.#activeStreams];
|
|
510
|
+
const streamResults = await Promise.allSettled(
|
|
511
|
+
streams.map((stream) => GitPersistenceAdapter.#closeStream(stream))
|
|
512
|
+
);
|
|
513
|
+
this.#activeStreams.clear();
|
|
514
|
+
failures.push(
|
|
515
|
+
...streamResults
|
|
516
|
+
.filter((result) => result.status === 'rejected')
|
|
517
|
+
.map((result) => result.reason)
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
try {
|
|
521
|
+
await this.#sessions.close();
|
|
522
|
+
} catch (error) {
|
|
523
|
+
failures.push(error);
|
|
524
|
+
} finally {
|
|
525
|
+
this.#metadataCache = null;
|
|
526
|
+
this.#treeCache = null;
|
|
527
|
+
}
|
|
528
|
+
if (failures.length > 0) {
|
|
529
|
+
throw new AggregateError(failures, 'Git persistence adapter failed to close cleanly');
|
|
530
|
+
}
|
|
531
|
+
})();
|
|
532
|
+
return await this.#closePromise;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
async [Symbol.asyncDispose]() {
|
|
536
|
+
await this.close();
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
#assertOpen() {
|
|
540
|
+
if (this.#closed) {
|
|
541
|
+
throw createCasError('Git persistence adapter is closed', ErrorCodes.RESOURCE_CLOSED);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
#runOperation(operation) {
|
|
546
|
+
this.#assertOpen();
|
|
547
|
+
const promise = Promise.resolve().then(operation);
|
|
548
|
+
this.#activeOperations.add(promise);
|
|
549
|
+
void promise.then(
|
|
550
|
+
() => this.#activeOperations.delete(promise),
|
|
551
|
+
() => this.#activeOperations.delete(promise)
|
|
552
|
+
);
|
|
553
|
+
return promise;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
async #retireSessions(protocols) {
|
|
557
|
+
const results = await Promise.allSettled(
|
|
558
|
+
protocols.map((protocol) => this.#sessions.retire(protocol))
|
|
559
|
+
);
|
|
560
|
+
const failures = results
|
|
561
|
+
.filter((result) => result.status === 'rejected')
|
|
562
|
+
.map((result) => result.reason);
|
|
563
|
+
if (failures.length === 1) {
|
|
564
|
+
throw failures[0];
|
|
565
|
+
}
|
|
566
|
+
if (failures.length > 1) {
|
|
567
|
+
throw new AggregateError(failures, 'Multiple Git object sessions failed to retire');
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
static #isObjectBufferLimit(error) {
|
|
572
|
+
return error?.details?.code === 'OBJECT_BUFFER_LIMIT_EXCEEDED';
|
|
573
|
+
}
|
|
574
|
+
|
|
254
575
|
/**
|
|
255
576
|
* @param {number} metadataCacheEntries
|
|
256
577
|
*/
|
|
@@ -264,7 +585,40 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
264
585
|
throw createCasError(
|
|
265
586
|
'Git metadata cache entries must be a positive safe integer',
|
|
266
587
|
ErrorCodes.INVALID_OPTIONS,
|
|
267
|
-
{ option: 'metadataCacheEntries', metadataCacheEntries }
|
|
588
|
+
{ option: 'metadataCacheEntries', metadataCacheEntries }
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
static #assertTreeCacheEntries(treeCacheEntries) {
|
|
593
|
+
if (Number.isSafeInteger(treeCacheEntries) && treeCacheEntries >= 1) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
throw createCasError(
|
|
597
|
+
'Git tree cache entries must be a positive safe integer',
|
|
598
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
599
|
+
{ option: 'treeCacheEntries', treeCacheEntries }
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
static #assertSessionIdleTimeout(sessionIdleTimeoutMs) {
|
|
604
|
+
if (Number.isSafeInteger(sessionIdleTimeoutMs) && sessionIdleTimeoutMs >= 1) {
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
throw createCasError(
|
|
608
|
+
'Git session idle timeout must be a positive safe integer',
|
|
609
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
610
|
+
{ option: 'sessionIdleTimeoutMs', sessionIdleTimeoutMs }
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
static #assertTreeCacheBytes(treeCacheBytes) {
|
|
615
|
+
if (Number.isSafeInteger(treeCacheBytes) && treeCacheBytes >= 0) {
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
throw createCasError(
|
|
619
|
+
'Git tree cache bytes must be a non-negative safe integer',
|
|
620
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
621
|
+
{ option: 'treeCacheBytes', treeCacheBytes }
|
|
268
622
|
);
|
|
269
623
|
}
|
|
270
624
|
|
|
@@ -297,8 +651,44 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
297
651
|
* @returns {AsyncIterable<Buffer>}
|
|
298
652
|
*/
|
|
299
653
|
async *#bufferStream(stream) {
|
|
300
|
-
|
|
301
|
-
|
|
654
|
+
try {
|
|
655
|
+
for await (const chunk of stream) {
|
|
656
|
+
yield GitPersistenceAdapter.#toBuffer(chunk);
|
|
657
|
+
}
|
|
658
|
+
} finally {
|
|
659
|
+
try {
|
|
660
|
+
await GitPersistenceAdapter.#closeStream(stream);
|
|
661
|
+
} finally {
|
|
662
|
+
this.#activeStreams.delete(stream);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
async #openBufferStream(args) {
|
|
668
|
+
const stream = await this.policy.execute(
|
|
669
|
+
async () => await this.plumbing.executeStream({ args })
|
|
670
|
+
);
|
|
671
|
+
this.#activeStreams.add(stream);
|
|
672
|
+
return this.#bufferStream(stream);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
static async #closeStream(stream) {
|
|
676
|
+
const operations = [];
|
|
677
|
+
if (typeof stream.destroy === 'function') {
|
|
678
|
+
operations.push(Promise.resolve().then(() => stream.destroy()));
|
|
679
|
+
}
|
|
680
|
+
if (stream.finished !== undefined) {
|
|
681
|
+
operations.push(Promise.resolve(stream.finished));
|
|
682
|
+
}
|
|
683
|
+
const results = await Promise.allSettled(operations);
|
|
684
|
+
const failures = results
|
|
685
|
+
.filter((result) => result.status === 'rejected')
|
|
686
|
+
.map((result) => result.reason);
|
|
687
|
+
if (failures.length === 1) {
|
|
688
|
+
throw failures[0];
|
|
689
|
+
}
|
|
690
|
+
if (failures.length > 1) {
|
|
691
|
+
throw new AggregateError(failures, 'Git output stream failed to terminate cleanly');
|
|
302
692
|
}
|
|
303
693
|
}
|
|
304
694
|
|
|
@@ -321,7 +711,11 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
321
711
|
* @returns {number}
|
|
322
712
|
*/
|
|
323
713
|
static #validatedReadBlobLimit(maxBytes) {
|
|
324
|
-
if (
|
|
714
|
+
if (
|
|
715
|
+
!Number.isSafeInteger(maxBytes) ||
|
|
716
|
+
maxBytes < MIN_READ_BLOB_LIMIT ||
|
|
717
|
+
maxBytes > MAX_BLOB_SIZE_LIMIT
|
|
718
|
+
) {
|
|
325
719
|
throw createCasError(
|
|
326
720
|
`maxBytes must be an integer in [${MIN_READ_BLOB_LIMIT}, ${MAX_BLOB_SIZE_LIMIT}]`,
|
|
327
721
|
ErrorCodes.INVALID_OPTIONS,
|
|
@@ -330,7 +724,7 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
330
724
|
value: maxBytes,
|
|
331
725
|
min: MIN_READ_BLOB_LIMIT,
|
|
332
726
|
max: MAX_BLOB_SIZE_LIMIT,
|
|
333
|
-
}
|
|
727
|
+
}
|
|
334
728
|
);
|
|
335
729
|
}
|
|
336
730
|
return maxBytes;
|
|
@@ -341,7 +735,11 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
341
735
|
* @returns {void}
|
|
342
736
|
*/
|
|
343
737
|
static #assertMaxBlobSize(maxBlobSize) {
|
|
344
|
-
if (
|
|
738
|
+
if (
|
|
739
|
+
!Number.isInteger(maxBlobSize) ||
|
|
740
|
+
maxBlobSize < MIN_MAX_BLOB_SIZE ||
|
|
741
|
+
maxBlobSize > MAX_BLOB_SIZE_LIMIT
|
|
742
|
+
) {
|
|
345
743
|
throw createCasError(
|
|
346
744
|
`maxBlobSize must be an integer in [${MIN_MAX_BLOB_SIZE}, ${MAX_BLOB_SIZE_LIMIT}]`,
|
|
347
745
|
ErrorCodes.INVALID_OPTIONS,
|
|
@@ -350,7 +748,7 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
350
748
|
value: maxBlobSize,
|
|
351
749
|
min: MIN_MAX_BLOB_SIZE,
|
|
352
750
|
max: MAX_BLOB_SIZE_LIMIT,
|
|
353
|
-
}
|
|
751
|
+
}
|
|
354
752
|
);
|
|
355
753
|
}
|
|
356
754
|
}
|
|
@@ -376,19 +774,15 @@ export default class GitPersistenceAdapter extends GitPersistencePort {
|
|
|
376
774
|
static #parseTreeEntry(entry) {
|
|
377
775
|
const tabIndex = entry.indexOf('\t');
|
|
378
776
|
if (tabIndex === -1) {
|
|
379
|
-
throw new CasError(
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
{ rawEntry: entry },
|
|
383
|
-
);
|
|
777
|
+
throw new CasError(`Malformed ls-tree entry: ${entry}`, ErrorCodes.TREE_PARSE_ERROR, {
|
|
778
|
+
rawEntry: entry,
|
|
779
|
+
});
|
|
384
780
|
}
|
|
385
781
|
const meta = entry.slice(0, tabIndex).split(' ');
|
|
386
782
|
if (meta.length !== 3) {
|
|
387
|
-
throw new CasError(
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
{ rawEntry: entry },
|
|
391
|
-
);
|
|
783
|
+
throw new CasError(`Malformed ls-tree entry: ${entry}`, ErrorCodes.TREE_PARSE_ERROR, {
|
|
784
|
+
rawEntry: entry,
|
|
785
|
+
});
|
|
392
786
|
}
|
|
393
787
|
return {
|
|
394
788
|
mode: meta[0],
|