@leed-ai/git-gateway-worker 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 +49 -2
- package/dist/audit/audit-recorder.d.ts +2 -0
- package/dist/audit/audit-recorder.js +53 -0
- package/dist/audit/audit-recorder.js.map +1 -0
- package/dist/audit/push-report.d.ts +13 -0
- package/dist/audit/push-report.js +66 -0
- package/dist/audit/push-report.js.map +1 -0
- package/dist/audit-recorder-K1Ns3tfC.d.ts +31 -0
- package/dist/auth/authenticate.d.ts +14 -0
- package/dist/auth/authenticate.js +59 -0
- package/dist/auth/authenticate.js.map +1 -0
- package/dist/auth/authorize.d.ts +12 -0
- package/dist/auth/authorize.js +57 -0
- package/dist/auth/authorize.js.map +1 -0
- package/dist/auth/read-token-auth.d.ts +60 -0
- package/dist/auth/read-token-auth.js +83 -0
- package/dist/auth/read-token-auth.js.map +1 -0
- package/dist/cache/decision-cache.d.ts +2 -0
- package/dist/cache/decision-cache.js +45 -0
- package/dist/cache/decision-cache.js.map +1 -0
- package/dist/commit-workflow-schemas-BWlg7bZR.d.ts +95 -0
- package/dist/decision-cache-87U32iSj.d.ts +24 -0
- package/dist/gateway-app.d.ts +69 -0
- package/dist/gateway-app.js +80 -0
- package/dist/gateway-app.js.map +1 -0
- package/dist/gateway-context-Boh9I0mk.d.ts +54 -0
- package/dist/gateway-env-DnsBU4Pq.d.ts +40 -0
- package/dist/gateway-worker.d.ts +13 -0
- package/dist/gateway-worker.js +22 -0
- package/dist/gateway-worker.js.map +1 -0
- package/dist/git-route-Spxi09Rm.d.ts +22 -0
- package/dist/health.d.ts +6 -0
- package/dist/health.js +16 -0
- package/dist/health.js.map +1 -0
- package/dist/logger-BZ1zyKLb.d.ts +27 -0
- package/dist/logging/logger.d.ts +2 -0
- package/dist/logging/logger.js +15 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/on-push-DTREOB__.d.ts +62 -0
- package/dist/policy/branch-policy.d.ts +24 -0
- package/dist/policy/branch-policy.js +125 -0
- package/dist/policy/branch-policy.js.map +1 -0
- package/dist/policy/enforce-branch-policy.d.ts +30 -0
- package/dist/policy/enforce-branch-policy.js +214 -0
- package/dist/policy/enforce-branch-policy.js.map +1 -0
- package/dist/policy/fast-forward.d.ts +32 -0
- package/dist/policy/fast-forward.js +95 -0
- package/dist/policy/fast-forward.js.map +1 -0
- package/dist/policy/ref-pattern.d.ts +7 -0
- package/dist/policy/ref-pattern.js +25 -0
- package/dist/policy/ref-pattern.js.map +1 -0
- package/dist/policy/rule-branch-policy.d.ts +17 -0
- package/dist/policy/rule-branch-policy.js +69 -0
- package/dist/policy/rule-branch-policy.js.map +1 -0
- package/dist/proxy/defer.d.ts +8 -0
- package/dist/proxy/defer.js +15 -0
- package/dist/proxy/defer.js.map +1 -0
- package/dist/proxy/forward-read.d.ts +11 -0
- package/dist/proxy/forward-read.js +53 -0
- package/dist/proxy/forward-read.js.map +1 -0
- package/dist/proxy/gateway-deps.d.ts +39 -0
- package/dist/proxy/gateway-deps.js +0 -0
- package/dist/proxy/gateway-env.d.ts +2 -0
- package/dist/proxy/gateway-env.js +0 -0
- package/dist/proxy/git-responses.d.ts +25 -0
- package/dist/proxy/git-responses.js +83 -0
- package/dist/proxy/git-responses.js.map +1 -0
- package/dist/proxy/git-route.d.ts +2 -0
- package/dist/proxy/git-route.js +34 -0
- package/dist/proxy/git-route.js.map +1 -0
- package/dist/proxy/push-events.d.ts +28 -0
- package/dist/proxy/push-events.js +65 -0
- package/dist/proxy/push-events.js.map +1 -0
- package/dist/proxy/push-guard.d.ts +43 -0
- package/dist/proxy/push-guard.js +81 -0
- package/dist/proxy/push-guard.js.map +1 -0
- package/dist/proxy/push-options.d.ts +16 -0
- package/dist/proxy/push-options.js +33 -0
- package/dist/proxy/push-options.js.map +1 -0
- package/dist/proxy/receive-pack.d.ts +17 -0
- package/dist/proxy/receive-pack.js +244 -0
- package/dist/proxy/receive-pack.js.map +1 -0
- package/dist/proxy/request-log.d.ts +13 -0
- package/dist/proxy/request-log.js +41 -0
- package/dist/proxy/request-log.js.map +1 -0
- package/dist/proxy/resolve-route.d.ts +9 -0
- package/dist/proxy/resolve-route.js +22 -0
- package/dist/proxy/resolve-route.js.map +1 -0
- package/dist/proxy/upstream-call.d.ts +23 -0
- package/dist/proxy/upstream-call.js +63 -0
- package/dist/proxy/upstream-call.js.map +1 -0
- package/dist/proxy/upstream.d.ts +2 -0
- package/dist/proxy/upstream.js +68 -0
- package/dist/proxy/upstream.js.map +1 -0
- package/dist/push-event-f-5TsFUY.d.ts +15 -0
- package/dist/push-policy/changed-paths.d.ts +26 -0
- package/dist/push-policy/changed-paths.js +117 -0
- package/dist/push-policy/changed-paths.js.map +1 -0
- package/dist/push-policy/pushed-objects.d.ts +30 -0
- package/dist/push-policy/pushed-objects.js +76 -0
- package/dist/push-policy/pushed-objects.js.map +1 -0
- package/dist/push-policy/run-push-policy.d.ts +35 -0
- package/dist/push-policy/run-push-policy.js +77 -0
- package/dist/push-policy/run-push-policy.js.map +1 -0
- package/dist/push-policy/tree-diff.d.ts +21 -0
- package/dist/push-policy/tree-diff.js +78 -0
- package/dist/push-policy/tree-diff.js.map +1 -0
- package/dist/push-policy/uncheckable.d.ts +14 -0
- package/dist/push-policy/uncheckable.js +25 -0
- package/dist/push-policy/uncheckable.js.map +1 -0
- package/dist/rpc/branch-methods.d.ts +16 -0
- package/dist/rpc/branch-methods.js +43 -0
- package/dist/rpc/branch-methods.js.map +1 -0
- package/dist/rpc/commit-attempt.d.ts +27 -0
- package/dist/rpc/commit-attempt.js +85 -0
- package/dist/rpc/commit-attempt.js.map +1 -0
- package/dist/rpc/commit-graph.d.ts +20 -0
- package/dist/rpc/commit-graph.js +38 -0
- package/dist/rpc/commit-graph.js.map +1 -0
- package/dist/rpc/commit-method.d.ts +13 -0
- package/dist/rpc/commit-method.js +30 -0
- package/dist/rpc/commit-method.js.map +1 -0
- package/dist/rpc/commit-source.d.ts +12 -0
- package/dist/rpc/commit-source.js +9 -0
- package/dist/rpc/commit-source.js.map +1 -0
- package/dist/rpc/gateway-context.d.ts +2 -0
- package/dist/rpc/gateway-context.js +30 -0
- package/dist/rpc/gateway-context.js.map +1 -0
- package/dist/rpc/gateway-rpc.d.ts +10 -0
- package/dist/rpc/gateway-rpc.js +48 -0
- package/dist/rpc/gateway-rpc.js.map +1 -0
- package/dist/rpc/gateway-service.d.ts +24 -0
- package/dist/rpc/gateway-service.js +63 -0
- package/dist/rpc/gateway-service.js.map +1 -0
- package/dist/rpc/history-page.d.ts +35 -0
- package/dist/rpc/history-page.js +122 -0
- package/dist/rpc/history-page.js.map +1 -0
- package/dist/rpc/lifecycle-methods.d.ts +12 -0
- package/dist/rpc/lifecycle-methods.js +39 -0
- package/dist/rpc/lifecycle-methods.js.map +1 -0
- package/dist/rpc/paint-down.d.ts +17 -0
- package/dist/rpc/paint-down.js +96 -0
- package/dist/rpc/paint-down.js.map +1 -0
- package/dist/rpc/promote-attempt.d.ts +21 -0
- package/dist/rpc/promote-attempt.js +55 -0
- package/dist/rpc/promote-attempt.js.map +1 -0
- package/dist/rpc/push-ref.d.ts +21 -0
- package/dist/rpc/push-ref.js +47 -0
- package/dist/rpc/push-ref.js.map +1 -0
- package/dist/rpc/read-methods.d.ts +17 -0
- package/dist/rpc/read-methods.js +94 -0
- package/dist/rpc/read-methods.js.map +1 -0
- package/dist/rpc/read-token.d.ts +39 -0
- package/dist/rpc/read-token.js +107 -0
- package/dist/rpc/read-token.js.map +1 -0
- package/dist/rpc/repo-access.d.ts +36 -0
- package/dist/rpc/repo-access.js +75 -0
- package/dist/rpc/repo-access.js.map +1 -0
- package/dist/rpc/sync-attempt.d.ts +23 -0
- package/dist/rpc/sync-attempt.js +105 -0
- package/dist/rpc/sync-attempt.js.map +1 -0
- package/dist/sync-workflow-schemas-DCNmNE5-.d.ts +57 -0
- package/dist/upstream-DvqMCj5N.d.ts +15 -0
- package/dist/workflow-starter-BqhwX6oA.d.ts +45 -0
- package/dist/workflows/commit-workflow-schemas.d.ts +2 -0
- package/dist/workflows/commit-workflow-schemas.js +84 -0
- package/dist/workflows/commit-workflow-schemas.js.map +1 -0
- package/dist/workflows/commit-workflow.d.ts +16 -0
- package/dist/workflows/commit-workflow.js +22 -0
- package/dist/workflows/commit-workflow.js.map +1 -0
- package/dist/workflows/on-push.d.ts +2 -0
- package/dist/workflows/on-push.js +90 -0
- package/dist/workflows/on-push.js.map +1 -0
- package/dist/workflows/push-event.d.ts +2 -0
- package/dist/workflows/push-event.js +28 -0
- package/dist/workflows/push-event.js.map +1 -0
- package/dist/workflows/run-commit-workflow.d.ts +15 -0
- package/dist/workflows/run-commit-workflow.js +49 -0
- package/dist/workflows/run-commit-workflow.js.map +1 -0
- package/dist/workflows/run-sync-workflow.d.ts +14 -0
- package/dist/workflows/run-sync-workflow.js +67 -0
- package/dist/workflows/run-sync-workflow.js.map +1 -0
- package/dist/workflows/step-errors.d.ts +15 -0
- package/dist/workflows/step-errors.js +24 -0
- package/dist/workflows/step-errors.js.map +1 -0
- package/dist/workflows/step-runner.d.ts +31 -0
- package/dist/workflows/step-runner.js +6 -0
- package/dist/workflows/step-runner.js.map +1 -0
- package/dist/workflows/sync-branch-workflow.d.ts +15 -0
- package/dist/workflows/sync-branch-workflow.js +21 -0
- package/dist/workflows/sync-branch-workflow.js.map +1 -0
- package/dist/workflows/sync-workflow-schemas.d.ts +2 -0
- package/dist/workflows/sync-workflow-schemas.js +104 -0
- package/dist/workflows/sync-workflow-schemas.js.map +1 -0
- package/dist/workflows/workflow-instance-id.d.ts +12 -0
- package/dist/workflows/workflow-instance-id.js +33 -0
- package/dist/workflows/workflow-instance-id.js.map +1 -0
- package/dist/workflows/workflow-runtime.d.ts +7 -0
- package/dist/workflows/workflow-runtime.js +11 -0
- package/dist/workflows/workflow-runtime.js.map +1 -0
- package/dist/workflows/workflow-starter.d.ts +2 -0
- package/dist/workflows/workflow-starter.js +40 -0
- package/dist/workflows/workflow-starter.js.map +1 -0
- package/package.json +381 -4
- package/src/audit/audit-recorder.ts +74 -0
- package/src/audit/push-report.ts +60 -0
- package/src/auth/authenticate.ts +68 -0
- package/src/auth/authorize.ts +57 -0
- package/src/auth/read-token-auth.ts +119 -0
- package/src/cache/decision-cache.ts +57 -0
- package/src/gateway-app.ts +147 -0
- package/src/gateway-worker.ts +32 -0
- package/src/health.ts +12 -0
- package/src/logging/logger.ts +40 -0
- package/src/policy/branch-policy.ts +119 -0
- package/src/policy/enforce-branch-policy.ts +213 -0
- package/src/policy/fast-forward.ts +136 -0
- package/src/policy/ref-pattern.ts +24 -0
- package/src/policy/rule-branch-policy.ts +76 -0
- package/src/proxy/defer.ts +18 -0
- package/src/proxy/forward-read.ts +52 -0
- package/src/proxy/gateway-deps.ts +43 -0
- package/src/proxy/gateway-env.ts +43 -0
- package/src/proxy/git-responses.ts +89 -0
- package/src/proxy/git-route.ts +49 -0
- package/src/proxy/push-events.ts +91 -0
- package/src/proxy/push-guard.ts +109 -0
- package/src/proxy/push-options.ts +38 -0
- package/src/proxy/receive-pack.ts +257 -0
- package/src/proxy/request-log.ts +44 -0
- package/src/proxy/resolve-route.ts +24 -0
- package/src/proxy/upstream-call.ts +71 -0
- package/src/proxy/upstream.ts +76 -0
- package/src/push-policy/changed-paths.ts +143 -0
- package/src/push-policy/pushed-objects.ts +88 -0
- package/src/push-policy/run-push-policy.ts +99 -0
- package/src/push-policy/tree-diff.ts +107 -0
- package/src/push-policy/uncheckable.ts +27 -0
- package/src/rpc/branch-methods.ts +57 -0
- package/src/rpc/commit-attempt.ts +87 -0
- package/src/rpc/commit-graph.ts +44 -0
- package/src/rpc/commit-method.ts +36 -0
- package/src/rpc/commit-source.ts +15 -0
- package/src/rpc/gateway-context.ts +77 -0
- package/src/rpc/gateway-rpc.ts +65 -0
- package/src/rpc/gateway-service.ts +85 -0
- package/src/rpc/history-page.ts +163 -0
- package/src/rpc/lifecycle-methods.ts +50 -0
- package/src/rpc/paint-down.ts +120 -0
- package/src/rpc/promote-attempt.ts +52 -0
- package/src/rpc/push-ref.ts +54 -0
- package/src/rpc/read-methods.ts +111 -0
- package/src/rpc/read-token.ts +135 -0
- package/src/rpc/repo-access.ts +112 -0
- package/src/rpc/sync-attempt.ts +84 -0
- package/src/workflows/commit-workflow-schemas.ts +95 -0
- package/src/workflows/commit-workflow.ts +39 -0
- package/src/workflows/on-push.ts +128 -0
- package/src/workflows/push-event.ts +41 -0
- package/src/workflows/run-commit-workflow.ts +45 -0
- package/src/workflows/run-sync-workflow.ts +52 -0
- package/src/workflows/step-errors.ts +26 -0
- package/src/workflows/step-runner.ts +36 -0
- package/src/workflows/sync-branch-workflow.ts +40 -0
- package/src/workflows/sync-workflow-schemas.ts +122 -0
- package/src/workflows/workflow-instance-id.ts +26 -0
- package/src/workflows/workflow-runtime.ts +13 -0
- package/src/workflows/workflow-starter.ts +69 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { StorageBackend } from "@leed-ai/git-gateway-contract/storage/storage-backend";
|
|
2
|
+
/** Whether an update is a fast-forward: "yes", "no", or "unknown" when it cannot be shown either way. */
|
|
3
|
+
export type FastForwardVerdict = 'yes' | 'no' | 'unknown';
|
|
4
|
+
/** Where the ancestry walk finds commits. */
|
|
5
|
+
export interface AncestrySources {
|
|
6
|
+
/** The repository the push targets. */
|
|
7
|
+
readonly repo: string;
|
|
8
|
+
readonly backend: Pick<StorageBackend, 'readCommit' | 'isAncestor'>;
|
|
9
|
+
/** Commits in the pushed pack (not yet in storage), by id, with their parents. */
|
|
10
|
+
readonly packCommits?: ReadonlyMap<string, readonly string[]>;
|
|
11
|
+
/**
|
|
12
|
+
* Most calls made to the backend before giving up with "unknown": commits read by the walk, or
|
|
13
|
+
* `isAncestor` questions when the backend answers them.
|
|
14
|
+
*/
|
|
15
|
+
readonly maxReads: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Decides whether moving a ref from `oldSha` to `newSha` is a fast-forward — whether `oldSha` is
|
|
19
|
+
* `newSha` or one of its ancestors.
|
|
20
|
+
*
|
|
21
|
+
* When the backend offers `isAncestor`, the commits of the pushed pack are walked from `newSha`
|
|
22
|
+
* and the backend is asked once for each commit the walk leaves the pack at (the commits storage
|
|
23
|
+
* already has): "yes" when `oldSha` is one of the pack commits or an ancestor of any of those,
|
|
24
|
+
* "no" when it is neither. Otherwise parents are walked breadth-first from `newSha`, taking each
|
|
25
|
+
* commit from the pushed pack when it is there and from the backend otherwise, until `oldSha` is
|
|
26
|
+
* found ("yes") or the history is exhausted ("no").
|
|
27
|
+
*
|
|
28
|
+
* A commit found in neither place, a backend failure, or more than `maxReads` backend calls gives
|
|
29
|
+
* "unknown": the caller cannot prove the update safe and must treat it as a force push.
|
|
30
|
+
*/
|
|
31
|
+
export declare function proveFastForward(oldSha: string, newSha: string, sources: AncestrySources): Promise<FastForwardVerdict>;
|
|
32
|
+
//# sourceMappingURL=fast-forward.d.ts.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/policy/fast-forward.ts
|
|
2
|
+
/**
|
|
3
|
+
* Decides whether moving a ref from `oldSha` to `newSha` is a fast-forward — whether `oldSha` is
|
|
4
|
+
* `newSha` or one of its ancestors.
|
|
5
|
+
*
|
|
6
|
+
* When the backend offers `isAncestor`, the commits of the pushed pack are walked from `newSha`
|
|
7
|
+
* and the backend is asked once for each commit the walk leaves the pack at (the commits storage
|
|
8
|
+
* already has): "yes" when `oldSha` is one of the pack commits or an ancestor of any of those,
|
|
9
|
+
* "no" when it is neither. Otherwise parents are walked breadth-first from `newSha`, taking each
|
|
10
|
+
* commit from the pushed pack when it is there and from the backend otherwise, until `oldSha` is
|
|
11
|
+
* found ("yes") or the history is exhausted ("no").
|
|
12
|
+
*
|
|
13
|
+
* A commit found in neither place, a backend failure, or more than `maxReads` backend calls gives
|
|
14
|
+
* "unknown": the caller cannot prove the update safe and must treat it as a force push.
|
|
15
|
+
*/
|
|
16
|
+
async function proveFastForward(oldSha, newSha, sources) {
|
|
17
|
+
if (oldSha === newSha) return "yes";
|
|
18
|
+
const { backend, repo } = sources;
|
|
19
|
+
if (backend.isAncestor === void 0) return walkHistory(oldSha, newSha, sources);
|
|
20
|
+
return askBackend(oldSha, newSha, sources, async (ancestor, descendant) => backend.isAncestor?.(repo, ancestor, descendant) ?? null);
|
|
21
|
+
}
|
|
22
|
+
/** Walks the pack's commits from `newSha`; returns "yes" on meeting `oldSha`, else where the walk left the pack. */
|
|
23
|
+
function walkPack(oldSha, newSha, packCommits) {
|
|
24
|
+
const seen = /* @__PURE__ */ new Set([newSha]);
|
|
25
|
+
const stored = [];
|
|
26
|
+
let frontier = [newSha];
|
|
27
|
+
while (frontier.length > 0) {
|
|
28
|
+
const next = [];
|
|
29
|
+
for (const sha of frontier) {
|
|
30
|
+
const parents = packCommits?.get(sha);
|
|
31
|
+
if (parents === void 0) {
|
|
32
|
+
stored.push(sha);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
for (const parent of parents) {
|
|
36
|
+
if (parent === oldSha) return "yes";
|
|
37
|
+
if (!seen.has(parent)) {
|
|
38
|
+
seen.add(parent);
|
|
39
|
+
next.push(parent);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
frontier = next;
|
|
44
|
+
}
|
|
45
|
+
return stored;
|
|
46
|
+
}
|
|
47
|
+
async function askBackend(oldSha, newSha, sources, isAncestor) {
|
|
48
|
+
const stored = walkPack(oldSha, newSha, sources.packCommits);
|
|
49
|
+
if (stored === "yes") return "yes";
|
|
50
|
+
if (stored.length > sources.maxReads) return "unknown";
|
|
51
|
+
for (const sha of stored) {
|
|
52
|
+
let answer;
|
|
53
|
+
try {
|
|
54
|
+
answer = await isAncestor(oldSha, sha);
|
|
55
|
+
} catch {
|
|
56
|
+
return "unknown";
|
|
57
|
+
}
|
|
58
|
+
if (answer === null) return "unknown";
|
|
59
|
+
if (answer) return "yes";
|
|
60
|
+
}
|
|
61
|
+
return "no";
|
|
62
|
+
}
|
|
63
|
+
async function walkHistory(oldSha, newSha, sources) {
|
|
64
|
+
const seen = /* @__PURE__ */ new Set([newSha]);
|
|
65
|
+
let frontier = [newSha];
|
|
66
|
+
let reads = 0;
|
|
67
|
+
while (frontier.length > 0) {
|
|
68
|
+
const next = [];
|
|
69
|
+
for (const sha of frontier) {
|
|
70
|
+
let parents = sources.packCommits?.get(sha);
|
|
71
|
+
if (parents === void 0) {
|
|
72
|
+
reads += 1;
|
|
73
|
+
if (reads > sources.maxReads) return "unknown";
|
|
74
|
+
try {
|
|
75
|
+
parents = (await sources.backend.readCommit(sources.repo, sha))?.parents;
|
|
76
|
+
} catch {
|
|
77
|
+
return "unknown";
|
|
78
|
+
}
|
|
79
|
+
if (parents === void 0) return "unknown";
|
|
80
|
+
}
|
|
81
|
+
for (const parent of parents) {
|
|
82
|
+
if (parent === oldSha) return "yes";
|
|
83
|
+
if (!seen.has(parent)) {
|
|
84
|
+
seen.add(parent);
|
|
85
|
+
next.push(parent);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
frontier = next;
|
|
90
|
+
}
|
|
91
|
+
return "no";
|
|
92
|
+
}
|
|
93
|
+
export { proveFastForward };
|
|
94
|
+
|
|
95
|
+
//# sourceMappingURL=fast-forward.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fast-forward.js","names":[],"sources":["../../src/policy/fast-forward.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';\n\n/** Whether an update is a fast-forward: \"yes\", \"no\", or \"unknown\" when it cannot be shown either way. */\nexport type FastForwardVerdict = 'yes' | 'no' | 'unknown';\n\n/** Where the ancestry walk finds commits. */\nexport interface AncestrySources {\n /** The repository the push targets. */\n readonly repo: string;\n readonly backend: Pick<StorageBackend, 'readCommit' | 'isAncestor'>;\n /** Commits in the pushed pack (not yet in storage), by id, with their parents. */\n readonly packCommits?: ReadonlyMap<string, readonly string[]>;\n /**\n * Most calls made to the backend before giving up with \"unknown\": commits read by the walk, or\n * `isAncestor` questions when the backend answers them.\n */\n readonly maxReads: number;\n}\n\n/**\n * Decides whether moving a ref from `oldSha` to `newSha` is a fast-forward — whether `oldSha` is\n * `newSha` or one of its ancestors.\n *\n * When the backend offers `isAncestor`, the commits of the pushed pack are walked from `newSha`\n * and the backend is asked once for each commit the walk leaves the pack at (the commits storage\n * already has): \"yes\" when `oldSha` is one of the pack commits or an ancestor of any of those,\n * \"no\" when it is neither. Otherwise parents are walked breadth-first from `newSha`, taking each\n * commit from the pushed pack when it is there and from the backend otherwise, until `oldSha` is\n * found (\"yes\") or the history is exhausted (\"no\").\n *\n * A commit found in neither place, a backend failure, or more than `maxReads` backend calls gives\n * \"unknown\": the caller cannot prove the update safe and must treat it as a force push.\n */\nexport async function proveFastForward(\n oldSha: string,\n newSha: string,\n sources: AncestrySources,\n): Promise<FastForwardVerdict> {\n if (oldSha === newSha) return 'yes';\n const { backend, repo } = sources;\n if (backend.isAncestor === undefined) return walkHistory(oldSha, newSha, sources);\n return askBackend(\n oldSha,\n newSha,\n sources,\n async (ancestor, descendant) => backend.isAncestor?.(repo, ancestor, descendant) ?? null,\n );\n}\n\n/** Walks the pack's commits from `newSha`; returns \"yes\" on meeting `oldSha`, else where the walk left the pack. */\nfunction walkPack(\n oldSha: string,\n newSha: string,\n packCommits: ReadonlyMap<string, readonly string[]> | undefined,\n): 'yes' | string[] {\n const seen = new Set<string>([newSha]);\n const stored: string[] = [];\n let frontier = [newSha];\n while (frontier.length > 0) {\n const next: string[] = [];\n for (const sha of frontier) {\n const parents = packCommits?.get(sha);\n if (parents === undefined) {\n stored.push(sha);\n continue;\n }\n for (const parent of parents) {\n if (parent === oldSha) return 'yes';\n if (!seen.has(parent)) {\n seen.add(parent);\n next.push(parent);\n }\n }\n }\n frontier = next;\n }\n return stored;\n}\n\nasync function askBackend(\n oldSha: string,\n newSha: string,\n sources: AncestrySources,\n isAncestor: (ancestor: string, descendant: string) => Promise<boolean | null>,\n): Promise<FastForwardVerdict> {\n const stored = walkPack(oldSha, newSha, sources.packCommits);\n if (stored === 'yes') return 'yes';\n if (stored.length > sources.maxReads) return 'unknown';\n for (const sha of stored) {\n let answer: boolean | null;\n try {\n answer = await isAncestor(oldSha, sha);\n } catch {\n return 'unknown';\n }\n if (answer === null) return 'unknown';\n if (answer) return 'yes';\n }\n return 'no';\n}\n\nasync function walkHistory(\n oldSha: string,\n newSha: string,\n sources: AncestrySources,\n): Promise<FastForwardVerdict> {\n const seen = new Set<string>([newSha]);\n let frontier = [newSha];\n let reads = 0;\n while (frontier.length > 0) {\n const next: string[] = [];\n for (const sha of frontier) {\n let parents = sources.packCommits?.get(sha);\n if (parents === undefined) {\n reads += 1;\n if (reads > sources.maxReads) return 'unknown';\n try {\n parents = (await sources.backend.readCommit(sources.repo, sha))?.parents;\n } catch {\n return 'unknown';\n }\n if (parents === undefined) return 'unknown';\n }\n for (const parent of parents) {\n if (parent === oldSha) return 'yes';\n if (!seen.has(parent)) {\n seen.add(parent);\n next.push(parent);\n }\n }\n }\n frontier = next;\n }\n return 'no';\n}\n"],"mappings":";;;;;;;;;;;;;;;AAkCA,eAAsB,iBACpB,QACA,QACA,SAC6B;CAC7B,IAAI,WAAW,QAAQ,OAAO;CAC9B,MAAM,EAAE,SAAS,SAAS;CAC1B,IAAI,QAAQ,eAAe,KAAA,GAAW,OAAO,YAAY,QAAQ,QAAQ,OAAO;CAChF,OAAO,WACL,QACA,QACA,SACA,OAAO,UAAU,eAAe,QAAQ,aAAa,MAAM,UAAU,UAAU,KAAK,IACtF;AACF;;AAGA,SAAS,SACP,QACA,QACA,aACkB;CAClB,MAAM,uBAAO,IAAI,IAAY,CAAC,MAAM,CAAC;CACrC,MAAM,SAAmB,CAAC;CAC1B,IAAI,WAAW,CAAC,MAAM;CACtB,OAAO,SAAS,SAAS,GAAG;EAC1B,MAAM,OAAiB,CAAC;EACxB,KAAK,MAAM,OAAO,UAAU;GAC1B,MAAM,UAAU,aAAa,IAAI,GAAG;GACpC,IAAI,YAAY,KAAA,GAAW;IACzB,OAAO,KAAK,GAAG;IACf;GACF;GACA,KAAK,MAAM,UAAU,SAAS;IAC5B,IAAI,WAAW,QAAQ,OAAO;IAC9B,IAAI,CAAC,KAAK,IAAI,MAAM,GAAG;KACrB,KAAK,IAAI,MAAM;KACf,KAAK,KAAK,MAAM;IAClB;GACF;EACF;EACA,WAAW;CACb;CACA,OAAO;AACT;AAEA,eAAe,WACb,QACA,QACA,SACA,YAC6B;CAC7B,MAAM,SAAS,SAAS,QAAQ,QAAQ,QAAQ,WAAW;CAC3D,IAAI,WAAW,OAAO,OAAO;CAC7B,IAAI,OAAO,SAAS,QAAQ,UAAU,OAAO;CAC7C,KAAK,MAAM,OAAO,QAAQ;EACxB,IAAI;EACJ,IAAI;GACF,SAAS,MAAM,WAAW,QAAQ,GAAG;EACvC,QAAQ;GACN,OAAO;EACT;EACA,IAAI,WAAW,MAAM,OAAO;EAC5B,IAAI,QAAQ,OAAO;CACrB;CACA,OAAO;AACT;AAEA,eAAe,YACb,QACA,QACA,SAC6B;CAC7B,MAAM,uBAAO,IAAI,IAAY,CAAC,MAAM,CAAC;CACrC,IAAI,WAAW,CAAC,MAAM;CACtB,IAAI,QAAQ;CACZ,OAAO,SAAS,SAAS,GAAG;EAC1B,MAAM,OAAiB,CAAC;EACxB,KAAK,MAAM,OAAO,UAAU;GAC1B,IAAI,UAAU,QAAQ,aAAa,IAAI,GAAG;GAC1C,IAAI,YAAY,KAAA,GAAW;IACzB,SAAS;IACT,IAAI,QAAQ,QAAQ,UAAU,OAAO;IACrC,IAAI;KACF,WAAW,MAAM,QAAQ,QAAQ,WAAW,QAAQ,MAAM,GAAG,EAAA,EAAI;IACnE,QAAQ;KACN,OAAO;IACT;IACA,IAAI,YAAY,KAAA,GAAW,OAAO;GACpC;GACA,KAAK,MAAM,UAAU,SAAS;IAC5B,IAAI,WAAW,QAAQ,OAAO;IAC9B,IAAI,CAAC,KAAK,IAAI,MAAM,GAAG;KACrB,KAAK,IAAI,MAAM;KACf,KAAK,KAAK,MAAM;IAClB;GACF;EACF;EACA,WAAW;CACb;CACA,OAAO;AACT"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiles a {@link RefPattern} glob into a matcher over whole fully qualified refs: "**" matches
|
|
3
|
+
* any run of characters including "/", "*" any run inside one path component, everything else
|
|
4
|
+
* itself.
|
|
5
|
+
*/
|
|
6
|
+
export declare function compileRefPattern(pattern: string): (ref: string) => boolean;
|
|
7
|
+
//# sourceMappingURL=ref-pattern.d.ts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { RefPattern } from "@leed-ai/git-gateway-contract/proxy/branch-rule";
|
|
2
|
+
// src/policy/ref-pattern.ts
|
|
3
|
+
const SPECIAL = /[.+?^${}()|[\]\\]/g;
|
|
4
|
+
/**
|
|
5
|
+
* Compiles a {@link RefPattern} glob into a matcher over whole fully qualified refs: "**" matches
|
|
6
|
+
* any run of characters including "/", "*" any run inside one path component, everything else
|
|
7
|
+
* itself.
|
|
8
|
+
*/
|
|
9
|
+
function compileRefPattern(pattern) {
|
|
10
|
+
const glob = RefPattern.parse(pattern);
|
|
11
|
+
let source = "";
|
|
12
|
+
for (let index = 0; index < glob.length; index += 1) {
|
|
13
|
+
const char = glob[index] ?? "";
|
|
14
|
+
if (char === "*" && glob[index + 1] === "*") {
|
|
15
|
+
source += ".*";
|
|
16
|
+
index += 1;
|
|
17
|
+
} else if (char === "*") source += "[^/]*";
|
|
18
|
+
else source += char.replace(SPECIAL, String.raw`\$&`);
|
|
19
|
+
}
|
|
20
|
+
const regex = new RegExp(`^${source}$`, "s");
|
|
21
|
+
return (ref) => regex.test(ref);
|
|
22
|
+
}
|
|
23
|
+
export { compileRefPattern };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=ref-pattern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ref-pattern.js","names":[],"sources":["../../src/policy/ref-pattern.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport { RefPattern } from '@leed-ai/git-gateway-contract/proxy/branch-rule';\n\nconst SPECIAL = /[.+?^${}()|[\\]\\\\]/g;\n\n/**\n * Compiles a {@link RefPattern} glob into a matcher over whole fully qualified refs: \"**\" matches\n * any run of characters including \"/\", \"*\" any run inside one path component, everything else\n * itself.\n */\nexport function compileRefPattern(pattern: string): (ref: string) => boolean {\n const glob = RefPattern.parse(pattern);\n let source = '';\n for (let index = 0; index < glob.length; index += 1) {\n const char = glob[index] ?? '';\n if (char === '*' && glob[index + 1] === '*') {\n source += '.*';\n index += 1;\n } else if (char === '*') source += '[^/]*';\n else source += char.replace(SPECIAL, String.raw`\\$&`);\n }\n const regex = new RegExp(`^${source}$`, 's');\n return (ref) => regex.test(ref);\n}\n"],"mappings":";;AAGA,MAAM,UAAU;;;;;;AAOhB,SAAgB,kBAAkB,SAA2C;CAC3E,MAAM,OAAO,WAAW,MAAM,OAAO;CACrC,IAAI,SAAS;CACb,KAAK,IAAI,QAAQ,GAAG,QAAQ,KAAK,QAAQ,SAAS,GAAG;EACnD,MAAM,OAAO,KAAK,UAAU;EAC5B,IAAI,SAAS,OAAO,KAAK,QAAQ,OAAO,KAAK;GAC3C,UAAU;GACV,SAAS;EACX,OAAO,IAAI,SAAS,KAAK,UAAU;OAC9B,UAAU,KAAK,QAAQ,SAAS,OAAO,GAAG,KAAK;CACtD;CACA,MAAM,QAAQ,IAAI,OAAO,IAAI,OAAO,IAAI,GAAG;CAC3C,QAAQ,QAAQ,MAAM,KAAK,GAAG;AAChC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Principal } from "@leed-ai/git-gateway-contract/proxy/principal";
|
|
2
|
+
import { RefUpdate } from "@leed-ai/git-gateway-contract/schemas/ref-update";
|
|
3
|
+
import { PolicyResult } from "@leed-ai/git-gateway-contract/proxy/policy-result";
|
|
4
|
+
import { BranchPolicyConfigInput } from "@leed-ai/git-gateway-contract/proxy/branch-rule";
|
|
5
|
+
import { BranchPolicy } from "@leed-ai/git-gateway-contract/proxy/gateway-hooks";
|
|
6
|
+
/**
|
|
7
|
+
* The reference {@link BranchPolicy}: an ordered list of {@link BranchRule}s, first match wins,
|
|
8
|
+
* with a fallback for refs no rule matches. Each rule says who may push the refs it matches,
|
|
9
|
+
* whether they may be created or deleted, and whether force pushes are allowed; an update that is
|
|
10
|
+
* allowed but not forced comes back "fast-forward-only" for the gateway to prove.
|
|
11
|
+
*/
|
|
12
|
+
export declare class RuleBranchPolicy implements BranchPolicy {
|
|
13
|
+
#private;
|
|
14
|
+
constructor(config?: BranchPolicyConfigInput);
|
|
15
|
+
check(principal: Principal, _repo: string, updates: readonly RefUpdate[]): PolicyResult;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=rule-branch-policy.d.ts.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { compileRefPattern } from "./ref-pattern.js";
|
|
2
|
+
import { ZERO_SHA } from "@leed-ai/git-gateway-contract/schemas/sha";
|
|
3
|
+
import { BranchPolicyConfig } from "@leed-ai/git-gateway-contract/proxy/branch-rule";
|
|
4
|
+
// src/policy/rule-branch-policy.ts
|
|
5
|
+
/** Whether a principal is selected (a null selector selects everyone). */
|
|
6
|
+
function selects(selector, principal) {
|
|
7
|
+
if (selector === null) return true;
|
|
8
|
+
return selector.subjects.includes(principal.subject) || principal.roles.some((role) => selector.roles.includes(role));
|
|
9
|
+
}
|
|
10
|
+
/** A short ref name for messages: "main" for refs/heads/main, "tag v1" for refs/tags/v1. */
|
|
11
|
+
function describeRef(ref) {
|
|
12
|
+
if (ref.startsWith("refs/heads/")) return ref.slice(11);
|
|
13
|
+
return `tag ${ref.slice(10)}`;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The reference {@link BranchPolicy}: an ordered list of {@link BranchRule}s, first match wins,
|
|
17
|
+
* with a fallback for refs no rule matches. Each rule says who may push the refs it matches,
|
|
18
|
+
* whether they may be created or deleted, and whether force pushes are allowed; an update that is
|
|
19
|
+
* allowed but not forced comes back "fast-forward-only" for the gateway to prove.
|
|
20
|
+
*/
|
|
21
|
+
var RuleBranchPolicy = class {
|
|
22
|
+
#rules;
|
|
23
|
+
#fallback;
|
|
24
|
+
constructor(config = {}) {
|
|
25
|
+
const parsed = BranchPolicyConfig.parse(config);
|
|
26
|
+
this.#rules = parsed.rules.map((rule) => ({
|
|
27
|
+
...rule,
|
|
28
|
+
matches: compileRefPattern(rule.pattern)
|
|
29
|
+
}));
|
|
30
|
+
this.#fallback = {
|
|
31
|
+
...parsed.fallback,
|
|
32
|
+
matches: () => true,
|
|
33
|
+
message: null
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
check(principal, _repo, updates) {
|
|
37
|
+
return { refs: updates.map((update) => this.#decide(principal, update)) };
|
|
38
|
+
}
|
|
39
|
+
#decide(principal, update) {
|
|
40
|
+
const rule = this.#rules.find((candidate) => candidate.matches(update.ref)) ?? this.#fallback;
|
|
41
|
+
const name = describeRef(update.ref);
|
|
42
|
+
const deny = (what) => ({
|
|
43
|
+
ref: update.ref,
|
|
44
|
+
decision: "deny",
|
|
45
|
+
reason: rule.message ?? `${what} ${name} is not allowed`
|
|
46
|
+
});
|
|
47
|
+
if (!selects(rule.pushers, principal)) return deny("pushing to");
|
|
48
|
+
if (update.oldSha === ZERO_SHA) return rule.create ? {
|
|
49
|
+
ref: update.ref,
|
|
50
|
+
decision: "allow"
|
|
51
|
+
} : deny("creating");
|
|
52
|
+
if (update.newSha === ZERO_SHA) return rule.delete ? {
|
|
53
|
+
ref: update.ref,
|
|
54
|
+
decision: "allow"
|
|
55
|
+
} : deny("deleting");
|
|
56
|
+
if (rule.forcePush) return {
|
|
57
|
+
ref: update.ref,
|
|
58
|
+
decision: "allow"
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
ref: update.ref,
|
|
62
|
+
decision: "fast-forward-only",
|
|
63
|
+
reason: rule.message ?? `force-pushing ${name} is not allowed`
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
export { RuleBranchPolicy };
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=rule-branch-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-branch-policy.js","names":[],"sources":["../../src/policy/rule-branch-policy.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport {\n BranchPolicyConfig,\n type BranchPermissions,\n type BranchPolicyConfigInput,\n type PrincipalSelector,\n} from '@leed-ai/git-gateway-contract/proxy/branch-rule';\nimport type { BranchPolicy } from '@leed-ai/git-gateway-contract/proxy/gateway-hooks';\nimport type { PolicyResult, RefDecision } from '@leed-ai/git-gateway-contract/proxy/policy-result';\nimport type { Principal } from '@leed-ai/git-gateway-contract/proxy/principal';\nimport type { RefUpdate } from '@leed-ai/git-gateway-contract/schemas/ref-update';\nimport { ZERO_SHA } from '@leed-ai/git-gateway-contract/schemas/sha';\n\nimport { compileRefPattern } from './ref-pattern.ts';\n\ninterface CompiledRule extends BranchPermissions {\n readonly matches: (ref: string) => boolean;\n readonly message: string | null;\n}\n\n/** Whether a principal is selected (a null selector selects everyone). */\nfunction selects(selector: PrincipalSelector | null, principal: Principal): boolean {\n if (selector === null) return true;\n return (\n selector.subjects.includes(principal.subject) ||\n principal.roles.some((role) => selector.roles.includes(role))\n );\n}\n\n/** A short ref name for messages: \"main\" for refs/heads/main, \"tag v1\" for refs/tags/v1. */\nfunction describeRef(ref: string): string {\n if (ref.startsWith('refs/heads/')) return ref.slice('refs/heads/'.length);\n return `tag ${ref.slice('refs/tags/'.length)}`;\n}\n\n/**\n * The reference {@link BranchPolicy}: an ordered list of {@link BranchRule}s, first match wins,\n * with a fallback for refs no rule matches. Each rule says who may push the refs it matches,\n * whether they may be created or deleted, and whether force pushes are allowed; an update that is\n * allowed but not forced comes back \"fast-forward-only\" for the gateway to prove.\n */\nexport class RuleBranchPolicy implements BranchPolicy {\n readonly #rules: readonly CompiledRule[];\n readonly #fallback: CompiledRule;\n\n constructor(config: BranchPolicyConfigInput = {}) {\n const parsed = BranchPolicyConfig.parse(config);\n this.#rules = parsed.rules.map((rule) => ({ ...rule, matches: compileRefPattern(rule.pattern) }));\n this.#fallback = { ...parsed.fallback, matches: () => true, message: null };\n }\n\n check(principal: Principal, _repo: string, updates: readonly RefUpdate[]): PolicyResult {\n return { refs: updates.map((update) => this.#decide(principal, update)) };\n }\n\n #decide(principal: Principal, update: RefUpdate): RefDecision {\n const rule = this.#rules.find((candidate) => candidate.matches(update.ref)) ?? this.#fallback;\n const name = describeRef(update.ref);\n const deny = (what: string): RefDecision => ({\n ref: update.ref,\n decision: 'deny',\n reason: rule.message ?? `${what} ${name} is not allowed`,\n });\n if (!selects(rule.pushers, principal)) return deny('pushing to');\n if (update.oldSha === ZERO_SHA)\n return rule.create ? { ref: update.ref, decision: 'allow' } : deny('creating');\n if (update.newSha === ZERO_SHA)\n return rule.delete ? { ref: update.ref, decision: 'allow' } : deny('deleting');\n if (rule.forcePush) return { ref: update.ref, decision: 'allow' };\n return {\n ref: update.ref,\n decision: 'fast-forward-only',\n reason: rule.message ?? `force-pushing ${name} is not allowed`,\n };\n }\n}\n"],"mappings":";;;;;AAqBA,SAAS,QAAQ,UAAoC,WAA+B;CAClF,IAAI,aAAa,MAAM,OAAO;CAC9B,OACE,SAAS,SAAS,SAAS,UAAU,OAAO,KAC5C,UAAU,MAAM,MAAM,SAAS,SAAS,MAAM,SAAS,IAAI,CAAC;AAEhE;;AAGA,SAAS,YAAY,KAAqB;CACxC,IAAI,IAAI,WAAW,aAAa,GAAG,OAAO,IAAI,MAAM,EAAoB;CACxE,OAAO,OAAO,IAAI,MAAM,EAAmB;AAC7C;;;;;;;AAQA,IAAa,mBAAb,MAAsD;CACpD;CACA;CAEA,YAAY,SAAkC,CAAC,GAAG;EAChD,MAAM,SAAS,mBAAmB,MAAM,MAAM;EAC9C,KAAK,SAAS,OAAO,MAAM,KAAK,UAAU;GAAE,GAAG;GAAM,SAAS,kBAAkB,KAAK,OAAO;EAAE,EAAE;EAChG,KAAK,YAAY;GAAE,GAAG,OAAO;GAAU,eAAe;GAAM,SAAS;EAAK;CAC5E;CAEA,MAAM,WAAsB,OAAe,SAA6C;EACtF,OAAO,EAAE,MAAM,QAAQ,KAAK,WAAW,KAAK,QAAQ,WAAW,MAAM,CAAC,EAAE;CAC1E;CAEA,QAAQ,WAAsB,QAAgC;EAC5D,MAAM,OAAO,KAAK,OAAO,MAAM,cAAc,UAAU,QAAQ,OAAO,GAAG,CAAC,KAAK,KAAK;EACpF,MAAM,OAAO,YAAY,OAAO,GAAG;EACnC,MAAM,QAAQ,UAA+B;GAC3C,KAAK,OAAO;GACZ,UAAU;GACV,QAAQ,KAAK,WAAW,GAAG,KAAK,GAAG,KAAK;EAC1C;EACA,IAAI,CAAC,QAAQ,KAAK,SAAS,SAAS,GAAG,OAAO,KAAK,YAAY;EAC/D,IAAI,OAAO,WAAW,UACpB,OAAO,KAAK,SAAS;GAAE,KAAK,OAAO;GAAK,UAAU;EAAQ,IAAI,KAAK,UAAU;EAC/E,IAAI,OAAO,WAAW,UACpB,OAAO,KAAK,SAAS;GAAE,KAAK,OAAO;GAAK,UAAU;EAAQ,IAAI,KAAK,UAAU;EAC/E,IAAI,KAAK,WAAW,OAAO;GAAE,KAAK,OAAO;GAAK,UAAU;EAAQ;EAChE,OAAO;GACL,KAAK,OAAO;GACZ,UAAU;GACV,QAAQ,KAAK,WAAW,iBAAiB,KAAK;EAChD;CACF;AACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { r as Defer } from "../audit-recorder-K1Ns3tfC.js";
|
|
2
|
+
import { Context } from "hono";
|
|
3
|
+
/**
|
|
4
|
+
* Work to run after the response: handed to the runtime's `waitUntil` when the request has an
|
|
5
|
+
* execution context (in a Worker), otherwise left to run on its own. The work must not reject.
|
|
6
|
+
*/
|
|
7
|
+
export declare function deferWith(context: Context): Defer;
|
|
8
|
+
//# sourceMappingURL=defer.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/proxy/defer.ts
|
|
2
|
+
/**
|
|
3
|
+
* Work to run after the response: handed to the runtime's `waitUntil` when the request has an
|
|
4
|
+
* execution context (in a Worker), otherwise left to run on its own. The work must not reject.
|
|
5
|
+
*/
|
|
6
|
+
function deferWith(context) {
|
|
7
|
+
return (work) => {
|
|
8
|
+
try {
|
|
9
|
+
context.executionCtx.waitUntil(work);
|
|
10
|
+
} catch {}
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export { deferWith };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=defer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defer.js","names":[],"sources":["../../src/proxy/defer.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport type { Context } from 'hono';\n\nimport type { Defer } from '../audit/audit-recorder.ts';\n\n/**\n * Work to run after the response: handed to the runtime's `waitUntil` when the request has an\n * execution context (in a Worker), otherwise left to run on its own. The work must not reject.\n */\nexport function deferWith(context: Context): Defer {\n return (work) => {\n try {\n context.executionCtx.waitUntil(work);\n } catch {\n // No execution context (tests, Node): the promise runs to completion on its own.\n }\n };\n}\n"],"mappings":";;;;;AASA,SAAgB,UAAU,SAAyB;CACjD,QAAQ,SAAS;EACf,IAAI;GACF,QAAQ,aAAa,UAAU,IAAI;EACrC,QAAQ,CAER;CACF;AACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { GatewayDeps } from "./gateway-deps.js";
|
|
2
|
+
import { n as GatewayEnv } from "../gateway-env-DnsBU4Pq.js";
|
|
3
|
+
import { Handler } from "hono";
|
|
4
|
+
/**
|
|
5
|
+
* Forwards an authorized request that writes nothing — either ref advertisement, or a fetch's
|
|
6
|
+
* git-upload-pack — to the storage host and streams the answer back. Request and response bodies
|
|
7
|
+
* stream through without being buffered or parsed, so protocol v2 (requested with the Git-Protocol
|
|
8
|
+
* header) passes through to a host that speaks it.
|
|
9
|
+
*/
|
|
10
|
+
export declare function forwardRead(deps: GatewayDeps): Handler<GatewayEnv>;
|
|
11
|
+
//# sourceMappingURL=forward-read.d.ts.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { deferWith } from "./defer.js";
|
|
2
|
+
import { textResponse } from "./git-responses.js";
|
|
3
|
+
import { callUpstream, streamToClient } from "./upstream-call.js";
|
|
4
|
+
// src/proxy/forward-read.ts
|
|
5
|
+
/**
|
|
6
|
+
* Forwards an authorized request that writes nothing — either ref advertisement, or a fetch's
|
|
7
|
+
* git-upload-pack — to the storage host and streams the answer back. Request and response bodies
|
|
8
|
+
* stream through without being buffered or parsed, so protocol v2 (requested with the Git-Protocol
|
|
9
|
+
* header) passes through to a host that speaks it.
|
|
10
|
+
*/
|
|
11
|
+
function forwardRead(deps) {
|
|
12
|
+
return async (context) => {
|
|
13
|
+
const requestId = context.get("requestId");
|
|
14
|
+
const principal = context.get("principal");
|
|
15
|
+
const git = context.get("git");
|
|
16
|
+
const defer = deferWith(context);
|
|
17
|
+
const body = git.endpoint === "info-refs" ? null : context.req.raw.body;
|
|
18
|
+
let result;
|
|
19
|
+
try {
|
|
20
|
+
result = await callUpstream(deps, context, body);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
deps.logger.log({
|
|
23
|
+
level: "error",
|
|
24
|
+
message: "storage host unreachable",
|
|
25
|
+
requestId,
|
|
26
|
+
repo: git.repo,
|
|
27
|
+
error: error instanceof Error ? error.message : String(error)
|
|
28
|
+
});
|
|
29
|
+
const reason = "The storage backend is unreachable; try again later";
|
|
30
|
+
deps.audit.record({
|
|
31
|
+
requestId,
|
|
32
|
+
principal,
|
|
33
|
+
git,
|
|
34
|
+
outcome: "failed",
|
|
35
|
+
reason,
|
|
36
|
+
refs: []
|
|
37
|
+
}, defer, requestId);
|
|
38
|
+
return textResponse(502, reason, { "X-Request-Id": requestId });
|
|
39
|
+
}
|
|
40
|
+
deps.audit.record({
|
|
41
|
+
requestId,
|
|
42
|
+
principal,
|
|
43
|
+
git,
|
|
44
|
+
outcome: result.ok ? "allowed" : "failed",
|
|
45
|
+
reason: result.ok ? null : result.reason,
|
|
46
|
+
refs: []
|
|
47
|
+
}, defer, requestId);
|
|
48
|
+
return result.ok ? streamToClient(result.response, result.response.body, requestId) : result.response;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export { forwardRead };
|
|
52
|
+
|
|
53
|
+
//# sourceMappingURL=forward-read.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"forward-read.js","names":[],"sources":["../../src/proxy/forward-read.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport type { Handler } from 'hono';\n\nimport { deferWith } from './defer.ts';\nimport type { GatewayDeps } from './gateway-deps.ts';\nimport type { GatewayEnv } from './gateway-env.ts';\nimport { textResponse } from './git-responses.ts';\nimport { callUpstream, streamToClient } from './upstream-call.ts';\n\n/**\n * Forwards an authorized request that writes nothing — either ref advertisement, or a fetch's\n * git-upload-pack — to the storage host and streams the answer back. Request and response bodies\n * stream through without being buffered or parsed, so protocol v2 (requested with the Git-Protocol\n * header) passes through to a host that speaks it.\n */\nexport function forwardRead(deps: GatewayDeps): Handler<GatewayEnv> {\n return async (context) => {\n const requestId = context.get('requestId');\n const principal = context.get('principal');\n const git = context.get('git');\n const defer = deferWith(context);\n const body = git.endpoint === 'info-refs' ? null : context.req.raw.body;\n let result;\n try {\n result = await callUpstream(deps, context, body);\n } catch (error) {\n deps.logger.log({\n level: 'error',\n message: 'storage host unreachable',\n requestId,\n repo: git.repo,\n error: error instanceof Error ? error.message : String(error),\n });\n const reason = 'The storage backend is unreachable; try again later';\n deps.audit.record({ requestId, principal, git, outcome: 'failed', reason, refs: [] }, defer, requestId);\n return textResponse(502, reason, { 'X-Request-Id': requestId });\n }\n deps.audit.record(\n {\n requestId,\n principal,\n git,\n outcome: result.ok ? 'allowed' : 'failed',\n reason: result.ok ? null : result.reason,\n refs: [],\n },\n defer,\n requestId,\n );\n return result.ok ? streamToClient(result.response, result.response.body, requestId) : result.response;\n };\n}\n"],"mappings":";;;;;;;;;;AAeA,SAAgB,YAAY,MAAwC;CAClE,OAAO,OAAO,YAAY;EACxB,MAAM,YAAY,QAAQ,IAAI,WAAW;EACzC,MAAM,YAAY,QAAQ,IAAI,WAAW;EACzC,MAAM,MAAM,QAAQ,IAAI,KAAK;EAC7B,MAAM,QAAQ,UAAU,OAAO;EAC/B,MAAM,OAAO,IAAI,aAAa,cAAc,OAAO,QAAQ,IAAI,IAAI;EACnE,IAAI;EACJ,IAAI;GACF,SAAS,MAAM,aAAa,MAAM,SAAS,IAAI;EACjD,SAAS,OAAO;GACd,KAAK,OAAO,IAAI;IACd,OAAO;IACP,SAAS;IACT;IACA,MAAM,IAAI;IACV,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;GAC9D,CAAC;GACD,MAAM,SAAS;GACf,KAAK,MAAM,OAAO;IAAE;IAAW;IAAW;IAAK,SAAS;IAAU;IAAQ,MAAM,CAAC;GAAE,GAAG,OAAO,SAAS;GACtG,OAAO,aAAa,KAAK,QAAQ,EAAE,gBAAgB,UAAU,CAAC;EAChE;EACA,KAAK,MAAM,OACT;GACE;GACA;GACA;GACA,SAAS,OAAO,KAAK,YAAY;GACjC,QAAQ,OAAO,KAAK,OAAO,OAAO;GAClC,MAAM,CAAC;EACT,GACA,OACA,SACF;EACA,OAAO,OAAO,KAAK,eAAe,OAAO,UAAU,OAAO,SAAS,MAAM,SAAS,IAAI,OAAO;CAC/F;AACF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { n as AuditRecorder } from "../audit-recorder-K1Ns3tfC.js";
|
|
2
|
+
import { t as DecisionCache } from "../decision-cache-87U32iSj.js";
|
|
3
|
+
import { r as Logger } from "../logger-BZ1zyKLb.js";
|
|
4
|
+
import { t as OnPushHook } from "../on-push-DTREOB__.js";
|
|
5
|
+
import { t as UpstreamFetch } from "../upstream-DvqMCj5N.js";
|
|
6
|
+
import { SmartHttpConfig } from "@leed-ai/git-gateway-contract/proxy/smart-http-config";
|
|
7
|
+
import { Principal } from "@leed-ai/git-gateway-contract/proxy/principal";
|
|
8
|
+
import { Authenticator, Authorizer, BranchPolicy, PushPolicy } from "@leed-ai/git-gateway-contract/proxy/gateway-hooks";
|
|
9
|
+
import { StorageBackend } from "@leed-ai/git-gateway-contract/storage/storage-backend";
|
|
10
|
+
/** A cached authentication or authorization decision. */
|
|
11
|
+
export type CachedDecision = {
|
|
12
|
+
readonly kind: 'principal';
|
|
13
|
+
readonly principal: Principal;
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: 'allowed';
|
|
16
|
+
readonly allowed: boolean;
|
|
17
|
+
};
|
|
18
|
+
/** Everything the gateway's middleware and handlers share, resolved from the app options. */
|
|
19
|
+
export interface GatewayDeps {
|
|
20
|
+
readonly backend: StorageBackend;
|
|
21
|
+
readonly authenticator: Authenticator;
|
|
22
|
+
readonly authorizer: Authorizer;
|
|
23
|
+
readonly branchPolicy: BranchPolicy;
|
|
24
|
+
/** The deployment's rules over what a push changes, run after the branch policy; undefined: none. */
|
|
25
|
+
readonly pushPolicy: PushPolicy | undefined;
|
|
26
|
+
readonly audit: AuditRecorder;
|
|
27
|
+
readonly logger: Logger;
|
|
28
|
+
readonly cache: DecisionCache<CachedDecision>;
|
|
29
|
+
readonly config: SmartHttpConfig;
|
|
30
|
+
/** How long decisions are cached, in milliseconds (GatewayConfig.authCacheSeconds). */
|
|
31
|
+
readonly cacheTtlMs: number;
|
|
32
|
+
readonly fetch: UpstreamFetch;
|
|
33
|
+
readonly now: () => Date;
|
|
34
|
+
/** Run after every accepted push (the sync rules and the deployment's hook); undefined: none. */
|
|
35
|
+
readonly onPush: OnPushHook | undefined;
|
|
36
|
+
/** Receives onPush failures after they are logged. */
|
|
37
|
+
readonly onPushError: (error: unknown) => void;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=gateway-deps.d.ts.map
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { r as GitServiceName } from "../git-route-Spxi09Rm.js";
|
|
2
|
+
/** Headers git smart-HTTP responses carry so no proxy or browser caches them. */
|
|
3
|
+
export declare const NO_CACHE: Readonly<Record<string, string>>;
|
|
4
|
+
/** A plain-text response (for errors outside the git protocol). */
|
|
5
|
+
export declare function textResponse(status: number, body: string, headers?: Record<string, string>): Response;
|
|
6
|
+
/** 401 with a Basic challenge, so git asks its credential helper and retries. */
|
|
7
|
+
export declare function unauthorized(realm: string): Response;
|
|
8
|
+
/**
|
|
9
|
+
* A refusal git prints as "fatal: remote error: <message>": an "ERR" packet, after the
|
|
10
|
+
* "# service=…" preamble for an info/refs request.
|
|
11
|
+
*/
|
|
12
|
+
export declare function errResponse(service: GitServiceName, endpoint: 'info-refs' | 'rpc', message: string): Response;
|
|
13
|
+
/** One update a refused push names, and why it was refused. */
|
|
14
|
+
export interface RefRefusal {
|
|
15
|
+
readonly ref: string;
|
|
16
|
+
readonly reason: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The receive-pack response refusing a whole push in-protocol: "ng <ref> <reason>" for every
|
|
20
|
+
* update (git prints "! [remote rejected] <ref> (<reason>)"), with each reason also sent as
|
|
21
|
+
* progress text when a side-band was negotiated (git prints "remote: …"). A client that did not
|
|
22
|
+
* ask for report-status gets an ERR packet instead.
|
|
23
|
+
*/
|
|
24
|
+
export declare function refusePush(capabilities: readonly string[], refusals: readonly RefRefusal[]): Response;
|
|
25
|
+
//# sourceMappingURL=git-responses.d.ts.map
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { concatBytes } from "@leed-ai/git-gateway-core/bytes";
|
|
2
|
+
import { FLUSH_PKT, encodePktLine } from "@leed-ai/git-gateway-core/pkt-line/encode";
|
|
3
|
+
import { encodeErrPacket, encodeReportStatus, reportStatusFraming } from "@leed-ai/git-gateway-core/receive-pack/report-status";
|
|
4
|
+
import { BAND_PROGRESS, SIDE_BAND_64K_MAX_PAYLOAD, SIDE_BAND_MAX_PAYLOAD, encodeSideBand } from "@leed-ai/git-gateway-core/receive-pack/side-band";
|
|
5
|
+
// src/proxy/git-responses.ts
|
|
6
|
+
/** Headers git smart-HTTP responses carry so no proxy or browser caches them. */
|
|
7
|
+
const NO_CACHE = {
|
|
8
|
+
"Cache-Control": "no-cache, max-age=0, must-revalidate",
|
|
9
|
+
Expires: "Fri, 01 Jan 1980 00:00:00 GMT",
|
|
10
|
+
Pragma: "no-cache"
|
|
11
|
+
};
|
|
12
|
+
/** A plain-text response (for errors outside the git protocol). */
|
|
13
|
+
function textResponse(status, body, headers = {}) {
|
|
14
|
+
return new Response(body.endsWith("\n") ? body : `${body}\n`, {
|
|
15
|
+
status,
|
|
16
|
+
headers: {
|
|
17
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
18
|
+
...NO_CACHE,
|
|
19
|
+
...headers
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** 401 with a Basic challenge, so git asks its credential helper and retries. */
|
|
24
|
+
function unauthorized(realm) {
|
|
25
|
+
return textResponse(401, "Authentication required", { "WWW-Authenticate": `Basic realm="${realm}"` });
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A refusal git prints as "fatal: remote error: <message>": an "ERR" packet, after the
|
|
29
|
+
* "# service=…" preamble for an info/refs request.
|
|
30
|
+
*/
|
|
31
|
+
function errResponse(service, endpoint, message) {
|
|
32
|
+
const err = encodeErrPacket(message);
|
|
33
|
+
const body = endpoint === "info-refs" ? concatBytes([
|
|
34
|
+
encodePktLine(`# service=${service}\n`),
|
|
35
|
+
FLUSH_PKT,
|
|
36
|
+
err
|
|
37
|
+
]) : err;
|
|
38
|
+
return new Response(body, {
|
|
39
|
+
status: 200,
|
|
40
|
+
headers: {
|
|
41
|
+
"Content-Type": `application/x-${service}-${endpoint === "info-refs" ? "advertisement" : "result"}`,
|
|
42
|
+
...NO_CACHE
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The receive-pack response refusing a whole push in-protocol: "ng <ref> <reason>" for every
|
|
48
|
+
* update (git prints "! [remote rejected] <ref> (<reason>)"), with each reason also sent as
|
|
49
|
+
* progress text when a side-band was negotiated (git prints "remote: …"). A client that did not
|
|
50
|
+
* ask for report-status gets an ERR packet instead.
|
|
51
|
+
*/
|
|
52
|
+
function refusePush(capabilities, refusals) {
|
|
53
|
+
const headers = {
|
|
54
|
+
"Content-Type": "application/x-git-receive-pack-result",
|
|
55
|
+
...NO_CACHE
|
|
56
|
+
};
|
|
57
|
+
if (!(capabilities.includes("report-status") || capabilities.includes("report-status-v2"))) return new Response(encodeErrPacket(refusals[0]?.reason ?? "push refused"), {
|
|
58
|
+
status: 200,
|
|
59
|
+
headers
|
|
60
|
+
});
|
|
61
|
+
const framing = reportStatusFraming(capabilities);
|
|
62
|
+
const report = encodeReportStatus({
|
|
63
|
+
unpack: "ok",
|
|
64
|
+
refs: refusals.map(({ ref, reason }) => ({
|
|
65
|
+
ref,
|
|
66
|
+
status: "ng",
|
|
67
|
+
reason
|
|
68
|
+
}))
|
|
69
|
+
}, framing);
|
|
70
|
+
if (framing.sideBand === "none") return new Response(report, {
|
|
71
|
+
status: 200,
|
|
72
|
+
headers
|
|
73
|
+
});
|
|
74
|
+
const progress = new TextEncoder().encode(refusals.map(({ ref, reason }) => `git-gateway: ${ref}: ${reason}\n`).join(""));
|
|
75
|
+
const maxPayload = framing.sideBand === "side-band" ? SIDE_BAND_MAX_PAYLOAD : SIDE_BAND_64K_MAX_PAYLOAD;
|
|
76
|
+
return new Response(concatBytes([encodeSideBand(BAND_PROGRESS, progress, maxPayload), report]), {
|
|
77
|
+
status: 200,
|
|
78
|
+
headers
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export { NO_CACHE, errResponse, refusePush, textResponse, unauthorized };
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=git-responses.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-responses.js","names":[],"sources":["../../src/proxy/git-responses.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport { concatBytes } from '@leed-ai/git-gateway-core/bytes';\nimport { encodePktLine, FLUSH_PKT } from '@leed-ai/git-gateway-core/pkt-line/encode';\nimport {\n encodeErrPacket,\n encodeReportStatus,\n reportStatusFraming,\n} from '@leed-ai/git-gateway-core/receive-pack/report-status';\nimport {\n BAND_PROGRESS,\n encodeSideBand,\n SIDE_BAND_64K_MAX_PAYLOAD,\n SIDE_BAND_MAX_PAYLOAD,\n} from '@leed-ai/git-gateway-core/receive-pack/side-band';\n\nimport type { GitServiceName } from './git-route.ts';\n\n/** Headers git smart-HTTP responses carry so no proxy or browser caches them. */\nexport const NO_CACHE: Readonly<Record<string, string>> = {\n 'Cache-Control': 'no-cache, max-age=0, must-revalidate',\n Expires: 'Fri, 01 Jan 1980 00:00:00 GMT',\n Pragma: 'no-cache',\n};\n\n/** A plain-text response (for errors outside the git protocol). */\nexport function textResponse(status: number, body: string, headers: Record<string, string> = {}): Response {\n return new Response(body.endsWith('\\n') ? body : `${body}\\n`, {\n status,\n headers: { 'Content-Type': 'text/plain; charset=utf-8', ...NO_CACHE, ...headers },\n });\n}\n\n/** 401 with a Basic challenge, so git asks its credential helper and retries. */\nexport function unauthorized(realm: string): Response {\n return textResponse(401, 'Authentication required', { 'WWW-Authenticate': `Basic realm=\"${realm}\"` });\n}\n\n/**\n * A refusal git prints as \"fatal: remote error: <message>\": an \"ERR\" packet, after the\n * \"# service=…\" preamble for an info/refs request.\n */\nexport function errResponse(\n service: GitServiceName,\n endpoint: 'info-refs' | 'rpc',\n message: string,\n): Response {\n const err = encodeErrPacket(message);\n const body =\n endpoint === 'info-refs' ? concatBytes([encodePktLine(`# service=${service}\\n`), FLUSH_PKT, err]) : err;\n const kind = endpoint === 'info-refs' ? 'advertisement' : 'result';\n return new Response(body, {\n status: 200,\n headers: { 'Content-Type': `application/x-${service}-${kind}`, ...NO_CACHE },\n });\n}\n\n/** One update a refused push names, and why it was refused. */\nexport interface RefRefusal {\n readonly ref: string;\n readonly reason: string;\n}\n\n/**\n * The receive-pack response refusing a whole push in-protocol: \"ng <ref> <reason>\" for every\n * update (git prints \"! [remote rejected] <ref> (<reason>)\"), with each reason also sent as\n * progress text when a side-band was negotiated (git prints \"remote: …\"). A client that did not\n * ask for report-status gets an ERR packet instead.\n */\nexport function refusePush(capabilities: readonly string[], refusals: readonly RefRefusal[]): Response {\n const headers = { 'Content-Type': 'application/x-git-receive-pack-result', ...NO_CACHE };\n const wantsReport = capabilities.includes('report-status') || capabilities.includes('report-status-v2');\n if (!wantsReport) {\n return new Response(encodeErrPacket(refusals[0]?.reason ?? 'push refused'), { status: 200, headers });\n }\n const framing = reportStatusFraming(capabilities);\n const report = encodeReportStatus(\n { unpack: 'ok', refs: refusals.map(({ ref, reason }) => ({ ref, status: 'ng', reason })) },\n framing,\n );\n if (framing.sideBand === 'none') return new Response(report, { status: 200, headers });\n const progress = new TextEncoder().encode(\n refusals.map(({ ref, reason }) => `git-gateway: ${ref}: ${reason}\\n`).join(''),\n );\n const maxPayload = framing.sideBand === 'side-band' ? SIDE_BAND_MAX_PAYLOAD : SIDE_BAND_64K_MAX_PAYLOAD;\n return new Response(concatBytes([encodeSideBand(BAND_PROGRESS, progress, maxPayload), report]), {\n status: 200,\n headers,\n });\n}\n"],"mappings":";;;;;;AAkBA,MAAa,WAA6C;CACxD,iBAAiB;CACjB,SAAS;CACT,QAAQ;AACV;;AAGA,SAAgB,aAAa,QAAgB,MAAc,UAAkC,CAAC,GAAa;CACzG,OAAO,IAAI,SAAS,KAAK,SAAS,IAAI,IAAI,OAAO,GAAG,KAAK,KAAK;EAC5D;EACA,SAAS;GAAE,gBAAgB;GAA6B,GAAG;GAAU,GAAG;EAAQ;CAClF,CAAC;AACH;;AAGA,SAAgB,aAAa,OAAyB;CACpD,OAAO,aAAa,KAAK,2BAA2B,EAAE,oBAAoB,gBAAgB,MAAM,GAAG,CAAC;AACtG;;;;;AAMA,SAAgB,YACd,SACA,UACA,SACU;CACV,MAAM,MAAM,gBAAgB,OAAO;CACnC,MAAM,OACJ,aAAa,cAAc,YAAY;EAAC,cAAc,aAAa,QAAQ,GAAG;EAAG;EAAW;CAAG,CAAC,IAAI;CAEtG,OAAO,IAAI,SAAS,MAAM;EACxB,QAAQ;EACR,SAAS;GAAE,gBAAgB,iBAAiB,QAAQ,GAHzC,aAAa,cAAc,kBAAkB;GAGO,GAAG;EAAS;CAC7E,CAAC;AACH;;;;;;;AAcA,SAAgB,WAAW,cAAiC,UAA2C;CACrG,MAAM,UAAU;EAAE,gBAAgB;EAAyC,GAAG;CAAS;CAEvF,IAAI,EADgB,aAAa,SAAS,eAAe,KAAK,aAAa,SAAS,kBAAkB,IAEpG,OAAO,IAAI,SAAS,gBAAgB,SAAS,EAAE,EAAE,UAAU,cAAc,GAAG;EAAE,QAAQ;EAAK;CAAQ,CAAC;CAEtG,MAAM,UAAU,oBAAoB,YAAY;CAChD,MAAM,SAAS,mBACb;EAAE,QAAQ;EAAM,MAAM,SAAS,KAAK,EAAE,KAAK,cAAc;GAAE;GAAK,QAAQ;GAAM;EAAO,EAAE;CAAE,GACzF,OACF;CACA,IAAI,QAAQ,aAAa,QAAQ,OAAO,IAAI,SAAS,QAAQ;EAAE,QAAQ;EAAK;CAAQ,CAAC;CACrF,MAAM,WAAW,IAAI,YAAY,CAAC,CAAC,OACjC,SAAS,KAAK,EAAE,KAAK,aAAa,gBAAgB,IAAI,IAAI,OAAO,GAAG,CAAC,CAAC,KAAK,EAAE,CAC/E;CACA,MAAM,aAAa,QAAQ,aAAa,cAAc,wBAAwB;CAC9E,OAAO,IAAI,SAAS,YAAY,CAAC,eAAe,eAAe,UAAU,UAAU,GAAG,MAAM,CAAC,GAAG;EAC9F,QAAQ;EACR;CACF,CAAC;AACH"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { RepoName } from "@leed-ai/git-gateway-contract/schemas/names";
|
|
2
|
+
// src/proxy/git-route.ts
|
|
3
|
+
const PATH = /^\/([^/]+)\.git\/(info\/refs|git-upload-pack|git-receive-pack)$/;
|
|
4
|
+
/**
|
|
5
|
+
* Resolves `/<repo>.git/info/refs?service=…`, `/<repo>.git/git-upload-pack` and
|
|
6
|
+
* `/<repo>.git/git-receive-pack`. An info/refs request without a smart service is "dumb-http"
|
|
7
|
+
* (unsupported); anything else, including a repository name the contract does not allow, is
|
|
8
|
+
* "not-found".
|
|
9
|
+
*/
|
|
10
|
+
function resolveGitRequest(url) {
|
|
11
|
+
const match = PATH.exec(url.pathname);
|
|
12
|
+
if (match === null) return "not-found";
|
|
13
|
+
let name;
|
|
14
|
+
try {
|
|
15
|
+
name = decodeURIComponent(match[1] ?? "");
|
|
16
|
+
} catch {
|
|
17
|
+
return "not-found";
|
|
18
|
+
}
|
|
19
|
+
const repo = RepoName.safeParse(name);
|
|
20
|
+
if (!repo.success) return "not-found";
|
|
21
|
+
const rest = match[2];
|
|
22
|
+
const service = rest === "info/refs" ? url.searchParams.get("service") : rest;
|
|
23
|
+
if (service !== "git-upload-pack" && service !== "git-receive-pack") return "dumb-http";
|
|
24
|
+
const endpoint = rest === "info/refs" ? "info-refs" : service === "git-upload-pack" ? "upload-pack" : "receive-pack";
|
|
25
|
+
return {
|
|
26
|
+
repo: repo.data,
|
|
27
|
+
endpoint,
|
|
28
|
+
service,
|
|
29
|
+
operation: service === "git-receive-pack" ? "write" : "read"
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export { resolveGitRequest };
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=git-route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-route.js","names":[],"sources":["../../src/proxy/git-route.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport type { GitOperation, SmartHttpEndpoint } from '@leed-ai/git-gateway-contract/proxy/git-operation';\nimport { RepoName } from '@leed-ai/git-gateway-contract/schemas/names';\n\n/** A git service as named on the wire. */\nexport type GitServiceName = 'git-upload-pack' | 'git-receive-pack';\n\n/** What a smart-HTTP request addresses. */\nexport interface GitRequest {\n readonly repo: RepoName;\n readonly endpoint: SmartHttpEndpoint;\n readonly service: GitServiceName;\n readonly operation: GitOperation;\n}\n\n/** Why a path is not a smart-HTTP request the gateway serves. */\nexport type GitRouteFailure = 'not-found' | 'dumb-http';\n\nconst PATH = /^\\/([^/]+)\\.git\\/(info\\/refs|git-upload-pack|git-receive-pack)$/;\n\n/**\n * Resolves `/<repo>.git/info/refs?service=…`, `/<repo>.git/git-upload-pack` and\n * `/<repo>.git/git-receive-pack`. An info/refs request without a smart service is \"dumb-http\"\n * (unsupported); anything else, including a repository name the contract does not allow, is\n * \"not-found\".\n */\nexport function resolveGitRequest(url: URL): GitRequest | GitRouteFailure {\n const match = PATH.exec(url.pathname);\n if (match === null) return 'not-found';\n let name: string;\n try {\n name = decodeURIComponent(match[1] ?? '');\n } catch {\n return 'not-found';\n }\n const repo = RepoName.safeParse(name);\n if (!repo.success) return 'not-found';\n const rest = match[2];\n const service = rest === 'info/refs' ? url.searchParams.get('service') : rest;\n if (service !== 'git-upload-pack' && service !== 'git-receive-pack') return 'dumb-http';\n const endpoint: SmartHttpEndpoint =\n rest === 'info/refs' ? 'info-refs' : service === 'git-upload-pack' ? 'upload-pack' : 'receive-pack';\n return {\n repo: repo.data,\n endpoint,\n service,\n operation: service === 'git-receive-pack' ? 'write' : 'read',\n };\n}\n"],"mappings":";;AAkBA,MAAM,OAAO;;;;;;;AAQb,SAAgB,kBAAkB,KAAwC;CACxE,MAAM,QAAQ,KAAK,KAAK,IAAI,QAAQ;CACpC,IAAI,UAAU,MAAM,OAAO;CAC3B,IAAI;CACJ,IAAI;EACF,OAAO,mBAAmB,MAAM,MAAM,EAAE;CAC1C,QAAQ;EACN,OAAO;CACT;CACA,MAAM,OAAO,SAAS,UAAU,IAAI;CACpC,IAAI,CAAC,KAAK,SAAS,OAAO;CAC1B,MAAM,OAAO,MAAM;CACnB,MAAM,UAAU,SAAS,cAAc,IAAI,aAAa,IAAI,SAAS,IAAI;CACzE,IAAI,YAAY,qBAAqB,YAAY,oBAAoB,OAAO;CAC5E,MAAM,WACJ,SAAS,cAAc,cAAc,YAAY,oBAAoB,gBAAgB;CACvF,OAAO;EACL,MAAM,KAAK;EACX;EACA;EACA,WAAW,YAAY,qBAAqB,UAAU;CACxD;AACF"}
|