app/lib/timing.ts
42 lines and 6 functions in DrDustinEdwards/dustinedwards-info, at commit 1590ba4 on 2026-10-24. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| wantsTiming | 12 to 14 | 3 | No data: no coverage report in this map | No data: no coverage report in this map | No data: no mutants in this code |
| timed | 16 to 28 | 2 | No data: no coverage report in this map | No data: no coverage report in this map | No data: no mutants in this code |
| timedLoader | 34 to 41 | 1 | No data: no coverage report in this map | No data: no coverage report in this map | No data: no mutants in this code |
| <anonymous:40> | 40 to 40 | 1 | No data: no coverage report in this map | No data: no coverage report in this map | No data: no mutants in this code |
| serverTiming | 44 to 48 | 1 | No data: no coverage report in this map | No data: no coverage report in this map | No data: no mutants in this code |
| <anonymous:46> | 46 to 46 | 1 | No data: no coverage report in this map | No data: no coverage report in this map | No data: no mutants in this code |
Imports
It imports no other module of this repo. Imported by app/db/posts.ts, app/lib/editor/link-targets.server.ts, app/lib/health/checks.server.ts, app/lib/search/ask.server.ts, app/root.tsx, app/routes/admin._index.tsx, app/routes/admin.kb.tsx, app/routes/admin.media._index.tsx, app/routes/admin.mentions.tsx, app/routes/admin.posts.$slug.edit.tsx, app/routes/admin.posts.$slug.history.tsx, app/routes/admin.posts.$slug.revisions.tsx, app/routes/admin.posts._index.tsx, app/routes/admin.posts.new.tsx, app/routes/admin.tools.tsx, app/routes/admin.tsx, app/routes/blog._index.tsx, app/routes/home.tsx, workers/app.ts.
Source
import { createContext, type RouterContextProvider } from "react-router";
export type Timings = Array<{ name: string; ms: number }>;
// Shared so the `admin.tsx` middleware and child loaders write into one Server-Timing header.
export const timingsContext = createContext<{ timings?: Timings }>({});
/**
* Whether this request collects Server-Timing marks: every admin request (a signed-in person's, never cached, so the
* numbers cost nothing to read and are there when a page is slow), and any other page that asks with `?timing=1`.
*/
export function wantsTiming(url: URL) {
return url.searchParams.get("timing") === "1" || url.pathname === "/admin" || url.pathname.startsWith("/admin/");
}
export async function timed<T>(
into: Timings | undefined,
name: string,
fn: () => Promise<T>,
): Promise<T> {
if (!into) return fn();
const start = performance.now();
try {
return await fn();
} finally {
into.push({ name, ms: performance.now() - start });
}
}
/**
* Runs a loader body as one timed mark, `loader_total` unless named. Wrapping the body rather than
* pushing before the last return is what counts the early returns and the throws too.
*/
export function timedLoader<T>(
context: Readonly<RouterContextProvider>,
fn: (timings: Timings | undefined) => Promise<T>,
name = "loader_total",
): Promise<T> {
const timings = context.get(timingsContext).timings;
return timed(timings, name, () => fn(timings));
}
// Names must be tokens: one malformed name makes the browser silently discard the whole header.
export function serverTiming(timings: Timings) {
return timings
.map(({ name, ms }) => `${name.replace(/[^A-Za-z0-9_-]/g, "")};dur=${ms.toFixed(1)}`)
.join(", ");
}