/
index.ts
303 lines (256 loc) · 8.15 KB
/
index.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
299
300
301
302
303
import hasha from 'hasha';
import Git from 'simple-git';
import { join } from 'upath';
import { getAdminConfig } from '../../config/admin';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as memCache from '../../util/cache/memory';
import * as packageCache from '../../util/cache/package';
import { privateCacheDir, readFile } from '../../util/fs';
import { Http } from '../../util/http';
import * as cargoVersioning from '../../versioning/cargo';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
export const id = 'crate';
export const customRegistrySupport = true;
export const defaultRegistryUrls = ['https://crates.io'];
export const defaultVersioning = cargoVersioning.id;
export const registryStrategy = 'first';
const http = new Http(id);
const CRATES_IO_BASE_URL =
'https://raw.githubusercontent.com/rust-lang/crates.io-index/master/';
export enum RegistryFlavor {
/** https://crates.io, supports rawgit access */
CratesIo,
/** https://cloudsmith.io, needs git clone */
Cloudsmith,
/** unknown, assuming private git repository */
Other,
}
export interface RegistryInfo {
flavor: RegistryFlavor;
/** raw URL of the registry, as specified in cargo config */
rawUrl?: string;
/** parsed URL of the registry */
url?: URL;
/** path where the registry is cloned */
clonePath?: string;
}
export function getIndexSuffix(lookupName: string): string[] {
const len = lookupName.length;
if (len === 1) {
return ['1', lookupName];
}
if (len === 2) {
return ['2', lookupName];
}
if (len === 3) {
return ['3', lookupName[0], lookupName];
}
return [lookupName.slice(0, 2), lookupName.slice(2, 4), lookupName];
}
interface CrateRecord {
vers: string;
yanked: boolean;
}
export async function fetchCrateRecordsPayload(
info: RegistryInfo,
lookupName: string
): Promise<string> {
if (info.clonePath) {
const path = join(info.clonePath, ...getIndexSuffix(lookupName));
return readFile(path, 'utf8');
}
if (info.flavor === RegistryFlavor.CratesIo) {
const crateUrl = CRATES_IO_BASE_URL + getIndexSuffix(lookupName).join('/');
try {
return (await http.get(crateUrl)).body;
} catch (err) {
if (
err.statusCode === 429 ||
(err.statusCode >= 500 && err.statusCode < 600)
) {
throw new ExternalHostError(err);
}
throw err;
}
}
throw new Error(`unsupported crate registry flavor: ${info.flavor}`);
}
/**
* Computes the dependency URL for a crate, given
* registry information
*/
function getDependencyUrl(info: RegistryInfo, lookupName: string): string {
switch (info.flavor) {
case RegistryFlavor.CratesIo:
return `https://crates.io/crates/${lookupName}`;
case RegistryFlavor.Cloudsmith: {
// input: https://dl.cloudsmith.io/basic/$org/$repo/cargo/index.git
const tokens = info.url.pathname.split('/');
const org = tokens[2];
const repo = tokens[3];
return `https://cloudsmith.io/~${org}/repos/${repo}/packages/detail/cargo/${lookupName}`;
}
default:
return `${info.rawUrl}/${lookupName}`;
}
}
/**
* Given a Git URL, computes a semi-human-readable name for a folder in which to
* clone the repository.
*/
function cacheDirFromUrl(url: URL): string {
const proto = url.protocol.replace(/:$/, '');
const host = url.hostname;
const hash = hasha(url.pathname, {
algorithm: 'sha256',
}).substr(0, 7);
return `crate-registry-${proto}-${host}-${hash}`;
}
/**
* Fetches information about a registry, by url.
* If no url is given, assumes crates.io.
* If an url is given, assumes it's a valid Git repository
* url and clones it to cache.
*/
async function fetchRegistryInfo(
config: GetReleasesConfig,
registryUrl: string
): Promise<RegistryInfo | null> {
let url: URL;
try {
url = new URL(registryUrl);
} catch (err) {
logger.debug({ registryUrl }, 'could not parse registry URL');
return null;
}
let flavor: RegistryFlavor;
if (url.hostname === 'crates.io') {
flavor = RegistryFlavor.CratesIo;
} else if (url.hostname === 'dl.cloudsmith.io') {
flavor = RegistryFlavor.Cloudsmith;
} else {
flavor = RegistryFlavor.Other;
}
const registry: RegistryInfo = {
flavor,
rawUrl: registryUrl,
url,
};
if (flavor !== RegistryFlavor.CratesIo) {
if (!getAdminConfig().allowCustomCrateRegistries) {
logger.warn(
'crate datasource: allowCustomCrateRegistries=true is required for registries other than crates.io, bailing out'
);
return null;
}
const cacheKey = `crate-datasource/registry-clone-path/${registryUrl}`;
const cacheKeyForError = `crate-datasource/registry-clone-path/${registryUrl}/error`;
// We need to ensure we don't run `git clone` in parallel. Therefore we store
// a promise of the running operation in the mem cache, which in the end resolves
// to the file path of the cloned repository.
const clonePathPromise: Promise<string> | null = memCache.get(cacheKey);
let clonePath: string;
// eslint-disable-next-line @typescript-eslint/no-misused-promises
if (clonePathPromise) {
clonePath = await clonePathPromise;
} else {
clonePath = join(privateCacheDir(), cacheDirFromUrl(url));
logger.info({ clonePath, registryUrl }, `Cloning private cargo registry`);
const git = Git();
const clonePromise = git.clone(registryUrl, clonePath, {
'--depth': 1,
});
memCache.set(
cacheKey,
clonePromise.then(() => clonePath).catch(() => null)
);
try {
await clonePromise;
} catch (err) {
logger.warn(
{ err, lookupName: config.lookupName, registryUrl },
'failed cloning git registry'
);
memCache.set(cacheKeyForError, err);
return null;
}
}
if (!clonePath) {
const err = memCache.get(cacheKeyForError);
logger.warn(
{ err, lookupName: config.lookupName, registryUrl },
'Previous git clone failed, bailing out.'
);
return null;
}
registry.clonePath = clonePath;
}
return registry;
}
export function areReleasesCacheable(registryUrl: string): boolean {
// We only cache public releases, we don't want to cache private
// cloned data between runs.
return registryUrl === 'https://crates.io';
}
export async function getReleases(
config: GetReleasesConfig
): Promise<ReleaseResult | null> {
const { lookupName, registryUrl } = config;
// istanbul ignore if
if (!registryUrl) {
logger.warn(
'crate datasource: No registryUrl specified, cannot perform getReleases'
);
return null;
}
const cacheable = areReleasesCacheable(registryUrl);
const cacheNamespace = 'datasource-crate';
const cacheKey = `${registryUrl}/${lookupName}`;
if (cacheable) {
const cachedResult = await packageCache.get<ReleaseResult>(
cacheNamespace,
cacheKey
);
// istanbul ignore if
if (cachedResult) {
logger.debug({ cacheKey }, 'Returning cached resource');
return cachedResult;
}
}
const registryInfo = await fetchRegistryInfo(config, registryUrl);
if (!registryInfo) {
logger.debug({ registryUrl }, 'Could not fetch registry info');
return null;
}
const dependencyUrl = getDependencyUrl(registryInfo, lookupName);
const payload = await fetchCrateRecordsPayload(registryInfo, lookupName);
const lines = payload
.split('\n') // break into lines
.map((line) => line.trim()) // remove whitespace
.filter((line) => line.length !== 0) // remove empty lines
.map((line) => JSON.parse(line) as CrateRecord); // parse
const result: ReleaseResult = {
dependencyUrl,
releases: [],
};
result.releases = lines
.map((version) => {
const release: Release = {
version: version.vers,
};
if (version.yanked) {
release.isDeprecated = true;
}
return release;
})
.filter((release) => release.version);
if (!result.releases.length) {
return null;
}
if (cacheable) {
const cacheMinutes = 10;
await packageCache.set(cacheNamespace, cacheKey, result, cacheMinutes);
}
return result;
}