HttpController.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS103: Rewrite code to no longer use __guard__
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let HttpController
  15. const DocumentManager = require('./DocumentManager')
  16. const HistoryManager = require('./HistoryManager')
  17. const ProjectManager = require('./ProjectManager')
  18. const Errors = require('./Errors')
  19. const logger = require('logger-sharelatex')
  20. const Metrics = require('./Metrics')
  21. const ProjectFlusher = require('./ProjectFlusher')
  22. const DeleteQueueManager = require('./DeleteQueueManager')
  23. const async = require('async')
  24. const TWO_MEGABYTES = 2 * 1024 * 1024
  25. module.exports = HttpController = {
  26. getDoc(req, res, next) {
  27. let fromVersion
  28. if (next == null) {
  29. next = function (error) {}
  30. }
  31. const { doc_id } = req.params
  32. const { project_id } = req.params
  33. logger.log({ project_id, doc_id }, 'getting doc via http')
  34. const timer = new Metrics.Timer('http.getDoc')
  35. if ((req.query != null ? req.query.fromVersion : undefined) != null) {
  36. fromVersion = parseInt(req.query.fromVersion, 10)
  37. } else {
  38. fromVersion = -1
  39. }
  40. return DocumentManager.getDocAndRecentOpsWithLock(
  41. project_id,
  42. doc_id,
  43. fromVersion,
  44. function (error, lines, version, ops, ranges, pathname) {
  45. timer.done()
  46. if (error != null) {
  47. return next(error)
  48. }
  49. logger.log({ project_id, doc_id }, 'got doc via http')
  50. if (lines == null || version == null) {
  51. return next(new Errors.NotFoundError('document not found'))
  52. }
  53. return res.json({
  54. id: doc_id,
  55. lines,
  56. version,
  57. ops,
  58. ranges,
  59. pathname
  60. })
  61. }
  62. )
  63. },
  64. _getTotalSizeOfLines(lines) {
  65. let size = 0
  66. for (const line of lines) {
  67. size += line.length + 1
  68. }
  69. return size
  70. },
  71. getProjectDocsAndFlushIfOld(req, res, next) {
  72. if (next == null) {
  73. next = function (error) {}
  74. }
  75. const { project_id } = req.params
  76. const projectStateHash = req.query != null ? req.query.state : undefined
  77. // exclude is string of existing docs "id:version,id:version,..."
  78. const excludeItems =
  79. __guard__(req.query != null ? req.query.exclude : undefined, (x) =>
  80. x.split(',')
  81. ) || []
  82. logger.log({ project_id, exclude: excludeItems }, 'getting docs via http')
  83. const timer = new Metrics.Timer('http.getAllDocs')
  84. const excludeVersions = {}
  85. for (const item of excludeItems) {
  86. const [id, version] = item.split(':')
  87. excludeVersions[id] = version
  88. }
  89. logger.log(
  90. { project_id, projectStateHash, excludeVersions },
  91. 'excluding versions'
  92. )
  93. return ProjectManager.getProjectDocsAndFlushIfOld(
  94. project_id,
  95. projectStateHash,
  96. excludeVersions,
  97. function (error, result) {
  98. timer.done()
  99. if (error instanceof Errors.ProjectStateChangedError) {
  100. return res.sendStatus(409) // conflict
  101. } else if (error != null) {
  102. return next(error)
  103. } else {
  104. logger.log(
  105. {
  106. project_id,
  107. result: result.map((doc) => `${doc._id}:${doc.v}`)
  108. },
  109. 'got docs via http'
  110. )
  111. return res.send(result)
  112. }
  113. }
  114. )
  115. },
  116. clearProjectState(req, res, next) {
  117. if (next == null) {
  118. next = function (error) {}
  119. }
  120. const { project_id } = req.params
  121. const timer = new Metrics.Timer('http.clearProjectState')
  122. logger.log({ project_id }, 'clearing project state via http')
  123. return ProjectManager.clearProjectState(project_id, function (error) {
  124. timer.done()
  125. if (error != null) {
  126. return next(error)
  127. } else {
  128. return res.sendStatus(200)
  129. }
  130. })
  131. },
  132. setDoc(req, res, next) {
  133. if (next == null) {
  134. next = function (error) {}
  135. }
  136. const { doc_id } = req.params
  137. const { project_id } = req.params
  138. const { lines, source, user_id, undoing } = req.body
  139. const lineSize = HttpController._getTotalSizeOfLines(lines)
  140. if (lineSize > TWO_MEGABYTES) {
  141. logger.log(
  142. { project_id, doc_id, source, lineSize, user_id },
  143. 'document too large, returning 406 response'
  144. )
  145. return res.sendStatus(406)
  146. }
  147. logger.log(
  148. { project_id, doc_id, lines, source, user_id, undoing },
  149. 'setting doc via http'
  150. )
  151. const timer = new Metrics.Timer('http.setDoc')
  152. return DocumentManager.setDocWithLock(
  153. project_id,
  154. doc_id,
  155. lines,
  156. source,
  157. user_id,
  158. undoing,
  159. function (error) {
  160. timer.done()
  161. if (error != null) {
  162. return next(error)
  163. }
  164. logger.log({ project_id, doc_id }, 'set doc via http')
  165. return res.sendStatus(204)
  166. }
  167. )
  168. }, // No Content
  169. flushDocIfLoaded(req, res, next) {
  170. if (next == null) {
  171. next = function (error) {}
  172. }
  173. const { doc_id } = req.params
  174. const { project_id } = req.params
  175. logger.log({ project_id, doc_id }, 'flushing doc via http')
  176. const timer = new Metrics.Timer('http.flushDoc')
  177. return DocumentManager.flushDocIfLoadedWithLock(
  178. project_id,
  179. doc_id,
  180. function (error) {
  181. timer.done()
  182. if (error != null) {
  183. return next(error)
  184. }
  185. logger.log({ project_id, doc_id }, 'flushed doc via http')
  186. return res.sendStatus(204)
  187. }
  188. )
  189. }, // No Content
  190. deleteDoc(req, res, next) {
  191. if (next == null) {
  192. next = function (error) {}
  193. }
  194. const { doc_id } = req.params
  195. const { project_id } = req.params
  196. const ignoreFlushErrors = req.query.ignore_flush_errors === 'true'
  197. const timer = new Metrics.Timer('http.deleteDoc')
  198. logger.log({ project_id, doc_id }, 'deleting doc via http')
  199. return DocumentManager.flushAndDeleteDocWithLock(
  200. project_id,
  201. doc_id,
  202. { ignoreFlushErrors },
  203. function (error) {
  204. timer.done()
  205. // There is no harm in flushing project history if the previous call
  206. // failed and sometimes it is required
  207. HistoryManager.flushProjectChangesAsync(project_id)
  208. if (error != null) {
  209. return next(error)
  210. }
  211. logger.log({ project_id, doc_id }, 'deleted doc via http')
  212. return res.sendStatus(204)
  213. }
  214. )
  215. }, // No Content
  216. flushProject(req, res, next) {
  217. if (next == null) {
  218. next = function (error) {}
  219. }
  220. const { project_id } = req.params
  221. logger.log({ project_id }, 'flushing project via http')
  222. const timer = new Metrics.Timer('http.flushProject')
  223. return ProjectManager.flushProjectWithLocks(project_id, function (error) {
  224. timer.done()
  225. if (error != null) {
  226. return next(error)
  227. }
  228. logger.log({ project_id }, 'flushed project via http')
  229. return res.sendStatus(204)
  230. })
  231. }, // No Content
  232. deleteProject(req, res, next) {
  233. if (next == null) {
  234. next = function (error) {}
  235. }
  236. const { project_id } = req.params
  237. logger.log({ project_id }, 'deleting project via http')
  238. const options = {}
  239. if (req.query != null ? req.query.background : undefined) {
  240. options.background = true
  241. } // allow non-urgent flushes to be queued
  242. if (req.query != null ? req.query.shutdown : undefined) {
  243. options.skip_history_flush = true
  244. } // don't flush history when realtime shuts down
  245. if (req.query != null ? req.query.background : undefined) {
  246. return ProjectManager.queueFlushAndDeleteProject(project_id, function (
  247. error
  248. ) {
  249. if (error != null) {
  250. return next(error)
  251. }
  252. logger.log({ project_id }, 'queue delete of project via http')
  253. return res.sendStatus(204)
  254. }) // No Content
  255. } else {
  256. const timer = new Metrics.Timer('http.deleteProject')
  257. return ProjectManager.flushAndDeleteProjectWithLocks(
  258. project_id,
  259. options,
  260. function (error) {
  261. timer.done()
  262. if (error != null) {
  263. return next(error)
  264. }
  265. logger.log({ project_id }, 'deleted project via http')
  266. return res.sendStatus(204)
  267. }
  268. )
  269. }
  270. }, // No Content
  271. deleteMultipleProjects(req, res, next) {
  272. if (next == null) {
  273. next = function (error) {}
  274. }
  275. const project_ids =
  276. (req.body != null ? req.body.project_ids : undefined) || []
  277. logger.log({ project_ids }, 'deleting multiple projects via http')
  278. return async.eachSeries(
  279. project_ids,
  280. function (project_id, cb) {
  281. logger.log({ project_id }, 'queue delete of project via http')
  282. return ProjectManager.queueFlushAndDeleteProject(project_id, cb)
  283. },
  284. function (error) {
  285. if (error != null) {
  286. return next(error)
  287. }
  288. return res.sendStatus(204)
  289. }
  290. )
  291. }, // No Content
  292. acceptChanges(req, res, next) {
  293. if (next == null) {
  294. next = function (error) {}
  295. }
  296. const { project_id, doc_id } = req.params
  297. let change_ids = req.body != null ? req.body.change_ids : undefined
  298. if (change_ids == null) {
  299. change_ids = [req.params.change_id]
  300. }
  301. logger.log(
  302. { project_id, doc_id },
  303. `accepting ${change_ids.length} changes via http`
  304. )
  305. const timer = new Metrics.Timer('http.acceptChanges')
  306. return DocumentManager.acceptChangesWithLock(
  307. project_id,
  308. doc_id,
  309. change_ids,
  310. function (error) {
  311. timer.done()
  312. if (error != null) {
  313. return next(error)
  314. }
  315. logger.log(
  316. { project_id, doc_id },
  317. `accepted ${change_ids.length} changes via http`
  318. )
  319. return res.sendStatus(204)
  320. }
  321. )
  322. }, // No Content
  323. deleteComment(req, res, next) {
  324. if (next == null) {
  325. next = function (error) {}
  326. }
  327. const { project_id, doc_id, comment_id } = req.params
  328. logger.log({ project_id, doc_id, comment_id }, 'deleting comment via http')
  329. const timer = new Metrics.Timer('http.deleteComment')
  330. return DocumentManager.deleteCommentWithLock(
  331. project_id,
  332. doc_id,
  333. comment_id,
  334. function (error) {
  335. timer.done()
  336. if (error != null) {
  337. return next(error)
  338. }
  339. logger.log(
  340. { project_id, doc_id, comment_id },
  341. 'deleted comment via http'
  342. )
  343. return res.sendStatus(204)
  344. }
  345. )
  346. }, // No Content
  347. updateProject(req, res, next) {
  348. if (next == null) {
  349. next = function (error) {}
  350. }
  351. const timer = new Metrics.Timer('http.updateProject')
  352. const { project_id } = req.params
  353. const {
  354. projectHistoryId,
  355. userId,
  356. docUpdates,
  357. fileUpdates,
  358. version
  359. } = req.body
  360. logger.log(
  361. { project_id, docUpdates, fileUpdates, version },
  362. 'updating project via http'
  363. )
  364. return ProjectManager.updateProjectWithLocks(
  365. project_id,
  366. projectHistoryId,
  367. userId,
  368. docUpdates,
  369. fileUpdates,
  370. version,
  371. function (error) {
  372. timer.done()
  373. if (error != null) {
  374. return next(error)
  375. }
  376. logger.log({ project_id }, 'updated project via http')
  377. return res.sendStatus(204)
  378. }
  379. )
  380. }, // No Content
  381. resyncProjectHistory(req, res, next) {
  382. if (next == null) {
  383. next = function (error) {}
  384. }
  385. const { project_id } = req.params
  386. const { projectHistoryId, docs, files } = req.body
  387. logger.log(
  388. { project_id, docs, files },
  389. 'queuing project history resync via http'
  390. )
  391. return HistoryManager.resyncProjectHistory(
  392. project_id,
  393. projectHistoryId,
  394. docs,
  395. files,
  396. function (error) {
  397. if (error != null) {
  398. return next(error)
  399. }
  400. logger.log({ project_id }, 'queued project history resync via http')
  401. return res.sendStatus(204)
  402. }
  403. )
  404. },
  405. flushAllProjects(req, res, next) {
  406. if (next == null) {
  407. next = function (error) {}
  408. }
  409. res.setTimeout(5 * 60 * 1000)
  410. const options = {
  411. limit: req.query.limit || 1000,
  412. concurrency: req.query.concurrency || 5,
  413. dryRun: req.query.dryRun || false
  414. }
  415. return ProjectFlusher.flushAllProjects(options, function (
  416. err,
  417. project_ids
  418. ) {
  419. if (err != null) {
  420. logger.err({ err }, 'error bulk flushing projects')
  421. return res.sendStatus(500)
  422. } else {
  423. return res.send(project_ids)
  424. }
  425. })
  426. },
  427. flushQueuedProjects(req, res, next) {
  428. if (next == null) {
  429. next = function (error) {}
  430. }
  431. res.setTimeout(10 * 60 * 1000)
  432. const options = {
  433. limit: req.query.limit || 1000,
  434. timeout: 5 * 60 * 1000,
  435. min_delete_age: req.query.min_delete_age || 5 * 60 * 1000
  436. }
  437. return DeleteQueueManager.flushAndDeleteOldProjects(options, function (
  438. err,
  439. flushed
  440. ) {
  441. if (err != null) {
  442. logger.err({ err }, 'error flushing old projects')
  443. return res.sendStatus(500)
  444. } else {
  445. logger.log({ flushed }, 'flush of queued projects completed')
  446. return res.send({ flushed })
  447. }
  448. })
  449. }
  450. }
  451. function __guard__(value, transform) {
  452. return typeof value !== 'undefined' && value !== null
  453. ? transform(value)
  454. : undefined
  455. }