snapshot.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 'use strict'
  2. const assert = require('check-types').assert
  3. const BPromise = require('bluebird')
  4. const OError = require('@overleaf/o-error')
  5. const FileMap = require('./file_map')
  6. const V2DocVersions = require('./v2_doc_versions')
  7. /**
  8. * @typedef {import("./types").BlobStore} BlobStore
  9. * @typedef {import("./change")} Change
  10. * @typedef {import("./operation/text_operation")} TextOperation
  11. */
  12. /**
  13. * @classdesc A Snapshot represents the state of a {@link Project} at a
  14. * particular version.
  15. */
  16. class Snapshot {
  17. static fromRaw(raw) {
  18. assert.object(raw.files, 'bad raw.files')
  19. return new Snapshot(
  20. FileMap.fromRaw(raw.files),
  21. raw.projectVersion,
  22. V2DocVersions.fromRaw(raw.v2DocVersions)
  23. )
  24. }
  25. toRaw() {
  26. const raw = {
  27. files: this.fileMap.toRaw(),
  28. }
  29. if (this.projectVersion) raw.projectVersion = this.projectVersion
  30. if (this.v2DocVersions) raw.v2DocVersions = this.v2DocVersions.toRaw()
  31. return raw
  32. }
  33. /**
  34. * @constructor
  35. * @param {FileMap} [fileMap]
  36. * @param {string} [projectVersion]
  37. * @param {V2DocVersions} [v2DocVersions]
  38. */
  39. constructor(fileMap, projectVersion, v2DocVersions) {
  40. assert.maybe.instance(fileMap, FileMap, 'bad fileMap')
  41. this.fileMap = fileMap || new FileMap({})
  42. this.projectVersion = projectVersion
  43. this.v2DocVersions = v2DocVersions
  44. }
  45. /**
  46. * @return {string | null | undefined}
  47. */
  48. getProjectVersion() {
  49. return this.projectVersion
  50. }
  51. setProjectVersion(projectVersion) {
  52. assert.maybe.match(
  53. projectVersion,
  54. Snapshot.PROJECT_VERSION_RX,
  55. 'Snapshot: bad projectVersion'
  56. )
  57. this.projectVersion = projectVersion
  58. }
  59. /**
  60. * @return {V2DocVersions | null | undefined}
  61. */
  62. getV2DocVersions() {
  63. return this.v2DocVersions
  64. }
  65. setV2DocVersions(v2DocVersions) {
  66. assert.maybe.instance(
  67. v2DocVersions,
  68. V2DocVersions,
  69. 'Snapshot: bad v2DocVersions'
  70. )
  71. this.v2DocVersions = v2DocVersions
  72. }
  73. updateV2DocVersions(v2DocVersions) {
  74. // merge new v2DocVersions into this.v2DocVersions
  75. v2DocVersions.applyTo(this)
  76. }
  77. /**
  78. * The underlying file map.
  79. * @return {FileMap}
  80. */
  81. getFileMap() {
  82. return this.fileMap
  83. }
  84. /**
  85. * The pathnames of all of the files.
  86. *
  87. * @return {Array.<string>} in no particular order
  88. */
  89. getFilePathnames() {
  90. return this.fileMap.getPathnames()
  91. }
  92. /**
  93. * Get a File by its pathname.
  94. * @see FileMap#getFile
  95. */
  96. getFile(pathname) {
  97. return this.fileMap.getFile(pathname)
  98. }
  99. /**
  100. * Add the given file to the snapshot.
  101. * @see FileMap#addFile
  102. */
  103. addFile(pathname, file) {
  104. this.fileMap.addFile(pathname, file)
  105. }
  106. /**
  107. * Move or remove a file.
  108. * @see FileMap#moveFile
  109. */
  110. moveFile(pathname, newPathname) {
  111. this.fileMap.moveFile(pathname, newPathname)
  112. }
  113. /**
  114. * The number of files in the snapshot.
  115. *
  116. * @return {number}
  117. */
  118. countFiles() {
  119. return this.fileMap.countFiles()
  120. }
  121. /**
  122. * Edit the content of an editable file.
  123. *
  124. * Throws an error if no file with the given name exists.
  125. *
  126. * @param {string} pathname
  127. * @param {TextOperation} textOperation
  128. */
  129. editFile(pathname, textOperation) {
  130. const file = this.fileMap.getFile(pathname)
  131. if (!file) {
  132. throw new Snapshot.EditMissingFileError(
  133. `can't find file for editing: ${pathname}`
  134. )
  135. }
  136. file.edit(textOperation)
  137. }
  138. /**
  139. * Apply all changes in sequence. Modifies the snapshot in place.
  140. *
  141. * Ignore recoverable errors (caused by historical bad data) unless opts.strict is true
  142. *
  143. * @param {Change[]} changes
  144. * @param {object} opts
  145. * @param {boolean} opts.strict - do not ignore recoverable errors
  146. */
  147. applyAll(changes, opts) {
  148. for (const change of changes) {
  149. change.applyTo(this, opts)
  150. }
  151. }
  152. /**
  153. * If the Files in this Snapshot reference blob hashes, add them to the given
  154. * set.
  155. *
  156. * @param {Set.<String>} blobHashes
  157. */
  158. findBlobHashes(blobHashes) {
  159. // eslint-disable-next-line array-callback-return
  160. this.fileMap.map(file => {
  161. const hash = file.getHash()
  162. if (hash) blobHashes.add(hash)
  163. })
  164. }
  165. /**
  166. * Load all of the files in this snapshot.
  167. *
  168. * @param {string} kind see {File#load}
  169. * @param {BlobStore} blobStore
  170. * @return {Promise}
  171. */
  172. loadFiles(kind, blobStore) {
  173. return BPromise.props(this.fileMap.map(file => file.load(kind, blobStore)))
  174. }
  175. /**
  176. * Store each of the files in this snapshot and return the raw snapshot for
  177. * long term storage.
  178. *
  179. * @param {BlobStore} blobStore
  180. * @param {number} [concurrency]
  181. * @return {Promise.<Object>}
  182. */
  183. store(blobStore, concurrency) {
  184. assert.maybe.number(concurrency, 'bad concurrency')
  185. const projectVersion = this.projectVersion
  186. const rawV2DocVersions = this.v2DocVersions
  187. ? this.v2DocVersions.toRaw()
  188. : undefined
  189. return this.fileMap
  190. .mapAsync(file => file.store(blobStore), concurrency)
  191. .then(rawFiles => {
  192. return {
  193. files: rawFiles,
  194. projectVersion,
  195. v2DocVersions: rawV2DocVersions,
  196. }
  197. })
  198. }
  199. /**
  200. * Create a deep clone of this snapshot.
  201. *
  202. * @return {Snapshot}
  203. */
  204. clone() {
  205. return Snapshot.fromRaw(this.toRaw())
  206. }
  207. }
  208. class EditMissingFileError extends OError {}
  209. Snapshot.EditMissingFileError = EditMissingFileError
  210. Snapshot.PROJECT_VERSION_RX_STRING = '^[0-9]+\\.[0-9]+$'
  211. Snapshot.PROJECT_VERSION_RX = new RegExp(Snapshot.PROJECT_VERSION_RX_STRING)
  212. module.exports = Snapshot