2
2
3
3
const util = require ( '../core/util' )
4
4
const DecoratorHandler = require ( '../handler/decorator-handler' )
5
- const { parseCacheControlHeader, parseVaryHeader, UNSAFE_METHODS , assertCacheStoreType } = require ( '../util/cache' )
5
+ const {
6
+ assertCacheStoreType,
7
+ parseCacheControlHeader,
8
+ parseVaryHeader,
9
+ UNSAFE_METHODS
10
+ } = require ( '../util/cache' )
6
11
7
12
/**
8
13
* Writes a response to a CacheStore and then passes it on to the next handler
9
14
*/
10
15
class CacheHandler extends DecoratorHandler {
11
16
/**
12
- * @type {import('../../types/cache-interceptor.d.ts').default.CacheOptions | null }
13
- */
14
- #opts = null
17
+ * @type {import('../../types/cache-interceptor.d.ts').default.CacheStore }
18
+ */
19
+ #store
20
+
15
21
/**
16
- * @type {import('../../types/dispatcher.d.ts').default.RequestOptions | null }
22
+ * @type {import('../../types/dispatcher.d.ts').default.RequestOptions }
17
23
*/
18
- #req = null
24
+ #requestOptions
25
+
19
26
/**
20
- * @type {import('../../types/dispatcher.d.ts').default.DispatchHandlers | null }
27
+ * @type {import('../../types/dispatcher.d.ts').default.DispatchHandlers }
21
28
*/
22
- #handler = null
29
+ #handler
30
+
23
31
/**
24
32
* @type {import('../../types/cache-interceptor.d.ts').default.CacheStoreWriteable | undefined }
25
33
*/
26
34
#writeStream
27
35
28
36
/**
29
37
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions } opts
30
- * @param {import('../../types/dispatcher.d.ts').default.RequestOptions } req
38
+ * @param {import('../../types/dispatcher.d.ts').default.RequestOptions } requestOptions
31
39
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers } handler
32
40
*/
33
- constructor ( opts , req , handler ) {
41
+ constructor ( opts , requestOptions , handler ) {
34
42
super ( handler )
35
43
36
44
if ( typeof opts !== 'object' ) {
37
45
throw new TypeError ( `expected opts to be an object, got type ${ typeof opts } ` )
38
46
}
39
47
40
- assertCacheStoreType ( opts . store )
48
+ const { store } = opts
49
+
50
+ assertCacheStoreType ( store )
41
51
42
- if ( typeof req !== 'object' ) {
52
+ if ( typeof requestOptions !== 'object' ) {
43
53
throw new TypeError ( `expected req to be an object, got type ${ typeof opts } ` )
44
54
}
45
55
46
56
if ( typeof handler !== 'object' ) {
47
57
throw new TypeError ( `expected handler to be an object, got type ${ typeof opts } ` )
48
58
}
49
59
50
- this . #opts = opts
51
- this . #req = req
60
+ this . #store = store
61
+ this . #requestOptions = requestOptions
52
62
this . #handler = handler
53
63
}
54
64
@@ -75,14 +85,14 @@ class CacheHandler extends DecoratorHandler {
75
85
)
76
86
77
87
if (
78
- UNSAFE_METHODS . includes ( this . #req . method ) &&
88
+ UNSAFE_METHODS . includes ( this . #requestOptions . method ) &&
79
89
statusCode >= 200 &&
80
90
statusCode <= 399
81
91
) {
82
92
// https://www.rfc-editor.org/rfc/rfc9111.html#name-invalidating-stored-respons
83
93
// Try/catch for if it's synchronous
84
94
try {
85
- const result = this . #opts . store . deleteByOrigin ( this . #req . origin )
95
+ const result = this . #store. deleteByOrigin ( this . #requestOptions . origin )
86
96
if (
87
97
result &&
88
98
typeof result . catch === 'function' &&
@@ -103,7 +113,7 @@ class CacheHandler extends DecoratorHandler {
103
113
const cacheControlHeader = headers [ 'cache-control' ]
104
114
const contentLengthHeader = headers [ 'content-length' ]
105
115
106
- if ( ! cacheControlHeader || ! contentLengthHeader || this . #opts . store . isFull ) {
116
+ if ( ! cacheControlHeader || ! contentLengthHeader || this . #store. isFull ) {
107
117
// Don't have the headers we need, can't cache
108
118
return downstreamOnHeaders ( )
109
119
}
@@ -122,7 +132,7 @@ class CacheHandler extends DecoratorHandler {
122
132
const staleAt = determineStaleAt ( now , headers , cacheControlDirectives )
123
133
if ( staleAt ) {
124
134
const varyDirectives = headers . vary
125
- ? parseVaryHeader ( headers . vary , this . #req . headers )
135
+ ? parseVaryHeader ( headers . vary , this . #requestOptions . headers )
126
136
: undefined
127
137
const deleteAt = determineDeleteAt ( now , cacheControlDirectives , staleAt )
128
138
@@ -132,7 +142,7 @@ class CacheHandler extends DecoratorHandler {
132
142
cacheControlDirectives
133
143
)
134
144
135
- this . #writeStream = this . #opts . store . createWriteStream ( this . #req , {
145
+ this . #writeStream = this . #store. createWriteStream ( this . #requestOptions , {
136
146
statusCode,
137
147
statusMessage,
138
148
rawHeaders : strippedHeaders ,
0 commit comments