2020-05-06 10:12:36 +00:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 10:12:17 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2020-05-06 10:12:47 +00:00
|
|
|
let MockWebApi
|
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const app = express()
|
|
|
|
const MAX_REQUEST_SIZE = 2 * (2 * 1024 * 1024 + 64 * 1024)
|
2020-05-06 10:12:17 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
module.exports = MockWebApi = {
|
|
|
|
docs: {},
|
2020-05-06 10:12:17 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
clearDocs() {
|
|
|
|
return (this.docs = {})
|
|
|
|
},
|
2020-05-06 10:12:17 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
insertDoc(project_id, doc_id, doc) {
|
|
|
|
if (doc.version == null) {
|
|
|
|
doc.version = 0
|
|
|
|
}
|
|
|
|
if (doc.lines == null) {
|
|
|
|
doc.lines = []
|
|
|
|
}
|
|
|
|
doc.pathname = '/a/b/c.tex'
|
|
|
|
return (this.docs[`${project_id}:${doc_id}`] = doc)
|
|
|
|
},
|
2020-05-06 10:12:17 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
setDocument(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
lastUpdatedAt,
|
|
|
|
lastUpdatedBy,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
if (callback == null) {
|
2021-10-27 09:49:18 +00:00
|
|
|
callback = function () {}
|
2020-05-06 10:12:47 +00:00
|
|
|
}
|
|
|
|
const doc =
|
|
|
|
this.docs[`${project_id}:${doc_id}`] ||
|
|
|
|
(this.docs[`${project_id}:${doc_id}`] = {})
|
|
|
|
doc.lines = lines
|
|
|
|
doc.version = version
|
|
|
|
doc.ranges = ranges
|
|
|
|
doc.pathname = '/a/b/c.tex'
|
|
|
|
doc.lastUpdatedAt = lastUpdatedAt
|
|
|
|
doc.lastUpdatedBy = lastUpdatedBy
|
|
|
|
return callback(null)
|
|
|
|
},
|
2020-05-06 10:12:17 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
getDocument(project_id, doc_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 09:49:18 +00:00
|
|
|
callback = function () {}
|
2020-05-06 10:12:47 +00:00
|
|
|
}
|
|
|
|
return callback(null, this.docs[`${project_id}:${doc_id}`])
|
|
|
|
},
|
2020-05-06 10:12:17 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
run() {
|
|
|
|
app.get('/project/:project_id/doc/:doc_id', (req, res, next) => {
|
|
|
|
return this.getDocument(
|
|
|
|
req.params.project_id,
|
|
|
|
req.params.doc_id,
|
|
|
|
(error, doc) => {
|
|
|
|
if (error != null) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
} else if (doc != null) {
|
|
|
|
return res.send(JSON.stringify(doc))
|
|
|
|
} else {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-05-06 10:12:17 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
app.post(
|
|
|
|
'/project/:project_id/doc/:doc_id',
|
|
|
|
bodyParser.json({ limit: MAX_REQUEST_SIZE }),
|
|
|
|
(req, res, next) => {
|
|
|
|
return MockWebApi.setDocument(
|
|
|
|
req.params.project_id,
|
|
|
|
req.params.doc_id,
|
|
|
|
req.body.lines,
|
|
|
|
req.body.version,
|
|
|
|
req.body.ranges,
|
|
|
|
req.body.lastUpdatedAt,
|
|
|
|
req.body.lastUpdatedBy,
|
2021-07-13 11:04:42 +00:00
|
|
|
error => {
|
2020-05-06 10:12:47 +00:00
|
|
|
if (error != null) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
} else {
|
2022-09-21 12:01:32 +00:00
|
|
|
return res.json({ rev: '123' })
|
2020-05-06 10:12:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2020-05-06 10:12:17 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
return app
|
2021-07-13 11:04:42 +00:00
|
|
|
.listen(3000, error => {
|
2020-05-06 10:12:47 +00:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
})
|
2021-07-13 11:04:42 +00:00
|
|
|
.on('error', error => {
|
2020-05-06 10:12:47 +00:00
|
|
|
console.error('error starting MockWebApi:', error.message)
|
|
|
|
return process.exit(1)
|
|
|
|
})
|
2021-07-13 11:04:42 +00:00
|
|
|
},
|
2020-05-06 10:12:47 +00:00
|
|
|
}
|
2014-02-12 10:40:42 +00:00
|
|
|
|
2020-05-06 10:12:47 +00:00
|
|
|
MockWebApi.run()
|