Skip to content

Commit 19ba64c

Browse files
committed
Adjust response body hash key to store fewer responses bodies
To save on storage space
1 parent 0b98185 commit 19ba64c

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

services/feed-requests/src/feed-fetcher/feed-fetcher.service.ts

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,46 @@ const convertHeaderValue = (val?: string | string[] | null) => {
3434
const trimHeadersForStorage = (
3535
obj?: Record<string, string | undefined>,
3636
): Record<string, string> => {
37+
if (!obj) {
38+
return {};
39+
}
40+
3741
const trimmed = Object.entries(obj || {}).reduce((acc, [key, val]) => {
3842
if (val) {
39-
acc[key] = val;
43+
acc[key.toLowerCase()] = val;
4044
}
4145

4246
return acc;
4347
}, {} as Record<string, string>);
4448

45-
if (!obj) {
46-
return trimmed;
47-
}
48-
49-
for (const key in trimmed) {
50-
if (trimmed[key]) {
51-
trimmed[key.toLowerCase()] = trimmed[key];
52-
}
53-
}
54-
5549
if (trimmed.authorization) {
5650
trimmed.authorization = 'SECRET';
5751
}
5852

5953
return trimmed;
6054
};
6155

56+
const convertFetchOptionsForHashKey = (options: FetchOptions) => {
57+
const { headers, ...rest } = options;
58+
59+
const prunedHeaders = Object.entries(headers || {}).reduce(
60+
(acc, [key, val]) => {
61+
// these keys would result in a new hash every time, so ignore it to save space
62+
if (key !== 'if-none-match' && key !== 'if-modified-since') {
63+
acc[key] = val;
64+
}
65+
66+
return acc;
67+
},
68+
{} as Record<string, string>,
69+
);
70+
71+
return JSON.stringify({
72+
...rest,
73+
headers: prunedHeaders,
74+
});
75+
};
76+
6277
interface FetchOptions {
6378
headers?: Record<string, string>;
6479
proxyUri?: string;
@@ -107,32 +122,6 @@ export class FeedFetcherService {
107122
return this.partitionedRequestsStore.getLatestNextRetryDate(lookupKey);
108123
}
109124

110-
// async getLatestRequestHeaders({
111-
// url,
112-
// }: {
113-
// url: string;
114-
// }): Promise<Response['headers']> {
115-
// const request = await this.requestRepo.findOne(
116-
// {
117-
// url,
118-
// status: RequestStatus.OK,
119-
// },
120-
// {
121-
// orderBy: {
122-
// createdAt: 'DESC',
123-
// },
124-
// populate: ['response'],
125-
// fields: ['response.headers'],
126-
// },
127-
// );
128-
129-
// if (!request) {
130-
// return {};
131-
// }
132-
133-
// return request.response?.headers || {};
134-
// }
135-
136125
async getLatestRequest({
137126
url,
138127
lookupKey,
@@ -283,7 +272,9 @@ export class FeedFetcherService {
283272
}
284273

285274
const key =
286-
url + JSON.stringify(request.fetchOptions) + res.status.toString();
275+
url +
276+
convertFetchOptionsForHashKey(request.fetchOptions) +
277+
res.status.toString();
287278

288279
if (text.length) {
289280
response.responseHashKey = sha1.copy().update(key).digest('hex');

0 commit comments

Comments
 (0)