-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.ts
335 lines (311 loc) · 8.58 KB
/
fetch.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { readFile } from 'fs/promises';
import http from 'http';
import https from 'https';
import os from 'os';
import path from 'path';
import { fileURLToPath } from 'url';
import fetch from 'node-fetch';
import type { AbortError, FetchError, RequestInit } from 'node-fetch';
import semver from 'semver';
import type { Registry } from './config.js';
import log from './log.js';
interface VersionsCache {
versions: semver.SemVer[];
callbackId: NodeJS.Timeout;
}
class CrateVersionsCache {
private cache: Map<string, VersionsCache>;
private expiration: number;
constructor() {
this.cache = new Map();
// 1 hour
this.expiration = 3600000;
}
set = (key: string, versions: semver.SemVer[]) => {
const cache = this.cache.get(key);
if (cache !== undefined) {
clearTimeout(cache.callbackId);
}
this.cache.set(key, {
versions,
callbackId: setTimeout(() => this.cache.delete(key), this.expiration),
});
};
get = (key: string) => {
return this.cache.get(key)?.versions;
};
}
const crateVersionsCache = new CrateVersionsCache();
type LocalSource = 'local registry' | 'cache';
type Source = 'registry' | LocalSource;
export async function fetchVersions(
name: string,
registry: Registry,
useCache: boolean,
): Promise<semver.SemVer[] | Error> {
let versions = crateVersionsCache.get(name);
if (versions === undefined) {
if (useCache && registry.cache !== undefined) {
const v = await fetchLocal(
name,
resolveCacheDir(registry.cache),
'cache',
);
if (!(v instanceof Error)) {
crateVersionsCache.set(name, v);
versions = v;
}
}
if (versions === undefined) {
let v: semver.SemVer[] | Error;
if (registry.index.protocol === 'file') {
v = await fetchLocal(
name,
fileURLToPath(registry.index),
'local registry',
);
} else {
v = await fetchRemote(name, registry.index);
}
if (!(v instanceof Error)) {
crateVersionsCache.set(name, v);
}
return v;
}
}
return versions;
}
const httpAgent = new http.Agent({
maxSockets: 6,
});
const httpsAgent = new https.Agent({
maxSockets: 6,
});
async function fetchRemote(
name: string,
registry: URL,
): Promise<semver.SemVer[] | Error> {
const url = new URL(
path.posix.join(registry.pathname, resolveIndexPath(name)),
registry,
);
log.info(`${name} - fetching versions from registry: ${url}`);
const response = await safeFetch(
url,
{
agent: (url) => {
if (url.protocol === 'https:') {
return httpsAgent;
} else {
return httpAgent;
}
},
headers: {
'User-Agent':
'VSCode.SparseCrates (https://marketplace.visualstudio.com/items?itemName=citreae535.sparse-crates)',
},
},
30000,
);
if (response instanceof Error) {
const e = response;
let message: string;
if (e.name === 'AbortError') {
message = 'connection to registry timeout';
} else {
message = `registry fetch error: ${e.message}`;
}
log.error(`${name} - ${message}`);
return new Error(message);
} else if (response.ok) {
return parseIndex(name, response.buffer, 'registry');
} else {
let message: string;
if (
response.status === 404 ||
response.status === 410 ||
response.status === 451
) {
// https://doc.rust-lang.org/cargo/reference/registry-index.html#nonexistent-crates
message = `crate not found in registry: HTTP ${response.status}`;
} else {
message = `unexpected response code: HTTP ${response.status}`;
}
log.error(`${name} - ${message}`);
return new Error(message);
}
}
async function fetchLocal(
name: string,
dir: string,
source: LocalSource,
): Promise<semver.SemVer[] | Error> {
const p = path.resolve(dir, resolveIndexPath(name));
log.info(`${name} - fetching versions from ${source}: ${p}`);
const buffer = await safeReadFile(p);
if (buffer instanceof Error) {
const e = buffer;
let message: string;
if (e?.code === 'ENOENT') {
message = `crate not found in ${source}`;
} else {
message = `${source} read error: ${e}`;
}
log.error(`${name} - ${message}`);
return new Error(message);
} else {
return parseIndex(name, buffer, source);
}
}
/** The cache file version of Cargo 0.69 / Rust 1.68.0.
* This is Cargo's internal implementation detail.
* https://docs.rs/cargo/0.69.0/src/cargo/sources/registry/index.rs.html#727
*/
const CURRENT_CACHE_VERSION = 3;
/** The index format version of Cargo 0.69 / Rust 1.68.0.
* This is Cargo's internal implementation detail.
* https://docs.rs/cargo/0.69.0/src/cargo/sources/registry/mod.rs.html#262
*/
const INDEX_FORMAT_VERSION = 2;
function parseIndex(
name: string,
buffer: Buffer,
source: Source,
): semver.SemVer[] | Error {
let lines: string[];
if (source === 'cache') {
// The cache format is Cargo's internal implementation detail.
// https://docs.rs/cargo/0.69.0/src/cargo/sources/registry/index.rs.html#690
const cacheVersion = buffer.readUInt8(0);
const indexVersion = buffer.readUint32LE(1);
if (cacheVersion !== CURRENT_CACHE_VERSION) {
const message = `unknown cache version found in cache: ${cacheVersion}`;
log.warn(`${name} - ${message}`);
return new Error(message);
} else if (indexVersion !== INDEX_FORMAT_VERSION) {
const message = `unknown index version found in cache: ${indexVersion}`;
log.warn(`${name} - ${message}`);
return new Error(message);
} else {
lines = buffer
.toString('utf8', 5)
.split('\0')
.filter((_, i) => i % 2 === 0 && i !== 0);
}
} else {
lines = buffer.toString('utf8').trim().split('\n');
}
const versions = lines
.map((line, i) => {
const v = parseRelease(line, name);
if (v instanceof Error) {
log.warn(`${name} - ${source} index line ${i} - ${v.message}`);
return;
} else {
return v;
}
})
.filter((v): v is semver.SemVer => v !== undefined);
const len = versions.length;
if (len === 0) {
const message = `no version found in ${source}`;
log.warn(`${name} - ${message}`);
return new Error(message);
} else {
log.info(`${name} - ${len} versions parsed from ${source}`);
return versions;
}
}
interface Release {
name?: string;
vers?: string;
yanked?: boolean;
}
function parseRelease(
s: string,
name: string,
): semver.SemVer | Error | undefined {
const r: Release | Error = safeJsonParse(s);
if (r instanceof Error) {
return new Error(`invalid JSON: ${r}`);
} else {
const version = semver.parse(r.vers);
if (r.name !== name) {
return new Error(`crate name does not match: ${r.name}`);
} else if (version === null) {
return new Error(`invalid semver: ${r.vers}`);
} else if (r.yanked === undefined) {
return new Error(`"yanked" key missing`);
} else if (!r.yanked) {
return version;
} else {
return;
}
}
}
interface HttpResponse {
ok: boolean;
status: number;
buffer: Buffer;
}
async function safeFetch(
url: URL,
init: RequestInit,
timeout: number,
): Promise<HttpResponse | AbortError | FetchError> {
const controller = new AbortController();
init.signal = controller.signal;
const t = setTimeout(() => controller.abort(), timeout);
try {
const r = await fetch(url, init);
return {
ok: r.ok,
status: r.status,
buffer: Buffer.from(await r.arrayBuffer()),
};
} catch (err) {
return err as AbortError | FetchError;
} finally {
clearTimeout(t);
}
}
async function safeReadFile(
path: string,
): Promise<Buffer | NodeJS.ErrnoException> {
try {
return await readFile(path);
} catch (err) {
return err as NodeJS.ErrnoException;
}
}
function safeJsonParse(s: string) {
try {
return JSON.parse(s);
} catch (err) {
return err as Error;
}
}
function resolveCacheDir(cacheDir: string): string {
let cargoHome = process.env.CARGO_HOME;
if (cargoHome === undefined) {
cargoHome = path.resolve(os.homedir(), '.cargo');
}
return path.resolve(cargoHome, 'registry/index', cacheDir, '.cache');
}
/**
* https://docs.rs/cargo/latest/cargo/sources/registry/index.html#the-format-of-the-index
*/
function resolveIndexPath(name: string): string {
switch (name.length) {
case 0:
return '';
case 1:
return `1/${name}`;
case 2:
return `2/${name}`;
case 3:
return `3/${name.charAt(0)}/${name}`;
default:
return `${name.substring(0, 2)}/${name.substring(2, 4)}/${name}`;
}
}