-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparse.ts
99 lines (77 loc) · 2.22 KB
/
parse.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
import type { CacheControl } from './index';
import { cacheControlSymbol } from './internal';
import { isDuration, isTruthy, parseRawHeaders } from './util';
const number = Number;
/**
* Parses the Cache-Control header.
*
* You can check if a object was returned by this function with {@link isCacheControl} .
*
* @param {string} Header The header to parse
* @returns {CacheControl} The parsed cache control header
*/
export function parse(headerStr?: string): CacheControl {
const header: CacheControl = Object.defineProperty({}, cacheControlSymbol, {
enumerable: false,
value: 1
});
if (!headerStr || typeof headerStr !== 'string') {
return header;
}
const headers = parseRawHeaders(headerStr);
const maxAge = headers['max-age'];
const maxStale = headers['max-stale'];
const minFresh = headers['min-fresh'];
const sMaxAge = headers['s-maxage'];
const staleIfError = headers['stale-if-error'];
const staleWhileRevalidate = headers['stale-while-revalidate'];
if (isTruthy(headers.immutable)) {
header.immutable = true;
}
if (isDuration(maxAge)) {
header.maxAge = number(maxAge);
}
if (isDuration(maxStale)) {
header.maxStale = number(maxStale);
}
if (isDuration(minFresh)) {
header.minFresh = number(minFresh);
}
if (isTruthy(headers['must-revalidate'])) {
header.mustRevalidate = true;
}
if (isTruthy(headers['must-understand'])) {
header.mustUnderstand = true;
}
if (isTruthy(headers['no-cache'])) {
header.noCache = true;
}
if (isTruthy(headers['no-store'])) {
header.noStore = true;
}
if (isTruthy(headers['no-transform'])) {
header.noTransform = true;
}
if (isTruthy(headers['only-if-cached'])) {
header.onlyIfCached = true;
}
if (isTruthy(headers.private)) {
header.private = true;
}
if (isTruthy(headers['proxy-revalidate'])) {
header.proxyRevalidate = true;
}
if (isTruthy(headers.public)) {
header.public = true;
}
if (isDuration(sMaxAge)) {
header.sMaxAge = number(sMaxAge);
}
if (isDuration(staleIfError)) {
header.staleIfError = number(staleIfError);
}
if (isDuration(staleWhileRevalidate)) {
header.staleWhileRevalidate = number(staleWhileRevalidate);
}
return header;
}