UNPKG

rpki-validator

Version:

An RPKI validator that works also in the browser.

217 lines (216 loc) 9.95 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function getVrpKey(vrp) { var _vrp$asid, _vrp$maxlen; return "".concat(vrp.prefix, "-").concat((_vrp$asid = vrp.asid) !== null && _vrp$asid !== void 0 ? _vrp$asid : vrp.asn, "-").concat((_vrp$maxlen = vrp.maxlen) !== null && _vrp$maxlen !== void 0 ? _vrp$maxlen : vrp.maxLength); } function makeUnique(arr) { var uniq = {}; if (!!arr) { var _iterator = _createForOfIteratorHelper(arr), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var item = _step.value; uniq[item.id] = item; } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } return Object.values(uniq); } function getManifestKey(file) { return file.split("/").slice(-2).join("-"); } var MetaIndex = exports["default"] = /*#__PURE__*/_createClass(function MetaIndex() { var _this = this; _classCallCheck(this, MetaIndex); _defineProperty(this, "ids", {}); _defineProperty(this, "type", {}); _defineProperty(this, "vrps", {}); _defineProperty(this, "ski", {}); _defineProperty(this, "aki", {}); _defineProperty(this, "manifests", {}); _defineProperty(this, "metadata", {}); _defineProperty(this, "destroy", function () { _this.ids = {}; _this.type = {}; _this.vrps = {}; _this.ski = {}; _this.aki = {}; _this.manifests = {}; _this.metadata = {}; }); _defineProperty(this, "add", function (item) { if (!item.type) { return false; // Skip line } if (item.type === "metadata") { _this.metadata = item; } else { try { _this._add(item); } catch (e) { return false; // Skip malformed line } } }); _defineProperty(this, "_add", function (_ref) { var type = _ref.type, valid_since = _ref.valid_since, valid_until = _ref.valid_until, file = _ref.file, hash_id = _ref.hash_id, ski = _ref.ski, aki = _ref.aki, _ref$vrps = _ref.vrps, vrps = _ref$vrps === void 0 ? [] : _ref$vrps, manifest = _ref.manifest; var item = { type: type, valid_since: valid_since, valid_until: valid_until, file: file, hash_id: hash_id, ski: ski, aki: aki, manifest: manifest ? getManifestKey(manifest) : undefined }; _this.ids[hash_id] = item; if (type === "manifest") { var manifestKey = getManifestKey(file); _this.manifests[manifestKey] = item; } else { _this.type[type] = _this.type[type] || []; _this.type[type].push(_this.ids[hash_id]); if (ski) { _this.ski[ski] = _this.ski[ski] || []; _this.ski[ski].push(_this.ids[hash_id]); } if (aki) { _this.aki[aki] = _this.aki[aki] || []; _this.aki[aki].push(_this.ids[hash_id]); } var _iterator2 = _createForOfIteratorHelper(vrps), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var vrp = _step2.value; var key = getVrpKey(vrp); _this.vrps[key] = _this.vrps[key] || []; _this.vrps[key].push(_this.ids[hash_id]); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } }); _defineProperty(this, "get", function (id) { return _this.ids[id]; }); _defineProperty(this, "getByType", function (type) { return _this.type[type]; }); _defineProperty(this, "getVRPs", function (vrp) { return makeUnique(_this.vrps[getVrpKey(vrp)]); }); _defineProperty(this, "getParents", function (data) { if (!Array.isArray(data)) { data = [data]; } return makeUnique(data.map(function (i) { var _this$_getSki; return (_this$_getSki = _this._getSki(i.aki)) === null || _this$_getSki === void 0 ? void 0 : _this$_getSki.filter(function (p) { return i.hash_id !== p.hash_id; }); }).flat().filter(function (i) { return !!i; })); }); _defineProperty(this, "getStructure", function (vrp) { var tree = []; var count = 100; var item = makeUnique(_this.vrps[getVrpKey(vrp)]); while (item.length && count > 0) { tree.push(item); item = _this.getParents(item); count--; } return tree.flat().map(function (i) { return _objectSpread(_objectSpread({}, i), {}, { manifest: i.manifest ? _this.manifests[i.manifest] : undefined }); }); }); _defineProperty(this, "getFlattenStructure", function (vrp) { var items = _this.getStructure(vrp); var manifests = []; var _iterator3 = _createForOfIteratorHelper(items), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var item = _step3.value; if (item.manifest) { manifests.push(item.manifest); delete item.manifest; } } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } return [].concat(_toConsumableArray(items), manifests); }); _defineProperty(this, "getExpiring", function (vrp, expires, now) { return _this.getFlattenStructure(vrp).filter(function (i) { return i.valid_until === expires && (!i.valid_since || !now || i.valid_since <= now); }); }); _defineProperty(this, "getChildren", function (data) { if (!Array.isArray(data)) { data = [data]; } return makeUnique(data.map(function (i) { return _this._getAki(i.ski); }).flat()); }); _defineProperty(this, "_getSki", function (ski) { if (ski) { return _this.ski[ski]; } else { return null; } }); _defineProperty(this, "_getAki", function (aki) { if (aki) { return _this.aki[aki]; } else { return null; } }); });