• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Signed = exports.isMetadataKind = exports.MetadataKind = void 0;
7const util_1 = __importDefault(require("util"));
8const error_1 = require("./error");
9const utils_1 = require("./utils");
10const SPECIFICATION_VERSION = ['1', '0', '31'];
11var MetadataKind;
12(function (MetadataKind) {
13    MetadataKind["Root"] = "root";
14    MetadataKind["Timestamp"] = "timestamp";
15    MetadataKind["Snapshot"] = "snapshot";
16    MetadataKind["Targets"] = "targets";
17})(MetadataKind = exports.MetadataKind || (exports.MetadataKind = {}));
18function isMetadataKind(value) {
19    return (typeof value === 'string' &&
20        Object.values(MetadataKind).includes(value));
21}
22exports.isMetadataKind = isMetadataKind;
23/***
24 * A base class for the signed part of TUF metadata.
25 *
26 * Objects with base class Signed are usually included in a ``Metadata`` object
27 * on the signed attribute. This class provides attributes and methods that
28 * are common for all TUF metadata types (roles).
29 */
30class Signed {
31    constructor(options) {
32        this.specVersion = options.specVersion || SPECIFICATION_VERSION.join('.');
33        const specList = this.specVersion.split('.');
34        if (!(specList.length === 2 || specList.length === 3) ||
35            !specList.every((item) => isNumeric(item))) {
36            throw new error_1.ValueError('Failed to parse specVersion');
37        }
38        // major version must match
39        if (specList[0] != SPECIFICATION_VERSION[0]) {
40            throw new error_1.ValueError('Unsupported specVersion');
41        }
42        this.expires = options.expires || new Date().toISOString();
43        this.version = options.version || 1;
44        this.unrecognizedFields = options.unrecognizedFields || {};
45    }
46    equals(other) {
47        if (!(other instanceof Signed)) {
48            return false;
49        }
50        return (this.specVersion === other.specVersion &&
51            this.expires === other.expires &&
52            this.version === other.version &&
53            util_1.default.isDeepStrictEqual(this.unrecognizedFields, other.unrecognizedFields));
54    }
55    isExpired(referenceTime) {
56        if (!referenceTime) {
57            referenceTime = new Date();
58        }
59        return referenceTime >= new Date(this.expires);
60    }
61    static commonFieldsFromJSON(data) {
62        const { spec_version, expires, version, ...rest } = data;
63        if (utils_1.guard.isDefined(spec_version) && !(typeof spec_version === 'string')) {
64            throw new TypeError('spec_version must be a string');
65        }
66        if (utils_1.guard.isDefined(expires) && !(typeof expires === 'string')) {
67            throw new TypeError('expires must be a string');
68        }
69        if (utils_1.guard.isDefined(version) && !(typeof version === 'number')) {
70            throw new TypeError('version must be a number');
71        }
72        return {
73            specVersion: spec_version,
74            expires,
75            version,
76            unrecognizedFields: rest,
77        };
78    }
79}
80exports.Signed = Signed;
81function isNumeric(str) {
82    return !isNaN(Number(str));
83}
84