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

feat(datasource/npm): cache etag for reuse #19823

Merged
merged 9 commits into from
Jan 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/modules/datasource/npm/get.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import * as httpMock from '../../../../test/http-mock';
import { mocked } from '../../../../test/util';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import * as _packageCache from '../../../util/cache/package';
import * as hostRules from '../../../util/host-rules';
import { Http } from '../../../util/http';
import { getDependency } from './get';
import { resolveRegistryUrl, setNpmrc } from './npmrc';

jest.mock('../../../util/cache/package');

const packageCache = mocked(_packageCache);

function getPath(s = ''): string {
const [x] = s.split('\n');
const prePath = x.replace(/^.*https:\/\/test\.org/, '');
Expand Down Expand Up @@ -463,4 +469,35 @@ describe('modules/datasource/npm/get', () => {
]
`);
});

it('returns cached legacy', async () => {
packageCache.get.mockResolvedValueOnce({ some: 'result' });
const dep = await getDependency(http, 'https://some.url', 'some-package');
expect(dep).toMatchObject({ some: 'result' });
});

it('returns unexpired cache', async () => {
packageCache.get.mockResolvedValueOnce({
some: 'result',
cacheData: { softExpireAt: new Date('2099').toISOString() },
});
const dep = await getDependency(http, 'https://some.url', 'some-package');
expect(dep).toMatchObject({ some: 'result' });
});

it('returns soft expired cache', async () => {
packageCache.get.mockResolvedValueOnce({
some: 'result',
cacheData: {
softExpireAt: new Date('2020').toISOString(),
etag: 'some-etag',
},
});
setNpmrc('registry=https://test.org\n_authToken=XXX');

httpMock.scope('https://test.org').get('/@neutrinojs%2Freact').reply(304);
const registryUrl = resolveRegistryUrl('@neutrinojs/react');
const dep = await getDependency(http, registryUrl, '@neutrinojs/react');
expect(dep).toMatchObject({ some: 'result' });
});
});
62 changes: 53 additions & 9 deletions lib/modules/datasource/npm/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import * as packageCache from '../../../util/cache/package';
import type { Http } from '../../../util/http';
import type { HttpOptions } from '../../../util/http/types';
import { regEx } from '../../../util/regex';
import { joinUrlParts } from '../../../util/url';
import { id } from './common';
import type { NpmDependency, NpmRelease, NpmResponse } from './types';
import type {
CachedNpmDependency,
NpmDependency,
NpmRelease,
NpmResponse,
} from './types';

interface PackageSource {
sourceUrl?: string;
Expand Down Expand Up @@ -56,19 +62,54 @@ export async function getDependency(

// Now check the persistent cache
const cacheNamespace = 'datasource-npm';
const cachedResult = await packageCache.get<NpmDependency>(
const cachedResult = await packageCache.get<CachedNpmDependency>(
cacheNamespace,
packageUrl
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
if (cachedResult.cacheData) {
const softExpireAt = new Date(cachedResult.cacheData.softExpireAt);
viceice marked this conversation as resolved.
Show resolved Hide resolved
if (softExpireAt > new Date()) {
logger.debug('Cached result is not expired - reusing');
rarkins marked this conversation as resolved.
Show resolved Hide resolved
delete cachedResult.cacheData;
return cachedResult;
}
logger.debug('Cached result is soft expired');
rarkins marked this conversation as resolved.
Show resolved Hide resolved
} else {
logger.debug('Reusing legacy cached result');
rarkins marked this conversation as resolved.
Show resolved Hide resolved
return cachedResult;
}
}
const cacheMinutes = process.env.RENOVATE_CACHE_NPM_MINUTES
? parseInt(process.env.RENOVATE_CACHE_NPM_MINUTES, 10)
: 1;
const newDate = new Date();
newDate.setMinutes(newDate.getMinutes() + cacheMinutes);
viceice marked this conversation as resolved.
Show resolved Hide resolved
const softExpireAt = newDate.toISOString();
const hardExpireMinutes = 24 * 60; // 1 day
rarkins marked this conversation as resolved.
Show resolved Hide resolved

const uri = url.parse(packageUrl);

try {
const raw = await http.getJson<NpmResponse>(packageUrl);
const options: HttpOptions = {};
if (cachedResult?.cacheData?.etag) {
logger.debug('Using cached etag');
options.headers = { 'If-None-Match': cachedResult.cacheData.etag };
}
const raw = await http.getJson<NpmResponse>(packageUrl, options);
if (cachedResult?.cacheData && raw.statusCode === 304) {
logger.debug('Cached data is unchanged and can be reused');
rarkins marked this conversation as resolved.
Show resolved Hide resolved
cachedResult.cacheData.softExpireAt = softExpireAt;
await packageCache.set(
cacheNamespace,
packageUrl,
cachedResult,
hardExpireMinutes
);
delete cachedResult.cacheData;
return cachedResult;
}
const etag = raw.headers.etag;
const res = raw.body;
if (!res.versions || !Object.keys(res.versions).length) {
// Registry returned a 200 OK but with no versions
Expand Down Expand Up @@ -125,9 +166,6 @@ export async function getDependency(
});
logger.trace({ dep }, 'dep');
// serialize first before saving
const cacheMinutes = process.env.RENOVATE_CACHE_NPM_MINUTES
? parseInt(process.env.RENOVATE_CACHE_NPM_MINUTES, 10)
: 15;
// TODO: use dynamic detection of public repos instead of a static list (#9587)
const whitelistedPublicScopes = [
'@graphql-codegen',
Expand All @@ -140,7 +178,13 @@ export async function getDependency(
(whitelistedPublicScopes.includes(packageName.split('/')[0]) ||
!packageName.startsWith('@'))
) {
await packageCache.set(cacheNamespace, packageUrl, dep, cacheMinutes);
const cacheData = { softExpireAt, etag };
await packageCache.set(
cacheNamespace,
packageUrl,
{ ...dep, cacheData },
hardExpireMinutes
rarkins marked this conversation as resolved.
Show resolved Hide resolved
);
}
return dep;
} catch (err) {
Expand Down
7 changes: 7 additions & 0 deletions lib/modules/datasource/npm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ export interface NpmDependency extends ReleaseResult {
sourceDirectory?: string;
}

export interface CachedNpmDependency extends NpmDependency {
cacheData?: {
etag: string | undefined;
softExpireAt: string;
};
}

export type Npmrc = Record<string, any>;