snapshot.js 5.5 KB

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