@@ -62,10 +62,13 @@ module.exports = (opts = {}) => {
6262 }
6363
6464 /**
65- * @param {import('node:stream').Readable } stream
66- * @param {import('../../types/cache-interceptor.d.ts').default.CachedResponse } value
65+ * @param {import('../../types/cache-interceptor.d.ts').default.GetResult } result
6766 */
68- const respondWithCachedValue = ( stream , { cachedAt, rawHeaders, statusCode, statusMessage } ) => {
67+ const respondWithCachedValue = ( { cachedAt, rawHeaders, statusCode, statusMessage, body } ) => {
68+ const stream = util . isStream ( body )
69+ ? body
70+ : Readable . from ( body ?? [ ] )
71+
6972 assert ( ! stream . destroyed , 'stream should not be destroyed' )
7073 assert ( ! stream . readableDidRead , 'stream should not be readableDidRead' )
7174
@@ -122,26 +125,21 @@ module.exports = (opts = {}) => {
122125 /**
123126 * @param {import('../../types/cache-interceptor.d.ts').default.GetResult } result
124127 */
125- const handleStream = ( result ) => {
126- const { response : value , body } = result
127-
128+ const handleResult = ( result ) => {
128129 // TODO (perf): Readable.from path can be optimized...
129- const stream = util . isStream ( body )
130- ? body
131- : Readable . from ( body ?? [ ] )
132130
133- if ( ! stream && opts . method !== 'HEAD' ) {
131+ if ( ! result . body && opts . method !== 'HEAD' ) {
134132 throw new Error ( 'stream is undefined but method isn\'t HEAD' )
135133 }
136134
137135 // Check if the response is stale
138136 const now = Date . now ( )
139- if ( now < value . staleAt ) {
137+ if ( now < result . staleAt ) {
140138 // Dump request body.
141139 if ( util . isStream ( opts . body ) ) {
142140 opts . body . on ( 'error' , ( ) => { } ) . destroy ( )
143141 }
144- respondWithCachedValue ( stream , value )
142+ respondWithCachedValue ( result )
145143 } else if ( util . isStream ( opts . body ) && util . bodyLength ( opts . body ) !== 0 ) {
146144 // If body is is stream we can't revalidate...
147145 // TODO (fix): This could be less strict...
@@ -153,15 +151,15 @@ module.exports = (opts = {}) => {
153151 ...opts ,
154152 headers : {
155153 ...opts . headers ,
156- 'if-modified-since' : new Date ( value . cachedAt ) . toUTCString ( )
154+ 'if-modified-since' : new Date ( result . cachedAt ) . toUTCString ( )
157155 }
158156 } ,
159157 new CacheRevalidationHandler (
160158 ( success ) => {
161159 if ( success ) {
162- respondWithCachedValue ( stream , value )
163- } else {
164- stream . on ( 'error' , ( ) => { } ) . destroy ( )
160+ respondWithCachedValue ( result )
161+ } else if ( util . isStream ( result . body ) ) {
162+ result . body . on ( 'error' , ( ) => { } ) . destroy ( )
165163 }
166164 } ,
167165 new CacheHandler ( globalOpts , cacheKey , handler )
@@ -175,7 +173,7 @@ module.exports = (opts = {}) => {
175173 if ( ! result ) {
176174 dispatch ( opts , new CacheHandler ( globalOpts , cacheKey , handler ) )
177175 } else {
178- handleStream ( result )
176+ handleResult ( result )
179177 }
180178 } , err => {
181179 if ( typeof handler . onError === 'function' ) {
@@ -185,7 +183,7 @@ module.exports = (opts = {}) => {
185183 }
186184 } )
187185 } else {
188- handleStream ( result )
186+ handleResult ( result )
189187 }
190188
191189 return true
0 commit comments