Skip to content
Snippets Groups Projects
Select Git revision
  • f98eafe4968a93d58ffddc702f8db99e0b24335b
  • main default protected
  • renovate/docker.io-library-golang-1.x
  • renovate/ghcr.io-mastodon-mastodon-4.x
  • renovate/docker.io-bitnami-kubectl-1.x
  • renovate/mikefarah-yq-4.x
  • renovate/redis-21.x
  • renovate/mariadb-21.x
  • renovate/quay.io-shivering-isles-koolbox-2025.x
  • renovate/go-1.x
  • renovate/fluxcd-flux2-2.x
  • renovate/prometheus-json-exporter-0.x
  • renovate/amd-gpu-0.x
  • renovate/github.com-prometheus-common-0.x
  • renovate/siderolabs-kubelet-1.33.x
  • renovate/kubernetes-go
  • renovate/external-snapshotter-8.x
  • renovate/gcr.io-projectsigstore-cosign-2.x
  • renovate/kubernetes-sigs-cluster-api-1.x
  • renovate/cloudflare-5.x
  • renovate/docker.io-library-nextcloud-31.x
  • v25.07
  • v25.06
  • v25.05
  • v25.04
  • v25.03
  • v25.02
  • v25.01
  • v24.12
  • v24.11
  • v24.10
  • v24.09
  • v24.08
  • v24.07
  • v24.06
  • v24.05
  • v24.04
  • v24.03
  • v24.02
  • v24.01
  • v23.12
41 results

external-links.js

Blame
  • regex.ts 1.73 KiB
    import is from '@sindresorhus/is';
    import { CONFIG_VALIDATION } from '../constants/error-messages';
    import { logger } from '../logger';
    
    let RegEx: RegExpConstructor;
    
    try {
      // eslint-disable-next-line
      const RE2 = require('re2');
      // Test if native is working
      new RE2('.*').exec('test');
      logger.debug('Using RE2 as regex engine');
      RegEx = RE2;
    } catch (err) {
      logger.warn({ err }, 'RE2 not usable, falling back to RegExp');
      RegEx = RegExp;
    }
    
    export function regEx(pattern: string, flags?: string): RegExp {
      try {
        return new RegEx(pattern, flags);
      } catch (err) {
        const error = new Error(CONFIG_VALIDATION);
        error.configFile = pattern;
        error.validationError = `Invalid regular expression: ${pattern}`;
        throw error;
      }
    }
    
    export function escapeRegExp(input: string): string {
      return input.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
    }
    
    const configValStart = /^!?\//;
    const configValEnd = /\/$/;
    
    export function isConfigRegex(input: unknown): input is string {
      return (
        is.string(input) && configValStart.test(input) && configValEnd.test(input)
      );
    }
    
    function parseConfigRegex(input: string): RegExp | null {
      try {
        const regexString = input
          .replace(configValStart, '')
          .replace(configValEnd, '');
        return regEx(regexString);
      } catch (err) {
        // no-op
      }
      return null;
    }
    
    type ConfigRegexPredicate = (string) => boolean;
    
    export function configRegexPredicate(input: string): ConfigRegexPredicate {
      const configRegex = parseConfigRegex(input);
      if (configRegex) {
        const isPositive = !input.startsWith('!');
        return (x: string): boolean => {
          const res = configRegex.test(x);
          return isPositive ? res : !res;
        };
      }
      return null;
    }