record.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import type Config from './config'
  2. import type { RecordHash, RecordMate } from './config'
  3. export type RecordProps = {
  4. config: Config
  5. }
  6. class Record {
  7. private config: Config
  8. constructor(options: RecordProps) {
  9. this.config = options.config
  10. }
  11. public getHosts() {
  12. return this.config.getRecord()?.hosts
  13. }
  14. public getHash() {
  15. return this.config.getRecord()?.hash
  16. }
  17. public contains(hosts: string[]) {
  18. const oldHosts = this.getHosts()
  19. if (!oldHosts) {
  20. return false
  21. }
  22. // require hosts is subset of oldHosts
  23. for (const host of hosts) {
  24. if (!oldHosts.includes(host)) {
  25. return false
  26. }
  27. }
  28. return true
  29. }
  30. // whether the files has been tampered with
  31. public equal(hash: RecordHash) {
  32. const oldHash = this.getHash()
  33. if (!oldHash) {
  34. return false
  35. }
  36. return oldHash.key === hash.key && oldHash.cert === hash.cert
  37. }
  38. public async update(record: RecordMate) {
  39. await this.config.merge({ record })
  40. }
  41. }
  42. export default Record