@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.
@@ -0,0 +1,124 @@
1
+ import { utf8ByteLength, utf8Decode } from '../../domain/encoding/utf8.js';
2
+
3
+ const TREE_ENTRY_OVERHEAD_BYTES = 128;
4
+ const TYPE_BY_MODE = new Map([
5
+ ['040000', 'tree'],
6
+ ['100644', 'blob'],
7
+ ['100755', 'blob'],
8
+ ['120000', 'blob'],
9
+ ['160000', 'commit'],
10
+ ]);
11
+
12
+ /**
13
+ * Decodes Git's canonical tree-object bytes at the persistence boundary.
14
+ */
15
+ export default class GitTreeObjectCodec {
16
+ /**
17
+ * @param {Uint8Array} content
18
+ * @param {string} treeOid
19
+ * @returns {{ entries: ReadonlyArray<Readonly<{mode: string, type: string, oid: string, name: string}>>, weight: number }}
20
+ */
21
+ static decode(content, treeOid) {
22
+ const bytes = GitTreeObjectCodec.#bytes(content);
23
+ const oidBytes = GitTreeObjectCodec.#oidBytes(treeOid);
24
+ const entries = [];
25
+ let cursor = 0;
26
+
27
+ while (cursor < bytes.length) {
28
+ const modeEnd = GitTreeObjectCodec.#find(bytes, 0x20, cursor);
29
+ const nameEnd = GitTreeObjectCodec.#find(bytes, 0x00, modeEnd + 1);
30
+ const oidEnd = nameEnd + 1 + oidBytes;
31
+ if (oidEnd > bytes.length) {
32
+ throw new TypeError('Git tree entry has a truncated object identifier');
33
+ }
34
+
35
+ const rawMode = utf8Decode(bytes.subarray(cursor, modeEnd));
36
+ const mode = rawMode.padStart(6, '0');
37
+ const type = TYPE_BY_MODE.get(mode);
38
+ const name = utf8Decode(bytes.subarray(modeEnd + 1, nameEnd));
39
+ if (type === undefined || name.length === 0 || name.includes('/')) {
40
+ throw new TypeError('Git tree entry has an invalid mode or name');
41
+ }
42
+
43
+ entries.push(
44
+ Object.freeze({
45
+ mode,
46
+ type,
47
+ oid: GitTreeObjectCodec.#hex(bytes.subarray(nameEnd + 1, oidEnd)),
48
+ name,
49
+ })
50
+ );
51
+ cursor = oidEnd;
52
+ }
53
+
54
+ const weight =
55
+ bytes.length +
56
+ entries.reduce(
57
+ (total, entry) => total + TREE_ENTRY_OVERHEAD_BYTES + utf8ByteLength(entry.name),
58
+ 0
59
+ );
60
+ return Object.freeze({ entries: Object.freeze(entries), weight });
61
+ }
62
+
63
+ /**
64
+ * Converts the existing `git mktree` line contract into typed session input.
65
+ * @param {string[]} lines
66
+ * @returns {Array<{mode: string, type: string, oid: string, name: string}>}
67
+ */
68
+ static parseMktreeLines(lines) {
69
+ if (!Array.isArray(lines)) {
70
+ throw new TypeError('Git tree entries must be an array');
71
+ }
72
+ return lines.map((line) => {
73
+ if (typeof line !== 'string') {
74
+ throw new TypeError('Git tree entry must be a string');
75
+ }
76
+ const tab = line.indexOf('\t');
77
+ const fields = tab === -1 ? [] : line.slice(0, tab).split(' ');
78
+ const name = tab === -1 ? '' : line.slice(tab + 1);
79
+ const [mode, type, oid] = fields;
80
+ if (
81
+ fields.length !== 3 ||
82
+ TYPE_BY_MODE.get(mode) !== type ||
83
+ !/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u.test(oid) ||
84
+ name.length === 0 ||
85
+ name.includes('\0') ||
86
+ name.includes('/')
87
+ ) {
88
+ throw new TypeError('Git tree entry line is invalid');
89
+ }
90
+ return { mode, type, oid, name };
91
+ });
92
+ }
93
+
94
+ static #bytes(content) {
95
+ if (!(content instanceof Uint8Array)) {
96
+ throw new TypeError('Git tree content must be a Uint8Array');
97
+ }
98
+ return content;
99
+ }
100
+
101
+ static #oidBytes(treeOid) {
102
+ if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u.test(treeOid)) {
103
+ throw new TypeError('Git tree object identifier is invalid');
104
+ }
105
+ return treeOid.length / 2;
106
+ }
107
+
108
+ static #find(bytes, needle, start) {
109
+ for (let index = start; index < bytes.length; index += 1) {
110
+ if (bytes[index] === needle) {
111
+ return index;
112
+ }
113
+ }
114
+ throw new TypeError('Git tree entry is missing a delimiter');
115
+ }
116
+
117
+ static #hex(bytes) {
118
+ let result = '';
119
+ for (const byte of bytes) {
120
+ result += byte.toString(16).padStart(2, '0');
121
+ }
122
+ return result;
123
+ }
124
+ }
@@ -1 +1 @@
1
- export const PACKAGE_VERSION = '6.5.0';
1
+ export const PACKAGE_VERSION = '6.5.2';
@@ -12,6 +12,20 @@ export default class GitPersistencePort {
12
12
  throw new Error('Not implemented');
13
13
  }
14
14
 
15
+ /**
16
+ * Writes a bounded group of blobs. Adapters may override this to amortize
17
+ * process startup while preserving visibility before the method resolves.
18
+ * @param {Iterable<Uint8Array>} contents
19
+ * @returns {Promise<string[]>}
20
+ */
21
+ async writeBlobs(contents) {
22
+ const oids = [];
23
+ for (const content of contents) {
24
+ oids.push(await this.writeBlob(content));
25
+ }
26
+ return oids;
27
+ }
28
+
15
29
  /**
16
30
  * Creates a Git tree object from formatted entries.
17
31
  * @param {string[]} _entries - Lines in `git mktree` format.
@@ -87,4 +101,16 @@ export default class GitPersistencePort {
87
101
  async readObjectSize(_oid) {
88
102
  throw new Error('Not implemented');
89
103
  }
104
+
105
+ /**
106
+ * Releases adapter-owned local resources. The default implementation is a
107
+ * no-op so persistence adapters without long-lived resources remain valid.
108
+ * @returns {Promise<void>}
109
+ */
110
+ async close() {}
111
+
112
+ /** @returns {Promise<void>} */
113
+ async [Symbol.asyncDispose]() {
114
+ await this.close();
115
+ }
90
116
  }
@@ -9,12 +9,54 @@ declare module '@git-stunts/plumbing' {
9
9
  env?: Record<string, string | undefined>;
10
10
  }
11
11
 
12
- interface StreamResult {
12
+ interface StreamResult extends AsyncIterable<Uint8Array | string> {
13
13
  collect(options?: { asString?: boolean; maxBytes?: number }): Promise<string | Uint8Array>;
14
+ destroy(): Promise<void>;
15
+ finished: Promise<{ code: number; stderr: string }>;
14
16
  }
15
17
 
16
18
  type ShellRunner = (options: Record<string, unknown>) => Promise<unknown>;
17
19
 
20
+ interface GitObjectInfo {
21
+ oid: string;
22
+ type: string;
23
+ size: number;
24
+ }
25
+
26
+ interface GitObject extends GitObjectInfo {
27
+ content: Uint8Array;
28
+ }
29
+
30
+ interface GitCatFileSession {
31
+ info(objectName: string): Promise<GitObjectInfo>;
32
+ read(objectName: string, options?: { maxBytes?: number }): Promise<GitObject>;
33
+ close(): Promise<void>;
34
+ terminate(): Promise<void>;
35
+ }
36
+
37
+ interface GitMktreeSession {
38
+ write(
39
+ entries: Array<{ mode: string; type: string; oid: string; name: string }>
40
+ ): Promise<string>;
41
+ close(): Promise<void>;
42
+ terminate(): Promise<void>;
43
+ }
44
+
45
+ interface GitFastImportSession {
46
+ writeBlob(content: string | Uint8Array): Promise<string>;
47
+ checkpoint(): Promise<void>;
48
+ close(): Promise<void>;
49
+ abort(): Promise<void>;
50
+ }
51
+
52
+ export class GitObjectMissingError extends Error {
53
+ details?: Record<string, unknown>;
54
+ }
55
+
56
+ export class GitProtocolError extends Error {}
57
+
58
+ export class InvalidArgumentError extends Error {}
59
+
18
60
  export class ShellRunnerFactory {
19
61
  static ENV_BUN: 'bun';
20
62
  static ENV_DENO: 'deno';
@@ -29,6 +71,9 @@ declare module '@git-stunts/plumbing' {
29
71
  static createRepository(options?: { cwd?: string; env?: string }): Promise<unknown>;
30
72
  execute(options: ExecuteOptions): Promise<string>;
31
73
  executeStream(options: ExecuteOptions): Promise<StreamResult>;
74
+ openCatFileSession(): Promise<GitCatFileSession>;
75
+ openMktreeSession(): Promise<GitMktreeSession>;
76
+ openFastImportSession(): Promise<GitFastImportSession>;
32
77
  }
33
78
  }
34
79