-
Notifications
You must be signed in to change notification settings - Fork 648
/
hint.ts
539 lines (428 loc) · 18.6 KB
/
hint.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**
* @fileoverview Checks if your cache-control header and asset strategy follows best practices
*/
import { debug as d } from '@hint/utils-debug';
import { isDataURI, normalizeHeaderValue } from '@hint/utils-network';
import { IHint, FetchEnd } from 'hint/dist/src/lib/types';
import { HintContext } from 'hint/dist/src/lib/hint-context';
import meta from './meta';
import { getMessage } from './i18n.import';
import { Severity } from '@hint/utils-types';
const debug = d(__filename);
type TargetType = 'fetch' | 'html';
type Directives = Map<string, number | null>;
type ParsedDirectives = {
header: string;
invalidDirectives: Map<string, string | null>;
invalidValues: Map<string, string>;
usedDirectives: Directives;
};
export default class HttpCacheHint implements IHint {
public static readonly meta = meta;
public constructor(context: HintContext) {
const immutableEdgeVersions = [
'edge 15',
'edge 16',
'edge 17',
'edge 18'
];
/**
* https://caniuse.com/#search=immutable shows as starting:
* * Firefox 49
* * Edge 15 (supports stops with Chromium based one)
* * Safari 11 (destkop and iOS)
*
* Firefox and Safari versions are old enough that we can assume
* support for any version
*/
const immutableSupported = context.targetedBrowsers.some((browser) => {
if (immutableEdgeVersions.includes(browser)) {
return true;
}
if (browser.startsWith('firefox') || browser.includes('safari')) {
return true;
}
return false;
});
/**
* Max time the HTML of a page can be cached.
* https://jakearchibald.com/2016/caching-best-practices/#used-carefully-max-age-mutable-content-can-be-beneficial
*/
const maxAgeTarget = context.hintOptions && context.hintOptions.maxAgeTarget || 180; // 3 minutes
/** Max time a resource (CSS, JS, images, etc.) can be cached.*/
const maxAgeResource = context.hintOptions && context.hintOptions.maxAgeResource || 31536000; // 1 year
/** Resources' mediaType that should be cached for a long time.*/
const longCached = [
'application/manifest+json',
'audio/ogg',
'audio/mpeg',
'audio/mp4',
'font/collection',
'application/vnd.ms-fontobject',
'font/opentype',
'font/otf',
'font/ttf',
'font/woff',
'font/woff2',
'image/bmp',
'image/gif',
'image/jpeg',
'image/png',
'image/svg+xml',
'image/webp',
'image/x-icon',
'text/css',
'text/javascript',
'video/mp4',
'video/ogg',
'video/webm'];
/** The predefined patterns for file revving.*/
const predefinedRevvingPatterns: RegExp[] = [
/*
* E.g.: version/timestamp/hash
*
* Live example: https://regex101.com/r/KDPUtH/
*/
/*
* - https://example.com/assets/jquery-2.1.1.js
* - https://example.com/assets/jquery-2.1.1.min.js
* - https://example.com/assets/jquery-3.0.0-beta.js
* - https://example.com/assets/favicon.123.ico
* - https://example.com/wp-content/uploads/fvm/out/header-cb050ccd-1524626949.min.js
*/
/\/[^/]+[._-]v?\d+(\.\d+(\.\d+)?)?[^/]*\.\w+$/i,
/*
* - https://cdn.example.com/jquery.lazy/1.6.5/jquery.lazy.min.js
*/
/\/v?\d+\.\d+\.\d+.*?\//i,
/*
* - https://example.com/site/javascript/v5/jquery.cookie.js
* - https://static.xx.fbcdn.net/rsrc.php/v3iJhv4/yG/l/en_US/sqNNamBywvN.js
*/
/\/v\d.*?\//i,
/*
* - https://example.com/assets/unicorn-d41d8cd98f.css
* - https://example.com/assets/app.e1c7a.bundle.js
* - https://example.com/assets/9f61f58dd1cc3bb82182.bundle.js
* - https://example.com/assets/9f61f.js
* - https://example.com/assets/9f61f.min.js
*/
/\/([^/]+[._-])?([0-9a-f]{5,})([._-].*?)?\.\w+$/i
];
/** The cache revving patterns to use for matching.*/
let cacheRevvingPatterns: RegExp[] = [];
/**
* Parses the `Cache-Control` header of a response creating an object with valid and invalid directives,
* as well as invalid values.
*/
const parseCacheControlHeader = (cacheControlHeader: string): ParsedDirectives => {
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
const directives = ['must-revalidate', 'no-cache', 'no-store', 'no-transform', 'public', 'private', 'proxy-revalidate'];
const valueDirectives = ['max-age', 's-maxage'];
const extensionDirectives = ['immutable', 'stale-while-revalidate', 'stale-if-error'];
const usedDirectives = cacheControlHeader.split(',').map((value) => {
return value.trim();
});
const parsedCacheControlHeader = usedDirectives.reduce((parsed: ParsedDirectives, current: string) => {
const [directive, value] = current.split('=');
// Empty string, e.g. `cache-control: max-age=12345,` --> `['max-age=123456', '']`
if (!directive) {
return parsed;
}
// Validate directive with value. E.g.: max-age=<seconds>
if (directive && value) {
/*
* Check if the directive has a value when it shouldn't
* E.g.: no-cache=12345
*/
if (!valueDirectives.includes(directive)) {
parsed.invalidValues.set(directive, value);
return parsed;
}
/*
* Check if the directive has the right value (positive integer)
* E.g.: max-age=3600
*/
const seconds = parseFloat(value);
if (!value || isNaN(seconds) || !Number.isInteger(seconds) || seconds < 0) {
parsed.invalidValues.set(directive, value);
return parsed;
}
parsed.usedDirectives.set(directive, seconds);
return parsed;
}
/*
* Check the directive is valid
* E.g.: no-cache
*/
if (directives.includes(directive) || extensionDirectives.includes(directive)) {
parsed.usedDirectives.set(directive, null);
} else {
parsed.invalidDirectives.set(directive, null);
}
return parsed;
},
{
header: cacheControlHeader,
invalidDirectives: new Map(),
invalidValues: new Map(),
usedDirectives: new Map<string, number>()
});
return parsedCacheControlHeader;
};
/**
* Transforms a Map of directives to a string with the following format:
*
* directive1=value1
* directive2
* diretive3=value3
*/
const directivesToString = (directives: Map<string, any>) => {
let str = '';
directives.forEach((val, key) => {
if (str.length > 0) {
str += '\n';
}
str += key;
if (val) {
str += `=${val}`;
}
});
return str;
};
/**
* Compares if the `max-age` or `s-maxage` directives of `directives` are smaller (<0),
* equal (0), or bigger (>0) than the given `threshold`.
*/
const compareToMaxAge = (directives: Directives, threshold: number): number => {
const maxAge = directives.get('max-age');
const sMaxAge = directives.get('s-maxage');
if (maxAge) {
return maxAge === threshold ? 0 : maxAge - threshold;
}
if (sMaxAge) {
return sMaxAge === threshold ? 0 : sMaxAge - threshold;
}
return -1;
};
/*
* ------------------------------------------------------------------------------
* Directive validators
* ------------------------------------------------------------------------------
*/
/**
* Prevents agains the usage of non recommended directives (`must-revalidate`)
*/
const nonRecommendedDirectives = (directives: Directives): string | null => {
const noDirectives = ['must-revalidate', 'no-store'];
for (const noDirective of noDirectives) {
if (directives.has(noDirective)) {
return noDirective;
}
}
return null;
};
/*
* Validate if cache-control exists, not having this header is
* an error because it's up to the browser vendor to decide what
* to do.
*/
const hasCacheControl = (directives: ParsedDirectives, fetchEnd: FetchEnd): boolean => {
const { resource, response: { headers } } = fetchEnd;
const cacheControl: string | null = headers && headers['cache-control'] || null;
if (!cacheControl) {
context.report(
resource,
getMessage('noHeaderFound', context.language),
{ severity: Severity.error }
);
return false;
}
return true;
};
/*
* Validates if all the cache-control directives and values are correct.
*/
const hasInvalidDirectives = (directives: ParsedDirectives, fetchEnd: FetchEnd): boolean => {
const { header, invalidDirectives, invalidValues } = directives;
const { resource } = fetchEnd;
const codeSnippet = `Cache-Control: ${header}`;
const codeLanguage = 'http';
if (invalidDirectives.size > 0) {
const message: string = getMessage('directiveInvalid', context.language, [
invalidDirectives.size === 1 ? 'directive' : 'directives',
Array.from(invalidDirectives.keys()).join(', '),
invalidDirectives.size === 1 ? 'is' : 'are'
]);
context.report(
resource,
message,
{ codeLanguage, codeSnippet, severity: Severity.error }
);
return false;
}
if (invalidValues.size > 0) {
const message: string = getMessage('directiveInvalidValue', context.language, [
invalidValues.size === 1 ? 'directive has' : 'directives have',
directivesToString(invalidValues)
]);
context.report(
resource,
message,
{ codeLanguage, codeSnippet, severity: Severity.error }
);
return false;
}
return true;
};
/*
* Validates if there is any non recommended directives.
*/
const hasNoneNonRecommendedDirectives = (directives: ParsedDirectives, fetchEnd: FetchEnd): boolean => {
const { header, usedDirectives } = directives;
const { resource } = fetchEnd;
const nonRecommendedDirective = nonRecommendedDirectives(usedDirectives);
if (nonRecommendedDirective) {
const message: string = getMessage('directiveNotRecomended', context.language, nonRecommendedDirective);
context.report(
resource,
message,
{
codeLanguage: 'http',
codeSnippet: `Cache-Control: ${header}`,
severity: Severity.warning
}
);
return false;
}
return true;
};
/**
* Validates that `no-cache` and `no-store` are not used in combination
* with `max-age` or `s-maxage`.
*/
const validateDirectiveCombinations = (directives: ParsedDirectives, fetchEnd: FetchEnd): boolean => {
const { header, usedDirectives } = directives;
if (usedDirectives.has('no-cache') || usedDirectives.has('no-store')) {
const hasMaxAge = (usedDirectives.has('max-age') || usedDirectives.has('s-maxage'));
if (hasMaxAge) {
const message: string = getMessage('wrongCombination', context.language, header);
context.report(
fetchEnd.resource,
message,
{
codeLanguage: 'http',
codeSnippet: `Cache-Control: ${header}`,
severity: Severity.error
}
);
return false;
}
}
return true;
};
/**
* Validates the target uses no-cache or a small max-age value
*/
const hasSmallCache = (directives: ParsedDirectives, fetchEnd: FetchEnd): boolean => {
const { header, usedDirectives } = directives;
if (usedDirectives.has('no-cache')) {
return true;
}
const isValidCache = compareToMaxAge(usedDirectives, maxAgeTarget) <= 0;
if (!isValidCache) {
const message: string = getMessage('targetShouldNotBeCached', context.language, [maxAgeTarget, header]);
context.report(
fetchEnd.resource,
message,
{
codeLanguage: 'http',
codeSnippet: `Cache-Control: ${header}`,
severity: Severity.warning
}
);
return false;
}
return true;
};
/**
* Validates that a resource (JS, CSS, images, etc.) is using the right file revving format.
*/
const usesFileRevving = (directives: ParsedDirectives, fetchEnd: FetchEnd): boolean => {
const { element, resource } = fetchEnd;
const matches = cacheRevvingPatterns.find((pattern) => {
return !!resource.match(pattern);
});
if (!matches) {
const message: string = getMessage('noCacheBustingPattern', context.language, resource);
context.report(resource, message, { element, severity: Severity.warning });
return false;
}
return true;
};
/**
* Validates that a resource (JS, CSS, images, etc.) has the right caching directives.
*/
const hasLongCache = (directives: ParsedDirectives, fetchEnd: FetchEnd): boolean => {
const { header, usedDirectives } = directives;
const { resource } = fetchEnd;
const codeSnippet = `Cache-Control: ${header}`;
const codeLanguage = 'http';
const longCache = compareToMaxAge(usedDirectives, maxAgeResource) >= 0;
const immutable = usedDirectives.has('immutable');
const isCacheBusted = usesFileRevving(directives, fetchEnd);
let validates = true;
// We want long caches with "immutable" for static resources
if (usedDirectives.has('no-cache') || !longCache) {
const message: string = getMessage('staticResourceCacheValue', context.language, [maxAgeResource, header]);
const severity = isCacheBusted ? Severity.warning : Severity.hint;
context.report(resource, message, { codeLanguage, codeSnippet, severity });
validates = false;
}
if (!immutable) {
const message: string = getMessage('staticNotImmutable', context.language, header);
const severity = immutableSupported && isCacheBusted ? Severity.warning : Severity.hint;
context.report(resource, message, { codeLanguage, codeSnippet, severity });
validates = false;
}
return validates;
};
const validate = (fetchEnd: FetchEnd, eventName: string) => {
const type: TargetType = eventName === 'fetch::end::html' ? 'html' : 'fetch';
const { resource } = fetchEnd;
// This check does not make sense for data URIs.
if (isDataURI(resource)) {
debug(`Check does not apply for data URIs`);
return;
}
const headers = fetchEnd.response.headers;
const { response: { mediaType } } = fetchEnd;
const cacheControlHeaderValue: string = normalizeHeaderValue(headers, 'cache-control', '')!; // won't return null since default value was provided
const parsedDirectives: ParsedDirectives = parseCacheControlHeader(cacheControlHeaderValue);
const validators = [
hasCacheControl,
hasInvalidDirectives,
hasNoneNonRecommendedDirectives,
validateDirectiveCombinations
];
if (type === 'html') {
validators.push(hasSmallCache);
} else if (type === 'fetch' && longCached.includes(mediaType)) {
// Check if there are custom revving patterns
let customRegex: RegExp[] | null = context.hintOptions && context.hintOptions.revvingPatterns || null;
if (customRegex) {
customRegex = customRegex.map((reg) => {
return new RegExp(reg, 'i');
});
}
cacheRevvingPatterns = customRegex || predefinedRevvingPatterns;
validators.push(hasLongCache);
}
validators.every((validator) => {
return validator(parsedDirectives, fetchEnd);
});
return;
};
context.on('fetch::end::*', validate);
}
}