-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.ts
More file actions
74 lines (67 loc) · 2.79 KB
/
Copy pathwindow.ts
File metadata and controls
74 lines (67 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
/**
* Server-side commit-window cap used by every chart query, the TypeScript port
* of `server/src/api/window.rs`.
*
* Visual downsampling is the client's job; this module only decides how many of
* the most recent commits (by `commits.timestamp`) a chart loads.
*/
/** Default window when no `?n=` is supplied. */
export const DEFAULT_COMMIT_WINDOW = 100;
/**
* Largest numeric `?n=` accepted before clamping. `?n=all` remains the explicit
* opt-in for full history, with no server-side cap.
*/
export const MAX_NUMERIC_COMMIT_WINDOW = 1000;
/**
* Resolved commit window. `Last(n)` keeps the most recent `n` commits; `All`
* returns every commit ever ingested.
*/
export type CommitWindow = { kind: 'all' } | { kind: 'last'; n: number };
/**
* Parse the `?n=...` query parameter. `null`/`undefined` and malformed values
* fall back to the default window. `"all"` (any case, surrounding whitespace
* trimmed) means unbounded. Numeric values are floored to `1` (so `?n=0`
* becomes `?n=1`) and clamped down to [`MAX_NUMERIC_COMMIT_WINDOW`]. A value
* that overflows `u32` (the Rust parse type) is treated as malformed and falls
* back to the default, matching `window.rs`.
*/
export function parseCommitWindow(raw: string | null | undefined): CommitWindow {
if (raw === null || raw === undefined) {
return { kind: 'last', n: DEFAULT_COMMIT_WINDOW };
}
const trimmed = raw.trim();
if (/^all$/i.test(trimmed)) {
return { kind: 'all' };
}
// Rust's `str::parse::<u32>()` accepts an optional leading `+` then ASCII
// digits, and rejects signs, decimals, and overflow. Anything it would reject
// falls back to the default window.
if (!/^\+?\d+$/.test(trimmed)) {
return { kind: 'last', n: DEFAULT_COMMIT_WINDOW };
}
const value = Number(trimmed);
const U32_MAX = 4_294_967_295;
if (value > U32_MAX) {
return { kind: 'last', n: DEFAULT_COMMIT_WINDOW };
}
const clamped = Math.min(Math.max(value, 1), MAX_NUMERIC_COMMIT_WINDOW);
return { kind: 'last', n: clamped };
}
/**
* The `LIMIT` value for the most-recent-commits subquery, or `null` for an
* unbounded `All` window (no `LIMIT` clause). Mirrors `window.rs::limit_param`.
*/
export function commitWindowLimit(window: CommitWindow): number | null {
return window.kind === 'all' ? null : window.n;
}
/**
* Render the window as the value the URL carries (`"100"` / `"all"`). Ported
* ahead of its consumer: v3's `CommitWindow::url_value` is used by the HTML
* toolbar and permalink links, which arrive with the PR-4.4.b client islands.
* Until then this is exercised only by its unit test.
*/
export function commitWindowUrlValue(window: CommitWindow): string {
return window.kind === 'all' ? 'all' : String(window.n);
}