Skip to content
Snippets Groups Projects
Select Git revision
  • e209808fde936890d0a10e719a53f4546ab6ff9a
  • main default protected
  • feat/37531-npm-install-twice
  • renovate/main-ghcr.io-renovatebot-base-image-11.x
  • feat/37517-base64-private-key
  • next
  • feat/gnupg
  • renovate/main-redis-5.x
  • chore/update-static-data
  • feat/poetry/supersede-pep621
  • fix/markdown/linking
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • 41.73.2
  • 41.73.1
  • 41.73.0
  • 41.72.1
  • 41.72.0
  • 42.0.0-next.2
  • 42.0.0-next.1
  • 41.71.1
  • 41.71.0
  • 41.70.3
  • 41.70.2
  • 41.70.1
  • 41.70.0
  • 41.69.1
  • 41.69.0
  • 41.68.0
  • 41.67.0
  • 41.66.3
  • 41.66.2
  • 41.66.1
41 results

extract.ts

Blame
  • util.ts 2.99 KiB
    import cryptoRandomString from 'crypto-random-string';
    import findUp from 'find-up';
    import upath from 'upath';
    import { XmlDocument } from 'xmldoc';
    import { defaultRegistryUrls } from '../../datasource/nuget';
    import { logger } from '../../logger';
    import { readFile } from '../../util/fs';
    import { regEx } from '../../util/regex';
    import type { Registry } from './types';
    
    async function readFileAsXmlDocument(file: string): Promise<XmlDocument> {
      try {
        return new XmlDocument(await readFile(file, 'utf8'));
      } catch (err) {
        logger.debug({ err }, `failed to parse '${file}' as XML document`);
        return undefined;
      }
    }
    
    /* istanbul ignore next */
    export function getRandomString(): string {
      return cryptoRandomString({ length: 16 });
    }
    
    const defaultRegistries = defaultRegistryUrls.map(
      (registryUrl) => ({ url: registryUrl } as Registry)
    );
    
    export function getDefaultRegistries(): Registry[] {
      return [...defaultRegistries];
    }
    
    export async function getConfiguredRegistries(
      packageFile: string,
      localDir: string
    ): Promise<Registry[] | undefined> {
      // Valid file names taken from https://github.com/NuGet/NuGet.Client/blob/f64621487c0b454eda4b98af853bf4a528bef72a/src/NuGet.Core/NuGet.Configuration/Settings/Settings.cs#L34
      const nuGetConfigFileNames = ['nuget.config', 'NuGet.config', 'NuGet.Config'];
      // normalize paths, otherwise startsWith can fail because of path delimitter mismatch
      const normalizedLocalDir = upath.normalizeSafe(localDir);
      const nuGetConfigPath = await findUp(nuGetConfigFileNames, {
        cwd: upath.dirname(upath.join(normalizedLocalDir, packageFile)),
        type: 'file',
      });
    
      if (
        !nuGetConfigPath ||
        upath.normalizeSafe(nuGetConfigPath).startsWith(normalizedLocalDir) !== true
      ) {
        return undefined;
      }
    
      logger.debug({ nuGetConfigPath }, 'found NuGet.config');
      const nuGetConfig = await readFileAsXmlDocument(nuGetConfigPath);
      if (!nuGetConfig) {
        return undefined;
      }
    
      const packageSources = nuGetConfig.childNamed('packageSources');
      if (!packageSources) {
        return undefined;
      }
    
      const registries = getDefaultRegistries();
      for (const child of packageSources.children) {
        if (child.type === 'element') {
          if (child.name === 'clear') {
            logger.debug(`clearing registry URLs`);
            registries.length = 0;
          } else if (child.name === 'add') {
            const isHttpUrl = regEx(/^https?:\/\//i).test(child.attr.value);
            if (isHttpUrl) {
              let registryUrl = child.attr.value;
              if (child.attr.protocolVersion) {
                registryUrl += `#protocolVersion=${child.attr.protocolVersion}`;
              }
              logger.debug({ registryUrl }, 'adding registry URL');
              registries.push({
                name: child.attr.key,
                url: registryUrl,
              });
            } else {
              logger.debug(
                { registryUrl: child.attr.value },
                'ignoring local registry URL'
              );
            }
          }
          // child.name === 'remove' not supported
        }
      }
      return registries;
    }