diff --git a/package.json b/package.json index f153c017d..358924ae8 100644 --- a/package.json +++ b/package.json @@ -47,9 +47,8 @@ "dependencies": { "@sindresorhus/is": "^5.2.0", "@szmarczak/http-timer": "^5.0.1", - "@types/cacheable-request": "^6.0.2", "cacheable-lookup": "^6.0.4", - "cacheable-request": "^7.0.2", + "cacheable-request": "^10.1.2", "decompress-response": "^6.0.0", "form-data-encoder": "^2.1.2", "get-stream": "^6.0.1", diff --git a/source/core/index.ts b/source/core/index.ts index 5d0e2f6b4..178b07e86 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -6,7 +6,12 @@ import http, {ServerResponse} from 'node:http'; import type {ClientRequest, RequestOptions} from 'node:http'; import type {Socket} from 'node:net'; import timer from '@szmarczak/http-timer'; -import CacheableRequest from 'cacheable-request'; +import CacheableRequest, { + CacheError as CacheableCacheError, + type StorageAdapter, + type CacheableRequestFunction, + type CacheableOptions, +} from 'cacheable-request'; import decompressResponse from 'decompress-response'; import is from '@sindresorhus/is'; import {buffer as getBuffer} from 'get-stream'; @@ -124,12 +129,7 @@ export type RequestEvents = { off: GotEventFunction; }; -export type CacheableRequestFunction = ( - options: string | URL | NativeRequestOptions, - cb?: (response: ServerResponse | ResponseLike) => void -) => CacheableRequest.Emitter; - -const cacheableStore = new WeakableMap(); +const cacheableStore = new WeakableMap(); const redirectCodes: ReadonlySet = new Set([300, 301, 302, 303, 304, 307, 308]); @@ -968,9 +968,9 @@ export default class Request extends Duplex implements RequestEvents { } } - private _prepareCache(cache: string | CacheableRequest.StorageAdapter) { + private _prepareCache(cache: string | StorageAdapter) { if (!cacheableStore.has(cache)) { - cacheableStore.set(cache, new CacheableRequest( + const cacheableRequest = new CacheableRequest( ((requestOptions: RequestOptions, handler?: (response: IncomingMessageWithTimings) => void): ClientRequest => { const result = (requestOptions as any)._request(requestOptions, handler); @@ -1009,8 +1009,9 @@ export default class Request extends Duplex implements RequestEvents { return result; }) as typeof http.request, - cache as CacheableRequest.StorageAdapter, - )); + cache as StorageAdapter, + ); + cacheableStore.set(cache, cacheableRequest.request()); } } @@ -1022,7 +1023,7 @@ export default class Request extends Duplex implements RequestEvents { let request: ClientRequest | Promise; // TODO: Fix `cacheable-response`. This is ugly. - const cacheRequest = cacheableStore.get((options as any).cache)!(options, async (response: any) => { + const cacheRequest = cacheableStore.get((options as any).cache)!(options as CacheableOptions, async (response: any) => { response._readableState.autoDestroy = false; if (request) { @@ -1108,7 +1109,7 @@ export default class Request extends Duplex implements RequestEvents { if (options.cache) { (this._requestOptions as any)._request = request; (this._requestOptions as any).cache = options.cache; - this._prepareCache(options.cache as CacheableRequest.StorageAdapter); + this._prepareCache(options.cache as StorageAdapter); } // Cache support @@ -1144,7 +1145,7 @@ export default class Request extends Duplex implements RequestEvents { void this._onResponse(requestOrResponse as IncomingMessageWithTimings); } } catch (error) { - if (error instanceof CacheableRequest.CacheError) { + if (error instanceof CacheableCacheError) { throw new CacheError(error, this); } diff --git a/source/core/options.ts b/source/core/options.ts index ae321d408..02fc90d7f 100644 --- a/source/core/options.ts +++ b/source/core/options.ts @@ -24,7 +24,7 @@ import CacheableLookup from 'cacheable-lookup'; import http2wrapper, {type ClientHttp2Session} from 'http2-wrapper'; import {isFormData} from 'form-data-encoder'; import type {FormDataLike} from 'form-data-encoder'; -import type CacheableRequest from 'cacheable-request'; +import type {StorageAdapter} from 'cacheable-request'; import type ResponseLike from 'responselike'; import type {IncomingMessageWithTimings} from '@szmarczak/http-timer'; import type {CancelableRequest} from '../as-promise/types.js'; @@ -1806,11 +1806,11 @@ export default class Options { @default false */ - get cache(): string | CacheableRequest.StorageAdapter | boolean | undefined { + get cache(): string | StorageAdapter | boolean | undefined { return this._internals.cache; } - set cache(value: string | CacheableRequest.StorageAdapter | boolean | undefined) { + set cache(value: string | StorageAdapter | boolean | undefined) { assert.any([is.object, is.string, is.boolean, is.undefined], value); if (value === true) {