UNPKG

1.95 kBTypeScriptView Raw
1interface ServerTiming {
2 /** The name attribute is required, and gives a short name to the server-specified metric. */
3 name: string;
4 /** (Optional) The duration attribute is a double (like `0.0`) that contains the server-specified metric duration. Usually milliseconds. */
5 duration?: number;
6 /** (Optional) The description describes the server-specified metric. */
7 description?: string;
8}
9declare type ServerTimings = Array<ServerTiming>;
10/**
11 * Parse a given [`Server-Timing`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing) header string value into corresponding timing components.
12 *
13 * @example
14 * ```
15 * parse('missedCache')
16 * // [{ name: "missedCache" }]
17 * parse('cpu;dur=2.4')
18 * // [{ name: "cpu", duration: 2.4 }]
19 * parse('cache;desc="Cache Read";dur=23.2')
20 * // [{ name: "cache", description: "Cache Read", duration: 23.2 }]
21 * parse('db;dur=53, app;dur=47.2')
22 * // [{ name: "db", duration: 53 }, { name: "app", duration: 47.2 }]
23 * ```
24 * @param timingHeader Header value (as string) to parse.
25 * @returns {ServerTimings} Timing components as an object.
26 */
27declare const parse: (timingHeader: string) => ServerTimings;
28/**
29 * Stringify a given {@link ServerTimings} object into a [`Server-Timing`](https://www.w3.org/TR/server-timing/#the-server-timing-header-field) header string value.
30 *
31 * @example
32 * ```
33 * stringify([{ name: "missedCache" }])
34 * // missedCache
35 * stringify([{ name: "cpu", duration: 2.4 }])
36 * // cpu;dur=2.4
37 * stringify([{ name: "cache", description: "Cache Read", duration: 23.2 }])
38 * // cache;desc="Cache Read";dur=23.2
39 * stringify([{ name: "db", duration: 53 }, { name: "app", duration: 47.2 }])
40 * // db;dur=53, app;dur=47.2
41 * ```
42 * @param {ServerTimings} timings Timings object to stringify.
43 * @returns Server-Timings header value as string.
44 */
45declare const stringify: (timings: ServerTimings) => string;
46
47export { ServerTimings, parse, stringify };