first commit

This commit is contained in:
2025-04-24 13:11:28 +08:00
commit ff9c54d5e4
5960 changed files with 834111 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
FROM fsouza/fake-gcs-server:latest
RUN apk add --update --no-cache curl
COPY healthcheck.sh /healthcheck.sh
HEALTHCHECK --interval=1s --timeout=1s --retries=30 CMD /healthcheck.sh http://localhost:9090
CMD ["--port=9090", "--scheme=http"]

View File

@@ -0,0 +1,9 @@
#!/bin/sh
# health check to allow 404 status code as valid
STATUSCODE=$(curl --silent --output /dev/null --write-out "%{http_code}" "$1")
# will be 000 on non-http error (e.g. connection failure)
if test "$STATUSCODE" -ge 500 || test "$STATUSCODE" -lt 200; then
exit 1
fi
exit 0

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,511 @@
const { db, ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai')
const DocstoreApp = require('./helpers/DocstoreApp')
const Errors = require('../../../app/js/Errors')
const Settings = require('@overleaf/settings')
const { Storage } = require('@google-cloud/storage')
const DocstoreClient = require('./helpers/DocstoreClient')
function deleteTestSuite(deleteDoc) {
before(async function () {
// Create buckets needed by the archiving part of these tests
const storage = new Storage(Settings.docstore.gcs.endpoint)
await storage.createBucket(Settings.docstore.bucket)
await storage.createBucket(`${Settings.docstore.bucket}-deleted`)
})
after(async function () {
// Tear down the buckets created above
const storage = new Storage(Settings.docstore.gcs.endpoint)
await storage.bucket(Settings.docstore.bucket).deleteFiles()
await storage.bucket(Settings.docstore.bucket).delete()
await storage.bucket(`${Settings.docstore.bucket}-deleted`).deleteFiles()
await storage.bucket(`${Settings.docstore.bucket}-deleted`).delete()
})
beforeEach(function (done) {
this.project_id = new ObjectId()
this.doc_id = new ObjectId()
this.lines = ['original', 'lines']
this.version = 42
this.ranges = []
DocstoreApp.ensureRunning(() => {
DocstoreClient.createDoc(
this.project_id,
this.doc_id,
this.lines,
this.version,
this.ranges,
error => {
if (error) {
throw error
}
done()
}
)
})
})
it('should show as not deleted on /deleted', function (done) {
DocstoreClient.isDocDeleted(
this.project_id,
this.doc_id,
(error, res, body) => {
if (error) return done(error)
expect(res.statusCode).to.equal(200)
expect(body).to.have.property('deleted').to.equal(false)
done()
}
)
})
describe('when the doc exists', function () {
beforeEach(function (done) {
deleteDoc(this.project_id, this.doc_id, (error, res, doc) => {
if (error) return done(error)
this.res = res
done()
})
})
afterEach(function (done) {
db.docs.deleteOne({ _id: this.doc_id }, done)
})
it('should mark the doc as deleted on /deleted', function (done) {
DocstoreClient.isDocDeleted(
this.project_id,
this.doc_id,
(error, res, body) => {
if (error) return done(error)
expect(res.statusCode).to.equal(200)
expect(body).to.have.property('deleted').to.equal(true)
done()
}
)
})
it('should insert a deleted doc into the docs collection', function (done) {
db.docs.find({ _id: this.doc_id }).toArray((error, docs) => {
if (error) return done(error)
docs[0]._id.should.deep.equal(this.doc_id)
docs[0].lines.should.deep.equal(this.lines)
docs[0].deleted.should.equal(true)
done()
})
})
it('should not export the doc to s3', function (done) {
setTimeout(() => {
DocstoreClient.getS3Doc(this.project_id, this.doc_id, error => {
expect(error).to.be.instanceOf(Errors.NotFoundError)
done()
})
}, 1000)
})
})
describe('when archiveOnSoftDelete is enabled', function () {
let archiveOnSoftDelete
beforeEach('overwrite settings', function () {
archiveOnSoftDelete = Settings.docstore.archiveOnSoftDelete
Settings.docstore.archiveOnSoftDelete = true
})
afterEach('restore settings', function () {
Settings.docstore.archiveOnSoftDelete = archiveOnSoftDelete
})
beforeEach('delete Doc', function (done) {
deleteDoc(this.project_id, this.doc_id, (error, res) => {
if (error) return done(error)
this.res = res
done()
})
})
beforeEach(function waitForBackgroundFlush(done) {
setTimeout(done, 500)
})
afterEach(function cleanupDoc(done) {
db.docs.deleteOne({ _id: this.doc_id }, done)
})
it('should set the deleted flag in the doc', function (done) {
db.docs.findOne({ _id: this.doc_id }, (error, doc) => {
if (error) {
return done(error)
}
expect(doc.deleted).to.equal(true)
done()
})
})
it('should set inS3 and unset lines and ranges in the doc', function (done) {
db.docs.findOne({ _id: this.doc_id }, (error, doc) => {
if (error) {
return done(error)
}
expect(doc.lines).to.not.exist
expect(doc.ranges).to.not.exist
expect(doc.inS3).to.equal(true)
done()
})
})
it('should set the doc in s3 correctly', function (done) {
DocstoreClient.getS3Doc(this.project_id, this.doc_id, (error, s3doc) => {
if (error) {
return done(error)
}
expect(s3doc.lines).to.deep.equal(this.lines)
expect(s3doc.ranges).to.deep.equal(this.ranges)
done()
})
})
})
describe('when the doc exists in another project', function () {
const otherProjectId = new ObjectId()
it('should show as not existing on /deleted', function (done) {
DocstoreClient.isDocDeleted(otherProjectId, this.doc_id, (error, res) => {
if (error) return done(error)
expect(res.statusCode).to.equal(404)
done()
})
})
it('should return a 404 when trying to delete', function (done) {
deleteDoc(otherProjectId, this.doc_id, (error, res) => {
if (error) return done(error)
expect(res.statusCode).to.equal(404)
done()
})
})
})
describe('when the doc does not exist', function () {
it('should show as not existing on /deleted', function (done) {
const missingDocId = new ObjectId()
DocstoreClient.isDocDeleted(
this.project_id,
missingDocId,
(error, res) => {
if (error) return done(error)
expect(res.statusCode).to.equal(404)
done()
}
)
})
it('should return a 404', function (done) {
const missingDocId = new ObjectId()
deleteDoc(this.project_id, missingDocId, (error, res, doc) => {
if (error) return done(error)
res.statusCode.should.equal(404)
done()
})
})
})
}
describe('Delete via PATCH', function () {
deleteTestSuite(DocstoreClient.deleteDoc)
describe('when providing a custom doc name in the delete request', function () {
beforeEach(function (done) {
DocstoreClient.deleteDocWithName(
this.project_id,
this.doc_id,
'wombat.tex',
done
)
})
it('should insert the doc name into the docs collection', function (done) {
db.docs.find({ _id: this.doc_id }).toArray((error, docs) => {
if (error) return done(error)
expect(docs[0].name).to.equal('wombat.tex')
done()
})
})
})
describe('when providing a custom deletedAt date in the delete request', function () {
beforeEach('record date and delay', function (done) {
this.deletedAt = new Date()
setTimeout(done, 5)
})
beforeEach('perform deletion with past date', function (done) {
DocstoreClient.deleteDocWithDate(
this.project_id,
this.doc_id,
this.deletedAt,
done
)
})
it('should insert the date into the docs collection', function (done) {
db.docs.find({ _id: this.doc_id }).toArray((error, docs) => {
if (error) return done(error)
expect(docs[0].deletedAt.toISOString()).to.equal(
this.deletedAt.toISOString()
)
done()
})
})
})
describe('when providing no doc name in the delete request', function () {
beforeEach(function (done) {
DocstoreClient.deleteDocWithName(
this.project_id,
this.doc_id,
'',
(error, res) => {
this.res = res
done(error)
}
)
})
it('should reject the request', function () {
expect(this.res.statusCode).to.equal(400)
})
})
describe('when providing no date in the delete request', function () {
beforeEach(function (done) {
DocstoreClient.deleteDocWithDate(
this.project_id,
this.doc_id,
'',
(error, res) => {
this.res = res
done(error)
}
)
})
it('should reject the request', function () {
expect(this.res.statusCode).to.equal(400)
})
})
describe('before deleting anything', function () {
it('should show nothing in deleted docs response', function (done) {
DocstoreClient.getAllDeletedDocs(
this.project_id,
(error, deletedDocs) => {
if (error) return done(error)
expect(deletedDocs).to.deep.equal([])
done()
}
)
})
})
describe('when the doc gets a name on delete', function () {
beforeEach(function (done) {
this.deletedAt = new Date()
DocstoreClient.deleteDocWithDate(
this.project_id,
this.doc_id,
this.deletedAt,
done
)
})
it('should show the doc in deleted docs response', function (done) {
DocstoreClient.getAllDeletedDocs(
this.project_id,
(error, deletedDocs) => {
if (error) return done(error)
expect(deletedDocs).to.deep.equal([
{
_id: this.doc_id.toString(),
name: 'main.tex',
deletedAt: this.deletedAt.toISOString(),
},
])
done()
}
)
})
describe('after deleting multiple docs', function () {
beforeEach('create doc2', function (done) {
this.doc_id2 = new ObjectId()
DocstoreClient.createDoc(
this.project_id,
this.doc_id2,
this.lines,
this.version,
this.ranges,
done
)
})
beforeEach('delete doc2', function (done) {
this.deletedAt2 = new Date()
DocstoreClient.deleteDocWithDateAndName(
this.project_id,
this.doc_id2,
this.deletedAt2,
'two.tex',
done
)
})
beforeEach('create doc3', function (done) {
this.doc_id3 = new ObjectId()
DocstoreClient.createDoc(
this.project_id,
this.doc_id3,
this.lines,
this.version,
this.ranges,
done
)
})
beforeEach('delete doc3', function (done) {
this.deletedAt3 = new Date()
DocstoreClient.deleteDocWithDateAndName(
this.project_id,
this.doc_id3,
this.deletedAt3,
'three.tex',
done
)
})
it('should show all the docs as deleted', function (done) {
DocstoreClient.getAllDeletedDocs(
this.project_id,
(error, deletedDocs) => {
if (error) return done(error)
expect(deletedDocs).to.deep.equal([
{
_id: this.doc_id3.toString(),
name: 'three.tex',
deletedAt: this.deletedAt3.toISOString(),
},
{
_id: this.doc_id2.toString(),
name: 'two.tex',
deletedAt: this.deletedAt2.toISOString(),
},
{
_id: this.doc_id.toString(),
name: 'main.tex',
deletedAt: this.deletedAt.toISOString(),
},
])
done()
}
)
})
describe('with one more than max_deleted_docs permits', function () {
let maxDeletedDocsBefore
beforeEach(function () {
maxDeletedDocsBefore = Settings.max_deleted_docs
Settings.max_deleted_docs = 2
})
afterEach(function () {
Settings.max_deleted_docs = maxDeletedDocsBefore
})
it('should omit the first deleted doc', function (done) {
DocstoreClient.getAllDeletedDocs(
this.project_id,
(error, deletedDocs) => {
if (error) return done(error)
expect(deletedDocs).to.deep.equal([
{
_id: this.doc_id3.toString(),
name: 'three.tex',
deletedAt: this.deletedAt3.toISOString(),
},
{
_id: this.doc_id2.toString(),
name: 'two.tex',
deletedAt: this.deletedAt2.toISOString(),
},
// dropped main.tex
])
done()
}
)
})
})
})
})
})
describe("Destroying a project's documents", function () {
beforeEach(function (done) {
this.project_id = new ObjectId()
this.doc_id = new ObjectId()
this.lines = ['original', 'lines']
this.version = 42
this.ranges = []
DocstoreApp.ensureRunning(() => {
DocstoreClient.createDoc(
this.project_id,
this.doc_id,
this.lines,
this.version,
this.ranges,
error => {
if (error) {
throw error
}
done()
}
)
})
})
describe('when the doc exists', function () {
beforeEach(function (done) {
DocstoreClient.destroyAllDoc(this.project_id, done)
})
it('should remove the doc from the docs collection', function (done) {
db.docs.find({ _id: this.doc_id }).toArray((err, docs) => {
expect(err).not.to.exist
expect(docs).to.deep.equal([])
done()
})
})
})
describe('when the doc is archived', function () {
beforeEach(function (done) {
DocstoreClient.archiveAllDoc(this.project_id, err => {
if (err) {
return done(err)
}
DocstoreClient.destroyAllDoc(this.project_id, done)
})
})
it('should remove the doc from the docs collection', function (done) {
db.docs.find({ _id: this.doc_id }).toArray((err, docs) => {
expect(err).not.to.exist
expect(docs).to.deep.equal([])
done()
})
})
it('should remove the doc contents from s3', function (done) {
DocstoreClient.getS3Doc(this.project_id, this.doc_id, error => {
expect(error).to.be.instanceOf(Errors.NotFoundError)
done()
})
})
})
})

View File

@@ -0,0 +1,112 @@
/* eslint-disable
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { ObjectId } = require('mongodb-legacy')
const async = require('async')
const DocstoreApp = require('./helpers/DocstoreApp')
const DocstoreClient = require('./helpers/DocstoreClient')
describe('Getting all docs', function () {
beforeEach(function (done) {
this.project_id = new ObjectId()
this.docs = [
{
_id: new ObjectId(),
lines: ['one', 'two', 'three'],
ranges: { mock: 'one' },
rev: 2,
},
{
_id: new ObjectId(),
lines: ['aaa', 'bbb', 'ccc'],
ranges: { mock: 'two' },
rev: 4,
},
{
_id: new ObjectId(),
lines: ['111', '222', '333'],
ranges: { mock: 'three' },
rev: 6,
},
]
this.deleted_doc = {
_id: new ObjectId(),
lines: ['deleted'],
ranges: { mock: 'four' },
rev: 8,
}
const version = 42
const jobs = Array.from(this.docs).map(doc =>
(doc => {
return callback => {
return DocstoreClient.createDoc(
this.project_id,
doc._id,
doc.lines,
version,
doc.ranges,
callback
)
}
})(doc)
)
jobs.push(cb => {
return DocstoreClient.createDoc(
this.project_id,
this.deleted_doc._id,
this.deleted_doc.lines,
version,
this.deleted_doc.ranges,
err => {
if (err) return done(err)
return DocstoreClient.deleteDoc(
this.project_id,
this.deleted_doc._id,
cb
)
}
)
})
jobs.unshift(cb => DocstoreApp.ensureRunning(cb))
return async.series(jobs, done)
})
it('getAllDocs should return all the (non-deleted) docs', function (done) {
return DocstoreClient.getAllDocs(this.project_id, (error, res, docs) => {
if (error != null) {
throw error
}
docs.length.should.equal(this.docs.length)
for (let i = 0; i < docs.length; i++) {
const doc = docs[i]
doc.lines.should.deep.equal(this.docs[i].lines)
}
return done()
})
})
return it('getAllRanges should return all the (non-deleted) doc ranges', function (done) {
return DocstoreClient.getAllRanges(this.project_id, (error, res, docs) => {
if (error != null) {
throw error
}
docs.length.should.equal(this.docs.length)
for (let i = 0; i < docs.length; i++) {
const doc = docs[i]
doc.ranges.should.deep.equal(this.docs[i].ranges)
}
return done()
})
})
})

View File

@@ -0,0 +1,139 @@
const Settings = require('@overleaf/settings')
const { ObjectId } = require('../../../app/js/mongodb')
const DocstoreApp = require('./helpers/DocstoreApp')
const DocstoreClient = require('./helpers/DocstoreClient')
const { Storage } = require('@google-cloud/storage')
describe('Getting A Doc from Archive', function () {
before(function (done) {
return DocstoreApp.ensureRunning(done)
})
before(async function () {
const storage = new Storage(Settings.docstore.gcs.endpoint)
await storage.createBucket(Settings.docstore.bucket)
await storage.createBucket(`${Settings.docstore.bucket}-deleted`)
})
after(async function () {
// Tear down the buckets created above
const storage = new Storage(Settings.docstore.gcs.endpoint)
await storage.bucket(Settings.docstore.bucket).deleteFiles()
await storage.bucket(Settings.docstore.bucket).delete()
await storage.bucket(`${Settings.docstore.bucket}-deleted`).deleteFiles()
await storage.bucket(`${Settings.docstore.bucket}-deleted`).delete()
})
describe('for an archived doc', function () {
before(function (done) {
this.project_id = new ObjectId()
this.timeout(1000 * 30)
this.doc = {
_id: new ObjectId(),
lines: ['foo', 'bar'],
ranges: {},
version: 2,
}
DocstoreClient.createDoc(
this.project_id,
this.doc._id,
this.doc.lines,
this.doc.version,
this.doc.ranges,
error => {
if (error) {
return done(error)
}
DocstoreClient.archiveDoc(
this.project_id,
this.doc._id,
(error, res) => {
this.res = res
if (error) {
return done(error)
}
done()
}
)
}
)
})
it('should successully archive the doc', function (done) {
this.res.statusCode.should.equal(204)
done()
})
it('should return the doc lines and version from persistent storage', function (done) {
return DocstoreClient.peekDoc(
this.project_id,
this.doc._id,
{},
(error, res, doc) => {
if (error) return done(error)
res.statusCode.should.equal(200)
res.headers['x-doc-status'].should.equal('archived')
doc.lines.should.deep.equal(this.doc.lines)
doc.version.should.equal(this.doc.version)
doc.ranges.should.deep.equal(this.doc.ranges)
return done()
}
)
})
it('should return the doc lines and version from persistent storage on subsequent requests', function (done) {
return DocstoreClient.peekDoc(
this.project_id,
this.doc._id,
{},
(error, res, doc) => {
if (error) return done(error)
res.statusCode.should.equal(200)
res.headers['x-doc-status'].should.equal('archived')
doc.lines.should.deep.equal(this.doc.lines)
doc.version.should.equal(this.doc.version)
doc.ranges.should.deep.equal(this.doc.ranges)
return done()
}
)
})
describe('for an non-archived doc', function () {
before(function (done) {
this.project_id = new ObjectId()
this.timeout(1000 * 30)
this.doc = {
_id: new ObjectId(),
lines: ['foo', 'bar'],
ranges: {},
version: 2,
}
DocstoreClient.createDoc(
this.project_id,
this.doc._id,
this.doc.lines,
this.doc.version,
this.doc.ranges,
done
)
})
it('should return the doc lines and version from mongo', function (done) {
return DocstoreClient.peekDoc(
this.project_id,
this.doc._id,
{},
(error, res, doc) => {
if (error) return done(error)
res.statusCode.should.equal(200)
res.headers['x-doc-status'].should.equal('active')
doc.lines.should.deep.equal(this.doc.lines)
doc.version.should.equal(this.doc.version)
doc.ranges.should.deep.equal(this.doc.ranges)
return done()
}
)
})
})
})
})

View File

@@ -0,0 +1,137 @@
/* eslint-disable
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { ObjectId } = require('mongodb-legacy')
const DocstoreApp = require('./helpers/DocstoreApp')
const DocstoreClient = require('./helpers/DocstoreClient')
describe('Getting a doc', function () {
beforeEach(function (done) {
this.project_id = new ObjectId()
this.doc_id = new ObjectId()
this.lines = ['original', 'lines']
this.version = 42
this.ranges = {
changes: [
{
id: new ObjectId().toString(),
op: { i: 'foo', p: 3 },
meta: {
user_id: new ObjectId().toString(),
ts: new Date().toString(),
},
},
],
}
return DocstoreApp.ensureRunning(() => {
return DocstoreClient.createDoc(
this.project_id,
this.doc_id,
this.lines,
this.version,
this.ranges,
error => {
if (error != null) {
throw error
}
return done()
}
)
})
})
describe('when the doc exists', function () {
return it('should get the doc lines and version', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.lines)
doc.version.should.equal(this.version)
doc.ranges.should.deep.equal(this.ranges)
return done()
}
)
})
})
describe('when the doc does not exist', function () {
return it('should return a 404', function (done) {
const missingDocId = new ObjectId()
return DocstoreClient.getDoc(
this.project_id,
missingDocId,
{},
(error, res, doc) => {
if (error) return done(error)
res.statusCode.should.equal(404)
return done()
}
)
})
})
return describe('when the doc is a deleted doc', function () {
beforeEach(function (done) {
this.deleted_doc_id = new ObjectId()
return DocstoreClient.createDoc(
this.project_id,
this.deleted_doc_id,
this.lines,
this.version,
this.ranges,
error => {
if (error != null) {
throw error
}
return DocstoreClient.deleteDoc(
this.project_id,
this.deleted_doc_id,
done
)
}
)
})
it('should return the doc', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.deleted_doc_id,
{ include_deleted: true },
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.lines)
doc.version.should.equal(this.version)
doc.ranges.should.deep.equal(this.ranges)
doc.deleted.should.equal(true)
return done()
}
)
})
return it('should return a 404 when the query string is not set', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.deleted_doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
res.statusCode.should.equal(404)
return done()
}
)
})
})
})

View File

@@ -0,0 +1,557 @@
/* eslint-disable
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { ObjectId } = require('mongodb-legacy')
const DocstoreApp = require('./helpers/DocstoreApp')
const DocstoreClient = require('./helpers/DocstoreClient')
describe('Applying updates to a doc', function () {
beforeEach(function (done) {
this.project_id = new ObjectId()
this.doc_id = new ObjectId()
this.originalLines = ['original', 'lines']
this.newLines = ['new', 'lines']
this.originalRanges = {
changes: [
{
id: new ObjectId().toString(),
op: { i: 'foo', p: 3 },
meta: {
user_id: new ObjectId().toString(),
ts: new Date().toString(),
},
},
],
}
this.newRanges = {
changes: [
{
id: new ObjectId().toString(),
op: { i: 'bar', p: 6 },
meta: {
user_id: new ObjectId().toString(),
ts: new Date().toString(),
},
},
],
}
this.version = 42
return DocstoreApp.ensureRunning(() => {
return DocstoreClient.createDoc(
this.project_id,
this.doc_id,
this.originalLines,
this.version,
this.originalRanges,
error => {
if (error != null) {
throw error
}
return done()
}
)
})
})
describe('when nothing has been updated', function () {
beforeEach(function (done) {
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.originalLines,
this.version,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.body = body
return done()
}
)
})
it('should return modified = false', function () {
return this.body.modified.should.equal(false)
})
return it('should not update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
doc.version.should.equal(this.version)
doc.ranges.should.deep.equal(this.originalRanges)
return done()
}
)
})
})
describe('when the lines have changed', function () {
beforeEach(function (done) {
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.newLines,
this.version,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.body = body
return done()
}
)
})
it('should return modified = true', function () {
return this.body.modified.should.equal(true)
})
it('should return the rev', function () {
return this.body.rev.should.equal(2)
})
return it('should update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.newLines)
doc.version.should.equal(this.version)
doc.ranges.should.deep.equal(this.originalRanges)
return done()
}
)
})
})
describe('when the version has changed', function () {
beforeEach(function (done) {
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.originalLines,
this.version + 1,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.body = body
return done()
}
)
})
it('should return modified = true', function () {
return this.body.modified.should.equal(true)
})
it('should return the rev', function () {
return this.body.rev.should.equal(1)
})
return it('should update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
doc.version.should.equal(this.version + 1)
doc.ranges.should.deep.equal(this.originalRanges)
return done()
}
)
})
})
describe('when the version was decremented', function () {
beforeEach(function (done) {
DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.newLines,
this.version - 1,
this.newRanges,
(error, res, body) => {
if (error) return done(error)
this.res = res
this.body = body
done()
}
)
})
it('should return 409', function () {
this.res.statusCode.should.equal(409)
})
it('should not update the doc in the API', function (done) {
DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
doc.version.should.equal(this.version)
doc.ranges.should.deep.equal(this.originalRanges)
done()
}
)
})
})
describe('when the ranges have changed', function () {
beforeEach(function (done) {
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.originalLines,
this.version,
this.newRanges,
(error, res, body) => {
if (error) return done(error)
this.body = body
return done()
}
)
})
it('should return modified = true', function () {
return this.body.modified.should.equal(true)
})
it('should return the rev', function () {
return this.body.rev.should.equal(2)
})
return it('should update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
doc.version.should.equal(this.version)
doc.ranges.should.deep.equal(this.newRanges)
return done()
}
)
})
})
describe('when the doc does not exist', function () {
beforeEach(function (done) {
this.missing_doc_id = new ObjectId()
return DocstoreClient.updateDoc(
this.project_id,
this.missing_doc_id,
this.originalLines,
0,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.res = res
this.body = body
return done()
}
)
})
it('should create the doc', function () {
return this.body.rev.should.equal(1)
})
return it('should be retreivable', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.missing_doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
doc.version.should.equal(0)
doc.ranges.should.deep.equal(this.originalRanges)
return done()
}
)
})
})
describe('when malformed doc lines are provided', function () {
describe('when the lines are not an array', function () {
beforeEach(function (done) {
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
{ foo: 'bar' },
this.version,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.res = res
this.body = body
return done()
}
)
})
it('should return 400', function () {
return this.res.statusCode.should.equal(400)
})
return it('should not update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
return done()
}
)
})
})
return describe('when the lines are not present', function () {
beforeEach(function (done) {
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
null,
this.version,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.res = res
this.body = body
return done()
}
)
})
it('should return 400', function () {
return this.res.statusCode.should.equal(400)
})
return it('should not update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
return done()
}
)
})
})
})
describe('when no version is provided', function () {
beforeEach(function (done) {
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.originalLines,
null,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.res = res
this.body = body
return done()
}
)
})
it('should return 400', function () {
return this.res.statusCode.should.equal(400)
})
return it('should not update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
doc.version.should.equal(this.version)
return done()
}
)
})
})
describe('when the content is large', function () {
beforeEach(function (done) {
const line = new Array(1025).join('x') // 1kb
this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1mb
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.largeLines,
this.version,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.body = body
return done()
}
)
})
it('should return modified = true', function () {
return this.body.modified.should.equal(true)
})
return it('should update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.largeLines)
return done()
}
)
})
})
describe('when there is a large json payload', function () {
beforeEach(function (done) {
const line = new Array(1025).join('x') // 1kb
this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1kb
this.originalRanges.padding = Array.apply(null, Array(2049)).map(
() => line
) // 2mb + 1kb
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.largeLines,
this.version,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.res = res
this.body = body
return done()
}
)
})
it('should return modified = true', function () {
return this.body.modified.should.equal(true)
})
return it('should update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.largeLines)
return done()
}
)
})
})
describe('when the document body is too large', function () {
beforeEach(function (done) {
const line = new Array(1025).join('x') // 1kb
this.largeLines = Array.apply(null, Array(2049)).map(() => line) // 2mb + 1kb
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.largeLines,
this.version,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.res = res
this.body = body
return done()
}
)
})
it('should return 413', function () {
return this.res.statusCode.should.equal(413)
})
it('should report body too large', function () {
return this.res.body.should.equal('document body too large')
})
return it('should not update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
return done()
}
)
})
})
return describe('when the json payload is too large', function () {
beforeEach(function (done) {
const line = new Array(1025).join('x') // 1kb
this.largeLines = Array.apply(null, Array(1024)).map(() => line) // 1kb
this.originalRanges.padding = Array.apply(null, Array(6144)).map(
() => line
) // 6mb
return DocstoreClient.updateDoc(
this.project_id,
this.doc_id,
this.largeLines,
this.version,
this.originalRanges,
(error, res, body) => {
if (error) return done(error)
this.res = res
this.body = body
return done()
}
)
})
return it('should not update the doc in the API', function (done) {
return DocstoreClient.getDoc(
this.project_id,
this.doc_id,
{},
(error, res, doc) => {
if (error) return done(error)
doc.lines.should.deep.equal(this.originalLines)
return done()
}
)
})
})
})

View File

@@ -0,0 +1,26 @@
const app = require('../../../../app')
const settings = require('@overleaf/settings')
module.exports = {
running: false,
initing: false,
callbacks: [],
ensureRunning(callback) {
if (this.running) {
return callback()
} else if (this.initing) {
return this.callbacks.push(callback)
}
this.initing = true
this.callbacks.push(callback)
app.listen(settings.internal.docstore.port, '127.0.0.1', error => {
if (error != null) {
throw error
}
this.running = true
for (callback of Array.from(this.callbacks)) {
callback()
}
})
},
}

View File

@@ -0,0 +1,195 @@
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 await new Promise((resolve, reject) => {
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 await streamToString(stream)
}
module.exports = DocstoreClient = {
createDoc(projectId, docId, lines, version, ranges, callback) {
return DocstoreClient.updateDoc(
projectId,
docId,
lines,
version,
ranges,
callback
)
},
getDoc(projectId, docId, qs, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
json: true,
qs,
},
callback
)
},
peekDoc(projectId, docId, qs, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/peek`,
json: true,
qs,
},
callback
)
},
isDocDeleted(projectId, docId, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}/deleted`,
json: true,
},
callback
)
},
getAllDocs(projectId, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc`,
json: true,
},
(req, res, body) => {
callback(req, res, body)
}
)
},
getAllDeletedDocs(projectId, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc-deleted`,
json: true,
},
(error, res, body) => {
if (error) return callback(error)
if (res.statusCode !== 200) {
return callback(new Error('unexpected statusCode'))
}
callback(null, body)
}
)
},
getAllRanges(projectId, callback) {
request.get(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/ranges`,
json: true,
},
callback
)
},
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,
ranges,
},
},
callback
)
},
deleteDoc(projectId, docId, callback) {
DocstoreClient.deleteDocWithDateAndName(
projectId,
docId,
new Date(),
'main.tex',
callback
)
},
deleteDocWithDate(projectId, docId, date, callback) {
DocstoreClient.deleteDocWithDateAndName(
projectId,
docId,
date,
'main.tex',
callback
)
},
deleteDocWithName(projectId, docId, name, callback) {
DocstoreClient.deleteDocWithDateAndName(
projectId,
docId,
new Date(),
name,
callback
)
},
deleteDocWithDateAndName(projectId, docId, deletedAt, name, callback) {
request.patch(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/doc/${docId}`,
json: { name, deleted: true, deletedAt },
},
callback
)
},
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
)
},
destroyAllDoc(projectId, callback) {
request.post(
{
url: `http://127.0.0.1:${settings.internal.docstore.port}/project/${projectId}/destroy`,
},
callback
)
},
getS3Doc(projectId, docId, callback) {
getStringFromPersistor(
Persistor,
settings.docstore.bucket,
`${projectId}/${docId}`
)
.then(data => {
callback(null, JSON.parse(data))
})
.catch(callback)
},
}