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}
Diagnostics4 git calls · 373 ms
CallStatusSizeTime0373 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 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 · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=4 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 · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=4 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.7 KiB365 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=365 git.response.bytes=2746 git.response.cache=MISS git.response.duration_ms=365 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=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2746 git.pkt.payload.max=2725 git.sideband.band1.bytes=2724 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
- a5e1b562c96a5b65
- Request
- 18a9d911…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format