ClsiStateManager.js 2.5 KB

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