record.ts 1.0 KB

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