HttpController.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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(project_id, doc_id, from, to, function (
  156. error,
  157. diff
  158. ) {
  159. if (error != null) {
  160. return next(error)
  161. }
  162. return res.json({ diff })
  163. })
  164. },
  165. getUpdates(req, res, next) {
  166. let before, min_count
  167. if (next == null) {
  168. next = function (error) {}
  169. }
  170. const { project_id } = req.params
  171. if (req.query.before != null) {
  172. before = parseInt(req.query.before, 10)
  173. }
  174. if (req.query.min_count != null) {
  175. min_count = parseInt(req.query.min_count, 10)
  176. }
  177. return UpdatesManager.getSummarizedProjectUpdates(
  178. project_id,
  179. { before, min_count },
  180. function (error, updates, nextBeforeTimestamp) {
  181. if (error != null) {
  182. return next(error)
  183. }
  184. return res.json({
  185. updates,
  186. nextBeforeTimestamp
  187. })
  188. }
  189. )
  190. },
  191. exportProject(req, res, next) {
  192. // The project history can be huge:
  193. // - updates can weight MBs for insert/delete of full doc
  194. // - multiple updates form a pack
  195. // Flush updates per pack onto the wire.
  196. const { project_id } = req.params
  197. logger.log({ project_id }, 'exporting project history')
  198. UpdatesManager.exportProject(project_id, function (
  199. err,
  200. { updates, userIds },
  201. confirmWrite
  202. ) {
  203. const abortStreaming = req.aborted || res.finished || res.destroyed
  204. if (abortStreaming) {
  205. // Tell the producer to stop emitting data
  206. if (confirmWrite) confirmWrite(new Error('stop'))
  207. return
  208. }
  209. const hasStartedStreamingResponse = res.headersSent
  210. if (err) {
  211. logger.error({ project_id, err }, 'export failed')
  212. if (!hasStartedStreamingResponse) {
  213. // Generate a nice 500
  214. return next(err)
  215. } else {
  216. // Stop streaming
  217. return res.destroy()
  218. }
  219. }
  220. // Compose the response incrementally
  221. const isFirstWrite = !hasStartedStreamingResponse
  222. const isLastWrite = updates.length === 0
  223. if (isFirstWrite) {
  224. // The first write will emit the 200 status, headers and start of the
  225. // response payload (open array)
  226. res.setHeader('Content-Type', 'application/json')
  227. res.setHeader('Trailer', 'X-User-Ids')
  228. res.writeHead(200)
  229. res.write('[')
  230. }
  231. if (!isFirstWrite && !isLastWrite) {
  232. // Starting from the 2nd non-empty write, emit a continuing comma.
  233. // write 1: [updates1
  234. // write 2: ,updates2
  235. // write 3: ,updates3
  236. // write N: ]
  237. res.write(',')
  238. }
  239. // Every write will emit a blob onto the response stream:
  240. // '[update1,update2,...]'
  241. // ^^^^^^^^^^^^^^^^^^^
  242. res.write(JSON.stringify(updates).slice(1, -1), confirmWrite)
  243. if (isLastWrite) {
  244. // The last write will have no updates and will finish the response
  245. // payload (close array) and emit the userIds as trailer.
  246. res.addTrailers({ 'X-User-Ids': JSON.stringify(userIds) })
  247. res.end(']')
  248. }
  249. })
  250. },
  251. restore(req, res, next) {
  252. if (next == null) {
  253. next = function (error) {}
  254. }
  255. let { doc_id, project_id, version } = req.params
  256. const user_id = req.headers['x-user-id']
  257. version = parseInt(version, 10)
  258. return RestoreManager.restoreToBeforeVersion(
  259. project_id,
  260. doc_id,
  261. version,
  262. user_id,
  263. function (error) {
  264. if (error != null) {
  265. return next(error)
  266. }
  267. return res.sendStatus(204)
  268. }
  269. )
  270. },
  271. pushDocHistory(req, res, next) {
  272. if (next == null) {
  273. next = function (error) {}
  274. }
  275. const { project_id } = req.params
  276. const { doc_id } = req.params
  277. logger.log({ project_id, doc_id }, 'pushing all finalised changes to s3')
  278. return PackManager.pushOldPacks(project_id, doc_id, function (error) {
  279. if (error != null) {
  280. return next(error)
  281. }
  282. return res.sendStatus(204)
  283. })
  284. },
  285. pullDocHistory(req, res, next) {
  286. if (next == null) {
  287. next = function (error) {}
  288. }
  289. const { project_id } = req.params
  290. const { doc_id } = req.params
  291. logger.log({ project_id, doc_id }, 'pulling all packs from s3')
  292. return PackManager.pullOldPacks(project_id, doc_id, function (error) {
  293. if (error != null) {
  294. return next(error)
  295. }
  296. return res.sendStatus(204)
  297. })
  298. },
  299. healthCheck(req, res) {
  300. return HealthChecker.check(function (err) {
  301. if (err != null) {
  302. logger.err({ err }, 'error performing health check')
  303. return res.sendStatus(500)
  304. } else {
  305. return res.sendStatus(200)
  306. }
  307. })
  308. },
  309. checkLock(req, res) {
  310. return HealthChecker.checkLock(function (err) {
  311. if (err != null) {
  312. logger.err({ err }, 'error performing lock check')
  313. return res.sendStatus(500)
  314. } else {
  315. return res.sendStatus(200)
  316. }
  317. })
  318. }
  319. }