Skip to content

Commit

Permalink
Update cacheable-request (#2146)
Browse files Browse the repository at this point in the history
  • Loading branch information
alphmt committed Sep 19, 2022
1 parent e049e94 commit 4c3762a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -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",
Expand Down
29 changes: 15 additions & 14 deletions source/core/index.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -124,12 +129,7 @@ export type RequestEvents<T> = {
off: GotEventFunction<T>;
};

export type CacheableRequestFunction = (
options: string | URL | NativeRequestOptions,
cb?: (response: ServerResponse | ResponseLike) => void
) => CacheableRequest.Emitter;

const cacheableStore = new WeakableMap<string | CacheableRequest.StorageAdapter, CacheableRequestFunction>();
const cacheableStore = new WeakableMap<string | StorageAdapter, CacheableRequestFunction>();

const redirectCodes: ReadonlySet<number> = new Set([300, 301, 302, 303, 304, 307, 308]);

Expand Down Expand Up @@ -969,9 +969,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}
}

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);

Expand Down Expand Up @@ -1010,8 +1010,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {

return result;
}) as typeof http.request,
cache as CacheableRequest.StorageAdapter,
));
cache as StorageAdapter,
);
cacheableStore.set(cache, cacheableRequest.request());
}
}

Expand All @@ -1023,7 +1024,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
let request: ClientRequest | Promise<ClientRequest>;

// 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) {
Expand Down Expand Up @@ -1109,7 +1110,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
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
Expand Down Expand Up @@ -1145,7 +1146,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
void this._onResponse(requestOrResponse as IncomingMessageWithTimings);
}
} catch (error) {
if (error instanceof CacheableRequest.CacheError) {
if (error instanceof CacheableCacheError) {
throw new CacheError(error, this);
}

Expand Down
6 changes: 3 additions & 3 deletions source/core/options.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4c3762a

Please sign in to comment.