@@ -6,7 +6,12 @@ import http, {ServerResponse} from 'node:http';
6
6
import type { ClientRequest , RequestOptions } from 'node:http' ;
7
7
import type { Socket } from 'node:net' ;
8
8
import timer from '@szmarczak/http-timer' ;
9
- import CacheableRequest from 'cacheable-request' ;
9
+ import CacheableRequest , {
10
+ CacheError as CacheableCacheError ,
11
+ type StorageAdapter ,
12
+ type CacheableRequestFunction ,
13
+ type CacheableOptions ,
14
+ } from 'cacheable-request' ;
10
15
import decompressResponse from 'decompress-response' ;
11
16
import is from '@sindresorhus/is' ;
12
17
import { buffer as getBuffer } from 'get-stream' ;
@@ -124,12 +129,7 @@ export type RequestEvents<T> = {
124
129
off : GotEventFunction < T > ;
125
130
} ;
126
131
127
- export type CacheableRequestFunction = (
128
- options : string | URL | NativeRequestOptions ,
129
- cb ?: ( response : ServerResponse | ResponseLike ) => void
130
- ) => CacheableRequest . Emitter ;
131
-
132
- const cacheableStore = new WeakableMap < string | CacheableRequest . StorageAdapter , CacheableRequestFunction > ( ) ;
132
+ const cacheableStore = new WeakableMap < string | StorageAdapter , CacheableRequestFunction > ( ) ;
133
133
134
134
const redirectCodes : ReadonlySet < number > = new Set ( [ 300 , 301 , 302 , 303 , 304 , 307 , 308 ] ) ;
135
135
@@ -969,9 +969,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
969
969
}
970
970
}
971
971
972
- private _prepareCache ( cache : string | CacheableRequest . StorageAdapter ) {
972
+ private _prepareCache ( cache : string | StorageAdapter ) {
973
973
if ( ! cacheableStore . has ( cache ) ) {
974
- cacheableStore . set ( cache , new CacheableRequest (
974
+ const cacheableRequest = new CacheableRequest (
975
975
( ( requestOptions : RequestOptions , handler ?: ( response : IncomingMessageWithTimings ) => void ) : ClientRequest => {
976
976
const result = ( requestOptions as any ) . _request ( requestOptions , handler ) ;
977
977
@@ -1010,8 +1010,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
1010
1010
1011
1011
return result ;
1012
1012
} ) as typeof http . request ,
1013
- cache as CacheableRequest . StorageAdapter ,
1014
- ) ) ;
1013
+ cache as StorageAdapter ,
1014
+ ) ;
1015
+ cacheableStore . set ( cache , cacheableRequest . request ( ) ) ;
1015
1016
}
1016
1017
}
1017
1018
@@ -1023,7 +1024,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
1023
1024
let request : ClientRequest | Promise < ClientRequest > ;
1024
1025
1025
1026
// TODO: Fix `cacheable-response`. This is ugly.
1026
- const cacheRequest = cacheableStore . get ( ( options as any ) . cache ) ! ( options , async ( response : any ) => {
1027
+ const cacheRequest = cacheableStore . get ( ( options as any ) . cache ) ! ( options as CacheableOptions , async ( response : any ) => {
1027
1028
response . _readableState . autoDestroy = false ;
1028
1029
1029
1030
if ( request ) {
@@ -1109,7 +1110,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
1109
1110
if ( options . cache ) {
1110
1111
( this . _requestOptions as any ) . _request = request ;
1111
1112
( this . _requestOptions as any ) . cache = options . cache ;
1112
- this . _prepareCache ( options . cache as CacheableRequest . StorageAdapter ) ;
1113
+ this . _prepareCache ( options . cache as StorageAdapter ) ;
1113
1114
}
1114
1115
1115
1116
// Cache support
@@ -1145,7 +1146,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
1145
1146
void this . _onResponse ( requestOrResponse as IncomingMessageWithTimings ) ;
1146
1147
}
1147
1148
} catch ( error ) {
1148
- if ( error instanceof CacheableRequest . CacheError ) {
1149
+ if ( error instanceof CacheableCacheError ) {
1149
1150
throw new CacheError ( error , this ) ;
1150
1151
}
1151
1152
0 commit comments