Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cacheable-request #2146

Merged
merged 7 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -49,7 +49,7 @@
"@szmarczak/http-timer": "^5.0.1",
"@types/cacheable-request": "^6.0.2",
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
"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",
Expand Down
25 changes: 11 additions & 14 deletions source/core/index.ts
Expand Up @@ -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';
alphmt marked this conversation as resolved.
Show resolved Hide resolved
import decompressResponse from 'decompress-response';
import is from '@sindresorhus/is';
import {buffer as getBuffer} from 'get-stream';
Expand Down Expand Up @@ -124,12 +125,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 @@ -968,9 +964,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 @@ -1009,8 +1005,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {

return result;
}) as typeof http.request,
cache as CacheableRequest.StorageAdapter,
));
cache as StorageAdapter,
).createCacheableRequest();
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
cacheableStore.set(cache, cacheableRequest);
}
}

Expand All @@ -1022,7 +1019,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 @@ -1108,7 +1105,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 @@ -1144,7 +1141,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
6 changes: 3 additions & 3 deletions test/retry.ts
Expand Up @@ -613,7 +613,7 @@ test('respects backoffLimit', withServer, async (t, server, got) => {
}).json<number[]>();

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!);
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
});