All files / src/zone-file make-zonefile.ts

55.39% Statements 77/139
8.77% Branches 5/57
94.11% Functions 16/17
59.68% Lines 77/129

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238                        4x   4x 2x       2x 2x 2x 2x   2x                         2x   2x     4x       2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 2x   2x       2x 2x 2x   2x       2x 2x               2x       2x 2x             2x       2x 2x             2x       2x 2x             2x       2x 2x             2x       2x 2x             2x       2x 2x             2x       2x 2x                                   2x       2x 2x                   2x       2x 2x             2x       2x 2x 2x 2x 2x 2x 2x 2x     2x       2x       2x 2x 2x    
import {
  AType,
  CNAMEType,
  MXType,
  NSType,
  SoaType,
  SPFType,
  SRVType,
  TXTType,
  URIType,
  ZoneFileObject,
} from './types';
import { getZoneFileTemplate } from './template';
 
export function makeProfileZoneFile(origin: string, tokenFileUrl: string): string {
  Iif (!tokenFileUrl.includes('://')) {
    throw new Error('Invalid token file url');
  }
 
  const urlScheme = tokenFileUrl.split('://')[0];
  const urlParts = tokenFileUrl.split('://')[1].split('/');
  const domain = urlParts[0];
  const pathname = `/${urlParts.slice(1).join('/')}`;
 
  const zoneFile = {
    $origin: origin,
    $ttl: 3600,
    uri: [
      {
        name: '_http._tcp',
        priority: 10,
        weight: 1,
        target: `${urlScheme}://${domain}${pathname}`,
      },
    ],
  };
 
  const zoneFileTemplate = '{$origin}\n{$ttl}\n{uri}\n';
 
  return makeZoneFile(zoneFile, zoneFileTemplate);
}
 
export function makeZoneFile(
  jsonZoneFile: ZoneFileObject,
  template: string = getZoneFileTemplate()
) {
  template = processOrigin(jsonZoneFile['$origin'], template);
  template = processTTL(jsonZoneFile['$ttl'], template);
  template = processSOA(jsonZoneFile['soa'], template);
  template = processNS(jsonZoneFile['ns'], template);
  template = processA(jsonZoneFile['a'], template);
  template = processAAAA(jsonZoneFile['aaaa'], template);
  template = processCNAME(jsonZoneFile['cname'], template);
  template = processMX(jsonZoneFile['mx'], template);
  template = processPTR(jsonZoneFile['ptr'], template);
  template = processTXT(jsonZoneFile['txt'], template);
  template = processSRV(jsonZoneFile['srv'], template);
  template = processSPF(jsonZoneFile['spf'], template);
  template = processURI(jsonZoneFile['uri'], template);
  template = processValues(jsonZoneFile, template);
  return template.replace(/\n{2,}/gim, '\n\n');
}
 
function processOrigin(data: string | undefined, template: string) {
  let ret = '';
  if (typeof data !== 'undefined') {
    ret += '$ORIGIN ' + data;
  }
  return template.replace('{$origin}', ret);
}
 
function processTTL(data: number | undefined, template: string) {
  let ret = '';
  if (typeof data !== 'undefined') {
    ret += '$TTL ' + data;
  }
  return template.replace('{$ttl}', ret);
}
 
function processSOA(data: SoaType | undefined, template: string) {
  let ret = template;
  Iif (typeof data !== 'undefined') {
    data.name = data.name || '@';
    data.ttl = data.ttl || '';
    for (const key in data) {
      const value = (data as Record<string, string>)[key];
      ret = ret.replace('{' + key + '}', value + '\t');
    }
  }
  return ret;
}
 
function processNS(data: NSType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tNS\t' + record.host + '\n';
    }
  }
  return template.replace('{ns}', ret);
}
 
function processA(data: AType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tA\t' + record.ip + '\n';
    }
  }
  return template.replace('{a}', ret);
}
 
function processAAAA(data: AType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tAAAA\t' + record.ip + '\n';
    }
  }
  return template.replace('{aaaa}', ret);
}
 
function processCNAME(data: CNAMEType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tCNAME\t' + record.alias + '\n';
    }
  }
  return template.replace('{cname}', ret);
}
 
function processMX(data: MXType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tMX\t' + record.preference + '\t' + record.host + '\n';
    }
  }
  return template.replace('{mx}', ret);
}
 
function processPTR(data: NSType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tPTR\t' + record.host + '\n';
    }
  }
  return template.replace('{ptr}', ret);
}
 
function processTXT(data: TXTType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tTXT\t';
      const txtData = record.txt;
      if (typeof txtData === 'string') {
        ret += '"' + txtData + '"';
      } else Iif (txtData instanceof Array) {
        ret += txtData
          .map(function (datum) {
            return '"' + datum + '"';
          })
          .join(' ');
      }
      ret += '\n';
    }
  }
  return template.replace('{txt}', ret);
}
 
function processSRV(data: SRVType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tSRV\t' + record.priority + '\t';
      ret += record.weight + '\t';
      ret += record.port + '\t';
      ret += record.target + '\n';
    }
  }
  return template.replace('{srv}', ret);
}
 
function processSPF(data: SPFType[] | undefined, template: string) {
  let ret = '';
  Iif (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tSPF\t' + record.data + '\n';
    }
  }
  return template.replace('{spf}', ret);
}
 
function processURI(data: URIType[] | undefined, template: string) {
  let ret = '';
  if (data) {
    for (const record of data) {
      ret += (record.name || '@') + '\t';
      Iif (record.ttl) ret += record.ttl + '\t';
      ret += 'IN\tURI\t' + record.priority + '\t';
      ret += record.weight + '\t';
      ret += '"' + record.target + '"\n';
    }
  }
  return template.replace('{uri}', ret);
}
 
function processValues(jsonZoneFile: ZoneFileObject, template: string) {
  template = template.replace(
    '{zone}',
    jsonZoneFile['$origin'] || (jsonZoneFile['soa'] ? jsonZoneFile['soa']['name'] : false) || ''
  );
  template = template.replace('{datetime}', new Date().toISOString());
  const time = Math.round(Date.now() / 1000);
  return template.replace('{time}', `${time}`);
}