@leed-ai/git-gateway-testkit 0.0.0-stage → 0.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/LICENSE +201 -0
- package/README.md +19 -2
- package/dist/contract-suite/ancestry-cases.d.ts +4 -0
- package/dist/contract-suite/ancestry-cases.js +49 -0
- package/dist/contract-suite/ancestry-cases.js.map +1 -0
- package/dist/contract-suite/backend-case.d.ts +35 -0
- package/dist/contract-suite/backend-case.js +17 -0
- package/dist/contract-suite/backend-case.js.map +1 -0
- package/dist/contract-suite/backend-contract-suite.d.ts +44 -0
- package/dist/contract-suite/backend-contract-suite.js +59 -0
- package/dist/contract-suite/backend-contract-suite.js.map +1 -0
- package/dist/contract-suite/backend-fixtures.d.ts +25 -0
- package/dist/contract-suite/backend-fixtures.js +79 -0
- package/dist/contract-suite/backend-fixtures.js.map +1 -0
- package/dist/contract-suite/content-cases.d.ts +4 -0
- package/dist/contract-suite/content-cases.js +31 -0
- package/dist/contract-suite/content-cases.js.map +1 -0
- package/dist/contract-suite/lifecycle-cases.d.ts +4 -0
- package/dist/contract-suite/lifecycle-cases.js +86 -0
- package/dist/contract-suite/lifecycle-cases.js.map +1 -0
- package/dist/contract-suite/push-cases.d.ts +4 -0
- package/dist/contract-suite/push-cases.js +92 -0
- package/dist/contract-suite/push-cases.js.map +1 -0
- package/dist/contract-suite/push-helpers.d.ts +17 -0
- package/dist/contract-suite/push-helpers.js +47 -0
- package/dist/contract-suite/push-helpers.js.map +1 -0
- package/dist/contract-suite/read-cases.d.ts +4 -0
- package/dist/contract-suite/read-cases.js +186 -0
- package/dist/contract-suite/read-cases.js.map +1 -0
- package/dist/contract-suite/transport-cases.d.ts +4 -0
- package/dist/contract-suite/transport-cases.js +105 -0
- package/dist/contract-suite/transport-cases.js.map +1 -0
- package/dist/git-binary.d.ts +19 -0
- package/dist/git-binary.js +54 -0
- package/dist/git-binary.js.map +1 -0
- package/dist/git-sandbox.d.ts +33 -0
- package/dist/git-sandbox.js +76 -0
- package/dist/git-sandbox.js.map +1 -0
- package/dist/memory/memory-backend.d.ts +45 -0
- package/dist/memory/memory-backend.js +154 -0
- package/dist/memory/memory-backend.js.map +1 -0
- package/dist/memory/memory-reads.d.ts +9 -0
- package/dist/memory/memory-reads.js +39 -0
- package/dist/memory/memory-reads.js.map +1 -0
- package/dist/memory/memory-repository.d.ts +38 -0
- package/dist/memory/memory-repository.js +97 -0
- package/dist/memory/memory-repository.js.map +1 -0
- package/dist/memory/memory-tokens.d.ts +28 -0
- package/dist/memory/memory-tokens.js +59 -0
- package/dist/memory/memory-tokens.js.map +1 -0
- package/dist/memory/pack-selection.d.ts +30 -0
- package/dist/memory/pack-selection.js +104 -0
- package/dist/memory/pack-selection.js.map +1 -0
- package/dist/memory/receive-pack-service.d.ts +15 -0
- package/dist/memory/receive-pack-service.js +126 -0
- package/dist/memory/receive-pack-service.js.map +1 -0
- package/dist/memory/smart-http-server.d.ts +15 -0
- package/dist/memory/smart-http-server.js +65 -0
- package/dist/memory/smart-http-server.js.map +1 -0
- package/dist/memory/upload-pack-service.d.ts +15 -0
- package/dist/memory/upload-pack-service.js +123 -0
- package/dist/memory/upload-pack-service.js.map +1 -0
- package/dist/node/serve-fetch-handler.d.ts +16 -0
- package/dist/node/serve-fetch-handler.js +52 -0
- package/dist/node/serve-fetch-handler.js.map +1 -0
- package/dist/node/serve-streaming.d.ts +16 -0
- package/dist/node/serve-streaming.js +87 -0
- package/dist/node/serve-streaming.js.map +1 -0
- package/package.json +175 -4
- package/src/contract-suite/ancestry-cases.ts +56 -0
- package/src/contract-suite/backend-case.ts +53 -0
- package/src/contract-suite/backend-contract-suite.ts +89 -0
- package/src/contract-suite/backend-fixtures.ts +89 -0
- package/src/contract-suite/content-cases.ts +55 -0
- package/src/contract-suite/lifecycle-cases.ts +121 -0
- package/src/contract-suite/push-cases.ts +123 -0
- package/src/contract-suite/push-helpers.ts +63 -0
- package/src/contract-suite/read-cases.ts +213 -0
- package/src/contract-suite/transport-cases.ts +134 -0
- package/src/git-binary.ts +65 -0
- package/src/git-sandbox.ts +87 -0
- package/src/memory/memory-backend.ts +188 -0
- package/src/memory/memory-reads.ts +52 -0
- package/src/memory/memory-repository.ts +115 -0
- package/src/memory/memory-tokens.ts +64 -0
- package/src/memory/pack-selection.ts +127 -0
- package/src/memory/receive-pack-service.ts +141 -0
- package/src/memory/smart-http-server.ts +78 -0
- package/src/memory/upload-pack-service.ts +132 -0
- package/src/node/serve-fetch-handler.ts +71 -0
- package/src/node/serve-streaming.ts +107 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { Commit } from '@leed-ai/git-gateway-contract/schemas/commit';
|
|
3
|
+
import { FilePath, RepoName, Revision } from '@leed-ai/git-gateway-contract/schemas/names';
|
|
4
|
+
import { Sha } from '@leed-ai/git-gateway-contract/schemas/sha';
|
|
5
|
+
import { GitAccess, GitRemote } from '@leed-ai/git-gateway-contract/storage/git-remote';
|
|
6
|
+
import type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';
|
|
7
|
+
import { repoExists, repoNotFound } from '@leed-ai/git-gateway-contract/storage/storage-errors';
|
|
8
|
+
import { parseStorageInput } from '@leed-ai/git-gateway-contract/storage/storage-input';
|
|
9
|
+
import { StorageLogOptions, StorageRef } from '@leed-ai/git-gateway-contract/storage/storage-read';
|
|
10
|
+
import {
|
|
11
|
+
CreateStorageRepoOptions,
|
|
12
|
+
StorageRepoInfo,
|
|
13
|
+
} from '@leed-ai/git-gateway-contract/storage/storage-repo-info';
|
|
14
|
+
import { StorageTreeEntry } from '@leed-ai/git-gateway-contract/storage/storage-tree-entry';
|
|
15
|
+
import type { FetchLike } from '@leed-ai/git-gateway-core/smart-http/git-http-client';
|
|
16
|
+
|
|
17
|
+
import { fileAt, firstParentLog, resolveRevision } from './memory-reads.ts';
|
|
18
|
+
import { MemoryRepository } from './memory-repository.ts';
|
|
19
|
+
import { MemoryTokens } from './memory-tokens.ts';
|
|
20
|
+
import { serveSmartHttp } from './smart-http-server.ts';
|
|
21
|
+
|
|
22
|
+
/** Options of a {@link MemoryBackend}. */
|
|
23
|
+
export interface MemoryBackendOptions {
|
|
24
|
+
/** Base URL of the git remotes it hands out; default "http://memory.git-gateway.invalid". */
|
|
25
|
+
readonly baseUrl?: string;
|
|
26
|
+
/** The clock for creation times and token expiry; default the system clock. */
|
|
27
|
+
readonly now?: () => Date;
|
|
28
|
+
/** Lifetime of the tokens gitRemote() mints, in seconds; default 300. */
|
|
29
|
+
readonly tokenTtlSeconds?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const TYPE_OF_MODE = { '040000': 'tree', '160000': 'commit' } as const;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A complete {@link StorageBackend} held in memory, with a real git smart-HTTP server (protocol
|
|
36
|
+
* v0/v1 upload-pack and receive-pack) in {@link handle}, so the git binary and the gateway can
|
|
37
|
+
* push and fetch against it. Serve {@link handle} over HTTP for a git client, or pass
|
|
38
|
+
* {@link fetch} wherever a FetchLike is taken.
|
|
39
|
+
*/
|
|
40
|
+
export class MemoryBackend implements StorageBackend {
|
|
41
|
+
readonly #repos = new Map<string, MemoryRepository>();
|
|
42
|
+
readonly #tokens: MemoryTokens;
|
|
43
|
+
readonly #now: () => Date;
|
|
44
|
+
readonly #ttl: number;
|
|
45
|
+
#baseUrl: string;
|
|
46
|
+
|
|
47
|
+
constructor(options: MemoryBackendOptions = {}) {
|
|
48
|
+
this.#now = options.now ?? (() => new Date());
|
|
49
|
+
this.#tokens = new MemoryTokens(this.#now);
|
|
50
|
+
this.#ttl = options.tokenTtlSeconds ?? 300;
|
|
51
|
+
this.#baseUrl = (options.baseUrl ?? 'http://memory.git-gateway.invalid').replace(/\/$/, '');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Sets the base URL of the remotes handed out from now on (e.g. once an HTTP server has a port). */
|
|
55
|
+
setBaseUrl(baseUrl: string): void {
|
|
56
|
+
this.#baseUrl = baseUrl.replace(/\/$/, '');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Answers a git smart-HTTP request for one of the repositories. */
|
|
60
|
+
readonly handle = async (request: Request): Promise<Response> =>
|
|
61
|
+
serveSmartHttp({ tokens: this.#tokens, repository: (name) => this.#repos.get(name) }, request);
|
|
62
|
+
|
|
63
|
+
/** {@link handle} as a FetchLike, for clients that would otherwise call the network. */
|
|
64
|
+
readonly fetch: FetchLike = async (request) => this.handle(request);
|
|
65
|
+
|
|
66
|
+
async createRepo(name: string, options: CreateStorageRepoOptions = {}): Promise<StorageRepoInfo> {
|
|
67
|
+
const repo = parseStorageInput(RepoName, name);
|
|
68
|
+
const { defaultBranch } = parseStorageInput(CreateStorageRepoOptions, options);
|
|
69
|
+
if (this.#repos.has(repo)) throw repoExists(repo);
|
|
70
|
+
const created = new MemoryRepository(repo, defaultBranch, this.#now().toISOString());
|
|
71
|
+
this.#repos.set(repo, created);
|
|
72
|
+
return this.#info(created);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async deleteRepo(name: string): Promise<void> {
|
|
76
|
+
const repo = parseStorageInput(RepoName, name);
|
|
77
|
+
this.#repos.delete(repo);
|
|
78
|
+
this.#tokens.revokeRepo(repo);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async repoInfo(name: string): Promise<StorageRepoInfo | null> {
|
|
82
|
+
const repo = this.#repos.get(parseStorageInput(RepoName, name));
|
|
83
|
+
return repo === undefined ? null : this.#info(repo);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async resolveRef(name: string, ref: string): Promise<string | null> {
|
|
87
|
+
const repoName = parseStorageInput(RepoName, name);
|
|
88
|
+
const parsed = parseStorageInput(StorageRef, ref);
|
|
89
|
+
const repo = this.#require(repoName);
|
|
90
|
+
return repo.refs.get(parsed.startsWith('refs/') ? parsed : `refs/heads/${parsed}`) ?? null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async readCommit(name: string, sha: string): Promise<Commit | null> {
|
|
94
|
+
const repoName = parseStorageInput(RepoName, name);
|
|
95
|
+
const id = parseStorageInput(Sha, sha);
|
|
96
|
+
const commit = this.#require(repoName).commit(id);
|
|
97
|
+
return commit === null ? null : Commit.parse(commit);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async readTree(name: string, sha: string): Promise<StorageTreeEntry[] | null> {
|
|
101
|
+
const repoName = parseStorageInput(RepoName, name);
|
|
102
|
+
const id = parseStorageInput(Sha, sha);
|
|
103
|
+
const entries = this.#require(repoName).tree(id);
|
|
104
|
+
if (entries === null) return null;
|
|
105
|
+
return entries.map((entry) =>
|
|
106
|
+
StorageTreeEntry.parse({
|
|
107
|
+
...entry,
|
|
108
|
+
type: entry.mode === '040000' || entry.mode === '160000' ? TYPE_OF_MODE[entry.mode] : 'blob',
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async readBlob(name: string, sha: string): Promise<Uint8Array | null> {
|
|
114
|
+
const repoName = parseStorageInput(RepoName, name);
|
|
115
|
+
const id = parseStorageInput(Sha, sha);
|
|
116
|
+
const blob = this.#require(repoName).object(id, 'blob');
|
|
117
|
+
return blob === null ? null : blob.data.slice();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async readFile(name: string, ref: string, path: string): Promise<Uint8Array | null> {
|
|
121
|
+
const repoName = parseStorageInput(RepoName, name);
|
|
122
|
+
const revision = parseStorageInput(Revision, ref);
|
|
123
|
+
const file = parseStorageInput(FilePath, path);
|
|
124
|
+
const repo = this.#require(repoName);
|
|
125
|
+
const commit = resolveRevision(repo, revision);
|
|
126
|
+
const data = commit === null ? null : fileAt(repo, commit, file);
|
|
127
|
+
return data === null ? null : data.slice();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async log(name: string, ref: string, options: StorageLogOptions): Promise<Commit[]> {
|
|
131
|
+
const repoName = parseStorageInput(RepoName, name);
|
|
132
|
+
const revision = parseStorageInput(Revision, ref);
|
|
133
|
+
const { limit, offset } = parseStorageInput(StorageLogOptions, options);
|
|
134
|
+
const repo = this.#require(repoName);
|
|
135
|
+
const start = resolveRevision(repo, revision);
|
|
136
|
+
return start === null
|
|
137
|
+
? []
|
|
138
|
+
: firstParentLog(repo, start, limit, offset).map((commit) => Commit.parse(commit));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Walks parents from `descendant`; null when either commit is not in the repository. */
|
|
142
|
+
async isAncestor(name: string, ancestor: string, descendant: string): Promise<boolean | null> {
|
|
143
|
+
const repoName = parseStorageInput(RepoName, name);
|
|
144
|
+
const older = parseStorageInput(Sha, ancestor);
|
|
145
|
+
const newer = parseStorageInput(Sha, descendant);
|
|
146
|
+
const repo = this.#require(repoName);
|
|
147
|
+
if (repo.commit(older) === null || repo.commit(newer) === null) return null;
|
|
148
|
+
const seen = new Set([newer]);
|
|
149
|
+
const queue = [newer];
|
|
150
|
+
for (let sha = queue.shift(); sha !== undefined; sha = queue.shift()) {
|
|
151
|
+
if (sha === older) return true;
|
|
152
|
+
for (const parent of repo.commit(sha)?.parents ?? []) {
|
|
153
|
+
if (!seen.has(parent)) {
|
|
154
|
+
seen.add(parent);
|
|
155
|
+
queue.push(parent);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async gitRemote(name: string, access: string): Promise<GitRemote> {
|
|
163
|
+
const repoName = parseStorageInput(RepoName, name);
|
|
164
|
+
const level = parseStorageInput(GitAccess, access);
|
|
165
|
+
const repo = this.#require(repoName);
|
|
166
|
+
const minted = this.#tokens.mint(repo.name, level, this.#ttl);
|
|
167
|
+
return GitRemote.parse({
|
|
168
|
+
url: `${this.#baseUrl}/${repo.name}.git`,
|
|
169
|
+
headers: { Authorization: `Bearer ${minted.token}` },
|
|
170
|
+
expiresAt: minted.expiresAt.toISOString(),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The repository `repo` (already parsed with RepoName, as every other argument must be first). */
|
|
175
|
+
#require(repo: string): MemoryRepository {
|
|
176
|
+
const found = this.#repos.get(repo);
|
|
177
|
+
if (found === undefined) throw repoNotFound(repo);
|
|
178
|
+
return found;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
#info(repo: MemoryRepository): StorageRepoInfo {
|
|
182
|
+
return StorageRepoInfo.parse({
|
|
183
|
+
name: repo.name,
|
|
184
|
+
defaultBranch: repo.defaultBranch,
|
|
185
|
+
createdAt: repo.createdAt,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { Commit } from '@leed-ai/git-gateway-contract/schemas/commit';
|
|
3
|
+
|
|
4
|
+
import type { MemoryRepository } from './memory-repository.ts';
|
|
5
|
+
|
|
6
|
+
const SHA = /^[0-9a-f]{40}$/;
|
|
7
|
+
|
|
8
|
+
/** The commit a revision names: a commit id as is, otherwise the branch "refs/heads/<name>". */
|
|
9
|
+
export function resolveRevision(repo: MemoryRepository, revision: string): string | null {
|
|
10
|
+
if (SHA.test(revision)) return repo.object(revision, 'commit') === null ? null : revision;
|
|
11
|
+
return repo.refs.get(`refs/heads/${revision}`) ?? null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The blob at `path` beneath a commit's tree, or null when there is no file there. */
|
|
15
|
+
export function fileAt(repo: MemoryRepository, commitSha: string, path: string): Uint8Array | null {
|
|
16
|
+
const commit = repo.commit(commitSha);
|
|
17
|
+
if (commit === null) return null;
|
|
18
|
+
let tree = commit.tree;
|
|
19
|
+
const segments = path.split('/');
|
|
20
|
+
for (const [index, segment] of segments.entries()) {
|
|
21
|
+
const entry = repo.tree(tree)?.find((candidate) => candidate.name === segment);
|
|
22
|
+
if (entry === undefined) return null;
|
|
23
|
+
if (index === segments.length - 1) {
|
|
24
|
+
return entry.mode === '040000' || entry.mode === '160000'
|
|
25
|
+
? null
|
|
26
|
+
: (repo.object(entry.sha, 'blob')?.data ?? null);
|
|
27
|
+
}
|
|
28
|
+
if (entry.mode !== '040000') return null;
|
|
29
|
+
tree = entry.sha;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The first-parent chain from a commit, newest first: `offset` skipped, then up to `limit`. */
|
|
35
|
+
export function firstParentLog(
|
|
36
|
+
repo: MemoryRepository,
|
|
37
|
+
start: string,
|
|
38
|
+
limit: number,
|
|
39
|
+
offset: number,
|
|
40
|
+
): Commit[] {
|
|
41
|
+
const out: Commit[] = [];
|
|
42
|
+
let index = 0;
|
|
43
|
+
let next: string | undefined = start;
|
|
44
|
+
while (next !== undefined && out.length < limit) {
|
|
45
|
+
const commit = repo.commit(next);
|
|
46
|
+
if (commit === null) break;
|
|
47
|
+
if (index >= offset) out.push(commit);
|
|
48
|
+
index += 1;
|
|
49
|
+
next = commit.parents[0];
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { Commit } from '@leed-ai/git-gateway-contract/schemas/commit';
|
|
3
|
+
import { decodeCommit } from '@leed-ai/git-gateway-core/objects/commit-object';
|
|
4
|
+
import { decodeTree } from '@leed-ai/git-gateway-core/objects/tree-object';
|
|
5
|
+
import type { GitObject, GitTreeEntry } from '@leed-ai/git-gateway-core/schemas/git-object';
|
|
6
|
+
|
|
7
|
+
/** One repository held in memory: its objects, its refs and the branch HEAD names. */
|
|
8
|
+
export class MemoryRepository {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly defaultBranch: string;
|
|
11
|
+
readonly createdAt: string;
|
|
12
|
+
/** Every object by id. Objects are never removed, like a git repository before gc. */
|
|
13
|
+
readonly objects = new Map<string, GitObject>();
|
|
14
|
+
/** Every ref by full name ("refs/heads/main") to the id it names. */
|
|
15
|
+
readonly refs = new Map<string, string>();
|
|
16
|
+
/** Ids whose whole closure (everything they reach) is known to be present. */
|
|
17
|
+
readonly #complete = new Set<string>();
|
|
18
|
+
|
|
19
|
+
constructor(name: string, defaultBranch: string, createdAt: string) {
|
|
20
|
+
this.name = name;
|
|
21
|
+
this.defaultBranch = defaultBranch;
|
|
22
|
+
this.createdAt = createdAt;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The full name of the ref HEAD points at. */
|
|
26
|
+
get headRef(): string {
|
|
27
|
+
return `refs/heads/${this.defaultBranch}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Stores objects; an id already present is kept as it is. */
|
|
31
|
+
put(objects: readonly GitObject[]): void {
|
|
32
|
+
for (const object of objects) if (!this.objects.has(object.sha)) this.objects.set(object.sha, object);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** An object of a given type by id, or null. */
|
|
36
|
+
object(sha: string, type?: GitObject['type']): GitObject | null {
|
|
37
|
+
const object = this.objects.get(sha);
|
|
38
|
+
return object === undefined || (type !== undefined && object.type !== type) ? null : object;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A commit by id, decoded, or null. */
|
|
42
|
+
commit(sha: string): Commit | null {
|
|
43
|
+
const object = this.object(sha, 'commit');
|
|
44
|
+
return object === null ? null : decodeCommit(sha, object.data);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A tree's entries by id, decoded, or null. */
|
|
48
|
+
tree(sha: string): GitTreeEntry[] | null {
|
|
49
|
+
const object = this.object(sha, 'tree');
|
|
50
|
+
return object === null ? null : decodeTree(object.data, sha);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The ids an object links to: a commit's tree and parents, a tree's entries (submodules excluded). */
|
|
54
|
+
links(object: GitObject): string[] {
|
|
55
|
+
if (object.type === 'commit') {
|
|
56
|
+
const commit = decodeCommit(object.sha, object.data);
|
|
57
|
+
return [commit.tree, ...commit.parents];
|
|
58
|
+
}
|
|
59
|
+
if (object.type === 'tree') {
|
|
60
|
+
return decodeTree(object.data, object.sha)
|
|
61
|
+
.filter((entry) => entry.mode !== '160000')
|
|
62
|
+
.map((entry) => entry.sha);
|
|
63
|
+
}
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The ids of objects reachable from `starts` that are missing, stopping at objects already
|
|
69
|
+
* known complete. Records every fully present closure as complete, so later checks are cheap.
|
|
70
|
+
*/
|
|
71
|
+
missingFrom(starts: readonly string[]): string[] {
|
|
72
|
+
const missing: string[] = [];
|
|
73
|
+
const seen = new Set<string>();
|
|
74
|
+
const visited: string[] = [];
|
|
75
|
+
const stack = [...starts];
|
|
76
|
+
while (stack.length > 0) {
|
|
77
|
+
const sha = stack.pop() ?? '';
|
|
78
|
+
if (seen.has(sha) || this.#complete.has(sha)) continue;
|
|
79
|
+
seen.add(sha);
|
|
80
|
+
const object = this.objects.get(sha);
|
|
81
|
+
if (object === undefined) {
|
|
82
|
+
missing.push(sha);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
visited.push(sha);
|
|
86
|
+
stack.push(...this.links(object));
|
|
87
|
+
}
|
|
88
|
+
if (missing.length === 0) for (const sha of visited) this.#complete.add(sha);
|
|
89
|
+
return missing;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Every object reachable from `wants`, leaving out everything reachable from `haves`. */
|
|
93
|
+
reachable(wants: readonly string[], haves: readonly string[]): GitObject[] {
|
|
94
|
+
const excluded = this.#closure(
|
|
95
|
+
haves.filter((sha) => this.objects.has(sha)),
|
|
96
|
+
new Set(),
|
|
97
|
+
);
|
|
98
|
+
const included = this.#closure(wants, excluded);
|
|
99
|
+
return [...included].flatMap((sha) => this.objects.get(sha) ?? []);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
#closure(starts: readonly string[], stop: ReadonlySet<string>): Set<string> {
|
|
103
|
+
const out = new Set<string>();
|
|
104
|
+
const stack = [...starts];
|
|
105
|
+
while (stack.length > 0) {
|
|
106
|
+
const sha = stack.pop() ?? '';
|
|
107
|
+
if (out.has(sha) || stop.has(sha)) continue;
|
|
108
|
+
const object = this.objects.get(sha);
|
|
109
|
+
if (object === undefined) continue;
|
|
110
|
+
out.add(sha);
|
|
111
|
+
stack.push(...this.links(object));
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { GitAccess } from '@leed-ai/git-gateway-contract/storage/git-remote';
|
|
3
|
+
|
|
4
|
+
/** A minted credential: which repository it opens, for what, and until when. */
|
|
5
|
+
export interface MemoryGrant {
|
|
6
|
+
readonly repo: string;
|
|
7
|
+
readonly access: GitAccess;
|
|
8
|
+
/** Expiry in milliseconds since the epoch. */
|
|
9
|
+
readonly expiresAt: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Short-lived bearer tokens for the in-memory git remotes, scoped to one repository and access level. */
|
|
13
|
+
export class MemoryTokens {
|
|
14
|
+
readonly #grants = new Map<string, MemoryGrant>();
|
|
15
|
+
readonly #now: () => Date;
|
|
16
|
+
|
|
17
|
+
constructor(now: () => Date) {
|
|
18
|
+
this.#now = now;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Mints a random token for `repo` that lasts `ttlSeconds`. */
|
|
22
|
+
mint(repo: string, access: GitAccess, ttlSeconds: number): { token: string; expiresAt: Date } {
|
|
23
|
+
const token = `mem_${crypto.randomUUID().replaceAll('-', '')}`;
|
|
24
|
+
const expiresAt = this.#now().getTime() + ttlSeconds * 1000;
|
|
25
|
+
this.#grants.set(token, { repo, access, expiresAt });
|
|
26
|
+
return { token, expiresAt: new Date(expiresAt) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The live grant a token carries, or null when it is unknown or expired. */
|
|
30
|
+
check(token: string): MemoryGrant | null {
|
|
31
|
+
const grant = this.#grants.get(token);
|
|
32
|
+
if (grant === undefined) return null;
|
|
33
|
+
if (grant.expiresAt <= this.#now().getTime()) {
|
|
34
|
+
this.#grants.delete(token);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return grant;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Revokes every token of a repository (when it is deleted). */
|
|
41
|
+
revokeRepo(repo: string): void {
|
|
42
|
+
for (const [token, grant] of this.#grants) if (grant.repo === repo) this.#grants.delete(token);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The credential a request carries: a Bearer token, or the password of HTTP Basic (git sends
|
|
48
|
+
* credentials as Basic; the password is the token). Null when there is none.
|
|
49
|
+
*/
|
|
50
|
+
export function credentialOf(request: Request): string | null {
|
|
51
|
+
const header = request.headers.get('Authorization');
|
|
52
|
+
if (header === null) return null;
|
|
53
|
+
const [scheme, value] = header.split(' ', 2);
|
|
54
|
+
if (value === undefined) return null;
|
|
55
|
+
if (scheme?.toLowerCase() === 'bearer') return value;
|
|
56
|
+
if (scheme?.toLowerCase() !== 'basic') return null;
|
|
57
|
+
try {
|
|
58
|
+
const decoded = atob(value);
|
|
59
|
+
const colon = decoded.indexOf(':');
|
|
60
|
+
return colon === -1 ? null : decoded.slice(colon + 1);
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { GitObject } from '@leed-ai/git-gateway-core/schemas/git-object';
|
|
3
|
+
|
|
4
|
+
import type { MemoryRepository } from './memory-repository.ts';
|
|
5
|
+
|
|
6
|
+
/** What an upload-pack request asks to have sent. */
|
|
7
|
+
export interface PackSelection {
|
|
8
|
+
readonly wants: readonly string[];
|
|
9
|
+
readonly haves: readonly string[];
|
|
10
|
+
/** Commits the client says it has without their parents (its "shallow" lines). */
|
|
11
|
+
readonly clientShallow?: readonly string[];
|
|
12
|
+
/** "deepen <n>": commits at most this many generations from each wanted commit. */
|
|
13
|
+
readonly depth?: number;
|
|
14
|
+
/** A partial-clone filter: "blob:none", "blob:limit=<n>[kmg]" or "tree:<depth>". */
|
|
15
|
+
readonly filter?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The objects to send and the shallow boundary to report. */
|
|
19
|
+
export interface SelectedPack {
|
|
20
|
+
readonly objects: GitObject[];
|
|
21
|
+
/** Commits the client must now treat as having no parents (the new deepen boundary). */
|
|
22
|
+
readonly shallow: string[];
|
|
23
|
+
/** Commits of the client's old boundary whose parents are now sent. */
|
|
24
|
+
readonly unshallow: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const UNITS: Readonly<Record<string, number>> = { '': 1, k: 1024, m: 1024 ** 2, g: 1024 ** 3 };
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether an object `depth` levels below a root tree passes the filter. Explicit wants always pass.
|
|
31
|
+
* As in git's list-objects-filter.c, blob:limit=<n> keeps only blobs strictly smaller than n bytes.
|
|
32
|
+
*/
|
|
33
|
+
function passesFilter(filter: string | undefined, object: GitObject, depth: number): boolean {
|
|
34
|
+
if (filter === undefined) return true;
|
|
35
|
+
if (filter === 'blob:none') return object.type !== 'blob';
|
|
36
|
+
const limit = /^blob:limit=(\d+)([kmg]?)$/.exec(filter);
|
|
37
|
+
if (limit !== null)
|
|
38
|
+
return object.type !== 'blob' || object.data.length < Number(limit[1]) * (UNITS[limit[2] ?? ''] ?? 1);
|
|
39
|
+
const tree = /^tree:(\d+)$/.exec(filter);
|
|
40
|
+
return tree === null || depth < Number(tree[1]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Walks commits from the wants: within `depth` generations when deepening (through commits the
|
|
45
|
+
* client already has, which are not sent), otherwise until reaching what the client has. Returns
|
|
46
|
+
* the commits to send and the boundary commits whose parents are cut off.
|
|
47
|
+
*/
|
|
48
|
+
function selectCommits(
|
|
49
|
+
repo: MemoryRepository,
|
|
50
|
+
wants: readonly string[],
|
|
51
|
+
depth: number | undefined,
|
|
52
|
+
excluded: ReadonlySet<string>,
|
|
53
|
+
): { commits: string[]; boundary: string[]; walked: Set<string> } {
|
|
54
|
+
const commits: string[] = [];
|
|
55
|
+
const boundary: string[] = [];
|
|
56
|
+
const walked = new Set<string>();
|
|
57
|
+
let frontier = wants.filter((sha) => repo.object(sha, 'commit') !== null);
|
|
58
|
+
for (let generation = 1; frontier.length > 0; generation += 1) {
|
|
59
|
+
const next: string[] = [];
|
|
60
|
+
for (const sha of frontier) {
|
|
61
|
+
if (walked.has(sha) || (depth === undefined && excluded.has(sha))) continue;
|
|
62
|
+
walked.add(sha);
|
|
63
|
+
if (!excluded.has(sha)) commits.push(sha);
|
|
64
|
+
const parents = repo.commit(sha)?.parents ?? [];
|
|
65
|
+
if (depth !== undefined && generation >= depth) {
|
|
66
|
+
if (parents.length > 0) boundary.push(sha);
|
|
67
|
+
} else next.push(...parents);
|
|
68
|
+
}
|
|
69
|
+
frontier = next;
|
|
70
|
+
}
|
|
71
|
+
return { commits, boundary, walked };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Everything the client has: the closure of its haves, not crossing its shallow boundary. */
|
|
75
|
+
function clientHas(
|
|
76
|
+
repo: MemoryRepository,
|
|
77
|
+
haves: readonly string[],
|
|
78
|
+
clientShallow: ReadonlySet<string>,
|
|
79
|
+
): Set<string> {
|
|
80
|
+
const out = new Set<string>();
|
|
81
|
+
const stack = haves.filter((sha) => repo.objects.has(sha));
|
|
82
|
+
while (stack.length > 0) {
|
|
83
|
+
const sha = stack.pop() ?? '';
|
|
84
|
+
if (out.has(sha)) continue;
|
|
85
|
+
const object = repo.objects.get(sha);
|
|
86
|
+
if (object === undefined) continue;
|
|
87
|
+
out.add(sha);
|
|
88
|
+
const links = repo.links(object);
|
|
89
|
+
stack.push(...(object.type === 'commit' && clientShallow.has(sha) ? links.slice(0, 1) : links));
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Chooses the objects an upload-pack answer carries: everything reachable from the wants and not
|
|
96
|
+
* from the haves (whose history stops at the client's shallow commits), cut at `depth`
|
|
97
|
+
* generations of commits when deepening, and with the filter
|
|
98
|
+
* applied to trees and blobs (depth counted from each root tree, a wanted tree being a root).
|
|
99
|
+
* Explicitly wanted objects are always sent, as git sends them.
|
|
100
|
+
*/
|
|
101
|
+
export function selectPack(repo: MemoryRepository, selection: PackSelection): SelectedPack {
|
|
102
|
+
const clientShallow = new Set(selection.clientShallow ?? []);
|
|
103
|
+
const excluded = clientHas(repo, selection.haves, clientShallow);
|
|
104
|
+
const { commits, boundary, walked } = selectCommits(repo, selection.wants, selection.depth, excluded);
|
|
105
|
+
const shallow = boundary.filter((sha) => !clientShallow.has(sha));
|
|
106
|
+
const unshallow = [...clientShallow].filter((sha) => walked.has(sha) && !boundary.includes(sha));
|
|
107
|
+
const out = new Map<string, GitObject>();
|
|
108
|
+
const visit = (sha: string, depth: number, explicit: boolean): void => {
|
|
109
|
+
if (out.has(sha) || excluded.has(sha)) return;
|
|
110
|
+
const object = repo.object(sha);
|
|
111
|
+
if (object === null || object.type === 'commit') return;
|
|
112
|
+
if (!explicit && !passesFilter(selection.filter, object, depth)) return;
|
|
113
|
+
out.set(sha, object);
|
|
114
|
+
if (object.type === 'tree')
|
|
115
|
+
for (const entry of repo.tree(sha) ?? [])
|
|
116
|
+
if (entry.mode !== '160000') visit(entry.sha, depth + 1, false);
|
|
117
|
+
};
|
|
118
|
+
for (const sha of commits) {
|
|
119
|
+
const commit = repo.commit(sha);
|
|
120
|
+
const object = repo.object(sha, 'commit');
|
|
121
|
+
if (commit === null || object === null) continue;
|
|
122
|
+
out.set(sha, object);
|
|
123
|
+
visit(commit.tree, 0, false);
|
|
124
|
+
}
|
|
125
|
+
for (const want of selection.wants) visit(want, 0, true);
|
|
126
|
+
return { objects: [...out.values()], shallow, unshallow };
|
|
127
|
+
}
|