马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timestamp = void 0;
const base_1 = require("./base");
const file_1 = require("./file");
const utils_1 = require("./utils");
/**
 * A container for the signed part of timestamp metadata.
 *
 * A top-level that specifies the latest version of the snapshot role metadata file,
 * and hence the latest versions of all metadata and targets on the repository.
 */
class Timestamp extends base_1.Signed {
    constructor(options) {
        super(options);
        this.type = base_1.MetadataKind.Timestamp;
        this.snapshotMeta = options.snapshotMeta || new file_1.MetaFile({ version: 1 });
    }
    equals(other) {
        if (!(other instanceof Timestamp)) {
            return false;
        }
        return super.equals(other) && this.snapshotMeta.equals(other.snapshotMeta);
    }
    toJSON() {
        return {
            _type: this.type,
            spec_version: this.specVersion,
            version: this.version,
            expires: this.expires,
            meta: { 'snapshot.json': this.snapshotMeta.toJSON() },
            ...this.unrecognizedFields,
        };
    }
    static fromJSON(data) {
        const { unrecognizedFields, ...commonFields } = base_1.Signed.commonFieldsFromJSON(data);
        const { meta, ...rest } = unrecognizedFields;
        return new Timestamp({
            ...commonFields,
            snapshotMeta: snapshotMetaFromJSON(meta),
            unrecognizedFields: rest,
        });
    }
}
exports.Timestamp = Timestamp;
function snapshotMetaFromJSON(data) {
    let snapshotMeta;
    if (utils_1.guard.isDefined(data)) {
        const snapshotData = data['snapshot.json'];
        if (!utils_1.guard.isDefined(snapshotData) || !utils_1.guard.isObject(snapshotData)) {
            throw new TypeError('missing snapshot.json in meta');
        }
        else {
            snapshotMeta = file_1.MetaFile.fromJSON(snapshotData);
        }
    }
    return snapshotMeta;
}