ClsiStateManager.mjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS205: Consider reworking code to avoid use of IIFEs
  8. * DS207: Consider shorter variations of null checks
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. import crypto from 'node:crypto'
  12. import ProjectEntityHandler from '../Project/ProjectEntityHandler.mjs'
  13. // The "state" of a project is a hash of the relevant attributes in the
  14. // project object in this case we only need the rootFolder.
  15. //
  16. // The idea is that it will change if any doc or file is
  17. // created/renamed/deleted, and also if the content of any file (not
  18. // doc) changes.
  19. //
  20. // When the hash changes the full set of files on the CLSI will need to
  21. // be updated. If it doesn't change then we can overwrite changed docs
  22. // in place on the clsi, getting them from the docupdater.
  23. //
  24. // The docupdater is responsible for setting the key in redis, and
  25. // unsetting it if it removes any documents from the doc updater.
  26. const buildState = s =>
  27. crypto.createHash('sha1').update(s, 'utf8').digest('hex')
  28. export default {
  29. computeHash(project, options) {
  30. const { docs, files } =
  31. ProjectEntityHandler.getAllEntitiesFromProject(project)
  32. const fileList = Array.from(files || []).map(
  33. f => `${f.file._id}:${f.file.rev}:${f.file.created}:${f.path}`
  34. )
  35. const docList = Array.from(docs || []).map(d => `${d.doc._id}:${d.path}`)
  36. const sortedEntityList = [
  37. ...Array.from(docList),
  38. ...Array.from(fileList),
  39. ].sort()
  40. // ignore the isAutoCompile options as it doesn't affect the
  41. // output, but include all other options e.g. draft
  42. const optionsList = (() => {
  43. const result = []
  44. const object = options || {}
  45. for (const key in object) {
  46. const value = object[key]
  47. if (!['isAutoCompile', 'buildId'].includes(key)) {
  48. result.push(`option ${key}:${value}`)
  49. }
  50. }
  51. return result
  52. })()
  53. const sortedOptionsList = optionsList.sort()
  54. const hash = buildState(
  55. [...Array.from(sortedEntityList), ...Array.from(sortedOptionsList)].join(
  56. '\n'
  57. )
  58. )
  59. return hash
  60. },
  61. }