• 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.Targets = void 0;
7const util_1 = __importDefault(require("util"));
8const base_1 = require("./base");
9const delegations_1 = require("./delegations");
10const file_1 = require("./file");
11const utils_1 = require("./utils");
12// Container for the signed part of targets metadata.
13//
14// Targets contains verifying information about target files and also delegates
15// responsible to other Targets roles.
16class Targets extends base_1.Signed {
17    constructor(options) {
18        super(options);
19        this.type = base_1.MetadataKind.Targets;
20        this.targets = options.targets || {};
21        this.delegations = options.delegations;
22    }
23    addTarget(target) {
24        this.targets[target.path] = target;
25    }
26    equals(other) {
27        if (!(other instanceof Targets)) {
28            return false;
29        }
30        return (super.equals(other) &&
31            util_1.default.isDeepStrictEqual(this.targets, other.targets) &&
32            util_1.default.isDeepStrictEqual(this.delegations, other.delegations));
33    }
34    toJSON() {
35        const json = {
36            _type: this.type,
37            spec_version: this.specVersion,
38            version: this.version,
39            expires: this.expires,
40            targets: targetsToJSON(this.targets),
41            ...this.unrecognizedFields,
42        };
43        if (this.delegations) {
44            json.delegations = this.delegations.toJSON();
45        }
46        return json;
47    }
48    static fromJSON(data) {
49        const { unrecognizedFields, ...commonFields } = base_1.Signed.commonFieldsFromJSON(data);
50        const { targets, delegations, ...rest } = unrecognizedFields;
51        return new Targets({
52            ...commonFields,
53            targets: targetsFromJSON(targets),
54            delegations: delegationsFromJSON(delegations),
55            unrecognizedFields: rest,
56        });
57    }
58}
59exports.Targets = Targets;
60function targetsToJSON(targets) {
61    return Object.entries(targets).reduce((acc, [path, target]) => ({
62        ...acc,
63        [path]: target.toJSON(),
64    }), {});
65}
66function targetsFromJSON(data) {
67    let targets;
68    if (utils_1.guard.isDefined(data)) {
69        if (!utils_1.guard.isObjectRecord(data)) {
70            throw new TypeError('targets must be an object');
71        }
72        else {
73            targets = Object.entries(data).reduce((acc, [path, target]) => ({
74                ...acc,
75                [path]: file_1.TargetFile.fromJSON(path, target),
76            }), {});
77        }
78    }
79    return targets;
80}
81function delegationsFromJSON(data) {
82    let delegations;
83    if (utils_1.guard.isDefined(data)) {
84        if (!utils_1.guard.isObject(data)) {
85            throw new TypeError('delegations must be an object');
86        }
87        else {
88            delegations = delegations_1.Delegations.fromJSON(data);
89        }
90    }
91    return delegations;
92}
93