UpdateTranslator.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. const rawChange = {
  70. operations,
  71. v2Authors: _.compact([update.meta.user_id]),
  72. timestamp: new Date(update.meta.ts).toISOString(),
  73. projectVersion,
  74. v2DocVersions: Object.keys(v2DocVersions).length ? v2DocVersions : null,
  75. }
  76. if (update.meta.origin) {
  77. rawChange.origin = update.meta.origin
  78. } else if (update.meta.type === 'external' && update.meta.source) {
  79. rawChange.origin = { kind: update.meta.source }
  80. }
  81. const change = Core.Change.fromRaw(rawChange)
  82. change.operations = OperationsCompressor.compressOperations(change.operations)
  83. return change
  84. }
  85. function _isRenameUpdate(update) {
  86. return update.new_pathname != null
  87. }
  88. function _isAddDocUpdate(update) {
  89. return update.doc != null && update.docLines != null
  90. }
  91. function _isAddFileUpdate(update) {
  92. return update.file != null && update.url != null
  93. }
  94. export function isTextUpdate(update) {
  95. return (
  96. update.doc != null &&
  97. update.op != null &&
  98. update.meta.pathname != null &&
  99. update.meta.doc_length != null
  100. )
  101. }
  102. export function isProjectStructureUpdate(update) {
  103. return isAddUpdate(update) || _isRenameUpdate(update)
  104. }
  105. export function isAddUpdate(update) {
  106. return _isAddDocUpdate(update) || _isAddFileUpdate(update)
  107. }
  108. export function _convertPathname(pathname) {
  109. // Strip leading /
  110. pathname = pathname.replace(/^\//, '')
  111. // Replace \\ with _. Backslashes are no longer allowed
  112. // in projects in web, but we have some which have gone through
  113. // into history before this restriction was added. This makes
  114. // them valid for the history store.
  115. // See https://github.com/overleaf/write_latex/issues/4471
  116. pathname = pathname.replace(/\\/g, '_')
  117. // workaround for filenames containing asterisks, this will
  118. // fail if a corresponding replacement file already exists but it
  119. // would fail anyway without this attempt to fix the pathname.
  120. // See https://github.com/overleaf/sharelatex/issues/900
  121. pathname = pathname.replace(/\*/g, '__ASTERISK__')
  122. // workaround for filenames beginning with spaces
  123. // See https://github.com/overleaf/sharelatex/issues/1404
  124. // note: we have already stripped any leading slash above
  125. pathname = pathname.replace(/^ /, '__SPACE__') // handle top-level
  126. pathname = pathname.replace(/\/ /g, '/__SPACE__') // handle folders
  127. return pathname
  128. }
  129. class TextOperationsBuilder {
  130. constructor(docId, docLength, pathname) {
  131. this.operations = []
  132. this.doc_id = docId
  133. this.doc_length = docLength
  134. this.pathname = pathname
  135. }
  136. addOp(op) {
  137. let retain
  138. if (op.c != null) {
  139. return // ignore comment op
  140. }
  141. if (op.i == null && op.d == null) {
  142. throw new Errors.UnexpectedOpTypeError('unexpected op type', { op })
  143. }
  144. // We sometimes receive operations that operate at positions outside the
  145. // doc_length. Document updater coerces the position to the end of the
  146. // document. We do the same here.
  147. const pos = Math.min(op.p, this.doc_length)
  148. const textOperation = []
  149. if (pos > 0) {
  150. textOperation.push(pos)
  151. }
  152. if (op.i != null) {
  153. textOperation.push(op.i)
  154. retain = this.doc_length - pos
  155. this.doc_length += op.i.length
  156. }
  157. if (op.d != null) {
  158. textOperation.push(-op.d.length)
  159. retain = this.doc_length - pos - op.d.length
  160. this.doc_length -= op.d.length
  161. }
  162. if (retain > 0) {
  163. textOperation.push(retain)
  164. }
  165. this.pushTextOperation(textOperation)
  166. }
  167. pushTextOperation(textOperation) {
  168. this.operations.push({
  169. pathname: this.pathname,
  170. textOperation,
  171. })
  172. }
  173. finish() {
  174. return this.operations
  175. }
  176. }