isUrlMatchingNoProxy.js.flow 1021 Bytes
Newer Older
jakkree kongpha's avatar
jakkree kongpha committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// @flow

import {
  parse as parseUrl,
} from 'url';
import matcher from 'matcher';
import {
  UnexpectedStateError,
} from '../errors';

export default (subjectUrl: string, noProxy: string) => {
  const subjectUrlTokens = parseUrl(subjectUrl);

  const rules = noProxy.split(/[\s,]/);

  for (const rule of rules) {
    const ruleMatch = rule
      .replace(/^(?<leadingDot>\.)/, '*')
      .match(/^(?<hostname>.+?)(?::(?<port>\d+))?$/);

    if (!ruleMatch || !ruleMatch.groups) {
      throw new UnexpectedStateError('Invalid NO_PROXY pattern.');
    }

    if (!ruleMatch.groups.hostname) {
      throw new UnexpectedStateError('NO_PROXY entry pattern must include hostname. Use * to match any hostname.');
    }

    const hostnameIsMatch = matcher.isMatch(subjectUrlTokens.hostname, ruleMatch.groups.hostname);

    if (hostnameIsMatch && (!ruleMatch.groups || !ruleMatch.groups.port || subjectUrlTokens.port && subjectUrlTokens.port === ruleMatch.groups.port)) {
      return true;
    }
  }

  return false;
};