timestamp.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Timestamp = void 0;
  4. const base_1 = require("./base");
  5. const file_1 = require("./file");
  6. const utils_1 = require("./utils");
  7. /**
  8. * A container for the signed part of timestamp metadata.
  9. *
  10. * A top-level that specifies the latest version of the snapshot role metadata file,
  11. * and hence the latest versions of all metadata and targets on the repository.
  12. */
  13. class Timestamp extends base_1.Signed {
  14. constructor(options) {
  15. super(options);
  16. this.type = base_1.MetadataKind.Timestamp;
  17. this.snapshotMeta = options.snapshotMeta || new file_1.MetaFile({ version: 1 });
  18. }
  19. equals(other) {
  20. if (!(other instanceof Timestamp)) {
  21. return false;
  22. }
  23. return super.equals(other) && this.snapshotMeta.equals(other.snapshotMeta);
  24. }
  25. toJSON() {
  26. return {
  27. _type: this.type,
  28. spec_version: this.specVersion,
  29. version: this.version,
  30. expires: this.expires,
  31. meta: { 'snapshot.json': this.snapshotMeta.toJSON() },
  32. ...this.unrecognizedFields,
  33. };
  34. }
  35. static fromJSON(data) {
  36. const { unrecognizedFields, ...commonFields } = base_1.Signed.commonFieldsFromJSON(data);
  37. const { meta, ...rest } = unrecognizedFields;
  38. return new Timestamp({
  39. ...commonFields,
  40. snapshotMeta: snapshotMetaFromJSON(meta),
  41. unrecognizedFields: rest,
  42. });
  43. }
  44. }
  45. exports.Timestamp = Timestamp;
  46. function snapshotMetaFromJSON(data) {
  47. let snapshotMeta;
  48. if (utils_1.guard.isDefined(data)) {
  49. const snapshotData = data['snapshot.json'];
  50. if (!utils_1.guard.isDefined(snapshotData) || !utils_1.guard.isObject(snapshotData)) {
  51. throw new TypeError('missing snapshot.json in meta');
  52. }
  53. else {
  54. snapshotMeta = file_1.MetaFile.fromJSON(snapshotData);
  55. }
  56. }
  57. return snapshotMeta;
  58. }