-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rrtypes.ts
52 lines (47 loc) · 1.4 KB
/
Rrtypes.ts
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
/** @format */
import data from "./RrtypesData.json";
import _ from "lodash";
import Debug from "debug";
const log = Debug("fetch-dns:rrtypes");
const debug = log.extend("debug");
if (debug.enabled) log.enabled = true;
const rrtypeMap = _.fromPairs(
_.flatMap(data, (rrtype) => {
debug("Processing RRTYPE map entry", rrtype);
const { TYPE, Value } = rrtype || {};
if (
TYPE === "Unassigned" ||
TYPE === "Private use" ||
TYPE === "Reserved"
) {
debug("Skipping unused RRTYPE allocation", TYPE, Value);
return [];
}
const [start, stop] = _.map(_.split(Value, "-", 2), _.toFinite);
if (_.includes(Value, "-")) {
if (start === 0 || stop === 0) {
log("Discovered an unbounded value", rrtype);
return [];
} else {
return _.map(_.range(start, stop + 1), (i) => [_.toString(i), TYPE]);
}
} else {
return [[Value, TYPE]];
}
}),
);
log(rrtypeMap);
export default function lookupRrtype(rrtype: number): string | false {
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
const result = _.get(rrtypeMap, _.toString(rrtype), null);
debug("Lookup up RRTYPE", { rrtype, result });
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
if (_.isNil(result) || result === false) return false;
if (_.isString(result)) {
return result;
} else {
throw new Error(
`INTERNAL ERROR: rrtype return was neither nil nor a string, but '${typeof result}'`,
);
}
}