DocstoreClient.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let DocstoreClient
  15. const request = require('request').defaults({ jar: false })
  16. const settings = require('settings-sharelatex')
  17. const Persistor = require('../../../../app/js/PersistorManager')
  18. async function streamToString(stream) {
  19. const chunks = []
  20. return new Promise((resolve, reject) => {
  21. stream.on('data', (chunk) => chunks.push(chunk))
  22. stream.on('error', reject)
  23. stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
  24. })
  25. }
  26. async function getStringFromPersistor(persistor, bucket, key) {
  27. const stream = await persistor.getObjectStream(bucket, key, {})
  28. stream.resume()
  29. return streamToString(stream)
  30. }
  31. module.exports = DocstoreClient = {
  32. createDoc(project_id, doc_id, lines, version, ranges, callback) {
  33. if (callback == null) {
  34. callback = function (error) {}
  35. }
  36. return DocstoreClient.updateDoc(
  37. project_id,
  38. doc_id,
  39. lines,
  40. version,
  41. ranges,
  42. callback
  43. )
  44. },
  45. getDoc(project_id, doc_id, qs, callback) {
  46. if (callback == null) {
  47. callback = function (error, res, body) {}
  48. }
  49. return request.get(
  50. {
  51. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
  52. json: true,
  53. qs
  54. },
  55. callback
  56. )
  57. },
  58. getAllDocs(project_id, callback) {
  59. if (callback == null) {
  60. callback = function (error, res, body) {}
  61. }
  62. return request.get(
  63. {
  64. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc`,
  65. json: true
  66. },
  67. (req, res, body) => {
  68. callback(req, res, body)
  69. }
  70. )
  71. },
  72. getAllRanges(project_id, callback) {
  73. if (callback == null) {
  74. callback = function (error, res, body) {}
  75. }
  76. return request.get(
  77. {
  78. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/ranges`,
  79. json: true
  80. },
  81. callback
  82. )
  83. },
  84. updateDoc(project_id, doc_id, lines, version, ranges, callback) {
  85. if (callback == null) {
  86. callback = function (error, res, body) {}
  87. }
  88. return request.post(
  89. {
  90. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
  91. json: {
  92. lines,
  93. version,
  94. ranges
  95. }
  96. },
  97. callback
  98. )
  99. },
  100. deleteDoc(project_id, doc_id, callback) {
  101. if (callback == null) {
  102. callback = function (error, res, body) {}
  103. }
  104. return request.del(
  105. {
  106. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`
  107. },
  108. callback
  109. )
  110. },
  111. archiveAllDoc(project_id, callback) {
  112. if (callback == null) {
  113. callback = function (error, res, body) {}
  114. }
  115. return request.post(
  116. {
  117. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/archive`
  118. },
  119. callback
  120. )
  121. },
  122. destroyAllDoc(project_id, callback) {
  123. if (callback == null) {
  124. callback = function (error, res, body) {}
  125. }
  126. return request.post(
  127. {
  128. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/destroy`
  129. },
  130. callback
  131. )
  132. },
  133. getS3Doc(project_id, doc_id, callback) {
  134. getStringFromPersistor(
  135. Persistor,
  136. settings.docstore.bucket,
  137. `${project_id}/${doc_id}`
  138. )
  139. .then((data) => {
  140. callback(null, JSON.parse(data))
  141. })
  142. .catch(callback)
  143. }
  144. }