summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/tuf-js/dist/models/targets.js
blob: 90a2528764708ec3dd38f7d1836b508f3995e47b (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Targets = void 0;
const util_1 = __importDefault(require("util"));
const guard_1 = require("../utils/guard");
const types_1 = require("../utils/types");
const base_1 = require("./base");
const delegations_1 = require("./delegations");
const file_1 = require("./file");
// Container for the signed part of targets metadata.
//
// Targets contains verifying information about target files and also delegates
// responsible to other Targets roles.
class Targets extends base_1.Signed {
    constructor(options) {
        super(options);
        this.type = types_1.MetadataKind.Targets;
        this.targets = options.targets || {};
        this.delegations = options.delegations;
    }
    equals(other) {
        if (!(other instanceof Targets)) {
            return false;
        }
        return (super.equals(other) &&
            util_1.default.isDeepStrictEqual(this.targets, other.targets) &&
            util_1.default.isDeepStrictEqual(this.delegations, other.delegations));
    }
    toJSON() {
        const json = {
            spec_version: this.specVersion,
            version: this.version,
            expires: this.expires,
            targets: targetsToJSON(this.targets),
            ...this.unrecognizedFields,
        };
        if (this.delegations) {
            json.delegations = this.delegations.toJSON();
        }
        return json;
    }
    static fromJSON(data) {
        const { unrecognizedFields, ...commonFields } = base_1.Signed.commonFieldsFromJSON(data);
        const { targets, delegations, ...rest } = unrecognizedFields;
        return new Targets({
            ...commonFields,
            targets: targetsFromJSON(targets),
            delegations: delegationsFromJSON(delegations),
            unrecognizedFields: rest,
        });
    }
}
exports.Targets = Targets;
function targetsToJSON(targets) {
    return Object.entries(targets).reduce((acc, [path, target]) => ({
        ...acc,
        [path]: target.toJSON(),
    }), {});
}
function targetsFromJSON(data) {
    let targets;
    if ((0, guard_1.isDefined)(data)) {
        if (!(0, guard_1.isObjectRecord)(data)) {
            throw new TypeError('targets must be an object');
        }
        else {
            targets = Object.entries(data).reduce((acc, [path, target]) => ({
                ...acc,
                [path]: file_1.TargetFile.fromJSON(path, target),
            }), {});
        }
    }
    return targets;
}
function delegationsFromJSON(data) {
    let delegations;
    if ((0, guard_1.isDefined)(data)) {
        if (!(0, guard_1.isObject)(data)) {
            throw new TypeError('delegations must be an object');
        }
        else {
            delegations = delegations_1.Delegations.fromJSON(data);
        }
    }
    return delegations;
}