Skip to content

Commit

Permalink
fix: lazy-load fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Apr 27, 2023
1 parent 40b62d8 commit 73aacb5
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {loadNpmConfigs} = require('./files');

const DEFAULT_REGISTRY_URL = 'https://registry.npmjs.org/';
let registriesInfo = [];
let fetch = () => {};
let fetch;

const replaceEnvVars = (str) =>
str.replace(/\${([^}]+)}/g, (match, variableName) => process.env[variableName] || '');
Expand Down Expand Up @@ -67,15 +67,10 @@ const getRegistriesInfo = (appPath) => {
return registries.map((reg) => ({...reg, url: new URL(reg.url)}));
};

const loadRegistriesInfo = (appPath) => {
const setupRegistries = (appPath) => {
registriesInfo = getRegistriesInfo(appPath);
};

const setupRegistries = async (appPath) => {
loadRegistriesInfo(appPath);
fetch = (await import('node-fetch')).default;
};

const getRegistryInfoForPackage = (packageName) => {
if (packageName.includes('/')) {
const [packageOrg] = packageName.split('/');
Expand All @@ -98,6 +93,11 @@ const getRegistryData = async (packageName, packageVersion) => {

const registryInfo = getRegistryInfoForPackage(packageName);
const packageUrl = new URL(`/${packageName}`, registryInfo?.url || DEFAULT_REGISTRY_URL);

if (!fetch) {
fetch = (await import('node-fetch')).default;
}

const responseRaw = await fetch(packageUrl.href, {
headers: {
...(registryInfo?.token && {Authorization: `Bearer ${registryInfo.token}`}),
Expand Down Expand Up @@ -188,7 +188,6 @@ const getRegistryAudit = async (packageName, packageVersion, packageGraph) => {

module.exports = {
setupRegistries,
loadRegistriesInfo,
getRegistryData,
getRegistryDataMultiple,
getRegistryAudit,
Expand Down

0 comments on commit 73aacb5

Please sign in to comment.