@git-stunts/git-cas 3.1.0 → 5.0.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/index.d.ts CHANGED
@@ -11,13 +11,39 @@ import type {
11
11
  CryptoPort,
12
12
  CodecPort,
13
13
  GitPersistencePort,
14
+ ObservabilityPort,
14
15
  CasServiceOptions,
15
16
  DeriveKeyOptions,
16
17
  DeriveKeyResult,
17
18
  } from "./src/domain/services/CasService.js";
18
19
 
19
20
  export { CasService, Manifest, Chunk };
20
- export type { EncryptionMeta, ManifestData, CompressionMeta, KdfParams, SubManifestRef, CryptoPort, CodecPort, GitPersistencePort, CasServiceOptions, DeriveKeyOptions, DeriveKeyResult };
21
+ export type { EncryptionMeta, ManifestData, CompressionMeta, KdfParams, SubManifestRef, CryptoPort, CodecPort, GitPersistencePort, ObservabilityPort, CasServiceOptions, DeriveKeyOptions, DeriveKeyResult };
22
+
23
+ /** Abstract port for splitting a byte stream into chunks. */
24
+ export declare class ChunkingPort {
25
+ get strategy(): string;
26
+ get params(): Record<string, unknown>;
27
+ chunk(source: AsyncIterable<Buffer>): AsyncIterable<Buffer>;
28
+ }
29
+
30
+ /** Fixed-size chunking adapter. */
31
+ export declare class FixedChunker extends ChunkingPort {
32
+ constructor(options?: { chunkSize?: number });
33
+ get strategy(): "fixed";
34
+ get params(): { chunkSize: number };
35
+ }
36
+
37
+ /** Content-defined chunking adapter using buzhash rolling hash. */
38
+ export declare class CdcChunker extends ChunkingPort {
39
+ constructor(options?: {
40
+ minChunkSize?: number;
41
+ maxChunkSize?: number;
42
+ targetChunkSize?: number;
43
+ });
44
+ get strategy(): "cdc";
45
+ get params(): { target: number; min: number; max: number };
46
+ }
21
47
 
22
48
  /** Abstract port for cryptographic operations. */
23
49
  export declare class CryptoPortBase {
@@ -93,14 +119,58 @@ export declare class CborCodec extends CodecPortBase {
93
119
  constructor();
94
120
  }
95
121
 
122
+ /** No-op observability adapter. */
123
+ export declare class SilentObserver {
124
+ metric(channel: string, data: Record<string, unknown>): void;
125
+ log(level: string, msg: string, meta?: Record<string, unknown>): void;
126
+ span(name: string): { end(meta?: Record<string, unknown>): void };
127
+ }
128
+
129
+ /** EventEmitter-based observability adapter for backward compatibility. */
130
+ export declare class EventEmitterObserver {
131
+ metric(channel: string, data: Record<string, unknown>): void;
132
+ log(level: string, msg: string, meta?: Record<string, unknown>): void;
133
+ span(name: string): { end(meta?: Record<string, unknown>): void };
134
+ on(event: string, listener: (...args: unknown[]) => void): this;
135
+ removeListener(event: string, listener: (...args: unknown[]) => void): this;
136
+ listenerCount(event: string): number;
137
+ }
138
+
139
+ /** Stats-collecting observability adapter. */
140
+ export declare class StatsCollector {
141
+ metric(channel: string, data: Record<string, unknown>): void;
142
+ log(level: string, msg: string, meta?: Record<string, unknown>): void;
143
+ span(name: string): { end(meta?: Record<string, unknown>): void };
144
+ summary(): {
145
+ chunksProcessed: number;
146
+ bytesTotal: number;
147
+ elapsed: number;
148
+ throughput: number;
149
+ errors: number;
150
+ };
151
+ }
152
+
153
+ /** Declarative chunking strategy configuration. */
154
+ export interface ChunkingConfig {
155
+ strategy: "fixed" | "cdc";
156
+ chunkSize?: number;
157
+ targetChunkSize?: number;
158
+ minChunkSize?: number;
159
+ maxChunkSize?: number;
160
+ }
161
+
96
162
  /** Constructor options for {@link ContentAddressableStore}. */
97
163
  export interface ContentAddressableStoreOptions {
98
164
  plumbing: unknown;
99
165
  chunkSize?: number;
100
166
  codec?: CodecPort;
101
167
  crypto?: CryptoPort;
168
+ observability?: ObservabilityPort;
102
169
  policy?: unknown;
103
170
  merkleThreshold?: number;
171
+ concurrency?: number;
172
+ chunking?: ChunkingConfig;
173
+ chunker?: ChunkingPort;
104
174
  }
105
175
 
106
176
  /** A single vault entry. */
@@ -257,6 +327,12 @@ export default class ContentAddressableStore {
257
327
  passphrase?: string;
258
328
  }): Promise<{ buffer: Buffer; bytesWritten: number }>;
259
329
 
330
+ restoreStream(options: {
331
+ manifest: Manifest;
332
+ encryptionKey?: Buffer;
333
+ passphrase?: string;
334
+ }): AsyncIterable<Buffer>;
335
+
260
336
  createTree(options: { manifest: Manifest }): Promise<string>;
261
337
 
262
338
  verifyIntegrity(manifest: Manifest): Promise<boolean>;
package/index.js CHANGED
@@ -3,8 +3,10 @@
3
3
  * @fileoverview Content Addressable Store - Managed blob storage in Git.
4
4
  */
5
5
 
6
- import { createReadStream, writeFileSync } from 'node:fs';
6
+ import { createReadStream, createWriteStream } from 'node:fs';
7
7
  import path from 'node:path';
8
+ import { Readable, Transform } from 'node:stream';
9
+ import { pipeline } from 'node:stream/promises';
8
10
  import CasService from './src/domain/services/CasService.js';
9
11
  import VaultService from './src/domain/services/VaultService.js';
10
12
  import GitPersistenceAdapter from './src/infrastructure/adapters/GitPersistenceAdapter.js';
@@ -13,8 +15,15 @@ import NodeCryptoAdapter from './src/infrastructure/adapters/NodeCryptoAdapter.j
13
15
  import Manifest from './src/domain/value-objects/Manifest.js';
14
16
  import Chunk from './src/domain/value-objects/Chunk.js';
15
17
  import CryptoPort from './src/ports/CryptoPort.js';
18
+ import ChunkingPort from './src/ports/ChunkingPort.js';
19
+ import ObservabilityPort from './src/ports/ObservabilityPort.js';
16
20
  import JsonCodec from './src/infrastructure/codecs/JsonCodec.js';
17
21
  import CborCodec from './src/infrastructure/codecs/CborCodec.js';
22
+ import SilentObserver from './src/infrastructure/adapters/SilentObserver.js';
23
+ import EventEmitterObserver from './src/infrastructure/adapters/EventEmitterObserver.js';
24
+ import StatsCollector from './src/infrastructure/adapters/StatsCollector.js';
25
+ import FixedChunker from './src/infrastructure/chunkers/FixedChunker.js';
26
+ import CdcChunker from './src/infrastructure/chunkers/CdcChunker.js';
18
27
 
19
28
  export {
20
29
  CasService,
@@ -23,10 +32,17 @@ export {
23
32
  GitRefAdapter,
24
33
  NodeCryptoAdapter,
25
34
  CryptoPort,
35
+ ChunkingPort,
36
+ ObservabilityPort,
26
37
  Manifest,
27
38
  Chunk,
28
39
  JsonCodec,
29
- CborCodec
40
+ CborCodec,
41
+ SilentObserver,
42
+ EventEmitterObserver,
43
+ StatsCollector,
44
+ FixedChunker,
45
+ CdcChunker,
30
46
  };
31
47
 
32
48
  /**
@@ -58,16 +74,24 @@ export default class ContentAddressableStore {
58
74
  * @param {number} [options.chunkSize] - Chunk size in bytes (default 256 KiB).
59
75
  * @param {import('./src/ports/CodecPort.js').default} [options.codec] - Manifest codec (default JsonCodec).
60
76
  * @param {import('./src/ports/CryptoPort.js').default} [options.crypto] - Crypto adapter (auto-detected if omitted).
77
+ * @param {import('./src/ports/ObservabilityPort.js').default} [options.observability] - Observability adapter (SilentObserver if omitted).
61
78
  * @param {import('@git-stunts/alfred').Policy} [options.policy] - Resilience policy for Git I/O.
62
79
  * @param {number} [options.merkleThreshold=1000] - Chunk count threshold for Merkle manifests.
80
+ * @param {number} [options.concurrency=1] - Maximum parallel chunk I/O operations.
81
+ * @param {{ strategy: string, chunkSize?: number, targetChunkSize?: number, minChunkSize?: number, maxChunkSize?: number }} [options.chunking] - Chunking strategy config.
82
+ * @param {import('./src/ports/ChunkingPort.js').default} [options.chunker] - Pre-built ChunkingPort instance (advanced).
63
83
  */
64
- constructor({ plumbing, chunkSize, codec, policy, crypto, merkleThreshold }) {
84
+ constructor({ plumbing, chunkSize, codec, policy, crypto, observability, merkleThreshold, concurrency, chunking, chunker }) {
65
85
  this.plumbing = plumbing;
66
86
  this.chunkSizeConfig = chunkSize;
67
87
  this.codecConfig = codec;
68
88
  this.policyConfig = policy;
69
89
  this.cryptoConfig = crypto;
90
+ this.observabilityConfig = observability;
70
91
  this.merkleThresholdConfig = merkleThreshold;
92
+ this.concurrencyConfig = concurrency;
93
+ this.chunkingConfig = chunking;
94
+ this.chunkerConfig = chunker;
71
95
  this.service = null;
72
96
  this.#servicePromise = null;
73
97
  }
@@ -88,6 +112,34 @@ export default class ContentAddressableStore {
88
112
  return await this.#servicePromise;
89
113
  }
90
114
 
115
+ /**
116
+ * Resolves the chunker from config options.
117
+ * @private
118
+ * @returns {import('./src/ports/ChunkingPort.js').default|undefined}
119
+ */
120
+ #resolveChunker() {
121
+ // Direct ChunkingPort instance takes precedence
122
+ if (this.chunkerConfig) {
123
+ return this.chunkerConfig;
124
+ }
125
+ // Build from declarative chunking config
126
+ if (this.chunkingConfig) {
127
+ if (this.chunkingConfig.strategy === 'cdc') {
128
+ return new CdcChunker({
129
+ targetChunkSize: this.chunkingConfig.targetChunkSize,
130
+ minChunkSize: this.chunkingConfig.minChunkSize,
131
+ maxChunkSize: this.chunkingConfig.maxChunkSize,
132
+ });
133
+ }
134
+ // 'fixed' or unrecognized — fall through to default (FixedChunker via CasService)
135
+ if (this.chunkingConfig.strategy === 'fixed' && this.chunkingConfig.chunkSize) {
136
+ return new FixedChunker({ chunkSize: this.chunkingConfig.chunkSize });
137
+ }
138
+ }
139
+ // undefined → CasService will default to FixedChunker
140
+ return undefined;
141
+ }
142
+
91
143
  /**
92
144
  * Constructs adapters, resolves crypto, and creates CasService + VaultService.
93
145
  * @private
@@ -99,12 +151,16 @@ export default class ContentAddressableStore {
99
151
  policy: this.policyConfig
100
152
  });
101
153
  const crypto = this.cryptoConfig || await getDefaultCryptoAdapter();
154
+ const chunker = this.#resolveChunker();
102
155
  this.service = new CasService({
103
156
  persistence,
104
157
  chunkSize: this.chunkSizeConfig,
105
158
  codec: this.codecConfig || new JsonCodec(),
106
159
  crypto,
160
+ observability: this.observabilityConfig || new SilentObserver(),
107
161
  merkleThreshold: this.merkleThresholdConfig,
162
+ concurrency: this.concurrencyConfig,
163
+ chunker,
108
164
  });
109
165
 
110
166
  const ref = new GitRefAdapter({
@@ -253,12 +309,17 @@ export default class ContentAddressableStore {
253
309
  */
254
310
  async restoreFile({ manifest, encryptionKey, passphrase, outputPath }) {
255
311
  const service = await this.#getService();
256
- const { buffer, bytesWritten } = await service.restore({
257
- manifest,
258
- encryptionKey,
259
- passphrase,
312
+ const iterable = service.restoreStream({ manifest, encryptionKey, passphrase });
313
+ const readable = Readable.from(iterable);
314
+ const writable = createWriteStream(outputPath);
315
+ let bytesWritten = 0;
316
+ const counter = new Transform({
317
+ transform(chunk, _encoding, cb) {
318
+ bytesWritten += chunk.length;
319
+ cb(null, chunk);
320
+ },
260
321
  });
261
- writeFileSync(outputPath, buffer);
322
+ await pipeline(readable, counter, writable);
262
323
  return { bytesWritten };
263
324
  }
264
325
 
@@ -275,6 +336,19 @@ export default class ContentAddressableStore {
275
336
  return await service.restore(options);
276
337
  }
277
338
 
339
+ /**
340
+ * Restores a file from its manifest as an async iterable of Buffer chunks.
341
+ * @param {Object} options
342
+ * @param {import('./src/domain/value-objects/Manifest.js').default} options.manifest - The file manifest.
343
+ * @param {Buffer} [options.encryptionKey] - 32-byte key, required if manifest is encrypted.
344
+ * @param {string} [options.passphrase] - Passphrase for KDF-based decryption.
345
+ * @returns {AsyncIterable<Buffer>}
346
+ */
347
+ async *restoreStream(options) {
348
+ const service = await this.#getService();
349
+ yield* service.restoreStream(options);
350
+ }
351
+
278
352
  /**
279
353
  * Creates a Git tree object from a manifest.
280
354
  * @param {Object} options
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@git-stunts/git-cas",
3
- "version": "3.1.0",
3
+ "version": "5.0.0",
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",
@@ -38,6 +38,30 @@ export declare const CompressionSchema: z.ZodObject<{
38
38
  algorithm: z.ZodEnum<["gzip"]>;
39
39
  }>;
40
40
 
41
+ /** Validates fixed-size chunking parameters. */
42
+ export declare const FixedChunkingSchema: z.ZodObject<{
43
+ strategy: z.ZodLiteral<"fixed">;
44
+ params: z.ZodObject<{
45
+ chunkSize: z.ZodNumber;
46
+ }>;
47
+ }>;
48
+
49
+ /** Validates content-defined chunking (CDC) parameters. */
50
+ export declare const CdcChunkingSchema: z.ZodObject<{
51
+ strategy: z.ZodLiteral<"cdc">;
52
+ params: z.ZodObject<{
53
+ target: z.ZodNumber;
54
+ min: z.ZodNumber;
55
+ max: z.ZodNumber;
56
+ }>;
57
+ }>;
58
+
59
+ /** Validates chunking metadata (fixed or CDC). */
60
+ export declare const ChunkingSchema: z.ZodDiscriminatedUnion<
61
+ "strategy",
62
+ [typeof FixedChunkingSchema, typeof CdcChunkingSchema]
63
+ >;
64
+
41
65
  /** Validates a sub-manifest reference in a v2 Merkle manifest. */
42
66
  export declare const SubManifestRefSchema: z.ZodObject<{
43
67
  oid: z.ZodString;
@@ -54,5 +78,6 @@ export declare const ManifestSchema: z.ZodObject<{
54
78
  chunks: z.ZodArray<typeof ChunkSchema>;
55
79
  encryption: z.ZodOptional<typeof EncryptionSchema>;
56
80
  compression: z.ZodOptional<typeof CompressionSchema>;
81
+ chunking: z.ZodOptional<typeof ChunkingSchema>;
57
82
  subManifests: z.ZodOptional<z.ZodArray<typeof SubManifestRefSchema>>;
58
83
  }>;
@@ -38,6 +38,30 @@ export const CompressionSchema = z.object({
38
38
  algorithm: z.enum(['gzip']),
39
39
  });
40
40
 
41
+ /** Validates fixed-size chunking parameters. */
42
+ export const FixedChunkingSchema = z.object({
43
+ strategy: z.literal('fixed'),
44
+ params: z.object({
45
+ chunkSize: z.number().int().positive(),
46
+ }),
47
+ });
48
+
49
+ /** Validates content-defined chunking (CDC) parameters. */
50
+ export const CdcChunkingSchema = z.object({
51
+ strategy: z.literal('cdc'),
52
+ params: z.object({
53
+ target: z.number().int().positive(),
54
+ min: z.number().int().positive(),
55
+ max: z.number().int().positive(),
56
+ }),
57
+ });
58
+
59
+ /** Validates chunking metadata (fixed or CDC). */
60
+ export const ChunkingSchema = z.discriminatedUnion('strategy', [
61
+ FixedChunkingSchema,
62
+ CdcChunkingSchema,
63
+ ]);
64
+
41
65
  /** Validates a sub-manifest reference in a v2 Merkle manifest. */
42
66
  export const SubManifestRefSchema = z.object({
43
67
  oid: z.string().min(1),
@@ -54,5 +78,6 @@ export const ManifestSchema = z.object({
54
78
  chunks: z.array(ChunkSchema),
55
79
  encryption: EncryptionSchema.optional(),
56
80
  compression: CompressionSchema.optional(),
81
+ chunking: ChunkingSchema.optional(),
57
82
  subManifests: z.array(SubManifestRefSchema).optional(),
58
83
  });
@@ -3,7 +3,6 @@
3
3
  * Domain service for Content Addressable Storage operations.
4
4
  */
5
5
 
6
- import { EventEmitter } from "node:events";
7
6
  import Manifest from "../value-objects/Manifest.js";
8
7
  import type { EncryptionMeta, CompressionMeta, KdfParams } from "../value-objects/Manifest.js";
9
8
 
@@ -40,13 +39,22 @@ export interface GitPersistencePort {
40
39
  ): Promise<Array<{ mode: string; type: string; oid: string; name: string }>>;
41
40
  }
42
41
 
42
+ /** Port interface for observability (metrics, logging, tracing). */
43
+ export interface ObservabilityPort {
44
+ metric(channel: string, data: Record<string, unknown>): void;
45
+ log(level: "debug" | "info" | "warn" | "error", msg: string, meta?: Record<string, unknown>): void;
46
+ span(name: string): { end(meta?: Record<string, unknown>): void };
47
+ }
48
+
43
49
  /** Constructor options for {@link CasService}. */
44
50
  export interface CasServiceOptions {
45
51
  persistence: GitPersistencePort;
46
52
  codec: CodecPort;
47
53
  crypto: CryptoPort;
54
+ observability: ObservabilityPort;
48
55
  chunkSize?: number;
49
56
  merkleThreshold?: number;
57
+ concurrency?: number;
50
58
  }
51
59
 
52
60
  /** Options for key derivation. */
@@ -74,12 +82,14 @@ export interface DeriveKeyResult {
74
82
  * Provides chunking, encryption, and integrity verification for storing
75
83
  * arbitrary data in Git's object database.
76
84
  */
77
- export default class CasService extends EventEmitter {
85
+ export default class CasService {
78
86
  readonly persistence: GitPersistencePort;
79
87
  readonly codec: CodecPort;
80
88
  readonly crypto: CryptoPort;
89
+ readonly observability: ObservabilityPort;
81
90
  readonly chunkSize: number;
82
91
  readonly merkleThreshold: number;
92
+ readonly concurrency: number;
83
93
 
84
94
  constructor(options: CasServiceOptions);
85
95
 
@@ -112,6 +122,12 @@ export default class CasService extends EventEmitter {
112
122
  passphrase?: string;
113
123
  }): Promise<{ buffer: Buffer; bytesWritten: number }>;
114
124
 
125
+ restoreStream(options: {
126
+ manifest: Manifest;
127
+ encryptionKey?: Buffer;
128
+ passphrase?: string;
129
+ }): AsyncIterable<Buffer>;
130
+
115
131
  readManifest(options: { treeOid: string }): Promise<Manifest>;
116
132
 
117
133
  deleteAsset(options: {