GitHubivarvong
ply
main
1// Shared wire helpers for the framing and pack spaces: a real http transport (src/http.js createTransport) over a fake
2// fetch that answers with exactly the bytes a cell renders, so every cell goes through ply's own pkt-line reader
3// (pktStep/pktFinish in the transport), its own consumers and its own error typing. No network, no server process.
4import { createTransport as createTransportR } from '../../src/http.js';
5import { Tracer } from '../../src/otel.js';
6import { withLimits } from '../../src/limits.js';
7import { isPlyCode } from '../../src/errors.js';
9import { unwrap } from '../unwrap.js';
10// Throwing adapters over the Result API (0.2.0): the assertions below are about the throw.
11const createTransport = (...a) => unwrap(createTransportR(...a));
12const te = new TextEncoder();
13export const enc = s => (typeof s === 'string' ? te.encode(s) : s);
14export const cat = parts => { let n = 0; for (const p of parts) n += p.length; const o = new Uint8Array(n); let i = 0; for (const p of parts) { o.set(p, i); i += p.length; } return o; };
15// A pkt-line exactly as git's writer frames it (4 lower-case hex digits, inclusive length). No bound: cells build
16// over-long frames on purpose.
17export const frame = payload => { const p = enc(payload); return cat([te.encode((p.length + 4).toString(16).padStart(4, '0')), p]); };
18export const FLUSH = te.encode('0000'), DELIM = te.encode('0001'), END = te.encode('0002');
19export const band = (b, body) => frame(cat([Uint8Array.of(b), enc(body)]));
21// responder(kind, service, body) -> { status?, contentType?, bytes } | undefined (404)
22export function fakeFetch(responder) {
23 return async (url, init = {}) => {
24 const u = new URL(url);
25 const adv = u.pathname.endsWith('/info/refs');
26 const service = adv ? u.searchParams.get('service') : u.pathname.split('/').pop();
27 const r = await responder(adv ? 'advertise' : 'post', service, init.body ?? null);
28 if (!r) return new Response('not found', { status: 404 });
29 const ct = r.contentType ?? `application/x-${service}-${adv ? 'advertisement' : 'result'}`;
30 return new Response(r.bytes, { status: r.status ?? 200, headers: { 'content-type': ct } });
31 };
32}
34// Each fake is its own server at one URL: its own quirks record, not the remote's remembered one (#136), so a test that
35// sets treeFilter on one transport does not change the next.
36export function transport(responder, limits = {}, extra = {}) {
37 return createTransport('https://git.example/repo.git', { tracer: new Tracer(), fetchImpl: fakeFetch(responder), limits: withLimits(limits), quirks: {}, ...extra });
38}
40// The outcome of an operation as a tagged union. `typed` holds only an error ply built with fail() (a code from its
41// table, the plain Error class): a TypeError or RangeError that `coded()` stamped with a code is `untyped`, a bug.
42export async function outcome(fn) {
43 try { return { tag: 'ok', value: await fn() }; } catch (e) {
44 if (!(e instanceof Error) || e.constructor !== Error || !isPlyCode(e.code)) return { tag: 'untyped', code: e?.code, name: e?.constructor?.name, message: String(e?.message ?? e) };
45 return { tag: 'typed', code: e.code, message: e.message };
46 }
47}
48export function outcomeSync(fn) {
49 try { return { tag: 'ok', value: fn() }; } catch (e) {
50 if (!(e instanceof Error) || e.constructor !== Error || !isPlyCode(e.code)) return { tag: 'untyped', code: e?.code, name: e?.constructor?.name, message: String(e?.message ?? e) };
51 return { tag: 'typed', code: e.code, message: e.message };
52 }
53}
55// A v2 fetch response: the given section lines, then `packfile` with the pack in band-1 frames of `chunk` bytes, a flush.
56export function v2PackResponse(pack, { chunk = 8192, before = [] } = {}) {
57 const frames = [...before.map(l => frame(l)), frame('packfile\n')];
58 for (let i = 0; i < pack.length; i += chunk) frames.push(band(1, pack.subarray(i, i + chunk)));
59 if (pack.length === 0) frames.push(band(1, new Uint8Array(0)));
60 frames.push(FLUSH);
61 return cat(frames);
62}
Diagnostics7 git calls · 42 ms
CallStatusSizeTime042 ms
discover200191 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=191 git.server.agent=git/github-9922ec520325-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=191 git.pkt.payload.max=36
ls-refs20012 KiB3 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=11990 git.response.cache=UPDATING git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=152 git.pkt.data=151 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11990 git.pkt.payload.max=122 git.ls_refs.count=151 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 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=4347 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 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=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4347 git.pkt.payload.max=4247 git.sideband.band1.bytes=4247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=4247 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400 git.pack.full=10Open pack map
fetchtree:0200919 B · 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=919 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=919 git.pkt.payload.max=892 git.sideband.band1.bytes=892 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=892 git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 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=5724 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetchtree:02001.4 KiB · hit3 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=1428 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=74d7bb09987bbe78a9c0ea8f43a1b5a4e079f538 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1428 git.pkt.payload.max=1401 git.sideband.band1.bytes=1401 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1401 git.pack.trailer=65ea1a3aeef333a80a2dfa3f441e629d90497e8eOpen pack map
fetch2001.8 KiB · hit29 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=29 git.response.bytes=1805 git.response.cache=HIT git.response.duration_ms=29 git.fetch.wants=f2d77a92deadcff689cfa8c51180a22107f03e19 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1805 git.pkt.payload.max=1778 git.sideband.band1.bytes=1778 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://github.com/ivarvong/ply
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e1721e19ca6102
- Request
- 60694b75…
- Protocol
- v2 · sha1
- Agent
- git/github-9922ec520325-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format