snapshot.js 7.0 KB

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