DocstoreClient.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. getAllDeletedDocs(project_id, callback) {
  82. request.get(
  83. {
  84. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc-deleted`,
  85. json: true
  86. },
  87. (error, res, body) => {
  88. if (error) return callback(error)
  89. if (res.statusCode !== 200) {
  90. return callback(new Error('unexpected statusCode'))
  91. }
  92. callback(null, body)
  93. }
  94. )
  95. },
  96. getAllRanges(project_id, callback) {
  97. if (callback == null) {
  98. callback = function (error, res, body) {}
  99. }
  100. return request.get(
  101. {
  102. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/ranges`,
  103. json: true
  104. },
  105. callback
  106. )
  107. },
  108. updateDoc(project_id, doc_id, lines, version, ranges, callback) {
  109. if (callback == null) {
  110. callback = function (error, res, body) {}
  111. }
  112. return request.post(
  113. {
  114. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
  115. json: {
  116. lines,
  117. version,
  118. ranges
  119. }
  120. },
  121. callback
  122. )
  123. },
  124. deleteDoc(project_id, doc_id, callback) {
  125. DocstoreClient.deleteDocWithDateAndName(
  126. project_id,
  127. doc_id,
  128. new Date(),
  129. 'main.tex',
  130. callback
  131. )
  132. },
  133. deleteDocWithDate(project_id, doc_id, date, callback) {
  134. DocstoreClient.deleteDocWithDateAndName(
  135. project_id,
  136. doc_id,
  137. date,
  138. 'main.tex',
  139. callback
  140. )
  141. },
  142. deleteDocWithName(project_id, doc_id, name, callback) {
  143. DocstoreClient.deleteDocWithDateAndName(
  144. project_id,
  145. doc_id,
  146. new Date(),
  147. name,
  148. callback
  149. )
  150. },
  151. deleteDocWithDateAndName(project_id, doc_id, deletedAt, name, callback) {
  152. request.patch(
  153. {
  154. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}`,
  155. json: { name, deleted: true, deletedAt }
  156. },
  157. callback
  158. )
  159. },
  160. archiveAllDoc(project_id, callback) {
  161. if (callback == null) {
  162. callback = function (error, res, body) {}
  163. }
  164. return request.post(
  165. {
  166. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/archive`
  167. },
  168. callback
  169. )
  170. },
  171. archiveDocById(project_id, doc_id, callback) {
  172. if (callback == null) {
  173. callback = function (error, res, body) {}
  174. }
  175. return request.post(
  176. {
  177. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/doc/${doc_id}/archive`
  178. },
  179. callback
  180. )
  181. },
  182. destroyAllDoc(project_id, callback) {
  183. if (callback == null) {
  184. callback = function (error, res, body) {}
  185. }
  186. return request.post(
  187. {
  188. url: `http://localhost:${settings.internal.docstore.port}/project/${project_id}/destroy`
  189. },
  190. callback
  191. )
  192. },
  193. getS3Doc(project_id, doc_id, callback) {
  194. getStringFromPersistor(
  195. Persistor,
  196. settings.docstore.bucket,
  197. `${project_id}/${doc_id}`
  198. )
  199. .then((data) => {
  200. callback(null, JSON.parse(data))
  201. })
  202. .catch(callback)
  203. }
  204. }