From 4c3762af0661e1be2fa0abdff02af1eb497fc115 Mon Sep 17 00:00:00 2001 From: alphmth <39932421+alphmth@users.noreply.github.com> Date: Mon, 19 Sep 2022 09:37:22 +0500 Subject: [PATCH] Update `cacheable-request` (#2146) --- package.json | 3 +-- source/core/index.ts | 29 +++++++++++++++-------------- source/core/options.ts | 6 +++--- 3 files changed, 19 insertions(+), 19 deletions(-) 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 9fa49df49..5a803c737 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]); @@ -969,9 +969,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); @@ -1010,8 +1010,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()); } } @@ -1023,7 +1024,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) { @@ -1109,7 +1110,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 @@ -1145,7 +1146,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 cc722bff7..478fdf4c1 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) {