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}
Diagnostics4 git calls · 359 ms
CallStatusSizeTime0359 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 B · hit3 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=HIT 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 1020038 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=38434 git.response.cache=HIT git.response.duration_ms=3 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=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=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2002.2 KiB353 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=353 git.response.bytes=2261 git.response.cache=MISS git.response.duration_ms=353 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=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2261 git.pkt.payload.max=2240 git.sideband.band1.bytes=2239 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.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e2cd0ce8a17929
- Request
- f6bd0755…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format