HttpController.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let HttpController
  16. const UpdatesManager = require('./UpdatesManager')
  17. const DiffManager = require('./DiffManager')
  18. const PackManager = require('./PackManager')
  19. const RestoreManager = require('./RestoreManager')
  20. const logger = require('logger-sharelatex')
  21. const HealthChecker = require('./HealthChecker')
  22. const _ = require('underscore')
  23. module.exports = HttpController = {
  24. flushDoc(req, res, next) {
  25. if (next == null) {
  26. next = function (error) {}
  27. }
  28. const { doc_id } = req.params
  29. const { project_id } = req.params
  30. logger.log({ project_id, doc_id }, 'compressing doc history')
  31. return UpdatesManager.processUncompressedUpdatesWithLock(
  32. project_id,
  33. doc_id,
  34. function (error) {
  35. if (error != null) {
  36. return next(error)
  37. }
  38. return res.sendStatus(204)
  39. }
  40. )
  41. },
  42. flushProject(req, res, next) {
  43. if (next == null) {
  44. next = function (error) {}
  45. }
  46. const { project_id } = req.params
  47. logger.log({ project_id }, 'compressing project history')
  48. return UpdatesManager.processUncompressedUpdatesForProject(
  49. project_id,
  50. function (error) {
  51. if (error != null) {
  52. return next(error)
  53. }
  54. return res.sendStatus(204)
  55. }
  56. )
  57. },
  58. flushAll(req, res, next) {
  59. // limit on projects to flush or -1 for all (default)
  60. if (next == null) {
  61. next = function (error) {}
  62. }
  63. const limit = req.query.limit != null ? parseInt(req.query.limit, 10) : -1
  64. logger.log({ limit }, 'flushing all projects')
  65. return UpdatesManager.flushAll(limit, function (error, result) {
  66. if (error != null) {
  67. return next(error)
  68. }
  69. const { failed, succeeded, all } = result
  70. const status = `${succeeded.length} succeeded, ${failed.length} failed`
  71. if (limit === 0) {
  72. return res
  73. .status(200)
  74. .send(`${status}\nwould flush:\n${all.join('\n')}\n`)
  75. } else if (failed.length > 0) {
  76. logger.log({ failed, succeeded }, 'error flushing projects')
  77. return res
  78. .status(500)
  79. .send(`${status}\nfailed to flush:\n${failed.join('\n')}\n`)
  80. } else {
  81. return res
  82. .status(200)
  83. .send(
  84. `${status}\nflushed ${succeeded.length} projects of ${all.length}\n`
  85. )
  86. }
  87. })
  88. },
  89. checkDanglingUpdates(req, res, next) {
  90. if (next == null) {
  91. next = function (error) {}
  92. }
  93. logger.log('checking dangling updates')
  94. return UpdatesManager.getDanglingUpdates(function (error, result) {
  95. if (error != null) {
  96. return next(error)
  97. }
  98. if (result.length > 0) {
  99. logger.log({ dangling: result }, 'found dangling updates')
  100. return res.status(500).send(`dangling updates:\n${result.join('\n')}\n`)
  101. } else {
  102. return res.status(200).send('no dangling updates found\n')
  103. }
  104. })
  105. },
  106. checkDoc(req, res, next) {
  107. if (next == null) {
  108. next = function (error) {}
  109. }
  110. const { doc_id } = req.params
  111. const { project_id } = req.params
  112. logger.log({ project_id, doc_id }, 'checking doc history')
  113. return DiffManager.getDocumentBeforeVersion(
  114. project_id,
  115. doc_id,
  116. 1,
  117. function (error, document, rewoundUpdates) {
  118. if (error != null) {
  119. return next(error)
  120. }
  121. const broken = []
  122. for (const update of Array.from(rewoundUpdates)) {
  123. for (const op of Array.from(update.op)) {
  124. if (op.broken === true) {
  125. broken.push(op)
  126. }
  127. }
  128. }
  129. if (broken.length > 0) {
  130. return res.send(broken)
  131. } else {
  132. return res.sendStatus(204)
  133. }
  134. }
  135. )
  136. },
  137. getDiff(req, res, next) {
  138. let from, to
  139. if (next == null) {
  140. next = function (error) {}
  141. }
  142. const { doc_id } = req.params
  143. const { project_id } = req.params
  144. if (req.query.from != null) {
  145. from = parseInt(req.query.from, 10)
  146. } else {
  147. from = null
  148. }
  149. if (req.query.to != null) {
  150. to = parseInt(req.query.to, 10)
  151. } else {
  152. to = null
  153. }
  154. logger.log({ project_id, doc_id, from, to }, 'getting diff')
  155. return DiffManager.getDiff(
  156. project_id,
  157. doc_id,
  158. from,
  159. to,
  160. function (error, diff) {
  161. if (error != null) {
  162. return next(error)
  163. }
  164. return res.json({ diff })
  165. }
  166. )
  167. },
  168. getUpdates(req, res, next) {
  169. let before, min_count
  170. if (next == null) {
  171. next = function (error) {}
  172. }
  173. const { project_id } = req.params
  174. if (req.query.before != null) {
  175. before = parseInt(req.query.before, 10)
  176. }
  177. if (req.query.min_count != null) {
  178. min_count = parseInt(req.query.min_count, 10)
  179. }
  180. return UpdatesManager.getSummarizedProjectUpdates(
  181. project_id,
  182. { before, min_count },
  183. function (error, updates, nextBeforeTimestamp) {
  184. if (error != null) {
  185. return next(error)
  186. }
  187. return res.json({
  188. updates,
  189. nextBeforeTimestamp,
  190. })
  191. }
  192. )
  193. },
  194. exportProject(req, res, next) {
  195. // The project history can be huge:
  196. // - updates can weight MBs for insert/delete of full doc
  197. // - multiple updates form a pack
  198. // Flush updates per pack onto the wire.
  199. const { project_id } = req.params
  200. logger.log({ project_id }, 'exporting project history')
  201. UpdatesManager.exportProject(
  202. project_id,
  203. function (err, { updates, userIds }, confirmWrite) {
  204. const abortStreaming = req.aborted || res.finished || res.destroyed
  205. if (abortStreaming) {
  206. // Tell the producer to stop emitting data
  207. if (confirmWrite) confirmWrite(new Error('stop'))
  208. return
  209. }
  210. const hasStartedStreamingResponse = res.headersSent
  211. if (err) {
  212. logger.error({ project_id, err }, 'export failed')
  213. if (!hasStartedStreamingResponse) {
  214. // Generate a nice 500
  215. return next(err)
  216. } else {
  217. // Stop streaming
  218. return res.destroy()
  219. }
  220. }
  221. // Compose the response incrementally
  222. const isFirstWrite = !hasStartedStreamingResponse
  223. const isLastWrite = updates.length === 0
  224. if (isFirstWrite) {
  225. // The first write will emit the 200 status, headers and start of the
  226. // response payload (open array)
  227. res.setHeader('Content-Type', 'application/json')
  228. res.setHeader('Trailer', 'X-User-Ids')
  229. res.writeHead(200)
  230. res.write('[')
  231. }
  232. if (!isFirstWrite && !isLastWrite) {
  233. // Starting from the 2nd non-empty write, emit a continuing comma.
  234. // write 1: [updates1
  235. // write 2: ,updates2
  236. // write 3: ,updates3
  237. // write N: ]
  238. res.write(',')
  239. }
  240. // Every write will emit a blob onto the response stream:
  241. // '[update1,update2,...]'
  242. // ^^^^^^^^^^^^^^^^^^^
  243. res.write(JSON.stringify(updates).slice(1, -1), confirmWrite)
  244. if (isLastWrite) {
  245. // The last write will have no updates and will finish the response
  246. // payload (close array) and emit the userIds as trailer.
  247. res.addTrailers({ 'X-User-Ids': JSON.stringify(userIds) })
  248. res.end(']')
  249. }
  250. }
  251. )
  252. },
  253. restore(req, res, next) {
  254. if (next == null) {
  255. next = function (error) {}
  256. }
  257. let { doc_id, project_id, version } = req.params
  258. const user_id = req.headers['x-user-id']
  259. version = parseInt(version, 10)
  260. return RestoreManager.restoreToBeforeVersion(
  261. project_id,
  262. doc_id,
  263. version,
  264. user_id,
  265. function (error) {
  266. if (error != null) {
  267. return next(error)
  268. }
  269. return res.sendStatus(204)
  270. }
  271. )
  272. },
  273. pushDocHistory(req, res, next) {
  274. if (next == null) {
  275. next = function (error) {}
  276. }
  277. const { project_id } = req.params
  278. const { doc_id } = req.params
  279. logger.log({ project_id, doc_id }, 'pushing all finalised changes to s3')
  280. return PackManager.pushOldPacks(project_id, doc_id, function (error) {
  281. if (error != null) {
  282. return next(error)
  283. }
  284. return res.sendStatus(204)
  285. })
  286. },
  287. pullDocHistory(req, res, next) {
  288. if (next == null) {
  289. next = function (error) {}
  290. }
  291. const { project_id } = req.params
  292. const { doc_id } = req.params
  293. logger.log({ project_id, doc_id }, 'pulling all packs from s3')
  294. return PackManager.pullOldPacks(project_id, doc_id, function (error) {
  295. if (error != null) {
  296. return next(error)
  297. }
  298. return res.sendStatus(204)
  299. })
  300. },
  301. healthCheck(req, res) {
  302. return HealthChecker.check(function (err) {
  303. if (err != null) {
  304. logger.err({ err }, 'error performing health check')
  305. return res.sendStatus(500)
  306. } else {
  307. return res.sendStatus(200)
  308. }
  309. })
  310. },
  311. checkLock(req, res) {
  312. return HealthChecker.checkLock(function (err) {
  313. if (err != null) {
  314. logger.err({ err }, 'error performing lock check')
  315. return res.sendStatus(500)
  316. } else {
  317. return res.sendStatus(200)
  318. }
  319. })
  320. },
  321. }