UpdateTranslator.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import _ from 'lodash'
  2. import Core from 'overleaf-editor-core'
  3. import * as Errors from './Errors.js'
  4. import * as OperationsCompressor from './OperationsCompressor.js'
  5. export function convertToChanges(projectId, updatesWithBlobs, callback) {
  6. let changes
  7. try {
  8. // convert update to change
  9. changes = updatesWithBlobs.map(update =>
  10. _convertToChange(projectId, update)
  11. )
  12. } catch (error1) {
  13. const error = error1
  14. if (
  15. error instanceof Errors.UpdateWithUnknownFormatError ||
  16. error instanceof Errors.UnexpectedOpTypeError
  17. ) {
  18. return callback(error)
  19. } else {
  20. throw error
  21. }
  22. }
  23. callback(null, changes)
  24. }
  25. function _convertToChange(projectId, updateWithBlob) {
  26. let operations
  27. const { update } = updateWithBlob
  28. let projectVersion = null
  29. const v2DocVersions = {}
  30. if (_isRenameUpdate(update)) {
  31. operations = [
  32. {
  33. pathname: _convertPathname(update.pathname),
  34. newPathname: _convertPathname(update.new_pathname),
  35. },
  36. ]
  37. projectVersion = update.version
  38. } else if (isAddUpdate(update)) {
  39. operations = [
  40. {
  41. pathname: _convertPathname(update.pathname),
  42. file: {
  43. hash: updateWithBlob.blobHash,
  44. },
  45. },
  46. ]
  47. projectVersion = update.version
  48. } else if (isTextUpdate(update)) {
  49. const docLength = update.meta.doc_length
  50. let pathname = update.meta.pathname
  51. pathname = _convertPathname(pathname)
  52. const builder = new TextOperationsBuilder(update.doc, docLength, pathname)
  53. // convert ops
  54. for (const op of update.op) {
  55. builder.addOp(op)
  56. } // if this throws an exception it will be caught in convertToChanges
  57. operations = builder.finish()
  58. // add doc version information if present
  59. if (update.v != null) {
  60. v2DocVersions[update.doc] = { pathname, v: update.v }
  61. }
  62. } else {
  63. const error = new Errors.UpdateWithUnknownFormatError(
  64. 'update with unknown format',
  65. { projectId, update }
  66. )
  67. throw error
  68. }
  69. let v2Authors
  70. if (update.meta.user_id === 'anonymous-user') {
  71. // history-v1 uses null to represent an anonymous author
  72. v2Authors = [null]
  73. } else {
  74. // user_id is missing on resync operations that update the contents of a doc
  75. v2Authors = _.compact([update.meta.user_id])
  76. }
  77. const rawChange = {
  78. operations,
  79. v2Authors,
  80. timestamp: new Date(update.meta.ts).toISOString(),
  81. projectVersion,
  82. v2DocVersions: Object.keys(v2DocVersions).length ? v2DocVersions : null,
  83. }
  84. if (update.meta.origin) {
  85. rawChange.origin = update.meta.origin
  86. } else if (update.meta.type === 'external' && update.meta.source) {
  87. rawChange.origin = { kind: update.meta.source }
  88. }
  89. const change = Core.Change.fromRaw(rawChange)
  90. change.operations = OperationsCompressor.compressOperations(change.operations)
  91. return change
  92. }
  93. function _isRenameUpdate(update) {
  94. return update.new_pathname != null
  95. }
  96. function _isAddDocUpdate(update) {
  97. return update.doc != null && update.docLines != null
  98. }
  99. function _isAddFileUpdate(update) {
  100. return update.file != null && update.url != null
  101. }
  102. export function isTextUpdate(update) {
  103. return (
  104. update.doc != null &&
  105. update.op != null &&
  106. update.meta.pathname != null &&
  107. update.meta.doc_length != null
  108. )
  109. }
  110. export function isProjectStructureUpdate(update) {
  111. return isAddUpdate(update) || _isRenameUpdate(update)
  112. }
  113. export function isAddUpdate(update) {
  114. return _isAddDocUpdate(update) || _isAddFileUpdate(update)
  115. }
  116. export function _convertPathname(pathname) {
  117. // Strip leading /
  118. pathname = pathname.replace(/^\//, '')
  119. // Replace \\ with _. Backslashes are no longer allowed
  120. // in projects in web, but we have some which have gone through
  121. // into history before this restriction was added. This makes
  122. // them valid for the history store.
  123. // See https://github.com/overleaf/write_latex/issues/4471
  124. pathname = pathname.replace(/\\/g, '_')
  125. // workaround for filenames containing asterisks, this will
  126. // fail if a corresponding replacement file already exists but it
  127. // would fail anyway without this attempt to fix the pathname.
  128. // See https://github.com/overleaf/sharelatex/issues/900
  129. pathname = pathname.replace(/\*/g, '__ASTERISK__')
  130. // workaround for filenames beginning with spaces
  131. // See https://github.com/overleaf/sharelatex/issues/1404
  132. // note: we have already stripped any leading slash above
  133. pathname = pathname.replace(/^ /, '__SPACE__') // handle top-level
  134. pathname = pathname.replace(/\/ /g, '/__SPACE__') // handle folders
  135. return pathname
  136. }
  137. class TextOperationsBuilder {
  138. constructor(docId, docLength, pathname) {
  139. this.operations = []
  140. this.doc_id = docId
  141. this.doc_length = docLength
  142. this.pathname = pathname
  143. }
  144. addOp(op) {
  145. let retain
  146. if (op.c != null) {
  147. return // ignore comment op
  148. }
  149. if (op.i == null && op.d == null) {
  150. throw new Errors.UnexpectedOpTypeError('unexpected op type', { op })
  151. }
  152. // We sometimes receive operations that operate at positions outside the
  153. // doc_length. Document updater coerces the position to the end of the
  154. // document. We do the same here.
  155. const pos = Math.min(op.p, this.doc_length)
  156. const textOperation = []
  157. if (pos > 0) {
  158. textOperation.push(pos)
  159. }
  160. if (op.i != null) {
  161. textOperation.push(op.i)
  162. retain = this.doc_length - pos
  163. this.doc_length += op.i.length
  164. }
  165. if (op.d != null) {
  166. textOperation.push(-op.d.length)
  167. retain = this.doc_length - pos - op.d.length
  168. this.doc_length -= op.d.length
  169. }
  170. if (retain > 0) {
  171. textOperation.push(retain)
  172. }
  173. this.pushTextOperation(textOperation)
  174. }
  175. pushTextOperation(textOperation) {
  176. this.operations.push({
  177. pathname: this.pathname,
  178. textOperation,
  179. })
  180. }
  181. finish() {
  182. return this.operations
  183. }
  184. }