• 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.Snapshot = void 0;
7const util_1 = __importDefault(require("util"));
8const base_1 = require("./base");
9const file_1 = require("./file");
10const utils_1 = require("./utils");
11/**
12 * A container for the signed part of snapshot metadata.
13 *
14 * Snapshot contains information about all target Metadata files.
15 * A top-level role that specifies the latest versions of all targets metadata files,
16 * and hence the latest versions of all targets (including any dependencies between them) on the repository.
17 */
18class Snapshot extends base_1.Signed {
19    constructor(opts) {
20        super(opts);
21        this.type = base_1.MetadataKind.Snapshot;
22        this.meta = opts.meta || { 'targets.json': new file_1.MetaFile({ version: 1 }) };
23    }
24    equals(other) {
25        if (!(other instanceof Snapshot)) {
26            return false;
27        }
28        return super.equals(other) && util_1.default.isDeepStrictEqual(this.meta, other.meta);
29    }
30    toJSON() {
31        return {
32            _type: this.type,
33            meta: metaToJSON(this.meta),
34            spec_version: this.specVersion,
35            version: this.version,
36            expires: this.expires,
37            ...this.unrecognizedFields,
38        };
39    }
40    static fromJSON(data) {
41        const { unrecognizedFields, ...commonFields } = base_1.Signed.commonFieldsFromJSON(data);
42        const { meta, ...rest } = unrecognizedFields;
43        return new Snapshot({
44            ...commonFields,
45            meta: metaFromJSON(meta),
46            unrecognizedFields: rest,
47        });
48    }
49}
50exports.Snapshot = Snapshot;
51function metaToJSON(meta) {
52    return Object.entries(meta).reduce((acc, [path, metadata]) => ({
53        ...acc,
54        [path]: metadata.toJSON(),
55    }), {});
56}
57function metaFromJSON(data) {
58    let meta;
59    if (utils_1.guard.isDefined(data)) {
60        if (!utils_1.guard.isObjectRecord(data)) {
61            throw new TypeError('meta field is malformed');
62        }
63        else {
64            meta = Object.entries(data).reduce((acc, [path, metadata]) => ({
65                ...acc,
66                [path]: file_1.MetaFile.fromJSON(metadata),
67            }), {});
68        }
69    }
70    return meta;
71}
72