main
1// GET /v1/read's body (src/hold-last.ts): the final bytes are released only once the verified source has ended, so a
2// complete Content-Length body is never on the wire while verification is still running, and a failed verification
3// leaves the body short.
4import { test } from 'node:test';
5import assert from 'node:assert/strict';
6import { holdLastChunk } from '../src/hold-last.ts';
8const enc = (s: string) => new TextEncoder().encode(s);
9const dec = (b: Uint8Array[]) => new TextDecoder().decode(Buffer.concat(b));
11// ply's openBlob shape: each chunk is passed on as soon as it is hashed; the hash is checked in flush, after the last
12// chunk. `verify` is that check; it resolves (verified) or rejects (corrupt) when the test says so.
13function verifyingSource(chunks: string[], verify: Promise<void>) {
14 return new ReadableStream<Uint8Array>({ start(c) { for (const x of chunks) c.enqueue(enc(x)); c.close(); } })
15 .pipeThrough(new TransformStream<Uint8Array, Uint8Array>({ transform(chunk, ctl) { ctl.enqueue(chunk); }, async flush() { await verify; } }));
16}
18function gate() { let open!: () => void, fail!: (e: Error) => void; const p = new Promise<void>((a, b) => { open = a; fail = b; }); return { p, open, fail }; }
20test('without the hold, the whole body is readable before verification ends (the canceled-read race)', async () => {
21 const g = gate();
22 const r = verifyingSource(['hello ', 'world'], g.p).getReader();
23 const got: Uint8Array[] = [];
24 for (let i = 0; i < 2; i++) { const x = await r.read(); assert.equal(x.done, false); got.push(x.value!); }
25 assert.equal(dec(got), 'hello world', 'every byte is out while the check is pending');
26 g.open(); assert.equal((await r.read()).done, true);
27});
29test('holdLastChunk: the last chunk waits for the source to end, then the body ends with it', async () => {
30 const g = gate();
31 const r = holdLastChunk(verifyingSource(['hello ', 'wor', 'ld'], g.p)).getReader();
32 const got: Uint8Array[] = [];
33 got.push((await r.read()).value!); got.push((await r.read()).value!);
34 assert.equal(dec(got), 'hello wor');
35 let settled = false;
36 const last = r.read().then(x => { settled = true; return x; });
37 for (let i = 0; i < 20; i++) await new Promise(res => setImmediate(res));
38 assert.equal(settled, false, 'the final chunk is held while verification is pending');
39 g.open();
40 const x = await last; assert.equal(x.done, false); got.push(x.value!);
41 assert.equal(dec(got), 'hello world');
42 assert.equal((await r.read()).done, true);
43});
45test('holdLastChunk: a failed verification errors the stream with the last chunk never released (short body)', async () => {
46 const g = gate();
47 const r = holdLastChunk(verifyingSource(['abc', 'def'], g.p)).getReader();
48 assert.equal(dec([(await r.read()).value!]), 'abc');
49 const pending = r.read();
50 g.fail(Object.assign(new Error('blob content hashes to x'), { code: 'pack_corrupt' }));
51 await assert.rejects(pending, (e: Partial<{ code: string }>) => e.code === 'pack_corrupt');
52});
54test('holdLastChunk: a single-chunk blob (the common small file) is released only at the end; empty chunks and empty blobs pass', async () => {
55 const g = gate();
56 const r = holdLastChunk(verifyingSource(['tiny'], g.p)).getReader();
57 let settled = false;
58 const first = r.read().then(x => { settled = true; return x; });
59 for (let i = 0; i < 20; i++) await new Promise(res => setImmediate(res));
60 assert.equal(settled, false);
61 g.open();
62 assert.equal(dec([(await first).value!]), 'tiny');
63 assert.equal((await r.read()).done, true);
64 const e = holdLastChunk(verifyingSource(['', 'a', '', 'b', ''], Promise.resolve())).getReader();
65 const out: Uint8Array[] = []; for (;;) { const x = await e.read(); if (x.done) break; out.push(x.value); }
66 assert.deepEqual(out.map(b => new TextDecoder().decode(b)), ['a', 'b']);
67 assert.equal((await holdLastChunk(verifyingSource([], Promise.resolve())).getReader().read()).done, true);
68});
Diagnostics4 git calls · 69 ms
CallStatusSizeTime069 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 B3 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=UPDATING 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.4 KiB · hit64 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=64 git.response.bytes=1440 git.response.cache=HIT git.response.duration_ms=64 git.fetch.wants=ad6b52cdc962b767729d0394925be7d3b0d8baab 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=1440 git.pkt.payload.max=1419 git.sideband.band1.bytes=1418 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
- a5e32558cbb498f1
- Request
- cddbd4d3…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format