@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,41 @@
|
|
|
1
|
+
import { RequestId } from "@leed-ai/git-gateway-contract/proxy/audit-event";
|
|
2
|
+
// src/proxy/request-log.ts
|
|
3
|
+
/** The id of a request: its Cloudflare Ray id when it has a valid one, otherwise a random UUID. */
|
|
4
|
+
function requestIdOf(request) {
|
|
5
|
+
const ray = RequestId.safeParse(request.headers.get("CF-Ray"));
|
|
6
|
+
return ray.success ? ray.data : crypto.randomUUID();
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The first middleware: gives the request an id (returned as X-Request-Id) and writes one
|
|
10
|
+
* structured log entry when it is answered — method, path, status, duration, repository,
|
|
11
|
+
* endpoint and principal subject. Never the query string beyond `service`, never a header value,
|
|
12
|
+
* never a credential.
|
|
13
|
+
*/
|
|
14
|
+
function requestLog(logger, now) {
|
|
15
|
+
return async (context, next) => {
|
|
16
|
+
const requestId = requestIdOf(context.req.raw);
|
|
17
|
+
context.set("requestId", requestId);
|
|
18
|
+
const started = now().getTime();
|
|
19
|
+
await next();
|
|
20
|
+
context.res.headers.set("X-Request-Id", requestId);
|
|
21
|
+
const url = new URL(context.req.url);
|
|
22
|
+
const git = context.get("git");
|
|
23
|
+
const principal = context.get("principal");
|
|
24
|
+
logger.log({
|
|
25
|
+
level: context.res.status >= 500 ? "error" : "info",
|
|
26
|
+
message: "request",
|
|
27
|
+
requestId,
|
|
28
|
+
method: context.req.method,
|
|
29
|
+
path: url.pathname,
|
|
30
|
+
service: url.searchParams.get("service"),
|
|
31
|
+
status: context.res.status,
|
|
32
|
+
durationMs: now().getTime() - started,
|
|
33
|
+
repo: git?.repo ?? null,
|
|
34
|
+
endpoint: git?.endpoint ?? null,
|
|
35
|
+
principal: principal?.subject ?? null
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export { requestIdOf, requestLog };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=request-log.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-log.js","names":[],"sources":["../../src/proxy/request-log.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport { RequestId } from '@leed-ai/git-gateway-contract/proxy/audit-event';\nimport type { MiddlewareHandler } from 'hono';\n\nimport type { Logger } from '../logging/logger.ts';\nimport type { GatewayEnv } from './gateway-env.ts';\n\n/** The id of a request: its Cloudflare Ray id when it has a valid one, otherwise a random UUID. */\nexport function requestIdOf(request: Request): string {\n const ray = RequestId.safeParse(request.headers.get('CF-Ray'));\n return ray.success ? ray.data : crypto.randomUUID();\n}\n\n/**\n * The first middleware: gives the request an id (returned as X-Request-Id) and writes one\n * structured log entry when it is answered — method, path, status, duration, repository,\n * endpoint and principal subject. Never the query string beyond `service`, never a header value,\n * never a credential.\n */\nexport function requestLog(logger: Logger, now: () => Date): MiddlewareHandler<GatewayEnv> {\n return async (context, next) => {\n const requestId = requestIdOf(context.req.raw);\n context.set('requestId', requestId);\n const started = now().getTime();\n await next();\n context.res.headers.set('X-Request-Id', requestId);\n const url = new URL(context.req.url);\n const git = context.get('git') as GatewayEnv['Variables']['git'] | undefined;\n const principal = context.get('principal') as GatewayEnv['Variables']['principal'] | undefined;\n logger.log({\n level: context.res.status >= 500 ? 'error' : 'info',\n message: 'request',\n requestId,\n method: context.req.method,\n path: url.pathname,\n service: url.searchParams.get('service'),\n status: context.res.status,\n durationMs: now().getTime() - started,\n repo: git?.repo ?? null,\n endpoint: git?.endpoint ?? null,\n principal: principal?.subject ?? null,\n });\n };\n}\n"],"mappings":";;;AAQA,SAAgB,YAAY,SAA0B;CACpD,MAAM,MAAM,UAAU,UAAU,QAAQ,QAAQ,IAAI,QAAQ,CAAC;CAC7D,OAAO,IAAI,UAAU,IAAI,OAAO,OAAO,WAAW;AACpD;;;;;;;AAQA,SAAgB,WAAW,QAAgB,KAAgD;CACzF,OAAO,OAAO,SAAS,SAAS;EAC9B,MAAM,YAAY,YAAY,QAAQ,IAAI,GAAG;EAC7C,QAAQ,IAAI,aAAa,SAAS;EAClC,MAAM,UAAU,IAAI,CAAC,CAAC,QAAQ;EAC9B,MAAM,KAAK;EACX,QAAQ,IAAI,QAAQ,IAAI,gBAAgB,SAAS;EACjD,MAAM,MAAM,IAAI,IAAI,QAAQ,IAAI,GAAG;EACnC,MAAM,MAAM,QAAQ,IAAI,KAAK;EAC7B,MAAM,YAAY,QAAQ,IAAI,WAAW;EACzC,OAAO,IAAI;GACT,OAAO,QAAQ,IAAI,UAAU,MAAM,UAAU;GAC7C,SAAS;GACT;GACA,QAAQ,QAAQ,IAAI;GACpB,MAAM,IAAI;GACV,SAAS,IAAI,aAAa,IAAI,SAAS;GACvC,QAAQ,QAAQ,IAAI;GACpB,YAAY,IAAI,CAAC,CAAC,QAAQ,IAAI;GAC9B,MAAM,KAAK,QAAQ;GACnB,UAAU,KAAK,YAAY;GAC3B,WAAW,WAAW,WAAW;EACnC,CAAC;CACH;AACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { n as GatewayEnv } from "../gateway-env-DnsBU4Pq.js";
|
|
2
|
+
import { MiddlewareHandler } from "hono";
|
|
3
|
+
/**
|
|
4
|
+
* Route middleware for `/<repo>.git/…`: resolves the repository and endpoint and checks the
|
|
5
|
+
* method, before any authentication. Dumb HTTP (info/refs without a smart service) is refused
|
|
6
|
+
* with 403, as git-http-backend does; unknown paths and invalid repository names are 404.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolveRoute(): MiddlewareHandler<GatewayEnv>;
|
|
9
|
+
//# sourceMappingURL=resolve-route.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { textResponse } from "./git-responses.js";
|
|
2
|
+
import { resolveGitRequest } from "./git-route.js";
|
|
3
|
+
// src/proxy/resolve-route.ts
|
|
4
|
+
/**
|
|
5
|
+
* Route middleware for `/<repo>.git/…`: resolves the repository and endpoint and checks the
|
|
6
|
+
* method, before any authentication. Dumb HTTP (info/refs without a smart service) is refused
|
|
7
|
+
* with 403, as git-http-backend does; unknown paths and invalid repository names are 404.
|
|
8
|
+
*/
|
|
9
|
+
function resolveRoute() {
|
|
10
|
+
return async (context, next) => {
|
|
11
|
+
const resolved = resolveGitRequest(new URL(context.req.url));
|
|
12
|
+
if (resolved === "not-found") return textResponse(404, "Not found");
|
|
13
|
+
if (resolved === "dumb-http") return textResponse(403, "Dumb HTTP is not supported; use a smart git client");
|
|
14
|
+
const expected = resolved.endpoint === "info-refs" ? "GET" : "POST";
|
|
15
|
+
if (context.req.method !== expected) return textResponse(405, "Method not allowed", { Allow: expected });
|
|
16
|
+
context.set("git", resolved);
|
|
17
|
+
return next();
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export { resolveRoute };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=resolve-route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-route.js","names":[],"sources":["../../src/proxy/resolve-route.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport type { MiddlewareHandler } from 'hono';\n\nimport type { GatewayEnv } from './gateway-env.ts';\nimport { textResponse } from './git-responses.ts';\nimport { resolveGitRequest } from './git-route.ts';\n\n/**\n * Route middleware for `/<repo>.git/…`: resolves the repository and endpoint and checks the\n * method, before any authentication. Dumb HTTP (info/refs without a smart service) is refused\n * with 403, as git-http-backend does; unknown paths and invalid repository names are 404.\n */\nexport function resolveRoute(): MiddlewareHandler<GatewayEnv> {\n return async (context, next) => {\n const resolved = resolveGitRequest(new URL(context.req.url));\n if (resolved === 'not-found') return textResponse(404, 'Not found');\n if (resolved === 'dumb-http')\n return textResponse(403, 'Dumb HTTP is not supported; use a smart git client');\n const expected = resolved.endpoint === 'info-refs' ? 'GET' : 'POST';\n if (context.req.method !== expected) return textResponse(405, 'Method not allowed', { Allow: expected });\n context.set('git', resolved);\n return next();\n };\n}\n"],"mappings":";;;;;;;;AAYA,SAAgB,eAA8C;CAC5D,OAAO,OAAO,SAAS,SAAS;EAC9B,MAAM,WAAW,kBAAkB,IAAI,IAAI,QAAQ,IAAI,GAAG,CAAC;EAC3D,IAAI,aAAa,aAAa,OAAO,aAAa,KAAK,WAAW;EAClE,IAAI,aAAa,aACf,OAAO,aAAa,KAAK,oDAAoD;EAC/E,MAAM,WAAW,SAAS,aAAa,cAAc,QAAQ;EAC7D,IAAI,QAAQ,IAAI,WAAW,UAAU,OAAO,aAAa,KAAK,sBAAsB,EAAE,OAAO,SAAS,CAAC;EACvG,QAAQ,IAAI,OAAO,QAAQ;EAC3B,OAAO,KAAK;CACd;AACF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GatewayDeps } from "./gateway-deps.js";
|
|
2
|
+
import { n as GatewayEnv } from "../gateway-env-DnsBU4Pq.js";
|
|
3
|
+
import { Context } from "hono";
|
|
4
|
+
/** The storage host's answer, or the response to give the client instead. */
|
|
5
|
+
export type UpstreamResult = {
|
|
6
|
+
readonly ok: true;
|
|
7
|
+
readonly response: Response;
|
|
8
|
+
} | {
|
|
9
|
+
readonly ok: false;
|
|
10
|
+
readonly response: Response;
|
|
11
|
+
readonly reason: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Mints a git remote for this request from the backend (a fresh short-lived credential each time)
|
|
15
|
+
* and sends the request to the storage host. A 2xx answer comes back as is for the caller to
|
|
16
|
+
* stream; anything else becomes a plain-text error for the client: 404 for a missing repository,
|
|
17
|
+
* 502 when the host refused the gateway's own credential or failed. The host's headers and the
|
|
18
|
+
* credential never reach the client. `body` errors (a refused push) surface as a thrown error.
|
|
19
|
+
*/
|
|
20
|
+
export declare function callUpstream(deps: GatewayDeps, context: Context<GatewayEnv>, body: ReadableStream<Uint8Array> | null): Promise<UpstreamResult>;
|
|
21
|
+
/** The client response for a 2xx storage host response, streamed through untouched. */
|
|
22
|
+
export declare function streamToClient(upstream: Response, body: ReadableStream<Uint8Array> | null, requestId: string): Response;
|
|
23
|
+
//# sourceMappingURL=upstream-call.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { textResponse } from "./git-responses.js";
|
|
2
|
+
import { clientHeaders, upstreamRequest } from "./upstream.js";
|
|
3
|
+
import { readGatewayError } from "@leed-ai/git-gateway-contract/gateway-error";
|
|
4
|
+
// src/proxy/upstream-call.ts
|
|
5
|
+
function failed(requestId, status, reason) {
|
|
6
|
+
return {
|
|
7
|
+
ok: false,
|
|
8
|
+
reason,
|
|
9
|
+
response: textResponse(status, reason, { "X-Request-Id": requestId })
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Mints a git remote for this request from the backend (a fresh short-lived credential each time)
|
|
14
|
+
* and sends the request to the storage host. A 2xx answer comes back as is for the caller to
|
|
15
|
+
* stream; anything else becomes a plain-text error for the client: 404 for a missing repository,
|
|
16
|
+
* 502 when the host refused the gateway's own credential or failed. The host's headers and the
|
|
17
|
+
* credential never reach the client. `body` errors (a refused push) surface as a thrown error.
|
|
18
|
+
*/
|
|
19
|
+
async function callUpstream(deps, context, body) {
|
|
20
|
+
const requestId = context.get("requestId");
|
|
21
|
+
const git = context.get("git");
|
|
22
|
+
let remote;
|
|
23
|
+
try {
|
|
24
|
+
remote = await deps.backend.gitRemote(git.repo, git.operation);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
const gatewayError = readGatewayError(error);
|
|
27
|
+
if (gatewayError?.code === "repo_not_found") return failed(requestId, 404, `Repository not found: ${git.repo}`);
|
|
28
|
+
deps.logger.log({
|
|
29
|
+
level: "error",
|
|
30
|
+
message: "minting the storage remote failed",
|
|
31
|
+
requestId,
|
|
32
|
+
repo: git.repo,
|
|
33
|
+
error: gatewayError?.code ?? (error instanceof Error ? error.name : "unknown")
|
|
34
|
+
});
|
|
35
|
+
return failed(requestId, 502, "The storage backend is unavailable; try again later");
|
|
36
|
+
}
|
|
37
|
+
const upstream = await deps.fetch(upstreamRequest(context.req.raw, git, remote, body));
|
|
38
|
+
if (upstream.status >= 200 && upstream.status < 300) return {
|
|
39
|
+
ok: true,
|
|
40
|
+
response: upstream
|
|
41
|
+
};
|
|
42
|
+
await upstream.body?.cancel().catch(() => void 0);
|
|
43
|
+
deps.logger.log({
|
|
44
|
+
level: upstream.status === 404 ? "info" : "error",
|
|
45
|
+
message: "storage host refused the request",
|
|
46
|
+
requestId,
|
|
47
|
+
repo: git.repo,
|
|
48
|
+
endpoint: git.endpoint,
|
|
49
|
+
upstreamStatus: upstream.status
|
|
50
|
+
});
|
|
51
|
+
if (upstream.status === 404) return failed(requestId, 404, `Repository not found: ${git.repo}`);
|
|
52
|
+
return failed(requestId, 502, "The storage backend failed to answer; try again later");
|
|
53
|
+
}
|
|
54
|
+
/** The client response for a 2xx storage host response, streamed through untouched. */
|
|
55
|
+
function streamToClient(upstream, body, requestId) {
|
|
56
|
+
return new Response(body, {
|
|
57
|
+
status: upstream.status,
|
|
58
|
+
headers: clientHeaders(upstream, requestId)
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
export { callUpstream, streamToClient };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=upstream-call.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upstream-call.js","names":[],"sources":["../../src/proxy/upstream-call.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport { readGatewayError } from '@leed-ai/git-gateway-contract/gateway-error';\nimport type { Context } from 'hono';\n\nimport type { GatewayDeps } from './gateway-deps.ts';\nimport type { GatewayEnv } from './gateway-env.ts';\nimport { textResponse } from './git-responses.ts';\nimport { clientHeaders, upstreamRequest } from './upstream.ts';\n\n/** The storage host's answer, or the response to give the client instead. */\nexport type UpstreamResult =\n | { readonly ok: true; readonly response: Response }\n | { readonly ok: false; readonly response: Response; readonly reason: string };\n\nfunction failed(requestId: string, status: number, reason: string): UpstreamResult {\n return { ok: false, reason, response: textResponse(status, reason, { 'X-Request-Id': requestId }) };\n}\n\n/**\n * Mints a git remote for this request from the backend (a fresh short-lived credential each time)\n * and sends the request to the storage host. A 2xx answer comes back as is for the caller to\n * stream; anything else becomes a plain-text error for the client: 404 for a missing repository,\n * 502 when the host refused the gateway's own credential or failed. The host's headers and the\n * credential never reach the client. `body` errors (a refused push) surface as a thrown error.\n */\nexport async function callUpstream(\n deps: GatewayDeps,\n context: Context<GatewayEnv>,\n body: ReadableStream<Uint8Array> | null,\n): Promise<UpstreamResult> {\n const requestId = context.get('requestId');\n const git = context.get('git');\n let remote;\n try {\n remote = await deps.backend.gitRemote(git.repo, git.operation);\n } catch (error) {\n const gatewayError = readGatewayError(error);\n if (gatewayError?.code === 'repo_not_found')\n return failed(requestId, 404, `Repository not found: ${git.repo}`);\n deps.logger.log({\n level: 'error',\n message: 'minting the storage remote failed',\n requestId,\n repo: git.repo,\n error: gatewayError?.code ?? (error instanceof Error ? error.name : 'unknown'),\n });\n return failed(requestId, 502, 'The storage backend is unavailable; try again later');\n }\n const upstream = await deps.fetch(upstreamRequest(context.req.raw, git, remote, body));\n if (upstream.status >= 200 && upstream.status < 300) return { ok: true, response: upstream };\n await upstream.body?.cancel().catch(() => undefined);\n deps.logger.log({\n level: upstream.status === 404 ? 'info' : 'error',\n message: 'storage host refused the request',\n requestId,\n repo: git.repo,\n endpoint: git.endpoint,\n upstreamStatus: upstream.status,\n });\n if (upstream.status === 404) return failed(requestId, 404, `Repository not found: ${git.repo}`);\n return failed(requestId, 502, 'The storage backend failed to answer; try again later');\n}\n\n/** The client response for a 2xx storage host response, streamed through untouched. */\nexport function streamToClient(\n upstream: Response,\n body: ReadableStream<Uint8Array> | null,\n requestId: string,\n): Response {\n return new Response(body, { status: upstream.status, headers: clientHeaders(upstream, requestId) });\n}\n"],"mappings":";;;;AAcA,SAAS,OAAO,WAAmB,QAAgB,QAAgC;CACjF,OAAO;EAAE,IAAI;EAAO;EAAQ,UAAU,aAAa,QAAQ,QAAQ,EAAE,gBAAgB,UAAU,CAAC;CAAE;AACpG;;;;;;;;AASA,eAAsB,aACpB,MACA,SACA,MACyB;CACzB,MAAM,YAAY,QAAQ,IAAI,WAAW;CACzC,MAAM,MAAM,QAAQ,IAAI,KAAK;CAC7B,IAAI;CACJ,IAAI;EACF,SAAS,MAAM,KAAK,QAAQ,UAAU,IAAI,MAAM,IAAI,SAAS;CAC/D,SAAS,OAAO;EACd,MAAM,eAAe,iBAAiB,KAAK;EAC3C,IAAI,cAAc,SAAS,kBACzB,OAAO,OAAO,WAAW,KAAK,yBAAyB,IAAI,MAAM;EACnE,KAAK,OAAO,IAAI;GACd,OAAO;GACP,SAAS;GACT;GACA,MAAM,IAAI;GACV,OAAO,cAAc,SAAS,iBAAiB,QAAQ,MAAM,OAAO;EACtE,CAAC;EACD,OAAO,OAAO,WAAW,KAAK,qDAAqD;CACrF;CACA,MAAM,WAAW,MAAM,KAAK,MAAM,gBAAgB,QAAQ,IAAI,KAAK,KAAK,QAAQ,IAAI,CAAC;CACrF,IAAI,SAAS,UAAU,OAAO,SAAS,SAAS,KAAK,OAAO;EAAE,IAAI;EAAM,UAAU;CAAS;CAC3F,MAAM,SAAS,MAAM,OAAO,CAAC,CAAC,YAAY,KAAA,CAAS;CACnD,KAAK,OAAO,IAAI;EACd,OAAO,SAAS,WAAW,MAAM,SAAS;EAC1C,SAAS;EACT;EACA,MAAM,IAAI;EACV,UAAU,IAAI;EACd,gBAAgB,SAAS;CAC3B,CAAC;CACD,IAAI,SAAS,WAAW,KAAK,OAAO,OAAO,WAAW,KAAK,yBAAyB,IAAI,MAAM;CAC9F,OAAO,OAAO,WAAW,KAAK,uDAAuD;AACvF;;AAGA,SAAgB,eACd,UACA,MACA,WACU;CACV,OAAO,IAAI,SAAS,MAAM;EAAE,QAAQ,SAAS;EAAQ,SAAS,cAAc,UAAU,SAAS;CAAE,CAAC;AACpG"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { NO_CACHE } from "./git-responses.js";
|
|
2
|
+
// src/proxy/upstream.ts
|
|
3
|
+
/** Response headers passed back from the storage host; everything else (cookies, auth challenges) is dropped. */
|
|
4
|
+
const RESPONSE_HEADERS = [
|
|
5
|
+
"content-type",
|
|
6
|
+
"cache-control",
|
|
7
|
+
"expires",
|
|
8
|
+
"pragma"
|
|
9
|
+
];
|
|
10
|
+
/**
|
|
11
|
+
* The protocol version header to send upstream. Fetches pass it through untouched, so protocol v2
|
|
12
|
+
* reaches a host that speaks it; pushes use protocol v1 receive-pack (which every client and host
|
|
13
|
+
* supports), so a "version=2" request is dropped and the host answers in v0/v1.
|
|
14
|
+
*/
|
|
15
|
+
function gitProtocol(request, git) {
|
|
16
|
+
const value = request.headers.get("Git-Protocol");
|
|
17
|
+
if (value === null) return null;
|
|
18
|
+
if (git.service === "git-upload-pack") return value;
|
|
19
|
+
const kept = value.split(":").filter((part) => part.trim() !== "" && !/^version=2$/.test(part.trim())).join(":");
|
|
20
|
+
return kept === "" ? null : kept;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The request to send to the storage host for a client request: the host's URL for the same
|
|
24
|
+
* endpoint, the credential headers minted for this request, and only the client headers the git
|
|
25
|
+
* protocol needs (Content-Type, Accept, Git-Protocol, User-Agent, and Content-Encoding for a
|
|
26
|
+
* fetch). The client's Authorization, cookies and anything else never leave the gateway.
|
|
27
|
+
*/
|
|
28
|
+
function upstreamRequest(request, git, remote, body) {
|
|
29
|
+
const headers = new Headers();
|
|
30
|
+
for (const name of [
|
|
31
|
+
"Content-Type",
|
|
32
|
+
"Accept",
|
|
33
|
+
"User-Agent"
|
|
34
|
+
]) {
|
|
35
|
+
const value = request.headers.get(name);
|
|
36
|
+
if (value !== null) headers.set(name, value);
|
|
37
|
+
}
|
|
38
|
+
const protocol = gitProtocol(request, git);
|
|
39
|
+
if (protocol !== null) headers.set("Git-Protocol", protocol);
|
|
40
|
+
const encoding = request.headers.get("Content-Encoding");
|
|
41
|
+
if (encoding !== null && git.service === "git-upload-pack" && body !== null) headers.set("Content-Encoding", encoding);
|
|
42
|
+
for (const [name, value] of Object.entries(remote.headers)) headers.set(name, value);
|
|
43
|
+
const base = remote.url.replace(/\/$/, "");
|
|
44
|
+
const url = git.endpoint === "info-refs" ? `${base}/info/refs?service=${git.service}` : `${base}/${git.service}`;
|
|
45
|
+
const init = {
|
|
46
|
+
method: git.endpoint === "info-refs" ? "GET" : "POST",
|
|
47
|
+
headers,
|
|
48
|
+
redirect: "manual"
|
|
49
|
+
};
|
|
50
|
+
if (body !== null) {
|
|
51
|
+
init.body = body;
|
|
52
|
+
init.duplex = "half";
|
|
53
|
+
}
|
|
54
|
+
return new Request(url, init);
|
|
55
|
+
}
|
|
56
|
+
/** The response headers to give the client for a storage host response. */
|
|
57
|
+
function clientHeaders(upstream, requestId) {
|
|
58
|
+
const headers = new Headers(NO_CACHE);
|
|
59
|
+
for (const name of RESPONSE_HEADERS) {
|
|
60
|
+
const value = upstream.headers.get(name);
|
|
61
|
+
if (value !== null) headers.set(name, value);
|
|
62
|
+
}
|
|
63
|
+
headers.set("X-Request-Id", requestId);
|
|
64
|
+
return headers;
|
|
65
|
+
}
|
|
66
|
+
export { clientHeaders, upstreamRequest };
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=upstream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upstream.js","names":[],"sources":["../../src/proxy/upstream.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport type { GitRemote } from '@leed-ai/git-gateway-contract/storage/git-remote';\n\nimport { NO_CACHE } from './git-responses.ts';\nimport type { GitRequest } from './git-route.ts';\n\n/** Sends one HTTP request: `fetch`, or a stand-in such as an in-memory backend's handler. */\nexport type UpstreamFetch = (request: Request) => Promise<Response>;\n\n/** Response headers passed back from the storage host; everything else (cookies, auth challenges) is dropped. */\nconst RESPONSE_HEADERS = ['content-type', 'cache-control', 'expires', 'pragma'] as const;\n\n/**\n * The protocol version header to send upstream. Fetches pass it through untouched, so protocol v2\n * reaches a host that speaks it; pushes use protocol v1 receive-pack (which every client and host\n * supports), so a \"version=2\" request is dropped and the host answers in v0/v1.\n */\nfunction gitProtocol(request: Request, git: GitRequest): string | null {\n const value = request.headers.get('Git-Protocol');\n if (value === null) return null;\n if (git.service === 'git-upload-pack') return value;\n const kept = value\n .split(':')\n .filter((part) => part.trim() !== '' && !/^version=2$/.test(part.trim()))\n .join(':');\n return kept === '' ? null : kept;\n}\n\n/**\n * The request to send to the storage host for a client request: the host's URL for the same\n * endpoint, the credential headers minted for this request, and only the client headers the git\n * protocol needs (Content-Type, Accept, Git-Protocol, User-Agent, and Content-Encoding for a\n * fetch). The client's Authorization, cookies and anything else never leave the gateway.\n */\nexport function upstreamRequest(\n request: Request,\n git: GitRequest,\n remote: GitRemote,\n body: ReadableStream<Uint8Array> | null,\n): Request {\n const headers = new Headers();\n for (const name of ['Content-Type', 'Accept', 'User-Agent']) {\n const value = request.headers.get(name);\n if (value !== null) headers.set(name, value);\n }\n const protocol = gitProtocol(request, git);\n if (protocol !== null) headers.set('Git-Protocol', protocol);\n const encoding = request.headers.get('Content-Encoding');\n if (encoding !== null && git.service === 'git-upload-pack' && body !== null)\n headers.set('Content-Encoding', encoding);\n for (const [name, value] of Object.entries(remote.headers)) headers.set(name, value);\n const base = remote.url.replace(/\\/$/, '');\n const url =\n git.endpoint === 'info-refs' ? `${base}/info/refs?service=${git.service}` : `${base}/${git.service}`;\n const init: RequestInit & { duplex?: 'half' } = {\n method: git.endpoint === 'info-refs' ? 'GET' : 'POST',\n headers,\n redirect: 'manual',\n };\n if (body !== null) {\n init.body = body;\n init.duplex = 'half';\n }\n return new Request(url, init);\n}\n\n/** The response headers to give the client for a storage host response. */\nexport function clientHeaders(upstream: Response, requestId: string): Headers {\n const headers = new Headers(NO_CACHE);\n for (const name of RESPONSE_HEADERS) {\n const value = upstream.headers.get(name);\n if (value !== null) headers.set(name, value);\n }\n headers.set('X-Request-Id', requestId);\n return headers;\n}\n"],"mappings":";;;AAUA,MAAM,mBAAmB;CAAC;CAAgB;CAAiB;CAAW;AAAQ;;;;;;AAO9E,SAAS,YAAY,SAAkB,KAAgC;CACrE,MAAM,QAAQ,QAAQ,QAAQ,IAAI,cAAc;CAChD,IAAI,UAAU,MAAM,OAAO;CAC3B,IAAI,IAAI,YAAY,mBAAmB,OAAO;CAC9C,MAAM,OAAO,MACV,MAAM,GAAG,CAAC,CACV,QAAQ,SAAS,KAAK,KAAK,MAAM,MAAM,CAAC,cAAc,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,CACxE,KAAK,GAAG;CACX,OAAO,SAAS,KAAK,OAAO;AAC9B;;;;;;;AAQA,SAAgB,gBACd,SACA,KACA,QACA,MACS;CACT,MAAM,UAAU,IAAI,QAAQ;CAC5B,KAAK,MAAM,QAAQ;EAAC;EAAgB;EAAU;CAAY,GAAG;EAC3D,MAAM,QAAQ,QAAQ,QAAQ,IAAI,IAAI;EACtC,IAAI,UAAU,MAAM,QAAQ,IAAI,MAAM,KAAK;CAC7C;CACA,MAAM,WAAW,YAAY,SAAS,GAAG;CACzC,IAAI,aAAa,MAAM,QAAQ,IAAI,gBAAgB,QAAQ;CAC3D,MAAM,WAAW,QAAQ,QAAQ,IAAI,kBAAkB;CACvD,IAAI,aAAa,QAAQ,IAAI,YAAY,qBAAqB,SAAS,MACrE,QAAQ,IAAI,oBAAoB,QAAQ;CAC1C,KAAK,MAAM,CAAC,MAAM,UAAU,OAAO,QAAQ,OAAO,OAAO,GAAG,QAAQ,IAAI,MAAM,KAAK;CACnF,MAAM,OAAO,OAAO,IAAI,QAAQ,OAAO,EAAE;CACzC,MAAM,MACJ,IAAI,aAAa,cAAc,GAAG,KAAK,qBAAqB,IAAI,YAAY,GAAG,KAAK,GAAG,IAAI;CAC7F,MAAM,OAA0C;EAC9C,QAAQ,IAAI,aAAa,cAAc,QAAQ;EAC/C;EACA,UAAU;CACZ;CACA,IAAI,SAAS,MAAM;EACjB,KAAK,OAAO;EACZ,KAAK,SAAS;CAChB;CACA,OAAO,IAAI,QAAQ,KAAK,IAAI;AAC9B;;AAGA,SAAgB,cAAc,UAAoB,WAA4B;CAC5E,MAAM,UAAU,IAAI,QAAQ,QAAQ;CACpC,KAAK,MAAM,QAAQ,kBAAkB;EACnC,MAAM,QAAQ,SAAS,QAAQ,IAAI,IAAI;EACvC,IAAI,UAAU,MAAM,QAAQ,IAAI,MAAM,KAAK;CAC7C;CACA,QAAQ,IAAI,gBAAgB,SAAS;CACrC,OAAO;AACT"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** A push the gateway accepted, as onPush hooks receive it. */
|
|
3
|
+
declare const PushEvent: z.ZodObject<{
|
|
4
|
+
repo: z.ZodString;
|
|
5
|
+
updates: z.ZodArray<z.ZodObject<{
|
|
6
|
+
ref: z.ZodString;
|
|
7
|
+
oldSha: z.ZodString;
|
|
8
|
+
newSha: z.ZodString;
|
|
9
|
+
}, z.core.$strict>>;
|
|
10
|
+
pushedAt: z.ZodISODateTime;
|
|
11
|
+
}, z.core.$strict>;
|
|
12
|
+
/** A push event, inferred from {@link PushEvent}. */
|
|
13
|
+
type PushEvent = z.infer<typeof PushEvent>;
|
|
14
|
+
export { PushEvent as t };
|
|
15
|
+
//# sourceMappingURL=push-event-f-5TsFUY.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PushedObjects } from "./pushed-objects.js";
|
|
2
|
+
import { ReadBudget } from "./uncheckable.js";
|
|
3
|
+
import { RefUpdate } from "@leed-ai/git-gateway-contract/schemas/ref-update";
|
|
4
|
+
import { StorageBackend } from "@leed-ai/git-gateway-contract/storage/storage-backend";
|
|
5
|
+
import { PushedCommit } from "@leed-ai/git-gateway-contract/proxy/push-policy";
|
|
6
|
+
/** Where changed paths are computed from, and the bounds on computing them. */
|
|
7
|
+
export interface ChangedPathSources {
|
|
8
|
+
readonly repo: string;
|
|
9
|
+
readonly backend: Pick<StorageBackend, 'readCommit' | 'readTree' | 'readBlob'>;
|
|
10
|
+
readonly objects: PushedObjects;
|
|
11
|
+
readonly budget: ReadBudget;
|
|
12
|
+
readonly maxChangedPaths: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Computes, for each update of a push, the commits it brings (those of the pushed pack reachable
|
|
16
|
+
* from its new id) and the paths each changes. Trees and commits come from the pack first and
|
|
17
|
+
* from storage otherwise, each storage call counted against the budget; reads are shared across
|
|
18
|
+
* updates. Throws {@link Uncheckable} when something cannot be read or a bound is passed.
|
|
19
|
+
*/
|
|
20
|
+
export declare class ChangedPaths {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(sources: ChangedPathSources);
|
|
23
|
+
/** The new commits an update brings, newest first, with their changes. */
|
|
24
|
+
commitsOf(update: RefUpdate): Promise<PushedCommit[]>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=changed-paths.d.ts.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ChangeSink, diffTrees } from "./tree-diff.js";
|
|
2
|
+
import { Uncheckable } from "./uncheckable.js";
|
|
3
|
+
import { isCoreError } from "@leed-ai/git-gateway-core/core-error";
|
|
4
|
+
import { ZERO_SHA } from "@leed-ai/git-gateway-contract/schemas/sha";
|
|
5
|
+
import { decodeTree } from "@leed-ai/git-gateway-core/objects/tree-object";
|
|
6
|
+
// src/push-policy/changed-paths.ts
|
|
7
|
+
const SUBMODULE = "160000";
|
|
8
|
+
/**
|
|
9
|
+
* Computes, for each update of a push, the commits it brings (those of the pushed pack reachable
|
|
10
|
+
* from its new id) and the paths each changes. Trees and commits come from the pack first and
|
|
11
|
+
* from storage otherwise, each storage call counted against the budget; reads are shared across
|
|
12
|
+
* updates. Throws {@link Uncheckable} when something cannot be read or a bound is passed.
|
|
13
|
+
*/
|
|
14
|
+
var ChangedPaths = class {
|
|
15
|
+
#sources;
|
|
16
|
+
#sink;
|
|
17
|
+
#trees = /* @__PURE__ */ new Map();
|
|
18
|
+
#commitTrees = /* @__PURE__ */ new Map();
|
|
19
|
+
#sizes = /* @__PURE__ */ new Map();
|
|
20
|
+
constructor(sources) {
|
|
21
|
+
this.#sources = sources;
|
|
22
|
+
this.#sink = new ChangeSink(sources.maxChangedPaths);
|
|
23
|
+
}
|
|
24
|
+
/** The new commits an update brings, newest first, with their changes. */
|
|
25
|
+
async commitsOf(update) {
|
|
26
|
+
if (update.newSha === ZERO_SHA) return [];
|
|
27
|
+
const { commits } = this.#sources.objects;
|
|
28
|
+
const result = [];
|
|
29
|
+
const seen = /* @__PURE__ */ new Set([update.newSha]);
|
|
30
|
+
const queue = [update.newSha];
|
|
31
|
+
for (let sha = queue.shift(); sha !== void 0; sha = queue.shift()) {
|
|
32
|
+
const commit = commits.get(sha);
|
|
33
|
+
if (commit === void 0) continue;
|
|
34
|
+
if (commit.parents === null) throw new Uncheckable(`commit ${sha} has a header git reads differently`);
|
|
35
|
+
result.push({
|
|
36
|
+
sha,
|
|
37
|
+
parents: [...commit.parents],
|
|
38
|
+
changes: await this.#changesOf(commit.tree, commit.parents)
|
|
39
|
+
});
|
|
40
|
+
for (const parent of commit.parents) if (!seen.has(parent)) {
|
|
41
|
+
seen.add(parent);
|
|
42
|
+
queue.push(parent);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A commit's changes: against its parent, the empty tree for a root, and for a merge only the
|
|
49
|
+
* paths that differ from every parent (the first parent's view of each).
|
|
50
|
+
*/
|
|
51
|
+
async #changesOf(tree, parents) {
|
|
52
|
+
const lookup = async (sha) => this.#tree(sha);
|
|
53
|
+
const [first, ...others] = parents;
|
|
54
|
+
let changes = await diffTrees(lookup, first === void 0 ? null : await this.#treeOf(first), tree, this.#sink);
|
|
55
|
+
for (const other of others) {
|
|
56
|
+
const paths = new Set((await diffTrees(lookup, await this.#treeOf(other), tree, this.#sink)).map((c) => c.path));
|
|
57
|
+
changes = changes.filter((change) => paths.has(change.path));
|
|
58
|
+
}
|
|
59
|
+
const sorted = changes.toSorted((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
60
|
+
return Promise.all(sorted.map(async (change) => ({
|
|
61
|
+
...change,
|
|
62
|
+
size: await this.#sizeOf(change)
|
|
63
|
+
})));
|
|
64
|
+
}
|
|
65
|
+
async #sizeOf(change) {
|
|
66
|
+
if (change.newSha === null || change.newMode === SUBMODULE) return null;
|
|
67
|
+
const sha = change.newSha;
|
|
68
|
+
const inPack = this.#sources.objects.blobSizes.get(sha);
|
|
69
|
+
if (inPack !== void 0) return inPack;
|
|
70
|
+
return this.#cached(this.#sizes, sha, async () => {
|
|
71
|
+
this.#sources.budget.spend();
|
|
72
|
+
const blob = await this.#sources.backend.readBlob(this.#sources.repo, sha);
|
|
73
|
+
if (blob === null) throw new Uncheckable(`blob ${sha} is in neither the pushed pack nor storage`);
|
|
74
|
+
return blob.length;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async #treeOf(commitSha) {
|
|
78
|
+
const inPack = this.#sources.objects.commits.get(commitSha);
|
|
79
|
+
if (inPack !== void 0) return inPack.tree;
|
|
80
|
+
return this.#cached(this.#commitTrees, commitSha, async () => {
|
|
81
|
+
this.#sources.budget.spend();
|
|
82
|
+
const commit = await this.#sources.backend.readCommit(this.#sources.repo, commitSha);
|
|
83
|
+
if (commit === null) throw new Uncheckable(`commit ${commitSha} is in neither the pushed pack nor storage`);
|
|
84
|
+
return commit.tree;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async #tree(sha) {
|
|
88
|
+
return this.#cached(this.#trees, sha, async () => {
|
|
89
|
+
const data = this.#sources.objects.trees.get(sha);
|
|
90
|
+
if (data !== void 0) try {
|
|
91
|
+
return decodeTree(data, sha);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (isCoreError(error)) throw new Uncheckable(`tree ${sha} could not be read (${error.message})`);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
this.#sources.budget.spend();
|
|
97
|
+
const entries = await this.#sources.backend.readTree(this.#sources.repo, sha);
|
|
98
|
+
if (entries === null) throw new Uncheckable(`tree ${sha} is in neither the pushed pack nor storage`);
|
|
99
|
+
return entries.map(({ name, mode, sha: id }) => ({
|
|
100
|
+
name,
|
|
101
|
+
mode,
|
|
102
|
+
sha: id
|
|
103
|
+
}));
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async #cached(cache, key, load) {
|
|
107
|
+
let found = cache.get(key);
|
|
108
|
+
if (found === void 0) {
|
|
109
|
+
found = load();
|
|
110
|
+
cache.set(key, found);
|
|
111
|
+
}
|
|
112
|
+
return found;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
export { ChangedPaths };
|
|
116
|
+
|
|
117
|
+
//# sourceMappingURL=changed-paths.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"changed-paths.js","names":[],"sources":["../../src/push-policy/changed-paths.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport type { ChangedPath, PushedCommit } from '@leed-ai/git-gateway-contract/proxy/push-policy';\nimport type { RefUpdate } from '@leed-ai/git-gateway-contract/schemas/ref-update';\nimport { ZERO_SHA } from '@leed-ai/git-gateway-contract/schemas/sha';\nimport type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';\nimport { isCoreError } from '@leed-ai/git-gateway-core/core-error';\nimport { decodeTree } from '@leed-ai/git-gateway-core/objects/tree-object';\nimport type { GitTreeEntry } from '@leed-ai/git-gateway-core/schemas/git-object';\n\nimport type { PushedObjects } from './pushed-objects.ts';\nimport { ChangeSink, diffTrees, type TreeChange } from './tree-diff.ts';\nimport { Uncheckable, type ReadBudget } from './uncheckable.ts';\n\n/** Where changed paths are computed from, and the bounds on computing them. */\nexport interface ChangedPathSources {\n readonly repo: string;\n readonly backend: Pick<StorageBackend, 'readCommit' | 'readTree' | 'readBlob'>;\n readonly objects: PushedObjects;\n readonly budget: ReadBudget;\n readonly maxChangedPaths: number;\n}\n\nconst SUBMODULE = '160000';\n\n/**\n * Computes, for each update of a push, the commits it brings (those of the pushed pack reachable\n * from its new id) and the paths each changes. Trees and commits come from the pack first and\n * from storage otherwise, each storage call counted against the budget; reads are shared across\n * updates. Throws {@link Uncheckable} when something cannot be read or a bound is passed.\n */\nexport class ChangedPaths {\n readonly #sources: ChangedPathSources;\n readonly #sink: ChangeSink;\n readonly #trees = new Map<string, Promise<readonly GitTreeEntry[]>>();\n readonly #commitTrees = new Map<string, Promise<string>>();\n readonly #sizes = new Map<string, Promise<number>>();\n\n constructor(sources: ChangedPathSources) {\n this.#sources = sources;\n this.#sink = new ChangeSink(sources.maxChangedPaths);\n }\n\n /** The new commits an update brings, newest first, with their changes. */\n async commitsOf(update: RefUpdate): Promise<PushedCommit[]> {\n if (update.newSha === ZERO_SHA) return [];\n const { commits } = this.#sources.objects;\n const result: PushedCommit[] = [];\n const seen = new Set([update.newSha]);\n const queue = [update.newSha];\n for (let sha = queue.shift(); sha !== undefined; sha = queue.shift()) {\n const commit = commits.get(sha);\n if (commit === undefined) continue;\n if (commit.parents === null) throw new Uncheckable(`commit ${sha} has a header git reads differently`);\n result.push({\n sha,\n parents: [...commit.parents],\n changes: await this.#changesOf(commit.tree, commit.parents),\n });\n for (const parent of commit.parents) {\n if (!seen.has(parent)) {\n seen.add(parent);\n queue.push(parent);\n }\n }\n }\n return result;\n }\n\n /**\n * A commit's changes: against its parent, the empty tree for a root, and for a merge only the\n * paths that differ from every parent (the first parent's view of each).\n */\n async #changesOf(tree: string, parents: readonly string[]): Promise<ChangedPath[]> {\n const lookup = async (sha: string) => this.#tree(sha);\n const [first, ...others] = parents;\n let changes = await diffTrees(\n lookup,\n first === undefined ? null : await this.#treeOf(first),\n tree,\n this.#sink,\n );\n for (const other of others) {\n const paths = new Set(\n (await diffTrees(lookup, await this.#treeOf(other), tree, this.#sink)).map((c) => c.path),\n );\n changes = changes.filter((change) => paths.has(change.path));\n }\n const sorted = changes.toSorted((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));\n return Promise.all(sorted.map(async (change) => ({ ...change, size: await this.#sizeOf(change) })));\n }\n\n async #sizeOf(change: TreeChange): Promise<number | null> {\n if (change.newSha === null || change.newMode === SUBMODULE) return null;\n const sha = change.newSha;\n const inPack = this.#sources.objects.blobSizes.get(sha);\n if (inPack !== undefined) return inPack;\n return this.#cached(this.#sizes, sha, async () => {\n this.#sources.budget.spend();\n const blob = await this.#sources.backend.readBlob(this.#sources.repo, sha);\n if (blob === null) throw new Uncheckable(`blob ${sha} is in neither the pushed pack nor storage`);\n return blob.length;\n });\n }\n\n async #treeOf(commitSha: string): Promise<string> {\n const inPack = this.#sources.objects.commits.get(commitSha);\n if (inPack !== undefined) return inPack.tree;\n return this.#cached(this.#commitTrees, commitSha, async () => {\n this.#sources.budget.spend();\n const commit = await this.#sources.backend.readCommit(this.#sources.repo, commitSha);\n if (commit === null)\n throw new Uncheckable(`commit ${commitSha} is in neither the pushed pack nor storage`);\n return commit.tree;\n });\n }\n\n async #tree(sha: string): Promise<readonly GitTreeEntry[]> {\n return this.#cached(this.#trees, sha, async () => {\n const data = this.#sources.objects.trees.get(sha);\n if (data !== undefined) {\n try {\n return decodeTree(data, sha);\n } catch (error) {\n if (isCoreError(error)) throw new Uncheckable(`tree ${sha} could not be read (${error.message})`);\n throw error;\n }\n }\n this.#sources.budget.spend();\n const entries = await this.#sources.backend.readTree(this.#sources.repo, sha);\n if (entries === null) throw new Uncheckable(`tree ${sha} is in neither the pushed pack nor storage`);\n return entries.map(({ name, mode, sha: id }) => ({ name, mode, sha: id }));\n });\n }\n\n async #cached<T>(cache: Map<string, Promise<T>>, key: string, load: () => Promise<T>): Promise<T> {\n let found = cache.get(key);\n if (found === undefined) {\n found = load();\n cache.set(key, found);\n }\n return found;\n }\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ReadBudget } from "./uncheckable.js";
|
|
2
|
+
import { StorageBackend } from "@leed-ai/git-gateway-contract/storage/storage-backend";
|
|
3
|
+
/** A commit of the pushed pack: its tree, and its parents as git reads them (null: unreadable). */
|
|
4
|
+
export interface PackCommit {
|
|
5
|
+
readonly tree: string;
|
|
6
|
+
readonly parents: readonly string[] | null;
|
|
7
|
+
}
|
|
8
|
+
/** The objects of a pushed pack, indexed for computing changed paths. */
|
|
9
|
+
export interface PushedObjects {
|
|
10
|
+
readonly commits: ReadonlyMap<string, PackCommit>;
|
|
11
|
+
/** Tree objects' content by id. */
|
|
12
|
+
readonly trees: ReadonlyMap<string, Uint8Array>;
|
|
13
|
+
/** Blob sizes by id. */
|
|
14
|
+
readonly blobSizes: ReadonlyMap<string, number>;
|
|
15
|
+
}
|
|
16
|
+
/** Where a pushed pack's thin-pack bases are read from, and the bounds on reading it. */
|
|
17
|
+
export interface PushedObjectSources {
|
|
18
|
+
readonly repo: string;
|
|
19
|
+
readonly backend: Pick<StorageBackend, 'readTree' | 'readBlob'>;
|
|
20
|
+
readonly budget: ReadBudget;
|
|
21
|
+
readonly maxObjectBytes: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Reads a whole pushed pack (its checksum verified) into commits, trees and blob sizes, resolving
|
|
25
|
+
* deltas against objects in the pack and thin-pack bases in storage. Throws {@link Uncheckable}
|
|
26
|
+
* when the pack is malformed, needs more than `maxObjectBytes` or more storage reads than the
|
|
27
|
+
* budget allows, or names a base storage does not have.
|
|
28
|
+
*/
|
|
29
|
+
export declare function readPushedObjects(pack: Uint8Array, sources: PushedObjectSources): Promise<PushedObjects>;
|
|
30
|
+
//# sourceMappingURL=pushed-objects.d.ts.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Uncheckable } from "./uncheckable.js";
|
|
2
|
+
import { isCoreError } from "@leed-ai/git-gateway-core/core-error";
|
|
3
|
+
import { encodeTree } from "@leed-ai/git-gateway-core/objects/tree-object";
|
|
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 { readPack } from "@leed-ai/git-gateway-core/pack/read-pack";
|
|
7
|
+
// src/push-policy/pushed-objects.ts
|
|
8
|
+
/**
|
|
9
|
+
* Reads a thin pack's delta base from storage: as a tree (re-encoded, and accepted only when it
|
|
10
|
+
* hashes to the id asked for, so a backend's view of the tree cannot pass for different bytes)
|
|
11
|
+
* or as a blob. Each storage call counts against the budget.
|
|
12
|
+
*/
|
|
13
|
+
async function lookupBase(sources, sha) {
|
|
14
|
+
sources.budget.spend();
|
|
15
|
+
const entries = await sources.backend.readTree(sources.repo, sha);
|
|
16
|
+
if (entries !== null) {
|
|
17
|
+
const data = encodeTree(entries.map(({ name, mode, sha: id }) => ({
|
|
18
|
+
name,
|
|
19
|
+
mode,
|
|
20
|
+
sha: id
|
|
21
|
+
})));
|
|
22
|
+
return await hashObject("tree", data) === sha ? {
|
|
23
|
+
type: "tree",
|
|
24
|
+
sha,
|
|
25
|
+
data
|
|
26
|
+
} : null;
|
|
27
|
+
}
|
|
28
|
+
sources.budget.spend();
|
|
29
|
+
const blob = await sources.backend.readBlob(sources.repo, sha);
|
|
30
|
+
if (blob === null || await hashObject("blob", blob) !== sha) return null;
|
|
31
|
+
return {
|
|
32
|
+
type: "blob",
|
|
33
|
+
sha,
|
|
34
|
+
data: blob
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Reads a whole pushed pack (its checksum verified) into commits, trees and blob sizes, resolving
|
|
39
|
+
* deltas against objects in the pack and thin-pack bases in storage. Throws {@link Uncheckable}
|
|
40
|
+
* when the pack is malformed, needs more than `maxObjectBytes` or more storage reads than the
|
|
41
|
+
* budget allows, or names a base storage does not have.
|
|
42
|
+
*/
|
|
43
|
+
async function readPushedObjects(pack, sources) {
|
|
44
|
+
let objects;
|
|
45
|
+
try {
|
|
46
|
+
objects = await readPack(pack, {
|
|
47
|
+
maxObjectBytes: sources.maxObjectBytes,
|
|
48
|
+
lookupBase: async (sha) => lookupBase(sources, sha)
|
|
49
|
+
});
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof Uncheckable) throw error;
|
|
52
|
+
if (isCoreError(error, "unsupported_request")) throw new Uncheckable(`it needs more than ${sources.maxObjectBytes} bytes of objects`);
|
|
53
|
+
if (isCoreError(error)) throw new Uncheckable(`the pushed pack could not be read (${error.message})`);
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
const commits = /* @__PURE__ */ new Map();
|
|
57
|
+
const trees = /* @__PURE__ */ new Map();
|
|
58
|
+
const blobSizes = /* @__PURE__ */ new Map();
|
|
59
|
+
for (const object of objects) if (object.type === "tree") trees.set(object.sha, object.data);
|
|
60
|
+
else if (object.type === "blob") blobSizes.set(object.sha, object.data.length);
|
|
61
|
+
else {
|
|
62
|
+
const header = commitHeaderLines(new TextDecoder().decode(object.data));
|
|
63
|
+
commits.set(object.sha, {
|
|
64
|
+
tree: (header[0] ?? "").slice(5),
|
|
65
|
+
parents: readCommitParents(header)
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
commits,
|
|
70
|
+
trees,
|
|
71
|
+
blobSizes
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export { readPushedObjects };
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=pushed-objects.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pushed-objects.js","names":[],"sources":["../../src/push-policy/pushed-objects.ts"],"sourcesContent":["// SPDX-License-Identifier: Apache-2.0\nimport type { StorageBackend } from '@leed-ai/git-gateway-contract/storage/storage-backend';\nimport { isCoreError } from '@leed-ai/git-gateway-core/core-error';\nimport { commitHeaderLines, readCommitParents } from '@leed-ai/git-gateway-core/objects/commit-parents';\nimport { hashObject } from '@leed-ai/git-gateway-core/objects/hash-object';\nimport { encodeTree } from '@leed-ai/git-gateway-core/objects/tree-object';\nimport { readPack } from '@leed-ai/git-gateway-core/pack/read-pack';\nimport type { GitObject } from '@leed-ai/git-gateway-core/schemas/git-object';\n\nimport { Uncheckable, type ReadBudget } from './uncheckable.ts';\n\n/** A commit of the pushed pack: its tree, and its parents as git reads them (null: unreadable). */\nexport interface PackCommit {\n readonly tree: string;\n readonly parents: readonly string[] | null;\n}\n\n/** The objects of a pushed pack, indexed for computing changed paths. */\nexport interface PushedObjects {\n readonly commits: ReadonlyMap<string, PackCommit>;\n /** Tree objects' content by id. */\n readonly trees: ReadonlyMap<string, Uint8Array>;\n /** Blob sizes by id. */\n readonly blobSizes: ReadonlyMap<string, number>;\n}\n\n/** Where a pushed pack's thin-pack bases are read from, and the bounds on reading it. */\nexport interface PushedObjectSources {\n readonly repo: string;\n readonly backend: Pick<StorageBackend, 'readTree' | 'readBlob'>;\n readonly budget: ReadBudget;\n readonly maxObjectBytes: number;\n}\n\n/**\n * Reads a thin pack's delta base from storage: as a tree (re-encoded, and accepted only when it\n * hashes to the id asked for, so a backend's view of the tree cannot pass for different bytes)\n * or as a blob. Each storage call counts against the budget.\n */\nasync function lookupBase(sources: PushedObjectSources, sha: string): Promise<GitObject | null> {\n sources.budget.spend();\n const entries = await sources.backend.readTree(sources.repo, sha);\n if (entries !== null) {\n const data = encodeTree(entries.map(({ name, mode, sha: id }) => ({ name, mode, sha: id })));\n return (await hashObject('tree', data)) === sha ? { type: 'tree', sha, data } : null;\n }\n sources.budget.spend();\n const blob = await sources.backend.readBlob(sources.repo, sha);\n if (blob === null || (await hashObject('blob', blob)) !== sha) return null;\n return { type: 'blob', sha, data: blob };\n}\n\n/**\n * Reads a whole pushed pack (its checksum verified) into commits, trees and blob sizes, resolving\n * deltas against objects in the pack and thin-pack bases in storage. Throws {@link Uncheckable}\n * when the pack is malformed, needs more than `maxObjectBytes` or more storage reads than the\n * budget allows, or names a base storage does not have.\n */\nexport async function readPushedObjects(\n pack: Uint8Array,\n sources: PushedObjectSources,\n): Promise<PushedObjects> {\n let objects: GitObject[];\n try {\n objects = await readPack(pack, {\n maxObjectBytes: sources.maxObjectBytes,\n lookupBase: async (sha) => lookupBase(sources, sha),\n });\n } catch (error) {\n if (error instanceof Uncheckable) throw error;\n if (isCoreError(error, 'unsupported_request'))\n throw new Uncheckable(`it needs more than ${sources.maxObjectBytes} bytes of objects`);\n if (isCoreError(error)) throw new Uncheckable(`the pushed pack could not be read (${error.message})`);\n throw error;\n }\n const commits = new Map<string, PackCommit>();\n const trees = new Map<string, Uint8Array>();\n const blobSizes = new Map<string, number>();\n for (const object of objects) {\n if (object.type === 'tree') trees.set(object.sha, object.data);\n else if (object.type === 'blob') blobSizes.set(object.sha, object.data.length);\n else {\n const header = commitHeaderLines(new TextDecoder().decode(object.data));\n commits.set(object.sha, { tree: (header[0] ?? '').slice(5), parents: readCommitParents(header) });\n }\n }\n return { commits, trees, blobSizes };\n}\n"],"mappings":";;;;;;;;;;;;AAuCA,eAAe,WAAW,SAA8B,KAAwC;CAC9F,QAAQ,OAAO,MAAM;CACrB,MAAM,UAAU,MAAM,QAAQ,QAAQ,SAAS,QAAQ,MAAM,GAAG;CAChE,IAAI,YAAY,MAAM;EACpB,MAAM,OAAO,WAAW,QAAQ,KAAK,EAAE,MAAM,MAAM,KAAK,UAAU;GAAE;GAAM;GAAM,KAAK;EAAG,EAAE,CAAC;EAC3F,OAAQ,MAAM,WAAW,QAAQ,IAAI,MAAO,MAAM;GAAE,MAAM;GAAQ;GAAK;EAAK,IAAI;CAClF;CACA,QAAQ,OAAO,MAAM;CACrB,MAAM,OAAO,MAAM,QAAQ,QAAQ,SAAS,QAAQ,MAAM,GAAG;CAC7D,IAAI,SAAS,QAAS,MAAM,WAAW,QAAQ,IAAI,MAAO,KAAK,OAAO;CACtE,OAAO;EAAE,MAAM;EAAQ;EAAK,MAAM;CAAK;AACzC;;;;;;;AAQA,eAAsB,kBACpB,MACA,SACwB;CACxB,IAAI;CACJ,IAAI;EACF,UAAU,MAAM,SAAS,MAAM;GAC7B,gBAAgB,QAAQ;GACxB,YAAY,OAAO,QAAQ,WAAW,SAAS,GAAG;EACpD,CAAC;CACH,SAAS,OAAO;EACd,IAAI,iBAAiB,aAAa,MAAM;EACxC,IAAI,YAAY,OAAO,qBAAqB,GAC1C,MAAM,IAAI,YAAY,sBAAsB,QAAQ,eAAe,kBAAkB;EACvF,IAAI,YAAY,KAAK,GAAG,MAAM,IAAI,YAAY,sCAAsC,MAAM,QAAQ,EAAE;EACpG,MAAM;CACR;CACA,MAAM,0BAAU,IAAI,IAAwB;CAC5C,MAAM,wBAAQ,IAAI,IAAwB;CAC1C,MAAM,4BAAY,IAAI,IAAoB;CAC1C,KAAK,MAAM,UAAU,SACnB,IAAI,OAAO,SAAS,QAAQ,MAAM,IAAI,OAAO,KAAK,OAAO,IAAI;MACxD,IAAI,OAAO,SAAS,QAAQ,UAAU,IAAI,OAAO,KAAK,OAAO,KAAK,MAAM;MACxE;EACH,MAAM,SAAS,kBAAkB,IAAI,YAAY,CAAC,CAAC,OAAO,OAAO,IAAI,CAAC;EACtE,QAAQ,IAAI,OAAO,KAAK;GAAE,OAAO,OAAO,MAAM,GAAA,CAAI,MAAM,CAAC;GAAG,SAAS,kBAAkB,MAAM;EAAE,CAAC;CAClG;CAEF,OAAO;EAAE;EAAS;EAAO;CAAU;AACrC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { RefRefusal } from "../proxy/git-responses.js";
|
|
2
|
+
import { SmartHttpConfig } from "@leed-ai/git-gateway-contract/proxy/smart-http-config";
|
|
3
|
+
import { Principal } from "@leed-ai/git-gateway-contract/proxy/principal";
|
|
4
|
+
import { RefUpdate } from "@leed-ai/git-gateway-contract/schemas/ref-update";
|
|
5
|
+
import { PushPolicyNeeds } from "@leed-ai/git-gateway-contract/proxy/push-policy-result";
|
|
6
|
+
import { PushPolicy } from "@leed-ai/git-gateway-contract/proxy/gateway-hooks";
|
|
7
|
+
import { StorageBackend } from "@leed-ai/git-gateway-contract/storage/storage-backend";
|
|
8
|
+
import { PushedCommit } from "@leed-ai/git-gateway-contract/proxy/push-policy";
|
|
9
|
+
/** What running the push policy needs from the gateway. */
|
|
10
|
+
export interface PushPolicyDeps {
|
|
11
|
+
readonly pushPolicy: PushPolicy;
|
|
12
|
+
readonly backend: StorageBackend;
|
|
13
|
+
readonly config: SmartHttpConfig;
|
|
14
|
+
}
|
|
15
|
+
/** Who pushes what, where. */
|
|
16
|
+
export interface PushPolicyPush {
|
|
17
|
+
readonly principal: Principal;
|
|
18
|
+
readonly repo: string;
|
|
19
|
+
readonly updates: readonly RefUpdate[];
|
|
20
|
+
}
|
|
21
|
+
/** Asks the policy whether it needs changed paths for a push. */
|
|
22
|
+
export declare function pushPolicyNeeds(deps: PushPolicyDeps, push: PushPolicyPush): Promise<PushPolicyNeeds>;
|
|
23
|
+
/**
|
|
24
|
+
* Runs the policy's check over a push with, per update, its commits (null when changed paths
|
|
25
|
+
* were not asked for) and returns the refused updates: denied ones with the policy's reason, and
|
|
26
|
+
* any the result does not name.
|
|
27
|
+
*/
|
|
28
|
+
export declare function judgeWithPushPolicy(deps: PushPolicyDeps, push: PushPolicyPush, commits: readonly (PushedCommit[] | null)[]): Promise<RefRefusal[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Computes every update's commits and changed paths from the whole pushed pack (`pack`, checksum
|
|
31
|
+
* included) and storage, within the configured bounds, and runs the policy's check. A push that
|
|
32
|
+
* cannot be checked within them has every update refused, saying why.
|
|
33
|
+
*/
|
|
34
|
+
export declare function judgeChangedPaths(deps: PushPolicyDeps, push: PushPolicyPush, pack: Uint8Array): Promise<RefRefusal[]>;
|
|
35
|
+
//# sourceMappingURL=run-push-policy.d.ts.map
|