DocstoreClient.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. isDocDeleted(project_id, doc_id, callback) {
  59. request.get(
  60. {
  61. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/deleted`,
  62. json: true
  63. },
  64. callback
  65. )
  66. },
  67. getAllDocs(project_id, callback) {
  68. if (callback == null) {
  69. callback = function (error, res, body) {}
  70. }
  71. return request.get(
  72. {
  73. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc`,
  74. json: true
  75. },
  76. (req, res, body) => {
  77. callback(req, res, body)
  78. }
  79. )
  80. },
  81. getAllRanges(project_id, callback) {
  82. if (callback == null) {
  83. callback = function (error, res, body) {}
  84. }
  85. return request.get(
  86. {
  87. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/ranges`,
  88. json: true
  89. },
  90. callback
  91. )
  92. },
  93. updateDoc(project_id, doc_id, lines, version, ranges, callback) {
  94. if (callback == null) {
  95. callback = function (error, res, body) {}
  96. }
  97. return request.post(
  98. {
  99. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
  100. json: {
  101. lines,
  102. version,
  103. ranges
  104. }
  105. },
  106. callback
  107. )
  108. },
  109. deleteDoc(project_id, doc_id, callback) {
  110. if (callback == null) {
  111. callback = function (error, res, body) {}
  112. }
  113. return request.del(
  114. {
  115. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`
  116. },
  117. callback
  118. )
  119. },
  120. archiveAllDoc(project_id, callback) {
  121. if (callback == null) {
  122. callback = function (error, res, body) {}
  123. }
  124. return request.post(
  125. {
  126. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/archive`
  127. },
  128. callback
  129. )
  130. },
  131. archiveDocById(project_id, doc_id, callback) {
  132. if (callback == null) {
  133. callback = function (error, res, body) {}
  134. }
  135. return request.post(
  136. {
  137. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/archive`
  138. },
  139. callback
  140. )
  141. },
  142. destroyAllDoc(project_id, callback) {
  143. if (callback == null) {
  144. callback = function (error, res, body) {}
  145. }
  146. return request.post(
  147. {
  148. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/destroy`
  149. },
  150. callback
  151. )
  152. },
  153. getS3Doc(project_id, doc_id, callback) {
  154. getStringFromPersistor(
  155. Persistor,
  156. settings.docstore.bucket,
  157. `${project_id}/${doc_id}`
  158. )
  159. .then((data) => {
  160. callback(null, JSON.parse(data))
  161. })
  162. .catch(callback)
  163. }
  164. }