@git-stunts/git-cas 6.5.6 → 6.5.8
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 +27 -0
- package/GUIDE.md +29 -0
- package/README.md +16 -10
- package/UPGRADING.md +48 -0
- package/build-info.json +2 -2
- package/docs/API.md +66 -12
- package/docs/releases/v6.5.7.md +56 -0
- package/docs/releases/v6.5.8.md +89 -0
- package/index.d.ts +50 -0
- package/index.js +15 -1
- package/package.json +4 -2
- package/src/domain/services/AssetService.js +201 -10
- package/src/domain/services/BoundedWriteWavePersistence.js +235 -0
- package/src/domain/services/BundleBatchPlanner.js +349 -0
- package/src/domain/services/BundleService.js +225 -2
- package/src/domain/services/CasService.d.ts +16 -0
- package/src/domain/services/CasService.js +9 -18
- package/src/domain/services/ManifestRepository.js +129 -50
- package/src/domain/services/ManifestTreeService.js +47 -0
- package/src/domain/services/PageService.js +26 -9
- package/src/domain/services/RootSet.js +32 -0
- package/src/domain/services/RootSetPersistence.js +58 -1
- package/src/domain/services/StagedTarget.js +19 -0
- package/src/domain/services/StagingWorkspace.js +21 -7
- package/src/domain/services/forkCasPersistence.js +20 -0
- package/src/infrastructure/adapters/GitObjectSessionPool.js +47 -5
- package/src/infrastructure/adapters/GitPersistenceAdapter.js +217 -16
- package/src/infrastructure/adapters/GitPersistenceWriteScope.js +154 -0
- package/src/infrastructure/adapters/GitRefAdapter.js +38 -3
- package/src/infrastructure/adapters/GitUpdateRefSessionPool.js +115 -0
- package/src/package-version.js +1 -1
- package/src/ports/GitPersistencePort.js +43 -0
- package/src/ports/GitRefPort.js +8 -0
package/index.js
CHANGED
|
@@ -210,6 +210,7 @@ export default class ContentAddressableStore {
|
|
|
210
210
|
});
|
|
211
211
|
this.assets = Object.freeze({
|
|
212
212
|
put: async (options) => (await this.#getAssetService()).put(options),
|
|
213
|
+
putBatch: async (options) => (await this.#getAssetService()).putBatch(options),
|
|
213
214
|
adopt: async (options) => (await this.#getAssetService()).adopt(options),
|
|
214
215
|
open: (options) => this.#openAsset(options),
|
|
215
216
|
});
|
|
@@ -222,6 +223,7 @@ export default class ContentAddressableStore {
|
|
|
222
223
|
this.bundles = Object.freeze({
|
|
223
224
|
put: async (options) => (await this.#getBundleService()).put(options),
|
|
224
225
|
putOrdered: async (options) => (await this.#getBundleService()).putOrdered(options),
|
|
226
|
+
putOrderedBatch: async (options) => (await this.#getBundleService()).putOrderedBatch(options),
|
|
225
227
|
getMember: async (options) => (await this.#getBundleService()).getMember(options),
|
|
226
228
|
getMemberReference: async (options) => (
|
|
227
229
|
await this.#getBundleService()
|
|
@@ -262,6 +264,8 @@ export default class ContentAddressableStore {
|
|
|
262
264
|
#stagingWorkspaceRegistry = null;
|
|
263
265
|
/** @type {RepositoryDoctor|null} */
|
|
264
266
|
#repositoryDoctor = null;
|
|
267
|
+
/** @type {GitRefAdapter|null} */
|
|
268
|
+
#ref = null;
|
|
265
269
|
#closePromise = null;
|
|
266
270
|
#closed = false;
|
|
267
271
|
#servicePromise = null;
|
|
@@ -314,6 +318,7 @@ export default class ContentAddressableStore {
|
|
|
314
318
|
plumbing: cfg.plumbing,
|
|
315
319
|
policy: cfg.policy,
|
|
316
320
|
});
|
|
321
|
+
this.#ref = ref;
|
|
317
322
|
this.#vault = new VaultService({
|
|
318
323
|
persistence,
|
|
319
324
|
ref,
|
|
@@ -627,7 +632,16 @@ export default class ContentAddressableStore {
|
|
|
627
632
|
return;
|
|
628
633
|
}
|
|
629
634
|
const service = await this.#servicePromise;
|
|
630
|
-
await
|
|
635
|
+
const results = await Promise.allSettled([
|
|
636
|
+
service.persistence.close(),
|
|
637
|
+
this.#ref?.close(),
|
|
638
|
+
]);
|
|
639
|
+
const failures = results
|
|
640
|
+
.filter((result) => result.status === 'rejected')
|
|
641
|
+
.map((result) => result.reason);
|
|
642
|
+
if (failures.length > 0) {
|
|
643
|
+
throw new AggregateError(failures, 'Content-addressable store failed to close cleanly');
|
|
644
|
+
}
|
|
631
645
|
})();
|
|
632
646
|
return await this.#closePromise;
|
|
633
647
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@git-stunts/git-cas",
|
|
3
|
-
"version": "6.5.
|
|
3
|
+
"version": "6.5.8",
|
|
4
4
|
"description": "Content-addressed storage backed by Git's object database, with optional encryption and pluggable codecs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -43,6 +43,8 @@
|
|
|
43
43
|
"docs/releases/v6.5.4.md",
|
|
44
44
|
"docs/releases/v6.5.5.md",
|
|
45
45
|
"docs/releases/v6.5.6.md",
|
|
46
|
+
"docs/releases/v6.5.7.md",
|
|
47
|
+
"docs/releases/v6.5.8.md",
|
|
46
48
|
"docs/THREAT_MODEL.md",
|
|
47
49
|
"docs/VAULT_INTERNALS.md",
|
|
48
50
|
"docs/WALKTHROUGH.md",
|
|
@@ -108,7 +110,7 @@
|
|
|
108
110
|
"@flyingrobots/bijou-tui": "^7.2.0",
|
|
109
111
|
"@flyingrobots/bijou-tui-app": "^7.2.0",
|
|
110
112
|
"@git-stunts/alfred": "^0.10.0",
|
|
111
|
-
"@git-stunts/plumbing": "^3.
|
|
113
|
+
"@git-stunts/plumbing": "^3.3.0",
|
|
112
114
|
"@git-stunts/vault": "^1.0.1",
|
|
113
115
|
"cbor-x": "^1.6.0",
|
|
114
116
|
"commander": "14.0.3",
|
|
@@ -2,8 +2,14 @@ import createCasError from '../errors/createCasError.js';
|
|
|
2
2
|
import { ErrorCodes } from '../errors/index.js';
|
|
3
3
|
import AssetHandle from '../value-objects/AssetHandle.js';
|
|
4
4
|
import StagedAsset from '../value-objects/StagedAsset.js';
|
|
5
|
+
import BoundedWriteWavePersistence from './BoundedWriteWavePersistence.js';
|
|
6
|
+
import forkCasPersistence from './forkCasPersistence.js';
|
|
7
|
+
import { recordStagedTarget } from './StagedTarget.js';
|
|
5
8
|
|
|
6
9
|
const DEFAULT_CLOCK = Object.freeze({ now: () => new Date() });
|
|
10
|
+
export const DEFAULT_ASSET_WRITE_BATCH_ASSETS = 4;
|
|
11
|
+
export const DEFAULT_ASSET_WRITE_BATCH_OBJECTS = 4_096;
|
|
12
|
+
export const DEFAULT_ASSET_WRITE_BATCH_BYTES = 256 * 1024 * 1024;
|
|
7
13
|
|
|
8
14
|
/**
|
|
9
15
|
* High-level asset handle boundary over the existing streaming CAS pipeline.
|
|
@@ -30,15 +36,91 @@ export default class AssetService {
|
|
|
30
36
|
* @returns {Promise<StagedAsset>}
|
|
31
37
|
*/
|
|
32
38
|
async put(options) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
return await this.#putWith(this.#cas, options);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Stores an input-ordered group with bounded concurrent CAS pipelines. */
|
|
43
|
+
async putBatch(options = {}) {
|
|
44
|
+
const batch = AssetService.#batchOptions(options);
|
|
45
|
+
return await withWriteScope(
|
|
46
|
+
this.#cas.persistence,
|
|
47
|
+
async (persistence) => await this.#putBatch(batch, persistence)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async #putBatch(batch, scopedPersistence) {
|
|
52
|
+
const persistence = new BoundedWriteWavePersistence({
|
|
53
|
+
persistence: scopedPersistence,
|
|
54
|
+
maxBatchObjects: batch.maxBatchObjects,
|
|
55
|
+
maxBatchBytes: batch.maxBatchBytes,
|
|
56
|
+
});
|
|
57
|
+
const cas = forkCasPersistence(this.#cas, persistence);
|
|
58
|
+
const manifests = new Array(batch.assets.length);
|
|
59
|
+
const state = { next: 0, error: null, errorIndex: null };
|
|
60
|
+
const workers = Array.from(
|
|
61
|
+
{ length: Math.min(batch.maxBatchAssets, batch.assets.length) },
|
|
62
|
+
() => this.#storeBatchWorker({ batch, cas, manifests, state })
|
|
63
|
+
);
|
|
64
|
+
await Promise.allSettled(workers);
|
|
65
|
+
if (state.error !== null) {
|
|
66
|
+
throw AssetService.#batchFailure(state.error, persistence, {
|
|
67
|
+
stagedAssetCount: 0,
|
|
68
|
+
batchIndex: state.errorIndex,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
let oids;
|
|
72
|
+
try {
|
|
73
|
+
oids = await cas.createTrees(
|
|
74
|
+
manifests.map((manifest, index) => ({
|
|
75
|
+
manifest,
|
|
76
|
+
merkleThreshold: batch.assets[index]?.merkleThreshold,
|
|
77
|
+
}))
|
|
78
|
+
);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
throw AssetService.#batchFailure(error, persistence, { stagedAssetCount: 0 });
|
|
81
|
+
}
|
|
82
|
+
const results = manifests.map((manifest, index) =>
|
|
83
|
+
this.#staged({ manifest, oid: oids[index] })
|
|
84
|
+
);
|
|
85
|
+
return Object.freeze(results);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async #putWith(cas, options) {
|
|
89
|
+
const manifest = await this.#storeWith(cas, options);
|
|
90
|
+
const oid = await cas.createTree({
|
|
36
91
|
manifest,
|
|
37
92
|
merkleThreshold: options?.merkleThreshold,
|
|
38
93
|
});
|
|
39
94
|
return this.#staged({ manifest, oid });
|
|
40
95
|
}
|
|
41
96
|
|
|
97
|
+
async #storeBatchWorker({ batch, cas, manifests, state }) {
|
|
98
|
+
while (state.error === null) {
|
|
99
|
+
const index = state.next;
|
|
100
|
+
if (index >= batch.assets.length) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
state.next += 1;
|
|
104
|
+
try {
|
|
105
|
+
manifests[index] = await this.#storeWith(cas, batch.assets[index], state);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (state.error === null) {
|
|
108
|
+
state.error = error;
|
|
109
|
+
state.errorIndex = index;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async #storeWith(cas, options, batchState = null) {
|
|
116
|
+
const filename = options?.filename ?? options?.slug;
|
|
117
|
+
const source =
|
|
118
|
+
batchState !== null && options?.source?.[Symbol.asyncIterator]
|
|
119
|
+
? cancelAfterBatchFailure(options.source, batchState)
|
|
120
|
+
: options?.source;
|
|
121
|
+
return await cas.store({ ...options, source, filename });
|
|
122
|
+
}
|
|
123
|
+
|
|
42
124
|
/**
|
|
43
125
|
* Adopts a validated existing git-cas manifest tree into an asset handle.
|
|
44
126
|
*
|
|
@@ -172,13 +254,15 @@ export default class AssetService {
|
|
|
172
254
|
}
|
|
173
255
|
|
|
174
256
|
#staged({ manifest, oid }) {
|
|
175
|
-
return
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
257
|
+
return recordStagedTarget(
|
|
258
|
+
new StagedAsset({
|
|
259
|
+
handle: new AssetHandle({ codec: this.#cas.codec.extension, oid }),
|
|
260
|
+
slug: manifest.slug,
|
|
261
|
+
filename: manifest.filename,
|
|
262
|
+
size: manifest.size,
|
|
263
|
+
observedAt: this.#observedAt(),
|
|
264
|
+
})
|
|
265
|
+
);
|
|
182
266
|
}
|
|
183
267
|
|
|
184
268
|
#observedAt() {
|
|
@@ -210,4 +294,111 @@ export default class AssetService {
|
|
|
210
294
|
throw createCasError('AssetService clock must provide now()', ErrorCodes.INVALID_OPTIONS);
|
|
211
295
|
}
|
|
212
296
|
}
|
|
297
|
+
|
|
298
|
+
static #batchOptions(options) {
|
|
299
|
+
if (!options || typeof options !== 'object' || !Array.isArray(options.assets)) {
|
|
300
|
+
throw createCasError('Asset batch must provide an asset array', ErrorCodes.INVALID_OPTIONS);
|
|
301
|
+
}
|
|
302
|
+
const batch = {
|
|
303
|
+
assets: options.assets,
|
|
304
|
+
maxBatchAssets: options.maxBatchAssets ?? DEFAULT_ASSET_WRITE_BATCH_ASSETS,
|
|
305
|
+
maxBatchObjects: options.maxBatchObjects ?? DEFAULT_ASSET_WRITE_BATCH_OBJECTS,
|
|
306
|
+
maxBatchBytes: options.maxBatchBytes ?? DEFAULT_ASSET_WRITE_BATCH_BYTES,
|
|
307
|
+
};
|
|
308
|
+
AssetService.#assertBatchLimit(batch.maxBatchAssets, 'assets', 64);
|
|
309
|
+
AssetService.#assertBatchLimit(batch.maxBatchObjects, 'objects', 100_000);
|
|
310
|
+
AssetService.#assertBatchLimit(batch.maxBatchBytes, 'bytes', 1024 * 1024 * 1024);
|
|
311
|
+
return batch;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
static #assertBatchLimit(value, label, maximum) {
|
|
315
|
+
if (!Number.isSafeInteger(value) || value < 1 || value > maximum) {
|
|
316
|
+
throw createCasError(
|
|
317
|
+
`Asset batch ${label} limit is outside its supported range`,
|
|
318
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
319
|
+
{ label, value, maximum }
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
static #batchFailure(error, persistence, { stagedAssetCount, batchIndex = null }) {
|
|
325
|
+
const sourceMeta = batchErrorMeta(error);
|
|
326
|
+
const failure = createCasError({
|
|
327
|
+
message: error instanceof Error ? error.message : String(error),
|
|
328
|
+
code: batchErrorCode(error),
|
|
329
|
+
documentationUrl: batchErrorDocumentation(error),
|
|
330
|
+
meta: {
|
|
331
|
+
...sourceMeta,
|
|
332
|
+
originalError: sourceMeta.originalError ?? error,
|
|
333
|
+
...(batchIndex === null ? {} : { batchIndex }),
|
|
334
|
+
staging: {
|
|
335
|
+
...persistence.snapshot(),
|
|
336
|
+
stagedAssetCount,
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
});
|
|
340
|
+
failure.cause = error;
|
|
341
|
+
return failure;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function batchErrorCode(error) {
|
|
346
|
+
return error && typeof error === 'object' && typeof error.code === 'string'
|
|
347
|
+
? error.code
|
|
348
|
+
: ErrorCodes.GIT_ERROR;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function batchErrorMeta(error) {
|
|
352
|
+
return error && typeof error === 'object' && error.meta && typeof error.meta === 'object'
|
|
353
|
+
? error.meta
|
|
354
|
+
: {};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function batchErrorDocumentation(error) {
|
|
358
|
+
return error && typeof error === 'object' && typeof error.documentationUrl === 'string'
|
|
359
|
+
? error.documentationUrl
|
|
360
|
+
: undefined;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function cancelAfterBatchFailure(source, state) {
|
|
364
|
+
return {
|
|
365
|
+
async *[Symbol.asyncIterator]() {
|
|
366
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
367
|
+
let complete = false;
|
|
368
|
+
try {
|
|
369
|
+
while (state.error === null) {
|
|
370
|
+
const next = await iterator.next();
|
|
371
|
+
if (next.done) {
|
|
372
|
+
complete = true;
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (state.error !== null) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
yield next.value;
|
|
379
|
+
}
|
|
380
|
+
} finally {
|
|
381
|
+
if (!complete) {
|
|
382
|
+
await closeAsyncIterator(iterator);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
async function closeAsyncIterator(iterator) {
|
|
390
|
+
if (typeof iterator.return !== 'function') {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
try {
|
|
394
|
+
await iterator.return();
|
|
395
|
+
} catch {
|
|
396
|
+
// Preserve the first batch failure; iterator cleanup is best effort.
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async function withWriteScope(persistence, operation) {
|
|
401
|
+
return typeof persistence.withWriteScope === 'function'
|
|
402
|
+
? await persistence.withWriteScope(operation)
|
|
403
|
+
: await operation(persistence);
|
|
213
404
|
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import GitPersistencePort from '../../ports/GitPersistencePort.js';
|
|
2
|
+
import createCasError from '../errors/createCasError.js';
|
|
3
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
4
|
+
import { utf8ByteLength } from '../encoding/utf8.js';
|
|
5
|
+
|
|
6
|
+
const MAX_PROTOCOL_OBJECTS = 256;
|
|
7
|
+
const MAX_PROTOCOL_BYTES = 64 * 1024 * 1024;
|
|
8
|
+
|
|
9
|
+
/** Opt-in persistence decorator that coalesces concurrent immutable writes. */
|
|
10
|
+
export default class BoundedWriteWavePersistence extends GitPersistencePort {
|
|
11
|
+
#failure = null;
|
|
12
|
+
#flushing = false;
|
|
13
|
+
#maxBatchBytes;
|
|
14
|
+
#maxBatchObjects;
|
|
15
|
+
#persistence;
|
|
16
|
+
#queue = [];
|
|
17
|
+
#scheduled = false;
|
|
18
|
+
#writeBytes = 0;
|
|
19
|
+
#writeObjects = 0;
|
|
20
|
+
|
|
21
|
+
constructor({ persistence, maxBatchObjects, maxBatchBytes }) {
|
|
22
|
+
super();
|
|
23
|
+
this.#persistence = persistence;
|
|
24
|
+
this.#maxBatchObjects = maxBatchObjects;
|
|
25
|
+
this.#maxBatchBytes = maxBatchBytes;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async writeBlob(content) {
|
|
29
|
+
return await this.#enqueue({ kind: 'blob', value: content, bytes: contentBytes(content) });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async writeBlobs(contents) {
|
|
33
|
+
return await Promise.all([...contents].map((content) => this.writeBlob(content)));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async writeTree(entries) {
|
|
37
|
+
return await this.#enqueue({ kind: 'tree', value: entries, bytes: treeBytes(entries) });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async writeTrees(trees) {
|
|
41
|
+
return await Promise.all([...trees].map((entries) => this.writeTree(entries)));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async readBlob(oid, maxBytes) {
|
|
45
|
+
return await this.#persistence.readBlob(oid, maxBytes);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async readBlobStream(oid) {
|
|
49
|
+
return await this.#persistence.readBlobStream(oid);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async readTree(oid) {
|
|
53
|
+
return await this.#persistence.readTree(oid);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async readTreeEntry(oid, path) {
|
|
57
|
+
return await this.#persistence.readTreeEntry(oid, path);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async *iterateTree(oid) {
|
|
61
|
+
yield* this.#persistence.iterateTree(oid);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async readObjectType(oid) {
|
|
65
|
+
return await this.#persistence.readObjectType(oid);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async readObjectSize(oid) {
|
|
69
|
+
return await this.#persistence.readObjectSize(oid);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async readObjectInfos(oids) {
|
|
73
|
+
if (typeof this.#persistence.readObjectInfos === 'function') {
|
|
74
|
+
return await this.#persistence.readObjectInfos(oids);
|
|
75
|
+
}
|
|
76
|
+
return await super.readObjectInfos(oids);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
setMaxBlobSize(maxBlobSize) {
|
|
80
|
+
this.#persistence.setMaxBlobSize?.(maxBlobSize);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
snapshot() {
|
|
84
|
+
return Object.freeze({
|
|
85
|
+
writeObjects: this.#writeObjects,
|
|
86
|
+
writeBytes: this.#writeBytes,
|
|
87
|
+
failed: this.#failure !== null,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#enqueue(request) {
|
|
92
|
+
if (this.#failure !== null) {
|
|
93
|
+
return Promise.reject(this.#failure);
|
|
94
|
+
}
|
|
95
|
+
const objects = this.#writeObjects + 1;
|
|
96
|
+
const bytes = this.#writeBytes + request.bytes;
|
|
97
|
+
if (objects > this.#maxBatchObjects || bytes > this.#maxBatchBytes) {
|
|
98
|
+
this.#failure = batchLimit({ objects, bytes }, {
|
|
99
|
+
objects: this.#maxBatchObjects,
|
|
100
|
+
bytes: this.#maxBatchBytes,
|
|
101
|
+
});
|
|
102
|
+
return Promise.reject(this.#failure);
|
|
103
|
+
}
|
|
104
|
+
this.#writeObjects = objects;
|
|
105
|
+
this.#writeBytes = bytes;
|
|
106
|
+
const result = new Promise((resolve, reject) => {
|
|
107
|
+
this.#queue.push({ ...request, resolve, reject });
|
|
108
|
+
});
|
|
109
|
+
this.#schedule();
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#schedule() {
|
|
114
|
+
if (this.#scheduled || this.#flushing) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this.#scheduled = true;
|
|
118
|
+
void Promise.resolve().then(() => {
|
|
119
|
+
this.#scheduled = false;
|
|
120
|
+
return this.#drain();
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async #drain() {
|
|
125
|
+
if (this.#flushing) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
this.#flushing = true;
|
|
129
|
+
try {
|
|
130
|
+
while (this.#queue.length > 0 && this.#failure === null) {
|
|
131
|
+
const group = this.#takeGroup();
|
|
132
|
+
await this.#writeGroup(group);
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
this.#failure = error;
|
|
136
|
+
} finally {
|
|
137
|
+
if (this.#failure !== null) {
|
|
138
|
+
this.#rejectQueued(this.#failure);
|
|
139
|
+
}
|
|
140
|
+
this.#flushing = false;
|
|
141
|
+
if (this.#queue.length > 0) {
|
|
142
|
+
this.#schedule();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
#takeGroup() {
|
|
148
|
+
const first = this.#queue[0];
|
|
149
|
+
const group = [];
|
|
150
|
+
let bytes = 0;
|
|
151
|
+
while (group.length < MAX_PROTOCOL_OBJECTS && this.#queue[0]?.kind === first.kind) {
|
|
152
|
+
const next = this.#queue[0];
|
|
153
|
+
if (group.length > 0 && bytes + next.bytes > MAX_PROTOCOL_BYTES) {
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
group.push(this.#queue.shift());
|
|
157
|
+
bytes += next.bytes;
|
|
158
|
+
if (next.bytes > MAX_PROTOCOL_BYTES) {
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return group;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async #writeGroup(group) {
|
|
166
|
+
try {
|
|
167
|
+
const oids = group.length === 1 || group[0].bytes > MAX_PROTOCOL_BYTES
|
|
168
|
+
? [await this.#writeOne(group[0])]
|
|
169
|
+
: await this.#writeMany(group);
|
|
170
|
+
if (oids.length !== group.length) {
|
|
171
|
+
throw createCasError(
|
|
172
|
+
'Persistence returned the wrong number of write-wave object identifiers',
|
|
173
|
+
ErrorCodes.GIT_ERROR,
|
|
174
|
+
{ expected: group.length, actual: oids.length },
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
group.forEach((request, index) => request.resolve(oids[index]));
|
|
178
|
+
} catch (error) {
|
|
179
|
+
group.forEach((request) => request.reject(error));
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async #writeOne(request) {
|
|
185
|
+
return request.kind === 'blob'
|
|
186
|
+
? await this.#persistence.writeBlob(request.value)
|
|
187
|
+
: await this.#persistence.writeTree(request.value);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async #writeMany(group) {
|
|
191
|
+
const values = group.map((request) => request.value);
|
|
192
|
+
if (group[0].kind === 'blob' && typeof this.#persistence.writeBlobs === 'function') {
|
|
193
|
+
return await this.#persistence.writeBlobs(values);
|
|
194
|
+
}
|
|
195
|
+
if (group[0].kind === 'tree' && typeof this.#persistence.writeTrees === 'function') {
|
|
196
|
+
return await this.#persistence.writeTrees(values);
|
|
197
|
+
}
|
|
198
|
+
const oids = [];
|
|
199
|
+
for (const request of group) {
|
|
200
|
+
oids.push(await this.#writeOne(request));
|
|
201
|
+
}
|
|
202
|
+
return oids;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
#rejectQueued(error) {
|
|
206
|
+
for (const request of this.#queue.splice(0)) {
|
|
207
|
+
request.reject(error);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function contentBytes(content) {
|
|
213
|
+
if (content instanceof Uint8Array) {
|
|
214
|
+
return content.byteLength;
|
|
215
|
+
}
|
|
216
|
+
if (typeof content === 'string') {
|
|
217
|
+
return utf8ByteLength(content);
|
|
218
|
+
}
|
|
219
|
+
return Number.MAX_SAFE_INTEGER;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function treeBytes(entries) {
|
|
223
|
+
if (!Array.isArray(entries)) {
|
|
224
|
+
return Number.MAX_SAFE_INTEGER;
|
|
225
|
+
}
|
|
226
|
+
return entries.reduce((total, line) => total + utf8ByteLength(line) + 1, 0);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function batchLimit(observed, maximum) {
|
|
230
|
+
return createCasError(
|
|
231
|
+
'Asset write batch exceeds its configured aggregate limit',
|
|
232
|
+
ErrorCodes.INVALID_OPTIONS,
|
|
233
|
+
{ observed, maximum },
|
|
234
|
+
);
|
|
235
|
+
}
|