All files / src parsers.js

63.68% Statements 114/179
85% Branches 17/20
34.78% Functions 8/23
63.68% Lines 114/179

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180193x 193x 193x 193x 193x 193x 193x                 193x 193x             193x 193x           193x 193x 2x 2x     2x 2x 2x 2x 193x 193x 195x 195x 7x 7x 188x 188x 188x 195x 193x 193x 193x 193x 30x 21x 21x 9x 30x 193x 193x 193x 193x 3x 1x 1x 2x 3x 193x 193x 193x 193x 193x 193x 193x                 193x 193x     193x 193x 193x 193x             193x 193x 193x 193x 193x           193x 193x                   193x 193x 193x 193x 193x 193x 193x 193x 190x 190x 190x 3x 3x 3x 3x 3x 3x 3x 187x 187x 187x 190x             190x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 187x 187x 187x 187x 187x 187x 187x 187x 187x 193x 193x 193x                  
import ISO8601 from 'iso8601-duration';
import * as Application from './models/application.js';
import { NG_MEMBER_PREFIXES } from './models/ng.js';
 
const addonOptionsRegex = /^[\w-]+=.+$/;
 
export function addonOptions(options) {
  const optionsArray = typeof options === 'string' ? [options] : options;
  for (const option of optionsArray) {
    if (!option.match(addonOptionsRegex)) {
      throw new Error('Invalid option: ' + option);
    }
  }
  return optionsArray.join(',');
}
 
export function flavor(flavor) {
  const flavors = Application.listAvailableFlavors();
  if (flavors.includes(flavor)) {
    return flavor;
  }
  throw new Error('Invalid value: ' + flavor);
}
 
export function buildFlavor(flavorOrDisabled) {
  if (flavorOrDisabled === 'disabled') {
    return flavorOrDisabled;
  }
  return flavor(flavorOrDisabled);
}
 
export function date(dateString) {
  const date = new Date(dateString);
  if (isNaN(dateString) && !isNaN(date.getTime())) {
    return date;
  }
 
  const seconds = durationInSeconds(dateString);
  return new Date(Date.now() - seconds * 1000);
}
 
export function futureDateOrDuration(dateString) {
  const date = new Date(dateString);
  if (isNaN(dateString) && !isNaN(date.getTime())) {
    return date;
  }
 
  const seconds = durationInSeconds(dateString);
  return new Date(Date.now() + seconds * 1000);
}
 
const appIdRegex = /^app_[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
 
export function appIdOrName(string) {
  if (string.match(appIdRegex)) {
    return { app_id: string };
  }
  return { app_name: string };
}
 
const orgaIdRegex = /^(user_|orga_)[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
 
export function orgaIdOrName(string) {
  if (string.match(orgaIdRegex)) {
    return { orga_id: string };
  }
  return { orga_name: string };
}
 
const addonIdRegex = /^addon_[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const operatorIdRegex =
  /^(keycloak|otoroshi|matomo|metabase)_[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const ulidRegex = /^(kubernetes)_[0-9A-HJ-NP-TV-Z]{26}$/i;
 
export function addonIdOrName(string) {
  if (string.match(addonIdRegex)) {
    return { addon_id: string };
  }
  if (string.match(operatorIdRegex) || string.match(ulidRegex)) {
    return { operator_id: string };
  }
  return { addon_name: string };
}
 
export function commaSeparated(string) {
  return string.split(',');
}
 
const flavorCountRegex = /^[^:]+:\d+$/;
 
export function flavorCount(string) {
  if (!flavorCountRegex.test(string)) {
    throw new Error('Expected format: <flavor>:<count>');
  }
  const [flavor, count] = string.split(':');
  return { flavor: flavor.toUpperCase(), targetNodeCount: Number(count) };
}
 
// /^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?$/i;
const tagRegex = /^[^,\s]+$/;
 
export function tag(string) {
  if (string.match(tagRegex)) {
    return string;
  }
  throw new Error(`Invalid tag '${string}'. Should match ${tagRegex}`);
}
 
export function tags(string) {
  if (String(string).length === 0) {
    return [];
  }
  const tags = String(string).split(',');
  for (const current of tags) {
    tag(current); // will throw if invalid
  }
  return tags;
}
 
/**
 * Parse a duration into seconds
 * A Zero seconds duration is allowed
 * @param {string} durationStr an ISO8601, 1h or a positive number
 * @returns {number} number of seconds
 */
export function durationInSeconds(durationStr = '') {
  const errorMessage = `Invalid duration: "${durationStr}", expect (IS0 8601 duration / a "1h, 1m, 30s" like duration / a positive number in seconds)`;
 
  if (durationStr.startsWith('P')) {
    try {
      const d = ISO8601.parse(durationStr);
      return ISO8601.toSeconds(d);
    } catch {
      throw new Error(errorMessage);
    }
  }
 
  try {
    return parseSimpleDuration(durationStr);
  } catch {
    const n = Number.parseInt(durationStr);
    if (isNaN(n) || n < 0) {
      throw new Error(errorMessage);
    }
    return n;
  }
}
 
const SHORT_UNITS_TO_ISO = {
  ms: (v) => `PT${(v / 1000).toFixed(3)}S`,
  s: (v) => `PT${v}S`,
  m: (v) => `PT${v}M`,
  h: (v) => `PT${v}H`,
  d: (v) => `P${v}D`,
  w: (v) => `P${v}W`,
  M: (v) => `P${v}M`,
  y: (v) => `P${v}Y`,
};
 
function parseSimpleDuration(durationStr) {
  const { rawValue, unit } = durationStr.match(/^(?<rawValue>\d+)(?<unit>.*)$/)?.groups ?? {};
  if (unit in SHORT_UNITS_TO_ISO) {
    const value = Number(rawValue);
    const isoDuration = SHORT_UNITS_TO_ISO[unit](value);
    const d = ISO8601.parse(isoDuration);
    return ISO8601.toSeconds(d);
  }
}
 
// Network Groups parsers
export function ngResourceType(string) {
  if (string.startsWith('ng_')) {
    return { ngId: string };
  }
  if (Object.keys(NG_MEMBER_PREFIXES).some((prefix) => string.startsWith(prefix))) {
    return { memberId: string };
  }
  return { ngResourceLabel: string };
}