DocstoreClient.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. let DocstoreClient
  2. const request = require('request').defaults({ jar: false })
  3. const settings = require('@overleaf/settings')
  4. const Persistor = require('../../../../app/js/PersistorManager')
  5. async function streamToString(stream) {
  6. const chunks = []
  7. return await new Promise((resolve, reject) => {
  8. stream.on('data', chunk => chunks.push(chunk))
  9. stream.on('error', reject)
  10. stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
  11. })
  12. }
  13. async function getStringFromPersistor(persistor, bucket, key) {
  14. const stream = await persistor.getObjectStream(bucket, key, {})
  15. stream.resume()
  16. return await streamToString(stream)
  17. }
  18. module.exports = DocstoreClient = {
  19. createDoc(projectId, docId, lines, version, ranges, callback) {
  20. return DocstoreClient.updateDoc(
  21. projectId,
  22. docId,
  23. lines,
  24. version,
  25. ranges,
  26. callback
  27. )
  28. },
  29. getDoc(projectId, docId, qs, callback) {
  30. request.get(
  31. {
  32. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
  33. json: true,
  34. qs,
  35. },
  36. callback
  37. )
  38. },
  39. peekDoc(projectId, docId, qs, callback) {
  40. request.get(
  41. {
  42. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/peek`,
  43. json: true,
  44. qs,
  45. },
  46. callback
  47. )
  48. },
  49. isDocDeleted(projectId, docId, callback) {
  50. request.get(
  51. {
  52. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/deleted`,
  53. json: true,
  54. },
  55. callback
  56. )
  57. },
  58. getAllDocs(projectId, callback) {
  59. request.get(
  60. {
  61. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc`,
  62. json: true,
  63. },
  64. (req, res, body) => {
  65. callback(req, res, body)
  66. }
  67. )
  68. },
  69. getAllDeletedDocs(projectId, callback) {
  70. request.get(
  71. {
  72. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc-deleted`,
  73. json: true,
  74. },
  75. (error, res, body) => {
  76. if (error) return callback(error)
  77. if (res.statusCode !== 200) {
  78. return callback(new Error('unexpected statusCode'))
  79. }
  80. callback(null, body)
  81. }
  82. )
  83. },
  84. getAllRanges(projectId, callback) {
  85. request.get(
  86. {
  87. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/ranges`,
  88. json: true,
  89. },
  90. callback
  91. )
  92. },
  93. getCommentThreadIds(projectId, callback) {
  94. request.get(
  95. {
  96. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/comment-thread-ids`,
  97. json: true,
  98. },
  99. callback
  100. )
  101. },
  102. getTrackedChangesUserIds(projectId, callback) {
  103. request.get(
  104. {
  105. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/tracked-changes-user-ids`,
  106. json: true,
  107. },
  108. callback
  109. )
  110. },
  111. updateDoc(projectId, docId, lines, version, ranges, callback) {
  112. return request.post(
  113. {
  114. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
  115. json: {
  116. lines,
  117. version,
  118. ranges,
  119. },
  120. },
  121. callback
  122. )
  123. },
  124. deleteDoc(projectId, docId, callback) {
  125. DocstoreClient.deleteDocWithDateAndName(
  126. projectId,
  127. docId,
  128. new Date(),
  129. 'main.tex',
  130. callback
  131. )
  132. },
  133. deleteDocWithDate(projectId, docId, date, callback) {
  134. DocstoreClient.deleteDocWithDateAndName(
  135. projectId,
  136. docId,
  137. date,
  138. 'main.tex',
  139. callback
  140. )
  141. },
  142. deleteDocWithName(projectId, docId, name, callback) {
  143. DocstoreClient.deleteDocWithDateAndName(
  144. projectId,
  145. docId,
  146. new Date(),
  147. name,
  148. callback
  149. )
  150. },
  151. deleteDocWithDateAndName(projectId, docId, deletedAt, name, callback) {
  152. request.patch(
  153. {
  154. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
  155. json: { name, deleted: true, deletedAt },
  156. },
  157. callback
  158. )
  159. },
  160. archiveAllDoc(projectId, callback) {
  161. request.post(
  162. {
  163. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/archive`,
  164. },
  165. callback
  166. )
  167. },
  168. archiveDoc(projectId, docId, callback) {
  169. request.post(
  170. {
  171. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/archive`,
  172. },
  173. callback
  174. )
  175. },
  176. destroyAllDoc(projectId, callback) {
  177. request.post(
  178. {
  179. url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/destroy`,
  180. },
  181. callback
  182. )
  183. },
  184. healthCheck(callback) {
  185. request.get(
  186. `http://127.0.0.1:${settings.internal.docstore.port}/health_check`,
  187. callback
  188. )
  189. },
  190. getS3Doc(projectId, docId, callback) {
  191. getStringFromPersistor(
  192. Persistor,
  193. settings.docstore.bucket,
  194. `${projectId}/${docId}`
  195. )
  196. .then(data => {
  197. callback(null, JSON.parse(data))
  198. })
  199. .catch(callback)
  200. },
  201. }