-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
versions-endpoint-cache.ts
298 lines (258 loc) · 8.47 KB
/
versions-endpoint-cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { z } from 'zod';
import { logger } from '../../../logger';
import { getElapsedMinutes } from '../../../util/date';
import { Http, HttpError } from '../../../util/http';
import type { HttpOptions } from '../../../util/http/types';
import { newlineRegex } from '../../../util/regex';
import { Result } from '../../../util/result';
import { LooseArray } from '../../../util/schema-utils';
import { copystr } from '../../../util/string';
import { parseUrl } from '../../../util/url';
type PackageVersions = Map<string, string[]>;
interface VersionsEndpointData {
packageVersions: PackageVersions;
syncedAt: Date;
contentLength: number;
/**
* Last 33 characters of the response (32 hex digits + newline)
*/
contentTail: string;
}
function getContentTail(content: string): string {
return content.slice(-33);
}
function getContentHead(content: string): string {
return content.slice(0, 33);
}
function stripContentHead(content: string): string {
return content.slice(33);
}
function reconcilePackageVersions(
packageVersions: PackageVersions,
versionLines: VersionLines
): PackageVersions {
for (const line of versionLines) {
const packageName = copystr(line.packageName);
let versions = packageVersions.get(packageName) ?? [];
const { deletedVersions, addedVersions } = line;
if (deletedVersions.size > 0) {
versions = versions.filter((v) => !deletedVersions.has(v));
}
if (addedVersions.length > 0) {
const existingVersions = new Set(versions);
for (const addedVersion of addedVersions) {
if (!existingVersions.has(addedVersion)) {
const version = copystr(addedVersion);
versions.push(version);
}
}
}
packageVersions.set(packageName, versions);
}
return packageVersions;
}
function parseFullBody(body: string): VersionsEndpointResult {
const packageVersions = reconcilePackageVersions(
new Map<string, string[]>(),
VersionLines.parse(body)
);
const syncedAt = new Date();
const contentLength = body.length;
const contentTail = getContentTail(body);
return Result.ok({
packageVersions,
syncedAt,
contentLength,
contentTail,
});
}
type VersionsEndpointResult = Result<VersionsEndpointData, 'unsupported-api'>;
export const memCache = new Map<string, VersionsEndpointResult>();
function cacheResult(
registryUrl: string,
result: VersionsEndpointResult
): void {
const registryHostname = parseUrl(registryUrl)?.hostname;
if (registryHostname === 'rubygems.org') {
memCache.set(registryUrl, result);
}
}
const VersionLines = z
.string()
.transform((x) => x.split(newlineRegex))
.pipe(
LooseArray(
z
.string()
.transform((line) => line.trim())
.refine((line) => line.length > 0)
.refine((line) => !line.startsWith('created_at:'))
.refine((line) => line !== '---')
.transform((line) => line.split(' '))
.pipe(z.tuple([z.string(), z.string()]).rest(z.string()))
.transform(([packageName, versions]) => {
const deletedVersions = new Set<string>();
const addedVersions: string[] = [];
for (const version of versions.split(',')) {
if (version.startsWith('-')) {
deletedVersions.add(version.slice(1));
} else {
addedVersions.push(version);
}
}
return { packageName, deletedVersions, addedVersions };
})
)
);
type VersionLines = z.infer<typeof VersionLines>;
function isStale(regCache: VersionsEndpointData): boolean {
return getElapsedMinutes(regCache.syncedAt) >= 15;
}
export type VersionsResult = Result<
string[],
'unsupported-api' | 'package-not-found'
>;
export class VersionsEndpointCache {
constructor(private readonly http: Http) {}
private cacheRequests = new Map<string, Promise<VersionsEndpointResult>>();
/**
* At any given time, there should only be one request for a given registryUrl.
*/
private async getCache(registryUrl: string): Promise<VersionsEndpointResult> {
const oldResult = memCache.get(registryUrl);
if (!oldResult) {
const newResult = await this.fullSync(registryUrl);
cacheResult(registryUrl, newResult);
return newResult;
}
const { val: data } = oldResult.unwrap();
if (!data) {
return oldResult;
}
if (isStale(data)) {
memCache.delete(registryUrl); // If no error is thrown, we'll re-set the cache
const newResult = await this.deltaSync(data, registryUrl);
cacheResult(registryUrl, newResult);
return newResult;
}
return oldResult;
}
async getVersions(
registryUrl: string,
packageName: string
): Promise<VersionsResult> {
/**
* Ensure that only one request for a given registryUrl is in flight at a time.
*/
let cacheRequest = this.cacheRequests.get(registryUrl);
if (!cacheRequest) {
cacheRequest = this.getCache(registryUrl);
this.cacheRequests.set(registryUrl, cacheRequest);
}
let cachedResult: VersionsEndpointResult;
try {
cachedResult = await cacheRequest;
} finally {
this.cacheRequests.delete(registryUrl);
}
const { val: cachedData } = cachedResult.unwrap();
if (!cachedData) {
logger.debug(
{ packageName, registryUrl },
'Rubygems: endpoint not supported'
);
return Result.err('unsupported-api');
}
const versions = cachedData.packageVersions.get(packageName);
if (!versions?.length) {
logger.debug(
{ packageName, registryUrl },
'Rubygems: versions not found'
);
return Result.err('package-not-found');
}
return Result.ok(versions);
}
private async fullSync(registryUrl: string): Promise<VersionsEndpointResult> {
try {
const url = `${registryUrl}/versions`;
const opts: HttpOptions = { headers: { 'Accept-Encoding': 'gzip' } };
const { body } = await this.http.get(url, opts);
return parseFullBody(body);
} catch (err) {
if (err instanceof HttpError && err.response?.statusCode === 404) {
return Result.err('unsupported-api');
}
throw err;
}
}
private async deltaSync(
oldCache: VersionsEndpointData,
registryUrl: string
): Promise<VersionsEndpointResult> {
try {
const url = `${registryUrl}/versions`;
const startByte = oldCache.contentLength - oldCache.contentTail.length;
const opts: HttpOptions = {
headers: {
['Accept-Encoding']: 'deflate, compress, br', // Note: `gzip` usage breaks http client, when used with `Range` header
['Range']: `bytes=${startByte}-`,
},
};
const { statusCode, body } = await this.http.get(url, opts);
/**
* Rubygems will return the full body instead of `416 Range Not Satisfiable`.
* In this case, status code will be 200 instead of 206.
*/
if (statusCode === 200) {
return parseFullBody(body);
}
/**
* We request data in range that overlaps previously fetched data.
* If the head of the response doesn't match the tail of the previous response,
* it means that the data we have is no longer valid.
* In this case we start over with a full sync.
*/
const contentHead = getContentHead(body);
if (contentHead !== oldCache.contentTail) {
return this.fullSync(registryUrl);
}
/**
* Update the cache with the new data.
*/
const delta = stripContentHead(body);
const packageVersions = reconcilePackageVersions(
oldCache.packageVersions,
VersionLines.parse(delta)
);
const syncedAt = new Date();
const contentLength = oldCache.contentLength + delta.length;
const contentTail = getContentTail(body);
return Result.ok({
packageVersions,
syncedAt,
contentLength,
contentTail,
});
} catch (err) {
if (err instanceof HttpError) {
const responseStatus = err.response?.statusCode;
/**
* In case of `416 Range Not Satisfiable` we do a full sync.
* This is unlikely to happen in real life, but anyway.
*/
if (responseStatus === 416) {
return this.fullSync(registryUrl);
}
/**
* If the endpoint is not supported, we stop trying.
* This is unlikely to happen in real life, but still.
*/
if (responseStatus === 404) {
return Result.err('unsupported-api');
}
}
throw err;
}
}
}