Artifactspublic-v0

ply

main
ply4 git calls474 ms40 KiB2 cachedprotocol v2
109 lines6.4 KiBRaw
1// Errors as values. A fallible function returns Result<T, E>; it does not throw. `throw` is reserved for
2// programmer bugs (assertNever). Plain frozen-shape objects: no classes, no runtime dependency, nothing to tree-shake.
4export type Ok<T> = { readonly ok: true; readonly value: T };
5export type Err<E> = { readonly ok: false; readonly error: E };
6export type Result<T, E> = Ok<T> | Err<E>;
8export const ok = <T>(value: T): Ok<T> => ({ ok: true, value });
9export const err = <E>(error: E): Err<E> => ({ ok: false, error });
11// The compile-time exhaustiveness check. A switch over a union lists every member and has no `default`;
12// the next statement is `assertNever(x)`. A new member leaves `x` short of `never`, so the call does not type-check.
13export function assertNever(x: never): never {
14 throw new Error(`unreachable: ${String(x)}`);
17// Sequencing without exceptions. `andThen` short-circuits on the first Err; the error types union.
18export const map = <T, U, E>(r: Result<T, E>, f: (t: T) => U): Result<U, E> => (r.ok ? ok(f(r.value)) : r);
19export const mapErr = <T, E, F>(r: Result<T, E>, f: (e: E) => F): Result<T, F> => (r.ok ? r : err(f(r.error)));
20export const andThen = <T, U, E, F>(r: Result<T, E>, f: (t: T) => Result<U, F>): Result<U, E | F> => (r.ok ? f(r.value) : r);
22// Exhaustive match over a tagged union: one handler per tag, checked by the compiler (a missing or extra tag
23// does not type-check), each handler receiving the narrowed member.
24export type Handlers<U extends { readonly code: string }, R> = { readonly [K in U['code']]: (u: Extract<U, { readonly code: K }>) => R };
25export const match = <U extends { readonly code: string }, R>(u: U, h: Handlers<U, R>): R =>
26 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- h[u.code] is the handler for u's own member (Handlers)
27 (h[u.code as U['code']] as (u: U) => R)(u);
29// The same check for a union discriminated by `kind` (a typed miss, or one operation's error union).
30export type KindHandlers<U extends { readonly kind: string }, R> = { readonly [K in U['kind']]: (u: Extract<U, { readonly kind: K }>) => R };
31export const matchKind = <U extends { readonly kind: string }, R>(u: U, h: KindHandlers<U, R>): R =>
32 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- h[u.kind] is the handler for u's own member (KindHandlers)
33 (h[u.kind as U['kind']] as (u: U) => R)(u);
35// What a span records as `error.kind`. A typed miss is the discriminant callers switch on (#131); an operation
36// that distinguishes variants of one code sets `kind`; `reason` is the closed sub-reason (#76); otherwise the code.
37const FALLBACK_KIND = 'Error';
38const isRecord = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null;
39const stringField = (v: Record<string, unknown>, key: string): string | null => {
40 if (!Object.hasOwn(v, key)) return null;
41 const value = v[key];
42 return typeof value === 'string' ? value : null;
43};
44export function errorKind(e: unknown): string {
45 if (!isRecord(e)) return FALLBACK_KIND;
46 const miss = e['miss'];
47 if (isRecord(miss)) {
48 const missKind = stringField(miss, 'kind');
49 if (missKind !== null) return missKind;
50 }
51 const kind = stringField(e, 'kind');
52 if (kind !== null) return kind;
53 const reason = stringField(e, 'reason');
54 if (reason !== null) return reason;
55 const code = stringField(e, 'code');
56 if (code !== null) return code;
57 return FALLBACK_KIND;
59export const errorKindAttr = (e: unknown): { readonly 'error.kind': string } => ({ 'error.kind': errorKind(e) });
61// Absence, in the same closed-tag style as a typed miss (t134u). A missing value is None.
62// Result keeps its `ok` discriminant (the 0.2.0 decision). Option uses `tag`. The helpers below are the composition.
63export type Some<T> = { readonly tag: 'some'; readonly value: T };
64export type None = { readonly tag: 'none' };
65export type Option<T> = Some<T> | None;
67export const some = <T>(value: T): Some<T> => ({ tag: 'some', value });
68export const none: None = { tag: 'none' };
70// Total over any input (#296): a value that is not an Option (null, a number, an object with another tag) is neither
71// some nor none, so both answer false for it rather than throwing on the property read.
72const isTagged = (o: unknown, tag: Option<unknown>['tag']): boolean => isRecord(o) && o['tag'] === tag;
73export const isSome = <T>(o: Option<T>): o is Some<T> => isTagged(o, 'some');
74export const isNone = <T>(o: Option<T>): o is None => isTagged(o, 'none');
76export type OptionHandlers<T, R> = { readonly some: (value: T) => R; readonly none: (absent: None) => R };
77export const matchOption = <T, R>(o: Option<T>, h: OptionHandlers<T, R>): R => {
78 if (o.tag === 'none') return h.none(o);
79 // Not a method call named `some`: that is Array#some to the linter.
80 const present = h.some;
81 return present(o.value);
82};
84// Ok becomes Some. Err becomes None; the error is dropped on purpose (None carries no reason).
85export const resultToOption = <T, E>(r: Result<T, E>): Option<T> => (r.ok ? some(r.value) : none);
87// Some becomes Ok. None becomes Err(error). The error is required: None has nothing to put there.
88export const optionToResult = <T, E>(o: Option<T>, error: E): Result<T, E> => (o.tag === 'some' ? ok(o.value) : err(error));
90// Ok(Some(v)) → Some(Ok(v)); Ok(None) → None; Err(e) → Some(Err(e)).
91export const transposeResultOption = <T, E>(r: Result<Option<T>, E>): Option<Result<T, E>> => {
92 if (!r.ok) return some(err(r.error));
93 return r.value.tag === 'some' ? some(ok(r.value.value)) : none;
94};
96// Some(Ok(v)) → Ok(Some(v)); Some(Err(e)) → Err(e); None → Ok(None).
97export const transposeOptionResult = <T, E>(o: Option<Result<T, E>>): Result<Option<T>, E> => {
98 if (o.tag === 'none') return ok(none);
99 return o.value.ok ? ok(some(o.value.value)) : err(o.value.error);
100};
102// map, flatMap, and getOrElse. matchOption is the exhaustive eliminator; these are its two branches.
103// Names follow matchOption: `map` is already Result's. None is returned as itself, so a chain does not allocate a fresh absence.
104// getOrElse takes a value the caller already computed. A fallback that should run only on None belongs in matchOption.
105export const mapOption = <T, U>(o: Option<T>, f: (value: T) => U): Option<U> => (o.tag === 'some' ? some(f(o.value)) : o);
107export const flatMapOption = <T, U>(o: Option<T>, f: (value: T) => Option<U>): Option<U> => (o.tag === 'some' ? f(o.value) : o);
109export const getOrElse = <T>(o: Option<T>, fallback: T): T => (o.tag === 'some' ? o.value : fallback);
Diagnostics4 git calls · 474 ms
CallStatusSizeTime0474 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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=55
Open pack map
fetch2002.4 KiB468 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=468
git.response.bytes=2465
git.response.cache=MISS
git.response.duration_ms=468
git.fetch.wants=1281bddb72ae89c2a324651e9277414a731be549
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=2465
git.pkt.payload.max=2444
git.sideband.band1.bytes=2443
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
a5e2cb0608f47929
Request
3bb2a20f…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format