main
ply4 git calls259 ms43 KiB2 cachedprotocol v2
216 lines7.9 KiBRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import {
4 atIndex,
5 cacheMatch,
6 captureGroup,
7 chainGet,
8 decodeJson,
9 dig,
10 endOptionalSpan,
11 envBinding,
12 fromAbsent,
13 fromNullable,
14 header,
15 isPlatformAbsent,
16 jsonField,
17 manualInit,
18 mapGet,
19 otelAttribute,
20 otelListField,
21 queryParam,
22 r2Get,
23 recordGet,
24 requestHeader,
25 requestQuery,
26 textAt,
27 thrownField,
28 writeSpanAttribute,
29} from "../src/absent.ts";
30import { none, some } from "../src/option.ts";
32test("fromAbsent keeps null and fromNullable does not", () => {
33 const missing = new Map<string, string>().get("missing");
34 assert.equal(isPlatformAbsent(missing), true);
35 assert.deepEqual(fromAbsent(missing), none());
36 assert.deepEqual(fromNullable(missing), none());
37 assert.deepEqual(fromAbsent(null), some(null));
38 assert.deepEqual(fromNullable(null), none());
39 assert.deepEqual(fromNullable(0), some(0));
40 assert.deepEqual(fromNullable(""), some(""));
41 assert.deepEqual(fromAbsent("x"), some("x"));
42});
44test("map and record reads turn a missing key into none", () => {
45 const present = new Map([["k", "v"]]);
46 assert.deepEqual(mapGet(present, "k"), some("v"));
47 assert.deepEqual(mapGet(present, "missing"), none());
48 assert.deepEqual(recordGet({ a: "1" }, "a"), some("1"));
49 assert.deepEqual(recordGet({ a: "1" }, "b"), none());
50 assert.deepEqual(recordGet({ a: null }, "a"), some(null));
51 assert.deepEqual(recordGet(null, "a"), none());
52 const missingRecord = new Map<string, Record<string, string>>().get("env");
53 assert.deepEqual(recordGet(missingRecord, "a"), none());
54});
56test("chainGet and dig collapse a broken chain", () => {
57 assert.deepEqual(dig({ a: { b: 1 } }, ["a", "b"]), some(1));
58 assert.deepEqual(dig({ a: null }, ["a", "b"]), none());
59 assert.deepEqual(dig(null, ["a"]), none());
60 assert.deepEqual(dig({ a: 1 }, []), some({ a: 1 }));
61 assert.deepEqual(chainGet({ a: null }, "a"), none());
62 assert.deepEqual(chainGet(1, "a"), none());
63});
65test("atIndex and capture groups treat a hole as none", () => {
66 const sparse: string[] = [];
67 sparse.length = 1;
68 assert.deepEqual(atIndex(sparse, 0), none());
69 assert.deepEqual(atIndex(["a"], 0), some("a"));
70 assert.deepEqual(atIndex(["a"], -1), none());
71 assert.deepEqual(atIndex(["a"], 1.5), none());
72 assert.deepEqual(textAt("ab", 1), some("b"));
73 assert.deepEqual(textAt("ab", 2), none());
74 assert.deepEqual(textAt("ab", -1), none());
75 const match = /^(\d+)|([a-z]+)$/.exec("ab");
76 assert.deepEqual(captureGroup(match, 1), none());
77 assert.deepEqual(captureGroup(match, 2), some("ab"));
78 assert.deepEqual(captureGroup(null, 0), none());
79});
81test("headers, query, and request helpers", () => {
82 const headers = new Headers();
83 assert.deepEqual(header(headers, "etag"), none());
84 headers.set("etag", "abc");
85 assert.deepEqual(header(headers, "etag"), some("abc"));
86 const params = new URL("https://ply.example/v1/read?path=README").searchParams;
87 assert.deepEqual(queryParam(params, "path"), some("README"));
88 assert.deepEqual(queryParam(params, "ref"), none());
89 const request = new Request("https://ply.example/v1/read?path=README", { headers: { "x-ply-unit": "abc" } });
90 assert.deepEqual(requestHeader(request, "x-ply-unit"), some("abc"));
91 assert.deepEqual(requestHeader(request, "authorization"), none());
92 assert.deepEqual(requestQuery(request, "path"), some("README"));
93 assert.deepEqual(requestQuery(request, "ref"), none());
94});
96test("env bindings treat a missing binding as none and empty text as present", () => {
97 const missing = new Map<string, string>().get("FLAGS");
98 assert.deepEqual(envBinding(missing), none());
99 assert.deepEqual(envBinding(null), none());
100 assert.deepEqual(envBinding(""), some(""));
101 assert.deepEqual(envBinding("5"), some("5"));
102});
104test("cache match and R2 get", async () => {
105 const miss = {
106 async match(_request: RequestInfo | URL): Promise<Response | null> {
107 return null;
108 },
109 };
110 assert.deepEqual(await cacheMatch(miss, "https://flags.internal/config"), none());
111 const response = new Response("ok");
112 const hit = {
113 async match(_request: RequestInfo | URL): Promise<Response | null> {
114 return response;
115 },
116 };
117 assert.deepEqual(await cacheMatch(hit, "https://flags.internal/config"), some(response));
118 const empty = {
119 async get(_key: string): Promise<{ etag: string } | null> {
120 return null;
121 },
122 };
123 assert.deepEqual(await r2Get(empty, "flags/config.json"), none());
124 const object = { etag: "rev" };
125 const bucket = {
126 async get(_key: string): Promise<{ etag: string } | null> {
127 return object;
128 },
129 };
130 assert.deepEqual(await r2Get(bucket, "flags/config.json"), some(object));
131});
133test("JSON null is a value and a missing field is none", () => {
134 assert.deepEqual(decodeJson("{"), { tag: "invalid", reason: "syntax" });
135 assert.deepEqual(decodeJson("null"), { tag: "value", value: null });
136 const decoded = decodeJson('{"a":null,"b":1}');
137 assert.equal(decoded.tag, "value");
138 if (decoded.tag !== "value") return;
139 assert.deepEqual(jsonField(decoded.value, "missing"), none());
140 assert.deepEqual(jsonField(decoded.value, "a"), some(null));
141 assert.deepEqual(jsonField(decoded.value, "b"), some(1));
142 assert.deepEqual(jsonField(null, "a"), none());
143 assert.deepEqual(jsonField(["a"], "0"), none());
144});
146test("OTel attribute absence", () => {
147 assert.deepEqual(otelAttribute({ "flags.revision": "abc" }, "flags.revision"), some("abc"));
148 assert.deepEqual(otelAttribute({ "flags.revision": "abc" }, "flags.unit_id"), none());
149 assert.deepEqual(otelAttribute({ "flags.count": 2 }, "flags.count"), some(2));
150 assert.deepEqual(otelAttribute({ bad: { nested: true } }, "bad"), none());
151 assert.deepEqual(otelAttribute(null, "flags.revision"), none());
152 const list = [
153 { key: "git.fetch.filter", value: "tree:0" },
154 { key: "empty", value: null },
155 ];
156 assert.deepEqual(otelListField(list, "git.fetch.filter"), some("tree:0"));
157 assert.deepEqual(otelListField(list, "empty"), some(null));
158 assert.deepEqual(otelListField(list, "missing"), none());
159 assert.deepEqual(otelListField(null, "k"), none());
160});
162test("thrown fields include prototype message", () => {
163 const err = Object.assign(new Error("nope"), { code: "not_found" });
164 assert.deepEqual(thrownField(err, "code"), some("not_found"));
165 assert.deepEqual(thrownField(err, "message"), some("nope"));
166 assert.deepEqual(thrownField(err, "status"), none());
167 assert.deepEqual(thrownField(null, "code"), none());
168 assert.deepEqual(thrownField("x", "code"), none());
169});
171test("manual init turns none into the host absent value", () => {
172 const missing = manualInit({ method: "GET", headers: {}, body: none(), signal: none() });
173 assert.equal(missing.method, "GET");
174 assert.equal(missing.redirect, "manual");
175 assert.equal(isPlatformAbsent(missing.body), true);
176 assert.equal(isPlatformAbsent(missing.signal), true);
177 const body = new Uint8Array([1]);
178 const signal = new AbortController().signal;
179 const present = manualInit({
180 method: "POST",
181 headers: { "content-type": "text/plain" },
182 body: some(body),
183 signal: some(signal),
184 });
185 assert.equal(present.body, body);
186 assert.equal(present.signal, signal);
187});
189test("span writers ignore a missing span and a throwing span", () => {
190 const seen: string[] = [];
191 const span = {
192 setAttribute(key: string, value: string | number | boolean): void {
193 seen.push(`${key}=${String(value)}`);
194 },
195 end(): void {
196 seen.push("end");
197 },
198 };
199 writeSpanAttribute(span, "flags.cache_status", "HIT");
200 writeSpanAttribute(null, "flags.cache_status", "MISS");
201 writeSpanAttribute({}, "flags.cache_status", "MISS");
202 endOptionalSpan(span);
203 endOptionalSpan(null);
204 endOptionalSpan({});
205 const boom = {
206 setAttribute(): void {
207 throw new Error("telemetry down");
208 },
209 end(): void {
210 throw new Error("telemetry down");
211 },
212 };
213 writeSpanAttribute(boom, "flags.cache_status", "HIT");
214 endOptionalSpan(boom);
215 assert.deepEqual(seen, ["flags.cache_status=HIT", "end"]);
216});
Diagnostics4 git calls · 259 ms
CallStatusSizeTime0259 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=2
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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=3
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=61
Open pack map
fetch2002.2 KiB254 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=254
git.response.bytes=2217
git.response.cache=MISS
git.response.duration_ms=254
git.fetch.wants=1dd09b14d009cdfaff0e60a0e5b77a6d8595028a
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=2217
git.pkt.payload.max=2196
git.sideband.band1.bytes=2195
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
a5e2f5228e727089
Request
88ec9e5c…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format