ResourceStateManager.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS201: Simplify complex destructure assignments
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. let ResourceStateManager
  13. const Path = require('path')
  14. const fs = require('fs')
  15. const logger = require('logger-sharelatex')
  16. const Errors = require('./Errors')
  17. const SafeReader = require('./SafeReader')
  18. module.exports = ResourceStateManager = {
  19. // The sync state is an identifier which must match for an
  20. // incremental update to be allowed.
  21. //
  22. // The initial value is passed in and stored on a full
  23. // compile, along with the list of resources..
  24. //
  25. // Subsequent incremental compiles must come with the same value - if
  26. // not they will be rejected with a 409 Conflict response. The
  27. // previous list of resources is returned.
  28. //
  29. // An incremental compile can only update existing files with new
  30. // content. The sync state identifier must change if any docs or
  31. // files are moved, added, deleted or renamed.
  32. SYNC_STATE_FILE: '.project-sync-state',
  33. SYNC_STATE_MAX_SIZE: 128 * 1024,
  34. saveProjectState(state, resources, basePath, callback) {
  35. const stateFile = Path.join(basePath, this.SYNC_STATE_FILE)
  36. if (state == null) {
  37. // remove the file if no state passed in
  38. logger.log({ state, basePath }, 'clearing sync state')
  39. fs.unlink(stateFile, function (err) {
  40. if (err && err.code !== 'ENOENT') {
  41. return callback(err)
  42. } else {
  43. return callback()
  44. }
  45. })
  46. } else {
  47. logger.log({ state, basePath }, 'writing sync state')
  48. const resourceList = resources.map((resource) => resource.path)
  49. fs.writeFile(
  50. stateFile,
  51. [...resourceList, `stateHash:${state}`].join('\n'),
  52. callback
  53. )
  54. }
  55. },
  56. checkProjectStateMatches(state, basePath, callback) {
  57. const stateFile = Path.join(basePath, this.SYNC_STATE_FILE)
  58. const size = this.SYNC_STATE_MAX_SIZE
  59. SafeReader.readFile(stateFile, size, 'utf8', function (
  60. err,
  61. result,
  62. bytesRead
  63. ) {
  64. if (err) {
  65. return callback(err)
  66. }
  67. if (bytesRead === size) {
  68. logger.error(
  69. { file: stateFile, size, bytesRead },
  70. 'project state file truncated'
  71. )
  72. }
  73. const array = result.toString().split('\n')
  74. const adjustedLength = Math.max(array.length, 1)
  75. const resourceList = array.slice(0, adjustedLength - 1)
  76. const oldState = array[adjustedLength - 1]
  77. const newState = `stateHash:${state}`
  78. logger.log(
  79. { state, oldState, basePath, stateMatches: newState === oldState },
  80. 'checking sync state'
  81. )
  82. if (newState !== oldState) {
  83. return callback(
  84. new Errors.FilesOutOfSyncError('invalid state for incremental update')
  85. )
  86. } else {
  87. const resources = resourceList.map((path) => ({ path }))
  88. callback(null, resources)
  89. }
  90. })
  91. },
  92. checkResourceFiles(resources, allFiles, basePath, callback) {
  93. // check the paths are all relative to current directory
  94. let file
  95. const containsRelativePath = (resource) => {
  96. const dirs = resource.path.split('/')
  97. return dirs.indexOf('..') !== -1
  98. }
  99. if (resources.some(containsRelativePath)) {
  100. return callback(new Error('relative path in resource file list'))
  101. }
  102. // check if any of the input files are not present in list of files
  103. const seenFiles = new Set(allFiles)
  104. const missingFiles = resources
  105. .map((resource) => resource.path)
  106. .filter((path) => !seenFiles.has(path))
  107. if (missingFiles.length > 0) {
  108. logger.err(
  109. { missingFiles, basePath, allFiles, resources },
  110. 'missing input files for project'
  111. )
  112. return callback(
  113. new Errors.FilesOutOfSyncError(
  114. 'resource files missing in incremental update'
  115. )
  116. )
  117. } else {
  118. callback()
  119. }
  120. },
  121. }