-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathhttp-cache.js
More file actions
44 lines (35 loc) · 870 Bytes
/
Copy pathhttp-cache.js
File metadata and controls
44 lines (35 loc) · 870 Bytes
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
function parseHttpDate(value) {
if (!value) {
return null;
}
const time = Date.parse(value);
return Number.isFinite(time) ? time : null;
}
function normalizeEtag(etag) {
return String(etag || "").trim();
}
function etagMatches(ifNoneMatch, etag) {
if (!ifNoneMatch) {
return false;
}
if (ifNoneMatch.trim() === "*") {
return true;
}
return ifNoneMatch
.split(",")
.map(normalizeEtag)
.includes(normalizeEtag(etag));
}
function shouldReturnNotModified(headers, etag, modifiedTimeMs) {
if (etagMatches(headers["if-none-match"], etag)) {
return true;
}
const ifModifiedSince = parseHttpDate(headers["if-modified-since"]);
if (ifModifiedSince === null) {
return false;
}
return Math.floor(modifiedTimeMs / 1000) <= Math.floor(ifModifiedSince / 1000);
}
module.exports = {
shouldReturnNotModified
};