Skip to content

Commit

Permalink
refactor: http cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jun 13, 2020
1 parent 6c38eb3 commit 75b85eb
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions lib/util/http/index.ts
@@ -1,6 +1,5 @@
import crypto from 'crypto';
import URL from 'url';
import { GotPromise } from 'got';
import * as runCache from '../cache/run';
import { clone } from '../clone';
import got from '../got';
Expand Down Expand Up @@ -34,10 +33,7 @@ export interface HttpResponse<T = string> {
headers: any;
}

async function cloneResponse<T>(
promisedResponse: GotPromise<any>
): Promise<HttpResponse<T>> {
const response = await promisedResponse;
function cloneResponse<T>(response: any): HttpResponse<T> {
// clone body and headers so that the cached result doesn't get accidentally mutated
return {
body: clone<T>(response.body),
Expand All @@ -48,7 +44,7 @@ async function cloneResponse<T>(
export class Http<GetOptions = HttpOptions, PostOptions = HttpPostOptions> {
constructor(private hostType: string, private options?: HttpOptions) {}

protected request<T>(
protected async request<T>(
requestUrl: string | URL,
httpOptions?: InternalHttpOptions
): Promise<HttpResponse<T> | null> {
Expand Down Expand Up @@ -94,24 +90,24 @@ export class Http<GetOptions = HttpOptions, PostOptions = HttpPostOptions> {
options = applyAuthorization(options);

// Cache GET requests unless useCache=false
let promisedRes: GotPromise<any>;
if (options.method === 'get') {
const cacheKey = crypto
.createHash('md5')
.update('got-' + JSON.stringify({ url, headers: options.headers }))
.digest('hex');
if (options.useCache !== false) {
// check cache unless bypassing it
promisedRes = runCache.get(cacheKey);
}
if (promisedRes === undefined) {
// cache miss OR cache bypass
promisedRes = got(url, options);
const cacheKey = crypto
.createHash('md5')
.update('got-' + JSON.stringify({ url, headers: options.headers }))
.digest('hex');
if (options.method === 'get' && options.useCache !== false) {
// return from cache if present
const cachedRes = runCache.get(cacheKey);
// istanbul ignore if
if (cachedRes) {
return cloneResponse<T>(await cachedRes);
}
runCache.set(cacheKey, promisedRes); // always set
return cloneResponse<T>(promisedRes);
}
return cloneResponse<T>(got(url, options));
const promisedRes = got(url, options);
if (options.method === 'get') {
runCache.set(cacheKey, promisedRes); // always set if it's a get
}
const res = await promisedRes;
return cloneResponse<T>(res);
}

get(url: string, options: HttpOptions = {}): Promise<HttpResponse> {
Expand Down

0 comments on commit 75b85eb

Please sign in to comment.