@@ -6,7 +6,12 @@ import http, {ServerResponse} from 'node:http';
66import type { ClientRequest , RequestOptions } from 'node:http' ;
77import type { Socket } from 'node:net' ;
88import 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' ;
1015import decompressResponse from 'decompress-response' ;
1116import is from '@sindresorhus/is' ;
1217import { buffer as getBuffer } from 'get-stream' ;
@@ -124,12 +129,7 @@ export type RequestEvents<T> = {
124129 off : GotEventFunction < T > ;
125130} ;
126131
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 > ( ) ;
133133
134134const redirectCodes : ReadonlySet < number > = new Set ( [ 300 , 301 , 302 , 303 , 304 , 307 , 308 ] ) ;
135135
@@ -969,9 +969,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
969969 }
970970 }
971971
972- private _prepareCache ( cache : string | CacheableRequest . StorageAdapter ) {
972+ private _prepareCache ( cache : string | StorageAdapter ) {
973973 if ( ! cacheableStore . has ( cache ) ) {
974- cacheableStore . set ( cache , new CacheableRequest (
974+ const cacheableRequest = new CacheableRequest (
975975 ( ( requestOptions : RequestOptions , handler ?: ( response : IncomingMessageWithTimings ) => void ) : ClientRequest => {
976976 const result = ( requestOptions as any ) . _request ( requestOptions , handler ) ;
977977
@@ -1010,8 +1010,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
10101010
10111011 return result ;
10121012 } ) as typeof http . request ,
1013- cache as CacheableRequest . StorageAdapter ,
1014- ) ) ;
1013+ cache as StorageAdapter ,
1014+ ) ;
1015+ cacheableStore . set ( cache , cacheableRequest . request ( ) ) ;
10151016 }
10161017 }
10171018
@@ -1023,7 +1024,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
10231024 let request : ClientRequest | Promise < ClientRequest > ;
10241025
10251026 // 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 ) => {
10271028 response . _readableState . autoDestroy = false ;
10281029
10291030 if ( request ) {
@@ -1109,7 +1110,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
11091110 if ( options . cache ) {
11101111 ( this . _requestOptions as any ) . _request = request ;
11111112 ( this . _requestOptions as any ) . cache = options . cache ;
1112- this . _prepareCache ( options . cache as CacheableRequest . StorageAdapter ) ;
1113+ this . _prepareCache ( options . cache as StorageAdapter ) ;
11131114 }
11141115
11151116 // Cache support
@@ -1145,7 +1146,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
11451146 void this . _onResponse ( requestOrResponse as IncomingMessageWithTimings ) ;
11461147 }
11471148 } catch ( error ) {
1148- if ( error instanceof CacheableRequest . CacheError ) {
1149+ if ( error instanceof CacheableCacheError ) {
11491150 throw new CacheError ( error , this ) ;
11501151 }
11511152
0 commit comments