Setting the file. One moment.
Display Labels · Vercel Optimize · vercel-labs/agent-skills · Skills Docs
ContentsBack to the top of the page This file
Number 7.85
Position 85 of 155
Type JavaScript
Size 7 KB
Lines 185 lib/ display-labels.mjs
JavaScript · 185 lines · 7 KB
[
'cache_header_gap'
,
'Missing cache headers'
],
9 [ 'image_optimization' , 'Image optimization' ],
10 [ 'external_api_slow' , 'Slow external API' ],
11 [ 'isr_overrevalidation' , 'ISR over-revalidation' ],
12 [ 'middleware_heavy' , 'Heavy middleware' ],
13 [ 'cwv_poor' , 'Poor Core Web Vitals' ],
14 [ 'platform_fluid_compute' , 'Fluid Compute usage' ],
15 [ 'platform_bot_protection' , 'Bot traffic' ],
16 [ 'rendering_candidate' , 'Rendering opportunity' ],
17 [ 'missing_cache_headers' , 'Missing cache headers' ],
18 [ 'max_age_without_s_maxage' , 'Browser-only cache header' ],
19 [ 'force_dynamic' , 'Forced dynamic rendering' ],
20 [ 'headers_in_page' , 'Dynamic API in page' ],
21 [ 'unoptimized_image' , 'Image optimization gap' ],
22 [ 'large_static_asset' , 'Large static asset' ],
23 [ 'source_maps_production' , 'Production source maps' ],
24 [ 'edge_heavy_import' , 'Heavy Edge import' ],
25 ]);
26
27 const SIGNAL_LABELS = new Map ([
28 [ 'inv' , 'function invocations' ],
29 [ 'runs' , 'function invocations' ],
30 [ 'middleware_inv' , 'middleware invocations' ],
31 [ 'total_req' , 'total requests' ],
32 [ 'requests' , 'requests' ],
33 [ 'p95' , '95th percentile duration' ],
34 [ 'p75' , '75th percentile duration' ],
35 [ '5xx' , '5xx error rate' ],
36 [ 'errs' , '5xx errors' ],
37 [ 'rate' , '5xx error rate' ],
38 [ 'cache' , 'cache hit rate' ],
39 [ 'get' , 'GET request share' ],
40 [ 'cold' , 'cold start rate' ],
41 [ 'writes' , 'ISR write units' ],
42 [ 'reads' , 'ISR read units' ],
43 [ 'w/r' , 'ISR writes per read' ],
44 [ 'ratio' , 'ratio' ],
45 [ 'host' , 'host' ],
46 [ 'calls' , 'external API calls' ],
47 [ 'edge_cost' , 'Edge Request cost units' ],
48 [ 'bot_protection' , 'Bot Protection' ],
49 [ 'bot_fdt_pct' , 'bot Fast Data Transfer share' ],
50 [ 'LCP' , 'Largest Contentful Paint (LCP)' ],
51 [ 'INP' , 'Interaction to Next Paint (INP)' ],
52 [ 'CLS' , 'Cumulative Layout Shift (CLS)' ],
53 ]);
54
55 const REQUEST_COUNT_KINDS = new Set ([
56 'uncached_route' ,
57 ]);
58
59 const PUBLIC_ASSIGNMENT_LABELS = new Map ([
60 ... SIGNAL_LABELS ,
61 [ 'deepDive.latency.p95' , 'deepDive latency p95' ],
62 [ 'deepDive.cpu.p95' , 'deepDive CPU p95' ],
63 [ 'deepDive.ttfb.p95' , 'deepDive TTFB p95' ],
64 [ 'cpu.p95' , 'CPU p95' ],
65 [ 'latency.p95' , 'latency p95' ],
66 [ 'ttfb.p95' , 'TTFB p95' ],
67 [ 'cache_result' , 'cache result' ],
68 [ 'http_status' , 'HTTP status' ],
69 [ 'error_code' , 'error code' ],
70 [ 'status' , 'status' ],
71 [ 'count' , 'count' ],
72 ]);
73
74 export function formatKind ( kind ) {
75 if ( ! kind) return 'Candidate' ;
76 if ( KIND_LABELS . has (kind)) return KIND_LABELS . get (kind);
77 return String (kind)
78 . split ( / [_-] + / g )
79 . filter (Boolean)
80 . map (( part ) => part. charAt ( 0 ). toUpperCase () + part. slice ( 1 ))
81 . join ( ' ' ) || 'Candidate' ;
82 }
83
84 export function formatRoute ( candidate ) {
85 const route = candidate?.displayRoute ?? candidate?.route ?? candidate?.hostname ?? null ;
86 if (route) return String ( canonicalizeRoute (route));
87 if (Array. isArray (candidate?.files) && candidate.files. length > 0 ) return candidate.files[ 0 ];
88 return 'account-wide' ;
89 }
90
91 export function formatSignal ( signal , context = {}) {
92 if ( typeof signal !== 'string' || signal. trim () === '' ) return 'no signal recorded' ;
93
94 const parts = signal
95 . split ( ',' )
96 . map (( part ) => part. trim ())
97 . filter (Boolean)
98 . map (( part ) => formatSignalPart (part, context));
99
100 return parts. length > 0 ? parts. join ( '; ' ) : signal;
101 }
102
103 export function formatPublicText ( value ) {
104 if (value == null ) return '' ;
105 return normalizeObservedWindowUnits ( String (value))
106 . replace ( / \b o11y \b / gi , 'observability' )
107 . replace ( / \b cache [- ] components gotcha \b / gi , 'Cache Components edge case' )
108 . replace ( / \b cache_result \b (?! \s * =)/ g , 'cache result' )
109 . replace ( / \b http_status \b (?! \s * =)/ g , 'HTTP status' )
110 . replace ( / \b error_code \b (?! \s * =)/ g , 'error code' )
111 . replace ( /,(?= \s * ( [A-Za-z0-9][\w./-] * )=)/ g , ( match , key ) =>
112 PUBLIC_ASSIGNMENT_LABELS . has (key) ? '; ' : match
113 )
114 . replace ( / \b ( [A-Za-z0-9][\w./-] * )=( [ ^ ,;\s] + )/ g , ( match , key , rawValue ) => {
115 const label = PUBLIC_ASSIGNMENT_LABELS . get (key);
116 if ( ! label) return match;
117 return `${ label }: ${ formatSignalValue ( key , rawValue ) }` ;
118 })
119 . replace ( / \b (cache breakdown [ ^ .!?\n;] {0,160}? ) \b (?:function \s + ) ? invocations \b / gi , ( match , prefix ) =>
120 / \b status distribution \b / i . test (prefix) ? match : `${ prefix }requests`
121 )
122 . replace ( / \b (cache breakdown [ ^ .!?\n;] {0,220}?\b out of \s + [\d,] + ) \s + invocations \b / gi , '$1 requests' )
123 . replace ( / \b (cache hits over \s + [\d,.] + (?: \s ? (?:K | M | B)) ? ) \s + invocations \b / gi , '$1 requests' )
124 . replace ( / \b (?:function \s + ) ? invocations \b ( [ ^ .!?\n;] {0,120}\b (?:empty \s + ) ? cache result(?: label) ?\b )/ gi , 'requests$1' );
125 }
126
127 export function normalizeObservedWindowUnits ( value ) {
128 if (value == null ) return '' ;
129 return String (value)
130 . replace ( /(?<! \$ ) \b ( \d[\d,.] * (?: \s ? (?:K | M | B | KB | MB | GB | TB)) ? ) \/ mo \b / gi , '$1/window' )
131 . replace ( / \b monthly \s + function \s + invocations \b / gi , 'function invocations/window' )
132 . replace ( / \b (requests ?| invocations ?| GETs | bytes | egress | bandwidth | writes ?| reads ?| errors ? ) \/ mo \b / gi , '$1/window' )
133 . replace ( / \b monthly \s + (requests ?| invocations ?| GETs | bytes | egress | bandwidth | writes ?| reads ?| errors ? ) \b / gi , '$1/window' )
134 . replace ( / \b ( \d[\d,.] * (?: \s ? (?:K | M | B | KB | MB | GB | TB)) ? ) \s + function \s + invocations \s + per month \b / gi , '$1 function invocations/window' )
135 . replace ( / \b ( \d[\d,.] * (?: \s ? (?:K | M | B | KB | MB | GB | TB)) ? ) \s + (requests ?| GETs | invocations ?| bytes | writes ?| reads ?| errors ? ) \s + per month \b / gi , '$1 $2/window' )
136 . replace ( / \b ( \d[\d,.] * (?: \s ? (?:K | M | B | KB | MB | GB | TB)) ? ) \/ window \s + (requests ?| GETs | (?:function \s + ) ? invocations ?| bytes | egress | bandwidth | writes ?| reads ?| errors ? ) \b / gi , '$1 $2 in this window' )
137 . replace ( / \b ( \d[\d,.] * (?: \s ? (?:K | M | B | KB | MB | GB | TB)) ? ) \s + (requests ?| GETs | (?:function \s + ) ? invocations ?| bytes | egress | bandwidth | writes ?| reads ?| errors ? ) \/ window \b / gi , '$1 $2 in this window' )
138 . replace ( / \b ( \d[\d,.] * (?: \s ? (?:K | M | B | KB | MB | GB | TB)) ? ) \/ window \b / gi , '$1 in this window' )
139 . replace ( / \b (requests ?| invocations ?| GETs | bytes | egress | bandwidth | writes ?| reads ?| errors ? ) \/ window \b / gi , '$1 in this window' );
140 }
141
142 export function formatCandidateLine ( candidate ) {
143 return `${ formatKind ( candidate ?. kind ) } on ${ formatRoute ( candidate ) } - ${ formatSignal ( candidate ?. o11ySignal , candidate ) }` ;
144 }
145
146 export function formatCandidateLabel ( candidate ) {
147 return `${ formatKind ( candidate ?. kind ) } on ${ formatRoute ( candidate ) }` ;
148 }
149
150 function formatSignalPart ( part , context = {}) {
151 const eq = part. indexOf ( '=' );
152 if (eq === - 1 ) return part;
153
154 const key = part. slice ( 0 , eq). trim ();
155 const value = part. slice (eq + 1 ). trim ();
156 const label = signalLabel (key, context);
157 return `${ label }: ${ formatSignalValue ( key , value ) }` ;
158 }
159
160 function signalLabel ( key , context = {}) {
161 const kind = typeof context === 'string' ? context : context?.kind;
162 if (key === 'inv' && REQUEST_COUNT_KINDS . has (kind)) return 'requests' ;
163 return SIGNAL_LABELS . get (key) ?? humanizeKey (key);
164 }
165
166 function humanizeKey ( key ) {
167 return String (key)
168 . replaceAll ( '.' , ' ' )
169 . replaceAll ( '_' , ' ' )
170 . replaceAll ( '-' , ' ' )
171 . trim ();
172 }
173
174 function formatSignalValue ( key , value ) {
175 if (key === 'inv' || key === 'runs' || key === 'middleware_inv' || key === 'total_req' || key === 'requests' || key === 'calls' || key === 'errs' || key === 'writes' || key === 'reads' ) {
176 return formatNumberLike (value);
177 }
178 return value;
179 }
180
181 function formatNumberLike ( value ) {
182 const n = Number (value);
183 if ( ! Number. isFinite (n)) return value;
184 return new Intl. NumberFormat ( 'en-US' , { maximumFractionDigits: 2 }). format (n);
185 }