main
1// Compile-time checks: `tsc --noEmit` must pass; every @ts-expect-error must still be an error.
2import { assertNever, match, matchKind, matchOption, ok, some, none, optionToResult, resultToOption, transposeOptionResult, transposeResultOption, andThen, type Option, type Result } from '../../src/result.js';
3import { fail, type PlyError, type Code, type Miss } from '../../src/errors.js';
5const r: Result<number, PlyError> = Math.random() > 0.5 ? ok(1) : fail('timeout', 't');
6if (r.ok) { const n: number = r.value; void n; } else { const c: Code = r.error.code; void c; }
7// @ts-expect-error value is not accessible without narrowing
8void r.value;
10// exhaustive switch
11const describe = (e: PlyError): string => {
12 switch (e.code) {
13 case 'http': return `status ${e.status}`; // narrowed: status exists
14 case 'limit_exceeded': return `${e.what} ${e.got} > ${e.max}`;
15 case 'ambiguous': return String(e.original ?? 'unknown');
16 case 'cas_conflict': return `${e.expected} != ${e.actual}`;
17 case 'match_count': return String(e.matches);
18 case 'merge_conflict': return e.conflicts.join(',');
19 case 'bad_request': case 'unsupported': case 'network': case 'timeout': case 'aborted': case 'auth':
20 case 'not_found': case 'rate_limited': case 'upstream_5xx': case 'not_git':
21 case 'protocol': case 'pack_corrupt': case 'remote_error': case 'lfs_pointer':
22 case 'push_rejected': case 'push_partial': case 'not_fast_forward':
23 case 'not_linear': case 'unrelated_histories': case 'internal':
24 return e.message;
25 }
26 return assertNever(e);
27};
28void describe;
29const missingCase = (e: PlyError): string => {
30 switch (e.code) {
31 case 'http': return 'x';
32 }
33 // @ts-expect-error not exhaustive: e is not never here
34 return assertNever(e);
35};
36void missingCase;
38// payload required by code
39fail('http', 'x', { status: 500 });
40// @ts-expect-error http requires status
41fail('http', 'x');
42// @ts-expect-error timeout takes no payload
43fail('timeout', 'x', { status: 1 });
44// @ts-expect-error unknown code
45fail('nope', 'x');
47// match: every tag, and only tags
48type Step = { code: 'more'; n: number } | { code: 'done' };
49const s = { code: 'more', n: 1 } as Step;
50match(s, { more: x => x.n, done: () => 0 });
51// @ts-expect-error missing handler
52match(s, { more: x => x.n });
53// @ts-expect-error extra handler
54match(s, { more: x => x.n, done: () => 0, other: () => 1 });
56// matchKind: the #131 miss union, and any operation error discriminated by kind
57declare const miss: Miss;
58const missLabel = matchKind(miss, {
59 ref: x => x.ref,
60 head_unborn: x => x.target,
61 head_absent: () => 'absent',
62 path: x => x.missing,
63 not_directory: x => x.mode,
64 is_directory: x => x.tree,
65 submodule: x => x.commit,
66 commit: x => x.miss.kind,
67 repository: () => 'repository',
68});
69void missLabel;
70// @ts-expect-error missing kind handler
71matchKind(miss, { ref: x => x.ref });
73type OpErr = { readonly kind: 'not_string'; readonly which: 'left' } | { readonly kind: 'not_bytes'; readonly which: 'right' };
74declare const opErr: OpErr;
75matchKind(opErr, { not_string: x => x.which, not_bytes: x => x.which });
76// @ts-expect-error missing not_bytes
77matchKind(opErr, { not_string: x => x.which });
79// Option: both tags, and it composes with Result.
80const present: Option<number> = some(1);
81const absent: Option<number> = none;
82matchOption(present, { some: n => n, none: () => 0 });
83// @ts-expect-error missing none
84matchOption(present, { some: n => n });
85const back: Result<number, 'missing'> = optionToResult(absent, 'missing');
86const dropped: Option<number> = resultToOption(back);
87const nested: Option<Result<number, 'missing'>> = transposeResultOption(ok(absent));
88const flat: Result<Option<number>, 'missing'> = transposeOptionResult(some(ok(1)));
89void dropped; void nested; void flat;
91// andThen unions error types
92const a = andThen(ok(1) as Result<number, 'e1'>, n => (n > 0 ? ok(String(n)) : { ok: false as const, error: 'e2' as const }));
93const e: Result<string, 'e1' | 'e2'> = a; void e;
Diagnostics4 git calls · 421 ms
CallStatusSizeTime0421 ms
discover200152 B3 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=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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=416 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=2 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
fetch2001.5 KiB416 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1561 git.response.cache=MISS git.response.duration_ms=416 git.fetch.wants=ccc70a8b0d3cbc3a24c563a2052b6c9733c8c6ad 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=1561 git.pkt.payload.max=1540 git.sideband.band1.bytes=1539 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
- a5e321d2a8645aae
- Request
- 2ccedf7d…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format