-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
79 lines (69 loc) · 1.93 KB
/
mod.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
/*!
* Based on https://github.com/jshttp/fresh/blob/master/index.js
* Copyright(c) 2012 TJ Holowaychuk
* Copyright(c) 2016-2017 Douglas Christopher Wilson
* Copyright(c) 2020 Christian Norrman
* MIT Licensed
*/
/**
* Regex to check if no-cache exists in a Cache-Control header.
* @constant
* @private
*/
const CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/;
/**
* Regex to sanitize ETags within a If-None-Match header.
* @constant
* @private
*/
const NONE_MATCH_SANITIZE_REGEXP = /(W\/| )/g;
/**
* Check freshness of the response using request and response headers.
* @param {Headers} reqHeaders Server request headers object
* @param {Headers} resHeaders Server response headers object
*
* @returns {boolean}
*/
export default function fresh(
reqHeaders: Headers,
resHeaders: Headers,
): boolean {
const modifiedSince = reqHeaders.get("If-Modified-Since");
const noneMatch = reqHeaders.get("If-None-Match");
// unconditional request
if (!modifiedSince && !noneMatch) {
return false;
}
// If-None-Match
if (noneMatch && noneMatch !== "*") {
let etag = resHeaders.get("ETag");
if (!etag) {
return false;
} else if (etag.startsWith("W/")) {
etag = etag.substring(2);
}
const etags = noneMatch
.replace(NONE_MATCH_SANITIZE_REGEXP, "")
.split(",");
if (!etags.includes(etag)) {
return false;
}
}
// If-Modified-Since
if (modifiedSince) {
const lastModified = resHeaders.get("Last-Modified");
if (
!lastModified || !(Date.parse(lastModified) <= Date.parse(modifiedSince))
) {
return false;
}
}
// Always return stale when Cache-Control: no-cache
// to support end-to-end reload requests
// https://tools.ietf.org/html/rfc2616#section-14.9.4
const cacheControl = reqHeaders.get("Cache-Control");
if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) {
return false;
}
return true;
}