codekingpro/portable-devtools
114k
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 type = base_1.MetadataKind.Targets;
18 targets;
19 delegations;
20 constructor(options) {
21 super(options);
22 this.targets = options.targets || {};
23 this.delegations = options.delegations;
24 }
25 addTarget(target) {
26 this.targets[target.path] = target;
27 }
28 equals(other) {
29 if (!(other instanceof Targets)) {
30 return false;
31 }
32 return (super.equals(other) &&
33 util_1.default.isDeepStrictEqual(this.targets, other.targets) &&
34 util_1.default.isDeepStrictEqual(this.delegations, other.delegations));
35 }
36 toJSON() {
37 const json = {
38 _type: this.type,
39 spec_version: this.specVersion,
40 version: this.version,
41 expires: this.expires,
42 targets: targetsToJSON(this.targets),
43 ...this.unrecognizedFields,
44 };
45 if (this.delegations) {
46 json.delegations = this.delegations.toJSON();
47 }
48 return json;
49 }
50 static fromJSON(data) {
51 const { unrecognizedFields, ...commonFields } = base_1.Signed.commonFieldsFromJSON(data);
52 const { targets, delegations, ...rest } = unrecognizedFields;
53 return new Targets({
54 ...commonFields,
55 targets: targetsFromJSON(targets),
56 delegations: delegationsFromJSON(delegations),
57 unrecognizedFields: rest,
58 });
59 }
60}
61exports.Targets = Targets;
62function targetsToJSON(targets) {
63 return Object.entries(targets).reduce((acc, [path, target]) => ({
64 ...acc,
65 [path]: target.toJSON(),
66 }), {});
67}
68function targetsFromJSON(data) {
69 let targets;
70 if (utils_1.guard.isDefined(data)) {
71 if (!utils_1.guard.isObjectRecord(data)) {
72 throw new TypeError('targets must be an object');
73 }
74 else {
75 targets = Object.entries(data).reduce((acc, [path, target]) => ({
76 ...acc,
77 [path]: file_1.TargetFile.fromJSON(path, target),
78 }), {});
79 }
80 }
81 return targets;
82}
83function delegationsFromJSON(data) {
84 let delegations;
85 if (utils_1.guard.isDefined(data)) {
86 if (!utils_1.guard.isObject(data)) {
87 throw new TypeError('delegations must be an object');
88 }
89 else {
90 delegations = delegations_1.Delegations.fromJSON(data);
91 }
92 }
93 return delegations;
94}
95 