GitHubivarvong
ply
main
1// zlib (RFC 1950) + deflate (RFC 1951) inflate of a buffer in hand, reporting the bytes it consumed: a collector over the
2// pull decoder (inflatestream.ts), which is the only deflate decoder in ply (#172). Pack objects are concatenated zlib
3// streams with no length prefix; `consumed` is what positions a buffered reader at the next object header.
4import type { Result } from './result.js';
5import { ok, assertNever } from './result.js';
6import { fail } from './errors.js';
7import { fromNullable, fromNullish, type Input } from './absent.js';
8import { DEFAULT_LIMITS } from './limits.js';
9import type { InflateStats, InflateError } from './inflatestream.js';
10import { inflateInit, inflateFeed, inflateEnd, declaredBound, cappedBound, zlibWrap, rawWrap } from './inflatestream.js';
12export type { Huffman, InflateStats, InflateError } from './inflatestream.js';
13export { huffBuild, adler32, FIXED_LIT, FIXED_DIST, CLORDER, DEFLATE_TABLES } from './inflatestream.js';
15// expectedSize: the size the output must have (a pack header's); maxOutput: the cap on the output (limits.maxObjectSize
16// when left out); verifyAdler: check the zlib trailer's checksum (default true); raw: a deflate stream with no zlib
17// header or trailer (default false).
18export type InflateConfig = {
19 readonly expectedSize: number;
20 readonly maxOutput: number;
21 readonly verifyAdler: boolean;
22 readonly raw: boolean;
23};
24export type InflateOptions = Input<InflateConfig>;
25// The first output buffer without a declared size: at least this, or the input's length (a stored member's output).
26const GUESS_MIN = 256;
27// The output buffer doubles when it is full.
28const GROWTH = 2;
29export type Inflated = { readonly out: Uint8Array; readonly consumed: number; readonly stats: InflateStats };
31// The member at buf[pos..], whole: the input is closed once it is fed, so the pull ends the member or says where it was
32// cut. With a declared size the output is one buffer of that size; without one, the chunks joined once at the end.
33// pos is a position in buf: a non-negative safe integer no greater than buf.length (at the end there are no bytes, a
34// cut member). Anything else is `bad_request` before a byte is read: subarray reads a negative offset from the end of
35// the buffer and coerces a string or a fraction, so an unchecked pos inflated bytes the caller never named (or threw).
36// @decoder inflate prefix=inflate
37export function inflate(buf: Uint8Array, pos = 0, opts: InflateOptions = {}): Result<Inflated, InflateError> {
38 // isSafeInteger is false for every non-number (no coercion); the message names the type, never an unbounded value
39 if (!Number.isSafeInteger(pos) || pos < 0 || pos > buf.length) return fail('bad_request', `inflate: pos (${typeof pos === 'number' ? String(pos) : typeof pos}) is not a position in a buffer of ${buf.length} bytes`);
40 const { verifyAdler = true, raw = false } = opts;
41 const expected = fromNullish(opts.expectedSize);
42 // Omitted means the object cap; a given cap is checked by the pull (inflateInit), so `null` is bad_request there.
43 const givenMax = fromNullable(opts.maxOutput);
44 const maxOutput = givenMax.tag === 'some' ? givenMax.value : DEFAULT_LIMITS.maxObjectSize;
45 const bound = expected.tag === 'some' ? declaredBound(expected.value, maxOutput) : cappedBound(maxOutput);
46 const opened = inflateInit({ bound, wrap: raw ? rawWrap() : zlibWrap(verifyAdler) });
47 if (!opened.ok) return opened;
48 const st = opened.value;
49 inflateFeed(st, buf.subarray(pos));
50 // A declared size: the pull never yields past it, so the chunks land in one buffer of that size, made when the first
51 // chunk arrives (a size past the cap is refused at the zlib header, before anything is allocated). Without one the
52 // buffer starts at a guess and doubles, held to the cap (the pull refuses output past it before the buffer grows).
53 let out = new Uint8Array(0);
54 let o = 0;
55 const room = (n: number): void => {
56 // Stryker disable next-line EqualityOperator: at an exact fit, `<` grows a new buffer and copies the output into it: the same bytes come out
57 if (o + n <= out.length) return;
58 // Stryker disable next-line ArithmeticOperator,MethodExpression,ConditionalExpression: the buffer is grown until it holds the output, so a different positive first size yields the same bytes
59 let cap = expected.tag === 'some' ? expected.value : Math.min(maxOutput, Math.max(out.length * GROWTH, GUESS_MIN, buf.length - pos));
60 // Stryker disable next-line ArithmeticOperator,MethodExpression,EqualityOperator: doubling stops once the buffer holds o+n, which the pull held within the cap
61 while (cap < o + n) cap = Math.min(maxOutput, cap * GROWTH);
62 const grown = new Uint8Array(cap);
63 grown.set(out.subarray(0, o));
64 out = grown;
65 };
66 for (;;) {
67 const r = inflateEnd(st);
68 if (!r.ok) return r;
69 const piece = r.value;
70 switch (piece.kind) {
71 case 'end': return ok({ out: out.subarray(0, o), consumed: piece.consumed, stats: piece.stats });
72 case 'chunk':
73 room(piece.bytes.length);
74 out.set(piece.bytes, o);
75 o += piece.bytes.length;
76 continue;
77 // Stryker disable next-line all: a closed input never answers `need` (inflateEnd refuses the cut instead)
78 case 'need': continue;
79 }
80 // Stryker disable next-line ConditionalExpression,BlockStatement,CallExpression: unreachable, the compiler's exhaustiveness check
81 return assertNever(piece);
82 }
83}
Diagnostics6 git calls · 90 ms
CallStatusSizeTime090 ms
discover200191 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 KiB · 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=11990 git.response.cache=HIT git.response.duration_ms=2 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 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:02002.4 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=83 git.response.bytes=2507 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=8f720d29ff85ce682ffdad9c393b04a3834c3203 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=2507 git.pkt.payload.max=2480 git.sideband.band1.bytes=2480 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=2480 git.pack.trailer=723b6bf1d4d6021fe5eb940c622b9ff00ee4a381Open pack map
fetch2002.2 KiB83 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2286 git.response.cache=MISS git.response.duration_ms=83 git.fetch.wants=96b81a14f709d322ac5752fec97045090af9e594 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=2286 git.pkt.payload.max=2259 git.sideband.band1.bytes=2259 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
- a5e15f640df3725b
- Request
- 80bad7fd…
- 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