@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.
Files changed (269) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +49 -2
  3. package/dist/audit/audit-recorder.d.ts +2 -0
  4. package/dist/audit/audit-recorder.js +53 -0
  5. package/dist/audit/audit-recorder.js.map +1 -0
  6. package/dist/audit/push-report.d.ts +13 -0
  7. package/dist/audit/push-report.js +66 -0
  8. package/dist/audit/push-report.js.map +1 -0
  9. package/dist/audit-recorder-K1Ns3tfC.d.ts +31 -0
  10. package/dist/auth/authenticate.d.ts +14 -0
  11. package/dist/auth/authenticate.js +59 -0
  12. package/dist/auth/authenticate.js.map +1 -0
  13. package/dist/auth/authorize.d.ts +12 -0
  14. package/dist/auth/authorize.js +57 -0
  15. package/dist/auth/authorize.js.map +1 -0
  16. package/dist/auth/read-token-auth.d.ts +60 -0
  17. package/dist/auth/read-token-auth.js +83 -0
  18. package/dist/auth/read-token-auth.js.map +1 -0
  19. package/dist/cache/decision-cache.d.ts +2 -0
  20. package/dist/cache/decision-cache.js +45 -0
  21. package/dist/cache/decision-cache.js.map +1 -0
  22. package/dist/commit-workflow-schemas-BWlg7bZR.d.ts +95 -0
  23. package/dist/decision-cache-87U32iSj.d.ts +24 -0
  24. package/dist/gateway-app.d.ts +69 -0
  25. package/dist/gateway-app.js +80 -0
  26. package/dist/gateway-app.js.map +1 -0
  27. package/dist/gateway-context-Boh9I0mk.d.ts +54 -0
  28. package/dist/gateway-env-DnsBU4Pq.d.ts +40 -0
  29. package/dist/gateway-worker.d.ts +13 -0
  30. package/dist/gateway-worker.js +22 -0
  31. package/dist/gateway-worker.js.map +1 -0
  32. package/dist/git-route-Spxi09Rm.d.ts +22 -0
  33. package/dist/health.d.ts +6 -0
  34. package/dist/health.js +16 -0
  35. package/dist/health.js.map +1 -0
  36. package/dist/logger-BZ1zyKLb.d.ts +27 -0
  37. package/dist/logging/logger.d.ts +2 -0
  38. package/dist/logging/logger.js +15 -0
  39. package/dist/logging/logger.js.map +1 -0
  40. package/dist/on-push-DTREOB__.d.ts +62 -0
  41. package/dist/policy/branch-policy.d.ts +24 -0
  42. package/dist/policy/branch-policy.js +125 -0
  43. package/dist/policy/branch-policy.js.map +1 -0
  44. package/dist/policy/enforce-branch-policy.d.ts +30 -0
  45. package/dist/policy/enforce-branch-policy.js +214 -0
  46. package/dist/policy/enforce-branch-policy.js.map +1 -0
  47. package/dist/policy/fast-forward.d.ts +32 -0
  48. package/dist/policy/fast-forward.js +95 -0
  49. package/dist/policy/fast-forward.js.map +1 -0
  50. package/dist/policy/ref-pattern.d.ts +7 -0
  51. package/dist/policy/ref-pattern.js +25 -0
  52. package/dist/policy/ref-pattern.js.map +1 -0
  53. package/dist/policy/rule-branch-policy.d.ts +17 -0
  54. package/dist/policy/rule-branch-policy.js +69 -0
  55. package/dist/policy/rule-branch-policy.js.map +1 -0
  56. package/dist/proxy/defer.d.ts +8 -0
  57. package/dist/proxy/defer.js +15 -0
  58. package/dist/proxy/defer.js.map +1 -0
  59. package/dist/proxy/forward-read.d.ts +11 -0
  60. package/dist/proxy/forward-read.js +53 -0
  61. package/dist/proxy/forward-read.js.map +1 -0
  62. package/dist/proxy/gateway-deps.d.ts +39 -0
  63. package/dist/proxy/gateway-deps.js +0 -0
  64. package/dist/proxy/gateway-env.d.ts +2 -0
  65. package/dist/proxy/gateway-env.js +0 -0
  66. package/dist/proxy/git-responses.d.ts +25 -0
  67. package/dist/proxy/git-responses.js +83 -0
  68. package/dist/proxy/git-responses.js.map +1 -0
  69. package/dist/proxy/git-route.d.ts +2 -0
  70. package/dist/proxy/git-route.js +34 -0
  71. package/dist/proxy/git-route.js.map +1 -0
  72. package/dist/proxy/push-events.d.ts +28 -0
  73. package/dist/proxy/push-events.js +65 -0
  74. package/dist/proxy/push-events.js.map +1 -0
  75. package/dist/proxy/push-guard.d.ts +43 -0
  76. package/dist/proxy/push-guard.js +81 -0
  77. package/dist/proxy/push-guard.js.map +1 -0
  78. package/dist/proxy/push-options.d.ts +16 -0
  79. package/dist/proxy/push-options.js +33 -0
  80. package/dist/proxy/push-options.js.map +1 -0
  81. package/dist/proxy/receive-pack.d.ts +17 -0
  82. package/dist/proxy/receive-pack.js +244 -0
  83. package/dist/proxy/receive-pack.js.map +1 -0
  84. package/dist/proxy/request-log.d.ts +13 -0
  85. package/dist/proxy/request-log.js +41 -0
  86. package/dist/proxy/request-log.js.map +1 -0
  87. package/dist/proxy/resolve-route.d.ts +9 -0
  88. package/dist/proxy/resolve-route.js +22 -0
  89. package/dist/proxy/resolve-route.js.map +1 -0
  90. package/dist/proxy/upstream-call.d.ts +23 -0
  91. package/dist/proxy/upstream-call.js +63 -0
  92. package/dist/proxy/upstream-call.js.map +1 -0
  93. package/dist/proxy/upstream.d.ts +2 -0
  94. package/dist/proxy/upstream.js +68 -0
  95. package/dist/proxy/upstream.js.map +1 -0
  96. package/dist/push-event-f-5TsFUY.d.ts +15 -0
  97. package/dist/push-policy/changed-paths.d.ts +26 -0
  98. package/dist/push-policy/changed-paths.js +117 -0
  99. package/dist/push-policy/changed-paths.js.map +1 -0
  100. package/dist/push-policy/pushed-objects.d.ts +30 -0
  101. package/dist/push-policy/pushed-objects.js +76 -0
  102. package/dist/push-policy/pushed-objects.js.map +1 -0
  103. package/dist/push-policy/run-push-policy.d.ts +35 -0
  104. package/dist/push-policy/run-push-policy.js +77 -0
  105. package/dist/push-policy/run-push-policy.js.map +1 -0
  106. package/dist/push-policy/tree-diff.d.ts +21 -0
  107. package/dist/push-policy/tree-diff.js +78 -0
  108. package/dist/push-policy/tree-diff.js.map +1 -0
  109. package/dist/push-policy/uncheckable.d.ts +14 -0
  110. package/dist/push-policy/uncheckable.js +25 -0
  111. package/dist/push-policy/uncheckable.js.map +1 -0
  112. package/dist/rpc/branch-methods.d.ts +16 -0
  113. package/dist/rpc/branch-methods.js +43 -0
  114. package/dist/rpc/branch-methods.js.map +1 -0
  115. package/dist/rpc/commit-attempt.d.ts +27 -0
  116. package/dist/rpc/commit-attempt.js +85 -0
  117. package/dist/rpc/commit-attempt.js.map +1 -0
  118. package/dist/rpc/commit-graph.d.ts +20 -0
  119. package/dist/rpc/commit-graph.js +38 -0
  120. package/dist/rpc/commit-graph.js.map +1 -0
  121. package/dist/rpc/commit-method.d.ts +13 -0
  122. package/dist/rpc/commit-method.js +30 -0
  123. package/dist/rpc/commit-method.js.map +1 -0
  124. package/dist/rpc/commit-source.d.ts +12 -0
  125. package/dist/rpc/commit-source.js +9 -0
  126. package/dist/rpc/commit-source.js.map +1 -0
  127. package/dist/rpc/gateway-context.d.ts +2 -0
  128. package/dist/rpc/gateway-context.js +30 -0
  129. package/dist/rpc/gateway-context.js.map +1 -0
  130. package/dist/rpc/gateway-rpc.d.ts +10 -0
  131. package/dist/rpc/gateway-rpc.js +48 -0
  132. package/dist/rpc/gateway-rpc.js.map +1 -0
  133. package/dist/rpc/gateway-service.d.ts +24 -0
  134. package/dist/rpc/gateway-service.js +63 -0
  135. package/dist/rpc/gateway-service.js.map +1 -0
  136. package/dist/rpc/history-page.d.ts +35 -0
  137. package/dist/rpc/history-page.js +122 -0
  138. package/dist/rpc/history-page.js.map +1 -0
  139. package/dist/rpc/lifecycle-methods.d.ts +12 -0
  140. package/dist/rpc/lifecycle-methods.js +39 -0
  141. package/dist/rpc/lifecycle-methods.js.map +1 -0
  142. package/dist/rpc/paint-down.d.ts +17 -0
  143. package/dist/rpc/paint-down.js +96 -0
  144. package/dist/rpc/paint-down.js.map +1 -0
  145. package/dist/rpc/promote-attempt.d.ts +21 -0
  146. package/dist/rpc/promote-attempt.js +55 -0
  147. package/dist/rpc/promote-attempt.js.map +1 -0
  148. package/dist/rpc/push-ref.d.ts +21 -0
  149. package/dist/rpc/push-ref.js +47 -0
  150. package/dist/rpc/push-ref.js.map +1 -0
  151. package/dist/rpc/read-methods.d.ts +17 -0
  152. package/dist/rpc/read-methods.js +94 -0
  153. package/dist/rpc/read-methods.js.map +1 -0
  154. package/dist/rpc/read-token.d.ts +39 -0
  155. package/dist/rpc/read-token.js +107 -0
  156. package/dist/rpc/read-token.js.map +1 -0
  157. package/dist/rpc/repo-access.d.ts +36 -0
  158. package/dist/rpc/repo-access.js +75 -0
  159. package/dist/rpc/repo-access.js.map +1 -0
  160. package/dist/rpc/sync-attempt.d.ts +23 -0
  161. package/dist/rpc/sync-attempt.js +105 -0
  162. package/dist/rpc/sync-attempt.js.map +1 -0
  163. package/dist/sync-workflow-schemas-DCNmNE5-.d.ts +57 -0
  164. package/dist/upstream-DvqMCj5N.d.ts +15 -0
  165. package/dist/workflow-starter-BqhwX6oA.d.ts +45 -0
  166. package/dist/workflows/commit-workflow-schemas.d.ts +2 -0
  167. package/dist/workflows/commit-workflow-schemas.js +84 -0
  168. package/dist/workflows/commit-workflow-schemas.js.map +1 -0
  169. package/dist/workflows/commit-workflow.d.ts +16 -0
  170. package/dist/workflows/commit-workflow.js +22 -0
  171. package/dist/workflows/commit-workflow.js.map +1 -0
  172. package/dist/workflows/on-push.d.ts +2 -0
  173. package/dist/workflows/on-push.js +90 -0
  174. package/dist/workflows/on-push.js.map +1 -0
  175. package/dist/workflows/push-event.d.ts +2 -0
  176. package/dist/workflows/push-event.js +28 -0
  177. package/dist/workflows/push-event.js.map +1 -0
  178. package/dist/workflows/run-commit-workflow.d.ts +15 -0
  179. package/dist/workflows/run-commit-workflow.js +49 -0
  180. package/dist/workflows/run-commit-workflow.js.map +1 -0
  181. package/dist/workflows/run-sync-workflow.d.ts +14 -0
  182. package/dist/workflows/run-sync-workflow.js +67 -0
  183. package/dist/workflows/run-sync-workflow.js.map +1 -0
  184. package/dist/workflows/step-errors.d.ts +15 -0
  185. package/dist/workflows/step-errors.js +24 -0
  186. package/dist/workflows/step-errors.js.map +1 -0
  187. package/dist/workflows/step-runner.d.ts +31 -0
  188. package/dist/workflows/step-runner.js +6 -0
  189. package/dist/workflows/step-runner.js.map +1 -0
  190. package/dist/workflows/sync-branch-workflow.d.ts +15 -0
  191. package/dist/workflows/sync-branch-workflow.js +21 -0
  192. package/dist/workflows/sync-branch-workflow.js.map +1 -0
  193. package/dist/workflows/sync-workflow-schemas.d.ts +2 -0
  194. package/dist/workflows/sync-workflow-schemas.js +104 -0
  195. package/dist/workflows/sync-workflow-schemas.js.map +1 -0
  196. package/dist/workflows/workflow-instance-id.d.ts +12 -0
  197. package/dist/workflows/workflow-instance-id.js +33 -0
  198. package/dist/workflows/workflow-instance-id.js.map +1 -0
  199. package/dist/workflows/workflow-runtime.d.ts +7 -0
  200. package/dist/workflows/workflow-runtime.js +11 -0
  201. package/dist/workflows/workflow-runtime.js.map +1 -0
  202. package/dist/workflows/workflow-starter.d.ts +2 -0
  203. package/dist/workflows/workflow-starter.js +40 -0
  204. package/dist/workflows/workflow-starter.js.map +1 -0
  205. package/package.json +381 -4
  206. package/src/audit/audit-recorder.ts +74 -0
  207. package/src/audit/push-report.ts +60 -0
  208. package/src/auth/authenticate.ts +68 -0
  209. package/src/auth/authorize.ts +57 -0
  210. package/src/auth/read-token-auth.ts +119 -0
  211. package/src/cache/decision-cache.ts +57 -0
  212. package/src/gateway-app.ts +147 -0
  213. package/src/gateway-worker.ts +32 -0
  214. package/src/health.ts +12 -0
  215. package/src/logging/logger.ts +40 -0
  216. package/src/policy/branch-policy.ts +119 -0
  217. package/src/policy/enforce-branch-policy.ts +213 -0
  218. package/src/policy/fast-forward.ts +136 -0
  219. package/src/policy/ref-pattern.ts +24 -0
  220. package/src/policy/rule-branch-policy.ts +76 -0
  221. package/src/proxy/defer.ts +18 -0
  222. package/src/proxy/forward-read.ts +52 -0
  223. package/src/proxy/gateway-deps.ts +43 -0
  224. package/src/proxy/gateway-env.ts +43 -0
  225. package/src/proxy/git-responses.ts +89 -0
  226. package/src/proxy/git-route.ts +49 -0
  227. package/src/proxy/push-events.ts +91 -0
  228. package/src/proxy/push-guard.ts +109 -0
  229. package/src/proxy/push-options.ts +38 -0
  230. package/src/proxy/receive-pack.ts +257 -0
  231. package/src/proxy/request-log.ts +44 -0
  232. package/src/proxy/resolve-route.ts +24 -0
  233. package/src/proxy/upstream-call.ts +71 -0
  234. package/src/proxy/upstream.ts +76 -0
  235. package/src/push-policy/changed-paths.ts +143 -0
  236. package/src/push-policy/pushed-objects.ts +88 -0
  237. package/src/push-policy/run-push-policy.ts +99 -0
  238. package/src/push-policy/tree-diff.ts +107 -0
  239. package/src/push-policy/uncheckable.ts +27 -0
  240. package/src/rpc/branch-methods.ts +57 -0
  241. package/src/rpc/commit-attempt.ts +87 -0
  242. package/src/rpc/commit-graph.ts +44 -0
  243. package/src/rpc/commit-method.ts +36 -0
  244. package/src/rpc/commit-source.ts +15 -0
  245. package/src/rpc/gateway-context.ts +77 -0
  246. package/src/rpc/gateway-rpc.ts +65 -0
  247. package/src/rpc/gateway-service.ts +85 -0
  248. package/src/rpc/history-page.ts +163 -0
  249. package/src/rpc/lifecycle-methods.ts +50 -0
  250. package/src/rpc/paint-down.ts +120 -0
  251. package/src/rpc/promote-attempt.ts +52 -0
  252. package/src/rpc/push-ref.ts +54 -0
  253. package/src/rpc/read-methods.ts +111 -0
  254. package/src/rpc/read-token.ts +135 -0
  255. package/src/rpc/repo-access.ts +112 -0
  256. package/src/rpc/sync-attempt.ts +84 -0
  257. package/src/workflows/commit-workflow-schemas.ts +95 -0
  258. package/src/workflows/commit-workflow.ts +39 -0
  259. package/src/workflows/on-push.ts +128 -0
  260. package/src/workflows/push-event.ts +41 -0
  261. package/src/workflows/run-commit-workflow.ts +45 -0
  262. package/src/workflows/run-sync-workflow.ts +52 -0
  263. package/src/workflows/step-errors.ts +26 -0
  264. package/src/workflows/step-runner.ts +36 -0
  265. package/src/workflows/sync-branch-workflow.ts +40 -0
  266. package/src/workflows/sync-workflow-schemas.ts +122 -0
  267. package/src/workflows/workflow-instance-id.ts +26 -0
  268. package/src/workflows/workflow-runtime.ts +13 -0
  269. package/src/workflows/workflow-starter.ts +69 -0
@@ -0,0 +1,24 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import type { MiddlewareHandler } from 'hono';
3
+
4
+ import type { GatewayEnv } from './gateway-env.ts';
5
+ import { textResponse } from './git-responses.ts';
6
+ import { resolveGitRequest } from './git-route.ts';
7
+
8
+ /**
9
+ * Route middleware for `/<repo>.git/…`: resolves the repository and endpoint and checks the
10
+ * method, before any authentication. Dumb HTTP (info/refs without a smart service) is refused
11
+ * with 403, as git-http-backend does; unknown paths and invalid repository names are 404.
12
+ */
13
+ export function resolveRoute(): MiddlewareHandler<GatewayEnv> {
14
+ return async (context, next) => {
15
+ const resolved = resolveGitRequest(new URL(context.req.url));
16
+ if (resolved === 'not-found') return textResponse(404, 'Not found');
17
+ if (resolved === 'dumb-http')
18
+ return textResponse(403, 'Dumb HTTP is not supported; use a smart git client');
19
+ const expected = resolved.endpoint === 'info-refs' ? 'GET' : 'POST';
20
+ if (context.req.method !== expected) return textResponse(405, 'Method not allowed', { Allow: expected });
21
+ context.set('git', resolved);
22
+ return next();
23
+ };
24
+ }
@@ -0,0 +1,71 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { readGatewayError } from '@leed-ai/git-gateway-contract/gateway-error';
3
+ import type { Context } from 'hono';
4
+
5
+ import type { GatewayDeps } from './gateway-deps.ts';
6
+ import type { GatewayEnv } from './gateway-env.ts';
7
+ import { textResponse } from './git-responses.ts';
8
+ import { clientHeaders, upstreamRequest } from './upstream.ts';
9
+
10
+ /** The storage host's answer, or the response to give the client instead. */
11
+ export type UpstreamResult =
12
+ | { readonly ok: true; readonly response: Response }
13
+ | { readonly ok: false; readonly response: Response; readonly reason: string };
14
+
15
+ function failed(requestId: string, status: number, reason: string): UpstreamResult {
16
+ return { ok: false, reason, response: textResponse(status, reason, { 'X-Request-Id': requestId }) };
17
+ }
18
+
19
+ /**
20
+ * Mints a git remote for this request from the backend (a fresh short-lived credential each time)
21
+ * and sends the request to the storage host. A 2xx answer comes back as is for the caller to
22
+ * stream; anything else becomes a plain-text error for the client: 404 for a missing repository,
23
+ * 502 when the host refused the gateway's own credential or failed. The host's headers and the
24
+ * credential never reach the client. `body` errors (a refused push) surface as a thrown error.
25
+ */
26
+ export async function callUpstream(
27
+ deps: GatewayDeps,
28
+ context: Context<GatewayEnv>,
29
+ body: ReadableStream<Uint8Array> | null,
30
+ ): Promise<UpstreamResult> {
31
+ const requestId = context.get('requestId');
32
+ const git = context.get('git');
33
+ let remote;
34
+ try {
35
+ remote = await deps.backend.gitRemote(git.repo, git.operation);
36
+ } catch (error) {
37
+ const gatewayError = readGatewayError(error);
38
+ if (gatewayError?.code === 'repo_not_found')
39
+ return failed(requestId, 404, `Repository not found: ${git.repo}`);
40
+ deps.logger.log({
41
+ level: 'error',
42
+ message: 'minting the storage remote failed',
43
+ requestId,
44
+ repo: git.repo,
45
+ error: gatewayError?.code ?? (error instanceof Error ? error.name : 'unknown'),
46
+ });
47
+ return failed(requestId, 502, 'The storage backend is unavailable; try again later');
48
+ }
49
+ const upstream = await deps.fetch(upstreamRequest(context.req.raw, git, remote, body));
50
+ if (upstream.status >= 200 && upstream.status < 300) return { ok: true, response: upstream };
51
+ await upstream.body?.cancel().catch(() => undefined);
52
+ deps.logger.log({
53
+ level: upstream.status === 404 ? 'info' : 'error',
54
+ message: 'storage host refused the request',
55
+ requestId,
56
+ repo: git.repo,
57
+ endpoint: git.endpoint,
58
+ upstreamStatus: upstream.status,
59
+ });
60
+ if (upstream.status === 404) return failed(requestId, 404, `Repository not found: ${git.repo}`);
61
+ return failed(requestId, 502, 'The storage backend failed to answer; try again later');
62
+ }
63
+
64
+ /** The client response for a 2xx storage host response, streamed through untouched. */
65
+ export function streamToClient(
66
+ upstream: Response,
67
+ body: ReadableStream<Uint8Array> | null,
68
+ requestId: string,
69
+ ): Response {
70
+ return new Response(body, { status: upstream.status, headers: clientHeaders(upstream, requestId) });
71
+ }
@@ -0,0 +1,76 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import type { GitRemote } from '@leed-ai/git-gateway-contract/storage/git-remote';
3
+
4
+ import { NO_CACHE } from './git-responses.ts';
5
+ import type { GitRequest } from './git-route.ts';
6
+
7
+ /** Sends one HTTP request: `fetch`, or a stand-in such as an in-memory backend's handler. */
8
+ export type UpstreamFetch = (request: Request) => Promise<Response>;
9
+
10
+ /** Response headers passed back from the storage host; everything else (cookies, auth challenges) is dropped. */
11
+ const RESPONSE_HEADERS = ['content-type', 'cache-control', 'expires', 'pragma'] as const;
12
+
13
+ /**
14
+ * The protocol version header to send upstream. Fetches pass it through untouched, so protocol v2
15
+ * reaches a host that speaks it; pushes use protocol v1 receive-pack (which every client and host
16
+ * supports), so a "version=2" request is dropped and the host answers in v0/v1.
17
+ */
18
+ function gitProtocol(request: Request, git: GitRequest): string | null {
19
+ const value = request.headers.get('Git-Protocol');
20
+ if (value === null) return null;
21
+ if (git.service === 'git-upload-pack') return value;
22
+ const kept = value
23
+ .split(':')
24
+ .filter((part) => part.trim() !== '' && !/^version=2$/.test(part.trim()))
25
+ .join(':');
26
+ return kept === '' ? null : kept;
27
+ }
28
+
29
+ /**
30
+ * The request to send to the storage host for a client request: the host's URL for the same
31
+ * endpoint, the credential headers minted for this request, and only the client headers the git
32
+ * protocol needs (Content-Type, Accept, Git-Protocol, User-Agent, and Content-Encoding for a
33
+ * fetch). The client's Authorization, cookies and anything else never leave the gateway.
34
+ */
35
+ export function upstreamRequest(
36
+ request: Request,
37
+ git: GitRequest,
38
+ remote: GitRemote,
39
+ body: ReadableStream<Uint8Array> | null,
40
+ ): Request {
41
+ const headers = new Headers();
42
+ for (const name of ['Content-Type', 'Accept', 'User-Agent']) {
43
+ const value = request.headers.get(name);
44
+ if (value !== null) headers.set(name, value);
45
+ }
46
+ const protocol = gitProtocol(request, git);
47
+ if (protocol !== null) headers.set('Git-Protocol', protocol);
48
+ const encoding = request.headers.get('Content-Encoding');
49
+ if (encoding !== null && git.service === 'git-upload-pack' && body !== null)
50
+ headers.set('Content-Encoding', encoding);
51
+ for (const [name, value] of Object.entries(remote.headers)) headers.set(name, value);
52
+ const base = remote.url.replace(/\/$/, '');
53
+ const url =
54
+ git.endpoint === 'info-refs' ? `${base}/info/refs?service=${git.service}` : `${base}/${git.service}`;
55
+ const init: RequestInit & { duplex?: 'half' } = {
56
+ method: git.endpoint === 'info-refs' ? 'GET' : 'POST',
57
+ headers,
58
+ redirect: 'manual',
59
+ };
60
+ if (body !== null) {
61
+ init.body = body;
62
+ init.duplex = 'half';
63
+ }
64
+ return new Request(url, init);
65
+ }
66
+
67
+ /** The response headers to give the client for a storage host response. */
68
+ export function clientHeaders(upstream: Response, requestId: string): Headers {
69
+ const headers = new Headers(NO_CACHE);
70
+ for (const name of RESPONSE_HEADERS) {
71
+ const value = upstream.headers.get(name);
72
+ if (value !== null) headers.set(name, value);
73
+ }
74
+ headers.set('X-Request-Id', requestId);
75
+ return headers;
76
+ }
@@ -0,0 +1,143 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import type { ChangedPath, PushedCommit } from '@leed-ai/git-gateway-contract/proxy/push-policy';
3
+ import type { RefUpdate } from '@leed-ai/git-gateway-contract/schemas/ref-update';
4
+ import { ZERO_SHA } from '@leed-ai/git-gateway-contract/schemas/sha';
5
+ import type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';
6
+ import { isCoreError } from '@leed-ai/git-gateway-core/core-error';
7
+ import { decodeTree } from '@leed-ai/git-gateway-core/objects/tree-object';
8
+ import type { GitTreeEntry } from '@leed-ai/git-gateway-core/schemas/git-object';
9
+
10
+ import type { PushedObjects } from './pushed-objects.ts';
11
+ import { ChangeSink, diffTrees, type TreeChange } from './tree-diff.ts';
12
+ import { Uncheckable, type ReadBudget } from './uncheckable.ts';
13
+
14
+ /** Where changed paths are computed from, and the bounds on computing them. */
15
+ export interface ChangedPathSources {
16
+ readonly repo: string;
17
+ readonly backend: Pick<StorageBackend, 'readCommit' | 'readTree' | 'readBlob'>;
18
+ readonly objects: PushedObjects;
19
+ readonly budget: ReadBudget;
20
+ readonly maxChangedPaths: number;
21
+ }
22
+
23
+ const SUBMODULE = '160000';
24
+
25
+ /**
26
+ * Computes, for each update of a push, the commits it brings (those of the pushed pack reachable
27
+ * from its new id) and the paths each changes. Trees and commits come from the pack first and
28
+ * from storage otherwise, each storage call counted against the budget; reads are shared across
29
+ * updates. Throws {@link Uncheckable} when something cannot be read or a bound is passed.
30
+ */
31
+ export class ChangedPaths {
32
+ readonly #sources: ChangedPathSources;
33
+ readonly #sink: ChangeSink;
34
+ readonly #trees = new Map<string, Promise<readonly GitTreeEntry[]>>();
35
+ readonly #commitTrees = new Map<string, Promise<string>>();
36
+ readonly #sizes = new Map<string, Promise<number>>();
37
+
38
+ constructor(sources: ChangedPathSources) {
39
+ this.#sources = sources;
40
+ this.#sink = new ChangeSink(sources.maxChangedPaths);
41
+ }
42
+
43
+ /** The new commits an update brings, newest first, with their changes. */
44
+ async commitsOf(update: RefUpdate): Promise<PushedCommit[]> {
45
+ if (update.newSha === ZERO_SHA) return [];
46
+ const { commits } = this.#sources.objects;
47
+ const result: PushedCommit[] = [];
48
+ const seen = new Set([update.newSha]);
49
+ const queue = [update.newSha];
50
+ for (let sha = queue.shift(); sha !== undefined; sha = queue.shift()) {
51
+ const commit = commits.get(sha);
52
+ if (commit === undefined) continue;
53
+ if (commit.parents === null) throw new Uncheckable(`commit ${sha} has a header git reads differently`);
54
+ result.push({
55
+ sha,
56
+ parents: [...commit.parents],
57
+ changes: await this.#changesOf(commit.tree, commit.parents),
58
+ });
59
+ for (const parent of commit.parents) {
60
+ if (!seen.has(parent)) {
61
+ seen.add(parent);
62
+ queue.push(parent);
63
+ }
64
+ }
65
+ }
66
+ return result;
67
+ }
68
+
69
+ /**
70
+ * A commit's changes: against its parent, the empty tree for a root, and for a merge only the
71
+ * paths that differ from every parent (the first parent's view of each).
72
+ */
73
+ async #changesOf(tree: string, parents: readonly string[]): Promise<ChangedPath[]> {
74
+ const lookup = async (sha: string) => this.#tree(sha);
75
+ const [first, ...others] = parents;
76
+ let changes = await diffTrees(
77
+ lookup,
78
+ first === undefined ? null : await this.#treeOf(first),
79
+ tree,
80
+ this.#sink,
81
+ );
82
+ for (const other of others) {
83
+ const paths = new Set(
84
+ (await diffTrees(lookup, await this.#treeOf(other), tree, this.#sink)).map((c) => c.path),
85
+ );
86
+ changes = changes.filter((change) => paths.has(change.path));
87
+ }
88
+ const sorted = changes.toSorted((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
89
+ return Promise.all(sorted.map(async (change) => ({ ...change, size: await this.#sizeOf(change) })));
90
+ }
91
+
92
+ async #sizeOf(change: TreeChange): Promise<number | null> {
93
+ if (change.newSha === null || change.newMode === SUBMODULE) return null;
94
+ const sha = change.newSha;
95
+ const inPack = this.#sources.objects.blobSizes.get(sha);
96
+ if (inPack !== undefined) return inPack;
97
+ return this.#cached(this.#sizes, sha, async () => {
98
+ this.#sources.budget.spend();
99
+ const blob = await this.#sources.backend.readBlob(this.#sources.repo, sha);
100
+ if (blob === null) throw new Uncheckable(`blob ${sha} is in neither the pushed pack nor storage`);
101
+ return blob.length;
102
+ });
103
+ }
104
+
105
+ async #treeOf(commitSha: string): Promise<string> {
106
+ const inPack = this.#sources.objects.commits.get(commitSha);
107
+ if (inPack !== undefined) return inPack.tree;
108
+ return this.#cached(this.#commitTrees, commitSha, async () => {
109
+ this.#sources.budget.spend();
110
+ const commit = await this.#sources.backend.readCommit(this.#sources.repo, commitSha);
111
+ if (commit === null)
112
+ throw new Uncheckable(`commit ${commitSha} is in neither the pushed pack nor storage`);
113
+ return commit.tree;
114
+ });
115
+ }
116
+
117
+ async #tree(sha: string): Promise<readonly GitTreeEntry[]> {
118
+ return this.#cached(this.#trees, sha, async () => {
119
+ const data = this.#sources.objects.trees.get(sha);
120
+ if (data !== undefined) {
121
+ try {
122
+ return decodeTree(data, sha);
123
+ } catch (error) {
124
+ if (isCoreError(error)) throw new Uncheckable(`tree ${sha} could not be read (${error.message})`);
125
+ throw error;
126
+ }
127
+ }
128
+ this.#sources.budget.spend();
129
+ const entries = await this.#sources.backend.readTree(this.#sources.repo, sha);
130
+ if (entries === null) throw new Uncheckable(`tree ${sha} is in neither the pushed pack nor storage`);
131
+ return entries.map(({ name, mode, sha: id }) => ({ name, mode, sha: id }));
132
+ });
133
+ }
134
+
135
+ async #cached<T>(cache: Map<string, Promise<T>>, key: string, load: () => Promise<T>): Promise<T> {
136
+ let found = cache.get(key);
137
+ if (found === undefined) {
138
+ found = load();
139
+ cache.set(key, found);
140
+ }
141
+ return found;
142
+ }
143
+ }
@@ -0,0 +1,88 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';
3
+ import { isCoreError } from '@leed-ai/git-gateway-core/core-error';
4
+ import { commitHeaderLines, readCommitParents } from '@leed-ai/git-gateway-core/objects/commit-parents';
5
+ import { hashObject } from '@leed-ai/git-gateway-core/objects/hash-object';
6
+ import { encodeTree } from '@leed-ai/git-gateway-core/objects/tree-object';
7
+ import { readPack } from '@leed-ai/git-gateway-core/pack/read-pack';
8
+ import type { GitObject } from '@leed-ai/git-gateway-core/schemas/git-object';
9
+
10
+ import { Uncheckable, type ReadBudget } from './uncheckable.ts';
11
+
12
+ /** A commit of the pushed pack: its tree, and its parents as git reads them (null: unreadable). */
13
+ export interface PackCommit {
14
+ readonly tree: string;
15
+ readonly parents: readonly string[] | null;
16
+ }
17
+
18
+ /** The objects of a pushed pack, indexed for computing changed paths. */
19
+ export interface PushedObjects {
20
+ readonly commits: ReadonlyMap<string, PackCommit>;
21
+ /** Tree objects' content by id. */
22
+ readonly trees: ReadonlyMap<string, Uint8Array>;
23
+ /** Blob sizes by id. */
24
+ readonly blobSizes: ReadonlyMap<string, number>;
25
+ }
26
+
27
+ /** Where a pushed pack's thin-pack bases are read from, and the bounds on reading it. */
28
+ export interface PushedObjectSources {
29
+ readonly repo: string;
30
+ readonly backend: Pick<StorageBackend, 'readTree' | 'readBlob'>;
31
+ readonly budget: ReadBudget;
32
+ readonly maxObjectBytes: number;
33
+ }
34
+
35
+ /**
36
+ * Reads a thin pack's delta base from storage: as a tree (re-encoded, and accepted only when it
37
+ * hashes to the id asked for, so a backend's view of the tree cannot pass for different bytes)
38
+ * or as a blob. Each storage call counts against the budget.
39
+ */
40
+ async function lookupBase(sources: PushedObjectSources, sha: string): Promise<GitObject | null> {
41
+ sources.budget.spend();
42
+ const entries = await sources.backend.readTree(sources.repo, sha);
43
+ if (entries !== null) {
44
+ const data = encodeTree(entries.map(({ name, mode, sha: id }) => ({ name, mode, sha: id })));
45
+ return (await hashObject('tree', data)) === sha ? { type: 'tree', sha, data } : null;
46
+ }
47
+ sources.budget.spend();
48
+ const blob = await sources.backend.readBlob(sources.repo, sha);
49
+ if (blob === null || (await hashObject('blob', blob)) !== sha) return null;
50
+ return { type: 'blob', sha, data: blob };
51
+ }
52
+
53
+ /**
54
+ * Reads a whole pushed pack (its checksum verified) into commits, trees and blob sizes, resolving
55
+ * deltas against objects in the pack and thin-pack bases in storage. Throws {@link Uncheckable}
56
+ * when the pack is malformed, needs more than `maxObjectBytes` or more storage reads than the
57
+ * budget allows, or names a base storage does not have.
58
+ */
59
+ export async function readPushedObjects(
60
+ pack: Uint8Array,
61
+ sources: PushedObjectSources,
62
+ ): Promise<PushedObjects> {
63
+ let objects: GitObject[];
64
+ try {
65
+ objects = await readPack(pack, {
66
+ maxObjectBytes: sources.maxObjectBytes,
67
+ lookupBase: async (sha) => lookupBase(sources, sha),
68
+ });
69
+ } catch (error) {
70
+ if (error instanceof Uncheckable) throw error;
71
+ if (isCoreError(error, 'unsupported_request'))
72
+ throw new Uncheckable(`it needs more than ${sources.maxObjectBytes} bytes of objects`);
73
+ if (isCoreError(error)) throw new Uncheckable(`the pushed pack could not be read (${error.message})`);
74
+ throw error;
75
+ }
76
+ const commits = new Map<string, PackCommit>();
77
+ const trees = new Map<string, Uint8Array>();
78
+ const blobSizes = new Map<string, number>();
79
+ for (const object of objects) {
80
+ if (object.type === 'tree') trees.set(object.sha, object.data);
81
+ else if (object.type === 'blob') blobSizes.set(object.sha, object.data.length);
82
+ else {
83
+ const header = commitHeaderLines(new TextDecoder().decode(object.data));
84
+ commits.set(object.sha, { tree: (header[0] ?? '').slice(5), parents: readCommitParents(header) });
85
+ }
86
+ }
87
+ return { commits, trees, blobSizes };
88
+ }
@@ -0,0 +1,99 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import type { PushPolicy } from '@leed-ai/git-gateway-contract/proxy/gateway-hooks';
3
+ import type { Principal } from '@leed-ai/git-gateway-contract/proxy/principal';
4
+ import type { PushedCommit } from '@leed-ai/git-gateway-contract/proxy/push-policy';
5
+ import { PushPolicyNeeds, PushPolicyResult } from '@leed-ai/git-gateway-contract/proxy/push-policy-result';
6
+ import type { RefUpdate } from '@leed-ai/git-gateway-contract/schemas/ref-update';
7
+ import type { SmartHttpConfig } from '@leed-ai/git-gateway-contract/proxy/smart-http-config';
8
+ import type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';
9
+
10
+ import { reasonLine } from '../policy/branch-policy.ts';
11
+ import type { RefRefusal } from '../proxy/git-responses.ts';
12
+ import { ChangedPaths } from './changed-paths.ts';
13
+ import { readPushedObjects } from './pushed-objects.ts';
14
+ import { ReadBudget, Uncheckable } from './uncheckable.ts';
15
+
16
+ /** What running the push policy needs from the gateway. */
17
+ export interface PushPolicyDeps {
18
+ readonly pushPolicy: PushPolicy;
19
+ readonly backend: StorageBackend;
20
+ readonly config: SmartHttpConfig;
21
+ }
22
+
23
+ /** Who pushes what, where. */
24
+ export interface PushPolicyPush {
25
+ readonly principal: Principal;
26
+ readonly repo: string;
27
+ readonly updates: readonly RefUpdate[];
28
+ }
29
+
30
+ /** Asks the policy whether it needs changed paths for a push. */
31
+ export async function pushPolicyNeeds(deps: PushPolicyDeps, push: PushPolicyPush): Promise<PushPolicyNeeds> {
32
+ return PushPolicyNeeds.parse(await deps.pushPolicy.needs(push.principal, push.repo, push.updates));
33
+ }
34
+
35
+ /**
36
+ * Runs the policy's check over a push with, per update, its commits (null when changed paths
37
+ * were not asked for) and returns the refused updates: denied ones with the policy's reason, and
38
+ * any the result does not name.
39
+ */
40
+ export async function judgeWithPushPolicy(
41
+ deps: PushPolicyDeps,
42
+ push: PushPolicyPush,
43
+ commits: readonly (PushedCommit[] | null)[],
44
+ ): Promise<RefRefusal[]> {
45
+ const result = PushPolicyResult.parse(
46
+ await deps.pushPolicy.check({
47
+ principal: push.principal,
48
+ repo: push.repo,
49
+ updates: push.updates.map((update, index) => ({ ...update, commits: commits[index] ?? null })),
50
+ }),
51
+ );
52
+ const decisions = new Map(result.refs.map((decision) => [decision.ref, decision]));
53
+ const refusals: RefRefusal[] = [];
54
+ for (const update of push.updates) {
55
+ const decision = decisions.get(update.ref);
56
+ if (decision === undefined)
57
+ refusals.push({ ref: update.ref, reason: 'the push policy made no decision for this ref' });
58
+ else if (decision.decision === 'deny')
59
+ refusals.push({ ref: update.ref, reason: reasonLine(decision.reason) });
60
+ }
61
+ return refusals;
62
+ }
63
+
64
+ /**
65
+ * Computes every update's commits and changed paths from the whole pushed pack (`pack`, checksum
66
+ * included) and storage, within the configured bounds, and runs the policy's check. A push that
67
+ * cannot be checked within them has every update refused, saying why.
68
+ */
69
+ export async function judgeChangedPaths(
70
+ deps: PushPolicyDeps,
71
+ push: PushPolicyPush,
72
+ pack: Uint8Array,
73
+ ): Promise<RefRefusal[]> {
74
+ const { config, backend } = deps;
75
+ let commits: PushedCommit[][];
76
+ try {
77
+ const budget = new ReadBudget(config.maxPushPolicyReads);
78
+ const objects = await readPushedObjects(pack, {
79
+ repo: push.repo,
80
+ backend,
81
+ budget,
82
+ maxObjectBytes: config.maxPushPolicyObjectBytes,
83
+ });
84
+ const paths = new ChangedPaths({
85
+ repo: push.repo,
86
+ backend,
87
+ objects,
88
+ budget,
89
+ maxChangedPaths: config.maxChangedPaths,
90
+ });
91
+ commits = [];
92
+ for (const update of push.updates) commits.push(await paths.commitsOf(update));
93
+ } catch (error) {
94
+ if (!(error instanceof Uncheckable)) throw error;
95
+ const reason = reasonLine(`the push policy could not check this push: ${error.message}`);
96
+ return push.updates.map((update) => ({ ref: update.ref, reason }));
97
+ }
98
+ return judgeWithPushPolicy(deps, push, commits);
99
+ }
@@ -0,0 +1,107 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import type { ChangedPath } from '@leed-ai/git-gateway-contract/proxy/push-policy';
3
+ import type { GitTreeEntry } from '@leed-ai/git-gateway-core/schemas/git-object';
4
+
5
+ import { Uncheckable } from './uncheckable.ts';
6
+
7
+ /** A changed path before its size is known. */
8
+ export type TreeChange = Omit<ChangedPath, 'size'>;
9
+
10
+ /** Where tree entries come from; throws {@link Uncheckable} for a tree it cannot find. */
11
+ export type TreeLookup = (sha: string) => Promise<readonly GitTreeEntry[]>;
12
+
13
+ /** Longest path a ChangedPath may carry. */
14
+ const MAX_PATH = 4096;
15
+ const TREE_MODE = '040000';
16
+
17
+ /** Collects changes, refusing more than `limit` of them across every diff it is used for. */
18
+ export class ChangeSink {
19
+ readonly #limit: number;
20
+ #count = 0;
21
+
22
+ constructor(limit: number) {
23
+ this.#limit = limit;
24
+ }
25
+
26
+ /** Counts one more change; throws {@link Uncheckable} past the limit. */
27
+ take(change: TreeChange, into: TreeChange[]): void {
28
+ this.#count += 1;
29
+ if (this.#count > this.#limit) throw new Uncheckable(`it changes more than ${this.#limit} paths`);
30
+ if (change.path.length > MAX_PATH) throw new Uncheckable(`a path is longer than ${MAX_PATH} characters`);
31
+ into.push(change);
32
+ }
33
+ }
34
+
35
+ const added = (path: string, entry: GitTreeEntry): TreeChange => ({
36
+ path,
37
+ change: 'added',
38
+ oldMode: null,
39
+ newMode: entry.mode,
40
+ oldSha: null,
41
+ newSha: entry.sha,
42
+ });
43
+
44
+ const deleted = (path: string, entry: GitTreeEntry): TreeChange => ({
45
+ path,
46
+ change: 'deleted',
47
+ oldMode: entry.mode,
48
+ newMode: null,
49
+ oldSha: entry.sha,
50
+ newSha: null,
51
+ });
52
+
53
+ /**
54
+ * The files that differ between two trees (null for the empty tree), recursing only into subtrees
55
+ * whose ids differ, so the cost is proportional to what changed. A directory added or removed
56
+ * yields each file in it; an entry that turns from a file into a directory (or back) is the file
57
+ * deleted (or added) plus the directory's files. A mode change alone is "modified".
58
+ */
59
+ export async function diffTrees(
60
+ lookup: TreeLookup,
61
+ oldTree: string | null,
62
+ newTree: string | null,
63
+ sink: ChangeSink,
64
+ prefix = '',
65
+ ): Promise<TreeChange[]> {
66
+ const changes: TreeChange[] = [];
67
+ if (oldTree === newTree) return changes;
68
+ const before = new Map((oldTree === null ? [] : await lookup(oldTree)).map((entry) => [entry.name, entry]));
69
+ const after = new Map((newTree === null ? [] : await lookup(newTree)).map((entry) => [entry.name, entry]));
70
+ const names = [...new Set([...before.keys(), ...after.keys()])].toSorted();
71
+ for (const name of names) {
72
+ const old = before.get(name);
73
+ const current = after.get(name);
74
+ if (old?.sha === current?.sha && old?.mode === current?.mode) continue;
75
+ const path = `${prefix}${name}`;
76
+ const oldIsTree = old?.mode === TREE_MODE;
77
+ const newIsTree = current?.mode === TREE_MODE;
78
+ if (oldIsTree || newIsTree) {
79
+ const nested = await diffTrees(
80
+ lookup,
81
+ oldIsTree ? (old?.sha ?? null) : null,
82
+ newIsTree ? (current?.sha ?? null) : null,
83
+ sink,
84
+ `${path}/`,
85
+ );
86
+ changes.push(...nested);
87
+ if (old !== undefined && !oldIsTree) sink.take(deleted(path, old), changes);
88
+ if (current !== undefined && !newIsTree) sink.take(added(path, current), changes);
89
+ continue;
90
+ }
91
+ if (old !== undefined && current !== undefined) {
92
+ sink.take(
93
+ {
94
+ path,
95
+ change: 'modified',
96
+ oldMode: old.mode,
97
+ newMode: current.mode,
98
+ oldSha: old.sha,
99
+ newSha: current.sha,
100
+ },
101
+ changes,
102
+ );
103
+ } else if (old !== undefined) sink.take(deleted(path, old), changes);
104
+ else if (current !== undefined) sink.take(added(path, current), changes);
105
+ }
106
+ return changes;
107
+ }
@@ -0,0 +1,27 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ /** Thrown when a push cannot be checked within the configured bounds; the push is refused. */
4
+ export class Uncheckable extends Error {
5
+ override readonly name = 'Uncheckable';
6
+ }
7
+
8
+ /** Counts storage calls against a limit (SmartHttpConfig.maxPushPolicyReads). */
9
+ export class ReadBudget {
10
+ readonly #limit: number;
11
+ #used = 0;
12
+
13
+ constructor(limit: number) {
14
+ this.#limit = limit;
15
+ }
16
+
17
+ /** How many calls have been made. */
18
+ get used(): number {
19
+ return this.#used;
20
+ }
21
+
22
+ /** Records one storage call; throws {@link Uncheckable} past the limit. */
23
+ spend(): void {
24
+ this.#used += 1;
25
+ if (this.#used > this.#limit) throw new Uncheckable(`it needs more than ${this.#limit} storage reads`);
26
+ }
27
+ }