overleaf/services/docstore/test/acceptance/js/helpers/DocstoreClient.js

196 lines
4.4 KiB
JavaScript
Raw Normal View History

let DocstoreClient
const request = require('request').defaults({ jar: false })
const settings = require('@overleaf/settings')
const Persistor = require('../../../../app/js/PersistorManager')
async function streamToString(stream) {
const chunks = []
return new Promise((resolve, reject) => {
2021-07-13 07:04:48 -04:00
stream.on('data', chunk => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
})
}
async function getStringFromPersistor(persistor, bucket, key) {
const stream = await persistor.getObjectStream(bucket, key, {})
stream.resume()
return streamToString(stream)
}
module.exports = DocstoreClient = {
2021-08-03 05:12:16 -04:00
createDoc(projectId, docId, lines, version, ranges, callback) {
return DocstoreClient.updateDoc(
2021-08-03 05:12:16 -04:00
projectId,
docId,
lines,
version,
ranges,
callback
)
},
2014-04-30 08:06:12 -04:00
2021-08-03 05:12:16 -04:00
getDoc(projectId, docId, qs, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
json: true,
2021-07-13 07:04:48 -04:00
qs,
},
callback
)
},
2021-08-03 05:12:16 -04:00
peekDoc(projectId, docId, qs, callback) {
2021-08-03 05:07:00 -04:00
request.get(
2021-07-30 11:06:16 -04:00
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/peek`,
2021-07-30 11:06:16 -04:00
json: true,
qs,
},
callback
)
},
2021-08-03 05:12:16 -04:00
isDocDeleted(projectId, docId, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/deleted`,
2021-07-13 07:04:48 -04:00
json: true,
},
callback
)
},
2021-08-03 05:12:16 -04:00
getAllDocs(projectId, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc`,
2021-07-13 07:04:48 -04:00
json: true,
},
(req, res, body) => {
callback(req, res, body)
}
)
},
2021-08-03 05:12:16 -04:00
getAllDeletedDocs(projectId, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc-deleted`,
2021-07-13 07:04:48 -04:00
json: true,
},
(error, res, body) => {
if (error) return callback(error)
if (res.statusCode !== 200) {
return callback(new Error('unexpected statusCode'))
}
callback(null, body)
}
)
},
2021-08-03 05:12:16 -04:00
getAllRanges(projectId, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/ranges`,
2021-07-13 07:04:48 -04:00
json: true,
},
callback
)
},
2014-04-30 08:06:12 -04:00
2021-08-03 05:12:16 -04:00
updateDoc(projectId, docId, lines, version, ranges, callback) {
return request.post(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
json: {
lines,
version,
2021-07-13 07:04:48 -04:00
ranges,
},
},
callback
)
},
2021-08-03 05:12:16 -04:00
deleteDoc(projectId, docId, callback) {
DocstoreClient.deleteDocWithDateAndName(
2021-08-03 05:12:16 -04:00
projectId,
docId,
new Date(),
'main.tex',
callback
)
},
2021-08-03 05:12:16 -04:00
deleteDocWithDate(projectId, docId, date, callback) {
DocstoreClient.deleteDocWithDateAndName(
2021-08-03 05:12:16 -04:00
projectId,
docId,
date,
'main.tex',
callback
)
},
2021-08-03 05:12:16 -04:00
deleteDocWithName(projectId, docId, name, callback) {
DocstoreClient.deleteDocWithDateAndName(
2021-08-03 05:12:16 -04:00
projectId,
docId,
new Date(),
name,
callback
)
},
2021-08-03 05:12:16 -04:00
deleteDocWithDateAndName(projectId, docId, deletedAt, name, callback) {
request.patch(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
2021-07-13 07:04:48 -04:00
json: { name, deleted: true, deletedAt },
},
callback
)
},
2021-08-03 05:12:16 -04:00
archiveAllDoc(projectId, callback) {
request.post(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/archive`,
},
callback
)
},
archiveDoc(projectId, docId, callback) {
request.post(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/archive`,
},
callback
)
},
2021-08-03 05:12:16 -04:00
destroyAllDoc(projectId, callback) {
request.post(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/destroy`,
},
callback
)
},
2021-08-03 05:12:16 -04:00
getS3Doc(projectId, docId, callback) {
getStringFromPersistor(
Persistor,
settings.docstore.bucket,
2021-08-03 05:12:16 -04:00
`${projectId}/${docId}`
)
2021-07-13 07:04:48 -04:00
.then(data => {
callback(null, JSON.parse(data))
})
.catch(callback)
2021-07-13 07:04:48 -04:00
},
}