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