@git-stunts/git-cas 4.0.1 → 5.1.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 +47 -0
- package/README.md +15 -0
- package/bin/actions.js +5 -0
- package/bin/git-cas.js +137 -26
- package/bin/ui/progress.js +4 -4
- package/index.d.ts +54 -2
- package/index.js +85 -2
- package/package.json +1 -1
- package/src/domain/schemas/ManifestSchema.d.ts +35 -0
- package/src/domain/schemas/ManifestSchema.js +35 -0
- package/src/domain/services/CasService.d.ts +15 -0
- package/src/domain/services/CasService.js +321 -66
- package/src/domain/value-objects/Manifest.d.ts +10 -0
- package/src/domain/value-objects/Manifest.js +7 -1
- package/src/infrastructure/chunkers/CdcChunker.js +339 -0
- package/src/infrastructure/chunkers/FixedChunker.js +49 -0
- package/src/ports/ChunkingPort.js +45 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content-Defined Chunking engine using Buzhash rolling hash.
|
|
3
|
+
*
|
|
4
|
+
* Splits an async stream of buffers into content-defined chunks whose
|
|
5
|
+
* boundaries are determined by the rolling hash value. This provides
|
|
6
|
+
* shift-resistant deduplication — small edits only affect nearby chunks.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```js
|
|
10
|
+
* import CdcChunker from './CdcChunker.js';
|
|
11
|
+
* const chunker = new CdcChunker({ targetChunkSize: 262144 });
|
|
12
|
+
* for await (const chunk of chunker.chunk(source)) {
|
|
13
|
+
* console.log(chunk.length);
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import ChunkingPort from '../../ports/ChunkingPort.js';
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Buzhash byte-table (256 entries, deterministic)
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Generated from a seeded xorshift64 PRNG so every runtime produces the
|
|
24
|
+
// same table without needing `crypto.getRandomValues`.
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Generates the deterministic 256-entry Buzhash byte table using a
|
|
29
|
+
* seeded xorshift64 PRNG.
|
|
30
|
+
* @returns {Uint32Array}
|
|
31
|
+
*/
|
|
32
|
+
function generateBuzTable() {
|
|
33
|
+
const table = new Uint32Array(256);
|
|
34
|
+
// Seed chosen arbitrarily — must never change.
|
|
35
|
+
let s = BigInt('0x6a09e667f3bcc908');
|
|
36
|
+
for (let i = 0; i < 256; i++) {
|
|
37
|
+
// xorshift64
|
|
38
|
+
s ^= s << 13n;
|
|
39
|
+
s &= 0xffffffffffffffffn;
|
|
40
|
+
s ^= s >> 7n;
|
|
41
|
+
s &= 0xffffffffffffffffn;
|
|
42
|
+
s ^= s << 17n;
|
|
43
|
+
s &= 0xffffffffffffffffn;
|
|
44
|
+
table[i] = Number(s & 0xffffffffn);
|
|
45
|
+
}
|
|
46
|
+
return table;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @type {Uint32Array} */
|
|
50
|
+
const BUZ_TABLE = generateBuzTable();
|
|
51
|
+
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// Constants
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
/** Window size for the Buzhash sliding window. */
|
|
57
|
+
const WINDOW_SIZE = 64;
|
|
58
|
+
const WINDOW_MASK = WINDOW_SIZE - 1; // 63, for fast modulo
|
|
59
|
+
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// Rolling-hash state object
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Mutable rolling-hash + chunk-accumulation state.
|
|
66
|
+
*
|
|
67
|
+
* Passed by reference into the per-phase helper functions so that the
|
|
68
|
+
* `chunk()` async generator stays short and simple. Also carries the
|
|
69
|
+
* chunker configuration to keep helper parameter counts low.
|
|
70
|
+
*
|
|
71
|
+
* @typedef {Object} ChunkState
|
|
72
|
+
* @property {number} hash - Current Buzhash value.
|
|
73
|
+
* @property {Uint8Array} win - Sliding window ring buffer.
|
|
74
|
+
* @property {number} winPos - Current write position in `win`.
|
|
75
|
+
* @property {number} hashFed - Bytes fed since last reset.
|
|
76
|
+
* @property {Buffer} chunkBuf - Pre-allocated chunk output buffer.
|
|
77
|
+
* @property {number} chunkLen - Bytes written to chunkBuf so far.
|
|
78
|
+
* @property {number} minSize - Minimum chunk size.
|
|
79
|
+
* @property {number} maxSize - Maximum chunk size.
|
|
80
|
+
* @property {number} mask - Boundary detection mask.
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Reset rolling-hash and chunk accumulation state for a new chunk.
|
|
85
|
+
* @param {ChunkState} st
|
|
86
|
+
*/
|
|
87
|
+
function resetState(st) {
|
|
88
|
+
st.hash = 0;
|
|
89
|
+
st.winPos = 0;
|
|
90
|
+
st.hashFed = 0;
|
|
91
|
+
st.chunkLen = 0;
|
|
92
|
+
st.win.fill(0);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// Per-phase helpers (keep the async generator under the line/complexity cap)
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Phase 1 — fill the sliding window (first 64 bytes of each chunk).
|
|
101
|
+
* Uses a simplified hash update (no outgoing-byte removal).
|
|
102
|
+
*
|
|
103
|
+
* @param {ChunkState} st
|
|
104
|
+
* @param {Buffer} buf - Source buffer being processed.
|
|
105
|
+
* @param {number} srcPos - Current read offset in `buf`.
|
|
106
|
+
* @returns {number} Updated srcPos.
|
|
107
|
+
*/
|
|
108
|
+
function fillWindow(st, buf, srcPos) {
|
|
109
|
+
const need = WINDOW_SIZE - st.hashFed;
|
|
110
|
+
const avail = buf.length - srcPos;
|
|
111
|
+
const n = need < avail ? need : avail;
|
|
112
|
+
const end = srcPos + n;
|
|
113
|
+
const table = BUZ_TABLE;
|
|
114
|
+
|
|
115
|
+
while (srcPos < end) {
|
|
116
|
+
const byte = buf[srcPos];
|
|
117
|
+
st.hash = (((st.hash << 1) | (st.hash >>> 31)) ^ table[byte]) >>> 0;
|
|
118
|
+
st.win[st.winPos] = byte;
|
|
119
|
+
st.winPos = (st.winPos + 1) & WINDOW_MASK;
|
|
120
|
+
st.chunkBuf[st.chunkLen++] = byte;
|
|
121
|
+
srcPos++;
|
|
122
|
+
}
|
|
123
|
+
st.hashFed += n;
|
|
124
|
+
return srcPos;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Phase 2 — pre-min region (window full, no boundary checks).
|
|
129
|
+
* Bulk-copies bytes into the chunk buffer while updating the hash.
|
|
130
|
+
*
|
|
131
|
+
* @param {ChunkState} st
|
|
132
|
+
* @param {Buffer} buf
|
|
133
|
+
* @param {number} srcPos
|
|
134
|
+
* @returns {number} Updated srcPos.
|
|
135
|
+
*/
|
|
136
|
+
function feedPreMin(st, buf, srcPos) {
|
|
137
|
+
const needed = st.minSize - st.chunkLen;
|
|
138
|
+
const avail = buf.length - srcPos;
|
|
139
|
+
const n = needed < avail ? needed : avail;
|
|
140
|
+
const end = srcPos + n;
|
|
141
|
+
const table = BUZ_TABLE;
|
|
142
|
+
|
|
143
|
+
// Bulk-copy into chunk buffer
|
|
144
|
+
buf.copy(st.chunkBuf, st.chunkLen, srcPos, end);
|
|
145
|
+
|
|
146
|
+
// Feed bytes into rolling hash (window is full)
|
|
147
|
+
while (srcPos < end) {
|
|
148
|
+
const byte = buf[srcPos];
|
|
149
|
+
st.hash = (((st.hash << 1) | (st.hash >>> 31)) ^ table[st.win[st.winPos]] ^ table[byte]) >>> 0;
|
|
150
|
+
st.win[st.winPos] = byte;
|
|
151
|
+
st.winPos = (st.winPos + 1) & WINDOW_MASK;
|
|
152
|
+
srcPos++;
|
|
153
|
+
}
|
|
154
|
+
st.chunkLen += n;
|
|
155
|
+
return srcPos;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Phase 3 — scan for a content-defined boundary in [minSize, maxSize).
|
|
160
|
+
*
|
|
161
|
+
* @param {ChunkState} st
|
|
162
|
+
* @param {Buffer} buf
|
|
163
|
+
* @param {number} srcPos
|
|
164
|
+
* @returns {{ srcPos: number, found: boolean }}
|
|
165
|
+
*/
|
|
166
|
+
function scanBoundary(st, buf, srcPos) {
|
|
167
|
+
const { mask, maxSize } = st;
|
|
168
|
+
const table = BUZ_TABLE;
|
|
169
|
+
const limit = buf.length < (srcPos + maxSize - st.chunkLen)
|
|
170
|
+
? buf.length
|
|
171
|
+
: (srcPos + maxSize - st.chunkLen);
|
|
172
|
+
|
|
173
|
+
let h = st.hash;
|
|
174
|
+
let wp = st.winPos;
|
|
175
|
+
const { win, chunkBuf } = st;
|
|
176
|
+
let cl = st.chunkLen;
|
|
177
|
+
|
|
178
|
+
while (srcPos < limit) {
|
|
179
|
+
const byte = buf[srcPos];
|
|
180
|
+
h = (((h << 1) | (h >>> 31)) ^ table[win[wp]] ^ table[byte]) >>> 0;
|
|
181
|
+
win[wp] = byte;
|
|
182
|
+
wp = (wp + 1) & WINDOW_MASK;
|
|
183
|
+
chunkBuf[cl++] = byte;
|
|
184
|
+
srcPos++;
|
|
185
|
+
|
|
186
|
+
if ((h & mask) === 0) {
|
|
187
|
+
st.hash = h;
|
|
188
|
+
st.winPos = wp;
|
|
189
|
+
st.chunkLen = cl;
|
|
190
|
+
return { srcPos, found: true };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
st.hash = h;
|
|
195
|
+
st.winPos = wp;
|
|
196
|
+
st.chunkLen = cl;
|
|
197
|
+
return { srcPos, found: false };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Process a single source buffer through all three CDC phases.
|
|
202
|
+
*
|
|
203
|
+
* Returns an array of completed chunks (may be empty if no boundary
|
|
204
|
+
* was found within this buffer).
|
|
205
|
+
*
|
|
206
|
+
* @param {ChunkState} st
|
|
207
|
+
* @param {Buffer} buf
|
|
208
|
+
* @returns {Buffer[]} Completed chunks ready to yield.
|
|
209
|
+
*/
|
|
210
|
+
function processBuf(st, buf) {
|
|
211
|
+
/** @type {Buffer[]} */
|
|
212
|
+
const completed = [];
|
|
213
|
+
let srcPos = 0;
|
|
214
|
+
|
|
215
|
+
while (srcPos < buf.length) {
|
|
216
|
+
// Phase 1: fill window if needed
|
|
217
|
+
if (st.hashFed < WINDOW_SIZE) {
|
|
218
|
+
srcPos = fillWindow(st, buf, srcPos);
|
|
219
|
+
if (st.hashFed < WINDOW_SIZE) { break; }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Phase 2: bulk-feed bytes until minSize
|
|
223
|
+
if (st.chunkLen < st.minSize) {
|
|
224
|
+
srcPos = feedPreMin(st, buf, srcPos);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Phase 3: scan for boundary
|
|
228
|
+
const result = scanBoundary(st, buf, srcPos);
|
|
229
|
+
srcPos = result.srcPos;
|
|
230
|
+
|
|
231
|
+
if (result.found || st.chunkLen >= st.maxSize) {
|
|
232
|
+
completed.push(Buffer.from(st.chunkBuf.subarray(0, st.chunkLen)));
|
|
233
|
+
resetState(st);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return completed;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
// CdcChunker
|
|
242
|
+
// ---------------------------------------------------------------------------
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @typedef {Object} CdcChunkerOptions
|
|
246
|
+
* @property {number} [minChunkSize=65536] - Minimum chunk size in bytes (64 KiB).
|
|
247
|
+
* @property {number} [maxChunkSize=1048576] - Maximum chunk size in bytes (1 MiB).
|
|
248
|
+
* @property {number} [targetChunkSize=262144] - Target (average) chunk size in bytes (256 KiB).
|
|
249
|
+
*/
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* CDC chunker that uses a Buzhash rolling hash to find content-defined
|
|
253
|
+
* chunk boundaries within an async byte stream.
|
|
254
|
+
*/
|
|
255
|
+
export default class CdcChunker extends ChunkingPort {
|
|
256
|
+
/** @type {number} */ #minChunkSize;
|
|
257
|
+
/** @type {number} */ #maxChunkSize;
|
|
258
|
+
/** @type {number} */ #targetChunkSize;
|
|
259
|
+
/** @type {number} */ #mask;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* @param {CdcChunkerOptions} [options]
|
|
263
|
+
*/
|
|
264
|
+
constructor({
|
|
265
|
+
minChunkSize = 65_536,
|
|
266
|
+
maxChunkSize = 1_048_576,
|
|
267
|
+
targetChunkSize = 262_144,
|
|
268
|
+
} = {}) {
|
|
269
|
+
super();
|
|
270
|
+
if (minChunkSize > maxChunkSize) {
|
|
271
|
+
throw new RangeError(
|
|
272
|
+
`minChunkSize (${minChunkSize}) must not exceed maxChunkSize (${maxChunkSize})`,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
if (targetChunkSize < minChunkSize || targetChunkSize > maxChunkSize) {
|
|
276
|
+
throw new RangeError(
|
|
277
|
+
`targetChunkSize (${targetChunkSize}) must be in [${minChunkSize}, ${maxChunkSize}]`,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
this.#minChunkSize = minChunkSize;
|
|
282
|
+
this.#maxChunkSize = maxChunkSize;
|
|
283
|
+
this.#targetChunkSize = targetChunkSize;
|
|
284
|
+
|
|
285
|
+
// Mask: nearest power-of-2 minus 1 that is <= targetChunkSize.
|
|
286
|
+
// E.g. target 262144 (2^18) -> mask 0x3FFFF (2^18 - 1).
|
|
287
|
+
const bits = Math.floor(Math.log2(targetChunkSize));
|
|
288
|
+
this.#mask = ((1 << bits) - 1) >>> 0;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** @override */
|
|
292
|
+
get strategy() {
|
|
293
|
+
return 'cdc';
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** @override */
|
|
297
|
+
get params() {
|
|
298
|
+
return {
|
|
299
|
+
target: this.#targetChunkSize,
|
|
300
|
+
min: this.#minChunkSize,
|
|
301
|
+
max: this.#maxChunkSize,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Splits an async byte stream into content-defined chunks.
|
|
307
|
+
*
|
|
308
|
+
* Yields `Buffer` instances. The caller may concatenate them or write
|
|
309
|
+
* each one directly to storage.
|
|
310
|
+
*
|
|
311
|
+
* @override
|
|
312
|
+
* @param {AsyncIterable<Buffer>} source - Incoming byte stream.
|
|
313
|
+
* @yields {Buffer} Content-defined chunks.
|
|
314
|
+
*/
|
|
315
|
+
async *chunk(source) {
|
|
316
|
+
/** @type {ChunkState} */
|
|
317
|
+
const st = {
|
|
318
|
+
hash: 0,
|
|
319
|
+
win: new Uint8Array(WINDOW_SIZE),
|
|
320
|
+
winPos: 0,
|
|
321
|
+
hashFed: 0,
|
|
322
|
+
chunkBuf: Buffer.allocUnsafe(this.#maxChunkSize),
|
|
323
|
+
chunkLen: 0,
|
|
324
|
+
minSize: this.#minChunkSize,
|
|
325
|
+
maxSize: this.#maxChunkSize,
|
|
326
|
+
mask: this.#mask,
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
for await (const buf of source) {
|
|
330
|
+
const chunks = processBuf(st, buf);
|
|
331
|
+
for (const c of chunks) { yield c; }
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Flush final partial chunk (may be < minSize — allowed for EOF).
|
|
335
|
+
if (st.chunkLen > 0) {
|
|
336
|
+
yield Buffer.from(st.chunkBuf.subarray(0, st.chunkLen));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import ChunkingPort from '../../ports/ChunkingPort.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* {@link ChunkingPort} implementation that splits data into fixed-size chunks.
|
|
5
|
+
*
|
|
6
|
+
* Buffers incoming data and yields exactly {@link chunkSize} byte chunks.
|
|
7
|
+
* The final chunk may be smaller than {@link chunkSize} if the source does
|
|
8
|
+
* not divide evenly. An empty source produces no chunks.
|
|
9
|
+
*/
|
|
10
|
+
export default class FixedChunker extends ChunkingPort {
|
|
11
|
+
/** @type {number} */
|
|
12
|
+
#chunkSize;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {Object} [options]
|
|
16
|
+
* @param {number} [options.chunkSize=262144] - Chunk size in bytes (default 256 KiB).
|
|
17
|
+
*/
|
|
18
|
+
constructor({ chunkSize = 262144 } = {}) {
|
|
19
|
+
super();
|
|
20
|
+
this.#chunkSize = chunkSize;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @override */
|
|
24
|
+
get strategy() {
|
|
25
|
+
return 'fixed';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @override */
|
|
29
|
+
get params() {
|
|
30
|
+
return { chunkSize: this.#chunkSize };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @override */
|
|
34
|
+
async *chunk(source) {
|
|
35
|
+
let buffer = Buffer.alloc(0);
|
|
36
|
+
|
|
37
|
+
for await (const data of source) {
|
|
38
|
+
buffer = Buffer.concat([buffer, data]);
|
|
39
|
+
while (buffer.length >= this.#chunkSize) {
|
|
40
|
+
yield buffer.slice(0, this.#chunkSize);
|
|
41
|
+
buffer = buffer.slice(this.#chunkSize);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (buffer.length > 0) {
|
|
46
|
+
yield buffer;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract port for splitting a byte stream into chunks.
|
|
3
|
+
*
|
|
4
|
+
* Implementations define a chunking strategy (e.g. fixed-size, content-defined)
|
|
5
|
+
* and expose the configuration parameters that govern it.
|
|
6
|
+
*
|
|
7
|
+
* @abstract
|
|
8
|
+
*/
|
|
9
|
+
export default class ChunkingPort {
|
|
10
|
+
constructor() {
|
|
11
|
+
if (new.target === ChunkingPort) {
|
|
12
|
+
throw new Error('ChunkingPort is abstract and cannot be instantiated directly');
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Returns the strategy identifier for this chunker (e.g. `'fixed'`, `'cdc'`).
|
|
18
|
+
* @abstract
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
get strategy() {
|
|
22
|
+
throw new Error('Not implemented');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns the configuration parameters for this chunker.
|
|
27
|
+
* @abstract
|
|
28
|
+
* @returns {Object}
|
|
29
|
+
*/
|
|
30
|
+
get params() {
|
|
31
|
+
throw new Error('Not implemented');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Splits an async byte stream into chunks.
|
|
36
|
+
*
|
|
37
|
+
* @abstract
|
|
38
|
+
* @param {AsyncIterable<Buffer>} _source - The input byte stream.
|
|
39
|
+
* @yields {Buffer} Chunks of data whose size depends on the strategy.
|
|
40
|
+
* @returns {AsyncGenerator<Buffer>}
|
|
41
|
+
*/
|
|
42
|
+
async *chunk(_source) { // eslint-disable-line require-yield
|
|
43
|
+
throw new Error('Not implemented');
|
|
44
|
+
}
|
|
45
|
+
}
|