MongoAWS.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. let MongoAWS
  17. const settings = require('settings-sharelatex')
  18. const logger = require('logger-sharelatex')
  19. const AWS = require('aws-sdk')
  20. const S3S = require('s3-streams')
  21. const { db, ObjectId } = require('./mongodb')
  22. const JSONStream = require('JSONStream')
  23. const ReadlineStream = require('byline')
  24. const zlib = require('zlib')
  25. const Metrics = require('@overleaf/metrics')
  26. const DAYS = 24 * 3600 * 1000 // one day in milliseconds
  27. const createStream = function (streamConstructor, project_id, doc_id, pack_id) {
  28. const AWS_CONFIG = {
  29. accessKeyId: settings.trackchanges.s3.key,
  30. secretAccessKey: settings.trackchanges.s3.secret,
  31. endpoint: settings.trackchanges.s3.endpoint,
  32. s3ForcePathStyle: settings.trackchanges.s3.pathStyle
  33. }
  34. return streamConstructor(new AWS.S3(AWS_CONFIG), {
  35. Bucket: settings.trackchanges.stores.doc_history,
  36. Key: project_id + '/changes-' + doc_id + '/pack-' + pack_id
  37. })
  38. }
  39. module.exports = MongoAWS = {
  40. archivePack(project_id, doc_id, pack_id, _callback) {
  41. if (_callback == null) {
  42. _callback = function (error) {}
  43. }
  44. const callback = function (...args) {
  45. _callback(...Array.from(args || []))
  46. return (_callback = function () {})
  47. }
  48. const query = {
  49. _id: ObjectId(pack_id),
  50. doc_id: ObjectId(doc_id)
  51. }
  52. if (project_id == null) {
  53. return callback(new Error('invalid project id'))
  54. }
  55. if (doc_id == null) {
  56. return callback(new Error('invalid doc id'))
  57. }
  58. if (pack_id == null) {
  59. return callback(new Error('invalid pack id'))
  60. }
  61. logger.log({ project_id, doc_id, pack_id }, 'uploading data to s3')
  62. const upload = createStream(S3S.WriteStream, project_id, doc_id, pack_id)
  63. return db.docHistory.findOne(query, function (err, result) {
  64. if (err != null) {
  65. return callback(err)
  66. }
  67. if (result == null) {
  68. return callback(new Error('cannot find pack to send to s3'))
  69. }
  70. if (result.expiresAt != null) {
  71. return callback(new Error('refusing to send pack with TTL to s3'))
  72. }
  73. const uncompressedData = JSON.stringify(result)
  74. if (uncompressedData.indexOf('\u0000') !== -1) {
  75. const error = new Error('null bytes found in upload')
  76. logger.error({ err: error, project_id, doc_id, pack_id }, error.message)
  77. return callback(error)
  78. }
  79. return zlib.gzip(uncompressedData, function (err, buf) {
  80. logger.log(
  81. {
  82. project_id,
  83. doc_id,
  84. pack_id,
  85. origSize: uncompressedData.length,
  86. newSize: buf.length
  87. },
  88. 'compressed pack'
  89. )
  90. if (err != null) {
  91. return callback(err)
  92. }
  93. upload.on('error', (err) => callback(err))
  94. upload.on('finish', function () {
  95. Metrics.inc('archive-pack')
  96. logger.log({ project_id, doc_id, pack_id }, 'upload to s3 completed')
  97. return callback(null)
  98. })
  99. upload.write(buf)
  100. return upload.end()
  101. })
  102. })
  103. },
  104. readArchivedPack(project_id, doc_id, pack_id, _callback) {
  105. if (_callback == null) {
  106. _callback = function (error, result) {}
  107. }
  108. const callback = function (...args) {
  109. _callback(...Array.from(args || []))
  110. return (_callback = function () {})
  111. }
  112. if (project_id == null) {
  113. return callback(new Error('invalid project id'))
  114. }
  115. if (doc_id == null) {
  116. return callback(new Error('invalid doc id'))
  117. }
  118. if (pack_id == null) {
  119. return callback(new Error('invalid pack id'))
  120. }
  121. logger.log({ project_id, doc_id, pack_id }, 'downloading data from s3')
  122. const download = createStream(S3S.ReadStream, project_id, doc_id, pack_id)
  123. const inputStream = download
  124. .on('open', (obj) => 1)
  125. .on('error', (err) => callback(err))
  126. const gunzip = zlib.createGunzip()
  127. gunzip.setEncoding('utf8')
  128. gunzip.on('error', function (err) {
  129. logger.log(
  130. { project_id, doc_id, pack_id, err },
  131. 'error uncompressing gzip stream'
  132. )
  133. return callback(err)
  134. })
  135. const outputStream = inputStream.pipe(gunzip)
  136. const parts = []
  137. outputStream.on('error', (err) => callback(err))
  138. outputStream.on('end', function () {
  139. let object
  140. logger.log({ project_id, doc_id, pack_id }, 'download from s3 completed')
  141. try {
  142. object = JSON.parse(parts.join(''))
  143. } catch (e) {
  144. return callback(e)
  145. }
  146. object._id = ObjectId(object._id)
  147. object.doc_id = ObjectId(object.doc_id)
  148. object.project_id = ObjectId(object.project_id)
  149. for (const op of Array.from(object.pack)) {
  150. if (op._id != null) {
  151. op._id = ObjectId(op._id)
  152. }
  153. }
  154. return callback(null, object)
  155. })
  156. return outputStream.on('data', (data) => parts.push(data))
  157. },
  158. unArchivePack(project_id, doc_id, pack_id, callback) {
  159. if (callback == null) {
  160. callback = function (error) {}
  161. }
  162. return MongoAWS.readArchivedPack(project_id, doc_id, pack_id, function (
  163. err,
  164. object
  165. ) {
  166. if (err != null) {
  167. return callback(err)
  168. }
  169. Metrics.inc('unarchive-pack')
  170. // allow the object to expire, we can always retrieve it again
  171. object.expiresAt = new Date(Date.now() + 7 * DAYS)
  172. logger.log({ project_id, doc_id, pack_id }, 'inserting object from s3')
  173. return db.docHistory.insertOne(object, (err, confirmation) => {
  174. if (err) return callback(err)
  175. object._id = confirmation.insertedId
  176. callback(null, object)
  177. })
  178. })
  179. }
  180. }