DocUpdaterClient.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let DocUpdaterClient
  14. const Settings = require('settings-sharelatex')
  15. const rclient = require('redis-sharelatex').createClient(
  16. Settings.redis.documentupdater
  17. )
  18. const keys = Settings.redis.documentupdater.key_schema
  19. const request = require('request').defaults({ jar: false })
  20. const async = require('async')
  21. const rclient_sub = require('redis-sharelatex').createClient(
  22. Settings.redis.pubsub
  23. )
  24. rclient_sub.subscribe('applied-ops')
  25. rclient_sub.setMaxListeners(0)
  26. module.exports = DocUpdaterClient = {
  27. randomId() {
  28. const chars = __range__(1, 24, true).map(
  29. (i) => Math.random().toString(16)[2]
  30. )
  31. return chars.join('')
  32. },
  33. subscribeToAppliedOps(callback) {
  34. return rclient_sub.on('message', callback)
  35. },
  36. sendUpdate(project_id, doc_id, update, callback) {
  37. return rclient.rpush(
  38. keys.pendingUpdates({ doc_id }),
  39. JSON.stringify(update),
  40. (error) => {
  41. if (error) {
  42. return callback(error)
  43. }
  44. const doc_key = `${project_id}:${doc_id}`
  45. return rclient.sadd('DocsWithPendingUpdates', doc_key, (error) => {
  46. if (error) {
  47. return callback(error)
  48. }
  49. return rclient.rpush('pending-updates-list', doc_key, callback)
  50. })
  51. }
  52. )
  53. },
  54. sendUpdates(project_id, doc_id, updates, callback) {
  55. return DocUpdaterClient.preloadDoc(project_id, doc_id, (error) => {
  56. if (error) {
  57. return callback(error)
  58. }
  59. const jobs = []
  60. for (const update of Array.from(updates)) {
  61. ;((update) =>
  62. jobs.push((callback) =>
  63. DocUpdaterClient.sendUpdate(project_id, doc_id, update, callback)
  64. ))(update)
  65. }
  66. return async.series(jobs, (err) =>
  67. DocUpdaterClient.waitForPendingUpdates(project_id, doc_id, callback)
  68. )
  69. })
  70. },
  71. waitForPendingUpdates(project_id, doc_id, callback) {
  72. return async.retry(
  73. { times: 30, interval: 100 },
  74. (cb) =>
  75. rclient.llen(keys.pendingUpdates({ doc_id }), (err, length) => {
  76. if (length > 0) {
  77. return cb(new Error('updates still pending'))
  78. } else {
  79. return cb()
  80. }
  81. }),
  82. callback
  83. )
  84. },
  85. getDoc(project_id, doc_id, callback) {
  86. return request.get(
  87. `http://localhost:3003/project/${project_id}/doc/${doc_id}`,
  88. (error, res, body) => {
  89. if (body != null && res.statusCode >= 200 && res.statusCode < 300) {
  90. body = JSON.parse(body)
  91. }
  92. return callback(error, res, body)
  93. }
  94. )
  95. },
  96. getDocAndRecentOps(project_id, doc_id, fromVersion, callback) {
  97. return request.get(
  98. `http://localhost:3003/project/${project_id}/doc/${doc_id}?fromVersion=${fromVersion}`,
  99. (error, res, body) => {
  100. if (body != null && res.statusCode >= 200 && res.statusCode < 300) {
  101. body = JSON.parse(body)
  102. }
  103. return callback(error, res, body)
  104. }
  105. )
  106. },
  107. preloadDoc(project_id, doc_id, callback) {
  108. return DocUpdaterClient.getDoc(project_id, doc_id, callback)
  109. },
  110. flushDoc(project_id, doc_id, callback) {
  111. return request.post(
  112. `http://localhost:3003/project/${project_id}/doc/${doc_id}/flush`,
  113. (error, res, body) => callback(error, res, body)
  114. )
  115. },
  116. setDocLines(project_id, doc_id, lines, source, user_id, undoing, callback) {
  117. return request.post(
  118. {
  119. url: `http://localhost:3003/project/${project_id}/doc/${doc_id}`,
  120. json: {
  121. lines,
  122. source,
  123. user_id,
  124. undoing
  125. }
  126. },
  127. (error, res, body) => callback(error, res, body)
  128. )
  129. },
  130. deleteDoc(project_id, doc_id, callback) {
  131. return request.del(
  132. `http://localhost:3003/project/${project_id}/doc/${doc_id}`,
  133. (error, res, body) => callback(error, res, body)
  134. )
  135. },
  136. flushProject(project_id, callback) {
  137. return request.post(
  138. `http://localhost:3003/project/${project_id}/flush`,
  139. callback
  140. )
  141. },
  142. deleteProject(project_id, callback) {
  143. return request.del(`http://localhost:3003/project/${project_id}`, callback)
  144. },
  145. deleteProjectOnShutdown(project_id, callback) {
  146. return request.del(
  147. `http://localhost:3003/project/${project_id}?background=true&shutdown=true`,
  148. callback
  149. )
  150. },
  151. flushOldProjects(callback) {
  152. return request.get(
  153. 'http://localhost:3003/flush_queued_projects?min_delete_age=1',
  154. callback
  155. )
  156. },
  157. acceptChange(project_id, doc_id, change_id, callback) {
  158. return request.post(
  159. `http://localhost:3003/project/${project_id}/doc/${doc_id}/change/${change_id}/accept`,
  160. callback
  161. )
  162. },
  163. removeComment(project_id, doc_id, comment, callback) {
  164. return request.del(
  165. `http://localhost:3003/project/${project_id}/doc/${doc_id}/comment/${comment}`,
  166. callback
  167. )
  168. },
  169. getProjectDocs(project_id, projectStateHash, callback) {
  170. return request.get(
  171. `http://localhost:3003/project/${project_id}/doc?state=${projectStateHash}`,
  172. (error, res, body) => {
  173. if (body != null && res.statusCode >= 200 && res.statusCode < 300) {
  174. body = JSON.parse(body)
  175. }
  176. return callback(error, res, body)
  177. }
  178. )
  179. },
  180. sendProjectUpdate(
  181. project_id,
  182. userId,
  183. docUpdates,
  184. fileUpdates,
  185. version,
  186. callback
  187. ) {
  188. return request.post(
  189. {
  190. url: `http://localhost:3003/project/${project_id}`,
  191. json: { userId, docUpdates, fileUpdates, version }
  192. },
  193. (error, res, body) => callback(error, res, body)
  194. )
  195. }
  196. }
  197. function __range__(left, right, inclusive) {
  198. const range = []
  199. const ascending = left < right
  200. const end = !inclusive ? right : ascending ? right + 1 : right - 1
  201. for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
  202. range.push(i)
  203. }
  204. return range
  205. }