main
1// One request, four stages. parse, authorize, and execute return a Result.
2// respond is the only place that turns a Result into an HTTP status.
3// A throw is an unexpected state of that request: the edge answers it and records the error kind.
5export type Result<T, E> =
6 | { readonly ok: true; readonly value: T }
7 | { readonly ok: false; readonly error: E };
9export type Stage = 'parse' | 'authorize' | 'execute';
11export type UnexpectedFailure = {
12 readonly kind: 'unexpected';
13 readonly stage: Stage;
14 readonly name: string;
15};
17export function ok<T>(value: T): Result<T, never> {
18 return { ok: true, value };
19}
21export function err<E>(error: E): Result<never, E> {
22 return { ok: false, error };
23}
25export function isUnexpected<E>(error: E | UnexpectedFailure): error is UnexpectedFailure {
26 return !!error && typeof error === 'object' && 'kind' in error && error.kind === 'unexpected';
27}
29export type HttpReply = {
30 readonly status: number;
31 readonly body: BodyInit | null;
32 readonly headers: Headers;
33};
35export function httpReply(status: number, body: BodyInit | null, headers: Headers): HttpReply {
36 return { status, body, headers };
37}
39export function responseOf(reply: HttpReply): Response {
40 return new Response(reply.body, { status: reply.status, headers: reply.headers });
41}
43type Stages<P, A, X, E> = {
44 parse: () => Promise<Result<P, E>> | Result<P, E>;
45 authorize: (parsed: P) => Promise<Result<A, E>> | Result<A, E>;
46 execute: (authorized: A) => Promise<Result<X, E>> | Result<X, E>;
47};
49function thrownName(error: unknown): string {
50 return error instanceof Error && error.name ? error.name : 'Error';
51}
53function assertStage(stage: Stage): Stage {
54 switch (stage) {
55 case 'parse':
56 case 'authorize':
57 case 'execute':
58 return stage;
59 default:
60 return assertNever(stage);
61 }
62}
64function assertNever(value: never): never {
65 throw new Error(`unhandled variant ${JSON.stringify(value)}`);
66}
68/**
69 * Run parse, then authorize, then execute. respond maps the Result to a Response.
70 * A throw becomes `{ kind: 'unexpected' }` for this request only. The error message stays off the log.
71 */
72export async function runRequest<P, A, X, E>(
73 stages: Stages<P, A, X, E>,
74 respond: (result: Result<X, E | UnexpectedFailure>) => Promise<Response> | Response,
75): Promise<Response> {
76 let stage: Stage = 'parse';
77 try {
78 const parsed = await stages.parse();
79 if (!parsed.ok) return await respond(parsed);
80 stage = 'authorize';
81 const authorized = await stages.authorize(parsed.value);
82 if (!authorized.ok) return await respond(authorized);
83 stage = 'execute';
84 const executed = await stages.execute(authorized.value);
85 return await respond(executed);
86 } catch (error) {
87 const failure: UnexpectedFailure = { kind: 'unexpected', stage: assertStage(stage), name: thrownName(error) };
88 try {
89 return await respond({ ok: false, error: failure });
90 } catch {
91 console.log(JSON.stringify({ msg: 'ply.request', error_kind: failure.kind, stage: failure.stage, error_name: failure.name }));
92 return new Response('unexpected\n', {
93 status: 500,
94 headers: { 'cache-control': 'no-store', 'content-type': 'text/plain; charset=utf-8', 'x-ply-error-kind': failure.kind },
95 });
96 }
97 }
98}
Diagnostics4 git calls · 24 ms
CallStatusSizeTime024 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2001.2 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=1252 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=5f9feabe93b889309015c93f1bf933fcf2fe5058 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1252 git.pkt.payload.max=1231 git.sideband.band1.bytes=1230 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e23ce26cde4bc2
- Request
- 14bad991…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format