1 | import { npmRegistry, npmRegistryMirrors } from '../data/registries.esm.js';
|
2 | import { fetch } from './fetch.esm.js';
|
3 | import { log } from './log.esm.js';
|
4 |
|
5 | async function fetchFromRegistry({
|
6 | endpoint,
|
7 | headers,
|
8 | query,
|
9 | registry = npmRegistry,
|
10 | mirrors = npmRegistryMirrors,
|
11 | cached
|
12 | }) {
|
13 | const urls = [registry, ...mirrors].map(host => {
|
14 | const url = new URL(endpoint, host);
|
15 | url.search = query != null ? query : '';
|
16 | return url.href;
|
17 | });
|
18 | let lastError;
|
19 |
|
20 | for (const url of urls) {
|
21 | try {
|
22 | const json = await fetch({
|
23 | url,
|
24 | headers,
|
25 | cached
|
26 | });
|
27 | return json;
|
28 | } catch (err) {
|
29 |
|
30 | lastError = err;
|
31 | }
|
32 | }
|
33 |
|
34 | log('fetchFromRegistry: cannot retrieve data from registry or mirrors: %O', {
|
35 | endpoint,
|
36 | headers,
|
37 | query,
|
38 | registry,
|
39 | mirrors,
|
40 | lastError
|
41 | });
|
42 | throw lastError;
|
43 | }
|
44 |
|
45 | export { fetchFromRegistry };
|
46 |
|