Skip to content

Commit

Permalink
feat: Drop older timeStamp field support (#33734)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Jan 22, 2025
1 parent a33d3ea commit da5c5ed
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 58 deletions.
4 changes: 2 additions & 2 deletions lib/util/http/cache/abstract-http-cache-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { logger } from '../../../logger';
import { HttpCacheStats } from '../../stats';
import type { GotOptions, HttpResponse } from '../types';
import { copyResponse } from '../util';
import { HttpCacheSchema } from './schema';
import type { HttpCache, HttpCacheProvider } from './types';
import { type HttpCache, HttpCacheSchema } from './schema';
import type { HttpCacheProvider } from './types';

export abstract class AbstractHttpCacheProvider implements HttpCacheProvider {
protected abstract load(url: string): Promise<unknown>;
Expand Down
23 changes: 1 addition & 22 deletions lib/util/http/cache/repository-http-cache-provider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Http } from '..';
import * as httpMock from '../../../../test/http-mock';
import { logger } from '../../../../test/util';
import { getCache, resetCache } from '../../cache/repository';
import { resetCache } from '../../cache/repository';
import { repoCacheProvider } from './repository-http-cache-provider';

describe('util/http/cache/repository-http-cache-provider', () => {
Expand Down Expand Up @@ -59,27 +59,6 @@ describe('util/http/cache/repository-http-cache-provider', () => {
});
});

it('uses older cache format', async () => {
const repoCache = getCache();
repoCache.httpCache = {
'https://example.com/foo/bar': {
etag: '123',
lastModified: 'Mon, 01 Jan 2000 00:00:00 GMT',
httpResponse: { statusCode: 200, body: { msg: 'Hello, world!' } },
timeStamp: new Date().toISOString(),
},
};
httpMock.scope('https://example.com').get('/foo/bar').reply(304);

const res = await http.getJsonUnchecked('https://example.com/foo/bar');

expect(res).toMatchObject({
statusCode: 200,
body: { msg: 'Hello, world!' },
authorization: false,
});
});

it('reports if cache could not be persisted', async () => {
httpMock
.scope('https://example.com')
Expand Down
2 changes: 1 addition & 1 deletion lib/util/http/cache/repository-http-cache-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getCache } from '../../cache/repository';
import { AbstractHttpCacheProvider } from './abstract-http-cache-provider';
import type { HttpCache } from './types';
import type { HttpCache } from './schema';

export class RepositoryHttpCacheProvider extends AbstractHttpCacheProvider {
override load(url: string): Promise<unknown> {
Expand Down
34 changes: 8 additions & 26 deletions lib/util/http/cache/schema.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
import { z } from 'zod';

const invalidFieldsMsg =
'Cache object should have `etag` or `lastModified` fields';

export const HttpCacheSchema = z
.object({
// TODO: remove this migration part during the Christmas eve 2024
timeStamp: z.string().optional(),
timestamp: z.string().optional(),
})
.passthrough()
.transform((data) => {
if (data.timeStamp) {
data.timestamp = data.timeStamp;
delete data.timeStamp;
}
return data;
etag: z.string().optional(),
lastModified: z.string().optional(),
httpResponse: z.unknown(),
timestamp: z.string(),
})
.pipe(
z
.object({
etag: z.string().optional(),
lastModified: z.string().optional(),
httpResponse: z.unknown(),
timestamp: z.string(),
})
.refine(
({ etag, lastModified }) => etag ?? lastModified,
invalidFieldsMsg,
),
.refine(
({ etag, lastModified }) => etag ?? lastModified,
'Cache object should have `etag` or `lastModified` fields',
)
.nullable()
.catch(null);
export type HttpCache = z.infer<typeof HttpCacheSchema>;
7 changes: 0 additions & 7 deletions lib/util/http/cache/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import type { GotOptions, HttpResponse } from '../types';

export interface HttpCache {
etag?: string;
lastModified?: string;
httpResponse: unknown;
timestamp: string;
}

export interface HttpCacheProvider {
setCacheHeaders<T extends Pick<GotOptions, 'headers'>>(
url: string,
Expand Down

0 comments on commit da5c5ed

Please sign in to comment.