Skip to content
112 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

What g1t can't do yet, and an open letter to Cloudflare1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 PRIMARY_WINDOW_SECONDS,
6 bookmarkCookie,
7 coveredMs,
8 mayWrite,
9 metricName,
10 readBookmarks,
11 rpcMethodOf,
12 serverTiming,
13 serviceDuration,
Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily14 databaseTime,
What g1t can't do yet, and an open letter to Cloudflare15 sessionFor,
Public pages cacheable again: a signed-out GET never sets g1t_d1; stars, about and public_links are reads16 setsBookmark,
What g1t can't do yet, and an open letter to Cloudflare17 writeBookmarks,
18} from "./perf.ts";
19
20const BOOKMARK = "0000002c-00000004-00004f95-c7f4a9b2e8d1f0c3b6a5d4e3f2a1b0c9";
21
22test("bookmarks survive the cookie", () => {
23 const value = writeBookmarks({ at: 1_800_000_000, services: { work: BOOKMARK, repos: BOOKMARK } });
24 assert.equal(value, `at:1800000000~repos:${BOOKMARK}~work:${BOOKMARK}`);
25 assert.deepEqual(readBookmarks(`g1t_session=abc; g1t_d1=${value}; other=1`), {
26 at: 1_800_000_000,
27 services: { repos: BOOKMARK, work: BOOKMARK },
28 });
29});
30
31test("a malformed or foreign cookie entry is dropped", () => {
32 assert.deepEqual(readBookmarks(null), { at: null, services: {} });
33 assert.deepEqual(readBookmarks("g1t_d1=at:soon~work:bad bookmark~actions:abc~repos:" + BOOKMARK), {
34 at: null,
35 services: { repos: BOOKMARK },
36 });
37 // Only a service that reads with sessions is kept.
38 assert.equal(writeBookmarks({ at: null, services: { actions: BOOKMARK } }), "");
39});
40
41test("the cookie is HttpOnly, short-lived and Secure over HTTPS", () => {
42 const cookie = bookmarkCookie({ at: 1, services: {} }, true);
43 assert.match(cookie, /^g1t_d1=at:1; Path=\/; HttpOnly; Secure; SameSite=Lax; Max-Age=300$/);
44 assert.doesNotMatch(bookmarkCookie({ at: 1, services: {} }, false), /Secure/);
45});
46
47test("what each call asks for", () => {
48 const now = 1_800_000_000;
49 const none = { at: null, services: {} };
50 // A service without sessions is left alone: its primary, as before.
51 assert.equal(sessionFor("actions", none, false, now), null);
52 // A form post reads the primary everywhere.
53 assert.equal(sessionFor("work", { at: null, services: { work: BOOKMARK } }, true, now), "first-primary");
54 // A page with no recent write reads the nearest copy.
55 assert.equal(sessionFor("work", none, false, now), "first-unconstrained");
56 // Just after a write, every service reads its primary, bookmark or not.
57 const justWrote = { at: now - 5, services: { work: BOOKMARK } };
58 assert.equal(sessionFor("work", justWrote, false, now), "first-primary");
59 assert.equal(sessionFor("repos", justWrote, false, now), "first-primary");
60 // Later, the bookmark keeps the read at least as new as the write.
61 const later = { at: now - PRIMARY_WINDOW_SECONDS, services: { work: BOOKMARK } };
62 assert.equal(sessionFor("work", later, false, now), BOOKMARK);
63 assert.equal(sessionFor("repos", later, false, now), "first-unconstrained");
64});
65
66test("only known reads are taken not to write", () => {
Merge project overview: one branch_drift call, spliced histories, cached tags, 6 repos calls instead of 2567 for (const method of ["get_pull", "list_pulls", "counts", "user_for_session", "explore", "usage", "get", "list", "queue", "pulls_for_repos", "stars", "about", "public_links", "branch_drift", "tags", "commit_checks", "shortcuts", "last_commits", "languages"]) {
What g1t can't do yet, and an open letter to Cloudflare68 assert.equal(mayWrite(method), false, method);
69 }
70 for (const method of ["merge_pull", "verify_email", "github_finish", "sign_in", "something_new"]) {
71 assert.equal(mayWrite(method), true, method);
72 }
73 assert.equal(rpcMethodOf("https://service/rpc/get_pull"), "get_pull");
74 assert.equal(rpcMethodOf("https://service/other"), "");
75});
76
Public pages cacheable again: a signed-out GET never sets g1t_d1; stars, about and public_links are reads77test("a signed-out GET never sets the bookmark cookie, so public pages stay cacheable", () => {
78 const read = { writing: false, signedIn: false };
79 assert.equal(setsBookmark({ ...read, wrote: true, hasSession: false }), false);
80 assert.equal(setsBookmark({ ...read, wrote: true, hasSession: true }), true);
81 assert.equal(setsBookmark({ ...read, wrote: false, hasSession: true }), false);
82 // A form post, or signing in on a GET, always does.
83 assert.equal(setsBookmark({ writing: true, signedIn: false, wrote: false, hasSession: false }), true);
84 assert.equal(setsBookmark({ writing: false, signedIn: true, wrote: false, hasSession: false }), true);
85});
86
What g1t can't do yet, and an open letter to Cloudflare87test("timings", () => {
88 assert.equal(serviceDuration('svc;dur=12;desc="session"'), 12);
89 assert.equal(serviceDuration("repo;dur=3, svc;dur=7.5"), 7.5);
90 assert.equal(serviceDuration(null), null);
Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily91 assert.deepEqual(databaseTime('svc;dur=40;desc="session", db;dur=22;desc="2 round trips, 21 statements", rpc;dur=18;desc="1 calls"'), { ms: 22, trips: 2 });
92 assert.equal(databaseTime('svc;dur=40;desc="session"'), null);
What g1t can't do yet, and an open letter to Cloudflare93 // Overlapping calls are counted once.
94 assert.equal(coveredMs([[0, 100], [50, 120], [200, 210]]), 130);
95 assert.equal(coveredMs([]), 0);
96 assert.equal(metricName("routes/repo/pull"), "repo.pull");
97 const header = serverTiming({
98 totalMs: 180,
99 loaders: [{ id: "routes/repo/pull", ms: 150, kind: "loader" }],
100 rpcMs: 140,
101 services: {
Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily102 work: { calls: 3, wallMs: 120, serviceMs: 90, dbMs: 40, dbTrips: 3 },
What g1t can't do yet, and an open letter to Cloudflare103 repos: { calls: 1, wallMs: 30, serviceMs: 0 },
104 },
105 sessions: "work=unconstrained",
106 });
107 assert.equal(
108 header,
109 'total;dur=180;desc="web to first byte", loader.repo.pull;dur=150, rpc;dur=140;desc="4 service calls, overlap counted once", ' +
Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily110 'work;dur=120;desc="3 calls, 90ms inside, db 40ms in 3 round trips", repos;dur=30;desc="1 call", d1;desc="work=unconstrained"',
What g1t can't do yet, and an open letter to Cloudflare111 );
112});

This file's history is long; its oldest lines are credited to the oldest commit read.