ResourceStateManager.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS201: Simplify complex destructure assignments
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. let ResourceStateManager
  17. const Path = require('path')
  18. const fs = require('fs')
  19. const logger = require('logger-sharelatex')
  20. const settings = require('settings-sharelatex')
  21. const Errors = require('./Errors')
  22. const SafeReader = require('./SafeReader')
  23. module.exports = ResourceStateManager = {
  24. // The sync state is an identifier which must match for an
  25. // incremental update to be allowed.
  26. //
  27. // The initial value is passed in and stored on a full
  28. // compile, along with the list of resources..
  29. //
  30. // Subsequent incremental compiles must come with the same value - if
  31. // not they will be rejected with a 409 Conflict response. The
  32. // previous list of resources is returned.
  33. //
  34. // An incremental compile can only update existing files with new
  35. // content. The sync state identifier must change if any docs or
  36. // files are moved, added, deleted or renamed.
  37. SYNC_STATE_FILE: '.project-sync-state',
  38. SYNC_STATE_MAX_SIZE: 128 * 1024,
  39. saveProjectState(state, resources, basePath, callback) {
  40. if (callback == null) {
  41. callback = function(error) {}
  42. }
  43. const stateFile = Path.join(basePath, this.SYNC_STATE_FILE)
  44. if (state == null) {
  45. // remove the file if no state passed in
  46. logger.log({ state, basePath }, 'clearing sync state')
  47. return fs.unlink(stateFile, function(err) {
  48. if (err != null && err.code !== 'ENOENT') {
  49. return callback(err)
  50. } else {
  51. return callback()
  52. }
  53. })
  54. } else {
  55. logger.log({ state, basePath }, 'writing sync state')
  56. const resourceList = Array.from(resources).map(resource => resource.path)
  57. return fs.writeFile(
  58. stateFile,
  59. [...Array.from(resourceList), `stateHash:${state}`].join('\n'),
  60. callback
  61. )
  62. }
  63. },
  64. checkProjectStateMatches(state, basePath, callback) {
  65. if (callback == null) {
  66. callback = function(error, resources) {}
  67. }
  68. const stateFile = Path.join(basePath, this.SYNC_STATE_FILE)
  69. const size = this.SYNC_STATE_MAX_SIZE
  70. return SafeReader.readFile(stateFile, size, 'utf8', function(
  71. err,
  72. result,
  73. bytesRead
  74. ) {
  75. if (err != null) {
  76. return callback(err)
  77. }
  78. if (bytesRead === size) {
  79. logger.error(
  80. { file: stateFile, size, bytesRead },
  81. 'project state file truncated'
  82. )
  83. }
  84. const array =
  85. __guard__(result != null ? result.toString() : undefined, x =>
  86. x.split('\n')
  87. ) || []
  88. const adjustedLength = Math.max(array.length, 1)
  89. const resourceList = array.slice(0, adjustedLength - 1)
  90. const oldState = array[adjustedLength - 1]
  91. const newState = `stateHash:${state}`
  92. logger.log(
  93. { state, oldState, basePath, stateMatches: newState === oldState },
  94. 'checking sync state'
  95. )
  96. if (newState !== oldState) {
  97. return callback(
  98. new Errors.FilesOutOfSyncError('invalid state for incremental update')
  99. )
  100. } else {
  101. const resources = Array.from(resourceList).map(path => ({ path }))
  102. return callback(null, resources)
  103. }
  104. })
  105. },
  106. checkResourceFiles(resources, allFiles, basePath, callback) {
  107. // check the paths are all relative to current directory
  108. let file
  109. if (callback == null) {
  110. callback = function(error) {}
  111. }
  112. for (file of Array.from(resources || [])) {
  113. for (const dir of Array.from(
  114. __guard__(file != null ? file.path : undefined, x => x.split('/'))
  115. )) {
  116. if (dir === '..') {
  117. return callback(new Error('relative path in resource file list'))
  118. }
  119. }
  120. }
  121. // check if any of the input files are not present in list of files
  122. const seenFile = {}
  123. for (file of Array.from(allFiles)) {
  124. seenFile[file] = true
  125. }
  126. const missingFiles = Array.from(resources)
  127. .filter(resource => !seenFile[resource.path])
  128. .map(resource => resource.path)
  129. if ((missingFiles != null ? missingFiles.length : undefined) > 0) {
  130. logger.err(
  131. { missingFiles, basePath, allFiles, resources },
  132. 'missing input files for project'
  133. )
  134. return callback(
  135. new Errors.FilesOutOfSyncError(
  136. 'resource files missing in incremental update'
  137. )
  138. )
  139. } else {
  140. return callback()
  141. }
  142. }
  143. }
  144. function __guard__(value, transform) {
  145. return typeof value !== 'undefined' && value !== null
  146. ? transform(value)
  147. : undefined
  148. }