/
versions-datasource.ts
201 lines (174 loc) · 6.05 KB
/
versions-datasource.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
import { z } from 'zod';
import { PAGE_NOT_FOUND_ERROR } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { getElapsedMinutes } from '../../../util/date';
import { HttpError } from '../../../util/http';
import { newlineRegex } from '../../../util/regex';
import { LooseArray } from '../../../util/schema-utils';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
type PackageReleases = Map<string, string[]>;
interface RegistryCache {
lastSync: Date;
packageReleases: PackageReleases;
contentLength: number;
isSupported: boolean;
registryUrl: string;
}
export const memCache = new Map<string, RegistryCache>();
const Lines = 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 Lines = z.infer<typeof Lines>;
export class VersionsDatasource extends Datasource {
private isInitialFetch = true;
constructor(override readonly id: string) {
super(id);
}
getRegistryCache(registryUrl: string): RegistryCache {
const cacheKey = `rubygems-versions-cache:${registryUrl}`;
const regCache = memCache.get(cacheKey) ?? {
lastSync: new Date('2000-01-01'),
packageReleases: new Map<string, string[]>(),
contentLength: 0,
isSupported: false,
registryUrl,
};
memCache.set(cacheKey, regCache);
return regCache;
}
async getReleases({
registryUrl,
packageName,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
logger.debug(`getRubygemsOrgDependency(${packageName})`);
// istanbul ignore if
if (!registryUrl) {
return null;
}
const regCache = this.getRegistryCache(registryUrl);
await this.syncVersions(regCache);
if (!regCache.isSupported) {
throw new Error(PAGE_NOT_FOUND_ERROR);
}
const versions = regCache.packageReleases.get(packageName);
if (!versions) {
return null;
}
const releases = versions.map((version) => ({ version }));
return { releases };
}
/**
* Since each `/versions` reponse exceed 10MB,
* there is potential for a memory leak if we construct slices
* of the response body and cache them long-term:
*
* https://bugs.chromium.org/p/v8/issues/detail?id=2869
*
* This method meant to be called for `version` and `packageName`
* before storing them in the cache.
*/
private copystr(x: string): string {
const len = Buffer.byteLength(x, 'utf8');
const buf = this.isInitialFetch
? Buffer.allocUnsafe(len) // allocate from pre-allocated buffer
: Buffer.allocUnsafeSlow(len); // allocate standalone buffer
buf.write(x, 'utf8');
return buf.toString('utf8');
}
private updatePackageReleases(
packageReleases: PackageReleases,
lines: Lines
): void {
for (const line of lines) {
const packageName = this.copystr(line.packageName);
let versions = packageReleases.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 = this.copystr(addedVersion);
versions.push(version);
}
}
}
packageReleases.set(packageName, versions);
}
}
async updateRubyGemsVersions(regCache: RegistryCache): Promise<void> {
const url = `${regCache.registryUrl}/versions`;
const options = {
headers: {
'accept-encoding': 'gzip',
range: `bytes=${regCache.contentLength}-`,
},
};
let newLines: string;
try {
logger.debug('Rubygems: Fetching rubygems.org versions');
const startTime = Date.now();
newLines = (await this.http.get(url, options)).body;
const durationMs = Math.round(Date.now() - startTime);
logger.debug(`Rubygems: Fetched rubygems.org versions in ${durationMs}`);
} catch (err) /* istanbul ignore next */ {
if (err instanceof HttpError && err.response?.statusCode === 404) {
regCache.isSupported = false;
return;
}
if (err.statusCode === 416) {
logger.debug('Rubygems: No update');
regCache.lastSync = new Date();
return;
}
regCache.contentLength = 0;
regCache.packageReleases.clear();
logger.debug({ err }, 'Rubygems fetch error');
throw new ExternalHostError(err);
}
regCache.isSupported = true;
regCache.lastSync = new Date();
const lines = Lines.parse(newLines);
this.updatePackageReleases(regCache.packageReleases, lines);
this.isInitialFetch = false;
}
private updateRubyGemsVersionsPromise: Promise<void> | null = null;
async syncVersions(regCache: RegistryCache): Promise<void> {
const isStale = getElapsedMinutes(regCache.lastSync) >= 15;
if (isStale) {
this.updateRubyGemsVersionsPromise =
this.updateRubyGemsVersionsPromise ??
this.updateRubyGemsVersions(regCache);
await this.updateRubyGemsVersionsPromise;
this.updateRubyGemsVersionsPromise = null;
}
}
}