From 3b41b2dc5a0fb09e4068b9305672d5bfdefc8fb6 Mon Sep 17 00:00:00 2001 From: alphmth <> Date: Wed, 14 Sep 2022 22:58:21 +0500 Subject: [PATCH 1/7] updated cacheable-request --- package.json | 2 +- source/core/index.ts | 25 +++++++++++-------------- source/core/options.ts | 6 +++--- test/retry.ts | 6 +++--- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index f153c017d..e3bb50ce0 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "@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.0.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..e4fb514a0 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -6,7 +6,8 @@ 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} from 'cacheable-request'; +import type {StorageAdapter, CacheableRequestFunction, CacheableOptions} from 'cacheable-request'; import decompressResponse from 'decompress-response'; import is from '@sindresorhus/is'; import {buffer as getBuffer} from 'get-stream'; @@ -124,12 +125,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 +964,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 +1005,9 @@ export default class Request extends Duplex implements RequestEvents { return result; }) as typeof http.request, - cache as CacheableRequest.StorageAdapter, - )); + cache as StorageAdapter, + ).createCacheableRequest(); + cacheableStore.set(cache, cacheableRequest); } } @@ -1022,7 +1019,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 +1105,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 +1141,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) { diff --git a/test/retry.ts b/test/retry.ts index 890789b6b..a8112225e 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -613,7 +613,7 @@ test('respects backoffLimit', withServer, async (t, server, got) => { }).json(); t.is(body.length, 3); - t.true(body[0]! < 400); - t.true(body[1]! < 400); - t.true(body[2]! < 400); + t.true(body[0]! < 400!); + t.true(body[1]! < 400!); + t.true(body[2]! < 400!); }); From 64f8b87888943f8232d406774aed190fc3bb0cc7 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 15 Sep 2022 01:13:13 +0700 Subject: [PATCH 2/7] Update index.ts --- source/core/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/source/core/index.ts b/source/core/index.ts index e4fb514a0..cce61d418 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1007,6 +1007,7 @@ export default class Request extends Duplex implements RequestEvents { }) as typeof http.request, cache as StorageAdapter, ).createCacheableRequest(); + cacheableStore.set(cache, cacheableRequest); } } From 563e65aabb8eaa0bac074e3a64ecfe42c9d92202 Mon Sep 17 00:00:00 2001 From: alphmth <39932421+alphmth@users.noreply.github.com> Date: Wed, 14 Sep 2022 23:15:39 +0500 Subject: [PATCH 3/7] Update source/core/index.ts Co-authored-by: Sindre Sorhus --- source/core/index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/source/core/index.ts b/source/core/index.ts index cce61d418..2a525ca50 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -6,8 +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, {CacheError as CacheableCacheError} from 'cacheable-request'; -import type {StorageAdapter, CacheableRequestFunction, CacheableOptions} 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'; From 0990716bd1b9ec1ba8c7cd12767024627405d285 Mon Sep 17 00:00:00 2001 From: alphmth <> Date: Wed, 14 Sep 2022 23:23:23 +0500 Subject: [PATCH 4/7] fixed typo --- source/core/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index 2a525ca50..e3c599002 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -8,7 +8,7 @@ import type {Socket} from 'node:net'; import timer from '@szmarczak/http-timer'; import CacheableRequest, { CacheError as CacheableCacheError, - type {StorageAdapter, + type StorageAdapter, type CacheableRequestFunction, type CacheableOptions, } from 'cacheable-request'; From cf2c0fc80a353342cf1cb4aabf84e27b1b5b72d3 Mon Sep 17 00:00:00 2001 From: alphmth <> Date: Wed, 14 Sep 2022 23:41:14 +0500 Subject: [PATCH 5/7] remove cacheable-request types --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index e3bb50ce0..902ee549d 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "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": "^10.0.2", "decompress-response": "^6.0.0", From f6a0d9a7488f9cdd6a295b6b39bc4925c7369b40 Mon Sep 17 00:00:00 2001 From: alphmth <> Date: Thu, 15 Sep 2022 09:38:15 +0500 Subject: [PATCH 6/7] reverted test case --- test/retry.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/retry.ts b/test/retry.ts index a8112225e..890789b6b 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -613,7 +613,7 @@ test('respects backoffLimit', withServer, async (t, server, got) => { }).json(); t.is(body.length, 3); - t.true(body[0]! < 400!); - t.true(body[1]! < 400!); - t.true(body[2]! < 400!); + t.true(body[0]! < 400); + t.true(body[1]! < 400); + t.true(body[2]! < 400); }); From c6b0e6c1bc7b0597a7bc1c7032783c01510618bb Mon Sep 17 00:00:00 2001 From: alphmth <39932421+alphmth@users.noreply.github.com> Date: Fri, 16 Sep 2022 18:13:04 +0500 Subject: [PATCH 7/7] updated cacheable-request --- package.json | 2 +- source/core/index.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 902ee549d..358924ae8 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "@sindresorhus/is": "^5.2.0", "@szmarczak/http-timer": "^5.0.1", "cacheable-lookup": "^6.0.4", - "cacheable-request": "^10.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 e3c599002..178b07e86 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1010,9 +1010,8 @@ export default class Request extends Duplex implements RequestEvents { return result; }) as typeof http.request, cache as StorageAdapter, - ).createCacheableRequest(); - - cacheableStore.set(cache, cacheableRequest); + ); + cacheableStore.set(cache, cacheableRequest.request()); } }