7
7
parseVaryHeader
8
8
} = require ( '../util/cache' )
9
9
10
+ function noop ( ) { }
11
+
10
12
/**
11
13
* Writes a response to a CacheStore and then passes it on to the next handler
12
14
*/
@@ -46,6 +48,17 @@ class CacheHandler extends DecoratorHandler {
46
48
this . #handler = handler
47
49
}
48
50
51
+ onConnect ( abort ) {
52
+ if ( this . #writeStream) {
53
+ this . #writeStream. destroy ( )
54
+ this . #writeStream = undefined
55
+ }
56
+
57
+ if ( typeof this . #handler. onConnect === 'function' ) {
58
+ this . #handler. onConnect ( abort )
59
+ }
60
+ }
61
+
49
62
/**
50
63
* @see {DispatchHandlers.onHeaders}
51
64
*
@@ -61,49 +74,46 @@ class CacheHandler extends DecoratorHandler {
61
74
resume ,
62
75
statusMessage
63
76
) {
64
- const downstreamOnHeaders = ( ) => this . #handler. onHeaders (
65
- statusCode ,
66
- rawHeaders ,
67
- resume ,
68
- statusMessage
69
- )
77
+ const downstreamOnHeaders = ( ) => {
78
+ if ( typeof this . #handler. onHeaders === 'function' ) {
79
+ return this . #handler. onHeaders (
80
+ statusCode ,
81
+ rawHeaders ,
82
+ resume ,
83
+ statusMessage
84
+ )
85
+ } else {
86
+ return true
87
+ }
88
+ }
70
89
71
90
if (
72
91
! util . safeHTTPMethods . includes ( this . #requestOptions. method ) &&
73
92
statusCode >= 200 &&
74
93
statusCode <= 399
75
94
) {
76
- // https://www.rfc-editor.org/rfc/rfc9111.html#name-invalidating-stored-respons
77
- // Try/catch for if it's synchronous
95
+ // https://www.rfc-editor.org/rfc/rfc9111.html#name-invalidating-stored-response
78
96
try {
79
- const result = this . #store. deleteByOrigin ( this . #requestOptions. origin )
80
- if (
81
- result &&
82
- typeof result . catch === 'function' &&
83
- typeof this . #handler. onError === 'function'
84
- ) {
85
- // Fail silently
86
- result . catch ( _ => { } )
87
- }
88
- } catch ( err ) {
97
+ this . #store. deleteByOrigin ( this . #requestOptions. origin . toString ( ) ) ?. catch ?. ( noop )
98
+ } catch {
89
99
// Fail silently
90
100
}
91
-
92
101
return downstreamOnHeaders ( )
93
102
}
94
103
95
104
const headers = util . parseHeaders ( rawHeaders )
96
105
97
106
const cacheControlHeader = headers [ 'cache-control' ]
98
- const contentLengthHeader = headers [ 'content-length' ]
99
-
100
- if ( ! cacheControlHeader || ! contentLengthHeader || this . #store. isFull ) {
101
- // Don't have the headers we need, can't cache
107
+ if ( ! cacheControlHeader || typeof cacheControlHeader !== 'string' ) {
108
+ // Don't have cache-control, can't cache.
102
109
return downstreamOnHeaders ( )
103
110
}
104
111
105
- const contentLength = Number ( contentLengthHeader )
112
+ const contentLengthHeader = headers [ 'content-length' ]
113
+ const contentLength = contentLengthHeader ? Number ( contentLengthHeader ) : null
106
114
if ( ! Number . isInteger ( contentLength ) ) {
115
+ // Don't know the final size, don't cache.
116
+ // TODO (fix): Why not cache?
107
117
return downstreamOnHeaders ( )
108
118
}
109
119
@@ -137,18 +147,24 @@ class CacheHandler extends DecoratorHandler {
137
147
} )
138
148
139
149
if ( this . #writeStream) {
140
- this . #writeStream. on ( 'drain' , resume )
141
- this . #writeStream. on ( 'error' , ( ) => {
142
- this . #writeStream = undefined
143
- resume ( )
144
- } )
150
+ const handler = this
151
+ this . #writeStream
152
+ . on ( 'drain' , resume )
153
+ . on ( 'error' , function ( ) {
154
+ // TODO (fix): Make error somehow observable?
155
+ } )
156
+ . on ( 'close' , function ( ) {
157
+ if ( handler . #writeStream === this ) {
158
+ handler . #writeStream = undefined
159
+ }
160
+
161
+ // TODO (fix): Should we resume even if was paused downstream?
162
+ resume ( )
163
+ } )
145
164
}
146
165
}
147
166
148
- if ( typeof this . #handler. onHeaders === 'function' ) {
149
- return downstreamOnHeaders ( )
150
- }
151
- return false
167
+ return downstreamOnHeaders ( )
152
168
}
153
169
154
170
/**
@@ -178,10 +194,7 @@ class CacheHandler extends DecoratorHandler {
178
194
*/
179
195
onComplete ( rawTrailers ) {
180
196
if ( this . #writeStream) {
181
- if ( rawTrailers ) {
182
- this . #writeStream. rawTrailers = rawTrailers
183
- }
184
-
197
+ this . #writeStream. rawTrailers = rawTrailers ?? [ ]
185
198
this . #writeStream. end ( )
186
199
}
187
200
@@ -332,7 +345,7 @@ function stripNecessaryHeaders (rawHeaders, parsedHeaders, cacheControlDirective
332
345
for ( let i = 0 ; i < headerNames . length ; i ++ ) {
333
346
const header = headerNames [ i ]
334
347
335
- if ( headersToRemove . indexOf ( header ) !== - 1 ) {
348
+ if ( headersToRemove . includes ( header ) ) {
336
349
// We have a at least one header we want to remove
337
350
if ( ! strippedHeaders ) {
338
351
// This is the first header we want to remove, let's create the object
0 commit comments