GitHubivarvong
ply
main
1// The only file under src/ that may spell the platform's missing value, an optional member (`?:`),
2// optional chaining, or nullish coalescing. Mutation sharding assumes a flat src/ (scripts/mutate.mjs), so
3// this boundary stays here rather than in a subdirectory.
4//
5// Oid's brand is `string & { readonly [brand] }`. The same intersection on the platform missing value is
6// `never` (TypeScript 7), so it cannot carry a phantom. The brand is this file: every platform read comes
7// through a function here, and what leaves is Option from result.ts (the one Option). Null stays a wire value.
8import { none, some, type Option } from './result.js';
10// The platform missing value as a test, for a hot loop where an Option per read would allocate (otel.ts flatValue).
11export function isMissing(value: unknown): value is undefined {
12 return value === undefined;
13}
15// Drops only the platform missing value. Null stays Some(null).
16export function fromNullable<T>(value: T): Option<Exclude<T, undefined>> {
17 if (isMissing(value)) return none;
18 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the missing value is excluded; a generic T does not narrow
19 return some(value as Exclude<T, undefined>);
20}
22// `?.` and `??` collapse null and the platform missing value. That collapse happens here, once.
23export function fromNullish<T>(value: T): Option<Exclude<T, null | undefined>> {
24 if (value === null || isMissing(value)) return none;
25 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- null and the missing value are excluded; a generic T does not narrow
26 return some(value as Exclude<T, null | undefined>);
27}
29// A field typed as Option that a JavaScript caller omitted. A real Option passes through; omission is None.
30export function asOption<T>(value: Option<T> | undefined): Option<T> {
31 if (isMissing(value)) return none;
32 return value;
33}
35// A platform API that still demands its missing value. The word stays in this file; callers pass the result on.
36export function unwrapOrAbsent<T>(value: Option<T>): T | undefined {
37 if (value.tag === 'none') return undefined;
38 return value.value;
39}
41// A field of any value, as `value?.[key]` read it: None when the value is not an object, lacks the field, or holds
42// null or the platform missing value there. The one reader for thrown values and foreign records.
43export function field(value: unknown, key: string): Option<unknown> {
44 if (typeof value !== 'object' || value === null || !(key in value)) return none;
45 return fromNullish(Reflect.get(value, key));
46}
48// A function's last parameter that a caller may leave out or pass as the platform missing value, so an optional signal
49// is forwarded as it is (`inner.read(offset, length, signal)`). Spread as a rest parameter: `...signal: SignalArg`.
50export type SignalArg = [signal?: AbortSignal | undefined];
52// ---- the input edge ----
53// What a caller may leave out of a record: every key optional, and an explicit missing value means the same as
54// leaving the key out. Entry points declare their options as Input<Config> and parse them once with withDefaults;
55// past that line the record is total and no code asks whether a field is there.
56export type Input<T> = { readonly [K in keyof T]?: T[K] | undefined };
59// Fill every key the caller left out (or passed as the missing value) from `defaults`. Keys outside `defaults` are not
60// read: the config's type is the schema. `defaults` is the caller's fresh record (a mutable default such as a quirks
61// object must not be shared between two calls), so it is filled in place and returned.
62// An options or spec argument after its signature default (`= {}`): a plain object. null, a number or an array is the
63// caller's error, answered as bad_request by the entry point that names it (#296). undefined never reaches it.
64export function isArgObject(value: unknown): boolean {
65 return typeof value === 'object' && value !== null && !Array.isArray(value);
66}
68export function withDefaults<T extends object>(input: Input<T> | null | undefined, defaults: T): T {
69 if (input === null || isMissing(input)) return defaults;
70 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the keys of T, as Object.keys cannot type them
71 for (const key of Object.keys(defaults) as Array<keyof T>) {
72 const given = input[key];
73 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Input<T>[K] without its missing value is T[K]
74 if (!isMissing(given)) defaults[key] = given as T[keyof T];
75 }
76 return defaults;
77}
79// A record the caller owns and shares between calls (a transport's learned quirks): fill the keys it lacks in place,
80// so the caller's object stays the one every later call sees, and return it as the total type.
81export function fillIn<T extends object>(target: Input<T>, defaults: T): T {
82 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the keys of T, as Object.keys cannot type them
83 for (const key of Object.keys(defaults) as Array<keyof T>) {
84 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- a writable view of the caller's record, filled key by key
85 if (isMissing(target[key])) (target as { [K in keyof T]: T[K] })[key] = defaults[key];
86 }
87 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- every key of T is now present
88 return target as T;
89}
91// Direct index read: a negative index is absent, not the element from the end (that is Array#at's other behaviour).
92type Indexed<T> = ArrayLike<T> & { at(index: number): T | undefined };
94export function at<T>(items: Indexed<T>, index: number): Option<T> {
95 // A non-integer or a negative index is absent. Array#at would coerce 1.5 to 1 and wrap -1 to the last element.
96 // Past the end is a missing read, which fromNullable turns into None (no separate length test: it would be the same result).
97 if (!Number.isInteger(index) || index < 0) return none;
98 return fromNullable(items.at(index));
99}
101// Same read, with the caller's domain fallback. Does not allocate an Option (hot paths: hex tables, byte scans).
102export function atOr<T>(items: Indexed<T>, index: number, fallback: T): T {
103 if (!Number.isInteger(index) || index < 0) return fallback;
104 const value = items.at(index);
105 if (isMissing(value)) return fallback;
106 return value;
107}
109export function mapGet<K, V>(table: ReadonlyMap<K, V>, key: K): Option<V> {
110 return fromNullable(table.get(key));
111}
113export function readKey<T extends object, K extends keyof T>(obj: T, key: K): Option<Exclude<T[K], undefined>> {
114 return fromNullable(obj[key]);
115}
117// Invalid JSON is None. JSON null is Some(null); callers that want null as absence use fromNullish on the value.
118export function jsonDecode(text: string): Option<unknown> {
119 try {
120 const value: unknown = JSON.parse(text);
121 return fromNullable(value);
122 } catch {
123 return none;
124 }
125}
127// A byte index is in [lo, hi]. Out of range, a non-integer index, and a hole are not. No Option allocation:
128// UTF-8 and the pack parsers ask this per byte.
129export function byteInRange(buf: Uint8Array, index: number, lo: number, hi: number): boolean {
130 if (!Number.isInteger(index) || index < 0) return false;
131 const value = buf.at(index);
132 // Stryker disable next-line LogicalOperator: a missing byte compared with >= is false, so dropping the missing test changes no result (it is there for the type)
133 return value !== undefined && value >= lo && value <= hi;
134}
Diagnostics8 git calls · 261 ms
CallStatusSizeTime0261 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 KiB84 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=84 git.response.bytes=11990 git.response.cache=EXPIRED git.response.duration_ms=84 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 1200347 B · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=347 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=1 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=347 git.pkt.payload.max=247 git.sideband.band1.bytes=247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=247 git.pack.trailer=4b72f39b5ea57c5ae3009f90b53e3a6a155093e7Open pack map
fetchtree:0 · deepen 102004.2 KiB · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=55 git.response.bytes=4347 git.response.cache=HIT git.response.duration_ms=5 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
fetchcombine:blob:none+tree:2 · deepen 1429—55 ms
git.upload-pack fetch
http.response.status_code=429
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.cache=BYPASS
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
error.type=rate_limited
error=HTTP 429 git-upload-pack: \u000d
<html>\u000d
<head>\u000d
<meta content="origin" name="referrer">\u000d
<title>Rate limit · GitHub</title>\u000d
<meta name="vfetchtree: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:02002.4 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=2507 git.response.cache=HIT git.response.duration_ms=2 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.7 KiB117 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=117 git.response.bytes=2772 git.response.cache=MISS git.response.duration_ms=117 git.fetch.wants=2f536ecfc97bcc0e547218b8530d26d896bd6654 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=2772 git.pkt.payload.max=2745 git.sideband.band1.bytes=2745 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
- a5e2b384f9a9cf33
- Request
- 446ceedc…
- 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