DocumentUpdaterManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. let DocumentUpdaterManager
  13. const request = require('request')
  14. const logger = require('@overleaf/logger')
  15. const Settings = require('@overleaf/settings')
  16. const Errors = require('./Errors')
  17. module.exports = DocumentUpdaterManager = {
  18. _requestDocument(projectId, docId, url, callback) {
  19. if (callback == null) {
  20. callback = function () {}
  21. }
  22. logger.debug({ projectId, docId }, 'getting doc from document updater')
  23. return request.get(url, function (error, res, body) {
  24. if (error != null) {
  25. return callback(error)
  26. }
  27. if (res.statusCode >= 200 && res.statusCode < 300) {
  28. try {
  29. body = JSON.parse(body)
  30. } catch (error1) {
  31. error = error1
  32. return callback(error)
  33. }
  34. logger.debug(
  35. { projectId, docId, version: body.version },
  36. 'got doc from document updater'
  37. )
  38. return callback(null, body.lines.join('\n'), body.version)
  39. } else {
  40. error = new Error(
  41. `doc updater returned a non-success status code: ${res.statusCode}`
  42. )
  43. logger.error(
  44. { err: error, projectId, docId, url },
  45. 'error accessing doc updater'
  46. )
  47. if (res.statusCode === 404) {
  48. return callback(
  49. new Errors.NotFoundError('doc not found', {
  50. projectId,
  51. docId,
  52. })
  53. )
  54. } else {
  55. return callback(error)
  56. }
  57. }
  58. })
  59. },
  60. getDocument(projectId, docId, callback) {
  61. const url = `${Settings.apis.documentupdater.url}/project/${projectId}/doc/${docId}`
  62. DocumentUpdaterManager._requestDocument(projectId, docId, url, callback)
  63. },
  64. peekDocument(projectId, docId, callback) {
  65. const url = `${Settings.apis.documentupdater.url}/project/${projectId}/doc/${docId}/peek`
  66. DocumentUpdaterManager._requestDocument(projectId, docId, url, callback)
  67. },
  68. setDocument(projectId, docId, content, userId, callback) {
  69. if (callback == null) {
  70. callback = function () {}
  71. }
  72. const url = `${Settings.apis.documentupdater.url}/project/${projectId}/doc/${docId}`
  73. logger.debug({ projectId, docId }, 'setting doc in document updater')
  74. return request.post(
  75. {
  76. url,
  77. json: {
  78. lines: content.split('\n'),
  79. source: 'restore',
  80. user_id: userId,
  81. undoing: true,
  82. },
  83. },
  84. function (error, res, body) {
  85. if (error != null) {
  86. return callback(error)
  87. }
  88. if (res.statusCode >= 200 && res.statusCode < 300) {
  89. return callback(null)
  90. } else {
  91. error = new Error(
  92. `doc updater returned a non-success status code: ${res.statusCode}`
  93. )
  94. logger.error(
  95. { err: error, projectId, docId, url },
  96. 'error accessing doc updater'
  97. )
  98. return callback(error)
  99. }
  100. }
  101. )
  102. },
  103. }
  104. module.exports.promises = {
  105. // peekDocument returns two arguments so we can't use util.promisify, which only handles a single argument, we need
  106. // to treat this it as a special case.
  107. peekDocument: (projectId, docId) => {
  108. return new Promise((resolve, reject) => {
  109. DocumentUpdaterManager.peekDocument(
  110. projectId,
  111. docId,
  112. (err, content, version) => {
  113. if (err) {
  114. reject(err)
  115. } else {
  116. resolve([content, version])
  117. }
  118. }
  119. )
  120. })
  121. },
  122. }