GitHubivarvong
ply
main
1// inflate(buf, pos): pos is a position in buf, a non-negative safe integer no greater than buf.length. Anything else is
2// the caller's mistake and is refused as `bad_request` before a byte is read: never a throw out of the Result, and never
3// a read from somewhere pos does not name (Uint8Array.subarray reads a negative offset from the END and coerces a
4// string or a fraction, so an unchecked pos inflated bytes the caller never pointed at and answered ok).
5//
6// Expected values come from outside the code under test: the members are node:zlib's, a valid pos must give the bytes
7// node:zlib inflates from the same offset and the length of the member node:zlib wrote, and a pos at the very end is a
8// valid position holding no bytes, which zlib itself calls a cut stream (Z_BUF_ERROR, "unexpected end of file"), not a
9// bad argument. The refused cells are the contract's: each names a value that is not an index into the buffer.
10import { test } from 'vitest';
11import assert from 'node:assert/strict';
12import zlib from 'node:zlib';
13import { inflate } from '../src/inflate.js';
15const text = new TextEncoder().encode('hello, pos');
16const member = zlib.deflateSync(text);
17const junk = new Uint8Array(8).fill(0x78);
18// junk, then a member, then an EMPTY member as the last bytes: the tail a negative pos would read
19const empty = zlib.deflateSync(new Uint8Array(0));
20const buf = new Uint8Array([...junk, ...member, ...empty]);
22// never throws: a throw is reported as a failed cell with the thrown value, not as the test's own crash
23const run = pos => {
24 try { return inflate(buf, pos); } catch (e) { return { ok: false, threw: e }; }
25};
27test('a pos that is not a position in the buffer is bad_request: no throw, no read', () => {
28 // The third column is what the message calls the value, written out by hand: a number is named as JavaScript prints
29 // it, anything else by its type alone (a string's content never reaches the message, so the message is bounded
30 // whatever the caller passed). buf is 8 + member + empty bytes long; its length is spelled from the parts.
31 const cells = [
32 ['negative', -1, '-1'],
33 ['negative, the length of the trailing member (subarray reads it from the end)', -8, '-8'],
34 ['fractional', 1.5, '1.5'],
35 ['fractional, a hair past a valid position', 8.5, '8.5'],
36 ['NaN', Number.NaN, 'NaN'],
37 ['Infinity', Number.POSITIVE_INFINITY, 'Infinity'],
38 ['-Infinity', Number.NEGATIVE_INFINITY, '-Infinity'],
39 ['past the end by one', buf.length + 1, String(8 + member.length + empty.length + 1)],
40 ['past the end, not a safe integer', 2 ** 53, '9007199254740992'],
41 ['a string that spells a valid position', '8', 'string'],
42 ['a string that spells zero', '0', 'string'],
43 ['an empty string', '', 'string'],
44 ['a long string', 'x'.repeat(10_000), 'string'],
45 ['null', null, 'object'],
46 ['a bigint', 8n, 'bigint'],
47 ['an object with valueOf', { valueOf: () => junk.length }, 'object'],
48 ];
49 assert.equal(empty.length, 8, 'node:zlib\'s empty member is 8 bytes (2 header, 2 block, 4 adler), the -8 cell\'s premise');
50 for (const [name, pos, named] of cells) {
51 const r = run(pos);
52 assert.equal(r.threw, undefined, `${name}: threw ${r.threw}`);
53 assert.equal(r.ok, false, `${name}: answered ok (${r.ok ? `${r.value.out.length} B, consumed ${r.value.consumed}` : ''})`);
54 assert.equal(r.error.code, 'bad_request', `${name}: ${r.error.code}: ${r.error.message}`);
55 assert.equal(r.error.message, `inflate: pos (${named}) is not a position in a buffer of ${8 + member.length + empty.length} bytes`, name);
56 }
57});
59test('a valid pos inflates the member there: node:zlib\'s bytes and the member\'s length', () => {
60 for (const [pos, want, len] of [[junk.length, text, member.length], [junk.length + member.length, new Uint8Array(0), empty.length]]) {
61 const r = run(pos);
62 assert.equal(r.ok, true, `pos ${pos}: ${r.ok ? '' : r.error?.message ?? r.threw}`);
63 assert.deepEqual([...r.value.out], [...want]);
64 assert.deepEqual([...r.value.out], [...zlib.inflateSync(buf.subarray(pos, pos + len))], 'node:zlib from the same offset');
65 assert.equal(r.value.consumed, len, 'consumed is the length of the member node:zlib wrote');
66 }
67 // omitted and -0 are position 0
68 const whole = inflate(member);
69 assert.equal(whole.ok && whole.value.consumed, member.length);
70 const negZero = inflate(member, -0);
71 assert.equal(negZero.ok && negZero.value.consumed, member.length);
72});
74test('pos exactly at the end is a valid position with no bytes: a cut member (zlib: unexpected end of file), not bad_request', () => {
75 // the oracle: zlib on the same (empty) input reports a truncated stream
76 assert.throws(() => zlib.inflateSync(buf.subarray(buf.length)), /unexpected end of file/);
77 const r = run(buf.length);
78 assert.equal(r.threw, undefined);
79 assert.equal(r.ok, false);
80 assert.equal(r.error.code, 'pack_corrupt');
81 // the same answer as an empty buffer at position 0
82 const e = inflate(new Uint8Array(0), 0);
83 assert.equal(e.ok, false);
84 assert.equal(e.error.code, 'pack_corrupt');
85 assert.equal(r.error.message, e.error.message);
86});
Diagnostics6 git calls · 248 ms
CallStatusSizeTime0248 ms
discover200191 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=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 KiB81 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=80 git.response.bytes=11990 git.response.cache=EXPIRED git.response.duration_ms=81 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 · 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=4347 git.response.cache=HIT git.response.duration_ms=3 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 · 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=919 git.response.cache=HIT git.response.duration_ms=2 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 · hit4 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=5724 git.response.cache=HIT git.response.duration_ms=4 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
fetch2002.0 KiB158 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=157 git.response.bytes=2056 git.response.cache=MISS git.response.duration_ms=158 git.fetch.wants=d2a1a7dfd682cd258d824b5a73f3818be74527bf 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=2056 git.pkt.payload.max=2029 git.sideband.band1.bytes=2029 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
- a5e2d257cacecf3a
- Request
- 1200b4e8…
- 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